From e33f9b05fb5ca8afa412a411b0bf07c6675eff10 Mon Sep 17 00:00:00 2001 From: Daniel Barnes Date: Sat, 23 Aug 2014 15:50:49 -0600 Subject: [PATCH] use proper sed-like syntax for corrections, including regexes, backreferences, and a few flags --- plugins/correction.py | 40 ++++++++++++++-------------------------- 1 file changed, 14 insertions(+), 26 deletions(-) diff --git a/plugins/correction.py b/plugins/correction.py index 7259c5d1..506214c4 100644 --- a/plugins/correction.py +++ b/plugins/correction.py @@ -1,40 +1,28 @@ -from util import hook - import re -CORRECTION_RE = r'^(s|S)/.*/.*/?\S*$' - - -@hook.regex(CORRECTION_RE) -def correction(match, input=None, conn=None, message=None): - split = input.msg.split("/") +from util import hook - if len(split) == 4: - nick = split[3].lower() - else: - nick = None +correction_s = r"^[sS]/(.*/.*/([igux]{,4}))\S*$" +correction_re = re.compile(correction_s) - find = split[1] - replace = split[2] - if not find and not find.isspace(): - return u"Find must not be blank." +@hook.regex(correction_s) +def correction(match, input=None, conn=None, message=None): + find, replacement, flags = tuple([b.replace("\/", "/") for b in re.split(r"(? {}".format(name, msg.replace(find, "\x02" + replace + "\x02"))) + message(u"Correction, <{}> {}".format(nick, find_re.sub("\x02" + replacement + "\x02", msg, count=int("g" not in flags)))) return else: continue - - return u"Did not find {} in any recent messages.".format(find) - + return "Did not find {} in any recent messages.".format(to_find)