From 0f0fc6dbb7e7354ac662951f124b0a64c54b741b Mon Sep 17 00:00:00 2001 From: John Denker Date: Mon, 23 Jul 2012 21:37:35 -0700 Subject: get line-counter to work --- tools/skrewt.c | 134 +++++++++++++++++++++++++++++++++------------------------ 1 file changed, 77 insertions(+), 57 deletions(-) (limited to 'tools/skrewt.c') diff --git a/tools/skrewt.c b/tools/skrewt.c index ed0e627..d2e1bbc 100644 --- a/tools/skrewt.c +++ b/tools/skrewt.c @@ -145,56 +145,52 @@ string basename(const string path){ string progname, progid; int mypid; - /* Content-Type: text/plain; charset="us-ascii" */ /* Content-Type: multipart/mixed; boundary="1170861315-1262462055-1341954763=:92165" */ +// void parse_content(const string type_spec_line, string &maintype, string &boundary) { - string mainline(type_spec_line); - - string get_type(toLower(mainline)); - size_t where = get_type.find_first_of(" \t;\n"); - if (where == string::npos) { - // keep whole string - } - else { - get_type = get_type.substr(0,where); - } - where = get_type.find("/"); - if (where == string::npos){ - maintype = ""; - } else { - maintype = get_type.substr(0, where); - } -} - -#ifdef xxxxxxxxxxxxxxxxxxxx - if - if (0) cerr << "type (" << get_type << ") " - << (text_type?"text":"nope") << endl; - - - string srch = "boundary="; - where = headrec.find(srch); - if (where != string::npos) { - where += srch.length(); - boundary = headrec.substr(where); - if (boundary[0] == '"') { - boundary = boundary.substr(1); - where = boundary.find_first_of("\""); - } else { - where = boundary.find_first_of(" \t;\n"); - } - if (where == string::npos) { - /* do nothing, boundary=boundary as a whole */ - } else { - boundary = boundary.substr(0, where); - } - } - } -#endif + cerr << "parser called with: " << type_spec_line << endl; + string get_type(type_spec_line); + size_t where = get_type.find_first_of(" \t;\n"); + string rest; + if (where == string::npos) { + // keep whole string + } + else { + rest = get_type.substr(where+1); + get_type = get_type.substr(0,where); + } + where = get_type.find("/"); + if (where == string::npos){ + maintype = ""; + cerr << "could not find / in " << get_type << endl; + } else { + maintype = get_type.substr(0, where); + } +// now need to find boundary + string srch = "boundary="; + where = rest.find(srch); + if (where != string::npos) { + where += srch.length(); + boundary = rest.substr(where); + if (boundary[0] == '"') { + boundary = boundary.substr(1); + where = boundary.find_first_of("\""); + } else { + where = boundary.find_first_of(" \t;\n"); + } + if (where == string::npos) { + /* do nothing, boundary=boundary as a whole */ + } else { + boundary = boundary.substr(0, where); + } + } else { + //xxxxxxx cerr << "boundary= not found in " << type_spec_line << endl; + } +} //////////////////////////////////////////////////////////// @@ -329,15 +325,17 @@ int main(int _argc, const char** _argv){ exeunt(ex_spam); // disallow mail with no date } - string contype; - int textlines(0); - parse_content(content_type, contype, boundary); - int is_text = contype == "text"; + string main_contype; + parse_content(content_type, main_contype, boundary); + int currently_text = main_contype == "text"; + int main_multipart = main_contype == "multipart"; // early-stage thinking has been done. // Now spew the rest of the message - cerr << "body begins: " << contype << " " << is_text << endl; - int inheads(0); + cerr << "body begins: " << main_contype << " " << currently_text << " " << boundary << endl; + int in_subheads(0); + int textlines(0); + for (;;){ // outer loop over all lines in the body if (cin.eof()) break; if (cin.bad()) return 1; @@ -351,19 +349,41 @@ int main(int _argc, const char** _argv){ } bigbuf.push_back(line); cout << line << endl; - if (line == "--" + boundary) { - inheads = 1; + if (in_subheads){ + if (line == "" || line == "\r") in_subheads = 0; + } + if (in_subheads){ + string sub_contype; + string junk; +// in principle could worry about folded headers, +// but in this application it doesn't actually matter + string headword; + string rest; + size_t where = line.find(":"); + if (where != string::npos) { + headword = line.substr(0, where); + rest = ltrim(line.substr(1+where)); + } + headword = toLower(headword); + if (headword == "content-type") { + parse_content(rest, sub_contype, junk); + currently_text = sub_contype == "text"; + cerr << "setting contype '" << sub_contype << "' " << currently_text << " ... " << textlines << endl; + } } else { - if (is_text) { - if (ltrim(line).length()) textlines++; + if (main_multipart && line == "--" + boundary) { + //xxxx cerr << "found subhead boundary" << endl; + in_subheads = 1; + continue; } + if (currently_text) textlines++; } } - if (0) cerr << "textlines: " << textlines << endl; - if (000 && !textlines) { + if (1) cerr << "textlines: " << textlines << endl; + if (1 && !textlines) { cerr << progid << " rejection: no text" << endl; - exeunt(ex_spam); +// exeunt(ex_spam); } cerr << progid << " normal completion" << endl; exit(ex_good); -- cgit v1.2.3