Skip to content
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

Fix ircStatusNotices when channels are not lowercase #219

Merged
merged 1 commit into from
Apr 14, 2017
Merged
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
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