- "Decode one part of the message"
-
- decode_headers(msg)
- encoding = msg["Content-Transfer-Encoding"]
-
- if encoding in (None, '', '7bit', '8bit', 'binary'):
- outstring = str(msg.get_payload())
- else: # Decode from transfer ecoding to text or binary form
- outstring = str(msg.get_payload(decode=1))
- set_header(msg, "Content-Transfer-Encoding", "8bit")
- msg["X-MIME-Autoconverted"] = "from %s to 8bit by %s id %s" % (encoding, host_name, me)
-
- # Test all mask lists and find what to do with this content type
- masks = []
- ctype = msg.get_content_type()
- if ctype:
- masks.append(ctype)
- mtype = msg.get_content_maintype()
- if mtype:
- masks.append(mtype + '/*')
- masks.append('*/*')
-
- for content_type in masks:
- if content_type in GlobalOptions.totext_mask:
- totext(msg, outstring)
- return
- elif content_type in GlobalOptions.binary_mask:
- output_headers(msg)
- output(outstring)
- return
- elif content_type in GlobalOptions.ignore_mask:
- output_headers(msg)
- output("\nMessage body of type `%s' skipped.\n" % content_type)
- return
- elif content_type in GlobalOptions.error_mask:
- raise ValueError, "content type `%s' prohibited" % content_type
-
- # Neither content type nor masks were listed - decode by default
- totext(msg, outstring)
-
-
-def decode_file(infile):
- "Decode the entire message"
-
- msg = email.message_from_file(infile)
- boundary = msg.get_boundary()
-
- if msg.is_multipart():
- decode_headers(msg)
- output_headers(msg)
-
- if msg.preamble: # Preserve the first part, it is probably not a RFC822-message
- output(msg.preamble) # Usually it is just a few lines of text (MIME warning)
-
- for subpart in msg.get_payload():
- output("\n--%s\n" % boundary)
- decode_part(subpart)
-
- output("\n--%s--\n" % boundary)
-
- if msg.epilogue:
- output(msg.epilogue)
-
- else:
- if msg.has_key("Content-Type"): # Simple one-part message - decode it
- decode_part(msg)
-
- else: # Not a message, just text - copy it literally
- output(str(msg))
+ "Decode one part of the message"
+
+ decode_headers(msg)
+ encoding = msg["Content-Transfer-Encoding"]
+
+ if encoding in (None, '', '7bit', '8bit', 'binary'):
+ outstring = str(msg.get_payload())
+ else: # Decode from transfer ecoding to text or binary form
+ outstring = str(msg.get_payload(decode=1))
+ set_header(msg, "Content-Transfer-Encoding", "8bit")
+ msg["X-MIME-Autoconverted"] = "from %s to 8bit by %s id %s" % (encoding, gopts.host_name, me)
+
+ # Test all mask lists and find what to do with this content type
+ masks = []
+ ctype = msg.get_content_type()
+ if ctype:
+ masks.append(ctype)
+ mtype = msg.get_content_maintype()
+ if mtype:
+ masks.append(mtype + '/*')
+ masks.append('*/*')
+
+ for content_type in masks:
+ if content_type in gopts.totext_mask:
+ totext(msg, outstring)
+ return
+ elif content_type in gopts.binary_mask:
+ output_headers(msg)
+ output(outstring)
+ return
+ elif content_type in gopts.ignore_mask:
+ output_headers(msg)
+ output("\nMessage body of type `%s' skipped.\n" % content_type)
+ return
+ elif content_type in gopts.error_mask:
+ raise ValueError, "content type `%s' prohibited" % content_type
+
+ # Neither content type nor masks were listed - decode by default
+ totext(msg, outstring)
+
+
+def decode_multipart(msg):
+ "Decode multipart"
+
+ if msg.is_multipart():
+ decode_headers(msg)
+ output_headers(msg)
+
+ if msg.preamble: # Preserve the first part, it is probably not a RFC822-message
+ output(msg.preamble) # Usually it is just a few lines of text (MIME warning)
+
+ boundary = msg.get_boundary()
+
+ for subpart in msg.get_payload():
+ if boundary:
+ output("\n--%s\n" % boundary)
+
+ # Recursively decode all parts of the subpart
+ decode_message(subpart)
+
+ if boundary:
+ output("\n--%s--\n" % boundary)
+
+ if msg.epilogue:
+ output(msg.epilogue)
+
+def decode_message(msg):
+ "Decode message"
+
+ if msg.is_multipart():
+ decode_multipart(msg)
+ elif msg.has_key("Content-Type"): # Simple one-part message - decode it
+ decode_part(msg)
+ else: # Not a message, just text - copy it literally
+ output(msg.as_string())