diff options
author | Brian Sherson <caretaker82@euclid.shersonb.net> | 2014-02-09 13:27:05 -0800 |
---|---|---|
committer | Brian Sherson <caretaker82@euclid.shersonb.net> | 2014-02-09 13:27:05 -0800 |
commit | 0ec6553cffaa4bf115efddf815caa7cea56527d7 (patch) | |
tree | 7036b07f84ddc642dbfbcf4eb150b0a2e734ee08 /sedbot.py | |
parent | 13ca15787aaccb8eb17f4d0b548b17b7036fbaf5 (diff) |
Minor updates
Diffstat (limited to 'sedbot.py')
-rw-r--r-- | sedbot.py | 73 |
1 files changed, 34 insertions, 39 deletions
@@ -5,52 +5,48 @@ import time class SED(object): + def __init__(self, expiry=1800): self.__name__ = "SED Bot" self.__version__ = "0.0.2" self.expiry = expiry self.history = [] - self.trigger = r"^!?s([,/#])(.*)$" - self.pattern = r"^((?:[^%(delimiter)s]|\\.)*)%(delimiter)s((?:[^%(delimiter)s]|\\.)*)%(delimiter)s([igv]*)$" + self.pattern = r"^!?s([,/#])((?:.|\\\1)*)\1((?:.|\\\1)*)\1([ig]*)$" def onChanMsg(self, IRC, user, channel, targetprefix, msg): - matches = re.findall(self.trigger, msg) + matches = re.findall(self.pattern, msg) if matches: - delimiter, args = matches[0] - validate = re.findall(self.pattern%vars(), args) - if validate: - find, replace, flags = validate[0] - find = re.sub(r"\\([,/#\\])", r"\1", find) - replace = re.sub(r"\\([,/#\\])", r"\1", replace) - if "v" in flags: - channel.msg("Delimiter: '%s', Search pattern: '%s', Replacement: '%s', Flags: '%s'" % (delimiter, find, replace, flags), origin=self) - match = False - for t, IRC2, user2, channel2, targetprefix2, msg2, isaction in self.history.__reversed__(): - if channel != channel2: - continue - try: - if re.findall(find, msg2, flags=re.I if "i" in flags else 0): - sub = re.sub(find, replace, msg2, flags=re.I if "i" in flags else 0) - match = True - else: - continue - except: - channel.msg("%s: Invalid syntax" % - user.nick, origin=self) - raise - if isaction: - channel.msg("What %s really meant was: *%s %s" % (user2.nick, user2.nick, sub), origin=self) + separator, find, replace, flags = matches[0] + find = re.sub("\\\\([,/#\\\\])", "\\1", find) + replace = re.sub("\\\\(,/#\\\\)", "\\1", replace) + match = False + for t, IRC2, user2, channel2, msg2, isaction in self.history.__reversed__(): + if channel != channel2: + continue + try: + if re.findall(find, msg2): + sub = re.sub( + find, replace, msg2, flags=re.I if "i" in flags else 0) + match = True else: - channel.msg("What %s really meant to say was: %s" % (user2.nick, sub), origin=self) - break - if not match: - channel.msg("%s: I tried. I really tried! But I could not find the pattern: %s" % (user.nick, find), origin=self) - else: - channel.msg("%s: Invalid syntax. Did you forget a trailing delimiter?" % user.nick, origin=self) + continue + except: + channel.msg("%s: Invalid syntax" % user.nick, origin=self) + raise + if isaction: + channel.msg("What %s really meant was: *%s %s" % + (user2.nick, user2.nick, sub), origin=self) + else: + channel.msg("What %s really meant to say was: %s" % + (user2.nick, sub), origin=self) + break + if not match: + channel.msg( + "%s: I tried. I really tried! But I could not find the pattern: %s" % + (user.nick, find), origin=self) else: - self.history.append((time.time( - ), IRC, user, channel, targetprefix, msg, False)) - while len(self.history) and self.history[0][0] < time.time()-1800: + self.history.append((time.time(), IRC, user, channel, msg, False)) + while len(self.history) and self.history[0][0] < time.time() - 1800: del self.history[0] def onSendChanMsg(self, IRC, origin, channel, targetprefix, msg): @@ -58,9 +54,8 @@ class SED(object): self.onChanMsg(IRC, IRC.identity, channel, targetprefix, msg) def onChanAction(self, IRC, user, channel, targetprefix, action): - self.history.append((time.time( - ), IRC, user, channel, targetprefix, action, True)) - while len(self.history) and self.history[0][0] < time.time()-1800: + self.history.append((time.time(), IRC, user, channel, action, True)) + while len(self.history) and self.history[0][0] < time.time() - 1800: del self.history[0] def onSendChanAction(self, IRC, origin, channel, targetprefix, action): |