diff options
author | pazz <patricktotzke@gmail.com> | 2011-06-22 23:25:29 +0100 |
---|---|---|
committer | pazz <patricktotzke@gmail.com> | 2011-06-22 23:25:29 +0100 |
commit | c2dad59bba232b396f487d9ba33fe77a12790f7c (patch) | |
tree | a238a0bb5fa961f1d2fdce92db37a5083c284e82 | |
parent | b64c9809eead156ac154951171808106cfe509ae (diff) |
added accounts and sender, sendCmd works
-rw-r--r-- | alot/account.py | 46 | ||||
-rw-r--r-- | alot/buffer.py | 2 | ||||
-rw-r--r-- | alot/command.py | 21 | ||||
-rw-r--r-- | alot/helper.py | 5 | ||||
-rwxr-xr-x | alot/init.py | 4 | ||||
-rw-r--r-- | alot/send.py | 54 | ||||
-rw-r--r-- | alot/settings.py | 39 | ||||
-rw-r--r-- | alot/ui.py | 5 |
8 files changed, 166 insertions, 10 deletions
diff --git a/alot/account.py b/alot/account.py new file mode 100644 index 00000000..b00cee57 --- /dev/null +++ b/alot/account.py @@ -0,0 +1,46 @@ +""" +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 + +from send import SendmailSender + + +class Account: + def __init__(self, address, realname=None, + gpg_key=None, + signature=None, + sender_type='sendmail', + sendmail_command='sendmail', + sent_mailbox=None): + self.address = address + self.realname = realname + self.gpg_key = gpg_key + self.signature = signature + self.sender_type = sender_type + + if sent_mailbox: + #parse mailbox url + self.mailbox = mailbox.Maildir(sent_mailbox) + else: + self.mailbox = sent_mailbox + + if self.sender_type == 'sendmail': + self.sender = SendmailSender(sendmail_command, + mailbox=self.mailbox) diff --git a/alot/buffer.py b/alot/buffer.py index 131661d5..b12f353c 100644 --- a/alot/buffer.py +++ b/alot/buffer.py @@ -271,7 +271,7 @@ class EnvelopeBuffer(Buffer): Buffer.__init__(self, ui, self.body, 'envelope') self._autoparms = {'email': self.get_email} self.bindings = { - 'y': ('send', {}), + 'y': ('send', {'envelope': self}), } def get_email(self): diff --git a/alot/command.py b/alot/command.py index 90035781..7102d89b 100644 --- a/alot/command.py +++ b/alot/command.py @@ -6,7 +6,7 @@ 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. -Notmuch is distributed in the hope that it will be useful, but WITHOUT +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. @@ -31,7 +31,9 @@ import shlex import buffer from settings import config from settings import get_hook +from settings import get_account_by_address import completion +import helper class Command: @@ -274,16 +276,21 @@ class ToggleThreadTagCommand(Command): class SendMailCommand(Command): - def __init__(self, email=None, **kwargs): + def __init__(self, email=None, envelope=None, **kwargs): self.email = email + self.envelope_buffer = envelope Command.__init__(self, **kwargs) def apply(self, ui): - #def onSuccess(): - #ui.infopoup('send successful') - args = shlex.split(config.get('general', 'sendmail_cmd')) - proc = subprocess.Popen(args, stdin=subprocess.PIPE) - proc.communicate(self.email.as_string()) + sname,saddr = helper.parse_addr(self.email.get('From')) + account = get_account_by_address(saddr) + if account.sender.send_mail(self.email): + # ui.infopoup('send successful') + if self.envelope_buffer: + ui.apply_command(BufferCloseCommand(buffer=self.envelope_buffer)) + else: + pass + class ComposeCommand(Command): def __init__(self, email=None, **kwargs): diff --git a/alot/helper.py b/alot/helper.py index 6efb396f..cdc471bc 100644 --- a/alot/helper.py +++ b/alot/helper.py @@ -21,6 +21,7 @@ from datetime import timedelta import shlex import subprocess +import email def shorten(string, maxlen): @@ -46,3 +47,7 @@ def pretty_datetime(d): def cmd_output(command_line): args = shlex.split(command_line) return subprocess.check_output(args) + +def parse_addr(addr): + return email.Utils.parseaddr(addr) + diff --git a/alot/init.py b/alot/init.py index 849993e7..b014ccc6 100755 --- a/alot/init.py +++ b/alot/init.py @@ -60,6 +60,7 @@ def main(): configfilename = os.path.expanduser(args.configfile) settings.setup(configfilename) + # setup logging numeric_loglevel = getattr(logging, args.debug_level.upper(), None) logfilename = os.path.expanduser(args.logfile) @@ -68,9 +69,12 @@ def main(): # get ourselves a database manager dbman = DBManager(path=args.db_path, ro=args.read_only) + # read accounts + accounts = settings.get_accounts() # setup and start interface ui = UI(dbman, logger, + accounts, args.query, args.colours, ) diff --git a/alot/send.py b/alot/send.py new file mode 100644 index 00000000..ea3ac27c --- /dev/null +++ b/alot/send.py @@ -0,0 +1,54 @@ +""" +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> +""" + +from mailbox import Mailbox +import shlex +import subprocess +import logging +import time +import email + + +class Sender: + def send_mail(self, email): + pass + + def __init__(self, mailbox=None): + self.mailbox = mailbox + + def store_mail(self, email): + if self.mailbox: + self.mailbox.lock() + 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) + args = shlex.split(self.cmd) + proc = subprocess.Popen(args, stdin=subprocess.PIPE) + proc.communicate(mail.as_string()) + self.store_mail(mail) + return True diff --git a/alot/settings.py b/alot/settings.py index 8a63f9c1..c4bc4708 100644 --- a/alot/settings.py +++ b/alot/settings.py @@ -19,8 +19,10 @@ Copyright (C) 2011 Patrick Totzke <patricktotzke@gmail.com> import imp import os import mailcap +import logging from ConfigParser import SafeConfigParser +from account import Account DEFAULTS = { @@ -239,3 +241,40 @@ def get_mime_handler(mime_type, key, interactive=True): mime_type, key='copiousoutput') return mc_tuple[1][key] + +def get_accounts(): + allowed = ['realname', + 'address', + 'gpg_key', + 'signature', + 'sender_type', + 'sendmail_command', + 'sent_mailbox'] + manditory = ['realname', 'address'] + sections = config.sections() + accountsections = filter(lambda s: s.startswith('account '), sections) + accounts = [] + for s in accountsections: + options = filter(lambda x: x in allowed, config.options(s)) + args = {} + for o in options: + args[o] = config.get(s, o) + if o in manditory: + manditory.remove(o) + if not manditory: + logging.info(args) + accounts.append(Account(**args)) + else: + pass + # log info + return accounts + + +def get_account_by_address(address): + accounts = get_accounts() + matched = [a for a in accounts if a.address==address] + if len(matched) == 1: + return matched.pop() + else: + return None + @@ -30,9 +30,10 @@ class UI: buffers = [] current_buffer = None - def __init__(self, db, log, initialquery, colourmode): - self.logger = log + def __init__(self, db, log, accounts, initialquery, colourmode): self.dbman = db + self.logger = log + self.accounts = accounts if not colourmode: colourmode = config.getint('general', 'colourmode') |