diff options
author | Brian Sherson <caretaker82@euclid.shersonb.net> | 2013-12-04 01:35:44 -0800 |
---|---|---|
committer | Brian Sherson <caretaker82@euclid.shersonb.net> | 2013-12-04 01:35:44 -0800 |
commit | 0d802dab5b586d511a6291248702c910502acaf7 (patch) | |
tree | f370db15b1a29c2fa8b6699188d28923b1361462 /sedbot.py | |
parent | 5cf8d2185f89aaf30e856bd0dbca88fbb51789ba (diff) |
Significant changes
Diffstat (limited to 'sedbot.py')
-rw-r--r-- | sedbot.py | 105 |
1 files changed, 43 insertions, 62 deletions
@@ -7,72 +7,53 @@ import time class SED(object): def __init__(self, expiry=1800): self.__name__ = "SED Bot" - self.__version__ = "0.0.1" + self.__version__ = "0.0.2" self.expiry = expiry self.history = [] self.pattern = r"^!?s([,/#])((?:.|\\\1)*)\1((?:.|\\\1)*)\1([ig]*)$" - def onRecv(self, IRC, line, data): - if data is None: - return - self.replace(IRC, *data) + def onChanMsg(self, IRC, user, channel, targetprefix, msg): + matches = re.findall(self.pattern, 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: + 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, msg, False)) + while len(self.history) and self.history[0][0] < time.time()-1800: + del self.history[0] - def onSend(self, IRC, line, data, origin): - if origin == self: - return - #print data - (cmd, target, params, extinfo) = data - if IRC.identity: - self.replace(IRC, IRC.identity.nick, - IRC.identity.idnt, IRC.identity.host, *data) + def onSendChanMsg(self, IRC, origin, channel, targetprefix, msg): + if origin != self: # Ignore messages sent from THIS addon. + self.onChanMsg(IRC, IRC.identity, channel, targetprefix, msg) - def replace(self, IRC, origin, ident, host, cmd, target, params, extinfo): - ### Clear out old data that has expired. - while len(self.history) and self.history[0][0] < time.time()-self.expiry: + 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: del self.history[0] - if len(target) and target[0] == "#" and cmd == "PRIVMSG": - target = IRC.channel(target) - matches = re.findall(self.pattern, extinfo) - if matches: - separator, find, replace, flags = matches[0] - #print matches - find = re.sub("\\\\([,/#\\\\])", "\\1", find) - replace = re.sub("\\\\(,/#\\\\)", "\\1", replace) - #print find, replace - match = False - #print self.history - #print find - #print replace - for t, IRC2, (origin2, ident2, host2, cmd2, target2, params2, extinfo2) in self.history.__reversed__(): - #print target, target2, origin2, extinfo2 - if target != target2: - continue - action = re.findall("^\x01ACTION\\s+(.*)\x01$", extinfo2) - #print action - if action: - try: - if re.findall(find, action[0]): - sub = re.sub(find, replace, action[0], flags=re.I if "i" in flags else 0) - target.msg("What %s really meant was: *%s %s" % (origin2, origin2, sub), origin=self) - match = True - break - except: - target.msg("%s: Invalid syntax" % (origin), - origin=self) - raise - else: - try: - if re.findall(find, extinfo2): - sub = re.sub(find, replace, extinfo2, flags=re.I if "i" in flags else 0) - target.msg("What %s really meant to say was: %s" % (origin2, sub), origin=self) - match = True - break - except: - target.msg("%s: Invalid syntax" % (origin), - origin=self) - raise - if not match: - target.msg("%s: I tried. I really tried! But I could not find the pattern: %s" % (origin, find), origin=self) - else: - self.history.append((time.time(), IRC, (origin, ident, - host, cmd, target, params, extinfo))) + + def onSendChanAction(self, IRC, origin, channel, targetprefix, action): + if origin != self: # Ignore messages sent from THIS addon. + self.onChanAction(IRC, IRC.identity, channel, targetprefix, action) |