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