diff --git a/dr_botzo/markov/ircplugin.py b/dr_botzo/markov/ircplugin.py index 2afe9b7..85a690d 100644 --- a/dr_botzo/markov/ircplugin.py +++ b/dr_botzo/markov/ircplugin.py @@ -20,6 +20,11 @@ class Markov(Plugin): self.connection.add_global_handler('pubmsg', getattr(self, 'handle_chatter'), -20) self.connection.add_global_handler('privmsg', getattr(self, 'handle_chatter'), -20) + self.connection.reactor.add_global_regex_handler('pubmsg', r'^!markov\s+reply(\s+min=(\d+))?(\s+max=(\d+))?(\s+(.*)$|$)', + getattr(self, 'handle_reply'), -20) + self.connection.reactor.add_global_regex_handler('privmsg', r'^!markov\s+reply(\s+min=(\d+))?(\s+max=(\d+))?(\s+(.*)$|$)', + getattr(self, 'handle_reply'), -20) + super(Markov, self).start() def stop(self): @@ -28,8 +33,37 @@ class Markov(Plugin): self.connection.remove_global_handler('pubmsg', getattr(self, 'handle_chatter')) self.connection.remove_global_handler('privmsg', getattr(self, 'handle_chatter')) + self.connection.reactor.remove_global_regex_handler('pubmsg', getattr(self, 'handle_reply')) + self.connection.reactor.remove_global_regex_handler('privmsg', getattr(self, 'handle_reply')) + super(Markov, self).stop() + def handle_reply(self, connection, event, match): + """Generate a reply to one line, without learning it.""" + + target = reply_destination_for_event(event) + + min_size = 15 + max_size = 30 + context = markovlib.get_or_create_target_context(target) + + if match.group(2): + min_size = int(match.group(2)) + if match.group(4): + max_size = int(match.group(4)) + + if match.group(5) != '': + line = match.group(6) + topics = [x for x in line.split(' ') if len(x) >= 3] + + return self.bot.reply(event, u" ".join(markovlib.generate_line(context, topics=topics, + min_words=min_size, max_words=max_size, + max_sentences=1))) + else: + return self.bot.reply(event, u" ".join(markovlib.generate_line(context, min_words=min_size, + max_words=max_size, + max_sentences=1))) + def handle_chatter(self, connection, event): """Learn from IRC chatter."""