Channel case-sensitivity issues. Main cause of "Cannot call method 'clientEvent'...
[KiwiIRC.git] / server / irc / channel.js
index 819e06c5a532546e606acd016952563641a5a52b..2226828a0372d1a49b44b3946af723867929330c 100644 (file)
@@ -4,7 +4,9 @@ var util        = require('util'),
 
 var IrcChannel = function(irc_connection, name) {
     this.irc_connection = irc_connection;
-    this.name = name;
+
+    // Lowercase the channel name so we don't run into case-sensitive issues
+    this.name = name.toLowerCase();
 
     this.members = [];
     this.ban_list_buffer = [];
@@ -25,90 +27,147 @@ var IrcChannel = function(irc_connection, name) {
         banlist:        onBanList,
         banlist_end:    onBanListEnd,
         topicsetby:     onTopicSetBy,
-        mode:           onMode
+        mode:           onMode,
+        info:           onChannelInfo
     };
-    EventBinder.bindIrcEvents('channel:' + this.name, this.irc_events, this, irc_connection);
-}
+    EventBinder.bindIrcEvents('channel ' + this.name, this.irc_events, this, irc_connection);
+};
 
 
 module.exports = IrcChannel;
 
 
 IrcChannel.prototype.dispose = function (){
-    EventBinder.unbindIrcEvents('channel:' + this.name, this.irc_events, this.irc_connection);
+    EventBinder.unbindIrcEvents('channel ' + this.name, this.irc_events, this.irc_connection);
     this.irc_connection = undefined;
 };
 
 
 
 function onJoin(event) {
-    this.irc_connection.clientEvent('join', {
-        channel: this.name,
-        nick: event.nick,
-        ident: event.ident,
-        hostname: event.hostname
-    });
+    var that = this;
 
-    // If we've just joined this channel then request get a nick list
-    if (event.nick === this.irc_connection.nick) {
-        this.irc_connection.write('NAMES ' + this.name);
-    }
-};
+    global.modules.emit('irc channel join', {
+        channel: this,
+        connection: this.irc_connection,
+        irc_event: event
+    })
+    .done(function() {
+        that.irc_connection.clientEvent('join', {
+            channel: that.name,
+            nick: event.nick,
+            ident: event.ident,
+            hostname: event.hostname,
+            time: event.time
+        });
+    });
+}
 
 
 function onPart(event) {
-    this.irc_connection.clientEvent('part', {
-        nick: event.nick,
-        ident: event.ident,
-        hostname: event.hostname,
-        channel: this.name,
-        message: event.message
+    var that = this;
+
+    global.modules.emit('irc channel part', {
+        channel: this,
+        connection: this.irc_connection,
+        irc_event: event
+    })
+    .done(function() {
+        that.irc_connection.clientEvent('part', {
+            nick: event.nick,
+            ident: event.ident,
+            hostname: event.hostname,
+            channel: that.name,
+            message: event.message,
+            time: event.time
+        });
     });
-};
+}
 
 
 function onKick(event) {
-    this.irc_connection.clientEvent('kick', {
-        kicked: event.kicked,  // Nick of the kicked
-        nick: event.nick, // Nick of the kicker
-        ident: event.ident,
-        hostname: event.hostname,
-        channel: this.name,
-        message: event.message
+    var that = this;
+
+    global.modules.emit('irc channel kick', {
+        channel: this,
+        connection: this.irc_connection,
+        irc_event: event
+    })
+    .done(function() {
+        that.irc_connection.clientEvent('kick', {
+            kicked: event.kicked,  // Nick of the kicked
+            nick: event.nick, // Nick of the kicker
+            ident: event.ident,
+            hostname: event.hostname,
+            channel: that.name,
+            message: event.message,
+            time: event.time
+        });
     });
-};
+}
 
 
 function onQuit(event) {
-    this.irc_connection.clientEvent('quit', {
-        nick: event.nick,
-        ident: event.ident,
-        hostname: event.hostname,
-        message: event.message
+    var that = this;
+
+    global.modules.emit('irc channel quit', {
+        channel: this,
+        connection: this.irc_connection,
+        irc_event: event
+    })
+    .done(function() {
+        that.irc_connection.clientEvent('quit', {
+            nick: event.nick,
+            ident: event.ident,
+            hostname: event.hostname,
+            message: event.message,
+            time: event.time
+        });
     });
-};
+}
 
 
 function onMsg(event) {
-    this.irc_connection.clientEvent('msg', {
-        nick: event.nick,
-        ident: event.ident,
-        hostname: event.hostname,
-        channel: this.name,
-        msg: event.msg
+    var that = this;
+
+    global.modules.emit('irc message', {
+        channel: this,
+        connection: this.irc_connection,
+        irc_event: event
+    })
+    .done(function() {
+        that.irc_connection.clientEvent('msg', {
+            nick: event.nick,
+            ident: event.ident,
+            hostname: event.hostname,
+            channel: that.name,
+            msg: event.msg,
+            time: event.time
+        });
     });
-};
+}
 
 
 function onNotice(event) {
-    this.irc_connection.clientEvent('notice', {
-        nick: event.nick,
-        ident: event.ident,
-        hostname: event.hostname,
-        target: event.target,
-        msg: event.msg
+    var that = this;
+
+    global.modules.emit('irc channel notice', {
+        channel: this,
+        connection: this.irc_connection,
+        irc_event: event
+    })
+    .done(function() {
+        that.irc_connection.clientEvent('notice', {
+            from_server: event.from_server,
+            nick: event.nick,
+            ident: event.ident,
+            hostname: event.hostname,
+            target: event.target,
+            msg: event.msg,
+            time: event.time
+        });
     });
-};
+}
 
 
 function onCtcpRequest(event) {
@@ -118,9 +177,10 @@ function onCtcpRequest(event) {
         hostname: event.hostname,
         target: event.target,
         type: event.type,
-        msg: event.msg
+        msg: event.msg,
+        time: event.time
     });
-};
+}
 
 
 function onCtcpResponse(event) {
@@ -130,9 +190,10 @@ function onCtcpResponse(event) {
         hostname: event.hostname,
         target: event.target,
         type: event.type,
-        msg: event.msg
+        msg: event.msg,
+        time: event.time
     });
-};
+}
 
 
 // TODO: Split event.users into batches of 50
@@ -143,7 +204,7 @@ function onNicklist(event) {
     });
     // TODO: uncomment when using an IrcUser per nick
     //updateUsersList.call(this, event.users);
