]>
Commit | Line | Data |
---|---|---|
d7e98027 | 1 | # Erebus IRC bot - Author: Erebus Team |
6501c178 | 2 | # module for 's/regex/replacement/' style correction |
3 | # see for usage examples: https://github.com/zonidjan/erebus/commit/d7e9802778477f1faa26a03078cb1b3c018a5e5c | |
d7e98027 | 4 | # This file is released into the public domain; see http://unlicense.org/ |
5 | ||
6 | # module info | |
7 | modinfo = { | |
8 | 'author': 'Erebus Team', | |
9 | 'license': 'public domain', | |
fa93b933 | 10 | 'compatible': [0], # compatible module API versions |
d7e98027 | 11 | 'depends': [], # other modules required to work properly? |
6501c178 | 12 | 'softdeps': [], # modules which are preferred but not required |
d7e98027 | 13 | } |
d7e98027 | 14 | |
15 | # preamble | |
16 | import modlib | |
17 | lib = modlib.modlib(__name__) | |
18 | modstart = lib.modstart | |
19 | modstop = lib.modstop | |
20 | ||
21 | # module code | |
22 | import re | |
23 | from collections import namedtuple | |
24 | re_findsub = re.compile(r"s(.)(?P<search>[^\1]+)\1(?P<replace>[^\1]+)\1(?P<global>g)?;?") | |
25 | Line = namedtuple('Line', ['sender', 'msg']) | |
26 | lastline = {} | |
27 | @lib.hooknum("PRIVMSG") | |
28 | def privmsg_hook(bot, line): | |
29 | pieces = line.split(None, 3) | |
30 | fromnick = pieces[0][1:].split('!')[0] | |
31 | chan = pieces[2] | |
32 | msg = pieces[3][1:] | |
33 | mo = re_findsub.match(msg) | |
34 | if mo: | |
35 | print lastline[chan] | |
36 | print mo.groupdict() | |
37 | if mo.group('global') is not None: | |
38 | count = 0 # unlimited | |
39 | else: | |
40 | count = 1 # only first | |
41 | try: | |
42 | newline = re.sub(mo.group('search'), mo.group('replace'), lastline[chan].msg, count) | |
43 | except Exception as e: print e; return # ignore it if it doesn't work | |
44 | print newline | |
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) |