diff --git a/dr_botzo/ircbot/bot.py b/dr_botzo/ircbot/bot.py index f25a4f0..b8a0ed0 100644 --- a/dr_botzo/ircbot/bot.py +++ b/dr_botzo/ircbot/bot.py @@ -43,12 +43,12 @@ class DrReactor(irc.client.Reactor): on_schedule=on_schedule) self.regex_handlers = {} - def add_global_regex_handler(self, event, regex, handler, priority=0): + def add_global_regex_handler(self, events, regex, handler, priority=0): """Adds a global handler function for a specific event type and regex. Arguments: - event --- Event type (a string). + events --- Event type(s) (a list of strings). handler -- Callback function taking connection and event parameters. @@ -67,11 +67,15 @@ class DrReactor(irc.client.Reactor): work, though it turns out most modules probably want this one. """ + if type(events) != list: + events = [events] + handler = PrioritizedRegexHandler(priority, regex, handler) - with self.mutex: - log.debug(u"in add_global_regex_handler") - event_regex_handlers = self.regex_handlers.setdefault(event, []) - bisect.insort(event_regex_handlers, handler) + for event in events: + with self.mutex: + log.debug(u"in add_global_regex_handler") + event_regex_handlers = self.regex_handlers.setdefault(event, []) + bisect.insort(event_regex_handlers, handler) def remove_global_regex_handler(self, event, handler): """Removes a global regex handler function. diff --git a/dr_botzo/ircbot/ircplugins/echo.py b/dr_botzo/ircbot/ircplugins/echo.py index e97f4df..d1fd766 100644 --- a/dr_botzo/ircbot/ircplugins/echo.py +++ b/dr_botzo/ircbot/ircplugins/echo.py @@ -13,9 +13,7 @@ class Echo(Plugin): def start(self): """Set up the handlers.""" - self.connection.reactor.add_global_regex_handler('pubmsg', r'^!echo\s+(.*)$', - getattr(self, 'handle_echo'), -20) - self.connection.reactor.add_global_regex_handler('privmsg', r'^!echo\s+(.*)$', + self.connection.reactor.add_global_regex_handler(['pubmsg', 'privmsg'], r'^!echo\s+(.*)$', getattr(self, 'handle_echo'), -20) super(Echo, self).start() diff --git a/dr_botzo/ircbot/ircplugins/ircmgmt.py b/dr_botzo/ircbot/ircplugins/ircmgmt.py index b0fc1ef..ff3b9a8 100644 --- a/dr_botzo/ircbot/ircplugins/ircmgmt.py +++ b/dr_botzo/ircbot/ircplugins/ircmgmt.py @@ -14,19 +14,13 @@ class ChannelManagement(Plugin): def start(self): """Set up the handlers.""" - self.connection.reactor.add_global_regex_handler('pubmsg', r'^!join\s+([\S]+)', - getattr(self, 'handle_join'), -20) - self.connection.reactor.add_global_regex_handler('privmsg', r'^!join\s+([\S]+)', + self.connection.reactor.add_global_regex_handler(['pubmsg', 'privmsg'], r'^!join\s+([\S]+)', getattr(self, 'handle_join'), -20) - self.connection.reactor.add_global_regex_handler('pubmsg', r'^!part\s+([\S]+)', - getattr(self, 'handle_part'), -20) - self.connection.reactor.add_global_regex_handler('privmsg', r'^!part\s+([\S]+)', + self.connection.reactor.add_global_regex_handler(['pubmsg', 'privmsg'], r'^!part\s+([\S]+)', getattr(self, 'handle_part'), -20) - self.connection.reactor.add_global_regex_handler('pubmsg', r'^!quit\s*(.*)', - getattr(self, 'handle_quit'), -20) - self.connection.reactor.add_global_regex_handler('privmsg', r'^!quit\s*(.*)', + self.connection.reactor.add_global_regex_handler(['pubmsg', 'privmsg'], r'^!quit\s*(.*)', getattr(self, 'handle_quit'), -20) super(ChannelManagement, self).start() diff --git a/dr_botzo/markov/ircplugin.py b/dr_botzo/markov/ircplugin.py index 8bbe659..205c4e7 100644 --- a/dr_botzo/markov/ircplugin.py +++ b/dr_botzo/markov/ircplugin.py @@ -20,9 +20,7 @@ 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+(.*)$|$)', + self.connection.reactor.add_global_regex_handler(['pubmsg', 'privmsg'], r'^!markov\s+reply(\s+min=(\d+))?(\s+max=(\d+))?(\s+(.*)$|$)', getattr(self, 'handle_reply'), -20) super(Markov, self).start() diff --git a/dr_botzo/weather/ircplugin.py b/dr_botzo/weather/ircplugin.py index 7255e45..bd725f6 100644 --- a/dr_botzo/weather/ircplugin.py +++ b/dr_botzo/weather/ircplugin.py @@ -15,9 +15,7 @@ class Weather(Plugin): def start(self): """Set up the handlers.""" - self.connection.reactor.add_global_regex_handler('pubmsg', r'^!weather\s+(.*)$', - getattr(self, 'handle_weather'), -20) - self.connection.reactor.add_global_regex_handler('privmsg', r'^^!weather\s+(.*)$', + self.connection.reactor.add_global_regex_handler(['pubmsg', 'privmsg'], r'^!weather\s+(.*)$', getattr(self, 'handle_weather'), -20) super(Weather, self).start()