Added away(msg, optional callback) + ignored rpl_luserunknown + emit 'unhandled' #80

Closed
wants to merge 4 commits into
from
Jump to file or symbol
Failed to load files and symbols.
+59 −3
Diff settings

Always

Just for now

@@ -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
View
@@ -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);
View
@@ -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);
View
@@ -96,13 +96,20 @@ function Client(server, nick, opt) {
break;
case "rpl_luserclient":
case "rpl_luserop":
+ case "rpl_luserunknown":
case "rpl_luserchannels":
case "rpl_luserme":
case "rpl_localusers":
case "rpl_globalusers":
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};