X-Git-Url: https://git.phdru.name/?a=blobdiff_plain;ds=sidebyside;f=mimedecode.py;h=64281715ac0a213f584eb66e41ea20936bde1bd7;hb=fa2d7bb797e9f0735f299cd19192ac3782475a96;hp=70776d3d1401bb3c39d31ef512ff853a2265d0d4;hpb=cb1f0763841bbeced908b053960baf228b692ca1;p=mimedecode.git diff --git a/mimedecode.py b/mimedecode.py index 70776d3..6428171 100755 --- a/mimedecode.py +++ b/mimedecode.py @@ -18,7 +18,7 @@ Broytman mimedecode.py version %s, %s def usage(code=0, errormsg=''): version(0) sys.stdout.write("""\ - 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] [-beit mask] [-o output_file] [input_file [output_file]] + 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] [-o output_file] [input_file [output_file]] """ % me) if errormsg: sys.stderr.write(errormsg + '\n') @@ -272,14 +272,6 @@ def decode_part(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 = [] @@ -291,11 +283,26 @@ def decode_part(msg): masks.append(mtype + '/*') masks.append('*/*') + left_binary = False + for content_type in masks: + if content_type in gopts.binary_mask: + left_binary = True + break + + 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, gopts.host_name, me) + for content_type in masks: if content_type in gopts.totext_mask: totext(msg, outstring) return - elif content_type in gopts.binary_mask: + elif content_type in gopts.binary_mask or \ + content_type in gopts.decoded_binary_mask: output_headers(msg) output(outstring) return @@ -318,12 +325,19 @@ def decode_multipart(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("\n") + first_subpart = True boundary = msg.get_boundary() for subpart in msg.get_payload(): if boundary: - output("\n--%s\n" % boundary) + if first_subpart: + first_subpart = False + else: + output("\n") + output("--%s\n" % boundary) # Recursively decode all parts of the subpart decode_message(subpart) @@ -373,7 +387,8 @@ class GlobalOptions: set_header_param = [] totext_mask = [] # A list of content-types to decode - binary_mask = [] # A list to pass through + binary_mask = [] # A list of content-types to pass through + decoded_binary_mask = [] # A list of content-types to pass through (content-transfer-decoded) ignore_mask = [] # Ignore (skip, do not decode and do not include into output) error_mask = [] # Raise error if encounter one of these @@ -383,12 +398,12 @@ class GlobalOptions: gopts = GlobalOptions -def get_opt(): +def get_opts(): from getopt import getopt, GetoptError try: options, arguments = getopt(sys.argv[1:], - 'hVcCDPH:f:d:p:r:R:b:e:i:t:o:', + 'hVcCDPH:f:d:p:r:R:b:B:e:i:t:o:', ['help', 'version', 'host=', 'set-header=', 'set-param=']) except GetoptError: usage(1) @@ -431,8 +446,10 @@ def get_opt(): gopts.set_header_param.append((header, param, value)) elif option == '-t': gopts.totext_mask.append(value) - elif option == '-b': + elif option == '-B': gopts.binary_mask.append(value) + elif option == '-b': + gopts.decoded_binary_mask.append(value) elif option == '-i': gopts.ignore_mask.append(value) elif option == '-e': @@ -446,7 +463,7 @@ def get_opt(): if __name__ == "__main__": - arguments = get_opt() + arguments = get_opts() la = len(arguments) if la == 0: @@ -500,7 +517,8 @@ if __name__ == "__main__": msg[header] = value for header, param, value in gopts.set_header_param: - msg.set_param(param, value, header) + if header in msg: + msg.set_param(param, value, header) try: decode_message(msg)