2 # spamassassin handling split from spamscan
5 # unfortunatly we can't use strict;
10 use lib qw(/usr/lib/debbugs);
12 use Mail::CrossAssassin;
13 use Mail::SpamAssassin;
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);
25 sub header_or_empty ($$) {
26 my ($mail, $hdr) = @_;
27 my $value = $mail->get_header($hdr);
30 # replace newlines with '\n'
37 my $spam = Mail::SpamAssassin->new({
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),
44 # wait longer for a lock
45 $spam->init_learner({wait_for_lock => 1});
47 $spam->compile_now(1); # use all user preferences
49 while (my $id = <STDIN>) {
52 if (not defined $nf) {
53 die "Could not read nf: $!";
56 unless (rename "incoming/S$id", "incoming/R$id") {
57 die "Could not rename incoming/S$id: $!";
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]);
69 my $mail = $spam->parse(\@textarray);
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;
85 die "Child could not read seen: $!" if not defined $seen;
91 $headers = join('',$mail->get_all_headers());
92 $body = join('', @{$mail->get_body()});
93 $out .= " spam $seen duplicate\n";
95 $status = $spam->check($mail);
96 ($headers, $body) = split /\n\n/, $status->rewrite_mail(), 2;
100 if ($status->is_spam()) {
102 my $score = sprintf "%.1f/%.1f %d",
103 $status->get_score(), $status->get_required_score(),
105 $out .= " spam $score\n";
107 } elsif ($status->get_score() > 0 && $ca_score >= $config{spam_max_cross}) {
109 my $score = sprintf "%.1f/%.1f %d",
110 $status->get_score(), $status->get_required_score(), $ca_score;
111 $out .= " spam $score\n";
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: $!";
120 print OUT "X-CrossAssassin-Score: $ca_score\n"
121 or die "print incoming/I$id: $!";
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(),
135 open OUT, '>>', ($todo == 1) ? $spam_mailbox : $cross_mailbox
136 or die "Could not open assassinated: $!";
137 print OUT $headers or die "print assassinated: $!";
139 print OUT "X-CrossAssassin-Score: $ca_score\n"
140 or die "print assassinated: $!";
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: $!";
149 print "$nseen\n$out\n";
150 $status->finish() unless($seen);