File: weeder.cc

package info (click to toggle)
mailfilter 0.8.6-2
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 3,280 kB
  • ctags: 2,501
  • sloc: cpp: 2,452; sh: 1,393; ansic: 1,259; lex: 570; yacc: 468; makefile: 167; perl: 72
file content (505 lines) | stat: -rw-r--r-- 13,960 bytes parent folder | download | duplicates (5)
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
// weeder.cc - source file for the mailfilter program
// Copyright (c) 2003 - 2009  Andreas Bauer <baueran@gmail.com>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
// USA.

#include <vector>
#include <string>
#include <iostream>
#include <cstring>
#include "weeder.hh"
#include "header.hh"
#include "preferences.hh"
#include "mailfilter.hh"
#include "feedback.hh"
#include "defines.hh"

using namespace std;

extern string int_to_string (int);

// The function takes a pointer to a Header class which contains a
// message header.  It then determines whether this header is spam, a
// duplicate, or "good" mail.  It returns 1 if the mail qualifies as
// spam, 0 if it should not be deleted, and a negative integer upon
// error.

int Weeder :: is_weed (Header* the_header)
{
  int status;

  status = check_duplicates (the_header);
  if (status == 1)
    return 1;             // Spam.
  else if (status < 0)
    return status;        // Error.

  status = check_allow_rules (the_header);
  if (status == 1)
    return 1;             // Spam.
  else if (status == 0)
    return 0;             // Friend.
  else if (status < 0)
    return status;        // Error.

  status = check_maxlength (the_header);
  if (status == 1)
    return 1;             // Spam.

  status = check_deny_rules (the_header);
  if (status == 1)
    return 1;             // Spam.
  else if (status < 0)
    return status;        // Error.

  status = check_scores (the_header);
  if (status == 1)
    return 1;             // Spam.

  return 0;               // Leave message alone.
}

// This function returns 1 if the message was considered being a
// duplicate, and 0 otherwise.  A negative integer is returned upon
// error.

int Weeder :: check_duplicates (Header* the_header)
{
  Feedback* logger = Feedback :: Instance ();

  if (!the_header)
    return GEN_FAILURE_FLAG;
  
  // If the mailer didn't attach a valid message-ID, give up scanning
  // for duplicated messages immediately.
  if (the_header->ID ()->length() <= 0)
    return 0;

  string cur_line;
  
  if (Preferences :: Instance ().delete_duplicates ())
    {
      for (vector<string> :: iterator cur_ID = msg_ids.begin ();
	   cur_ID != msg_ids.end ();
	   cur_ID++)
	if (*(the_header->ID ()) == *cur_ID)
	  {
	    logger->print_msg ("Deny: "
			       + *(the_header->from ()) + ": "
			       + *(the_header->subject ()) + ", "
			       + *(the_header->date ())
			       + " [Duplicate].",
			       2);
	    return 1;
	  }
      msg_ids.push_back (*(the_header->ID ()));
    }

  return 0;
}

// This function returns 1 if the message was considered being spam, 0
// if it is a friend.  A negative integer is returned upon error.

int Weeder :: check_allow_rules (Header* the_header) const
{
  Feedback* logger = Feedback :: Instance ();

  if (!the_header)
    return GEN_FAILURE_FLAG;
  
  string cur_line;
  
  for (vector<entry> :: iterator cur_entry = the_header->entries ()->begin ();
       cur_entry != the_header->entries ()->end ();
       cur_entry++)
    {
      cur_line = cur_entry->tag + ": " + cur_entry->body;
      for (vector<Filter> :: iterator cur_allow =
	     Preferences :: Instance ().allow_filters ()->begin ();
	   cur_allow != Preferences :: Instance ().allow_filters ()->end ();
	   cur_allow++)
	{
	  if (!cur_allow->is_negative ()
	      && regexec (cur_allow->comp_exp (),
			  cur_line.c_str (),
			  0, 
			  NULL,
			  0) 
	      == 0)
	    {
	      logger->print_msg ("Allow: "
				 + *(the_header->from ()) + ": "
				 + *(the_header->subject ()) + ", "
				 + *(the_header->date ())
				 + " [\"" 
				 + cur_allow->expression ()
				 + "\" matches \""
				 + cur_line + "\"].",
				 4);
	      
	      // OK, friendly message detected.  Now check, whether
	      // MAXSIZE_ALLOW applies.
	      if (Preferences :: Instance ().max_size_allow () > 0
		  && the_header->size () > Preferences :: Instance ().max_size_allow ())
		{
		  logger->print_msg ("Deny: "
				     + *(the_header->from ()) + ": "
				     + *(the_header->subject ()) + ", "
				     + *(the_header->date ())
				     + " [Maxsize_Allow exceeded].",
				     2);
		  return 1;
		}

	      return 0;
	    }
	}
    }

  // Evaluate the negative allow rules which works slightly different
  // to the above algorithm: instead of going through each line and
  // each filter, we go through each negative filter and then through
  // all lines of the header.
  if (Preferences :: Instance ().neg_allows () > 0)
    {
      for (vector<Filter> :: iterator cur_allow =
	     Preferences :: Instance ().allow_filters ()->begin ();
	   cur_allow != Preferences :: Instance ().allow_filters ()->end ();
	   cur_allow++)
	{
	  if (cur_allow->is_negative ())
	    {
	      for (vector<entry> :: iterator cur_entry =
		     the_header->entries ()->begin ();
		   cur_entry != the_header->entries ()->end ();
		   cur_entry++)
		{
		  cur_line = cur_entry->tag + ": " + cur_entry->body;
		  if (regexec (cur_allow->comp_exp (),
			       cur_line.c_str (),
			       0, 
			       NULL,
			       0) 
		      == 0)
		    break;
		  
		  if (cur_entry + 1 == the_header->entries ()->end ())
		    {
		      logger->print_msg ("Allow: "
					 + *(the_header->from ()) + ": "
					 + *(the_header->subject ()) + ", "
					 + *(the_header->date ())
					 + " [Negative allow rule \""
					 + cur_allow->expression ()
					 + "\" did not match].",
					 4);
		      return 0;
		    }
		}
	    }
	}
    }
  
  return 2;
}

