From cb1f0763841bbeced908b053960baf228b692ca1 Mon Sep 17 00:00:00 2001 From: Oleg Broytman Date: Sun, 9 Mar 2014 16:54:51 +0400 Subject: [PATCH] Add option --set-param=header:param=value Add option --set-param=header:param=value to set header parameter's value (only at the top level). --- ANNOUNCE | 3 + TODO | 4 - mimedecode.docbook | 13 ++++ mimedecode.py | 16 +++- test/expected/msg_02-1.txt | 155 +++++++++++++++++++++++++++++++++++++ test/test_all | 2 + 6 files changed, 186 insertions(+), 7 deletions(-) create mode 100644 test/expected/msg_02-1.txt diff --git a/ANNOUNCE b/ANNOUNCE index 27b4e40..e3be73c 100644 --- a/ANNOUNCE +++ b/ANNOUNCE @@ -28,6 +28,9 @@ WHAT'S NEW in version 2.5.0 (2014-03-??) Add option --set-header=header:value to set header's value (only at the top level). + Add option --set-param=header:param=value to set header parameter's value +(only at the top level). + 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 587eb42..53d09af 100644 --- a/TODO +++ b/TODO @@ -1,7 +1,3 @@ -Add option --set-param=header:param=value to set header parameter's value (only -at the top level). - - Add options --save-headers, --save-body and --save-message to save decoded headers/bodies/messages of parts to files. diff --git a/mimedecode.docbook b/mimedecode.docbook index 86e84a6..8a939f6 100644 --- a/mimedecode.docbook +++ b/mimedecode.docbook @@ -86,6 +86,9 @@ + + + @@ -370,6 +373,16 @@ + + --set-param header:param=value + + + The program sets or changes value for the header's parameter to the + given value (only at the top-level message). The header must exist. + + + + -b mask diff --git a/mimedecode.py b/mimedecode.py index 6ed2560..70776d3 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] [-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] [-beit mask] [-o output_file] [input_file [output_file]] """ % me) if errormsg: sys.stderr.write(errormsg + '\n') @@ -370,7 +370,7 @@ class GlobalOptions: # A list of header/value pairs to set set_header_value = [] # A list of header/parameter/value triples to set - #set_header_param = [] + set_header_param = [] totext_mask = [] # A list of content-types to decode binary_mask = [] # A list to pass through @@ -389,7 +389,7 @@ def get_opt(): try: options, arguments = getopt(sys.argv[1:], 'hVcCDPH:f:d:p:r:R:b:e:i:t:o:', - ['help', 'version', 'host=', 'set-header=']) + ['help', 'version', 'host=', 'set-header=', 'set-param=']) except GetoptError: usage(1) @@ -422,6 +422,13 @@ def get_opt(): gopts.remove_headers_params.append(value.split(':', 1)) elif option == '--set-header': gopts.set_header_value.append(value.split(':', 1)) + elif option == '--set-param': + header, value = value.split(':', 1) + if '=' in value: + param, value = value.split('=', 1) + else: + param, value = value.split(':', 1) + gopts.set_header_param.append((header, param, value)) elif option == '-t': gopts.totext_mask.append(value) elif option == '-b': @@ -492,6 +499,9 @@ if __name__ == "__main__": for header, value in gopts.set_header_value: msg[header] = value + for header, param, value in gopts.set_header_param: + msg.set_param(param, value, header) + try: decode_message(msg) finally: diff --git a/test/expected/msg_02-1.txt b/test/expected/msg_02-1.txt new file mode 100644 index 0000000..410e9f3 --- /dev/null +++ b/test/expected/msg_02-1.txt @@ -0,0 +1,155 @@ +From test Sat Feb 1 00:00:00 2014 +MIME-version: 1.0 +From: ppp-request@zzz.org +Sender: ppp-admin@zzz.org +To: ppp@zzz.org +Subject: Ppp digest, Vol 1 #2 - 5 msgs +Date: Fri, 20 Apr 2001 20:18:00 -0400 (EDT) +X-Mailman-Version: 2.0.4 +Content-Type: multipart/mixed; boundary="192.168.1.2.889.32614.987812255.500.21814" +X-Mailer: Mailman v2.0.4; test="set" + + +--192.168.1.2.889.32614.987812255.500.21814 +Content-description: Masthead (Ppp digest, Vol 1 #2) +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 + +Send Ppp mailing list submissions to + ppp@zzz.org + +To subscribe or unsubscribe via the World Wide Web, visit + http://www.zzz.org/mailman/listinfo/ppp +or, via email, send a message with subject or body 'help' to + ppp-request@zzz.org + +You can reach the person managing the list at + ppp-admin@zzz.org + +When replying, please edit your Subject line so it is more specific +than "Re: Contents of Ppp digest..." + + +--192.168.1.2.889.32614.987812255.500.21814 +Content-description: Today's Topics (5 msgs) +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 + +Today's Topics: + + 1. testing #1 (Barry A. Warsaw) + 2. testing #2 (Barry A. Warsaw) + 3. testing #3 (Barry A. Warsaw) + 4. testing #4 (Barry A. Warsaw) + 5. testing #5 (Barry A. Warsaw) + +--192.168.1.2.889.32614.987812255.500.21814 +Content-Type: multipart/digest; boundary="__--__--" + + +--__--__-- + +Message: 1 +Content-Transfer-Encoding: 7bit +Date: Fri, 20 Apr 2001 20:16:13 -0400 +To: ppp@zzz.org +From: barry@digicool.com (Barry A. Warsaw) +Subject: [Ppp] testing #1 +Precedence: bulk +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 + + +hello + + +--__--__-- + +Message: 2 +Date: Fri, 20 Apr 2001 20:16:21 -0400 +Content-Transfer-Encoding: 7bit +To: ppp@zzz.org +From: barry@digicool.com (Barry A. Warsaw) +Precedence: bulk +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 + + +hello + + +--__--__-- + +Message: 3 +Date: Fri, 20 Apr 2001 20:16:25 -0400 +Content-Transfer-Encoding: 7bit +To: ppp@zzz.org +From: barry@digicool.com (Barry A. Warsaw) +Subject: [Ppp] testing #3 +Precedence: bulk +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 + + +hello + + +--__--__-- + +Message: 4 +Date: Fri, 20 Apr 2001 20:16:28 -0400 +Content-Transfer-Encoding: 7bit +To: ppp@zzz.org +From: barry@digicool.com (Barry A. Warsaw) +Subject: [Ppp] testing #4 +Precedence: bulk +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 + + +hello + + +--__--__-- + +Message: 5 +Date: Fri, 20 Apr 2001 20:16:32 -0400 +Content-Transfer-Encoding: 7bit +To: ppp@zzz.org +From: barry@digicool.com (Barry A. Warsaw) +Subject: [Ppp] testing #5 +Precedence: bulk +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 + + +hello + + + + +--__--__---- + +--192.168.1.2.889.32614.987812255.500.21814 +Content-description: Digest Footer +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 + +_______________________________________________ +Ppp mailing list +Ppp@zzz.org +http://www.zzz.org/mailman/listinfo/ppp + + +--192.168.1.2.889.32614.987812255.500.21814-- + +End of Ppp Digest + diff --git a/test/test_all b/test/test_all index 5d4ca8a..c627685 100755 --- a/test/test_all +++ b/test/test_all @@ -54,6 +54,8 @@ test_file msg_16.txt msg_16-2.txt -r \*,-mime-version,-content-type test_file msg_29.txt msg_29-1.txt -R Content-Type:title test_file msg_18.txt msg_18-1.txt -R X-Foobar-Spoink-Defrobnit:\* test_file msg_01.txt msg_01-1.txt --set-header=X-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 if [ "$RC" -eq 0 ]; then echo "All tests passed!" -- 2.39.5