Skip to content

Commit 8538c05

Browse files
committed
Update correction re to be more precise and handle escapes more sanely
1 parent b0a0292 commit 8538c05

1 file changed

Lines changed: 11 additions & 9 deletions

File tree

plugins/correction.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import re
22

33
from cloudbot import hook
4-
54
from cloudbot.util.formatting import ireplace
65

7-
correction_re = re.compile(r"^[sS]/(.*/.*(?:/[igx]{,4})?)\S*$")
6+
correction_re = re.compile(r"^[sS]/(?:(.*?)(?<!\\)/(.*?)(?:(?<!\\)/([igx]{,4}))?)\s*$")
7+
unescape_re = re.compile(r'\\(.)')
88

99

1010
@hook.regex(correction_re)
@@ -14,13 +14,13 @@ def correction(match, conn, nick, chan, message):
1414
:type conn: cloudbot.client.Client
1515
:type chan: str
1616
"""
17-
groups = [b.replace("\/", "/") for b in re.split(r"(?<!\\)/", match.groups()[0])]
17+
groups = [unescape_re.sub(r"\1", group or "") for group in match.groups()]
1818
find = groups[0]
1919
replace = groups[1]
2020
if find == replace:
2121
return "really dude? you want me to replace {} with {}?".format(find, replace)
22-
23-
if not find.strip(): # Replacing empty or entirely whitespace strings is spammy
22+
23+
if not find.strip(): # Replacing empty or entirely whitespace strings is spammy
2424
return "really dude? you want me to replace nothing with {}?".format(replace)
2525

2626
for item in conn.history[chan].__reversed__():
@@ -30,18 +30,20 @@ def correction(match, conn, nick, chan, message):
3030
continue
3131

3232
if find.lower() in msg.lower():
33+
find_esc = re.escape(find)
34+
replace_esc = re.escape(replace)
3335
if "\x01ACTION" in msg:
3436
msg = msg.replace("\x01ACTION", "").replace("\x01", "")
35-
mod_msg = ireplace(msg, find, "\x02" + replace + "\x02")
37+
mod_msg = ireplace(msg, find_esc, "\x02" + replace_esc + "\x02")
3638
message("Correction, * {} {}".format(name, mod_msg))
3739
else:
38-
mod_msg = ireplace(msg, find, "\x02" + replace + "\x02")
40+
mod_msg = ireplace(msg, find_esc, "\x02" + replace_esc + "\x02")
3941
message("Correction, <{}> {}".format(name, mod_msg))
4042

41-
msg = ireplace(msg, find, replace)
43+
msg = ireplace(msg, find_esc, replace_esc)
4244
if nick.lower() == name.lower():
4345
conn.history[chan].append((name, timestamp, msg))
4446
return
4547
else:
4648
continue
47-
# return("No matches for \"\x02{}\x02\" in recent messages from \x02{}\x02. You can only correct your own messages.".format(find, nick))
49+
# return("No matches for \"\x02{}\x02\" in recent messages from \x02{}\x02. You can only correct your own messages.".format(find, nick))

0 commit comments

Comments
 (0)