From 8adffebc1371154f9c55e34b4c9d04a11923e637 Mon Sep 17 00:00:00 2001 From: Patrick Totzke Date: Wed, 20 Mar 2013 20:29:20 +0000 Subject: respect mailcap commands that expect stdin This ensures that we respect if a command as specified per mailcap expects its input via stdin and not per tempfile. This was already done for opening of attachments but not for interpreting non-text/plain message parts. fix #584 --- alot/db/utils.py | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) (limited to 'alot/db') diff --git a/alot/db/utils.py b/alot/db/utils.py index 2ee9e03e..1280667a 100644 --- a/alot/db/utils.py +++ b/alot/db/utils.py @@ -86,31 +86,43 @@ def extract_body(mail, types=None): #get mime handler key = 'copiousoutput' handler, entry = settings.mailcap_find_match(ctype, key=key) + tempfile_name = None + stdin = None if entry: - # open tempfile, respect mailcaps nametemplate - nametemplate = entry.get('nametemplate', '%s') - prefix, suffix = parse_mailcap_nametemplate(nametemplate) - tmpfile = tempfile.NamedTemporaryFile(delete=False, - prefix=prefix, - suffix=suffix) - # write payload to tmpfile - tmpfile.write(raw_payload) - tmpfile.close() + handler_raw_commandstring = entry['view'] + # in case the mailcap defined command contains no '%s', + # we pipe the files content to the handling command via stdin + if '%s' in handler_raw_commandstring: + # open tempfile, respect mailcaps nametemplate + nametemplate = entry.get('nametemplate', '%s') + prefix, suffix = parse_mailcap_nametemplate(nametemplate) + tmpfile = tempfile.NamedTemporaryFile(delete=False, + prefix=prefix, + suffix=suffix) + # write payload to tmpfile + tmpfile.write(raw_payload) + tmpfile.close() + tempfile_name = tmpfile.name + else: + stdin = raw_payload # read parameter, create handler command parms = tuple(map('='.join, part.get_params())) # create and call external command cmd = mailcap.subst(entry['view'], ctype, - filename=tmpfile.name, plist=parms) + filename=tempfile_name, plist=parms) logging.debug('command: %s' % cmd) logging.debug('parms: %s' % str(parms)) cmdlist = split_commandstring(cmd) # call handler - rendered_payload, errmsg, retval = helper.call_cmd(cmdlist) + rendered_payload, errmsg, retval = helper.call_cmd(cmdlist, stdin=stdin) + # remove tempfile - os.unlink(tmpfile.name) + if tempfile_name: + os.unlink(tempfile_name) + if rendered_payload: # handler had output body_parts.append(string_sanitize(rendered_payload)) return u'\n\n'.join(body_parts) -- cgit v1.2.3