From b9bf09e6b64af029828f362eb0a302d5854f4c36 Mon Sep 17 00:00:00 2001 From: Luke Rogers Date: Fri, 28 Feb 2014 13:59:31 +1300 Subject: [PATCH] rewrite correction to use new history tracker --- plugins/correction.py | 48 +++++++++++++++++++++---------------------- plugins/history.py | 2 +- 2 files changed, 25 insertions(+), 25 deletions(-) diff --git a/plugins/correction.py b/plugins/correction.py index db370fa..97ea19f 100644 --- a/plugins/correction.py +++ b/plugins/correction.py @@ -1,30 +1,30 @@ from util import hook +import re + +CORRECTION_RE = re.compile(r'^(s|S)/.*/.*/\S*$') + @hook.regex(r'^(s|S)/.*/.*/\S*$') -def correction(inp, message=None, input=None, notice=None, db=None): - splitinput = input.msg.split("/") - if splitinput[3]: - nick = splitinput[3] - else: - nick = input.nick - last_message = db.execute("select name, quote from seen_user where name" - " like ? and chan = ?", (nick.lower(), input.chan.lower())).fetchone() +def correction(inp, input=None, bot=None, message=None): + split = input.msg.split("/") - if last_message: - splitinput = input.msg.split("/") - find = splitinput[1] - replace = splitinput[2] - if find in last_message[1]: - if "\x01ACTION" in last_message[1]: - msg = last_message[1].replace("\x01ACTION ", "/me ").replace("\x01", "") - else: - msg = last_message[1] - message(u"Correction, <{}> {}".format(nick, msg.replace(find, "\x02" + replace + "\x02"))) + find = split[1] + replace = split[2] + + for item in bot.history[input.chan].__reversed__(): + name, timestamp, msg = item + if "/" in msg: + if re.match(CORRECTION_RE, msg): + # don't correct corrections, it gets really confusing + continue + if find in msg: + if "\x01ACTION" in msg: + msg = msg.replace("\x01ACTION ", "/me ").replace("\x01", "") + message(u"Correction, <{}> {}".format(name, msg.replace(find, "\x02" + replace + "\x02"))) + return else: - notice(u"{} can't be found in your last message".format(find)) - else: - if nick == input.nick: - notice(u"I haven't seen you say anything here yet") - else: - notice(u"I haven't seen {} say anything here yet".format(nick)) + continue + + return "Did not find {} in any recent messages.".format(find) + diff --git a/plugins/history.py b/plugins/history.py index 8f77117..438aed3 100644 --- a/plugins/history.py +++ b/plugins/history.py @@ -35,7 +35,7 @@ def track_history(input, message_time, bot): bot.history[input.chan] = deque(maxlen=1000) history = bot.history[input.chan] - data = (input.nick.lower(), message_time, input.msg) + data = (input.nick, message_time, input.msg) history.append(data)