#! /usr/bin/env python
"""Decode MIME message"""
-import sys, os
+import os
import subprocess
+import sys
+
from mimedecode_version import __version__, \
__author__, __copyright__, __license__
+
if sys.version_info[0] >= 3:
# Replace email.message._formatparam with _formatparam from Python 2.7
# to avoid re-encoding non-ascii params.
""" % (__version__, __copyright__))
if exit: sys.exit(0)
+
def usage(code=0, errormsg=''):
version(0)
sys.stdout.write("""\
# together into the final string.
return ' '.join(rtn)
+
def decode_header(msg, header):
"Decode mail header (if exists) and put it back, if it was encoded"
def _get_exceptions(list):
return [x[1:].lower() for x in list[1:] if x[0] == '-']
+
def _decode_headers_params(msg, header, decode_all_params, param_list):
if decode_all_params:
params = msg.get_params(header=header)
for param in param_list:
decode_header_param(msg, header, param)
+
def _remove_headers_params(msg, header, remove_all_params, param_list):
if remove_all_params:
params = msg.get_params(header=header)
for param in param_list:
msg.del_param(param, header)
+
def decode_headers(msg):
"Decode message headers according to global options"
caps = None # Globally stored mailcap database; initialized only if needed
+
def decode_body(msg, s):
"Decode body to plain text using first copiousoutput filter from mailcap"
- import mailcap, tempfile
+ import mailcap
+ import tempfile
global caps
if caps is None:
mimetypes = None
+
def _guess_extension(ctype):
global mimetypes
if mimetypes is None:
mimetypes._db.read(user_mime_type)
return mimetypes.guess_extension(ctype)
+
def _save_message(msg, outstring, save_headers=False, save_body=False):
for header, param in (
("Content-Disposition", "filename"),
global output
save_output = output
outfile = open_output_file(fname)
+
def _output_bytes(s):
if not isinstance(s, bytes):
s = s.encode(g.default_encoding, "replace")
outfile.write(s)
+
output = _output_bytes
if save_headers:
output_headers(msg)
if content_type in g.error_mask:
raise ValueError("content type %s prohibited" % ctype)
+
def decode_multipart(msg):
"Decode multipart"
output_filename = None
destination_dir = os.curdir
+
g = GlobalOptions