Import Gnus v5.10.4.
[elisp/gnus.git-] / lisp / spam.el
1 ;; TODO: spam scores, detection of spam in newsgroups, cross-server splitting, remote processing, training through files
2
3 ;;; spam.el --- Identifying spam
4 ;; Copyright (C) 2002, 2003 Free Software Foundation, Inc.
5
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; Keywords: network
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Commentary:
27
28 ;;; This module addresses a few aspects of spam control under Gnus.  Page
29 ;;; breaks are used for grouping declarations and documentation relating to
30 ;;; each particular aspect.
31
32 ;;; The integration with Gnus is not yet complete.  See various `FIXME'
33 ;;; comments, below, for supplementary explanations or discussions.
34
35 ;;; Several TODO items are marked as such
36
37 ;;; Code:
38
39 (eval-when-compile (require 'cl))
40
41 (require 'gnus-sum)
42
43 (require 'gnus-uu)                      ; because of key prefix issues
44 ;;; for the definitions of group content classification and spam processors
45 (require 'gnus) 
46 (require 'message)              ;for the message-fetch-field functions
47
48 ;; for nnimap-split-download-body-default
49 (eval-when-compile (require 'nnimap))
50
51 ;; autoload executable-find
52 (eval-and-compile
53   ;; executable-find is not autoloaded in Emacs 20
54   (autoload 'executable-find "executable"))
55
56 ;; autoload query-dig
57 (eval-and-compile
58   (autoload 'query-dig "dig"))
59
60 ;; autoload spam-report
61 (eval-and-compile
62   (autoload 'spam-report-gmane "spam-report"))
63
64 ;; autoload gnus-registry
65 (eval-and-compile
66   (autoload 'gnus-registry-group-count "gnus-registry")
67   (autoload 'gnus-registry-add-group "gnus-registry")
68   (autoload 'gnus-registry-store-extra-entry "gnus-registry")
69   (autoload 'gnus-registry-fetch-extra "gnus-registry"))
70
71 ;; autoload query-dns
72 (eval-and-compile
73   (autoload 'query-dns "dns"))
74
75 ;;; Main parameters.
76
77 (defgroup spam nil
78   "Spam configuration.")
79
80 (defcustom spam-directory "~/News/spam/"
81   "Directory for spam whitelists and blacklists."
82   :type 'directory
83   :group 'spam)
84
85 (defcustom spam-move-spam-nonspam-groups-only t
86   "Whether spam should be moved in non-spam groups only.
87 When t, only ham and unclassified groups will have their spam moved
88 to the spam-process-destination.  When nil, spam will also be moved from
89 spam groups."
90   :type 'boolean
91   :group 'spam)
92
93 (defcustom spam-process-ham-in-nonham-groups nil
94   "Whether ham should be processed in non-ham groups."
95   :type 'boolean
96   :group 'spam)
97
98 (defcustom spam-log-to-registry nil
99   "Whether spam/ham processing should be logged in the registry."
100   :type 'boolean
101   :group 'spam)
102
103 (defcustom spam-split-symbolic-return nil
104   "Whether spam-split should work with symbols or group names."
105   :type 'boolean
106   :group 'spam)
107
108 (defcustom spam-split-symbolic-return-positive nil
109   "Whether spam-split should ALWAYS work with symbols or group
110   names.  Do not set this if you use spam-split in a fancy split
111   method."
112   :type 'boolean
113   :group 'spam)
114
115 (defcustom spam-process-ham-in-spam-groups nil
116   "Whether ham should be processed in spam groups."
117   :type 'boolean
118   :group 'spam)
119
120 (defcustom spam-mark-only-unseen-as-spam t
121   "Whether only unseen articles should be marked as spam in spam
122 groups.  When nil, all unread articles in a spam group are marked as
123 spam.  Set this if you want to leave an article unread in a spam group
124 without losing it to the automatic spam-marking process."
125   :type 'boolean
126   :group 'spam)
127
128 (defcustom spam-mark-ham-unread-before-move-from-spam-group nil
129   "Whether ham should be marked unread before it's moved out of a spam
130 group according to ham-process-destination.  This variable is an
131 official entry in the international Longest Variable Name
132 Competition."
133   :type 'boolean
134   :group 'spam)
135
136 (defcustom spam-disable-spam-split-during-ham-respool nil
137   "Whether spam-split should be ignored while resplitting ham in
138 a process destination.  This is useful to prevent ham from ending
139 up in the same spam group after the resplit.  Don't set this to t
140 if you have spam-split as the last rule in your split
141 configuration."
142   :type 'boolean
143   :group 'spam)
144
145 (defcustom spam-autodetect-recheck-messages nil
146   "Should spam.el recheck all meessages when autodetecting?
147 Normally this is nil, so only unseen messages will be checked."
148   :type 'boolean
149   :group 'spam)
150
151 (defcustom spam-whitelist (expand-file-name "whitelist" spam-directory)
152   "The location of the whitelist.
153 The file format is one regular expression per line.
154 The regular expression is matched against the address."
155   :type 'file
156   :group 'spam)
157
158 (defcustom spam-blacklist (expand-file-name "blacklist" spam-directory)
159   "The location of the blacklist.
160 The file format is one regular expression per line.
161 The regular expression is matched against the address."
162   :type 'file
163   :group 'spam)
164
165 (defcustom spam-use-dig t
166   "Whether query-dig should be used instead of query-dns."
167   :type 'boolean
168   :group 'spam)
169
170 (defcustom spam-use-blacklist nil
171   "Whether the blacklist should be used by spam-split."
172   :type 'boolean
173   :group 'spam)
174
175 (defcustom spam-blacklist-ignored-regexes nil
176   "Regular expressions that the blacklist should ignore."
177   :type '(repeat (regexp :tag "Regular expression to ignore when blacklisting"))
178   :group 'spam)
179
180 (defcustom spam-use-whitelist nil
181   "Whether the whitelist should be used by spam-split."
182   :type 'boolean
183   :group 'spam)
184
185 (defcustom spam-use-whitelist-exclusive nil
186   "Whether whitelist-exclusive should be used by spam-split.
187 Exclusive whitelisting means that all messages from senders not in the whitelist
188 are considered spam."
189   :type 'boolean
190   :group 'spam)
191
192 (defcustom spam-use-blackholes nil
193   "Whether blackholes should be used by spam-split."
194   :type 'boolean
195   :group 'spam)
196
197 (defcustom spam-use-hashcash nil
198   "Whether hashcash payments should be detected by spam-split."
199   :type 'boolean
200   :group 'spam)
201
202 (defcustom spam-use-regex-headers nil
203   "Whether a header regular expression match should be used by spam-split.
204 Also see the variables `spam-regex-headers-spam' and `spam-regex-headers-ham'."
205   :type 'boolean
206   :group 'spam)
207
208 (defcustom spam-use-regex-body nil
209   "Whether a body regular expression match should be used by spam-split.
210 Also see the variables `spam-regex-body-spam' and `spam-regex-body-ham'."
211   :type 'boolean
212   :group 'spam)
213
214 (defcustom spam-use-bogofilter-headers nil
215   "Whether bogofilter headers should be used by spam-split.
216 Enable this if you pre-process messages with Bogofilter BEFORE Gnus sees them."
217   :type 'boolean
218   :group 'spam)
219
220 (defcustom spam-use-bogofilter nil
221   "Whether bogofilter should be invoked by spam-split.
222 Enable this if you want Gnus to invoke Bogofilter on new messages."
223   :type 'boolean
224   :group 'spam)
225
226 (defcustom spam-use-BBDB nil
227   "Whether BBDB should be used by spam-split."
228   :type 'boolean
229   :group 'spam)
230
231 (defcustom spam-use-BBDB-exclusive nil
232   "Whether BBDB-exclusive should be used by spam-split.
233 Exclusive BBDB means that all messages from senders not in the BBDB are 
234 considered spam."
235   :type 'boolean
236   :group 'spam)
237
238 (defcustom spam-use-ifile nil
239   "Whether ifile should be used by spam-split."
240   :type 'boolean
241   :group 'spam)
242
243 (defcustom spam-use-stat nil
244   "Whether spam-stat should be used by spam-split."
245   :type 'boolean
246   :group 'spam)
247
248 (defcustom spam-use-spamoracle nil
249   "Whether spamoracle should be used by spam-split."
250   :type 'boolean
251   :group 'spam)
252
253 (defcustom spam-install-hooks (or
254                                spam-use-dig
255                                spam-use-blacklist
256                                spam-use-whitelist 
257                                spam-use-whitelist-exclusive 
258                                spam-use-blackholes 
259                                spam-use-hashcash 
260                                spam-use-regex-headers 
261                                spam-use-regex-body 
262                                spam-use-bogofilter-headers 
263                                spam-use-bogofilter 
264                                spam-use-BBDB 
265                                spam-use-BBDB-exclusive 
266                                spam-use-ifile 
267                                spam-use-stat
268                                spam-use-spamoracle)
269   "Whether the spam hooks should be installed, default to t if one of
270 the spam-use-* variables is set."
271   :group 'spam
272   :type 'boolean)
273
274 (defcustom spam-split-group "spam"
275   "Group name where incoming spam should be put by spam-split."
276   :type 'string
277   :group 'spam)
278
279 ;;; TODO: deprecate this variable, it's confusing since it's a list of strings,
280 ;;; not regular expressions
281 (defcustom spam-junk-mailgroups (cons 
282                                  spam-split-group 
283                                  '("mail.junk" "poste.pourriel"))
284   "Mailgroups with spam contents.
285 All unmarked article in such group receive the spam mark on group entry."
286   :type '(repeat (string :tag "Group"))
287   :group 'spam)
288
289 (defcustom spam-blackhole-servers '("bl.spamcop.net" "relays.ordb.org" 
290                                     "dev.null.dk" "relays.visi.com")
291   "List of blackhole servers."
292   :type '(repeat (string :tag "Server"))
293   :group 'spam)
294
295 (defcustom spam-blackhole-good-server-regex nil
296   "String matching IP addresses that should not be checked in the blackholes"
297   :type '(radio (const nil)
298                 (regexp :format "%t: %v\n" :size 0))
299   :group 'spam)
300
301 (defcustom spam-face 'gnus-splash-face
302   "Face for spam-marked articles"
303   :type 'face
304   :group 'spam)
305
306 (defcustom spam-regex-headers-spam '("^X-Spam-Flag: YES")
307   "Regular expression for positive header spam matches"
308   :type '(repeat (regexp :tag "Regular expression to match spam header"))
309   :group 'spam)
310
311 (defcustom spam-regex-headers-ham '("^X-Spam-Flag: NO")
312   "Regular expression for positive header ham matches"
313   :type '(repeat (regexp :tag "Regular expression to match ham header"))
314   :group 'spam)
315
316 (defcustom spam-regex-body-spam '()
317   "Regular expression for positive body spam matches"
318   :type '(repeat (regexp :tag "Regular expression to match spam body"))
319   :group 'spam)
320
321 (defcustom spam-regex-body-ham '()
322   "Regular expression for positive body ham matches"
323   :type '(repeat (regexp :tag "Regular expression to match ham body"))
324   :group 'spam)
325
326 (defgroup spam-ifile nil
327   "Spam ifile configuration."
328   :group 'spam)
329
330 (defcustom spam-ifile-path (executable-find "ifile")
331   "File path of the ifile executable program."
332   :type '(choice (file :tag "Location of ifile")
333                  (const :tag "ifile is not installed"))
334   :group 'spam-ifile)
335
336 (defcustom spam-ifile-database-path nil
337   "File path of the ifile database."
338   :type '(choice (file :tag "Location of the ifile database")
339                  (const :tag "Use the default"))
340   :group 'spam-ifile)
341
342 (defcustom spam-ifile-spam-category "spam"
343   "Name of the spam ifile category."  
344   :type 'string
345   :group 'spam-ifile)
346
347 (defcustom spam-ifile-ham-category nil
348   "Name of the ham ifile category.  If nil, the current group name will
349 be used."
350   :type '(choice (string :tag "Use a fixed category")
351                  (const :tag "Use the current group name"))
352   :group 'spam-ifile)
353
354 (defcustom spam-ifile-all-categories nil
355   "Whether the ifile check will return all categories, or just spam.
356 Set this to t if you want to use the spam-split invocation of ifile as
357 your main source of newsgroup names."
358   :type 'boolean
359   :group 'spam-ifile)
360
361 (defgroup spam-bogofilter nil
362   "Spam bogofilter configuration."
363   :group 'spam)
364
365 (defcustom spam-bogofilter-path (executable-find "bogofilter")
366   "File path of the Bogofilter executable program."
367   :type '(choice (file :tag "Location of bogofilter")
368                  (const :tag "Bogofilter is not installed"))
369   :group 'spam-bogofilter)
370
371 (defcustom spam-bogofilter-header "X-Bogosity"
372   "The header that Bogofilter inserts in messages."
373   :type 'string
374   :group 'spam-bogofilter)
375
376 (defcustom spam-bogofilter-spam-switch "-s"
377   "The switch that Bogofilter uses to register spam messages."
378   :type 'string
379   :group 'spam-bogofilter)
380
381 (defcustom spam-bogofilter-ham-switch "-n"
382   "The switch that Bogofilter uses to register ham messages."
383   :type 'string
384   :group 'spam-bogofilter)
385
386 (defcustom spam-bogofilter-spam-strong-switch "-S"
387   "The switch that Bogofilter uses to unregister ham messages."
388   :type 'string
389   :group 'spam-bogofilter)
390
391 (defcustom spam-bogofilter-ham-strong-switch "-N"
392   "The switch that Bogofilter uses to unregister spam messages."
393   :type 'string
394   :group 'spam-bogofilter)
395
396 (defcustom spam-bogofilter-bogosity-positive-spam-header "^\\(Yes\\|Spam\\)"
397   "The regex on `spam-bogofilter-header' for positive spam identification."
398   :type 'regexp
399   :group 'spam-bogofilter)
400
401 (defcustom spam-bogofilter-database-directory nil
402   "Directory path of the Bogofilter databases."
403   :type '(choice (directory 
404                   :tag "Location of the Bogofilter database directory")
405                  (const :tag "Use the default"))
406   :group 'spam-bogofilter)
407
408 (defgroup spam-spamoracle nil
409   "Spam spamoracle configuration."
410   :group 'spam)
411
412 (defcustom spam-spamoracle-database nil 
413   "Location of spamoracle database file. When nil, use the default
414 spamoracle database."
415   :type '(choice (directory :tag "Location of spamoracle database file.")
416                  (const :tag "Use the default"))
417   :group 'spam-spamoracle)
418
419 (defcustom spam-spamoracle-binary (executable-find "spamoracle")
420   "Location of the spamoracle binary."
421   :type '(choice (directory :tag "Location of the spamoracle binary")
422                  (const :tag "Use the default"))
423   :group 'spam-spamoracle)
424
425 ;;; Key bindings for spam control.
426
427 (gnus-define-keys gnus-summary-mode-map
428   "St" spam-bogofilter-score
429   "Sx" gnus-summary-mark-as-spam
430   "Mst" spam-bogofilter-score
431   "Msx" gnus-summary-mark-as-spam
432   "\M-d" gnus-summary-mark-as-spam)
433
434 (defvar spam-old-ham-articles nil
435   "List of old ham articles, generated when a group is entered.")
436
437 (defvar spam-old-spam-articles nil
438   "List of old spam articles, generated when a group is entered.")
439
440 (defvar spam-split-disabled nil
441   "If non-nil, spam-split is disabled, and always returns nil.")
442
443 (defvar spam-split-last-successful-check nil
444   "spam-split will set this to nil or a spam-use-XYZ check if it
445   finds ham or spam.")
446
447 ;; convenience functions
448 (defun spam-xor (a b) ; logical exclusive or
449   (and (or a b) (not (and a b))))
450
451 (defun spam-group-ham-mark-p (group mark &optional spam)
452   (when (stringp group)
453     (let* ((marks (spam-group-ham-marks group spam))
454            (marks (if (symbolp mark) 
455                       marks 
456                     (mapcar 'symbol-value marks))))
457       (memq mark marks))))
458
459 (defun spam-group-spam-mark-p (group mark)
460   (spam-group-ham-mark-p group mark t))
461
462 (defun spam-group-ham-marks (group &optional spam)
463   (when (stringp group)
464     (let* ((marks (if spam
465                       (gnus-parameter-spam-marks group)
466                     (gnus-parameter-ham-marks group)))
467            (marks (car marks))
468            (marks (if (listp (car marks)) (car marks) marks)))
469       marks)))
470
471 (defun spam-group-spam-marks (group)
472   (spam-group-ham-marks group t))
473
474 (defun spam-group-spam-contents-p (group)
475   (if (stringp group)
476       (or (member group spam-junk-mailgroups)
477           (memq 'gnus-group-spam-classification-spam 
478                 (gnus-parameter-spam-contents group)))
479     nil))
480   
481 (defun spam-group-ham-contents-p (group)
482   (if (stringp group)
483       (memq 'gnus-group-spam-classification-ham 
484             (gnus-parameter-spam-contents group))
485     nil))
486
487 (defvar spam-list-of-processors
488   '((gnus-group-spam-exit-processor-report-gmane spam spam-use-gmane)
489     (gnus-group-spam-exit-processor-bogofilter   spam spam-use-bogofilter)
490     (gnus-group-spam-exit-processor-blacklist    spam spam-use-blacklist)
491     (gnus-group-spam-exit-processor-ifile        spam spam-use-ifile)
492     (gnus-group-spam-exit-processor-stat         spam spam-use-stat)
493     (gnus-group-spam-exit-processor-spamoracle   spam spam-use-spamoracle)
494     (gnus-group-ham-exit-processor-ifile         ham spam-use-ifile)
495     (gnus-group-ham-exit-processor-bogofilter    ham spam-use-bogofilter)
496     (gnus-group-ham-exit-processor-stat          ham spam-use-stat)
497     (gnus-group-ham-exit-processor-whitelist     ham spam-use-whitelist)
498     (gnus-group-ham-exit-processor-BBDB          ham spam-use-BBDB)
499     (gnus-group-ham-exit-processor-copy          ham spam-use-ham-copy)
500     (gnus-group-ham-exit-processor-spamoracle    ham spam-use-spamoracle))
501   "The spam-list-of-processors list contains pairs associating a
502 ham/spam exit processor variable with a classification and a
503 spam-use-* variable.")
504
505 (defun spam-group-processor-p (group processor)
506   (if (and (stringp group)
507            (symbolp processor))
508       (or (member processor (nth 0 (gnus-parameter-spam-process group)))
509           (spam-group-processor-multiple-p 
510            group 
511            (cdr-safe (assoc processor spam-list-of-processors))))
512     nil))
513
514 (defun spam-group-processor-multiple-p (group processor-info)
515   (let* ((classification (nth 0 processor-info))
516          (check (nth 1 processor-info))
517          (parameters (nth 0 (gnus-parameter-spam-process group)))
518          found)
519     (dolist (parameter parameters)
520       (when (and (null found)
521                  (listp parameter)
522                  (eq classification (nth 0 parameter))
523                  (eq check (nth 1 parameter)))
524         (setq found t)))
525     found))
526
527 (defun spam-group-spam-processor-report-gmane-p (group)
528   (spam-group-processor-p group 'gnus-group-spam-exit-processor-report-gmane))
529
530 (defun spam-group-spam-processor-bogofilter-p (group)
531   (spam-group-processor-p group 'gnus-group-spam-exit-processor-bogofilter))
532
533 (defun spam-group-spam-processor-blacklist-p (group)
534   (spam-group-processor-p group 'gnus-group-spam-exit-processor-blacklist))
535
536 (defun spam-group-spam-processor-ifile-p (group)
537   (spam-group-processor-p group 'gnus-group-spam-exit-processor-ifile))
538
539 (defun spam-group-ham-processor-ifile-p (group)
540   (spam-group-processor-p group 'gnus-group-ham-exit-processor-ifile))
541
542 (defun spam-group-spam-processor-spamoracle-p (group)
543   (spam-group-processor-p group 'gnus-group-spam-exit-processor-spamoracle))
544
545 (defun spam-group-ham-processor-bogofilter-p (group)
546   (spam-group-processor-p group 'gnus-group-ham-exit-processor-bogofilter))
547
548 (defun spam-group-spam-processor-stat-p (group)
549   (spam-group-processor-p group 'gnus-group-spam-exit-processor-stat))
550
551 (defun spam-group-ham-processor-stat-p (group)
552   (spam-group-processor-p group 'gnus-group-ham-exit-processor-stat))
553
554 (defun spam-group-ham-processor-whitelist-p (group)
555   (spam-group-processor-p group 'gnus-group-ham-exit-processor-whitelist))
556
557 (defun spam-group-ham-processor-BBDB-p (group)
558   (spam-group-processor-p group 'gnus-group-ham-exit-processor-BBDB))
559
560 (defun spam-group-ham-processor-copy-p (group)
561   (spam-group-processor-p group 'gnus-group-ham-exit-processor-copy))
562
563 (defun spam-group-ham-processor-spamoracle-p (group)
564   (spam-group-processor-p group 'gnus-group-ham-exit-processor-spamoracle))
565
566 ;;; Summary entry and exit processing.
567
568 (defun spam-summary-prepare ()
569   (setq spam-old-ham-articles 
570         (spam-list-articles gnus-newsgroup-articles 'ham))
571   (setq spam-old-spam-articles 
572         (spam-list-articles gnus-newsgroup-articles 'spam))
573   (spam-mark-junk-as-spam-routine))
574
575 ;; The spam processors are invoked for any group, spam or ham or neither
576 (defun spam-summary-prepare-exit ()
577   (unless gnus-group-is-exiting-without-update-p
578     (gnus-message 6 "Exiting summary buffer and applying spam rules")
579
580     ;; first of all, unregister any articles that are no longer ham or spam
581     ;; we have to iterate over the processors, or else we'll be too slow
582     (dolist (classification '(spam ham))
583       (let* ((old-articles (if (eq classification 'spam)
584                                spam-old-spam-articles 
585                              spam-old-ham-articles))
586              (new-articles (spam-list-articles 
587                             gnus-newsgroup-articles 
588                             classification))
589              (changed-articles (gnus-set-difference old-articles new-articles)))
590         ;; now that we have the changed articles, we go through the processors
591         (dolist (processor-param spam-list-of-processors)
592           (let ((processor (nth 0 processor-param))
593                 (processor-classification (nth 1 processor-param))
594                 (check (nth 2 processor-param))
595                 unregister-list)
596             (dolist (article changed-articles)
597               (let ((id (spam-fetch-field-message-id-fast article)))
598                 (when (spam-log-unregistration-needed-p 
599                        id 'process classification check)
600                   (push article unregister-list))))
601             ;; call spam-register-routine with specific articles to unregister,
602             ;; when there are articles to unregister and the check is enabled
603             (when (and unregister-list (symbol-value check))
604               (spam-register-routine classification check t unregister-list))))))
605       
606     ;; find all the spam processors applicable to this group
607     (dolist (processor-param spam-list-of-processors)
608       (let ((processor (nth 0 processor-param))
609             (classification (nth 1 processor-param))
610             (check (nth 2 processor-param)))
611         (when (and (eq 'spam classification)
612                    (spam-group-processor-p gnus-newsgroup-name processor))
613           (spam-register-routine classification check))))
614
615     (if spam-move-spam-nonspam-groups-only      
616         (when (not (spam-group-spam-contents-p gnus-newsgroup-name))
617           (spam-mark-spam-as-expired-and-move-routine
618            (gnus-parameter-spam-process-destination gnus-newsgroup-name)))
619       (gnus-message 5 "Marking spam as expired and moving it to %s" 
620                     gnus-newsgroup-name)
621       (spam-mark-spam-as-expired-and-move-routine 
622        (gnus-parameter-spam-process-destination gnus-newsgroup-name)))
623
624     ;; now we redo spam-mark-spam-as-expired-and-move-routine to only
625     ;; expire spam, in case the above did not expire them
626     (gnus-message 5 "Marking spam as expired without moving it")
627     (spam-mark-spam-as-expired-and-move-routine nil)
628
629     (when (or (spam-group-ham-contents-p gnus-newsgroup-name)
630               (and (spam-group-spam-contents-p gnus-newsgroup-name)
631                    spam-process-ham-in-spam-groups)
632               spam-process-ham-in-nonham-groups)
633       ;; find all the ham processors applicable to this group
634       (dolist (processor-param spam-list-of-processors)
635         (let ((processor (nth 0 processor-param))
636               (classification (nth 1 processor-param))
637               (check (nth 2 processor-param)))
638           (when (and (eq 'ham classification)
639                      (spam-group-processor-p gnus-newsgroup-name processor))
640             (spam-register-routine classification check)))))
641
642     (when (spam-group-ham-processor-copy-p gnus-newsgroup-name)
643       (gnus-message 5 "Copying ham")
644       (spam-ham-copy-routine
645        (gnus-parameter-ham-process-destination gnus-newsgroup-name)))
646
647     ;; now move all ham articles out of spam groups
648     (when (spam-group-spam-contents-p gnus-newsgroup-name)
649       (gnus-message 5 "Moving ham messages from spam group")
650       (spam-ham-move-routine
651        (gnus-parameter-ham-process-destination gnus-newsgroup-name))))
652
653   (setq spam-old-ham-articles nil)
654   (setq spam-old-spam-articles nil))
655
656 (defun spam-mark-junk-as-spam-routine ()
657   ;; check the global list of group names spam-junk-mailgroups and the
658   ;; group parameters
659   (when (spam-group-spam-contents-p gnus-newsgroup-name)
660     (gnus-message 5 "Marking %s articles as spam"
661                   (if spam-mark-only-unseen-as-spam 
662                       "unseen"
663                     "unread"))
664     (let ((articles (if spam-mark-only-unseen-as-spam 
665                         gnus-newsgroup-unseen
666                       gnus-newsgroup-unreads)))
667       (dolist (article articles)
668         (gnus-summary-mark-article article gnus-spam-mark)))))
669
670 (defun spam-mark-spam-as-expired-and-move-routine (&rest groups)
671   (if (and (car-safe groups) (listp (car-safe groups)))
672       (apply 'spam-mark-spam-as-expired-and-move-routine (car groups))
673     (gnus-summary-kill-process-mark)
674     (let ((articles gnus-newsgroup-articles)
675           (backend-supports-deletions
676            (gnus-check-backend-function
677             'request-move-article gnus-newsgroup-name))
678           article tomove deletep)
679       (dolist (article articles)
680         (when (eq (gnus-summary-article-mark article) gnus-spam-mark)
681           (gnus-summary-mark-article article gnus-expirable-mark)
682           (push article tomove)))
683     
684       ;; now do the actual copies
685       (dolist (group groups)
686         (when (and tomove
687                    (stringp group))
688           (dolist (article tomove)
689             (gnus-summary-set-process-mark article))
690           (when tomove
691             (if (or (not backend-supports-deletions)
692                     (> (length groups) 1))
693                 (progn 
694                   (gnus-summary-copy-article nil group)
695                   (setq deletep t))
696               (gnus-summary-move-article nil group)))))
697     
698       ;; now delete the articles, if there was a copy done, and the
699       ;; backend allows it
700       (when (and deletep backend-supports-deletions)
701         (dolist (article tomove)
702           (gnus-summary-set-process-mark article))
703         (when tomove
704           (let ((gnus-novice-user nil)) ; don't ask me if I'm sure
705             (gnus-summary-delete-article nil))))
706     
707       (gnus-summary-yank-process-mark))))
708  
709 (defun spam-ham-copy-or-move-routine (copy groups)
710   (gnus-summary-kill-process-mark)
711   (let ((todo (spam-list-articles gnus-newsgroup-articles 'ham))
712         (backend-supports-deletions
713          (gnus-check-backend-function
714           'request-move-article gnus-newsgroup-name))
715         (respool-method (gnus-find-method-for-group gnus-newsgroup-name))
716         article mark todo deletep respool)
717
718     (when (member 'respool groups)
719       (setq respool t)                  ; boolean for later
720       (setq groups '("fake"))) ; when respooling, groups are dynamic so fake it
721
722     ;; now do the actual move
723     (dolist (group groups)
724       (when (and todo (stringp group))
725         (dolist (article todo)
726           (when spam-mark-ham-unread-before-move-from-spam-group
727             (gnus-summary-mark-article article gnus-unread-mark))
728           (gnus-summary-set-process-mark article))
729
730         (if respool                        ; respooling is with a "fake" group
731             (let ((spam-split-disabled
732                    (or spam-split-disabled
733                        spam-disable-spam-split-during-ham-respool)))
734               (gnus-summary-respool-article nil respool-method))
735           (if (or (not backend-supports-deletions) ; else, we are not respooling
736                   (> (length groups) 1))
737               (progn                ; if copying, copy and set deletep
738                 (gnus-summary-copy-article nil group)
739                 (setq deletep t))
740             (gnus-summary-move-article nil group))))) ; else move articles
741     
742     ;; now delete the articles, unless a) copy is t, and there was a copy done
743     ;;                                 b) a move was done to a single group
744     ;;                                 c) backend-supports-deletions is nil
745     (unless copy
746       (when (and deletep backend-supports-deletions)
747         (dolist (article todo)
748           (gnus-summary-set-process-mark article))
749         (when todo
750           (let ((gnus-novice-user nil)) ; don't ask me if I'm sure
751             (gnus-summary-delete-article nil))))))
752   
753   (gnus-summary-yank-process-mark))
754  
755 (defun spam-ham-copy-routine (&rest groups)
756   (if (and (car-safe groups) (listp (car-safe groups)))
757       (apply 'spam-ham-copy-routine (car groups))
758     (spam-ham-copy-or-move-routine t groups)))
759  
760 (defun spam-ham-move-routine (&rest groups)
761   (if (and (car-safe groups) (listp (car-safe groups)))
762       (apply 'spam-ham-move-routine (car groups))
763     (spam-ham-copy-or-move-routine nil groups)))
764  
765 (eval-and-compile
766   (defalias 'spam-point-at-eol (if (fboundp 'point-at-eol)
767                                    'point-at-eol
768                                  'line-end-position)))
769
770 (defun spam-get-article-as-string (article)
771   (let ((article-buffer (spam-get-article-as-buffer article))
772         article-string)
773     (when article-buffer
774       (save-window-excursion
775         (set-buffer article-buffer)
776         (setq article-string (buffer-string))))
777     article-string))
778
779 (defun spam-get-article-as-buffer (article)
780   (let ((article-buffer))
781     (when (numberp article)
782       (save-window-excursion
783         (gnus-summary-goto-subject article)
784         (gnus-summary-show-article t)
785         (setq article-buffer (get-buffer gnus-article-buffer))))
786     article-buffer))
787
788 ;; disabled for now
789 ;; (defun spam-get-article-as-filename (article)
790 ;;   (let ((article-filename))
791 ;;     (when (numberp article)
792 ;;       (nnml-possibly-change-directory 
793 ;;        (gnus-group-real-name gnus-newsgroup-name))
794 ;;       (setq article-filename (expand-file-name 
795 ;;                              (int-to-string article) nnml-current-directory)))
796 ;;     (if (file-exists-p article-filename)
797 ;;      article-filename
798 ;;       nil)))
799
800 (defun spam-fetch-field-from-fast (article)
801   "Fetch the `from' field quickly, using the internal gnus-data-list function"
802   (if (and (numberp article)
803            (assoc article (gnus-data-list nil)))
804       (mail-header-from 
805        (gnus-data-header (assoc article (gnus-data-list nil))))
806     nil))
807
808 (defun spam-fetch-field-subject-fast (article)
809   "Fetch the `subject' field quickly, using the internal
810   gnus-data-list function"
811   (if (and (numberp article)
812            (assoc article (gnus-data-list nil)))
813       (mail-header-subject 
814        (gnus-data-header (assoc article (gnus-data-list nil))))
815     nil))
816
817 (defun spam-fetch-field-message-id-fast (article)
818   "Fetch the `Message-ID' field quickly, using the internal
819   gnus-data-list function"
820   (if (and (numberp article)
821            (assoc article (gnus-data-list nil)))
822       (mail-header-message-id 
823        (gnus-data-header (assoc article (gnus-data-list nil))))
824     nil))
825
826 \f
827 ;;;; Spam determination.
828
829 (defvar spam-list-of-checks
830   '((spam-use-blacklist          . spam-check-blacklist)
831     (spam-use-regex-headers      . spam-check-regex-headers)
832     (spam-use-regex-body         . spam-check-regex-body)
833     (spam-use-whitelist          . spam-check-whitelist)
834     (spam-use-BBDB               . spam-check-BBDB)
835     (spam-use-ifile              . spam-check-ifile)
836     (spam-use-spamoracle         . spam-check-spamoracle)
837     (spam-use-stat               . spam-check-stat)
838     (spam-use-blackholes         . spam-check-blackholes)
839     (spam-use-hashcash           . spam-check-hashcash)
840     (spam-use-bogofilter-headers . spam-check-bogofilter-headers)
841     (spam-use-bogofilter         . spam-check-bogofilter))
842   "The spam-list-of-checks list contains pairs associating a
843 parameter variable with a spam checking function.  If the
844 parameter variable is true, then the checking function is called,
845 and its value decides what happens.  Each individual check may
846 return nil, t, or a mailgroup name.  The value nil means that the
847 check does not yield a decision, and so, that further checks are
848 needed.  The value t means that the message is definitely not
849 spam, and that further spam checks should be inhibited.
850 Otherwise, a mailgroup name or the symbol 'spam (depending on
851 spam-split-symbolic-return) is returned where the mail should go,
852 and further checks are also inhibited.  The usual mailgroup name
853 is the value of `spam-split-group', meaning that the message is
854 definitely a spam.")
855
856 (defvar spam-list-of-statistical-checks 
857   '(spam-use-ifile
858     spam-use-regex-body 
859     spam-use-stat 
860     spam-use-bogofilter
861     spam-use-spamoracle)
862   "The spam-list-of-statistical-checks list contains all the mail
863 splitters that need to have the full message body available.")
864
865 ;;;TODO: modify to invoke self with each check if invoked without specifics
866 (defun spam-split (&rest specific-checks)
867   "Split this message into the `spam' group if it is spam.
868 This function can be used as an entry in `nnmail-split-fancy',
869 for example like this: (: spam-split).  It can take checks as
870 parameters.  A string as a parameter will set the
871 spam-split-group to that string.
872
873 See the Info node `(gnus)Fancy Mail Splitting' for more details."
874   (interactive)
875   (setq spam-split-last-successful-check nil)
876   (unless spam-split-disabled
877     (let ((spam-split-group-choice spam-split-group))
878       (dolist (check specific-checks)
879         (when (stringp check)
880           (setq spam-split-group-choice check)
881           (setq specific-checks (delq check specific-checks))))
882       
883       (let ((spam-split-group spam-split-group-choice))
884         (save-excursion
885           (save-restriction
886             (dolist (check spam-list-of-statistical-checks)
887               (when (and (symbolp check) (symbol-value check))
888                 (widen)
889                 (gnus-message 8 "spam-split: widening the buffer (%s requires it)"
890                               (symbol-name check))
891                 (return)))
892             ;;   (progn (widen) (debug (buffer-string)))
893             (let ((list-of-checks spam-list-of-checks)
894                   decision)
895               (while (and list-of-checks (not decision))
896                 (let ((pair (pop list-of-checks)))
897                   (when (and (symbol-value (car pair))
898                              (or (null specific-checks)
899                                  (memq (car pair) specific-checks)))
900                     (gnus-message 5 "spam-split: calling the %s function" 
901                                   (symbol-name (cdr pair)))
902                     (setq decision (funcall (cdr pair)))
903                     ;; if we got a decision at all, save the current check
904                     (when decision
905                       (setq spam-split-last-successful-check (car pair)))
906
907                     (when (eq decision 'spam)
908                       (if spam-split-symbolic-return
909                           (setq decision spam-split-group)
910                         (gnus-error
911                          5 
912                          (format "spam-split got %s but %s is nil"
913                                  (symbol-name decision)
914                                  (symbol-name spam-split-symbolic-return))))))))
915               (if (eq decision t)
916                   (if spam-split-symbolic-return-positive 'ham nil)
917                 decision))))))))
918
919 (defun spam-find-spam ()
920   "This function will detect spam in the current newsgroup using spam-split"
921   (interactive)
922   
923   (let* ((group gnus-newsgroup-name)
924          (autodetect (gnus-parameter-spam-autodetect group))
925          (methods (gnus-parameter-spam-autodetect-methods group))
926          (first-method (nth 0 methods)))
927   (when (and autodetect 
928              (not (equal first-method 'none)))
929     (mapcar
930      (lambda (article)
931        (let ((id (spam-fetch-field-message-id-fast article))
932              (subject (spam-fetch-field-subject-fast article))
933              (sender (spam-fetch-field-from-fast article)))
934          (unless (and spam-log-to-registry
935                       (spam-log-registered-p id 'incoming))
936            (let* ((spam-split-symbolic-return t)
937                   (spam-split-symbolic-return-positive t)
938                   (split-return
939                    (with-temp-buffer
940                      (gnus-request-article-this-buffer 
941                       article 
942                       group)
943                      (if (or (null first-method)
944                              (equal first-method 'default))
945                          (spam-split)
946                        (apply 'spam-split methods)))))
947              (if (equal split-return 'spam)
948                  (gnus-summary-mark-article article gnus-spam-mark))
949
950              (when (and split-return spam-log-to-registry)
951                (when (zerop (gnus-registry-group-count id))
952                  (gnus-registry-add-group
953                   id group subject sender))
954
955                (spam-log-processing-to-registry 
956                 id
957                 'incoming
958                 split-return
959                 spam-split-last-successful-check
960                 group))))))
961      (if spam-autodetect-recheck-messages
962          gnus-newsgroup-articles
963        gnus-newsgroup-unseen)))))
964
965 (defvar spam-registration-functions
966   ;; first the ham register, second the spam register function
967   ;; third the ham unregister, fourth the spam unregister function
968   '((spam-use-blacklist  nil 
969                          spam-blacklist-register-routine
970                          nil
971                          spam-blacklist-unregister-routine)
972     (spam-use-whitelist  spam-whitelist-register-routine
973                          nil
974                          spam-whitelist-unregister-routine
975                          nil)
976     (spam-use-BBDB       spam-BBDB-register-routine 
977                          nil
978                          spam-BBDB-unregister-routine 
979                          nil)
980     (spam-use-ifile      spam-ifile-register-ham-routine 
981                          spam-ifile-register-spam-routine
982                          spam-ifile-unregister-ham-routine 
983                          spam-ifile-unregister-spam-routine)
984     (spam-use-spamoracle spam-spamoracle-learn-ham 
985                          spam-spamoracle-learn-spam
986                          spam-spamoracle-unlearn-ham 
987                          spam-spamoracle-unlearn-spam)
988     (spam-use-stat       spam-stat-register-ham-routine 
989                          spam-stat-register-spam-routine
990                          spam-stat-unregister-ham-routine 
991                          spam-stat-unregister-spam-routine)
992     ;; note that spam-use-gmane is not a legitimate check
993     (spam-use-gmane      nil 
994                          spam-report-gmane-register-routine
995                          ;; does Gmane support unregistration?
996                          nil
997                          nil)
998     (spam-use-bogofilter spam-bogofilter-register-ham-routine 
999                          spam-bogofilter-register-spam-routine
1000                          spam-bogofilter-unregister-ham-routine 
1001                          spam-bogofilter-unregister-spam-routine))
1002   "The spam-registration-functions list contains pairs
1003 associating a parameter variable with the ham and spam
1004 registration functions, and the ham and spam unregistration
1005 functions")
1006
1007 (defun spam-classification-valid-p (classification)
1008   (or  (eq classification 'spam)
1009        (eq classification 'ham)))
1010
1011 (defun spam-process-type-valid-p (process-type)
1012   (or  (eq process-type 'incoming)
1013        (eq process-type 'process)))
1014
1015 (defun spam-registration-check-valid-p (check)
1016   (assoc check spam-registration-functions))
1017
1018 (defun spam-unregistration-check-valid-p (check)
1019   (assoc check spam-registration-functions))
1020
1021 (defun spam-registration-function (classification check)
1022   (let ((flist (cdr-safe (assoc check spam-registration-functions))))
1023     (if (eq classification 'spam)
1024         (nth 1 flist)
1025       (nth 0 flist))))
1026
1027 (defun spam-unregistration-function (classification check)
1028   (let ((flist (cdr-safe (assoc check spam-registration-functions))))
1029     (if (eq classification 'spam)
1030         (nth 3 flist)
1031       (nth 2 flist))))
1032
1033 (defun spam-list-articles (articles classification)
1034   (let ((mark-check (if (eq classification 'spam) 
1035                         'spam-group-spam-mark-p 
1036                       'spam-group-ham-mark-p))
1037         list mark-cache-yes mark-cache-no)
1038     (dolist (article articles)
1039       (let ((mark (gnus-summary-article-mark article)))
1040         (unless (memq mark mark-cache-no)
1041           (if (memq mark mark-cache-yes)
1042               (push article list)
1043             ;; else, we have to actually check the mark
1044             (if (funcall mark-check
1045                          gnus-newsgroup-name 
1046                          mark)
1047                 (progn
1048                   (push article list)
1049                   (push mark mark-cache-yes))
1050               (push mark mark-cache-no))))))
1051     list))
1052
1053 (defun spam-register-routine (classification 
1054                               check 
1055                               &optional unregister 
1056                               specific-articles)
1057   (when (and (spam-classification-valid-p classification)
1058              (spam-registration-check-valid-p check))
1059     (let* ((register-function
1060             (spam-registration-function classification check))
1061            (unregister-function
1062             (spam-unregistration-function classification check))
1063            (run-function (if unregister 
1064                              unregister-function 
1065                            register-function))
1066            (log-function (if unregister
1067                              'spam-log-undo-registration
1068                            'spam-log-processing-to-registry))
1069            article articles)
1070
1071       (when run-function
1072         ;; make list of articles, using specific-articles if given
1073         (setq articles (or specific-articles
1074                            (spam-list-articles 
1075                             gnus-newsgroup-articles 
1076                             classification)))
1077         ;; process them
1078         (gnus-message 5 "%s %d %s articles with classification %s, check %s"
1079                       (if unregister "Unregistering" "Registering")
1080                       (length articles)
1081                       (if specific-articles "specific" "")
1082                       (symbol-name classification)
1083                       (symbol-name check))
1084         (funcall run-function articles)
1085         ;; now log all the registrations (or undo them, depending on unregister)
1086         (dolist (article articles)
1087           (funcall log-function
1088                    (spam-fetch-field-message-id-fast article)
1089                    'process
1090                    classification
1091                    check
1092                    gnus-newsgroup-name))))))
1093
1094 ;;; log a ham- or spam-processor invocation to the registry
1095 (defun spam-log-processing-to-registry (id type classification check group)
1096   (when spam-log-to-registry
1097     (if (and (stringp id)
1098              (stringp group)
1099              (spam-process-type-valid-p type)
1100              (spam-classification-valid-p classification)
1101              (spam-registration-check-valid-p check))
1102         (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type)))
1103               (cell (list classification check group)))
1104           (push cell cell-list)
1105           (gnus-registry-store-extra-entry
1106            id
1107            type
1108            cell-list))
1109
1110       (gnus-message 5 (format "%s called with bad ID, type, classification, check, or group"
1111                               "spam-log-processing-to-registry")))))
1112
1113 ;;; check if a ham- or spam-processor registration has been done
1114 (defun spam-log-registered-p (id type)
1115   (when spam-log-to-registry
1116     (if (and (stringp id)
1117              (spam-process-type-valid-p type))
1118         (cdr-safe (gnus-registry-fetch-extra id type))
1119       (progn 
1120         (gnus-message 5 (format "%s called with bad ID, type, classification, or check"
1121                                 "spam-log-registered-p"))
1122         nil))))
1123
1124 ;;; check if a ham- or spam-processor registration needs to be undone
1125 (defun spam-log-unregistration-needed-p (id type classification check)
1126   (when spam-log-to-registry
1127     (if (and (stringp id)
1128              (spam-process-type-valid-p type)
1129              (spam-classification-valid-p classification)
1130              (spam-registration-check-valid-p check))
1131         (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type)))
1132               found)
1133           (dolist (cell cell-list)
1134             (unless found
1135               (when (and (eq classification (nth 0 cell))
1136                          (eq check (nth 1 cell)))
1137                 (setq found t))))
1138           found)
1139       (progn 
1140         (gnus-message 5 (format "%s called with bad ID, type, classification, or check"
1141                                 "spam-log-unregistration-needed-p"))
1142         nil))))
1143
1144
1145 ;;; undo a ham- or spam-processor registration (the group is not used)
1146 (defun spam-log-undo-registration (id type classification check &optional group)
1147   (when (and spam-log-to-registry
1148              (spam-log-unregistration-needed-p id type classification check))
1149     (if (and (stringp id)
1150              (spam-process-type-valid-p type)
1151              (spam-classification-valid-p classification)
1152              (spam-registration-check-valid-p check))
1153         (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type)))
1154               new-cell-list found)
1155           (dolist (cell cell-list)
1156             (unless (and (eq classification (nth 0 cell))
1157                          (eq check (nth 1 cell)))
1158               (push cell new-cell-list)))
1159           (gnus-registry-store-extra-entry
1160            id
1161            type
1162            new-cell-list))
1163       (progn 
1164         (gnus-message 5 (format "%s called with bad ID, type, check, or group"
1165                                 "spam-log-undo-registration"))
1166         nil))))
1167
1168 ;;; set up IMAP widening if it's necessary  
1169 (defun spam-setup-widening ()
1170   (dolist (check spam-list-of-statistical-checks)
1171     (when (symbol-value check)
1172       (setq nnimap-split-download-body-default t))))
1173
1174 \f
1175 ;;;; Regex body
1176
1177 (defun spam-check-regex-body ()
1178   (let ((spam-regex-headers-ham spam-regex-body-ham)
1179         (spam-regex-headers-spam spam-regex-body-spam))
1180     (spam-check-regex-headers t)))
1181
1182 \f
1183 ;;;; Regex headers
1184
1185 (defun spam-check-regex-headers (&optional body)
1186   (let ((type (if body "body" "header"))
1187         (spam-split-group (if spam-split-symbolic-return
1188                               'spam 
1189                             spam-split-group))
1190         ret found)
1191     (dolist (h-regex spam-regex-headers-ham)
1192       (unless found
1193         (goto-char (point-min))
1194         (when (re-search-forward h-regex nil t)
1195           (message "Ham regex %s search positive." type)
1196           (setq found t))))
1197     (dolist (s-regex spam-regex-headers-spam)
1198       (unless found
1199         (goto-char (point-min))
1200         (when (re-search-forward s-regex nil t)
1201           (message "Spam regex %s search positive." type)
1202           (setq found t)
1203           (setq ret spam-split-group))))
1204     ret))
1205
1206 \f
1207 ;;;; Blackholes.
1208
1209 (defun spam-reverse-ip-string (ip)
1210   (when (stringp ip)
1211     (mapconcat 'identity
1212                (nreverse (split-string ip "\\."))
1213                ".")))
1214
1215 (defun spam-check-blackholes ()
1216   "Check the Received headers for blackholed relays."
1217   (let ((headers (nnmail-fetch-field "received"))
1218         (spam-split-group (if spam-split-symbolic-return
1219                               'spam 
1220                             spam-split-group))
1221         ips matches)
1222     (when headers
1223       (with-temp-buffer
1224         (insert headers)
1225         (goto-char (point-min))
1226         (gnus-message 5 "Checking headers for relay addresses")
1227         (while (re-search-forward
1228                 "\\([0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+\\)" nil t)
1229           (gnus-message 9 "Blackhole search found host IP %s." (match-string 1))
1230           (push (spam-reverse-ip-string (match-string 1))
1231                 ips)))
1232       (dolist (server spam-blackhole-servers)
1233         (dolist (ip ips)
1234           (unless (and spam-blackhole-good-server-regex
1235                        ;; match the good-server-regex against the reversed (again) IP string
1236                        (string-match 
1237                         spam-blackhole-good-server-regex
1238                         (spam-reverse-ip-string ip)))
1239             (unless matches
1240               (let ((query-string (concat ip "." server)))
1241                 (if spam-use-dig
1242                     (let ((query-result (query-dig query-string)))
1243                       (when query-result
1244                         (gnus-message 5 "(DIG): positive blackhole check '%s'" 
1245                                       query-result)
1246                         (push (list ip server query-result)
1247                               matches)))
1248                   ;; else, if not using dig.el
1249                   (when (query-dns query-string)
1250                     (gnus-message 5 "positive blackhole check")
1251                     (push (list ip server (query-dns query-string 'TXT))
1252                           matches)))))))))
1253     (when matches
1254       spam-split-group)))
1255 \f
1256 ;;;; Hashcash.
1257
1258 (condition-case nil
1259     (progn
1260       (require 'hashcash)
1261       
1262       (defun spam-check-hashcash ()
1263         "Check the headers for hashcash payments."
1264         (mail-check-payment)))   ;mail-check-payment returns a boolean
1265
1266   (file-error (progn
1267                 (defalias 'mail-check-payment 'ignore)
1268                 (defalias 'spam-check-hashcash 'ignore))))
1269 \f
1270 ;;;; BBDB 
1271
1272 ;;; original idea for spam-check-BBDB from Alexander Kotelnikov
1273 ;;; <sacha@giotto.sj.ru>
1274
1275 ;; all this is done inside a condition-case to trap errors
1276
1277 (condition-case nil
1278     (progn
1279       (require 'bbdb)
1280       (require 'bbdb-com)
1281       
1282       (defun spam-enter-ham-BBDB (addresses &optional remove)
1283         "Enter an address into the BBDB; implies ham (non-spam) sender"
1284         (dolist (from addresses)
1285           (when (stringp from)
1286             (let* ((parsed-address (gnus-extract-address-components from))
1287                    (name (or (nth 0 parsed-address) "Ham Sender"))
1288                    (remove-function (if remove 
1289                                         'bbdb-delete-record-internal
1290                                       'ignore))
1291                    (net-address (nth 1 parsed-address))
1292                    (record (and net-address 
1293                                 (bbdb-search-simple nil net-address))))
1294               (when net-address
1295                 (gnus-message 5 "%s address %s %s BBDB" 
1296                               (if remove "Deleting" "Adding") 
1297                               from
1298                               (if remove "from" "to"))
1299                 (if record
1300                     (funcall remove-function record)
1301                   (bbdb-create-internal name nil net-address nil nil 
1302                                         "ham sender added by spam.el")))))))
1303       
1304       (defun spam-BBDB-register-routine (articles &optional unregister)
1305         (let (addresses)
1306           (dolist (article articles)
1307             (when (stringp (spam-fetch-field-from-fast article))
1308               (push (spam-fetch-field-from-fast article) addresses)))
1309           ;; now do the register/unregister action
1310           (spam-enter-ham-BBDB addresses unregister)))
1311
1312       (defun spam-BBDB-unregister-routine (articles)
1313         (spam-BBDB-register-routine articles t))
1314
1315       (defun spam-check-BBDB ()
1316         "Mail from people in the BBDB is classified as ham or non-spam"
1317         (let ((who (nnmail-fetch-field "from"))
1318               (spam-split-group (if spam-split-symbolic-return
1319                                     'spam 
1320                                   spam-split-group)))
1321           (when who
1322             (setq who (nth 1 (gnus-extract-address-components who)))
1323             (if (bbdb-search-simple nil who)
1324                 t 
1325               (if spam-use-BBDB-exclusive
1326                   spam-split-group
1327                 nil))))))
1328
1329   (file-error (progn
1330                 (defalias 'bbdb-search-simple 'ignore)
1331                 (defalias 'spam-check-BBDB 'ignore)
1332                 (defalias 'spam-BBDB-register-routine 'ignore)
1333                 (defalias 'spam-enter-ham-BBDB 'ignore)
1334                 (defalias 'bbdb-create-internal 'ignore)
1335                 (defalias 'bbdb-delete-record-internal 'ignore)
1336                 (defalias 'bbdb-records 'ignore))))
1337
1338 \f
1339 ;;;; ifile
1340
1341 ;;; check the ifile backend; return nil if the mail was NOT classified
1342 ;;; as spam
1343
1344 (defun spam-get-ifile-database-parameter ()
1345   "Get the command-line parameter for ifile's database from
1346   spam-ifile-database-path."
1347   (if spam-ifile-database-path
1348       (format "--db-file=%s" spam-ifile-database-path)
1349     nil))
1350     
1351 (defun spam-check-ifile ()
1352   "Check the ifile backend for the classification of this message"
1353   (let ((article-buffer-name (buffer-name)) 
1354         (spam-split-group (if spam-split-symbolic-return
1355                               'spam 
1356                             spam-split-group))
1357         category return)
1358     (with-temp-buffer
1359       (let ((temp-buffer-name (buffer-name))
1360             (db-param (spam-get-ifile-database-parameter)))
1361         (save-excursion
1362           (set-buffer article-buffer-name)
1363           (apply 'call-process-region
1364                  (point-min) (point-max) spam-ifile-path
1365                  nil temp-buffer-name nil "-c"
1366                  (if db-param `(,db-param "-q") `("-q"))))
1367         ;; check the return now (we're back in the temp buffer)
1368         (goto-char (point-min))
1369         (if (not (eobp))
1370             (setq category (buffer-substring (point) (spam-point-at-eol))))
1371         (when (not (zerop (length category))) ; we need a category here
1372           (if spam-ifile-all-categories
1373               (setq return category)
1374             ;; else, if spam-ifile-all-categories is not set...
1375             (when (string-equal spam-ifile-spam-category category)
1376               (setq return spam-split-group)))))) ; note return is nil otherwise
1377     return))
1378
1379 (defun spam-ifile-register-with-ifile (articles category &optional unregister)
1380   "Register an article, given as a string, with a category.
1381 Uses `gnus-newsgroup-name' if category is nil (for ham registration)."
1382   (let ((category (or category gnus-newsgroup-name))
1383         (add-or-delete-option (if unregister "-d" "-i"))
1384         (db (spam-get-ifile-database-parameter))
1385         parameters)
1386     (with-temp-buffer
1387       (dolist (article articles)
1388         (let ((article-string (spam-get-article-as-string article)))
1389           (when (stringp article-string)
1390             (insert article-string))))
1391       (apply 'call-process-region
1392              (point-min) (point-max) spam-ifile-path
1393              nil nil nil 
1394              add-or-delete-option category
1395              (if db `(,db "-h") `("-h"))))))
1396
1397 (defun spam-ifile-register-spam-routine (articles &optional unregister)
1398   (spam-ifile-register-with-ifile articles spam-ifile-spam-category unregister))
1399
1400 (defun spam-ifile-unregister-spam-routine (articles)
1401   (spam-ifile-register-spam-routine articles t))
1402
1403 (defun spam-ifile-register-ham-routine (articles &optional unregister)
1404   (spam-ifile-register-with-ifile articles spam-ifile-ham-category unregister))
1405
1406 (defun spam-ifile-unregister-ham-routine (articles)
1407   (spam-ifile-register-ham-routine articles t))
1408
1409 \f
1410 ;;;; spam-stat
1411
1412 (condition-case nil
1413     (progn
1414       (let ((spam-stat-install-hooks nil))
1415         (require 'spam-stat))
1416       
1417       (defun spam-check-stat ()
1418         "Check the spam-stat backend for the classification of this message"
1419         (let ((spam-split-group (if spam-split-symbolic-return
1420                                     'spam 
1421                                   spam-split-group))
1422               (spam-stat-split-fancy-spam-group spam-split-group) ; override
1423               (spam-stat-buffer (buffer-name)) ; stat the current buffer
1424               category return)
1425           (spam-stat-split-fancy)))
1426
1427       (defun spam-stat-register-spam-routine (articles &optional unregister)
1428         (dolist (article articles)
1429           (let ((article-string (spam-get-article-as-string article)))
1430             (with-temp-buffer
1431               (insert article-string)
1432               (if unregister
1433                   (spam-stat-buffer-change-to-non-spam)
1434               (spam-stat-buffer-is-spam))))))
1435
1436       (defun spam-stat-unregister-spam-routine (articles)
1437         (spam-stat-register-spam-routine articles t))
1438
1439       (defun spam-stat-register-ham-routine (articles &optional unregister)
1440         (dolist (article articles)
1441           (let ((article-string (spam-get-article-as-string article)))
1442             (with-temp-buffer
1443               (insert article-string)
1444               (if unregister
1445                   (spam-stat-buffer-change-to-spam)
1446               (spam-stat-buffer-is-non-spam))))))
1447
1448       (defun spam-stat-unregister-ham-routine (articles)
1449         (spam-stat-register-ham-routine articles t))
1450
1451       (defun spam-maybe-spam-stat-load ()
1452         (when spam-use-stat (spam-stat-load)))
1453       
1454       (defun spam-maybe-spam-stat-save ()
1455         (when spam-use-stat (spam-stat-save))))
1456
1457   (file-error (progn
1458                 (defalias 'spam-stat-load 'ignore)
1459                 (defalias 'spam-stat-save 'ignore)
1460                 (defalias 'spam-maybe-spam-stat-load 'ignore)
1461                 (defalias 'spam-maybe-spam-stat-save 'ignore)
1462                 (defalias 'spam-stat-register-ham-routine 'ignore)
1463                 (defalias 'spam-stat-unregister-ham-routine 'ignore)
1464                 (defalias 'spam-stat-register-spam-routine 'ignore)
1465                 (defalias 'spam-stat-unregister-spam-routine 'ignore)
1466                 (defalias 'spam-stat-buffer-is-spam 'ignore)
1467                 (defalias 'spam-stat-buffer-change-to-spam 'ignore)
1468                 (defalias 'spam-stat-buffer-is-non-spam 'ignore)
1469                 (defalias 'spam-stat-buffer-change-to-non-spam 'ignore)
1470                 (defalias 'spam-stat-split-fancy 'ignore)
1471                 (defalias 'spam-check-stat 'ignore))))
1472
1473 \f
1474
1475 ;;;; Blacklists and whitelists.
1476
1477 (defvar spam-whitelist-cache nil)
1478 (defvar spam-blacklist-cache nil)
1479
1480 (defun spam-kill-whole-line ()
1481   (beginning-of-line)
1482   (let ((kill-whole-line t))
1483     (kill-line)))
1484
1485 ;;; address can be a list, too
1486 (defun spam-enter-whitelist (address &optional remove)
1487   "Enter ADDRESS (list or single) into the whitelist.  With a
1488   non-nil REMOVE, remove them."
1489   (interactive "sAddress: ")
1490   (spam-enter-list address spam-whitelist remove)
1491   (setq spam-whitelist-cache nil))
1492
1493 ;;; address can be a list, too
1494 (defun spam-enter-blacklist (address &optional remove)
1495   "Enter ADDRESS (list or single) into the blacklist.  With a
1496   non-nil REMOVE, remove them."
1497   (interactive "sAddress: ")
1498   (spam-enter-list address spam-blacklist remove)
1499   (setq spam-blacklist-cache nil))
1500
1501 (defun spam-enter-list (addresses file &optional remove)
1502   "Enter ADDRESSES into the given FILE.
1503 Either the whitelist or the blacklist files can be used.  With
1504 REMOVE not nil, remove the ADDRESSES."
1505   (if (stringp addresses)
1506       (spam-enter-list (list addresses) file remove)
1507     ;; else, we have a list of addresses here
1508     (unless (file-exists-p (file-name-directory file))
1509       (make-directory (file-name-directory file) t))
1510     (save-excursion
1511       (set-buffer
1512        (find-file-noselect file))
1513       (dolist (a addresses)
1514         (when (stringp a)
1515           (goto-char (point-min))
1516           (if (re-search-forward (regexp-quote a) nil t)
1517               ;; found the address
1518               (when remove
1519                 (spam-kill-whole-line))
1520             ;; else, the address was not found
1521             (unless remove
1522               (goto-char (point-max))
1523               (unless (bobp)
1524                 (insert "\n"))
1525               (insert a "\n")))))
1526       (save-buffer))))
1527
1528 ;;; returns t if the sender is in the whitelist, nil or
1529 ;;; spam-split-group otherwise
1530 (defun spam-check-whitelist ()
1531   ;; FIXME!  Should it detect when file timestamps change?
1532   (let ((spam-split-group (if spam-split-symbolic-return
1533                               'spam 
1534                             spam-split-group)))
1535     (unless spam-whitelist-cache
1536       (setq spam-whitelist-cache (spam-parse-list spam-whitelist)))
1537     (if (spam-from-listed-p spam-whitelist-cache) 
1538         t
1539       (if spam-use-whitelist-exclusive
1540           spam-split-group
1541         nil))))
1542
1543 (defun spam-check-blacklist ()
1544   ;; FIXME!  Should it detect when file timestamps change?
1545   (let ((spam-split-group (if spam-split-symbolic-return
1546                               'spam 
1547                             spam-split-group)))
1548     (unless spam-blacklist-cache
1549       (setq spam-blacklist-cache (spam-parse-list spam-blacklist)))
1550     (and (spam-from-listed-p spam-blacklist-cache) spam-split-group)))
1551
1552 (defun spam-parse-list (file)
1553   (when (file-readable-p file)
1554     (let (contents address)
1555       (with-temp-buffer
1556         (insert-file-contents file)
1557         (while (not (eobp))
1558           (setq address (buffer-substring (point) (spam-point-at-eol)))
1559           (forward-line 1)
1560           ;; insert the e-mail address if detected, otherwise the raw data
1561           (unless (zerop (length address))
1562             (let ((pure-address (nth 1 (gnus-extract-address-components address))))
1563               (push (or pure-address address) contents)))))
1564       (nreverse contents))))
1565
1566 (defun spam-from-listed-p (cache)
1567   (let ((from (nnmail-fetch-field "from"))
1568         found)
1569     (while cache
1570       (let ((address (pop cache)))
1571         (unless (zerop (length address)) ; 0 for a nil address too
1572           (setq address (regexp-quote address))
1573           ;; fix regexp-quote's treatment of user-intended regexes
1574           (while (string-match "\\\\\\*" address)
1575             (setq address (replace-match ".*" t t address))))
1576         (when (and address (string-match address from))
1577           (setq found t
1578                 cache nil))))
1579     found))
1580
1581 (defun spam-filelist-register-routine (articles blacklist &optional unregister)
1582   (let ((de-symbol (if blacklist 'spam-use-whitelist 'spam-use-blacklist))
1583         (declassification (if blacklist 'ham 'spam))
1584         (enter-function 
1585          (if blacklist 'spam-enter-blacklist 'spam-enter-whitelist))
1586         (remove-function
1587          (if blacklist 'spam-enter-whitelist 'spam-enter-blacklist))
1588         from addresses unregister-list)
1589     (dolist (article articles)
1590       (let ((from (spam-fetch-field-from-fast article))
1591             (id (spam-fetch-field-message-id-fast article))
1592             sender-ignored)
1593         (when (stringp from)
1594           (dolist (ignore-regex spam-blacklist-ignored-regexes)
1595             (when (and (not sender-ignored)
1596                        (stringp ignore-regex)
1597                        (string-match ignore-regex from))
1598               (setq sender-ignored t)))
1599           ;; remember the messages we need to unregister, unless remove is set
1600           (when (and
1601                  (null unregister) 
1602                  (spam-log-unregistration-needed-p
1603                   id 'process declassification de-symbol))
1604             (push from unregister-list))
1605           (unless sender-ignored
1606             (push from addresses)))))
1607
1608     (if unregister
1609         (funcall enter-function addresses t) ; unregister all these addresses
1610       ;; else, register normally and unregister what we need to
1611       (funcall remove-function unregister-list t)
1612       (dolist (article unregister-list)
1613         (spam-log-undo-registration
1614          (spam-fetch-field-message-id-fast article)
1615          'process
1616          declassification
1617          de-symbol))
1618       (funcall enter-function addresses nil))))
1619
1620 (defun spam-blacklist-unregister-routine (articles)
1621   (spam-blacklist-register-routine articles t))
1622
1623 (defun spam-blacklist-register-routine (articles &optional unregister)
1624   (spam-filelist-register-routine articles t unregister))
1625
1626 (defun spam-whitelist-unregister-routine (articles)
1627   (spam-whitelist-register-routine articles t))
1628
1629 (defun spam-whitelist-register-routine (articles &optional unregister)
1630   (spam-filelist-register-routine articles nil unregister))
1631
1632 \f
1633 ;;;; Spam-report glue
1634 (defun spam-report-gmane-register-routine (articles)
1635   (when articles
1636     (apply 'spam-report-gmane articles)))
1637
1638 \f
1639 ;;;; Bogofilter
1640 (defun spam-check-bogofilter-headers (&optional score)
1641   (let ((header (nnmail-fetch-field spam-bogofilter-header))
1642         (spam-split-group (if spam-split-symbolic-return
1643                               'spam 
1644                             spam-split-group)))
1645     (when header                        ; return nil when no header
1646       (if score                         ; scoring mode
1647           (if (string-match "spamicity=\\([0-9.]+\\)" header)
1648               (match-string 1 header)
1649             "0")
1650         ;; spam detection mode
1651         (when (string-match spam-bogofilter-bogosity-positive-spam-header
1652                             header)
1653           spam-split-group)))))
1654
1655 ;; return something sensible if the score can't be determined
1656 (defun spam-bogofilter-score ()
1657   "Get the Bogofilter spamicity score"
1658   (interactive)
1659   (save-window-excursion
1660     (gnus-summary-show-article t)
1661     (set-buffer gnus-article-buffer)
1662     (let ((score (or (spam-check-bogofilter-headers t)
1663                      (spam-check-bogofilter t))))
1664       (message "Spamicity score %s" score)
1665       (or score "0"))
1666     (gnus-summary-show-article)))
1667
1668 (defun spam-check-bogofilter (&optional score)
1669   "Check the Bogofilter backend for the classification of this message"
1670   (let ((article-buffer-name (buffer-name))
1671         (db spam-bogofilter-database-directory)
1672         return)
1673     (with-temp-buffer
1674       (let ((temp-buffer-name (buffer-name)))
1675         (save-excursion
1676           (set-buffer article-buffer-name)
1677           (apply 'call-process-region
1678                  (point-min) (point-max) 
1679                  spam-bogofilter-path
1680                  nil temp-buffer-name nil
1681                  (if db `("-d" ,db "-v") `("-v"))))
1682         (setq return (spam-check-bogofilter-headers score))))
1683     return))
1684
1685 (defun spam-bogofilter-register-with-bogofilter (articles 
1686                                                  spam 
1687                                                  &optional unregister)
1688   "Register an article, given as a string, as spam or non-spam."
1689   (dolist (article articles)
1690     (let ((article-string (spam-get-article-as-string article))
1691           (db spam-bogofilter-database-directory)
1692           (switch (if unregister
1693                       (if spam 
1694                           spam-bogofilter-spam-strong-switch
1695                         spam-bogofilter-ham-strong-switch)
1696                     (if spam 
1697                         spam-bogofilter-spam-switch 
1698                       spam-bogofilter-ham-switch))))
1699       (when (stringp article-string)
1700         (with-temp-buffer
1701           (insert article-string)
1702
1703           (apply 'call-process-region
1704                  (point-min) (point-max) 
1705                  spam-bogofilter-path
1706                  nil nil nil switch
1707                  (if db `("-d" ,db "-v") `("-v"))))))))
1708   
1709 (defun spam-bogofilter-register-spam-routine (articles &optional unregister)
1710   (spam-bogofilter-register-with-bogofilter articles t unregister))
1711
1712 (defun spam-bogofilter-unregister-spam-routine (articles)
1713   (spam-bogofilter-register-spam-routine articles t))
1714
1715 (defun spam-bogofilter-register-ham-routine (articles &optional unregister)
1716   (spam-bogofilter-register-with-bogofilter articles nil unregister))
1717
1718 (defun spam-bogofilter-unregister-ham-routine (articles)
1719   (spam-bogofilter-register-ham-routine articles t))
1720
1721
1722 \f
1723 ;;;; spamoracle
1724 (defun spam-check-spamoracle ()
1725   "Run spamoracle on an article to determine whether it's spam."
1726   (let ((article-buffer-name (buffer-name))
1727         (spam-split-group (if spam-split-symbolic-return
1728                               'spam 
1729                             spam-split-group)))
1730     (with-temp-buffer
1731       (let ((temp-buffer-name (buffer-name)))
1732         (save-excursion
1733           (set-buffer article-buffer-name)
1734           (let ((status 
1735                  (apply 'call-process-region 
1736                         (point-min) (point-max)
1737                         spam-spamoracle-binary 
1738                         nil temp-buffer-name nil
1739                         (if spam-spamoracle-database
1740                             `("-f" ,spam-spamoracle-database "mark")
1741                           '("mark")))))
1742             (if (eq 0 status)
1743                 (progn
1744                   (set-buffer temp-buffer-name)
1745                   (goto-char (point-min))
1746                   (when (re-search-forward "^X-Spam: yes;" nil t)
1747                     spam-split-group))
1748               (error "Error running spamoracle" status))))))))
1749
1750 (defun spam-spamoracle-learn (articles article-is-spam-p &optional unregister)
1751   "Run spamoracle in training mode."
1752   (with-temp-buffer
1753     (let ((temp-buffer-name (buffer-name)))
1754       (save-excursion
1755         (goto-char (point-min))
1756         (dolist (article articles)
1757           (insert (spam-get-article-as-string article)))
1758         (let* ((arg (if (spam-xor unregister article-is-spam-p)
1759                         "-spam" 
1760                       "-good"))
1761                (status 
1762                 (apply 'call-process-region
1763                        (point-min) (point-max)
1764                        spam-spamoracle-binary
1765                        nil temp-buffer-name nil
1766                        (if spam-spamoracle-database
1767                            `("-f" ,spam-spamoracle-database 
1768                              "add" ,arg)
1769                          `("add" ,arg)))))
1770           (when (not (eq 0 status))
1771             (error "Error running spamoracle" status)))))))
1772
1773 (defun spam-spamoracle-learn-ham (articles &optional unregister)
1774   (spam-spamoracle-learn articles nil unregister))
1775
1776 (defun spam-spamoracle-unlearn-ham (articles &optional unregister)
1777   (spam-spamoracle-learn-ham articles t))
1778
1779 (defun spam-spamoracle-learn-spam (articles &optional unregister)
1780   (spam-spamoracle-learn articles t unregister))
1781
1782 (defun spam-spamoracle-unlearn-spam (articles &optional unregister)
1783   (spam-spamoracle-learn-spam articles t))
1784
1785 \f
1786 ;;;; Hooks
1787
1788 ;;;###autoload
1789 (defun spam-initialize ()
1790   "Install the spam.el hooks and do other initialization"
1791   (interactive)
1792   (setq spam-install-hooks t)
1793   ;; TODO: How do we redo this every time spam-face is customized?
1794   (push '((eq mark gnus-spam-mark) . spam-face)
1795         gnus-summary-highlight)
1796   ;; Add hooks for loading and saving the spam stats
1797   (add-hook 'gnus-save-newsrc-hook 'spam-maybe-spam-stat-save)
1798   (add-hook 'gnus-get-top-new-news-hook 'spam-maybe-spam-stat-load)
1799   (add-hook 'gnus-startup-hook 'spam-maybe-spam-stat-load)
1800   (add-hook 'gnus-summary-prepare-exit-hook 'spam-summary-prepare-exit)
1801   (add-hook 'gnus-summary-prepare-hook 'spam-summary-prepare)
1802   (add-hook 'gnus-get-new-news-hook 'spam-setup-widening)
1803   (add-hook 'gnus-summary-prepare-hook 'spam-find-spam))
1804
1805 (defun spam-unload-hook ()
1806   "Uninstall the spam.el hooks"
1807   (interactive)
1808   (remove-hook 'gnus-save-newsrc-hook 'spam-maybe-spam-stat-save)
1809   (remove-hook 'gnus-get-top-new-news-hook 'spam-maybe-spam-stat-load)
1810   (remove-hook 'gnus-startup-hook 'spam-maybe-spam-stat-load)
1811   (remove-hook 'gnus-summary-prepare-exit-hook 'spam-summary-prepare-exit)
1812   (remove-hook 'gnus-summary-prepare-hook 'spam-summary-prepare)
1813   (remove-hook 'gnus-get-new-news-hook 'spam-setup-widening)
1814   (remove-hook 'gnus-summary-prepare-hook 'spam-find-spam))
1815
1816 (when spam-install-hooks
1817   (spam-initialize))
1818
1819 (provide 'spam)
1820
1821 ;;; spam.el ends here.