Channel case-sensitivity issues. Main cause of "Cannot call method 'clientEvent'...
[KiwiIRC.git] / server / irc / channel.js
index 57d197c7d91642ccf9e6a5da47b267d3bf912446..2226828a0372d1a49b44b3946af723867929330c 100644 (file)
-var util    = require('util'),
-    Binder  = require('./binder.js');
+var util        = require('util'),
+    EventBinder = require('./eventbinder.js'),
+    IrcUser     = require('./user.js');
 
-function IrcChannel(irc_connection, name) {
+var IrcChannel = function(irc_connection, name) {
     this.irc_connection = irc_connection;
-    this.name = name;
 
-    this.scope = 'channel:' + name;
-    Binder.call(this);
-    
+    // Lowercase the channel name so we don't run into case-sensitive issues
+    this.name = name.toLowerCase();
+
     this.members = [];
     this.ban_list_buffer = [];
-}
 
-util.inherits(IrcChannel, Binder);
+    // Listen for events on the IRC connection
+    this.irc_events = {
+        join:           onJoin,
+        part:           onPart,
+        kick:           onKick,
+        quit:           onQuit,
+        privmsg:        onMsg,
+        notice:         onNotice,
+        ctcp_request:   onCtcpRequest,
+        ctcp_response:  onCtcpResponse,
+        topic:          onTopic,
+        userlist:       onNicklist,
+        userlist_end:   onNicklistEnd,
+        banlist:        onBanList,
+        banlist_end:    onBanListEnd,
+        topicsetby:     onTopicSetBy,
+        mode:           onMode,
+        info:           onChannelInfo
+    };
+    EventBinder.bindIrcEvents('channel ' + this.name, this.irc_events, this, irc_connection);
+};
+
 
 module.exports = IrcChannel;
 
 
 IrcChannel.prototype.dispose = function (){
-    this.unbindEvents();
+    EventBinder.unbindIrcEvents('channel ' + this.name, this.irc_events, this.irc_connection);
     this.irc_connection = undefined;
 };
 
 
-IrcChannel.prototype.irc_events = {
-    join:           onJoin,
-    part:           onPart,
-    kick:           onKick,
-    quit:           onQuit,
-    privmsg:        onMsg,
-    notice:         onNotice,
-    ctcp_request:   onCtcpRequest,
-    ctcp_response:  onCtcpResponse,
-    topic:          onTopic,
-    nicklist:       onNicklist,
-    nicklistEnd:    onNicklistEnd,
-    banlist:        onBanList,
-    banlist_end:    onBanListEnd,
-    topicsetby:     onTopicSetby,
-    mode:           onMode
-};
-
 
 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 ' + channel);
-    }
-};
+    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;
 
-    this.dispose();
-};
+    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;
 
-    this.dispose();
-};
+    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;
 
-    this.dispose();
-};
+    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.message
+    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('msg', {
-        nick: event.nick,
-        ident: event.ident,
-        hostname: event.hostname,
-        channel: this.name,
-        msg: event.trailing
+    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) {
@@ -125,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) {
@@ -137,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
@@ -148,7 +202,9 @@ function onNicklist(event) {
         users: event.users,
         channel: this.name
     });
-};
+    // TODO: uncomment when using an IrcUser per nick
+    //updateUsersList.call(this, event.users);
+}
 
 
 function onNicklistEnd(event) {
@@ -156,36 +212,61 @@ function onNicklistEnd(event) {
         users: event.users,
         channel: this.name
     });
-};
+    // TODO: uncomment when using an IrcUser per nick
+    //updateUsersList.call(this, event.users);
+}
+
+function updateUsersList(users) {
+    var that = this;
+    if (users) {
+        users.forEach(function (user) {
+            if (!that.irc_connection.irc_users[user.nick]) {
+                that.irc_connection.irc_users[user.nick] = new IrcUser(that.irc_connection, user.nick);
+            }
+        });
+    }
+}
 
 
 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', {
@@ -193,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
+        });
+    });
+}