Commit | Line | Data |
---|---|---|
cefa0900 JA |
1 | var net = require('net'), |
2 | tls = require('tls'), | |
3 | util = require('util'), | |
b156e01a | 4 | dns = require('dns'), |
cefa0900 | 5 | _ = require('lodash'), |
94b00b2d | 6 | winston = require('winston'), |
4c25c0d7 | 7 | EventBinder = require('./eventbinder.js'), |
cefa0900 | 8 | IrcServer = require('./server.js'), |
cced0091 | 9 | IrcCommands = require('./commands.js'), |
cefa0900 | 10 | IrcChannel = require('./channel.js'), |
9650b343 | 11 | IrcUser = require('./user.js'), |
2abb4615 | 12 | EE = require('../ee.js'), |
1a452a02 | 13 | iconv = require('iconv-lite'), |
b5574d3b | 14 | Proxy = require('../proxy.js'), |
55ccaf50 | 15 | Socks; |
32a09dc1 D |
16 | |
17 | ||
55ccaf50 JA |
18 | // Break the Node.js version down into usable parts |
19 | var version_values = process.version.substr(1).split('.').map(function (item) { | |
20 | return parseInt(item, 10); | |
21 | }); | |
9de636f9 | 22 | |
0c0dfa95 | 23 | // If we have a suitable Nodejs version, bring in the SOCKS functionality |
9de636f9 | 24 | if (version_values[1] >= 10) { |
3b259b15 | 25 | Socks = require('socksjs'); |
55ccaf50 | 26 | } |
db8af19d | 27 | |
cc54c0a1 | 28 | var IrcConnection = function (hostname, port, ssl, nick, user, options, state, con_num) { |
2abb4615 | 29 | EE.call(this,{ |
cefa0900 | 30 | wildcard: true, |
d9285da9 | 31 | delimiter: ' ' |
cefa0900 | 32 | }); |
4c25c0d7 | 33 | this.setMaxListeners(0); |
32a09dc1 | 34 | |
cc54c0a1 D |
35 | options = options || {}; |
36 | ||
db8af19d | 37 | // Socket state |
63008d5e | 38 | this.connected = false; |
db8af19d | 39 | |
7746ca17 D |
40 | // If the connection closes and this is false, we reconnect |
41 | this.requested_disconnect = false; | |
42 | ||
fc1c21b8 D |
43 | // IRCd write buffers (flood controll) |
44 | this.write_buffer = []; | |
45 | ||
46 | // In process of writing the buffer? | |
47 | this.writing_buffer = false; | |
48 | ||
49 | // Max number of lines to write a second | |
50 | this.write_buffer_lines_second = 2; | |
51 | ||
db8af19d | 52 | // If registeration with the IRCd has completed |
63008d5e | 53 | this.registered = false; |
db8af19d D |
54 | |
55 | // If we are in the CAP negotiation stage | |
63008d5e | 56 | this.cap_negotiation = true; |
db8af19d D |
57 | |
58 | // User information | |
63008d5e D |
59 | this.nick = nick; |
60 | this.user = user; // Contains users real hostname and address | |
5973d01c | 61 | this.username = this.nick.replace(/[^0-9a-zA-Z\-_.\/]/, ''); |
63811c17 | 62 | this.gecos = ''; // Users real-name. Uses default from config if empty |
cc54c0a1 D |
63 | this.password = options.password || ''; |
64 | ||
65 | // Set the passed encoding. or the default if none giving or it fails | |
66 | if (!options.encoding || !this.setEncoding(options.encoding)) { | |
67 | this.setEncoding(global.config.default_encoding); | |
68 | } | |
32a09dc1 | 69 | |
b09157de JA |
70 | // State object |
71 | this.state = state; | |
32a09dc1 | 72 | |
cced0091 D |
73 | // Connection ID in the state |
74 | this.con_num = con_num; | |
75 | ||
76 | // IRC protocol handling | |
138d8d17 | 77 | this.irc_commands = new IrcCommands.Handler(this); |
cced0091 | 78 | |
cefa0900 JA |
79 | // IrcServer object |
80 | this.server = new IrcServer(this, hostname, port); | |
32a09dc1 | 81 | |
cefa0900 JA |
82 | // IrcUser objects |
83 | this.irc_users = Object.create(null); | |
efe9d487 D |
84 | |
85 | // TODO: use `this.nick` instead of `'*'` when using an IrcUser per nick | |
86 | this.irc_users[this.nick] = new IrcUser(this, '*'); | |
87 | ||
cefa0900 JA |
88 | // IrcChannel objects |
89 | this.irc_channels = Object.create(null); | |
db8af19d D |
90 | |
91 | // IRC connection information | |
63008d5e D |
92 | this.irc_host = {hostname: hostname, port: port}; |
93 | this.ssl = !(!ssl); | |
32a09dc1 | 94 | |
9650b343 | 95 | // SOCKS proxy details |
ac0b278c JA |
96 | // TODO: Wildcard matching of hostnames and/or CIDR ranges of IP addresses |
97 | 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)))) { | |
98 | this.socks = { | |
99 | host: global.config.socks_proxy.address, | |
100 | port: global.config.socks_proxy.port, | |
101 | user: global.config.socks_proxy.user, | |
102 | pass: global.config.socks_proxy.pass | |
103 | }; | |
104 | } else { | |
105 | this.socks = false; | |
106 | } | |
db8af19d | 107 | |
29bc2066 | 108 | // Kiwi proxy info may be set within a server module. {port: 7779, host: 'kiwi.proxy.com', ssl: false} |
4b329880 D |
109 | this.proxy = false; |
110 | ||
7cf75020 D |
111 | // Net. interface this connection should be made through |
112 | this.outgoing_interface = false; | |
113 | ||
db8af19d | 114 | // Options sent by the IRCd |
63008d5e | 115 | this.options = Object.create(null); |
d772b95d D |
116 | this.options.PREFIX = [ |
117 | {symbol: '~', mode: 'q'}, | |
118 | {symbol: '&', mode: 'a'}, | |
119 | {symbol: '@', mode: 'o'}, | |
120 | {symbol: '%', mode: 'h'}, | |
121 | {symbol: '+', mode: 'v'} | |
122 | ]; | |
123 | ||
63008d5e | 124 | this.cap = {requested: [], enabled: []}; |
db8af19d D |
125 | |
126 | // Is SASL supported on the IRCd | |
63008d5e | 127 | this.sasl = false; |
32a09dc1 | 128 | |
db8af19d | 129 | // Buffers for data sent from the IRCd |
63008d5e | 130 | this.hold_last = false; |
a8d8af60 | 131 | this.held_data = null; |
63008d5e | 132 | |
4c25c0d7 | 133 | this.applyIrcEvents(); |
63008d5e | 134 | }; |
2abb4615 | 135 | util.inherits(IrcConnection, EE); |
63008d5e D |
136 | |
137 | module.exports.IrcConnection = IrcConnection; | |
138 | ||
f9ff7686 | 139 | |
db8af19d | 140 | |
4c25c0d7 D |
141 | IrcConnection.prototype.applyIrcEvents = function () { |
142 | // Listen for events on the IRC connection | |
143 | this.irc_events = { | |
d9285da9 JA |
144 | 'server * connect': onServerConnect, |
145 | 'channel * join': onChannelJoin, | |
df6d68a5 D |
146 | |
147 | // TODO: uncomment when using an IrcUser per nick | |
148 | //'user:*:privmsg': onUserPrivmsg, | |
d9285da9 JA |
149 | 'user * nick': onUserNick, |
150 | 'channel * part': onUserParts, | |
151 | 'channel * quit': onUserParts, | |
152 | 'channel * kick': onUserKick | |
4c25c0d7 D |
153 | }; |
154 | ||
155 | EventBinder.bindIrcEvents('', this.irc_events, this, this); | |
156 | }; | |
157 | ||
db8af19d D |
158 | |
159 | /** | |
160 | * Start the connection to the IRCd | |
161 | */ | |
63008d5e | 162 | IrcConnection.prototype.connect = function () { |
7496de01 | 163 | var that = this; |
63008d5e | 164 | |
db8af19d D |
165 | // The socket connect event to listener for |
166 | var socket_connect_event_name = 'connect'; | |
167 | ||
7496de01 | 168 | // The destination address |
4b329880 D |
169 | var dest_addr; |
170 | if (this.socks) { | |
171 | dest_addr = this.socks.host; | |
172 | } else if (this.proxy) { | |
173 | dest_addr = this.proxy.host; | |
174 | } else { | |
175 | dest_addr = this.irc_host.hostname; | |
176 | } | |
db8af19d D |
177 | |
178 | // Make sure we don't already have an open connection | |
179 | this.disposeSocket(); | |
180 | ||
7746ca17 D |
181 | this.requested_disconnect = false; |
182 | ||
7496de01 | 183 | // Get the IP family for the dest_addr (either socks or IRCd destination) |
e991ec2f | 184 | getConnectionFamily(dest_addr, function getConnectionFamilyCb(err, family, host) { |
7496de01 D |
185 | var outgoing; |
186 | ||
187 | // Decide which net. interface to make the connection through | |
7cf75020 D |
188 | if (that.outgoing_interface) { |
189 | // An specific interface has been given for this connection | |
190 | outgoing = this.outgoing_interface; | |
191 | ||
192 | } else if (global.config.outgoing_address) { | |
193 | // Pick an interface from the config | |
7496de01 | 194 | if ((family === 'IPv6') && (global.config.outgoing_address.IPv6)) { |
b156e01a JA |
195 | outgoing = global.config.outgoing_address.IPv6; |
196 | } else { | |
7496de01 | 197 | outgoing = global.config.outgoing_address.IPv4 || '0.0.0.0'; |
ddae94f5 D |
198 | |
199 | // We don't have an IPv6 interface but dest_addr may still resolve to | |
200 | // an IPv4 address. Reset `host` and try connecting anyway, letting it | |
201 | // fail if an IPv4 resolved address is not found | |
202 | host = dest_addr; | |
b156e01a | 203 | } |
7496de01 | 204 | |
72280d53 D |
205 | // If we have an array of interfaces, select a random one |
206 | if (typeof outgoing !== 'string' && outgoing.length) { | |
207 | outgoing = outgoing[Math.floor(Math.random() * outgoing.length)]; | |
208 | } | |
209 | ||
210 | // Make sure we have a valid interface address | |
e8c4bb74 | 211 | if (typeof outgoing !== 'string') { |
72280d53 | 212 | outgoing = '0.0.0.0'; |
e8c4bb74 | 213 | } |
72280d53 | 214 | |
7496de01 D |
215 | } else { |
216 | // No config was found so use the default | |
217 | outgoing = '0.0.0.0'; | |
218 | } | |
219 | ||
220 | // Are we connecting through a SOCKS proxy? | |
6cd5ba87 | 221 | if (that.socks) { |
b156e01a | 222 | that.socket = Socks.connect({ |
4b329880 | 223 | host: that.irc_host.host, |
b156e01a JA |
224 | port: that.irc_host.port, |
225 | ssl: that.ssl, | |
226 | rejectUnauthorized: global.config.reject_unauthorised_certificates | |
4b329880 | 227 | }, {host: host, |
b156e01a JA |
228 | port: that.socks.port, |
229 | user: that.socks.user, | |
230 | pass: that.socks.pass, | |
231 | localAddress: outgoing | |
232 | }); | |
233 | ||
4b329880 D |
234 | } else if (that.proxy) { |
235 | that.socket = new Proxy.ProxySocket(that.proxy.port, host, { | |
b5574d3b | 236 | username: that.username, |
5bba0980 | 237 | interface: that.proxy.interface |
e0bdbc31 | 238 | }, {ssl: that.proxy.ssl}); |
96ecb5e7 D |
239 | |
240 | if (that.ssl) { | |
241 | that.socket.connectTls(that.irc_host.port, that.irc_host.hostname); | |
242 | } else { | |
243 | that.socket.connect(that.irc_host.port, that.irc_host.hostname); | |
244 | } | |
b5574d3b | 245 | |
7496de01 D |
246 | } else { |
247 | // No socks connection, connect directly to the IRCd | |
b156e01a JA |
248 | |
249 | if (that.ssl) { | |
250 | that.socket = tls.connect({ | |
ddae94f5 | 251 | host: host, |
b156e01a JA |
252 | port: that.irc_host.port, |
253 | rejectUnauthorized: global.config.reject_unauthorised_certificates, | |
254 | localAddress: outgoing | |
255 | }); | |
256 | ||
4de89532 D |
257 | // We need the raw socket connect event |
258 | that.socket.socket.on('connect', function() { rawSocketConnect.call(that, this); }); | |
259 | ||
b156e01a JA |
260 | socket_connect_event_name = 'secureConnect'; |
261 | ||
262 | } else { | |
263 | that.socket = net.connect({ | |
264 | host: host, | |
265 | port: that.irc_host.port, | |
266 | localAddress: outgoing | |
267 | }); | |
268 | } | |
7496de01 | 269 | } |
b156e01a | 270 | |
7496de01 | 271 | // Apply the socket listeners |
e991ec2f | 272 | that.socket.on(socket_connect_event_name, function socketConnectCb() { |
9e225dc6 | 273 | |
4de89532 D |
274 | // TLS connections have the actual socket as a property |
275 | var is_tls = (typeof this.socket !== 'undefined') ? | |
276 | true : | |
277 | false; | |
9e225dc6 | 278 | |
4de89532 | 279 | // TLS sockets have already called this |
e8c4bb74 | 280 | if (!is_tls) { |
4de89532 | 281 | rawSocketConnect.call(that, this); |
e8c4bb74 | 282 | } |
cc3c5d04 | 283 | |
4de89532 | 284 | that.connected = true; |
cc3c5d04 | 285 | |
b156e01a JA |
286 | socketConnectHandler.call(that); |
287 | }); | |
32a09dc1 | 288 | |
e991ec2f | 289 | that.socket.on('error', function socketErrorCb(event) { |
b156e01a JA |
290 | that.emit('error', event); |
291 | }); | |
32a09dc1 | 292 | |
7496de01 | 293 | that.socket.on('data', function () { |
8b967a99 | 294 | socketOnData.apply(that, arguments); |
b156e01a | 295 | }); |
32a09dc1 | 296 | |
e991ec2f | 297 | that.socket.on('close', function socketCloseCb(had_error) { |
7746ca17 D |
298 | // If that.connected is false, we never actually managed to connect |
299 | var was_connected = that.connected, | |
300 | had_registered = that.server.registered, | |
301 | should_reconnect = false; | |
302 | ||
b156e01a | 303 | that.connected = false; |
7746ca17 | 304 | that.server.reset(); |
cc3c5d04 D |
305 | |
306 | // Remove this socket form the identd lookup | |
9e225dc6 D |
307 | if (that.identd_port_pair) { |
308 | delete global.clients.port_pairs[that.identd_port_pair]; | |
cc3c5d04 D |
309 | } |
310 | ||
7746ca17 D |
311 | should_reconnect = (!that.requested_disconnect && was_connected && had_registered); |
312 | ||
313 | if (should_reconnect) { | |
314 | that.emit('reconnecting'); | |
315 | } else { | |
316 | that.emit('close', had_error); | |
317 | } | |
db8af19d | 318 | |
b156e01a JA |
319 | // Close the whole socket down |
320 | that.disposeSocket(); | |
7746ca17 D |
321 | |
322 | // If this socket closing was not expected and we did actually connect and | |
323 | // we did previously completely register on the network, then reconnect | |
324 | if (should_reconnect) { | |
325 | setTimeout(function() { | |
326 | that.connect(); | |
327 | }, 3000); | |
328 | } | |
b156e01a | 329 | }); |
7496de01 | 330 | }); |
2a8e95d1 | 331 | }; |
2a8e95d1 | 332 | |
3c91bff8 JA |
333 | /** |
334 | * Send an event to the client | |
335 | */ | |
336 | IrcConnection.prototype.clientEvent = function (event_name, data, callback) { | |
cbdab563 | 337 | data.connection_id = this.con_num; |
3c91bff8 JA |
338 | this.state.sendIrcCommand(event_name, data, callback); |
339 | }; | |
db8af19d D |
340 | |
341 | /** | |
342 | * Write a line of data to the IRCd | |
56f0f1b9 D |
343 | * @param data The line of data to be sent |
344 | * @param force Write the data now, ignoring any write queue | |
db8af19d | 345 | */ |
9c6e1a86 | 346 | IrcConnection.prototype.write = function (data, force, force_complete_fn) { |
1a452a02 | 347 | //ENCODE string to encoding of the server |
ae3506f9 | 348 | var encoded_buffer = iconv.encode(data + '\r\n', this.encoding); |
fc1c21b8 | 349 | |
56f0f1b9 | 350 | if (force) { |
9c6e1a86 | 351 | this.socket.write(encoded_buffer, force_complete_fn); |
56f0f1b9 D |
352 | return; |
353 | } | |
354 | ||
fc1c21b8 D |
355 | this.write_buffer.push(encoded_buffer); |
356 | ||
357 | // Only flush if we're not writing already | |
e8c4bb74 | 358 | if (!this.writing_buffer) { |
fc1c21b8 | 359 | this.flushWriteBuffer(); |
e8c4bb74 | 360 | } |
fc1c21b8 D |
361 | }; |
362 | ||
363 | ||
364 | ||
365 | /** | |
366 | * Flush the write buffer to the server in a throttled fashion | |
367 | */ | |
368 | IrcConnection.prototype.flushWriteBuffer = function () { | |
14a18c21 D |
369 | |
370 | // In case the socket closed between writing our queue.. clean up | |
371 | if (!this.connected) { | |
372 | this.write_buffer = []; | |
373 | this.writing_buffer = false; | |
374 | return; | |
375 | } | |
376 | ||
fc1c21b8 D |
377 | this.writing_buffer = true; |
378 | ||
379 | // Disabled write buffer? Send everything we have | |
380 | if (!this.write_buffer_lines_second) { | |
9478baca | 381 | this.write_buffer.forEach(function(buffer) { |
fc1c21b8 D |
382 | this.socket.write(buffer); |
383 | this.write_buffer = null; | |
384 | }); | |
385 | ||
386 | this.write_buffer = []; | |
387 | this.writing_buffer = false; | |
388 | ||
389 | return; | |
390 | } | |
391 | ||
392 | // Nothing to write? Stop writing and leave | |
393 | if (this.write_buffer.length === 0) { | |
394 | this.writing_buffer = false; | |
395 | return; | |
396 | } | |
397 | ||
398 | this.socket.write(this.write_buffer[0]); | |
399 | this.write_buffer = this.write_buffer.slice(1); | |
400 | ||
14a18c21 D |
401 | // Call this function again at some point if we still have data to write |
402 | if (this.write_buffer.length > 0) { | |
403 | setTimeout(this.flushWriteBuffer.bind(this), 1000 / this.write_buffer_lines_second); | |
404 | } else { | |
405 | // No more buffers to write.. so we've finished | |
406 | this.writing_buffer = false; | |
407 | } | |
2a8e95d1 D |
408 | }; |
409 | ||
db8af19d D |
410 | |
411 | ||
412 | /** | |
56f0f1b9 | 413 | * Close the connection to the IRCd after forcing one last line |
db8af19d | 414 | */ |
9478baca | 415 | IrcConnection.prototype.end = function (data) { |
9c6e1a86 D |
416 | var that = this; |
417 | ||
e8c4bb74 | 418 | if (!this.socket) { |
56f0f1b9 | 419 | return; |
e8c4bb74 | 420 | } |
56f0f1b9 | 421 | |
7746ca17 D |
422 | this.requested_disconnect = true; |
423 | ||
e8c4bb74 | 424 | if (data) { |
9c6e1a86 D |
425 | // Once the last bit of data has been sent, then re-run this function to close the socket |
426 | this.write(data, true, function() { | |
427 | that.end(); | |
428 | }); | |
429 | ||
430 | return; | |
e8c4bb74 | 431 | } |
32a09dc1 | 432 | |
db8af19d | 433 | this.socket.end(); |
2a8e95d1 D |
434 | }; |
435 | ||
db8af19d D |
436 | |
437 | ||
b0587b80 D |
438 | /** |
439 | * Check if any server capabilities are enabled | |
440 | */ | |
441 | IrcConnection.prototype.capContainsAny = function (caps) { | |
442 | var enabled_caps; | |
443 | ||
444 | if (!caps instanceof Array) { | |
445 | caps = [caps]; | |
446 | } | |
447 | ||
448 | enabled_caps = _.intersection(this.cap.enabled, caps); | |
449 | return enabled_caps.length > 0; | |
450 | }; | |
451 | ||
452 | ||
453 | ||
db8af19d D |
454 | /** |
455 | * Clean up this IrcConnection instance and any sockets | |
456 | */ | |
c08717da | 457 | IrcConnection.prototype.dispose = function () { |
a787f79f D |
458 | // If we're still connected, wait until the socket is closed before disposing |
459 | // so that all the events are still correctly triggered | |
460 | if (this.socket && this.connected) { | |
14a18c21 | 461 | this.end(); |
a787f79f D |
462 | return; |
463 | } | |
464 | ||
465 | if (this.socket) { | |
466 | this.disposeSocket(); | |
a787f79f D |
467 | } |
468 | ||
cefa0900 JA |
469 | _.each(this.irc_users, function (user) { |
470 | user.dispose(); | |
471 | }); | |
472 | _.each(this.irc_channels, function (chan) { | |
473 | chan.dispose(); | |
474 | }); | |
ebe178d6 D |
475 | this.irc_users = undefined; |
476 | this.irc_channels = undefined; | |
477 | ||
478 | this.server.dispose(); | |
479 | this.server = undefined; | |
4c25c0d7 | 480 | |
a787f79f | 481 | this.irc_commands = undefined; |
4c25c0d7 | 482 | |
a787f79f | 483 | EventBinder.unbindIrcEvents('', this.irc_events, this); |
5e211c3d | 484 | |
14a18c21 | 485 | this.removeAllListeners(); |
c08717da D |
486 | }; |
487 | ||
488 | ||
2a8e95d1 | 489 | |
db8af19d D |
490 | /** |
491 | * Clean up any sockets for this IrcConnection | |
492 | */ | |
493 | IrcConnection.prototype.disposeSocket = function () { | |
494 | if (this.socket) { | |
6b8fa7a6 | 495 | this.socket.end(); |
db8af19d D |
496 | this.socket.removeAllListeners(); |
497 | this.socket = null; | |
498 | } | |
2a8e95d1 D |
499 | }; |
500 | ||
d1b3e8b3 VDF |
501 | /** |
502 | * Set a new encoding for this connection | |
503 | * Return true in case of success | |
504 | */ | |
505 | ||
506 | IrcConnection.prototype.setEncoding = function (encoding) { | |
3ddc135a D |
507 | var encoded_test; |
508 | ||
3efb4f33 VDF |
509 | try { |
510 | encoded_test = iconv.encode("TEST", encoding); | |
511 | //This test is done to check if this encoding also supports | |
512 | //the ASCII charset required by the IRC protocols | |
513 | //(Avoid the use of base64 or incompatible encodings) | |
27bca914 | 514 | if (encoded_test == "TEST") { // jshint ignore:line |
3efb4f33 VDF |
515 | this.encoding = encoding; |
516 | return true; | |
517 | } | |
518 | return false; | |
519 | } catch (err) { | |
520 | return false; | |
65c5a477 | 521 | } |
d1b3e8b3 | 522 | }; |
2a8e95d1 | 523 | |
b156e01a JA |
524 | function getConnectionFamily(host, callback) { |
525 | if (net.isIP(host)) { | |
526 | if (net.isIPv4(host)) { | |
3e01340e | 527 | callback(null, 'IPv4', host); |
b156e01a | 528 | } else { |
3e01340e | 529 | callback(null, 'IPv6', host); |
b156e01a JA |
530 | } |
531 | } else { | |
e991ec2f | 532 | dns.resolve6(host, function resolve6Cb(err, addresses) { |
b156e01a JA |
533 | if (!err) { |
534 | callback(null, 'IPv6', addresses[0]); | |
535 | } else { | |
e991ec2f | 536 | dns.resolve4(host, function resolve4Cb(err, addresses) { |
b156e01a JA |
537 | if (!err) { |
538 | callback(null, 'IPv4',addresses[0]); | |
539 | } else { | |
540 | callback(err); | |
541 | } | |
542 | }); | |
543 | } | |
544 | }); | |
545 | } | |
546 | } | |
547 | ||
db8af19d | 548 | |
4c25c0d7 D |
549 | function onChannelJoin(event) { |
550 | var chan; | |
551 | ||
552 | // Only deal with ourselves joining a channel | |
e8c4bb74 | 553 | if (event.nick !== this.nick) { |
4c25c0d7 | 554 | return; |
e8c4bb74 | 555 | } |
4c25c0d7 D |
556 | |
557 | // We should only ever get a JOIN command for a channel | |
558 | // we're not already a member of.. but check we don't | |
559 | // have this channel in case something went wrong somewhere | |
560 | // at an earlier point | |
561 | if (!this.irc_channels[event.channel]) { | |
562 | chan = new IrcChannel(this, event.channel); | |
563 | this.irc_channels[event.channel] = chan; | |
564 | chan.irc_events.join.call(chan, event); | |
565 | } | |
566 | } | |
567 | ||
568 | ||
569 | function onServerConnect(event) { | |
570 | this.nick = event.nick; | |
4c25c0d7 D |
571 | } |
572 | ||
573 | ||
574 | function onUserPrivmsg(event) { | |
575 | var user; | |
576 | ||
577 | // Only deal with messages targetted to us | |
e8c4bb74 | 578 | if (event.channel !== this.nick) { |
4c25c0d7 | 579 | return; |
e8c4bb74 | 580 | } |
4c25c0d7 D |
581 | |
582 | if (!this.irc_users[event.nick]) { | |
583 | user = new IrcUser(this, event.nick); | |
584 | this.irc_users[event.nick] = user; | |
585 | user.irc_events.privmsg.call(user, event); | |
586 | } | |
587 | } | |
588 | ||
589 | ||
9be602fc | 590 | function onUserNick(event) { |
9be602fc | 591 | // Only deal with messages targetted to us |
e8c4bb74 | 592 | if (event.nick !== this.nick) { |
9be602fc | 593 | return; |
e8c4bb74 | 594 | } |
9be602fc D |
595 | |
596 | this.nick = event.newnick; | |
597 | } | |
598 | ||
599 | ||
4c25c0d7 D |
600 | function onUserParts(event) { |
601 | // Only deal with ourselves leaving a channel | |
e8c4bb74 | 602 | if (event.nick !== this.nick) { |
4c25c0d7 | 603 | return; |
e8c4bb74 | 604 | } |
4c25c0d7 D |
605 | |
606 | if (this.irc_channels[event.channel]) { | |
607 | this.irc_channels[event.channel].dispose(); | |
608 | delete this.irc_channels[event.channel]; | |
609 | } | |
610 | } | |
611 | ||
6d2df752 MC |
612 | function onUserKick(event){ |
613 | // Only deal with ourselves being kicked from a channel | |
e8c4bb74 | 614 | if (event.kicked !== this.nick) { |
6d2df752 | 615 | return; |
e8c4bb74 | 616 | } |
6d2df752 MC |
617 | |
618 | if (this.irc_channels[event.channel]) { | |
619 | this.irc_channels[event.channel].dispose(); | |
620 | delete this.irc_channels[event.channel]; | |
621 | } | |
622 | ||
623 | } | |
624 | ||
4c25c0d7 D |
625 | |
626 | ||
4de89532 D |
627 | /** |
628 | * When a socket connects to an IRCd | |
629 | * May be called before any socket handshake are complete (eg. TLS) | |
630 | */ | |
631 | var rawSocketConnect = function(socket) { | |
632 | // Make note of the port numbers for any identd lookups | |
633 | // Nodejs < 0.9.6 has no socket.localPort so check this first | |
353d261a | 634 | if (typeof socket.localPort !== 'undefined') { |
4de89532 D |
635 | this.identd_port_pair = socket.localPort.toString() + '_' + socket.remotePort.toString(); |
636 | global.clients.port_pairs[this.identd_port_pair] = this; | |
637 | } | |
638 | }; | |
639 | ||
4c25c0d7 | 640 | |
db8af19d D |
641 | /** |
642 | * Handle the socket connect event, starting the IRCd registration | |
643 | */ | |
644 | var socketConnectHandler = function () { | |
15fefff7 D |
645 | var that = this, |
646 | connect_data; | |
647 | ||
648 | // Build up data to be used for webirc/etc detection | |
649 | connect_data = { | |
bd7196e1 D |
650 | connection: this, |
651 | ||
652 | // Array of lines to be sent to the IRCd before anything else | |
653 | prepend_data: [] | |
15fefff7 D |
654 | }; |
655 | ||
656 | // Let the webirc/etc detection modify any required parameters | |
266b5087 | 657 | connect_data = findWebIrc.call(this, connect_data); |
15fefff7 | 658 | |
e991ec2f | 659 | global.modules.emit('irc authorize', connect_data).done(function ircAuthorizeCb() { |
63811c17 | 660 | var gecos = that.gecos; |
c14b67ae | 661 | |
63811c17 D |
662 | if (!gecos && global.config.default_gecos) { |
663 | // We don't have a gecos yet, so use the default | |
cb234b7e | 664 | gecos = global.config.default_gecos.toString().replace('%n', that.nick); |
63811c17 D |
665 | gecos = gecos.replace('%h', that.user.hostname); |
666 | ||
667 | } else if (!gecos) { | |
668 | // We don't have a gecos nor a default, so lets set somthing | |
669 | gecos = '[www.kiwiirc.com] ' + that.nick; | |
cb234b7e D |
670 | } |
671 | ||
bd7196e1 D |
672 | // Send any initial data for webirc/etc |
673 | if (connect_data.prepend_data) { | |
674 | _.each(connect_data.prepend_data, function(data) { | |
675 | that.write(data); | |
676 | }); | |
677 | } | |
678 | ||
679 | that.write('CAP LS'); | |
680 | ||
e8c4bb74 | 681 | if (that.password) { |
bd7196e1 | 682 | that.write('PASS ' + that.password); |
e8c4bb74 | 683 | } |
32a09dc1 | 684 | |
bd7196e1 | 685 | that.write('NICK ' + that.nick); |
cb234b7e | 686 | that.write('USER ' + that.username + ' 0 0 :' + gecos); |
32a09dc1 | 687 | |
bd7196e1 D |
688 | that.emit('connected'); |
689 | }); | |
7dfe47c6 | 690 | }; |
2a8e95d1 | 691 | |
15fefff7 | 692 | |
db8af19d D |
693 | |
694 | /** | |
695 | * Load any WEBIRC or alternative settings for this connection | |
696 | * Called in scope of the IrcConnection instance | |
697 | */ | |
15fefff7 | 698 | function findWebIrc(connect_data) { |
db8af19d D |
699 | var webirc_pass = global.config.webirc_pass, |
700 | ip_as_username = global.config.ip_as_username, | |
701 | tmp; | |
702 | ||
15fefff7 D |
703 | |
704 | // Do we have a WEBIRC password for this? | |
bd7196e1 | 705 | if (webirc_pass && webirc_pass[this.irc_host.hostname]) { |
db8af19d | 706 | // Build the WEBIRC line to be sent before IRC registration |
bd7196e1 D |
707 | tmp = 'WEBIRC ' + webirc_pass[this.irc_host.hostname] + ' KiwiIRC '; |
708 | tmp += this.user.hostname + ' ' + this.user.address; | |
db8af19d | 709 | |
15fefff7 D |
710 | connect_data.prepend_data = [tmp]; |
711 | } | |
712 | ||
713 | ||
714 | // Check if we need to pass the users IP as its username/ident | |
bd7196e1 | 715 | if (ip_as_username && ip_as_username.indexOf(this.irc_host.hostname) > -1) { |
15fefff7 | 716 | // Get a hex value of the clients IP |
9478baca | 717 | this.username = this.user.address.split('.').map(function ipSplitMapCb(i){ |
32800bf5 D |
718 | var hex = parseInt(i, 10).toString(16); |
719 | ||
720 | // Pad out the hex value if it's a single char | |
e8c4bb74 | 721 | if (hex.length === 1) { |
32800bf5 | 722 | hex = '0' + hex; |
e8c4bb74 | 723 | } |
32800bf5 D |
724 | |
725 | return hex; | |
15fefff7 D |
726 | }).join(''); |
727 | ||
728 | } | |
729 | ||
730 | return connect_data; | |
731 | } | |
732 | ||
733 | ||
db8af19d | 734 | /** |
8b967a99 | 735 | * Buffer any data we get from the IRCd until we have complete lines. |
db8af19d | 736 | */ |
8b967a99 D |
737 | function socketOnData(data) { |
738 | var data_pos, // Current position within the data Buffer | |
75083de9 D |
739 | line_start = 0, |
740 | lines = [], | |
ae3506f9 | 741 | i, |
f429dec1 JA |
742 | max_buffer_size = 1024; // 1024 bytes is the maximum length of two RFC1459 IRC messages. |
743 | // May need tweaking when IRCv3 message tags are more widespread | |
3ec786bc | 744 | |
a8d8af60 | 745 | // Split data chunk into individual lines |
75083de9 D |
746 | for (data_pos = 0; data_pos < data.length; data_pos++) { |
747 | if (data[data_pos] === 0x0A) { // Check if byte is a line feed | |
748 | lines.push(data.slice(line_start, data_pos)); | |
749 | line_start = data_pos + 1; | |
a8d8af60 JA |
750 | } |
751 | } | |
d1b3e8b3 | 752 | |
75083de9 D |
753 | // No complete lines of data? Check to see if buffering the data would exceed the max buffer size |
754 | if (!lines[0]) { | |
755 | if ((this.held_data ? this.held_data.length : 0 ) + data.length > max_buffer_size) { | |
f429dec1 JA |
756 | // Buffering this data would exeed our max buffer size |
757 | this.emit('error', 'Message buffer too large'); | |
758 | this.socket.destroy(); | |
75083de9 | 759 | |
f429dec1 | 760 | } else { |
75083de9 D |
761 | |
762 | // Append the incomplete line to our held_data and wait for more | |
f429dec1 JA |
763 | if (this.held_data) { |
764 | this.held_data = Buffer.concat([this.held_data, data], this.held_data.length + data.length); | |
765 | } else { | |
766 | this.held_data = data; | |
767 | } | |
f429dec1 | 768 | } |
75083de9 D |
769 | |
770 | // No complete lines to process.. | |
771 | return; | |
f429dec1 JA |
772 | } |
773 | ||
a8d8af60 JA |
774 | // If we have an incomplete line held from the previous chunk of data |
775 | // merge it with the first line from this chunk of data | |
776 | if (this.hold_last && this.held_data !== null) { | |
75083de9 | 777 | lines[0] = Buffer.concat([this.held_data, lines[0]], this.held_data.length + lines[0].length); |
2a8e95d1 | 778 | this.hold_last = false; |
a8d8af60 | 779 | this.held_data = null; |
2a8e95d1 | 780 | } |
db8af19d | 781 | |
a8d8af60 JA |
782 | // If the last line of data in this chunk is not complete, hold it so |
783 | // it can be merged with the first line from the next chunk | |
75083de9 D |
784 | if (line_start < data_pos) { |
785 | if ((data.length - line_start) > max_buffer_size) { | |
f429dec1 JA |
786 | // Buffering this data would exeed our max buffer size |
787 | this.emit('error', 'Message buffer too large'); | |
788 | this.socket.destroy(); | |
789 | return; | |
790 | } | |
75083de9 | 791 | |
2a8e95d1 | 792 | this.hold_last = true; |
75083de9 D |
793 | this.held_data = new Buffer(data.length - line_start); |
794 | data.copy(this.held_data, 0, line_start); | |
2a8e95d1 | 795 | } |
db8af19d | 796 | |
a8d8af60 | 797 | // Process our data line by line |
e8c4bb74 | 798 | for (i = 0; i < lines.length; i++) { |
8b967a99 | 799 | parseIrcLine.call(this, lines[i]); |
e8c4bb74 | 800 | } |
8b967a99 D |
801 | |
802 | } | |
803 | ||
804 | ||
805 | ||
806 | /** | |
807 | * The regex that parses a line of data from the IRCd | |
808 | * Deviates from the RFC a little to support the '/' character now used in some | |
809 | * IRCds | |
810 | */ | |
b38c590f | 811 | var parse_regex = /^(?:(?:(?:@([^ ]+) )?):(?:([^\s!]+)|([^\s!]+)!([^\s@]+)@?([^\s]+)?) )?(\S+)(?: (?!:)(.+?))?(?: :(.*))?$/i; |
8b967a99 D |
812 | |
813 | function parseIrcLine(buffer_line) { | |
814 | var msg, | |
17dfa698 | 815 | i, |
8b967a99 D |
816 | tags = [], |
817 | tag, | |
17dfa698 | 818 | line = '', |
c61b3f74 | 819 | msg_obj; |
8b967a99 D |
820 | |
821 | // Decode server encoding | |
822 | line = iconv.decode(buffer_line, this.encoding); | |
17dfa698 JA |
823 | if (!line) { |
824 | return; | |
825 | } | |
8b967a99 D |
826 | |
827 | // Parse the complete line, removing any carriage returns | |
828 | msg = parse_regex.exec(line.replace(/^\r+|\r+$/, '')); | |
829 | ||
830 | if (!msg) { | |
831 | // The line was not parsed correctly, must be malformed | |
94b00b2d | 832 | winston.warn('Malformed IRC line: %s', line.replace(/^\r+|\r+$/, '')); |
8b967a99 D |
833 | return; |
834 | } | |
835 | ||
836 | // Extract any tags (msg[1]) | |
837 | if (msg[1]) { | |
838 | tags = msg[1].split(';'); | |
839 | ||
17dfa698 JA |
840 | for (i = 0; i < tags.length; i++) { |
841 | tag = tags[i].split('='); | |
842 | tags[i] = {tag: tag[0], value: tag[1]}; | |
2a8e95d1 D |
843 | } |
844 | } | |
8b967a99 | 845 | |
c61b3f74 | 846 | msg_obj = { |
8b967a99 D |
847 | tags: tags, |
848 | prefix: msg[2], | |
849 | nick: msg[3], | |
850 | ident: msg[4], | |
851 | hostname: msg[5] || '', | |
852 | command: msg[6], | |
17dfa698 | 853 | params: msg[7] ? msg[7].split(/ +/) : [] |
8b967a99 D |
854 | }; |
855 | ||
17dfa698 | 856 | if (msg[8]) { |
c61b3f74 | 857 | msg_obj.params.push(msg[8].trim()); |
17dfa698 JA |
858 | } |
859 | ||
138d8d17 | 860 | this.irc_commands.dispatch(new IrcCommands.Command(msg_obj.command.toUpperCase(), msg_obj)); |
8b967a99 | 861 | } |