From 74b149d2a20476445ff6ebe78fd235b1b38f1af8 Mon Sep 17 00:00:00 2001 From: Patrick Totzke Date: Thu, 18 Aug 2011 18:19:55 +0100 Subject: attach signature file to outgoing mails --- alot/command.py | 57 ++++++++++++++++++++++----------------------------------- 1 file changed, 22 insertions(+), 35 deletions(-) (limited to 'alot/command.py') diff --git a/alot/command.py b/alot/command.py index 3f7578a6..cc7950c2 100644 --- a/alot/command.py +++ b/alot/command.py @@ -17,8 +17,8 @@ along with notmuch. If not, see . Copyright (C) 2011 Patrick Totzke """ import os -import glob import code +import glob import logging import threading import subprocess @@ -41,6 +41,7 @@ import buffer import settings import widgets import completion +import helper from db import DatabaseROError from db import DatabaseLockedError from completion import ContactsCompleter @@ -834,6 +835,16 @@ class EnvelopeSendCommand(Command): sname, saddr = email.Utils.parseaddr(frm) account = ui.accountman.get_account_by_address(saddr) if account: + # attach signature file if present + if account.signature: + sig = os.path.expanduser(account.signature) + if os.path.isfile(sig): + helper.attach(sig, mail, filename='signature') + else: + ui.notify('could not locate signature: %s' % sig, priority='error') + if not ui.choice('send without signature') == 'yes': + return + clearme = ui.notify('sending..', timeout=-1, block=False) reason = account.send_mail(mail) ui.clear_notify([clearme]) @@ -849,11 +860,12 @@ class EnvelopeSendCommand(Command): class EnvelopeAttachCommand(Command): - def __init__(self, path=None, **kwargs): + def __init__(self, path=None, mail=None, **kwargs): Command.__init__(self, **kwargs) self.files = [] if path: self.files = glob.glob(os.path.expanduser(path)) + self.mail = mail def apply(self, ui): if not self.files: @@ -865,40 +877,15 @@ class EnvelopeAttachCommand(Command): ui.notify('no matches, abort') return logging.info(self.files) - msg = ui.current_buffer.get_email() + + msg = self.mail + if not msg: + msg = ui.current_buffer.get_email() for path in self.files: - ctype, encoding = mimetypes.guess_type(path) - if ctype is None or encoding is not None: - # No guess could be made, or the file is encoded (compressed), - # so use a generic bag-of-bits type. - ctype = 'application/octet-stream' - maintype, subtype = ctype.split('/', 1) - if maintype == 'text': - fp = open(path) - # Note: we should handle calculating the charset - part = MIMEText(fp.read(), _subtype=subtype) - fp.close() - elif maintype == 'image': - fp = open(path, 'rb') - part = MIMEImage(fp.read(), _subtype=subtype) - fp.close() - elif maintype == 'audio': - fp = open(path, 'rb') - part = MIMEAudio(fp.read(), _subtype=subtype) - fp.close() - else: - fp = open(path, 'rb') - part = MIMEBase(maintype, subtype) - part.set_payload(fp.read()) - fp.close() - # Encode the payload using Base64 - encoders.encode_base64(part) - # Set the filename parameter - part.add_header('Content-Disposition', 'attachment', - filename=os.path.basename(path)) - msg.attach(part) - - ui.current_buffer.set_email(msg) + helper.attach(path, msg) + + if not self.mail: # set the envelope msg iff we got it from there + ui.current_buffer.set_email(msg) # TAGLIST -- cgit v1.2.3 From 2aead82dd6feddb5b95e76421d0385deb6f5978c Mon Sep 17 00:00:00 2001 From: Patrick Totzke Date: Thu, 18 Aug 2011 18:33:24 +0100 Subject: add 'signature_filename' in accounts this lets you specify the filename used in the signature attachment --- alot/account.py | 7 ++++++- alot/command.py | 6 +++++- data/example.rc | 3 ++- 3 files changed, 13 insertions(+), 3 deletions(-) (limited to 'alot/command.py') diff --git a/alot/account.py b/alot/account.py index 33cda217..58640d38 100644 --- a/alot/account.py +++ b/alot/account.py @@ -42,9 +42,12 @@ class Account: """gpg fingerprint. CURRENTLY IGNORED""" signature = None """path to a signature file to append to outgoing mails.""" + signature_filename = None + """filename of signature file in attachment""" def __init__(self, address=None, aliases=None, realname=None, gpg_key=None, - signature=None, sent_box=None, draft_box=None): + signature=None, signature_filename=None, sent_box=None, + draft_box=None): self.address = address self.aliases = [] if aliases: @@ -52,6 +55,7 @@ class Account: self.realname = realname self.gpg_key = gpg_key self.signature = signature + self.signature_filename = signature_filename self.sent_box = None if sent_box: @@ -160,6 +164,7 @@ class AccountManager: 'aliases', 'gpg_key', 'signature', + 'signature_filename', 'type', 'sendmail_command', 'sent_box', diff --git a/alot/command.py b/alot/command.py index cc7950c2..2b6bc30a 100644 --- a/alot/command.py +++ b/alot/command.py @@ -839,7 +839,11 @@ class EnvelopeSendCommand(Command): if account.signature: sig = os.path.expanduser(account.signature) if os.path.isfile(sig): - helper.attach(sig, mail, filename='signature') + if account.signature_filename: + name = account.signature_filename + else: + name = None + helper.attach(sig, mail, filename=name) else: ui.notify('could not locate signature: %s' % sig, priority='error') if not ui.choice('send without signature') == 'yes': diff --git a/data/example.rc b/data/example.rc index 076a006c..368449a0 100644 --- a/data/example.rc +++ b/data/example.rc @@ -16,7 +16,8 @@ realname = john doe address = john.doe@gmail.com aliases = john.doe@googlemail.com;john.d@g.com # ';' separated list of alias addresses gpg_key = 123ABC -signature = /home/john/.signature +signature = /home/john/.signature.gmail # path to signature file +signature_filename = sig.txt # filename in attachment part sendmail_command = msmtp --account=gmail -t sent_mailbox = maildir:///home/john/mail/gmail/Sent # we accept mbox,maildir,mh,babyl,mmdf here -- cgit v1.2.3 From ea3916eaef3bcc0b2df6b36b849a46254767d828 Mon Sep 17 00:00:00 2001 From: Patrick Totzke Date: Fri, 19 Aug 2011 10:26:47 +0100 Subject: fix: issue with encoders in helper.attach --- alot/command.py | 7 ++----- alot/helper.py | 2 +- 2 files changed, 3 insertions(+), 6 deletions(-) (limited to 'alot/command.py') diff --git a/alot/command.py b/alot/command.py index 2b6bc30a..86d24db2 100644 --- a/alot/command.py +++ b/alot/command.py @@ -28,11 +28,7 @@ import mimetypes from email.parser import Parser from email import Charset from email.header import Header -from email import encoders from email.message import Message -from email.mime.audio import MIMEAudio -from email.mime.base import MIMEBase -from email.mime.image import MIMEImage from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart import urwid @@ -845,7 +841,8 @@ class EnvelopeSendCommand(Command): name = None helper.attach(sig, mail, filename=name) else: - ui.notify('could not locate signature: %s' % sig, priority='error') + ui.notify('could not locate signature: %s' % sig, + priority='error') if not ui.choice('send without signature') == 'yes': return diff --git a/alot/helper.py b/alot/helper.py index 6aaaf917..eb0d4978 100644 --- a/alot/helper.py +++ b/alot/helper.py @@ -87,7 +87,7 @@ def attach(path, mail, filename=None): part.set_payload(fp.read()) fp.close() # Encode the payload using Base64 - encoders.encode_base64(part) + email.encoders.encode_base64(part) # Set the filename parameter if not filename: filename = os.path.basename(path) -- cgit v1.2.3 From ba32be4bb710ddbe28ad84c11c2b975d7f874ab4 Mon Sep 17 00:00:00 2001 From: Patrick Totzke Date: Sat, 20 Aug 2011 10:31:41 +0100 Subject: make editors write encoding user configurable this defaults to 'UTF-8' --- alot/command.py | 3 ++- alot/settings.py | 1 + data/example.full.rc | 1 + 3 files changed, 4 insertions(+), 1 deletion(-) (limited to 'alot/command.py') diff --git a/alot/command.py b/alot/command.py index 2b6bc30a..d6eef770 100644 --- a/alot/command.py +++ b/alot/command.py @@ -754,7 +754,8 @@ class EnvelopeEditCommand(Command): def openEnvelopeFromTmpfile(): f = open(tf.name) - editor_input = f.read().decode('utf-8') + enc = settings.config.get('general', 'editor_writes_encoding') + editor_input = f.read().decode(enc) #split editor out headertext, bodytext = editor_input.split('\n\n', 1) diff --git a/alot/settings.py b/alot/settings.py index f8e9ca7e..55b543a3 100644 --- a/alot/settings.py +++ b/alot/settings.py @@ -28,6 +28,7 @@ DEFAULTS = { 'general': { 'colourmode': '256', 'editor_cmd': "/usr/bin/vim -f -c 'set filetype=mail' +", + 'editor_writes_encoding': 'UTF-8', 'terminal_cmd': 'x-terminal-emulator -e', 'spawn_editor': 'False', 'displayed_headers': 'From,To,Cc,Bcc,Subject', diff --git a/data/example.full.rc b/data/example.full.rc index 4a0e3496..38577d00 100644 --- a/data/example.full.rc +++ b/data/example.full.rc @@ -21,6 +21,7 @@ displayed_headers = From,To,Cc,Bcc,Subject # editor command editor_cmd = /usr/bin/vim -f -c 'set filetype=mail' + +editor_writes_encoding' = UTF-8 # timeout in secs after a failed attempt to flush is repeated flush_retry_timeout = 5 -- cgit v1.2.3 From e6d68139f063c48a0b1cf29dfbe012e460b31ce8 Mon Sep 17 00:00:00 2001 From: Patrick Totzke Date: Sat, 20 Aug 2011 13:31:42 +0100 Subject: fix: handle canceling in compose prompts issue #29 --- alot/command.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'alot/command.py') diff --git a/alot/command.py b/alot/command.py index 2b296d28..7ccf49c4 100644 --- a/alot/command.py +++ b/alot/command.py @@ -366,7 +366,7 @@ class ComposeCommand(Command): cmpl = AccountCompleter(ui.accountman) fromaddress = ui.prompt(prefix='From>', completer=cmpl, tab=1) validaddresses = [a.address for a in accounts] + [None] - while fromaddress not in validaddresses: + while fromaddress not in validaddresses: # TODO: not cool ui.notify('no account for this address. ( cancels)') fromaddress = ui.prompt(prefix='From>', completer=cmpl) if not fromaddress: @@ -378,10 +378,16 @@ class ComposeCommand(Command): #get To header if 'To' not in self.mail: to = ui.prompt(prefix='To>', completer=ContactsCompleter()) + if to == None: + ui.notify('canceled') + return self.mail['To'] = encode_header('to', to) if settings.config.getboolean('general', 'ask_subject') and \ not 'Subject' in self.mail: subject = ui.prompt(prefix='Subject>') + if subject == None: + ui.notify('canceled') + return self.mail['Subject'] = encode_header('subject', subject) ui.apply_command(EnvelopeEditCommand(mail=self.mail)) -- cgit v1.2.3