16 this.init = function (objs
) {
29 starttls
= objs
.starttls
;
30 kiwi
= require('./kiwi.js');
39 * Some process changes
41 this.setTitle = function () {
42 process
.title
= 'kiwiirc';
45 this.changeUser = function () {
46 if (typeof kiwi
.config
.group
!== 'undefined' && kiwi
.config
.group
!== '') {
48 process
.setgid(kiwi
.config
.group
);
50 console
.log('Failed to set gid: ' + err
);
55 if (typeof kiwi
.config
.user
!== 'undefined' && kiwi
.config
.user
!== '') {
57 process
.setuid(kiwi
.config
.user
);
59 console
.log('Failed to set uid: ' + e
);
78 RPL_WHOISSERVER
: '312',
79 RPL_WHOISOPERATOR
: '313',
81 RPL_ENDOFWHOIS
: '318',
82 RPL_WHOISCHANNELS
: '319',
86 RPL_ENDOFNAMES
: '366',
88 RPL_WHOISMODES
: '379',
89 ERR_NOSUCHNICK
: '401',
90 ERR_CANNOTSENDTOCHAN
: '404',
91 ERR_TOOMANYCHANNELS
: '405',
92 ERR_NICKNAMEINUSE
: '433',
93 ERR_USERNOTINCHANNEL
: '441',
94 ERR_NOTONCHANNEL
: '442',
95 ERR_NOTREGISTERED
: '451',
96 ERR_LINKCHANNEL
: '470',
97 ERR_CHANNELISFULL
: '471',
98 ERR_INVITEONLYCHAN
: '473',
99 ERR_BANNEDFROMCHAN
: '474',
100 ERR_BADCHANNELKEY
: '475',
101 ERR_CHANOPRIVSNEEDED
: '482',
107 this.parseIRCMessage = function (websocket
, ircSocket
, data
) {
108 /*global ircSocketDataHandler */
109 var msg
, regex
, opts
, options
, opt
, i
, j
, matches
, nick
, users
, chan
, channel
, params
, prefix
, prefixes
, nicklist
, caps
, rtn
, obj
;
110 //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;
111 //regex = /^(?::(\S+) )?(\S+)(?: (?!:)(.+?))?(?: :(.+))?$/i;
112 regex
= /^(?::(?:([a-z0-9\x5B-\x60\x7B-\x7D\.\-]+)|([a-z0-9\x5B-\x60\x7B-\x7D\.\-]+)!([a-z0-9~\.\-_|]+)@?([a-z0-9\.\-:\/]+)?) )?(\S+)(?: (?!:)(.+?))?(?: :(.+))?$/i;
114 msg
= regex
.exec(data
);
120 hostname
: msg
[4] || '',
122 params
: msg
[6] || '',
123 trailing
: (msg
[7]) ? msg
[7].trim() : ''
126 switch (msg
.command
.toUpperCase()) {
128 websocket
.sendServerLine('PONG ' + msg
.trailing
);
130 case ircNumerics
.RPL_WELCOME
:
131 if (ircSocket
.IRC
.CAP
.negotiating
) {
132 ircSocket
.IRC
.CAP
.negotiating
= false;
133 ircSocket
.IRC
.CAP
.enabled
= [];
134 ircSocket
.IRC
.CAP
.requested
= [];
135 ircSocket
.IRC
.registered
= true;
137 //regex = /([a-z0-9\x5B-\x60\x7B-\x7D\.\-]+)!([a-z0-9~\.\-_|]+)@?([a-z0-9\.\-:\/]+)/i;
138 //matches = regex.exec(msg.trailing);
139 nick
= msg
.params
.split(' ')[0];
140 websocket
.sendClientEvent('connect', {connected
: true, host
: null, nick
: nick
});
142 case ircNumerics
.RPL_ISUPPORT
:
143 opts
= msg
.params
.split(" ");
145 for (i
= 0; i
< opts
.length
; i
++) {
146 opt
= opts
[i
].split("=", 2);
147 opt
[0] = opt
[0].toUpperCase();
148 ircSocket
.IRC
.options
[opt
[0]] = (typeof opt
[1] !== 'undefined') ? opt
[1] : true;
149 if (_
.include(['NETWORK', 'PREFIX', 'CHANTYPES'], opt
[0])) {
150 if (opt
[0] === 'PREFIX') {
151 regex
= /\(([^)]*)\)(.*)/;
152 matches
= regex
.exec(opt
[1]);
153 if ((matches
) && (matches
.length
=== 3)) {
154 ircSocket
.IRC
.options
[opt
[0]] = [];
155 for (j
= 0; j
< matches
[2].length
; j
++) {
156 //ircSocket.IRC.options[opt[0]][matches[2].charAt(j)] = matches[1].charAt(j);
157 ircSocket
.IRC
.options
[opt
[0]].push({symbol
: matches
[2].charAt(j
), mode
: matches
[1].charAt(j
)});
165 websocket
.sendClientEvent('options', {server
: '', "options": ircSocket
.IRC
.options
});
167 case ircNumerics
.RPL_WHOISUSER
:
168 case ircNumerics
.RPL_WHOISSERVER
:
169 case ircNumerics
.RPL_WHOISOPERATOR
:
170 case ircNumerics
.RPL_ENDOFWHOIS
:
171 case ircNumerics
.RPL_WHOISCHANNELS
:
172 case ircNumerics
.RPL_WHOISMODES
:
173 websocket
.sendClientEvent('whois', {server
: '', nick
: msg
.params
.split(" ", 3)[1], "msg": msg
.trailing
});
175 case ircNumerics
.RPL_WHOISIDLE
:
176 params
= msg
.params
.split(" ", 4);
177 rtn
= {server
: '', nick
: params
[1], idle
: params
[2]};
179 rtn
.logon
= params
[3];
181 websocket
.sendClientEvent('whois', rtn
);
183 case ircNumerics
.RPL_MOTD
:
184 websocket
.sendClientEvent('motd', {server
: '', "msg": msg
.trailing
});
186 case ircNumerics
.RPL_NAMEREPLY
:
187 params
= msg
.params
.split(" ");
190 users
= msg
.trailing
.split(" ");
191 prefixes
= _
.values(ircSocket
.IRC
.options
.PREFIX
);
194 _
.each(users
, function (user
) {
195 if (_
.include(prefix
, user
.charAt(0))) {
196 prefix
= user
.charAt(0);
197 user
= user
.substring(1);
198 nicklist
[user
] = prefix
;
203 websocket
.sendClientEvent('userlist', {server
: '', 'users': nicklist
, channel
: chan
});
209 websocket
.sendClientEvent('userlist', {server
: '', "users": nicklist
, channel
: chan
});
214 case ircNumerics
.RPL_ENDOFNAMES
:
215 websocket
.sendClientEvent('userlist_end', {server
: '', channel
: msg
.params
.split(" ")[1]});
217 case ircNumerics
.ERR_LINKCHANNEL
:
218 params
= msg
.params
.split(" ");
219 websocket
.sendClientEvent('channel_redirect', {from: params
[1], to
: params
[2]});
221 case ircNumerics
.ERR_NOSUCHNICK
:
222 websocket
.sendClientEvent('irc_error', {error
: 'no_such_nick', nick
: msg
.params
.split(" ")[1], reason
: msg
.trailing
});
225 // Some BNC's send malformed JOIN causing the channel to be as a
226 // parameter instead of trailing.
227 if (typeof msg
.trailing
=== 'string' && msg
.trailing
!== '') {
228 channel
= msg
.trailing
;
229 } else if (typeof msg
.params
=== 'string' && msg
.params
!== '') {
230 channel
= msg
.params
;
233 websocket
.sendClientEvent('join', {nick
: msg
.nick
, ident
: msg
.ident
, hostname
: msg
.hostname
, channel
: channel
});
234 if (msg
.nick
=== ircSocket
.IRC
.nick
) {
235 websocket
.sendServerLine('NAMES ' + msg
.trailing
);
239 websocket
.sendClientEvent('part', {nick
: msg
.nick
, ident
: msg
.ident
, hostname
: msg
.hostname
, channel
: msg
.params
.trim(), message
: msg
.trailing
});
242 params
= msg
.params
.split(" ");
243 websocket
.sendClientEvent('kick', {kicked
: params
[1], nick
: msg
.nick
, ident
: msg
.ident
, hostname
: msg
.hostname
, channel
: params
[0].trim(), message
: msg
.trailing
});
246 websocket
.sendClientEvent('quit', {nick
: msg
.nick
, ident
: msg
.ident
, hostname
: msg
.hostname
, message
: msg
.trailing
});
249 if ((msg
.trailing
.charAt(0) === String
.fromCharCode(1)) && (msg
.trailing
.charAt(msg
.trailing
.length
- 1) === String
.fromCharCode(1))) {
250 // It's a CTCP response
251 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)});
253 websocket
.sendClientEvent('notice', {nick
: msg
.nick
, ident
: msg
.ident
, hostname
: msg
.hostname
, target
: msg
.params
.trim(), msg
: msg
.trailing
});
257 websocket
.sendClientEvent('nick', {nick
: msg
.nick
, ident
: msg
.ident
, hostname
: msg
.hostname
, newnick
: msg
.trailing
});
260 obj
= {nick
: msg
.nick
, channel
: msg
.params
, topic
: msg
.trailing
};
261 websocket
.sendClientEvent('topic', obj
);
263 case ircNumerics
.RPL_TOPIC
:
264 obj
= {nick
: '', channel
: msg
.params
.split(" ")[1], topic
: msg
.trailing
};
265 websocket
.sendClientEvent('topic', obj
);
267 case ircNumerics
.RPL_NOTOPIC
:
268 obj
= {nick
: '', channel
: msg
.params
.split(" ")[1], topic
: ''};
269 websocket
.sendClientEvent('topic', obj
);
272 opts
= msg
.params
.split(" ");
273 params
= {nick
: msg
.nick
};
274 switch (opts
.length
) {
276 params
.effected_nick
= opts
[0];
277 params
.mode
= msg
.trailing
;
280 params
.channel
= opts
[0];
281 params
.mode
= opts
[1];
284 params
.channel
= opts
[0];
285 params
.mode
= opts
[1];
286 params
.effected_nick
= opts
[2];
289 websocket
.sendClientEvent('mode', params
);
292 if ((msg
.trailing
.charAt(0) === String
.fromCharCode(1)) && (msg
.trailing
.charAt(msg
.trailing
.length
- 1) === String
.fromCharCode(1))) {
293 // It's a CTCP request
294 if (msg
.trailing
.substr(1, 6) === 'ACTION') {
295 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)});
296 } else if (msg
.trailing
.substr(1, 4) === 'KIWI') {
297 var tmp
= msg
.trailing
.substr(6, msg
.trailing
.length
- 2);
298 var namespace = tmp
.split(' ', 1)[0];
299 websocket
.sendClientEvent('kiwi', {namespace: namespace, data
: tmp
.substr(namespace.length
+1)});
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');
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)});
307 obj
= {nick
: msg
.nick
, ident
: msg
.ident
, hostname
: msg
.hostname
, channel
: msg
.params
.trim(), msg
: msg
.trailing
};
308 websocket
.sendClientEvent('msg', obj
);
312 caps
= kiwi
.config
.cap_options
;
313 options
= msg
.trailing
.split(" ");
314 switch (_
.last(msg
.params
.split(" "))) {
317 _
.each(_
.intersect(caps
, options
), function (cap
) {
322 ircSocket
.IRC
.CAP
.requested
.push(cap
);
324 if (opts
.length
> 0) {
325 websocket
.sendServerLine('CAP REQ :' + opts
);
327 websocket
.sendServerLine('CAP END');
330 /*if (_.include(options, 'tls')) {
331 websocket.sendServerLine('STARTTLS');
332 ircSocket.IRC.CAP.requested.push('tls');
336 _
.each(options
, function (cap
) {
337 ircSocket
.IRC
.CAP
.enabled
.push(cap
);
339 if (_
.last(msg
.params
.split(" ")) !== '*') {
340 ircSocket
.IRC
.CAP
.requested
= [];
341 ircSocket
.IRC
.CAP
.negotiating
= false;
342 websocket
.sendServerLine('CAP END');
346 ircSocket
.IRC
.CAP
.requested
= [];
347 ircSocket
.IRC
.CAP
.negotiating
= false;
348 websocket
.sendServerLine('CAP END');
352 /*case ircNumerics.RPL_STARTTLS:
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);
361 ircSocket = ssl_socket;
363 _.each(listeners, function (listener) {
364 ircSocket.addListener('data', listener);
367 //console.log(ircSocket);
372 case ircNumerics
.ERR_CANNOTSENDTOCHAN
:
373 websocket
.sendClientEvent('irc_error', {error
: 'cannot_send_to_chan', channel
: msg
.params
.split(" ")[1], reason
: msg
.trailing
});
375 case ircNumerics
.ERR_TOOMANYCHANNELS
:
376 websocket
.sendClientEvent('irc_error', {error
: 'too_many_channels', channel
: msg
.params
.split(" ")[1], reason
: msg
.trailing
});
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
});
382 case ircNumerics
.ERR_NOTONCHANNEL
:
383 websocket
.sendClientEvent('irc_error', {error
: 'not_on_channel', channel
: msg
.params
.split(" ")[1], reason
: msg
.trailing
});
385 case ircNumerics
.ERR_CHANNELISFULL
:
386 websocket
.sendClientEvent('irc_error', {error
: 'channel_is_full', channel
: msg
.params
.split(" ")[1], reason
: msg
.trailing
});
388 case ircNumerics
.ERR_INVITEONLYCHAN
:
389 websocket
.sendClientEvent('irc_error', {error
: 'invite_only_channel', channel
: msg
.params
.split(" ")[1], reason
: msg
.trailing
});
391 case ircNumerics
.ERR_BANNEDFROMCHAN
:
392 websocket
.sendClientEvent('irc_error', {error
: 'banned_from_channel', channel
: msg
.params
.split(" ")[1], reason
: msg
.trailing
});
394 case ircNumerics
.ERR_BADCHANNELKEY
:
395 websocket
.sendClientEvent('irc_error', {error
: 'bad_channel_key', channel
: msg
.params
.split(" ")[1], reason
: msg
.trailing
});
397 case ircNumerics
.ERR_CHANOPRIVSNEEDED
:
398 websocket
.sendClientEvent('irc_error', {error
: 'chanop_privs_needed', channel
: msg
.params
.split(" ")[1], reason
: msg
.trailing
});
400 case ircNumerics
.ERR_NICKNAMEINUSE
:
401 websocket
.sendClientEvent('irc_error', {error
: 'nickname_in_use', nick
: _
.last(msg
.params
.split(" ")), reason
: msg
.trailing
});
405 websocket
.sendClientEvent('irc_error', {error
: 'error', reason
: msg
.trailing
});
406 websocket
.disconnect();
408 case ircNumerics
.ERR_NOTREGISTERED
:
409 if (ircSocket
.IRC
.registered
) {
410 console
.log('Kiwi thinks user is registered, but the IRC server thinks differently');
414 console
.log("Unknown command (" + String(msg
.command
).toUpperCase() + ")");
417 console
.log("Malformed IRC line: " + data
);
427 * NOTE: Some IRC servers or BNC's out there incorrectly use
428 * only \n as a line splitter.
430 this.ircSocketDataHandler = function (data
, websocket
, ircSocket
) {
432 if ((ircSocket
.holdLast
) && (ircSocket
.held
!== '')) {
433 data
= ircSocket
.held
+ data
;
434 ircSocket
.holdLast
= false;
437 if (data
.substr(-1) !== '\n') {
438 ircSocket
.holdLast
= true;
440 data
= data
.split("\n");
441 for (i
= 0; i
< data
.length
; i
++) {
443 if ((ircSocket
.holdLast
) && (i
=== data
.length
- 1)) {
444 ircSocket
.held
= data
[i
];
448 // We have a complete line of data, parse it!
449 kiwi
.parseIRCMessage(websocket
, ircSocket
, data
[i
].replace(/^\r+|\r+$/, ''));
458 this.httpHandler = function (request
, response
) {
459 var uri
, uri_parts
, subs
, useragent
, agent
, server_set
, server
, nick
, debug
, touchscreen
, hash
,
460 min
= {}, public_http_path
;
461 if (kiwi
.config
.handle_http
) {
462 uri
= url
.parse(request
.url
, true);
463 uri_parts
= uri
.pathname
.split('/');
465 subs
= uri
.pathname
.substr(0, 4);
466 if (uri
.pathname
=== '/js/all.js') {
467 if (kiwi
.cache
.alljs
=== '') {
468 public_http_path
= kiwi
.kiwi_root
+ '/' + kiwi
.config
.public_http
;
470 min
.util
= fs
.readFileSync(public_http_path
+ 'js/util.js');
471 min
.gateway
= fs
.readFileSync(public_http_path
+ 'js/gateway.js');
472 min
.front
= fs
.readFileSync(public_http_path
+ 'js/front.js');
473 min
.iscroll
= fs
.readFileSync(public_http_path
+ 'js/iscroll.js');
474 min
.ast
= jsp
.parse(min
.util
+ min
.gateway
+ min
.front
+ min
.iscroll
);
475 min
.ast
= pro
.ast_mangle(min
.ast
);
476 min
.ast
= pro
.ast_squeeze(min
.ast
);
477 min
.final_code
= pro
.gen_code(min
.ast
);
478 kiwi
.cache
.alljs
= min
.final_code
;
479 hash
= crypto
.createHash('md5').update(kiwi
.cache
.alljs
);
480 kiwi
.cache
.alljs_hash
= hash
.digest('base64');
482 if (request
.headers
['if-none-match'] === kiwi
.cache
.alljs_hash
) {
483 response
.statusCode
= 304;
485 response
.setHeader('ETag', kiwi
.cache
.alljs_hash
);
486 response
.write(kiwi
.cache
.alljs
);
489 } else if ((subs
=== '/js/') || (subs
=== '/css') || (subs
=== '/img')) {
490 request
.addListener('end', function () {
491 kiwi
.fileServer
.serve(request
, response
);
493 } else if (uri
.pathname
=== '/' || uri_parts
[1] === 'client') {
494 useragent
= (typeof request
.headers
=== 'string') ? request
.headers
['user-agent'] : '';
495 if (useragent
.match(/android/i) !== -1) {
498 } else if (useragent
.match(/iphone/) !== -1) {
501 } else if (useragent
.match(/ipad/) !== -1) {
504 } else if (useragent
.match(/ipod/) !== -1) {
514 if (uri_parts
[1] !== 'client') {
516 server_set
= ((typeof uri
.query
.server
!== 'undefined') && (uri
.query
.server
!== ''));
517 server
= uri
.query
.server
|| 'irc.anonnet.org';
518 nick
= uri
.query
.nick
|| '';
519 debug
= (uri
.query
.debug
!== '');
522 server
= 'irc.anonnet.org';
526 server_set
= ((typeof uri_parts
[2] !== 'undefined') && (uri_parts
[2] !== ''));
527 server
= server_set
? uri_parts
[2] : 'irc.anonnet.org';
528 nick
= uri
.query
.nick
|| '';
531 response
.setHeader('X-Generated-By', 'KiwiIRC');
532 hash
= crypto
.createHash('md5').update(touchscreen
? 't' : 'f').update(debug
? 't' : 'f').update(server_set
? 't' : 'f').update(server
).update(nick
).update(agent
).update(JSON
.stringify(kiwi
.config
)).digest('base64');
533 if (kiwi
.cache
.html
[hash
]) {
534 if (request
.headers
['if-none-match'] === kiwi
.cache
.html
[hash
].hash
) {
535 response
.statusCode
= 304;
537 response
.setHeader('Etag', kiwi
.cache
.html
[hash
].hash
);
538 response
.write(kiwi
.cache
.html
[hash
].html
);
542 kiwi
.jade
.renderFile(__dirname
+ '/client/index.html.jade', { locals
: { "touchscreen": touchscreen
, "debug": debug
, "server_set": server_set
, "server": server
, "nick": nick
, "agent": agent
, "config": kiwi
.config
}}, function (err
, html
) {
544 var hash2
= crypto
.createHash('md5').update(html
).digest('base64');
545 kiwi
.cache
.html
[hash
] = {"html": html
, "hash": hash2
};
546 if (request
.headers
['if-none-match'] === hash2
) {
547 response
.statusCode
= 304;
549 response
.setHeader('Etag', hash2
);
550 response
.write(html
);
553 response
.statusCode
= 500;
558 } else if (uri
.pathname
.substr(0, 10) === '/socket.io') {
561 response
.statusCode
= 404;
570 this.websocketListen = function (port
, host
, handler
, secure
, key
, cert
) {
571 if (kiwi
.httpServer
) {
572 kiwi
.httpServer
.close();
575 kiwi
.httpServer
= https
.createServer({key
: fs
.readFileSync(__dirname
+ '/' + key
), cert
: fs
.readFileSync(__dirname
+ '/' + cert
)}, handler
);
576 kiwi
.io
= ws
.listen(kiwi
.httpServer
, {secure
: true});
577 kiwi
.httpServer
.listen(port
, host
);
579 kiwi
.httpServer
= http
.createServer(handler
);
580 kiwi
.io
= ws
.listen(kiwi
.httpServer
, {secure
: false});
581 kiwi
.httpServer
.listen(port
, host
);
584 kiwi
.io
.set('log level', 1);
585 kiwi
.io
.enable('browser client minification');
586 kiwi
.io
.enable('browser client etag');
587 kiwi
.io
.set('transports', kiwi
.config
.transports
);
589 kiwi
.io
.of('/kiwi').authorization(function (handshakeData
, callback
) {
590 var address
= handshakeData
.address
.address
;
592 if (typeof kiwi
.connections
[address
] === 'undefined') {
593 kiwi
.connections
[address
] = {count
: 0, sockets
: []};
595 callback(null, true);
596 }).on('connection', kiwi
.websocketConnection
);
604 this.websocketConnection = function (websocket
) {
606 websocket
.kiwi
= {address
: websocket
.handshake
.address
.address
};
607 con
= kiwi
.connections
[websocket
.kiwi
.address
];
609 if (con
.count
>= kiwi
.config
.max_client_conns
) {
610 websocket
.emit('too_many_connections');
611 websocket
.disconnect();
614 con
.sockets
.push(websocket
);
616 websocket
.sendClientEvent = function (event_name
, data
) {
617 var ev
= kiwi
.kiwi_mod
.run(event_name
, data
, {websocket
: this});
618 if(ev
=== null) return;
620 data
.event
= event_name
;
621 websocket
.emit('message', data
);
624 websocket
.sendServerLine = function (data
, eol
) {
625 eol
= (typeof eol
=== 'undefined') ? '\r\n' : eol
;
628 websocket
.ircSocket
.write(data
+ eol
);
632 websocket
.on('irc connect', kiwi
.websocketIRCConnect
);
633 websocket
.on('message', kiwi
.websocketMessage
);
634 websocket
.on('disconnect', kiwi
.websocketDisconnect
);
642 this.websocketIRCConnect = function (websocket
, nick
, host
, port
, ssl
, callback
) {
644 //setup IRC connection
646 ircSocket
= net
.createConnection(port
, host
);
648 ircSocket
= tls
.connect(port
, host
);
650 ircSocket
.setEncoding('ascii');
651 ircSocket
.IRC
= {options
: {}, CAP
: {negotiating
: true, requested
: [], enabled
: []}, registered
: false};
652 ircSocket
.on('error', function (e
) {
653 if (ircSocket
.IRC
.registered
) {
654 websocket
.emit('disconnect');
656 websocket
.emit('error', e
.message
);
659 websocket
.ircSocket
= ircSocket
;
660 ircSocket
.holdLast
= false;
663 ircSocket
.on('data', function (data
) {
664 kiwi
.ircSocketDataHandler(data
, websocket
, ircSocket
);
667 ircSocket
.IRC
.nick
= nick
;
668 // Send the login data
669 dns
.reverse(websocket
.kiwi
.address
, function (err
, domains
) {
670 //console.log(domains);
671 websocket
.kiwi
.hostname
= (err
) ? websocket
.kiwi
.address
: _
.first(domains
);
672 if ((kiwi
.config
.webirc
) && (kiwi
.config
.webirc_pass
[host
])) {
673 websocket
.sendServerLine('WEBIRC ' + kiwi
.config
.webirc_pass
[host
] + ' KiwiIRC ' + websocket
.kiwi
.hostname
+ ' ' + websocket
.kiwi
.address
);
675 websocket
.sendServerLine('CAP LS');
676 websocket
.sendServerLine('NICK ' + nick
);
677 websocket
.sendServerLine('USER kiwi_' + nick
.replace(/[^0-9a-zA-Z\-_.]/, '') + ' 0 0 :' + nick
);
679 if ((callback
) && (typeof (callback
) === 'function')) {
687 this.websocketMessage = function (websocket
, msg
, callback
) {
690 msg
.data
= JSON
.parse(msg
.data
);
691 args
= msg
.data
.args
;
692 switch (msg
.data
.method
) {
694 if ((args
.target
) && (args
.msg
)) {
695 obj
= kiwi
.kiwi_mod
.run('msgsend', args
, {websocket
: websocket
});
697 websocket
.sendServerLine('PRIVMSG ' + args
.target
+ ' :' + args
.msg
);
702 if ((args
.target
) && (args
.msg
)) {
703 websocket
.sendServerLine('PRIVMSG ' + args
.target
+ ' :\001ACTION ' + args
.msg
+ '\001');
708 if ((args
.target
) && (args
.data
)) {
709 websocket
.sendServerLine('PRIVMSG ' + args
.target
+ ' :\001KIWI ' + args
.data
+ '\001');
714 websocket
.sendServerLine(args
.data
);
718 _
.each(args
.channel
.split(","), function (chan
) {
719 websocket
.sendServerLine('JOIN ' + chan
);
725 websocket
.sendServerLine('TOPIC ' + args
.channel
+ ' :' + args
.topic
);
729 websocket
.ircSocket
.end('QUIT :' + args
.message
+ '\r\n');
730 websocket
.sentQUIT
= true;
731 websocket
.ircSocket
.destroySoon();
732 websocket
.disconnect();
735 if ((args
.target
) && (args
.msg
)) {
736 websocket
.sendServerLine('NOTICE ' + args
.target
+ ' :' + args
.msg
);
741 if ((callback
) && (typeof (callback
) === 'function')) {
745 console
.log("Caught error: " + e
);
751 this.websocketDisconnect = function (websocket
) {
754 if ((!websocket
.sentQUIT
) && (websocket
.ircSocket
)) {
756 websocket
.ircSocket
.end('QUIT :' + kiwi
.config
.quit_message
+ '\r\n');
757 websocket
.sentQUIT
= true;
758 websocket
.ircSocket
.destroySoon();
762 con
= kiwi
.connections
[websocket
.kiwi
.address
];
764 con
.sockets
= _
.reject(con
.sockets
, function (sock
) {
765 return sock
=== websocket
;
774 this.rehash = function () {
776 reload_config
= kiwi
.loadConfig();
778 // If loading the new config errored out, dont attempt any changes
779 if (reload_config
=== false) {
783 // We just want the settings that have been changed
784 changes
= reload_config
[1];
786 if (Object
.keys(changes
).length
!== 0) {
787 console
.log('%s config changes: \n', Object
.keys(changes
).length
, changes
);
795 kiwi
.websocketListen(kiwi
.config
.port
, kiwi
.config
.bind_address
, kiwi
.httpHandler
, kiwi
.config
.listen_ssl
, kiwi
.config
.ssl_key
, kiwi
.config
.ssl_cert
);
797 delete changes
.bind_address
;
798 delete changes
.listen_ssl
;
799 delete changes
.ssl_key
;
800 delete changes
.ssl_cert
;
806 delete changes
.group
;
810 kiwi
.kiwi_mod
.loadModules(kiwi_root
, kiwi
.config
);
811 kiwi
.kiwi_mod
.printMods();
812 delete changes
.module_dir
;
813 delete changes
.modules
;
819 // Also clear the kiwi.cached javascript and HTML
820 if (kiwi
.config
.handle_http
) {
821 kiwi
.cache
= {alljs
: '', html
: []};
832 * KiwiIRC controlling via STDIN
834 this.manageControll = function (data
) {
835 var parts
= data
.toString().trim().split(' ');
838 console
.log('Rehashing...');
839 console
.log(kiwi
.rehash() ? 'Rehash complete' : 'Rehash failed');
843 console
.log('Recoding...');
844 console
.log(kiwi
.recode() ? 'Recode complete' : 'Recode failed');
848 if (parts
[1] === 'reload') {
849 console
.log('Reloading module (' + parts
[2] + ')..');
850 kiwi
.kiwi_mod
.reloadModule(parts
[2]);
855 if (parts
[1] === 'clear') {
856 kiwi
.cache
.html
= {};
857 kiwi
.cache
.alljs
= '';
858 console
.log('HTML cache cleared');
863 var connections_cnt
= 0;
864 for (var i
in kiwi
.connections
) {
865 connections_cnt
= connections_cnt
+ parseInt(kiwi
.connections
[i
].count
, 10);
867 console
.log(connections_cnt
.toString() + ' connected clients');
871 console
.log('Unknown command \'' + parts
[0] + '\'');