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 | |
| parent | 13ca15787aaccb8eb17f4d0b548b17b7036fbaf5 (diff) | |
Minor updates
| -rw-r--r-- | sedbot.py | 73 | ||||
| -rw-r--r-- | testaddon.py | 142 | 
2 files changed, 109 insertions, 106 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): diff --git a/testaddon.py b/testaddon.py index 5379188..f0e14a7 100644 --- a/testaddon.py +++ b/testaddon.py @@ -1,213 +1,221 @@  class Addon(object): +      def onAddonAdd(self, IRC, **params): -        ### Stuff you want the Addon to do when added or inserted into the addon list of an IRC instance. +        # Stuff you want the Addon to do when added or inserted into the addon +        # list of an IRC instance.          print "onAddonAdd:", IRC, params      def onAddonRem(self, IRC): -        ### Stuff you want the Addon to do when removed from the addon list of an IRC instance. +        # Stuff you want the Addon to do when removed from the addon list of an +        # IRC instance.          print "onAddonRem:", IRC      def onSessionOpen(self, IRC): -        ### Called when the thread handling the IRC instance is started. +        # Called when the thread handling the IRC instance is started.          print "onSessionOpen:", IRC      def onSessionClose(self, IRC): -        ### Called when the thread handling the IRC instance is terminated. +        # Called when the thread handling the IRC instance is terminated.          print "onSessionClose:", IRC      def onConnectAttempt(self, IRC): -        ### Called when a connection attempt is made. +        # Called when a connection attempt is made.          print "onConnectAttempt:", IRC      def onConnectFail(self, IRC, exc, excmsg, tb): -        ### Called when a connection attempt fails. +        # Called when a connection fails.          print "onConnectFail:", IRC, exc, excmsg, tb      def onConnect(self, IRC): -        ### Called when a connection is established. +        # Called when a connection is established.          print "onConnect:", IRC      def onRegistered(self, IRC): -        ### Called when registration is complete. +        # Called when registration is complete.          print "onRegistered:", IRC -    def onDisconnect(self, IRC): -        ### Called when a connection is terminated. +    def onDisconnect(self, IRC, expected): +        # Called when a connection is terminated.          print "onDisconnect:", IRC      def onRecv(self, IRC, line, origin=None, cmd=None, target=None, targetprefix=None, params=None, extinfo=None): -        ### Called when a line of data is received from the IRC server. If line also matches ":origin cmd target [params] :[extinfo]", -        ### then other variables are provided, with origin and target automatically converted to Channel and User instances as needed. +        # Called when a line of data is received from the IRC server. If line also matches ":origin cmd target [params] :[extinfo]", +        # then other variables are provided, with origin and target +        # automatically converted to Channel and User instances as needed.          pass      def onSend(self, IRC, origin, line, cmd=None, target=None, targetprefix=None, params=None, extinfo=None): -        ### Called when a line of data is sent to the IRC server. If line also matches "cmd target [params] :[extinfo]", -        ### then other variables are provided, with target automatically converted to Channel and User instances as needed. -        ### The variable origin refers to a class instance voluntarily identifying itself as that which requested data be sent. +        # Called when a line of data is sent to the IRC server. If line also matches "cmd target [params] :[extinfo]", +        # then other variables are provided, with target automatically converted to Channel and User instances as needed. +        # The variable origin refers to a class instance voluntarily +        # identifying itself as that which requested data be sent.          pass      def onMOTD(self, IRC, motdgreet, motd, motdend): -        ### Called when MOTD is received. +        # Called when MOTD is received.          print "onMOTD:", motdgreet          for line in motd:              print line          print motdend      def onJoin(self, IRC, user, channel): -        ### Called when somebody joins a channel, includes bot. +        # Called when somebody joins a channel, includes bot.          print "onJoin:", user, channel      def onMeJoin(self, IRC, channel): -        ### Called when the bot enters the channel. +        # Called when the bot enters the channel.          print "onMeJoin:", channel      def onChanMsg(self, IRC, user, channel, targetprefix, msg): -        ### Called when someone sends a PRIVMSG to channel. -        print "onChanMsg: [%s%s] <%s> %s" % ( -            targetprefix, channel.name, user.nick, msg) +        # Called when someone sends a PRIVMSG to channel. +        print "onChanMsg: [%s%s] <%s> %s" % (targetprefix, channel.name, user.nick, msg)      def onChanAction(self, IRC, user, channel, targetprefix, action): -        ### Called when someone sends an action (/me) to channel. -        print "onChanAction: [%s%s] *%s %s" % ( -            targetprefix, channel.name, user.nick, action) +        # Called when someone sends an action (/me) to channel. +        print "onChanAction: [%s%s] *%s %s" % (targetprefix, channel.name, user.nick, action)      def onSendChanMsg(self, IRC, origin, channel, targetprefix, msg): -        ### Called when bot sends a PRIVMSG to channel. -        ### The variable origin refers to a class instance voluntarily identifying itself as that which requested data be sent. -        print "onSendChanMsg: [%s%s] <%s> %s" % ( -            targetprefix, channel.name, IRC.identity.nick, msg) +        # Called when bot sends a PRIVMSG to channel. +        # The variable origin refers to a class instance voluntarily +        # identifying itself as that which requested data be sent. +        print "onSendChanMsg: [%s%s] <%s> %s" % (targetprefix, channel.name, IRC.identity.nick, msg)      def onSendChanAction(self, IRC, origin, channel, targetprefix, action): -        ### origin is the source of the channel message -        ### Called when bot sends an action (/me) to channel. -        ### The variable origin refers to a class instance voluntarily identifying itself as that which requested data be sent. -        print "onSendChanAction: [%s%s] *%s %s" % ( -            targetprefix, channel.name, IRC.identity.nick, action) +        # origin is the source of the channel message +        # Called when bot sends an action (/me) to channel. +        # The variable origin refers to a class instance voluntarily +        # identifying itself as that which requested data be sent. +        print "onSendChanAction: [%s%s] *%s %s" % (targetprefix, channel.name, IRC.identity.nick, action)      def onPrivMsg(self, IRC, user, msg): -        ### Called when someone sends a PRIVMSG to the bot. +        # Called when someone sends a PRIVMSG to the bot.          print "onPrivMsg: <%s> %s" % (user.nick, msg)      def onPrivAction(self, IRC, user, action): -        ### Called when someone sends an action (/me) to the bot. +        # Called when someone sends an action (/me) to the bot.          print "onPrivAction: *%s %s" % (user.nick, action)      def onSendPrivMsg(self, IRC, origin, user, msg): -        ### Called when bot sends a PRIVMSG to a user. -        ### The variable origin refers to a class instance voluntarily identifying itself as that which requested data be sent. +        # Called when bot sends a PRIVMSG to a user. +        # The variable origin refers to a class instance voluntarily +        # identifying itself as that which requested data be sent.          print "onSendPrivMsg: <%s> %s" % (IRC.identity.nick, msg)      def onSendPrivAction(self, IRC, origin, user, action): -        ### Called when bot sends an action (/me) to a user. -        ### The variable origin refers to a class instance voluntarily identifying itself as that which requested data be sent. +        # Called when bot sends an action (/me) to a user. +        # The variable origin refers to a class instance voluntarily +        # identifying itself as that which requested data be sent.          print "onSendPrivAction: *%s %s" % (IRC.identity.nick, action)      def onNickChange(self, IRC, user, newnick): -        ### Called when somebody changes nickname. +        # Called when somebody changes nickname.          print "onNickChange:", user, newnick      def onMeNickChange(self, IRC, newnick): -        ### Called when the bot changes nickname. +        # Called when the bot changes nickname.          print "onMeNickChange:", newnick      def onPart(self, IRC, user, channel, partmsg): -        ### Called when somebody parts the channel, includes bot. +        # Called when somebody parts the channel, includes bot.          print "onPart:", user, channel, partmsg      def onMePart(self, IRC, channel, partmsg): -        ### Called when the bot parts the channel. +        # Called when the bot parts the channel.          print "onMePart:", channel, partmsg      def onKick(self, IRC, kicker, channel, kicked, kickmsg): -        ### Called when somebody is kicked from the channel, includes bot. +        # Called when somebody is kicked from the channel, includes bot.          print "onKick:", kicker, channel, kicked, kickmsg      def onMeKick(self, IRC, channel, kicked, kickmsg): -        ### Called when the bot kicks somebody from the channel. +        # Called when the bot kicks somebody from the channel.          print "onMeKick:", channel, kicked, kickmsg      def onMeKicked(self, IRC, kicker, channel, kickmsg): -        ### Called when the bot is kicked from the channel. +        # Called when the bot is kicked from the channel.          print "onMeKicked:", kicker, channel, kickmsg      def onQuit(self, IRC, user, quitmsg): -        ### Called when somebody quits IRC. +        # Called when somebody quits IRC.          print "onQuit:", user, quitmsg      def onNames(self, IRC, channel, channame, endmsg, nameslist): -        ### Called when a NAMES list is received. +        # Called when a NAMES list is received.          print "onNames:", channel, channame, endmsg, nameslist      def onWhois(self, IRC, **whoisdata): -        ### Called when a WHOIS reply is received. +        # Called when a WHOIS reply is received.          print "onWhois:", whoisdata      def onWho(self, IRC, params, wholist, endmsg): -        ### Called when a WHO list is received. +        # Called when a WHO list is received.          print "onWho:", params          for item in wholist:              print item          print endmsg      def onList(self, IRC, chanlistbegin, chanlist, endmsg): -        ### Called when a channel list is received. +        # Called when a channel list is received.          print "onList:", chanlistbegin          for item in chanlist:              print item          print endmsg      def onTopic(self, IRC, channel, topic): -        ### Called when channel topic is received via 332 response. +        # Called when channel topic is received via 332 response.          print "onTopic:", channel, topic      def onTopicSet(self, IRC, user, channel, topic): -        ### Called when channel topic is changed. +        # Called when channel topic is changed.          print "onChannelSet:", user, channel, topic      def onChanModeSet(self, IRC, user, channel, modedelta): -        ### Called when channel modes are changed. -        ### modedelta is a list of tuples of the format ("+x", parameter), ("+x", None) if no parameter is provided. +        # Called when channel modes are changed. +        # modedelta is a list of tuples of the format ("+x", parameter), ("+x", +        # None) if no parameter is provided.          print "onChanModeSet:", user, channel, modedelta      def onChannelModes(self, IRC, channel, modedelta): -        ### Called when channel modes are received via 324 response. +        # Called when channel modes are received via 324 response.          print "onChannelModes:", channel, modedelta      def onBan(self, IRC, user, channel, banmask): -        ### Called when a ban is set. +        # Called when a ban is set.          print "onBan:", user, channel, banmask      def onMeBan(self, IRC, user, channel, banmask): -        ### Called when a ban matching bot is set. +        # Called when a ban matching bot is set.          print "Help! I'm being banned!", user, channel, banmask      def onUnban(self, IRC, user, channel, banmask): -        ### Called when a ban is removed. +        # Called when a ban is removed.          print "onUnban:", user, channel, banmask      def onMeUnban(self, IRC, user, channel, banmask): -        ### Called when a ban on the bot is removed. +        # Called when a ban on the bot is removed.          print "Squeee!!! I've been unbanned!", user, channel, banmask      def onOp(self, IRC, user, channel, modeuser): -        ### Called when user gives ops to modeuser is set. +        # Called when user gives ops to modeuser is set.          print "onOp:", user, channel, modeuser      def onMeOp(self, IRC, user, channel): -        ### Called when user ops bot. +        # Called when user ops bot.          print "onMeOp", user, channel      def onDeop(self, IRC, user, channel, modeuser): -        ### Called when user deops modeuser. +        # Called when user deops modeuser.          print "onDeop:", user, channel, modeuser      def onMeDeop(self, IRC, user, channel): -        ### Called when user deops bot. +        # Called when user deops bot.          print "onMeDeop", user, channel -    ### There are a few other event handlers supported by the irc.Connection class. Furthermore, one can program -    ### numeric-based handlers to handle numeric events that are not captured by a named event handler as follows: +    # There are a few other event handlers supported by the irc.Connection class. Furthermore, one can program +    # numeric-based handlers to handle numeric events that are not captured by +    # a named event handler as follows:      def onNNN(self, IRC, params, extinfo): -        ### Where NNN is a three-digit (leading zeros if necessary) code used to handle a numeric NNN event. +        # Where NNN is a three-digit (leading zeros if necessary) code used to +        # handle a numeric NNN event.          pass | 