int Weeder :: check_maxlength (Header* the_header) const
{
  Feedback* logger = Feedback :: Instance ();

  string cur_line;

  if (Preferences :: Instance ().maxlength () == 0)
    return 0;

  for (vector<entry> :: iterator cur_entry = the_header->entries ()->begin ();
       cur_entry != the_header->entries ()->end ();
       cur_entry++)
    {
      cur_line = cur_entry->tag + ": " + cur_entry->body;
      
      if (cur_line.length () > (unsigned int)Preferences :: Instance ().maxlength ())
	{
	  logger->print_msg ("Deny: "
			    + *(the_header->from ()) + ": "
			    + *(the_header->subject ()) + ", "
			    + *(the_header->date ())
			    + " [\""
			    + cur_entry->tag
			    + "\" exceeded maxlength].",
			    2);
	  return 1;
	}
    }
  
  return 0;
}

// This function returns 1 if the message was considered being spam, 0
// otherwise.  A negative integer is returned upon error.

int Weeder :: check_deny_rules (Header* the_header) const
{
  Feedback* logger = Feedback :: Instance ();

  if (!the_header)
    return GEN_FAILURE_FLAG;
  
  string cur_line;

  if (Preferences :: Instance ().max_size_deny () > 0
      && the_header->size () > Preferences :: Instance ().max_size_deny ())
    {
      logger->print_msg ("Deny: "
			 + *(the_header->from ()) + ": "
			 + *(the_header->subject ()) + ", "
			 + *(the_header->date ())
			 + " [Maxsize_Deny exceeded].",
			 2);
      return 1;
    }
  for (vector<entry> :: iterator cur_entry = the_header->entries ()->begin ();
       cur_entry != the_header->entries ()->end ();
       cur_entry++)
    {
      cur_line = cur_entry->tag + ": " + cur_entry->body;
      for (vector<Filter> :: iterator
	     cur_deny = Preferences :: Instance ().deny_filters ()->begin ();
	   cur_deny != Preferences :: Instance ().deny_filters ()->end ();
	   cur_deny++)
	{
	  if (!cur_deny->is_negative ()
	      && regexec (cur_deny->comp_exp (),
			  cur_line.c_str (),
			  0, 
			  NULL,
			  0) 
	      == 0)
	    {
	      logger->print_msg ("Deny: "
				+ *(the_header->from ()) + ": "
				+ *(the_header->subject ()) + ", "
				+ *(the_header->date ())
				+ " [\"" 
				+ cur_deny->expression () +
				+ "\" matches \""
				+ cur_line + "\"].",
				2);
	      return 1;
	    }
	  
	  // Check normalised subject, if necessary.
	  if (Preferences :: Instance ().normal ()
	      && !cur_deny->is_negative ()
	      && strcmp (cur_entry->tag.c_str (), "Subject") == 0
	      && regexec (cur_deny->comp_exp (),
			  (the_header->normal_subject ())->c_str (),
			  0, NULL, 0)
	      == 0)
	    {
	      logger->print_msg ("Deny: "
				 + *(the_header->from ()) + ": "
				 + *(the_header->subject ()) + ", "
				 + *(the_header->date ())
				 + " [\"" 
				 + cur_deny->expression () +
				 + "\" matches \""
				 + cur_line 
				 + "\" (normalised)].",
				 2);
	      return 1;
	    }
	}
    }
  
  // Evaluate the negative deny rules in a similar fashion as negative
  // allow rules (see above, for comments).
  if (Preferences :: Instance ().neg_denies () > 0)
    {
      for (vector<Filter> :: iterator cur_deny =
	     Preferences :: Instance ().deny_filters ()->begin ();
	   cur_deny != Preferences :: Instance ().deny_filters ()->end ();
	   cur_deny++)
	{
	  if (cur_deny->is_negative ())
	    {
	      for (vector<entry> :: iterator cur_entry =
		     the_header->entries ()->begin ();
		   cur_entry != the_header->entries ()->end ();
		   cur_entry++)
		{
		  cur_line = cur_entry->tag + ": " + cur_entry->body;
		  if (regexec (cur_deny->comp_exp (),
			       cur_line.c_str (),
			       0, 
			       NULL,
			       0) 
		      == 0)
		    break;
		  
		  // Check for normalised subject, if applicable.
		  if (Preferences :: Instance ().normal ()
		      && strcmp (cur_entry->tag.c_str (), "Subject") == 0
		      && regexec (cur_deny->comp_exp (),
				  (the_header->normal_subject ())->c_str (),
				  0, NULL, 0)
		      == 0)
		    break;
		  
		  if (cur_entry + 1 == the_header->entries ()->end ())
		    {
		      logger->print_msg ("Deny: "
					 + *(the_header->from ()) + ": "
					 + *(the_header->subject ()) + ", "
					 + *(the_header->date ())
					 + " [Negative deny rule \""
					 + cur_deny->expression ()
					 + "\" did not match].",
					 4);
		      return 1;
		    }
		}
	    }
	}
    }

  return 0;
}

