summaryrefslogtreecommitdiff
path: root/service/pixelated/bitmask_libraries/session.py
diff options
context:
space:
mode:
Diffstat (limited to 'service/pixelated/bitmask_libraries/session.py')
-rw-r--r--service/pixelated/bitmask_libraries/session.py73
1 files changed, 42 insertions, 31 deletions
diff --git a/service/pixelated/bitmask_libraries/session.py b/service/pixelated/bitmask_libraries/session.py
index a9cb15f2..7abe2a63 100644
--- a/service/pixelated/bitmask_libraries/session.py
+++ b/service/pixelated/bitmask_libraries/session.py
@@ -18,16 +18,15 @@ import traceback
import sys
import os
-from leap.mail.imap.fetch import LeapIncomingMail
-from leap.mail.imap.account import SoledadBackedAccount
-from leap.mail.imap.memorystore import MemoryStore
-from leap.mail.imap.soledadstore import SoledadStore
+from leap.mail.incoming.service import IncomingMail
from twisted.internet import reactor
from .nicknym import NickNym
from leap.auth import SRPAuth
+from pixelated.adapter.mailstore import LeapMailStore
from .soledad import SoledadSessionFactory
from .smtp import LeapSmtp
-
+from leap.mail.imap.account import IMAPAccount
+from twisted.internet import defer
SESSIONS = {}
@@ -47,48 +46,66 @@ class LeapSession(object):
- ``user_auth`` the secure remote password session data after authenticating with LEAP. See http://en.wikipedia.org/wiki/Secure_Remote_Password_protocol (SRPSession)
+ - ``mail_store`` the MailStore to access the users mails
+
- ``soledad_session`` the soledad session. See https://leap.se/soledad (LeapSecureRemotePassword)
- ``nicknym`` the nicknym instance. See https://leap.se/nicknym (NickNym)
- - ``account`` the actual leap mail account. Implements Twisted imap4.IAccount and imap4.INamespacePresenter (SoledadBackedAccount)
-
- ``incoming_mail_fetcher`` Background job for fetching incoming mails from LEAP server (LeapIncomingMail)
"""
- def __init__(self, provider, user_auth, soledad_session, nicknym, soledad_account, incoming_mail_fetcher, smtp):
+ def __init__(self, provider, user_auth, mail_store, soledad_session, nicknym, smtp):
self.smtp = smtp
self.config = provider.config
self.provider = provider
self.user_auth = user_auth
+ self.mail_store = mail_store
self.soledad_session = soledad_session
self.nicknym = nicknym
- self.account = soledad_account
- self.incoming_mail_fetcher = incoming_mail_fetcher
- self.soledad_session.soledad.sync(defer_decryption=False)
- self.nicknym.generate_openpgp_key()
- if self.config.start_background_jobs:
- self.start_background_jobs()
+ @defer.inlineCallbacks
+ def initial_sync(self):
+ yield self.sync()
+ yield self.after_first_sync()
+ defer.returnValue(self)
+
+ @defer.inlineCallbacks
+ def after_first_sync(self):
+ yield self.nicknym.generate_openpgp_key()
+ self.account = self._create_account(self.account_email, self.soledad_session)
+ self.incoming_mail_fetcher = yield self._create_incoming_mail_fetcher(
+ self.nicknym,
+ self.soledad_session,
+ self.account,
+ self.account_email())
+ reactor.callFromThread(self.incoming_mail_fetcher.startService)
+
+ def _create_account(self, user_mail, soledad_session):
+ account = IMAPAccount(user_mail, soledad_session.soledad)
+ return account
def account_email(self):
name = self.user_auth.username
return self.provider.address_for(name)
def close(self):
- self.stop_background_jobs()
+ self.stop_background_jobs
- def start_background_jobs(self):
- self.smtp.ensure_running()
- reactor.callFromThread(self.incoming_mail_fetcher.start_loop)
+ @defer.inlineCallbacks
+ def _create_incoming_mail_fetcher(self, nicknym, soledad_session, account, user_mail):
+ inbox = yield account.callWhenReady(lambda _: account.getMailbox('INBOX'))
+ defer.returnValue(IncomingMail(nicknym.keymanager,
+ soledad_session.soledad,
+ inbox.collection,
+ user_mail))
def stop_background_jobs(self):
- self.smtp.stop()
- reactor.callFromThread(self.incoming_mail_fetcher.stop)
+ reactor.callFromThread(self.incoming_mail_fetcher.stopService)
def sync(self):
try:
- self.soledad_session.sync()
+ return self.soledad_session.sync()
except:
traceback.print_exc(file=sys.stderr)
raise
@@ -117,14 +134,13 @@ class LeapSessionFactory(object):
account_email = self._provider.address_for(username)
soledad = SoledadSessionFactory.create(self._provider, auth.token, auth.uuid, password)
+ mail_store = LeapMailStore(soledad.soledad)
nicknym = self._create_nicknym(account_email, auth.token, auth.uuid, soledad)
- account = self._create_account(auth.uuid, soledad)
- incoming_mail_fetcher = self._create_incoming_mail_fetcher(nicknym, soledad, account, account_email)
smtp = LeapSmtp(self._provider, auth, nicknym.keymanager)
- return LeapSession(self._provider, auth, soledad, nicknym, account, incoming_mail_fetcher, smtp)
+ return LeapSession(self._provider, auth, mail_store, soledad, nicknym, smtp)
def _lookup_session(self, key):
global SESSIONS
@@ -152,10 +168,5 @@ class LeapSessionFactory(object):
def _create_nicknym(self, email_address, token, uuid, soledad_session):
return NickNym(self._provider, self._config, soledad_session, email_address, token, uuid)
- def _create_account(self, uuid, soledad_session):
- memstore = MemoryStore(permanent_store=SoledadStore(soledad_session.soledad))
- return SoledadBackedAccount(uuid, soledad_session.soledad, memstore)
-
- def _create_incoming_mail_fetcher(self, nicknym, soledad_session, account, email_address):
- return LeapIncomingMail(nicknym.keymanager, soledad_session.soledad, account,
- self._config.fetch_interval_in_s, email_address)
+ # memstore = MemoryStore(permanent_store=SoledadStore(soledad_session.soledad))
+ # return SoledadBackedAccount(uuid, soledad_session.soledad, memstore)