Separate IRC line parsing and socket onData parsing
[KiwiIRC.git] / server / irc / connection.js
index d61d0641ce1274a97ac4f85cfe803192ea33ad5a..8b1d3ba72a0f7fb24ff54c2cfb3f5d2e93843088 100644 (file)
@@ -23,7 +23,7 @@ if (version_values[1] >= 10) {
     Socks = require('socksjs');
 }
 
-var IrcConnection = function (hostname, port, ssl, nick, user, pass, state, con_num) {
+var IrcConnection = function (hostname, port, ssl, nick, user, options, state, con_num) {
     var that = this;
 
     EE.call(this,{
@@ -32,9 +32,8 @@ var IrcConnection = function (hostname, port, ssl, nick, user, pass, state, con_
     });
     this.setMaxListeners(0);
 
-    // Set the first configured encoding as the default encoding
-    this.encoding = global.config.default_encoding;
-    
+    options = options || {};
+
     // Socket state
     this.connected = false;
 
@@ -57,7 +56,12 @@ var IrcConnection = function (hostname, port, ssl, nick, user, pass, state, con_
     this.nick = nick;
     this.user = user;  // Contains users real hostname and address
     this.username = this.nick.replace(/[^0-9a-zA-Z\-_.\/]/, '');
-    this.password = pass;
+    this.password = options.password || '';
+
+    // Set the passed encoding. or the default if none giving or it fails
+    if (!options.encoding || !this.setEncoding(options.encoding)) {
+        this.setEncoding(global.config.default_encoding);
+    }
 
     // State object
     this.state = state;
@@ -106,7 +110,7 @@ var IrcConnection = function (hostname, port, ssl, nick, user, pass, state, con_
 
     // Buffers for data sent from the IRCd
     this.hold_last = false;
-    this.held_data = '';
+    this.held_data = null;
 
     this.applyIrcEvents();
 };
@@ -243,7 +247,7 @@ IrcConnection.prototype.connect = function () {
         });
 
         that.socket.on('data', function () {
-            parse.apply(that, arguments);
+            socketOnData.apply(that, arguments);
         });
 
         that.socket.on('close', function socketCloseCb(had_error) {
@@ -618,6 +622,76 @@ function findWebIrc(connect_data) {
 }
 
 
+/**
+ * Buffer any data we get from the IRCd until we have complete lines.
+ */
+function socketOnData(data) {
+    var data_pos,               // Current position within the data Buffer
+        line_start = 0,
+        lines = [],
+        line = '',
+        max_buffer_size = 1024; // 1024 bytes is the maximum length of two RFC1459 IRC messages.
+                                // May need tweaking when IRCv3 message tags are more widespread
+
+    // Split data chunk into individual lines
+    for (data_pos = 0; data_pos < data.length; data_pos++) {
+        if (data[data_pos] === 0x0A) { // Check if byte is a line feed
+            lines.push(data.slice(line_start, data_pos));
+            line_start = data_pos + 1;
+        }
+    }
+
+    // No complete lines of data? Check to see if buffering the data would exceed the max buffer size
+    if (!lines[0]) {
+        if ((this.held_data ? this.held_data.length : 0 ) + data.length > max_buffer_size) {
+            // Buffering this data would exeed our max buffer size
+            this.emit('error', 'Message buffer too large');
+            this.socket.destroy();
+
+        } else {
+
+            // Append the incomplete line to our held_data and wait for more
+            if (this.held_data) {
+                this.held_data = Buffer.concat([this.held_data, data], this.held_data.length + data.length);
+            } else {
+                this.held_data = data;
+            }
+        }
+
+        // No complete lines to process..
+        return;
+    }
+
+    // If we have an incomplete line held from the previous chunk of data
+    // merge it with the first line from this chunk of data
+    if (this.hold_last && this.held_data !== null) {
+        lines[0] = Buffer.concat([this.held_data, lines[0]], this.held_data.length + lines[0].length);
+        this.hold_last = false;
+        this.held_data = null;
+    }
+
+    // If the last line of data in this chunk is not complete, hold it so
+    // it can be merged with the first line from the next chunk
+    if (line_start < data_pos) {
+        if ((data.length - line_start) > max_buffer_size) {
+            // Buffering this data would exeed our max buffer size
+            this.emit('error', 'Message buffer too large');
+            this.socket.destroy();
+            return;
+        }
+
+        this.hold_last = true;
+        this.held_data = new Buffer(data.length - line_start);
+        data.copy(this.held_data, 0, line_start);
+    }
+
+    // Process our data line by line
+    for (i = 0; i < lines.length; i++)
+        parseIrcLine.call(this, lines[i]);
+
+}
+
+
 
 /**
  * The regex that parses a line of data from the IRCd
@@ -626,66 +700,47 @@ function findWebIrc(connect_data) {
  */
 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;
 
-var parse = function (data) {
-    var i,
-        msg,
-        msg2,
-        trm,
-        j,
+function parseIrcLine(buffer_line) {
+    var msg,
+        i, j,
         tags = [],
-        tag;
+        tag,
+        line = '';
 
-    //DECODE server encoding 
-    data = iconv.decode(data, this.encoding);
+    // Decode server encoding
+    line = iconv.decode(buffer_line, this.encoding);
+    if (!line) return;
 
-    if (this.hold_last && this.held_data !== '') {
-        data = this.held_data + data;
-        this.hold_last = false;
-        this.held_data = '';
-    }
+    // Parse the complete line, removing any carriage returns
+    msg = parse_regex.exec(line.replace(/^\r+|\r+$/, ''));
 
-    // If the last line is incomplete, hold it until we have more data
-    if (data.substr(-1) !== '\n') {
-        this.hold_last = true;
+    if (!msg) {
+        // The line was not parsed correctly, must be malformed
+        console.log("Malformed IRC line: " + line.replace(/^\r+|\r+$/, ''));
+        return;
     }
 
-    // Process our data line by line
-    data = data.split("\n");
-    for (i = 0; i < data.length; i++) {
-        if (!data[i]) break;
-
-        // 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.irc_commands.dispatch(msg.command.toUpperCase(), msg);
-        } else {
-            // The line was not parsed correctly, must be malformed
-            console.log("Malformed IRC line: " + data[i].replace(/^\r+|\r+$/, ''));
+    // Extract any tags (msg[1])
+    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.irc_commands.dispatch(msg.command.toUpperCase(), msg);
+}