Spam scanning investigation
[gnucomo.git] / src / spam / 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 subject("^Subject:");
61    regex returnpath("^Return-Path:");
62
63    openlog("gnucomo", 0, LOG_MAIL);
64
65
66    while (std::cin >> line)
67    {
68       //std::cout <<   "[" << state << "]   checking " << line << "\n";  // DEBUG
69       switch (state)
70       {
71       case 0:
72          if (line == fwd_header)
73          {
74             state = 1;
75          }
76          break;
77
78       case 1:
79          //  Inside the forwarded header
80          if (line == received || line == from || line == returnpath || line == subject)
81          {
82             header = line;
83             state = 2;
84          }
85          break;
86       case 2:
87          if (line == regex("^[^ ]+: "))
88          {
89             //std::cout << "Header: " << header << "\n";   // DEBUG
90             syslog(LOG_WARNING, "%s", (char *)header);
91
92             header = line;
93          }
94          else if (line == String(""))
95          {
96             state = 3;
97          }
98          else
99          {
100             header += " ";
101             header += line;
102          }
103       }
104    }
105
106    closelog();
107 }
108