ircbot: list of events to add_global_regex_handler

for convenience, pass a list of events to add_global_regex_handler if
you want to have multiple things fire the same handler. common case is
pubmsg and privmsg
This commit is contained in:
Brian S. Stephan 2015-05-15 22:08:00 -05:00
parent 6982e6d438
commit 134c02dc59
5 changed files with 16 additions and 24 deletions

View File

@ -43,12 +43,12 @@ class DrReactor(irc.client.Reactor):
on_schedule=on_schedule) on_schedule=on_schedule)
self.regex_handlers = {} 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. """Adds a global handler function for a specific event type and regex.
Arguments: Arguments:
event --- Event type (a string). events --- Event type(s) (a list of strings).
handler -- Callback function taking connection and event handler -- Callback function taking connection and event
parameters. parameters.
@ -67,11 +67,15 @@ class DrReactor(irc.client.Reactor):
work, though it turns out most modules probably want this one. work, though it turns out most modules probably want this one.
""" """
if type(events) != list:
events = [events]
handler = PrioritizedRegexHandler(priority, regex, handler) handler = PrioritizedRegexHandler(priority, regex, handler)
with self.mutex: for event in events:
log.debug(u"in add_global_regex_handler") with self.mutex:
event_regex_handlers = self.regex_handlers.setdefault(event, []) log.debug(u"in add_global_regex_handler")
bisect.insort(event_regex_handlers, handler) 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, event, handler):
"""Removes a global regex handler function. """Removes a global regex handler function.

View File

@ -13,9 +13,7 @@ class Echo(Plugin):
def start(self): def start(self):
"""Set up the handlers.""" """Set up the handlers."""
self.connection.reactor.add_global_regex_handler('pubmsg', r'^!echo\s+(.*)$', self.connection.reactor.add_global_regex_handler(['pubmsg', 'privmsg'], r'^!echo\s+(.*)$',
getattr(self, 'handle_echo'), -20)
self.connection.reactor.add_global_regex_handler('privmsg', r'^!echo\s+(.*)$',
getattr(self, 'handle_echo'), -20) getattr(self, 'handle_echo'), -20)
super(Echo, self).start() super(Echo, self).start()

View File

@ -14,19 +14,13 @@ class ChannelManagement(Plugin):
def start(self): def start(self):
"""Set up the handlers.""" """Set up the handlers."""
self.connection.reactor.add_global_regex_handler('pubmsg', 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('privmsg', r'^!join\s+([\S]+)',
getattr(self, 'handle_join'), -20) getattr(self, 'handle_join'), -20)
self.connection.reactor.add_global_regex_handler('pubmsg', 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('privmsg', r'^!part\s+([\S]+)',
getattr(self, 'handle_part'), -20) getattr(self, 'handle_part'), -20)
self.connection.reactor.add_global_regex_handler('pubmsg', r'^!quit\s*(.*)', self.connection.reactor.add_global_regex_handler(['pubmsg', 'privmsg'], r'^!quit\s*(.*)',
getattr(self, 'handle_quit'), -20)
self.connection.reactor.add_global_regex_handler('privmsg', r'^!quit\s*(.*)',
getattr(self, 'handle_quit'), -20) getattr(self, 'handle_quit'), -20)
super(ChannelManagement, self).start() super(ChannelManagement, self).start()

View File

@ -20,9 +20,7 @@ class Markov(Plugin):
self.connection.add_global_handler('pubmsg', getattr(self, 'handle_chatter'), -20) self.connection.add_global_handler('pubmsg', getattr(self, 'handle_chatter'), -20)
self.connection.add_global_handler('privmsg', 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+(.*)$|$)', 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)
self.connection.reactor.add_global_regex_handler('privmsg', r'^!markov\s+reply(\s+min=(\d+))?(\s+max=(\d+))?(\s+(.*)$|$)',
getattr(self, 'handle_reply'), -20) getattr(self, 'handle_reply'), -20)
super(Markov, self).start() super(Markov, self).start()

View File

@ -15,9 +15,7 @@ class Weather(Plugin):
def start(self): def start(self):
"""Set up the handlers.""" """Set up the handlers."""
self.connection.reactor.add_global_regex_handler('pubmsg', r'^!weather\s+(.*)$', self.connection.reactor.add_global_regex_handler(['pubmsg', 'privmsg'], r'^!weather\s+(.*)$',
getattr(self, 'handle_weather'), -20)
self.connection.reactor.add_global_regex_handler('privmsg', r'^^!weather\s+(.*)$',
getattr(self, 'handle_weather'), -20) getattr(self, 'handle_weather'), -20)
super(Weather, self).start() super(Weather, self).start()