From f09bc0663463640364367cd6a02fdbad7b84b0e8 Mon Sep 17 00:00:00 2001 From: "Brian S. Stephan" Date: Thu, 20 Dec 2012 10:20:33 -0600 Subject: [PATCH] Echo: rewrite to do regex handlers this is the first implementer of the new regex handler code, by all accounts it's working fine, hopefully the underlying code in DrBotIRC won't need to change as i go forward, but it might --- modules/Echo.py | 42 +++++++++++++++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 7 deletions(-) diff --git a/modules/Echo.py b/modules/Echo.py index aa7e60a..ddf44bc 100644 --- a/modules/Echo.py +++ b/modules/Echo.py @@ -14,9 +14,8 @@ GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . -""" -import re +""" from Module import Module @@ -24,12 +23,41 @@ class Echo(Module): """Repeat provided text.""" - def do(self, connection, event, nick, userhost, what, admin_unlocked): - """Repeat provided text.""" + def register_handlers(self): + """Hook handler functions into the IRC library.""" - match = re.search('^!echo\s+(.*)$', what) - if match: - return self.irc.reply(event, match.group(1)) + self.irc.add_global_regex_handler('pubmsg', r'^!echo\s+(.*)$', + self.echo) + self.irc.add_global_regex_handler('privmsg', r'^!echo\s+(.*)$', + self.echo) + + def unregister_handlers(self): + """Unhook handler functions from the IRC library.""" + + self.irc.remove_global_regex_handler('pubmsg', r'^!echo\s+(.*)$', + self.echo) + self.irc.remove_global_regex_handler('privmsg', r'^!echo\s+(.*)$', + self.echo) + + def echo(self, nick, userhost, event, from_admin, groups): + """Return the message received. + + Args: + nick source nickname (unused) + userhost source userhost (unused) + event IRC event, used in IRC reply, or none for direct call + from_admin whether or not the event came from an admin (unused) + groups list length 1, the message to echo + + """ + + msg = groups[0] + self.log.debug("replying with '{0:s}'".format(msg)) + + if event: + return self.irc.reply(event, msg) + else: + return msg # vi:tabstop=4:expandtab:autoindent # kate: indent-mode python;indent-width 4;replace-tabs on;