src/strip.vala
[app.mailtrimmer] / src / strip.vala
1 /**
2
3   needs to scan 2 things
4   a) our mailfort email database
5        point it at the top directory, containing YEAR/MONTH/DAY.... directories.
6        scan each file (over a year old...)
7        extract out the attachment, and replace with HTML
8        DATABASE? - mysql or sqlite? - 
9            filesize / name / date / checksum / mimetype -- into mailfort should be OK.
10   b) the imap user emails
11            loop through user's directories
12            check age of email .. over 1 years..
13            ?? how to prevent 'repeat' scanning of emails?
14               ??? hidden '.' files containing last scan date?
15
16            check if file exists in our DB.. - replace the link...
17            otherwise generate a file. + add to DB...
18            
19    c) retreival system
20      -> URL -> get file
21    d) redirect system.
22      -> URL -> redirect to correct server
23
24
25 More notes on our Mailfort DB sync:
26 * some of these attachments are already in the database...
27  - so we need to update the DB..
28  - probably worth putting the code in a stored procedure..
29  
30  -- key scenarios
31    * first scan (and extract)
32    * rescan (as I messed up the first time - fix the DB...)
33    * email scan - attachments might not have related messages.
34  
35  
36  - {id} attachment_init(
37                 {exim_msg_id}
38                 {chksum}
39                 {filename),
40         )
41         // creates or returns id (can look for existing messages?
42         // can do a merge?? - copy 'old' record data into 'new'....  "prefer checksummed"
43         
44         attachment_update(
45                 {id}
46                 {exim_msg_id}
47                 {mailfort_msg_sig}
48                 {file_size}
49                 {created} // message date..
50                 {chksum}
51                 {filename),
52         {mime_type}
53         )
54         attachment_update_store(
55                         {id}
56                         {stored_filename}
57         )
58
59
60 */ 
61
62 // valac --pkg gmime --vapi
63 /*
64
65 // http://www.fromdual.com/mysql-vala-program-example << check mysql if this does not work.
66
67  valac  -g --vapidir=. --thread  strip.vala   --vapidir=../vapi \
68      --pkg glib-2.0 --pkg mysql --pkg gio-2.0 --pkg posix --pkg gmime-2.6 \
69       --Xcc=-lmysqlclient  -v \
70        -o /tmp/strip
71 */ 
72  
73 public class StripApplication : GLib.Application {
74
75         public static string? opt_path = null;
76         public static string? opt_file = null;  
77         public static string? opt_target_path = null;
78         public static string? opt_db_host = "127.0.0.1";
79         public static string? opt_db_name = null;       
80         public static string? opt_db_user = null;               
81         public static string? opt_db_pass = null;               
82
83         public static int    opt_limit = -1;
84
85         public static int    opt_min_age = 6;
86
87
88         public static bool      opt_is_extracting = false;
89         public static bool      opt_is_replacing = false;
90         public static bool      opt_scan_imap  = false; 
91         public static bool      opt_scan_mailfort  = false;     
92         public static bool              opt_dump = false;       
93         public static bool              opt_debug = false; 
94         
95         public static bool opt_debug_sql = false;       
96         public static string? opt_replace_link = null;
97         
98         
99         public const GLib.OptionEntry[] options = {
100                 
101                 { "debug", 0, 0, OptionArg.NONE, ref opt_debug, "show debug messages for components", null },
102                 { "debug-sql", 0, 0, OptionArg.NONE, ref opt_debug_sql, "debug the SQL statements", null },         
103
104                 { "path", 0, 0, OptionArg.STRING, ref opt_path, "Directory where email to be parsed is", null },        
105                 { "file", 0, 0, OptionArg.STRING, ref opt_file, "A specific file to be parsed", null }, 
106
107                 { "target-path", 0, 0, OptionArg.STRING, ref opt_target_path, "Directory where attachments are to be put", null },
108
109                 { "link", 0, 0, OptionArg.STRING, ref opt_replace_link, "url for the replement link: eg. http://www.mysite.com/xxxx/%s", null },         
110                         
111                 { "host", 0, 0, OptionArg.STRING, ref opt_db_host, "Mysql host (default localhost)", null },    
112                 { "name", 0, 0, OptionArg.STRING, ref opt_db_name, "Mysql database name REQUIRED", null },      
113                 { "user", 0, 0, OptionArg.STRING, ref opt_db_user, "Mysql database user REQUIRED", null },      
114                 { "pass", 0, 0, OptionArg.STRING, ref opt_db_pass, "Mysql database password (default empty)", null },            
115
116                 { "extract", 0, 0, OptionArg.NONE, ref opt_is_extracting, "Should attachments be extracted (default NO)", null },
117                 { "replace", 0, 0, OptionArg.NONE, ref opt_is_replacing, "Should attachments be replaced (default NO)", null },
118                 { "dump", 0, 0, OptionArg.NONE, ref opt_dump, "Print the replaced mail contents to stdout", null },         
119
120                 { "limit", 0, 0, OptionArg.INT, ref opt_limit, "stop after X number of messages with attachments have been processed", null },         
121                 { "min-age", 0, 0, OptionArg.INT, ref opt_min_age, "minimum age of messages (default is 6 months)", null },
122
123                 { "scan-imap", 0, 0, OptionArg.NONE, ref opt_scan_imap, "scan an imap tree", null },
124                 { "scan-mailfort", 0, 0, OptionArg.NONE, ref opt_scan_mailfort, "scan a mailfort tree", null },  
125                 { null }       
126         };         
127     public StripApplication( string[] args ) 
128     {
129                  Object(
130             application_id: "org.roojs.mailstripper",
131             flags: ApplicationFlags.FLAGS_NONE
132          );
133  
134                         
135          var opt_context =  new GLib.OptionContext ("Mail Stripper");
136                         
137          try {
138                                 
139             opt_context.set_help_enabled (true);
140             opt_context.add_main_entries (options, null);
141             opt_context.parse ( ref  args);
142             //opt_detach = !optx_no_detach;
143                             
144  
145                             
146              // options that have to be set.. bee or hive... (or stop all)
147             if ((!opt_scan_mailfort && !opt_scan_imap) || (opt_scan_mailfort && opt_scan_imap))  {
148                stdout.printf ("You must specify the type of directory tree to scan - either imap or mailfort\n%s",
149                    opt_context.get_help(true, null));
150                GLib.Process.exit(Posix.EXIT_FAILURE);
151             }
152                         
153                          if ((opt_db_name == null || opt_db_name.length < 1 || opt_db_user == null || opt_db_user.length < 1))  {
154                stdout.printf ("You must specify the database name / user \n%s",
155                    opt_context.get_help(true, null));
156                GLib.Process.exit(Posix.EXIT_FAILURE);
157             }
158                          if ((opt_path == null || opt_path.length < 1)   )  {
159                stdout.printf ("You must specify the scan start path\n%s",
160                    opt_context.get_help(true, null));
161                GLib.Process.exit(Posix.EXIT_FAILURE);
162             }
163                         if (opt_replace_link == null || (opt_replace_link.length < 1))  {
164                stdout.printf ("You must specify the link to use in the replacement \n%s",
165                    opt_context.get_help(true, null));
166                GLib.Process.exit(Posix.EXIT_FAILURE);
167             }
168             if ((opt_is_replacing || opt_is_extracting ) && (opt_target_path == null || opt_target_path.length < 1)) {
169                       stdout.printf ("You must specify a target path to put attachments\n%s",
170                    opt_context.get_help(true, null));
171                GLib.Process.exit(Posix.EXIT_FAILURE);
172             }
173             
174             
175          } catch (GLib.OptionError e) {
176             stdout.printf ("error: %s\n", e.message);
177             stdout.printf ("Run '%s --help' to see a full list of available command line options.\n%s", 
178                       args[0], opt_context.get_help(true, null));
179             GLib.Process.exit(Posix.EXIT_FAILURE);
180          }
181         }
182          
183     public static int main(string[] args) 
184     {
185                 
186                 var application = new StripApplication(  args);
187                 
188                 GLib.Log.set_always_fatal(LogLevelFlags.LEVEL_ERROR | LogLevelFlags.LEVEL_CRITICAL); 
189            
190            if (opt_debug || opt_debug_sql) {
191                         GLib.Log.set_handler(null, 
192                         GLib.LogLevelFlags.LEVEL_DEBUG | GLib.LogLevelFlags.LEVEL_WARNING | GLib.LogLevelFlags.LEVEL_INFO, 
193                         (dom, lvl, msg) => {
194                                         print("%s\n", msg);
195                                 }
196                         );
197                 }
198         
199         GMime.init(0);
200                 if (StripApplication.opt_is_replacing) {
201                         StripApplication.opt_is_extracting = true;
202                 }
203   
204                 GLib.debug("scanning folder: %s", opt_path );
205                 
206                 var strip = new Strip( opt_path );
207  
208                 
209                 strip.mysql  = new Mysql.Database();
210                 if (!strip.mysql.real_connect(
211                                 opt_db_host,
212                                 opt_db_user ,
213                                 opt_db_pass == null ? "" : opt_db_pass, //passwd
214                                 opt_db_name, //DB
215                                 3306, // not changable...?
216                                 null
217                         )
218                 ) {
219                         stdout.printf("ERROR %u: Connection failed: %s\n", 
220                                 strip.mysql.errno(), strip.mysql.error()
221                         );
222
223                         return 1;
224                 }
225         if (opt_file != null) {
226                 strip.base_dir = opt_path;
227                 strip.scan_file( GLib.Path.get_dirname(opt_file),  GLib.Path.get_basename(opt_file));
228                 return 0;
229         }
230
231                 strip.scan_dir(opt_path);
232         
233
234         
235         return 0;
236     }
237 }
238
239 public class Strip : GLib.Object {
240         
241  
242         
243         public string base_dir = "";
244         
245         public Mysql.Database mysql;
246         
247         int processed = 0;
248     
249     uint64 used_space_before = 0;
250     uint64 used_space_after = 0;
251     
252     
253     public Strip(string base_dir)
254     {
255         this.base_dir = base_dir;
256     }
257     
258     public void handle_part(GMime.Object parent, GMime.Object mime_obj)
259     {
260                 if (mime_obj is GMime.Part) {
261                    var  p = (GMime.Part)mime_obj;
262                         var ct = p.get_content_type();
263                         var cd = p.get_content_disposition();
264                         
265                         var sid = p.get_header("X-strip-id");
266                     if (sid != null && sid.length > 0) {
267                         this.update_attachment_db(p);
268                             GLib.debug("Skip attachment replace - it's already been done");
269                         return;
270                         }
271                         
272                         if (cd == null || cd.get_disposition().down() != "attachment") {
273                                 return;
274                         }
275                         if (ct.get_media_type() == "text") {
276                                 return;
277                         }
278                         if (ct.to_string() == "application/pgp-encrypted") {
279                                 return;
280                         }
281                         if (ct.to_string() == "application/pgp-keys") {
282                                 return;
283                         }
284                         if (p.get_filename() == null) {
285                                 return;
286                         }
287                          // print("got part %s\n", ct.to_string());
288                          if (parent is GMime.Multipart) {
289                                 
290                                 this.replace_attachment(((GMime.Multipart)parent), p);
291                                 // remove it !?
292
293                           }
294
295
296                         return;
297                 }
298                 if (mime_obj is GMime.Multipart) {
299                         
300
301                         var  mp = (GMime.Multipart)mime_obj;
302                         //var ct = mp.get_content_type();
303
304                         //print("got multi-part %s\n", ct.to_string());
305                         for (var i = 0; i< mp.get_count(); i++) { 
306                           var mo = mp.get_part(i);
307                           this.handle_part(mime_obj,mo);
308                         }
309                    // ((GMime.Multipart)mime_obj).foreach((sub_obj) => {
310                    //     Strip.handle_part(sub_obj);
311                 //
312                    // });
313
314
315                         return;
316                 }
317
318                 if (mime_obj is GMime.MessagePart) {
319                         var msg = ((GMime.MessagePart)mime_obj).get_message();
320                         msg.foreach((subobj) => {
321                          this.handle_part(msg,subobj);
322                     });
323                 
324                         //print("got message-part\n");
325                         return;
326                 }
327                 
328                 if (mime_obj is GMime.Message) {
329                         var mp = ((GMime.Message) mime_obj).get_mime_part();
330
331                         if (!(mp is GMime.Multipart)) {
332                                 //GLib.debug("get mimepart does not return a Multipart?");
333                                 return;
334                         }
335                         
336                         var mpc = ((GMime.Multipart)mp).get_count();
337                         
338                         //GLib.debug("Message has %d parts", mpc); 
339                         for (var i =0 ; i < mpc; i++) {
340                                 //GLib.debug("Getting part %d", i); 
341                                 var submime_obj = ((GMime.Multipart)mp).get_part(i);
342                         this.handle_part(mp,submime_obj);                       
343                     }
344                         print("got message??\n");
345                         return;
346                 }
347                 
348                 print("got something else\n");
349
350
351     }
352     public void update_attachment_db(GMime.Part attachment)
353     {
354         // only called when we have an sid...
355         var sid = attachment.get_header("X-strip-id");
356         if (sid == null || sid.length < 1) {
357                 GLib.debug("Strange - update attachment db called ?");
358                 return;
359         }
360         
361         // initialize it with known data..
362         // that should wipe out dupes.
363         var filesize = this.query("SELECT filesize FROM Attachment WHERE id = %d".printf(
364                         int.parse(sid)));       
365                 if (int.parse(filesize) < 1) {
366                 GLib.debug("Could not get filesize from id :%s = %s", sid,filesize);
367                 Posix.exit(0);
368                 return;
369         }
370         
371         var chksum = this.query("SELECT  checksum FROM Attachment WHERE id = %d".printf(
372                         int.parse(sid)
373                 ));
374         var mime_filename = this.query("SELECT  mime_filename FROM Attachment WHERE id = %d".printf(
375                         int.parse(sid)));       
376                 
377         this.query("""
378              SELECT 
379                  attachment_init(
380                      '%s', '%s', '%s', %d
381                  ) as id 
382                  
383           """.printf(
384                           this.mysql_escape(this.active_message_exim_id),
385                           this.mysql_escape(chksum),
386                           this.mysql_escape(mime_filename),                       
387                           int.parse(filesize)
388                 ));
389         this.query("""
390                  SELECT attachment_update(
391                       %d, -- in_id INT(11),
392                       '%s', -- in_mime_type varchar(255),
393                       '%s', -- in_created DATETIME,
394                       '%s' -- in_mailfort_sig varchar(64)
395                  )
396               """.printf(
397                         int.parse(sid),
398                         "", // this will be ignored..
399                                 this.created_date,
400                                 this.mysql_escape(this.active_message_x_mailfort_sig)
401               
402               )
403                 );
404                 this.mysql.store_result();
405                 
406
407     
408     }
409     
410     
411     public void replace_attachment(GMime.Multipart parent, GMime.Part attachment)
412     {
413         var sid = attachment.get_header("X-strip-id");
414         if (sid != null && sid.length > 0) {
415                 GLib.debug("Skip attachment replace - it's already been done");
416                 return;
417         }
418         
419         var c = attachment.get_content_object();
420         
421         var filename = attachment.get_filename().replace("/", "-").replace("\n", "").replace("\t", " ");
422         var fn = GLib.Environment.get_tmp_dir() +
423                         "/"+ this.active_name + "."+   filename;
424
425             var outfile = new GMime.StreamFile.for_path(fn, "w");
426             outfile.set_owner(true);
427             var file_size = (int) c.write_to_stream(outfile);
428             var chksum = this.md5_file(fn);
429             outfile.flush();
430             outfile = null;
431         
432         if (file_size == 0) {
433
434                 GLib.debug("ERROR - file size of write to stream returned 0?");
435                 Posix.unlink(fn);               
436                 return;
437         }
438         
439         
440         
441  
442         var mime_type= attachment.get_content_type().to_string();
443         // at this point we have to do our database magic...
444         //filesize / name / date / checksum / mimetype -- into mailfort should be OK.
445         
446         var file_id = this.query("""
447                 SELECT 
448                 
449                 attachment_init(
450                                 '%s', -- in_msgid VARCHAR(32),
451                                 '%s', -- in_checksum VARCHAR(64),
452                                 '%s', -- in_mime_filename varchar(255)
453                                 %d -- filesize
454                         ) as id 
455                         
456           """.printf(
457                         this.mysql_escape(this.active_message_exim_id),
458                         chksum,
459                         this.mysql_escape( attachment.get_filename() ), // what is thsi is invalid?
460                          file_size)
461                 );
462                  
463                 
464                 if (file_id.length < 1) {
465                         GLib.debug("ERROR - CALL to attachment_init failed");
466                 Posix.unlink(fn);               
467                 return;
468                 
469                 }
470  
471                 if (int.parse(file_id) < 1) {
472                         GLib.debug("ERROR - CALL to attachment_init failed - returned 0?");
473                 Posix.unlink(fn);               
474                 return;
475                 
476                 }
477  
478         
479                 GLib.debug("fn = %s, m5=%s, id= %s", filename, mime_type, this.active_message_id);
480                 this.query("""
481                 
482                         SELECT attachment_update(
483                                 %d, -- in_id INT(11),
484                                 '%s', -- in_mime_type varchar(255),
485                                 '%s', -- in_created DATETIME,
486                                 '%s' -- in_mailfort_sig varchar(64)
487                                 
488                                 ) as result
489       """.printf(
490                 int.parse(file_id),
491                         this.mysql_escape(mime_type),
492                         this.created_date,
493                         this.mysql_escape(this.active_message_x_mailfort_sig)
494                 ));
495                  this.mysql.store_result();
496                                  
497  
498                 this.used_space_after += file_size;
499                         
500                 var target_fn = "";
501
502             if (StripApplication.opt_is_extracting) {
503                         target_fn = StripApplication.opt_target_path + "/" + this.created_dir +"/"+ file_id  + "-" + filename;
504                 } 
505                     
506             var stored =  "/" + this.created_dir +"/"+ file_id  + "-" + filename;
507                  this.query("""
508                 
509                         SELECT attachment_update_store(
510                                 %d, -- in_id INT(11),
511                                 '%s'  -- in_store_filename varchar(255),
512                          
513                                 
514                                 ) as result
515       """.printf(
516                 int.parse(file_id),
517                          this.mysql_escape( stored)
518                 ));   
519                          
520         var rep = new GMime.Part.with_type("text","html");
521         // we have to set up a redirect server - to redirect hpasite... to their internal service..
522         rep.set_filename(filename);
523         string txt = "<html><body>"+
524             "<a href=\"" + StripApplication.opt_replace_link + "/" +
525                         file_id + "/" + this.created_dir + "/"+chksum+"/"+ GLib.Uri.escape_string( filename) +"\">" + 
526             GLib.Uri.escape_string( filename) + // fixme needs html escaping...
527             "</a>" +
528             "</body></html>";
529
530         rep.get_content_type().set_parameter("charset", "utf-8");
531                 rep.set_header("X-strip-id", file_id);
532                 rep.set_header("X-strip-content-name",  filename);                              
533                 rep.set_header("X-strip-path", this.created_dir + "/" + file_id + "-" + filename);              
534                 rep.set_header("X-strip-content-type", mime_type);              
535         var stream =  new GMime.StreamMem.with_buffer(txt.data);
536         var con = new GMime.DataWrapper.with_stream(stream,GMime.ContentEncoding.DEFAULT);
537
538         rep.set_content_object(con);
539         GLib.debug("Replacing Attachment with HTML");
540         parent.replace(parent.index_of(attachment), rep);
541                 this.has_replaced = true;
542                  
543                 if (StripApplication.opt_is_extracting && target_fn.length > 0) {
544                         var dir = GLib.Path.get_dirname(target_fn);
545                         if (!FileUtils.test (dir, FileTest.IS_DIR)) {
546                                 GLib.DirUtils.create_with_parents(dir, 0755);
547                         }
548                         GLib.debug("Creating file %s", target_fn);
549                         if (!FileUtils.test (target_fn, FileTest.EXISTS)) {
550                                 Posix.link(fn, target_fn);
551                         }
552                 } else { 
553                         GLib.debug("Skipping extraction %s", target_fn);
554                 }
555                 Posix.unlink(fn);
556                 
557
558
559     }
560     public string query(string str)
561     {
562             return this.real_query(true, str);
563     }
564     public string execute(string str)
565     {
566             return this.real_query(false, str);
567     }
568     public string real_query(bool need_return, string str)
569     {
570                 GLib.debug("Before Query : %u  : %s\n", this.mysql.errno(), this.mysql.error());
571
572
573         if (StripApplication.opt_debug_sql) {
574                 GLib.debug("SQL: %s\n", str);
575                 }
576                 
577                 
578         
579         var rc=  this.mysql.query(str);         
580         if ( rc != 0 ) {
581
582                     GLib.debug("ERROR %u: Query failed: %s\n", this.mysql.errno(), this.mysql.error());
583                                 Posix.exit(1);
584                 }
585                 
586
587         var rs = mysql.use_result();
588         
589         var got_row = false;
590                 string[] row;
591                 string ret = "";
592                 while( (row = rs.fetch_row()) != null) { 
593                         got_row = true;
594                         ret = row[0];
595                 
596                 }
597                 if (!need_return) {
598                         return "";
599                 }
600                 if (!got_row) {
601                          GLib.debug("ERROR : no rows returned");
602                         Posix.exit(1);
603                         return "";
604                 }
605                 GLib.debug("got %s", ret);
606                 return ret;
607                 
608                  
609         }
610     
611     public string mysql_escape(string str)
612     {
613             unichar[] value_escaped = new unichar[str.length * 2 + 1];
614                 this.mysql.real_escape_string ((string) value_escaped, str, str.length);
615                 return (string) value_escaped;
616     }
617     
618     public string  md5_file(string fn) {
619               Checksum checksum = new Checksum (ChecksumType.MD5);
620
621               FileStream stream = FileStream.open (fn, "rb");
622               uint8 fbuf[100];
623               size_t size;
624
625               while ((size = stream.read (fbuf)) > 0) {
626                       checksum.update (fbuf, size);
627               }
628
629               unowned string digest = checksum.get_string ();
630               return digest;
631     }
632
633         string active_path = "";    
634     string active_name = "";
635     string active_message_id = "";
636     string active_message_x_mailfort_sig = "";
637     string active_message_exim_id = "";
638     bool has_replaced = false;
639     string created_date = ""; // should be YYYY-mm-dd
640     string created_dir = ""; // should be YYY/mm/dd
641     
642     public void scan_file(string path, string name)
643     {
644                 GLib.debug("Scan: %s/%s", path,name); 
645                 
646                 this.has_replaced = false; 
647         this.active_path = path;
648         this.active_name = name;
649         this.active_message_id = "";
650
651                 DateTime mailtime;
652                 if (StripApplication.opt_scan_mailfort) {
653                     this.created_dir = this.active_path.substring(this.base_dir.length + 1 );
654                         this.created_date = this.created_dir.replace("/", "-");
655                         var bits = this.created_date.split("-");
656                         mailtime = new DateTime.local(int.parse(bits[0]),int.parse(bits[1]),int.parse(bits[2]),0,0,0);
657                         
658                         var oldest = new  DateTime.now_local();
659                         oldest.add_months(-1 * StripApplication.opt_min_age);
660                         var tspan = mailtime.difference(oldest) / GLib.TimeSpan.DAY;
661
662                         if (tspan < 0) {
663                                 GLib.debug("skip file is %d days older than %d months", (int)tspan, StripApplication.opt_min_age);
664                                 return;
665                         }
666                         
667                 }
668         
669         
670                 var fileinfo = File.new_for_path(path +"/" + name)
671                                         .query_info(GLib.FileAttribute.STANDARD_SIZE+","+GLib.FileAttribute.TIME_MODIFIED
672                                                 ,GLib.FileQueryInfoFlags.NONE,null);
673         var file_size = (int) fileinfo.get_size();
674                 var mod_time = fileinfo.get_modification_time();
675                 
676                 
677                 
678                 if (!StripApplication.opt_scan_mailfort) {
679                    
680                 // it's a mail directory...
681                 // use the last modification time? as the default...
682                  mailtime = new DateTime.from_timeval_utc(mod_time);
683                  this.created_dir = mailtime.format("/%Y/%m/%d");
684                          this.created_date =  mailtime.format("%Y-%m-%d %H:%M:%S");
685  
686         }
687                 // check on age of file...
688                 
689                 
690                 
691                 
692                 
693         this.used_space_before += file_size;
694         
695         var stream = new GMime.StreamFs.for_path (path +"/" + name,Posix.O_RDONLY, 0);
696         //stream.set_owner(true);
697         var parser = new GMime.Parser.with_stream(stream);
698         var message = parser.construct_message();
699  
700                 if (message == null) {
701                         GLib.debug("Could not parse file? %s/%s", path,name);
702                 this.used_space_after += file_size;                     
703                 return;
704                 }       
705
706
707                 // check : - is message over a year old?                
708                 // get various msg info..
709                 this.active_message_id = message.get_message_id();
710                 this.active_message_x_mailfort_sig = message.get_header("x-mailfort-sig");
711                 var recvd = message.get_header("received");
712                 this.active_message_exim_id = "";
713                 if (recvd != null && recvd.length > 1) {
714                         // GLib.debug("RECV: %s", recvd);
715                         var lines = recvd.split("\t");
716                         for (var i = 0; i < lines.length;i++) {
717                                 var bits = lines[i].strip().split(" ");
718                                 if (bits[0] == "id") {
719                                         this.active_message_exim_id = bits[1].replace(";","");
720
721  
722                                         
723                                 }
724                                 if (lines[i].contains(";")) {
725                                         var dbits = lines[i].strip().split(";");                                
726                                         var timez = GMime.utils_header_decode_date(dbits[1], null);
727                                         if (timez != 0) {
728                                                 mailtime = new DateTime.from_unix_utc(timez);
729                                                 this.created_date = mailtime.format("%Y-%m-%d %H:%M:%S");
730                                                 // if it's not mailfort we can use that date to determine where to store it...
731                                                 if (!StripApplication.opt_scan_mailfort) {
732                                                         this.created_dir = mailtime.format("/%Y/%m/%d");
733                                                 }
734                                         }
735                                 }
736
737                         }
738                 }
739                 
740                 var oldest = new  DateTime.now_local();
741                 oldest.add_months(-1 * StripApplication.opt_min_age);
742                 var tspan = mailtime.difference(oldest) / GLib.TimeSpan.DAY;
743
744                 if (tspan < 0) {
745                         GLib.debug("skip file is %d days older than %d months", (int)tspan, StripApplication.opt_min_age);
746                         return;
747                 }
748
749                 
750                 
751                 
752                 /*
753                 GLib.debug("Message DATA:\n mid: %s\nmailfort: %s \nexim_id: %s",
754                         this.active_message_id,
755                         this.active_message_x_mailfort_sig,
756                         this.active_message_exim_id
757                 );
758                  */
759                         
760                 // DATE?
761                 
762                 var mp = message.get_mime_part();
763
764                 if (!(mp is GMime.Multipart)) {
765                         //GLib.debug("get mimepart does not return a Multipart?");
766                 this.used_space_after += file_size;                                             
767                         return;
768                 }
769                 
770                 var mpc = ((GMime.Multipart)mp).get_count();
771                 
772                 //GLib.debug("Message has %d parts", mpc); 
773                 for (var i =0 ; i < mpc; i++) {
774                         //GLib.debug("Getting part %d", i); 
775                         var mime_obj = ((GMime.Multipart)mp).get_part(i);
776             this.handle_part(mp,mime_obj);                      
777         }
778                 
779         parser= null;
780
781       //  stream.set_owner(false);
782             //stream.close();
783         stream = null;//.close();
784         
785         
786                 if (!this.has_replaced) {
787                         this.used_space_after += file_size;
788                         GLib.debug("skpping write file - no replacement occured");
789                         return;
790                 }
791                 string tmpfile = "";
792                 GMime.Stream outstream = new GMime.StreamNull();
793                 if (StripApplication.opt_is_replacing) {
794                 
795                         tmpfile = GLib.Environment.get_tmp_dir() +"/" + name;
796                 outstream = new GMime.StreamFile.for_path (tmpfile,"w");
797                 ((GMime.StreamFile)outstream).set_owner(true);
798         }
799                 if (StripApplication.opt_dump) {
800                         outstream = new GMime.StreamMem();
801         }
802         
803         file_size = (int) message.write_to_stream(outstream);
804         if (StripApplication.opt_is_replacing) {
805                 ((GMime.StreamFile)outstream).set_owner(false);
806         }
807                 if (StripApplication.opt_dump) {
808                         var ua = ((GMime.StreamMem)outstream).get_byte_array().data;
809                         print("%s\n", (string) ua);
810                 }        
811         message = null;
812         outstream.flush();
813         outstream.close();
814         GLib.debug("finished writing output %d", file_size);
815
816         //
817         
818           
819         this.used_space_after += file_size;
820         
821         
822         if (StripApplication.opt_is_replacing) {
823                 Posix.unlink(path +"/" + name);         
824                 GLib.debug("copy tmp file %s to %s" , tmpfile, path +"/" + name);               
825                 Posix.link(tmpfile, path +"/" + name);
826                 Posix.unlink(tmpfile);
827                 var nf = File.new_for_path(path +"/" + name);
828                 var newfileinfo = nf.query_info(GLib.FileAttribute.TIME_MODIFIED,GLib.FileQueryInfoFlags.NONE,null);
829                 newfileinfo.set_modification_time(mod_time);
830                 nf.set_attributes_from_info(newfileinfo,FileQueryInfoFlags.NONE);
831                 }
832         this.processed++;
833         
834         if (StripApplication.opt_limit > -1 && this.processed >= StripApplication.opt_limit) {
835                 GLib.debug("Reached replacement limit");
836                 Posix.exit(1);
837         }
838         
839         
840         
841         
842     }
843     
844     
845     public void scan_dir(string path)
846     {
847         var f = File.new_for_path(path);
848                 FileEnumerator file_enum;
849         var cancellable = new Cancellable ();
850         try {      
851             file_enum = f.enumerate_children(
852                 FileAttribute.STANDARD_DISPLAY_NAME + "," +   FileAttribute.STANDARD_TYPE,
853                         FileQueryInfoFlags.NOFOLLOW_SYMLINKS,  // FileQueryInfoFlags.NONE,
854                         cancellable
855                 );
856         } catch (Error e) {
857                 GLib.debug("Got error scanning dir? %s", e.message);
858             // FIXME - show error..
859             return;
860         }
861         FileInfo next_file;
862          
863         while (cancellable.is_cancelled () == false ) {
864             try {
865                 next_file = file_enum.next_file (cancellable);
866             } catch(Error e) {
867                 GLib.debug("error getting next file? %s", e.message);
868                 break;
869             }
870
871             if (next_file == null) {
872                 break;
873             }
874                 
875                 
876                 if (next_file.get_is_symlink()) {
877                 next_file = null;
878                 continue;
879             }
880             
881
882             if (next_file.get_file_type() != FileType.DIRECTORY) {
883                 
884                 if (next_file.get_display_name()[0] == ',') {
885                         continue;
886                 }
887                 // other files to ignore?
888                 
889                 this.scan_file(path , next_file.get_display_name());
890                                 if(this.has_replaced) {
891                          this.report_state("After scanning %s/%s".printf(path , next_file.get_display_name()));
892                         }
893                 continue;
894             }
895
896
897             //stdout.printf("Monitor.monitor: got file %s : type :%u\n",
898             //        next_file.get_display_name(), next_file.get_file_type());
899
900
901         
902             var ds = next_file.get_display_name();
903             // not really needed?? - we are storing attachments in a seperate location now...
904             if (ds[0] == '.') {
905                 next_file = null;
906                 continue;
907             }
908             if (ds == "attachments") {
909                         continue;
910                 }
911             
912             
913             var sp = path+"/"+next_file.get_display_name();
914             // skip modules.
915             //print("got a file : " + sp);
916          
917             next_file = null;
918             
919             
920             this.scan_dir(sp);
921             
922         }
923     
924     
925     }
926     void report_state(string msg) 
927     {
928         // Saved: 2G  Original 10G : 20%
929         GLib.debug("Saved : %s (%.1f%%) | Original %s | %s", 
930                         GLib.format_size(this.used_space_before - this.used_space_after), 
931                         100f * ((1f * (this.used_space_before - this.used_space_after)) / (this.used_space_before * 1f)), 
932                         GLib.format_size(this.used_space_before),                       
933                         msg
934                 );
935         
936         }
937         
938         
939
940 }