1 var net
= require('net'),
3 util
= require('util'),
5 EventEmitter2
= require('eventemitter2').EventEmitter2
,
6 EventBinder
= require('./eventbinder.js'),
7 IrcServer
= require('./server.js'),
8 IrcChannel
= require('./channel.js'),
9 IrcUser
= require('./user.js'),
13 // Break the Node.js version down into usable parts
14 var version_values
= process
.version
.substr(1).split('.').map(function (item
) {
15 return parseInt(item
, 10);
18 // If we have a suitable Nodejs version, bring int he socks functionality
19 if (version_values
[1] >= 10) {
20 Socks
= require('socksjs');
23 var IrcConnection = function (hostname
, port
, ssl
, nick
, user
, pass
, state
) {
26 EventEmitter2
.call(this,{
30 this.setMaxListeners(0);
33 this.connected
= false;
35 // If registeration with the IRCd has completed
36 this.registered
= false;
38 // If we are in the CAP negotiation stage
39 this.cap_negotiation
= true;
43 this.user
= user
; // Contains users real hostname and address
44 this.username
= this.nick
.replace(/[^0-9a-zA-Z\-_.\/]/, '');
51 this.server
= new IrcServer(this, hostname
, port
);
54 this.irc_users
= Object
.create(null);
56 // TODO: use `this.nick` instead of `'*'` when using an IrcUser per nick
57 this.irc_users
[this.nick
] = new IrcUser(this, '*');
60 this.irc_channels
= Object
.create(null);
62 // IRC connection information
63 this.irc_host
= {hostname
: hostname
, port
: port
};
66 // SOCKS proxy details
67 // TODO: Wildcard matching of hostnames and/or CIDR ranges of IP addresses
68 if ((global
.config
.socks_proxy
&& global
.config
.socks_proxy
.enabled
) && ((global
.config
.socks_proxy
.all
) || (_
.contains(global
.config
.socks_proxy
.proxy_hosts
, this.irc_host
.hostname
)))) {
70 host
: global
.config
.socks_proxy
.address
,
71 port
: global
.config
.socks_proxy
.port
,
72 user
: global
.config
.socks_proxy
.user
,
73 pass
: global
.config
.socks_proxy
.pass
79 // Options sent by the IRCd
80 this.options
= Object
.create(null);
81 this.cap
= {requested
: [], enabled
: []};
83 // Is SASL supported on the IRCd
86 // Buffers for data sent from the IRCd
87 this.hold_last
= false;
90 this.applyIrcEvents();
92 // Call any modules before making the connection
93 global
.modules
.emit('irc connecting', {connection
: this})
98 util
.inherits(IrcConnection
, EventEmitter2
);
100 module
.exports
.IrcConnection
= IrcConnection
;
104 IrcConnection
.prototype.applyIrcEvents = function () {
105 // Listen for events on the IRC connection
107 'server * connect': onServerConnect
,
108 'channel * join': onChannelJoin
,
110 // TODO: uncomment when using an IrcUser per nick
111 //'user:*:privmsg': onUserPrivmsg,
112 'user * nick': onUserNick
,
113 'channel * part': onUserParts
,
114 'channel * quit': onUserParts
,
115 'channel * kick': onUserKick
118 EventBinder
.bindIrcEvents('', this.irc_events
, this, this);
123 * Start the connection to the IRCd
125 IrcConnection
.prototype.connect = function () {
129 // The socket connect event to listener for
130 var socket_connect_event_name
= 'connect';
133 // Make sure we don't already have an open connection
134 this.disposeSocket();
136 // Are we connecting through a SOCKS proxy?
138 this.socket
= Socks
.connect({
139 host
: this.irc_host
.hostname
,
140 port
: this.irc_host
.port
,
142 rejectUnauthorized
: global
.config
.reject_unauthorised_certificates
143 }, {host
: this.socks
.host
,
144 port
: this.socks
.port
,
145 user
: this.socks
.user
,
146 pass
: this.socks
.pass
149 } else if (this.ssl
) {
150 this.socket
= tls
.connect({
151 host
: this.irc_host
.hostname
,
152 port
: this.irc_host
.port
,
153 rejectUnauthorized
: global
.config
.reject_unauthorised_certificates
156 socket_connect_event_name
= 'secureConnect';
159 this.socket
= net
.connect({
160 host
: this.irc_host
.hostname
,
161 port
: this.irc_host
.port
165 this.socket
.on(socket_connect_event_name
, function () {
166 that
.connected
= true;
167 socketConnectHandler
.call(that
);
170 this.socket
.setEncoding('utf-8');
172 this.socket
.on('error', function (event
) {
173 that
.emit('error', event
);
176 this.socket
.on('data', function () {
177 parse
.apply(that
, arguments
);
180 this.socket
.on('close', function (had_error
) {
181 that
.connected
= false;
184 // Close the whole socket down
185 that
.disposeSocket();
190 * Send an event to the client
192 IrcConnection
.prototype.clientEvent = function (event_name
, data
, callback
) {
193 data
.server
= this.con_num
;
194 this.state
.sendIrcCommand(event_name
, data
, callback
);
198 * Write a line of data to the IRCd
200 IrcConnection
.prototype.write = function (data
, callback
) {
201 this.socket
.write(data
+ '\r\n', 'utf-8', callback
);
207 * Close the connection to the IRCd after sending one last line
209 IrcConnection
.prototype.end = function (data
, callback
) {
219 * Clean up this IrcConnection instance and any sockets
221 IrcConnection
.prototype.dispose = function () {
222 _
.each(this.irc_users
, function (user
) {
225 _
.each(this.irc_channels
, function (chan
) {
228 this.irc_users
= undefined;
229 this.irc_channels
= undefined;
231 this.server
.dispose();
232 this.server
= undefined;
234 EventBinder
.unbindIrcEvents('', this.irc_events
, this);
236 this.disposeSocket();
237 this.removeAllListeners();
243 * Clean up any sockets for this IrcConnection
245 IrcConnection
.prototype.disposeSocket = function () {
248 this.socket
.removeAllListeners();
255 function onChannelJoin(event
) {
258 // Only deal with ourselves joining a channel
259 if (event
.nick
!== this.nick
)
262 // We should only ever get a JOIN command for a channel
263 // we're not already a member of.. but check we don't
264 // have this channel in case something went wrong somewhere
265 // at an earlier point
266 if (!this.irc_channels
[event
.channel
]) {
267 chan
= new IrcChannel(this, event
.channel
);
268 this.irc_channels
[event
.channel
] = chan
;
269 chan
.irc_events
.join
.call(chan
, event
);
274 function onServerConnect(event
) {
275 this.nick
= event
.nick
;
279 function onUserPrivmsg(event
) {
282 // Only deal with messages targetted to us
283 if (event
.channel
!== this.nick
)
286 if (!this.irc_users
[event
.nick
]) {
287 user
= new IrcUser(this, event
.nick
);
288 this.irc_users
[event
.nick
] = user
;
289 user
.irc_events
.privmsg
.call(user
, event
);
294 function onUserNick(event
) {
297 // Only deal with messages targetted to us
298 if (event
.nick
!== this.nick
)
301 this.nick
= event
.newnick
;
305 function onUserParts(event
) {
306 // Only deal with ourselves leaving a channel
307 if (event
.nick
!== this.nick
)
310 if (this.irc_channels
[event
.channel
]) {
311 this.irc_channels
[event
.channel
].dispose();
312 delete this.irc_channels
[event
.channel
];
316 function onUserKick(event
){
317 // Only deal with ourselves being kicked from a channel
318 if (event
.kicked
!== this.nick
)
321 if (this.irc_channels
[event
.channel
]) {
322 this.irc_channels
[event
.channel
].dispose();
323 delete this.irc_channels
[event
.channel
];
332 * Handle the socket connect event, starting the IRCd registration
334 var socketConnectHandler = function () {
338 // Build up data to be used for webirc/etc detection
342 // Array of lines to be sent to the IRCd before anything else
346 // Let the webirc/etc detection modify any required parameters
347 connect_data
= findWebIrc
.call(this, connect_data
);
349 global
.modules
.emit('irc authorize', connect_data
).done(function () {
350 // Send any initial data for webirc/etc
351 if (connect_data
.prepend_data
) {
352 _
.each(connect_data
.prepend_data
, function(data
) {
357 that
.write('CAP LS');
360 that
.write('PASS ' + that
.password
);
362 that
.write('NICK ' + that
.nick
);
363 that
.write('USER ' + that
.username
+ ' 0 0 :' + '[www.kiwiirc.com] ' + that
.nick
);
365 that
.emit('connected');
372 * Load any WEBIRC or alternative settings for this connection
373 * Called in scope of the IrcConnection instance
375 function findWebIrc(connect_data
) {
376 var webirc_pass
= global
.config
.webirc_pass
,
377 ip_as_username
= global
.config
.ip_as_username
,
381 // Do we have a WEBIRC password for this?
382 if (webirc_pass
&& webirc_pass
[this.irc_host
.hostname
]) {
383 // Build the WEBIRC line to be sent before IRC registration
384 tmp
= 'WEBIRC ' + webirc_pass
[this.irc_host
.hostname
] + ' KiwiIRC ';
385 tmp
+= this.user
.hostname
+ ' ' + this.user
.address
;
387 connect_data
.prepend_data
= [tmp
];
391 // Check if we need to pass the users IP as its username/ident
392 if (ip_as_username
&& ip_as_username
.indexOf(this.irc_host
.hostname
) > -1) {
393 // Get a hex value of the clients IP
394 this.username
= this.user
.address
.split('.').map(function(i
, idx
){
395 var hex
= parseInt(i
, 10).toString(16);
397 // Pad out the hex value if it's a single char
398 if (hex
.length
=== 1)
412 * The regex that parses a line of data from the IRCd
413 * Deviates from the RFC a little to support the '/' character now used in some
416 var parse_regex
= /^(?:(?:(?:(@[^ ]+) )?):(?:([a-z0-9\x5B-\x60\x7B-\x7D\.\-*]+)|([a-z0-9\x5B-\x60\x7B-\x7D\.\-*]+)!([^\x00\r\n\ ]+?)@?([a-z0-9\.\-:\/_]+)?) )?(\S+)(?: (?!:)(.+?))?(?: :(.+))?$/i;
418 var parse = function (data
) {
427 if (this.hold_last
&& this.held_data
!== '') {
428 data
= this.held_data
+ data
;
429 this.hold_last
= false;
433 // If the last line is incomplete, hold it until we have more data
434 if (data
.substr(-1) !== '\n') {
435 this.hold_last
= true;
438 // Process our data line by line
439 data
= data
.split("\n");
440 for (i
= 0; i
< data
.length
; i
++) {
443 // If flagged to hold the last line, store it and move on
444 if (this.hold_last
&& (i
=== data
.length
- 1)) {
445 this.held_data
= data
[i
];
449 // Parse the complete line, removing any carriage returns
450 msg
= parse_regex
.exec(data
[i
].replace(/^\r+|\r+$/, ''));
454 tags
= msg
[1].split(';');
455 for (j
= 0; j
< tags
.length
; j
++) {
456 tag
= tags
[j
].split('=');
457 tags
[j
] = {tag
: tag
[0], value
: tag
[1]};
465 hostname
: msg
[5] || '',
467 params
: msg
[7] || '',
468 trailing
: (msg
[8]) ? msg
[8].trim() : ''
470 msg
.params
= msg
.params
.split(' ');
471 this.irc_commands
.dispatch(msg
.command
.toUpperCase(), msg
);
473 // The line was not parsed correctly, must be malformed
474 console
.log("Malformed IRC line: " + data
[i
].replace(/^\r+|\r+$/, ''));