def _step_process_mail_backend(self, filepath): """ Processes the email pointed by filepath in an async fashion. yield this method in another inlineCallbacks method or return it for it to be run. :param filepath: Path of the file that changed :type filepath: twisted.python.filepath.FilePath """ log.msg("Processing new mail at %r" % (filepath.path,)) with filepath.open("r") as f: mail_data = f.read() msg = message_from_string(mail_data) uuid = self._get_owner(msg) if uuid is None: log.msg("Don't know how to deliver mail %r, skipping..." % (filepath.path,)) defer.returnValue(None) log.msg("Mail owner: %s" % (uuid,)) if uuid is None: log.msg("BUG: There was no uuid!") defer.returnValue(None) pubkey = yield self._users_cdb.getPubKey(uuid) if pubkey is None or len(pubkey) == 0: log.msg("No public key, stopping the processing chain") defer.returnValue(None) log.msg("Encrypting message to %s's pubkey" % (uuid,)) doc = yield self._encrypt_message(pubkey, msg) do_remove = yield self._export_message(uuid, doc) yield self._conditional_remove(do_remove, filepath)
def _process_incoming_email(users_db, mail_couchdb_url_prefix, self, filepath, mask): if os.path.split(filepath.dirname())[-1] == "new": logger.debug("Processing new mail at %s" % (filepath.path,)) with filepath.open("r") as f: mail_data = f.read() mail = message_from_string(mail_data) owner = mail["To"] if owner is None: # default to Delivered-To owner = mail["Delivered-To"] owner = owner.split("@")[0] owner = owner.split("+")[0] logger.debug("Mail owner: %s" % (owner,)) logger.debug("%s received a new mail" % (owner,)) d = users_db.queryByLoginOrAlias(owner) d.addCallback(_get_pubkey, (users_db)) d.addCallback(_encrypt_message, (owner, mail_data)) d.addCallback(_export_message, (mail_couchdb_url_prefix)) d.addCallback(_conditional_remove, (filepath))
def _step_process_mail_backend(self, filepath): """ Processes the email pointed by filepath in an async fashion. yield this method in another inlineCallbacks method or return it for it to be run. :param filepath: Path of the file that changed :type filepath: twisted.python.filepath.FilePath """ log.msg("Processing new mail at %r" % (filepath.path,)) with filepath.open("r") as f: mail_data = f.read() msg = message_from_string(mail_data) uuid = self._get_owner(msg) if uuid is None: log.msg("Don't know how to deliver mail %r, skipping..." % (filepath.path,)) bounce_reason = "Missing UUID: There was a problem " \ "locating the user in our database." yield self._bounce_message(msg, filepath, bounce_reason) defer.returnValue(None) log.msg("Mail owner: %s" % (uuid,)) if uuid is None: log.msg("BUG: There was no uuid!") defer.returnValue(None) pubkey = yield self._users_cdb.getPubkey(uuid) if pubkey is None or len(pubkey) == 0: log.msg( "No public key for %s, stopping the processing chain." % uuid) bounce_reason = "Missing PGP public key: There was a " \ "problem locating the user's public key in " \ "our database." yield self._bounce_message(msg, filepath, bounce_reason) defer.returnValue(None) log.msg("Encrypting message to %s's pubkey" % (uuid,)) doc = yield self._encrypt_message(pubkey, msg) do_remove = yield self._export_message(uuid, doc) yield self._conditional_remove(do_remove, filepath)
def _step_process_mail_backend(self, filepath): """ Processes the email pointed by filepath in an async fashion. yield this method in another inlineCallbacks method or return it for it to be run. :param filepath: Path of the file that changed :type filepath: twisted.python.filepath.FilePath """ log.msg("Processing new mail at %r" % (filepath.path,)) with filepath.open("r") as f: mail_data = f.read() msg = message_from_string(mail_data) uuid = self._get_owner(msg) if uuid is None: log.msg("Don't know how to deliver mail %r, skipping..." % (filepath.path,)) bounce_reason = "Missing UUID: There was a problem " \ "locating the user in our database." yield self._bounce_message(msg, filepath, bounce_reason) defer.returnValue(None) log.msg("Mail owner: %s" % (uuid,)) pubkey = yield self._users_cdb.getPubkey(uuid) if pubkey is None or len(pubkey) == 0: log.msg( "No public key for %s, stopping the processing chain." % uuid) bounce_reason = "Missing PGP public key: There was a " \ "problem locating the user's public key in " \ "our database." yield self._bounce_message(msg, filepath, bounce_reason) defer.returnValue(None) log.msg("Encrypting message to %s's pubkey" % (uuid,)) try: doc = yield self._encrypt_message(pubkey, mail_data) yield self._export_message(uuid, doc) yield self._remove(filepath) except Exception as e: yield self._bounce_with_timeout(filepath, msg, e)
def _process_incoming_email(self, otherself, filepath, mask): """ Callback that processes incoming email @param otherself: Watch object for the current callback from inotify @type otherself: twisted.internet.inotify._Watch @param filepath: Path of the file that changed @type filepath: twisted.python.filepath.FilePath @param mask: identifier for the type of change that triggered this callback @type mask: int """ if os.path.split(filepath.dirname())[-1] == "new": log.msg("Processing new mail at %s" % (filepath.path,)) with filepath.open("r") as f: mail_data = f.read() mail = message_from_string(mail_data) owner = mail["To"] if owner is None: # default to Delivered-To owner = mail["Delivered-To"] if owner is None: log.err("Malformed mail, neither To: nor " "Delivered-To: field") log.msg("Mail owner: %s" % (owner,)) log.msg("%s received a new mail" % (owner,)) dpubk = self._users_cdb.getPubKey(owner) duuid = self._users_cdb.queryByAddress(owner) d = DeferredList([dpubk, duuid]) d.addCallbacks(self._gather_uuid_pubkey, log.err) d.addCallbacks(self._encrypt_message, log.err, (owner, mail_data)) d.addCallbacks(self._export_message, log.err) d.addCallbacks(self._conditional_remove, log.err, (filepath,)) d.addErrback(log.err)
def delayed_handler(self, filepath, mask): time.sleep(1) handler(filepath.open())