-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
873 lines (787 loc) · 25.7 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//
// Copyright (C) 2020, Sol Boucher
// Copyright (C) 2020, The Vanguard Campaign Corps Mods (vanguardcampaign.org)
const fs = require('fs');
const http = require('http');
const https = require('https');
const messages = require('./messages');
const querystring = require('querystring');
const README = 'README';
const USERMANUAL = 'User instructions';
const cache = {
lines: {},
line: function(workspace, channel, quiet = false) {
var id = workspace + '#' + channel;
if(!this.lines[id]) {
var iable = 'LINE_' + escaped(workspace) + '_' + escaped(channel);
var other = process.env[iable];
if(!other) {
if(!quiet)
console.log('Environment is missing $' + iable);
return null;
}
other = other.split('#');
if(other.length != 2) {
console.log('Environment variable $' + iable + ' is not #-delimited');
return null;
}
this.lines[id] = {
workspace: other[0],
channel: other[1],
};
}
return this.lines[id];
},
tokens: {},
token: function(workspace) {
if(!workspace)
return null;
workspace = workspace.split('#');
if(workspace.length > 2)
return null;
else if(workspace.length == 2)
this.tokens[workspace[0]] = workspace[1];
return this.tokens[workspace[0]]
},
channels: {},
uids: {},
users: {},
teams: {},
workspaces: {},
channel: function(id, workspace) {
return cached(this.channels, id, 'conversations.info', 'channel', 'name', workspace);
},
uid: function(name, channel, workspace) {
var user = this.uids[workspace + '#' + channel + '#' + name];
if(user)
return user;
var store = this.uids;
return Object.keys(this.uids).filter(function(each) {
return each.startsWith(workspace + '#' + channel + '#') && store[each];
}).map(function(each) {
return each.replace(/.*#/, '');
}).sort();
},
user: async function(id, channel, workspace, update = true) {
var profile = await cached(this.users, id, 'users.info', 'user', 'profile', workspace, update);
if(!profile)
return profile;
if(update)
this.uids[workspace + '#' + channel + '#' + profile.real_name] = id;
return {
name: profile.real_name,
avatar: profile.image_512,
};
},
unuser: async function(id, channel, workspace) {
this.uids[workspace + '#' + channel + '#' + (await this.user(id)).name] = undefined;
},
team: function(chanid) {
return this.teams[chanid];
},
workspace: function(id) {
return cached(this.workspaces, id, 'team.info', 'team', 'domain');
},
dms: {},
ims: {},
dm: function(uid) {
var dm = this.dms[uid];
if(!dm) {
dm = {};
this.dms[uid] = dm;
}
return dm;
},
dmers: function(uid) {
var dmers = [];
for(var dmer in this.dms)
if(this.dms[dmer].uid == uid)
dmers.push(dmer);
return dmers;
},
im: async function(uid, workspace, init) {
var ims = this.ims[workspace]
if(!ims) {
ims = {};
this.ims[workspace] = ims;
}
if(ims[uid])
return ims[uid];
else if(init) {
ims[uid] = init;
return init;
} else {
var channels = await collect_call('conversations.list?types=im',
null, 'channels', workspace);
if(!channels) {
console.log('Workspace \'' + workspace
+ '\' missing OAuth scope im:read (' + uid + ')?');
return null;
}
for(var channel of channels)
if(!ims[channel.user])
ims[channel.user] = channel.id;
return ims[uid];
}
},
imer: function(imid, workspace) {
var ims = this.ims[workspace];
if(!ims)
return null;
for(var uid in ims)
if(ims[uid] == imid)
return uid;
return null;
},
bootstrap: async function(token) {
token = this.token(token);
if(!token)
return false;
var workspace = await call('team.info', null, token);
if(!workspace || !workspace.ok)
return workspace;
this.workspaces[workspace.team.id] = workspace.team.domain;
var channels = await collect_call('conversations.list?types=public_channel,private_channel,im',
null, 'channels', token);
if(!channels) {
console.log('Missing OAuth scope channels:read, groups:read, and/or im:read?');
return false;
}
for(var channel of channels)
if(channel.is_im || this.line(workspace.team.domain, channel.name, true)) {
this.teams[channel.id] = workspace.team.id;
var members = await collect_call('conversations.members?channel=' + channel.id,
null, 'members', token);
if(channel.is_im)
await this.im(channel.user, workspace.team.domain, channel.id);
else
for(var member of members)
await this.user(member, channel.name, workspace.team.domain);
}
return true;
},
};
const dedup = {};
const PORT = process.env.PORT;
if(!PORT) {
console.log('Environment is missing $PORT');
process.exit(1);
}
const LOGGING = process.env.LOGGING != undefined;
const TOKEN_0 = cache.token(process.env.TOKEN_0);
if(!TOKEN_0) {
console.log('Environment is missing $TOKEN_0 or it is not #-delimited');
console.log('Only URL verification is supported in this configuration');
}
bootstrap();
async function cached(memo, key, method, parameter, argument, workspace, update = true) {
if(!memo[key]) {
var lookup = await call(method + '?' + parameter + '=' + key, null, workspace);
if(!lookup || !lookup.ok) {
console.log('Failed to cache API response: ' + JSON.stringify(lookup));
console.trace();
return null;
}
if(update)
memo[key] = lookup[parameter][argument];
}
return memo[key];
}
function call(method, body, workspace) {
var token = cache.token(workspace);
if(!token)
token = workspace;
if(!token)
token = TOKEN_0;
var header = {
headers: {
Authorization: 'Bearer ' + token,
},
};
var payload = '';
if(body) {
header.method = 'POST';
header.headers['Content-Type'] = 'application/json';
payload = JSON.stringify(body);
}
var request = https.request('https://slack.com/api/' + method, header);
var response = new Promise(function(resolve) {
request.on('response', async function(res) {
resolve(JSON.parse(await stringify(res)));
});
});
request.end(payload);
return response;
}
async function collect_call(method, body, array, workspace) {
var collected = [];
var cursor = '';
var delim = '?';
if(method.indexOf('?') != -1)
delim = '&';
do {
var it = await call(method + cursor, body, workspace);
if(!it.ok)
return null;
collected = collected.concat(it[array]);
if(it.response_metadata && it.response_metadata.next_cursor) {
cursor = it.response_metadata.next_cursor;
cursor = delim + 'cursor=' + cursor.replace(/=/g, '%3D');
} else
cursor = '';
} while(cursor);
return collected;
}
function escaped(varname) {
return varname.replace(/-/g, '__hyphen__');
}
function stringify(stream) {
return new Promise(function(resolve) {
var chunks = [];
stream.setEncoding('utf8');
stream.on('data', function(chunk) {
chunks.push(chunk);
});
stream.on('end', function() {
resolve(chunks.join(''));
});
});
}
async function replace(string, regex, async_func) {
var futures = [];
string.replace(regex, function(...args) {
futures.push(async_func(...args));
});
var strings = await Promise.all(futures);
return string.replace(regex, function() {
return strings.shift();
});
}
async function process_users(in_workspace, in_channel, in_user, message, out_workspace, out_channel, display) {
if(typeof out_channel == 'string') {
var uid = cache.imer(out_channel, out_workspace);
if(uid) {
var name = await cache.user(uid, cache.line(in_workspace, in_channel), out_workspace);
out_channel = {
[name.name]: uid,
};
}
}
if(message.startsWith('@') || message.search(/[^<`]@/) != -1)
warning(in_workspace, display, in_user,
'*Warning:* If you want to tag someone in the bridged channel,'
+ ' you must enclose the mention in backticks (e.g., `@Their Name`).'
+ '\n_Edit your message if you wish to notify people!_');
var locals = {};
message = await replace(message, /<@([A-Z0-9]+)>/g, async function(orig, user) {
// Skip updating cache because it is possible to mention a user not in the channel!
user = await cache.user(user, in_channel, in_workspace, false);
if(user) {
locals[user.name] = true;
return '`@' + user.name + '`';
}
return orig;
});
var mismatches = [];
if(typeof out_channel == 'string')
message = await replace(message, /`@([^`]*)`/g, async function(orig, user) {
var uid = await cache.uid(user, out_channel, out_workspace);
if(!Array.isArray(uid))
return '<@' + uid + '>';
mismatches.push(user);
return orig;
});
else {
message = await replace(message, /`@([^`]*)`/g, async function(orig, user) {
var uid = out_channel[user];
if(uid)
return '<@' + uid + '>';
mismatches.push(user);
return orig;
});
out_channel = Array.from(Object.keys(out_channel));
}
mismatches = mismatches.filter(function(each) {
return !locals[each];
});
if(mismatches.length)
warning(in_workspace, display, in_user,
'*Warning:* Could not find anyone by the name(s) \''
+ mismatches.join('\', \'') + '\'!'
+ '\nMaybe you meant one of these people:'
+ '\n' + await list_users(out_workspace, out_channel) + '\n'
+ '_If so, edit your message so they will be notified!_');
return message;
}
async function process_args(in_workspace, in_channel, args) {
args = await replace(args, /<#([^|>]*)(|[^>]*)?>/, async function(orig, cid, cname) {
var channel = await cache.channel(cid, in_workspace);
if(channel) {
in_channel = channel;
return channel;
}
return cname.substring(1);
});
args = args.replace(/#(\S*)/, function(orig, cname) {
in_channel = cname;
return cname;
});
if(!in_channel)
return args;
args = await replace(args, /<@([^|>]*)(|[^>]*)?>/g, async function(orig, uid, uname) {
var user = await cache.user(uid, in_channel, in_workspace, false);
if(user)
return user.name;
return uname.substring(1);
});
args = args.replace(/`?@([^\`]*)`?/g, function(orig, uname) {
return uname;
});
return args;
}
async function list_users(workspace, channel) {
var users = channel;
if(!Array.isArray(channel))
users = await cache.uid('', channel, workspace);
return '`@' + users.join('`\n`@') + '`';
}
async function is_member(workspace, channel, uid) {
return !Array.isArray(cache.uid(
(await cache.user(uid, channel, workspace, false)).name,
channel, workspace));
}
async function select_user(dmer, in_workspace, in_channel, out_workspace, dmee, command) {
var dm = cache.dm(dmer);
dm.out_workspace = out_workspace;
dm.in_channel = in_channel;
dm.uid = dmee;
if(command)
dm.command = command;
// Cleaning the channel is actually necessary for its side effect of refreshing the cache
// of user ID -> DM channel mappings. Otherwise, building the DM mention table will fail.
var cleaned = await clean_channel(in_workspace, dmer)
var user = await cache.user(dmee, cache.line(in_workspace, in_channel), out_workspace);
await call('chat.postMessage', {
channel: dmer,
text: 'You are now DM\'ing `@' + user.name + '` from #' + in_channel + '.',
}, in_workspace);
if(!cleaned && dm.command)
warning(in_workspace, dmer, dmer,
'_To change this, use_ *' + dm.command + ' dm* _at any time._');
}
async function clean_channel(workspace, user) {
// The cache refresh triggered by this lookup is important when responding in a DM thread
// triggers an automatic DM recipient switch! See the related comment in select_user().
var convo = await cache.im(user, workspace);
var modified = false;
var latest;
await call('conversations.history?channel=' + convo + '&limit=1', null, workspace);
while((latest = (await call('conversations.history?channel='
+ convo + '&limit=1', null, workspace)).messages)
&& (latest = latest[0]) && latest.bot_id && !latest.username) {
await call('chat.delete', {
channel: user,
ts: latest.ts,
}, workspace);
modified = true;
}
return modified;
}
function warning(workspace, channel, user, text) {
return call('chat.postEphemeral', {
channel: channel,
user: user,
text: text,
}, workspace);
}
function failure(workspace, channel, user, code) {
var mess = '*Error* sending message: \'' + code + '\'\n'
switch(code) {
case 'channel_not_found':
case 'invalid_auth':
case 'missing_scope':
case 'not_allowed_token_type':
mess += 'This probably indicates that the Slackline server is misconfigured;'
+ ' please report this message to an infrastructure admin!\n';
break;
case 'not_in_channel':
mess += 'The destination channel exists, but does not contain a Slackline bot user;'
+ ' please report this message to a Slack admin!\n';
break;
case 'is_archived':
mess += 'The private destination channel has been archived.\n';
break;
case 'restricted_action':
mess += 'This may indicate that the destination channel disallows channel mentions;'
+ ' if you included an <!channel> (or similar), try resending without.\n';
break;
case 'account_inactive':
mess += 'The Slackline bot user was uninstalled from the destination workspace.\n';
break;
}
mess += '_Your message was probably not delivered to the bridged channel!_';
return warning(workspace, channel, user, mess);
}
async function bootstrap() {
for(var index = 0; process.env['TOKEN_' + index]; ++index)
if(!await cache.bootstrap(process.env['TOKEN_' + index]))
console.log('Failed to authenticate with token ' + index);
http.createServer(handle_connection).listen(PORT);
}
async function handle_connection(request, response) {
var payload = await stringify(request);
if(dedup[payload]) {
console.log('Acknowledging duplicate request');
return response.end();
}
dedup[payload] = true;
if(!payload) {
console.log('Empty request payload');
return response.end('Empty request payload');
}
if(payload.startsWith('{'))
payload = JSON.parse(payload);
else
payload = querystring.parse(payload);
switch(payload.type) {
case undefined:
return response.end(await handle_command(payload));
case 'event_callback':
if(!payload.event) {
console.log('event_callback without associated event in payload: ' + payload);
return response.end('event_callback without associated event');
}
handle_event(payload.event);
return response.end();
case 'url_verification':
return response.end(payload.challenge);
default:
console.log('Unhandled request type in payload: ' + payload);
return response.end('Unhandled request type: \'' + payload.type + '\'');
}
}
async function handle_command(payload) {
var command = payload.text.replace(/\s.*/, '');
var channel;
if(payload.channel_name == 'directmessage') {
if(!(channel = cache.dm(payload.user_id).in_channel))
channel = '';
} else
channel = await cache.channel(payload.channel_id, payload.team_domain);
var args = await process_args(payload.team_domain, channel, payload.text.replace(/\S+\s*/, ''));
var explicitchannel;
var error = '';
switch(command) {
case 'dm':
var argv = args.match(/(.*) - (.*)/);
if(argv) {
explicitchannel = channel;
args = argv[1];
channel = argv[2];
}
case 'list':
if(command == 'list' && args)
channel = args;
if(!channel)
return '*Error:* You must specify a bridged channel (could not infer it)!\n'
+ '_See_ *' + payload.command + ' help*.';
var paired = await cache.line(payload.team_domain, channel, true);
if(!paired && explicitchannel) {
// Maybe the user has a ' - ' in their name? Fall back to channel inference.
args += ' - ' + channel;
channel = explicitchannel;
paired = await cache.line(payload.team_domain, channel, true);
}
if(!paired) {
if(command == 'dm') {
cache.dm(payload.user_id).uid = undefined;
await clean_channel(payload.team_domain, payload.user_id);
}
return '*Error:* The channel \'' + channel + '\' is not bridged!';
} else if(!await is_member(payload.team_domain, channel, payload.user_id)) {
if(command == 'dm') {
cache.dm(payload.user_id).uid = undefined;
await clean_channel(payload.team_domain, payload.user_id);
}
return '*Error:* You are not a member of channel \'' + channel + '\'!';
}
if(command == 'list')
return 'Members bridged with channel \'' + channel + '\':\n'
+ await list_users(paired.workspace, paired.channel);
if(!args) {
cache.dm(payload.user_id).uid = undefined;
await clean_channel(payload.team_domain, payload.user_id);
return '*Error:* You must specify a user to direct message!\n'
+ '_See_ *' + payload.command + ' help* (on the *dm* command).';
}
var uid = await cache.uid(args, paired.channel, paired.workspace);
if(Array.isArray(uid)) {
cache.dm(payload.user_id).uid = undefined;
await clean_channel(payload.team_domain, payload.user_id);
return '*Error:* Could not find anyone by the name \''
+ args + '\' bridged with channel \'' + channel + '\'!'
+ '\nMaybe you meant one of these people:\n'
+ await list_users(paired.workspace, paired.channel);
}
select_user(payload.user_id, payload.team_domain, channel, paired.workspace, uid, payload.command);
return '';
case 'manual':
var readme = await fs.promises.readFile(README, {
encoding: 'utf8',
});
readme = readme.split('\n\n');
var usermanual = readme.findIndex(function(elem) {
return elem.startsWith(USERMANUAL);
});
return readme[usermanual + 1];
default:
error = '*Error:* Unrecognized command: \'' + command + '\'\n';
case 'help':
error += 'Supported commands:'
+ '\n>' + payload.command + ' help\n\tShow this help'
+ '\n>' + payload.command + ' manual\n\tShow detailed user documentation'
+ '\n>' + payload.command + ' list [channel]\n\tList bridged members of current channel (or specified [channel])'
+ '\n>' + payload.command + ' dm <user> [- channel]\n\tDirect message specified <user> (bridged via [channel])'
+ '\n\n_Note: In the above commands, <word> and [word] are not part of the command;'
+ ' rather, each <word> is a required argument that you must replace,'
+ ' and each [word] is an optional argument that you may either omit or replace._';
return error;
}
}
async function handle_event(event) {
if(event.type == 'member_joined_channel') {
handle_join(event);
return;
} else if(event.type == 'member_left_channel') {
handle_leave(event);
return;
} else if(event.type == 'reaction_added') {
if(await messages(event.item.ts)) {
var workspace = await cache.workspace(cache.team(event.item.channel));
warning(workspace, event.item.channel, event.user,
'*Warning:* Emoji reactions are currently unsupported.'
+ '\n_If you want the other channel to see, send an emoji message!_');
}
return;
} else if(event.subtype == 'file_share') {
var workspace = await cache.workspace(cache.team(event.channel));
warning(workspace, event.channel, event.user,
'*Warning:* File uploads are currently unsupported.'
+ '\n_If you want the other channel to see, link to cloud storage instead!_');
} else if(event.type != 'message') {
console.log('Unhandled type in event: ' + JSON.stringify(event));
return;
} else if(event.bot_id || (event.message && event.message.bot_id))
return;
if(LOGGING)
console.log(event);
var team = event.team;
if(event.subtype == 'message_deleted') {
var copy = await messages(event.deleted_ts);
if(copy) {
var ack = await call('chat.delete', {
channel: copy.out_conversation,
ts: copy.out_ts,
}, copy.out_workspace);
if(LOGGING)
console.log(ack);
await messages(event.deleted_ts, null);
await messages(copy.out_ts, null);
}
return;
} else if(event.subtype == 'message_changed') {
var copy = await messages(event.message.ts);
if(copy) {
var message = {
channel: copy.out_conversation,
ts: copy.out_ts,
text: event.message.text,
};
if(event.message.user)
await cache.user(event.message.user, copy.in_channel, copy.in_workspace);
message.text = await process_users(copy.in_workspace, copy.in_channel, event.message.user,
message.text, copy.out_workspace, copy.out_channel, event.channel);
var ack = await call('chat.update', message, copy.out_workspace);
if(LOGGING)
console.log(ack);
if(!ack.ok)
failure(workspace, event.channel, event.message.user, ack.error);
} else
warning(workspace, event.channel, event.message.user,
'*Error:* You cannot edit this message because it failed to send.');
return;
} else if(event.subtype && (event.subtype == 'thread_broadcast'
|| event.subtype.endsWith('_join') || event.subtype.endsWith('_leave')
|| event.subtype == 'file_share'))
team = cache.team(event.channel);
var message = {
text: event.text,
};
var thread;
if(event.thread_ts) {
thread = await messages(event.thread_ts);
if(thread) {
message.thread_ts = thread.out_ts;
if(event.subtype == 'thread_broadcast')
message.reply_broadcast = true;
}
}
var workspace = await cache.workspace(team);
if(!workspace)
return;
var channel;
var paired;
if(event.channel_type == 'im') {
var dm = cache.dm(event.user);
if(dm.uid) {
channel = dm.in_channel;
paired = {
workspace: dm.out_workspace,
channel: await cache.im(dm.uid, dm.out_workspace),
};
if(!paired.channel)
paired.uid = dm.uid;
}
var error;
if(message.thread_ts && (!paired
|| thread.out_workspace != paired.workspace || thread.out_conversation != paired.channel)) {
channel = cache.line(thread.out_workspace, thread.out_channel, true);
if(channel)
channel = channel.channel;
else
channel = thread.in_channel;
var remote;
if(!await is_member(workspace, channel, event.user))
error = '*Error:* You can no longer DM this person because you have'
+ ' been removed from the \'' + channel + '\' channel!';
else if((remote = cache.line(workspace, channel).channel)
&& !await is_member(thread.out_workspace, remote,
cache.imer(thread.out_conversation, thread.out_workspace)))
error = '*Error:* You can no longer DM this person because they have'
+ ' been unbridged from the \'' + channel + '\' channel!';
else {
paired = {
workspace: thread.out_workspace,
channel: thread.out_conversation,
};
var meta = await call('conversations.info?channel='
+ paired.channel, null, paired.workspace);
select_user(event.user, workspace, channel, paired.workspace, meta.channel.user);
}
}
if(!channel || !paired) {
await call('reactions.add', {
channel: event.channel,
timestamp: event.ts,
name: 'warning',
}, workspace);
if(!error)
error = '*Error:* You must either reply in a thread or specify a user to direct message!\n'
+ '_For help: click my avatar, choose an option beginning with \'/\', and hit send._';
warning(workspace, event.channel, event.user, error);
return;
}
} else {
channel = await cache.channel(event.channel, workspace);
paired = cache.line(workspace, channel);
if(!channel || !paired)
return;
}
message.channel = paired.channel;
if(!message.channel)
// DM'ing a user who has never opened the bot's Messages tab!
message.channel = paired.uid;
var user = await cache.user(event.user, channel, workspace);
if(user) {
var users = paired.channel;
message.icon_url = user.avatar;
message.username = user.name;
if(event.channel_type == 'im') {
message.username += ' - ' + cache.line(workspace, channel).channel;
if(!cache.imer(paired.channel, paired.workspace)) {
// The DM channel doesn't exist yet!
var name = await cache.user(paired.uid, cache.line(workspace, channel), paired.workspace);
users = {
[name.name]: paired.uid,
};
}
}
message.text = await process_users(workspace, channel, event.user,
message.text, paired.workspace, users, event.channel);
}
var ack = await call('chat.postMessage', message, paired.workspace);
if(LOGGING)
console.log(ack);
if(!ack.ok) {
await call('reactions.add', {
channel: event.channel,
timestamp: event.ts,
name: 'warning',
}, workspace);
failure(workspace, event.channel, event.user, ack.error);
return;
}
if(!paired.channel) {
// Newly-created DM channel!
paired.channel = ack.channel;
await cache.im(paired.uid, paired.workspace, paired.channel);
}
await messages(event.ts, {
in_workspace: workspace,
in_channel: channel,
out_workspace: paired.workspace,
out_channel: paired.channel,
out_conversation: ack.channel,
out_ts: ack.ts,
});
await messages(ack.ts, {
in_workspace: paired.workspace,
in_channel: paired.channel,
out_workspace: workspace,
out_channel: channel,
out_conversation: event.channel,
out_ts: event.ts,
});
}
async function handle_join(event) {
var workspace = await cache.workspace(event.team);
var channel = await cache.channel(event.channel, workspace);
await cache.user(event.user, channel, workspace);
}
async function handle_leave(event) {
var workspace = await cache.workspace(event.team);
var channel = await cache.channel(event.channel, workspace);
await cache.unuser(event.user, channel, workspace);
var dm = cache.dm(event.user);
if(dm && dm.in_channel == channel) {
dm.uid = undefined;
await clean_channel(workspace, event.user);
warning(workspace, event.user, event.user,
'You can no longer DM this person because you have been removed from the'
+ ' \'' + channel + '\' channel.');
}
var paired;
var dmers = cache.dmers(event.user);
for(var dmer of dmers) {
if(!paired)
paired = cache.line(workspace, channel);
var dimmer = cache.dm(dmer);
dimmer.uid = undefined;
await clean_channel(paired.workspace, dmer);
warning(paired.workspace, dmer, dmer,
'You can no longer DM this person because they have been unbridged from the'
+ ' \'' + dimmer.in_channel + '\' channel.');
}
}