From 779e307770431e065050edd6634c49fa2e319df0 Mon Sep 17 00:00:00 2001 From: Oleg Broytman Date: Sat, 8 Mar 2014 22:21:40 +0400 Subject: [PATCH] Change option -R to accept lists of headers and parameters Change option -R to accept lists of headers and parameters: -R h1,h2,h3:p1,p2,p3 -R h1,h2,h3:*,-p1,-p2,-p3 -R *,-h1,-h2,-h3:p1,p2,p3 -R *,-h1,-h2,-h3:*,-p1,-p2,-p3 --- ANNOUNCE | 5 +++++ TODO | 8 ------- mimedecode.docbook | 30 ++++++++++++++++++++++---- mimedecode.py | 53 +++++++++++++++++++++++++++++++++------------- test/test_all | 6 +++--- 5 files changed, 72 insertions(+), 30 deletions(-) diff --git a/ANNOUNCE b/ANNOUNCE index 2faa302..b400489 100644 --- a/ANNOUNCE +++ b/ANNOUNCE @@ -44,6 +44,11 @@ WHAT'S NEW in version 2.4.0 (2014-03-??) Change option -r to remove all headers and accept a list of exceptions: -r *,-h1,-h2,... + Change option -R to accept lists of headers and parameters: +-R h1,h2,h3:p1,p2,p3 +-R h1,h2,h3:*,-p1,-p2,-p3 +-R *,-h1,-h2,-h3:p1,p2,p3 +-R *,-h1,-h2,-h3:*,-p1,-p2,-p3 Add ChangeLog. diff --git a/TODO b/TODO index a54cf78..9288b3a 100644 --- a/TODO +++ b/TODO @@ -1,11 +1,3 @@ -Change option -R to accept lists of headers and parameters: --R h1,h2,h3:p1,p2,p3 -Allow * and exceptions: --R h1,h2,h3:*,-p1,-p2,-p3 --R *,-h1,-h2,-h3:p1,p2,p3 --R *,-h1,-h2,-h3:*,-p1,-p2,-p3 - - Publish docs in html format. diff --git a/mimedecode.docbook b/mimedecode.docbook index 7055662..6a926ad 100644 --- a/mimedecode.docbook +++ b/mimedecode.docbook @@ -68,7 +68,10 @@ - + + + + @@ -316,11 +319,30 @@ - -R header:param + -R header1[,header2,header3,...]:param1[,param2,param3,...] - Add the pair (header, param) to a list of headers parameters to - remove; initially the list is empty. + Add the parameters(s) to a list of headers parameters to remove; + the parameters will be decoded only for the given header(s). + Initially the list is empty. + + + + + + -R *[,-header1,-header2,-header3,...]:param1[,param2,param3,...] + + + + -R header1[,header2,header3,...]:*[,-param1,-param2,-param3,...] + + + + -R *[,-header1,-header2,-header3,...]:*[,-param1,-param2,-param3,...] + + + Remove listed parameters (or all parameters except listed) frome + these headers (or from all headers except listed). diff --git a/mimedecode.py b/mimedecode.py index 42318f4..637e275 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 header:param] [-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,...]] [-beit mask] [-o output_file] [input_file [output_file]] """ % me) if errormsg: sys.stderr.write(errormsg + '\n') @@ -108,6 +108,27 @@ def _decode_headers_params(msg, header, decode_all_params, param_list): for param in param_list: decode_header_param(msg, header, param) +def _remove_headers_params(msg, header, remove_all_params, param_list): + if remove_all_params: + params = msg.get_params(header=header) + if params: + if param_list: + for param, value in params: + if param not in param_list: + msg.del_param(param, header) + else: + value = msg[header] + if value is None: # No such header + return + if ';' not in value: # There are no parameters + return + del msg[header] # Delete all such headers + # Get the value without parameters and set it back + msg[header] = value.split(';')[0].strip() + else: + for param in param_list: + msg.del_param(param, header) + def decode_headers(msg): "Decode message headers according to global options" @@ -122,18 +143,20 @@ def decode_headers(msg): for header in header_list: del msg[header] - #for header in gopts.remove_all_params: - # value = msg[header] - # if value is None: # No such header - # continue - # if ';' not in value: # There are no parameters - # continue - # del msg[header] # Delete all such headers - # # Get the value without parameters and set it back - # msg[header] = value.split(';')[0].strip() - - for header, param in gopts.remove_header_params: - msg.del_param(param, header) + for header_list, param_list in gopts.remove_headers_params: + header_list = header_list.split(',') + param_list = param_list.split(',') + remove_all_params = param_list[0] == '*' # Remove all params except listed + if remove_all_params: + param_list = _get_exceptions(param_list) + if header_list[0] == '*': # Remove for all headers except listed + header_list = _get_exceptions(header_list) + for header in msg.keys(): + if header.lower() not in header_list: + _remove_headers_params(msg, header, remove_all_params, param_list) + else: # Decode for listed headers + for header in header_list: + _remove_headers_params(msg, header, remove_all_params, param_list) for header_list in gopts.decode_headers: header_list = header_list.split(',') @@ -342,7 +365,7 @@ class GlobalOptions: # A list of headers to remove remove_headers = [] # A list of headers parameters to remove - remove_header_params = [] + remove_headers_params = [] totext_mask = [] # A list of content-types to decode binary_mask = [] # A list to pass through @@ -391,7 +414,7 @@ def get_opt(): elif option == '-r': gopts.remove_headers.append(value) elif option == '-R': - gopts.remove_header_params.append(value.split(':', 1)) + gopts.remove_headers_params.append(value.split(':', 1)) elif option == '-t': gopts.totext_mask.append(value) elif option == '-b': diff --git a/test/test_all b/test/test_all index 2ea64c1..a00ba26 100755 --- a/test/test_all +++ b/test/test_all @@ -44,15 +44,15 @@ test_file msg_03.txt msg_03-1.txt -d '*,-cc' test_file msg_03.txt msg_03-1.txt -d \*,From,To,Subject,-cc 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 test_file msg_22.txt msg_22.txt -P -p Content-Type,Content-Disposition:name,filename test_file msg_22.txt msg_22.txt -P -p \*:name,filename test_file msg_22.txt msg_22.txt -P -p Content-Type,Content-Disposition:\* test_file msg_22.txt msg_22.txt -P -p '*,-Content-Id:*,-x-mac-type' +test_file msg_22.txt msg_22-1.txt -r content-id test_file msg_16.txt msg_16-1.txt -r Received,List-Help,List-Post,List-Subscribe,List-Id,List-Unsubscribe,List-Archive test_file msg_16.txt msg_16-2.txt -r \*,-mime-version,-content-type -#test_file msg_18.txt msg_18-1.txt --remove-params=X-Foobar-Spoink-Defrobnit +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:\* if [ "$RC" -eq 0 ]; then echo "All tests passed!" -- 2.39.2