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