Pass callback to state.connect, fix vars
[KiwiIRC.git] / server / irc / connection.js
index 7c3940b1ff2e3ba20f2ade815bc3e922ea4acd03..0d927c22ab6861639c07596583c6f2c1598e9791 100644 (file)
 var net     = require('net'),
     tls     = require('tls'),
     events  = require('events'),
-    util    = require('util');
+    util    = require('util'),
+    _       = require('lodash');
 
-var IrcConnection = function (hostname, port, ssl, nick, user, pass, webirc) {
+
+var IrcConnection = function (hostname, port, ssl, nick, user, pass, state) {
     var that = this;
     events.EventEmitter.call(this);
     
-    if (ssl) {
-        this.socket = tls.connect(port, hostname, {}, connect_handler);
+    // Socket state
+    this.connected = false;
+
+    // If registeration with the IRCd has completed
+    this.registered = false;
+
+    // If we are in the CAP negotiation stage
+    this.cap_negotiation = true;
+
+    // User information
+    this.nick = nick;
+    this.user = user;  // Contains users real hostname and address
+    this.username = this.nick.replace(/[^0-9a-zA-Z\-_.]/, '');
+    this.password = pass;
+    
+    // State object
+    this.state = state;
+
+    // IRC connection information
+    this.irc_host = {hostname: hostname, port: port};
+    this.ssl = !(!ssl);
+
+    // Options sent by the IRCd
+    this.options = Object.create(null);
+    this.cap = {requested: [], enabled: []};
+
+    // Is SASL supported on the IRCd
+    this.sasl = false;
+    
+    // Buffers for data sent from the IRCd
+    this.hold_last = false;
+    this.held_data = '';
+
+
+    // Call any modules before making the connection
+    global.modules.emit('irc:connecting', {connection: this})
+        .done(function () {
+            that.connect();
+        });
+};
+util.inherits(IrcConnection, events.EventEmitter);
+
+module.exports.IrcConnection = IrcConnection;
+
+
+
+
+/**
+ * Start the connection to the IRCd
+ */
+IrcConnection.prototype.connect = function () {
+    var that = this;
+
+    // The socket connect event to listener for
+    var socket_connect_event_name = 'connect';
+
+
+    // Make sure we don't already have an open connection
+    this.disposeSocket();
+
+    // Open either a secure or plain text socket
+    if (this.ssl) {
+        this.socket = tls.connect({
+            host: this.irc_host.hostname,
+            port: this.irc_host.port,
+            rejectUnauthorized: global.config.reject_unauthorised_certificates
+        });
+
+        socket_connect_event_name = 'secureConnect';
+
     } else {
-        this.socket = net.createConnection(port, hostname);
-        this.socket.on('connect', function () {
-            connect_handler.apply(that, arguments);
+        this.socket = net.connect({
+            host: this.irc_host.hostname,
+            port: this.irc_host.port
         });
     }
-    
+
+    this.socket.setEncoding('utf-8');
+
+    this.socket.on(socket_connect_event_name, function () {
+        that.connected = true;
+        socketConnectHandler.apply(that, arguments);
+    });
+
     this.socket.on('error', function (event) {
         that.emit('error', event);
+
     });
     
-    this.socket.setEncoding('utf-8');
-    
     this.socket.on('data', function () {
         parse.apply(that, arguments);
     });
     
-    this.socket.on('close', function () {
+    this.socket.on('close', function (had_error) {
+        that.connected = false;
         that.emit('close');
+
+        // Close the whole socket down
+        that.disposeSocket();
     });
-    
-    this.connected = false;
-    this.registered = false;
-    this.nick = nick;
-    this.user = user;
-    this.ssl = !(!ssl);
-    this.options = Object.create(null);
-    
-    this.webirc = webirc;
-    this.password = pass;
-    this.hold_last = false;
-    this.held_data = '';
 };
-util.inherits(IrcConnection, events.EventEmitter);
 
-module.exports.IrcConnection = IrcConnection;
 
 
+/**
+ * Write a line of data to the IRCd
+ */
 IrcConnection.prototype.write = function (data, callback) {
-    write.call(this, data + '\r\n', 'utf-8', callback);
+    this.socket.write(data + '\r\n', 'utf-8', callback);
 };
 
+
+
+/**
+ * Close the connection to the IRCd after sending one last line
+ */
 IrcConnection.prototype.end = function (data, callback) {
-    console.log('Closing IRC socket');
-    end.call(this, data + '\r\n', 'utf-8', callback);
+    if (data)
+        this.write(data);
+    
+    this.socket.end();
 };
 
-var write = function (data, encoding, callback) {
-    this.socket.write(data, encoding, callback);
+
+
+/**
+ * Clean up this IrcConnection instance and any sockets
+ */
+IrcConnection.prototype.dispose = function () {
+    this.disposeSocket();
+    this.removeAllListeners();
 };
 
-var end = function (data, encoding, callback) {
-    this.socket.end(data, encoding, callback);
+
+
+/**
+ * Clean up any sockets for this IrcConnection
+ */
+IrcConnection.prototype.disposeSocket = function () {
+    if (this.socket) {
+        this.socket.removeAllListeners();
+        this.socket = null;
+    }
 };
 
 
-var connect_handler = function () {
-    if (this.webirc) {
-        this.write('WEBIRC ' + this.webirc.pass + ' KiwiIRC ' + this.user.hostname + ' ' + this.user.address);
+
+/**
+ * Handle the socket connect event, starting the IRCd registration
+ */
+var socketConnectHandler = function () {
+    var that = this,
+        connect_data;
+
+    // Build up data to be used for webirc/etc detection
+    connect_data = {
+        connection: this,
+
+        // Array of lines to be sent to the IRCd before anything else
+        prepend_data: []
+    };
+
+    // Let the webirc/etc detection modify any required parameters
+    connect_data = findWebIrc.call(this, connect_data);
+
+    global.modules.emit('irc:authorize', connect_data).done(function () {
+        // Send any initial data for webirc/etc
+        if (connect_data.prepend_data) {
+            _.each(connect_data.prepend_data, function(data) {
+                that.write(data);
+            });
+        }
+
+        that.write('CAP LS');
+
+        if (that.password)
+            that.write('PASS ' + that.password);
+        
+        that.write('NICK ' + that.nick);
+        that.write('USER ' + that.username + ' 0 0 :' + '[www.kiwiirc.com] ' + that.nick);
+        
+        that.emit('connected');
+    });
+};
+
+
+
+/**
+ * Load any WEBIRC or alternative settings for this connection
+ * Called in scope of the IrcConnection instance
+ */
+function findWebIrc(connect_data) {
+    var webirc_pass = global.config.webirc_pass,
+        ip_as_username = global.config.ip_as_username,
+        tmp;
+
+
+    // Do we have a WEBIRC password for this?
+    if (webirc_pass && webirc_pass[this.irc_host.hostname]) {
+        // Build the WEBIRC line to be sent before IRC registration
+        tmp = 'WEBIRC ' + webirc_pass[this.irc_host.hostname] + ' KiwiIRC ';
+        tmp += this.user.hostname + ' ' + this.user.address;
+
+        connect_data.prepend_data = [tmp];
     }
-    if (this.password) {
-        this.write('PASS ' + this.password);
+
+
+    // Check if we need to pass the users IP as its username/ident
+    if (ip_as_username && ip_as_username.indexOf(this.irc_host.hostname) > -1) {
+        // Get a hex value of the clients IP
+        this.username = this.user.address.split('.').map(function(i, idx){
+            return parseInt(i, 10).toString(16);
+        }).join('');
+
     }
-    //this.write('CAP LS');
-    this.write('NICK ' + this.nick);
-    this.write('USER kiwi_' + this.nick.replace(/[^0-9a-zA-Z\-_.]/, '') + ' 0 0 :' + this.nick);
-    
-    this.connected = true;
-    console.log("IrcConnection.emit('connected')");
-    this.emit('connected');
-};
 
-parse_regex = /^(?::(?:([a-z0-9\x5B-\x60\x7B-\x7D\.\-]+)|([a-z0-9\x5B-\x60\x7B-\x7D\.\-]+)!([a-z0-9~\.\-_|]+)@?([a-z0-9\.\-:\/]+)?) )?(\S+)(?: (?!:)(.+?))?(?: :(.+))?$/i;
+    return connect_data;
+}
+
+
+
+/**
+ * The regex that parses a line of data from the IRCd
+ * Deviates from the RFC a little to support the '/' character now used in some
+ * IRCds
+ */
+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;
 
 var parse = function (data) {
     var i,
         msg,
-               msg2,
-               trm;
+        msg2,
+        trm,
+        j,
+        tags = [],
+        tag;
     
-    if ((this.hold_last) && (this.held_data !== '')) {
+    if (this.hold_last && this.held_data !== '') {
         data = this.held_data + data;
         this.hold_last = false;
         this.held_data = '';
     }
+
+    // If the last line is incomplete, hold it until we have more data
     if (data.substr(-1) !== '\n') {
         this.hold_last = true;
     }
+
+    // Process our data line by line
     data = data.split("\n");
     for (i = 0; i < data.length; i++) {
-        if (data[i]) {
-            if ((this.hold_last) && (i === data.length - 1)) {
-                this.held_data = data[i];
-                break;
-            }
+        if (!data[i]) break;
 
-            // We have a complete line of data, parse it!
-            msg = parse_regex.exec(data[i].replace(/^\r+|\r+$/, ''));
-            if (msg) {
-                msg = {
-                    prefix:     msg[1],
-                    nick:       msg[2],
-                    ident:      msg[3],
-                    hostname:   msg[4] || '',
-                    command:    msg[5],
-                    params:     msg[6] || '',
-                    trailing:   (msg[7]) ? msg[7].trim() : ''
-                };
-                msg.params = msg.params.split(' ');
-
-                this.emit('irc_' + msg.command.toUpperCase(), msg);
-            } else {
-                console.log("Malformed IRC line: " + data[i].replace(/^\r+|\r+$/, ''));
+        // If flagged to hold the last line, store it and move on
+        if (this.hold_last && (i === data.length - 1)) {
+            this.held_data = data[i];
+            break;
+        }
+
+        // Parse the complete line, removing any carriage returns
+        msg = parse_regex.exec(data[i].replace(/^\r+|\r+$/, ''));
+
+        if (msg) {
+            if (msg[1]) {
+                tags = msg[1].split(';');
+                for (j = 0; j < tags.length; j++) {
+                    tag = tags[j].split('=');
+                    tags[j] = {tag: tag[0], value: tag[1]};
+                }
             }
+            msg = {
+                tags:       tags,
+                prefix:     msg[2],
+                nick:       msg[3],
+                ident:      msg[4],
+                hostname:   msg[5] || '',
+                command:    msg[6],
+                params:     msg[7] || '',
+                trailing:   (msg[8]) ? msg[8].trim() : ''
+            };
+            msg.params = msg.params.split(' ');
+
+            this.emit('irc_' + msg.command.toUpperCase(), msg);
+
+        } else {
+
+            // The line was not parsed correctly, must be malformed
+            console.log("Malformed IRC line: " + data[i].replace(/^\r+|\r+$/, ''));
         }
     }
 };