From 87c15e3ec5c1ca607890bf72b0d90bb0f114cbb1 Mon Sep 17 00:00:00 2001 From: Ruben Pollan Date: Wed, 18 Feb 2015 10:24:03 -0600 Subject: Add callbacks to inbox insertions --- src/leap/mail/incoming/service.py | 38 ++++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 10 deletions(-) (limited to 'src/leap/mail/incoming/service.py') diff --git a/src/leap/mail/incoming/service.py b/src/leap/mail/incoming/service.py index 2902141..fadfd9f 100644 --- a/src/leap/mail/incoming/service.py +++ b/src/leap/mail/incoming/service.py @@ -135,12 +135,26 @@ class IncomingMail(Service): self._inbox = inbox self._userid = userid + self._listeners = [] self._loop = None self._check_period = check_period # initialize a mail parser only once self._parser = Parser() + def add_listener(self, listener): + """ + Add a listener to inbox insertions. + + This listener function will be called for each message added to the + inbox with its uid as parameter. This function should not be blocking + or it will block the incoming queue. + + :param listener: the listener function + :type listener: callable + """ + self._listeners.append(listener) + # # Public API: fetch, start_loop, stop. # @@ -699,17 +713,21 @@ class IncomingMail(Service): doc, data = msgtuple log.msg('adding message %s to local db' % (doc.doc_id,)) - #if isinstance(data, list): - #if empty(data): - #return False - #data = data[0] - def msgSavedCallback(result): - if not empty(result): - leap_events.signal(IMAP_MSG_SAVED_LOCALLY) - return self._delete_incoming_message(doc) - # TODO add notification as a callback - #leap_events.signal(IMAP_MSG_DELETED_INCOMING) + if empty(result): + return + + for listener in self._listeners: + listener(result) + + def signal_deleted(doc_id): + leap_events.signal(IMAP_MSG_DELETED_INCOMING) + return doc_id + + leap_events.signal(IMAP_MSG_SAVED_LOCALLY) + d = self._delete_incoming_message(doc) + d.addCallback(signal_deleted) + return d d = self._inbox.addMessage(data, (self.RECENT_FLAG,)) d.addCallbacks(msgSavedCallback, self._errback) -- cgit v1.2.3