-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathAdminCommands.js
250 lines (219 loc) · 7.27 KB
/
AdminCommands.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
"use strict";
var Promise = require('bluebird');
var AdminCommand = require("./AdminCommand");
var adminCommands = {};
function quotemeta(s) { return s.replace(/\W/g, '\\$&'); }
adminCommands.help = AdminCommand.makeHelpCommand(adminCommands);
adminCommands.list = new AdminCommand({
desc: "list the linked rooms",
opts: {
team: {
description: "Filter only rooms for this Slack team domain",
aliases: ['T'],
},
room: {
description: "Filter only Matrix room IDs containing this string fragment",
aliases: ['R'],
},
},
func: function(main, opts, args, respond) {
var name_filter;
if (opts.team) {
name_filter = new RegExp("^" + quotemeta(opts.team) + "\.#");
}
var found = 0;
main._rooms.forEach((room) => {
var matrix_room_id = room.getMatrixRoomId();
var slack_channel_id = room.getSlackChannelId();
var slack_channel_name = room.getSlackChannelName() || "UNKNOWN";
if (name_filter && !name_filter.exec(slack_channel_name)) {
return;
}
if (opts.room && matrix_room_id.indexOf(opts.room) === -1) {
return;
}
var slack = slack_channel_id ?
slack_channel_name + "(" + slack_channel_id + ")" :
slack_channel_name;
var status = room.getStatus();
if (!status.match(/^ready/)) status = status.toUpperCase();
found++;
respond(status + " " + slack + " -- " + matrix_room_id);
});
if (!found) {
respond("No rooms found");
}
},
});
adminCommands.show = new AdminCommand({
desc: "show a single connected room",
opts: {
channel_id: {
"channel_id|I": "Slack channel ID",
aliases: ['I'],
},
channel: {
description: "Slack channel name",
aliases: ['C'],
},
room: {
description: "Matrix room ID",
aliases: ['R'],
},
},
func: function(main, opts, args, respond) {
console.log("opts are", opts);
console.log("opts keys are", Object.keys(opts));
if (Object.keys(opts).length != 1) {
return Promise.reject("Require exactly one of --room, --channel or --channel_id");
}
var room = (opts.channel_id) ? main.getRoomBySlackChannelId(opts.channel_id) :
(opts.channel) ? main.getRoomBySlackChannelName(opts.channel) :
main.getRoomByMatrixRoomId(opts.room);
if (!room) {
respond("No such room");
return;
}
respond("Bridged Room:");
respond(" Status: " + room.getStatus());
respond(" Slack Name: " + room.getSlackChannelName() || "PENDING");
respond(" Webhook URI: " + room.getSlackWebhookUri());
respond(" Inbound ID: " + room.getInboundId());
respond(" Inbound URL: " + main.getInboundUrlForRoom(room));
respond(" Matrix room ID: " + room.getMatrixRoomId());
var oauth2 = main.getOAuth2();
if (oauth2) {
var authorize_url = oauth2.makeAuthorizeURL({
room: room,
state: room.getInboundId(),
});
respond(" OAuth2 authorize URL: " + authorize_url);
}
},
});
adminCommands.link = new AdminCommand({
desc: "connect a Matrix and a Slack room together",
opts: {
channel_id: {
description: "Slack channel ID",
aliases: ['I'],
},
channel: {
description: "Slack channel name",
aliases: ['C'],
},
room: {
description: "Matrix room ID",
aliases: ['R'],
required: true,
},
webhook_url: {
description: "Slack webhook URL. Used with slack outgoing hooks integration",
aliases: ['u'],
},
slack_bot_token: {
description: "Slack bot user token. Used with slack bot user & Events api",
aliases: ['t'],
},
slack_user_token: {
description: "Slack user token. Used to bridge files",
}
},
func: function(main, opts, args, respond) {
return main.actionLink({
matrix_room_id: opts.room,
slack_channel_name: opts.channel,
slack_channel_id: opts.channel_id,
slack_webhook_uri: opts.webhook_url,
slack_bot_token: opts.slack_bot_token,
slack_user_token: opts.slack_user_token,
}).then(
(room) => {
respond("Room is now " + room.getStatus());
if (room.getSlackWebhookUri()) {
respond("Inbound URL is " + main.getInboundUrlForRoom(room));
}
},
(e) => { respond("Cannot link - " + e ) }
);
},
});
adminCommands.unlink = new AdminCommand({
desc: "disconnect a linked Matrix and Slack room",
opts: {
channel_id: {
description: "Slack channel ID",
aliases: ['I'],
},
channel: {
description: "Slack channel name",
aliases: ['C'],
},
room: {
description: "Matrix room ID",
aliases: ['R'],
required: true,
},
},
func: function(main, opts, args, respond) {
return main.actionUnlink({
matrix_room_id: opts.room,
slack_channel_name: opts.channel,
slack_channel_id: opts.channel_id,
}).then(
() => { respond("Unlinked") },
(e) => { respond("Cannot unlink - " + e) }
);
},
});
adminCommands.join = new AdminCommand({
desc: "join a new room",
opts: {
room: {
description: "Matrix room ID",
aliases: ['R'],
},
},
args: ["room"],
func: function(main, opts, args, respond) {
return main.getBotIntent().join(opts.room).then(() => {
respond("Joined");
});
},
});
adminCommands.leave = new AdminCommand({
desc: "leave an unlinked room",
opts: {
room: {
description: "Matrix room ID",
aliases: ['R'],
},
},
args: ["room"],
func: function(main, opts, args, respond) {
var roomId = opts.room;
return main.listGhostUsers(roomId).then((user_ids) => {
respond("Draining " + user_ids.length + " ghosts from " + roomId);
return Promise.each(user_ids, (user_id) => {
return main._bridge.getIntent(user_id).leave(roomId);
});
}).then(() => {
return main.getBotIntent().leave(roomId);
}).then(() => {
respond("Drained");
});
},
});
adminCommands.stalerooms = new AdminCommand({
desc: "list rooms the bot user is a member of that are unlinked",
func: function(main, opts, args, respond) {
return main.listRoomsFor().then((room_ids) => {
room_ids.forEach((id) => {
if (id === main._config.matrix_admin_room) return;
if (main.getRoomByMatrixRoomId(id)) return;
respond(id);
});
});
},
});
module.exports = adminCommands;