summaryrefslogtreecommitdiff
path: root/mail
diff options
context:
space:
mode:
authorKali Kaneko <kali@leap.se>2013-08-06 01:37:10 +0200
committerKali Kaneko <kali@leap.se>2013-08-06 01:50:49 +0200
commitbfe87a08e264db1485150a9850b0f5b5ede10236 (patch)
treeadc6823458f152b8ce1e55a1b5d2a1f1f245ad2b /mail
parentdeab7f6ce43a638638fc306e37c23a2d2787bf9d (diff)
refactor recurring fetch
Diffstat (limited to 'mail')
-rw-r--r--mail/src/leap/mail/imap/fetch.py38
-rw-r--r--mail/src/leap/mail/imap/service/imap.py16
2 files changed, 40 insertions, 14 deletions
diff --git a/mail/src/leap/mail/imap/fetch.py b/mail/src/leap/mail/imap/fetch.py
index 566873be..1c418130 100644
--- a/mail/src/leap/mail/imap/fetch.py
+++ b/mail/src/leap/mail/imap/fetch.py
@@ -4,6 +4,7 @@ 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
@@ -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,15 +81,28 @@ 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')
try:
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),))
+ 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,))
diff --git a/mail/src/leap/mail/imap/service/imap.py b/mail/src/leap/mail/imap/service/imap.py
index 6a8d37fe..9e331b69 100644
--- a/mail/src/leap/mail/imap/service/imap.py
+++ b/mail/src/leap/mail/imap/service/imap.py
@@ -20,9 +20,7 @@ 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
@@ -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)
@@ -148,12 +146,10 @@ def run_service(*args, **kwargs):
fetcher = LeapIncomingMail(
keymanager,
soledad,
- factory.theAccount)
+ factory.theAccount,
+ check_period)
- lc = LoopingCall(fetcher.fetch)
- lc.start(check_period)
+ fetcher.start_loop()
logger.debug("IMAP4 Server is RUNNING in port %s" % (port,))
-
- # XXX maybe return both fetcher and lc??
- return lc
+ return fetcher