Slightly more lax RFC ruling on the IRC message format to work with several bouncers...
[KiwiIRC.git] / node / kiwi.js
1 /*jslint continue: true, forin: true, regexp: true, confusion: true, undef: false, node: true, sloppy: true, nomen: true, plusplus: true, maxerr: 50, indent: 4 */
2
3 var tls = require('tls'),
4 net = require('net'),
5 http = require('http'),
6 https = require('https'),
7 fs = require('fs'),
8 url = require('url'),
9 ws = require('socket.io'),
10 _ = require('./lib/underscore.min.js'),
11 starttls = require('./lib/starttls.js');
12
13 // Libraries may need to know kiwi.js path as __dirname
14 // only gives that librarys path. Set it here for usage later.
15 var kiwi_root = __dirname;
16
17
18 /*
19 * Find a config file in the following order:
20 * - /etc/kiwi/config.json
21 * - ./config.json
22 */
23 var config = null,
24 config_filename = 'config.json',
25 config_dirs = ['/etc/kiwiirc/', __dirname + '/'];
26
27 (function () {
28 var i;
29 for (i in config_dirs) {
30 try {
31 if (fs.lstatSync(config_dirs[i] + config_filename).isDirectory() === false) {
32 config = JSON.parse(fs.readFileSync(config_dirs[i] + config_filename, 'ascii'));
33 console.log('Using config file ' + config_dirs[i] + config_filename);
34 break;
35 }
36 } catch (e) {
37 continue;
38 }
39 }
40
41 if (config === null) {
42 console.log('Couldn\'t find a config file!');
43 process.exit(0);
44 }
45 }());
46
47
48 /*
49 * Load the modules as set in the config and print them out
50 */
51 var kiwi_mod = require('./lib/kiwi_mod.js');
52 kiwi_mod.loadModules(kiwi_root, config);
53 kiwi_mod.printMods();
54
55
56
57
58 /*
59 * Some process changes
60 */
61 process.title = 'kiwiirc';
62 var changeUser = function () {
63 if (typeof config.group !== 'undefined' && config.group !== '') {
64 try {
65 process.setgid(config.group);
66 } catch (err) {
67 console.log('Failed to set gid: ' + err);
68 process.exit();
69 }
70 }
71
72 if (typeof config.user !== 'undefined' && config.user !== '') {
73 try {
74 process.setuid(config.user);
75 } catch (e) {
76 console.log('Failed to set uid: ' + e);
77 process.exit();
78 }
79 }
80 };
81
82
83 /*
84 * And now KiwiIRC, the server :)
85 *
86 */
87
88 var ircNumerics = {
89 RPL_WELCOME: '001',
90 RPL_ISUPPORT: '005',
91 RPL_WHOISUSER: '311',
92 RPL_WHOISSERVER: '312',
93 RPL_WHOISOPERATOR: '313',
94 RPL_WHOISIDLE: '317',
95 RPL_ENDOFWHOIS: '318',
96 RPL_WHOISCHANNELS: '319',
97 RPL_NOTOPIC: '331',
98 RPL_TOPIC: '332',
99 RPL_NAMEREPLY: '353',
100 RPL_ENDOFNAMES: '366',
101 RPL_MOTD: '372',
102 RPL_WHOISMODES: '379',
103 ERR_NOSUCHNICK: '401',
104 ERR_CANNOTSENDTOCHAN: '404',
105 ERR_TOOMANYCHANNELS: '405',
106 ERR_USERNOTINCHANNEL: '441',
107 ERR_NOTONCHANNEL: '442',
108 ERR_LINKCHANNEL: '470',
109 ERR_CHANNELISFULL: '471',
110 ERR_INVITEONLYCHAN: '473',
111 ERR_BANNEDFROMCHAN: '474',
112 ERR_BADCHANNELKEY: '475',
113 ERR_CHANOPRIVSNEEDED: '482',
114 RPL_STARTTLS: '670'
115 };
116
117
118
119
120 var parseIRCMessage = function (websocket, ircSocket, data) {
121 /*global ircSocketDataHandler */
122 var msg, regex, opts, options, opt, i, j, matches, nick, users, chan, params, prefix, prefixes, nicklist, caps, rtn, obj;
123 regex = /^(?::(?:([a-z0-9\x5B-\x60\x7B-\x7D\.\-]+)|([a-z0-9\x5B-\x60\x7B-\x7D\.\-]+)!([a-z0-9~\.\-_|]+)@?([a-z0-9\.\-:]+)?) )?([a-z0-9]+)(?:(?: ([^:]+))?(?: :(.+))?)$/i;
124 msg = regex.exec(data);
125 if (msg) {
126 msg = {
127 prefix: msg[1],
128 nick: msg[2],
129 ident: msg[3],
130 hostname: (msg[4]) ? msg[4] : '',
131 command: msg[5],
132 params: msg[6] || '',
133 trailing: (msg[7]) ? msg[7].trim() : ''
134 };
135 switch (msg.command.toUpperCase()) {
136 case 'PING':
137 websocket.sendServerLine('PONG ' + msg.trailing);
138 break;
139 case ircNumerics.RPL_WELCOME:
140 if (ircSocket.IRC.CAP.negotiating) {
141 ircSocket.IRC.CAP.negotiating = false;
142 ircSocket.IRC.CAP.enabled = [];
143 ircSocket.IRC.CAP.requested = [];
144 }
145 websocket.sendClientEvent('connect', {connected: true, host: null});
146 break;
147 case ircNumerics.RPL_ISUPPORT:
148 opts = msg.params.split(" ");
149 options = [];
150 for (i = 0; i < opts.length; i++) {
151 opt = opts[i].split("=", 2);
152 opt[0] = opt[0].toUpperCase();
153 ircSocket.IRC.options[opt[0]] = opt[1] || true;
154 if (_.include(['NETWORK', 'PREFIX', 'CHANTYPES'], opt[0])) {
155 if (opt[0] === 'PREFIX') {
156 regex = /\(([^)]*)\)(.*)/;
157 matches = regex.exec(opt[1]);
158 if ((matches) && (matches.length === 3)) {
159 ircSocket.IRC.options[opt[0]] = [];
160 for (j = 0; j < matches[2].length; j++) {
161 //ircSocket.IRC.options[opt[0]][matches[2].charAt(j)] = matches[1].charAt(j);
162 ircSocket.IRC.options[opt[0]].push({symbol: matches[2].charAt(j), mode: matches[1].charAt(j)});
163 //console.log({symbol: matches[2].charAt(j), mode: matches[1].charAt(j)});
164 }
165 console.log(ircSocket.IRC.options);
166 }
167 }
168 }
169 }
170 websocket.sendClientEvent('options', {server: '', "options": ircSocket.IRC.options});
171 break;
172 case ircNumerics.RPL_WHOISUSER:
173 case ircNumerics.RPL_WHOISSERVER:
174 case ircNumerics.RPL_WHOISOPERATOR:
175 case ircNumerics.RPL_ENDOFWHOIS:
176 case ircNumerics.RPL_WHOISCHANNELS:
177 case ircNumerics.RPL_WHOISMODES:
178 websocket.sendClientEvent('whois', {server: '', nick: msg.params.split(" ", 3)[1], "msg": msg.trailing});
179 break;
180 case ircNumerics.RPL_WHOISIDLE:
181 params = msg.params.split(" ", 4);
182 rtn = {server: '', nick: params[1], idle: params[2]};
183 if (params[3]) {
184 rtn.logon = params[3];
185 }
186 websocket.sendClientEvent('whois', rtn);
187 break;
188 case ircNumerics.RPL_MOTD:
189 websocket.sendClientEvent('motd', {server: '', "msg": msg.trailing});
190 break;
191 case ircNumerics.RPL_NAMEREPLY:
192 params = msg.params.split(" ");
193 nick = params[0];
194 chan = params[2];
195 users = msg.trailing.split(" ");
196 prefixes = _.values(ircSocket.IRC.options.PREFIX);
197 nicklist = {};
198 i = 0;
199 _.each(users, function (user) {
200 if (_.include(prefix, user.charAt(0))) {
201 prefix = user.charAt(0);
202 user = user.substring(1);
203 nicklist[user] = prefix;
204 } else {
205 nicklist[user] = '';
206 }
207 if (i++ >= 50) {
208 websocket.sendClientEvent('userlist', {server: '', 'users': nicklist, channel: chan});
209 nicklist = {};
210 i = 0;
211 }
212 });
213 if (i > 0) {
214 websocket.sendClientEvent('userlist', {server: '', "users": nicklist, channel: chan});
215 } else {
216 console.log("oops");
217 }
218 break;
219 case ircNumerics.RPL_ENDOFNAMES:
220 websocket.sendClientEvent('userlist_end', {server: '', channel: msg.params.split(" ")[1]});
221 break;
222 case ircNumerics.ERR_LINKCHANNEL:
223 params = msg.params.split(" ");
224 websocket.sendClientEvent('channel_redirect', {from: params[1], to: params[2]});
225 break;
226 case ircNumerics.ERR_NOSUCHNICK:
227 websocket.sendClientEvent('irc_error', {error: 'no_such_nick', nick: msg.params.split(" ")[1], reason: msg.trailing});
228 break;
229 case 'JOIN':
230 // Some BNC's send malformed JOIN causing the channel to be as a
231 // parameter instead of trailing.
232 if(typeof msg.trailing === 'string' && msg.trailing !== ''){
233 channel = msg.trailing;
234 } else if(typeof msg.params === 'string' && msg.params !== ''){
235 channel = msg.params;
236 }
237
238 websocket.sendClientEvent('join', {nick: msg.nick, ident: msg.ident, hostname: msg.hostname, channel: channel});
239 if (msg.nick === ircSocket.IRC.nick) {
240 websocket.sendServerLine('NAMES ' + msg.trailing);
241 }
242 break;
243 case 'PART':
244 websocket.sendClientEvent('part', {nick: msg.nick, ident: msg.ident, hostname: msg.hostname, channel: msg.params.trim(), message: msg.trailing});
245 break;
246 case 'KICK':
247 params = msg.params.split(" ");
248 websocket.sendClientEvent('kick', {kicked: params[1], nick: msg.nick, ident: msg.ident, hostname: msg.hostname, channel: params[0].trim(), message: msg.trailing});
249 break;
250 case 'QUIT':
251 websocket.sendClientEvent('quit', {nick: msg.nick, ident: msg.ident, hostname: msg.hostname, message: msg.trailing});
252 break;
253 case 'NOTICE':
254 if ((msg.trailing.charAt(0) === String.fromCharCode(1)) && (msg.trailing.charAt(msg.trailing.length - 1) === String.fromCharCode(1))) {
255 // It's a CTCP response
256 websocket.sendClientEvent('ctcp_response', {nick: msg.nick, ident: msg.ident, hostname: msg.hostname, channel: msg.params.trim(), msg: msg.trailing.substr(1, msg.trailing.length - 2)});
257 } else {
258 websocket.sendClientEvent('notice', {nick: msg.nick, ident: msg.ident, hostname: msg.hostname, channel: msg.params.trim(), msg: msg.trailing});
259 }
260 break;
261 case 'NICK':
262 websocket.sendClientEvent('nick', {nick: msg.nick, ident: msg.ident, hostname: msg.hostname, newnick: msg.trailing});
263 break;
264 case 'TOPIC':
265 obj = {nick: msg.nick, channel: msg.params, topic: msg.trailing};
266 websocket.sendClientEvent('topic', obj);
267 break;
268 case ircNumerics.RPL_TOPIC:
269 obj = {nick: '', channel: msg.params.split(" ")[1], topic: msg.trailing};
270 websocket.sendClientEvent('topic', obj);
271 break;
272 case ircNumerics.RPL_NOTOPIC:
273 obj = {nick: '', channel: msg.params.split(" ")[1], topic: ''};
274 websocket.sendClientEvent('topic', obj);
275 break;
276 case 'MODE':
277 opts = msg.params.split(" ");
278 params = {nick: msg.nick};
279 switch (opts.length) {
280 case 1:
281 params.effected_nick = opts[0];
282 params.mode = msg.trailing;
283 break;
284 case 2:
285 params.channel = opts[0];
286 params.mode = opts[1];
287 break;
288 default:
289 params.channel = opts[0];
290 params.mode = opts[1];
291 params.effected_nick = opts[2];
292 break;
293 }
294 websocket.sendClientEvent('mode', params);
295 break;
296 case 'PRIVMSG':
297 if ((msg.trailing.charAt(0) === String.fromCharCode(1)) && (msg.trailing.charAt(msg.trailing.length - 1) === String.fromCharCode(1))) {
298 // It's a CTCP request
299 if (msg.trailing.substr(1, 6) === 'ACTION') {
300 websocket.sendClientEvent('action', {nick: msg.nick, ident: msg.ident, hostname: msg.hostname, channel: msg.params.trim(), msg: msg.trailing.substr(7, msg.trailing.length - 2)});
301 } else if (msg.trailing.substr(1, 7) === 'VERSION') {
302 ircSocket.write('NOTICE ' + msg.nick + ' :' + String.fromCharCode(1) + 'VERSION KiwiIRC' + String.fromCharCode(1) + '\r\n');
303 } else {
304 websocket.sendClientEvent('ctcp_request', {nick: msg.nick, ident: msg.ident, hostname: msg.hostname, channel: msg.params.trim(), msg: msg.trailing.substr(1, msg.trailing.length - 2)});
305 }
306 } else {
307 obj = {nick: msg.nick, ident: msg.ident, hostname: msg.hostname, channel: msg.params.trim(), msg: msg.trailing};
308 websocket.sendClientEvent('msg', obj);
309 }
310 break;
311 case 'CAP':
312 caps = config.cap_options;
313 options = msg.trailing.split(" ");
314 switch (_.first(msg.params.split(" "))) {
315 case 'LS':
316 opts = '';
317 _.each(_.intersect(caps, options), function (cap) {
318 if (opts !== '') {
319 opts += " ";
320 }
321 opts += cap;
322 ircSocket.IRC.CAP.requested.push(cap);
323 });
324 if (opts.length > 0) {
325 websocket.sendServerLine('CAP REQ :' + opts);
326 } else {
327 websocket.sendServerLine('CAP END');
328 }
329 // TLS is special
330 /*if (_.include(options, 'tls')) {
331 websocket.sendServerLine('STARTTLS');
332 ircSocket.IRC.CAP.requested.push('tls');
333 }*/
334 break;
335 case 'ACK':
336 _.each(options, function (cap) {
337 ircSocket.IRC.CAP.enabled.push(cap);
338 });
339 if (_.last(msg.params.split(" ")) !== '*') {
340 ircSocket.IRC.CAP.requested = [];
341 ircSocket.IRC.CAP.negotiating = false;
342 websocket.sendServerLine('CAP END');
343 }
344 break;
345 case 'NAK':
346 ircSocket.IRC.CAP.requested = [];
347 ircSocket.IRC.CAP.negotiating = false;
348 websocket.sendServerLine('CAP END');
349 break;
350 }
351 break;
352 /*case ircNumerics.RPL_STARTTLS:
353 try {
354 IRC = ircSocket.IRC;
355 listeners = ircSocket.listeners('data');
356 ircSocket.removeAllListeners('data');
357 ssl_socket = starttls(ircSocket, {}, function () {
358 ssl_socket.on("data", function (data) {
359 ircSocketDataHandler(data, websocket, ssl_socket);
360 });
361 ircSocket = ssl_socket;
362 ircSocket.IRC = IRC;
363 _.each(listeners, function (listener) {
364 ircSocket.addListener('data', listener);
365 });
366 });
367 //console.log(ircSocket);
368 } catch (e) {
369 console.log(e);
370 }
371 break;*/
372 case ircNumerics.ERR_CANNOTSENDTOCHAN:
373 websocket.sendClientEvent('irc_error', {error: 'cannot_send_to_chan', channel: msg.params.split(" ")[1], reason: msg.trailing});
374 break;
375 case ircNumerics.ERR_TOOMANYCHANNELS:
376 websocket.sendClientEvent('irc_error', {error: 'too_many_channels', channel: msg.params.split(" ")[1], reason: msg.trailing});
377 break;
378 case ircNumerics.ERR_USERNOTINCHANNEL:
379 params = msg.params.split(" ");
380 websocket.sendClientEvent('irc_error', {error: 'user_not_in_channel', nick: params[0], channel: params[1], reason: msg.trainling});
381 break;
382 case ircNumerics.ERR_NOTONCHANNEL:
383 websocket.sendClientEvent('irc_error', {error: 'not_on_channel', channel: msg.params.split(" ")[1], reason: msg.trailing});
384 break;
385 case ircNumerics.ERR_CHANNELISFULL:
386 websocket.sendClientEvent('irc_error', {error: 'channel_is_full', channel: msg.params.split(" ")[1], reason: msg.trailing});
387 break;
388 case ircNumerics.ERR_INVITEONLYCHAN:
389 websocket.sendClientEvent('irc_error', {error: 'invite_only_channel', channel: msg.params.split(" ")[1], reason: msg.trailing});
390 break;
391 case ircNumerics.ERR_BANNEDFROMCHAN:
392 websocket.sendClientEvent('irc_error', {error: 'banned_from_channel', channel: msg.params.split(" ")[1], reason: msg.trailing});
393 break;
394 case ircNumerics.ERR_BADCHANNELKEY:
395 websocket.sendClientEvent('irc_error', {error: 'bad_channel_key', channel: msg.params.split(" ")[1], reason: msg.trailing});
396 break;
397 case ircNumerics.ERR_CHANOPRIVSNEEDED:
398 websocket.sendClientEvent('irc_error', {error: 'chanop_privs_needed', channel: msg.params.split(" ")[1], reason: msg.trailing});
399 break;
400
401 default:
402 console.log("Unknown command (" + String(msg.command).toUpperCase() + ")");
403 }
404 } else {
405 console.log("Malformed IRC line");
406 }
407 };
408
409
410 /*
411 * NOTE: Some IRC servers or BNC's out there incorrectly use
412 * only \n as a line splitter.
413 */
414 var ircSocketDataHandler = function (data, websocket, ircSocket) {
415 var i;
416 if ((ircSocket.holdLast) && (ircSocket.held !== '')) {
417 data = ircSocket.held + data;
418 ircSocket.holdLast = false;
419 ircSocket.held = '';
420 }
421 if (data.substr(-2) === '\n') {
422 ircSocket.holdLast = true;
423 }
424 data = data.split("\n");
425 for (i = 0; i < data.length; i++) {
426 if (data[i]) {
427 if ((ircSocket.holdLast) && (i === data.length - 1)) {
428 ircSocket.held = data[i];
429 break;
430 }
431 console.log("->" + data[i]);
432 parseIRCMessage(websocket, ircSocket, data[i].replace(/^\r+|\r+$/, '') );
433 }
434 }
435 };
436
437 if (config.handle_http) {
438 var fileServer = new (require('node-static').Server)(__dirname + config.public_http);
439 var jade = require('jade');
440 }
441
442 var httpHandler = function (request, response) {
443 var uri, subs, useragent, agent, server_set, server, nick, debug, touchscreen;
444 if (config.handle_http) {
445 uri = url.parse(request.url);
446 subs = uri.pathname.substr(0, 4);
447 if ((subs === '/js/') || (subs === '/css') || (subs === '/img')) {
448 request.addListener('end', function () {
449 fileServer.serve(request, response);
450 });
451 } else if (uri.pathname === '/') {
452 useragent = (response.headers) ? response.headers['user-agent'] : '';
453 if (useragent.indexOf('android') !== -1) {
454 agent = 'android';
455 touchscreen = true;
456 } else if (useragent.indexOf('iphone') !== -1) {
457 agent = 'iphone';
458 touchscreen = true;
459 } else if (useragent.indexOf('ipad') !== -1) {
460 agent = 'ipad';
461 touchscreen = true;
462 } else if (useragent.indexOf('ipod') !== -1) {
463 agent = 'ipod';
464 touchscreen = true;
465 } else {
466 agent = 'normal';
467 touchscreen = false;
468 }
469 if (uri.query) {
470 server_set = (uri.query.server !== '');
471 server = uri.query.server || 'irc.anonnet.org';
472 nick = uri.query.nick || '';
473 debug = (uri.query.debug !== '');
474 } else {
475 server = 'irc.anonnet.org';
476 nick = '';
477 }
478 response.setHeader('Connection', 'close');
479 response.setHeader('X-Generated-By', 'KiwiIRC');
480 jade.renderFile(__dirname + '/client/index.html.jade', { locals: { "touchscreen": touchscreen, "debug": debug, "server_set": server_set, "server": server, "nick": nick, "agent": agent, "config": config }}, function (err, html) {
481 if (!err) {
482 response.write(html);
483 } else {
484 response.statusCode = 500;
485 }
486 response.end();
487 });
488 } else if (uri.pathname.substr(0, 10) === '/socket.io') {
489 return;
490 } else {
491 response.statusCode = 404;
492 response.end();
493 }
494 }
495 };
496
497 //setup websocket listener
498 if (config.listen_ssl) {
499 var httpServer = https.createServer({key: fs.readFileSync(__dirname + '/' + config.ssl_key), cert: fs.readFileSync(__dirname + '/' + config.ssl_cert)}, httpHandler);
500 var io = ws.listen(httpServer, {secure: true});
501 httpServer.listen(config.port, config.bind_address);
502 } else {
503 var httpServer = http.createServer(httpHandler);
504 var io = ws.listen(httpServer, {secure: false});
505 httpServer.listen(config.port, config.bind_address);
506 }
507 io.set('log level', 1);
508
509 // Now we're listening on the network, set our UID/GIDs if required
510 changeUser();
511
512 var connections = {};
513
514 // The main socket listening/handling routines
515 io.of('/kiwi').authorization(function (handshakeData, callback) {
516 var connection, address = handshakeData.address.address;
517 if (typeof connections[address] === 'undefined') {
518 connections[address] = {count: 0, sockets: []};
519 }
520 callback(null, true);
521 }).on('connection', function (websocket) {
522 var con;
523 websocket.kiwi = {address: websocket.handshake.address.address};
524 con = connections[websocket.kiwi.address];
525 if (con.count >= config.max_client_conns) {
526 websocket.emit('too_many_connections');
527 websocket.disconnect();
528 } else {
529 con.count += 1
530 con.sockets.push(websocket);
531
532 websocket.sendClientEvent = function (event_name, data) {
533 kiwi_mod.run(event_name, data, {websocket: this});
534 data.event = event_name;
535 websocket.emit('message', data);
536 };
537
538 websocket.sendServerLine = function (data, eol) {
539 eol = (typeof eol === 'undefined') ? '\r\n' : eol;
540 //console.log('Out: -----\n' + data + '\n-----');
541 websocket.ircSocket.write(data + eol);
542 };
543
544 websocket.on('irc connect', function (nick, host, port, ssl, callback) {
545 var ircSocket;
546 //setup IRC connection
547 if (!ssl) {
548 ircSocket = net.createConnection(port, host);
549 } else {
550 ircSocket = tls.connect(port, host);
551 }
552 ircSocket.setEncoding('ascii');
553 ircSocket.IRC = {options: {}, CAP: {negotiating: true, requested: [], enabled: []}};
554 websocket.ircSocket = ircSocket;
555 ircSocket.holdLast = false;
556 ircSocket.held = '';
557
558 ircSocket.on('data', function (data) {
559 //console.log('In: -----\n' + data + '\n-----');
560 ircSocketDataHandler(data, websocket, ircSocket);
561 });
562
563 ircSocket.IRC.nick = nick;
564 // Send the login data
565 websocket.sendServerLine('CAP LS');
566 websocket.sendServerLine('NICK ' + nick);
567 websocket.sendServerLine('USER ' + nick + '_kiwi 0 0 :' + nick);
568
569 if ((callback) && (typeof (callback) === 'function')) {
570 callback();
571 }
572 });
573 websocket.on('message', function (msg, callback) {
574 var args, obj;
575 try {
576 msg.data = JSON.parse(msg.data);
577 args = msg.data.args;
578 switch (msg.data.method) {
579 case 'msg':
580 if ((args.target) && (args.msg)) {
581 obj = kiwi_mod.run('msgsend', args, {websocket: websocket});
582 if (obj !== null) {
583 websocket.sendServerLine('PRIVMSG ' + args.target + ' :' + args.msg);
584 }
585 }
586 break;
587 case 'action':
588 if ((args.target) && (args.msg)) {
589 websocket.sendServerLine('PRIVMSG ' + args.target + ' :\ 1ACTION ' + args.msg);
590 }
591 break;
592 case 'raw':
593 websocket.sendServerLine(args.data);
594 break;
595 case 'join':
596 if (args.channel) {
597 _.each(args.channel.split(","), function (chan) {
598 websocket.sendServerLine('JOIN ' + chan);
599 });
600 }
601 break;
602 case 'topic':
603 if (args.channel) {
604 websocket.sendServerLine('TOPIC ' + args.channel + ' :' + args.topic);
605 }
606 break;
607 case 'quit':
608 websocket.ircSocket.end('QUIT :' + args.message + '\r\n');
609 websocket.sentQUIT = true;
610 websocket.ircSocket.destroySoon();
611 websocket.disconnect();
612 break;
613 case 'notice':
614 if ((args.target) && (args.msg)) {
615 websocket.sendServerLine('NOTICE ' + args.target + ' :' + args.msg);
616 }
617 break;
618 default:
619 }
620 if ((callback) && (typeof (callback) === 'function')) {
621 callback();
622 }
623 } catch (e) {
624 console.log("Caught error: " + e);
625 }
626 });
627
628
629 websocket.on('disconnect', function () {
630 if ((!websocket.sentQUIT) && (websocket.ircSocket)) {
631 websocket.ircSocket.end('QUIT :' + config.quit_message + '\r\n');
632 websocket.sentQUIT = true;
633 websocket.ircSocket.destroySoon();
634 }
635 con = connections[websocket.kiwi.address];
636 con.count -=1;
637 con.sockets = _.reject(con.sockets, function (sock) {
638 return sock === websocket;
639 });
640 });
641 }
642 });
643