]> jfr.im git - erebus.git/blob - modules/nitterize.py
e6aa92aabe6f0124ff467db4486b8d0c75c34635
[erebus.git] / modules / nitterize.py
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 re_findtwitter = re.compile(r"""https?://(?:www\.)?(?:twitter|x)\.com/([-./_a-z0-9]+)""", re.I)
26
27 @lib.hooknum("PRIVMSG")
28 def privmsg_hook(bot, line):
29 pieces = line.split(None, 3)
30 chan = pieces[2]
31 if chan[0] != "#": return
32 msg = pieces[3][1:]
33
34 replaced = ""
35 for mo in re_findtwitter.finditer(msg):
36 replaced += re_findtwitter.sub(bot.parent.cfg.get('nitterize', 'instance', 'https://nitter.net') + r"/\1", mo.group(0)) + " "
37 if replaced:
38 bot.msg(chan, "%s" % (replaced))
39
40 @lib.hook(glevel=lib.STAFF, needchan=False)
41 @lib.argsEQ(1)
42 def setnitter(bot, user, chan, realtarget, *args):
43 bot.parent.cfg.set('nitterize', 'instance', args[0])