summaryrefslogtreecommitdiff
path: root/tools/mail-scan.c
blob: 104768356741aa4d9b146082dd80ea700cfa5e1b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
///////////////////
// mail-scan.c

#include <iostream>
#include <stdlib.h>             /* for exit() */
#include <string>
#include <list>
#include <ctype.h>              /* toupper */
#include <signal.h>
#include <fstream>

#include <sys/types.h>          /*  for stat() */
#include <sys/stat.h>           /* stat() */

#include <stdio.h>              /* perror */
#include <boost/regex.hpp>
#include "utils.h"

using namespace std;

void usage(const int sts){
  (sts ? cerr : cout) <<
"Usage: mail-scan [options] filename [more filenames]\n"
"\n"
"  Options\n"
"    -help              print this msg (and exit immediately).\n"
"    -vert              invert:  print only if *no* match.\n"
"    -l                 print filename only, not matching text.\n"
"    -group             print a blank line after every match.\n"
"    -max nn            print at most nn records per file.\n"
"    -multi             print multi-line records on multiple lines\n"
"                       (as opposed to smashing them all onto one long line\n"
"    -addr              assume field contains somebody <foo@bar.com>;  print just foo@bar.com\n"
"\n"
"\n"
" Hint:\n"
"     mail-scan +from * | iconv -c ...\n"
"  | sed 's/.*@//;s/>$//' | sort | uniq -c | sort -nr > some-junk.from-count\n"
"\n"
" Also:\n"
"   grep score=[34] /home/user/Maildir/new/* -l | \\\n"
"        xargs mail-scan +From | blacklist-update\n"
" Then:\n"
"   grep score=[34] /home/user/Maildir/new/* -l | \\\n"
"        xargs mv-to -i /home/user/Maildir/spam/\n"

" Another hint:  using the '-addr' feature:\n"
"    mail-scan +x-spam.*:score=[234] /home/jean/Maildir/spam/* -l | \\\n"
"      xargs mail-scan +from -addr | sort | uniq -c | sort -nr | head -20\n"
;
  exit(sts);
}

// exit codes, compatible with spamassassin (not with qmail-queue)
const int sa_good(0);
const int sa_spam(1);
const int sa_usage(64);

void exeunt(const int sts){
  if (sts == sa_good) exit(sts);

  const char* foo = getenv("HI_Q_GROUP");
  if (!foo) exit(sts);

// No point in signalling ourself:
  sighandler_t rslt = signal(SIGUSR1, SIG_IGN);
  if (rslt == SIG_ERR) {
    cerr << "error setting signal" << endl;
  }
  int k = kill(-atoi(foo), SIGUSR1);
  if (k) {
    cerr << "kill failed on group " << atoi(foo) << " ... ";
    perror(0);
  }
  exit(sts);
}

class watcher {
public:
  string key;
  string val;
  boost::regex valrx;
  boost::regex keyrx;

  watcher(const string init)
  : key(""), val("")
  {
    size_t where = init.find(":");
    if (where != string::npos) {
      key = init.substr(0, where);
      val = init.substr(1+where);
    } else {
      key = init;
    }
    keyrx = boost::regex(key, boost::regex_constants::icase);
    valrx = boost::regex(val, boost::regex_constants::icase);
    //xx cerr << "watcher key: " << key << "  val: " << val << endl;
  }
};


