From d2564d25e802d1ee3230cf045c4940e836b5c6a2 Mon Sep 17 00:00:00 2001 From: John Denker Date: Sun, 29 Jul 2012 16:50:11 -0700 Subject: split ltgrey (and libltgrey) off from greylist; put some utility functions into their own file. --- tools/utils.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 tools/utils.c (limited to 'tools/utils.c') diff --git a/tools/utils.c b/tools/utils.c new file mode 100644 index 0000000..3ec6e4c --- /dev/null +++ b/tools/utils.c @@ -0,0 +1,44 @@ +#include +#include +#include +//#include /* for abs() */ +#include + +// strip off the directory part of a path, leaving just +// the basic filename +std::string basename(const std::string path){ + size_t where = path.rfind("/"); + if (where != std::string::npos) return path.substr(1+where); + return path; +} + +//////////////// +// little utility to help with argument parsing: +// +int prefix(const std::string shorter, const std::string longer){ + return shorter == longer.substr(0, shorter.length()); +} + +/////////////// +// print a time as (-)hh:mm:ss +// +std::string time_out(const int _ttt){ +using namespace std; + int ttt(abs(_ttt)); + int sec(ttt % 60); + int min((ttt / 60) % 60); + int hr(ttt / 3600); + stringstream foo; + int didsome(0); + if (_ttt < 0) foo << "-"; + if (hr) { + foo << hr << ":"; + didsome++; + } + if (didsome || min){ + foo << setw(didsome?2:1) << setfill('0') << min << ":"; + didsome++; + } + foo << setw(didsome?2:1) << setfill('0') << sec; + return foo.str(); +} -- cgit v1.2.3 From 63be414b62f3234ad80607b95e8e71e33bfd8025 Mon Sep 17 00:00:00 2001 From: John Denker Date: Sun, 29 Jul 2012 17:00:15 -0700 Subject: move more stuff to utils.c ... I hate duplication of code --- tools/hi-test.c | 14 +------------- tools/mail-scan.c | 26 +------------------------- tools/makefile | 13 ++++++++----- tools/skrewt.c | 32 +------------------------------- tools/utils.c | 16 ++++++++++++++++ tools/utils.h | 3 +++ 6 files changed, 30 insertions(+), 74 deletions(-) (limited to 'tools/utils.c') diff --git a/tools/hi-test.c b/tools/hi-test.c index 0661ada..cd0152c 100644 --- a/tools/hi-test.c +++ b/tools/hi-test.c @@ -6,6 +6,7 @@ #include #include /* perror() */ +#include "utils.h" using namespace std; @@ -16,13 +17,6 @@ const int sa_usage(64); int verbosity(0); -//////////////// -// little utility to help with argument parsing: -// -int prefix(const string shorter, const string longer){ - return shorter == longer.substr(0, shorter.length()); -} - void exeunt(const int sts){ if (sts == sa_good) exit(sts); @@ -69,12 +63,6 @@ void countsome(const int unit){ << " read " << total << " bytes from unit " << unit << endl; } -string basename(const string path){ - size_t where = path.rfind("/"); - if (where != string::npos) return path.substr(1+where); - return path; -} - int main(int _argc, const char** _argv){ int snooze(0); int status(0); diff --git a/tools/mail-scan.c b/tools/mail-scan.c index dc8aa5c..b0c4137 100644 --- a/tools/mail-scan.c +++ b/tools/mail-scan.c @@ -31,7 +31,7 @@ #include /* perror */ #include -////#include +#include "utils.h" using namespace std; @@ -105,30 +105,6 @@ int cmp_casefold(const std::string& a, const std::string& b) { return 0; } - -string toLower(const std::string& a){ - string rslt = a; - string::iterator rr; - for (rr = rslt.begin(); rr != rslt.end(); rr++){ - *rr = tolower(*rr); - } - return rslt; -} - -//////////////// -string ltrim(string foo){ - size_t where = foo.find_first_not_of(" \t\r\n"); - if (where == foo.npos) return foo; - return foo.substr(where); -} - -//////////////// -// little utility to help with argument parsing: -// -int prefix(const string shorter, const string longer){ - return shorter == longer.substr(0, shorter.length()); -} - void exeunt(const int sts){ if (sts == sa_good) exit(sts); diff --git a/tools/makefile b/tools/makefile index f0a3f70..6594ca8 100644 --- a/tools/makefile +++ b/tools/makefile @@ -37,8 +37,8 @@ all: $(qprogs) $(moreprogs) show: : --- $(qprogs) +++ $(moreprogs) -skrewt: skrewt.o - $(CC) $< -lboost_filesystem-mt -lboost_system -o $@ +skrewt: skrewt.o utils.o + $(CC) $^ -lboost_filesystem-mt -lboost_system -o $@ greylist: greylist.o utils.o $(CC) $^ -lboost_filesystem-mt -lboost_system -o $@ @@ -47,11 +47,14 @@ ltgrey: ltgrey.o utils.o libltgrey.o $(CC) $^ -lboost_filesystem-mt -lboost_system -o $@ wripper: wripper.o - $(CC) $< -o $@ + $(CC) $^ -o $@ chgrp daemon $@ && chmod g+s $@ || true -mail-scan: mail-scan.o - $(CC) $< -lboost_regex -o $@ +mail-scan: mail-scan.o utils.o + $(CC) $^ -lboost_regex -o $@ + +hi-test: hi-test.o utils.o + $(CC) $^ -lboost_regex -o $@ install: install $(qprogs) /var/qmail/bin/ diff --git a/tools/skrewt.c b/tools/skrewt.c index 3fee644..6749a01 100644 --- a/tools/skrewt.c +++ b/tools/skrewt.c @@ -38,7 +38,7 @@ void usage(const int sts){ } #include "qq_exit_codes.h" - +#include "utils.h" ///////////////////////////////////////////////////////// // Case insensitive comparison of strings @@ -83,23 +83,6 @@ int cmp_casefold(const std::string& a, const std::string& b) { return 0; } - -string toLower(const std::string& a){ - string rslt = a; - string::iterator rr; - for (rr = rslt.begin(); rr != rslt.end(); rr++){ - *rr = tolower(*rr); - } - return rslt; -} - -//////////////// -string ltrim(const string foo){ - size_t where = foo.find_first_not_of(" \t\r\n"); - if (where == foo.npos) return foo; - return foo.substr(where); -} - string noCR(const string bar){ string foo(bar); int len = foo.length(); @@ -111,13 +94,6 @@ string noCR(const string bar){ return foo; } -//////////////// -// little utility to help with argument parsing: -// -int prefix(const string shorter, const string longer){ - return shorter == longer.substr(0, shorter.length()); -} - void maybe_exeunt(const int sts, const int really){ if (!really) return; if (sts == ex_good) exit(sts); @@ -142,12 +118,6 @@ void exeunt(const int sts){ maybe_exeunt(sts, 1); } -string basename(const string path){ - size_t where = path.rfind("/"); - if (where != string::npos) return path.substr(1+where); - return path; -} - string progname, progid; int mypid; diff --git a/tools/utils.c b/tools/utils.c index 3ec6e4c..aecbfda 100644 --- a/tools/utils.c +++ b/tools/utils.c @@ -42,3 +42,19 @@ using namespace std; foo << setw(didsome?2:1) << setfill('0') << sec; return foo.str(); } + +std::string toLower(const std::string a){ + std::string rslt = a; + std::string::iterator rr; + for (rr = rslt.begin(); rr != rslt.end(); rr++){ + *rr = tolower(*rr); + } + return rslt; +} + +//////////////// +std::string ltrim(const std::string foo){ + size_t where = foo.find_first_not_of(" \t\r\n"); + if (where == foo.npos) return foo; + return foo.substr(where); +} diff --git a/tools/utils.h b/tools/utils.h index 450db85..ec467c6 100644 --- a/tools/utils.h +++ b/tools/utils.h @@ -1,3 +1,6 @@ std::string basename(const std::string path); int prefix(const std::string shorter, const std::string longer); std::string time_out(const int _ttt); + +std::string toLower(const std::string a); +std::string ltrim(const std::string a); -- cgit v1.2.3 From f8be4baf5a2318363b42f8883f66ed8a976dfc79 Mon Sep 17 00:00:00 2001 From: John Denker Date: Sun, 29 Jul 2012 23:26:28 -0700 Subject: scan40 appears to be working, much cleaner than last week's version --- tools/libltgrey.c | 63 +++++++++++++++++++++++++++++++++++++-------- tools/libltgrey.h | 7 +++++ tools/ltgrey.c | 77 ++----------------------------------------------------- tools/utils.c | 16 ++++++++++++ tools/utils.h | 1 + 5 files changed, 78 insertions(+), 86 deletions(-) (limited to 'tools/utils.c') diff --git a/tools/libltgrey.c b/tools/libltgrey.c index 3a60daa..4e92665 100644 --- a/tools/libltgrey.c +++ b/tools/libltgrey.c @@ -32,11 +32,6 @@ const int minimum_age(15*minute); const int maximum_age(32*day); const int probation(4*hour); -// beware: access illogically comes *before* modification -// in the array passed to utimes: -const int upd_ac(0); -const int upd_mod(1); - #if 0 void exeunt(const int sts){ if (sts == ex_good) exit(sts); @@ -408,8 +403,16 @@ done: } state_40 whatsit::get40(const string mid){ - string fname = parent_dir + "/quarante/mid_" + mid; - cerr << ".... " << fname << endl; + timeval junk[2]; + return get40(mid, junk); +} + +state_40 whatsit::get40(const string mid, timeval times[2]){ + timeval now; + gettimeofday(&now, NULL); + times[0] = times[1] = now; // in case of early return + string fname = parent_dir + "/quarante/mid_" + purify(mid); + //xxx cerr << ".... " << fname << endl; struct stat mid_stat; int rslt = stat(fname.c_str(), &mid_stat); if (rslt != 0) { @@ -419,8 +422,10 @@ state_40 whatsit::get40(const string mid){ perror(0); return fail; } - timeval now; - gettimeofday(&now, NULL); + times[upd_ac ].tv_sec = mid_stat.st_atime; + times[upd_ac ].tv_usec = 0; + times[upd_mod].tv_sec = mid_stat.st_mtime; + times[upd_mod].tv_usec = 0; int mod_age = now.tv_sec - mid_stat.st_mtime; // int ac_age = now.tv_sec - mid_stat.st_atime; if (mod_age < minimum_age) return green; @@ -429,8 +434,8 @@ state_40 whatsit::get40(const string mid){ } int whatsit::set40(const string mid, const int shift){ - string fname = parent_dir + "/quarante/mid_" + mid; - cerr << ".... " << fname << endl; + string fname = parent_dir + "/quarante/mid_" + purify(mid); + //xxx cerr << ".... " << fname << endl; int fd = creat(fname.c_str(), 0644); if (fd < 0){ cerr << progid << ": create failed for '" @@ -465,3 +470,39 @@ int whatsit::set40(const string mid, const int shift){ } return 0; } + +void whatsit::scan40(const int copies){ + timeval now; + gettimeofday(&now, NULL); + using namespace boost::filesystem; + + string dir40 = parent_dir + "/quarante"; + + if (is_directory(dir40)) { + for (directory_iterator itr(dir40); itr!=directory_iterator(); ++itr) { + string basename = itr->path().filename(); + if (is_regular_file(itr->status())) { + string tag("mid_"); + if (prefix(tag, basename)) { + for (int ii = 0; ii < copies; ii++) + cout << setw(20) << left << basename; // display filename only + + timeval times[2]; + state_40 rslt = get40(basename.substr(tag.length()), times); + int mod_age = now.tv_sec - times[upd_mod].tv_sec; + int ac_age = now.tv_sec - times[upd_ac ].tv_sec; + cout << setw(10) << right << time_out(mod_age) + << " " << setw(10) << right << time_out(ac_age); + cout << " " << decode_40[rslt]; + cout << endl; + } else { + /* filename does not begin with tag "mid_" */ + } + } + } + } + else { + // starting point is not a directory: + cout << (exists(dir40) ? "Found: " : "Not found: ") << dir40 << '\n'; + } +} diff --git a/tools/libltgrey.h b/tools/libltgrey.h index cf744d1..14ed144 100644 --- a/tools/libltgrey.h +++ b/tools/libltgrey.h @@ -3,6 +3,11 @@ #include #include +// beware: access illogically comes *before* modification +// in the array passed to utimes: +const int upd_ac(0); +const int upd_mod(1); + #define state_40_macro \ foo(unseen) \ foo(green) \ @@ -40,5 +45,7 @@ public: void dump(const std::string var); int maybe_mkdir(const std::string somedir, const std::string msg); state_40 get40(const std::string mid); + state_40 get40(const std::string mid, timeval times[2]); int set40(const std::string mid, const int shift); + void scan40(const int copies); }; diff --git a/tools/ltgrey.c b/tools/ltgrey.c index bb43b80..1494338 100644 --- a/tools/ltgrey.c +++ b/tools/ltgrey.c @@ -33,7 +33,7 @@ int main(int _argc, char** _argv){ string set40_mid; while (argc > 0) { string arg = argv[0]; argc--; argv++; - if (prefix(arg, "-scan")) { + if (prefix(arg, "-scan40")) { scanmode++; } else if (prefix(arg, "-copy")) { copies++; @@ -99,8 +99,7 @@ int main(int _argc, char** _argv){ } if (scanmode) { - string dirname = parent_dir + "/quarante"; - scan(foo.progid, dirname, copies); + foo.scan40(copies); return 0; } @@ -108,75 +107,3 @@ int main(int _argc, char** _argv){ return sts; } - -////////////////////////////////////////////////////////////////////// -// requires apt-get install libboost-filesystem-dev: -#include -#include -#include /* for stat(), getaddrinfo() */ -#include /* for stat() */ -#include /* for stat() */ -#include /* for perror */ -#include - -const int minute(60); -const int hour(60*minute); -const int day(24*hour); - -const int minimum_age(15*minute); -const int maximum_age(32*day); -const int probation(4*hour); - -void scan(const string progid, const string p, const int copies){ - timeval now; - gettimeofday(&now, NULL); - using namespace boost::filesystem; - - if (is_directory(p)) { - for (directory_iterator itr(p); itr!=directory_iterator(); ++itr) { - string basename = itr->path().filename(); - for (int ii = 0; ii < copies; ii++) - cout << setw(20) << left << basename << ' '; // display filename only - if (is_regular_file(itr->status())) { -// cout << " [" << file_size(itr->path()) << ']'; - struct stat mystat; - string fn = p + "/" + basename; - int rslt = stat(fn.c_str(), &mystat); - if (rslt != 0){ - cerr << progid << ": stat failed for '" - << fn << "' : "; - perror(0); - } - int mod_age = now.tv_sec - mystat.st_mtime; - int ac_age = now.tv_sec - mystat.st_atime; - cout << setw(10) << time_out(mod_age) - << " " << setw(10) << time_out(ac_age); - if (0) { - - } else if (mod_age < 0) { - cout << " penalty"; - } else if (mod_age < ac_age) { - cout << " parole"; - } else if (mod_age - ac_age < minimum_age // early bird, or completely unused - && mod_age > probation) { // did not diligently resubmit - cout << " disprobation"; - if (mod_age != ac_age) cout << "!"; - } else if (mod_age < minimum_age) { - cout << " young"; - if (mod_age != ac_age) cout << "!"; - } else if (mod_age == ac_age) { - cout << " unused"; - } else if (mod_age > maximum_age) { - cout << " expired"; - } else { - cout << " OK"; - } - } - cout << '\n'; - } - } - else { - // starting point is not a directory: - cout << (exists(p) ? "Found: " : "Not found: ") << p << '\n'; - } -} diff --git a/tools/utils.c b/tools/utils.c index aecbfda..c544f20 100644 --- a/tools/utils.c +++ b/tools/utils.c @@ -3,6 +3,7 @@ #include //#include /* for abs() */ #include +#include /* for isalnum() */ // strip off the directory part of a path, leaving just // the basic filename @@ -58,3 +59,18 @@ std::string ltrim(const std::string foo){ if (where == foo.npos) return foo; return foo.substr(where); } + +static const std::string Pure_Enough("+-_.,@%~"); + +std::string purify(const std::string arg){ + using namespace std; + string rslt(arg); + for (string::iterator ptr = rslt.begin(); + ptr != rslt.end(); ptr++){ + char ch = *ptr; + if (isalnum(ch)) continue; + if (Pure_Enough.find(ch) != string::npos) continue; + *ptr = '~'; + } + return rslt; +} diff --git a/tools/utils.h b/tools/utils.h index ec467c6..cbcb795 100644 --- a/tools/utils.h +++ b/tools/utils.h @@ -4,3 +4,4 @@ std::string time_out(const int _ttt); std::string toLower(const std::string a); std::string ltrim(const std::string a); +std::string purify(const std::string arg); -- cgit v1.2.3