diff options
author | Tomás Touceda <chiiph@leap.se> | 2013-11-05 14:47:13 -0300 |
---|---|---|
committer | Tomás Touceda <chiiph@leap.se> | 2013-11-07 10:40:30 -0300 |
commit | e108a2ffec444c09b3661379a1051fda1f9952cf (patch) | |
tree | 919811b5c02ba3c16475e6328516b9e3402ef31a /src/leap/mx | |
parent | 440a79fb330ffda37ed987cdc7e74333ed99266f (diff) |
Use inline defers for more readable code
Diffstat (limited to 'src/leap/mx')
-rw-r--r-- | src/leap/mx/mail_receiver.py | 57 |
1 files changed, 28 insertions, 29 deletions
diff --git a/src/leap/mx/mail_receiver.py b/src/leap/mx/mail_receiver.py index 8fcadce..741b99d 100644 --- a/src/leap/mx/mail_receiver.py +++ b/src/leap/mx/mail_receiver.py @@ -35,7 +35,7 @@ except ImportError: from email import message_from_string from twisted.application.service import Service -from twisted.internet import inotify +from twisted.internet import inotify, defer from twisted.python import filepath, log from leap.common.mail import get_email_charset @@ -91,33 +91,30 @@ class MailReceiver(Service): callbacks=[self._process_incoming_email], recursive=recursive) - def _encrypt_message(self, pubkey, uuid, message): + def _encrypt_message(self, pubkey, message): """ - Given a UUID, a public key and a message, it encrypts the - message to that public key. + Given a public key and a message, it encrypts the message to + that public key. The address is needed in order to build the OpenPGPKey object. - :param uuid_pubkey: tuple that holds the uuid and the public - key as it is returned by the previous call in the - chain - :type uuid_pubkey: tuple (str, str) + :param pubkey: public key for the owner of the message + :type pubkey: str :param message: message contents :type message: str - :return: uuid, doc to sync with Soledad or None, None if - something went wrong. - :rtype: tuple(str, SoledadDocument) + :return: doc to sync with Soledad or None, None if something + went wrong. + :rtype: SoledadDocument """ - if uuid is None or pubkey is None or len(pubkey) == 0: + if pubkey is None or len(pubkey) == 0: log.msg("_encrypt_message: Something went wrong, here's all " - "I know: %r | %r" % (uuid, pubkey)) + "I know: %r" % (pubkey,)) return None, None - log.msg("Encrypting message to %s's pubkey" % (uuid,)) - doc = SoledadDocument(doc_id=str(pyuuid.uuid4())) - encoding = get_email_charset(message, default=None) + encoding = get_email_charset(message.decode("utf8", "replace"), + default=None) if encoding is None: result = chardet.detect(message) encoding = result["encoding"] @@ -130,7 +127,7 @@ class MailReceiver(Service): ENC_SCHEME_KEY: EncryptionSchemes.NONE, ENC_JSON_KEY: json.dumps(data, encoding=encoding) } - return uuid, doc + return doc openpgp_key = None with openpgp.TempGPGWrapper(gpgbinary='/usr/bin/gpg') as gpg: @@ -149,22 +146,23 @@ class MailReceiver(Service): symmetric=False)) } - return uuid, doc + return doc - def _export_message(self, uuid_doc): + def _export_message(self, uuid, doc): """ Given a UUID and a SoledadDocument, it saves it directly in the couchdb that serves as a backend for Soledad, in a db accessible to the recipient of the mail. - :param uuid_doc: tuple that holds the UUID and SoledadDocument - :type uuid_doc: tuple(str, SoledadDocument) + :param uuid: the mail owner's uuid + :type uuid: str + :param doc: SoledadDocument that represents the email + :type doc: SoledadDocument :return: True if it's ok to remove the message, False otherwise :rtype: bool """ - uuid, doc = uuid_doc if uuid is None or doc is None: log.msg("_export_message: Something went wrong, here's all " "I know: %r | %r" % (uuid, doc)) @@ -222,6 +220,7 @@ class MailReceiver(Service): return uuid + @defer.inlineCallbacks def _process_incoming_email(self, otherself, filepath, mask): """ Callback that processes incoming email. @@ -252,12 +251,12 @@ class MailReceiver(Service): log.msg("BUG: There was no uuid!") return - d = self._users_cdb.getPubKey(uuid) - d.addCallbacks(self._encrypt_message, log.err, - (uuid, mail_data)) - d.addCallbacks(self._export_message, log.err) - d.addCallbacks(self._conditional_remove, log.err, - (filepath,)) - d.addErrback(log.err) + pubkey = yield self._users_cdb.getPubKey(uuid) + + log.msg("Encrypting message to %s's pubkey" % (uuid,)) + doc = yield self._encrypt_message(pubkey, mail_data) + + do_remove = yield self._export_message(uuid, doc) + yield self._conditional_remove(do_remove, filepath) except Exception: log.err() |