Fix parse_regex
[KiwiIRC.git] / server / irc / connection.js
index bc852997056041c29c22aade175026e9e3e8029a..49c7c810e5524e73e4e259f84e21a28fb2105724 100644 (file)
@@ -247,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) {
@@ -562,6 +562,8 @@ var socketConnectHandler = function () {
     connect_data = findWebIrc.call(this, connect_data);
 
     global.modules.emit('irc authorize', connect_data).done(function ircAuthorizeCb() {
+        var gecos = '[www.kiwiirc.com] ' + that.nick;
+
         // Send any initial data for webirc/etc
         if (connect_data.prepend_data) {
             _.each(connect_data.prepend_data, function(data) {
@@ -575,7 +577,7 @@ var socketConnectHandler = function () {
             that.write('PASS ' + that.password);
 
         that.write('NICK ' + that.nick);
-        that.write('USER ' + that.username + ' 0 0 :' + '[www.kiwiirc.com] ' + that.nick);
+        that.write('USER ' + that.username + ' 0 0 :' + (global.config.default_gecos || gecos));
 
         that.emit('connected');
     });
@@ -622,20 +624,11 @@ function findWebIrc(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
+ * Buffer any data we get from the IRCd until we have complete lines.
  */
-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 msg,
-        i, j,
-        data_pos,               // Current position within the data Buffer
-        tags = [],
-        tag,
+function socketOnData(data) {
+    var data_pos,               // Current position within the data Buffer
         line_start = 0,
         lines = [],
         line = '',
@@ -694,42 +687,62 @@ var parse = function (data) {
         data.copy(this.held_data, 0, line_start);
     }
 
-    // Don't need the data buffer any more, get rid of it
-    data = null;
-
     // Process our data line by line
-    for (i = 0; i < lines.length; i++) {
-        // Decode server encoding
-        line = iconv.decode(lines[i], this.encoding);
-        lines[i] = null;
-        if (!line) break;
-
-        // Parse the complete line, removing any carriage returns
-        msg = parse_regex.exec(line.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: " + line.replace(/^\r+|\r+$/, ''));
+    for (i = 0; i < lines.length; i++)
+        parseIrcLine.call(this, lines[i]);
+
+}
+
+
+
+/**
+ * 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\.\-*]+)!([^\x00\r\n\ ]+?)@?([a-z0-9\.\-:\/_]+)?) )?(\S+)(?: (?!:)(.+?))?(?: :(.+))?$/i;
+
+function parseIrcLine(buffer_line) {
+    var msg,
+        i, j,
+        tags = [],
+        tag,
+        line = '';
+
+    // Decode server encoding
+    line = iconv.decode(buffer_line, this.encoding);
+    if (!line) return;
+
+    // Parse the complete line, removing any carriage returns
+    msg = parse_regex.exec(line.replace(/^\r+|\r+$/, ''));
+
+    if (!msg) {
+        // The line was not parsed correctly, must be malformed
+        console.log("Malformed IRC line: " + line.replace(/^\r+|\r+$/, ''));
+        return;
+    }
+
+    // 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);
+}