def correction(match, conn, chan, message): """ :type match: re.__Match :type conn: ralybot.client.Client :type chan: str """ groups = [b.replace("\/", "/") for b in re.split(r"(?<!\\)/", match.groups()[0])] find = groups[0] replace = groups[1] if find == replace: return "Really dude? You want me to replace {} with {}?".format(find, replace) for item in conn.history[chan].__reversed__(): nick, timestamp, msg = item if correction_re.match(msg): # Don't correct corrections, it just gets really confusing. continue if find.lower() in msg.lower(): if "\x01ACTION" in msg: msg = msg.replace("\x01ACTION", "").replace("\x01", "") mod_msg = ireplace(msg, find, "\x02" + replace + "\x02") message("Correction, * {} {}".format(nick, mod_msg)) else: mod_msg = ireplace(msg, find, "\x02" + replace + "\x02") message("Correction, <{}> {}".format(nick, mod_msg)) msg = ireplace(msg, find, replace) conn.history[chan].append((nick, timestamp, msg)) return else: continue
def test_ireplace(): assert ireplace(test_ireplace_input, "fox", "cat") == "The quick brown cat cat cat jumped over the lazy dog" assert ireplace(test_ireplace_input, "FOX", "cAt") == "The quick brown cAt cAt cAt jumped over the lazy dog" assert ireplace(test_ireplace_input, "fox", "cat", 1) == "The quick brown cat fox FOX jumped over the lazy dog" assert ireplace(test_ireplace_input, "fox", "cat", 2) == "The quick brown cat cat FOX jumped over the lazy dog"