New program: spamdetect.
[gnucomo.git] / src / gcm_input / spamdetect.cpp
1 /**************************************************************************
2 **  (c) Copyright 2007, Andromeda Technology & Automation
3 ** This is free software; you can redistribute it and/or modify it under the
4 ** terms of the GNU General Public License, see the file COPYING.
5 ***************************************************************************
6 ** MODULE INFORMATION *
7 ***********************
8 **      FILE NAME      : spamdetect.cpp
9 **      SYSTEM NAME    : Gnucomo - Gnu Computer Monitoring
10 **      VERSION NUMBER : $Revision: 1.1 $
11 **
12 **  DESCRIPTION      :  
13 **
14 **  EXPORTED OBJECTS : 
15 **  LOCAL    OBJECTS : 
16 **  MODULES  USED    :
17 ***************************************************************************
18 **  ADMINISTRATIVE INFORMATION *
19 ********************************
20 **      ORIGINAL AUTHOR : Arjen Baart - arjen@andromeda.nl
21 **      CREATION DATE   : Nov 14, 2007
22 **      LAST UPDATE     : Nov Nov 14, 2007
23 **      MODIFICATIONS   : 
24 **************************************************************************/
25
26 /*****************************
27    $Log: spamdetect.cpp,v $
28    Revision 1.1  2007-11-14 16:20:05  arjen
29    New program: spamdetect.
30    Expirimental utility to log manually reported spam and have
31    Gnucomo detect the spammer's IP address.
32
33 *****************************/
34
35 static const char *RCSID = "$Id: spamdetect.cpp,v 1.1 2007-11-14 16:20:05 arjen Exp $";
36
37 #include <fstream>
38 #include <AXE/String.h>
39
40 #include <syslog.h>
41 #include <getopt.h>
42
43 int main(int argc, char *argv[])
44 {
45    const char *usage = "Usage: spamdetect\n";
46
47    String line;
48    String header;
49    int    state = 0;
50
51    regex fwd_header("---- Original Message -----");
52    regex received("^Received:");
53    regex from("^From:");
54    regex returnpath("^Return-Path:");
55
56    openlog("gnucomo", 0, LOG_MAIL);
57
58
59    while (std::cin >> line)
60    {
61       std::cout << line << "\n";
62       switch (state)
63       {
64       case 0:
65          if (line == fwd_header)
66          {
67             state = 1;
68             std::cout << "Forward header detected.\n";
69          }
70          break;
71
72       case 1:
73          //  Inside the forwarded header
74          if (line == received || line == from || line == returnpath)
75          {
76             header = line;
77             state = 2;
78             std::cout << "(1)Header = " << header << "\n";
79          }
80          break;
81       case 2:
82          if (line == regex("^[^ ]+: "))
83          {
84             std::cout << "Logging " << header << "\n";
85             syslog(LOG_WARNING, "%s", (char *)header);
86             header = "";
87             state = 1;
88             std::cout << "Next header.\n";
89             if (line == received || line == from || line == returnpath)
90             {
91                header = line;
92                state = 2;
93                std::cout << "(1)Header = " << header << "\n";
94             }
95          }
96          else if (line == String(""))
97          {
98             std::cout << "End of headers detected.\n";
99             state = 3;
100          }
101          else
102          {
103             header += " ";
104             header += line;
105             std::cout << "(2)Header = " << header << "\n";
106          }
107       }
108    }
109
110    closelog();
111 }
112