/
list.js
499 lines (462 loc) · 13.7 KB
/
list.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
"use strict";
// Description:
// A script that expands mentions of lists. Lists themselves can be used as
// members if prepended with '&', and mentions will be expanded recursively.
//
// Commands:
// hubot list lists - list all list names
// hubot list dump - list all list names and members
// hubot list create <list> - create a new list
// hubot list destroy <list> - destroy a list
// hubot list rename <old> <new> - rename a list
// hubot list add <list> <name> - add name to a list
// hubot list invite <list> <name> - invite to a list
// hubot list remove <list> <name> - remove name from a list
// hubot list info <list> - list members in list
// hubot list membership <name> - list lists that name is in
//
// Dependencies:
// None
//
// Configuration:
// HUBOT_LIST_ADMINS - Specify a comma-separated list of user IDs to
// designate as admins that can manage and send messages to the lists.
// This is in addition to the built-in admin group. This setting defaults
// to ''.
// HUBOT_LIST_DECORATOR - a character indicating how to decorate usernames.
// Valid settings are '<', '(', '[', and '{'. This variable can also be left
// unset. This setting defaults to ''.
// HUBOT_LIST_PREPEND_USERNAME - set to 'true' to disable prepending the
// original username to the prepended message. This variable can also be
// left unset. This setting defaults to 'false'.
// HUBOT_LIST_RECURSE - set to 'false' to disable recursive list expansion.
// The setting defaults to 'true'.
// HUBOT_LIST_AUTH - set to 'true' to enable a hubot-auth compatible wrapper
// for using hubot-list as a drop-in replacement for other scripts that
// may use hubot-auth. This setting defaults to 'false'.
//
//
// Author:
// Josh King <jking@chambana.net>, based on hubot-group by anishathalye
const IDENTIFIER = "[-._a-zA-Z0-9]+";
const LIST_ADMINS = process.env.HUBOT_LIST_ADMINS.split(",") || "";
const LIST_DECORATOR = process.env.HUBOT_LIST_DECORATOR || "";
const LIST_PREPEND_USERNAME = process.env.HUBOT_LIST_PREPEND_USERNAME || false;
const LIST_RECURSE = process.env.HUBOT_LIST_RECURSE || false;
const LIST_AUTH = process.env.HUBOT_LIST_AUTH || false;
function sorted(arr) {
const copy = Array.from(arr);
return copy.sort();
}
class List {
constructor(robot) {
this.load = this.load.bind(this);
this.members = this.members.bind(this);
this.lists = this.lists.bind(this);
this.exists = this.exists.bind(this);
this.create = this.create.bind(this);
this.destroy = this.destroy.bind(this);
this.rename = this.rename.bind(this);
this.add = this.add.bind(this);
this.remove = this.remove.bind(this);
this.membership = this.membership.bind(this);
this.ismember = this.ismember.bind(this);
this.isAdmin = this.isAdmin.bind(this);
this.robot = robot;
this.cache = {};
this.robot.brain.on("loaded", this.load);
if (this.robot.brain.data.users.length) {
this.load();
}
}
load() {
if (this.robot.brain.data.list) {
this.cache = this.robot.brain.data.list;
} else {
this.robot.brain.data.list = this.cache;
}
if (!this.exists("admins")) {
this.create("admins");
}
}
members(list) {
return sorted(this.cache[list] || []);
}
lists() {
return sorted(Object.keys(this.cache));
}
exists(list) {
return this.cache[list] != null;
}
create(list) {
if (this.exists(list)) {
return false;
} else {
this.cache[list] = [];
return true;
}
}
destroy(list) {
if (this.exists(list)) {
const mem = this.members(list);
delete this.cache[list];
return mem;
} else {
return null;
}
}
rename(from, to) {
if (!this.exists(from) || this.exists(to)) {
return false;
} else {
this.cache[to] = this.cache[from];
delete this.cache[from];
return true;
}
}
add(list, name) {
if (!this.exists(list)) {
this.robot.logger.error(
`Trying to add user ${name} to list ${list} but list doesn't exist.`
);
return false;
}
if (Array.from(this.cache[list]).includes(name)) {
this.robot.logger.error(
`Trying to add user ${name} to list ${list} but user is already on that list.`
);
return false;
} else {
this.robot.logger.info(`Adding user ${name} to list ${list}.`);
this.cache[list].push(name);
return true;
}
}
remove(list, name) {
if (!this.exists(list)) {
return false;
}
if (Array.from(this.cache[list]).includes(name)) {
const idx = this.cache[list].indexOf(name);
this.cache[list].splice(idx, 1);
return true;
} else {
return false;
}
}
membership(name) {
const lists = [];
for (let list of Object.keys(this.cache || {})) {
const names = this.cache[list];
if (Array.from(names).includes(name)) {
lists.push(list);
}
}
return lists;
}
ismember(list, name) {
if (!this.exists(list)) {
return false;
}
if (Array.from(this.cache[list]).includes(name)) {
return true;
} else {
return false;
}
}
isAdmin(name) {
return (
Array.from(LIST_ADMINS).includes(name) || this.ismember("admins", name)
);
}
}
class Auth {
constructor(list) {
this.isAdmin = this.isAdmin.bind(this);
this.hasRole = this.hasRole.bind(this);
this.usersWithRole = this.usersWithRole.bind(this);
this.userRoles = this.userRoles.bind(this);
this.list = list;
}
isAdmin(user) {
return this.list.isAdmin(user.id);
}
hasRole(user, role) {
return this.list.ismember(role, user.id);
}
usersWithRole(role) {
return this.list.members(role);
}
userRoles(user) {
return this.list.membership(user.id);
}
}
module.exports = function(robot) {
const list = new List(robot);
if (LIST_AUTH) {
robot.auth = new Auth(list);
}
robot.listenerMiddleware((context, next, done) => {
if (context.listener.options.id === "list.send") {
if (list.isAdmin(context.response.message.user.id)) {
// User is allowed access to this command
return next();
} else {
// Fail silently
return done();
}
} else if (context.listener.options.id === "list.response") {
return next();
} else if (
context.listener.options.id &&
context.listener.options.id.match(
new RegExp(`^list\\.[a-zA-Z0-9]+$`, "i")
)
) {
if (list.isAdmin(context.response.message.user.id)) {
// User is allowed access to this command
return next();
} else {
// Restricted command, but user isn't in whitelist
context.response.reply(
`I'm sorry, @${context.response.message.user.name}, but you don't have access to do that.`
);
return done();
}
} else {
// This is not a restricted command; allow everyone
return next();
}
});
const decorate = name => {
switch (LIST_DECORATOR) {
case "<":
return `<@${name}>`;
case "(":
return `(@${name})`;
case "[":
return `[@${name}]`;
case "{":
return `{@${name}}`;
default:
return `@${name}`;
}
};
robot.hear(new RegExp(`@${IDENTIFIER}`), { id: "list.send" }, res => {
let mem;
const response = [];
const tagged = [];
for (var g of Array.from(list.lists())) {
if (new RegExp(`(^|\\s)@${g}\\b`).test(res.message.text)) {
tagged.push(g);
}
}
if (LIST_RECURSE !== "false") {
const process = Array.from(tagged);
while (process.length > 0) {
g = process.shift();
for (mem of Array.from(list.members(g))) {
if (mem[0] === "&") {
mem = mem.substring(1);
// it's a list
if (
!Array.from(process).includes(mem) &&
!Array.from(tagged).includes(mem)
) {
tagged.push(mem);
process.push(mem);
}
}
}
}
}
// output results
const decorated = {};
const decorateOnce = name => {
if (name[0] === "&" || decorated[name]) {
return name;
} else {
decorated[name] = true;
return decorate(name);
}
};
let { text } = res.message;
if (LIST_PREPEND_USERNAME === "true") {
text = `${res.message.user.name}: ${message}`;
}
return (() => {
const result = [];
for (g of Array.from(tagged)) {
mem = list.members(g);
if (mem.length > 0) {
if (["SlackBot", "Room"].includes(robot.adapter.constructor.name)) {
result.push(
(() => {
const result1 = [];
for (let m of Array.from(mem)) {
const room = robot.adapter.client.rtm.dataStore.getDMByName(
m
);
result1.push(res.send({ room: room.id }, text));
}
return result1;
})()
);
} else if (["Signal"].includes(robot.adapter.constructor.name)) {
result.push(
(() => {
const result1 = [];
for (let m of Array.from(mem)) {
result1.push(robot.messageRoom(m, text));
}
return result1;
})()
);
} else {
result.push(undefined);
}
} else {
result.push(undefined);
}
}
return result;
})();
});
robot.respond("/list lists/i", { id: "list.lists" }, res =>
res.send(`Lists: ${list.lists().join(", ")}`)
);
robot.respond(new RegExp(`[L|l]ist\\s+dump`), { id: "list.dump" }, res => {
const response = [];
for (let g of Array.from(list.lists())) {
response.push(`*@${g}*: ${list.members(g).join(", ")}`);
}
if (response.length > 0) {
res.send(response.join("\n"));
}
});
robot.respond("/list create (.*)/i", { id: "list.create" }, res => {
const name = res.match[1];
if (list.create(name)) {
res.send(`Created list ${name}.`);
} else {
res.send(`List ${name} already exists!`);
}
});
robot.respond("/list destroy (.*)/i", { id: "list.destroy" }, res => {
const name = res.match[1];
const old = list.destroy(name);
if (old !== null) {
res.send(`Destroyed list ${name} (${old.join(", ")}).`);
} else {
res.send(`List ${name} does not exist!`);
}
});
robot.respond(
"/list rename (.*) (to)? (.*)/i",
{ id: "list.rename" },
res => {
const from = res.match[1];
const to = res.match[3];
if (list.rename(from, to)) {
res.send(`Renamed list ${from} to ${to}.`);
} else {
res.send(`Either list ${from} does not exist or ${to} already exists!`);
}
}
);
robot.respond("/list add (.*) (.*)/i", { id: "list.add" }, res => {
const g = res.match[1];
let names = res.match[2];
names = names.split(/\s+/);
if (!list.exists(g)) {
res.send(`List ${g} does not exist!`);
return;
}
const response = [];
for (let name of Array.from(names)) {
if (list.add(g, name)) {
response.push(`${name} added to list ${g}.`);
} else {
response.push(`${name} is already in list ${g}!`);
}
}
res.send(response.join("\n"));
});
robot.respond("/list remove (.*) (.*)/i", { id: "list.remove" }, res => {
const g = res.match[1];
let names = res.match[2];
names = names.split(/\s+/);
if (!list.exists(g)) {
res.send(`List ${g} does not exist!`);
return;
}
const response = [];
for (let name of Array.from(names)) {
if (list.remove(g, name)) {
response.push(`${name} removed from list ${g}.`);
} else {
response.push(`${name} is not in list ${g}!`);
}
}
res.send(response.join("\n"));
});
robot.respond("/list invite (.*) (.*)/i", { id: "list.invite" }, res => {
const g = res.match[1];
let names = res.match[2];
names = names.split(/\s+/);
if (!list.exists(g)) {
res.send(`List ${g} does not exist!`);
return;
}
const response = [];
for (let name of Array.from(names)) {
if (!list.ismember(g, name)) {
robot.messageRoom(
name,
`You have been invited to the list ${g}, reply 'YES' (all uppercase) to accept or 'NO' to reject.`
);
const user = robot.brain.userForName(name);
if (!user.invited_to) {
user.invited_to = [];
}
user.invited_to.push(g);
response.push(`${name} invited to list ${g}.`);
} else {
response.push(`${name} is already in list ${g}!`);
}
}
res.send(response.join("\n"));
});
robot.respond("/(YES|NO)/", { id: "list.response" }, res => {
const answer = res.match[1];
const user = robot.brain.userForId(res.message.user.id);
if (user.invited_to) {
if (answer === "YES") {
user.invited_to.forEach(l => {
list.add(l, user.id);
});
res.send("Okay, adding you to the list!");
} else if (answer === "NO") {
user.invited_to = null;
res.send("Okay, not adding you to the list!");
}
}
});
robot.respond("/list info (.*)/i", { id: "list.info" }, res => {
const name = res.match[1];
if (!list.exists(name)) {
res.send(`List ${name} does not exist!`);
return;
}
res.send(`*@${name}*: ${list.members(name).join(", ")}`);
});
robot.respond("/list membership (.*)/i", { id: "list.membership" }, res => {
const name = res.match[1];
const lists = list.membership(name);
if (lists.length > 0) {
res.send(`${name} is in ${list.membership(name).join(", ")}.`);
} else {
res.send(`${name} is not in any lists!`);
}
});
};