Use ACL instead of AXE for utilities
[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.2 $
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.2  2007-11-21 15:14:26  arjen
29    Removed debug output.
30
31    Revision 1.1  2007/11/14 16:20:05  arjen
32    New program: spamdetect.
33    Expirimental utility to log manually reported spam and have
34    Gnucomo detect the spammer's IP address.
35
36 *****************************/
37
38 static const char *RCSID = "$Id: spamdetect.cpp,v 1.2 2007-11-21 15:14:26 arjen Exp $";
39
40 #include <fstream>
41 #include <String.h>
42
43 #include <syslog.h>
44 #include <getopt.h>
45
46 int main(int argc, char *argv[])
47 {
48    const char *usage = "Usage: spamdetect\n";
49
50    String line;
51    String header;
52    int    state = 0;
53
54    //  From here, the original spam starts. Something like 
55    //  -------- Forwarded Message --------  or  -------- Original Message -------- 
56
57    regex fwd_header("---- .+ Message -----");
58    regex received("^Received:");
59    regex from("^From:");
60    regex returnpath("^Return-Path:");
61
62    openlog("gnucomo", 0, LOG_MAIL);
63
64
65    while (std::cin >> line)
66    {
67       switch (state)
68       {
69       case 0:
70          if (line == fwd_header)
71          {
72             state = 1;
73          }
74          break;
75
76       case 1:
77          //  Inside the forwarded header
78          if (line == received || line == from || line == returnpath)
79          {
80             header = line;
81             state = 2;
82          }
83          break;
84       case 2:
85          if (line == regex("^[^ ]+: "))
86          {
87             syslog(LOG_WARNING, "%s", (char *)header);
88             header = "";
89             state = 1;
90             if (line == received || line == from || line == returnpath)
91             {
92                header = line;
93                state = 2;
94             }
95          }
96          else if (line == String(""))
97          {
98             state = 3;
99          }
100          else
101          {
102             header += " ";
103             header += line;
104          }
105       }
106    }
107
108    closelog();
109 }
110