- "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)
+
+ # 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 = ctype.split('/')[0]
+ masks.append(mtype + '/*')
+ masks.append('*/*')
+
+ left_binary = False
+ for content_type in masks:
+ if content_type in g.totext_mask or \
+ content_type in g.decoded_binary_mask:
+ break
+ elif content_type in g.binary_mask:
+ left_binary = True
+ break
+ elif content_type in g.fully_ignore_mask:
+ return
+
+ encoding = msg["Content-Transfer-Encoding"]
+ if left_binary or encoding in (None, '', '7bit', '8bit', 'binary'):
+ outstring = msg.get_payload()
+ else: # Decode from transfer ecoding to text or binary form
+ outstring = 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, g.host_name, me)
+
+ for content_type in masks:
+ if content_type in g.totext_mask:
+ outstring = totext(msg, outstring)
+ break
+ elif content_type in g.binary_mask or \
+ content_type in g.decoded_binary_mask:
+ output_headers(msg)
+ output(outstring)
+ break
+ elif content_type in g.ignore_mask:
+ output_headers(msg)
+ output("%sMessage body of type %s skipped.%s" % (os.linesep, ctype, os.linesep))
+ break
+ elif content_type in g.error_mask:
+ break
+ else:
+ # Neither content type nor masks were listed - decode by default
+ outstring = totext(msg, outstring)
+
+ for content_type in masks:
+ if content_type in g.save_headers_mask:
+ _save_message(msg, outstring, save_headers=True, save_body=False)
+ if content_type in g.save_body_mask:
+ _save_message(msg, outstring, save_headers=False, save_body=True)
+ if content_type in g.save_message_mask:
+ _save_message(msg, outstring, save_headers=True, save_body=True)
+
+ for content_type in masks:
+ if content_type in g.error_mask:
+ raise ValueError("content type %s prohibited" % ctype)
+
+def decode_multipart(msg):
+ "Decode multipart"
+
+ decode_headers(msg)
+ boundary = msg.get_boundary()
+
+ masks = []
+ ctype = msg.get_content_type()
+ if ctype:
+ masks.append(ctype)
+ mtype = ctype.split('/')[0]
+ masks.append(mtype + '/*')
+ masks.append('*/*')
+
+ for content_type in masks:
+ if content_type in g.fully_ignore_mask:
+ return
+ elif content_type in g.ignore_mask:
+ output_headers(msg)
+ output("%sMessage body of type %s skipped.%s" % (os.linesep, ctype, os.linesep))
+ if boundary:
+ output("%s--%s--%s" % (os.linesep, boundary, os.linesep))
+ return
+
+ for content_type in masks:
+ if content_type in g.save_body_mask or \
+ content_type in g.save_message_mask:
+ _out_l = []
+ first_subpart = True
+ for subpart in msg.get_payload():
+ if first_subpart:
+ first_subpart = False
+ else:
+ _out_l.append(os.linesep)
+ _out_l.append("--%s%s" % (boundary, os.linesep))
+ _out_l.append(subpart.as_string())
+ _out_l.append("%s--%s--%s" % (os.linesep, boundary, os.linesep))
+ outstring = ''.join(_out_l)
+ break
+ else:
+ outstring = None
+
+ for content_type in masks:
+ if content_type in g.save_headers_mask:
+ _save_message(msg, outstring, save_headers=True, save_body=False)
+ if content_type in g.save_body_mask:
+ _save_message(msg, outstring, save_headers=False, save_body=True)
+ if content_type in g.save_message_mask:
+ _save_message(msg, outstring, save_headers=True, save_body=True)
+
+ for content_type in masks:
+ if content_type in g.error_mask:
+ raise ValueError("content type %s prohibited" % ctype)
+
+ 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)
+ if msg.preamble is not None:
+ output(os.linesep)
+
+ first_subpart = True
+ for subpart in msg.get_payload():
+ if boundary:
+ if first_subpart:
+ first_subpart = False
+ else:
+ output(os.linesep)
+ output("--%s%s" % (boundary, os.linesep))
+
+ # Recursively decode all parts of the subpart
+ decode_message(subpart)
+
+ if boundary:
+ output("%s--%s--%s" % (os.linesep, boundary, os.linesep))
+
+ if msg.epilogue:
+ output(msg.epilogue)
+
+
+def decode_message(msg):
+ "Decode message"
+
+ if msg.is_multipart():
+ decode_multipart(msg)
+ elif len(msg): # Simple one-part message (there are headers) - decode it
+ decode_part(msg)
+ else: # Not a message, just text - copy it literally
+ output(msg.as_string())
+
+
+def open_output_file(filename):
+ fullpath = os.path.abspath(os.path.join(g.destination_dir, filename))
+ full_dir = os.path.dirname(fullpath)
+ create = not os.path.isdir(full_dir)
+ if create:
+ os.makedirs(full_dir)
+ try:
+ return open(fullpath, 'wb')
+ except:
+ if create:
+ os.removedirs(full_dir)