summaryrefslogtreecommitdiff
path: root/src/leap/mail/imap
diff options
context:
space:
mode:
Diffstat (limited to 'src/leap/mail/imap')
-rw-r--r--src/leap/mail/imap/fetch.py54
-rw-r--r--src/leap/mail/imap/service/imap.py32
2 files changed, 55 insertions, 31 deletions
diff --git a/src/leap/mail/imap/fetch.py b/src/leap/mail/imap/fetch.py
index 48a45e6..1c41813 100644
--- a/src/leap/mail/imap/fetch.py
+++ b/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:
diff --git a/src/leap/mail/imap/service/imap.py b/src/leap/mail/imap/service/imap.py
index 49d54e3..9e331b6 100644
--- a/src/leap/mail/imap/service/imap.py
+++ b/src/leap/mail/imap/service/imap.py
@@ -20,15 +20,13 @@ Imap service initialization
import logging
logger = logging.getLogger(__name__)
-#from twisted.application import internet, service
from twisted.internet.protocol import ServerFactory
-from twisted.internet.task import LoopingCall
from twisted.mail import imap4
from twisted.python import log
from leap.common.check import leap_assert, leap_assert_type
-from leap.common.keymanager import KeyManager
+from leap.keymanager import KeyManager
from leap.mail.imap.server import SoledadBackedAccount
from leap.mail.imap.fetch import LeapIncomingMail
from leap.soledad import Soledad
@@ -36,8 +34,8 @@ from leap.soledad import Soledad
IMAP_PORT = 9930
# The default port in which imap service will run
-#INCOMING_CHECK_PERIOD = 10
-INCOMING_CHECK_PERIOD = 5
+# INCOMING_CHECK_PERIOD = 5
+INCOMING_CHECK_PERIOD = 60
# The period between succesive checks of the incoming mail
# queue (in seconds)
@@ -127,6 +125,9 @@ class LeapIMAPFactory(ServerFactory):
def run_service(*args, **kwargs):
"""
Main entry point to run the service from the client.
+
+ :returns: the LoopingCall instance that will have to be stoppped
+ before shutting down the client.
"""
leap_assert(len(args) == 2)
soledad, keymanager = args
@@ -139,29 +140,16 @@ def run_service(*args, **kwargs):
uuid = soledad._get_uuid()
factory = LeapIMAPFactory(uuid, soledad)
- # ---- for application framework
- #application = service.Application("LEAP IMAP4 Local Service")
- #imapService = internet.TCPServer(port, factory)
- #imapService.setServiceParent(application)
-
from twisted.internet import reactor
reactor.listenTCP(port, factory)
fetcher = LeapIncomingMail(
keymanager,
soledad,
- factory.theAccount)
+ factory.theAccount,
+ check_period)
- lc = LoopingCall(fetcher.fetch)
- lc.start(check_period)
+ fetcher.start_loop()
- # ---- for application framework
- #internet.TimerService(
- #check_period,
- #fetcher.fetch).setServiceParent(application)
-
- logger.debug('----------------------------------------')
logger.debug("IMAP4 Server is RUNNING in port %s" % (port,))
-
- #log.msg("IMAP4 Server is RUNNING in port %s" % (port,))
- #return application
+ return fetcher