]> git.donarmstrong.com Git - debbugs.git/blob - scripts/spamscan-sa
add back the other missing /usr/lib/debbugs
[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 $spam->compile_now(1); # use all user preferences
45
46 while (my $id = <STDIN>) {
47     chomp $id;
48     my $nf = <STDIN>;
49     if (not defined $nf) {
50          die "Could not read nf: $!";
51     }
52     chomp $nf;
53     unless (rename "incoming/S$id", "incoming/R$id") {
54         die "Could not rename incoming/S$id: $!";
55     }
56     my $out = "[$nf] $id scanning ...\n";
57     open MESSAGE, "< incoming/R$id" or die "open incoming/R$id: $!";
58     # Kludge to work around Received: then From_ weirdness in receive;
59     # remove when receive is fixed? We may continue to need it for
60     # reprocessing old messages.
61     my @textarray = <MESSAGE>;
62     if ($textarray[0] !~ /^From /) {
63         ($textarray[0], $textarray[1]) = ($textarray[1], $textarray[0]);
64     }
65     close MESSAGE;
66     my $mail = $spam->parse(\@textarray);
67     
68     my $messageid = header_or_empty($mail, 'Message-Id');
69     $out .= "  From: " . header_or_empty($mail, 'From') . "\n";
70     $out .= "  Subject: ". header_or_empty($mail, 'Subject') . "\n";
71     $out .= "  Date: " . header_or_empty($mail, 'Date') . "\n";
72     $out .= "  Message-Id: $messageid\n";
73     my $keys = ca_keys($mail->get_body);
74     print  "$keys\n$messageid\n"
75         or die "Could not send keys: $!";
76     my $ca_score = <STDIN>;
77     die "Could not read ca_score: $!" if not defined $ca_score;
78     chomp $ca_score;
79     my $todo = 0;
80     my ($headers, $body);
81     my $seen = <STDIN>;
82     die "Child could not read seen: $!" if not defined $seen;
83     chomp $seen;
84     my $status;
85     my $nseen = $seen;
86     if ($seen) {
87         $todo = 1;
88         $headers = join('',$mail->get_all_headers());
89         $body = join('', @{$mail->get_body()});
90         $out .= "  spam $seen duplicate\n";
91     } else {
92         $status = $spam->check($mail);
93         ($headers, $body) = split /\n\n/, $status->rewrite_mail(), 2;
94         $headers .= "\n";
95         $body .= "\n";
96         
97         if ($status->is_spam()) {
98             $todo = 1;
99             my $score = sprintf "%.1f/%.1f %d",
100                     $status->get_score(), $status->get_required_score(),
101                     $ca_score;
102             $out .= "  spam $score\n";
103             $nseen = $score;
104         } elsif ($status->get_score() > 0 && $ca_score >= $config{spam_max_cross}) {
105             $todo = 2;
106             my $score = sprintf "%.1f/%.1f %d",
107             $status->get_score(), $status->get_required_score(), $ca_score;
108             $out .= "  spam $score\n";
109             $nseen = $score;
110         } else {
111             my ($before, $received, $after) = $headers =~
112                 /(^.*?)(^Received\: \(at .*?\n)(.*$)/ms;
113             open OUT, "> incoming/I$id" or die "open incoming/I$id: $!";
114             print OUT $received . $before . $after
115                 or die "print incoming/I$id: $!";
116             if ($ca_score > 1) {
117                 print OUT "X-CrossAssassin-Score: $ca_score\n"
118                     or die "print incoming/I$id: $!";
119             }
120             print OUT "\n" or die "print incoming/I$id: $!";
121             print OUT $body or die "print incoming/I$id: $!";
122             close OUT or die "close incoming/I$id: $!";
123             unlink "incoming/R$id" or warn "unlink incoming/R$id: $!";
124             $out .= sprintf "  ok %.1f/%.1f %d\n",
125             $status->get_score(), $status->get_required_score(),
126                     $ca_score;
127         }
128     }
129     print "$todo\n";
130     <STDIN>;
131     if ($todo) {
132         open OUT, '>>', ($todo == 1) ? $spam_mailbox : $cross_mailbox
133             or die "Could not open assassinated: $!";
134         print OUT $headers or die "print assassinated: $!";
135         if ($ca_score > 1) {
136             print OUT "X-CrossAssassin-Score: $ca_score\n"
137                 or die "print assassinated: $!";
138         }
139         print OUT "\n" or die "print assassinated: $!";
140         $body =~ s/^From />From /gm;
141         print OUT $body or die "print assassinated: $!";
142         close OUT or die "Close assassinated: $!";
143         unlink "incoming/R$id" or warn "unlink incoming/R$id: $!";
144     }
145     $out =~ tr/\n/\r/;
146     print "$nseen\n$out\n";
147     $status->finish() unless($seen);
148     $mail->finish();
149 }