Merge branch 'settings' into development
[KiwiIRC.git] / server / client.js
index 36edf0c0254088b4a797f7c90b48cd172a24bb31..6fee1dc5cacc0803b5369c6a5eef4a9a4ab0b439 100755 (executable)
@@ -2,8 +2,8 @@ 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 +23,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: [],
@@ -68,22 +67,22 @@ Client.prototype.sendKiwiCommand = function (command, data, callback) {
 };
 
 Client.prototype.dispose = function () {
-    this.emit('destroy');
+    this.emit('dispose');
     this.removeAllListeners();
 };
 
 function handleClientMessage(msg, callback) {
-    var server, args, obj, channels, keys;
+    var server;
 
     // 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;
@@ -104,55 +103,23 @@ function handleClientMessage(msg, callback) {
 
 
 function kiwiCommand(command, callback) {
-    var that = this;
-    
     if (typeof callback !== 'function') {
         callback = function () {};
     }
+
     switch (command.command) {
         case 'connect':
             if (command.hostname && command.port && command.nick) {
                 var con;
 
-                if (global.config.restrict_server) {
-                    con = new IrcConnection(
-                        global.config.restrict_server,
-                        global.config.restrict_server_port,
-                        global.config.restrict_server_ssl,
-                        command.nick,
-                        {hostname: this.websocket.handshake.revdns, address: this.websocket.handshake.real_address},
-                        global.config.restrict_server_password);
-
-                } else {
-                    con = new IrcConnection(
-                        command.hostname,
-                        command.port,
-                        command.ssl,
-                        command.nick,
-                        {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;
-
-                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;
-                });
+                this.state.connect(
+                    (global.config.restrict_server || command.hostname),
+                    (global.config.restrict_server_port || command.port),
+                    (global.config.restrict_server_ssl || command.ssl),
+                    command.nick,
+                    {hostname: this.websocket.handshake.revdns, address: this.websocket.handshake.real_address},
+                    (global.config.restrict_server_password || command.password),
+                    callback);
             } else {
                 return callback('Hostname, port and nickname must be specified');
             }
@@ -165,13 +132,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();
 }
@@ -180,4 +141,4 @@ function websocketDisconnect() {
 // TODO: Should this close all the websocket connections too?
 function websocketError() {
     this.dispose();
-}
\ No newline at end of file
+}