Switching the old RPC methods to the websocketrpc
[KiwiIRC.git] / server / client.js
index 0d27ddb7bf97e4366704a80c330ee4c3b5a5d85e..8a0bc5f8d3828d1f0d5e7ffd54032c8462cb0ad9 100755 (executable)
@@ -34,13 +34,11 @@ var Client = function (websocket) {
 
     // Handler for any commands sent from the client
     this.client_commands = new ClientCommands(this);
+    this.client_commands.addRpcEvents(this, this.rpc);
+
+    // Handles the kiwi.* RPC functions
+    this.attachKiwiCommands();
 
-    this.rpc.on('irc', function (response, data) {
-        handleClientMessage.call(that, data, response);
-    });
-    this.rpc.on('kiwi', function (response, data) {
-        kiwiCommand.call(that, data, response);
-    });
     websocket.on('close', function () {
         websocketDisconnect.apply(that, arguments);
     });
@@ -80,88 +78,45 @@ Client.prototype.dispose = function () {
     this.removeAllListeners();
 };
 
-function handleClientMessage(msg, callback) {
-    var that = this,
-        server;
-
-    // Make sure we have a server number specified
-    if ((msg.connection_id === null) || (typeof msg.connection_id !== 'number')) {
-        return (typeof callback === 'function') ? callback('server not specified') : undefined;
-    } else if (!this.state.irc_connections[msg.connection_id]) {
-        return (typeof callback === 'function') ? callback('not connected to server') : undefined;
-    }
-
-    // The server this command is directed to
-    server = this.state.irc_connections[msg.connection_id];
-
-    if (typeof callback !== 'function') {
-        callback = null;
-    }
-
-    try {
-        msg.data = JSON.parse(msg.data);
-    } catch (e) {
-        kiwi.log('[handleClientMessage] JSON parsing error ' + msg.data);
-        return;
-    }
-
-    // Run the client command
-    global.modules.emit('client command', {
-        command: msg.data,
-        server: server
-    })
-    .done(function() {
-        that.client_commands.run(msg.data.method, msg.data.args, server, callback);
-    });
-}
-
-
-
 
-function kiwiCommand(command, callback) {
-    if (typeof callback !== 'function') {
-        callback = function () {};
-    }
 
-    switch (command.command) {
-        case 'connect':
-            if (command.hostname && command.port && command.nick) {
-                var options = {};
-
-                // Get any optional parameters that may have been passed
-                if (command.encoding)
-                    options.encoding = command.encoding;
-
-                options.password = global.config.restrict_server_password || command.password;
-
-                this.state.connect(
-                    (global.config.restrict_server || command.hostname),
-                    (global.config.restrict_server_port || command.port),
-                    (typeof global.config.restrict_server_ssl !== 'undefined' ?
-                        global.config.restrict_server_ssl :
-                        command.ssl),
-                    command.nick,
-                    {hostname: this.websocket.meta.revdns, address: this.websocket.meta.real_address},
-                    options,
-                    callback);
-            } else {
-                return callback('Hostname, port and nickname must be specified');
-            }
+Client.prototype.attachKiwiCommands = function() {
+    var that = this;
 
-            break;
+    this.rpc.on('kiwi.connect', function(callback, command) {
+        if (command.hostname && command.port && command.nick) {
+            var options = {};
+
+            // Get any optional parameters that may have been passed
+            if (command.encoding)
+                options.encoding = command.encoding;
+
+            options.password = global.config.restrict_server_password || command.password;
+
+            that.state.connect(
+                (global.config.restrict_server || command.hostname),
+                (global.config.restrict_server_port || command.port),
+                (typeof global.config.restrict_server_ssl !== 'undefined' ?
+                    global.config.restrict_server_ssl :
+                    command.ssl),
+                command.nick,
+                {hostname: that.websocket.meta.revdns, address: that.websocket.meta.real_address},
+                options,
+                callback);
+        } else {
+            return callback('Hostname, port and nickname must be specified');
+        }
+    });
 
-        case 'client_info':
-            // keep hold of selected parts of the client_info
-            this.client_info = {
-                build_version: command.build_version.toString() || undefined
-            };
 
-            break;
+    this.rpc.on('kiwi.client_info', function(callback, args) {
+        // keep hold of selected parts of the client_info
+        that.client_info = {
+            build_version: args.build_version.toString() || undefined
+        };
+    });
+};
 
-        default:
-            callback();
-    }
-}
 
 
 // Websocket has disconnected, so quit all the IRC connections