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