Skip to content
Snippets Groups Projects
Commit b0769346 authored by Atanamo's avatar Atanamo
Browse files

Post message to IRC - yey

parent e5daadf3
No related branches found
No related tags found
No related merge requests found
Loading
Loading
@@ -9,7 +9,10 @@ Config = require './config'
# Bot class
class SchizoBot
 
constructor: (@gameData) ->
gameData: null
botChannel: null
constructor: (@botChannel, @gameData) ->
console.log 'Constructing new Bot...'
 
@nickName = Config.BOT_NICK_PATTERN.replace(/<id>/i, @gameData.id)
Loading
Loading
@@ -30,7 +33,7 @@ class SchizoBot
floodProtection: true # Protect the bot from beeing kicket, if users are flooding
floodProtectionDelay: 10 # Delay messages with 10ms to avoid flooding
 
@client.addListener 'message', @handleMessage
@client.addListener 'message', @_handleIrcMessage
 
 
start: (channelList) ->
Loading
Loading
@@ -41,23 +44,46 @@ class SchizoBot
callback: ->
console.log 'Connected succesfully!'
 
getID: ->
return @gameData.id
#
# IRC event handlers
#
 
handleMessage: (from, to, message, isSecondTry=false) =>
_handleIrcMessage: (from, to, message, isSecondTry=false) =>
unless isSecondTry ## TODO: why is always isSecondTry = true ?
console.log 'FIRST TRY'
 
if message.indexOf(@nickName + ':') > -1 ## TODO: why is the command not recognized?
console.log 'HANDLING'
return @handleMessageToBot(from, to, message)
return @_handleIrcMessageToBot(from, to, message)
 
console.log 'DONT HANDLING'
console.log 'DONT HANDLING', from, to
#broudcast...
 
handleMessageToBot: (from, to, message) =>
_handleIrcMessageToBot: (from, to, message) =>
if message.indexOf('galaxy?') > -1
@client.say(to, 'Galaxy: ' + @realName)
else
#@handleMessage(from, to, message, true)
#@handleIrcMessage(from, to, message, true)
#
# BotChannel handling
#
handleWebClientMessage: (senderData, rawMessage) ->
clientNick = senderData.name or 'Anonymous'
messageText = "<#{clientNick}>: #{rawMessage}"
@client.say(Config.IRC_CHANNEL_GLOBAL, messageText)
 
 
# Export class
Loading
Loading
Loading
Loading
@@ -7,9 +7,11 @@ Channel = require './channel'
## Class definition
class BotChannel extends Channel
@_instance: null
_botList: null
 
#constructor: ->
constructor: ->
super
@_botList = {}
 
@getInstance: ->
unless @_instance?
Loading
Loading
@@ -18,9 +20,40 @@ class BotChannel extends Channel
return @_instance
 
 
# @override
addClient: (clientSocket, isRejoin=false) ->
super(clientSocket, true) # true, because: dont do that: db.addClientToChannel(clientSocket, @name)
 
addBot: (bot) ->
botID = bot.getID()
@_botList[botID] = bot
#
# Sending routines
#
# @override
_handleClientMessage: (clientSocket, messageText) =>
botID = clientSocket.identData.game_id or -1
targetBot = @_botList[botID]
return unless targetBot?
# Send to socket channel
super
# Send to IRC channel
targetBot.handleWebClientMessage(clientSocket.identData, messageText)
#
# Bot handling
#
handleBotMessage: (sender, msg) ->
 
 
## Export class
Loading
Loading
 
## Include app modules
Config = require './config'
#Config = require './config'
 
 
## Class definition
class Channel
@_instances: {}
 
_isPublic = false
_title = 'New Channel'
_eventNameMsg = 'message#unnamed'
_eventNameLeave = 'leave#unnamed'
_isPublic: false
_title: 'New Channel'
_eventNameMsg: 'message#unnamed'
_eventNameLeave: 'leave#unnamed'
 
constructor: (@name) ->
log.info 'Instance of new channel ' + @name
Loading
Loading
@@ -26,6 +26,11 @@ class Channel
@_instances[name] = new Channel(name)
return @_instances[name]
 
#
# Client management
#
addClient: (clientSocket, isRejoin=false) ->
clientSocket.emit 'joined', @name # Notice client for channel join
clientSocket.join(@name) # Join client to room of channel
Loading
Loading
@@ -46,7 +51,6 @@ class Channel
unless isRejoin
db.addClientToChannel(clientSocket, @name)
 
removeClient: (clientSocket, isDisconnect=false) ->
# Unregister events for this channel
clientSocket.removeAllListeners @_eventNameMsg
Loading
Loading
@@ -67,6 +71,10 @@ class Channel
db.removeClientFromChannel(clientSocket, @name)
 
 
#
# Sending routines
#
_sendClientList: (clientSocket) ->
clientSocketList = io.sockets.clients(@name)
#clientSocket.emit 'channel_clients', clientList
Loading
Loading
@@ -83,20 +91,25 @@ class Channel
io.sockets.in(@name).emit(eventName, data)
 
 
#
# Client event handlers
#
# @protected
_handleClientMessage: (clientSocket, messageText) =>
log.info 'Client message:', messageText
messageText = messageText?.trim()
 
