Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: einaros/node-irc
base: 7107a36c9b
...
head fork: einaros/node-irc
compare: afb53bdc2b
Checking mergeability… Don't worry, you can still create the pull request.
  • 3 commits
  • 3 files changed
  • 0 commit comments
  • 1 contributor
Showing with 131 additions and 53 deletions.
  1. +87 −25 IRC.js
  2. +25 −28 demo.js
  3. +19 −0 test/IRC.js
View
112 IRC.js
@@ -188,8 +188,52 @@ public(IRC.prototype, {
queue.inProgress += 1;
if (queue.inProgress > 1) {
queue.pending.push(handler);
- } else handler();
+ }
+ else handler();
},
+
+ whois: function(nick, callback) {
+ var handler = function() {
+ this._socket.write('WHOIS ' + nick + '\r\n');
+ this._queueEventPreInterceptor({
+ 'whois': function(who, whois) {
+ this._callQueue.whois.inProgress -= 1;;
+ var handled = false;
+ if (nick == who) {
+ if (typeof callback == 'function') callback(undefined, whois);
+ handled = true;
+ }
+ if (this._callQueue.whois.pending.length > 0) {
+ this._callQueue.whois.pending.shift()();
+ }
+ return handled;
+ }.bind(this),
+ 'errorcode': function(code, to, regarding, reason) {
+ if (['ERR_NOSUCHSERVER', 'ERR_NONICKNAMEGIVEN',
+ 'RPL_WHOISUSER', 'RPL_WHOISCHANNELS',
+ 'RPL_WHOISCHANNELS', 'RPL_WHOISSERVER',
+ 'RPL_AWAY', 'RPL_WHOISOPERATOR',
+ 'RPL_WHOISIDLE', 'ERR_NOSUCHNICK',
+ 'RPL_ENDOFWHOIS'].has(code)) {
+ if (typeof cb == 'function') cb(reason);
+ return true;
+ }
+ else if (code == 'ERR_NEEDMOREPARAMS' &&
+ regarding == 'JOIN') {
+ if (typeof cb == 'function') cb(reason);
+ return true;
+ }
+ }
+ });
+ }.bind(this);
+ var queue = this._callQueue.whois = this._callQueue.whois || { inProgress: 0, pending: [] };
+ queue.inProgress += 1;
+ if (queue.inProgress > 1) {
+ queue.pending.push(handler);
+ }
+ else handler();
+ },
+
nick: function(newnick, callback) {
this._socket.write('NICK ' + newnick + '\r\n');
this._queueEventPreInterceptor({
@@ -346,7 +390,7 @@ private(IRC.prototype, {
/* RPL_ENDOFMOTD */ '376': function(raw, from, text) {
return this._messageHandlers['372'].apply(this, arguments);
},
- /* RPL_NAMRPLY */ '353': function(raw, from, to, where, names) {
+ /* RPL_NAMRPLY */ '353': function(raw, from, to, type, where, names) {
this._cache['names'] = this._cache['names'] || {};
this._cache['names'][where] = (this._cache['names'][where] || []).concat(names.split(' '));
},
@@ -356,6 +400,25 @@ private(IRC.prototype, {
this.emit('names', where, names);
if (this._cache['names'] && this._cache['names'][where]) delete this._cache['names'][where];
},
+ /* RPL_WHOISUSER */ '311': function(raw, from, to, nick, ident, host, noop, realname) {
+ this._cache['whois'] = this._cache['whois'] || {};
+ var whois = this._cache['whois'][nick] = (this._cache['whois'][nick] || {});
+ whois.nick = nick;
+ whois.ident = ident;
+ whois.host = host;
+ whois.realname = realname;
+ },
+ /* RPL_WHOISCHANNELS */ '319': function(raw, from, to, nick, channels) {
+ this._cache['whois'] = this._cache['whois'] || {};
+ var whois = this._cache['whois'][nick] = (this._cache['whois'][nick] || {});
+ whois.channels = (whois.channels || []).concat(channels.replace(/[\+@]([#&])/g, '$1').split(' '));
+ },
+ /* RPL_ENDOFWHOIS */ '318': function(raw, from, to, nick) {
+ this._cache['whois'] = this._cache['whois'] || {};
+ var whois = this._cache['whois'][nick] = (this._cache['whois'][nick] || {});
+ this.emit('whois', nick, whois);
+ if (this._cache['whois'] && this._cache['whois'][nick]) delete this._cache['whois'][nick];
+ },
/* RPL_NOTOPIC */ '331': function(raw, from, to, where, topic) {
this.emit('topic', where, null, null, raw);
},
@@ -402,6 +465,7 @@ private(IRC.prototype, {
var identity = parseIdentity(from);
var data = data.match(/:?(.*)/);
if (!data) throw 'invalid NICK structure';
+ if (from == this._username) this._username = newnick;
this.emit('nick', identity.nick, data[1], raw);
},
'PART': function(raw, who, where) {
@@ -425,6 +489,8 @@ private(IRC.prototype, {
_processServerMessage: function(line) {
this._debug(4, 'Incoming: ' + line);
this.emit('raw', line);
+
+ // ctcp handling should be rewritten
var matches = line.match(/^:([^\s]*)\s([^\s]*)\s([^\s]*)\s:\u0001([^\s]*)\s(.*)\u0001/);
if (matches) {
var handler = this._messageHandlers['CTCP_' + matches[2] + '_' + matches[4]];
@@ -436,30 +502,26 @@ private(IRC.prototype, {
}
return;
}
- matches = line.match(/(?::([^\s]*)\s)?([^:]{1}[^\s]*)(?:\s([^:=*@]{1}[^\s]*))?(?:\s([^:=*@]{1}[^\s]*))?(?:\s(?:[@=*]\s)?([^:]{1}[^\s]*))?(?:\s:?(.*))?/);
- if (matches) {
- var handler = this._messageHandlers[matches[2]];
- var args = [line];
- if (typeof handler == 'function') {
- for (var i = 1; i < matches.length; ++i) {
- if (i != 2 && typeof matches[i] != 'undefined') args.push(matches[i]);
- }
- handler.apply(this, args);
- }
- else if (typeof handler == 'string') {
- for (var i = 1; i < matches.length; ++i) {
- if (i != 2 && typeof matches[i] != 'undefined') args.push(matches[i]);
- }
- args.unshift(handler);
- this._errorHandler.apply(this, args);
- }
- else {
- this._debug(2, 'Unhandled msg: ' + line);
- }
- return;
+
+ // anything other than ctcp
+ var parts = line.trim().split(/ :/)
+ , args = parts[0].split(' ');
+ if (parts.length > 0) args.push(parts[1]);
+ if (line.match(/^:/)) {
+ args[1] = args.splice(0, 1, args[1]);
+ args[1] = (args[1] + '').replace(/^:/, '');
+ }
+ var command = args[0].toUpperCase();
+ args = args.slice(1);
+ args.unshift(line);
+
+ var handler = this._messageHandlers[command];
+ if (typeof handler == 'function') handler.apply(this, args);
+ else if (typeof handler == 'string') {
+ args.unshift(handler);
+ this._errorHandler.apply(this, args);
}
- // Unknown
- this._debug(2, 'Unhandled server data: ' + line);
+ else this._debug(2, 'Unhandled msg: ' + line);
}
});
exports.IRC = IRC;
View
53 demo.js
@@ -1,39 +1,36 @@
var IRC = require('./IRC').IRC;
var irc = new IRC('irc.freenode.net', 6667);
-irc.setDebugLevel(4);
+irc.on('raw', function(data) { console.log(data); });
+irc.setDebugLevel(1);
irc.on('connected', function(server) {
console.log('connected to ' + server);
irc.join('#foobartest', function(error) {
- if (error) {
- console.log('error joining channel: ' + error);
- return;
- }
- // irc.privmsg('#foobartest', 'well hello yall');
- // irc.nick('muppetty2', function(old, newn) {
- // irc.privmsg('#foobartest', 'I\'m new!');
- // });
+ irc.privmsg('#foobartest', 'well hello yall');
+ irc.nick('muppetty2', function(old, newn) {
+ irc.privmsg('#foobartest', 'I\'m new!');
+ });
});
});
irc.on('topic', function(where, topic) {
console.log('topic of ' + where + ': ' + topic);
});
-irc.on('join', function(who, where) {
- console.log(who + ' joined ' + where);
- if (where == '#foobartest' && who != irc.whoami()) {
- irc.kick(where, who, 'woot', function(error) {
- if (error) {
- console.log('error kicking user: ' + error);
- return;
- }
- irc.mode(where, '+b', who + '!*@*', function(error) {
- if (error) {
- console.log('error banning user: ' + error);
- return;
- }
- });
- });
- }
-});
+// irc.on('join', function(who, where) {
+// console.log(who + ' joined ' + where);
+// if (where == '#foobartest' && who != irc.whoami()) {
+// irc.kick(where, who, 'woot', function(error) {
+// if (error) {
+// console.log('error kicking user: ' + error);
+// return;
+// }
+// irc.mode(where, '+b', who + '!*@*', function(error) {
+// if (error) {
+// console.log('error banning user: ' + error);
+// return;
+// }
+// });
+// });
+// }
+// });
irc.on('quit', function(who, message) {
console.log(who + ' quit: ' + message);
});
@@ -66,10 +63,10 @@ irc.on('ping-reply', function(from, ms) {
});
irc.on('errorcode', function(code) {
if (code == 'ERR_NICKNAMEINUSE') {
- irc.nick('einaros');
+ irc.nick('foomeh2');
}
});
-irc.connect('einaros', 'my name yeah', 'ident');
+irc.connect('foomeh', 'my name yeah', 'ident');
process.on('exit', function () {
irc.quit('bye');
});
View
19 test/IRC.js
@@ -607,6 +607,25 @@ describe('IRC', function() {
data(':irc.homelien.no 366 muppetty #foobartest :End of /NAMES list.\r\n');
assert.ok(eventEmitted);
});
+ it('whois calls callback when whois is done', function() {
+ var obj = fake(['on', 'setEncoding', 'connect', 'write']);
+ var irc = new IRC(obj);
+ irc._username = 'foomeh';
+ var eventEmitted = false;
+ irc.whois('foomeh', function(error, whois) {
+ eventEmitted = true;
+ assert.ok(!error);
+ assert.equal(['#fooofooofoo3', '#fooofooofoo2', '#fooofooofoo1'].sort().join(', '), whois.channels.sort().join(', '));
+ });
+ var data = obj.on.history.filter(function(args) { return args[0] == 'data'; }).map(function(args) { return args[1]; })[0];
+ data(':asimov.freenode.net 311 foomeh foomeh ~ident getinternet.no * :my name yeah\r\n');
+ data(':asimov.freenode.net 319 foomeh foomeh :@#fooofooofoo3 @#fooofooofoo2 @#fooofooofoo1\r\n');
+ data(':asimov.freenode.net 312 foomeh foomeh asimov.freenode.net :TX, USA\r\n');
+ data(':asimov.freenode.net 378 foomeh foomeh :is connecting from *@getinternet.no 84.55.51.127\r\n');
+ data(':asimov.freenode.net 317 foomeh foomeh 8 1333646400 :seconds idle, signon time\r\n');
+ data(':asimov.freenode.net 318 foomeh foomeh :End of /WHOIS list.\r\n');
+ assert.ok(eventEmitted);
+ });
it('mode +i on channel sends mode command to server', function() {
var obj = fake(['on', 'setEncoding', 'connect', 'write']);
var irc = new IRC(obj);

No commit comments for this range

Something went wrong with that request. Please try again.