X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=server%2Fclient.js;h=8a0bc5f8d3828d1f0d5e7ffd54032c8462cb0ad9;hb=ac102e9d310410f0e027acc7df876fd615bd2bff;hp=4bfeb1bf44beddcff72cef6c10b36c0e909a9bb9;hpb=5befab984655ad63a501741b5af45178dbb2826b;p=KiwiIRC.git diff --git a/server/client.js b/server/client.js index 4bfeb1b..8a0bc5f 100755 --- a/server/client.js +++ b/server/client.js @@ -2,20 +2,21 @@ var util = require('util'), events = require('events'), crypto = require('crypto'), _ = require('lodash'), - State = require('./irc/state.js'); + State = require('./irc/state.js'), IrcConnection = require('./irc/connection.js').IrcConnection, - IrcCommands = require('./irc/commands.js'), - 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') @@ -23,29 +24,32 @@ 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); + this.client_commands.addRpcEvents(this, this.rpc); - websocket.on('irc', function () { - handleClientMessage.apply(that, arguments); - }); - websocket.on('kiwi', function () { - kiwiCommand.apply(that, arguments); - }); - websocket.on('disconnect', function () { + // Handles the kiwi.* RPC functions + this.attachKiwiCommands(); + + websocket.on('close', function () { websocketDisconnect.apply(that, arguments); }); websocket.on('error', function () { websocketError.apply(that, arguments); }); + + this.disposed = false; + + // Let the client know it's finished connecting + this.sendKiwiCommand('connected'); }; util.inherits(Client, events.EventEmitter); @@ -59,95 +63,66 @@ 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, args, obj, channels, keys; - - // 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.state.irc_connections[msg.server]) { - return (typeof callback === 'function') ? callback('not connected to server') : undefined; - } - - // The server this command is directed to - server = this.state.irc_connections[msg.server]; - 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 - this.client_commands.run(msg.data.method, msg.data.args, server, callback); -} +Client.prototype.attachKiwiCommands = function() { + var that = this; + 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'); + } + }); + 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 + }; + }); +}; -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) { - this.state.connect( - 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, - callback); - - } else { - this.state.connect( - command.hostname, - command.port, - command.ssl, - command.nick, - {hostname: this.websocket.handshake.revdns, address: this.websocket.handshake.real_address}, - command.password, - callback); - } - } else { - return callback('Hostname, port and nickname must be specified'); - } - break; - default: - callback(); - } -} // Websocket has disconnected, so quit all the IRC connections function websocketDisconnect() { this.emit('disconnect'); - + this.dispose(); } @@ -155,4 +130,4 @@ function websocketDisconnect() { // TODO: Should this close all the websocket connections too? function websocketError() { this.dispose(); -} \ No newline at end of file +}