diff options
author | Tomás Touceda <chiiph@leap.se> | 2013-08-06 10:10:43 -0300 |
---|---|---|
committer | Tomás Touceda <chiiph@leap.se> | 2013-08-06 10:10:43 -0300 |
commit | 22e27a6f1ca4b5b53ac752e59ee7c21a1ed2d370 (patch) | |
tree | adc6823458f152b8ce1e55a1b5d2a1f1f245ad2b /mail/src/leap/mail/imap/fetch.py | |
parent | 1f300f31b50656a7b3259e92c54fc6885476690b (diff) | |
parent | bfe87a08e264db1485150a9850b0f5b5ede10236 (diff) |
Merge remote-tracking branch 'kali/feature/refactor_imap_fetch' into develop
Diffstat (limited to 'mail/src/leap/mail/imap/fetch.py')
-rw-r--r-- | mail/src/leap/mail/imap/fetch.py | 54 |
1 files changed, 45 insertions, 9 deletions
diff --git a/mail/src/leap/mail/imap/fetch.py b/mail/src/leap/mail/imap/fetch.py index 48a45e6..1c41813 100644 --- a/mail/src/leap/mail/imap/fetch.py +++ b/mail/src/leap/mail/imap/fetch.py @@ -1,15 +1,16 @@ import logging import json +import ssl from twisted.python import log from twisted.internet import defer +from twisted.internet.task import LoopingCall from twisted.internet.threads import deferToThread from leap.common.check import leap_assert, leap_assert_type +from leap.keymanager import openpgp from leap.soledad import Soledad -from leap.common.keymanager import openpgp - logger = logging.getLogger(__name__) @@ -26,7 +27,8 @@ class LeapIncomingMail(object): INCOMING_KEY = "incoming" CONTENT_KEY = "content" - def __init__(self, keymanager, soledad, imap_account): + def __init__(self, keymanager, soledad, imap_account, + check_period): """ Initialize LeapIMAP. @@ -39,10 +41,15 @@ class LeapIncomingMail(object): :param imap_account: the account to fetch periodically :type imap_account: SoledadBackedAccount + + :param check_period: the period to fetch new mail, in seconds. + :type check_period: int """ leap_assert(keymanager, "need a keymanager to initialize") leap_assert_type(soledad, Soledad) + leap_assert(check_period, "need a period to check incoming mail") + leap_assert_type(check_period, int) self._keymanager = keymanager self._soledad = soledad @@ -51,6 +58,16 @@ class LeapIncomingMail(object): self._pkey = self._keymanager.get_all_keys_in_local_db( private=True).pop() + self._loop = None + self._check_period = check_period + + self._create_soledad_indexes() + + def _create_soledad_indexes(self): + """ + Create needed indexes on soledad. + """ + self._soledad.create_index("just-mail", "incoming") def fetch(self): """ @@ -64,16 +81,33 @@ class LeapIncomingMail(object): d.addCallbacks(self._process_doclist, self._sync_soledad_err) return d + def start_loop(self): + """ + Starts a loop to fetch mail. + """ + self._loop = LoopingCall(self.fetch) + self._loop.start(self._check_period) + + def stop(self): + """ + Stops the loop that fetches mail. + """ + if self._loop: + self._loop.stop() + def _sync_soledad(self): log.msg('syncing soledad...') logger.debug('in soledad sync') - #import ipdb; ipdb.set_trace() - self._soledad.sync() - gen, doclist = self._soledad.get_all_docs() - #logger.debug("there are %s docs" % (len(doclist),)) - log.msg("there are %s docs" % (len(doclist),)) - return doclist + try: + self._soledad.sync() + doclist = self._soledad.get_from_index("just-mail", "*") + #log.msg("there are %s mails" % (len(doclist),)) + return doclist + except ssl.SSLError as exc: + logger.warning('SSL Error while syncing soledad: %r' % (exc,)) + except Exception as exc: + logger.warning('Error while syncing soledad: %r' % (exc,)) def _sync_soledad_err(self, f): log.err("error syncing soledad: %s" % (f.value,)) @@ -81,6 +115,8 @@ class LeapIncomingMail(object): def _process_doclist(self, doclist): log.msg('processing doclist') + if not doclist: + return for doc in doclist: keys = doc.content.keys() if self.ENC_SCHEME_KEY in keys and self.ENC_JSON_KEY in keys: |