forked from CloudBotIRC/CloudBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
correction.py
51 lines (42 loc) · 1.67 KB
/
correction.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import re
from cloudbot import hook
from cloudbot.util.formatting import ireplace
correction_re = re.compile(r"^[sS]/(.*/.*(?:/[igx]{,4})?)\S*$")
@hook.regex(correction_re)
def correction(match, conn, chan, message):
"""
:type match: re.__Match
:type conn: cloudbot.client.Client
:type chan: str
"""
groups = [b.replace("\/", "/") for b in re.split(r"(?<!\\)/", match.groups()[0])]
find_regex = groups[0]
replace = groups[1]
if chan not in conn.history:
# Bot has not recorded any history for the channel yet.
# No need to evaluate or throw an exception.
return
for item in conn.history[chan].__reversed__():
nick, timestamp, msg = item
if correction_re.match(msg):
# don't correct corrections, it gets really confusing
continue
try:
if re.search(find_regex, msg) is not None:
# Remove if there
if "\x01ACTION" in msg:
msg = msg.replace("\x01ACTION", "").replace("\x01", "")
# Send bolded correction
mod_msg = re.sub(find_regex, "\x02" + replace + "\x02", msg)
message("Correction, <{}> {}".format(nick, mod_msg))
# Add correction to the history
msg = re.sub(find_regex, replace, msg)
conn.history[chan].append((nick, timestamp, msg))
return
else:
continue
except IndexError:
# There was an invalid backreference in the replace string.
# Treat as if no matches were found by returning.
# The replace will never happen, anyway.
return