]>
jfr.im git - erebus.git/blob - modules/subtext.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/
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
19 lib
= modlib
.modlib(__name__
)
20 modstart
= lib
.modstart
25 from collections
import namedtuple
26 re_findsub
= re
.compile(r
"""s([/'";:!@#$%^&-_=`~])(?P<search>.+?)\1(?P<replace>.+?)(?:\1(?P<global>g)?|$)""")
27 Line
= namedtuple('Line', ['sender', 'msg'])
29 @lib.hooknum("PRIVMSG")
30 def privmsg_hook(bot
, line
):
31 pieces
= line
.split(None, 3)
32 fromnick
= pieces
[0][1:].split('!')[0]
34 if chan
[0] != "#": return
37 mo
= re_findsub
.match(msg
)
39 if mo
.group('global') is not None:
42 count
= 1 # only first
44 newline
= re
.sub(mo
.group('search'), mo
.group('replace'), lastline
[chan
].msg
, count
)
45 except Exception as e
: return # ignore it if it doesn't work
46 if newline
!= lastline
[chan
].msg
:
47 if lastline
[chan
].sender
== fromnick
:
48 bot
.msg(chan
, "<%s> %s" % (lastline
[chan
].sender
, newline
))
50 bot
.msg(chan
, "%s: <%s> %s" % (fromnick
, lastline
[chan
].sender
, newline
))
52 lastline
[chan
] = Line(sender
=fromnick
, msg
=msg
)