Skip to content

Commit

Permalink
Merge pull request #219 from Throne3d/fix/statuses-casing
Browse files Browse the repository at this point in the history
Fix ircStatusNotices when channels are not lowercase
  • Loading branch information
ekmartin authored Apr 14, 2017
2 parents 8c3ad07 + de448ea commit 4d238d9
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
17 changes: 13 additions & 4 deletions lib/bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,19 +125,24 @@ class Bot {
this.sendToDiscord(author, to, `*${text}*`);
});

this.ircClient.on('join', (channel, nick) => {
this.ircClient.on('join', (channelName, nick) => {
logger.debug('Received join:', channelName, nick);
if (!this.ircStatusNotices) return;
if (nick === this.nickname && !this.announceSelfJoin) return;
const channel = channelName.toLowerCase();
// self-join is announced before names (which includes own nick)
// so don't add nick to channelUsers
if (nick !== this.nickname) this.channelUsers[channel].add(nick);
this.sendExactToDiscord(channel, `*${nick}* has joined the channel`);
});

this.ircClient.on('part', (channel, nick, reason) => {
this.ircClient.on('part', (channelName, nick, reason) => {
logger.debug('Received part:', channelName, nick, reason);
if (!this.ircStatusNotices) return;
const channel = channelName.toLowerCase();
// remove list of users when no longer in channel (as it will become out of date)
if (nick === this.nickname) {
logger.debug('Deleting channelUsers as bot parted:', channel);
delete this.channelUsers[channel];
return;
}
Expand All @@ -146,15 +151,19 @@ class Bot {
});

this.ircClient.on('quit', (nick, reason, channels) => {
logger.debug('Received quit:', nick, channels);
if (!this.ircStatusNotices || nick === this.nickname) return;
channels.forEach((channel) => {
channels.forEach((channelName) => {
const channel = channelName.toLowerCase();
if (!this.channelUsers[channel].delete(nick)) return;
this.sendExactToDiscord(channel, `*${nick}* has quit (${reason})`);
});
});

this.ircClient.on('names', (channel, nicks) => {
this.ircClient.on('names', (channelName, nicks) => {
logger.debug('Received names:', channelName, nicks);
if (!this.ircStatusNotices) return;
const channel = channelName.toLowerCase();
this.channelUsers[channel] = new Set(Object.keys(nicks));
});

Expand Down
26 changes: 26 additions & 0 deletions test/bot-events.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,16 @@ describe('Bot Events', function () {
bot.channelUsers.should.deep.equal({ '#channel': channelNicks });
});

it('should lowercase the channelUsers mapping', function () {
const bot = createBot({ ...config, ircStatusNotices: true });
bot.connect();
const channel = '#channelName';
const nicks = { [bot.nickname]: '' };
bot.ircClient.emit('names', channel, nicks);
const channelNicks = new Set([bot.nickname]);
bot.channelUsers.should.deep.equal({ '#channelname': channelNicks });
});

it('should send join messages to discord when config enabled', function () {
const bot = createBot({ ...config, ircStatusNotices: true });
bot.connect();
Expand Down Expand Up @@ -222,6 +232,22 @@ describe('Bot Events', function () {
bot.sendExactToDiscord.getCall(1).args.should.deep.equal([channel3, text]);
});

it('should not crash with join/part/quit messages and weird channel casing', function () {
const bot = createBot({ ...config, ircStatusNotices: true });
bot.connect();

function wrap() {
const nick = 'user';
const reason = 'Leaving';
bot.ircClient.emit('names', '#Channel', { [bot.nickname]: '' });
bot.ircClient.emit('join', '#cHannel', nick);
bot.ircClient.emit('part', '#chAnnel', nick, reason);
bot.ircClient.emit('join', '#chaNnel', nick);
bot.ircClient.emit('quit', nick, reason, ['#chanNel']);
}
(wrap).should.not.throw();
});

it('should be possible to disable join/part/quit messages', function () {
const bot = createBot({ ...config, ircStatusNotices: false });
bot.connect();
Expand Down

0 comments on commit 4d238d9

Please sign in to comment.