00001
00037 #include <errno.h>
00038
00039 #include "SpamUtil.h"
00040 #include "Logger.h"
00041 #include "HeaderInfo.h"
00042
00043 void HeaderInfo::init()
00044 {
00045 mSubject[0] = '\0';
00046 mTo[0] = '\0';
00047 mFrom[0] = '\0';
00048 mFromNoColon[0] = '\0';
00049 mDate[0] = '\0';
00050 mReason[0] = '\0';
00051 }
00052
00053
00054
00059 void HeaderInfo::subject( const char *pBuf )
00060 {
00061 if (pBuf != 0) {
00062 strncpy(mSubject, pBuf, sizeof(mSubject) );
00063 if (mSubject[0] == '\0') {
00064 mSubject[0] = ' ';
00065 mSubject[1] = '\0';
00066 }
00067 else {
00068 SpamUtil().trimEnd(mSubject);
00069 }
00070 }
00071 }
00072
00073
00078 FILE *HeaderInfo::openTraceFile()
00079 {
00080 const char *traceFileName = "garbage_trace";
00081 const size_t MAX_FILE_SIZE = 50 * 1024;
00082 Logger log = pLogger->getLogger("HeaderInfo");
00083 char msgbuf[128];
00084 FILE *fp = 0;
00085 char *mode = "a";
00086 if ((fp = fopen(traceFileName, mode)) != 0) {
00087 int fileSize = SpamUtil().getFileSize( fp, traceFileName, msgbuf );
00088 if (fileSize >= 0) {
00089 if (fileSize > MAX_FILE_SIZE) {
00090 mode = "w";
00091 fclose(fp);
00092
00093 if ((fp = fopen(traceFileName, mode)) == 0) {
00094 char *err_reason = strerror( errno);
00095 sprintf( msgbuf, "could not re-open %s. Reason: %s",
00096 traceFileName, err_reason );
00097 log.log(Logger::ERROR, "openTraceFile", msgbuf);
00098 }
00099 }
00100 }
00101 else {
00102 log.log(Logger::ERROR, "openTraceFile", msgbuf);
00103 }
00104 }
00105 else {
00106 char *err_reason = strerror( errno);
00107 sprintf( msgbuf, "could not open %s. Reason: %s",
00108 traceFileName, err_reason );
00109 log.log(Logger::ERROR, "openTraceFile", msgbuf);
00110 }
00111
00112 return fp;
00113 }
00114
00115
00121 HeaderInfo::~HeaderInfo()
00122 {
00123 if (mDoTrace && klass() == MailFilter::GARBAGE) {
00124 FILE *fp = openTraceFile();
00125 if (fp != 0) {
00126 fprintf(fp, "\n");
00127 fprintf(fp, "From %s\n", SpamUtil().trimEnd(mFromNoColon));
00128 fprintf(fp, "From: %s\n", SpamUtil().trimEnd(mFrom));
00129 fprintf(fp, "To: %s\n", SpamUtil().trimEnd(mTo));
00130 fprintf(fp, "Subject: %s\n", SpamUtil().trimEnd(mSubject));
00131 fprintf(fp, "Date: %s\n", SpamUtil().trimEnd(mDate));
00132 fprintf(fp, "Reason-its-garbage: %s\n", SpamUtil().trimEnd(mReason));
00133 fclose(fp);
00134 }
00135 }
00136 }
00137
00138