summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Denker <jsd@av8n.com>2012-07-15 14:28:10 -0700
committerJohn Denker <jsd@av8n.com>2012-07-15 14:34:05 -0700
commit3e40c30e59db4f060d8f95fca0d5cded75e7ba64 (patch)
treefcd49f3421846017eac64e5c8e0c501ec3a27af2
parent6f18a13a558ecb43beb2eac3e289449c5a4b0aca (diff)
implement multi-line mode as an /option/
-rw-r--r--tools/mail-scan.c62
1 files changed, 45 insertions, 17 deletions
diff --git a/tools/mail-scan.c b/tools/mail-scan.c
index 1260f30..c2b798b 100644
--- a/tools/mail-scan.c
+++ b/tools/mail-scan.c
@@ -4,6 +4,9 @@
// scrutinize email
//
+// hint: mail-scan +from * \...
+// | sed 's/.*@//;s/>$//' | sort | uniq -c | sort -nr > some-junk.from-count
+
#include <iostream>
#include <stdlib.h> /* for exit() */
#include <string>
@@ -126,6 +129,18 @@ void exeunt(const int sts){
exit(sts);
}
+string join(const string sep, list<string> stuff){
+ string rslt;
+ if (!stuff.size()) return rslt;
+ list<string>::const_iterator ptr = stuff.begin();
+ rslt = *ptr++;
+ for (; ptr != stuff.end(); ptr++){
+ rslt += sep;
+ rslt += *ptr;
+ }
+ return rslt;
+}
+
////////////////////////////////////////////////////////////
int main(int _argc, const char** _argv){
//// pid_t pid = getpid();
@@ -138,6 +153,8 @@ int main(int _argc, const char** _argv){
int maxsize(1000000);
list<string> watchword;
list<string> dofile;
+ int vflag(0);
+ int multi(0);
while (argc) {
string arg(*argv); argv++; argc--;
@@ -145,7 +162,13 @@ int main(int _argc, const char** _argv){
if (prefix(arg, "-help")) {
usage(0);
}
- if (arg.substr(0,1) == "-") {
+ if (prefix(arg, "-verify")){
+ vflag++;
+ continue;
+ } else if (prefix(arg, "-multi")){
+ multi++;
+ continue;
+ } else if (arg.substr(0,1) == "-") {
cerr << "Unrecognized option '" << arg << "'" << endl;
cerr << "For help, try: " << progname << " -help" << endl;
exit(sa_usage);
@@ -161,41 +184,42 @@ int main(int _argc, const char** _argv){
file != dofile.end(); file++) {
ifstream infile;
infile.open(file->c_str());
- if (infile.bad()) {
+ if (!infile.good()) {
cerr << "Failed to open file: " << *file << endl;
+ exit(1);
}
int inheads(1);
string boundary("x-xx-x");
int msgsize(0);
- for (;;){
- if (infile.eof()) break;
- if (infile.bad()) return 1;
+ int foundsome_infile(0);
+ for (;;){ // loop over all records in file
if (inheads) {
- string header;
- if (getline(infile, header).fail()) continue;
- msgsize += header.length()+1;
- for (;;) {
+ list<string> Header;
+ string line;
+ for (;;) { // loop over all lines in this record
if (infile.eof()) break;
if (infile.bad()) return 1;
- char ch;
- if (infile.get(ch).fail()) continue;
- infile.putback(ch);
- if (ch != ' ' && ch != '\t') break;
- string line;
if (getline(infile, line).fail()) continue;
+ Header.push_back(line);
msgsize += line.length()+1;
if (msgsize > maxsize) {
cerr << "skrewt rejection: bigger than " << maxsize << endl;
exeunt(sa_spam);
}
- header += "\n" + line;
+ char ch;
+ if (infile.get(ch).fail()) continue;
+ infile.putback(ch);
+ if (ch != ' ' && ch != '\t') break;
}
- if (header.length() == 0) {
+ if (Header.front().length() == 0) {
inheads = 0;
}
else {
string headword;
string rest;
+ string header;
+ if (!multi) header = join(" ", Header);
+ else header = join("\n", Header);
size_t where = header.find(":");
if (where != string::npos) {
headword = header.substr(0, where);
@@ -205,7 +229,9 @@ int main(int _argc, const char** _argv){
for (list<string>::const_iterator ptr = watchword.begin();
ptr != watchword.end(); ptr++) {
if (headword == toLower(*ptr)) {
- cout << *file << " :: " << header << endl;
+ foundsome_infile++;
+ if (!vflag) cout // << foundsome_infile << " "
+ << *file << " :: " << header << endl;
}
}
}
@@ -215,5 +241,7 @@ int main(int _argc, const char** _argv){
break;
}
}
+ if (vflag && !foundsome_infile) cout << foundsome_infile
+ << " ... " << *file << endl;
}
}