From eee83ff25c87ab0f7abb8b77637f8b24a509250b Mon Sep 17 00:00:00 2001 From: Oleg Broytman Date: Sun, 9 Mar 2014 16:42:00 +0400 Subject: [PATCH] Add option --set-header=header:value Add option --set-header=header:value to set header's value (only at the top level). --- ANNOUNCE | 5 +++++ TODO | 5 ++--- mimedecode.docbook | 13 +++++++++++++ mimedecode.py | 18 +++++++++++++++--- test/expected/msg_01-1.txt | 22 ++++++++++++++++++++++ test/test_all | 1 + 6 files changed, 58 insertions(+), 6 deletions(-) create mode 100644 test/expected/msg_01-1.txt diff --git a/ANNOUNCE b/ANNOUNCE index 5969c15..27b4e40 100644 --- a/ANNOUNCE +++ b/ANNOUNCE @@ -23,6 +23,11 @@ parts ("attachments") all parts are decoded. Decoding can be controlled by command-line options. +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). + 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 058039d..587eb42 100644 --- a/TODO +++ b/TODO @@ -1,6 +1,5 @@ -Add option -s header:value to set header's value. - -Add option -S header:param=value to set header parameter's value. +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 diff --git a/mimedecode.docbook b/mimedecode.docbook index 8fbb51b..86e84a6 100644 --- a/mimedecode.docbook +++ b/mimedecode.docbook @@ -83,6 +83,9 @@ + + + @@ -357,6 +360,16 @@ + + --set-header header:value + + + The program sets or changes value for the header to the given value + (only at the top-level message). + + + + -b mask diff --git a/mimedecode.py b/mimedecode.py index 637e275..6ed2560 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,...]] [-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] [-beit mask] [-o output_file] [input_file [output_file]] """ % me) if errormsg: sys.stderr.write(errormsg + '\n') @@ -367,6 +367,11 @@ class GlobalOptions: # A list of headers parameters to remove remove_headers_params = [] + # A list of header/value pairs to set + set_header_value = [] + # A list of header/parameter/value triples to set + #set_header_param = [] + totext_mask = [] # A list of content-types to decode binary_mask = [] # A list to pass through ignore_mask = [] # Ignore (skip, do not decode and do not include into output) @@ -384,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=']) + ['help', 'version', 'host=', 'set-header=']) except GetoptError: usage(1) @@ -415,6 +420,8 @@ def get_opt(): gopts.remove_headers.append(value) elif option == '-R': gopts.remove_headers_params.append(value.split(':', 1)) + elif option == '--set-header': + gopts.set_header_value.append(value.split(':', 1)) elif option == '-t': gopts.totext_mask.append(value) elif option == '-b': @@ -480,8 +487,13 @@ if __name__ == "__main__": gopts.outfile = outfile output = outfile.write + msg = email.message_from_file(infile) + + for header, value in gopts.set_header_value: + msg[header] = value + try: - decode_message(email.message_from_file(infile)) + decode_message(msg) finally: infile.close() outfile.close() diff --git a/test/expected/msg_01-1.txt b/test/expected/msg_01-1.txt new file mode 100644 index 0000000..86498c6 --- /dev/null +++ b/test/expected/msg_01-1.txt @@ -0,0 +1,22 @@ +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 +X-Test: set +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 a00ba26..5d4ca8a 100755 --- a/test/test_all +++ b/test/test_all @@ -53,6 +53,7 @@ test_file msg_16.txt msg_16-1.txt -r Received,List-Help,List-Post,List-Subscribe 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 if [ "$RC" -eq 0 ]; then echo "All tests passed!" -- 2.39.5