From eff0fc85b15fae0cb100cbd7bc5b25a6bed941e8 Mon Sep 17 00:00:00 2001 From: Oleg Broytman Date: Sun, 23 Feb 2014 20:25:16 +0400 Subject: [PATCH] Add option -R to remove header's parameters --- mimedecode.docbook | 17 +++++++++++++++-- mimedecode.py | 12 ++++++++++-- test/expected/msg_29-1.txt | 21 +++++++++++++++++++++ test/test_all | 1 + 4 files changed, 47 insertions(+), 4 deletions(-) create mode 100644 test/expected/msg_29-1.txt diff --git a/mimedecode.docbook b/mimedecode.docbook index 0605fa8..87bd364 100644 --- a/mimedecode.docbook +++ b/mimedecode.docbook @@ -56,6 +56,9 @@ + + + @@ -214,7 +217,7 @@ command-line options. -p header:param - Add the pair (header, param) to a list of headers' parameters to + Add the pair (header, param) to a list of headers parameters to decode; initially the list contains header "Content-Type", parameter "name" and header "Content-Disposition", parameter "filename". @@ -226,7 +229,7 @@ command-line options. -P - Clear the list of headers' parameters to decode (make it empty). + Clear the list of headers parameters to decode (make it empty). @@ -241,6 +244,16 @@ command-line options. + + -R header:param + + + Add the pair (header, param) to a list of headers parameters to + remove; initially the list is empty. + + + + -b mask diff --git a/mimedecode.py b/mimedecode.py index c3e3ecf..1edd170 100755 --- a/mimedecode.py +++ b/mimedecode.py @@ -106,6 +106,9 @@ def decode_headers(msg): for header in gopts.remove_headers: del msg[header] + for header, param in gopts.remove_header_params: + msg.del_param(param, header) + def set_header(msg, header, value): "Replace header" @@ -278,7 +281,7 @@ class GlobalOptions: decode_headers = ["From", "To", "Cc", "Reply-To", "Mail-Followup-To", "Subject"] - # A list of headers' parameters to decode + # A list of headers parameters to decode decode_header_params = [ ("Content-Type", "name"), ("Content-Disposition", "filename"), @@ -286,6 +289,8 @@ class GlobalOptions: # A list of headers to remove remove_headers = [] + # A list of headers parameters to remove + remove_header_params = [] totext_mask = [] # A list of content-types to decode binary_mask = [] # A list to pass through @@ -302,7 +307,8 @@ def get_opt(): from getopt import getopt, GetoptError try: - options, arguments = getopt(sys.argv[1:], 'hVcCDPH:f:d:p:r:b:e:i:t:o:', + options, arguments = getopt(sys.argv[1:], + 'hVcCDPH:f:d:p:r:R:b:e:i:t:o:', ['help', 'version', 'host']) except GetoptError: usage(1) @@ -330,6 +336,8 @@ def get_opt(): gopts.decode_header_params = [] elif option == '-r': gopts.remove_headers.append(value) + elif option == '-R': + gopts.remove_header_params.append(value.split(':', 1)) elif option == '-t': gopts.totext_mask.append(value) elif option == '-b': diff --git a/test/expected/msg_29-1.txt b/test/expected/msg_29-1.txt new file mode 100644 index 0000000..4a8565f --- /dev/null +++ b/test/expected/msg_29-1.txt @@ -0,0 +1,21 @@ +From test Sat Feb 1 00:00:00 2014 +Return-Path: +Delivered-To: bbb@zzz.org +Received: by mail.zzz.org (Postfix, from userid 889) + id 27CEAD38CC; Fri, 4 May 2001 14:05:44 -0400 (EDT) +Content-Transfer-Encoding: 7bit +Message-ID: <15090.61304.110929.45684@aaa.zzz.org> +From: bbb@ddd.com (John X. Doe) +To: bbb@zzz.org +Subject: This is a test message +Date: Fri, 4 May 2001 14:05:44 -0400 +MIME-Version: 1.0 +Content-Type: text/plain; charset="utf-8" +X-MIME-Autoconverted: from us-ascii to utf-8 by test id mimedecode.py + + +Hi, + +Do you like this message? + +-Me diff --git a/test/test_all b/test/test_all index 19198b1..f08ff8c 100755 --- a/test/test_all +++ b/test/test_all @@ -42,6 +42,7 @@ done test_file msg_15.txt msg_15-1.txt -b text/html test_file msg_15.txt msg_15-2.txt -i text/html test_file msg_22.txt msg_22-1.txt -r content-id +test_file msg_29.txt msg_29-1.txt -R Content-Type:title if [ "$RC" -eq 0 ]; then echo "All tests passed!" -- 2.39.2