making it librejs compliant
[KiwiIRC.git] / server / irc / server.js
index b9c40cc7f026e15ef23dd79bfc38d5cdca92a026..42e32dff0b1e71886c74bb0419c125e384342725 100755 (executable)
@@ -1,72 +1,99 @@
 var util    = require('util'),
-    Binder  = require('./binder.js');
+    EventBinder  = require('./eventbinder.js'),
+    _ = require('lodash'),
+    Stats = require('../stats.js');
 
-var IrcServer = function (irc_connection, host, port) {
+var IrcServer = function (irc_connection) {
     this.irc_connection = irc_connection;
-    this.host = host;
-    this.port = port;
-    
-    this.scope = 'server:' + host;
-    
-    Binder.call(this);
-    
+
     this.list_buffer = [];
     this.motd_buffer = '';
+
+    // Date when registeration with the IRCd had completed
+    this.registered = false;
+
+    this.irc_events = {
+        connect:                onConnect,
+        options:                onOptions,
+        list_start:             onListStart,
+        list_channel:           onListChannel,
+        list_end:               onListEnd,
+        motd_start:             onMotdStart,
+        motd:                   onMotd,
+        motd_end:               onMotdEnd,
+        error:                  onError,
+        password_mismatch:      onPasswordMismatch,
+        channel_redirect:       onChannelRedirect,
+        no_such_nick:           onNoSuchNick,
+        cannot_send_to_channel: onCannotSendToChan,
+        too_many_channels:      onTooManyChannels,
+        user_not_in_channel:    onUserNotInChannel,
+        not_on_channel:         onNotOnChannel,
+        channel_is_full:        onChannelIsFull,
+        invite_only_channel:    onInviteOnlyChannel,
+        user_on_channel:        onUserAlreadyInChannel,
+        banned_from_channel:    onBannedFromChannel,
+        bad_channel_key:        onBadChannelKey,
+        chanop_privs_needed:    onChanopPrivsNeeded,
+        nickname_in_use:        onNicknameInUse,
+        erroneus_nickname:      onErroneusNickname,
+        unknown_command:        onUnknownCommand
+    };
+    EventBinder.bindIrcEvents('server *', this.irc_events, this, this.irc_connection);
+
 };
 
-util.inherits(IrcServer, Binder);
 
 module.exports = IrcServer;
 
-IrcServer.prototype.irc_events = {
-    connect:                onConnect,
-    options:                onOptions,
-    list_start:             onListStart,
-    list_channel:           onListChannel,
-    list_end:               onListEnd,
-    motd_start:             onMotdStart,
-    motd:                   onMotd,
-    motd_end:               onMotdEnd,
-    error:                  onError,
-    channel_redirect:       onChannelRedirect,
-    no_such_nick:           onNoSuchNick,
-    cannot_send_to_channel: onChannotSendToChan,
-    too_many_channels:      onTooManyChannels,
-    user_not_in_channel:    onUserNotInChannel,
-    not_on_channel:         onNotOnChannel,
-    channel_is_full:        onChannelisFull,
-    invite_only_channel:    onInviteOnlyChannel,
-    banned_from_channel:    onBannedFromChannel,
-    bad_channel_key:        onBadChannelKey,
-    chanop_privs_needed:    onChanopPrivsNeeded,
-    nickname_in_use:        onNicknameInUse
+
+IrcServer.prototype.dispose = function (){
+    EventBinder.unbindIrcEvents('server *', this.irc_events, this.irc_connection);
+    this.irc_connection = undefined;
+};
+
+
+IrcServer.prototype.reset = function() {
+    this.registered = false;
+    this.list_buffer = [];
+    this.motd_buffer = '';
 };
 
+
+
 function onConnect(event) {
+    Stats.incr('irc.connection.registered');
+    this.registered = new Date();
+
     this.irc_connection.clientEvent('connect', {
         nick: event.nick
     });
-};
+}
 
 function onOptions(event) {
     this.irc_connection.clientEvent('options', {
         options: event.options,
         cap: event.cap
     });
-};
+}
 
 function onListStart(event) {
     this.irc_connection.clientEvent('list_start', {});
-};
+    this.list_buffer = [];
+    this.busy_listing = true;
+}
 
 function onListChannel(event) {
     var buf;
+    if (!this.busy_listing) {
+      onListStart.call(this);
+    }
     this.list_buffer.push({
         channel: event.channel,
         num_users: event.num_users,
         topic: event.topic
     });
-    
+
     if (this.list_buffer.length > 200) {
         buf = _.sortBy(this.list_buffer, function (channel) {
             // sortBy sorts in ascending order, we want to sort by descending, hence using 0 - num_users.
@@ -76,51 +103,58 @@ function onListChannel(event) {
             chans: buf
         });
         this.list_buffer = [];
-    };
-};
+    }
+}
 
 function onListEnd(event) {
-    if (this.list_buffer.length > 200) {
-        buf = _.sortBy(this.list_buffer, function (channel) {
-            // sortBy sorts in ascending order, we want to sort by descending, hence using 0 - num_users.
-            return 0 - channel.num_users;
-        });
-        this.irc_connection.clientEvent('list_channel', {
-            chans: buf
-        });
-        this.list_buffer = [];
-    };
-    
+    var buf;
+
+    buf = _.sortBy(this.list_buffer, function (channel) {
+        // sortBy sorts in ascending order, we want to sort by descending, hence using 0 - num_users.
+        return 0 - channel.num_users;
+    });
+    this.irc_connection.clientEvent('list_channel', {
+        chans: buf
+    });
+    this.list_buffer = [];
+    this.busy_listing = false;
+
     this.irc_connection.clientEvent('list_end', {});
-};
+}
 
 function onMotdStart(event) {
     this.motd_buffer = '';
-};
+}
 
 function onMotd(event) {
     this.motd_buffer += event.motd;
-};
+}
 
 function onMotdEnd(event) {
     this.irc_connection.clientEvent('motd', {
         msg: this.motd_buffer
     });
-};
+}
 
 function onError(event) {
     this.irc_connection.clientEvent('irc_error', {
         error: 'error',
         reason: event.reason
     });
-};
+}
+
+function onPasswordMismatch(event) {
+    this.irc_connection.clientEvent('irc_error', {
+        error: 'password_mismatch'
+    });
+}
 
 function onChannelRedirect(event) {
     this.irc_connection.clientEvent('channel_redirect', {
         from: event.from,
         to: event.to
     });
-};
+}
 
 function onNoSuchNick(event) {
     this.irc_connection.clientEvent('irc_error', {
@@ -128,15 +162,15 @@ function onNoSuchNick(event) {
         nick: event.nick,
         reason: event.reason
     });
-};
+}
 
 function onCannotSendToChan(event) {
     this.irc_connection.clientEvent('irc_error', {
-        error: 'cannot_send_to_chan',
+        error: 'cannot_send_to_channel',
         channel: event.channel,
         reason: event.reason
     });
-};
+}
 
 function onTooManyChannels(event) {
     this.irc_connection.clientEvent('irc_error', {
@@ -144,7 +178,7 @@ function onTooManyChannels(event) {
         channel: event.channel,
         reason: event.reason
     });
-};
+}
 
 function onUserNotInChannel(event) {
     this.irc_connection.clientEvent('irc_error', {
@@ -153,7 +187,7 @@ function onUserNotInChannel(event) {
         channel: event.channel,
         reason: event.reason
     });
-};
+}
 
 function onNotOnChannel(event) {
     this.irc_connection.clientEvent('irc_error', {
@@ -161,7 +195,7 @@ function onNotOnChannel(event) {
         channel: event.channel,
         reason: event.reason
     });
-};
+}
 
 function onChannelIsFull(event) {
     this.irc_connection.clientEvent('irc_error', {
@@ -169,7 +203,7 @@ function onChannelIsFull(event) {
         channel: event.channel,
         reason: event.reason
     });
-};
+}
 
 function onInviteOnlyChannel(event) {
     this.irc_connection.clientEvent('irc_error', {
@@ -177,7 +211,15 @@ function onInviteOnlyChannel(event) {
         channel: event.channel,
         reason: event.reason
     });
-};
+}
+
+function onUserAlreadyInChannel(event) {
+    this.irc_connection.clientEvent('irc_error', {
+        error: 'user_on_channel',
+        channel: event.channel,
+        nick: event.nick
+    });
+}
 
 function onBannedFromChannel(event) {
     this.irc_connection.clientEvent('irc_error', {
@@ -185,7 +227,7 @@ function onBannedFromChannel(event) {
         channel: event.channel,
         reason: event.reason
     });
-};
+}
 
 function onBadChannelKey(event) {
     this.irc_connection.clientEvent('irc_error', {
@@ -193,7 +235,7 @@ function onBadChannelKey(event) {
         channel: event.channel,
         reason: event.reason
     });
-};
+}
 
 function onChanopPrivsNeeded(event) {
     this.irc_connection.clientEvent('irc_error', {
@@ -201,7 +243,7 @@ function onChanopPrivsNeeded(event) {
         channel: event.channel,
         reason: event.reason
     });
-};
+}
 
 function onNicknameInUse(event) {
     this.irc_connection.clientEvent('irc_error', {
@@ -209,4 +251,20 @@ function onNicknameInUse(event) {
         nick: event.nick,
         reason: event.reason
     });
-};
+}
+
+function onErroneusNickname(event) {
+    this.irc_connection.clientEvent('irc_error', {
+        error: 'erroneus_nickname',
+        nick: event.nick,
+        reason: event.reason
+    });
+}
+
+function onUnknownCommand(event) {
+    this.irc_connection.clientEvent('unknown_command', {
+        error: 'unknown_command',
+        command: event.command,
+        params: event.params
+    });
+}