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