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

Add support for relaying bridged users #201

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
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
33 changes: 27 additions & 6 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 All @@ -228,7 +244,12 @@ class Bot {
text = `Action: ${user.name} ${text}`;
}
logger.debug('Sending message to IRC', channelName, text);
this.ircClient.say(ircChannel, text);

while (text.length > 0) {
const msg = text.substring(0, 510);
text = text.substr(510);
this.ircClient.say(ircChannel, msg);
}
}
}

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