Get codebase into semi-working state
authorJack Allnutt <m2ys4u@gmail.com>
Sun, 27 Jan 2013 02:59:39 +0000 (02:59 +0000)
committerDarren <darren@darrenwhitlen.com>
Thu, 31 Jan 2013 14:58:43 +0000 (14:58 +0000)
server/irc/channel.js
server/irc/commands.js
server/irc/connection.js
server/irc/user.js

index e75bcf07e001b937036db7b388167e2841b15b91..b7aaa971e792a82376804a717b17ab715a33006c 100644 (file)
@@ -1,5 +1,6 @@
-var util    = require('util'),
-    EventBinder  = require('./eventbinder.js');
+var util        = require('util'),
+    EventBinder = require('./eventbinder.js'),
+    IrcUser     = require('./user.js');
 
 function IrcChannel(irc_connection, name) {
     this.irc_connection = irc_connection;
@@ -19,11 +20,11 @@ function IrcChannel(irc_connection, name) {
         ctcp_request:   onCtcpRequest,
         ctcp_response:  onCtcpResponse,
         topic:          onTopic,
-        nicklist:       onNicklist,
-        nicklistEnd:    onNicklistEnd,
+        userlist:       onNicklist,
+        userlist_end:   onNicklistEnd,
         banlist:        onBanList,
         banlist_end:    onBanListEnd,
-        topicsetby:     onTopicSetby,
+        topicsetby:     onTopicSetBy,
         mode:           onMode
     };
     EventBinder.bindIrcEvents('channel:' + this.name, this.irc_events, this, irc_connection);
@@ -34,7 +35,7 @@ module.exports = IrcChannel;
 
 
 IrcChannel.prototype.dispose = function (){
-    EventBinder.unbindIrcEvents('channel:' + this.name, this.irc_events);
+    EventBinder.unbindIrcEvents('channel:' + this.name, this.irc_events, this.irc_connection);
     this.irc_connection = undefined;
 };
 
@@ -45,12 +46,12 @@ function onJoin(event) {
         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);
     }
 };
 
@@ -100,7 +101,7 @@ function onMsg(event) {
         ident: event.ident,
         hostname: event.hostname,
         channel: this.name,
-        msg: event.message
+        msg: event.msg
     });
 };
 
@@ -146,6 +147,7 @@ function onNicklist(event) {
         users: event.users,
         channel: this.name
     });
+    updateUsersList.call(this, event.users);
 };
 
 
@@ -154,8 +156,20 @@ function onNicklistEnd(event) {
         users: event.users,
         channel: this.name
     });