////////////////////////////////////////////////////////////
int main(int _argc, const char** _argv){
////  pid_t pid = getpid();
////  cout << pid << endl;
////  cout << getpgid(pid) << endl;
  int argc(_argc);
  const char **argv(_argv);
  string progname(*argv); argv++; argc--;

  int maxsize(1000000);
  list<watcher> watchword;
  list<string> dofile;
  int vflag(0);
  int group_flag(0);
  int multi(0);
  int maxlines(0);
  int fname_only(0);
  int addr_mode(0);
  boost::regex host_filter(string("<.*@(.*)>"), boost::regex_constants::icase);
  boost::regex addr_filter(string("<(.*@.*)>"), boost::regex_constants::icase);

  while (argc) {
    string arg(*argv); argv++; argc--;
    if (arg.substr(0,2) == "--") arg = arg.substr(1);
    if (prefix(arg, "-help")) {
      usage(0);
    }
    if (prefix(arg, "-vert" /* short */)){
      vflag++;
      continue;
    } if (prefix(arg, "-group" /* short */)){
      group_flag++;
      continue;
    } else if (prefix(arg, "-l" /* short */)){
      fname_only++;
      continue;
    } else if (prefix(arg, "-address" /* long */)){
      addr_mode++;
      continue;
    } else if (prefix(arg, "-max" /* long */)){
      if (!argc){
        cerr << "Option '" << arg << "' requires an argument" << endl;
        cerr << "For help, try:  " << progname << " -help" << endl;
        exit(sa_usage);
      }
      maxlines = atoi(*argv++);  argc--;
      continue;
    } else if (prefix(arg, "-multi" /* long */)){
      multi++;
      continue;
    } else if (arg.substr(0,1) == "-") {
      cerr << "Unrecognized option '" << arg << "'" << endl;
      cerr << "For help, try:  " << progname << " -help" << endl;
      exit(sa_usage);
    }
    if (arg.substr(0,1) == "+") {
      watchword.push_back(arg.substr(1));
    } else {
      dofile.push_back(arg);
    }
  }

// loop over all files
  for (list<string>::const_iterator file = dofile.begin();
       file != dofile.end(); file++) {
    int didprint(0);
    struct stat filestatus;
    stat(file->c_str(), &filestatus );
    if (S_ISDIR(filestatus.st_mode)) {
      cerr << "is directory: " << *file << endl;
      continue;
    }

    ifstream infile;
    infile.open(file->c_str());
    if (!infile.good()) {
      cerr << "Failed to open file: " << *file << endl;
      // missing file is non-fatal;  go on to next file
      continue;
    }
    int inheads(1);
    string boundary("x-xx-x");
    int msgsize(0);
    int foundsome_infile(0);
    for (;;){           // loop over all records in this file
      if (inheads) {
        list<string> Header;
        string line;
        for (;;) {      // loop over all lines in this record
          if (infile.eof()) break;
          if (infile.bad()) {
            cerr << "mail-scan: read error on file '" << *file << "'" << endl;
            return 1;
          }
          if (getline(infile, line).fail()) continue;
          line = noCR(line);
          Header.push_back(line);
          msgsize += line.length()+1;
          if (msgsize > maxsize) {
            cerr << "skrewt rejection: bigger than " << maxsize << endl;
            exeunt(sa_spam);
          }
          char ch;
          if (infile.get(ch).fail()) continue;
          infile.putback(ch);
          if (ch != ' ' && ch != '\t') break;
        }
        if (Header.front().length() == 0) {
          inheads = 0;
          continue;     // blank line needs no further processing
        }
        string headword;      // the first thing on the line, e.g. "Subject"
        string rest;
        string header;
        if (!multi) header = join(" ", Header);
        else        header = join("\n", Header);
        size_t where = header.find(":");
        if (where != string::npos) {
          headword = header.substr(0, where);
          rest = ltrim(header.substr(1+where));
        }

        for (list<watcher>::const_iterator ptr = watchword.begin();
          ptr != watchword.end(); ptr++) {
// regex_match not regex_search ... keyrx must match *whole* headword
          if (boost::regex_match(headword, ptr->keyrx)){
            // here if match as to keyword;  check for match as to value
            if (ptr->val.length()==0
               || boost::regex_search(rest, ptr->valrx)){
              foundsome_infile++;
              if (!vflag) {
                if (!addr_mode){
                  // << foundsome_infile  << " " ;  (number of occurrences)
                  cout << *file;
                  if (!fname_only) {
                     cout << " :: " << header;
                  }
                  cout << endl;
                  didprint++;
                  if (maxlines && didprint >= maxlines) goto endfile;
                } else /* addr_mode */{
                   boost::smatch matches;
                   if (boost::regex_search(header, matches, addr_filter)){
                     cout << string(matches[1].first, matches[1].second) << endl;
                   } else {
                     string hdr(header);
                     size_t where = hdr.find(':');
                     if (where != string::npos) {
                       hdr = ltrim(hdr.substr(1+where));
                     } else {
                     // DUBIOUS: print the whole thing
                       cerr << "Warning, expected a ':', didn't see it"
                            << endl;
                     }
                     cout << hdr << endl;
                   }
                  didprint++;
                  if (maxlines && didprint >= maxlines) goto endfile;
                }
              }
            }
          }
        }
// only show file once, even if there might have been multiple matches:
        if (fname_only && didprint) break;
      } else {
        // not in header
        break;
      }
    }   // end loop over matching records in this file
endfile:;;;;

    if (vflag && !foundsome_infile) {
      cout << *file << endl;
      didprint++;
    }
    if (group_flag && didprint) cout << endl;
  }
}