-
Notifications
You must be signed in to change notification settings - Fork 1
/
CabalBot.py
644 lines (529 loc) · 21.5 KB
/
CabalBot.py
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
import sys
sys.path.insert(0, "/path/to/.sopel/plugins/cabalbot") # Help Sopel find the submodules we are about to load
import affeed
import autolink
import cabalutil
import confirmedfeed
import globalwatch
import gstools
import oresfeed
import pagewatch
import pushover
import rcfeed
import logreporter
import json
import random
import threading
import time
from sopel import plugin
from sseclient import SSEClient as EventSource
# Set some globals because repeating yourself is annoying
BOTADMINMSG = "This command is only available to the bot admins."
CHANCMDMSG = "This message must be used in a channel."
def setup(bot):
# Configure the RC feed listener, save to bot memory, and start it.
stop_event = threading.Event()
url = "https://stream.wikimedia.org/v2/stream/recentchange"
listen = threading.Thread(target=listener, args=(bot, url, stop_event))
bot.memory["wikistream_stop"] = stop_event
bot.memory["wikistream_listener"] = listen
#bot.memory["wikistream_listener"].start()
# Configure the ORES feed listener, save to bot memory, and start it.
ores_url = "https://stream.wikimedia.org/v2/stream/revision-score"
ores = threading.Thread(target=ores_listener, args=(bot, ores_url, stop_event))
bot.memory["ores_stop"] = stop_event
bot.memory["ores"] = ores
#bot.memory["ores"].start()
def listener(bot, url, stop_event):
# Listen to EventStream for Recent Changes
while not stop_event.is_set():
try:
for event in EventSource(url, headers="CabalBot v2.0 by Operator873 operator873@gmail.com"):
# Check for stop flag inside the loop
if stop_event.is_set():
return
if event.event == "message":
# Sometimes the EventStream sends garbage, discard if necessary
try:
change = json.loads(event.data)
dispatch(bot, change)
except ValueError:
continue
except StopIteration:
time.sleep(2)
continue
except Exception:
time.sleep(2)
continue
def ores_listener(bot, url, stop_event):
# Listen to EventStream for ORES events
while not stop_event.is_set():
try:
for event in EventSource(url, headers="CabalBot v2.0 by Operator873 operator873@gmail.com"):
# Check for stop flag inside the loop
if stop_event.is_set():
return
if event.event == "message":
# Sometimes the EventStream sends garbage, discard if necessary
try:
change = json.loads(event.data)
ores_dispatch(bot, change)
except ValueError:
continue
except StopIteration:
time.sleep(2)
continue
except Exception:
time.sleep(2)
continue
def dispatch(bot, change):
# Dispatch edits and page creations
if change["type"] == "edit" or change["type"] == "new":
# Check for specific pages in watch
if pagewatch.check(change):
pagewatch.new_report(bot, change)
# Check for pages watched globally
if globalwatch.check(change):
globalwatch.report(bot, change)
# If bot is Bot873, check for cssjs reporting
if (
pagewatch.checkcss(change)
and bot.nick == "Bot873"
):
pagewatch.cssjs(bot, change)
# If rc feed is being reported for a project, dispatch report
if rcfeed.check(change):
rcfeed.report(bot, change)
# If edits from un-confirmed accounts are being reported, dispatch report
if confirmedfeed.check(change):
confirmedfeed(bot, change)
# Dispatch Log events
if change["type"] == "log":
# Handles GS Log events
if (
gstools.check(change["wiki"])
and bot.nick == "Bot873"
):
gstools.reportbot, change)
if logreporter.check_for_log_reporter(change["wiki"]):
logreporter.log_report(bot, change)
# If abuse filter hits are being reported, dispatch report
if change["log_type"] == "abusefilter":
if affeed.check(change):
affeed.report(bot, change)
def ores_dispatch(bot, change):
# Dispatch ORES reports
if oresfeed.check(change):
oresfeed.report(bot, change)
@plugin.require_admin(message=BOTADMINMSG)
@plugin.require_chanmsg(CHANCMDMSG)
@plugin.command("feedadmin")
def feedcmd(bot, trigger): # !feedadmin {add/del/list} <target>
cabalutil.feedadmin(bot, trigger)
@plugin.command("speak")
@plugin.command("unmute")
def set_speak(bot, trigger): # Removes the channel from hushchannels and allows the bot to speak
cabalutil.watcherSpeak(bot, trigger)
@plugin.command("hush")
@plugin.command("mute")
def do_hush(bot, trigger): # Adds the channel to hushchannels where the bot will not speak
cabalutil.watcherHush(bot, trigger)
@plugin.require_admin(message=BOTADMINMSG)
@plugin.command("watchstart")
def start_listener(bot, trigger): # Manually restart listeners
if "wikistream_listener" not in bot.memory:
stop_event = threading.Event()
bot.memory["wikistream_stop"] = stop_event
url = "https://stream.wikimedia.org/v2/stream/recentchange"
listen = threading.Thread(
target=listener, args=(bot, url, bot.memory["wikistream_stop"])
)
bot.memory["wikistream_listener"] = listen
if "ores" not in bot.memory:
stop_event = threading.Event()
bot.memory["ores_stop"] = stop_event
ores_url = "https://stream.wikimedia.org/v2/stream/revision-score"
listen = threading.Thread(
target=ores_listener, args=(bot, ores_url, bot.memory["ores_stop"])
)
bot.memory["ores"] = listen
bot.memory["wikistream_listener"].start()
bot.memory["ores"].start()
bot.say("Listening to EventStreams...")
@plugin.interval(120)
def checkListener(bot): # Verify listeners are still listening every 2 mins and restart if needed
if bot.memory["wikistream_listener"].is_alive() is not True:
del bot.memory["wikistream_listener"]
del bot.memory["wikistream_stop"]
stop_event = threading.Event()
bot.memory["wikistream_stop"] = stop_event
url = "https://stream.wikimedia.org/v2/stream/recentchange"
listen = threading.Thread(
target=listener, args=(bot, url, bot.memory["wikistream_stop"])
)
bot.memory["wikistream_listener"] = listen
bot.memory["wikistream_listener"].start()
if bot.memory["ores"].is_alive() is not True:
del bot.memory["ores"]
del bot.memory["ores_stop"]
stop_event = threading.Event()
bot.memory["ores_stop"] = stop_event
ores_url = "https://stream.wikimedia.org/v2/stream/revision-score"
listen = threading.Thread(
target=ores_listener, args=(bot, ores_url, bot.memory["ores_stop"])
)
bot.memory["ores"] = listen
bot.memory["ores"] = listen
bot.memory["ores"].start()
@plugin.require_admin(message=BOTADMINMSG)
@plugin.command("watchstatus")
def watchStatus(bot, trigger): # Have the bot report the status of the listeners
if (
"wikistream_listener" in bot.memory
and bot.memory["wikistream_listener"].is_alive() is True
):
bot.say("RC stream listener is alive.")
else:
bot.say("RC stream listener is dead.")
if (
"ores" in bot.memory
and bot.memory["ores"].is_alive() is True
):
bot.say("ORES listener is alive.")
else:
bot.say("ORES listener is dead.")
@plugin.require_admin(message=BOTADMINMSG)
@plugin.command("watchstop")
def watchStop(bot, trigger): # Force the listeners to halt and deletes the thread from memory
if "wikistream_listener" not in bot.memory:
bot.say("RC stream listener isn't running.")
else:
try:
bot.memory["wikistream_stop"].set()
del bot.memory["wikistream_listener"]
bot.say("RC stream listener stopped.")
except Exception as e:
bot.say(str(e))
if "ores" not in bot.memory:
bot.say("ORES listener isn't running.")
else:
try:
bot.memory["ores_stop"].set()
del bot.memory["ores"]
bot.say("ORES listener stopped.")
except Exception as e:
bot.say(str(e))
@plugin.require_chanmsg(CHANCMDMSG)
@plugin.command("watch")
def watch(bot, trigger): # add a new specific page to watch on a specific project
watchAction = trigger.group(3)
if watchAction == "add" or watchAction == "Add" or watchAction == "+":
if trigger.group(5) == "":
bot.say("Command seems malformed. Syntax: !watch add proj page")
else:
bot.say(
pagewatch.watcherAdd(trigger.group(2), trigger.account, trigger.sender)
)
elif watchAction == "del" or watchAction == "Del" or watchAction == "-":
if trigger.group(5) == "":
bot.say("Command seems malformed. Syntax: !watch del proj page")
else:
bot.say(
pagewatch.watcherDel(trigger.group(2), trigger.account, trigger.sender)
)
elif watchAction == "ping" or watchAction == "Ping":
if trigger.group(6) == "":
bot.say("Command seems malformed. Syntax: !watch ping <on/off> proj page")
else:
bot.say(
pagewatch.watcherPing(trigger.group(2), trigger.account, trigger.sender)
)
else:
bot.say("I don't recognzie that command. Options are: Add & Del")
# !globalwatch ping on namespaceid title
# !globalwatch add namespaceid title
@plugin.require_chanmsg(CHANCMDMSG)
@plugin.command("globalwatch")
def gwatch(bot, trigger): # Globally watch a page in a certain namespace
watchAction = trigger.group(3)
if watchAction == "add" or watchAction == "Add" or watchAction == "+":
if trigger.group(5) == "" or trigger.group(5) is None:
bot.say(
"Command seems malformed. Syntax: !globalwatch add namespaceID page"
)
else:
bot.say(
globalwatch.addpage(trigger.group(2), trigger.account, trigger.sender)
)
elif watchAction == "del" or watchAction == "Del" or watchAction == "-":
if trigger.group(5) == "" or trigger.group(5) is None:
bot.say(
"Command seems malformed. Syntax: !globalwatch del namespaceID page"
)
else:
bot.say(
globalwatch.delpage(trigger.group(2), trigger.account, trigger.sender)
)
elif watchAction == "ping" or watchAction == "Ping":
if trigger.group(6) == "" or trigger.group(6) is None:
bot.say(
"Command seems malformed. Syntax: !globalwatch ping <on/off> namespaceID page"
)
else:
bot.say(globalwatch.ping(trigger.group(2), trigger.account, trigger.sender))
else:
bot.say("I don't recognize that command. Options are: add, del, & ping")
@plugin.require_chanmsg(CHANCMDMSG)
@plugin.command("namespace")
def get_namespace(bot, trigger): # Get namespace information by either number or name
bot.say(cabalutil.namespaces(trigger))
@plugin.require_chanmsg(CHANCMDMSG)
@plugin.command("abusefeed", "affeed")
def do_affeed(bot, trigger): # !abusefeed {start/stop} <project> || Controls the Abuse Filter feed for this channel
try:
action, project = trigger.group(2).split(' ', 1)
except ValueError:
bot.say("Missing project! Syntax: !abusefeed {start/stop} <project>")
return
if action.lower() == "start":
bot.say(affeed.start(trigger))
elif action.lower() == "stop":
bot.say(affeed.stop(trigger))
else:
bot.say("I'm not sure how to " + action + ". Try 'start' and 'stop'.")
@plugin.require_chanmsg(CHANCMDMSG)
@plugin.command("rcfeed")
def do_rcfeed(bot, trigger): # !rcfeed {start/stop} <project> || Controls the rc feed for this channel
try:
action, project = trigger.group(2).split(' ', 1)
except ValueError:
bot.say("Missing project! Syntax: !rcfeed {start/stop} <project>")
return
if action.lower() == "start":
bot.say(rcfeed.start(trigger))
elif action.lower() == "stop":
bot.say(rcfeed.stop(trigger))
else:
bot.say("I'm not sure how to " + action + ". Try 'start' and 'stop'.")
@plugin.require_chanmsg(CHANCMDMSG)
@plugin.command("confirmedfeed")
def do_confirmedfeed(bot, trigger):
# !confirmedfeed {start/stop} <project> || Controls the Confirmed feed for this channel
try:
action, project = trigger.group(2).split(' ', 1)
except ValueError:
bot.say("Missing project! Syntax: !confirmedfeed {start/stop} <project>")
return
if action.lower() == "start":
bot.say(confirmedfeed.start(trigger))
elif action.lower() == "stop":
bot.say(confirmedfeed.stop(trigger))
else:
bot.say("I'm not sure how to " + action + ". Try 'start' and 'stop'.")
@plugin.require_chanmsg(CHANCMDMSG)
@plugin.command("oresfeed", "vandalfeed", "vandalismfeed")
def do_oresfeed(bot, trigger): # # !oresfeed {start/stop} <project> || Controls the ORES feed for this channel
try:
action, project = trigger.group(2).split(' ', 1)
except ValueError:
bot.say("Missing project! Syntax: !oresfeed {start/stop} <project>")
return
if action.lower() == "start":
bot.say(oresfeed.start(trigger))
elif action.lower() == "stop":
bot.say(oresfeed.stop(trigger))
else:
bot.say("I'm not sure how to " + action + ". Try 'start' and 'stop'.")
#################################
# Log Reporter Commands #
#################################
@plugin.command("logreporter")
def handle_log_reporter(bot, trigger):
if trigger.group(3).lower() in ['del', 'delete', 'rm', 'remove', '-', 'add', '+']:
bot.say(logreporter.log_reporter_action(trigger, trigger.group(3)))
elif trigger.group(3).lower() == "start":
bot.say(logreporter.start_log_reporter(trigger))
elif trigger.group(3).lower() == "stop":
bot.say(logreporter.stop_log_reporter(trigger))
else:
bot.say("Command seems malformed. Syntax is: !logreporter <start/stop/add/del> <project> <args>")
@plugin.find(r"\[\[(.*?)\]\]")
def autolinker(bot, trigger): # Autolinker for standard [[wikilinks]]
if not autolink.checklang(trigger.sender) or cabalutil.ignored_nick(
trigger.account
):
return
else:
url = autolink.getlang(trigger.sender)
link = trigger.groups()[0].replace(" ", "_")
if url is None:
bot.say("I found the channel in the database, but there was no URL saved.")
else:
bot.say(url + link)
@plugin.find(r"\{\{(.*?)\}\}")
def autolinker_templates(bot, trigger): # Autolinker for {{Template:}} links
if not autolink.checklang(trigger.sender) or cabalutil.ignored_nick(
trigger.account
):
return
else:
url = autolink.getlang(trigger.sender)
link = trigger.groups()[0].replace(" ", "_")
if url is None:
bot.say("I found the channel in the database, but there was no URL saved.")
else:
bot.say(url + "Template:" + link)
@plugin.require_admin(message=BOTADMINMSG)
@plugin.require_chanmsg(CHANCMDMSG)
@plugin.command("setlang")
def setlang(bot, trigger): # Set the preferred language for a channel for Autolinks
# !setlang enwiki https://enwp.org/
if not trigger.group(4):
bot.say("Missing URL! Syntax is !setlang <project> <baseURL>")
return
if autolink.addlang(trigger.sender, trigger.group(3), trigger.group(4)):
bot.say(trigger.sender + " will use " + trigger.group(4))
@plugin.require_admin(message=BOTADMINMSG)
@plugin.require_chanmsg(CHANCMDMSG)
@plugin.command("unsetlang")
def unsetlang(bot, trigger): # Clear the set language for auto links (disables autolink)
if autolink.rmvlang(trigger.sender):
bot.say(trigger.sender + " was cleared.")
@plugin.require_admin(message=BOTADMINMSG)
@plugin.require_chanmsg(CHANCMDMSG)
@plugin.command("ignorenick")
def setignorenick(bot, trigger): # Tell the bot to NOT do autolinks for the provided IRC account name
# !ignorenick <ircAccountName>
if autolink.ignorenick(trigger.group(3), trigger.account):
bot.say("I'll ignore links from " + trigger.group(3) + " from now on.")
@plugin.require_admin(message=BOTADMINMSG)
@plugin.require_chanmsg(CHANCMDMSG)
@plugin.command("unignorenick")
def setunignorenick(bot, trigger): # Tell the bot to allow the IRC account to be autolinked again
# !unignorenick <ircAccountName>
if autolink.unignorenick(trigger.group(3)):
bot.say("I'll stop ignoring links from " + trigger.group(3) + ".")
@plugin.require_owner(message="This command is only available to the bot owner.")
@plugin.command("restartbot", "restart")
def do_bot_restart(bot, trigger): # Order the bot to restart with any provided message
if trigger.group(2):
bot.restart("Restarting by order of " + trigger.account + " Reason: " + trigger.group(2))
else:
bot.restart("Restarting by order of " + trigger.account)
@plugin.require_owner(message="This command is only available to the bot owner.")
@plugin.command("botdie", "quit")
def do_botdie(bot, trigger): # Order the bot to quit with the provided message
if trigger.group(2):
bot.quit("Bot ordered to die by " + trigger.account + " Reason: " + trigger.group(2))
else:
bot.quit("Bot ordered to die by " + trigger.account)
@plugin.command("commands", "help", "doc")
def get_help(bot, trigger):
bot.say(trigger.nick + ": My documentation can be found at https://github.com/Operator873/CabalBot")
##########################################
# GlobalSysBot Commands follow #
##########################################
@plugin.command("onirc")
def do_onirc(bot, trigger):
data = gstools.on_irc(trigger.group(3))
if data["ok"]:
for item in data["data"]:
bot.say(item)
if data["more"]:
bot.say(
"There are more pages listed. Re-run !onirc "
+ trigger.group(3)
+ " after deleting the above pages."
)
else:
bot.say(data["msg"])
@plugin.require_admin(message=BOTADMINMSG)
@plugin.command("addmember")
def do_addmember(bot, trigger):
bot.say(gstools.addGS(trigger))
@plugin.require_admin(message=BOTADMINMSG)
@plugin.command("removemember")
def do_delmember(bot, trigger):
bot.say(gstools.delGS(trigger))
@plugin.require_chanmsg(CHANCMDMSG)
@plugin.command("addwiki")
def add_wiki(bot, trigger):
if trigger.group(5) == "":
bot.say("Seems to be missing something. Syntax is !addwiki <project> <apiurl> <categoryname>")
else:
project, api, csd = trigger.group(2).split(' ', 2)
bot.say(gstools.add_wiki(project, api, csd))
@plugin.require_admin(message=BOTADMINMSG)
@plugin.require_chanmsg(CHANCMDMSG)
@plugin.command("delwiki")
def del_wiki(bot, trigger):
bot.say(gstools.del_wiki(trigger.group(3)))
@plugin.require_chanmsg(CHANCMDMSG)
@plugin.command('randomwiki')
def randomwiki(bot, trigger):
query = "SELECT project FROM GSwikis;"
wikis = cabalutil.do_sqlite(query, "all")
random_index = random.randint(0, len(wikis))
wiki = wikis[random_index][0]
bot.say(f"Random wiki: {wiki}")
# @plugin.require_chanmsg(CHANCMDMSG)
# @plugin.require_owner(message=BOTADMINMSG)
# @plugin.command('sync-gs-wikis')
# def sync_wikis(bot, trigger):
# list_of_wikis = []
# meta_wiki = "meta.wikimedia.org"
# get_wikis = {
# "action": "query",
# "format": "json",
# "list": "wikisets",
# "wsfrom": "Opted-out of global sysop wikis",
# "wsprop": "wikisnotincluded"
# }
##########################################
# Commands in development #
##########################################
@plugin.require_admin()
@plugin.require_chanmsg("This command must be used in a channel.")
@plugin.command("admin@simplewiki", "admin")
def regular_ping(bot, trigger):
if (
trigger.sender == "#wikipedia-simple"
or trigger.sender == "#wikipedia-simple-admins"
):
notification = (
trigger.nick
+ " in "
+ trigger.sender
+ " pinged admins with message: "
+ trigger.group(2)
)
msg = {
'data': notification,
'priority': -2
}
if pushover.send_alert(msg, bot.nick):
bot.say("Sending pushover alerts to opted-in admins as well.")
else:
bot.say("Something broke when I was attempting pushover notifications.")
@plugin.require_chanmsg("This command must be used in a channel.")
@plugin.command("testpushover")
def test_ping(bot, trigger):
if (
trigger.sender == "#wikipedia-simple"
or trigger.sender == "#wikipedia-simple-admins"
):
notification = (
trigger.nick
+ " in "
+ trigger.sender
+ " triggered a test notification with message: "
+ trigger.group(2)
)
msg = {
'data': notification,
'priority': -2
}
if pushover.send_alert(msg, bot.nick):
bot.say("Sending pushover test alert.")
else:
bot.say("Something broke when I was attempting pushover notifications.")