diff options
author | pazz <patricktotzke@gmail.com> | 2011-08-13 14:03:25 +0100 |
---|---|---|
committer | pazz <patricktotzke@gmail.com> | 2011-08-13 14:03:25 +0100 |
commit | 235d827db02ea5def93aeccd22d292951846626e (patch) | |
tree | 81bcef6d34da5b346b1ff66cd7d46ea291c9c1ae | |
parent | 893c5b71bd465edaab24ead2df7f5a2f811f9abf (diff) |
merged sender back into accounts
-rw-r--r-- | USAGE | 4 | ||||
-rw-r--r-- | alot/account.py | 141 | ||||
-rw-r--r-- | alot/command.py | 2 | ||||
-rwxr-xr-x | alot/init.py | 6 | ||||
-rw-r--r-- | alot/send.py | 73 | ||||
-rw-r--r-- | alot/settings.py | 1 | ||||
-rw-r--r-- | alot/ui.py | 2 | ||||
-rw-r--r-- | data/example.rc | 1 |
8 files changed, 121 insertions, 109 deletions
@@ -97,8 +97,8 @@ I use this for my uni-account: sendmail_command = msmtp --account=uoe -t sent_mailbox = maildir:///home/pazz/mail/uoe/Sent -Caution: Sending mails is only supported via sendmail for now. You need to set -sender_type to "sendmail" and specify your sendmail_command. +Caution: Sending mails is only supported via sendmail for now. If you want +to use a sendmail command different from 'sendmail', specify it as sendmail_command. send_mailbox specifies the mailbox where you want outgoing mails to be stored after successfully sending them. You can use mbox, maildir, mh, babyl and mmdf diff --git a/alot/account.py b/alot/account.py index 6c832b21..fcce9639 100644 --- a/alot/account.py +++ b/alot/account.py @@ -18,15 +18,35 @@ Copyright (C) 2011 Patrick Totzke <patricktotzke@gmail.com> """ import mailbox +import shlex +import subprocess +import logging +import time +import email from urlparse import urlparse from send import SendmailSender class Account: - def __init__(self, address, aliases=None, realname=None, gpg_key=None, signature=None, - sender_type='sendmail', sendmail_command='sendmail', - sent_mailbox=None): + """ + Datastructure that represents an email account. It manages + this account's settings, can send and store mails to + maildirs (drafts/send) + """ + address = None + """this accounts main email address""" + aliases = [] + """list of alternative addresses""" + realname = None + """real name used to format from-headers""" + gpg_key = None + """gpg fingerprint :note:currently ignored""" + signature = None + """signature to append to outgoing mails note::currently ignored""" + + def __init__(self, address=None, aliases=None, realname=None, gpg_key=None, + signature=None, sent_box=None, draft_box=None): self.address = address self.aliases = [] if aliases: @@ -34,25 +54,90 @@ class Account: self.realname = realname self.gpg_key = gpg_key self.signature = signature - self.sender_type = sender_type - self.mailbox = None - if sent_mailbox: + self.sent_box = None + if sent_box: mburl = urlparse(sent_mailbox) if mburl.scheme == 'mbox': - self.mailbox = mailbox.mbox(mburl.path) + self.sent_box = mailbox.mbox(mburl.path) elif mburl.scheme == 'maildir': - self.mailbox = mailbox.Maildir(mburl.path) + self.sent_box = mailbox.Maildir(mburl.path) elif mburl.scheme == 'mh': - self.mailbox = mailbox.MH(mburl.path) + self.sent_box = mailbox.MH(mburl.path) elif mburl.scheme == 'babyl': - self.mailbox = mailbox.Babyl(mburl.path) + self.sent_box = mailbox.Babyl(mburl.path) elif mburl.scheme == 'mmdf': - self.mailbox = mailbox.MMDF(mburl.path) + self.sent_box = mailbox.MMDF(mburl.path) - if self.sender_type == 'sendmail': - self.sender = SendmailSender(sendmail_command, - mailbox=self.mailbox) + self.draft_box = None + if draft_box: + mburl = urlparse(sent_mailbox) + if mburl.scheme == 'mbox': + self.draft_box = mailbox.mbox(mburl.path) + elif mburl.scheme == 'maildir': + self.draft_box = mailbox.Maildir(mburl.path) + elif mburl.scheme == 'mh': + self.draft_box = mailbox.MH(mburl.path) + elif mburl.scheme == 'babyl': + self.draft_box = mailbox.Babyl(mburl.path) + elif mburl.scheme == 'mmdf': + self.draft_box = mailbox.MMDF(mburl.path) + + def store_mail(self, mbx, mail): + """stores given mail in mailbox. if mailbox is maildir, set the S-flag. + :type mbx: `mailbox.Mailbox` + :type mail: `email.message.Message` or string + """ + mbx.lock() + if isinstance(mbx, mailbox.Maildir): + msg = mailbox.MaildirMessage(email) + msg.set_flags('S') + else: + msg = mailbox.Message(email) + key = mbx.add(email) + mbx.flush() + mbx.unlock() + + def store_sent_mail(self, mail): + """stores given mail as sent if sent_box is set""" + if self.sent_box: + self.store_mail(self.sent_box, mail) + + def store_draft_mail(self, mail): + """stores given mail as draft if draft_box is set""" + if self.draft_box: + self.store_mail(self.sent_box, mail) + + def send_mail(self, email): + """ + sends given email + :returns: tuple (success, reason) of types bool and str. + """ + return False, 'not implemented' + + +class SendmailAccount(Account): + """Account that knows how to send out mails via sendmail""" + def __init__(self, cmd, **kwargs): + Account.__init__(self, **kwargs) + self.cmd = cmd + + def send_mail(self, mail): + mail['Date'] = email.utils.formatdate(time.time(), True) + # no unicode in shlex on 2.x + args = shlex.split(self.cmd.encode('ascii')) + try: + proc = subprocess.Popen(args, stdin=subprocess.PIPE, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE) + out, err = proc.communicate(mail.as_string()) + except OSError, e: + return False, str(e) + '. sendmail_cmd set to: %s' % self.cmd + if proc.poll(): # returncode is not 0 + return False, err.strip() + else: + self.store_sent_mail(mail) + return True, '' class AccountManager: @@ -61,9 +146,10 @@ class AccountManager: 'aliases', 'gpg_key', 'signature', - 'sender_type', + 'type', 'sendmail_command', - 'sent_mailbox'] + 'sent_box', + 'draft_box'] manditory = ['realname', 'address'] accountmap = {} accounts = [] @@ -75,19 +161,22 @@ class AccountManager: for s in accountsections: options = filter(lambda x: x in self.allowed, config.options(s)) args = {} + to_set = self.manditory for o in options: args[o] = config.get(s, o) - if o in self.manditory: - self.manditory.remove(o) - if not self.manditory: - newacc = (Account(**args)) - self.accountmap[newacc.address] = newacc - self.accounts.append(newacc) - for alias in newacc.aliases: - self.accountmap[alias] = newacc + if o in to_set: + to_set.remove(o) # removes obligation + if not to_set: # all manditory fields were present + sender_type = args.pop('type', 'sendmail') + if sender_type == 'sendmail': + cmd = args.pop('sendmail_command', 'sendmail') + newacc = (SendmailAccount(cmd, **args)) + self.accountmap[newacc.address] = newacc + self.accounts.append(newacc) + for alias in newacc.aliases: + self.accountmap[alias] = newacc else: - pass - # log info + logging.info('account section %s lacks fields %s' % (s, to_set)) def get_accounts(self): """return known accounts diff --git a/alot/command.py b/alot/command.py index d66c3c6d..35b1359b 100644 --- a/alot/command.py +++ b/alot/command.py @@ -833,7 +833,7 @@ class EnvelopeSendCommand(Command): account = ui.accountman.get_account_by_address(saddr) if account: clearme = ui.notify('sending..', timeout=-1, block=False) - success, reason = account.sender.send_mail(mail) + success, reason = account.send_mail(mail) ui.clear_notify([clearme]) if success: cmd = BufferCloseCommand(buffer=envelope) diff --git a/alot/init.py b/alot/init.py index c6eb839a..c23fee93 100755 --- a/alot/init.py +++ b/alot/init.py @@ -63,15 +63,15 @@ def main(): settings.config.read(configfilename) settings.hooks.setup(settings.config.get('general', 'hooksfile')) - #accountman - aman = AccountManager(settings.config) - # setup logging numeric_loglevel = getattr(logging, args.debug_level.upper(), None) logfilename = os.path.expanduser(args.logfile) logging.basicConfig(level=numeric_loglevel, filename=logfilename) logger = logging.getLogger() + #accountman + aman = AccountManager(settings.config) + # get ourselves a database manager dbman = DBManager(path=args.db_path, ro=args.read_only) diff --git a/alot/send.py b/alot/send.py deleted file mode 100644 index 726f7854..00000000 --- a/alot/send.py +++ /dev/null @@ -1,73 +0,0 @@ -""" -This file is part of alot. - -Alot is free software: you can redistribute it and/or modify it -under the terms of the GNU General Public License as published by the -Free Software Foundation, either version 3 of the License, or (at your -option) any later version. - -Alot is distributed in the hope that it will be useful, but WITHOUT -ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or -FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License -for more details. - -You should have received a copy of the GNU General Public License -along with notmuch. If not, see <http://www.gnu.org/licenses/>. - -Copyright (C) 2011 Patrick Totzke <patricktotzke@gmail.com> -""" - -import mailbox -import shlex -import subprocess -import logging -import time -import email - - -class Sender: - def send_mail(self, email): - """ - sends given email - :returns: tuple (success, reason) of types bool and str. - """ - return False, 'not implemented' - - def __init__(self, mailbox=None): - self.mailbox = mailbox - - def store_mail(self, email): - if self.mailbox: - self.mailbox.lock() - if isinstance(self.mailbox, mailbox.Maildir): - msg = mailbox.MaildirMessage(email) - msg.set_flags('S') - else: - msg = mailbox.Message(email) - key = self.mailbox.add(email) - self.mailbox.flush() - self.mailbox.unlock() - - -class SendmailSender(Sender): - def __init__(self, sendmail_cmd, mailbox=None): - self.cmd = sendmail_cmd - self.mailbox = mailbox - - def send_mail(self, mail): - mail['Date'] = email.utils.formatdate(time.time(), True) - # no unicode in shlex on 2.x - args = shlex.split(self.cmd.encode('ascii')) - logging.info('args:%s' % args) - try: - proc = subprocess.Popen(args, stdin=subprocess.PIPE, - stdout=subprocess.PIPE, - stderr=subprocess.PIPE) - out, err = proc.communicate(mail.as_string()) - except OSError, e: - return False, str(e) + '. sendmail_cmd set to: %s' % self.cmd - if proc.poll(): # returncode is not 0 - return False, err.strip() - else: - self.store_mail(mail) - return True, '' diff --git a/alot/settings.py b/alot/settings.py index c4ac3c71..85d62352 100644 --- a/alot/settings.py +++ b/alot/settings.py @@ -22,7 +22,6 @@ import mailcap import codecs from ConfigParser import SafeConfigParser -from account import Account DEFAULTS = { @@ -58,8 +58,6 @@ class UI: colourmode = config.getint('general', 'colourmode') self.logger.info('setup gui in %d colours' % colourmode) self.mainframe = MainWidget(self) - for l in config.get_palette(): - log.info(l) self.mainloop = urwid.MainLoop(self.mainframe, config.get_palette(), handle_mouse=False, diff --git a/data/example.rc b/data/example.rc index 98a3ddb9..18ecc7a9 100644 --- a/data/example.rc +++ b/data/example.rc @@ -19,7 +19,6 @@ 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 -sender_type = sendmail sendmail_command = msmtp --account=gmail -t sent_mailbox = maildir:///home/john/mail/gmail/Sent # we accept mbox,maildir,mh,babyl,mmdf here |