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);
27 # replace newlines with '\n'
34 my $spam = Mail::SpamAssassin->new({
36 site_rules_filename => $config{spam_rules_dir},
37 userprefs_filename => $config{spam_user_prefs},
38 local_tests_only => ($config{spam_local_tests_only} || 0),
39 debug => ($ENV{DEBBUGS_SPAM_DEBUG} || 0),
41 $spam->compile_now(1); # use all user preferences
43 while (my $id = <STDIN>) {
46 if (not defined $nf) {
47 die "Could not read nf: $!";
50 unless (rename "incoming/S$id", "incoming/R$id") {
51 die "Could not rename incoming/S$id: $!";
53 my $out = "[$nf] $id scanning ...\n";
54 open MESSAGE, "< incoming/R$id" or die "open incoming/R$id: $!";
55 # Kludge to work around Received: then From_ weirdness in receive;
56 # remove when receive is fixed? We may continue to need it for
57 # reprocessing old messages.
58 my @textarray = <MESSAGE>;
59 if ($textarray[0] !~ /^From /) {
60 ($textarray[0], $textarray[1]) = ($textarray[1], $textarray[0]);
63 my $mail = $spam->parse(\@textarray);
65 my $messageid = header_or_empty($mail, 'Message-Id');
66 $out .= " From: " . header_or_empty($mail, 'From') . "\n";
67 $out .= " Subject: ". header_or_empty($mail, 'Subject') . "\n";
68 $out .= " Date: " . header_or_empty($mail, 'Date') . "\n";
69 $out .= " Message-Id: $messageid\n";
70 my $keys = ca_keys($mail->get_body);
71 print "$keys\n$messageid\n"
72 or die "Could not send keys: $!";
73 my $ca_score = <STDIN>;
74 die "Could not read ca_score: $!" if not defined $ca_score;
79 die "Child could not read seen: $!" if not defined $seen;
85 $headers = join('',$mail->get_all_headers());
86 $body = join('', @{$mail->get_body()});
87 $out .= " spam $seen duplicate\n";
89 $status = $spam->check($mail);
90 ($headers, $body) = split /\n\n/, $status->rewrite_mail(), 2;
94 if ($status->is_spam()) {
96 my $score = sprintf "%.1f/%.1f %d",
97 $status->get_score(), $status->get_required_score(),
99 $out .= " spam $score\n";
101 } elsif ($status->get_score() > 0 && $ca_score >= $config{spam_max_cross}) {
103 my $score = sprintf "%.1f/%.1f %d",
104 $status->get_score(), $status->get_required_score(), $ca_score;
105 $out .= " spam $score\n";
108 my ($before, $received, $after) = $headers =~
109 /(^.*?)(^Received\: \(at .*?\n)(.*$)/ms;
110 open OUT, "> incoming/I$id" or die "open incoming/I$id: $!";
111 print OUT $received . $before . $after
112 or die "print incoming/I$id: $!";
114 print OUT "X-CrossAssassin-Score: $ca_score\n"
115 or die "print incoming/I$id: $!";
117 print OUT "\n" or die "print incoming/I$id: $!";
118 print OUT $body or die "print incoming/I$id: $!";
119 close OUT or die "close incoming/I$id: $!";
120 unlink "incoming/R$id" or warn "unlink incoming/R$id: $!";
121 $out .= sprintf " ok %.1f/%.1f %d\n",
122 $status->get_score(), $status->get_required_score(),
129 open OUT, '>>', ($todo == 1) ? $spam_mailbox : $cross_mailbox
130 or die "Could not open assassinated: $!";
131 print OUT $headers or die "print assassinated: $!";
133 print OUT "X-CrossAssassin-Score: $ca_score\n"
134 or die "print assassinated: $!";
136 print OUT "\n" or die "print assassinated: $!";
137 $body =~ s/^From />From /gm;
138 print OUT $body or die "print assassinated: $!";
139 close OUT or die "Close assassinated: $!";
140 unlink "incoming/R$id" or warn "unlink incoming/R$id: $!";
143 print "$nseen\n$out\n";
144 $status->finish() unless($seen);