// deleted/main.cc // This file is part of Decimail; see http://decimail.org // (C) 2004-2007 Philip Endecott // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation; either version 2 of the License, or // any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "DmDatabase.hh" #include "Configuration.hh" #include "select.hh" #include using namespace std; using namespace pbe; static void delete_message(int msg_id) { assert(msg_id<1000000); char numstr[7]; sprintf(numstr,"%06d",msg_id); string fn = Configuration::singleton().get_str("message_backup_dir"); fn += numstr[0]; fn += numstr[1]; fn += "/"; fn += numstr[2]; fn += numstr[3]; fn += "/"; fn += numstr[4]; fn += numstr[5]; syslog(LOG_INFO,"Deleting %s",fn.c_str()); int rc = unlink(fn.c_str()); if (rc) { throw_ErrnoException("unlink("+fn+")"); } } static void wait_for_any_pending(DmDatabase& db) { while (1) { bool timed_out = db.get_fd().wait_until_readable_or_timeout(1000.0); if (timed_out) { return; } string n = db.get_any_notification(); if (n=="delete_log") { return; } } } static void delete_any_pending(DmDatabase& db) { Transaction t(db); Query delete_from_delete_log (db, "delete from delete_log where msg_id=$1"); ColumnQuery q(db, "select msg_id from delete_log for update"); typedef ColumnResult col_t; col_t r = q.runonce(); for (col_t::const_iterator i=r.begin(); i != r.end(); ++i) { int m = *i; delete_message(m); delete_from_delete_log(m); } t.commit(); } int main(int argc, char* argv[]) { try { try { openlog("dmdeleted",LOG_PID,LOG_MAIL); syslog(LOG_INFO,"Decimail deleted starting"); for (int i=1; i listen (db,"listen delete_log"); listen(); while(1) { delete_any_pending(db); wait_for_any_pending(db); } } RETHROW_MISC_EXCEPTIONS } catch (Exception& E) { ostringstream err_msg; E.report(err_msg); syslog(LOG_CRIT,"Decimail deleted failed: %s",err_msg.str().c_str()); exit(1); } }