From fbcec8a2828522a62c1c07877c3448d7a2a011eb Mon Sep 17 00:00:00 2001 From: Patrick Totzke Date: Thu, 2 Feb 2012 21:43:40 +0000 Subject: use asyncronous call to sendmail in account.send_mail and use call/errbacks in envelope accordingly --- alot/account.py | 20 ++++++++++++++++---- alot/commands/envelope.py | 34 +++++++++++++++++----------------- alot/helper.py | 6 ++++++ 3 files changed, 39 insertions(+), 21 deletions(-) diff --git a/alot/account.py b/alot/account.py index c7f0df89..c644abe1 100644 --- a/alot/account.py +++ b/alot/account.py @@ -182,11 +182,23 @@ class SendmailAccount(Account): def send_mail(self, mail): mail['Date'] = email.utils.formatdate(time.time(), True) cmdlist = shlex.split(self.cmd.encode('utf-8', errors='ignore')) - out, err, retval = helper.call_cmd(cmdlist, stdin=mail.as_string()) - if err: - errmsg = '%s. sendmail_cmd set to: %s' % (err, self.cmd) + + def cb(out): + logging.info('sent mail successfully') + logging.info(out) + + def errb(failure): + termobj = failure.value + errmsg = '%s\nsendmail_cmd set to: %s' % (str(termobj), self.cmd) + logging.error(errmsg) + logging.error(failure.getTraceback()) + logging.error(failure.value.stderr) raise SendingMailFailed(errmsg) - self.store_sent_mail(mail) + + d = helper.call_cmd_async(cmdlist, stdin=mail.as_string()) + d.addCallback(cb) + d.addErrback(errb) + return d class AccountManager(object): diff --git a/alot/commands/envelope.py b/alot/commands/envelope.py index af26b1ed..3dcc19f8 100644 --- a/alot/commands/envelope.py +++ b/alot/commands/envelope.py @@ -125,28 +125,28 @@ class SendCommand(Command): # send clearme = ui.notify('sending..', timeout=-1) + mail = envelope.construct_mail() def afterwards(returnvalue): + logging.debug('mail sent successfully') ui.clear_notify([clearme]) - if returnvalue == 'success': # successfully send mail - envelope.sent_time = datetime.datetime.now() - ui.apply_command(commands.globals.BufferCloseCommand()) - ui.notify('mail send successfully') - else: - ui.notify('failed to send: %s' % returnvalue, - priority='error') - - def thread_code(): - mail = envelope.construct_mail() - try: - account.send_mail(mail) - except SendingMailFailed as e: - return unicode(e) - else: - return 'success' + envelope.sent_time = datetime.datetime.now() + ui.apply_command(commands.globals.BufferCloseCommand()) + ui.notify('mail sent successfully') + # add mail to index + logging.debug('adding new mail to index') + account.store_sent_mail(mail) + + def errb(failure): + ui.clear_notify([clearme]) + failure.trap(SendingMailFailed) + errmsg = 'failed to send: %s' % failure.value + ui.notify(errmsg, priority='error') - d = threads.deferToThread(thread_code) + d = account.send_mail(mail) d.addCallback(afterwards) + d.addErrback(errb) + logging.debug('added errbacks,callbacks') @registerCommand(MODE, 'edit') diff --git a/alot/helper.py b/alot/helper.py index 752c2fb8..ac980124 100644 --- a/alot/helper.py +++ b/alot/helper.py @@ -15,6 +15,7 @@ from twisted.internet import reactor from twisted.internet.protocol import ProcessProtocol from twisted.internet.defer import Deferred import StringIO +import logging from settings import config @@ -286,10 +287,15 @@ def call_cmd_async(cmdlist, stdin=None): self.deferred.errback(terminated_obj) d = Deferred() + environment = os.environ + logging.debug('ENV = %s' % environment) proc = reactor.spawnProcess(_EverythingGetter(d), executable=cmdlist[0], + env=environment, args=cmdlist[1:]) if stdin: + logging.debug('writing to stdin') proc.write(stdin) + proc.closeStdin() return d -- cgit v1.2.3