From 2478edc3fbaac6f9aa62f17af1e18c0b7e4510f0 Mon Sep 17 00:00:00 2001 From: Oleg Broytman Date: Sun, 8 Jun 2014 17:15:56 +0400 Subject: [PATCH] Make option -i to work with multipart messages --- ChangeLog | 2 ++ TODO | 3 --- mimedecode.docbook | 6 ++++++ mimedecode.py | 20 ++++++++++++++++++-- test/expected/msg_36-1.txt | 22 ++++++++++++++++++++++ test/test_all | 1 + 6 files changed, 49 insertions(+), 5 deletions(-) create mode 100644 test/expected/msg_36-1.txt diff --git a/ChangeLog b/ChangeLog index a63d023..0b51d69 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,7 @@ Version 2.5.1 (2014-04-??) + Make option -i to work with multipart messages. + Open all files in binary mode. Output os.linesep instead of '\n'. Test --save-headers|body|message masks one after another to allow diff --git a/TODO b/TODO index 36e44bf..864a50f 100644 --- a/TODO +++ b/TODO @@ -2,9 +2,6 @@ Add option -I to completely skip (remove) subparts -- no headers, no body, no boundary, nothing. -Extend options -eIi to multipart subparts. - - Never touch multipart/encrypted and multipart/signed (but allow -I). diff --git a/mimedecode.docbook b/mimedecode.docbook index 67011a6..7ca12cd 100644 --- a/mimedecode.docbook +++ b/mimedecode.docbook @@ -520,6 +520,12 @@ cat input_file | mimedecode.py -o output_file earlier than -B */*. + + Option -i can also ignore a multipart subpart of a MIME message. In that case + the entire subtree of that multipart is removed and replaced with ignore + warning. + + Initially all 5 lists are empty, so without any additional parameters the program always uses the default decoding (as -t */*). diff --git a/mimedecode.py b/mimedecode.py index 6567e99..7d96a3d 100755 --- a/mimedecode.py +++ b/mimedecode.py @@ -377,6 +377,24 @@ 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.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 + output_headers(msg) if msg.preamble: # Preserve the first part, it is probably not a RFC822-message @@ -385,8 +403,6 @@ def decode_multipart(msg): output(os.linesep) first_subpart = True - boundary = msg.get_boundary() - for subpart in msg.get_payload(): if boundary: if first_subpart: diff --git a/test/expected/msg_36-1.txt b/test/expected/msg_36-1.txt new file mode 100644 index 0000000..40c6d8b --- /dev/null +++ b/test/expected/msg_36-1.txt @@ -0,0 +1,22 @@ +From test Sat Feb 1 00:00:00 2014 +Mime-Version: 1.0 +Content-Type: Multipart/Mixed; Boundary="NextPart" +To: IETF-Announce:; +From: Internet-Drafts@ietf.org +Subject: I-D ACTION:draft-ietf-mboned-mix-00.txt +Date: Tue, 22 Dec 1998 16:55:06 -0500 + +--NextPart + +Blah blah blah + +--NextPart +Content-Type: Multipart/Alternative; Boundary="OtherAccess" + + +Message body of type multipart/alternative skipped. + +--OtherAccess-- + +--NextPart-- + diff --git a/test/test_all b/test/test_all index c6a5498..ff48acc 100755 --- a/test/test_all +++ b/test/test_all @@ -60,6 +60,7 @@ 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 '*/*' test_file msg_13.txt msg_13.txt -t 'text/*' -b 'image/*' -B '*/*' +test_file msg_36.txt msg_36-1.txt -i multipart/alternative # Test --save-* test2() { -- 2.39.5