]> git.donarmstrong.com Git - debbugs.git/blob - scripts/spamscan-sa
wait longer for locks
[debbugs.git] / scripts / spamscan-sa
1 #! /usr/bin/perl
2 # spamassassin handling split from spamscan
3 #
4
5 # unfortunatly we can't use strict;
6
7 use warnings;
8 use strict;
9
10 use lib qw(/usr/lib/debbugs);
11
12 use Mail::CrossAssassin;
13 use Mail::SpamAssassin;
14
15 use Debbugs::Config qw(:config);
16 # New versions of debbugs will not allow use in /etc/debbugs/config
17 use POSIX qw(strftime);
18 my $spam_mailbox = strftime($config{spam_mailbox},gmtime);
19 my $cross_mailbox = strftime($config{spam_crossassassin_mailbox},gmtime);
20
21 umask 002;
22 $| = 1;
23 STDOUT->autoflush(1);
24
25 sub header_or_empty ($$) {
26     my ($mail, $hdr) = @_;
27     my $value = $mail->get_header($hdr);
28     if (defined $value) {
29         chomp $value;
30         # replace newlines with '\n'
31         $value =~ s/\n/\\n/g;
32         return $value;
33     }
34     return '';
35 }
36
37 my $spam = Mail::SpamAssassin->new({
38         dont_copy_prefs => 1,
39         site_rules_filename => $config{spam_rules_dir},
40         userprefs_filename => $config{spam_user_prefs},
41                 local_tests_only => ($config{spam_local_tests_only} || 0),
42                 debug => ($ENV{DEBBUGS_SPAM_DEBUG} || 0),
43 });
44 # wait longer for a lock
45 $spam->init_learner({wait_for_lock => 1});
46
47 $spam->compile_now(1); # use all user preferences
48
49 while (my $id = <STDIN>) {
50     chomp $id;
51     my $nf = <STDIN>;
52     if (not defined $nf) {
53          die "Could not read nf: $!";
54     }
55     chomp $nf;
56     unless (rename "incoming/S$id", "incoming/R$id") {
57         die "Could not rename incoming/S$id: $!";
58     }
59     my $out = "[$nf] $id scanning ...\n";
60     open MESSAGE, "< incoming/R$id" or die "open incoming/R$id: $!";
61     # Kludge to work around Received: then From_ weirdness in receive;
62     # remove when receive is fixed? We may continue to need it for
63     # reprocessing old messages.
64     my @textarray = <MESSAGE>;
65     if ($textarray[0] !~ /^From /) {
66         ($textarray[0], $textarray[1]) = ($textarray[1], $textarray[0]);
67     }
68     close MESSAGE;
69     my $mail = $spam->parse(\@textarray);
70     
71     my $messageid = header_or_empty($mail, 'Message-Id');
72     $out .= "  From: " . header_or_empty($mail, 'From') . "\n";
73     $out .= "  Subject: ". header_or_empty($mail, 'Subject') . "\n";
74     $out .= "  Date: " . header_or_empty($mail, 'Date') . "\n";
75     $out .= "  Message-Id: $messageid\n";
76     my $keys = ca_keys($mail->get_body);
77     print  "$keys\n$messageid\n"
78         or die "Could not send keys: $!";
79     my $ca_score = <STDIN>;
80     die "Could not read ca_score: $!" if not defined $ca_score;
81     chomp $ca_score;
82     my $todo = 0;
83     my ($headers, $body);
84     my $seen = <STDIN>;
85     die "Child could not read seen: $!" if not defined $seen;
86     chomp $seen;
87     my $status;
88     my $nseen = $seen;
89     if ($seen) {
90         $todo = 1;
91         $headers = join('',$mail->get_all_headers());
92         $body = join('', @{$mail->get_body()});
93         $out .= "  spam $seen duplicate\n";
94     } else {
95         $status = $spam->check($mail);
96         ($headers, $body) = split /\n\n/, $status->rewrite_mail(), 2;
97         $headers .= "\n";
98         $body .= "\n";
99         
100         if ($status->is_spam()) {
101             $todo = 1;
102             my $score = sprintf "%.1f/%.1f %d",
103                     $status->get_score(), $status->get_required_score(),
104                     $ca_score;
105             $out .= "  spam $score\n";
106             $nseen = $score;
107         } elsif ($status->get_score() > 0 && $ca_score >= $config{spam_max_cross}) {
108             $todo = 2;
109             my $score = sprintf "%.1f/%.1f %d",
110             $status->get_score(), $status->get_required_score(), $ca_score;
111             $out .= "  spam $score\n";
112             $nseen = $score;
113         } else {
114             my ($before, $received, $after) = $headers =~
115                 /(^.*?)(^Received\: \(at .*?\n)(.*$)/ms;
116             open OUT, "> incoming/I$id" or die "open incoming/I$id: $!";
117             print OUT $received . $before . $after
118                 or die "print incoming/I$id: $!";
119             if ($ca_score > 1) {
120                 print OUT "X-CrossAssassin-Score: $ca_score\n"
121                     or die "print incoming/I$id: $!";
122             }
123             print OUT "\n" or die "print incoming/I$id: $!";
124             print OUT $body or die "print incoming/I$id: $!";
125             close OUT or die "close incoming/I$id: $!";
126             unlink "incoming/R$id" or warn "unlink incoming/R$id: $!";
127             $out .= sprintf "  ok %.1f/%.1f %d\n",
128             $status->get_score(), $status->get_required_score(),
129                     $ca_score;
130         }
131     }
132     print "$todo\n";
133     <STDIN>;
134     if ($todo) {
135         open OUT, '>>', ($todo == 1) ? $spam_mailbox : $cross_mailbox
136             or die "Could not open assassinated: $!";
137         print OUT $headers or die "print assassinated: $!";
138         if ($ca_score > 1) {
139             print OUT "X-CrossAssassin-Score: $ca_score\n"
140                 or die "print assassinated: $!";
141         }
142         print OUT "\n" or die "print assassinated: $!";
143         $body =~ s/^From />From /gm;
144         print OUT $body or die "print assassinated: $!";
145         close OUT or die "Close assassinated: $!";
146         unlink "incoming/R$id" or warn "unlink incoming/R$id: $!";
147     }
148     $out =~ tr/\n/\r/;
149     print "$nseen\n$out\n";
150     $status->finish() unless($seen);
151     $mail->finish();
152 }