]>
Commit | Line | Data |
---|---|---|
cf400e09 JR |
1 | # Erebus IRC bot - Author: Erebus Team |
2 | # vim: fileencoding=utf-8 | |
3 | # module for 's/regex/replacement/' style correction | |
4 | # warning: arbitrary regex's are generally capable of various DoS attacks on CPU/memory usage. use with caution. | |
5 | # see for usage examples: https://github.com/zonidjan/erebus/commit/d7e9802778477f1faa26a03078cb1b3c018a5e5c | |
6 | # This file is released into the public domain; see http://unlicense.org/ | |
7 | ||
8 | # module info | |
9 | modinfo = { | |
10 | 'author': 'Erebus Team', | |
11 | 'license': 'public domain', | |
12 | 'compatible': [0], # compatible module API versions | |
13 | 'depends': [], # other modules required to work properly? | |
14 | 'softdeps': [], # modules which are preferred but not required | |
15 | } | |
16 | ||
17 | # preamble | |
18 | import modlib | |
19 | lib = modlib.modlib(__name__) | |
20 | modstart = lib.modstart | |
21 | modstop = lib.modstop | |
22 | ||
23 | # module code | |
24 | import re | |
25 | from collections import namedtuple | |
26 | re_findtwitter = re.compile(r"""https?://(?:www\.)?(?:twitter|x)\.com/([-.a-z0-9/]+)""", re.I) | |
27 | ||
28 | @lib.hooknum("PRIVMSG") | |
29 | def privmsg_hook(bot, line): | |
30 | pieces = line.split(None, 3) | |
31 | chan = pieces[2] | |
32 | msg = pieces[3][1:] | |
33 | replaced = "" | |
34 | for mo in re_findtwitter.finditer(msg): | |
35 | replaced += re_findtwitter.sub(r"https://nitter.projectsegfau.lt/\1", mo.group(0)) + " " | |
36 | if replaced: | |
37 | bot.msg(chan, "%s" % (replaced)) |