3 from . import mimedecode
8 Broytman mimedecode.py version %s, %s
9 """ % (mimedecode.__version__, mimedecode.__copyright__))
14 def usage(code=0, errormsg=''):
17 Usage: %s [-h|--help] [-V|--version] [-cCDP] [-H|--host=hostname] [-f charset] [-d header1[,h2,...]|*[,-h1,...]] [-p header1[,h2,h3,...]:param1[,p2,p3,...]] [-r header1[,h2,...]|*[,-h1,...]] [-R header1[,h2,h3,...]:param1[,p2,p3,...]] [--set-header header:value] [--set-param header:param=value] [-Bbeit mask] [--save-headers|body|message mask] [-O dest_dir] [-o output_file] [input_file [output_file]]
20 sys.stderr.write(errormsg + os.linesep)
25 from m_lib.defenc import default_encoding
26 recode_charset = 1 # recode charset of message body
30 # A list of headers to decode
31 decode_headers = ["From", "To", "Cc", "Reply-To", "Mail-Followup-To",
34 # A list of headers parameters to decode
35 decode_header_params = [
36 ("Content-Type", "name"),
37 ("Content-Disposition", "filename"),
40 # A list of headers to remove
42 # A list of headers parameters to remove
43 remove_headers_params = []
45 # A list of header/value pairs to set
47 # A list of header/parameter/value triples to set
50 totext_mask = [] # A list of content-types to decode
51 binary_mask = [] # A list of content-types to pass through
52 # A list of content-types to pass through (content-transfer-decoded).
53 decoded_binary_mask = []
54 # Ignore (do not decode and do not include into output)
55 # but output a warning instead of the body.
57 # Completely ignore - no headers, no body, no warning.
58 fully_ignore_mask = []
59 error_mask = [] # Raise error if encounter one of these
62 save_headers_mask = []
64 save_message_mask = []
67 output_filename = None
68 destination_dir = os.curdir
71 g = mimedecode.g = GlobalOptions
75 from getopt import getopt, GetoptError
78 options, arguments = getopt(
80 'hVcCDPH:f:d:p:r:R:b:B:e:I:i:t:O:o:',
81 ['help', 'version', 'host=',
82 'save-headers=', 'save-body=', 'save-message=',
83 'set-header=', 'set-param='])
87 for option, value in options:
88 if option in ('-h', '--help'):
90 elif option in ('-V', '--version'):
96 elif option in ('-H', '--host'):
99 g.default_encoding = value
101 if value.startswith('*'):
102 g.decode_headers = []
103 g.decode_headers.append(value)
105 g.decode_headers = []
107 g.decode_header_params.append(value.split(':', 1))
109 g.decode_header_params = []
111 g.remove_headers.append(value)
113 g.remove_headers_params.append(value.split(':', 1))
114 elif option == '--set-header':
115 g.set_header_value.append(value.split(':', 1))
116 elif option == '--set-param':
117 header, value = value.split(':', 1)
119 param, value = value.split('=', 1)
121 param, value = value.split(':', 1)
122 g.set_header_param.append((header, param, value))
124 g.totext_mask.append(value)
126 g.binary_mask.append(value)
128 g.decoded_binary_mask.append(value)
130 g.fully_ignore_mask.append(value)
132 g.ignore_mask.append(value)
134 g.error_mask.append(value)
135 elif option == '--save-headers':
136 g.save_headers_mask.append(value)
137 elif option == '--save-body':
138 g.save_body_mask.append(value)
139 elif option == '--save-message':
140 g.save_message_mask.append(value)
142 g.destination_dir = value
144 g.output_filename = value
152 arguments = get_opts()
156 g.input_filename = '-'
158 if g.output_filename:
159 outfile = open_output_file(g.output_filename)
161 g.output_filename = '-'
164 if (arguments[0] == '-'):
165 g.input_filename = '-'
168 g.input_filename = arguments[0]
169 infile = open(arguments[0], 'r')
171 if g.output_filename:
172 outfile = open_output_file(g.output_filename)
174 g.output_filename = '-'
177 if g.output_filename:
178 usage(1, 'Too many output filenames')
179 if (arguments[1] == '-'):
180 g.output_filename = '-'
183 g.output_filename = arguments[1]
184 outfile = open_output_file(g.output_filename)
186 usage(1, 'Too many arguments')
188 if (infile is sys.stdin) and sys.stdin.isatty():
189 if (outfile is sys.stdout) and sys.stdout.isatty():
191 usage(1, 'Filtering from console is forbidden')
195 g.host_name = socket.gethostname()
198 if hasattr(outfile, 'buffer'):
200 if not isinstance(s, bytes):
201 s = s.encode(g.default_encoding, "replace")
202 outfile.buffer.write(s)
203 output = output_bytes
205 output = outfile.write
206 mimedecode.output = output
209 msg = email.message_from_file(infile)
211 for header, value in g.set_header_value:
212 mimedecode.set_header(msg, header, value)
214 for header, param, value in g.set_header_param:
216 msg.set_param(param, value, header)
219 mimedecode.decode_message(msg)
225 if __name__ == "__main__":