From 07ca50dfb639be268355dae062c277146e79dcda Mon Sep 17 00:00:00 2001 From: "Brian S. Stephan" Date: Fri, 15 May 2015 17:35:03 -0500 Subject: [PATCH] ircbot: reply() method that supports recursion undecided if i'll bother to bring recursion back, but if it works out, provide this method to either reply or give text to the thing recursing. either way this is a bit clearer than using privmsg() directly, usually --- dr_botzo/ircbot/bot.py | 35 +++++++++++++++++++++++++++ dr_botzo/ircbot/ircplugins/ircmgmt.py | 4 +-- dr_botzo/markov/ircplugin.py | 16 ++++++------ 3 files changed, 45 insertions(+), 10 deletions(-) diff --git a/dr_botzo/ircbot/bot.py b/dr_botzo/ircbot/bot.py index bd4eb89..41e4c47 100644 --- a/dr_botzo/ircbot/bot.py +++ b/dr_botzo/ircbot/bot.py @@ -492,6 +492,41 @@ class IRCBot(irc.client.SimpleIRCClient): else: self.connection.send_raw("PRIVMSG {0:s} :{1:s}".format(target, text)) + def reply(self, event, replystr, stop=False): + """Reply over IRC to replypath or return a string with the reply. + + The primary utility for this is to properly handle recursion. The + recursion code will set up a couple hints that this method + picks up on and will appropriately send an IRC event or return a + string. + + Unless you know what you are doing, the modules you write should use + this method rather than send a privmsg reply, as failing to call this + method will certainly have recursion do odd things with your module. + + Args: + event incoming event + replystr the message to reply with + stop whether or not to let other handlers see this + + Returns: + The replystr if the event is inside recursion, or, potentially, + "NO MORE" to stop other event handlers from acting. + """ + + replypath = ircbotlib.reply_destination_for_event(event) + + if replystr is not None: + recursing = getattr(event, '_recursing', False) + if recursing: + return replystr + else: + replies = replystr.split('\n') + for reply in replies: + self.privmsg(replypath, reply) + if stop: + return "NO MORE" + def start(self): """Start the bot.""" diff --git a/dr_botzo/ircbot/ircplugins/ircmgmt.py b/dr_botzo/ircbot/ircplugins/ircmgmt.py index 0192170..40e8a40 100644 --- a/dr_botzo/ircbot/ircplugins/ircmgmt.py +++ b/dr_botzo/ircbot/ircplugins/ircmgmt.py @@ -53,7 +53,7 @@ class ChannelManagement(Plugin): chan_mod, c = IrcChannel.objects.get_or_create(name=channel) log.debug(u"joining channel %s", channel) self.connection.join(channel) - self.bot.privmsg(dest, "Joined channel {0:s}.".format(channel)) + self.bot.reply(event, "Joined channel {0:s}.".format(channel)) def handle_part(self, connection, event, match): """Handle the join command.""" @@ -66,7 +66,7 @@ class ChannelManagement(Plugin): chan_mod, c = IrcChannel.objects.get_or_create(name=channel) log.debug(u"parting channel %s", channel) self.connection.part(channel) - self.bot.privmsg(dest, "Parted channel {0:s}.".format(channel)) + self.bot.reply(event, "Parted channel {0:s}.".format(channel)) def handle_quit(self, connection, event, match): """Handle the join command.""" diff --git a/dr_botzo/markov/ircplugin.py b/dr_botzo/markov/ircplugin.py index dc0c697..e439a2e 100644 --- a/dr_botzo/markov/ircplugin.py +++ b/dr_botzo/markov/ircplugin.py @@ -55,18 +55,18 @@ class Markov(Plugin): # the speaker topics = [x for x in addressed_re.match(what).group(1).split(' ') if len(x) >= 3] - self.bot.privmsg(target, u"{0:s}: {1:s}" - u"".format(nick, u" ".join(markovlib.generate_line(context, - topics=topics, - max_sentences=1)))) + self.bot.reply(event, u"{0:s}: {1:s}" + u"".format(nick, u" ".join(markovlib.generate_line(context, + topics=topics, + max_sentences=1)))) else: # i wasn't addressed directly, so just respond topics = [x for x in what.split(' ') if len(x) >= 3] - self.bot.privmsg(target, u"{0:s}" - u"".format(u" ".join(markovlib.generate_line(context, - topics=topics, - max_sentences=1)))) + self.bot.reply(event, u"{0:s}" + u"".format(u" ".join(markovlib.generate_line(context, + topics=topics, + max_sentences=1)))) plugin = Markov