Skip to content

Commit ff1dc47

Browse files
committed
Update link_announcer.py to priority/halting system; add regex_hook param 'only_no_match'
1 parent f507f0e commit ff1dc47

3 files changed

Lines changed: 15 additions & 7 deletions

File tree

cloudbot/bot.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,12 +304,17 @@ def add_hook(hook, _event, _run_before=False):
304304
def regex_priority(t):
305305
return t[1].priority
306306

307+
regex_matched = False
307308
for regex, regex_hook in sorted(self.plugin_manager.regex_hooks, key=regex_priority, reverse=True):
308309
if not regex_hook.run_on_cmd and cmd_match:
309310
continue
310311

312+
if regex_hook.only_no_match and regex_matched:
313+
continue
314+
311315
regex_match = regex.search(event.content)
312316
if regex_match:
317+
regex_matched = True
313318
regex_event = RegexEvent(hook=regex_hook, match=regex_match, base_event=event)
314319
if not add_hook(regex_hook, regex_event):
315320
# The hook has an action of Action.HALT* so stop adding new tasks

cloudbot/plugin.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,7 @@ def __init__(self, plugin, regex_hook):
653653
"""
654654
self.run_on_cmd = regex_hook.kwargs.pop("run_on_cmd", False)
655655
self.priority = regex_hook.kwargs.pop("priority", Priority.NORMAL)
656+
self.only_no_match = regex_hook.kwargs.pop("only_no_match", False)
656657

657658
self.regexes = regex_hook.regexes
658659

plugins/link_announcer.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,18 @@
44
from contextlib import closing
55
from cloudbot import hook
66

7-
# This will match any URL except the patterns defined in blacklist.
8-
blacklist = '.*(reddit\.com|redd\.it|youtube\.com|youtu\.be|imdb\.com|spotify\.com|twitter\.com|twitch\.tv|amazon\.co|xkcd\.com|amzn\.co|steamcommunity\.com|steampowered\.com|newegg\.com|soundcloud\.com|vimeo\.com).*'
9-
url_re = re.compile('(?!{})http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+~]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+'.format(blacklist), re.I)
7+
from cloudbot.hook import Priority, Action
8+
9+
# This will match any URL, blacklist removed and abstracted to a priority/halting system
10+
url_re = re.compile(r'https?://(?:[a-zA-Z]|[0-9]|[$-_@.&+~]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', re.I)
1011

1112
opt_out = []
1213

1314
traditional = [
1415
(1024 ** 5, 'PB'),
15-
(1024 ** 4, 'TB'),
16-
(1024 ** 3, 'GB'),
17-
(1024 ** 2, 'MB'),
16+
(1024 ** 4, 'TB'),
17+
(1024 ** 3, 'GB'),
18+
(1024 ** 2, 'MB'),
1819
(1024 ** 1, 'KB'),
1920
(1024 ** 0, 'B'),
2021
]
@@ -29,7 +30,8 @@ def bytesto(bytes, system = traditional):
2930
amount = int(bytes/factor)
3031
return str(amount) + suffix
3132

32-
@hook.regex(url_re)
33+
34+
@hook.regex(url_re, priority=Priority.LOW, action=Action.HALTTYPE, only_no_match=True)
3335
def print_url_title(message, match, chan):
3436
if chan in opt_out:
3537
return

0 commit comments

Comments
 (0)