]> git.phdru.name Git - mimedecode.git/commitdiff
Change option -R to accept lists of headers and parameters
authorOleg Broytman <phd@phdru.name>
Sat, 8 Mar 2014 18:21:40 +0000 (22:21 +0400)
committerOleg Broytman <phd@phdru.name>
Sat, 8 Mar 2014 18:21:40 +0000 (22:21 +0400)
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
TODO
mimedecode.docbook
mimedecode.py
test/test_all

index 2faa3028b86f225da9937604d18ac27afd556d5c..b400489e12c7e8edcf7a5b8274b28a57a793f931 100644 (file)
--- 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 a54cf78396d6b931f30f81c038932d2e8095a50b..9288b3a66a8775051039e49104d5303b7069c17e 100644 (file)
--- 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.
 
 
index 7055662b4e60c25522141378d4c311ca9f856df1..6a926ad6c318eb9f87e619329763bac2314fbbd1 100644 (file)
          <option>-r *[,-header1,-header2,-header3...]</option>
       </arg>
       <arg choice="opt">
-         <option>-R header:param</option>
+        <option>-R header1[,header2,header3,...]:param1[,param2,param3,...]</option>
+        <option>-R *[,-header1,-header2,-header3,...]:param1[,param2,param3,...]</option>
+        <option>-R header1[,header2,header3,...]:*[,-param1,-param2,-param3,...]</option>
+        <option>-R *[,-header1,-header2,-header3,...]:*[,-param1,-param2,-param3,...]</option>
       </arg>
       <arg choice="opt">
          <option>-beit mask</option>
    </varlistentry>
 
    <varlistentry>
-      <term>-R header:param</term>
+      <term>-R header1[,header2,header3,...]:param1[,param2,param3,...]</term>
       <listitem>
          <para>
-            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.
+         </para>
+      </listitem>
+   </varlistentry>
+
+   <varlistentry>
+      <term>-R *[,-header1,-header2,-header3,...]:param1[,param2,param3,...]</term>
+   </varlistentry>
+
+   <varlistentry>
+      <term>-R header1[,header2,header3,...]:*[,-param1,-param2,-param3,...]</term>
+   </varlistentry>
+
+   <varlistentry>
+      <term>-R *[,-header1,-header2,-header3,...]:*[,-param1,-param2,-param3,...]</term>
+      <listitem>
+         <para>
+           Remove listed parameters (or all parameters except listed) frome
+           these headers (or from all headers except listed).
          </para>
       </listitem>
    </varlistentry>
index 42318f47594639ed667db47c753310647a402f54..637e275e3b127c6598850237fafcec0ca325c856 100755 (executable)
@@ -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':
index 2ea64c1326979fb267d83c5ca8e393f7e3592e2d..a00ba268c90d301760db8047c3a40ec34b90a77a 100755 (executable)
@@ -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!"