src/strip.vala
[app.mailtrimmer] / src / strip.vala
index 9412cda..b56ce44 100644 (file)
@@ -1,5 +1,13 @@
 /**
 
+ ** check left to do:  
+  - range scans on maildir
+  - see how replacing the links works in the resulting email via thunderbird etc..
+  - some checksum issues (see dupelicates?? suspect 0byte issues?)  -- seems ok now?
+
+
+
   needs to scan 2 things
   a) our mailfort email database
        point it at the top directory, containing YEAR/MONTH/DAY.... directories.
@@ -82,8 +90,8 @@ public class StripApplication : GLib.Application {
 
        public static int    opt_limit = -1;
 
-       public static int    open_age_newest = 1;
-       public static int    open_age_oldest = 6;
+       public static int    opt_age_newest = 1;
+       public static int    opt_age_oldest = 6;
 
 
        public static bool      opt_is_extracting = false;
@@ -230,7 +238,7 @@ public class StripApplication : GLib.Application {
                return 0;
         }
 
-               strip.scan_dir(opt_path);
+               strip.scan_dir(opt_path, "");
         
 
         
@@ -362,8 +370,28 @@ public class Strip : GLib.Object {
        
        // initialize it with known data..
        // that should wipe out dupes.
-       var filesize = this.query("SELECT filesize FROM Attachment WHERE id = %d".printf(
-                       int.parse(sid)));       
+       var matches = this.execute("SELECT count(id) as nid FROM Attachment WHERE id = %d".printf(
+                       int.parse(sid)));  
+
+                
+               if (matches == "") {     
+                       // 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);
+                       return;
+               }
+       
+       
+       // initialize it with known data..
+       // that should wipe out dupes.
+       var filesize = this.execute("SELECT filesize FROM Attachment WHERE id = %d".printf(
+                       int.parse(sid)));  
+
+               if (filesize=="") {      
+                  GLib.error("Ignoring record id (missing in database) :%s", sid);
+                  return;
+               }
                if (int.parse(filesize) < 1) {
                GLib.debug("Could not get filesize from id :%s = %s", sid,filesize);
                Posix.exit(0);
@@ -410,6 +438,43 @@ public class Strip : GLib.Object {
     }
     
     
+    public void fix_deleted_attachment_db(int id, GMime.Part attachment)
+    {
+               
+        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;
+        var chksum = this.md5_file(fn);
+               var mime_filename = attachment.get_header("X-strip-content-type");
+
+               var fileinfo = File.new_for_path(fn)
+                                       .query_info(GLib.FileAttribute.STANDARD_SIZE+","+GLib.FileAttribute.TIME_MODIFIED
+                                               ,GLib.FileQueryInfoFlags.NONE,null);
+       var file_size = (int) fileinfo.get_size();
+
+               
+
+      
+               this.query("""
+                        INSERT INTO Attachments
+                               (id, msgid, checksum, filesize)
+                               values
+                               ( 
+                            %d,   '%s', '%s',  '%s', %d
+                            ) 
+                            
+                     """.printf(
+                               id,
+                                     this.mysql_escape(this.active_message_exim_id),
+                                     this.mysql_escape(chksum),
+                                 this.mysql_escape(mime_filename),                       
+                                     file_size
+                        ));
+                        
+                GLib.error("added attachment?");
+    }
+    
+    
     public void replace_attachment(GMime.Multipart parent, GMime.Part attachment)
     {
         var sid = attachment.get_header("X-strip-id");
@@ -549,7 +614,10 @@ public class Strip : GLib.Object {
                        }
                        GLib.debug("Creating file %s", target_fn);
                        if (!FileUtils.test (target_fn, FileTest.EXISTS)) {
-                               Posix.link(fn, target_fn);
+                               var from = File.new_for_path (fn);
+                               var to =  File.new_for_path (target_fn);
+                               from.copy(to, 0, null);
+
                        }
                } else { 
                        GLib.debug("Skipping extraction %s", target_fn);
@@ -578,7 +646,7 @@ public class Strip : GLib.Object {
                
                
        
-       var rc=  this.mysql.query(str);         
+       var rc=  this.mysql.query(str); 
        if ( rc != 0 ) {
 
                    GLib.debug("ERROR %u: Query failed: %s\n", this.mysql.errno(), this.mysql.error());
@@ -588,6 +656,8 @@ public class Strip : GLib.Object {
 
         var rs = mysql.use_result();
         
+        //GLib.debug("got %d rows", (int) rs.num_rows());
+        
         var got_row = false;
                string[] row;
                string ret = "";
@@ -597,14 +667,20 @@ public class Strip : GLib.Object {
                
                }
                if (!need_return) {
-                       return "";
+               if (StripApplication.opt_debug_sql) {
+                               GLib.debug("got %s", got_row ? "=Nothing=" : ret);
+                       }
+                       return got_row ? "" : ret;
                }
                if (!got_row) {
+
                         GLib.debug("ERROR : no rows returned");
                        Posix.exit(1);
                        return "";
                }
-               GLib.debug("got %s", ret);
+       if (StripApplication.opt_debug_sql) {
+                       GLib.debug("got %s", ret);
+               }
                return ret;
                
                 
@@ -658,11 +734,19 @@ public class Strip : GLib.Object {
                        mailtime = new DateTime.local(int.parse(bits[0]),int.parse(bits[1]),int.parse(bits[2]),0,0,0);
                        
                        var oldest = new  DateTime.now_local();
-                       oldest = oldest.add_months(-1 * StripApplication.opt_min_age);
+                       oldest = oldest.add_months(-1 * StripApplication.opt_age_oldest);
                        var tspan = mailtime.difference(oldest) / GLib.TimeSpan.DAY;
 
                        if (tspan < 0) {
-                               GLib.debug("skip file is %d days older than %d months", (int)tspan, StripApplication.opt_min_age);
+                               GLib.debug("skip file is %d days older than %d months", (int)tspan, StripApplication.opt_age_oldest);
+                               return;
+                       }
+                       
+                       var newest = new  DateTime.now_local();
+                       newest = newest.add_months(-1 * StripApplication.opt_age_newest);
+                       tspan = mailtime.difference(newest) / GLib.TimeSpan.DAY;
+                       if (tspan > 0) {
+                               GLib.debug("skip file is %d days newer than %d months", (int)tspan, StripApplication.opt_age_newest);
                                return;
                        }
                        
@@ -713,26 +797,29 @@ public class Strip : GLib.Object {
                var recvd = message.get_header("received");
                this.active_message_exim_id = "";
                if (recvd != null && recvd.length > 1) {
-                       // GLib.debug("RECV: %s", recvd);
+                       GLib.debug("RECV: %s", recvd);
                        var lines = recvd.split("\t");
                        for (var i = 0; i < lines.length;i++) {
                                var bits = lines[i].strip().split(" ");
                                if (bits[0] == "id") {
                                        this.active_message_exim_id = bits[1].replace(";","");
 
-                                       
                                }
+                               
                                if (lines[i].contains(";")) {
                                        var dbits = lines[i].strip().split(";");                                
+                                       GLib.debug("Reading time from : %s", dbits[1]);
                                        var timez = GMime.utils_header_decode_date(dbits[1], null);
                                        if (timez != 0) {
                                                mailtime = new DateTime.from_unix_utc(timez);
                                                this.created_date = mailtime.format("%Y-%m-%d %H:%M:%S");
+                                               GLib.debug("Time is %s",this.created_date);
                                                // if it's not mailfort we can use that date to determine where to store it...
                                                if (!StripApplication.opt_scan_mailfort) {
                                                        this.created_dir = mailtime.format("%Y/%m/%d");
                                                }
+                                       } else {
+                                               GLib.debug("Could not read time from headers?");
                                        }
                                }
 
@@ -740,14 +827,21 @@ public class Strip : GLib.Object {
                }
                
                var oldest = new  DateTime.now_local();
-               oldest = oldest.add_months(-1 * StripApplication.opt_min_age);
+               oldest = oldest.add_months(-1 * StripApplication.opt_age_oldest);
                var rtspan = mailtime.difference(oldest) / GLib.TimeSpan.DAY;
-
+               GLib.debug("Checking oldest %d days difference", (int)rtspan   );
                if (rtspan < 0) {
-                       GLib.debug("skip(2) file is %d days older than %d months", (int)rtspan, StripApplication.opt_min_age);
+                       GLib.debug("skip(2) file is %d days older than %d months", (int)rtspan, StripApplication.opt_age_oldest);
+                       return;
+               }
+               var newest = new  DateTime.now_local();
+               newest = newest.add_months(-1 * StripApplication.opt_age_newest);
+               rtspan = mailtime.difference(newest) / GLib.TimeSpan.DAY;
+               if (rtspan > 0) {
+                       GLib.debug("skip(2) file is %d days newer than %d months : %s", (int)rtspan, StripApplication.opt_age_newest,
+                               mailtime.format("%Y-%m-%d %H:%M:%S"));
                        return;
                }
-
                
                
                
@@ -816,6 +910,7 @@ public class Strip : GLib.Object {
         GLib.debug("finished writing output %d", file_size);
 
         //
+        outstream = null;
         
          
        this.used_space_after += file_size;
@@ -824,12 +919,17 @@ public class Strip : GLib.Object {
        if (StripApplication.opt_is_replacing) {
                Posix.unlink(path +"/" + name);         
                GLib.debug("copy tmp file %s to %s" , tmpfile, path +"/" + name);               
-               Posix.link(tmpfile, path +"/" + name);
-               Posix.unlink(tmpfile);
-               var nf = File.new_for_path(path +"/" + name);
+               
+               // link will not work, as we are doing it accross file systems
+                       var from = File.new_for_path (tmpfile);
+                       var nf =  File.new_for_path (path +"/" + name);
+                       from.copy(nf, 0, null);
+                       
+
                var newfileinfo = nf.query_info(GLib.FileAttribute.TIME_MODIFIED,GLib.FileQueryInfoFlags.NONE,null);
                newfileinfo.set_modification_time(mod_time);
                nf.set_attributes_from_info(newfileinfo,FileQueryInfoFlags.NONE);
+               Posix.unlink(tmpfile);
                }
        this.processed++;
        
@@ -844,9 +944,58 @@ public class Strip : GLib.Object {
     }
     
     
-    public void scan_dir(string path)
+    public void scan_dir(string basepath, string subpath)
     {
-        var f = File.new_for_path(path);
+        
+        
+        // determine if path is to old to scan..
+        if (subpath.length > 0 && StripApplication.opt_scan_mailfort) {
+                       var year =  int.parse(subpath.substring(1,4));  // "/2000"
+                       var month = subpath.length > 5 ? int.parse(subpath.substring(6,2)) : 999; // "/2000/12"                 
+                       var day = subpath.length > 8 ? int.parse(subpath.substring(9,2)) : 999; // "/2000/12/01"                        
+               
+               var oldest = new  DateTime.now_local();
+                       oldest = oldest.add_months(-1 * StripApplication.opt_age_oldest);
+                       
+                       //GLib.debug("Checking directory %s is older than min: %d/%d/%d", subpath, oldest.get_year() , oldest.get_month(), oldest.get_day_of_month() );                                 
+                       
+                       if (year < oldest.get_year()) {
+                               GLib.debug("Skip directory %s is older than min year: %d", subpath, oldest.get_year());
+                               return;
+                       }
+                       if (year == oldest.get_year() &&  month < oldest.get_month()) {
+                               GLib.debug("Skip directory %s is older than min month: %d/%d", subpath, oldest.get_year() , oldest.get_month() );
+                               return;
+                       }
+               if (year == oldest.get_year() &&  month == oldest.get_month() && day < oldest.get_day_of_month()) {
+                               GLib.debug("Skip directory %s is older than min day: %d/%d/%d", subpath, oldest.get_year() , oldest.get_month(), oldest.get_day_of_month() );           
+                               return;
+                       }
+               
+               var newest = new  DateTime.now_local();
+                       newest = newest.add_months(-1 * StripApplication.opt_age_newest);
+                       
+                       //GLib.debug("Checking directory %s is newer than max: %d/%d/%d", subpath, newest.get_year() , newest.get_month(), newest.get_day_of_month() );                                 
+                       
+                       if (year > newest.get_year()) {
+                               GLib.debug("Skip directory %s is newer than max year: %d", subpath, newest.get_year());
+                               return;
+                       }
+                       if (year == newest.get_year() &&  month != 999 && month > newest.get_month()) {
+                               GLib.debug("Skip directory %s is newer than max month: %d/%d", subpath, newest.get_year() , newest.get_month() );
+                               return;
+                       }
+               if (year == newest.get_year() &&  month == newest.get_month() &&  day != 999 && day > newest.get_day_of_month()) {
+                               GLib.debug("Skip directory %s is newer than max day: %d/%d/%d", subpath, newest.get_year() , newest.get_month(), newest.get_day_of_month() );           
+                               return;
+                       }
+               
+               
+               
+        }
+        
+        
+        var f = File.new_for_path(basepath + subpath);
                FileEnumerator file_enum;
         var cancellable = new Cancellable ();
         try {      
@@ -880,17 +1029,21 @@ public class Strip : GLib.Object {
                 continue;
             }
             
-
+            var ds = next_file.get_display_name();
             if (next_file.get_file_type() != FileType.DIRECTORY) {
                 
-                if (next_file.get_display_name()[0] == ',') {
+                
+                
+                if (ds[0] == ',') {
                        continue;
                }
                // other files to ignore?
-                
-                this.scan_file(path , next_file.get_display_name());
+                if (Regex.match_simple (".tgz$", ds)) {
+                       continue;
+               }
+                this.scan_file(basepath + subpath , ds);
                                if(this.has_replaced) {
-                                this.report_state("After scanning %s/%s".printf(path , next_file.get_display_name()));
+                                this.report_state("After scanning %s/%s".printf(basepath + subpath , ds));
                        }
                 continue;
             }
@@ -901,7 +1054,7 @@ public class Strip : GLib.Object {
 
 
         
-            var ds = next_file.get_display_name();
+
             // not really needed?? - we are storing attachments in a seperate location now...
             if (ds[0] == '.') {
                 next_file = null;
@@ -912,14 +1065,14 @@ public class Strip : GLib.Object {
                }
             
             
-            var sp = path+"/"+next_file.get_display_name();
+            var sp = subpath+"/"+next_file.get_display_name();
             // skip modules.
             //print("got a file : " + sp);
          
             next_file = null;
             
             
-            this.scan_dir(sp);
+            this.scan_dir(basepath,sp);
             
         }