Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions docs/API.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions example/bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
10 changes: 10 additions & 0 deletions example/secure.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
33 changes: 30 additions & 3 deletions lib/irc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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(" "));
Expand Down Expand Up @@ -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};
Expand Down