diff --git a/docs/API.rst b/docs/API.rst index f85bd8a3..be8d8ad5 100644 --- a/docs/API.rst +++ b/docs/API.rst @@ -102,6 +102,15 @@ Client generating the whois information and is passed exactly the same information as a `whois` event described above. +.. js:function:: Client.away(awayMessage, callback) + + Set away status to `awayMessage` or reset it. + + :param string awayMessage: message to be set + :param function callback: Optional callback to fire when the server responds + to the away call with a message such as 'You have been marked as beeing + away.' or 'You are no longer marked as beeing away'. + .. js:function:: Client.list([arg1, arg2, ...]) Request a channel listing from the server. The arguments for this method are diff --git a/example/bot.js b/example/bot.js index 1b9eee9e..4df1a098 100755 --- a/example/bot.js +++ b/example/bot.js @@ -26,6 +26,16 @@ bot.addListener('message', function (from, to, message) { if ( message.match(/hello/i) ) { bot.say(to, 'Hello there ' + from); } + // set away on request + if (message.match(/away/i)) { + bot.away('I am not here, leave a msg!', function(serverResponse) { + console.log(serverResponse); + }); + } + // back + if (message.match(/back/i)) { + bot.away(); + } if ( message.match(/dance/) ) { setTimeout(function () { bot.say(to, "\u0001ACTION dances: :D\\-<\u0001") }, 1000); setTimeout(function () { bot.say(to, "\u0001ACTION dances: :D|-<\u0001") }, 2000); diff --git a/example/secure.js b/example/secure.js index 2be340a8..f1e84a71 100755 --- a/example/secure.js +++ b/example/secure.js @@ -40,6 +40,16 @@ bot.addListener('message', function (from, to, message) { if ( message.match(/hello/i) ) { bot.say(to, 'Hello there ' + from); } + // set away on request + if (message.match(/away/i)) { + bot.away('I am not here, leave a msg!', function(serverResponse) { + console.log(serverResponse); + }); + } + // back + if (message.match(/back/i)) { + bot.away(); + } if ( message.match(/dance/) ) { setTimeout(function () { bot.say(to, "\u0001ACTION dances: :D\\-<\u0001") }, 1000); setTimeout(function () { bot.say(to, "\u0001ACTION dances: :D|-<\u0001") }, 2000); diff --git a/lib/irc.js b/lib/irc.js index c7a4e5c3..ce24fdd1 100644 --- a/lib/irc.js +++ b/lib/irc.js @@ -96,6 +96,7 @@ function Client(server, nick, opt) { break; case "rpl_luserclient": case "rpl_luserop": + case "rpl_luserunknown": case "rpl_luserchannels": case "rpl_luserme": case "rpl_localusers": @@ -103,6 +104,12 @@ function Client(server, nick, opt) { case "rpl_statsconn": // Random welcome crap, ignoring break; + case "rpl_nowaway": + self.emit('nowaway', message.args[1]); + break; + case "rpl_unaway": + self.emit('unaway', message.args[1]); + break; case "err_nicknameinuse": if ( typeof(self.opt.nickMod) == 'undefined' ) self.opt.nickMod = 0; @@ -424,6 +431,10 @@ function Client(server, nick, opt) { util.log("\033[01;31mERROR: " + util.inspect(message) + "\033[0m"); } else { + // ignore quit error message (happens with some servers, ex: freenode) + if (self._lastCommand === 'QUIT' && command === 'ERROR' + && message.args[0] && message.args[0].match(/Closing link/i)) { break; } + self.emit('unhandled', message); if ( self.opt.debug ) util.log("\033[01;31mUnhandled message: " + util.inspect(message) + "\033[0m"); } @@ -505,7 +516,7 @@ Client.prototype.connect = function ( retryCount, callback ) { // {{{ util.log(self.conn.authorizationError); } }); - }else { + } else { self.conn = net.createConnection(self.opt.port, self.opt.server); } self.conn.requestedDisconnect = false; @@ -588,8 +599,8 @@ Client.prototype.send = function(command) { // {{{ args.push(arguments[k]); args[args.length-1] = ":" + args[args.length-1]; - // Remove the command - args.shift(); + // Remove the command from args and remember it + this._lastCommand = args.shift(); if ( this.opt.debug ) util.log('SEND: ' + command + " " + args.join(" ")); @@ -694,6 +705,22 @@ Client.prototype.list = function() { // {{{ args.unshift('LIST'); this.send.apply(this, args); } // }}} +Client.prototype.away = function(msg, callback) { // {{{ + if ( typeof callback === 'function' ) { + var callbackWrapper = function(msg) { + this.removeListener('nowaway', callbackWrapper); + this.removeListener('unaway', callbackWrapper); + return callback.apply(this, arguments); + }; + this.addListener('nowaway', callbackWrapper); + this.addListener('unaway', callbackWrapper); + } + if (msg) { + this.send('AWAY', msg); + } else { + this.send('AWAY'); + } +} // {{{ Client.prototype._addWhoisData = function(nick, key, value, onlyIfExists) { // {{{ if ( onlyIfExists && !this._whoisData[nick] ) return; this._whoisData[nick] = this._whoisData[nick] || {nick: nick};