2011-11-07 12:06:57 +00:00
|
|
|
|
#
|
|
|
|
|
# This file is part of Poezio.
|
|
|
|
|
#
|
|
|
|
|
# Poezio is free software: you can redistribute it and/or modify
|
|
|
|
|
# it under the terms of the zlib license. See the COPYING file.
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
Defines the EventHandler class
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
import logging
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
class EventHandler(object):
|
|
|
|
|
"""
|
|
|
|
|
A class keeping a list of possible events that are triggered
|
|
|
|
|
by poezio. You (a plugin for example) can add an event handler
|
|
|
|
|
associated with an event name, and whenever that event is triggered,
|
|
|
|
|
the callback is called
|
|
|
|
|
"""
|
|
|
|
|
def __init__(self):
|
|
|
|
|
self.events = {
|
|
|
|
|
'highlight': [],
|
|
|
|
|
'muc_say': [],
|
2011-11-12 02:42:07 +00:00
|
|
|
|
'muc_say_after': [],
|
2011-11-07 12:06:57 +00:00
|
|
|
|
'conversation_say': [],
|
2011-11-12 02:42:07 +00:00
|
|
|
|
'conversation_say_after': [],
|
2011-11-07 12:06:57 +00:00
|
|
|
|
'private_say': [],
|
2011-11-12 02:42:07 +00:00
|
|
|
|
'private_say_after': [],
|
2011-11-07 13:45:08 +00:00
|
|
|
|
'conversation_msg': [],
|
|
|
|
|
'private_msg': [],
|
|
|
|
|
'muc_msg': [],
|
2011-11-09 22:17:00 +00:00
|
|
|
|
'normal_chatstate': [],
|
2011-11-10 10:45:23 +00:00
|
|
|
|
'muc_chatstate': [],
|
|
|
|
|
'private_chatstate': [],
|
2011-11-09 22:17:00 +00:00
|
|
|
|
'normal_presence': [],
|
|
|
|
|
'muc_presence': [],
|
2012-02-16 01:24:54 +00:00
|
|
|
|
'muc_join': [],
|
|
|
|
|
'muc_kick': [],
|
|
|
|
|
'muc_nickchange': [],
|
|
|
|
|
'muc_ban': [],
|
2011-11-12 01:40:24 +00:00
|
|
|
|
'send_normal_presence': [],
|
2012-05-12 18:44:38 +00:00
|
|
|
|
'ignored_private': [],
|
2011-11-07 12:06:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-11-07 14:09:39 +00:00
|
|
|
|
def add_event_handler(self, name, callback, position=0):
|
2011-11-07 12:06:57 +00:00
|
|
|
|
"""
|
|
|
|
|
Add a callback to a given event.
|
|
|
|
|
Note that if that event name doesn’t exist, it just returns False.
|
|
|
|
|
If it was successfully added, it returns True
|
2011-11-09 17:38:56 +00:00
|
|
|
|
position: 0 means insert at the beginning, -1 means end
|
2011-11-07 12:06:57 +00:00
|
|
|
|
"""
|
|
|
|
|
if name not in self.events:
|
|
|
|
|
return False
|
|
|
|
|
|
2011-11-07 14:09:39 +00:00
|
|
|
|
if position >= 0:
|
2011-11-07 12:06:57 +00:00
|
|
|
|
self.events[name].insert(position, callback)
|
|
|
|
|
else:
|
2011-11-07 14:09:39 +00:00
|
|
|
|
self.events[name].append(callback)
|
2011-11-07 12:06:57 +00:00
|
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
def trigger(self, name, *args, **kwargs):
|
|
|
|
|
"""
|
2011-11-09 17:38:56 +00:00
|
|
|
|
Call all the callbacks associated to the given event name.
|
2011-11-07 12:06:57 +00:00
|
|
|
|
"""
|
2012-05-12 18:44:38 +00:00
|
|
|
|
callbacks = self.events.get(name, None)
|
|
|
|
|
if callbacks is None:
|
|
|
|
|
log.debug('%s: No such event.', name)
|
|
|
|
|
return
|
2011-11-07 12:06:57 +00:00
|
|
|
|
for callback in callbacks:
|
|
|
|
|
callback(*args, **kwargs)
|
|
|
|
|
|
|
|
|
|
def del_event_handler(self, name, callback):
|
|
|
|
|
"""
|
|
|
|
|
Remove the callback from the list of callbacks of the given event
|
|
|
|
|
"""
|
|
|
|
|
if not name:
|
|
|
|
|
for event in self.events:
|
|
|
|
|
while callback in self.events[event]:
|
|
|
|
|
self.events[event].remove(callback)
|
|
|
|
|
return True
|
|
|
|
|
else:
|
|
|
|
|
if callback in self.events[name]:
|
|
|
|
|
self.events[name].remove(callback)
|
|
|
|
|
|