-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
565 lines (540 loc) · 19.2 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
#!/usr/bin/env node
// Handler
var controller = require('./controller.js');
var executor = require('./executor.js');
var config = require('./config.js');
var banlist = require('./banlist.js');
var sqlite3 = require('sqlite3').verbose();
var db = new sqlite3.Database(config.db_file);
var connected = false;
// Main TODO: prevent user from ban bot itself - done
function processManagedCommand(ret) {
switch (ret.require_permission) {
case "admin":
// Check Admin
// We assume that Admin ID is in Config File
if (ret.user.id == config.admin_id) {
/*
* promote
* demote
* lock title
* lock photo
* banall
*/
switch (ret.type) {
case "promote":
// Check then update record
Promote(ret);
break;
case "demote":
// Check then update record
Demote(ret);
break;
case "set-lock-title-on":
titleLocker(true, ret);
controller.msg({
text: "Title Locker On!",
chat_id: -(ret.chatfrom)
});
break;
case "set-lock-title-off":
titleLocker(false, ret);
controller.msg({
text: "Title Locker Off!",
chat_id: -(ret.chatfrom)
});
break;
case "set-lock-photo-on":
photoLocker(true, ret);
controller.msg({
text: "Photo Locker On! Remember to read photo_lock_readme.txt before you use!",
chat_id: -(ret.chatfrom)
});
break;
case "set-lock-photo-off":
photoLocker(false, ret);
controller.msg({
text: "Photo Locker Off!",
chat_id: -(ret.chatfrom)
});
break;
case "banall":
Ban(ret);
break;
case "unbanall":
unBan(ret);
break;
}
}
break;
case "moderator":
db.get("SELECT * FROM moderator_list WHERE modid = $uid AND gid = $gid", {
$uid: ret.user.id,
$gid: ret.chatfrom
}, function (err, row) {
if (err) {
console.error(err);
} else if (row || ret.user.id == config.admin_id) {
// Check Passed.
switch (ret.type) {
case "kick":
// Check then update record
Kick(ret);
break;
case "ban":
// Check then update record
Ban(ret);
break;
case "unban":
unBan(ret);
break;
}
}
});
break;
case "anyone":
// kickme and help
switch (ret.type) {
case "kick":
// kickme :p
if (ret.target == ret.user.id) Kick(ret);
break;
case "help":
outputHelp(ret);
break;
case "modlist":
listModerators(ret);
break;
case "listban":
case "listbanall":
listBannings(ret);
break;
}
break;
default:
// ERROR
}
}
function outputHelp(ret) {
// Any Help msg?
var helpmsg = "PeaceManager-Lite 2nd Release\n";
helpmsg += "written by @wfjsw , Bot to manage group chats\n\n";
helpmsg += "User can use the following command:\n";
helpmsg += " /kickme - Remove me so that I can join again later (if you \"leave\", you may not be able to re-enter) \n";
helpmsg += "Moderators commands: \n";
helpmsg += " /kick (ID) - Kick by reply or by numeric ID \n";
helpmsg += " /ban (ID) - Ban by reply or by numeric ID \n";
helpmsg += " /unban (ID) - unBan by reply or by numeric ID \n";
helpmsg += "Owner commands:\n";
helpmsg += " /promote - Promote a user to Moderator by reply \n";
helpmsg += " /demote - Demote a user From Moderator by reply \n";
helpmsg += " /banall - Ban by reply or by numeric ID across ALL YOUR MANAGED GROUPS \n";
helpmsg += " /unbanall - unBan by reply or by numeric ID across ALL YOUR MANAGED GROUPS \n";
helpmsg += " /set lock title on/off - Lock or Unlock the Title of this group \n\n";
helpmsg += "PeaceManager-Lite version 2, Copyright(C) 2015 of wfjsw \n";
helpmsg += "PeaceManager-Lite comes with ABSOLUTELY NO WARRANTY; This is free software, and you are welcome to redistribute it under certain conditions; Read GNU General Public License 2.0 for details.";
controller.msg({
text: helpmsg,
chat_id: -(ret.chatfrom)
});
}
function requestPing(ret) {
executor.msg(ret.chatfrom, "Pong!");
}
function listModerators(ret) {
db.all("SELECT * FROM moderator_list WHERE modid = $uid AND gid = $gid", {
$uid: ret.target.id,
$gid: ret.chatfrom
}, function (err, rows) {
if (err) {
console.error(err);
} else if (rows == []) {
controller.msg({
text: "No Moderator Found",
chat_id: -(ret.chatfrom)
});
} else {
var output = "Moderator List for " + ret.chatfrom + "\n";
for (var i in rows) {
output += rows[i].modid + "\n";
}
controller.msg({
text: output,
chat_id: -(ret.chatfrom)
});
}
});
}
function listBannings(ret) {
// ignore too long
controller.msg({
text: "Not Implemented",
chat_id: -(ret.chatfrom)
});
}
function Promote(ret) {
// Check
db.get("SELECT * FROM moderator_list WHERE modid = $uid AND gid = $gid", {
$uid: ret.target.id,
$gid: ret.chatfrom
}, function (err, row) {
if (err) {
console.error(err);
controller.msg({
text: "Error occurred.",
chat_id: -(ret.chatfrom)
});
} else if (row === undefined) {
// Check Passed.
db.run("INSERT INTO moderator_list (modid, gid) VALUES ($uid, $gid)", {
$uid: ret.target.id,
$gid: ret.chatfrom
});
controller.msg({
text: "User @" + ret.target.username + " Promoted as an Moderator.",
chat_id: -(ret.chatfrom)
});
} // TODO: reply back
else {
controller.msg({
text: "User Exist.",
chat_id: -(ret.chatfrom)
});
}
});
}
function Demote(ret) {
// Check
db.get("SELECT * FROM moderator_list WHERE modid = $uid AND gid = $gid", {
$uid: ret.target.id,
$gid: ret.chatfrom
}, function (err, row) {
if (err) {
console.error(err);
} else if (row) {
// Check Passed.
db.run("DELETE FROM moderator_list WHERE modid = $uid AND gid = $gid", {
$uid: ret.target.id,
$gid: ret.chatfrom
});
controller.msg({
text: "User @" + ret.target.username + " Demoted.",
chat_id: -(ret.chatfrom)
});
} // TODO: reply back
});
}
function titleLocker(stat, ret){
var lock = stat ? 1 : 0;
db.run("UPDATE managed_group SET is_title_locked = $lock WHERE id = $gid", {
$gid: ret.chatfrom,
$lock: lock
});
}
function photoLocker(stat, ret) {
var lock = stat ? 1 : 0;
db.run("UPDATE managed_group SET is_photo_locked = $lock WHERE id = $gid", {
$gid: ret.chatfrom,
$lock: lock
});
}
function photoLockEnforce(ret) {
console.log("check lock" + ret.group);
if (ret.from.id != config.admin_id) {
db.get("SELECT * FROM managed_group WHERE id = $gid", {
$gid: ret.group
}, function (err, row) {
if (err) {
console.error(err);
// Send Error Msg
} else if (row.is_photo_locked == 1) {
executor.group_setphoto(ret.group, config.group_photo_dir + ret.group + ".jpg");
} else {
// Output something
controller.msg({
text: "#GroupPhotoChanged by @" + ret.from.username + " ( " + ret.from.id + " ) ",
chat_id: -(ret.group)
});
}
});
}
}
function Ban(ret) {
// Check
if (ret.target != config.admin_id && ret.target != config.bot_id)
db.get("SELECT * FROM moderator_list WHERE modid = $uid AND gid = $gid", {
$uid: ret.user.id,
$gid: ret.chatfrom
}, function (err, row) {
if (err) {
console.error(err);
} else if (row === undefined) {
if (ret.type == "ban") {
db.get("SELECT * FROM banned_list WHERE userid = $uid AND gid = $gid", {
$uid: ret.target,
$gid: ret.chatfrom
}, function (err, row) {
if (err) {
console.error(err);
} else if (row === undefined) {
// Check Passed.
db.run("INSERT INTO banned_list (userid, banbyid, gid) VALUES ($uid, $eid, $gid)", {
$uid: ret.target,
$eid: ret.user.id,
$gid: ret.chatfrom
});
Kick(ret);
} else {
controller.msg({
text: "User Already Banned!",
chat_id: -(ret.chatfrom)
});
}
});
} else if (ret.type == "banall") {
db.get("SELECT * FROM banall_list WHERE userid = $uid", {
$uid: ret.target
}, function (err, row) {
if (err) {
console.error(err);
} else if (row === undefined) {
// Check Passed.
db.run("INSERT INTO banall_list (userid) VALUES ($uid)", {
$uid: ret.target
});
Kick(ret); // KICK ALL?
} else {
controller.msg({
text: "User Already Banned!",
chat_id: -(ret.chatfrom)
});
}
});
}
}
});
}
function unBan(ret) {
// Check
if (ret.type == "unban") {
db.get("SELECT * FROM banned_list WHERE userid = $uid AND gid = $gid", {
$uid: ret.target,
$gid: ret.chatfrom
}, function (err, row) {
if (err) {
console.error(err);
} else if (row) {
// Check Passed.
db.run("DELETE FROM banned_list WHERE userid = $uid AND gid = $gid", {
$uid: ret.target,
$gid: ret.chatfrom
});
} else {
controller.msg({
text: "User is Not Banned!",
chat_id: -(ret.chatfrom)
});
}// Else: User Is Not Banned - done
});
} else if (ret.type == "unbanall") {
db.get("SELECT * FROM banall_list WHERE userid = $uid", {
$uid: ret.target
}, function (err, row) {
if (err) {
console.error(err);
} else if (row) {
// Check Passed.
db.run("DELETE FROM banall_list WHERE userid = $uid", {
$uid: ret.target
});
} else {
controller.msg({
text: "User is Not Banned across all managed Groups!",
chat_id: -(ret.chatfrom)
});
} // Else: User is Not Banned across All - done
});
}
}
function Kick(ret) {
if (ret.target != config.admin_id && ret.target != config.bot_id)
if (ret.type == "banall") {
db.each("SELECT * FROM managed_group", {}, function (err, row) {
if (err) {
console.error(err);
} else if (row) {
executor.kickuser(row.id, ret.target);
}
});
} else {
db.get("SELECT * FROM moderator_list WHERE modid = $uid AND gid = $gid", {
$uid: ret.user.id,
$gid: ret.chatfrom
}, function (err, row) {
if (err) {
console.error(err);
} else if (row === undefined) {
executor.kickuser(ret.chatfrom, ret.target);
}
});
}
}
controller.event.on('cmd_request', function (ret) {
if (config.dry_run) return;
switch (ret.area) {
case "any":
switch (ret.require_permission) {
case "anyone":
/* Command here:
* /help ---> divide into different situation
* /ping ---> pass the ping to Executor - done
*/
switch (ret.type) {
case "help":
outputHelp(ret);
break;
case "ping":
requestPing(ret);
break;
}
break;
default:
// Print Error and then
return;
}
break;
case "managed":
// Some Check Here
db.get("SELECT * FROM managed_group WHERE id = $gid", {
$gid: ret.chatfrom
}, function (err, row) {
if (err) {
console.error(err);
} else if (row) {
// Check Passed.
processManagedCommand(ret);
}
});
break;
}
});
controller.event.on('delete_chat_photo', function (ret) {
if (config.dry_run) return;
// Output id
photoLockEnforce(ret);
});
controller.event.on('new_chat_title', function (ret) {
if (config.dry_run) return;
// Check Lock - done
console.log("check lock" + ret.group);
db.get("SELECT * FROM managed_group WHERE id = $gid", {
$gid: ret.group
}, function (err, row) {
if (err) {
console.error(err);
// Send Error Msg
} else if (row.is_title_locked == 1 && ret.from.id != config.admin_id) {
executor.group_setname(ret.group, row.title);
} else {
// Output something
controller.msg({
text: "#GroupTitleChanged by @" + ret.from.username + " ( " + ret.from.id + " ) ",
chat_id: -(ret.group)
});
// Update Database
db.run("UPDATE managed_group SET title = $title WHERE id = $gid", {
$gid: ret.group,
$title: ret.title
});
}
});
});
controller.event.on('new_chat_photo', function (ret) {
if (config.dry_run) return;
// Output id
photoLockEnforce(ret);
});
controller.event.on('new_chat_participant', function (ret) {
if (config.dry_run) return;
// Check Ban DB
// First, check Hard-coded global ban db (Only Two User) :p ---> wfjsw/PeaceManager#1 - done
if (ret.user.id == 68256164 || ret.user.id == 53835259 || (banlist.enabled && banlist.banlist.indexOf(ret.user.id) > -1)) {
executor.kickuser(ret.group, ret.user.id);
return;
}
// Then, Check Ban All DataBase (Can't work around with the callback paramid :( ) - done
db.get("SELECT id FROM banall_list WHERE userid = $uid", {
$uid: ret.user.id
}, function (err, row) {
if (err) {
console.error(err);
// Send Error Msg
} else if (row) {
executor.kickuser(ret.group, ret.user.id);
}
});
// Check Ban Individal Database - done
db.get("SELECT id FROM banned_list WHERE userid = $uid AND gid = $gid", {
$uid: ret.user.id,
$gid: ret.group
}, function (err, row) {
if (err) {
console.error(err);
// Send Error Msg
} else if (row) {
executor.kickuser(ret.group, ret.user.id);
}
});
// Output User Details - done
controller.msg({
text: "#UserJoin @" + ret.user.username + " ( " + ret.user.id + " ) ",
chat_id: -(ret.group)
});
// Extra Bonus: Update Title And Database - done
db.get("SELECT * FROM managed_group WHERE id = $gid", {
$gid: ret.group
}, function (err, row) {
if (err) {
console.error(err);
} else if (row === undefined) {
// Not Exist
db.run("INSERT INTO managed_group (id, title, is_title_locked, is_photo_locked) VALUES ($id, $title, 0, 0)", {
$id: ret.group,
$title: ret.title
});
controller.msg({
text: "PeaceManager registered in group " + ret.group,
chat_id: -(ret.group)
});
controller.msg({
text: "If you want it unregistered, just remove this bot. You will lost anything except banlists.",
chat_id: -(ret.group)
});
} else if (row) {
db.run("UPDATE managed_group SET title = $title WHERE id = $gid", {
$gid: ret.group,
$title: ret.title
});
}
});
});
controller.event.on('left_chat_participant', function (ret) {
if (config.dry_run) return;
if (ret.user.id == config.bot_id) {
db.run("DELETE FROM managed_group WHERE id = $gid", {
$gid: ret.group
});
db.run("DELETE FROM moderator_list WHERE gid = $gid", {
$gid: ret.group
});
db.run("DELETE FROM banned_list WHERE gid = $gid", {
$gid: ret.group
});
}
});
// First Init
executor.init(config) // Missing config
.then(function (status) {
controller.init(config); // Missing config, either
connected = true;
});