From 903a8f5c80caea6df5dd413a3589b3bf498339ae Mon Sep 17 00:00:00 2001 From: "Brian S. Stephan" Date: Fri, 15 May 2015 22:15:43 -0500 Subject: [PATCH] ircbot: list to remove_global_regex_handler same as previous commit, inverse of add_, for convenience --- dr_botzo/ircbot/bot.py | 24 +++++++++++++++--------- dr_botzo/ircbot/ircplugins/echo.py | 3 +-- dr_botzo/ircbot/ircplugins/ircmgmt.py | 13 +++---------- dr_botzo/markov/ircplugin.py | 3 +-- dr_botzo/weather/ircplugin.py | 3 +-- 5 files changed, 21 insertions(+), 25 deletions(-) diff --git a/dr_botzo/ircbot/bot.py b/dr_botzo/ircbot/bot.py index b8a0ed0..72c5ce8 100644 --- a/dr_botzo/ircbot/bot.py +++ b/dr_botzo/ircbot/bot.py @@ -77,24 +77,30 @@ class DrReactor(irc.client.Reactor): event_regex_handlers = self.regex_handlers.setdefault(event, []) bisect.insort(event_regex_handlers, handler) - def remove_global_regex_handler(self, event, handler): + def remove_global_regex_handler(self, events, handler): """Removes a global regex handler function. Arguments: - event -- Event type (a string). + events -- Event type(s) (a list of strings). handler -- Callback function. Returns 1 on success, otherwise 0. """ - with self.mutex: - if not event in self.regex_handlers: - return 0 - for h in self.regex_handlers[event]: - if handler == h.callback: - self.regex_handlers[event].remove(h) - return 1 + ret = 1 + + if type(events) != list: + events = [events] + + for event in events: + with self.mutex: + if not event in self.regex_handlers: + ret = 0 + for h in self.regex_handlers[event]: + if handler == h.callback: + self.regex_handlers[event].remove(h) + return ret def _handle_event(self, connection, event): """Handle an Event event incoming on ServerConnection connection. diff --git a/dr_botzo/ircbot/ircplugins/echo.py b/dr_botzo/ircbot/ircplugins/echo.py index d1fd766..de3312e 100644 --- a/dr_botzo/ircbot/ircplugins/echo.py +++ b/dr_botzo/ircbot/ircplugins/echo.py @@ -21,8 +21,7 @@ class Echo(Plugin): def stop(self): """Tear down handlers.""" - self.connection.reactor.remove_global_regex_handler('pubmsg', getattr(self, 'handle_echo')) - self.connection.reactor.remove_global_regex_handler('privmsg', getattr(self, 'handle_echo')) + self.connection.reactor.remove_global_regex_handler(['pubmsg', 'privmsg'], getattr(self, 'handle_echo')) super(Echo, self).stop() diff --git a/dr_botzo/ircbot/ircplugins/ircmgmt.py b/dr_botzo/ircbot/ircplugins/ircmgmt.py index ff3b9a8..3c045cc 100644 --- a/dr_botzo/ircbot/ircplugins/ircmgmt.py +++ b/dr_botzo/ircbot/ircplugins/ircmgmt.py @@ -16,10 +16,8 @@ class ChannelManagement(Plugin): 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', 'privmsg'], r'^!part\s+([\S]+)', getattr(self, 'handle_part'), -20) - self.connection.reactor.add_global_regex_handler(['pubmsg', 'privmsg'], r'^!quit\s*(.*)', getattr(self, 'handle_quit'), -20) @@ -28,14 +26,9 @@ class ChannelManagement(Plugin): def stop(self): """Tear down handlers.""" - self.connection.reactor.remove_global_regex_handler('pubmsg', getattr(self, 'handle_join')) - self.connection.reactor.remove_global_regex_handler('privmsg', getattr(self, 'handle_join')) - - self.connection.reactor.remove_global_regex_handler('pubmsg', getattr(self, 'handle_part')) - self.connection.reactor.remove_global_regex_handler('privmsg', getattr(self, 'handle_part')) - - self.connection.reactor.remove_global_regex_handler('pubmsg', getattr(self, 'handle_quit')) - self.connection.reactor.remove_global_regex_handler('privmsg', getattr(self, 'handle_quit')) + self.connection.reactor.remove_global_regex_handler(['pubmsg', 'privmsg'], getattr(self, 'handle_join')) + self.connection.reactor.remove_global_regex_handler(['pubmsg', 'privmsg'], getattr(self, 'handle_part')) + self.connection.reactor.remove_global_regex_handler(['pubmsg', 'privmsg'], getattr(self, 'handle_quit')) super(ChannelManagement, self).stop() diff --git a/dr_botzo/markov/ircplugin.py b/dr_botzo/markov/ircplugin.py index 205c4e7..192ffe7 100644 --- a/dr_botzo/markov/ircplugin.py +++ b/dr_botzo/markov/ircplugin.py @@ -31,8 +31,7 @@ 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')) + self.connection.reactor.remove_global_regex_handler(['pubmsg', 'privmsg'], getattr(self, 'handle_reply')) super(Markov, self).stop() diff --git a/dr_botzo/weather/ircplugin.py b/dr_botzo/weather/ircplugin.py index bd725f6..77be484 100644 --- a/dr_botzo/weather/ircplugin.py +++ b/dr_botzo/weather/ircplugin.py @@ -23,8 +23,7 @@ class Weather(Plugin): def stop(self): """Tear down handlers.""" - self.connection.reactor.remove_global_regex_handler('pubmsg', getattr(self, 'handle_weather')) - self.connection.reactor.remove_global_regex_handler('privmsg', getattr(self, 'handle_weather')) + self.connection.reactor.remove_global_regex_handler(['pubmsg', 'privmsg'], getattr(self, 'handle_weather')) super(Weather, self).stop()