return unless messageText != ''
return if messageText is ''
 
@_sendToRoom 'message',
channel: @name
sender: clientSocket.identData.id
sender: clientSocket.identData
msg: messageText
 
 
_handleClientLeave: (clientSocket, isDisconnect=false) =>
log.warn 'TODO: Client left'
@removeClient(clientSocket, isDisconnect)
 
 
## Export class
Loading
Loading
Loading
Loading
@@ -20,8 +20,10 @@ class Database
# db.select("Select channel from channels, client_channels where client = '#{clientIdent}'")
 
tempdata = [
#id: 123
name: 'galaxy_test'
,
#id: 124
name: 'galaxy_test_group01'
]
 
Loading
Loading
Loading
Loading
@@ -37,10 +37,10 @@ GLOBAL.log = log
## Main class
class Gateway
constructor: ->
@bindServerEvents()
@bindSocketGlobalEvents()
@_bindServerEvents()
@_bindSocketGlobalEvents()
 
bindServerEvents: ->
_bindServerEvents: ->
## Register http server events
app.get '/', (request, response) ->
response.sendfile "./index.html"
Loading
Loading
@@ -55,28 +55,26 @@ class Gateway
server.on 'close', ->
log.info 'Server shut down'
 
bindSocketGlobalEvents: ->
_bindSocketGlobalEvents: ->
## Register common websocket events
io.sockets.on 'connection', @handleClientConnect
io.sockets.on 'connection', @_handleClientConnect
 
bindSocketClientEvents: (clientSocket) ->
_bindSocketClientEvents: (clientSocket) ->
## Register client socket events
#clientSocket.on 'join', (data) => @handleClientJoin(clientSocket, data)
clientSocket.on 'disconnect', @handleClientDisconnect
clientSocket.on 'disconnect', @_handleClientDisconnect
 
 
handleClientDisconnect: (clientSocket) =>
log.info 'Client disconnected...'
handleClientConnect: (clientSocket) =>
_handleClientConnect: (clientSocket) =>
log.info 'Client connected...'
 
# Set client identification data
# TODO
clientSocket.identData =
id: 42
name: 'TempName'
title: 'Temp Title'
game_id: 123
 
# Let client join default channel
botChannel = BotChannel.getInstance()
Loading
Loading
@@ -90,12 +88,16 @@ class Gateway
channel.addClient(clientSocket, true)
 
# Bind socket events to new client
@bindSocketClientEvents(clientSocket)
@_bindSocketClientEvents(clientSocket)
 
# Emit initial events for new client
clientSocket.emit 'welcome', 'hello out there!'
 
 
_handleClientDisconnect: (clientSocket) =>
log.info 'Client disconnected...'
start: ->
## Start the chat gateway ##
 
Loading
Loading
@@ -107,13 +109,23 @@ class Gateway
log.info 'Start listening...'
server.listen(8080)
 
###
bot = new Bot
# Create and connect the bots
@_setupBots()
_setupBots: ->
botChannel = BotChannel.getInstance()
# TODO: Multiple bots, each for one galaxy. Singleton-Concept?
bot = new Bot botChannel,
id: 123
name: 'Eine Testgalaxie'
 
bot.start()
###
# Add bots to botChannel
botChannel.addBot(bot)
 
 
 
Loading
Loading
Loading
Loading
@@ -90,9 +90,10 @@ class this.ChatController
sender = 'Server'
@_appendMessageToTab(tabPage, {sender, msg})
 
handleChannelMessage: ({channel, sender, msg}) ->
handleChannelMessage: (data) ->
channel = data.channel
tabPage = @_getChannelTabPage(channel)
@_appendMessageToTab(tabPage, {sender, msg})
@_appendMessageToTab(tabPage, data)
 
handleChannelJoined: (channel) =>
tabID = @_getChannelTabID(channel)
Loading
Loading
@@ -132,8 +133,8 @@ class this.ChatController
_getTabPage: (tabID) ->
return $('#' + tabID)
 
_appendMessageToTab: (tabPage, {sender, msg}) ->
if sender?.toString() == @instanceData.id
_appendMessageToTab: (tabPage, {sender, msg, isOwn}) ->
if isOwn
dataValue = 'own'
@ui.chatInput.val('')
else
Loading
Loading
Loading
Loading
@@ -33,6 +33,8 @@ class this.SocketClient
@chatController.handleServerMessage(text)
 
_handleMessageReceive: (data) =>
data.isOwn = (String(data.sender?.id) == String(@instanceData.id))
data.sender = data.sender?.name or data.sender?.id # Extract nick name from sender data
@chatController.handleChannelMessage(data)
 
_handleChannelJoined: (channel) =>
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment