diff options
Diffstat (limited to 'alot')
-rw-r--r-- | alot/commands/envelope.py | 79 |
1 files changed, 38 insertions, 41 deletions
diff --git a/alot/commands/envelope.py b/alot/commands/envelope.py index b138195d..45830988 100644 --- a/alot/commands/envelope.py +++ b/alot/commands/envelope.py @@ -154,50 +154,47 @@ class SaveCommand(Command): @registerCommand(MODE, 'send') class SendCommand(Command): """send mail""" - def __init__(self, mail=None, envelope=None, **kwargs): + def __init__(self, mail = None): """ :param mail: email to send :type email: email.message.Message - :param envelope: envelope to use to construct the outgoing mail. This - will be ignored in case the mail parameter is set. - :type envelope: alot.db.envelope.envelope """ - super().__init__(**kwargs) + super().__init__() self.mail = mail - self.envelope = envelope self.envelope_buffer = None - def _get_keys_addresses(self): + def _get_keys_addresses(self, envelope): addresses = set() - for key in self.envelope.encrypt_keys.values(): + for key in envelope.encrypt_keys.values(): for uid in key.uids: addresses.add(uid.email) return addresses - def _get_recipients_addresses(self): - tos = self.envelope.headers.get('To', []) - ccs = self.envelope.headers.get('Cc', []) + def _get_recipients_addresses(self, envelope): + tos = envelope.headers.get('To', []) + ccs = envelope.headers.get('Cc', []) return {a for (_, a) in email.utils.getaddresses(tos + ccs)} - def _is_encrypted_to_all_recipients(self): - recipients_addresses = self._get_recipients_addresses() - keys_addresses = self._get_keys_addresses() + def _is_encrypted_to_all_recipients(self, envelope): + recipients_addresses = self._get_recipients_addresses(envelope) + keys_addresses = self._get_keys_addresses(envelope) return recipients_addresses.issubset(keys_addresses) async def apply(self, ui): + envelope = None + if self.mail is None: - if self.envelope is None: - # needed to close later - self.envelope_buffer = ui.current_buffer - self.envelope = self.envelope_buffer.envelope + # needed to close later + self.envelope_buffer = ui.current_buffer + envelope = self.envelope_buffer.envelope # This is to warn the user before re-sending # an already sent message in case the envelope buffer # was not closed because it was the last remaining buffer. - if self.envelope.sent_time: - mod = self.envelope.modified_since_sent - when = self.envelope.sent_time - warning = 'A modified version of ' * mod + if envelope.sent_time: + mod = envelope.modified_since_sent + when = envelope.sent_time + warning = 'A modified version of ' * mod warning += 'this message has been sent at %s.' % when warning += ' Do you want to resend?' if (await ui.choice(warning, cancel='no', @@ -206,14 +203,14 @@ class SendCommand(Command): # don't do anything if another SendCommand is in the middle of # sending the message and we were triggered accidentally - if self.envelope.sending: + if envelope.sending: logging.debug('sending this message already!') return # Before attempting to construct mail, ensure that we're not trying # to encrypt a message with a BCC, since any BCC recipients will # receive a message that they cannot read! - if self.envelope.headers.get('Bcc') and self.envelope.encrypt: + if envelope.headers.get('Bcc') and envelope.encrypt: warning = textwrap.dedent("""\ Any BCC recipients will not be able to decrypt this message. Do you want to send anyway?""").replace('\n', ' ') @@ -223,8 +220,8 @@ class SendCommand(Command): # Check if an encrypted message is indeed encrypted to all its # recipients. - if (self.envelope.encrypt - and not self._is_encrypted_to_all_recipients()): + if (envelope.encrypt + and not self._is_encrypted_to_all_recipients(envelope)): warning = textwrap.dedent("""\ Message is not encrypted to all recipients. This means that not everyone will be able to decode and read this message. @@ -237,7 +234,7 @@ class SendCommand(Command): timeout=-1) try: - self.mail = self.envelope.construct_mail() + self.mail = envelope.construct_mail() self.mail = self.mail.as_string(policy=email.policy.SMTP) except GPGProblem as e: ui.clear_notify(clearme) @@ -263,8 +260,8 @@ class SendCommand(Command): # send out clearme = ui.notify('sending..', timeout=-1) - if self.envelope is not None: - self.envelope.sending = True + if envelope is not None: + envelope.sending = True # FIXME XXX horrible hack, fix the type fuckery to_send = self.mail @@ -276,9 +273,9 @@ class SendCommand(Command): try: await account.send_mail(to_send) except SendingMailFailed as e: - if self.envelope is not None: - self.envelope.account = account - self.envelope.sending = False + if envelope is not None: + envelope.account = account + envelope.sending = False ui.clear_notify(clearme) logging.error(traceback.format_exc()) errmsg = 'failed to send: {}'.format(e) @@ -290,21 +287,21 @@ class SendCommand(Command): ui.notify(errmsg, priority='error', block=True) else: initial_tags = frozenset() - if self.envelope is not None: - self.envelope.sending = False - self.envelope.sent_time = datetime.datetime.now() - initial_tags = self.envelope.tags + if envelope is not None: + envelope.sending = False + envelope.sent_time = datetime.datetime.now() + initial_tags = envelope.tags logging.debug('mail sent successfully') ui.clear_notify(clearme) if self.envelope_buffer is not None: cmd = commands.globals.BufferCloseCommand(self.envelope_buffer) await ui.apply_command(cmd) ui.notify('mail sent successfully') - if self.envelope is not None: - if self.envelope.replied: - await self.envelope.replied.tags_add(account.replied_tags) - if self.envelope.passed: - await self.envelope.passed.tags_add(account.passed_tags) + if envelope is not None: + if envelope.replied: + await envelope.replied.tags_add(account.replied_tags) + if envelope.passed: + await envelope.passed.tags_add(account.passed_tags) # store mail locally # This can raise StoreMailError |