Skip to content

Commit ce184fd

Browse files
authored
Merge pull request #24 from Throne3d/add/event-unhandled
Add event for unhandled messages
2 parents ef149a2 + 82fd0a5 commit ce184fd

File tree

3 files changed

+54
-3
lines changed

3 files changed

+54
-3
lines changed

docs/API.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,14 @@ Events
463463

464464
Emitted when ever the server responds with an error-type message.
465465
See the `raw` event for details on the `message` object.
466+
Unhandled messages, although they are shown as errors in the log, are not emitted using this event: see `unhandled`.
467+
468+
.. js:data:: 'unhandled'
469+
470+
`function (message) { }`
471+
472+
Emitted when ever the server responds with a message the bot doesn't recognize and doesn't handle.
473+
See the `raw` event for details on the `message` object.
466474

467475
Colors
468476
------

lib/irc.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -658,10 +658,11 @@ function Client(server, nick, opt) {
658658
default:
659659
if (message.commandType == 'error') {
660660
self.emit('error', message);
661-
self.out.error(util.inspect(message));
661+
self.out.error(message);
662662
}
663663
else {
664-
self.out.debug('\u001b[01;31mUnhandled message: ' + util.inspect(message) + '\u001b[0m');
664+
self.out.error('Unhandled message:', message);
665+
self.emit('unhandled', message);
665666
break;
666667
}
667668
}
@@ -860,6 +861,7 @@ Client.prototype.connect = function(retryCount, callback) {
860861

861862
lines.forEach(function iterator(line) {
862863
if (line.length) {
864+
self.out.debug('Received:', line);
863865
var message = parseMessage(line, self.opt.stripColors);
864866

865867
try {

test/test-irc.js

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,6 @@ test ('does not crash when disconnected and trying to send messages', function(t
148148
withClient(function(obj) {
149149
var client = obj.client;
150150
var mock = obj.mock;
151-
obj.closeWithEnd(t);
152151

153152
mock.server.on('connection', function() {
154153
mock.send(greeting);
@@ -163,6 +162,48 @@ test ('does not crash when disconnected and trying to send messages', function(t
163162
client.say('#channel', 'message2');
164163
client.end();
165164
client.say('#channel', 'message3');
165+
t.end();
166+
});
167+
});
168+
});
169+
170+
test ('unhandled messages are emitted appropriately', function(t) {
171+
withClient(function(obj) {
172+
var client = obj.client;
173+
var mock = obj.mock;
174+
obj.closeWithEnd(t);
175+
var endTimeout;
176+
177+
mock.server.on('connection', function() {
178+
mock.send(greeting);
179+
});
180+
181+
client.on('registered', function() {
182+
mock.send(':127.0.0.1 150 :test\r\n');
183+
//mock.close();
184+
client.on('error', function(){console.log("error");});
185+
client.on('unhandled', function(msg) {
186+
var expected = {
187+
prefix: '127.0.0.1',
188+
server: '127.0.0.1',
189+
rawCommand: '150',
190+
command: '150',
191+
commandType: 'normal',
192+
args: ['test']
193+
};
194+
t.deepEqual(msg, expected, 'unhandled message should be emitted as expected');
195+
client.disconnect();
196+
});
197+
endTimeout = setTimeout(function() {
198+
if (t.ended) return;
199+
t.ok(false, 'callback for event must be called');
200+
client.disconnect();
201+
}, 1000);
202+
});
203+
204+
client.conn.once('close', function() {
205+
clearTimeout(endTimeout)
206+
client.end();
166207
});
167208
});
168209
});

0 commit comments

Comments
 (0)