better section detection
[tridge/junkcode.git] / spamstopper.c
1 /* a simple program to detect if an email arriving on standard input is SPAM */
2
3 #include <stdio.h>
4 #include <string.h>
5 #include <fcntl.h>
6
7
8 /* a single occurance of any of these strings gets it banned */
9 static char *spam_strings[] = {
10 "Extractor Pro",
11 "HUGE PROFITS",
12 "removed from this advertiser",
13 "Adult Entertainment",
14 "PAY PLAN",
15 "FREE REPORT",
16 "RED HOT",
17 "$$$",
18 "MoneyMaker",
19 "cash register",
20 "build your business",
21 "having sex",
22 "home video",
23 "fraction of the price",
24 "ALL PRICES",
25 "Game List",
26 "electron28",
27 " rape ",
28 " sex ",
29 "secrets of success",
30 "attractive woman",
31 "love life",
32 "beautiful women",
33 "INTRODUCTORY OFFER",
34 "ORDER IT NOW",
35 "CREDIT CARD",
36 "Money Making",
37 "FREE TAPE",
38 "Network Marketing",
39 "HOME BUSINESS",
40 "Cold Cash",
41 "Adult Entertainment",
42 "COMMISSIONS",
43 "TOP PRICE",
44 "DON'T BELIEVE THEM",
45 "BELIEVE ME",
46 "HERE'S THE PROOF",
47 "SIMPLE DETAILS",
48 "DOLLAR BILL",
49 "MAKE MONEY",
50 NULL};
51
52
53
54 /* SPAM_WORDS_LIMIT occurances of any of these strings gets it banned */
55 #define SPAM_WORDS_LIMIT 2
56 static char *spam_words[] = {
57   "profit",
58   "Profit",
59   "PROFIT",
60
61   "cash",
62   "Cash",
63   "CASH",
64
65   "opportunity",
66   "Opportunity",
67   "OPPORTUNITY",
68
69   "money",
70   "Money",
71   "MONEY",
72
73   "Coming soon",
74
75   "business",
76
77   "video",
78
79   "marketing",
80
81   NULL};
82
83
84 static int match_spam_strings(char *buf, int size)
85 {
86   int i;
87
88   for (i=0;spam_strings[i];i++) {
89     if (memmem(buf, size, spam_strings[i], strlen(spam_strings[i])))
90       return 1;
91   }
92
93   return 0;
94 }
95
96
97 static int match_spam_words(char *buf, int size)
98 {
99   int i;
100   int count=0;
101
102   for (i=0;spam_words[i];i++) {
103     if (memmem(buf, size, spam_words[i], strlen(spam_words[i]))) {
104       count++;
105     }
106   }
107
108   if (count >= SPAM_WORDS_LIMIT)
109     return 1;
110
111   return 0;
112 }
113
114
115 /* messages longer than this get truncated */
116 #define MAXBUF 0xFFFF
117
118 int main(int argc, char *argv[])
119 {
120   char *buf;
121   int size;
122
123   if (argc != 2) {
124     fprintf(stderr,"spamstopper <spamfile>\n");
125     exit(1);
126   }
127
128   buf = (char *)malloc(MAXBUF);
129   if (!buf) return 0;
130
131   size = 0;
132   while (1) {
133     int n = read(0, buf+size, MAXBUF-(size+1));
134     if (n <= 0) break;
135     size += n;
136   }
137
138   if (size <= 0) return 0;
139
140   buf[size] = 0;
141
142   if (match_spam_strings(buf, size) ||
143       match_spam_words(buf, size)) {
144     char *spamfile = argv[1];
145     int fd;
146
147     fd = open(spamfile,O_CREAT|O_WRONLY|O_APPEND, 0666);
148     if (fd == -1) {
149       perror(spamfile);
150       exit(1);
151     }
152     write(fd, buf, size);
153     close(fd);
154     return 0;
155   }
156
157   /* its OK, pass it on */
158   write(1,buf,size);
159
160   return 0;
161 }
162
163