/** ** 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. scan each file (over a year old...) extract out the attachment, and replace with HTML DATABASE? - mysql or sqlite? - filesize / name / date / checksum / mimetype -- into mailfort should be OK. b) the imap user emails loop through user's directories check age of email .. over 1 years.. ?? how to prevent 'repeat' scanning of emails? ??? hidden '.' files containing last scan date? check if file exists in our DB.. - replace the link... otherwise generate a file. + add to DB... c) retreival system -> URL -> get file d) redirect system. -> URL -> redirect to correct server More notes on our Mailfort DB sync: * some of these attachments are already in the database... - so we need to update the DB.. - probably worth putting the code in a stored procedure.. -- key scenarios * first scan (and extract) * rescan (as I messed up the first time - fix the DB...) * email scan - attachments might not have related messages. - {id} attachment_init( {exim_msg_id} {chksum} {filename), ) // creates or returns id (can look for existing messages? // can do a merge?? - copy 'old' record data into 'new'.... "prefer checksummed" attachment_update( {id} {exim_msg_id} {mailfort_msg_sig} {file_size} {created} // message date.. {chksum} {filename), {mime_type} ) attachment_update_store( {id} {stored_filename} ) */ // valac --pkg gmime --vapi /* // http://www.fromdual.com/mysql-vala-program-example << check mysql if this does not work. valac -g --vapidir=. --thread strip.vala --vapidir=../vapi \ --pkg glib-2.0 --pkg mysql --pkg gio-2.0 --pkg posix --pkg gmime-2.6 \ --Xcc=-lmysqlclient -v \ -o /tmp/strip */ public class StripApplication : GLib.Application { public static string? opt_path = null; public static string? opt_file = null; public static string? opt_target_path = null; public static string? opt_db_host = "127.0.0.1"; public static string? opt_db_name = null; public static string? opt_db_user = null; public static string? opt_db_pass = null; public static int opt_limit = -1; public static int opt_age_newest = 1; public static int opt_age_oldest = 6; public static bool opt_is_extracting = false; public static bool opt_is_replacing = false; public static bool opt_scan_maildir = false; public static bool opt_scan_mailfort = false; public static bool opt_dump = false; public static bool opt_debug = false; public static bool opt_debug_sql = false; public static string? opt_replace_link = null; public const GLib.OptionEntry[] options = { { "debug", 0, 0, OptionArg.NONE, ref opt_debug, "show debug messages for components", null }, { "debug-sql", 0, 0, OptionArg.NONE, ref opt_debug_sql, "debug the SQL statements", null }, { "path", 0, 0, OptionArg.STRING, ref opt_path, "Directory where email to be parsed is", null }, { "file", 0, 0, OptionArg.STRING, ref opt_file, "A specific file to be parsed", null }, { "target-path", 0, 0, OptionArg.STRING, ref opt_target_path, "Directory where attachments are to be put", null }, { "link", 0, 0, OptionArg.STRING, ref opt_replace_link, "url for the replement link: eg. http://www.mysite.com/xxxx/%s", null }, { "host", 0, 0, OptionArg.STRING, ref opt_db_host, "Mysql host (default localhost)", null }, { "name", 0, 0, OptionArg.STRING, ref opt_db_name, "Mysql database name REQUIRED", null }, { "user", 0, 0, OptionArg.STRING, ref opt_db_user, "Mysql database user REQUIRED", null }, { "pass", 0, 0, OptionArg.STRING, ref opt_db_pass, "Mysql database password (default empty)", null }, { "extract", 0, 0, OptionArg.NONE, ref opt_is_extracting, "Should attachments be extracted (default NO)", null }, { "replace", 0, 0, OptionArg.NONE, ref opt_is_replacing, "Should attachments be replaced (default NO)", null }, { "dump", 0, 0, OptionArg.NONE, ref opt_dump, "Print the replaced mail contents to stdout", null }, { "limit", 0, 0, OptionArg.INT, ref opt_limit, "stop after X number of messages with attachments have been processed", null }, { "newest", 0, 0, OptionArg.INT, ref opt_age_newest, "do not replace messages newer that X months (default is 1 months)", null }, { "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 }, { null } }; public StripApplication( string[] args ) { Object( application_id: "org.roojs.mailstripper", flags: ApplicationFlags.FLAGS_NONE ); var opt_context = new GLib.OptionContext ("Mail Stripper"); try { opt_context.set_help_enabled (true); opt_context.add_main_entries (options, null); opt_context.parse ( ref args); //opt_detach = !optx_no_detach; // options that have to be set.. bee or hive... (or stop all) if ((!opt_scan_mailfort && !opt_scan_maildir) || (opt_scan_mailfort && opt_scan_maildir)) { stdout.printf ("You must specify the type of directory tree to scan - either imap or mailfort\n%s", opt_context.get_help(true, null)); GLib.Process.exit(Posix.EXIT_FAILURE); } if ((opt_db_name == null || opt_db_name.length < 1 || opt_db_user == null || opt_db_user.length < 1)) { stdout.printf ("You must specify the database name / user \n%s", opt_context.get_help(true, null)); GLib.Process.exit(Posix.EXIT_FAILURE); } if ((opt_path == null || opt_path.length < 1) ) { stdout.printf ("You must specify the scan start path\n%s", opt_context.get_help(true, null)); GLib.Process.exit(Posix.EXIT_FAILURE); } if (opt_replace_link == null || (opt_replace_link.length < 1)) { stdout.printf ("You must specify the link to use in the replacement \n%s", opt_context.get_help(true, null)); GLib.Process.exit(Posix.EXIT_FAILURE); } if ((opt_is_replacing || opt_is_extracting ) && (opt_target_path == null || opt_target_path.length < 1)) { stdout.printf ("You must specify a target path to put attachments\n%s", opt_context.get_help(true, null)); GLib.Process.exit(Posix.EXIT_FAILURE); } } catch (GLib.OptionError e) { stdout.printf ("error: %s\n", e.message); stdout.printf ("Run '%s --help' to see a full list of available command line options.\n%s", args[0], opt_context.get_help(true, null)); GLib.Process.exit(Posix.EXIT_FAILURE); } } public static int main(string[] args) { var application = new StripApplication( args); GLib.Log.set_always_fatal(LogLevelFlags.LEVEL_ERROR | LogLevelFlags.LEVEL_CRITICAL); if (opt_debug || opt_debug_sql) { GLib.Log.set_handler(null, GLib.LogLevelFlags.LEVEL_DEBUG | GLib.LogLevelFlags.LEVEL_WARNING | GLib.LogLevelFlags.LEVEL_INFO, (dom, lvl, msg) => { print("%s\n", msg); } ); } GMime.init(0); if (StripApplication.opt_is_replacing) { StripApplication.opt_is_extracting = true; } GLib.debug("scanning folder: %s", opt_path ); var strip = new Strip( opt_path ); strip.mysql = new Mysql.Database(); if (!strip.mysql.real_connect( opt_db_host, opt_db_user , opt_db_pass == null ? "" : opt_db_pass, //passwd opt_db_name, //DB 3306, // not changable...? null ) ) { stdout.printf("ERROR %u: Connection failed: %s\n", strip.mysql.errno(), strip.mysql.error() ); return 1; } if (opt_file != null) { strip.base_dir = opt_path; strip.scan_file( GLib.Path.get_dirname(opt_file), GLib.Path.get_basename(opt_file)); return 0; } strip.scan_dir(opt_path, ""); return 0; } } public class Strip : GLib.Object { public string base_dir = ""; public Mysql.Database mysql; int processed = 0; uint64 used_space_before = 0; uint64 used_space_after = 0; public Strip(string base_dir) { this.base_dir = base_dir; } public void handle_part(GMime.Object parent, GMime.Object mime_obj) { if (mime_obj is GMime.Part) { var p = (GMime.Part)mime_obj; var ct = p.get_content_type(); var cd = p.get_content_disposition(); var sid = p.get_header("X-strip-id"); if (sid != null && sid.length > 0) { this.update_attachment_db(p); GLib.debug("Skip attachment replace - it's already been done"); return; } if (cd == null || cd.get_disposition().down() != "attachment") { return; } if (ct.get_media_type() == "text") { return; } if (ct.to_string() == "application/pgp-encrypted") { return; } if (ct.to_string() == "application/pgp-keys") { return; } if (p.get_filename() == null) { return; } // print("got part %s\n", ct.to_string()); if (parent is GMime.Multipart) { this.replace_attachment(((GMime.Multipart)parent), p); // remove it !? } return; } if (mime_obj is GMime.Multipart) { var mp = (GMime.Multipart)mime_obj; //var ct = mp.get_content_type(); //print("got multi-part %s\n", ct.to_string()); for (var i = 0; i< mp.get_count(); i++) { var mo = mp.get_part(i); this.handle_part(mime_obj,mo); } // ((GMime.Multipart)mime_obj).foreach((sub_obj) => { // Strip.handle_part(sub_obj); // // }); return; } if (mime_obj is GMime.MessagePart) { var msg = ((GMime.MessagePart)mime_obj).get_message(); msg.foreach((subobj) => { this.handle_part(msg,subobj); }); //print("got message-part\n"); return; } if (mime_obj is GMime.Message) { var mp = ((GMime.Message) mime_obj).get_mime_part(); if (!(mp is GMime.Multipart)) { //GLib.debug("get mimepart does not return a Multipart?"); return; } var mpc = ((GMime.Multipart)mp).get_count(); //GLib.debug("Message has %d parts", mpc); for (var i =0 ; i < mpc; i++) { //GLib.debug("Getting part %d", i); var submime_obj = ((GMime.Multipart)mp).get_part(i); this.handle_part(mp,submime_obj); } print("got message??\n"); return; } print("got something else\n"); } public void update_attachment_db(GMime.Part attachment) { // only called when we have an sid... var sid = attachment.get_header("X-strip-id"); if (sid == null || sid.length < 1) { GLib.debug("Strange - update attachment db called ?"); return; } // initialize it with known data.. // that should wipe out dupes. var matches = this.execute("SELECT id 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); return; } var chksum = this.query("SELECT checksum FROM Attachment WHERE id = %d".printf( int.parse(sid) )); var mime_filename = this.query("SELECT mime_filename FROM Attachment WHERE id = %d".printf( int.parse(sid))); this.query(""" SELECT attachment_init( '%s', '%s', '%s', %d ) as id """.printf( this.mysql_escape(this.active_message_exim_id), this.mysql_escape(chksum), this.mysql_escape(mime_filename), int.parse(filesize) )); 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( int.parse(sid), "", // this will be ignored.. this.created_date, this.mysql_escape(this.active_message_x_mailfort_sig) ) ); this.mysql.store_result(); } 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.execute(""" 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, created ) VALUES ( %d, -- id '%s' , -- msgid 0, '%s' , -- filename '%s', -- mimetype '%s', -- stored file anme '', -- charset 'attachment', 0, 0, 0, %d, -- size %d, -- size '%s', -- checkum '%s' -- created: ) """.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), 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.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"); if (sid != null && sid.length > 0) { GLib.debug("Skip attachment replace - it's already been done"); return; } var c = attachment.get_content_object(); var filename = attachment.get_filename().replace("/", "-").replace("\n", "").replace("\t", " "); var fn = GLib.Environment.get_tmp_dir() + "/"+ this.active_name + "."+ filename; var outfile = new GMime.StreamFile.for_path(fn, "w"); outfile.set_owner(true); var file_size = (int) c.write_to_stream(outfile); var chksum = this.md5_file(fn); outfile.flush(); outfile = null; if (file_size == 0) { GLib.debug("ERROR - file size of write to stream returned 0?"); Posix.unlink(fn); return; } var mime_type= attachment.get_content_type().to_string(); // at this point we have to do our database magic... //filesize / name / date / checksum / mimetype -- into mailfort should be OK. var file_id = this.query(""" SELECT attachment_init( '%s', -- in_msgid VARCHAR(32), '%s', -- in_checksum VARCHAR(64), '%s', -- in_mime_filename varchar(255) %d -- filesize ) as id """.printf( this.mysql_escape(this.active_message_exim_id), chksum, this.mysql_escape( attachment.get_filename() ), // what is thsi is invalid? file_size) ); if (file_id.length < 1) { GLib.debug("ERROR - CALL to attachment_init failed"); Posix.unlink(fn); return; } if (int.parse(file_id) < 1) { GLib.debug("ERROR - CALL to attachment_init failed - returned 0?"); Posix.unlink(fn); return; } GLib.debug("fn = %s, m5=%s, id= %s", filename, mime_type, this.active_message_id); 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) ) as result """.printf( int.parse(file_id), this.mysql_escape(mime_type), this.created_date, this.mysql_escape(this.active_message_x_mailfort_sig) )); this.mysql.store_result(); this.used_space_after += file_size; var target_fn = ""; if (StripApplication.opt_is_extracting) { target_fn = StripApplication.opt_target_path + "/" + this.created_dir +"/"+ file_id + "-" + filename; } var stored = "/" + this.created_dir +"/"+ file_id + "-" + filename; this.query(""" SELECT attachment_update_store( %d, -- in_id INT(11), '%s' -- in_store_filename varchar(255), ) as result """.printf( int.parse(file_id), this.mysql_escape( stored) )); var rep = new GMime.Part.with_type("text","html"); // we have to set up a redirect server - to redirect hpasite... to their internal service.. rep.set_filename(filename); string txt = ""+ "" + GLib.Uri.escape_string( filename) + // fixme needs html escaping... "" + ""; 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-content-type", mime_type); var stream = new GMime.StreamMem.with_buffer(txt.data); var con = new GMime.DataWrapper.with_stream(stream,GMime.ContentEncoding.DEFAULT); rep.set_content_object(con); GLib.debug("Replacing Attachment with HTML"); parent.replace(parent.index_of(attachment), rep); this.has_replaced = true; if (StripApplication.opt_is_extracting && target_fn.length > 0) { var dir = GLib.Path.get_dirname(target_fn); if (!FileUtils.test (dir, FileTest.IS_DIR)) { GLib.DirUtils.create_with_parents(dir, 0755); } GLib.debug("Creating file %s", target_fn); if (!FileUtils.test (target_fn, FileTest.EXISTS)) { 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); } Posix.unlink(fn); } public string query(string str) { return this.real_query(true, str); } public string execute(string str) { return this.real_query(false, str); } public string real_query(bool need_return, string str) { GLib.debug("Before Query : %u : %s\n", this.mysql.errno(), this.mysql.error()); if (StripApplication.opt_debug_sql) { GLib.debug("SQL: %s\n", str); } var rc= this.mysql.query(str); if ( rc != 0 ) { GLib.debug("ERROR %u: Query failed: %s\n", this.mysql.errno(), this.mysql.error()); Posix.exit(1); } var rs = mysql.use_result(); //GLib.debug("got %d rows", (int) rs.num_rows()); var got_row = false; string[] row; string ret = ""; while( (row = rs.fetch_row()) != null) { got_row = true; ret = row[0]; } if (!need_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 ""; } if (StripApplication.opt_debug_sql) { GLib.debug("got %s", ret); } return ret; } public string mysql_escape(string str) { unichar[] value_escaped = new unichar[str.length * 2 + 1]; this.mysql.real_escape_string ((string) value_escaped, str, str.length); return (string) value_escaped; } public string md5_file(string fn) { Checksum checksum = new Checksum (ChecksumType.MD5); FileStream stream = FileStream.open (fn, "rb"); uint8 fbuf[100]; size_t size; while ((size = stream.read (fbuf)) > 0) { checksum.update (fbuf, size); } unowned string digest = checksum.get_string (); return digest; } string active_path = ""; string active_name = ""; string active_message_id = ""; string active_message_x_mailfort_sig = ""; string active_message_exim_id = ""; bool has_replaced = false; string created_date = ""; // should be YYYY-mm-dd string created_dir = ""; // should be YYY/mm/dd public void scan_file(string path, string name) { GLib.debug("Scan: %s/%s", path,name); this.has_replaced = false; this.active_path = path; this.active_name = name; this.active_message_id = ""; var mailtime = new DateTime.now_local(); if (StripApplication.opt_scan_mailfort) { this.created_dir = this.active_path.substring(this.base_dir.length + 1 ); this.created_date = this.created_dir.replace("/", "-"); var bits = this.created_date.split("-"); 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_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_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; } } var fileinfo = File.new_for_path(path +"/" + name) .query_info(GLib.FileAttribute.STANDARD_SIZE+","+GLib.FileAttribute.TIME_MODIFIED ,GLib.FileQueryInfoFlags.NONE,null); var file_size = (int) fileinfo.get_size(); var mod_time = fileinfo.get_modification_time(); if (!StripApplication.opt_scan_mailfort) { // it's a mail directory... // use the last modification time? as the default... mailtime = new DateTime.from_timeval_utc(mod_time); this.created_dir = mailtime.format("%Y/%m/%d"); this.created_date = mailtime.format("%Y-%m-%d %H:%M:%S"); } // check on age of file... this.used_space_before += file_size; var stream = new GMime.StreamFs.for_path (path +"/" + name,Posix.O_RDONLY, 0); //stream.set_owner(true); var parser = new GMime.Parser.with_stream(stream); var message = parser.construct_message(); if (message == null) { GLib.debug("Could not parse file? %s/%s", path,name); this.used_space_after += file_size; return; } // check : - is message over a year old? // get various msg info.. this.active_message_id = message.get_message_id(); this.active_message_x_mailfort_sig = message.get_header("x-mailfort-sig"); var recvd = message.get_header("received"); this.active_message_exim_id = ""; if (recvd != null && recvd.length > 1) { 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?"); } } } } var oldest = new DateTime.now_local(); 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_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; } /* GLib.debug("Message DATA:\n mid: %s\nmailfort: %s \nexim_id: %s", this.active_message_id, this.active_message_x_mailfort_sig, this.active_message_exim_id ); */ // DATE? var mp = message.get_mime_part(); if (!(mp is GMime.Multipart)) { //GLib.debug("get mimepart does not return a Multipart?"); this.used_space_after += file_size; return; } var mpc = ((GMime.Multipart)mp).get_count(); //GLib.debug("Message has %d parts", mpc); for (var i =0 ; i < mpc; i++) { //GLib.debug("Getting part %d", i); var mime_obj = ((GMime.Multipart)mp).get_part(i); this.handle_part(mp,mime_obj); } parser= null; // stream.set_owner(false); //stream.close(); stream = null;//.close(); if (!this.has_replaced) { this.used_space_after += file_size; GLib.debug("skpping write file - no replacement occured"); return; } string tmpfile = ""; GMime.Stream outstream = new GMime.StreamNull(); if (StripApplication.opt_is_replacing) { tmpfile = GLib.Environment.get_tmp_dir() +"/" + name; outstream = new GMime.StreamFile.for_path (tmpfile,"w"); ((GMime.StreamFile)outstream).set_owner(true); } if (StripApplication.opt_dump) { outstream = new GMime.StreamMem(); } file_size = (int) message.write_to_stream(outstream); if (StripApplication.opt_is_replacing) { ((GMime.StreamFile)outstream).set_owner(false); } if (StripApplication.opt_dump) { var ua = ((GMime.StreamMem)outstream).get_byte_array().data; print("%s\n", (string) ua); } message = null; outstream.flush(); outstream.close(); GLib.debug("finished writing output %d", file_size); // outstream = null; this.used_space_after += file_size; if (StripApplication.opt_is_replacing) { Posix.unlink(path +"/" + name); GLib.debug("copy tmp file %s to %s" , tmpfile, 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++; if (StripApplication.opt_limit > -1 && this.processed >= StripApplication.opt_limit) { GLib.debug("Reached replacement limit"); Posix.exit(1); } } public void scan_dir(string basepath, string subpath) { // 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 { file_enum = f.enumerate_children( FileAttribute.STANDARD_DISPLAY_NAME + "," + FileAttribute.STANDARD_TYPE, FileQueryInfoFlags.NOFOLLOW_SYMLINKS, // FileQueryInfoFlags.NONE, cancellable ); } catch (Error e) { GLib.debug("Got error scanning dir? %s", e.message); // FIXME - show error.. return; } FileInfo next_file; while (cancellable.is_cancelled () == false ) { try { next_file = file_enum.next_file (cancellable); } catch(Error e) { GLib.debug("error getting next file? %s", e.message); break; } if (next_file == null) { break; } if (next_file.get_is_symlink()) { next_file = null; continue; } var ds = next_file.get_display_name(); if (next_file.get_file_type() != FileType.DIRECTORY) { if (ds[0] == ',') { continue; } // other files to ignore? 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 , ds)); } continue; } //stdout.printf("Monitor.monitor: got file %s : type :%u\n", // next_file.get_display_name(), next_file.get_file_type()); // not really needed?? - we are storing attachments in a seperate location now... if (ds[0] == '.') { next_file = null; continue; } if (ds == "attachments") { continue; } var sp = subpath+"/"+next_file.get_display_name(); // skip modules. //print("got a file : " + sp); next_file = null; this.scan_dir(basepath,sp); } } void report_state(string msg) { // Saved: 2G Original 10G : 20% GLib.debug("Saved : %s (%.1f%%) | Original %s | %s", GLib.format_size(this.used_space_before - this.used_space_after), 100f * ((1f * (this.used_space_before - this.used_space_after)) / (this.used_space_before * 1f)), GLib.format_size(this.used_space_before), msg ); } }