fix typo
[KiwiIRC.git] / server / irc / connection.js
index cb4b4f2dd9e17153a0839cd2ab530698877685f3..b9c130586a3596ba527d5fb44c97ccb2925b5a09 100644 (file)
@@ -2,15 +2,14 @@ var net     = require('net'),
     tls     = require('tls'),
     events  = require('events'),
     util    = require('util'),
-    _       = require('underscore'),
-    config  = require('../configuration.js');
+    _       = require('underscore');
 
 var IrcConnection = function (hostname, port, ssl, nick, user, pass) {
     var that = this;
     events.EventEmitter.call(this);
     
     if (ssl) {
-        this.socket = tls.connect(port, hostname, {}, connect_handler);
+        this.socket = tls.connect({host: hostname, port: port, rejectUnauthorized: global.config.reject_unauthorised_certificates}, connect_handler);
     } else {
         this.socket = net.createConnection(port, hostname);
         this.socket.on('connect', function () {
@@ -34,11 +33,14 @@ var IrcConnection = function (hostname, port, ssl, nick, user, pass) {
     
     this.connected = false;
     this.registered = false;
+    this.cap_negotiation = true;
     this.nick = nick;
     this.user = user;
     this.irc_host = {hostname: hostname, port: port};
     this.ssl = !(!ssl);
     this.options = Object.create(null);
+    this.cap = {requested: [], enabled: []};
+    this.sasl = false;
     
     this.password = pass;
     this.hold_last = false;
@@ -94,17 +96,30 @@ var connect_handler = function () {
         });
     }
 
-    if (this.password) {
-        this.write('PASS ' + this.password);
-    }
-    
-    this.write('NICK ' + connect_data.nick);
-    this.write('USER ' + connect_data.username + ' 0 0 :' + connect_data.realname);
+    this.write('CAP LS');
+
+    this.registration_timeout = setTimeout(function () {
+        that.register.call(that);
+    }, 1000);
     
     this.connected = true;
     this.emit('connected');
 };
 
+IrcConnection.prototype.register = function () {
+    if (this.registration_timeout !== null) {
+        clearTimeout(this.registeration_timeout);
+        this.registration_timeout = null;
+    }
+    if ((this.password) && (!this.sasl)) {
+        this.write('PASS ' + this.password);
+    }
+    this.write('NICK ' + this.nick);
+    this.write('USER ' + this.nick.replace(/[^0-9a-zA-Z\-_.]/, '') + ' 0 0 :' + '[www.kiwiirc.com] ' + this.nick);
+    if (this.cap_negotation) {
+        this.write('CAP END');
+    }
+};
 
 
 
@@ -135,12 +150,15 @@ function findWebIrc(connect_data) {
 
 
 
-parse_regex = /^(?::(?:([a-z0-9\x5B-\x60\x7B-\x7D\.\-]+)|([a-z0-9\x5B-\x60\x7B-\x7D\.\-]+)!([a-z0-9~\.\-_|]+)@?([a-z0-9\.\-:\/]+)?) )?(\S+)(?: (?!:)(.+?))?(?: :(.+))?$/i;
+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 !== '')) {
         data = this.held_data + data;
@@ -161,14 +179,22 @@ var parse = function (data) {
             // We have a complete line of data, parse it!
             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 = {
-                    prefix:     msg[1],
-                    nick:       msg[2],
-                    ident:      msg[3],
-                    hostname:   msg[4] || '',
-                    command:    msg[5],
-                    params:     msg[6] || '',
-                    trailing:   (msg[7]) ? msg[7].trim() : ''
+                    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(' ');