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 websocket
.sendClientEvent('connect', {connected
: true, host
: null, nick
: matches
[1]});
141 case ircNumerics
.RPL_ISUPPORT
:
142 opts
= msg
.params
.split(" ");
144 for (i
= 0; i
< opts
.length
; i
++) {
145 opt
= opts
[i
].split("=", 2);
146 opt
[0] = opt
[0].toUpperCase();
147 ircSocket
.IRC
.options
[opt
[0]] = (typeof opt
[1] !== 'undefined') ? opt
[1] : true;
148 if (_
.include(['NETWORK', 'PREFIX', 'CHANTYPES'], opt
[0])) {
149 if (opt
[0] === 'PREFIX') {
150 regex
= /\(([^)]*)\)(.*)/;
151 matches
= regex
.exec(opt
[1]);
152 if ((matches
) && (matches
.length
=== 3)) {
153 ircSocket
.IRC
.options
[opt
[0]] = [];
154 for (j
= 0; j
< matches
[2].length
; j
++) {
155 //ircSocket.IRC.options[opt[0]][matches[2].charAt(j)] = matches[1].charAt(j);
156 ircSocket
.IRC
.options
[opt
[0]].push({symbol
: matches
[2].charAt(j
), mode
: matches
[1].charAt(j
)});
164 websocket
.sendClientEvent('options', {server
: '', "options": ircSocket
.IRC
.options
});
166 case ircNumerics
.RPL_WHOISUSER
:
167 case ircNumerics
.RPL_WHOISSERVER
:
168 case ircNumerics
.RPL_WHOISOPERATOR
:
169 case ircNumerics
.RPL_ENDOFWHOIS
:
170 case ircNumerics
.RPL_WHOISCHANNELS
:
171 case ircNumerics
.RPL_WHOISMODES
:
172 websocket
.sendClientEvent('whois', {server
: '', nick
: msg
.params
.split(" ", 3)[1], "msg": msg
.trailing
});
174 case ircNumerics
.RPL_WHOISIDLE
:
175 params
= msg
.params
.split(" ", 4);
176 rtn
= {server
: '', nick
: params
[1], idle
: params
[2]};
178 rtn
.logon
= params
[3];
180 websocket
.sendClientEvent('whois', rtn
);
182 case ircNumerics
.RPL_MOTD
:
183 websocket
.sendClientEvent('motd', {server
: '', "msg": msg
.trailing
});
185 case ircNumerics
.RPL_NAMEREPLY
:
186 params
= msg
.params
.split(" ");
189 users
= msg
.trailing
.split(" ");
190 prefixes
= _
.values(ircSocket
.IRC
.options
.PREFIX
);
193 _
.each(users
, function (user
) {
194 if (_
.include(prefix
, user
.charAt(0))) {
195 prefix
= user
.charAt(0);
196 user
= user
.substring(1);
197 nicklist
[user
] = prefix
;
202 websocket
.sendClientEvent('userlist', {server
: '', 'users': nicklist
, channel
: chan
});
208 websocket
.sendClientEvent('userlist', {server
: '', "users": nicklist
, channel
: chan
});
213 case ircNumerics
.RPL_ENDOFNAMES
:
214 websocket
.sendClientEvent('userlist_end', {server
: '', channel
: msg
.params
.split(" ")[1]});
216 case ircNumerics
.ERR_LINKCHANNEL
:
217 params
= msg
.params
.split(" ");
218 websocket
.sendClientEvent('channel_redirect', {from: params
[1], to
: params
[2]});
220 case ircNumerics
.ERR_NOSUCHNICK
:
221 websocket
.sendClientEvent('irc_error', {error
: 'no_such_nick', nick
: msg
.params
.split(" ")[1], reason
: msg
.trailing
});
224 // Some BNC's send malformed JOIN causing the channel to be as a
225 // parameter instead of trailing.
226 if (typeof msg
.trailing
=== 'string' && msg
.trailing
!== '') {
227 channel
= msg
.trailing
;
228 } else if (typeof msg
.params
=== 'string' && msg
.params
!== '') {
229 channel
= msg
.params
;
232 websocket
.sendClientEvent('join', {nick
: msg
.nick
, ident
: msg
.ident
, hostname
: msg
.hostname
, channel
: channel
});
233 if (msg
.nick
=== ircSocket
.IRC
.nick
) {
234 websocket
.sendServerLine('NAMES ' + msg
.trailing
);
238 websocket
.sendClientEvent('part', {nick
: msg
.nick
, ident
: msg
.ident
, hostname
: msg
.hostname
, channel
: msg
.params
.trim(), message
: msg
.trailing
});
241 params
= msg
.params
.split(" ");
242 websocket
.sendClientEvent('kick', {kicked
: params
[1], nick
: msg
.nick
, ident
: msg
.ident
, hostname
: msg
.hostname
, channel
: params
[0].trim(), message
: msg
.trailing
});
245 websocket
.sendClientEvent('quit', {nick
: msg
.nick
, ident
: msg
.ident
, hostname
: msg
.hostname
, message
: msg
.trailing
});
248 if ((msg
.trailing
.charAt(0) === String
.fromCharCode(1)) && (msg
.trailing
.charAt(msg
.trailing
.length
- 1) === String
.fromCharCode(1))) {
249 // It's a CTCP response
250 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)});
252 websocket
.sendClientEvent('notice', {nick
: msg
.nick
, ident
: msg
.ident
, hostname
: msg
.hostname
, target
: msg
.params
.trim(), msg
: msg
.trailing
});
256 websocket
.sendClientEvent('nick', {nick
: msg
.nick
, ident
: msg
.ident
, hostname
: msg
.hostname
, newnick
: msg
.trailing
});
259 obj
= {nick
: msg
.nick
, channel
: msg
.params
, topic
: msg
.trailing
};
260 websocket
.sendClientEvent('topic', obj
);
262 case ircNumerics
.RPL_TOPIC
:
263 obj
= {nick
: '', channel
: msg
.params
.split(" ")[1], topic
: msg
.trailing
};
264 websocket
.sendClientEvent('topic', obj
);
266 case ircNumerics
.RPL_NOTOPIC
:
267 obj
= {nick
: '', channel
: msg
.params
.split(" ")[1], topic
: ''};
268 websocket
.sendClientEvent('topic', obj
);
271 opts
= msg
.params
.split(" ");
272 params
= {nick
: msg
.nick
};
273 switch (opts
.length
) {
275 params
.effected_nick
= opts
[0];
276 params
.mode
= msg
.trailing
;
279 params
.channel
= opts
[0];
280 params
.mode
= opts
[1];
283 params
.channel
= opts
[0];
284 params
.mode
= opts
[1];
285 params
.effected_nick
= opts
[2];
288 websocket
.sendClientEvent('mode', params
);
291 if ((msg
.trailing
.charAt(0) === String
.fromCharCode(1)) && (msg
.trailing
.charAt(msg
.trailing
.length
- 1) === String
.fromCharCode(1))) {
292 // It's a CTCP request
293 if (msg
.trailing
.substr(1, 6) === 'ACTION') {
294 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)});
295 } else if (msg
.trailing
.substr(1, 7) === 'VERSION') {
296 ircSocket
.write('NOTICE ' + msg
.nick
+ ' :' + String
.fromCharCode(1) + 'VERSION KiwiIRC' + String
.fromCharCode(1) + '\r\n');
298 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)});
301 obj
= {nick
: msg
.nick
, ident
: msg
.ident
, hostname
: msg
.hostname
, channel
: msg
.params
.trim(), msg
: msg
.trailing
};
302 websocket
.sendClientEvent('msg', obj
);
306 caps
= kiwi
.config
.cap_options
;
307 options
= msg
.trailing
.split(" ");
308 switch (_
.last(msg
.params
.split(" "))) {
311 _
.each(_
.intersect(caps
, options
), function (cap
) {
316 ircSocket
.IRC
.CAP
.requested
.push(cap
);
318 if (opts
.length
> 0) {
319 websocket
.sendServerLine('CAP REQ :' + opts
);
321 websocket
.sendServerLine('CAP END');
324 /*if (_.include(options, 'tls')) {
325 websocket.sendServerLine('STARTTLS');
326 ircSocket.IRC.CAP.requested.push('tls');
330 _
.each(options
, function (cap
) {
331 ircSocket
.IRC
.CAP
.enabled
.push(cap
);
333 if (_
.last(msg
.params
.split(" ")) !== '*') {
334 ircSocket
.IRC
.CAP
.requested
= [];
335 ircSocket
.IRC
.CAP
.negotiating
= false;
336 websocket
.sendServerLine('CAP END');
340 ircSocket
.IRC
.CAP
.requested
= [];
341 ircSocket
.IRC
.CAP
.negotiating
= false;
342 websocket
.sendServerLine('CAP END');
346 /*case ircNumerics.RPL_STARTTLS:
349 listeners = ircSocket.listeners('data');
350 ircSocket.removeAllListeners('data');
351 ssl_socket = starttls(ircSocket, {}, function () {
352 ssl_socket.on("data", function (data) {
353 ircSocketDataHandler(data, websocket, ssl_socket);
355 ircSocket = ssl_socket;
357 _.each(listeners, function (listener) {
358 ircSocket.addListener('data', listener);
361 //console.log(ircSocket);
366 case ircNumerics
.ERR_CANNOTSENDTOCHAN
:
367 websocket
.sendClientEvent('irc_error', {error
: 'cannot_send_to_chan', channel
: msg
.params
.split(" ")[1], reason
: msg
.trailing
});
369 case ircNumerics
.ERR_TOOMANYCHANNELS
:
370 websocket
.sendClientEvent('irc_error', {error
: 'too_many_channels', channel
: msg
.params
.split(" ")[1], reason
: msg
.trailing
});
372 case ircNumerics
.ERR_USERNOTINCHANNEL
:
373 params
= msg
.params
.split(" ");
374 websocket
.sendClientEvent('irc_error', {error
: 'user_not_in_channel', nick
: params
[0], channel
: params
[1], reason
: msg
.trainling
});
376 case ircNumerics
.ERR_NOTONCHANNEL
:
377 websocket
.sendClientEvent('irc_error', {error
: 'not_on_channel', channel
: msg
.params
.split(" ")[1], reason
: msg
.trailing
});
379 case ircNumerics
.ERR_CHANNELISFULL
:
380 websocket
.sendClientEvent('irc_error', {error
: 'channel_is_full', channel
: msg
.params
.split(" ")[1], reason
: msg
.trailing
});
382 case ircNumerics
.ERR_INVITEONLYCHAN
:
383 websocket
.sendClientEvent('irc_error', {error
: 'invite_only_channel', channel
: msg
.params
.split(" ")[1], reason
: msg
.trailing
});
385 case ircNumerics
.ERR_BANNEDFROMCHAN
:
386 websocket
.sendClientEvent('irc_error', {error
: 'banned_from_channel', channel
: msg
.params
.split(" ")[1], reason
: msg
.trailing
});
388 case ircNumerics
.ERR_BADCHANNELKEY
:
389 websocket
.sendClientEvent('irc_error', {error
: 'bad_channel_key', channel
: msg
.params
.split(" ")[1], reason
: msg
.trailing
});
391 case ircNumerics
.ERR_CHANOPRIVSNEEDED
:
392 websocket
.sendClientEvent('irc_error', {error
: 'chanop_privs_needed', channel
: msg
.params
.split(" ")[1], reason
: msg
.trailing
});
394 case ircNumerics
.ERR_NICKNAMEINUSE
:
395 websocket
.sendClientEvent('irc_error', {error
: 'nickname_in_use', nick
: _
.last(msg
.params
.split(" ")), reason
: msg
.trailing
});
399 websocket
.sendClientEvent('irc_error', {error
: 'error', reason
: msg
.trailing
});
400 websocket
.disconnect();
402 case ircNumerics
.ERR_NOTREGISTERED
:
403 if (ircSocket
.IRC
.registered
) {
404 console
.log('Kiwi thinks user is registered, but the IRC server thinks differently');
408 console
.log("Unknown command (" + String(msg
.command
).toUpperCase() + ")");
411 console
.log("Malformed IRC line: " + data
);
421 * NOTE: Some IRC servers or BNC's out there incorrectly use
422 * only \n as a line splitter.
424 this.ircSocketDataHandler = function (data
, websocket
, ircSocket
) {
426 if ((ircSocket
.holdLast
) && (ircSocket
.held
!== '')) {
427 data
= ircSocket
.held
+ data
;
428 ircSocket
.holdLast
= false;
431 if (data
.substr(-1) !== '\n') {
432 ircSocket
.holdLast
= true;
434 data
= data
.split("\n");
435 for (i
= 0; i
< data
.length
; i
++) {
437 if ((ircSocket
.holdLast
) && (i
=== data
.length
- 1)) {
438 ircSocket
.held
= data
[i
];
442 // We have a complete line of data, parse it!
443 kiwi
.parseIRCMessage(websocket
, ircSocket
, data
[i
].replace(/^\r+|\r+$/, ''));
452 this.httpHandler = function (request
, response
) {
453 var uri
, subs
, useragent
, agent
, server_set
, server
, nick
, debug
, touchscreen
, hash
,
454 min
= {}, public_http_path
;
455 if (kiwi
.config
.handle_http
) {
456 uri
= url
.parse(request
.url
, true);
457 subs
= uri
.pathname
.substr(0, 4);
458 if (uri
.pathname
=== '/js/all.js') {
459 if (kiwi
.cache
.alljs
=== '') {
460 public_http_path
= kiwi
.kiwi_root
+ '/' + kiwi
.config
.public_http
;
462 min
.util
= fs
.readFileSync(public_http_path
+ 'js/util.js');
463 min
.gateway
= fs
.readFileSync(public_http_path
+ 'js/gateway.js');
464 min
.front
= fs
.readFileSync(public_http_path
+ 'js/front.js');
465 min
.iscroll
= fs
.readFileSync(public_http_path
+ 'js/iscroll.js');
466 min
.ast
= jsp
.parse(min
.util
+ min
.gateway
+ min
.front
+ min
.iscroll
);
467 min
.ast
= pro
.ast_mangle(min
.ast
);
468 min
.ast
= pro
.ast_squeeze(min
.ast
);
469 min
.final_code
= pro
.gen_code(min
.ast
);
470 kiwi
.cache
.alljs
= min
.final_code
;
471 hash
= crypto
.createHash('md5').update(kiwi
.cache
.alljs
);
472 kiwi
.cache
.alljs_hash
= hash
.digest('base64');
474 if (request
.headers
['if-none-match'] === kiwi
.cache
.alljs_hash
) {
475 response
.statusCode
= 304;
477 response
.setHeader('ETag', kiwi
.cache
.alljs_hash
);
478 response
.write(kiwi
.cache
.alljs
);
481 } else if ((subs
=== '/js/') || (subs
=== '/css') || (subs
=== '/img')) {
482 request
.addListener('end', function () {
483 kiwi
.fileServer
.serve(request
, response
);
485 } else if (uri
.pathname
=== '/') {
486 useragent
= (response
.headers
) ? response
.headers
['user-agent'] : '';
487 if (useragent
.indexOf('android') !== -1) {
490 } else if (useragent
.indexOf('iphone') !== -1) {
493 } else if (useragent
.indexOf('ipad') !== -1) {
496 } else if (useragent
.indexOf('ipod') !== -1) {
505 server_set
= ((typeof uri
.query
.server
!== 'undefined') && (uri
.query
.server
!== ''));
506 server
= uri
.query
.server
|| 'irc.anonnet.org';
507 nick
= uri
.query
.nick
|| '';
508 debug
= (uri
.query
.debug
!== '');
511 server
= 'irc.anonnet.org';
514 response
.setHeader('X-Generated-By', 'KiwiIRC');
515 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');
516 if (kiwi
.cache
.html
[hash
]) {
517 if (request
.headers
['if-none-match'] === kiwi
.cache
.html
[hash
].hash
) {
518 response
.statusCode
= 304;
520 response
.setHeader('Etag', kiwi
.cache
.html
[hash
].hash
);
521 response
.write(kiwi
.cache
.html
[hash
].html
);
525 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
) {
527 var hash2
= crypto
.createHash('md5').update(html
).digest('base64');
528 kiwi
.cache
.html
[hash
] = {"html": html
, "hash": hash2
};
529 if (request
.headers
['if-none-match'] === hash2
) {
530 response
.statusCode
= 304;
532 response
.setHeader('Etag', hash2
);
533 response
.write(html
);
536 response
.statusCode
= 500;
541 } else if (uri
.pathname
.substr(0, 10) === '/socket.io') {
544 response
.statusCode
= 404;
553 this.websocketListen = function (port
, host
, handler
, secure
, key
, cert
) {
554 if (kiwi
.httpServer
) {
555 kiwi
.httpServer
.close();
558 kiwi
.httpServer
= https
.createServer({key
: fs
.readFileSync(__dirname
+ '/' + key
), cert
: fs
.readFileSync(__dirname
+ '/' + cert
)}, handler
);
559 kiwi
.io
= ws
.listen(kiwi
.httpServer
, {secure
: true});
560 kiwi
.httpServer
.listen(port
, host
);
562 kiwi
.httpServer
= http
.createServer(handler
);
563 kiwi
.io
= ws
.listen(kiwi
.httpServer
, {secure
: false});
564 kiwi
.httpServer
.listen(port
, host
);
567 kiwi
.io
.set('log level', 1);
568 kiwi
.io
.enable('browser client minification');
569 kiwi
.io
.enable('browser client etag');
570 kiwi
.io
.set('transports', kiwi
.config
.transports
);
572 kiwi
.io
.of('/kiwi').authorization(function (handshakeData
, callback
) {
573 var address
= handshakeData
.address
.address
;
575 if (typeof kiwi
.connections
[address
] === 'undefined') {
576 kiwi
.connections
[address
] = {count
: 0, sockets
: []};
578 callback(null, true);
579 }).on('connection', kiwi
.websocketConnection
);
587 this.websocketConnection = function (websocket
) {
589 websocket
.kiwi
= {address
: websocket
.handshake
.address
.address
};
590 con
= kiwi
.connections
[websocket
.kiwi
.address
];
592 if (con
.count
>= kiwi
.config
.max_client_conns
) {
593 websocket
.emit('too_many_connections');
594 websocket
.disconnect();
597 con
.sockets
.push(websocket
);
599 websocket
.sendClientEvent = function (event_name
, data
) {
600 var ev
= kiwi
.kiwi_mod
.run(event_name
, data
, {websocket
: this});
601 if(ev
=== null) return;
603 data
.event
= event_name
;
604 websocket
.emit('message', data
);
607 websocket
.sendServerLine = function (data
, eol
) {
608 eol
= (typeof eol
=== 'undefined') ? '\r\n' : eol
;
609 websocket
.ircSocket
.write(data
+ eol
);
612 websocket
.on('irc connect', kiwi
.websocketIRCConnect
);
613 websocket
.on('message', kiwi
.websocketMessage
);
614 websocket
.on('disconnect', kiwi
.websocketDisconnect
);
622 this.websocketIRCConnect = function (websocket
, nick
, host
, port
, ssl
, callback
) {
624 //setup IRC connection
626 ircSocket
= net
.createConnection(port
, host
);
628 ircSocket
= tls
.connect(port
, host
);
630 ircSocket
.setEncoding('ascii');
631 ircSocket
.IRC
= {options
: {}, CAP
: {negotiating
: true, requested
: [], enabled
: []}, registered
: false};
632 ircSocket
.on('error', function (e
) {
633 if (ircSocket
.IRC
.registered
) {
634 websocket
.emit('disconnect');
636 websocket
.emit('error', e
.message
);
639 websocket
.ircSocket
= ircSocket
;
640 ircSocket
.holdLast
= false;
643 ircSocket
.on('data', function (data
) {
644 kiwi
.ircSocketDataHandler(data
, websocket
, ircSocket
);
647 ircSocket
.IRC
.nick
= nick
;
648 // Send the login data
649 dns
.reverse(websocket
.kiwi
.address
, function (err
, domains
) {
650 //console.log(domains);
651 websocket
.kiwi
.hostname
= (err
) ? websocket
.kiwi
.address
: _
.first(domains
);
652 if ((kiwi
.config
.webirc
) && (kiwi
.config
.webirc_pass
[host
])) {
653 websocket
.sendServerLine('WEBIRC ' + kiwi
.config
.webirc_pass
[host
] + ' KiwiIRC ' + websocket
.kiwi
.hostname
+ ' ' + websocket
.kiwi
.address
);
655 websocket
.sendServerLine('CAP LS');
656 websocket
.sendServerLine('NICK ' + nick
);
657 websocket
.sendServerLine('USER ' + nick
.replace(/[^0-9a-zA-Z\-_.]/, '') + '_kiwi 0 0 :' + nick
);
659 if ((callback
) && (typeof (callback
) === 'function')) {
667 this.websocketMessage = function (websocket
, msg
, callback
) {
670 msg
.data
= JSON
.parse(msg
.data
);
671 args
= msg
.data
.args
;
672 switch (msg
.data
.method
) {
674 if ((args
.target
) && (args
.msg
)) {
675 obj
= kiwi
.kiwi_mod
.run('msgsend', args
, {websocket
: websocket
});
677 websocket
.sendServerLine('PRIVMSG ' + args
.target
+ ' :' + args
.msg
);
682 if ((args
.target
) && (args
.msg
)) {
683 websocket
.sendServerLine('PRIVMSG ' + args
.target
+ ' :\ 1' + String
.fromCharCode(1) + 'ACTION ' + args
.msg
+ String
.fromCharCode(1));
687 websocket
.sendServerLine(args
.data
);
691 _
.each(args
.channel
.split(","), function (chan
) {
692 websocket
.sendServerLine('JOIN ' + chan
);
698 websocket
.sendServerLine('TOPIC ' + args
.channel
+ ' :' + args
.topic
);
702 websocket
.ircSocket
.end('QUIT :' + args
.message
+ '\r\n');
703 websocket
.sentQUIT
= true;
704 websocket
.ircSocket
.destroySoon();
705 websocket
.disconnect();
708 if ((args
.target
) && (args
.msg
)) {
709 websocket
.sendServerLine('NOTICE ' + args
.target
+ ' :' + args
.msg
);
714 if ((callback
) && (typeof (callback
) === 'function')) {
718 console
.log("Caught error: " + e
);
724 this.websocketDisconnect = function (websocket
) {
727 if ((!websocket
.sentQUIT
) && (websocket
.ircSocket
)) {
729 websocket
.ircSocket
.end('QUIT :' + kiwi
.config
.quit_message
+ '\r\n');
730 websocket
.sentQUIT
= true;
731 websocket
.ircSocket
.destroySoon();
735 con
= kiwi
.connections
[websocket
.kiwi
.address
];
737 con
.sockets
= _
.reject(con
.sockets
, function (sock
) {
738 return sock
=== websocket
;
747 this.rehash = function () {
749 reload_config
= kiwi
.loadConfig();
751 // If loading the new config errored out, dont attempt any changes
752 if (reload_config
=== false) {
756 // We just want the settings that have been changed
757 changes
= reload_config
[1];
759 if (Object
.keys(changes
).length
!== 0) {
760 console
.log('%s config changes: \n', Object
.keys(changes
).length
, changes
);
768 kiwi
.websocketListen(kiwi
.config
.port
, kiwi
.config
.bind_address
, kiwi
.httpHandler
, kiwi
.config
.listen_ssl
, kiwi
.config
.ssl_key
, kiwi
.config
.ssl_cert
);
770 delete changes
.bind_address
;
771 delete changes
.listen_ssl
;
772 delete changes
.ssl_key
;
773 delete changes
.ssl_cert
;
779 delete changes
.group
;
783 kiwi
.kiwi_mod
.loadModules(kiwi_root
, kiwi
.config
);
784 kiwi
.kiwi_mod
.printMods();
785 delete changes
.module_dir
;
786 delete changes
.modules
;
792 // Also clear the kiwi.cached javascript and HTML
793 if (kiwi
.config
.handle_http
) {
794 kiwi
.cache
= {alljs
: '', html
: []};
805 * KiwiIRC controlling via STDIN
807 this.manageControll = function (data
) {
808 var parts
= data
.toString().trim().split(' ');
811 console
.log('Rehashing...');
812 console
.log(kiwi
.rehash() ? 'Rehash complete' : 'Rehash failed');
816 console
.log('Recoding...');
817 console
.log(kiwi
.recode() ? 'Recode complete' : 'Recode failed');
821 if (parts
[1] === 'reload') {
822 console
.log('Reloading module (' + parts
[2] + ')..');
823 kiwi
.kiwi_mod
.reloadModule(parts
[2]);
828 if (parts
[1] === 'clear') {
829 kiwi
.cache
.html
= {};
830 console
.log('HTML cache cleared');
835 var connections_cnt
= 0;
836 for (var i
in kiwi
.connections
) {
837 connections_cnt
= connections_cnt
+ parseInt(kiwi
.connections
[i
].count
, 10);
839 console
.log(connections_cnt
.toString() + ' connected clients');
843 console
.log('Unknown command \'' + parts
[0] + '\'');