src/strip.vala
[app.mailtrimmer] / src / strip.vala
index c9c997b..c2fff35 100644 (file)
@@ -87,7 +87,9 @@ public class StripApplication : GLib.Application {
        public static string? opt_db_name = null;       
        public static string? opt_db_user = null;               
        public static string? opt_db_pass = null;               
-
+       public static string? opt_scan_mailfort_only = "";              
+       
+       
        public static int    opt_limit = -1;
 
        public static int    opt_age_newest = 1;
@@ -131,7 +133,8 @@ public class StripApplication : GLib.Application {
                { "oldest", 0, 0, OptionArg.INT, ref opt_age_oldest, "do not replace messages older than X (default is 6 months)", null },
 
                { "scan-maildir", 0, 0, OptionArg.NONE, ref opt_scan_maildir, "scan an maildir tree", null },
-               { "scan-mailfort", 0, 0, OptionArg.NONE, ref opt_scan_mailfort, "scan a mailfort tree", null },  
+               { "scan-mailfort", 0, 0, OptionArg.NONE, ref opt_scan_mailfort, "scan a mailfort tree", null }, 
+               { "scan-mailfort-only", 0, 0, OptionArg.STRING, ref opt_scan_mailfort_only, "scan a mailfort Year/month eg. 2010/08", null }, 
                { null }       
        };         
     public StripApplication( string[] args ) 
@@ -370,11 +373,11 @@ public class Strip : GLib.Object {
        
        // initialize it with known data..
        // that should wipe out dupes.
-       var matches = this.execute("SELECT count(id) as nid FROM Attachment WHERE id = %d".printf(
+       var matches = this.query("SELECT count(id)   FROM Attachment WHERE id = %d".printf(
                        int.parse(sid)));  
 
                 
-               if (matches == "") {     
+               if (matches == "0") {    
                        // our old mailfort code deleted the crap out of old records...
                        // if this occurs we will need to create the record again..
                        this.fix_deleted_attachment_db(int.parse(sid),attachment);
@@ -385,7 +388,7 @@ public class Strip : GLib.Object {
        
        // initialize it with known data..
        // that should wipe out dupes.
-       var filesize = this.execute("SELECT filesize FROM Attachment WHERE id = %d".printf(
+       var filesize = this.query("SELECT filesize FROM Attachment WHERE id = %d".printf(
                        int.parse(sid)));  
 
                if (filesize=="") {      
@@ -444,6 +447,13 @@ public class Strip : GLib.Object {
         var filename = attachment.get_header("X-strip-content-name");
         var file_path  = attachment.get_header("X-strip-path");
         var fn =  StripApplication.opt_target_path + "/" + file_path;
+        
+
+               if (!FileUtils.test (fn, FileTest.EXISTS)) {
+                       GLib.debug("SKIP -- file does not exist");
+                       return;
+       }
+        
         var chksum = this.md5_file(fn);
                var mime_type = attachment.get_header("X-strip-content-type");
 
@@ -452,10 +462,8 @@ public class Strip : GLib.Object {
                                                ,GLib.FileQueryInfoFlags.NONE,null);
        var file_size = (int) fileinfo.get_size();
 
-               
-
       
-               this.query("""
+               this.real_query(-1, """
                       
                       
                                INSERT INTO Attachment  (  
@@ -476,7 +484,8 @@ public class Strip : GLib.Object {
                                    mime_size ,
                                    filesize,
                                    
-                                   checksum
+                                   checksum,
+                                   created
 
                                ) VALUES (
                                        %d,  -- id
@@ -494,9 +503,10 @@ public class Strip : GLib.Object {
                                    0,
                                    %d, -- size
                                    %d, -- size
-                                   '%s' -- checkum
-
-                               )"
+                                   
+                                   '%s', -- checkum
+                                       '%s' -- created:
+                               )
                       
                       
                      """.printf(
@@ -505,27 +515,27 @@ public class Strip : GLib.Object {
                                      this.mysql_escape(filename),
                                  this.mysql_escape(mime_type),
                                  this.mysql_escape(file_path),
-                                       file_size
-                                     file_size
-                                     this.mysql_escape(chksum)
+                                       file_size,
+                                     file_size,
+                                     this.mysql_escape(chksum),
+                                       this.created_date
                         ));
              // this is done to fix the queue_id or maillog_id ??
                 this.query("""
                  SELECT attachment_update(
                       %d, -- in_id INT(11),
-
+                                               '', -- mime type
                       '%s', -- in_created DATETIME,
                       '%s' -- in_mailfort_sig varchar(64)
                  )
              """.printf(
-                               id,
-                       "", // this will be ignored..
+                               id, 
                                this.created_date,
                                this.mysql_escape(this.active_message_x_mailfort_sig)
              
              )
                );
-                GLib.error("added attachment?");
+               // GLib.error("added attachment?");
     }
     
     
@@ -541,7 +551,7 @@ public class Strip : GLib.Object {
         
         var filename = attachment.get_filename().replace("/", "-").replace("\n", "").replace("\t", " ");
         var fn = GLib.Environment.get_tmp_dir() +
-                       "/"+ this.active_name + "."+   filename;
+                       "/"+ this.active_name + "."+   GLib.Uri.escape_string(filename,"", false);
 
            var outfile = new GMime.StreamFile.for_path(fn, "w");
            outfile.set_owner(true);
@@ -577,7 +587,7 @@ public class Strip : GLib.Object {
           """.printf(
                        this.mysql_escape(this.active_message_exim_id),
                        chksum,
-                       this.mysql_escape( attachment.get_filename() ), // what is thsi is invalid?
+                       this.mysql_escape( GLib.Uri.escape_string(attachment.get_filename(),"", false) ), // what is thsi is invalid?
                         file_size)
                );
                 
@@ -621,10 +631,10 @@ public class Strip : GLib.Object {
                var target_fn = "";
 
            if (StripApplication.opt_is_extracting) {
-                       target_fn = StripApplication.opt_target_path + "/" + this.created_dir +"/"+ file_id  + "-" + filename;
+                       target_fn = StripApplication.opt_target_path + "/" + this.created_dir +"/"+ file_id  + "-" + GLib.Uri.escape_string(filename,"", false);
                } 
                    
-           var stored =  "/" + this.created_dir +"/"+ file_id  + "-" + filename;
+           var stored =  "/" + this.created_dir +"/"+ file_id  + "-" + GLib.Uri.escape_string(filename,"", false);
                 this.query("""
                
                        SELECT attachment_update_store(
@@ -643,15 +653,15 @@ public class Strip : GLib.Object {
         rep.set_filename(filename);
         string txt = "<html><body>"+
             "<a href=\"" + StripApplication.opt_replace_link + "/" +
-                       file_id + "/" + this.created_dir + "/"+chksum+"/"+ GLib.Uri.escape_string( filename) +"\">" + 
-            GLib.Uri.escape_string( filename) + // fixme needs html escaping...
+                       file_id + "/" + this.created_dir + "/"+chksum+"/"+ GLib.Uri.escape_string( filename, "", false) +"\">" + 
+            GLib.Uri.escape_string( filename, "", false) + // fixme needs html escaping...
             "</a>" +
             "</body></html>";
 
         rep.get_content_type().set_parameter("charset", "utf-8");
                rep.set_header("X-strip-id", file_id);
                rep.set_header("X-strip-content-name",  filename);                              
-               rep.set_header("X-strip-path", this.created_dir + "/" + file_id + "-" + filename);              
+               rep.set_header("X-strip-path", this.created_dir + "/" + file_id + "-" +  GLib.Uri.escape_string(filename,"", false));           
                rep.set_header("X-strip-content-type", mime_type);              
         var stream =  new GMime.StreamMem.with_buffer(txt.data);
         var con = new GMime.DataWrapper.with_stream(stream,GMime.ContentEncoding.DEFAULT);
@@ -683,13 +693,19 @@ public class Strip : GLib.Object {
     }
     public string query(string str)
     {
-           return this.real_query(true, str);
+           return this.real_query(1, str);
     }
     public string execute(string str)
     {
-           return this.real_query(false, str);
+           return this.real_query(0, str);
     }
-    public string real_query(bool need_return, string str)
+    /**
+    * need_return 
+    0 = no
+    1 = yes
+    -1 = don't try.
+    */
+    public string real_query(int need_return, string str)
     {
                GLib.debug("Before Query : %u  : %s\n", this.mysql.errno(), this.mysql.error());
 
@@ -706,9 +722,11 @@ public class Strip : GLib.Object {
                    GLib.debug("ERROR %u: Query failed: %s\n", this.mysql.errno(), this.mysql.error());
                                Posix.exit(1);
                }
-               
-
-        var rs = mysql.use_result();
+       var rs = mysql.use_result();
+               if (need_return == -1) {
+                       return "";
+               }
         
         //GLib.debug("got %d rows", (int) rs.num_rows());
         
@@ -720,7 +738,7 @@ public class Strip : GLib.Object {
                        ret = row[0];
                
                }
-               if (!need_return) {
+               if (need_return == 0) {
                if (StripApplication.opt_debug_sql) {
                                GLib.debug("got %s", got_row ? "=Nothing=" : ret);
                        }