]>
Commit | Line | Data |
---|---|---|
d7e98027 | 1 | # Erebus IRC bot - Author: Erebus Team |
4477123d | 2 | # vim: fileencoding=utf-8 |
6501c178 | 3 | # module for 's/regex/replacement/' style correction |
7cafe22c | 4 | # warning: arbitrary regex's are generally capable of various DoS attacks on CPU/memory usage. use with caution. |
d7e98027 | 5 | # This file is released into the public domain; see http://unlicense.org/ |
6 | ||
7 | # module info | |
8 | modinfo = { | |
9 | 'author': 'Erebus Team', | |
10 | 'license': 'public domain', | |
fa93b933 | 11 | 'compatible': [0], # compatible module API versions |
d7e98027 | 12 | 'depends': [], # other modules required to work properly? |
6501c178 | 13 | 'softdeps': [], # modules which are preferred but not required |
d7e98027 | 14 | } |
d7e98027 | 15 | |
16 | # preamble | |
17 | import modlib | |
18 | lib = modlib.modlib(__name__) | |
19 | modstart = lib.modstart | |
20 | modstop = lib.modstop | |
21 | ||
22 | # module code | |
23 | import re | |
24 | from collections import namedtuple | |
d524dcc8 | 25 | re_findsub = re.compile(r"""s([/'";:!@#$%^&-_=`~])(?P<search>.+?)\1(?P<replace>.*?)(?:\1(?P<global>g)?|$)""") |
d7e98027 | 26 | Line = namedtuple('Line', ['sender', 'msg']) |
27 | lastline = {} | |
28 | @lib.hooknum("PRIVMSG") | |
29 | def privmsg_hook(bot, line): | |
30 | pieces = line.split(None, 3) | |
31 | fromnick = pieces[0][1:].split('!')[0] | |
32 | chan = pieces[2] | |
366accf8 | 33 | if chan[0] != "#": return |
d7e98027 | 34 | msg = pieces[3][1:] |
366accf8 | 35 | |
d7e98027 | 36 | mo = re_findsub.match(msg) |
37 | if mo: | |
d7e98027 | 38 | if mo.group('global') is not None: |
39 | count = 0 # unlimited | |
40 | else: | |
41 | count = 1 # only first | |
42 | try: | |
43 | newline = re.sub(mo.group('search'), mo.group('replace'), lastline[chan].msg, count) | |
a28e2ae9 | 44 | except Exception as e: return # ignore it if it doesn't work |
d7e98027 | 45 | if newline != lastline[chan].msg: |
46 | if lastline[chan].sender == fromnick: | |
47 | bot.msg(chan, "<%s> %s" % (lastline[chan].sender, newline)) | |
48 | else: | |
49 | bot.msg(chan, "%s: <%s> %s" % (fromnick, lastline[chan].sender, newline)) | |
50 | else: | |
51 | lastline[chan] = Line(sender=fromnick, msg=msg) |