2 """Decode MIME message"""
6 __version__ = "$Revision$"[11:-2]
7 __revision__ = "$Id$"[5:-2]
8 __date__ = "$Date$"[7:-2]
9 __author__ = "Oleg Broytman <phd@phdru.name>"
10 __copyright__ = "Copyright (C) 2001-2010 PhiloSoft Design"
11 __license__ = "GNU GPL"
18 from cStringIO import StringIO
20 from StringIO import StringIO
24 host_name = socket.gethostname()
26 me = os.path.basename(sys.argv[0])
31 Broytman mimedecode.py version %s, %s
32 """ % (_version, __copyright__))
39 Usage: %s [-h|--help] [-V|--version] [-cCDP] [-f charset] [-d header] [-p header:param] [-beit mask] [filename]
44 def output(s, outfile = sys.stdout):
47 def output_headers(msg, outfile = sys.stdout):
48 unix_from = msg.get_unixfrom()
50 output(unix_from + os.linesep)
51 for key, value in msg.items():
52 output("%s: %s\n" % (key, value), outfile)
53 output("\n", outfile) # End of headers
56 def recode(s, charset):
57 return unicode(s, charset, "replace").encode(GlobalOptions.default_encoding, "replace")
60 def recode2(s, charset):
61 if charset and charset.lower() <> GlobalOptions.default_encoding:
62 s = recode(s, charset)
66 def _decode_header(s):
67 """Return a decoded string according to RFC 2047.
68 NOTE: This is almost the same as email.Utils.decode.
70 from types import ListType
73 L = email.Header.decode_header(s)
74 if not isinstance(L, ListType):
79 for atom, charset in L:
83 rtn.append(recode2(atom, charset))
85 del rtn[-1] # remove the last space
87 # Now that we've decoded everything, we just need to join all the parts
88 # together into the final string.
92 def decode_header(msg, header):
93 "Decode mail header (if exists) and put it back, if it was encoded"
95 if msg.has_key(header):
97 new_value = _decode_header(value)
98 if new_value <> value: # do not bother to touch msg if not changed
99 set_header(msg, header, new_value)
102 def _decode_header_param(s):
103 return recode2(s[2], s[0])
106 def decode_header_param(msg, header, param):
107 "Decode mail header's parameter (if exists) and put it back, if it was encoded"
109 if msg.has_key(header):
110 value = msg.get_param(param, header=header)
112 from types import TupleType
113 if isinstance(value, TupleType):
114 new_value = _decode_header_param(value)
116 new_value = _decode_header(value)
117 if new_value <> value: # do not bother to touch msg if not changed
118 msg.set_param(param, new_value, header)
121 def decode_headers(msg):
122 "Decode message headers according to global options"
124 for header in GlobalOptions.decode_headers:
125 decode_header(msg, header)
127 for header, param in GlobalOptions.decode_header_params:
128 decode_header_param(msg, header, param)
131 def set_header(msg, header, value):
134 if msg.has_key(header):
135 msg.replace_header(header, value)
140 def set_content_type(msg, newtype, charset=None):
141 msg.set_type(newtype)
144 msg.set_param("charset", charset, "Content-Type")
148 caps = None # Globally stored mailcap database; initialized only if needed
150 def decode_body(msg, s):
151 "Decode body to plain text using first copiousoutput filter from mailcap"
153 import mailcap, tempfile
157 caps = mailcap.getcaps()
159 content_type = msg.get_content_type()
160 filename = tempfile.mktemp()
163 entries = mailcap.lookup(caps, content_type, "view")
164 for entry in entries:
165 if entry.has_key('copiousoutput'):
166 if entry.has_key('test'):
167 test = mailcap.subst(entry['test'], content_type, filename)
168 if test and os.system(test) != 0:
170 command = mailcap.subst(entry["view"], content_type, filename)
176 file = open(filename, 'w')
180 pipe = os.popen(command, 'r')
185 set_content_type(msg, "text/plain")
186 msg["X-MIME-Autoconverted"] = "from %s to text/plain by %s id %s" % (content_type, host_name, command.split()[0])
191 def recode_charset(msg, s):
192 "Recode charset of the message to the default charset"
194 save_charset = charset = msg.get_content_charset()
195 if charset and charset.lower() <> GlobalOptions.default_encoding:
196 s = recode2(s, charset)
197 content_type = msg.get_content_type()
198 set_content_type(msg, content_type, GlobalOptions.default_encoding)
199 msg["X-MIME-Autoconverted"] = "from %s to %s by %s id %s" % (save_charset, GlobalOptions.default_encoding, host_name, me)
203 def totext(msg, instring):
204 "Convert instring content to text"
206 if msg.is_multipart(): # Recursively decode all parts of the multipart message
207 newfile = StringIO(str(msg))
212 # Decode body and recode charset
213 s = decode_body(msg, instring)
214 if GlobalOptions.recode_charset:
215 s = recode_charset(msg, s)
221 def decode_part(msg):
222 "Decode one part of the message"
225 encoding = msg["Content-Transfer-Encoding"]
227 if encoding in (None, '', '7bit', '8bit', 'binary'):
228 outstring = str(msg.get_payload())
229 else: # Decode from transfer ecoding to text or binary form
230 outstring = str(msg.get_payload(decode=1))
231 set_header(msg, "Content-Transfer-Encoding", "8bit")
232 msg["X-MIME-Autoconverted"] = "from %s to 8bit by %s id %s" % (encoding, host_name, me)
234 # Test all mask lists and find what to do with this content type
236 ctype = msg.get_content_type()
239 mtype = msg.get_content_maintype()
241 masks.append(mtype + '/*')
244 for content_type in masks:
245 if content_type in GlobalOptions.totext_mask:
246 totext(msg, outstring)
248 elif content_type in GlobalOptions.binary_mask:
252 elif content_type in GlobalOptions.ignore_mask:
254 output("\nMessage body of type `%s' skipped.\n" % content_type)
256 elif content_type in GlobalOptions.error_mask:
257 raise ValueError, "content type `%s' prohibited" % content_type
259 # Neither content type nor masks were listed - decode by default
260 totext(msg, outstring)
263 def decode_file(infile):
264 "Decode the entire message"
266 msg = email.message_from_file(infile)
267 boundary = msg.get_boundary()
269 if msg.is_multipart():
273 if msg.preamble: # Preserve the first part, it is probably not a RFC822-message
274 output(msg.preamble) # Usually it is just a few lines of text (MIME warning)
276 for subpart in msg.get_payload():
277 output("\n--%s\n" % boundary)
280 output("\n--%s--\n" % boundary)
286 if msg.has_key("Content-Type"): # Simple one-part message - decode it
289 else: # Not a message, just text - copy it literally
294 from m_lib.defenc import default_encoding
295 recode_charset = 1 # recode charset of message body
297 decode_headers = ["Subject", "From"] # A list of headers to decode
298 decode_header_params = [("Content-Type", "name"),
299 ("Content-Disposition", "filename")
300 ] # A list of headers' parameters to decode
302 totext_mask = [] # A list of content-types to decode
303 binary_mask = [] # A list to pass through
304 ignore_mask = [] # Ignore (skip, do not decode and do not include into output)
305 error_mask = [] # Raise error if encounter one of these
309 from getopt import getopt, GetoptError
312 options, arguments = getopt(sys.argv[1:], 'hVcCDPf:d:p:b:e:i:t:',
317 for option, value in options:
320 elif option == '--help':
324 elif option == '--version':
327 GlobalOptions.recode_charset = 1
329 GlobalOptions.recode_charset = 0
331 GlobalOptions.default_encoding = value
333 GlobalOptions.decode_headers.append(value)
335 GlobalOptions.decode_headers = []
337 GlobalOptions.decode_header_params.append(value.split(':', 1))
339 GlobalOptions.decode_header_params = []
341 GlobalOptions.totext_mask.append(value)
343 GlobalOptions.binary_mask.append(value)
345 GlobalOptions.ignore_mask.append(value)
347 GlobalOptions.error_mask.append(value)
354 if __name__ == "__main__":
362 elif arguments[0] == '-':
365 infile = open(arguments[0], 'r')