disabled blackhole checks by default
[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 nil
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   "List of blackhole servers.")
219
220 (defun spam-check-blackholes ()
221   "Check the Received headers for blackholed relays."
222   (let ((headers (message-fetch-field "received"))
223         ips matches)
224     (when headers
225       (with-temp-buffer
226         (insert headers)
227         (goto-char (point-min))
228         (while (re-search-forward
229                 "\\[\\([0-9]+.[0-9]+.[0-9]+.[0-9]+\\)\\]" nil t)
230           (message "Blackhole search found host IP %s." (match-string 1))
231           (push (mapconcat 'identity
232                            (nreverse (split-string (match-string 1) "\\."))
233                            ".")
234                 ips)))
235       (dolist (server spam-blackhole-servers)
236         (dolist (ip ips)
237           (let ((query-string (concat ip "." server)))
238             (if spam-use-dig
239                 (let ((query-result (query-dig query-string)))
240                   (when query-result
241                     (message "spam detected with blackhole check of relay %s (dig query result '%s')" query-string query-result)
242                     (push (list ip server query-result)
243                           matches)))
244               ;; else, if not using dig.el
245               (when (query-dns query-string)
246                 (push (list ip server (query-dns query-string 'TXT))
247                       matches)))))))
248     (when matches
249       spam-split-group)))
250 \f
251 ;;;; Blacklists and whitelists.
252
253 (defvar spam-directory "~/News/spam/"
254   "When spam files are kept.")
255
256 (defvar spam-whitelist (expand-file-name "whitelist" spam-directory)
257   "The location of the whitelist.
258 The file format is one regular expression per line.
259 The regular expression is matched against the address.")
260
261 (defvar spam-blacklist (expand-file-name "blacklist" spam-directory)
262   "The location of the blacklist.
263 The file format is one regular expression per line.
264 The regular expression is matched against the address.")
265
266 (defvar spam-whitelist-cache nil)
267 (defvar spam-blacklist-cache nil)
268
269 (defun spam-enter-whitelist (address)
270   "Enter ADDRESS into the whitelist."
271   (interactive "sAddress: ")
272   (spam-enter-list address spam-whitelist)
273   (setq spam-whitelist-cache nil))
274
275 (defun spam-enter-blacklist (address)
276   "Enter ADDRESS into the blacklist."
277   (interactive "sAddress: ")
278   (spam-enter-list address spam-blacklist)
279   (setq spam-blacklist-cache nil))
280
281 (defun spam-enter-list (address file)
282   "Enter ADDRESS into the given FILE, either the whitelist or the blacklist."
283   (unless (file-exists-p (file-name-directory file))
284     (make-directory (file-name-directory file) t))
285   (save-excursion
286     (set-buffer
287      (find-file-noselect file))
288     (goto-char (point-max))
289     (unless (bobp)
290       (insert "\n"))
291     (insert address "\n")
292     (save-buffer)))
293
294 ;;; returns nil if the sender is in the whitelist, spam-split-group otherwise
295 (defun spam-check-whitelist ()
296   ;; FIXME!  Should it detect when file timestamps change?
297   (unless spam-whitelist-cache
298     (setq spam-whitelist-cache (spam-parse-list spam-whitelist)))
299   (if (spam-from-listed-p spam-whitelist-cache) nil spam-split-group))
300
301 ;;; original idea from Alexander Kotelnikov <sacha@giotto.sj.ru>
302 (condition-case nil
303     (progn
304       (require 'bbdb-com)
305       (defun spam-check-bbdb ()
306         "We want messages from people who are in the BBDB not to be split to spam"
307         (let ((who (message-fetch-field "from")))
308           (when who
309             (setq who (regexp-quote (cadr (gnus-extract-address-components who))))
310             (if (bbdb-search (bbdb-records) nil nil who) nil spam-split-group)))))
311   (file-error (setq spam-list-of-checks
312                     (delete (assoc 'spam-use-bbdb spam-list-of-checks)
313                             spam-list-of-checks))))
314
315 ;;; check the ifile backend; return nil if the mail was NOT classified as spam
316 (condition-case nil
317     (progn
318       (require 'ifile-gnus)
319         ;;; 
320       (defun spam-check-ifile ()
321         (let ((ifile-primary-spam-group spam-split-group))
322           (ifile-spam-filter nil))))
323   (file-error (setq spam-list-of-checks
324                     (delete (assoc 'spam-use-ifile spam-list-of-checks)
325                             spam-list-of-checks))))
326
327 (defun spam-check-blacklist ()
328   ;; FIXME!  Should it detect when file timestamps change?
329   (unless spam-blacklist-cache
330     (setq spam-blacklist-cache (spam-parse-list spam-blacklist)))
331   (and (spam-from-listed-p spam-blacklist-cache) spam-split-group))
332
333 (eval-and-compile
334   (defalias 'spam-point-at-eol (if (fboundp 'point-at-eol)
335                                    'point-at-eol
336                                  'line-end-position)))
337
338 (defun spam-parse-list (file)
339   (when (file-readable-p file)
340     (let (contents address)
341       (with-temp-buffer
342         (insert-file-contents file)
343         (while (not (eobp))
344           (setq address (buffer-substring (point) (spam-point-at-eol)))
345           (forward-line 1)
346           (unless (zerop (length address))
347             (setq address (regexp-quote address))
348             (while (string-match "\\\\\\*" address)
349               (setq address (replace-match ".*" t t address)))
350             (push address contents))))
351       (nreverse contents))))
352
353 (defun spam-from-listed-p (cache)
354   (let ((from (message-fetch-field "from"))
355         found)
356     (while cache
357       (when (string-match (pop cache) from)
358         (setq found t
359               cache nil)))
360     found))
361
362 \f
363 ;;;; Training via Bogofilter.   Last updated 2002-09-02.
364
365 ;;; See Paul Graham article, at `http://www.paulgraham.com/spam.html'.
366
367 ;;; This page is for those wanting to control spam with the help of Eric
368 ;;; Raymond's speedy Bogofilter, see http://www.tuxedo.org/~esr/bogofilter.
369 ;;; This has been tested with a locally patched copy of version 0.4.
370
371 ;;; Make sure Bogofilter is installed.  Bogofilter internally uses Judy fast
372 ;;; associative arrays, so you need to install Judy first, and Bogofilter
373 ;;; next.  Fetch both distributions by visiting the following links and
374 ;;; downloading the latest version of each:
375 ;;;
376 ;;;     http://sourceforge.net/projects/judy/
377 ;;;     http://www.tuxedo.org/~esr/bogofilter/
378 ;;;
379 ;;; Unpack the Judy distribution and enter its main directory.  Then do:
380 ;;;
381 ;;;     ./configure
382 ;;;     make
383 ;;;     make install
384 ;;;
385 ;;; You will likely need to become super-user for the last step.  Then, unpack
386 ;;; the Bogofilter distribution and enter its main directory:
387 ;;;
388 ;;;     make
389 ;;;     make install
390 ;;;
391 ;;; Here as well, you need to become super-user for the last step.  Now,
392 ;;; initialize your word lists by doing, under your own identity:
393 ;;;
394 ;;;     mkdir ~/.bogofilter
395 ;;;     touch ~/.bogofilter/badlist
396 ;;;     touch ~/.bogofilter/goodlist
397 ;;;
398 ;;; These two files are text files you may edit, but you normally don't!
399
400 ;;; The `M-d' command gets added to Gnus summary mode, marking current article
401 ;;; as spam, showing it with the `H' mark.  Whenever you see a spam article,
402 ;;; make sure to mark its summary line with `M-d' before leaving the group.
403 ;;; Some groups, as per variable `spam-junk-mailgroups' below, receive articles
404 ;;; from Gnus splitting on clues added by spam recognisers, so for these
405 ;;; groups, we tack an `H' mark at group entry for all summary lines which
406 ;;; would otherwise have no other mark.  Make sure to _remove_ `H' marks for
407 ;;; any article which is _not_ genuine spam, before leaving such groups: you
408 ;;; may use `M-u' to "unread" the article, or `d' for declaring it read the
409 ;;; non-spam way.  When you leave a group, all `H' marked articles, saved or
410 ;;; unsaved, are sent to Bogofilter which will study them as spam samples.
411
412 ;;; Messages may also be deleted in various other ways, and unless
413 ;;; `spam-ham-marks-form' gets overridden below, marks `R' and `r' for default
414 ;;; read or explicit delete, marks `X' and 'K' for automatic or explicit
415 ;;; kills, as well as mark `Y' for low scores, are all considered to be
416 ;;; associated with articles which are not spam.  This assumption might be
417 ;;; false, in particular if you use kill files or score files as means for
418 ;;; detecting genuine spam, you should then adjust `spam-ham-marks-form'.  When
419 ;;; you leave a group, all _unsaved_ articles bearing any the above marks are
420 ;;; sent to Bogofilter which will study these as not-spam samples.  If you
421 ;;; explicit kill a lot, you might sometimes end up with articles marked `K'
422 ;;; which you never saw, and which might accidentally contain spam.  Best is
423 ;;; to make sure that real spam is marked with `H', and nothing else.
424
425 ;;; All other marks do not contribute to Bogofilter pre-conditioning.  In
426 ;;; particular, ticked, dormant or souped articles are likely to contribute
427 ;;; later, when they will get deleted for real, so there is no need to use
428 ;;; them prematurely.  Explicitly expired articles do not contribute, command
429 ;;; `E' is a way to get rid of an article without Bogofilter ever seeing it.
430
431 ;;; In a word, with a minimum of care for associating the `H' mark for spam
432 ;;; articles only, Bogofilter training all gets fairly automatic.  You should
433 ;;; do this until you get a few hundreds of articles in each category, spam
434 ;;; or not.  The shell command `head -1 ~/.bogofilter/*' shows both article
435 ;;; counts.  The command `S S' in summary mode, either for debugging or for
436 ;;; curiosity, triggers Bogofilter into displaying in another buffer the
437 ;;; "spamicity" score of the current article (between 0.0 and 1.0), together
438 ;;; with the article words which most significantly contribute to the score.
439
440 ;;; The real way for using Bogofilter, however, is to have some use tool like
441 ;;; `procmail' for invoking it on message reception, then adding some
442 ;;; recognisable header in case of detected spam.  Gnus splitting rules might
443 ;;; later trip on these added headers and react by sorting such articles into
444 ;;; specific junk folders as per `spam-junk-mailgroups'.  Here is a possible
445 ;;; `.procmailrc' contents (still untested -- please tell me how it goes):
446 ;;;
447 ;;; :0HBf:
448 ;;; * ? bogofilter
449 ;;; | formail -bfI "X-Spam-Status: Yes"
450
451 (defvar spam-output-buffer-name "*Bogofilter Output*"
452   "Name of buffer when displaying `bogofilter -v' output.")
453
454 (defvar spam-spaminfo-header-regexp
455   ;; FIXME!  In the following regexp, we should explain which tool produces
456   ;; which kind of header.  I do not even remember them all by now.  X-Junk
457   ;; (and previously X-NoSpam) are produced by the `NoSpam' tool, which has
458   ;; never been published, so it might not be reasonable leaving it in the
459   ;; list.
460   "^X-\\(jf\\|Junk\\|NoSpam\\|Spam\\|SB\\)[^:]*:"
461   "Regexp for spam markups in headers.
462 Markup from spam recognisers, as well as `Xref', are to be removed from
463 articles before they get registered by Bogofilter.")
464
465 (defvar spam-bogofilter-path (executable-find "bogofilter")
466   "File path of the Bogofilter executable program.
467 Force this variable to nil if you want to inhibit the functionality.")
468
469 (defun spam-check-bogofilter ()
470   ;; Dynamic spam check.  I do not know how to check the exit status,
471   ;; so instead, read `bogofilter -v' output.
472   (when (and spam-use-bogofilter spam-bogofilter-path)
473     (spam-bogofilter-articles nil "-v" (list (gnus-summary-article-number)))
474     (when (save-excursion
475             (set-buffer spam-output-buffer-name)
476             (goto-char (point-min))
477             (re-search-forward "Spamicity: \\(0\\.9\\|1\\.0\\)" nil t))
478       spam-split-group)))
479
480 (defun spam-bogofilter-score ()
481   "Use `bogofilter -v' on the current article.
482 This yields the 15 most discriminant words for this article and the
483 spamicity coefficient of each, and the overall article spamicity."
484   (interactive)
485   (when (and spam-use-bogofilter spam-bogofilter-path)
486     (spam-bogofilter-articles nil "-v" (list (gnus-summary-article-number)))
487     (with-current-buffer spam-output-buffer-name
488       (unless (zerop (buffer-size))
489         (if (<= (count-lines (point-min) (point-max)) 1)
490             (progn
491               (goto-char (point-max))
492               (when (bolp)
493                 (backward-char 1))
494               (message "%s" (buffer-substring (point-min) (point))))
495           (goto-char (point-min))
496           (display-buffer (current-buffer)))))))
497
498 (defun spam-bogofilter-register-routine ()
499   (when (and spam-use-bogofilter spam-bogofilter-path)
500     (let ((articles gnus-newsgroup-articles)
501           article mark ham-articles spam-articles)
502       (while articles
503         (setq article (pop articles)
504               mark (gnus-summary-article-mark article))
505         (cond ((memq mark spam-spam-marks) (push article spam-articles))
506               ((memq article gnus-newsgroup-saved))
507               ((memq mark spam-ham-marks) (push article ham-articles))))
508       (when ham-articles
509         (spam-bogofilter-articles "ham" "-n" ham-articles))
510       (when spam-articles
511         (spam-bogofilter-articles "SPAM" "-s" spam-articles)))))
512
513 (defvar spam-bogofilter-initial-timeout 40
514   "Timeout in seconds for the initial reply from the `bogofilter' program.")
515
516 (defvar spam-bogofilter-subsequent-timeout 15
517   "Timeout in seconds for any subsequent reply from the `bogofilter' program.")
518
519 (defun spam-bogofilter-articles (type option articles)
520   (let ((output-buffer (get-buffer-create spam-output-buffer-name))
521         (article-copy (get-buffer-create " *Bogofilter Article Copy*"))
522         (remove-regexp (concat spam-spaminfo-header-regexp "\\|Xref:"))
523         (counter 0)
524         prefix process article)
525     (when type
526       (setq prefix (format "Studying %d articles as %s..." (length articles)
527                            type))
528       (message "%s" prefix))
529     (save-excursion (set-buffer output-buffer) (erase-buffer))
530     (setq process (start-process "bogofilter" output-buffer
531                                  spam-bogofilter-path "-F" option))
532     (process-kill-without-query process t)
533     (unwind-protect
534         (save-window-excursion
535           (while articles
536             (setq counter (1+ counter))
537             (when prefix
538               (message "%s %d" prefix counter))
539             (setq article (pop articles))
540             (gnus-summary-goto-subject article)
541             (gnus-summary-select-article)
542             (gnus-eval-in-buffer-window article-copy
543               (insert-buffer-substring gnus-original-article-buffer)
544               ;; Remove spam classification redundant headers: they may induce
545               ;; unwanted biases in later analysis.
546               (goto-char (point-min))
547               (while (not (or (eobp) (= (following-char) ?\n)))
548                 (if (looking-at remove-regexp)
549                     (delete-region (point)
550                                    (save-excursion (forward-line 1) (point)))
551                   (forward-line 1)))
552               (goto-char (point-min))
553               ;; Bogofilter really wants From envelopes for counting articles.
554               ;; Fake one at the beginning, make sure there will be no other.
555               (if (looking-at "From ")
556                   (forward-line 1)
557                 (insert "From nobody " (current-time-string) "\n"))
558               (let (case-fold-search)
559                 (while (re-search-forward "^From " nil t)
560                   (beginning-of-line)
561                   (insert ">")))
562               (process-send-region process (point-min) (point-max))
563               (erase-buffer))))
564       ;; Sending the EOF is unwind-protected.  This is to prevent lost copies
565       ;; of `bogofilter', hung on reading their standard input, in case the
566       ;; whole registering process gets interrupted by the user.
567       (process-send-eof process))
568     (kill-buffer article-copy)
569     ;; Receive process output.  It sadly seems that we still have to protect
570     ;; ourselves against hung `bogofilter' processes.
571     (let ((status (process-status process))
572           (timeout (* 1000 spam-bogofilter-initial-timeout))
573           (quanta 200))                 ; also counted in milliseconds
574       (while (and (not (eq status 'exit)) (> timeout 0))
575         ;; `accept-process-output' timeout is counted in microseconds.
576         (setq timeout (if (accept-process-output process 0 (* 1000 quanta))
577                           (* 1000 spam-bogofilter-subsequent-timeout)
578                         (- timeout quanta))
579               status (process-status process)))
580       (if (eq status 'exit)
581           (when prefix
582             (message "%s done!" prefix))
583         ;; Sigh!  The process did time out...  Become brutal!
584         (interrupt-process process)
585         (message "%s %d INTERRUPTED!  (Article %d, status %s)"
586                  (or prefix "Bogofilter process...")
587                  counter article status)
588         ;; Give some time for user to read.  Sitting redisplays but gives up
589         ;; if input is pending.  Sleeping does not give up, but it does not
590         ;; redisplay either.  Mix both: let's redisplay and not give up.
591         (sit-for 1)
592         (sleep-for 3)))))
593
594 (provide 'spam)
595
596 ;;; spam.el ends here.