X-Git-Url: https://git.phdru.name/?a=blobdiff_plain;f=mimedecode.py;h=3f5879ef4496d7a64cdc89026dd328007615b6d6;hb=3eb76865c7fd23c300d8d60c42c74039532587c3;hp=aa915434f09574b8fd9d4ec5ef3d2ab40f355549;hpb=1b8fe309964ed2c651f3453ef98896a0301fe463;p=mimedecode.git diff --git a/mimedecode.py b/mimedecode.py index aa91543..3f5879e 100755 --- a/mimedecode.py +++ b/mimedecode.py @@ -1,10 +1,13 @@ #! /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. @@ -19,6 +22,7 @@ Broytman mimedecode.py version %s, %s """ % (__version__, __copyright__)) if exit: sys.exit(0) + def usage(code=0, errormsg=''): version(0) sys.stdout.write("""\ @@ -80,6 +84,7 @@ def _decode_header(s, strip=True): # 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" @@ -107,6 +112,7 @@ def decode_header_param(msg, header, param): 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) @@ -118,6 +124,7 @@ def _decode_headers_params(msg, header, decode_all_params, param_list): 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) @@ -139,6 +146,7 @@ def _remove_headers_params(msg, header, remove_all_params, param_list): for param in param_list: msg.del_param(param, header) + def decode_headers(msg): "Decode message headers according to global options" @@ -213,10 +221,12 @@ def set_content_type(msg, newtype, charset=None): 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: @@ -296,6 +306,7 @@ def totext(msg, instring): mimetypes = None + def _guess_extension(ctype): global mimetypes if mimetypes is None: @@ -306,6 +317,7 @@ def _guess_extension(ctype): 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"), @@ -334,10 +346,12 @@ def _save_message(msg, outstring, save_headers=False, save_body=False): 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) @@ -411,6 +425,7 @@ def decode_part(msg): if content_type in g.error_mask: raise ValueError("content type %s prohibited" % ctype) + def decode_multipart(msg): "Decode multipart" @@ -557,6 +572,7 @@ class GlobalOptions: output_filename = None destination_dir = os.curdir + g = GlobalOptions