* lpath.el: Fbind bbdb-records.
[gnus] / lisp / spam.el
1 ;;; spam.el --- Identifying spam
2 ;; Copyright (C) 2002 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; Keywords: network
6
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
23
24 ;;; Commentary:
25
26 ;;; This module addresses a few aspects of spam control under Gnus.  Page
27 ;;; breaks are used for grouping declarations and documentation relating to
28 ;;; each particular aspect.
29
30 ;;; The integration with Gnus is not yet complete.  See various `FIXME'
31 ;;; comments, below, for supplementary explanations or discussions.
32
33 ;;; Code:
34
35 (require 'gnus-sum)
36
37 ;; FIXME!  We should not require `dns' nor `message' until we actually
38 ;; need them.  Best would be to declare needed functions as auto-loadable.
39 (require 'dns)
40 (require 'message)
41
42 ;; Attempt to load BBDB macros
43 (eval-when-compile
44   (condition-case nil
45       (require 'bbdb-com)
46     (file-error (defalias 'bbdb-search 'ignore))))
47
48 ;; autoload executable-find
49 (eval-and-compile
50   ;; executable-find is not autoloaded in Emacs 20
51   (autoload 'executable-find "executable"))
52
53 ;; autoload ifile-spam-filter
54 (eval-and-compile
55   (autoload 'ifile-spam-filter "ifile-gnus"))
56
57 ;;; Main parameters.
58
59 (defvar spam-use-blacklist t
60   "True if the blacklist should be used.")
61
62 (defvar spam-use-whitelist nil
63   "True if the whitelist should be used.")
64
65 (defvar spam-use-blackholes nil
66   ;; FIXME!  Turned off for now.  The DNS routines are said to be flaky.
67   "True if blackholes should be used.")
68
69 (defvar spam-use-bogofilter nil
70   "True if bogofilter should be used.")
71
72 (defvar spam-use-bbdb nil
73   "True if BBDB should be used.")
74
75 (defvar spam-use-ifile nil
76   "True if ifile should be used.")
77
78 (defvar spam-split-group "spam"
79   "Usual group name where spam should be split.")
80
81 (defvar spam-junk-mailgroups
82   ;; FIXME!  The mailgroup list evidently depends on other choices made by the
83   ;; user, so the built-in default below is not likely to be appropriate.
84   (cons spam-split-group '("mail.junk" "poste.pourriel"))
85   "Mailgroups which are dedicated by splitting to receive various junk.
86 All unmarked article in such group receive the spam mark on group entry.")
87
88 ;; FIXME!  For `spam-ham-marks' and `spam-spam-marks', I wonder if it would
89 ;; not be easier for the user to just accept a string of mark letters, instead
90 ;; of a list of Gnus variable names.  In such case, the stunt of deferred
91 ;; evaluation would not be useful anymore.  Lars?? :-)
92
93 ;; FIXME!  It is rather questionable to see `K', `X' and `Y' as indicating
94 ;; positive ham.  It much depends on how and why people use kill files, score
95 ;; files, and the kill command.  Maybe it would be better, by default, to not
96 ;; process a message neither as ham nor spam, that is, just ignore it for
97 ;; learning purposes, when we are not sure of how the user sees it.
98 ;; But `r' and `R' should undoubtedly be seen as ham.
99
100 ;; FIXME!  Some might consider overkill to define a list of spam marks.  On
101 ;; the other hand, who knows, some users might for example like that
102 ;; explicitly `E'xpired articles be processed as positive spam.
103
104 (defvar spam-ham-marks
105   (list gnus-del-mark gnus-read-mark gnus-killed-mark
106          gnus-kill-file-mark gnus-low-score-mark)
107   "Marks considered as being ham (positively not spam).
108 Such articles will be transmitted to `bogofilter -n' on group exit.")
109
110 (defvar spam-spam-marks
111   (list gnus-spam-mark)
112   "Marks considered as being spam (positively spam).
113 Such articles will be transmitted to `bogofilter -s' on group exit.")
114
115 ;; FIXME!  Ideally, the remainder of this page should be fully integrated
116 ;; within `gnus-sum.el'.
117
118 ;;; Key bindings for spam control.
119
120 ;; FIXME!  The justification for `M-d' is that this is what Paul Graham
121 ;; suggests in his original article, and what Eric Raymond's patch for Mutt
122 ;; uses.  But more importantly, that binding was still free in Summary mode!
123
124 ;; FIXME!  Lars has not blessed the following key bindings yet.  It looks
125 ;; convenient that the score analysis command uses a sequence ending with the
126 ;; letter `t', so it nicely parallels `B t' or `V t'.  `M-d' is a kind of
127 ;; "alternate" `d', it is also the sequence suggested in Paul Graham article,
128 ;; and also in Eric Raymond's patch for Mutt.  `S x' might be the more
129 ;; official key binding for `M-d'.
130
131 (gnus-define-keys gnus-summary-mode-map
132   "St" spam-bogofilter-score
133   "Sx" gnus-summary-mark-as-spam
134   "\M-d" gnus-summary-mark-as-spam)
135
136 ;;; How to highlight a spam summary line.
137
138 ;; FIXME!  Of course, `gnus-splash-face' has another purpose.  Maybe a
139 ;; special face should be created, named and used instead, for spam lines.
140
141 (push '((eq mark gnus-spam-mark) . gnus-splash-face)
142       gnus-summary-highlight)
143
144 ;;; Hooks dispatching.  A bit raw for now.
145
146 (defun spam-summary-prepare ()
147   (spam-mark-junk-as-spam-routine))
148
149 (defun spam-summary-prepare-exit ()
150   (spam-bogofilter-register-routine))
151
152 (add-hook 'gnus-summary-prepare-hook 'spam-summary-prepare)
153 (add-hook 'gnus-summary-prepare-exit-hook 'spam-summary-prepare-exit)
154
155 (defun spam-mark-junk-as-spam-routine ()
156   (when (member gnus-newsgroup-name spam-junk-mailgroups)
157     (let ((articles gnus-newsgroup-articles)
158           article)
159       (while articles
160         (setq article (pop articles))
161         (when (eq (gnus-summary-article-mark article) gnus-unread-mark)
162           (gnus-summary-mark-article article gnus-spam-mark))))))
163 \f
164 ;;;; Spam determination.
165
166
167 (defvar spam-list-of-checks
168   '((spam-use-blacklist  . spam-check-blacklist)
169     (spam-use-whitelist  . spam-check-whitelist)
170     (spam-use-bbdb       . spam-check-bbdb)
171     (spam-use-ifile      . spam-check-ifile)
172     (spam-use-blackholes . spam-check-blackholes)
173     (spam-use-bogofilter . spam-check-bogofilter))
174 "The spam-list-of-checks list contains pairs associating a parameter
175 variable with a spam checking function.  If the parameter variable is
176 true, then the checking function is called, and its value decides what
177 happens.  Each individual check may return `nil', `t', or a mailgroup
178 name.  The value `nil' means that the check does not yield a decision,
179 and so, that further checks are needed.  The value `t' means that the
180 message is definitely not spam, and that further spam checks should be
181 inhibited.  Otherwise, a mailgroup name is returned where the mail
182 should go, and further checks are also inhibited.  The usual mailgroup
183 name is the value of `spam-split-group', meaning that the message is
184 definitely a spam.")
185
186 (defun spam-split ()
187   "Split this message into the `spam' group if it is spam.
188 This function can be used as an entry in `nnmail-split-fancy', for
189 example like this: (: spam-split)
190
191 See the Info node `(gnus)Fancy Mail Splitting' for more details."
192   (interactive)
193
194   (let ((list-of-checks spam-list-of-checks)
195         decision)
196     (while (and list-of-checks (not decision))
197       (let ((pair (pop list-of-checks)))
198         (when (eval (car pair))
199           (setq decision (apply (cdr pair))))))
200     (if (eq decision t)
201         nil
202       decision)))
203 \f
204 ;;;; Blackholes.
205
206 (defvar spam-blackhole-servers '("bl.spamcop.net"
207                                  "relays.ordb.org"
208                                  "dev.null.dk"
209                                  "relays.visi.com"
210                                  "rbl.maps.vix.com")
211   "List of blackhole servers.")
212
213 (defun spam-check-blackholes ()
214   "Check the Receieved headers for blackholed relays."
215   (let ((headers (message-fetch-field "received"))
216         ips matches)
217     (when headers
218       (with-temp-buffer
219         (insert headers)
220         (goto-char (point-min))
221         (while (re-search-forward
222                 "\\[\\([0-9]+.[0-9]+.[0-9]+.[0-9]+\\)\\]" nil t)
223           (message "Blackhole search found host IP %s." (match-string 1))
224           (push (mapconcat 'identity
225                            (nreverse (split-string (match-string 1) "\\."))
226                            ".")
227                 ips)))
228       (dolist (server spam-blackhole-servers)
229         (dolist (ip ips)
230           (when (query-dns (concat ip "." server))
231             (push (list ip server (query-dns (concat ip "." server) 'TXT))
232                   matches)))))
233     (when matches
234       spam-split-group)))
235 \f
236 ;;;; Blacklists and whitelists.
237
238 (defvar spam-directory "~/News/spam/"
239   "When spam files are kept.")
240
241 (defvar spam-whitelist (expand-file-name "whitelist" spam-directory)
242   "The location of the whitelist.
243 The file format is one regular expression per line.
244 The regular expression is matched against the address.")
245
246 (defvar spam-blacklist (expand-file-name "blacklist" spam-directory)
247   "The location of the blacklist.
248 The file format is one regular expression per line.
249 The regular expression is matched against the address.")
250
251 (defvar spam-whitelist-cache nil)
252 (defvar spam-blacklist-cache nil)
253
254 (defun spam-enter-whitelist (address)
255   "Enter ADDRESS into the whitelist."
256   (interactive "sAddress: ")
257   (spam-enter-list address spam-whitelist)
258   (setq spam-whitelist-cache nil))
259
260 (defun spam-enter-blacklist (address)
261   "Enter ADDRESS into the blacklist."
262   (interactive "sAddress: ")
263   (spam-enter-list address spam-blacklist)
264   (setq spam-blacklist-cache nil))
265
266 (defun spam-enter-list (address file)
267   "Enter ADDRESS into the given FILE, either the whitelist or the blacklist."
268   (unless (file-exists-p (file-name-directory file))
269     (make-directory (file-name-directory file) t))
270   (save-excursion
271     (set-buffer
272      (find-file-noselect file))
273     (goto-char (point-max))
274     (unless (bobp)
275       (insert "\n"))
276     (insert address "\n")
277     (save-buffer)))
278
279 ;;; returns nil if the sender is in the whitelist, spam-split-group otherwise
280 (defun spam-check-whitelist ()
281   ;; FIXME!  Should it detect when file timestamps change?
282   (unless spam-whitelist-cache
283     (setq spam-whitelist-cache (spam-parse-list spam-whitelist)))
284   (if (spam-from-listed-p spam-whitelist-cache) nil spam-split-group))
285
286 ;;; original idea from Alexander Kotelnikov <sacha@giotto.sj.ru>
287 (condition-case nil
288     (progn
289       (require 'bbdb-com)
290       (defun spam-check-bbdb ()
291         "We want messages from people who are in the BBDB not to be split to spam"
292         (let ((who (message-fetch-field "from")))
293           (when who
294             (setq who (regexp-quote (cadr (gnus-extract-address-components who))))
295             (if (bbdb-search (bbdb-records) nil nil who) nil spam-split-group)))))
296   (file-error (setq spam-list-of-checks
297                     (delete (assoc 'spam-use-bbdb spam-list-of-checks)
298                             spam-list-of-checks))))
299
300 ;;; check the ifile backend; return nil if the mail was NOT classified as spam
301 (condition-case nil
302     (progn
303       (require 'ifile-gnus)
304         ;;; 
305       (defun spam-check-ifile ()
306         (let ((ifile-primary-spam-group spam-split-group))
307           (ifile-spam-filter nil))))
308   (file-error (setq spam-list-of-checks
309                     (delete (assoc 'spam-use-ifile spam-list-of-checks)
310                             spam-list-of-checks))))
311
312 (defun spam-check-blacklist ()
313   ;; FIXME!  Should it detect when file timestamps change?
314   (unless spam-blacklist-cache
315     (setq spam-blacklist-cache (spam-parse-list spam-blacklist)))
316   (and (spam-from-listed-p spam-blacklist-cache) spam-split-group))
317
318 (eval-and-compile
319   (defalias 'spam-point-at-eol (if (fboundp 'point-at-eol)
320                                    'point-at-eol
321                                  'line-end-position)))
322
323 (defun spam-parse-list (file)
324   (when (file-readable-p file)
325     (let (contents address)
326       (with-temp-buffer
327         (insert-file-contents file)
328         (while (not (eobp))
329           (setq address (buffer-substring (point) (spam-point-at-eol)))
330           (forward-line 1)
331           (unless (zerop (length address))
332             (setq address (regexp-quote address))
333             (while (string-match "\\\\\\*" address)
334               (setq address (replace-match ".*" t t address)))
335             (push address contents))))
336       (nreverse contents))))
337
338 (defun spam-from-listed-p (cache)
339   (let ((from (message-fetch-field "from"))
340         found)
341     (while cache
342       (when (string-match (pop cache) from)
343         (setq found t
344               cache nil)))
345     found))
346
347 \f
348 ;;;; Training via Bogofilter.   Last updated 2002-09-02.
349
350 ;;; See Paul Graham article, at `http://www.paulgraham.com/spam.html'.
351
352 ;;; This page is for those wanting to control spam with the help of Eric
353 ;;; Raymond's speedy Bogofilter, see http://www.tuxedo.org/~esr/bogofilter.
354 ;;; This has been tested with a locally patched copy of version 0.4.
355
356 ;;; Make sure Bogofilter is installed.  Bogofilter internally uses Judy fast
357 ;;; associative arrays, so you need to install Judy first, and Bogofilter
358 ;;; next.  Fetch both distributions by visiting the following links and
359 ;;; downloading the latest version of each:
360 ;;;
361 ;;;     http://sourceforge.net/projects/judy/
362 ;;;     http://www.tuxedo.org/~esr/bogofilter/
363 ;;;
364 ;;; Unpack the Judy distribution and enter its main directory.  Then do:
365 ;;;
366 ;;;     ./configure
367 ;;;     make
368 ;;;     make install
369 ;;;
370 ;;; You will likely need to become super-user for the last step.  Then, unpack
371 ;;; the Bogofilter distribution and enter its main directory:
372 ;;;
373 ;;;     make
374 ;;;     make install
375 ;;;
376 ;;; Here as well, you need to become super-user for the last step.  Now,
377 ;;; initialises your word lists by doing, under your own identity:
378 ;;;
379 ;;;     mkdir ~/.bogofilter
380 ;;;     touch ~/.bogofilter/badlist
381 ;;;     touch ~/.bogofilter/goodlist
382 ;;;
383 ;;; These two files are text files you may edit, but you normally don't!
384
385 ;;; The `M-d' command gets added to Gnus summary mode, marking current article
386 ;;; as spam, showing it with the `H' mark.  Whenever you see a spam article,
387 ;;; make sure to mark its summary line with `M-d' before leaving the group.
388 ;;; Some groups, as per variable `spam-junk-mailgroups' below, receive articles
389 ;;; from Gnus splitting on clues added by spam recognisers, so for these
390 ;;; groups, we tack an `H' mark at group entry for all summary lines which
391 ;;; would otherwise have no other mark.  Make sure to _remove_ `H' marks for
392 ;;; any article which is _not_ genuine spam, before leaving such groups: you
393 ;;; may use `M-u' to "unread" the article, or `d' for declaring it read the
394 ;;; non-spam way.  When you leave a group, all `H' marked articles, saved or
395 ;;; unsaved, are sent to Bogofilter which will study them as spam samples.
396
397 ;;; Messages may also be deleted in various other ways, and unless
398 ;;; `spam-ham-marks-form' gets overridden below, marks `R' and `r' for default
399 ;;; read or explicit delete, marks `X' and 'K' for automatic or explicit
400 ;;; kills, as well as mark `Y' for low scores, are all considered to be
401 ;;; associated with articles which are not spam.  This assumption might be
402 ;;; false, in particular if you use kill files or score files as means for
403 ;;; detecting genuine spam, you should then adjust `spam-ham-marks-form'.  When
404 ;;; you leave a group, all _unsaved_ articles bearing any the above marks are
405 ;;; sent to Bogofilter which will study these as not-spam samples.  If you
406 ;;; explicit kill a lot, you might sometimes end up with articles marked `K'
407 ;;; which you never saw, and which might accidentally contain spam.  Best is
408 ;;; to make sure that real spam is marked with `H', and nothing else.
409
410 ;;; All other marks do not contribute to Bogofilter pre-conditioning.  In
411 ;;; particular, ticked, dormant or souped articles are likely to contribute
412 ;;; later, when they will get deleted for real, so there is no need to use
413 ;;; them prematurely.  Explicitly expired articles do not contribute, command
414 ;;; `E' is a way to get rid of an article without Bogofilter ever seeing it.
415
416 ;;; In a word, with a minimum of care for associating the `H' mark for spam
417 ;;; articles only, Bogofilter training all gets fairly automatic.  You should
418 ;;; do this until you get a few hundreds of articles in each category, spam
419 ;;; or not.  The shell command `head -1 ~/.bogofilter/*' shows both article
420 ;;; counts.  The command `S S' in summary mode, either for debugging or for
421 ;;; curiosity, triggers Bogofilter into displaying in another buffer the
422 ;;; "spamicity" score of the current article (between 0.0 and 1.0), together
423 ;;; with the article words which most significantly contribute to the score.
424
425 ;;; The real way for using Bogofilter, however, is to have some use tool like
426 ;;; `procmail' for invoking it on message reception, then adding some
427 ;;; recognisable header in case of detected spam.  Gnus splitting rules might
428 ;;; later trip on these added headers and react by sorting such articles into
429 ;;; specific junk folders as per `spam-junk-mailgroups'.  Here is a possible
430 ;;; `.procmailrc' contents (still untested -- please tell me how it goes):
431 ;;;
432 ;;; :0HBf:
433 ;;; * ? bogofilter
434 ;;; | formail -bfI "X-Spam-Status: Yes"
435
436 (defvar spam-output-buffer-name "*Bogofilter Output*"
437   "Name of buffer when displaying `bogofilter -v' output.")
438
439 (defvar spam-spaminfo-header-regexp
440   ;; FIXME!  In the following regexp, we should explain which tool produces
441   ;; which kind of header.  I do not even remember them all by now.  X-Junk
442   ;; (and previously X-NoSpam) are produced by the `NoSpam' tool, which has
443   ;; never been published, so it might not be reasonable leaving it in the
444   ;; list.
445   "^X-\\(jf\\|Junk\\|NoSpam\\|Spam\\|SB\\)[^:]*:"
446   "Regexp for spam markups in headers.
447 Markup from spam recognisers, as well as `Xref', are to be removed from
448 articles before they get registered by Bogofilter.")
449
450 (defvar spam-bogofilter-path (executable-find "bogofilter")
451   "File path of the Bogofilter executable program.
452 Force this variable to nil if you want to inhibit the functionality.")
453
454 (defun spam-check-bogofilter ()
455   ;; Dynamic spam check.  I do not know how to check the exit status,
456   ;; so instead, read `bogofilter -v' output.
457   (when (and spam-use-bogofilter spam-bogofilter-path)
458     (spam-bogofilter-articles nil "-v" (list (gnus-summary-article-number)))
459     (when (save-excursion
460             (set-buffer spam-output-buffer-name)
461             (goto-char (point-min))
462             (re-search-forward "Spamicity: \\(0\\.9\\|1\\.0\\)" nil t))
463       spam-split-group)))
464
465 (defun spam-bogofilter-score ()
466   "Use `bogofilter -v' on the current article.
467 This yields the 15 most discriminant words for this article and the
468 spamicity coefficient of each, and the overall article spamicity."
469   (interactive)
470   (when (and spam-use-bogofilter spam-bogofilter-path)
471     (spam-bogofilter-articles nil "-v" (list (gnus-summary-article-number)))
472     (with-current-buffer spam-output-buffer-name
473       (unless (zerop (buffer-size))
474         (if (<= (count-lines (point-min) (point-max)) 1)
475             (progn
476               (goto-char (point-max))
477               (when (bolp)
478                 (backward-char 1))
479               (message "%s" (buffer-substring (point-min) (point))))
480           (goto-char (point-min))
481           (display-buffer (current-buffer)))))))
482
483 (defun spam-bogofilter-register-routine ()
484   (when (and spam-use-bogofilter spam-bogofilter-path)
485     (let ((articles gnus-newsgroup-articles)
486           article mark ham-articles spam-articles)
487       (while articles
488         (setq article (pop articles)
489               mark (gnus-summary-article-mark article))
490         (cond ((memq mark spam-spam-marks) (push article spam-articles))
491               ((memq article gnus-newsgroup-saved))
492               ((memq mark spam-ham-marks) (push article ham-articles))))
493       (when ham-articles
494         (spam-bogofilter-articles "ham" "-n" ham-articles))
495       (when spam-articles
496         (spam-bogofilter-articles "SPAM" "-s" spam-articles)))))
497
498 (defvar spam-bogofilter-initial-timeout 40
499   "Timeout in seconds for the initial reply from the `bogofilter' program.")
500
501 (defvar spam-bogofilter-subsequent-timeout 15
502   "Timeout in seconds for any subsequent reply from the `bogofilter' program.")
503
504 (defun spam-bogofilter-articles (type option articles)
505   (let ((output-buffer (get-buffer-create spam-output-buffer-name))
506         (article-copy (get-buffer-create " *Bogofilter Article Copy*"))
507         (remove-regexp (concat spam-spaminfo-header-regexp "\\|Xref:"))
508         (counter 0)
509         prefix process article)
510     (when type
511       (setq prefix (format "Studying %d articles as %s..." (length articles)
512                            type))
513       (message "%s" prefix))
514     (save-excursion (set-buffer output-buffer) (erase-buffer))
515     (setq process (start-process "bogofilter" output-buffer
516                                  spam-bogofilter-path "-F" option))
517     (process-kill-without-query process t)
518     (unwind-protect
519         (save-window-excursion
520           (while articles
521             (setq counter (1+ counter))
522             (when prefix
523               (message "%s %d" prefix counter))
524             (setq article (pop articles))
525             (gnus-summary-goto-subject article)
526             (gnus-summary-select-article)
527             (gnus-eval-in-buffer-window article-copy
528               (insert-buffer-substring gnus-original-article-buffer)
529               ;; Remove spam classification redundant headers: they may induce
530               ;; unwanted biases in later analysis.
531               (goto-char (point-min))
532               (while (not (or (eobp) (= (following-char) ?\n)))
533                 (if (looking-at remove-regexp)
534                     (delete-region (point)
535                                    (save-excursion (forward-line 1) (point)))
536                   (forward-line 1)))
537               (goto-char (point-min))
538               ;; Bogofilter really wants From envelopes for counting articles.
539               ;; Fake one at the beginning, make sure there will be no other.
540               (if (looking-at "From ")
541                   (forward-line 1)
542                 (insert "From nobody " (current-time-string) "\n"))
543               (let (case-fold-search)
544                 (while (re-search-forward "^From " nil t)
545                   (beginning-of-line)
546                   (insert ">")))
547               (process-send-region process (point-min) (point-max))
548               (erase-buffer))))
549       ;; Sending the EOF is unwind-protected.  This is to prevent lost copies
550       ;; of `bogofilter', hung on reading their standard input, in case the
551       ;; whole registering process gets interrupted by the user.
552       (process-send-eof process))
553     (kill-buffer article-copy)
554     ;; Receive process output.  It sadly seems that we still have to protect
555     ;; ourselves against hung `bogofilter' processes.
556     (let ((status (process-status process))
557           (timeout (* 1000 spam-bogofilter-initial-timeout))
558           (quanta 200))                 ; also counted in milliseconds
559       (while (and (not (eq status 'exit)) (> timeout 0))
560         ;; `accept-process-output' timeout is counted in microseconds.
561         (setq timeout (if (accept-process-output process 0 (* 1000 quanta))
562                           (* 1000 spam-bogofilter-subsequent-timeout)
563                         (- timeout quanta))
564               status (process-status process)))
565       (if (eq status 'exit)
566           (when prefix
567             (message "%s done!" prefix))
568         ;; Sigh!  The process did time out...  Become brutal!
569         (interrupt-process process)
570         (message "%s %d INTERRUPTED!  (Article %d, status %s)"
571                  (or prefix "Bogofilter process...")
572                  counter article status)
573         ;; Give some time for user to read.  Sitting redisplays but gives up
574         ;; if input is pending.  Sleeping does not give up, but it does not
575         ;; redisplay either.  Mix both: let's redisplay and not give up.
576         (sit-for 1)
577         (sleep-for 3)))))
578
579 (provide 'spam)
580
581 ;;; spam.el ends here.