IrcConnection handling in State instead of Client
authorJack Allnutt <m2ys4u@gmail.com>
Wed, 23 Jan 2013 20:43:50 +0000 (20:43 +0000)
committerDarren <darren@darrenwhitlen.com>
Thu, 31 Jan 2013 14:58:42 +0000 (14:58 +0000)
server/client.js
server/irc/state.js [new file with mode: 0755]

index 428878c8467d5a6f9bb35cc9b590bdfa33fe860d..ad343f72a26b115ad237a5009875f43c42adfc22 100755 (executable)
@@ -2,6 +2,7 @@ var util             = require('util'),
     events           = require('events'),
     crypto           = require('crypto'),
     _                = require('lodash'),
+    State            = require('./irc/state.js');
     IrcConnection    = require('./irc/connection.js').IrcConnection,
     IrcCommands      = require('./irc/commands.js'),
     ClientCommands   = require('./clientcommands.js');
@@ -23,8 +24,7 @@ var Client = function (websocket) {
         .update(Math.floor(Math.random() * 100000).toString())
         .digest('hex');
     
-    this.irc_connections = [];
-    this.next_connection = 0;
+    this.state = new State(this);
     
     this.buffer = {
         list: [],
@@ -78,12 +78,12 @@ function handleClientMessage(msg, callback) {
     // Make sure we have a server number specified
     if ((msg.server === null) || (typeof msg.server !== 'number')) {
         return (typeof callback === 'function') ? callback('server not specified') : undefined;
-    } else if (!this.irc_connections[msg.server]) {
+    } else if (!this.state.irc_connections[msg.server]) {
         return (typeof callback === 'function') ? callback('not connected to server') : undefined;
     }
 
     // The server this command is directed to
-    server = this.irc_connections[msg.server];
+    server = this.state.irc_connections[msg.server];
 
     if (typeof callback !== 'function') {
         callback = null;
@@ -115,7 +115,7 @@ function kiwiCommand(command, callback) {
                 var con;
 
                 if (global.config.restrict_server) {
-                    con = new IrcConnection(
+                    this.state.connect(
                         global.config.restrict_server,
                         global.config.restrict_server_port,
                         global.config.restrict_server_ssl,
@@ -124,7 +124,7 @@ function kiwiCommand(command, callback) {
                         global.config.restrict_server_password);
 
                 } else {
-                    con = new IrcConnection(
+                    this.state.connect(
                         command.hostname,
                         command.port,
                         command.ssl,
@@ -132,28 +132,6 @@ function kiwiCommand(command, callback) {
                         {hostname: this.websocket.handshake.revdns, address: this.websocket.handshake.real_address},
                         command.password);
                 }
-
-                var con_num = this.next_connection++;
-                this.irc_connections[con_num] = con;
-                con.con_num = con_num;
-
-                var irc_commands = new IrcCommands(con, con_num, this);
-                irc_commands.bindEvents();
-                
-                con.on('connected', function () {
-                    return callback(null, con_num);
-                });
-                
-                con.on('error', function (err) {
-                    console.log('irc_connection error (' + command.hostname + '):', err);
-                    // TODO: Once multiple servers implemented, specify which server failed
-                    //that.sendKiwiCommand('error', {server: con_num, error: err});
-                    return callback(err.code, null);
-                });
-                
-                con.on('close', function () {
-                    that.irc_connections[con_num] = null;
-                });
             } else {
                 return callback('Hostname, port and nickname must be specified');
             }
@@ -166,13 +144,7 @@ function kiwiCommand(command, callback) {
 
 // Websocket has disconnected, so quit all the IRC connections
 function websocketDisconnect() {
-    _.each(this.irc_connections, function (irc_connection, i, cons) {
-        if (irc_connection) {
-            irc_connection.end('QUIT :' + (global.config.quit_message || ''));
-            irc_connection.dispose();
-            cons[i] = null;
-        }
-    });
+    this.emit('disconnect');
     
     this.dispose();
 }
diff --git a/server/irc/state.js b/server/irc/state.js
new file mode 100755 (executable)
index 0000000..97ced7f
--- /dev/null
@@ -0,0 +1,83 @@
+var IrcConnection = require('./connection.js');
+
+var State = function (client, save_state) {
+    events.EventEmitter.call(this);
+    this.client = client;
+    this.save_state = save_state || false;
+    
+    this.irc_connections = [];
+    this.next_connection = 0;
+    
+    this.client.on('disconnect', function () {
+        if (!this.save_state) {
+            _.each(this.irc_connections, function (irc_connection, i, cons) {
+                if (irc_connection) {
+                    irc_connection.end('QUIT :' + (global.config.quit_message || ''));
+                    irc_connection.dispose();
+                    cons[i] = null;
+                }
+            });
+            
+            this.dispose();
+        }
+    });
+};
+
+util.inherits(State, events.EventEmitter);
+
+module.exports = State;
+
+State.prototype.connect = function (hostname, port, ssl, nick, user, pass, callback) {
+    var that = this;
+    var con, con_num;
+    if (global.config.restrict_server) {
+        con = new IrcConnection(
+            global.config.restrict_server,
+            global.config.restrict_server_port,
+            global.config.restrict_server_ssl,
+            command.nick,
+            address,
+            global.config.restrict_server_password);
+
+    } else {
+        con = new IrcConnection(
+            command.hostname,
+            command.port,
+            command.ssl,
+            command.nick,
+            address,
+            command.password);
+    }
+    
+    con_num = this.next_connection++;
+    this.irc_connections[con_num] = con;
+    con.con_num = con_num;
+    
+    new IrcCommands(con, con_num, this).bindEvents();
+    
+    con.on('connected', function () {
+        return callback(null, con_num);
+    });
+    
+    con.on('error', function (err) {
+        console.log('irc_connection error (' + command.hostname + '):', err);
+        return callback(err.code, {server: con_num, error: err});
+    });
+
+    con.on('close', function () {
+        that.irc_connections[con_num] = null;
+    });
+};
+
+State.prototype.sendIrcCommand = function () {
+    this.client.sendIrcCommand.apply(this.client, arguments);
+};
+
+State.prototype.sendKiwiCommand = function () {
+    this.client.sendKiwicommand.apply(this.client, arguments);
+};
+
+State.prototype.dispose = function () {
+    this.emit('destroy');
+    this.removeAllListeners();
+};