Skip to content

Commit

Permalink
Handle channel_rename, reaction_removed, subteam_created, subteam_upd…
Browse files Browse the repository at this point in the history
…ated (#10 #28)
  • Loading branch information
adsr committed May 31, 2018
1 parent fa6b3c8 commit 67fd490
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 25 deletions.
1 change: 1 addition & 0 deletions bin/filter-tape.awk
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
/^$/ { next }
/^# tests / { print; next }
/^# pass / { print; next }
/^# fail / { print; next }
/^# ok/ { print; next }
/^# tape output off/ { off=1; next }
/^# tape output on/ { off=0; next }
Expand Down
57 changes: 34 additions & 23 deletions lib/irslackd.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,15 @@ class Irslackd {
[ 'message', self.makeSlackHandler(self.onSlackMessage) ],
[ 'channel_joined', self.makeSlackHandler(self.onSlackChannelJoined) ],
[ 'channel_left', self.makeSlackHandler(self.onSlackChannelLeft) ],
[ 'channel_rename', self.makeSlackHandler(self.onSlackChannelRename) ],
[ 'member_joined_channel', self.makeSlackHandler(self.onSlackMemberJoinedChannel) ],
[ 'member_left_channel', self.makeSlackHandler(self.onSlackMemberLeftChannel) ],
[ 'mpim_open', self.makeSlackHandler(self.onSlackMpimOpen) ],
[ 'mpim_close', self.makeSlackHandler(self.onSlackMpimClose) ],
[ 'reaction_added', self.makeSlackHandler(self.onSlackReactionAdded) ],
[ 'user_typing', self.makeSlackHandler(self.onSlackUserTyping) ],
[ 'reaction_removed', self.makeSlackHandler(self.onSlackReactionRemoved) ],
[ 'subteam_created', self.makeSlackHandler(self.onSlackSubteamUpdated) ],
[ 'subteam_updated', self.makeSlackHandler(self.onSlackSubteamUpdated) ],
[ 'slack_event', self.makeSlackHandler(self.onSlackEvent) ],
]).forEach((handler, event, map) => {
ircUser.slackRtm.on(event, handler);
Expand Down Expand Up @@ -403,6 +406,18 @@ class Irslackd {
await this.onIrcPart(ircUser, { args: [ ircChan ] }, event.channel);
}
}
async onSlackChannelRename(ircUser, event) {
let oldIrcChan = await this.resolveSlackChannel(ircUser, event.channel.id);
let newIrcChan = '#' + event.channel.name;
let isInChan = ircUser.inChannel.get(oldIrcChan);
if (isInChan) {
this.ircd.write(ircUser.socket, ircUser.ircNick, 'PART', [ oldIrcChan ]);
ircUser.inChannel.delete(oldIrcChan);
}
ircUser.ircToSlack.delete(oldIrcChan);
ircUser.slackToIrc.delete(event.channel.id);
if (isInChan) await this.onIrcJoinOne(ircUser, newIrcChan, event.channel.id);
}
async onSlackMemberJoinedChannel(ircUser, event) {
if (event.user === ircUser.slackUserId) return;
let [ircNick, ircChan] = await this.resolveSlackTarget(ircUser, event);
Expand All @@ -418,13 +433,22 @@ class Irslackd {
}
}
async onSlackMpimOpen(ircUser, event) {
this.onSlackChannelJoined(ircUser, event);
await this.onSlackChannelJoined(ircUser, event);
}
async onSlackMpimClose(ircUser, event) {
this.onSlackChannelLeft(ircUser, event);
await this.onSlackChannelLeft(ircUser, event);
}
async onSlackSubteamUpdated(ircUser, event) {
ircUser.mapIrcToSlack(event.subteam.handle, event.subteam.id);
}
async onSlackReactionAdded(ircUser, event) {
if (event.type !== 'message') return;
await this.onSlackReaction(ircUser, event, 'reacts');
}
async onSlackReactionRemoved(ircUser, event) {
await this.onSlackReaction(ircUser, event, 'unreacts');
}
async onSlackReaction(ircUser, event, verb) {
if (event.item.type !== 'message') return;
if (!event.item_user) {
let history = await ircUser.slackWeb.apiCallOrThrow('channels.history', {
channel: event.item.channel,
Expand Down Expand Up @@ -456,31 +480,18 @@ class Irslackd {
this.logError(ircUser, util.inspect(e));
return;
}
let message = this.meText('reacts @ ' + ircReactee + ': ' + event.reaction);
let message = this.meText(verb + ' @ ' + ircReactee + ': ' + event.reaction);
this.ircd.write(ircUser.socket, ircReacter, 'PRIVMSG', [ ircChan, message ]);
}
async onSlackUserTyping(ircUser, event) {
/*
let self = this;
let ircNick = this.resolveSlackUser(ircUser, event.user);
let ircChan = this.resolveSlackChannel(ircUser, event.channel);
ircNick = await ircNick;
ircChan = await ircChan;
if (!ircNick || !ircChan || ircChan.substr(0, 1) !== '#') return;
if (ircUser.typingTimer) return;
this.ircd.write(ircUser.socket, 'irslackd', 'MODE', [ ircChan, '+v', ircNick ]);
ircUser.typingTimer = setTimeout(function() {
self.ircd.write(ircUser.socket, 'irslackd', 'MODE', [ ircChan, '-v', ircNick ]);
ircUser.typingTimer = null;
}, 5000);
*/
}
async onIrcWho(ircUser, msg) {
await this.onIrcWhois(ircUser, msg);
}
async onIrcWhois(ircUser, msg) {
if (msg.args.length < 1) return;
let ircNick = msg.args[0];
if (ircNick.substr(0, 1) === '#') {
return; // Ignore channels
}
let slackUser = ircUser.ircToSlack.get(ircNick);
if (!slackUser) return;
try {
Expand All @@ -494,7 +505,7 @@ class Irslackd {
':' + user.user.real_name,
]);
} catch (e) {
this.ircd.write(ircUser.socket, 'irslackd', '318', [ '=', ircNick, ':End of WHOIS list' ]);
this.ircd.write(ircUser.socket, 'irslackd', '401', [ '=', ircNick, ':No such nick/channel' ]);
}
}
onIrcLine(ircUser, line) {
Expand Down Expand Up @@ -585,7 +596,7 @@ class Irslackd {
if (pipe >= 0) link = '<' + link.substr(0, pipe) + '> ' + '(' + link.substr(pipe + 1) + ')';
return link;
}
let ircTarget = ircUser.slackToIrc.get(slackId);
let ircTarget = ircUser.slackToIrc.get(slackId); // TODO resolveSlackTarget?
if (!ircTarget) return match;
if (prefix === '!subteam^') {
prefix = '@';
Expand Down
5 changes: 3 additions & 2 deletions tests/mocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class MockIrcSocket extends EventEmitter {
let expectedLine = this.expectedLines[i];
if (expectedLine.trim() === actualLine.trim()) {
this.t.ok(true, testMsg);
this.expectedLines.splice(i, 1);
return true;
}
}
Expand Down Expand Up @@ -125,8 +126,8 @@ async function connectOneIrcClient(t) {
]});
slackWeb.expect('users.setPresence', { presence: 'auto' }, { ok: true });
slackWeb.expect('usergroups.list', { include_count: false, include_disabled: false, include_users: false }, { ok: true, usergroups: [
{ id: '', handle: '' },
{ id: '', handle: '' },
{ id: 'S1234GRP1', handle: '@group1' },
{ id: 'S1234GRP2', handle: '@group2' },
]});
slackWeb.expect('conversations.members', { channel: 'C1234CHAN1' }, { ok: true, members: [
'U1234USER',
Expand Down
23 changes: 23 additions & 0 deletions tests/test_react.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict';

const test = require('tape');
const mocks = require('./mocks');

test('slack_react', async(t) => {
t.plan(1 + mocks.connectOneIrcClient.planCount);
const c = await mocks.connectOneIrcClient(t);
c.ircSocket.expect(':test_slack_user PRIVMSG #test_chan_1 :' + String.fromCharCode(1) + 'ACTION reacts @ test_slack_barr: sunglasses' + String.fromCharCode(1));
await c.daemon.onSlackReactionAdded(c.ircUser, {
type: 'reaction_added',
user: 'U1234USER',
reaction: 'sunglasses',
item_user: 'U1235BARR',
item: {
type: 'message',
channel: 'C1234CHAN1',
ts: '1360782400.498405',
},
event_ts: '1360782804.083113',
});
t.end();
});
35 changes: 35 additions & 0 deletions tests/test_rename.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict';

const test = require('tape');
const mocks = require('./mocks');

test('slack_rename', async(t) => {
t.plan(6 + mocks.connectOneIrcClient.planCount);
const c = await mocks.connectOneIrcClient(t);
c.slackWeb.expect('conversations.info', { channel: 'C1234CHAN1' }, {
ok: true,
channel: {
id: 'C1234CHAN1',
topic: {
value: 'foobar topic here',
},
},
});
c.slackWeb.expect('conversations.members', { channel: 'C1234CHAN1' }, { ok: true, members: [
'U1234USER',
'U1235BARR',
]});
c.ircSocket.expect(':test_slack_user PART #test_chan_1');
c.ircSocket.expect(':test_slack_user JOIN #test_chan_new');
c.ircSocket.expect(':irslackd 332 test_slack_user #test_chan_new :foobar topic here');
c.ircSocket.expect(':irslackd 353 test_slack_user = #test_chan_new :test_slack_user test_slack_user test_slack_barr');
await c.daemon.onSlackChannelRename(c.ircUser, {
type: 'channel_rename',
channel: {
id: 'C1234CHAN1',
name: 'test_chan_new',
created: 1527736458,
},
});
t.end();
});
34 changes: 34 additions & 0 deletions tests/test_usergroup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'use strict';

const test = require('tape');
const mocks = require('./mocks');

test('slack_usergroup_updated', async(t) => {
t.plan(1 + mocks.connectOneIrcClient.planCount);
const c = await mocks.connectOneIrcClient(t);
await c.daemon.onSlackSubteamUpdated(c.ircUser, {
type: 'subteam_updated',
subteam: {
id: 'S1234GRP1',
handle: 'newgroup1',
},
});
c.slackWeb.expect('chat.postMessage', {
channel: 'C1234CHAN1',
text: 'hello <!subteam^S1234GRP1|@newgroup1>',
as_user: true,
thread_ts: null,
}, {
ok: true,
channel: 'C1234CHAN1',
ts: '1234.5678',
});
await c.daemon.onIrcPrivmsg(c.ircUser, { args: [ '#test_chan_1', 'hello @newgroup1' ] });
await c.daemon.onSlackMessage(c.ircUser, {
text: 'hello world <!subteam^S1234GRP1|@newgroup1>',
user: 'U1234USER',
channel: 'C1234CHAN1',
ts: '1234.5678',
});
t.end();
});
19 changes: 19 additions & 0 deletions tests/test_whois.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict';

const test = require('tape');
const mocks = require('./mocks');

test('irc_whois', async(t) => {
t.plan(2 + mocks.connectOneIrcClient.planCount);
const c = await mocks.connectOneIrcClient(t);
c.slackWeb.expect('users.info', { user: 'U1235QUUX' }, {
ok: true,
user: {
name: 'test_slack_quux',
real_name: 'John Quux',
},
});
c.ircSocket.expect(':irslackd 311 = test_slack_quux test_slack_quux irslackd * :John Quux');
await c.daemon.onIrcWhois(c.ircUser, { args: [ 'test_slack_quux' ] });
t.end();
});

0 comments on commit 67fd490

Please sign in to comment.