src/strip.vala
[app.mailtrimmer] / src / strip.vala
index ee3132e..743f981 100644 (file)
@@ -3,7 +3,7 @@
  ** 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?)  
+  - some checksum issues (see dupelicates?? suspect 0byte issues?)  -- seems ok now?
  
 
 
@@ -370,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);
@@ -418,6 +438,100 @@ 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_type = 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 Attachment  (  
+                                       id, 
+                                       
+                                   msgid ,
+                                   queue_id ,
+                                   mime_filename ,
+                                   mime_type,
+                                    
+                                   stored_filename ,
+                                   mime_charset ,
+                                   mime_cdisp ,
+                                   mime_is_cover ,
+                                   
+                                   mime_is_multi ,
+                                   mime_is_mail,
+                                   mime_size ,
+                                   filesize,
+                                   
+                                   checksum
+
+                               ) VALUES (
+                                       %d,  -- id
+                                       
+                                   '%s' , -- msgid
+                                   0,
+                                   '%s'  , -- filename
+                                   '%s',  -- mimetype
+                                   
+                                   '%s', -- stored file anme
+                                   '',
+                                   'attachment',
+                                   0,
+                                   0,
+                                   0,
+                                   %d, -- size
+                                   %d, -- size
+                                   '%s' -- checkum
+
+                               )"
+                      
+                      
+                     """.printf(
+                               id,
+                                     this.mysql_escape(this.active_message_exim_id),
+                                     this.mysql_escape(filename),                                                            
+                                 this.mysql_escape(mime_type),                                                       
+                                 this.mysql_escape(file_path),                                                       
+                             file_size
+                     file_size
+                                     this.mysql_escape(chksum),
+
+
+                                     file_size
+                        ));
+                        
+                this.query("""
+                 SELECT attachment_update(
+                      %d, -- in_id INT(11),
+                      '%s', -- in_mime_type varchar(255),
+                      '%s', -- in_created DATETIME,
+                      '%s' -- in_mailfort_sig varchar(64)
+                 )
+             """.printf(
+                               id,
+                       "", // this will be ignored..
+                               this.created_date,
+                               this.mysql_escape(this.active_message_x_mailfort_sig)
+             
+             )
+               );
+                GLib.error("added attachment?");
+    }
+    
+    
     public void replace_attachment(GMime.Multipart parent, GMime.Part attachment)
     {
         var sid = attachment.get_header("X-strip-id");
@@ -557,7 +671,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);
@@ -586,7 +703,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());
@@ -596,6 +713,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 = "";
@@ -605,14 +724,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;
                
                 
@@ -842,6 +967,7 @@ public class Strip : GLib.Object {
         GLib.debug("finished writing output %d", file_size);
 
         //
+        outstream = null;
         
          
        this.used_space_after += file_size;
@@ -850,12 +976,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++;
        
@@ -955,17 +1086,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(basepath + subpath , 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(basepath + subpath , next_file.get_display_name()));
+                                this.report_state("After scanning %s/%s".printf(basepath + subpath , ds));
                        }
                 continue;
             }
@@ -976,7 +1111,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;