var util = require('util'),
events = require('events'),
- _ = require('underscore'),
+ 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');
+ ClientCommands = require('./clientcommands.js');
var Client = function (websocket) {
events.EventEmitter.call(this);
this.websocket = websocket;
+
+ // Clients address
+ this.real_address = this.websocket.handshake.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.irc_connections = [];
- this.next_connection = 0;
+ this.state = new State(this);
this.buffer = {
list: [],
Client.prototype.sendIrcCommand = function (command, data, callback) {
var c = {command: command, data: data};
- //console.log('C<--', c);
this.websocket.emit('irc', c, callback);
};
-Client.prototype.sendKiwiCommand = function (command, callback) {
- this.websocket.emit('kiwi', command, callback);
+Client.prototype.sendKiwiCommand = function (command, data, callback) {
+ var c = {command: command, data: data};
+ this.websocket.emit('kiwi', c, callback);
+};
+
+Client.prototype.dispose = function () {
+ this.emit('dispose');
+ this.removeAllListeners();
};
function handleClientMessage(msg, callback) {
// 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;
// Run the client command
this.client_commands.run(msg.data.method, msg.data.args, server, callback);
-};
+}
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 irc_commands = new IrcCommands(con, con_num, this);
- irc_commands.bindEvents();
-
- 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 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() {
- _.each(this.irc_connections, function (irc_connection, i, cons) {
- if (irc_connection) {
- irc_connection.end('QUIT :Kiwi IRC');
- cons[i] = null;
- }
- });
- this.emit('destroy');
-};
+ this.emit('disconnect');
+
+ this.dispose();
+}
// TODO: Should this close all the websocket connections too?
function websocketError() {
- this.emit('destroy');
-};
+ this.dispose();
+}
\ No newline at end of file