From bc6318c1a631d838dfe6bfe1c94638c59ea626bf Mon Sep 17 00:00:00 2001 From: Oleg Broytman Date: Sun, 8 Jun 2014 18:20:53 +0400 Subject: [PATCH] Add option -I to completely ignore a part --- ChangeLog | 3 +++ TODO | 4 ---- mimedecode.docbook | 19 +++++++++++++++---- mimedecode.py | 13 ++++++++++--- test/expected/msg_36-2.txt | 16 ++++++++++++++++ test/test_all | 1 + 6 files changed, 45 insertions(+), 11 deletions(-) create mode 100644 test/expected/msg_36-2.txt diff --git a/ChangeLog b/ChangeLog index 4011e27..dfcac6e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -2,6 +2,9 @@ Version 2.5.1 (2014-04-??) Make options -e/-i to work with multipart messages. + Add option -I to completely ignore a part - no headers, no body, + no warning. + Open all output 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 864a50f..ff19c32 100644 --- a/TODO +++ b/TODO @@ -1,7 +1,3 @@ -Add option -I to completely skip (remove) subparts -- no headers, no body, -no boundary, nothing. - - Never touch multipart/encrypted and multipart/signed (but allow -I). diff --git a/mimedecode.docbook b/mimedecode.docbook index d01f492..61a30b8 100644 --- a/mimedecode.docbook +++ b/mimedecode.docbook @@ -90,7 +90,7 @@ - + @@ -422,6 +422,17 @@ + + -I mask + + + Append mask to the list of content types to completely ignore. + There will be no output - no headers, no body, no warning. For a + multipart part the entire subtree is removed. + + + + -i mask @@ -521,9 +532,9 @@ cat input_file | mimedecode.py -o output_file - Options -e/-i can also work with multipart subparts of a MIME message. In - case of -i the entire subtree of that multipart is removed and replaced with - ignore warning. + Options -e/-I/-i can also work with multipart subparts of a MIME message. In + case of -I/-i the entire subtree of that multipart is removed; with -i it's + replaced with ignore warning. diff --git a/mimedecode.py b/mimedecode.py index d239a1f..8d50b7d 100755 --- a/mimedecode.py +++ b/mimedecode.py @@ -333,6 +333,8 @@ def decode_part(msg): 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'): @@ -388,7 +390,9 @@ def decode_multipart(msg): masks.append('*/*') for content_type in masks: - if content_type in g.ignore_mask: + 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: @@ -476,7 +480,8 @@ class GlobalOptions: totext_mask = [] # A list of content-types to decode 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) + ignore_mask = [] # Ignore (do not decode and do not include into output) but output a warning instead of the body + fully_ignore_mask = [] # Completely ignore - no headers, no body, no warning error_mask = [] # Raise error if encounter one of these save_counter = 0 @@ -496,7 +501,7 @@ def get_opts(): try: options, arguments = getopt(sys.argv[1:], - 'hVcCDPH:f:d:p:r:R:b:B:e:i:t:O:o:', + 'hVcCDPH:f:d:p:r:R:b:B:e:I:i:t:O:o:', ['help', 'version', 'host=', 'save-headers=', 'save-body=', 'save-message=', 'set-header=', 'set-param=']) @@ -545,6 +550,8 @@ def get_opts(): g.binary_mask.append(value) elif option == '-b': g.decoded_binary_mask.append(value) + elif option == '-I': + g.fully_ignore_mask.append(value) elif option == '-i': g.ignore_mask.append(value) elif option == '-e': diff --git a/test/expected/msg_36-2.txt b/test/expected/msg_36-2.txt new file mode 100644 index 0000000..e44d9c4 --- /dev/null +++ b/test/expected/msg_36-2.txt @@ -0,0 +1,16 @@ +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 + +--NextPart-- + diff --git a/test/test_all b/test/test_all index ff48acc..7eb656e 100755 --- a/test/test_all +++ b/test/test_all @@ -61,6 +61,7 @@ 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_file msg_36.txt msg_36-2.txt -I multipart/alternative # Test --save-* test2() { -- 2.39.2