Skip to content

Commit

Permalink
Merge 2c44cb2 into 0607ff8
Browse files Browse the repository at this point in the history
  • Loading branch information
markus456 authored Jun 1, 2017
2 parents 0607ff8 + 2c44cb2 commit 2ab49c8
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ Valid JSON cannot contain comments, so remember to remove them first!
// Prevent messages posted by Slackbot (e.g. Slackbot responses)
// from being posted into the IRC channel:
"muteSlackbot": true, // Off by default
"muteBridgedUsers": false, // On by default
// Sends messages to Slack whenever a user joins/leaves an IRC channel:
"ircStatusNotices": {
"join": false, // Don't send messages about joins
Expand Down
26 changes: 21 additions & 5 deletions lib/bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class Bot {
this.commandCharacters = options.commandCharacters || [];
this.channels = _.values(options.channelMapping);
this.muteSlackbot = options.muteSlackbot || false;
this.muteBridgedUsers = options.muteBridgedUsers;
this.muteUsers = {
slack: [],
irc: [],
Expand Down Expand Up @@ -100,10 +101,13 @@ class Bot {
});

this.slack.rtm.on('message', (message) => {
// Ignore bot messages and people leaving/joining
if (message.type === 'message' &&
(!message.subtype || ALLOWED_SUBTYPES.indexOf(message.subtype) > -1)) {
this.sendToIRC(message);
// Ignore bot messages and people leaving/joining
if (message.type === 'message') {
if (!message.subtype || ALLOWED_SUBTYPES.indexOf(message.subtype) > -1) {
this.sendToIRC(message);
} else if (message.subtype === 'bot_message' && message.username) {
this.sendToIRC(message);
}
}
});

Expand Down Expand Up @@ -202,11 +206,21 @@ class Bot {

const user = dataStore.getUserById(message.user);

if (this.muteUsers.slack.indexOf(user.name) !== -1) {
if (user && this.muteUsers.slack.indexOf(user.name) !== -1) {
logger.debug(`Muted message from Slack ${user.name}: ${message.text}`);
return;
}

if (message.subtype === 'bot_message') {
if (message.username && message.username.search(/[(]IRC[)]/) !== -1) {
logger.debug(`Muted self-generated message ${message.username}: ${message.text}`);
return;
} else if (this.muteBridgedUsers === true) {
logger.debug(`Muted message from a bridged user ${message.username}: ${message.text} ${this.muteBridgedUsers} ${message.subtype}`);
return;
}
}

const channelName = channel.is_channel ? `#${channel.name}` : channel.name;
const ircChannel = this.channelMapping[channelName];

Expand All @@ -219,6 +233,8 @@ class Bot {
this.ircClient.say(ircChannel, prelude);
} else if (!message.subtype) {
text = `<${user.name}> ${text}`;
} else if (message.subtype === 'bot_message') {
text = `<${message.username}> ${text}`;
} else if (message.subtype === 'file_share') {
text = `<${user.name}> File uploaded ${message.file.permalink} / ${message.file.permalink_public}`;
if (message.file.initial_comment) {
Expand Down
23 changes: 23 additions & 0 deletions test/bot-events.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,29 @@ describe('Bot Events', function () {
this.bot.sendToIRC.should.have.not.have.been.called;
});

it('should not send bot messages without usernames to irc', function () {
const message = {
type: 'message',
subtype: 'bot_message',
text: 'a message from a bot',
bot_id: 'ROBOT'
};
this.bot.slack.rtm.emit('message', message);
this.bot.sendToIRC.should.have.not.have.been.called;
});

it('should send bot messages with usernames to irc', function () {
const message = {
type: 'message',
subtype: 'bot_message',
text: 'a message from a user',
username: 'realuser',
bot_id: 'ROBOT'
};
this.bot.slack.rtm.emit('message', message);
this.bot.sendToIRC.should.have.have.been.called;
});

it('should send messages to slack', function () {
const channel = '#channel';
const author = 'user';
Expand Down
26 changes: 26 additions & 0 deletions test/bot.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,32 @@ describe('Bot', function () {
ClientStub.prototype.say.should.not.have.been.called;
});

it('should not send messages from bots with usernames if bridged user muting is on', function () {
this.bot.muteBridgedUsers = true;
const message = {
type: 'message',
subtype: 'bot_message',
bot_id: 'SOMEBOT',
username: 'someuser',
text: 'hello world!'
};
this.bot.sendToIRC(message);
ClientStub.prototype.say.should.not.have.been.called;
});

it('should send bot messages with usernames to irc', function () {
this.bot.muteBridgedUsers = false;
const message = {
type: 'message',
subtype: 'bot_message',
bot_id: 'SOMEBOT',
username: 'someuser',
text: 'hello world!'
};
this.bot.sendToIRC(message);
ClientStub.prototype.say.should.have.been.called;
});

it('should parse text from slack when sending messages', function () {
const text = '<@USOMEID> <@USOMEID|readable>';
const message = {
Expand Down

0 comments on commit 2ab49c8

Please sign in to comment.