Permalink
Browse files

Implemented whois method and event

  • Loading branch information...
1 parent 76ee333 commit 3e0948a329fa959c132440202fde8b81fa8f4791 Martyn Smith committed Sep 3, 2011
Showing with 81 additions and 8 deletions.
  1. +27 −0 API.md
  2. +7 −8 README.md
  3. +47 −0 lib/irc.js
View
@@ -154,6 +154,25 @@ Emitted when a user changes nick along with the channels the user is in.
Emitted when the client recieves an `/invite`.
+### Event: 'whois'
+
+`function (info) { }`
+
+Emitted whenever the server finishes outputting a WHOIS response. The
+information should look something like:
+
+ {
+ nick: "Ned",
+ user: "martyn",
+ host: "10.0.0.18",
+ realname: "Unknown",
+ channels: ["@#purpledishwashers", "#blah", "#mmmmbacon"],
+ server: "*.dollyfish.net.nz",
+ serverinfo: "The Dollyfish Underworld",
+ operator: "is an IRC Operator"
+ }
+
+
### Event: 'raw'
`function (message) { }`
@@ -213,6 +232,14 @@ Sends a message to the specified target.
Sends a notice to the specified target.
+### Client.whois(nick, callback)
+
+Request a whois for the specified `nick`.
+
+`callback` is fired when the server has finished generating the whois
+information and is passed exactly the same information as a `whois` event
+described above.
+
`target` is either a nickname, or a channel.
### Client.disconnect(message)
View
@@ -62,17 +62,16 @@ talking:
client.say('#yourchannel', "I'm a bot!");
client.say('nonbeliever', "SRSLY, I AM!");
-and sending notices:
+and many others. Check out the API documentation for a complete reference.
- client.notice('#yourchannel', "I'm a bot!");
- client.notice('nonbeliever', "SRSLY, I AM!");
-
-For any other commands you might want to send to the server you can use the
-send() message which sends raw messages to the server:
+For any commands that there aren't methods for you can use the send() method
+which sends raw messages to the server:
client.send('MODE', '#yourchannel', '+o', 'yournick');
-All commands and events are documented in `API.md` (hopefully). I hope to make
-a more complete set of events/commands over the next few weeks.
+All commands and events are documented in `API.md` (hopefully). If you find any
+methods/events missing that you'd really like to have included feel free to
+send me a pull request (preferred) or file an issue and I'll try get around to
+writing it.
[npm]: http://github.com/isaacs/npm
View
@@ -653,6 +653,30 @@ function Client(server, nick, opt) {
channel.topic = message.args[2];
}
break;
+ case "rpl_away":
+ self._addWhoisData(message.args[1], 'away', message.args[2], true);
+ break;
+ case "rpl_whoisuser":
+ self._addWhoisData(message.args[1], 'user', message.args[2]);
+ self._addWhoisData(message.args[1], 'host', message.args[3]);
+ self._addWhoisData(message.args[1], 'realname', message.args[5]);
+ break;
+ case "rpl_whoisidle":
+ self._addWhoisData(message.args[1], 'idle', message.args[2]);
+ break;
+ case "rpl_whoischannels":
+ self._addWhoisData(message.args[1], 'channels', message.args[2].trim().split(/\s+/)); // TODO - clean this up?
+ break;
+ case "rpl_whoisserver":
+ self._addWhoisData(message.args[1], 'server', message.args[2]);
+ self._addWhoisData(message.args[1], 'serverinfo', message.args[3]);
+ break;
+ case "rpl_whoisoperator":
+ self._addWhoisData(message.args[1], 'operator', message.args[2]);
+ break;
+ case "rpl_endofwhois":
+ self.emit('whois', self._clearWhoisData(message.args[1]));
+ break;
case "333":
// TODO emit?
var channel = self.chans[message.args[1]];
@@ -800,6 +824,7 @@ util.inherits(Client, process.EventEmitter);
Client.prototype.conn = null;
Client.prototype.chans = {};
+Client.prototype._whoisData = {};
Client.prototype.connect = function ( retryCount ) { // {{{
retryCount = retryCount || 0;
var self = this;
@@ -980,6 +1005,28 @@ Client.prototype.say = function(target, text) { // {{{
Client.prototype.notice = function(target, text) { // {{{
this.send('NOTICE', target, text);
} // }}}
+Client.prototype.whois = function(nick, callback) { // {{{
+ if ( typeof callback === 'function' ) {
+ var callbackWrapper = function(info) {
+ if ( info.nick == nick ) {
+ this.removeListener('whois', callbackWrapper);
+ return callback.apply(this, arguments);
+ }
+ };
+ this.addListener('whois', callbackWrapper);
+ }
+ this.send('WHOIS', nick);
+} // }}}
+Client.prototype._addWhoisData = function(nick, key, value, onlyIfExists) { // {{{
+ if ( onlyIfExists && !this._whoisData[nick] ) return;
+ this._whoisData[nick] = this._whoisData[nick] || {nick: nick};
+ this._whoisData[nick][key] = value;
+} // }}}
+Client.prototype._clearWhoisData = function(nick) { // {{{
+ var data = this._whoisData[nick];
+ delete this._whoisData[nick];
+ return data;
+} // }}}
/*
* parseMessage(line)

0 comments on commit 3e0948a

Please sign in to comment.