Batch clients into sets of 100 for reconfig command
[KiwiIRC.git] / server / irc / channel.js
index 3a8993f0d2c3671de615bbd58314e740d52de061..fcdeec28a4db9bc19e55b27bb30d57d74fc514d2 100644 (file)
@@ -1,76 +1,74 @@
-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);
-    
     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
+    };
+    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
-};
-
 
 function onJoin(event) {
-    this.irc_connection.sendIrcCommand('join', {
+    this.irc_connection.clientEvent('join', {
         channel: this.name,
         nick: event.nick,
         ident: event.ident,
-        hostname: event.hostname,
+        hostname: event.hostname
     });
 
     // 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);
+        this.irc_connection.write('NAMES ' + this.name);
     }
-};
+}
 
 
 function onPart(event) {
-    this.irc_connection.sendIrcCommand('part', {
+    this.irc_connection.clientEvent('part', {
         nick: event.nick,
         ident: event.ident,
         hostname: event.hostname,
         channel: this.name,
         message: event.message
     });
-
-    this.dispose();
-};
+}
 
 
 function onKick(event) {
-    this.irc_connection.sendIrcCommand('kick', {
+    this.irc_connection.clientEvent('kick', {
         kicked: event.kicked,  // Nick of the kicked
         nick: event.nick, // Nick of the kicker
         ident: event.ident,
@@ -78,47 +76,44 @@ function onKick(event) {
         channel: this.name,
         message: event.message
     });
-
-    this.dispose();
-};
+}
 
 
 function onQuit(event) {
-    this.irc_connection.sendIrcCommand('quit', {
+    this.irc_connection.clientEvent('quit', {
         nick: event.nick,
         ident: event.ident,
         hostname: event.hostname,
         message: event.message
     });
-
-    this.dispose();
-};
+}
 
 
 function onMsg(event) {
-    this.irc_connection.sendIrcCommand('msg', {
+    this.irc_connection.clientEvent('msg', {
         nick: event.nick,
         ident: event.ident,
         hostname: event.hostname,
         channel: this.name,
-        msg: event.message
+        msg: event.msg
     });
-};
+}
 
 
 function onNotice(event) {
-    this.irc_connection.sendIrcCommand('msg', {
+    this.irc_connection.clientEvent('notice', {
+        from_server: event.from_server,
         nick: event.nick,
         ident: event.ident,
         hostname: event.hostname,
-        channel: this.name,
-        msg: event.trailing
+        target: event.target,
+        msg: event.msg
     });
-};
+}
 
 
 function onCtcpRequest(event) {
-    this.irc_connection.sendIrcCommand('ctcp_request', {
+    this.irc_connection.clientEvent('ctcp_request', {
         nick: event.nick,
         ident: event.ident,
         hostname: event.hostname,
@@ -126,11 +121,11 @@ function onCtcpRequest(event) {
         type: event.type,
         msg: event.msg
     });
-};
+}
 
 
 function onCtcpResponse(event) {
-    this.irc_connection.sendIrcCommand('ctcp_response', {
+    this.irc_connection.clientEvent('ctcp_response', {
         nick: event.nick,
         ident: event.ident,
         hostname: event.hostname,
@@ -138,38 +133,53 @@ function onCtcpResponse(event) {
         type: event.type,
         msg: event.msg
     });
-};
+}
 
 
 // TODO: Split event.users into batches of 50
 function onNicklist(event) {
-    this.irc_connection.sendIrcCommand('userlist', {
+    this.irc_connection.clientEvent('userlist', {
         users: event.users,
         channel: this.name
     });
-};
+    // TODO: uncomment when using an IrcUser per nick
+    //updateUsersList.call(this, event.users);
+}
 
 
 function onNicklistEnd(event) {
-    this.irc_connection.sendIrcCommand('userlist_end', {
+    this.irc_connection.clientEvent('userlist_end', {
         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.sendIrcCommand('topic', {
+    this.irc_connection.clientEvent('topic', {
         nick: event.nick,
         channel: this.name,
         topic: event.topic
     });
-};
+}
 
 
 function onBanList(event) {
     this.ban_list_buffer.push(event);
-};
+}
 
 function onBanListEnd(event) {
     var that = this;
@@ -177,14 +187,14 @@ function onBanListEnd(event) {
         that.irc_connection.clientEvent('banlist', ban);
     });
     this.ban_list_buffer = [];
-};
+}
 
 function onTopic(event) {
     this.irc_connection.clientEvent('topic', {
         channel: event.channel,
         topic: event.topic
     });
-};
+}
 
 function onTopicSetBy(event) {
     this.irc_connection.clientEvent('topicsetby', {
@@ -192,30 +202,12 @@ function onTopicSetBy(event) {
         channel: event.channel,
         when: event.when
     });
-};
-
-
-/*
-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
+}
 
-Private message:
-    user:privmsg
-    user:*
-*/
\ No newline at end of file
+function onMode(event) {
+    this.irc_connection.clientEvent('mode', {
+        target: event.target,
+        nick: event.nick,
+        modes: event.modes
+    });
+}