2 # $Id: spamscan.in,v 1.8 2005/02/01 07:54:01 blarson Exp $
6 # Performs SpamAssassin checks on a message before allowing it through to
7 # the main incoming queue.
9 # Uses up: incoming/S<code><bugnum>.nn
10 # Temps: incoming/R.nn
11 # Creates: incoming/I.nn
20 use Debbugs::Config qw(:config);
22 use Debbugs::Common qw(:lock);
24 use Mail::CrossAssassin;
30 exit unless $config{spam_scan};
32 chdir $config{spool_dir} or die "chdir spool: $!\n";
37 filelock('incoming-spamscan');
41 my %spamseen : shared = ();
42 my @ids : shared = ();
43 my %fudged : shared = ();
44 my $spamscan_stop : shared = 0;
45 my $cross_key : shared;
46 my @cross_return : shared;
47 my $cross_tid : shared;
48 my $print_lock : shared;
49 my $assassinated_lock : shared;
50 my $crossassassinated_lock : shared;
51 my $threadsrunning : shared = 0;
53 # flush output immediately
61 my $user_prefs = "$ENV{HOME}/.spamassassin/user_prefs";
64 $user_prefs_time = (stat $user_prefs)[9];
66 die "$user_prefs not found";
69 # This thread handles the updating and querying of the crossassassin db
71 ca_init('\b\d{3,8}(?:-(?:close|done|forwarded|maintonly|submitter|quiet|subscribe))?\@'.$config{email_domain}, $config{spam_crossassassin_db});
72 my $mytid = threads->self->tid();
74 while ($spamscan_stop <= 1) {
77 lock $cross_key unless($cross_key);
79 last crosscheck if $spamscan_stop > 1;
80 lprint "{$mytid} cross waiting\n";
81 cond_timedwait $cross_key, (time() + 30);
83 last crosscheck if ($spamscan_stop > 1);
89 lprint "{$mytid} Cross nothing\n";
93 lprint "{$mytid} Cross{$ct}: $ck\n";
96 $cross_return[$ct] = ca_set($ck);
97 cond_signal @cross_return;
102 # multiple threads handle spamassassin
105 lock $threadsrunning;
108 my $mytid = threads->self->tid();
110 return if $spamscan_stop;
113 my $pid = open2($saout, $sain, "/usr/lib/debbugs/spamscan-sa");
114 lprint "{$mytid} forked $pid\n";
115 my $messages_handled=0;
116 pp: until ($spamscan_stop) {
118 lprint "{$mytid} $messages_handled messages handled\n";
126 cond_timedwait @ids, (time() + 30);
127 last pp if $spamscan_stop;
132 lprint "{$mytid} Waiting for spam to process\n";
135 print $sain "$id\n$nf\n";
136 lprint "{$mytid} $id is $nf\n";
138 unless (defined $keys) {
139 lprint "{$mytid} Could not get keys: $!\n";
143 my $messageid = <$saout>;
144 unless (defined($messageid)) {
145 lprint "{$mytid} Could not read messageid: $!\n";
149 lprint "{$mytid} $id $keys\n";
154 unless ($cross_key) {
157 cond_signal $cross_key;
161 lprint "{$mytid} zzz...\n";
162 select undef, undef, undef, 0.1;
167 if ($cross_return[$mytid]) {
168 $ca_score = $cross_return[$mytid];
169 undef $cross_return[$mytid];
173 lprint "{$mytid} z z z...\n";
174 select undef, undef, undef, 0.1;
176 lprint "{$mytid} $id: ca_score: $ca_score\n";
177 my $seen = $spamseen{$messageid};
178 $seen = '' unless $seen;
179 unless(print $sain "$ca_score\n$seen\n") {
180 lprint "{$mytid} Could not send ca_score: $!\n";
184 unless (defined($todo)) {
185 lprint "{$mytid} Could not read todo: $!\n";
191 lock $assassinated_lock;
192 print $sain "$todo\n";
194 } elsif ($todo == 2) {
195 lock $crossassassinated_lock;
196 print $sain "$todo\n";
199 print $sain "$todo\n";
202 unless(defined($nseen)) {
203 lprint "{$mytid} Could not read seen: $!\n";
204 start_sa() if (scalar(@ids) > ($threadsrunning * $config{spam_spams_per_thread})
205 && $threadsrunning < $config{spam_max_threads});
209 $spamseen{$messageid} = $nseen if ($nseen);
211 unless(defined($out)) {
212 lprint "{$mytid} Could not read out: $!\n";
220 lock $threadsrunning;
230 my $s = threads->create(\&sa)
231 or die "Could not start sa threads: $!";
233 push @sa_threads, $s;
236 my $cross_thread = threads->create(\&cross)
237 or die "Could not start cross thread: $!";
238 $cross_thread->detach;
242 my $stopafter = time() + $config{spam_keep_running};
246 if (-f 'spamscan-stop') {
247 lprint "spamscan-stop file created\n";
250 if ($user_prefs_time != (stat $user_prefs)[9]) {
251 # stop and wait to be re-invoked from cron
252 lprint "File $user_prefs changed\n";
257 if (time() > $stopafter) {
258 lprint "KeepRunning timer expired\n";
262 opendir my $dir, 'incoming' or die "opendir incoming: $!";
263 while (defined($_ = readdir $dir)) {
264 push @i, $1 if /^S(.*)/;
268 lprint "No more spam to process\n";
271 @i = sort {(split(/\./,$a))[1] <=> (split(/\./,$b))[1]} @i;
273 lprint "Messages to process: $m\n";
278 start_sa if (scalar(@ids) > (($threadsrunning - 1) * $config{spam_spams_per_thread})
279 && $threadsrunning < $config{spam_max_threads});
285 # wait for the spamassasin threads
292 while (my $t = shift @sa_threads) {
294 lprint "{} waiting for thread $tid\n";
296 while ($t->is_running and --$max_wait > 0) {
302 # wait for the crossassasin thread
305 lprint "{} waiting for cross thread\n";
308 cond_signal $cross_key;
311 while ($cross_thread->is_running and --$max_wait > 0) {
314 #$cross_thread->join;
317 foreach my $thread (threads->list()){