Merged in lodash from karbassi
[KiwiIRC.git] / server / client.js
1 var util = require('util'),
2 events = require('events'),
3 crypto = require('crypto'),
4 _ = require('lodash'),
5 IrcConnection = require('./irc/connection.js').IrcConnection,
6 IrcCommands = require('./irc/commands.js'),
7 ClientCommands = require('./clientcommands.js');
8
9
10 var Client = function (websocket) {
11 var that = this;
12
13 events.EventEmitter.call(this);
14 this.websocket = websocket;
15
16 // Clients address
17 this.real_address = this.websocket.handshake.real_address;
18
19 // A hash to identify this client instance
20 this.hash = crypto.createHash('sha256')
21 .update(this.real_address)
22 .update('' + Date.now())
23 .update(Math.floor(Math.random() * 100000).toString())
24 .digest('hex');
25
26 this.irc_connections = [];
27 this.next_connection = 0;
28
29 this.buffer = {
30 list: [],
31 motd: ''
32 };
33
34 // Handler for any commands sent from the client
35 this.client_commands = new ClientCommands(this);
36
37 websocket.on('irc', function () {
38 handleClientMessage.apply(that, arguments);
39 });
40 websocket.on('kiwi', function () {
41 kiwiCommand.apply(that, arguments);
42 });
43 websocket.on('disconnect', function () {
44 websocketDisconnect.apply(that, arguments);
45 });
46 websocket.on('error', function () {
47 websocketError.apply(that, arguments);
48 });
49 };
50 util.inherits(Client, events.EventEmitter);
51
52 module.exports.Client = Client;
53
54 // Callback API:
55 // Callbacks SHALL accept 2 arguments, error and response, in that order.
56 // error MUST be null where the command is successul.
57 // error MUST otherwise be a truthy value and SHOULD be a string where the cause of the error is known.
58 // response MAY be given even if error is truthy
59
60 Client.prototype.sendIrcCommand = function (command, data, callback) {
61 var c = {command: command, data: data};
62 this.websocket.emit('irc', c, callback);
63 };
64
65 Client.prototype.sendKiwiCommand = function (command, data, callback) {
66 var c = {command: command, data: data};
67 this.websocket.emit('kiwi', c, callback);
68 };
69
70 Client.prototype.dispose = function () {
71 this.emit('destroy');
72 this.removeAllListeners();
73 };
74
75 function handleClientMessage(msg, callback) {
76 var server, args, obj, channels, keys;
77
78 // Make sure we have a server number specified
79 if ((msg.server === null) || (typeof msg.server !== 'number')) {
80 return (typeof callback === 'function') ? callback('server not specified') : undefined;
81 } else if (!this.irc_connections[msg.server]) {
82 return (typeof callback === 'function') ? callback('not connected to server') : undefined;
83 }
84
85 // The server this command is directed to
86 server = this.irc_connections[msg.server];
87
88 if (typeof callback !== 'function') {
89 callback = null;
90 }
91
92 try {
93 msg.data = JSON.parse(msg.data);
94 } catch (e) {
95 kiwi.log('[handleClientMessage] JSON parsing error ' + msg.data);
96 return;
97 }
98
99 // Run the client command
100 this.client_commands.run(msg.data.method, msg.data.args, server, callback);
101 }
102
103
104
105
106 function kiwiCommand(command, callback) {
107 var that = this;
108
109 if (typeof callback !== 'function') {
110 callback = function () {};
111 }
112 switch (command.command) {
113 case 'connect':
114 if (command.hostname && command.port && command.nick) {
115 var con;
116
117 if (global.config.restrict_server) {
118 con = new IrcConnection(
119 global.config.restrict_server,
120 global.config.restrict_server_port,
121 global.config.restrict_server_ssl,
122 command.nick,
123 {hostname: this.websocket.handshake.revdns, address: this.websocket.handshake.real_address},
124 global.config.restrict_server_password);
125
126 } else {
127 con = new IrcConnection(
128 command.hostname,
129 command.port,
130 command.ssl,
131 command.nick,
132 {hostname: this.websocket.handshake.revdns, address: this.websocket.handshake.real_address},
133 command.password);
134 }
135
136 var con_num = this.next_connection++;
137 this.irc_connections[con_num] = con;
138
139 var irc_commands = new IrcCommands(con, con_num, this);
140 irc_commands.bindEvents();
141
142 con.on('connected', function () {
143 return callback(null, con_num);
144 });
145
146 con.on('error', function (err) {
147 console.log('irc_connection error (' + command.hostname + '):', err);
148 // TODO: Once multiple servers implemented, specify which server failed
149 //that.sendKiwiCommand('error', {server: con_num, error: err});
150 return callback(err.code, null);
151 });
152
153 con.on('close', function () {
154 that.irc_connections[con_num] = null;
155 });
156 } else {
157 return callback('Hostname, port and nickname must be specified');
158 }
159 break;
160 default:
161 callback();
162 }
163 }
164
165
166 // Websocket has disconnected, so quit all the IRC connections
167 function websocketDisconnect() {
168 _.each(this.irc_connections, function (irc_connection, i, cons) {
169 if (irc_connection) {
170 irc_connection.end('QUIT :' + (global.config.quit_message || ''));
171 irc_connection.dispose();
172 cons[i] = null;
173 }
174 });
175
176 this.dispose();
177 }
178
179
180 // TODO: Should this close all the websocket connections too?
181 function websocketError() {
182 this.dispose();
183 }