// import/import.cc // This file is part of Decimail; see http://decimail.org // (C) 2004 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 "Message.hh" #include "MessageFile.hh" #include "DmDatabase.hh" #include "Exception.hh" #include "Directory.hh" #include "utils.hh" #include #include using namespace std; void import_message(DmDatabase& db, string path, int id, string username) { MessageFile m(path,id); db.insert(m,username); } void scan_directory(DmDatabase& db, string prefix, string dn, string username) { Directory d(dn); for(Directory::const_iterator i=d.begin(); i!=d.end(); ++i) { if ((*i).type==Directory::Entry::file) { string path = (*i).pathname; int id = string_to_int(prefix + (*i).leafname); import_message(db,path,id,username); cerr << '.'; } else { if ((*i).leafname=="." || (*i).leafname=="..") { } else { scan_directory(db, prefix+(*i).leafname, (*i).pathname, username); } } } } int extract_id_from_fn(string fn) { int id=0; for(string::const_iterator i=fn.begin(); i!=fn.end(); ++i) { if (isdigit(*i)) { id = (10*id) + (*i - '0'); } } return id; } void scan_file(DmDatabase& db, string fn, string username) { int id = extract_id_from_fn(fn); cout << "message id = " << id << endl; import_message(db, fn, id, username); } static void usage(void) { cerr << "usage: dmimport [-d directory | -f file] username" << endl; } int main(int argc, char* argv[]) { if (argc!=4) { exit(1); } string opt=argv[1]; string fn=argv[2]; string username=argv[3]; try { try { DmDatabase db; DmDatabase::Transaction t(&db); if (opt=="-d") { scan_directory(db,"",fn,username); cerr << endl; } else if (opt=="-f") { scan_file(db,fn,username); } else { usage(); exit(1); } t.commit(); } RETHROW_MISC_EXCEPTIONS; } catch (Exception& e) { e.report(cerr); exit(e.exit_status); } }