From 043ad3074979b1d772f97b2253032bcc5ac7ddeb Mon Sep 17 00:00:00 2001 From: Brian Sherson Date: Sun, 8 Dec 2013 14:17:54 -0800 Subject: Fixed SEDBOT, added v flag --- sedbot.py | 61 +++++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 35 insertions(+), 26 deletions(-) (limited to 'sedbot.py') diff --git a/sedbot.py b/sedbot.py index a3817bc..d13105b 100644 --- a/sedbot.py +++ b/sedbot.py @@ -10,37 +10,46 @@ class SED(object): self.__version__ = "0.0.2" self.expiry = expiry self.history = [] - self.pattern = r"^!?s([,/#])((?:.|\\\1)*)\1((?:.|\\\1)*)\1([ig]*)$" + self.trigger = r"^!?s([,/#])(.*)$" + self.pattern = r"^((?:[^%(delimiter)s]|\\.)*)%(delimiter)s((?:[^%(delimiter)s]|\\.)*)%(delimiter)s([igv]*)$" def onChanMsg(self, IRC, user, channel, targetprefix, msg): - matches = re.findall(self.pattern, msg) + matches = re.findall(self.trigger, msg) if matches: - 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: + 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 - 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) + try: + if re.findall(find, msg2): + 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) + 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) else: - self.history.append((time.time(), IRC, user, channel, msg, False)) + self.history.append((time.time( + ), IRC, user, channel, targetprefix, msg, False)) while len(self.history) and self.history[0][0] < time.time()-1800: del self.history[0] -- cgit v1.2.3