11import re
22
33from cloudbot import hook
4-
54from 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,34 +14,36 @@ 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
26- for item in conn .history [chan ].__reversed__ ():
27- name , timestamp , msg = item
26+ for name , timestamp , msg in reversed (conn .history [chan ]):
2827 if correction_re .match (msg ):
2928 # don't correct corrections, it gets really confusing
3029 continue
3130
3231 if find .lower () in msg .lower ():
33- if "\x01 ACTION" in msg :
34- msg = msg .replace ("\x01 ACTION" , "" ).replace ("\x01 " , "" )
35- mod_msg = ireplace (msg , find , "\x02 " + replace + "\x02 " )
36- message ("Correction, * {} {}" .format (name , mod_msg ))
32+ find_esc = re .escape (find )
33+ replace_esc = re .escape (replace )
34+ if msg .startswith ('\x01 ACTION' ):
35+ mod_msg = msg [7 :].strip (' \x01 ' )
36+ fmt = "* {} {}"
3737 else :
38- mod_msg = ireplace (msg , find , "\x02 " + replace + "\x02 " )
39- message ("Correction, <{}> {}" .format (name , mod_msg ))
38+ mod_msg = msg
39+ fmt = "<{}> {}"
40+
41+ mod_msg = ireplace (mod_msg , find_esc , "\x02 " + replace_esc + "\x02 " )
4042
41- msg = ireplace (msg , find , replace )
43+ message ("Correction, {}" .format (fmt .format (name , mod_msg )))
44+
45+ msg = ireplace (msg , find_esc , replace_esc )
4246 if nick .lower () == name .lower ():
4347 conn .history [chan ].append ((name , timestamp , msg ))
44- return
45- else :
46- continue
47- # return("No matches for \"\x02{}\x02\" in recent messages from \x02{}\x02. You can only correct your own messages.".format(find, nick))
48+
49+ break
0 commit comments