diff --git a/API.md b/API.md index 3373a9dd..944ce304 100644 --- a/API.md +++ b/API.md @@ -203,6 +203,10 @@ You can read more about the IRC protocol by reading [RFC Emitted when ever the server responds with an error-type message. The message parameter is exactly as in the 'raw' event. +### Client.connect(retryCount, callback) + +Connects to the server. Used when `autoConnect` in the options is set to false. If `retryCount` is a function it will be treated as the `callback`. + ### Client.send(command, arg1, arg2, ...) Sends a raw message to the server, generally speaking it's best not to use this @@ -242,6 +246,6 @@ described above. `target` is either a nickname, or a channel. -### Client.disconnect(message) +### Client.disconnect(message, callback) -Disconnects from the IRC server sending the specified parting message. +Disconnects from the IRC server sending the specified parting message. If `message` is a function it will be treated as the `callback`. diff --git a/lib/irc.js b/lib/irc.js index 96ae9bbd..a90b0808 100644 --- a/lib/irc.js +++ b/lib/irc.js @@ -829,8 +829,14 @@ util.inherits(Client, process.EventEmitter); Client.prototype.conn = null; Client.prototype.chans = {}; Client.prototype._whoisData = {}; -Client.prototype.connect = function ( retryCount ) { // {{{ - retryCount = retryCount || 0; +Client.prototype.connect = function ( retryCount, callback ) { // {{{ + if (!retryCount || typeof(retryCount) === 'function') { + callback = retryCount; + retryCount = 0; + } + if (typeof(callback) === 'function') { + this.once('registered', callback); + } var self = this; self.chans = {}; // try to connect to the server @@ -918,13 +924,19 @@ Client.prototype.connect = function ( retryCount ) { // {{{ }, self.opt.retryDelay ); }); }; // }}} -Client.prototype.disconnect = function ( message ) { // {{{ - message = message || "node-irc says goodbye"; +Client.prototype.disconnect = function ( message, callback ) { // {{{ + if (!message || typeof(message) === 'function') { + callback = message; + message = "node-irc says goodbye"; + } var self = this; if ( self.conn.readyState == 'open' ) { self.send( "QUIT", message ); } self.conn.requestedDisconnect = true; + if (typeof(callback) === 'function') { + self.conn.once('end', callback); + } self.conn.end(); }; // }}} Client.prototype.send = function(command) { // {{{ @@ -971,8 +983,7 @@ Client.prototype.activateFloodProtection = function() { // {{{ }; // }}} Client.prototype.join = function(channel, callback) { // {{{ - var callbackWrapper = function () { - this.removeListener('join' + channel, callbackWrapper); + this.once('join' + channel, function () { // if join is successful, add this channel to opts.channels // so that it will be re-joined upon reconnect (as channels // specified in options are) @@ -983,17 +994,12 @@ Client.prototype.join = function(channel, callback) { // {{{ if ( typeof(callback) == 'function' ) { return callback.apply(this, arguments); } - }; - this.addListener('join' + channel, callbackWrapper); + }); this.send('JOIN', channel); } // }}} Client.prototype.part = function(channel, callback) { // {{{ if ( typeof(callback) == 'function' ) { - var callbackWrapper = function () { - this.removeListener('part' + channel, callbackWrapper); - return callback.apply(this, arguments); - }; - this.addListener('part' + channel, callbackWrapper); + this.once('part' + channel, callback); } // remove this channel from this.opt.channels so we won't rejoin