diff options
author | Tomás Touceda <chiiph@leap.se> | 2013-05-21 10:00:19 -0300 |
---|---|---|
committer | Tomás Touceda <chiiph@leap.se> | 2013-05-21 10:00:19 -0300 |
commit | e5fa0790f5231c333aba4bc5f6766556e062aa6c (patch) | |
tree | 6ffa31245438320da2934fda9aeb4f8248f0e3a7 /src/leap/mail/imap/fetch.py | |
parent | d24b8590ff06708032c3a7a5856b728d3fc48c9a (diff) | |
parent | ffedb3b8e32c062604cc2f178213c82d90843788 (diff) |
Merge remote-tracking branch 'kali/feature/imap-service2' into develop
Conflicts:
pkg/requirements.pip
src/leap/mail/imap/tests/test_imap.py
Diffstat (limited to 'src/leap/mail/imap/fetch.py')
-rw-r--r-- | src/leap/mail/imap/fetch.py | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/src/leap/mail/imap/fetch.py b/src/leap/mail/imap/fetch.py new file mode 100644 index 0000000..60ae387 --- /dev/null +++ b/src/leap/mail/imap/fetch.py @@ -0,0 +1,89 @@ +import json + +from twisted.python import log + +from leap.common.check import leap_assert, leap_assert_type +from leap.soledad import Soledad + +from leap.common.keymanager import openpgp + + +class LeapIncomingMail(object): + """ + Fetches mail from the incoming queue. + """ + def __init__(self, keymanager, soledad, imap_account): + + """ + Initialize LeapIMAP. + + :param keymanager: a keymanager instance + :type keymanager: keymanager.KeyManager + + :param soledad: a soledad instance + :type soledad: Soledad + + :param imap_account: the account to fetch periodically + :type imap_account: SoledadBackedAccount + """ + + leap_assert(keymanager, "need a keymanager to initialize") + leap_assert_type(soledad, Soledad) + + self._keymanager = keymanager + self._soledad = soledad + self.imapAccount = imap_account + + self._pkey = self._keymanager.get_all_keys_in_local_db( + private=True).pop() + + def fetch(self): + """ + Get new mail by syncing database, store it in the INBOX for the + user account, and remove from the incoming db. + """ + self._soledad.sync() + gen, doclist = self._soledad.get_all_docs() + #log.msg("there are %s docs" % (len(doclist),)) + + if doclist: + inbox = self.imapAccount.getMailbox('inbox') + + key = self._pkey + for doc in doclist: + keys = doc.content.keys() + if '_enc_scheme' in keys and '_enc_json' in keys: + + # XXX should check for _enc_scheme == "pubkey" || "none" + # that is what incoming mail uses. + + encdata = doc.content['_enc_json'] + decrdata = openpgp.decrypt_asym( + encdata, key, + # XXX get from public method instead + passphrase=self._soledad._passphrase) + if decrdata: + self.process_decrypted(doc, decrdata, inbox) + # XXX launch sync callback / defer + + def process_decrypted(self, doc, data, inbox): + """ + Process a successfully decrypted message + """ + log.msg("processing incoming message!") + msg = json.loads(data) + if not isinstance(msg, dict): + return False + if not msg.get('incoming', False): + return False + # ok, this is an incoming message + rawmsg = msg.get('content', None) + if not rawmsg: + return False + #log.msg("we got raw message") + + # add to inbox and delete from soledad + inbox.addMessage(rawmsg, ("\\Recent",)) + doc_id = doc.doc_id + self._soledad.delete_doc(doc) + log.msg("deleted doc %s from incoming" % doc_id) |