From 174d2e3ae49ed3776449d3dade24cebee304bfb5 Mon Sep 17 00:00:00 2001 From: John Denker Date: Thu, 22 Nov 2012 15:34:11 -0800 Subject: move argParser to utils.h --- tools/libltgrey.c | 7 +++-- tools/ltgrey.c | 84 ++++++++++++++++++++----------------------------------- tools/utils.h | 44 +++++++++++++++++++++++++++++ 3 files changed, 80 insertions(+), 55 deletions(-) (limited to 'tools') diff --git a/tools/libltgrey.c b/tools/libltgrey.c index 8ae374c..827dcee 100644 --- a/tools/libltgrey.c +++ b/tools/libltgrey.c @@ -368,9 +368,9 @@ int whatsit::set_rep(const string mid, const int shift){ return set_box(box_rep, mid, shift); } -int whatsit::set_box(const boxer box, const string mid, const int shift){ +int whatsit::set_box(const boxer box, const string ID, const int shift){ string fname = parent_dir + "/" + box.dir - + "/mid_" + purify(mid); + + "/" + box.prefix + purify(ID); //xxx cerr << ".... " << fname << endl; int fd = creat(fname.c_str(), 0644); if (fd < 0){ @@ -413,6 +413,9 @@ void whatsit::scan_box(const boxer mybox, const int copies){ using namespace boost::filesystem; string mydir = parent_dir + "/" + mybox.dir; + cerr << "scan_box: scanning " << mydir + << " prefix: " << mybox.prefix + << endl; if (is_directory(mydir)) { for (directory_iterator itr(mydir); itr!=directory_iterator(); ++itr) { diff --git a/tools/ltgrey.c b/tools/ltgrey.c index 577cbe9..85604e5 100644 --- a/tools/ltgrey.c +++ b/tools/ltgrey.c @@ -2,8 +2,8 @@ #include /* for exit(), atoi() */ #include "libltgrey.h" -#include "utils.h" #include "qq_exit_codes.h" +#include "utils.h" #include using namespace std; @@ -12,45 +12,6 @@ string progname; #define exeunt exit -typedef const char cc; -typedef cc* str; - -class argParser{ -public: - list argv; - string current_arg; - - argParser(const int _argc, const str _argv[]) - { - for (int ii=0; ii < _argc; ii++) { - argv.push_back(_argv[ii]); - } - } - string shift() { - string rslt = argv.front(); - argv.pop_front(); - return rslt; - } - void next(){ - current_arg = shift(); - } - size_t size() const { - return argv.size(); - } - int prefix(const string longer, const size_t required = 0){ - if (argv.size() < required) { - if (required==1) - cerr << "Option '" << current_arg - << "' requires an argument." << endl; - else - cerr << "Option '" << current_arg - << "' requires " << required << " arguments." << endl; - exeunt(ex_syserr); - } - return current_arg == longer.substr(0, current_arg.length()); - } -}; - void usage(const string parent_dir){ cout << "Usage: ltgrey [options]\n" @@ -58,10 +19,10 @@ void usage(const string parent_dir){ "Options include\n" " -scan40 # scan the quarantine directory\n" " -scanrep # scan the reputation directory\n" -" -copy # no idea\n" +" -copy # increment the 'copies' variable\n" " -verbose # increase the verbosity\n" -" -setrep mid # set the reputation for the given message-id\n" -" -getrep mid # look up the reputation\n" +" -setrep dom # set the reputation for the given domain\n" +" -getrep dom # look up the reputation for the given domain\n" " -shift ??? # no idea\n" " -stain ??? # not implemented\n" " -suffix ??? # no idea\n" @@ -72,6 +33,20 @@ void usage(const string parent_dir){ cout << "parent dir is " << parent_dir << endl; cout << "box_40.dir is " << box_40.dir << endl; cout << "box_rep.dir is " << box_rep.dir << endl; + + cout << +"\n" +"Principles of operation:\n" +"\n" +"1) Quarantine applies to a particular message. Messages\n" +" are identified by their MID i.e. message-ID.\n" +"\n" +"2a) Ideally, reputation applies to a sending-domain.\n" +"\n" +"2b) If we can't identify a domain that will take responsibility\n" +" for sending the message, then reputation applies to the host.\n" +; + return; } int main(int _argc, char** _argv){ @@ -100,11 +75,11 @@ int main(int _argc, char** _argv){ int dns_mode(0); string get_40_mid; string set_40_mid; - string get_rep_mid; - string set_rep_mid; + string get_rep_domain; + string set_rep_domain; argParser ARGS(argc, argv); - while (ARGS.size()) { + try {while (ARGS.size()) { ARGS.next(); if (0){ } else if (ARGS.prefix("-help")) { @@ -125,9 +100,9 @@ int main(int _argc, char** _argv){ } else if (ARGS.prefix("-set40", 1)) { set_40_mid = ARGS.shift(); } else if (ARGS.prefix("-setrep", 1)) { - set_rep_mid = ARGS.shift(); + set_rep_domain = ARGS.shift(); } else if (ARGS.prefix("-getrep", 1)) { - get_rep_mid = ARGS.shift(); + get_rep_domain = ARGS.shift(); } else if (ARGS.prefix("-shift"), 1) { shift = atoi(*argv++); argc--; } else if (ARGS.prefix("-stain", 1)) { @@ -137,8 +112,11 @@ int main(int _argc, char** _argv){ foo.suffix += ARGS.shift(); } else { cerr << "Unrecognized arg: " << ARGS.current_arg << endl; - exeunt(ex_syserr); + throw int(1); } + }} + catch (int){ + exeunt(ex_syserr); } // dns_mode mode ... @@ -160,14 +138,14 @@ int main(int _argc, char** _argv){ return foo.set_40(set_40_mid, shift); } - if (get_rep_mid.length()){ - box_state rslt = foo.get_rep(get_rep_mid); + if (get_rep_domain.length()){ + box_state rslt = foo.get_rep(get_rep_domain); cerr << foo.decode_40[rslt] << endl; return 0; } - if (set_rep_mid.length()){ - return foo.set_rep(set_rep_mid, shift); + if (set_rep_domain.length()){ + return foo.set_rep(set_rep_domain, shift); } if (scan40mode) { diff --git a/tools/utils.h b/tools/utils.h index 2c531b8..4f5418f 100644 --- a/tools/utils.h +++ b/tools/utils.h @@ -1,3 +1,6 @@ +#include +#include + std::string basename(const std::string path); int prefix(const std::string shorter, const std::string longer); std::string time_out(const int _ttt); @@ -12,3 +15,44 @@ std::string rtrim(const std::string foo, std::string trim(const std::string foo, const std::string strip = " \t\r\n"); + +typedef const char cc; +typedef cc* str; + +class argParser{ +public: + std::list argv; + std::string current_arg; + + argParser(const int _argc, const str _argv[]) + { + for (int ii=0; ii < _argc; ii++) { + argv.push_back(_argv[ii]); + } + } + std::string shift() { + using namespace std; + string rslt = argv.front(); + argv.pop_front(); + return rslt; + } + void next(){ + current_arg = shift(); + } + size_t size() const { + return argv.size(); + } + int prefix(const std::string longer, const size_t required = 0){ + using namespace std; + if (argv.size() < required) { + if (required==1) + cerr << "Option '" << current_arg + << "' requires an argument." << endl; + else + cerr << "Option '" << current_arg + << "' requires " << required << " arguments." << endl; + throw int(1); + } + return current_arg == longer.substr(0, current_arg.length()); + } +}; -- cgit v1.2.3