2 # $Id: spamscan.in,v 1.8 2005/02/01 07:54:01 blarson Exp $
6 # Performs SpamAssassin checks on a message before allowing it through to
7 # the main incoming queue.
9 # Uses up: incoming/S<code><bugnum>.nn
10 # Temps: incoming/R.nn
11 # Creates: incoming/I.nn
14 $config_path = '/etc/debbugs';
15 $lib_path = '/usr/lib/debbugs';
17 require "$config_path/config";
18 require "$lib_path/errorlib";
19 $ENV{PATH} = $lib_path . ':' . $ENV{PATH};
21 exit unless $gSpamScan;
23 chdir $gSpoolDir or die "chdir spool: $!\n";
26 use Mail::SpamAssassin;
27 use Mail::SpamAssassin::NoMailAudit;
29 use lib '/usr/lib/debbugs';
30 use Mail::CrossAssassin;
35 &filelock('incoming-spamscan');
39 ca_init('\b\d{3,8}(?:-(?:close|done|forwarded|maintonly|submitter|quiet))?\@bugs\.debian\.org', '/org/bugs.debian.org/CrossAssassinDb');
43 my $user_prefs = "$ENV{HOME}/.spamassassin/user_prefs";
46 $user_prefs_time = (stat $user_prefs)[9];
49 my $spam = Mail::SpamAssassin->new({
51 site_rules_filename => $gSpamRulesDir,
52 userprefs_filename => $user_prefs,
53 local_tests_only => ($gSpamLocalTestsOnly || 0),
54 debug => ($ENV{DEBBUGS_SPAM_DEBUG} || 0),
55 check_mx_delay => 2, # bit of a hack until we have parallelization
57 $spam->compile_now(1); # use all user preferences
64 sub header_or_empty ($$) {
65 my ($mail, $hdr) = @_;
66 my $value = $mail->get_header($hdr);
75 if (-f 'spamscan-stop') {
76 print "spamscan-stop file created\n";
80 if ($user_prefs_time != (stat $user_prefs)[9]) {
81 # stop and wait to be re-invoked from cron
87 opendir DIR, 'incoming' or die "opendir incoming: $!";
88 while (defined($_ = readdir DIR)) {
89 push @ids, $1 if /^S(.*)/;
97 unless (rename "incoming/S$id", "incoming/R$id") {
99 die "$id already fudged once! $!\n";
105 print "[$nf] $id scanning ...\n" or die "print log: $!";
107 open MESSAGE, "< incoming/R$id" or die "open incoming/R$id: $!";
109 # Kludge to work around Received: then From_ weirdness in receive;
110 # remove when receive is fixed? We may continue to need it for
111 # reprocessing old messages.
112 $textarray[0] = <MESSAGE>;
113 if ($textarray[0] =~ /^Received:/) {
114 my $maybefrom = <MESSAGE>;
115 if ($maybefrom =~ /^From /) {
116 $textarray[1] = $textarray[0];
117 $textarray[0] = $maybefrom;
119 $textarray[1] = $maybefrom;
122 push @textarray, <MESSAGE>;
124 my $mail = Mail::SpamAssassin::NoMailAudit->new(data => \@textarray);
127 my $messageid = header_or_empty($mail, 'Message-Id');
128 print " From: ", header_or_empty($mail, 'From'), "\n";
129 print " Subject: ", header_or_empty($mail, 'Subject'), "\n";
130 print " Date: ", header_or_empty($mail, 'Date'), "\n";
131 print " Message-Id: $messageid\n";
132 my $ca_score = ca_set(ca_keys($mail->get_body));
133 if (exists $spamseen{$messageid}) {
134 $mail->accept($gSpamMailbox);
135 unlink "incoming/R$id" or warn "unlink incoming/R$id: $!";
136 print " spam $spamseen{$messageid} duplicate\n"
137 or die "printf log: $!";
139 my $status = $spam->check($mail);
140 $status->rewrite_mail();
142 if ($status->is_spam()) {
143 $mail->accept($gSpamMailbox);
144 unlink "incoming/R$id" or warn "unlink incoming/R$id: $!";
145 my $score = sprintf "%.1f/%.1f %d",
146 $status->get_hits(), $status->get_required_hits(), $ca_score;
147 print " spam $score\n" or die "print log: $!";
148 $spamseen{$messageid} = $score;
149 } elsif ($status->get_hits() > 0 && $ca_score >= 4) {
150 $mail->accept($gCrossMailbox);
151 unlink "incoming/R$id" or warn "unlink incoming/R$id: $!";
152 my $score = sprintf "%.1f/%.1f %d",
153 $status->get_hits(), $status->get_required_hits(), $ca_score;
154 printf " spam $score\n" or die "printf log: $!";
155 $spamseen{$messageid} = $score;
157 open OUT, "> incoming/I$id" or die "open incoming/I$id: $!";
158 my @headers = $mail->get_all_headers();
159 if ($headers[0] =~ /^From /) {
160 my $from = $headers[0];
161 $headers[0] = $headers[1];
164 print OUT join '', @headers or die "print incoming/I$id: $!";
166 print OUT "X-CrossAssassin-Score: $ca_score\n"
167 or die "print incoming/I$id: $!";
169 print OUT "\n" or die "print incoming/I$id: $!";
170 print OUT @{$mail->get_body()} or die "print incoming/I$id: $!";
171 close OUT or die "close incoming/I$id: $!";
172 unlink "incoming/R$id" or warn "unlink incoming/R$id: $!";
173 printf " ok %.1f/%.1f %d\n",
174 $status->get_hits(), $status->get_required_hits(), $ca_score
175 or die "printf log: $!";