Prva verzija za buster.
[spamassassin-cn.git] / check_whitelist
1 #!/usr/bin/perl
2 #
3 # TODO: should this be made a top-level script, called "sa-awl"?
4
5 sub usage {
6   die "
7 usage: check_whitelist [--clean] [--min n] [dbfile]
8 ";
9 }
10
11 use strict;
12 use Fcntl;
13 use Getopt::Long;
14
15 use vars qw(
16                 $opt_clean $opt_min $opt_help
17         );
18
19 GetOptions(
20   'clean'               => \$opt_clean,
21   'min:i'               => \$opt_min,
22   'help'                => \$opt_help
23 ) or usage();
24 $opt_help and usage();
25
26 $opt_min ||= 2;
27
28 BEGIN { @AnyDBM_File::ISA = qw(DB_File GDBM_File NDBM_File SDBM_File); }
29 use AnyDBM_File ;
30
31 my $db;
32 if ($#ARGV == -1) {
33   $db = $ENV{HOME}."/.spamassassin/auto-whitelist";
34 } else {
35   $db = $ARGV[0];
36 }
37
38 my %h;
39 if ($opt_clean) {
40   tie %h, "AnyDBM_File",$db, O_RDWR,0600
41       or die "Cannot open r/w file $db: $!\n";
42 } else {
43   tie %h, "AnyDBM_File",$db, O_RDONLY,0600
44       or die "Cannot open file $db: $!\n";
45 }
46
47 my @k = grep(!/totscore$/,keys(%h));
48 for my $key (@k)
49 {
50   my $totscore = $h{"$key|totscore"};
51   my $count = $h{$key};
52   next unless defined($totscore);
53
54   if ($opt_clean) {
55     if ($count >= $opt_min) { next; }
56     print "cleaning: ";
57   }
58
59   printf "% 8.1f %15s  --  %s\n",
60                   $totscore/$count, (sprintf "(%.1f/%d)",$totscore,$count),
61                   $key;
62
63   if ($opt_clean) {
64     delete $h{"$key|totscore"};
65     delete $h{$key};
66   }
67 }
68 untie %h;
69
70 =head1 NAME
71
72 check_whitelist - examine and manipulate SpamAssassin's auto-whitelist db
73
74 =head1 SYNOPSIS
75
76 B<check_whitelist> [--clean] [--min n] [dbfile]
77
78 =head1 DESCRIPTION
79
80 Check or clean a SpamAssassin auto-whitelist (AWL) database file.
81
82 The name of the file is specified after any options, as C<dbfile>.
83 The default is C<$HOME/.spamassassin/auto-whitelist>.
84
85 =head1 OPTIONS
86
87 =over 4
88
89 =item --clean
90
91 Clean out infrequently-used AWL entries.  The C<--min> switch can be
92 used to select the threshold at which entries are kept or deleted.
93
94 =item --min n
95
96 Select the threshold at which entries are kept or deleted when C<--clean> is
97 used.  The default is C<2>, so entries that have only been seen once are
98 deleted.
99
100 =back
101
102 =head1 OUTPUT
103
104 The output looks like this:
105
106      AVG  (TOTSCORE/COUNT)  --  EMAIL|ip=IPBASE
107
108 For example:
109
110      0.0         (0.0/7)  --  dawson@example.com|ip=208.192
111     21.8        (43.7/2)  --  mcdaniel_2s2000@example.com|ip=200.106
112
113 C<AVG> is the average score;  C<TOTSCORE> is the total score of all mails seen
114 so far;  C<COUNT> is the number of messages seen from that sender;  C<EMAIL> is
115 the sender's email address, and C<IPBASE> is the B<AWL base IP address>.
116
117 B<AWL base IP address> is a way to identify the sender's IP address they
118 frequently send from, in an approximate way, but remaining hard for spammers to
119 spoof.  The algorithm is as follows:
120
121   - take the last Received header that contains a public IP address -- namely
122     one which is not in private, unrouted IP space.
123
124   - chop off the last two octets, assuming that the user may be in an ISP's
125     dynamic address pool.
126
127 =cut
128