summaryrefslogtreecommitdiff
path: root/tools/libskrewt.c
diff options
context:
space:
mode:
authorJohn Denker <jsd@av8n.com>2012-11-22 18:21:17 -0800
committerJohn Denker <jsd@av8n.com>2012-11-22 18:21:17 -0800
commit33981cf1aba48b7589fbdc685eeaa8a52ece8c4f (patch)
tree6eac350efefc6c04191b2e53fb58e9f6b4f436c9 /tools/libskrewt.c
parent4dd94a839cbae8a45889b224945eeaa6fb93b578 (diff)
add hooks for redirecting main output
Diffstat (limited to 'tools/libskrewt.c')
-rw-r--r--tools/libskrewt.c95
1 files changed, 89 insertions, 6 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;
}