var util = require('util'),
events = require('events'),
- _ = require('underscore'),
- IRCConnection = require('./irc-connection.js').IRCConnection,
- IRCCommands = require('./irc-commands.js'),
- ClientCommandset = require('./client-commands.js').ClientCommandset;
+ crypto = require('crypto'),
+ _ = require('lodash'),
+ State = require('./irc/state.js'),
+ IrcConnection = require('./irc/connection.js').IrcConnection,
+ ClientCommands = require('./clientcommands.js'),
+ WebsocketRpc = require('./websocketrpc.js');
+
var Client = function (websocket) {
- var c = this;
-
+ var that = this;
+
events.EventEmitter.call(this);
this.websocket = websocket;
-
- this.IRC_connections = [];
- this.next_connection = 0;
-
+ this.rpc = new WebsocketRpc(this.websocket);
+
+ // Clients address
+ this.real_address = this.websocket.meta.real_address;
+
+ // A hash to identify this client instance
+ this.hash = crypto.createHash('sha256')
+ .update(this.real_address)
+ .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 ClientCommandset(this);
+ this.client_commands = new ClientCommands(this);
- websocket.on('irc', function () {
- handleClientMessage.apply(c, arguments);
+ this.rpc.on('irc', function (response, data) {
+ handleClientMessage.call(that, data, response);
});
- websocket.on('kiwi', function () {
- kiwi_command.apply(c, arguments);
+ this.rpc.on('kiwi', function (response, data) {
+ kiwiCommand.call(that, data, response);
});
- websocket.on('disconnect', function () {
- disconnect.apply(c, arguments);
+ websocket.on('close', function () {
+ websocketDisconnect.apply(that, arguments);
});
websocket.on('error', function () {
- error.apply(c, arguments);
+ websocketError.apply(that, arguments);
});
+
+ this.disposed = false;
+
+ // Let the client know it's finished connecting
+ this.sendKiwiCommand('connected');
};
util.inherits(Client, events.EventEmitter);
// error MUST otherwise be a truthy value and SHOULD be a string where the cause of the error is known.
// response MAY be given even if error is truthy
-Client.prototype.sendIRCCommand = function (command, data, callback) {
+Client.prototype.sendIrcCommand = function (command, data, callback) {
+ var c = {command: command, data: data};
+ this.rpc.call('irc', c, callback);
+};
+
+Client.prototype.sendKiwiCommand = function (command, data, callback) {
var c = {command: command, data: data};
- console.log('C<--', c);
- this.websocket.emit('irc', c, callback);
+ this.rpc.call('kiwi', c, callback);
};
-Client.prototype.sendKiwiCommand = function (command, callback) {
- this.websocket.emit('kiwi', command, callback);
+Client.prototype.dispose = function () {
+ this.disposed = true;
+ this.rpc.dispose();
+ this.emit('dispose');
+ this.removeAllListeners();
};
-var handleClientMessage = function (msg, callback) {
- var server, args, obj, channels, keys;
+function handleClientMessage(msg, callback) {
+ var that = this,
+ server;
// Make sure we have a server number specified
if ((msg.server === null) || (typeof msg.server !== 'number')) {
- return callback('server not specified');
- } else if (!this.IRC_connections[msg.server]) {
- return callback('not connected to server');
+ 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.IRC_connections[msg.server];
+ server = this.state.irc_connections[msg.server];
if (typeof callback !== 'function') {
callback = null;
}
// 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);
+ });
+}
-var kiwi_command = function (command, callback) {
- var that = this;
- console.log(typeof callback);
+function kiwiCommand(command, callback) {
if (typeof callback !== 'function') {
callback = function () {};
}
+
switch (command.command) {
- case 'connect':
- if ((command.hostname) && (command.port) && (command.nick)) {
- var con = new IRCConnection(command.hostname, command.port, command.ssl,
- command.nick, {hostname: this.websocket.handshake.revdns, address: this.websocket.handshake.address.address},
- command.password, null);
-
- var con_num = this.next_connection++;
- this.IRC_connections[con_num] = con;
-
- var binder = new IRCCommands.Binder(con, con_num, this);
- binder.bind_irc_commands();
-
- con.on('connected', function () {
- console.log("con.on('connected')");
- return callback(null, con_num);
- });
-
- con.on('error', function (err) {
- this.websocket.sendKiwiCommand('error', {server: con_num, error: err});
- });
-
- con.on('close', function () {
- that.IRC_connections[con_num] = null;
- });
- } else {
- return callback('Hostname, port and nickname must be specified');
- }
- break;
- default:
- callback();
+ 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');
+ }
+ break;
+ default:
+ callback();
}
-};
+}
-var extension_command = function (command, callback) {
- if (typeof callback === 'function') {
- callback('not implemented');
- }
-};
-var disconnect = function () {
- _.each(this.IRC_connections, function (irc_connection, i, cons) {
- if (irc_connection) {
- irc_connection.end('QUIT :Kiwi IRC');
- cons[i] = null;
- }
- });
- this.emit('destroy');
-};
+// Websocket has disconnected, so quit all the IRC connections
+function websocketDisconnect() {
+ this.emit('disconnect');
-var error = function () {
- this.emit('destroy');
-};
+ this.dispose();
+}
+
+
+// TODO: Should this close all the websocket connections too?
+function websocketError() {
+ this.dispose();
+}