diff --git a/lib/irc.js b/lib/irc.js index b84c638b..2ed4b77d 100644 --- a/lib/irc.js +++ b/lib/irc.js @@ -461,6 +461,7 @@ Client.prototype.prefixForMode = {}; Client.prototype.modeForPrefix = {}; Client.prototype.chans = {}; Client.prototype._whoisData = {}; +Client.prototype.queueEmpty = true; Client.prototype.chanData = function( name, create ) { // {{{ var key = name.toLowerCase(); if ( create ) { @@ -587,26 +588,31 @@ Client.prototype.disconnect = function ( message, callback ) { // {{{ } message = message || "node-irc says goodbye"; var self = this; - if ( self.conn.readyState == 'open' ) { + if ( self.conn.writable ) { + self.conn.removeAllListeners('close'); self.send( "QUIT", message ); } - self.conn.requestedDisconnect = true; if (typeof(callback) === 'function') { self.conn.once('end', callback); } - self.conn.end(); + var intv = setInterval(function () { + if (self.queueEmpty) { + self.conn.requestedDisconnect = true; + self.conn.end(); + clearInterval(intv); + } + }, 100); }; // }}} Client.prototype.send = function(command) { // {{{ - var args = Array.prototype.slice.call(arguments); + var args = []; + for ( var k in arguments ) + args.push(arguments[k]); + args[args.length-1] = ":" + args[args.length-1]; // Remove the command args.shift(); - if ( args[args.length-1].match(/\s/) ) { - args[args.length-1] = ":" + args[args.length-1]; - } - - if ( this.opt.debug ) + if ( this.opt.debug && ! this.conn.requestedDisconnect ) util.log('SEND: ' + command + " " + args.join(" ")); if ( ! this.conn.requestedDisconnect ) { @@ -624,6 +630,7 @@ Client.prototype.activateFloodProtection = function(interval) { // {{{ // Wrapper for the original function. Just put everything to on central // queue. this.send = function() { + self.queueEmpty = false; cmdQueue.push(arguments); }; @@ -631,6 +638,8 @@ Client.prototype.activateFloodProtection = function(interval) { // {{{ var args = cmdQueue.shift(); if (args) { origSend.apply(self, args); + } else { + self.queueEmpty = true; } }; @@ -781,9 +790,9 @@ function parseMessage(line, stripColors) { // {{{ // Parse parameters if ( line.indexOf(':') != -1 ) { - match = line.match(/(.*)(?:^:|\s+:)(.*)/); - middle = match[1].trimRight(); - trailing = match[2]; + var index = line.indexOf(':'); + middle = line.substr(0, index).replace(/ +$/, ""); + trailing = line.substr(index+1); } else { middle = line;