-};
+}
 
 
 function onNicklistEnd(event) {
@@ -153,7 +214,7 @@ function onNicklistEnd(event) {
     });
     // TODO: uncomment when using an IrcUser per nick
     //updateUsersList.call(this, event.users);
-};
+}
 
 function updateUsersList(users) {
     var that = this;
@@ -168,32 +229,44 @@ function updateUsersList(users) {
 
 
 function onTopic(event) {
-    this.irc_connection.clientEvent('topic', {
-        nick: event.nick,
-        channel: this.name,
-        topic: event.topic
+    var that = this;
+
+    global.modules.emit('irc channel topic', {
+        channel: this,
+        connection: this.irc_connection,
+        irc_event: event
+    })
+    .done(function() {
+        that.irc_connection.clientEvent('topic', {
+            nick: event.nick,
+            channel: that.name,
+            topic: event.topic,
+            time: event.time
+        });
     });
-};
+}
+
+
+function onChannelInfo(event) {
+    // Channel info event may contain 1 of several types of info,
+    // including creation time, modes. So just pipe the event
+    // right through to the client
+    this.irc_connection.clientEvent('channel_info', event);
+}
 
 
 function onBanList(event) {
     this.ban_list_buffer.push(event);
-};
+}
 
 function onBanListEnd(event) {
-    var that = this;
-    this.ban_list_buffer.forEach(function (ban) {
-        that.irc_connection.clientEvent('banlist', ban);
+    this.irc_connection.clientEvent('banlist', {
+        channel: this.name,
+        bans: this.ban_list_buffer
     });
-    this.ban_list_buffer = [];
-};
 
-function onTopic(event) {
-    this.irc_connection.clientEvent('topic', {
-        channel: event.channel,
-        topic: event.topic
-    });
-};
+    this.ban_list_buffer = [];
+}
 
 function onTopicSetBy(event) {
     this.irc_connection.clientEvent('topicsetby', {
@@ -201,38 +274,22 @@ function onTopicSetBy(event) {
         channel: event.channel,
         when: event.when
     });
-};
+}
 
 function onMode(event) {
-    this.irc_connection.clientEvent('mode', {
-        target: event.target,
-        nick: event.nick,
-        modes: event.modes
-    });
-};
-
-
-/*
-server:event
-server:*
-channel:#channel:event
-channel:*:event
-user:event
-user:*
-
-Server disconnected:
-    server:disconnect
-    server:*
-
-Joining channel #kiwiirc:
-    channel:#kiwiirc:join
-    channel:*:join
-
-Channel message:
-    channel:#kiwiirc:privmsg
-    channel:*:privmsg
+    var that = this;
 
-Private message:
-    user:privmsg
-    user:*
-*/
\ No newline at end of file
+    global.modules.emit('irc channel mode', {
+        channel: this,
+        connection: this.irc_connection,
+        irc_event: event
+    })
+    .done(function() {
+        that.irc_connection.clientEvent('mode', {
+            target: event.target,
+            nick: event.nick,
+            modes: event.modes,
+            time: event.time
+        });
+    });
+}