From 607e68504d32244e26d4a4c01532f5168f03f4e8 Mon Sep 17 00:00:00 2001 From: Oleg Broytman Date: Mon, 10 Mar 2014 20:06:14 +0400 Subject: [PATCH] Add option -B to skip content-transfer-decoding --- ANNOUNCE | 2 + TODO | 8 +--- mimedecode.docbook | 14 +++++- mimedecode.py | 36 ++++++++------ test/expected/msg_13-1.txt | 96 ++++++++++++++++++++++++++++++++++++++ test/test_all | 1 + 6 files changed, 136 insertions(+), 21 deletions(-) create mode 100644 test/expected/msg_13-1.txt diff --git a/ANNOUNCE b/ANNOUNCE index cf2f308..58e552e 100644 --- a/ANNOUNCE +++ b/ANNOUNCE @@ -31,6 +31,8 @@ level). Add option --set-param=header:param=value to set header parameter's value (only at the top level). The header must exist. + Add option -B to skip content-transfer-decoding binary attachments. + WHAT'S NEW in version 2.4.0 (2014-03-08) Change option -d to accept a comma-separated list of headers: diff --git a/TODO b/TODO index 53d09af..29cc0a7 100644 --- a/TODO +++ b/TODO @@ -1,11 +1,7 @@ -Add options --save-headers, --save-body and --save-message to save -decoded headers/bodies/messages of parts to files. - Add option -O to set the destination directory. - -Add an option -B to skip content-transfer-decoding binary attachments (leave it -as base64 or such). +Add options --save-headers, --save-body and --save-message to save +decoded headers/bodies/messages of parts to files. Release 2.5.0. diff --git a/mimedecode.docbook b/mimedecode.docbook index 8a939f6..4011979 100644 --- a/mimedecode.docbook +++ b/mimedecode.docbook @@ -90,7 +90,7 @@ - + @@ -394,6 +394,16 @@ + + -B mask + + + Append mask to the list of binary content types that will be not + content-transfer-decoded (will be left as base64 or such). + + + + -e mask @@ -440,7 +450,7 @@ cat input_file | mimedecode.py -o output_file - The 4 list options (-beit) require more explanation. They allow a user to + The 5 list options (-Bbeit) require more explanation. They allow a user to control body decoding with great flexibility. Think about said mail archive; for example, its maintainer wants to put there only texts, convert Postscript/PDF to text, pass HTML and images as is, and ignore everything diff --git a/mimedecode.py b/mimedecode.py index 50bdcd4..c85bb8d 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 = 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) + 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 @@ -373,7 +380,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 @@ -388,7 +396,7 @@ def get_opt(): 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 +439,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': diff --git a/test/expected/msg_13-1.txt b/test/expected/msg_13-1.txt new file mode 100644 index 0000000..e5728bb --- /dev/null +++ b/test/expected/msg_13-1.txt @@ -0,0 +1,96 @@ +From test Sat Feb 1 00:00:00 2014 +MIME-Version: 1.0 +From: Barry +To: Dingus Lovers +Subject: Here is your dingus fish +Date: Fri, 20 Apr 2001 19:35:02 -0400 +Content-Type: multipart/mixed; boundary="OUTER" + + +--OUTER +Content-Type: text/plain; charset="us-ascii" + +A text/plain part + +--OUTER +Content-Type: multipart/mixed; boundary=BOUNDARY + + +--BOUNDARY +Content-Type: text/plain; charset="us-ascii" + +Hi there, + +This is the dingus fish. + +--BOUNDARY +Content-Type: image/gif; name="dingusfish.gif" +Content-Transfer-Encoding: base64 +content-disposition: attachment; filename="dingusfish.gif" + +R0lGODdhAAEAAfAAAP///wAAACwAAAAAAAEAAQAC/oSPqcvtD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1bPmHEpaZYZORHNlXUJM07hATwHR8MJYqkwWR +WaIezFhxSFlc8/Fq82hEnpeRozg3ULhhr9lAGtVEkCg5ZNRuuVleBPaZadhG0ZgkyPmDOTOKzViM +YgOcpukKqQcbjAWS0IleQ2ROjdh6A+md1qWdBRSX7iSYgFRTtRmBpJioieXJiHfJiMGIR9fJOn8I +MSfXYhspn4ooSa2mSAj4n+8Bmg03fBJZoPOJgsVZRxu1oOMRPXYYjdqjihFaEoZpXBREanuJoRI6 +cibFinq4ngUKh/wQd/H5ofYCZ0HJXR62opZFaAT0iFIZo4DIiUojkjeqKiuoZirKo5Y1a7AWckGa +BkuYoD5lpDK6eUs6CkDqpETwl1EqpfhJpVeKpVl6EgUAADs= + +--BOUNDARY-- + +--OUTER-- diff --git a/test/test_all b/test/test_all index 4401053..52b0313 100755 --- a/test/test_all +++ b/test/test_all @@ -58,6 +58,7 @@ test_file msg_01.txt msg_01-2.txt --set-header=X-Test:set --set-param=X-Test:tes test_file msg_02.txt msg_02.txt --set-param=X-Test:test:set test_file msg_02.txt msg_02-1.txt --set-param=X-Mailer:test=set test_file msg_02.txt msg_02-1.txt --set-param=X-Mailer:test:set +test_file msg_13.txt msg_13-1.txt -B '*/*' if [ "$RC" -eq 0 ]; then echo "All tests passed!" -- 2.39.2