From 6cf729f2608cd52a837bce8fb391f0a162243fa1 Mon Sep 17 00:00:00 2001 From: Patrick Totzke Date: Thu, 20 Sep 2012 21:49:20 +0100 Subject: Let SendCommand respect envelope/mail parameters ... to directly send a mail without the need for an envelope buffer to be open. --- alot/commands/envelope.py | 122 +++++++++++++++++++++++++++------------------- 1 file changed, 71 insertions(+), 51 deletions(-) (limited to 'alot/commands/envelope.py') diff --git a/alot/commands/envelope.py b/alot/commands/envelope.py index 2a041165..65cdebdc 100644 --- a/alot/commands/envelope.py +++ b/alot/commands/envelope.py @@ -114,69 +114,85 @@ class SaveCommand(Command): @registerCommand(MODE, 'send') class SendCommand(Command): """send mail""" + def __init__(self, mail=None, envelope=None, **kwargs): + """ + :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 + """ + Command.__init__(self, **kwargs) + self.mail = mail + self.envelope = envelope + self.envelope_buffer = None + @inlineCallbacks def apply(self, ui): - currentbuffer = ui.current_buffer # needed to close later - envelope = currentbuffer.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 envelope.sent_time: - warning = 'A modified version of ' * envelope.modified_since_sent - warning += 'this message has been sent at %s.' % envelope.sent_time - warning += ' Do you want to resend?' - if (yield ui.choice(warning, cancel='no', - msg_position='left')) == 'no': - return + if self.mail is None: + if self.envelope is None: + self.envelope_buffer = ui.current_buffer # needed to close later + self.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: + warning = 'A modified version of ' * self.envelope.modified_since_sent + warning += 'this message has been sent at %s.' % self.envelope.sent_time + warning += ' Do you want to resend?' + if (yield ui.choice(warning, cancel='no', + msg_position='left')) == 'no': + return - # don't do anything if another SendCommand is in the middle of sending - # the message and we were triggered accidentally - if envelope.sending: - msg = 'sending this message already!' - logging.debug(msg) - return + # don't do anything if another SendCommand is in the middle of sending + # the message and we were triggered accidentally + if self.envelope.sending: + msg = 'sending this message already!' + logging.debug(msg) + return - frm = envelope.get('From') - sname, saddr = email.Utils.parseaddr(frm) + frm = self.envelope.get('From') + sname, saddr = email.Utils.parseaddr(frm) - # determine account to use for sending - account = settings.get_account_by_address(saddr) - if account is None: - if not settings.get_accounts(): - ui.notify('no accounts set', priority='error') + # determine account to use for sending + account = settings.get_account_by_address(saddr) + if account is None: + if not settings.get_accounts(): + ui.notify('no accounts set', priority='error') + return + else: + account = settings.get_accounts()[0] + + clearme = ui.notify(u'constructing mail (GPG, attachments)\u2026', + timeout=-1) + + try: + self.mail = self.envelope.construct_mail() + self.mail['Date'] = email.Utils.formatdate(localtime=True) + self.mail = crypto.email_as_string(self.mail) + except GPGProblem, e: + ui.clear_notify([clearme]) + ui.notify(e.message, priority='error') return - else: - account = settings.get_accounts()[0] - - clearme = ui.notify(u'constructing mail (GPG, attachments)\u2026', - timeout=-1) - try: - mail = envelope.construct_mail() - mail['Date'] = email.Utils.formatdate(localtime=True) - mail = crypto.email_as_string(mail) - except GPGProblem, e: ui.clear_notify([clearme]) - ui.notify(e.message, priority='error') - return - - ui.clear_notify([clearme]) - - # send - clearme = ui.notify('sending..', timeout=-1) + # define call def afterwards(returnvalue): - envelope.sending = False + if self.envelope is not None: + self.envelope.sending = False + self.envelope.sent_time = datetime.datetime.now() logging.debug('mail sent successfully') ui.clear_notify([clearme]) - envelope.sent_time = datetime.datetime.now() - ui.apply_command(commands.globals.BufferCloseCommand()) + if self.envelope_buffer is not None: + cmd = commands.globals.BufferCloseCommand(self.envelope_buffer) + ui.apply_command(cmd) ui.notify('mail sent successfully') # store mail locally # This can raise StoreMailError - path = account.store_sent_mail(mail) + path = account.store_sent_mail(self.mail) # add mail to index if maildir path available if path is not None: @@ -184,8 +200,10 @@ class SendCommand(Command): ui.dbman.add_message(path, account.sent_tags + envelope.tags) ui.apply_command(globals.FlushCommand()) + # define errback def send_errb(failure): - envelope.sending = False + if self.envelope is not None: + self.envelope.sending = False ui.clear_notify([clearme]) failure.trap(SendingMailFailed) logging.error(failure.getTraceback()) @@ -198,12 +216,14 @@ class SendCommand(Command): errmsg = 'could not store mail: %s' % failure.value ui.notify(errmsg, priority='error') - envelope.sending = True - d = account.send_mail(mail) + # send out + clearme = ui.notify('sending..', timeout=-1) + if self.envelope is not None: + self.envelope.sending = True + d = account.send_mail(self.mail) d.addCallback(afterwards) d.addErrback(send_errb) d.addErrback(store_errb) - logging.debug('added errbacks,callbacks') @registerCommand(MODE, 'edit', arguments=[ -- cgit v1.2.3 From 1dee94c0b845dbea7d7c96fe192568190f0757a3 Mon Sep 17 00:00:00 2001 From: Patrick Totzke Date: Thu, 20 Sep 2012 22:30:00 +0100 Subject: make sure SendCommand works with mail parameter... ... being a string/email.message.Message. --- alot/commands/envelope.py | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) (limited to 'alot/commands/envelope.py') diff --git a/alot/commands/envelope.py b/alot/commands/envelope.py index 65cdebdc..d5a23980 100644 --- a/alot/commands/envelope.py +++ b/alot/commands/envelope.py @@ -152,18 +152,6 @@ class SendCommand(Command): logging.debug(msg) return - frm = self.envelope.get('From') - sname, saddr = email.Utils.parseaddr(frm) - - # determine account to use for sending - account = settings.get_account_by_address(saddr) - if account is None: - if not settings.get_accounts(): - ui.notify('no accounts set', priority='error') - return - else: - account = settings.get_accounts()[0] - clearme = ui.notify(u'constructing mail (GPG, attachments)\u2026', timeout=-1) @@ -178,7 +166,25 @@ class SendCommand(Command): ui.clear_notify([clearme]) - # define call + # determine account to use for sending + msg = self.mail + if not isinstance(msg, email.message.Message): + msg = email.message_from_string(self.mail) + sname, saddr = email.Utils.parseaddr(msg.get('From', '')) + account = settings.get_account_by_address(saddr) + if account is None: + if not settings.get_accounts(): + ui.notify('no accounts set', priority='error') + return + else: + account = settings.get_accounts()[0] + + # make sure self.mail is a string + logging.debug(self.mail.__class__) + if isinstance(self.mail, email.message.Message): + self.mail = str(self.mail) + + # define callback def afterwards(returnvalue): if self.envelope is not None: self.envelope.sending = False -- cgit v1.2.3 From fbaba32d29353dbf31a0e13f2ffee428ed234070 Mon Sep 17 00:00:00 2001 From: Patrick Totzke Date: Sat, 22 Sep 2012 20:02:16 +0100 Subject: fix issue with initial tags --- alot/commands/envelope.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'alot/commands/envelope.py') diff --git a/alot/commands/envelope.py b/alot/commands/envelope.py index d5a23980..614061f6 100644 --- a/alot/commands/envelope.py +++ b/alot/commands/envelope.py @@ -186,9 +186,11 @@ class SendCommand(Command): # define callback def afterwards(returnvalue): + initial_tags = [] if self.envelope is not None: self.envelope.sending = False self.envelope.sent_time = datetime.datetime.now() + initial_tags = self.envelope.tags logging.debug('mail sent successfully') ui.clear_notify([clearme]) if self.envelope_buffer is not None: @@ -203,7 +205,7 @@ class SendCommand(Command): # add mail to index if maildir path available if path is not None: logging.debug('adding new mail to index') - ui.dbman.add_message(path, account.sent_tags + envelope.tags) + ui.dbman.add_message(path, account.sent_tags + initial_tags) ui.apply_command(globals.FlushCommand()) # define errback -- cgit v1.2.3 From 36a3ff22e03a070cd5f4d9594ed4fb15d9f4cfe8 Mon Sep 17 00:00:00 2001 From: Patrick Totzke Date: Sat, 22 Sep 2012 20:50:27 +0100 Subject: pep8 fixes --- alot/addressbooks.py | 3 ++- alot/commands/envelope.py | 17 ++++++++++------- alot/commands/thread.py | 6 +++--- alot/completion.py | 3 ++- 4 files changed, 17 insertions(+), 12 deletions(-) (limited to 'alot/commands/envelope.py') diff --git a/alot/addressbooks.py b/alot/addressbooks.py index 7767e597..d3f64b6c 100644 --- a/alot/addressbooks.py +++ b/alot/addressbooks.py @@ -31,7 +31,8 @@ class AddressBook(object): query = '.*%s.*' % query for name, email in self.get_contacts(): try: - if re.match(query, name, self.reflags) or re.match(query, email, self.reflags): + if re.match(query, name, self.reflags) or \ + re.match(query, email, self.reflags): res.append((name, email)) except: pass diff --git a/alot/commands/envelope.py b/alot/commands/envelope.py index 614061f6..c461002e 100644 --- a/alot/commands/envelope.py +++ b/alot/commands/envelope.py @@ -118,8 +118,8 @@ class SendCommand(Command): """ :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. + :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 """ Command.__init__(self, **kwargs) @@ -131,22 +131,25 @@ class SendCommand(Command): def apply(self, ui): if self.mail is None: if self.envelope is None: - self.envelope_buffer = ui.current_buffer # needed to close later + # needed to close later + self.envelope_buffer = ui.current_buffer self.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: - warning = 'A modified version of ' * self.envelope.modified_since_sent - warning += 'this message has been sent at %s.' % self.envelope.sent_time + mod = self.envelope.modified_since_sent + when = self.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 (yield ui.choice(warning, cancel='no', msg_position='left')) == 'no': return - # don't do anything if another SendCommand is in the middle of sending - # the message and we were triggered accidentally + # don't do anything if another SendCommand is in the middle of + # sending the message and we were triggered accidentally if self.envelope.sending: msg = 'sending this message already!' logging.debug(msg) diff --git a/alot/commands/thread.py b/alot/commands/thread.py index 82043b44..1ee2d933 100644 --- a/alot/commands/thread.py +++ b/alot/commands/thread.py @@ -67,14 +67,14 @@ def determine_sender(mail, action='reply'): # and use that accounts realname and the found recipient address for account in my_accounts: acc_addresses = account.get_addresses() - for alias_re in acc_addresses: + for alias in acc_addresses: if realname is not None: break - regex = re.compile(alias_re) + regex = re.compile(alias) for rec in recipients: seen_name, seen_address = parseaddr(rec) if regex.match(seen_address): - logging.debug("match!: '%s' '%s'" % (seen_address, alias_re)) + logging.debug("match!: '%s' '%s'" % (seen_address, alias)) if settings.get(action + '_force_realname'): realname = account.realname else: diff --git a/alot/completion.py b/alot/completion.py index c3bc60d6..1d523326 100644 --- a/alot/completion.py +++ b/alot/completion.py @@ -381,7 +381,8 @@ class CommandCompleter(Completer): plist = params.split(' ', 1) if len(plist) == 1: # complete from header keys localprefix = params - headers = ['Subject', 'To', 'Cc', 'Bcc', 'In-Reply-To', 'From'] + headers = ['Subject', 'To', 'Cc', 'Bcc', 'In-Reply-To', + 'From'] localcompleter = StringlistCompleter(headers) localres = localcompleter.complete(localprefix, localpos) res = [(c, p + 6) for (c, p) in localres] -- cgit v1.2.3