// This function returns the score the message achieved.

int Weeder :: check_scores (Header* the_header) const
{
  Feedback* logger = Feedback :: Instance ();

  string cur_line;
  int msg_score = 0;

  // First test against the MAXSIZE_SCORE setting
  if (Preferences :: Instance ().max_size_score().score != 0
      && Preferences :: Instance ().max_size_score().size > 0
      && the_header->size() > Preferences :: Instance ().max_size_score().size)
    msg_score = Preferences :: Instance ().max_size_score().score;

  // Now check ordinary score rules.
  for (vector<entry> :: iterator cur_entry = the_header->entries ()->begin ();
       cur_entry != the_header->entries ()->end ();
       cur_entry++)
    {
      cur_line = cur_entry->tag + ": " + cur_entry->body;

      for (vector<Score> :: iterator cur_score = 
	     Preferences :: Instance ().score_filters ()->begin ();
	   cur_score != Preferences :: Instance ().score_filters ()->end ();
	   cur_score++)
	{
	  if (!cur_score->is_negative ()
	      // Check ordinary score rules...
	      && ((regexec (cur_score->comp_exp (),
			    cur_line.c_str (),
			    0, 
			    NULL,
			    0) == 0)
		  || // ...or, if that doesn't match, ...
		  // ...check normalised subject, if applicable.
		  (Preferences :: Instance ().normal ()
		   && strcmp (cur_entry->tag.c_str (), "Subject") == 0
		   && regexec (cur_score->comp_exp (),
			       (the_header->normal_subject ())->c_str (),
			       0, NULL, 0) == 0)))
	    {
	      msg_score += cur_score->score ();
	      logger->print_msg ("Score: \""
				 + cur_score->expression ()
				 + "\" matches \""
				 + cur_line
				 + "\" ["
				 + int_to_string (cur_score->score ())
				 + "].",
				 5);
	    }
	}
    }

  // Check negative scores now.
  for (vector<Score> :: iterator cur_score = 
	 Preferences :: Instance ().score_filters ()->begin ();
       cur_score != Preferences :: Instance ().score_filters ()->end ();
       cur_score++)
    {
      if (cur_score->is_negative ())
	{
	  for (vector<entry> :: iterator 
		 cur_entry = the_header->entries ()->begin ();
	       cur_entry != the_header->entries ()->end ();
	       cur_entry++)
	    {
	      cur_line = cur_entry->tag + ": " + cur_entry->body;
	      
	      if (regexec (cur_score->comp_exp (),
			   cur_line.c_str (),
			   0, 
			   NULL,
			   0) == 0)
		break;
	      
	      // Check normalised subject, if applicable.
	      if (Preferences :: Instance ().normal ()
		  && strcmp (cur_entry->tag.c_str (), "Subject") == 0
		  && regexec (cur_score->comp_exp (),
			      (the_header->normal_subject ())->c_str (),
			      0, NULL, 0) == 0)
		break;
	      
	      if (cur_entry + 1 == the_header->entries ()->end ())
		{
		  msg_score += cur_score->score ();
		  logger->print_msg ("Score: <> \""
				     + cur_score->expression ()
				     + "\" did not match "
				     + "["
				     +  int_to_string (cur_score->score ())
				     + "].",
				     5);
		}
	    }
	}
    }

  if (msg_score >= Preferences :: Instance ().highscore ())
    {
      logger->print_msg ("Deny: "
			 + *(the_header->from ()) + ": "
			 + *(the_header->subject ()) + ", "
			 + *(the_header->date ())
			 + " [Score: "
			 + int_to_string (msg_score)
			 + "].",
			 2);
      return 1;
    }
  
  logger->print_msg ("Pass: "
		     + *(the_header->from ()) + ": "
		     + *(the_header->subject ()) + ", "
		     + *(the_header->date ())
		     + " [Score: "
		     + int_to_string (msg_score)
		     + "].",
		     5);
  return 0;
}