summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Denker <jsd@av8n.com>2012-11-23 02:21:17 (GMT)
committerJohn Denker <jsd@av8n.com>2012-11-23 02:21:17 (GMT)
commit33981cf1aba48b7589fbdc685eeaa8a52ece8c4f (patch)
tree6eac350efefc6c04191b2e53fb58e9f6b4f436c9
parent4dd94a839cbae8a45889b224945eeaa6fb93b578 (diff)
add hooks for redirecting main output
-rw-r--r--tools/libskrewt.c95
-rw-r--r--tools/libskrewt.h5
-rw-r--r--tools/utils.h3
-rw-r--r--tools/ward.c112
4 files changed, 107 insertions, 108 deletions
diff --git a/tools/libskrewt.c b/tools/libskrewt.c
index 812b546..fb1cd4a 100644
--- a/tools/libskrewt.c
+++ b/tools/libskrewt.c
@@ -1,6 +1,5 @@
#include "libskrewt.h"
#include "utils.h"
-#include <iostream>
#include <sstream>
#include <signal.h>
@@ -86,6 +85,90 @@ int skrewt::krunch_rfrom(){
return 0;
}
+int skrewt::headers(istream& xin, std::ostream& xout){
+ //xxxx cerr << progid << " begins" << endl;
+ for (;;){ // outer loop over all records in the header
+ 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;
+ exeunt(ex_spam);
+ }
+ xout << line << endl;
+ bigbuf.push_back(line);
+ string headrec = noCR(line); // for a folded record, this is the first line
+
+ for (;;) { // inner loop to build a multi-line record e.g. folded record:
+ if (xin.eof()) break;
+ if (xin.bad()) return 1;
+ char ch;
+ if (xin.get(ch).fail()) continue;
+ xin.putback(ch);
+ if (ch != ' ' && ch != '\t') break;
+ string line;
+// on fail, go back to top of inner 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;
+ exeunt(ex_spam);
+ }
+ xout << line << endl;
+ bigbuf.push_back(line);
+ headrec += "\n" + noCR(line);
+ }
+// here with a fully assembled header record
+// headrec (unlike line) contains no DOS CR characters
+ int len = headrec.length();
+ if (len == 0) {
+ saw_blank_line = 1;
+ break; // no more headers in this message
+ }
+
+// here if it's a header line
+ string headword;
+ string rest;
+ size_t where = headrec.find(":");
+ if (where != string::npos) {
+ headword = headrec.substr(0, where);
+ rest = ltrim(headrec.substr(1+where));
+ }
+ headword = toLower(headword);
+ if (0){
+ } else if (headword == "from") {
+ from = rest;
+ } else if (headword == "to") {
+ to = rest;
+ } else if (headword == "return-path") {
+ return_path = rest;
+ } else if (headword == "message-id") {
+ message_id = rest;
+ } else if (headword == "received") {
+ if (!received_from.length() && prefix("from ", rest)){
+ received_from = rest;
+ }
+ } else if (headword == "date") {
+ date = rest;
+ } else if (headword == "subject") {
+ subject = rest;
+ } else if (headword == "content-type") {
+ content_type = rest;
+ } else if (headword == "delivered-to") {
+ delivered_to = rest;
+ }
+ //xxxx xout << headrec.length() << " ... ";
+ recno++;
+ if (0) if (recno <= 6) cerr << progid << "#" << recno
+ << " " << headrec << endl;
+ }
+ return 0;
+}
+
int skrewt::interstage(){
if (saw_blank_line) {/* ignore */}
// Note that the headers are in reverse-chronological order:
@@ -254,7 +337,7 @@ int skrewt::interstage(){
return 0;
}
-int skrewt::body(){
+int skrewt::body(std::istream& xin, std::ostream& xout){
string main_contype;
if (content_type.length())
parse_content(content_type, main_contype, boundary);
@@ -269,18 +352,18 @@ int skrewt::body(){
int textlines(0);
for (;;){ // outer loop over all lines in the body
- if (cin.eof()) break;
- if (cin.bad()) return 1;
+ 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(cin, line).fail()) continue;
+ 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);
- cout << line << endl;
+ xout << line << endl;
if (in_subheads){
if (line == "" || line == "\r") in_subheads = 0;
}
diff --git a/tools/libskrewt.h b/tools/libskrewt.h
index c5fea7e..8b0045f 100644
--- a/tools/libskrewt.h
+++ b/tools/libskrewt.h
@@ -1,5 +1,6 @@
#include <string>
#include <vector>
+#include <iostream>
#include "sepofra.h"
#include "qq_exit_codes.h" // a bit of a kludge
extern std::string progid;
@@ -35,9 +36,9 @@ public:
maxsize(1000*1000), error_exit(0), mid_required(0)
{}
- int headers();
+ int headers(std::istream& xin, std::ostream& xout);
int interstage();
- int body();
+ int body(std::istream& xin, std::ostream& xout);
int krunch_rfrom();
};
diff --git a/tools/utils.h b/tools/utils.h
index 1e6ac9d..0ef0fca 100644
--- a/tools/utils.h
+++ b/tools/utils.h
@@ -67,8 +67,9 @@ public:
argv.pop_front();
return rslt;
}
- void next(){
+ std::string next(){
current_arg = shift();
+ return current_arg;
}
size_t size() const {
return argv.size();
diff --git a/tools/ward.c b/tools/ward.c
index 277fd76..19d9d02 100644
--- a/tools/ward.c
+++ b/tools/ward.c
@@ -71,91 +71,6 @@ Received: from ip68-231-191-153.tc.ph.cox.net (HELO asclepias.av8n.net) (smtp@68
/home/jsd/Maildir/cur/1342363199.24320.cloud:2,
#endif
-int skrewt::headers(){
- //xxxx cerr << progid << " begins" << endl;
- for (;;){ // outer loop over all records in the header
- if (cin.eof()) break;
- if (cin.bad()) return 1;
-
- string line;
-// on fail, go back to top of outer loop and check for eof versus bad
- if (getline(cin, line).fail()) continue;
- msgsize += line.length()+1;
- if (msgsize > maxsize) {
- cerr << progid << " rejection: bigger than " << maxsize << endl;
- exeunt(ex_spam);
- }
- cout << line << endl;
- bigbuf.push_back(line);
- string headrec = noCR(line); // for a folded record, this is the first line
-
- for (;;) { // inner loop to build a multi-line record e.g. folded record:
- if (cin.eof()) break;
- if (cin.bad()) return 1;
- char ch;
- if (cin.get(ch).fail()) continue;
- cin.putback(ch);
- if (ch != ' ' && ch != '\t') break;
- string line;
-// on fail, go back to top of inner loop and check for eof versus bad
- if (getline(cin, line).fail()) continue;
- msgsize += line.length()+1;
- if (msgsize > maxsize) {
- cerr << progid << " rejection: bigger than " << maxsize << endl;
- exeunt(ex_spam);
- }
- cout << line << endl;
- bigbuf.push_back(line);
- headrec += "\n" + noCR(line);
- }
-// here with a fully assembled header record
-// headrec (unlike line) contains no DOS CR characters
- int len = headrec.length();
- if (len == 0) {
- saw_blank_line = 1;
- break; // no more headers in this message
- }
-
-// here if it's a header line
- string headword;
- string rest;
- size_t where = headrec.find(":");
- if (where != string::npos) {
- headword = headrec.substr(0, where);
- rest = ltrim(headrec.substr(1+where));
- }
- headword = toLower(headword);
- if (0){
- } else if (headword == "from") {
- from = rest;
- } else if (headword == "to") {
- to = rest;
- } else if (headword == "return-path") {
- return_path = rest;
- } else if (headword == "message-id") {
- message_id = rest;
- } else if (headword == "received") {
- if (!received_from.length() && prefix("from ", rest)){
- received_from = rest;
- }
- } else if (headword == "date") {
- date = rest;
- } else if (headword == "subject") {
- subject = rest;
- } else if (headword == "content-type") {
- content_type = rest;
- } else if (headword == "delivered-to") {
- delivered_to = rest;
- }
- //xxxx cout << headrec.length() << " ... ";
- recno++;
- if (0) if (recno <= 6) cerr << progid << "#" << recno
- << " " << headrec << endl;
- }
- return 0;
-}
-
-
////////////////////////////////////////////////////////////
int main(int _argc, const char** _argv){
@@ -171,23 +86,20 @@ int main(int _argc, const char** _argv){
skrewt mysk;
- while (argc) {
- string arg(*argv); argv++; argc--;
+ argParser ARGS(argc, argv);
+ try {while (ARGS.size()) {
+ string arg = ARGS.next();
if (arg.substr(0,2) == "--") arg = arg.substr(1);
- if (prefix(arg, "-help")) {
+ if (ARGS.prefix("-help")) {
usage(0);
}
if (0) {
- } else if (prefix(arg, "-mid-required")) {
+ } else if (ARGS.prefix("-mid-required")) {
mysk.mid_required++;
- } else if (prefix(arg, "-error-exit")) {
+ } else if (ARGS.prefix("-error-exit")) {
mysk.error_exit++;
- } else if (prefix(arg, "-maxsize")) {
- if (!argc) {
- cerr << "Option -maxsize requires an argument" << endl;
- exit(ex_usage);
- }
- mysk.maxsize = atoi(*argv); argv++; argc--;
+ } else if (ARGS.prefix("-maxsize", 1)) {
+ mysk.maxsize = atoi(ARGS.shift().c_str());
} else if (arg.substr(0,1) == "-") {
cerr << "Unrecognized option '" << arg << "'" << endl;
cerr << "For help, try: " << progname << " -help" << endl;
@@ -197,9 +109,12 @@ int main(int _argc, const char** _argv){
cerr << "For help, try: " << progname << " -help" << endl;
exit(ex_usage);
}
+ }}
+ catch (int) {
+ exit(ex_usage);
}
- int rslt = mysk.headers();
+ int rslt = mysk.headers(cin, cout);
if (rslt) return rslt;
// Headers are done.
@@ -208,7 +123,6 @@ int main(int _argc, const char** _argv){
rslt = mysk.interstage();
if (rslt) return rslt;
- rslt = mysk.body();
+ rslt = mysk.body(cin, cout);
return rslt;
-
}