allow IRCBot.reply() to work eventless

reply() used to require an event, but all it used it for was to
determine the destination and to identify recursion. basically, strictly
only -replies-. we can make this a more robust privmsg, too, by adding
explicit_target and inferring recursion as False. this will let
basically any code currently using privmsg to use reply instead, and
benefit from multi-line and line splitting

bss/dr.botzo#21
This commit is contained in:
Brian S. Stephan 2017-02-12 11:33:13 -06:00
parent 010afd05ce
commit 23bb5cdd78
1 changed files with 18 additions and 8 deletions

View File

@ -840,7 +840,7 @@ class IRCBot(irc.client.SimpleIRCClient):
log.debug("OUTGOING PRIVMSG: t[%s] m[%s]", target, text)
self.connection.send_raw("PRIVMSG {0:s} :{1:s}".format(target, text))
def reply(self, event, replystr, stop=False):
def reply(self, event, replystr, stop=False, explicit_target=None):
"""Reply over IRC to replypath or return a string with the reply.
The primary utility for this is to properly handle recursion. The
@ -856,19 +856,29 @@ class IRCBot(irc.client.SimpleIRCClient):
event incoming event
replystr the message to reply with
stop whether or not to let other handlers see this
explicit_target if event is none, use this for the destination
Returns:
The replystr if the event is inside recursion, or, potentially,
"NO MORE" to stop other event handlers from acting.
"""
if event:
log.debug("in reply for e[%s] r[%s]", event, replystr)
replypath = ircbotlib.reply_destination_for_event(event)
recursing = getattr(event, '_recursing', False)
log.debug("determined recursing to be %s", recursing)
elif explicit_target:
log.debug("in reply for e[NO EVENT] r[%s]", replystr)
replypath = explicit_target
recursing = False
else:
log.warning("reply() called with no event and no explicit target, aborting")
return
log.debug("replypath: %s", replypath)
if replystr is not None:
recursing = getattr(event, '_recursing', False)
log.debug("determined recursing to be %s", recursing)
if recursing:
return replystr
else: