1 var net
= require('net'),
3 events
= require('events'),
4 util
= require('util'),
8 var IrcConnection = function (hostname
, port
, ssl
, nick
, user
, pass
) {
10 events
.EventEmitter
.call(this);
13 this.connected
= false;
15 // If registeration with the IRCd has completed
16 this.registered
= false;
18 // If we are in the CAP negotiation stage
19 this.cap_negotiation
= true;
23 this.user
= user
; // Contains users real hostname and address
24 this.username
= this.nick
.replace(/[^0-9a-zA-Z\-_.]/, '');
27 // IRC connection information
28 this.irc_host
= {hostname
: hostname
, port
: port
};
31 // Options sent by the IRCd
32 this.options
= Object
.create(null);
33 this.cap
= {requested
: [], enabled
: []};
35 // Is SASL supported on the IRCd
38 // Buffers for data sent from the IRCd
39 this.hold_last
= false;
43 // Call any modules before making the connection
44 global
.modules
.emit('irc:connecting', {connection
: this})
49 util
.inherits(IrcConnection
, events
.EventEmitter
);
51 module
.exports
.IrcConnection
= IrcConnection
;
57 * Start the connection to the IRCd
59 IrcConnection
.prototype.connect = function () {
62 // The socket connect event to listener for
63 var socket_connect_event_name
= 'connect';
66 // Make sure we don't already have an open connection
69 // Open either a secure or plain text socket
71 this.socket
= tls
.connect({
72 host
: this.irc_host
.hostname
,
73 port
: this.irc_host
.port
,
74 rejectUnauthorized
: global
.config
.reject_unauthorised_certificates
77 socket_connect_event_name
= 'secureConnect';
80 this.socket
= net
.connect({
81 host
: this.irc_host
.hostname
,
82 port
: this.irc_host
.port
86 this.socket
.setEncoding('utf-8');
88 this.socket
.on(socket_connect_event_name
, function () {
89 that
.connected
= true;
90 socketConnectHandler
.apply(that
, arguments
);
93 this.socket
.on('error', function (event
) {
94 that
.emit('error', event
);
98 this.socket
.on('data', function () {
99 parse
.apply(that
, arguments
);
102 this.socket
.on('close', function (had_error
) {
103 that
.connected
= false;
106 // Close the whole socket down
107 that
.disposeSocket();
114 * Write a line of data to the IRCd
116 IrcConnection
.prototype.write = function (data
, callback
) {
117 this.socket
.write(data
+ '\r\n', 'utf-8', callback
);
123 * Close the connection to the IRCd after sending one last line
125 IrcConnection
.prototype.end = function (data
, callback
) {
135 * Clean up this IrcConnection instance and any sockets
137 IrcConnection
.prototype.dispose = function () {
138 this.disposeSocket();
139 this.removeAllListeners();
145 * Clean up any sockets for this IrcConnection
147 IrcConnection
.prototype.disposeSocket = function () {
149 this.socket
.removeAllListeners();
157 * Handle the socket connect event, starting the IRCd registration
159 var socketConnectHandler = function () {
163 // Build up data to be used for webirc/etc detection
167 // Array of lines to be sent to the IRCd before anything else
171 // Let the webirc/etc detection modify any required parameters
172 connect_data
= findWebIrc
.call(this, connect_data
);
174 global
.modules
.emit('irc:authorize', connect_data
).done(function () {
175 // Send any initial data for webirc/etc
176 if (connect_data
.prepend_data
) {
177 _
.each(connect_data
.prepend_data
, function(data
) {
182 that
.write('CAP LS');
185 that
.write('PASS ' + that
.password
);
187 that
.write('NICK ' + that
.nick
);
188 that
.write('USER ' + that
.username
+ ' 0 0 :' + '[www.kiwiirc.com] ' + that
.nick
);
190 that
.emit('connected');
197 * Load any WEBIRC or alternative settings for this connection
198 * Called in scope of the IrcConnection instance
200 function findWebIrc(connect_data
) {
201 var webirc_pass
= global
.config
.webirc_pass
,
202 ip_as_username
= global
.config
.ip_as_username
,
206 // Do we have a WEBIRC password for this?
207 if (webirc_pass
&& webirc_pass
[this.irc_host
.hostname
]) {
208 // Build the WEBIRC line to be sent before IRC registration
209 tmp
= 'WEBIRC ' + webirc_pass
[this.irc_host
.hostname
] + ' KiwiIRC ';
210 tmp
+= this.user
.hostname
+ ' ' + this.user
.address
;
212 connect_data
.prepend_data
= [tmp
];
216 // Check if we need to pass the users IP as its username/ident
217 if (ip_as_username
&& ip_as_username
.indexOf(this.irc_host
.hostname
) > -1) {
218 // Get a hex value of the clients IP
219 this.username
= this.user
.address
.split('.').map(function(i
, idx
){
220 return parseInt(i
, 10).toString(16);
231 * The regex that parses a line of data from the IRCd
232 * Deviates from the RFC a little to support the '/' character now used in some
235 var parse_regex
= /^(?:(?:(?:(@[^ ]+) )?):(?:([a-z0-9\x5B-\x60\x7B-\x7D\.\-]+)|([a-z0-9\x5B-\x60\x7B-\x7D\.\-]+)!([a-z0-9~\.\-_|]+)@?([a-z0-9\.\-:\/_]+)?) )?(\S+)(?: (?!:)(.+?))?(?: :(.+))?$/i;
237 var parse = function (data
) {
246 if (this.hold_last
&& this.held_data
!== '') {
247 data
= this.held_data
+ data
;
248 this.hold_last
= false;
252 // If the last line is incomplete, hold it until we have more data
253 if (data
.substr(-1) !== '\n') {
254 this.hold_last
= true;
257 // Process our data line by line
258 data
= data
.split("\n");
259 for (i
= 0; i
< data
.length
; i
++) {
262 // If flagged to hold the last line, store it and move on
263 if (this.hold_last
&& (i
=== data
.length
- 1)) {
264 this.held_data
= data
[i
];
268 // Parse the complete line, removing any carriage returns
269 msg
= parse_regex
.exec(data
[i
].replace(/^\r+|\r+$/, ''));
273 tags
= msg
[1].split(';');
274 for (j
= 0; j
< tags
.length
; j
++) {
275 tag
= tags
[j
].split('=');
276 tags
[j
] = {tag
: tag
[0], value
: tag
[1]};
284 hostname
: msg
[5] || '',
286 params
: msg
[7] || '',
287 trailing
: (msg
[8]) ? msg
[8].trim() : ''
289 msg
.params
= msg
.params
.split(' ');
291 this.emit('irc_' + msg
.command
.toUpperCase(), msg
);
295 // The line was not parsed correctly, must be malformed
296 console
.log("Malformed IRC line: " + data
[i
].replace(/^\r+|\r+$/, ''));