+    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', {
index e806ddbf7c2af0878ea4a26bf88642fbde395dd0..804b6c713e64d4f9ca774903bf6052ec334dce84 100644 (file)
@@ -150,7 +150,7 @@ var listeners = {
     'RPL_WHOISCHANNELS':       function (command) {
         this.irc_connection.emit('user:' + command.params[1] + ':whoischannels', {
             nick: command.params[1],
-            chans: commnd.trailing
+            chans: command.trailing
         });
     },
     'RPL_WHOISMODES': function (command) {
@@ -452,8 +452,8 @@ var listeners = {
             }
         } else {
             // A message to a user (private message) or to a channel?
-            namespace = (command.target == this.irc_connection.nick) ? 'user' : 'channel';
-            this.irc_connection.emit(namespace + ':' + command.nick + ':privmsg', {
+            namespace = (command.params[0] === this.irc_connection.nick) ? 'user:' + command.nick : 'channel:' + command.params[0];
+            this.irc_connection.emit(namespace + ':privmsg', {
                 nick: command.nick,
                 ident: command.ident,
                 hostname: command.hostname,
index 0657a270e3550291b8c3cfa34a80ab7c94adfe11..6c567e404a0eb4ae8c40643b6cd111a89299f9ab 100644 (file)
@@ -1,13 +1,19 @@
-var net     = require('net'),
-    tls     = require('tls'),
-    events  = require('events'),
-    util    = require('util'),
-    _       = require('lodash');
+var net             = require('net'),
+    tls             = require('tls'),
+    util            = require('util'),
+    _               = require('lodash'),
+    EventEmitter2   = require('eventemitter2').EventEmitter2,
+    IrcServer       = require('./server.js'),
+    IrcChannel      = require('./channel.js'),
+    IrcUser         = require('./user.js');
 
 
 var IrcConnection = function (hostname, port, ssl, nick, user, pass, state) {
     var that = this;
-    events.EventEmitter.call(this);
+    EventEmitter2.call(this,{
+        wildcard: true,
+        delimiter: ':'
+    });
     
     // Socket state
     this.connected = false;
@@ -26,6 +32,41 @@ var IrcConnection = function (hostname, port, ssl, nick, user, pass, state) {
     
     // State object
     this.state = state;
+    
+    // IrcServer object
+    this.server = new IrcServer(this, hostname, port);
+    
+    // IrcUser objects
+    this.irc_users = Object.create(null);
+    
+    // IrcChannel objects
+    this.irc_channels = Object.create(null);
+    
+    // Create IrcUser and IrcChannel objects when needed
+    // TODO: Remove IrcUser objects when they are no longer needed
+    this.on('server:*:connect', function (event) {
+        that.nick = event.nick;
+        that.irc_users[event.nick] = new IrcUser(that, event.nick);
+    });
+    this.on('channel:*:join', function (event) {
+        var chan;
+        if (event.nick === that.nick) {
+            chan = new IrcChannel(that, event.channel);
+            that.irc_channels[event.channel] = chan;
+            chan.irc_events.join.call(chan, event);
+        }
+    });
+    
+    this.on('user:*:privmsg', function (event) {
+        var user;
+        if (event.channel === that.nick) {
+            if (!that.irc_users[event.nick]) {
+                user = new IrcUser(that, event.nick);
+                that.irc_users[event.nick] = user;
+                user.irc_events.privmsg.call(user, event);
+            }
+        }
+    });
 
     // IRC connection information
     this.irc_host = {hostname: hostname, port: port};
@@ -49,7 +90,7 @@ var IrcConnection = function (hostname, port, ssl, nick, user, pass, state) {
             that.connect();
         });
 };
-util.inherits(IrcConnection, events.EventEmitter);
+util.inherits(IrcConnection, EventEmitter2);
 
 module.exports.IrcConnection = IrcConnection;
 
@@ -144,6 +185,14 @@ IrcConnection.prototype.end = function (data, callback) {
  * Clean up this IrcConnection instance and any sockets
  */
 IrcConnection.prototype.dispose = function () {
+    _.each(this.irc_users, function (user) {
+        user.dispose();
+    });
+    _.each(this.irc_channels, function (chan) {
+        chan.dispose();
+    });
+    this.irc_users = null;
+    this.irc_channels = null;
     this.disposeSocket();
     this.removeAllListeners();
 };
index 40fdf900227b8978b6371647030fe4275f7d863c..de66a5a78026115442bf07bbc518ec9b87cb56ad 100755 (executable)
@@ -8,14 +8,14 @@ var IrcUser = function (irc_connection, nick) {
     this.irc_events = {\r
         nick:           onNick,\r
         away:           onAway,\r
-        quit:           onKick,\r
+        quit:           onQuit,\r
         whoisuser:      onWhoisUser,\r
         whoisoperator:  onWhoisOperator,\r
         whoischannels:  onWhoisChannels,\r
         whoismodes:     onWhoisModes,\r
         whoisidle:      onWhoisIdle,\r
-        whoisregnick:   onRegNick,\r
-        endofwhois:     onEhoisEnd,\r
+        whoisregnick:   onWhoisRegNick,\r
+        endofwhois:     onWhoisEnd,\r
         notice:         onNotice,\r
         ctcp_response:  onCtcpResponse,\r
         privmsg:        onPrivmsg,\r
@@ -29,7 +29,7 @@ var IrcUser = function (irc_connection, nick) {
 module.exports = IrcUser;\r
 \r
 \r
-IrcUser.prototype.dispose = function (){\r
+IrcUser.prototype.dispose = function () {\r
     EventBinder.unbindIrcEvents('user:' + this.nick, this.irc_events);\r
     this.irc_connection = undefined;\r
 };\r
@@ -42,6 +42,9 @@ function onNick(event) {
         hostname: event.hostname,\r
         newnick: event.newnick\r
     });\r
+    EventBinder.unbindIrcEvents('user:' + this.nick, this.irc_events);\r
+    this.nick = event.newnick;\r
+    EventBinder.bindIrcEvents('user:' + this.nick, this.irc_events, this, irc_connection);\r
 };\r
 \r
 function onAway(event) {\r
@@ -102,7 +105,7 @@ function onWhoisModes(event) {
     });\r
 };\r
 \r
-function onWhoisUser(event) {\r
+function onWhoisIdle(event) {\r
     this.irc_connection.clientEvent('whois', {\r
         nick: event.nick,\r
         idle: event.idle,\r
@@ -148,7 +151,7 @@ function onCtcpResponse(event) {
 };\r
 \r
 function onPrivmsg(event) {\r
-    this.irc_connection.clientEvent('privmsg', {\r
+    this.irc_connection.clientEvent('msg', {\r
         nick: event.nick,\r
         ident: event.ident,\r
         hostname: event.hostname,\r