From abc922f8a3f40b5f011cffe5cbe06e2c2df90d02 Mon Sep 17 00:00:00 2001 From: John Denker Date: Fri, 23 Nov 2012 02:05:12 -0800 Subject: major cleanup, now handles nested multipart mime --- tools/libskrewt.c | 74 +++++++++++++++++++++++++++++-------------------------- 1 file changed, 39 insertions(+), 35 deletions(-) (limited to 'tools/libskrewt.c') diff --git a/tools/libskrewt.c b/tools/libskrewt.c index dc131f9..595c3fe 100644 --- a/tools/libskrewt.c +++ b/tools/libskrewt.c @@ -6,8 +6,9 @@ using namespace std; void parse_content(const string type_spec_line, - string &maintype, string &boundary) { - cerr << "parser called with: " << type_spec_line << endl; + string &maintype, string &boundary, + const string old_bdy) { + if (0) cerr << "parser called with: " << type_spec_line << endl; string get_type(type_spec_line); size_t where = get_type.find_first_of(" \t;\n"); @@ -46,7 +47,7 @@ void parse_content(const string type_spec_line, boundary = boundary.substr(0, where); } } else { - //xxxxxxx cerr << "boundary= not found in " << type_spec_line << endl; + boundary = old_bdy; } } @@ -56,7 +57,7 @@ int skrewt::krunch_rfrom(){ string word; parse >> word; if (word != "from") { - cerr << progid << " bad 'Received: from' line ... '" + cerr << progid << " bad 'Received: from' line; no from, instead '" << word << "'" << endl; return ex_syserr; } @@ -136,8 +137,8 @@ xstr skrewt::getRecord(istream& xin){ << " folded header rejection: bigger than " << maxsize << endl; exeunt(ex_spam); } - bigbuf.push_back(line.str); - headrec += "\n" + noCR(line.str); + bigbuf.push_back(more.str); + headrec += "\n" + noCR(more.str); } return {0, headrec}; } @@ -202,7 +203,7 @@ int skrewt::headers(istream& xin){ return 0; } -int skrewt::dump_headers(std::ostream& xout){ +int skrewt::dump_bigbuf(std::ostream& xout){ for (vector::const_iterator foo = bigbuf.begin(); foo != bigbuf.end(); foo++){ xout << *foo << endl; @@ -394,11 +395,21 @@ public: string bdy; }; +void dump(const list sitch){ + int depth = sitch.size(); + cerr << " depth: " << depth; + if (depth){ + cerr << " tp: " << sitch.front().tp + << " bdy: " << sitch.front().bdy; + } + cerr << endl; +} + int skrewt::body(std::istream& xin, std::ostream& xout){ list sitch; if (content_type.length()) { string tp, bdy; - parse_content(content_type, tp, bdy); + parse_content(content_type, tp, bdy, ""); sitch.push_front({tp, bdy}); } else { // assume single-part, all text if not otherwise specified: @@ -409,10 +420,8 @@ int skrewt::body(std::istream& xin, std::ostream& xout){ // Now spew the rest of the message // Note that multipart messages can have sub-headers. - cerr << "body begins: tp: " << sitch.front().tp - << " bdy: " << sitch.front().bdy - << " depth: " << sitch.size() - << endl; + cerr << "body begins: "; + dump(sitch); int in_subheads(0); int textlines(0); @@ -420,29 +429,20 @@ int skrewt::body(std::istream& xin, std::ostream& xout){ for (;;){ // outer loop over all lines in the body if (xin.eof()) break; if (xin.bad()) return 1; - string line; -// on fail, go back to top of outer loop and check for eof versus bad - if (getline(xin, line).fail()) continue; - msgsize += line.length()+1; - if (msgsize > maxsize) { - cerr << progid << " rejection: bigger than " << maxsize << endl; - maybe_exeunt(ex_spam, error_exit); - } - bigbuf.push_back(line); - xout << line << endl; + xstr rec = getRecord(xin); + if (rec.err) break; + string line = rec.str; if (0) cerr << "+++" << line << endl; if (in_subheads){ if (line == "" || line == "\r") { in_subheads = 0; cerr << "+++ end of subhead" << endl; string tp, bdy; - parse_content(sub_conntype, tp, bdy); - cerr << tp << " '" << bdy << "'" << endl; + parse_content(sub_conntype, tp, bdy, sitch.front().bdy); sitch.push_front({tp, bdy}); - cerr << "payload mode: tp: " << sitch.front().tp - << " bdy: " << sitch.front().bdy - << " depth: " << sitch.size() - << endl; + + cerr << "payload mode: "; + dump(sitch); } } if (/* still */in_subheads){ @@ -463,20 +463,24 @@ int skrewt::body(std::istream& xin, std::ostream& xout){ sub_conntype = rest; } } else { - if (sitch.front().tp == "multipart" + if (sitch.front().bdy != "" && line == "--" + sitch.front().bdy) { - cerr << "boundary: begin subhead block" << endl; + if (sitch.front().tp != "multipart") + sitch.pop_front(); + cerr << "boundary: begin subhead block "; + dump(sitch); in_subheads = 1; sub_conntype = ""; continue; } - if (sitch.front().tp == "multipart" + if (sitch.front().bdy != "" && line == "--" + sitch.front().bdy + "--") { + string dead_bdy = sitch.front().bdy; sitch.pop_front(); - cerr << "found subhead termination --> " - << sitch.front().tp - << endl; - in_subheads = 1; + if (sitch.front().bdy == dead_bdy) sitch.pop_front(); + cerr << "boundary: termination "; + dump(sitch); + in_subheads = 0; continue; } if (sitch.front().tp == "text") textlines++; -- cgit v1.2.3