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