From 452f62ecf41f416c7a3af33f8e73e306cdafd1c4 Mon Sep 17 00:00:00 2001 From: mathieui Date: Mon, 7 Nov 2011 13:06:57 +0100 Subject: [PATCH 1/2] Actually add the events.py file --- src/events.py | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 src/events.py diff --git a/src/events.py b/src/events.py new file mode 100644 index 00000000..2363eca8 --- /dev/null +++ b/src/events.py @@ -0,0 +1,70 @@ +# +# 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 = { + # when you are highlighted in a muc tab + 'highlight': [], + 'muc_say': [], + 'conversation_say': [], + 'private_say': [], + } + + def add_event_handler(self, name, callback, first=True, last=False, position=None): + """ + 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 + """ + if name not in self.events: + return False + + if first: + self.events[name].insert(0, callback) + elif last: + self.events[name].append(callback) + elif position != None and isinstance(position, int): + self.events[name].insert(position, callback) + else: + self.events[name].insert(0, callback) + + return True + + def trigger(self, name, *args, **kwargs): + """ + Call all the callbacks associated to the given event name + """ + callbacks = self.events[name] + 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) + From 1f3fafe15569c50fc877b5b24209e1d1214ae97a Mon Sep 17 00:00:00 2001 From: mathieui Date: Mon, 7 Nov 2011 14:45:08 +0100 Subject: [PATCH 2/2] Add somme triggers --- src/core.py | 3 +++ src/events.py | 3 +++ 2 files changed, 6 insertions(+) diff --git a/src/core.py b/src/core.py index 8e85ce90..d19443a8 100644 --- a/src/core.py +++ b/src/core.py @@ -555,6 +555,7 @@ class Core(object): tab = self.open_private_window(room_from, nick_from, False) if not tab: return + self.events.trigger('private_msg', message) body = xhtml.get_body_from_message_stanza(message) if not body: return @@ -604,6 +605,7 @@ class Core(object): When receiving "normal" messages (from someone in our roster) """ jid = message['from'] + self.events.trigger('conversation_msg', message) body = xhtml.get_body_from_message_stanza(message) if not body: if message['type'] == 'error': @@ -1044,6 +1046,7 @@ class Core(object): if tab.get_user_by_name(nick_from) and\ tab.get_user_by_name(nick_from) in tab.ignores: return + self.events.trigger('muc_mg', message) body = xhtml.get_body_from_message_stanza(message) if body: date = date if delayed == True else None diff --git a/src/events.py b/src/events.py index 2363eca8..d4fc8fc6 100644 --- a/src/events.py +++ b/src/events.py @@ -25,6 +25,9 @@ class EventHandler(object): 'muc_say': [], 'conversation_say': [], 'private_say': [], + 'conversation_msg': [], + 'private_msg': [], + 'muc_msg': [], } def add_event_handler(self, name, callback, first=True, last=False, position=None):