2 # spamassassin handling split from spamscan
5 # unfortunatly we can't use strict;
9 use Mail::CrossAssassin;
10 use Mail::SpamAssassin;
12 use Debbugs::Config qw(:config);
13 # New versions of debbugs will not allow use in /etc/debbugs/config
14 use POSIX qw(strftime);
15 my $spam_mailbox = strftime($config{spam_mailbox},gmtime);
16 my $cross_mailbox = strftime($config{spam_crossassassin_mailbox},gmtime);
22 sub header_or_empty ($$) {
23 my ($mail, $hdr) = @_;
24 my $value = $mail->get_header($hdr);
33 my $spam = Mail::SpamAssassin->new({
35 site_rules_filename => $config{spam_rules_dir},
36 userprefs_filename => $config{spam_user_prefs},
37 local_tests_only => ($config{spam_local_tests_only} || 0),
38 debug => ($ENV{DEBBUGS_SPAM_DEBUG} || 0),
40 $spam->compile_now(1); # use all user preferences
42 while (my $id = <STDIN>) {
45 if (not defined $nf) {
46 die "Could not read nf: $!";
49 unless (rename "incoming/S$id", "incoming/R$id") {
50 die "Could not rename incoming/S$id: $!";
52 my $out = "[$nf] $id scanning ...\n";
53 open MESSAGE, "< incoming/R$id" or die "open incoming/R$id: $!";
54 # Kludge to work around Received: then From_ weirdness in receive;
55 # remove when receive is fixed? We may continue to need it for
56 # reprocessing old messages.
57 my @textarray = <MESSAGE>;
58 if ($textarray[0] !~ /^From /) {
59 ($textarray[0], $textarray[1]) = ($textarray[1], $textarray[0]);
62 my $mail = $spam->parse(\@textarray);
64 my $messageid = header_or_empty($mail, 'Message-Id');
65 $out .= " From: " . header_or_empty($mail, 'From') . "\n";
66 $out .= " Subject: ". header_or_empty($mail, 'Subject') . "\n";
67 $out .= " Date: " . header_or_empty($mail, 'Date') . "\n";
68 $out .= " Message-Id: $messageid\n";
69 my $keys = ca_keys($mail->get_body);
70 print "$keys\n$messageid\n"
71 or die "Could not send keys: $!";
72 my $ca_score = <STDIN>;
73 die "Could not read ca_score: $!" if not defined $ca_score;
78 die "Child could not read seen: $!" if not defined $seen;
84 $headers = join('',$mail->get_all_headers());
85 $body = join('', @{$mail->get_body()});
86 $out .= " spam $seen duplicate\n";
88 $status = $spam->check($mail);
89 ($headers, $body) = split /\n\n/, $status->rewrite_mail(), 2;
93 if ($status->is_spam()) {
95 my $score = sprintf "%.1f/%.1f %d",
96 $status->get_score(), $status->get_required_score(),
98 $out .= " spam $score\n";
100 } elsif ($status->get_score() > 0 && $ca_score >= $config{spam_max_cross}) {
102 my $score = sprintf "%.1f/%.1f %d",
103 $status->get_score(), $status->get_required_score(), $ca_score;
104 $out .= " spam $score\n";
107 my ($before, $received, $after) = $headers =~
108 /(^.*?)(^Received\: \(at .*?\n)(.*$)/ms;
109 open OUT, "> incoming/I$id" or die "open incoming/I$id: $!";
110 print OUT $received . $before . $after
111 or die "print incoming/I$id: $!";
113 print OUT "X-CrossAssassin-Score: $ca_score\n"
114 or die "print incoming/I$id: $!";
116 print OUT "\n" or die "print incoming/I$id: $!";
117 print OUT $body or die "print incoming/I$id: $!";
118 close OUT or die "close incoming/I$id: $!";
119 unlink "incoming/R$id" or warn "unlink incoming/R$id: $!";
120 $out .= sprintf " ok %.1f/%.1f %d\n",
121 $status->get_score(), $status->get_required_score(),
128 open OUT, '>>', ($todo == 1) ? $spam_mailbox : $cross_mailbox
129 or die "Could not open assassinated: $!";
130 print OUT $headers or die "print assassinated: $!";
132 print OUT "X-CrossAssassin-Score: $ca_score\n"
133 or die "print assassinated: $!";
135 print OUT "\n" or die "print assassinated: $!";
136 $body =~ s/^From />From /gm;
137 print OUT $body or die "print assassinated: $!";
138 close OUT or die "Close assassinated: $!";
139 unlink "incoming/R$id" or warn "unlink incoming/R$id: $!";
142 print "$nseen\n$out\n";
143 $status->finish() unless($seen);