From 07744a0f66c797c13823a86804f8c9d08811e7e1 Mon Sep 17 00:00:00 2001 From: "Brian S. Stephan" Date: Thu, 29 Mar 2012 20:07:32 -0500 Subject: [PATCH] indicate recursion better by adding _recursing to Event for simplicity's sake, this was added to the extlib/irclib rather than subclassing. because i'm lazy. anyway, check that flag instead of doing the event._target = None hack, since that hack was breaking Markov. for an unrelated reason (what to learn and not learn), update Markov also remove an unused method that was getting in my way while coding this --- DrBotIRC.py | 17 ++--------------- Module.py | 2 +- extlib/irclib.py | 1 + modules/Markov.py | 13 +++++-------- 4 files changed, 9 insertions(+), 24 deletions(-) diff --git a/DrBotIRC.py b/DrBotIRC.py index cb1f9c7..4626e7e 100644 --- a/DrBotIRC.py +++ b/DrBotIRC.py @@ -139,7 +139,6 @@ class DrBotIRC(irclib.IRC): h = self.handlers replies = [] - event._target = None for handler in h.get("all_events", []) + h.get(event.eventtype(), []): ret = handler[1](connection, event) if ret: @@ -237,20 +236,6 @@ class DrBotIRC(irclib.IRC): # if we got here, there are no matching aliases, so return what we got return event - def reply(self, connection, event, replystr, stop_responding=False): - """Reply over IRC to replypath or return a string with the reply.""" - - replypath = event.target() - if replystr is not None: - if replypath is None: - return replystr - else: - replies = replystr.split('\n') - for reply in replies: - connection.privmsg(replypath, reply) - if stop_responding: - return "NO MORE" - def try_recursion(self, connection, event): """Scan message for subcommands to execute and use as part of this command. @@ -277,6 +262,8 @@ class DrBotIRC(irclib.IRC): # copy the event and see if IT has recursion to do newevent = copy.deepcopy(event) newevent.arguments()[0] = subcmd + newevent._recursing = True + self.try_recursion(connection, newevent) # recursion over, check for aliases diff --git a/Module.py b/Module.py index c414bef..7835cd3 100644 --- a/Module.py +++ b/Module.py @@ -114,7 +114,7 @@ class Module(object): replypath = irclib.nm_to_n(event.source()) if replystr is not None: - if replypath is None: + if event._recursing: return replystr else: replies = replystr.split('\n') diff --git a/extlib/irclib.py b/extlib/irclib.py index 5f7141c..defde24 100644 --- a/extlib/irclib.py +++ b/extlib/irclib.py @@ -1132,6 +1132,7 @@ class Event: self._eventtype = eventtype self._source = source self._target = target + self._recursing = False if arguments: self._arguments = arguments else: diff --git a/modules/Markov.py b/modules/Markov.py index d4a826d..3047e34 100644 --- a/modules/Markov.py +++ b/modules/Markov.py @@ -130,8 +130,6 @@ class Markov(Module): db.commit() db.close() self.db_register_module_version(self.__class__.__name__, version) - - self._learn_line('','') except sqlite3.Error as e: db.rollback() db.close() @@ -171,7 +169,7 @@ class Markov(Module): if self.learnre.search(what) or self.replyre.search(what): return - self._learn_line(what, target) + self._learn_line(what, target, event) def do(self, connection, event, nick, userhost, what, admin_unlocked): """Handle commands and inputs.""" @@ -207,7 +205,7 @@ class Markov(Module): match = self.learnre.search(what) if match: line = match.group(1) - self._learn_line(line, target) + self._learn_line(line, target, event) # return what was learned, for weird chaining purposes return line @@ -288,7 +286,7 @@ class Markov(Module): self.sendmsg(self.connection, t['target'], 'shutting up for 30 seconds due to last 30 seconds of activity') - def _learn_line(self, line, target): + def _learn_line(self, line, target, event): """Create Markov chains from the provided line.""" # set up the head of the chain @@ -297,9 +295,8 @@ class Markov(Module): context_id = self._get_context_id_for_target(target) - # if there's no target, this is probably a sub-command. don't learn it - if target: - + # don't learn recursion + if not event._recursing: words = line.split() if len(words) <= 0: return line