summaryrefslogtreecommitdiff
path: root/alot
diff options
context:
space:
mode:
authorPatrick Totzke <patricktotzke@gmail.com>2011-10-22 17:13:48 +0100
committerPatrick Totzke <patricktotzke@gmail.com>2011-10-22 17:13:48 +0100
commite8daead1deab6a88d3f907732eef234b077672ad (patch)
treeceaf5b46ebaeb43c4ba5e3364b6d9902c8477c87 /alot
parent1f00db1dd10a385d64cdd9889eeb42942fd4b7dd (diff)
use templates in global.compose command
Diffstat (limited to 'alot')
-rw-r--r--alot/commands/globals.py84
1 files changed, 62 insertions, 22 deletions
diff --git a/alot/commands/globals.py b/alot/commands/globals.py
index 86ad801e..f7c29fd6 100644
--- a/alot/commands/globals.py
+++ b/alot/commands/globals.py
@@ -372,39 +372,79 @@ class HelpCommand(Command):
@registerCommand(MODE, 'compose', help='compose a new email',
arguments=[
(['--sender'], {'nargs': '?', 'help':'sender'}),
+ (['--template'], {'nargs':'?',
+ 'help':'path to a template message file'}),
(['--subject'], {'nargs':'?', 'help':'subject line'}),
- (['--to'], {'nargs':'+', 'help':'recipient'}),
+ (['--to'], {'nargs':'+', 'help':'recipients'}),
(['--cc'], {'nargs':'+', 'help':'copy to'}),
(['--bcc'], {'nargs':'+', 'help':'blind copy to'}),
])
class ComposeCommand(Command):
- def __init__(self, mail=None, headers={},
+ def __init__(self, mail=None, headers={}, template=None,
sender=u'', subject=u'', to=[], cc=[], bcc=[],
**kwargs):
Command.__init__(self, **kwargs)
- if not mail:
- self.mail = MIMEMultipart()
- self.mail.attach(MIMEText('', 'plain', 'UTF-8'))
- else:
- self.mail = mail
- for key, value in headers.items():
- self.mail[key] = encode_header(key, value)
- if sender:
- self.mail['From'] = encode_header('From', sender)
- if subject:
- self.mail['Subject'] = encode_header('Subject', subject)
- if to:
- self.mail['To'] = encode_header('To', ','.join(to))
- if cc:
- self.mail['Cc'] = encode_header('Cc', ','.join(cc))
- if bcc:
- self.mail['Bcc'] = encode_header('Bcc', ','.join(bcc))
+ self.mail = mail
+ self.template = template
+ self.headers = headers
+ self.sender = sender
+ self.subject = subject
+ self.to = to
+ self.cc = cc
+ self.bcc = bcc
@defer.inlineCallbacks
def apply(self, ui):
- # TODO: fill with default header (per account)
- # get From header
+ # set self.mail
+ if self.mail is None:
+ if self.template is not None:
+ #get location of tempsdir, containing msg templates
+ tempdir = settings.config.get('general', 'template_dir')
+ tempdir = os.path.expanduser(tempdir)
+ if not tempdir:
+ xdgdir = os.environ.get('XDG_CONFIG_HOME',
+ os.path.expanduser('~/.config'))
+ tempdir = os.path.join(xdgdir, 'alot', 'templates')
+
+ path = os.path.expanduser(self.template)
+ if not os.path.dirname(path): # use tempsdir
+ if not os.path.isdir(tempdir):
+ ui.notify('no templates directory: %s' % tempdir,
+ priority='error')
+ return
+ path = os.path.join(tempdir, path)
+
+ if not os.path.isfile(path):
+ ui.notify('could not find template: %s' % path,
+ priority='error')
+ return
+ try:
+ self.mail = email.message_from_file(open(path))
+ except Exception, e:
+ ui.notify(str(e), priority='error')
+ return
+ else: # no mail or template given
+ self.mail = MIMEMultipart()
+ self.mail.attach(MIMEText('', 'plain', 'UTF-8'))
+
+ # set forced headers
+ for key, value in self.headers.items():
+ self.mail[key] = encode_header(key, value)
+
+ # set forced headers for separate parameters
+ if self.sender:
+ self.mail['From'] = encode_header('From', self.sender)
+ if self.subject:
+ self.mail['Subject'] = encode_header('Subject', self.subject)
+ if self.to:
+ self.mail['To'] = encode_header('To', ','.join(self.to))
+ if self.cc:
+ self.mail['Cc'] = encode_header('Cc', ','.join(self.cc))
+ if self.bcc:
+ self.mail['Bcc'] = encode_header('Bcc', ','.join(self.bcc))
+
+ # get missing From header
if not 'From' in self.mail:
accounts = ui.accountman.get_accounts()
if len(accounts) == 0:
@@ -427,7 +467,7 @@ class ComposeCommand(Command):
a = ui.accountman.get_account_by_address(fromaddress)
self.mail['From'] = "%s <%s>" % (a.realname, a.address)
- #get To header
+ # get missing To header
if 'To' not in self.mail:
name, addr = email.Utils.parseaddr(self.mail.get('From'))
a = ui.accountman.get_account_by_address(addr)