Merge pull request #480 from M2Ys4U/config_commandline
[KiwiIRC.git] / server / client.js
index c9c13827234c946411f6b13a0136f7d01a0275ae..d41dc52e3db96568ccc86567c3fa6f87727bab82 100755 (executable)
@@ -4,17 +4,19 @@ var util             = require('util'),
     _                = require('lodash'),
     State            = require('./irc/state.js'),
     IrcConnection    = require('./irc/connection.js').IrcConnection,
-    ClientCommands   = require('./clientcommands.js');
+    ClientCommands   = require('./clientcommands.js'),
+    WebsocketRpc     = require('./websocketrpc.js');
 
 
 var Client = function (websocket) {
     var that = this;
-    
+
     events.EventEmitter.call(this);
     this.websocket = websocket;
+    this.rpc = new WebsocketRpc(this.websocket);
 
     // Clients address
-    this.real_address = this.websocket.handshake.real_address;
+    this.real_address = this.websocket.meta.real_address;
 
     // A hash to identify this client instance
     this.hash = crypto.createHash('sha256')
@@ -22,24 +24,24 @@ var Client = function (websocket) {
         .update('' + Date.now())
         .update(Math.floor(Math.random() * 100000).toString())
         .digest('hex');
-    
+
     this.state = new State(this);
-    
+
     this.buffer = {
         list: [],
         motd: ''
     };
-    
+
     // Handler for any commands sent from the client
     this.client_commands = new ClientCommands(this);
 
-    websocket.on('irc', function () {
-        handleClientMessage.apply(that, arguments);
+    this.rpc.on('irc', function (response, data) {
+        handleClientMessage.call(that, data, response);
     });
-    websocket.on('kiwi', function () {
-        kiwiCommand.apply(that, arguments);
+    this.rpc.on('kiwi', function (response, data) {
+        kiwiCommand.call(that, data, response);
     });
-    websocket.on('disconnect', function () {
+    websocket.on('close', function () {
         websocketDisconnect.apply(that, arguments);
     });
     websocket.on('error', function () {
@@ -47,6 +49,9 @@ var Client = function (websocket) {
     });
 
     this.disposed = false;
+
+    // Let the client know it's finished connecting
+    this.sendKiwiCommand('connected');
 };
 util.inherits(Client, events.EventEmitter);
 
@@ -60,22 +65,24 @@ module.exports.Client = Client;
 
 Client.prototype.sendIrcCommand = function (command, data, callback) {
     var c = {command: command, data: data};
-    this.websocket.emit('irc', c, callback);
+    this.rpc.call('irc', c, callback);
 };
 
 Client.prototype.sendKiwiCommand = function (command, data, callback) {
     var c = {command: command, data: data};
-    this.websocket.emit('kiwi', c, callback);
+    this.rpc.call('kiwi', c, callback);
 };
 
 Client.prototype.dispose = function () {
     this.disposed = true;
+    this.rpc.dispose();
     this.emit('dispose');
     this.removeAllListeners();
 };
 
 function handleClientMessage(msg, callback) {
-    var server;
+    var that = this,
+        server;
 
     // Make sure we have a server number specified
     if ((msg.server === null) || (typeof msg.server !== 'number')) {
@@ -99,7 +106,13 @@ function handleClientMessage(msg, callback) {
     }
 
     // Run the client command
-    this.client_commands.run(msg.data.method, msg.data.args, server, callback);
+    global.modules.emit('client command', {
+        command: msg.data,
+        server: server
+    })
+    .done(function() {
+        that.client_commands.run(msg.data.method, msg.data.args, server, callback);
+    });
 }
 
 
@@ -128,7 +141,7 @@ function kiwiCommand(command, callback) {
                         global.config.restrict_server_ssl :
                         command.ssl),
                     command.nick,
-                    {hostname: this.websocket.handshake.revdns, address: this.websocket.handshake.real_address},
+                    {hostname: this.websocket.meta.revdns, address: this.websocket.meta.real_address},
                     options,
                     callback);
             } else {
@@ -144,7 +157,7 @@ function kiwiCommand(command, callback) {
 // Websocket has disconnected, so quit all the IRC connections
 function websocketDisconnect() {
     this.emit('disconnect');
-    
+
     this.dispose();
 }