1 ;;; spam.el --- Identifying spam
2 ;; Copyright (C) 2002, 2003 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
26 ;;; This module addresses a few aspects of spam control under Gnus. Page
27 ;;; breaks are used for grouping declarations and documentation relating to
28 ;;; each particular aspect.
30 ;;; The integration with Gnus is not yet complete. See various `FIXME'
31 ;;; comments, below, for supplementary explanations or discussions.
33 ;;; Several TODO items are marked as such
37 (eval-when-compile (require 'cl))
41 (require 'gnus-uu) ; because of key prefix issues
42 (require 'gnus) ; for the definitions of group content classification and spam processors
43 (require 'message) ;for the message-fetch-field functions
45 ;; for nnimap-split-download-body-default
46 (eval-when-compile (require 'nnimap))
48 ;; autoload executable-find
50 ;; executable-find is not autoloaded in Emacs 20
51 (autoload 'executable-find "executable"))
55 (autoload 'query-dig "dig"))
57 ;; autoload spam-report
59 (autoload 'spam-report-gmane "spam-report"))
63 (autoload 'query-dns "dns"))
68 "Spam configuration.")
70 (defcustom spam-directory "~/News/spam/"
71 "Directory for spam whitelists and blacklists."
75 (defcustom spam-move-spam-nonspam-groups-only t
76 "Whether spam should be moved in non-spam groups only.
77 When t, only ham and unclassified groups will have their spam moved
78 to the spam-process-destination. When nil, spam will also be moved from
83 (defcustom spam-process-ham-in-nonham-groups nil
84 "Whether ham should be processed in non-ham groups."
88 (defcustom spam-process-ham-in-spam-groups nil
89 "Whether ham should be processed in spam groups."
93 (defcustom spam-mark-only-unseen-as-spam t
94 "Whether only unseen articles should be marked as spam in spam
95 groups. When nil, all unread articles in a spam group are marked as
96 spam. Set this if you want to leave an article unread in a spam group
97 without losing it to the automatic spam-marking process."
101 (defcustom spam-mark-ham-unread-before-move-from-spam-group nil
102 "Whether ham should be marked unread before it's moved out of a spam
103 group according to ham-process-destination. This variable is an
104 official entry in the international Longest Variable Name
109 (defcustom spam-whitelist (expand-file-name "whitelist" spam-directory)
110 "The location of the whitelist.
111 The file format is one regular expression per line.
112 The regular expression is matched against the address."
116 (defcustom spam-blacklist (expand-file-name "blacklist" spam-directory)
117 "The location of the blacklist.
118 The file format is one regular expression per line.
119 The regular expression is matched against the address."
123 (defcustom spam-use-dig t
124 "Whether query-dig should be used instead of query-dns."
128 (defcustom spam-use-blacklist nil
129 "Whether the blacklist should be used by spam-split."
133 (defcustom spam-use-whitelist nil
134 "Whether the whitelist should be used by spam-split."
138 (defcustom spam-use-whitelist-exclusive nil
139 "Whether whitelist-exclusive should be used by spam-split.
140 Exclusive whitelisting means that all messages from senders not in the whitelist
141 are considered spam."
145 (defcustom spam-use-blackholes nil
146 "Whether blackholes should be used by spam-split."
150 (defcustom spam-use-hashcash nil
151 "Whether hashcash payments should be detected by spam-split."
155 (defcustom spam-use-regex-headers nil
156 "Whether a header regular expression match should be used by spam-split.
157 Also see the variables `spam-regex-headers-spam' and `spam-regex-headers-ham'."
161 (defcustom spam-use-regex-body nil
162 "Whether a body regular expression match should be used by spam-split.
163 Also see the variables `spam-regex-body-spam' and `spam-regex-body-ham'."
167 (defcustom spam-use-bogofilter-headers nil
168 "Whether bogofilter headers should be used by spam-split.
169 Enable this if you pre-process messages with Bogofilter BEFORE Gnus sees them."
173 (defcustom spam-use-bogofilter nil
174 "Whether bogofilter should be invoked by spam-split.
175 Enable this if you want Gnus to invoke Bogofilter on new messages."
179 (defcustom spam-use-BBDB nil
180 "Whether BBDB should be used by spam-split."
184 (defcustom spam-use-BBDB-exclusive nil
185 "Whether BBDB-exclusive should be used by spam-split.
186 Exclusive BBDB means that all messages from senders not in the BBDB are
191 (defcustom spam-use-ifile nil
192 "Whether ifile should be used by spam-split."
196 (defcustom spam-use-stat nil
197 "Whether spam-stat should be used by spam-split."
201 (defcustom spam-use-spamoracle nil
202 "Whether spamoracle should be used by spam-split."
206 (defcustom spam-install-hooks (or
210 spam-use-whitelist-exclusive
213 spam-use-regex-headers
215 spam-use-bogofilter-headers
218 spam-use-BBDB-exclusive
222 "Whether the spam hooks should be installed, default to t if one of
223 the spam-use-* variables is set."
224 :group 'gnus-registry
227 (defcustom spam-split-group "spam"
228 "Group name where incoming spam should be put by spam-split."
232 ;;; TODO: deprecate this variable, it's confusing since it's a list of strings, not regular expressions
233 (defcustom spam-junk-mailgroups (cons spam-split-group '("mail.junk" "poste.pourriel"))
234 "Mailgroups with spam contents.
235 All unmarked article in such group receive the spam mark on group entry."
236 :type '(repeat (string :tag "Group"))
239 (defcustom spam-blackhole-servers '("bl.spamcop.net" "relays.ordb.org"
240 "dev.null.dk" "relays.visi.com")
241 "List of blackhole servers."
242 :type '(repeat (string :tag "Server"))
245 (defcustom spam-blackhole-good-server-regex nil
246 "String matching IP addresses that should not be checked in the blackholes"
247 :type '(radio (const nil)
248 (regexp :format "%t: %v\n" :size 0))
251 (defcustom spam-face 'gnus-splash-face
252 "Face for spam-marked articles"
256 (defcustom spam-regex-headers-spam '("^X-Spam-Flag: YES")
257 "Regular expression for positive header spam matches"
258 :type '(repeat (regexp :tag "Regular expression to match spam header"))
261 (defcustom spam-regex-headers-ham '("^X-Spam-Flag: NO")
262 "Regular expression for positive header ham matches"
263 :type '(repeat (regexp :tag "Regular expression to match ham header"))
266 (defcustom spam-regex-body-spam '()
267 "Regular expression for positive body spam matches"
268 :type '(repeat (regexp :tag "Regular expression to match spam body"))
271 (defcustom spam-regex-body-ham '()
272 "Regular expression for positive body ham matches"
273 :type '(repeat (regexp :tag "Regular expression to match ham body"))
276 (defgroup spam-ifile nil
277 "Spam ifile configuration."
280 (defcustom spam-ifile-path (executable-find "ifile")
281 "File path of the ifile executable program."
282 :type '(choice (file :tag "Location of ifile")
283 (const :tag "ifile is not installed"))
286 (defcustom spam-ifile-database-path nil
287 "File path of the ifile database."
288 :type '(choice (file :tag "Location of the ifile database")
289 (const :tag "Use the default"))
292 (defcustom spam-ifile-spam-category "spam"
293 "Name of the spam ifile category."
297 (defcustom spam-ifile-ham-category nil
298 "Name of the ham ifile category. If nil, the current group name will
300 :type '(choice (string :tag "Use a fixed category")
301 (const :tag "Use the current group name"))
304 (defcustom spam-ifile-all-categories nil
305 "Whether the ifile check will return all categories, or just spam.
306 Set this to t if you want to use the spam-split invocation of ifile as
307 your main source of newsgroup names."
311 (defgroup spam-bogofilter nil
312 "Spam bogofilter configuration."
315 (defcustom spam-bogofilter-path (executable-find "bogofilter")
316 "File path of the Bogofilter executable program."
317 :type '(choice (file :tag "Location of bogofilter")
318 (const :tag "Bogofilter is not installed"))
319 :group 'spam-bogofilter)
321 (defcustom spam-bogofilter-header "X-Bogosity"
322 "The header that Bogofilter inserts in messages."
324 :group 'spam-bogofilter)
326 (defcustom spam-bogofilter-spam-switch "-s"
327 "The switch that Bogofilter uses to register spam messages."
329 :group 'spam-bogofilter)
331 (defcustom spam-bogofilter-ham-switch "-n"
332 "The switch that Bogofilter uses to register ham messages."
334 :group 'spam-bogofilter)
336 (defcustom spam-bogofilter-bogosity-positive-spam-header "^\\(Yes\\|Spam\\)"
337 "The regex on `spam-bogofilter-header' for positive spam identification."
339 :group 'spam-bogofilter)
341 (defcustom spam-bogofilter-database-directory nil
342 "Directory path of the Bogofilter databases."
343 :type '(choice (directory :tag "Location of the Bogofilter database directory")
344 (const :tag "Use the default"))
347 (defgroup spam-spamoracle nil
348 "Spam ifile configuration."
351 (defcustom spam-spamoracle-database nil
352 "Location of spamoracle database file. When nil, use the default
353 spamoracle database."
354 :type '(choice (directory :tag "Location of spamoracle database file.")
355 (const :tag "Use the default"))
356 :group 'spam-spamoracle)
358 (defcustom spam-spamoracle-binary (executable-find "spamoracle")
359 "Location of the spamoracle binary."
360 :type '(choice (directory :tag "Location of the spamoracle binary")
361 (const :tag "Use the default"))
362 :group 'spam-spamoracle)
364 ;;; Key bindings for spam control.
366 (gnus-define-keys gnus-summary-mode-map
367 "St" spam-bogofilter-score
368 "Sx" gnus-summary-mark-as-spam
369 "Mst" spam-bogofilter-score
370 "Msx" gnus-summary-mark-as-spam
371 "\M-d" gnus-summary-mark-as-spam)
373 ;; convenience functions
374 (defun spam-group-ham-mark-p (group mark &optional spam)
375 (when (stringp group)
376 (let* ((marks (spam-group-ham-marks group spam))
377 (marks (if (symbolp mark)
379 (mapcar 'symbol-value marks))))
382 (defun spam-group-spam-mark-p (group mark)
383 (spam-group-ham-mark-p group mark t))
385 (defun spam-group-ham-marks (group &optional spam)
386 (when (stringp group)
387 (let* ((marks (if spam
388 (gnus-parameter-spam-marks group)
389 (gnus-parameter-ham-marks group)))
391 (marks (if (listp (car marks)) (car marks) marks)))
394 (defun spam-group-spam-marks (group)
395 (spam-group-ham-marks group t))
397 (defun spam-group-spam-contents-p (group)
399 (or (member group spam-junk-mailgroups)
400 (memq 'gnus-group-spam-classification-spam
401 (gnus-parameter-spam-contents group)))
404 (defun spam-group-ham-contents-p (group)
406 (memq 'gnus-group-spam-classification-ham
407 (gnus-parameter-spam-contents group))
410 (defun spam-group-processor-p (group processor)
411 (if (and (stringp group)
413 (member processor (car (gnus-parameter-spam-process group)))
416 (defun spam-group-spam-processor-report-gmane-p (group)
417 (spam-group-processor-p group 'gnus-group-spam-exit-processor-report-gmane))
419 (defun spam-group-spam-processor-bogofilter-p (group)
420 (spam-group-processor-p group 'gnus-group-spam-exit-processor-bogofilter))
422 (defun spam-group-spam-processor-blacklist-p (group)
423 (spam-group-processor-p group 'gnus-group-spam-exit-processor-blacklist))
425 (defun spam-group-spam-processor-ifile-p (group)
426 (spam-group-processor-p group 'gnus-group-spam-exit-processor-ifile))
428 (defun spam-group-ham-processor-ifile-p (group)
429 (spam-group-processor-p group 'gnus-group-ham-exit-processor-ifile))
431 (defun spam-group-spam-processor-spamoracle-p (group)
432 (spam-group-processor-p group 'gnus-group-spam-exit-processor-spamoracle))
434 (defun spam-group-ham-processor-bogofilter-p (group)
435 (spam-group-processor-p group 'gnus-group-ham-exit-processor-bogofilter))
437 (defun spam-group-spam-processor-stat-p (group)
438 (spam-group-processor-p group 'gnus-group-spam-exit-processor-stat))
440 (defun spam-group-ham-processor-stat-p (group)
441 (spam-group-processor-p group 'gnus-group-ham-exit-processor-stat))
443 (defun spam-group-ham-processor-whitelist-p (group)
444 (spam-group-processor-p group 'gnus-group-ham-exit-processor-whitelist))
446 (defun spam-group-ham-processor-BBDB-p (group)
447 (spam-group-processor-p group 'gnus-group-ham-exit-processor-BBDB))
449 (defun spam-group-ham-processor-copy-p (group)
450 (spam-group-processor-p group 'gnus-group-ham-exit-processor-copy))
452 (defun spam-group-ham-processor-spamoracle-p (group)
453 (spam-group-processor-p group 'gnus-group-ham-exit-processor-spamoracle))
455 ;;; Summary entry and exit processing.
457 (defun spam-summary-prepare ()
458 (spam-mark-junk-as-spam-routine))
460 ;; The spam processors are invoked for any group, spam or ham or neither
461 (defun spam-summary-prepare-exit ()
462 (unless gnus-group-is-exiting-without-update-p
463 (gnus-message 6 "Exiting summary buffer and applying spam rules")
464 (when (and spam-bogofilter-path
465 (spam-group-spam-processor-bogofilter-p gnus-newsgroup-name))
466 (gnus-message 5 "Registering spam with bogofilter")
467 (spam-bogofilter-register-spam-routine))
469 (when (and spam-ifile-path
470 (spam-group-spam-processor-ifile-p gnus-newsgroup-name))
471 (gnus-message 5 "Registering spam with ifile")
472 (spam-ifile-register-spam-routine))
474 (when (spam-group-spam-processor-spamoracle-p gnus-newsgroup-name)
475 (gnus-message 5 "Registering spam with spamoracle")
476 (spam-spamoracle-learn-spam))
478 (when (spam-group-spam-processor-stat-p gnus-newsgroup-name)
479 (gnus-message 5 "Registering spam with spam-stat")
480 (spam-stat-register-spam-routine))
482 (when (spam-group-spam-processor-blacklist-p gnus-newsgroup-name)
483 (gnus-message 5 "Registering spam with the blacklist")
484 (spam-blacklist-register-routine))
486 (when (spam-group-spam-processor-report-gmane-p gnus-newsgroup-name)
487 (gnus-message 5 "Registering spam with the Gmane report")
488 (spam-report-gmane-register-routine))
490 (if spam-move-spam-nonspam-groups-only
491 (when (not (spam-group-spam-contents-p gnus-newsgroup-name))
492 (spam-mark-spam-as-expired-and-move-routine
493 (gnus-parameter-spam-process-destination gnus-newsgroup-name)))
494 (gnus-message 5 "Marking spam as expired and moving it to %s" gnus-newsgroup-name)
495 (spam-mark-spam-as-expired-and-move-routine
496 (gnus-parameter-spam-process-destination gnus-newsgroup-name)))
498 ;; now we redo spam-mark-spam-as-expired-and-move-routine to only
499 ;; expire spam, in case the above did not expire them
500 (gnus-message 5 "Marking spam as expired without moving it")
501 (spam-mark-spam-as-expired-and-move-routine nil)
503 (when (or (spam-group-ham-contents-p gnus-newsgroup-name)
504 (and (spam-group-spam-contents-p gnus-newsgroup-name)
505 spam-process-ham-in-spam-groups)
506 spam-process-ham-in-nonham-groups)
507 (when (spam-group-ham-processor-whitelist-p gnus-newsgroup-name)
508 (gnus-message 5 "Registering ham with the whitelist")
509 (spam-whitelist-register-routine))
510 (when (spam-group-ham-processor-ifile-p gnus-newsgroup-name)
511 (gnus-message 5 "Registering ham with ifile")
512 (spam-ifile-register-ham-routine))
513 (when (spam-group-ham-processor-bogofilter-p gnus-newsgroup-name)
514 (gnus-message 5 "Registering ham with Bogofilter")
515 (spam-bogofilter-register-ham-routine))
516 (when (spam-group-ham-processor-stat-p gnus-newsgroup-name)
517 (gnus-message 5 "Registering ham with spam-stat")
518 (spam-stat-register-ham-routine))
519 (when (spam-group-ham-processor-BBDB-p gnus-newsgroup-name)
520 (gnus-message 5 "Registering ham with the BBDB")
521 (spam-BBDB-register-routine))
522 (when (spam-group-ham-processor-spamoracle-p gnus-newsgroup-name)
523 (gnus-message 5 "Registering ham with spamoracle")
524 (spam-spamoracle-learn-ham)))
526 (when (spam-group-ham-processor-copy-p gnus-newsgroup-name)
527 (gnus-message 5 "Copying ham")
528 (spam-ham-copy-routine
529 (gnus-parameter-ham-process-destination gnus-newsgroup-name)))
531 ;; now move all ham articles out of spam groups
532 (when (spam-group-spam-contents-p gnus-newsgroup-name)
533 (gnus-message 5 "Moving ham messages from spam group")
534 (spam-ham-move-routine
535 (gnus-parameter-ham-process-destination gnus-newsgroup-name)))))
537 (defun spam-mark-junk-as-spam-routine ()
538 ;; check the global list of group names spam-junk-mailgroups and the
540 (when (spam-group-spam-contents-p gnus-newsgroup-name)
541 (gnus-message 5 "Marking %s articles as spam"
542 (if spam-mark-only-unseen-as-spam
545 (let ((articles (if spam-mark-only-unseen-as-spam
546 gnus-newsgroup-unseen
547 gnus-newsgroup-unreads)))
548 (dolist (article articles)
549 (gnus-summary-mark-article article gnus-spam-mark)))))
551 (defun spam-mark-spam-as-expired-and-move-routine (&rest groups)
552 (gnus-summary-kill-process-mark)
553 (let ((articles gnus-newsgroup-articles)
554 article tomove todelete)
555 (dolist (article articles)
556 (when (eq (gnus-summary-article-mark article) gnus-spam-mark)
557 (gnus-summary-mark-article article gnus-expirable-mark)
558 (push article tomove)))
560 ;; now do the actual copies
561 (dolist (group groups)
564 (dolist (article tomove)
565 (gnus-summary-set-process-mark article))
567 (gnus-summary-copy-article nil group)
570 ;; now delete the articles, if there was a copy done
572 (dolist (article tomove)
573 (gnus-summary-set-process-mark article))
575 (gnus-summary-delete-article nil)))
577 (gnus-summary-yank-process-mark)))
579 (defun spam-ham-copy-or-move-routine (copy groups)
580 (gnus-summary-kill-process-mark)
581 (let ((articles gnus-newsgroup-articles)
582 article mark todo todelete)
583 (dolist (article articles)
584 (when (spam-group-ham-mark-p gnus-newsgroup-name
585 (gnus-summary-article-mark article))
586 (push article todo)))
588 ;; now do the actual move
589 (dolist (group groups)
590 (when (and todo (stringp group))
591 (dolist (article todo)
592 (when spam-mark-ham-unread-before-move-from-spam-group
593 (gnus-summary-mark-article article gnus-unread-mark))
594 (gnus-summary-set-process-mark article))
595 (gnus-summary-copy-article nil group)
598 ;; now delete the articles, unless copy is t, and when there was a copy done
601 (dolist (article todo)
602 (gnus-summary-set-process-mark article))
604 (gnus-summary-delete-article nil)))))
606 (gnus-summary-yank-process-mark))
608 (defun spam-ham-copy-routine (&rest groups)
609 (spam-ham-copy-or-move-routine t groups))
611 (defun spam-ham-move-routine (&rest groups)
612 (spam-ham-copy-or-move-routine nil groups))
614 (defun spam-generic-register-routine (spam-func ham-func)
615 (let ((articles gnus-newsgroup-articles)
616 article mark ham-articles spam-articles)
619 (setq article (pop articles)
620 mark (gnus-summary-article-mark article))
621 (cond ((spam-group-spam-mark-p gnus-newsgroup-name mark)
622 (push article spam-articles))
623 ((memq article gnus-newsgroup-saved))
624 ((spam-group-ham-mark-p gnus-newsgroup-name mark)
625 (push article ham-articles))))
627 (when (and ham-articles ham-func)
628 (mapc ham-func ham-articles)) ; we use mapc because unlike
629 ; mapcar it discards the
631 (when (and spam-articles spam-func)
632 (mapc spam-func spam-articles)))) ; we use mapc because unlike
633 ; mapcar it discards the
637 (defalias 'spam-point-at-eol (if (fboundp 'point-at-eol)
639 'line-end-position)))
641 (defun spam-get-article-as-string (article)
642 (let ((article-buffer (spam-get-article-as-buffer article))
645 (save-window-excursion
646 (set-buffer article-buffer)
647 (setq article-string (buffer-string))))
650 (defun spam-get-article-as-buffer (article)
651 (let ((article-buffer))
652 (when (numberp article)
653 (save-window-excursion
654 (gnus-summary-goto-subject article)
655 (gnus-summary-show-article t)
656 (setq article-buffer (get-buffer gnus-article-buffer))))
660 ;; (defun spam-get-article-as-filename (article)
661 ;; (let ((article-filename))
662 ;; (when (numberp article)
663 ;; (nnml-possibly-change-directory (gnus-group-real-name gnus-newsgroup-name))
664 ;; (setq article-filename (expand-file-name (int-to-string article) nnml-current-directory)))
665 ;; (if (file-exists-p article-filename)
669 (defun spam-fetch-field-from-fast (article)
670 "Fetch the `from' field quickly, using the internal gnus-data-list function"
671 (if (and (numberp article)
672 (assoc article (gnus-data-list nil)))
673 (mail-header-from (gnus-data-header (assoc article (gnus-data-list nil))))
676 (defun spam-fetch-field-subject-fast (article)
677 "Fetch the `subject' field quickly, using the internal gnus-data-list function"
678 (if (and (numberp article)
679 (assoc article (gnus-data-list nil)))
680 (mail-header-subject (gnus-data-header (assoc article (gnus-data-list nil))))
684 ;;;; Spam determination.
686 (defvar spam-list-of-checks
687 '((spam-use-blacklist . spam-check-blacklist)
688 (spam-use-regex-headers . spam-check-regex-headers)
689 (spam-use-regex-body . spam-check-regex-body)
690 (spam-use-whitelist . spam-check-whitelist)
691 (spam-use-BBDB . spam-check-BBDB)
692 (spam-use-ifile . spam-check-ifile)
693 (spam-use-spamoracle . spam-check-spamoracle)
694 (spam-use-stat . spam-check-stat)
695 (spam-use-blackholes . spam-check-blackholes)
696 (spam-use-hashcash . spam-check-hashcash)
697 (spam-use-bogofilter-headers . spam-check-bogofilter-headers)
698 (spam-use-bogofilter . spam-check-bogofilter))
699 "The spam-list-of-checks list contains pairs associating a parameter
700 variable with a spam checking function. If the parameter variable is
701 true, then the checking function is called, and its value decides what
702 happens. Each individual check may return nil, t, or a mailgroup
703 name. The value nil means that the check does not yield a decision,
704 and so, that further checks are needed. The value t means that the
705 message is definitely not spam, and that further spam checks should be
706 inhibited. Otherwise, a mailgroup name is returned where the mail
707 should go, and further checks are also inhibited. The usual mailgroup
708 name is the value of `spam-split-group', meaning that the message is
711 (defvar spam-list-of-statistical-checks
712 '(spam-use-ifile spam-use-regex-body spam-use-stat spam-use-bogofilter spam-use-spamoracle)
713 "The spam-list-of-statistical-checks list contains all the mail
714 splitters that need to have the full message body available.")
716 ;;;TODO: modify to invoke self with each specific check if invoked without specific checks
717 (defun spam-split (&rest specific-checks)
718 "Split this message into the `spam' group if it is spam.
719 This function can be used as an entry in `nnmail-split-fancy', for
720 example like this: (: spam-split). It can take checks as parameters.
722 See the Info node `(gnus)Fancy Mail Splitting' for more details."
726 (dolist (check spam-list-of-statistical-checks)
727 (when (symbol-value check)
729 (gnus-message 8 "spam-split: widening the buffer (%s requires it)"
732 ;; (progn (widen) (debug (buffer-string)))
733 (let ((list-of-checks spam-list-of-checks)
735 (while (and list-of-checks (not decision))
736 (let ((pair (pop list-of-checks)))
737 (when (and (symbol-value (car pair))
738 (or (null specific-checks)
739 (memq (car pair) specific-checks)))
740 (gnus-message 5 "spam-split: calling the %s function" (symbol-name (cdr pair)))
741 (setq decision (funcall (cdr pair))))))
746 (defun spam-setup-widening ()
747 (dolist (check spam-list-of-statistical-checks)
748 (when (symbol-value check)
749 (setq nnimap-split-download-body-default t))))
754 (defun spam-check-regex-body ()
755 (let ((spam-regex-headers-ham spam-regex-body-ham)
756 (spam-regex-headers-spam spam-regex-body-spam))
757 (spam-check-regex-headers t)))
762 (defun spam-check-regex-headers (&optional body)
763 (let ((type (if body "body" "header"))
765 (dolist (h-regex spam-regex-headers-ham)
767 (goto-char (point-min))
768 (when (re-search-forward h-regex nil t)
769 (message "Ham regex %s search positive." type)
771 (dolist (s-regex spam-regex-headers-spam)
773 (goto-char (point-min))
774 (when (re-search-forward s-regex nil t)
775 (message "Spam regex %s search positive." type)
777 (setq ret spam-split-group))))
783 (defun spam-reverse-ip-string (ip)
786 (nreverse (split-string ip "\\."))
789 (defun spam-check-blackholes ()
790 "Check the Received headers for blackholed relays."
791 (let ((headers (nnmail-fetch-field "received"))
796 (goto-char (point-min))
797 (gnus-message 5 "Checking headers for relay addresses")
798 (while (re-search-forward
799 "\\([0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+\\)" nil t)
800 (gnus-message 9 "Blackhole search found host IP %s." (match-string 1))
801 (push (spam-reverse-ip-string (match-string 1))
803 (dolist (server spam-blackhole-servers)
805 (unless (and spam-blackhole-good-server-regex
806 ;; match the good-server-regex against the reversed (again) IP string
808 spam-blackhole-good-server-regex
809 (spam-reverse-ip-string ip)))
811 (let ((query-string (concat ip "." server)))
813 (let ((query-result (query-dig query-string)))
815 (gnus-message 5 "(DIG): positive blackhole check '%s'"
817 (push (list ip server query-result)
819 ;; else, if not using dig.el
820 (when (query-dns query-string)
821 (gnus-message 5 "positive blackhole check")
822 (push (list ip server (query-dns query-string 'TXT))
833 (defun spam-check-hashcash ()
834 "Check the headers for hashcash payments."
835 (mail-check-payment))) ;mail-check-payment returns a boolean
838 (defalias 'mail-check-payment 'ignore)
839 (defalias 'spam-check-hashcash 'ignore))))
843 ;;; original idea for spam-check-BBDB from Alexander Kotelnikov
844 ;;; <sacha@giotto.sj.ru>
846 ;; all this is done inside a condition-case to trap errors
853 (defun spam-enter-ham-BBDB (from)
854 "Enter an address into the BBDB; implies ham (non-spam) sender"
856 (let* ((parsed-address (gnus-extract-address-components from))
857 (name (or (car parsed-address) "Ham Sender"))
858 (net-address (car (cdr parsed-address))))
859 (gnus-message 5 "Adding address %s to BBDB" from)
860 (when (and net-address
861 (not (bbdb-search-simple nil net-address)))
862 (bbdb-create-internal name nil net-address nil nil
863 "ham sender added by spam.el")))))
865 (defun spam-BBDB-register-routine ()
866 (spam-generic-register-routine
871 (spam-enter-ham-BBDB (spam-fetch-field-from-fast article)))))
873 (defun spam-check-BBDB ()
874 "Mail from people in the BBDB is classified as ham or non-spam"
875 (let ((who (nnmail-fetch-field "from")))
877 (setq who (cadr (gnus-extract-address-components who)))
878 (if (bbdb-search-simple nil who)
880 (if spam-use-BBDB-exclusive
885 (defalias 'bbdb-search-simple 'ignore)
886 (defalias 'spam-check-BBDB 'ignore)
887 (defalias 'spam-BBDB-register-routine 'ignore)
888 (defalias 'spam-enter-ham-BBDB 'ignore)
889 (defalias 'bbdb-create-internal 'ignore)
890 (defalias 'bbdb-records 'ignore))))
895 ;;; check the ifile backend; return nil if the mail was NOT classified
898 (defun spam-get-ifile-database-parameter ()
899 "Get the command-line parameter for ifile's database from spam-ifile-database-path."
900 (if spam-ifile-database-path
901 (format "--db-file=%s" spam-ifile-database-path)
904 (defun spam-check-ifile ()
905 "Check the ifile backend for the classification of this message"
906 (let ((article-buffer-name (buffer-name))
909 (let ((temp-buffer-name (buffer-name))
910 (db-param (spam-get-ifile-database-parameter)))
912 (set-buffer article-buffer-name)
914 (call-process-region (point-min) (point-max) spam-ifile-path
915 nil temp-buffer-name nil "-q" "-c" db-param)
916 (call-process-region (point-min) (point-max) spam-ifile-path
917 nil temp-buffer-name nil "-q" "-c")))
918 (goto-char (point-min))
920 (setq category (buffer-substring (point) (spam-point-at-eol))))
921 (when (not (zerop (length category))) ; we need a category here
922 (if spam-ifile-all-categories
923 (setq return category)
924 ;; else, if spam-ifile-all-categories is not set...
925 (when (string-equal spam-ifile-spam-category category)
926 (setq return spam-split-group))))))
929 (defun spam-ifile-register-with-ifile (article-string category)
930 "Register an article, given as a string, with a category.
931 Uses `gnus-newsgroup-name' if category is nil (for ham registration)."
932 (when (stringp article-string)
933 (let ((category (or category gnus-newsgroup-name))
934 (db-param (spam-get-ifile-database-parameter)))
936 (insert article-string)
938 (call-process-region (point-min) (point-max) spam-ifile-path
940 "-h" "-i" category db-param)
941 (call-process-region (point-min) (point-max) spam-ifile-path
943 "-h" "-i" category))))))
945 (defun spam-ifile-register-spam-routine ()
946 (spam-generic-register-routine
948 (spam-ifile-register-with-ifile
949 (spam-get-article-as-string article) spam-ifile-spam-category))
952 (defun spam-ifile-register-ham-routine ()
953 (spam-generic-register-routine
956 (spam-ifile-register-with-ifile
957 (spam-get-article-as-string article) spam-ifile-ham-category))))
964 (let ((spam-stat-install-hooks nil))
965 (require 'spam-stat))
967 (defun spam-check-stat ()
968 "Check the spam-stat backend for the classification of this message"
969 (let ((spam-stat-split-fancy-spam-group spam-split-group) ; override
970 (spam-stat-buffer (buffer-name)) ; stat the current buffer
972 (spam-stat-split-fancy)))
974 (defun spam-stat-register-spam-routine ()
975 (spam-generic-register-routine
977 (let ((article-string (spam-get-article-as-string article)))
979 (insert article-string)
980 (spam-stat-buffer-is-spam))))
983 (defun spam-stat-register-ham-routine ()
984 (spam-generic-register-routine
987 (let ((article-string (spam-get-article-as-string article)))
989 (insert article-string)
990 (spam-stat-buffer-is-non-spam))))))
992 (defun spam-maybe-spam-stat-load ()
993 (when spam-use-stat (spam-stat-load)))
995 (defun spam-maybe-spam-stat-save ()
996 (when spam-use-stat (spam-stat-save))))
999 (defalias 'spam-maybe-spam-stat-load 'ignore)
1000 (defalias 'spam-maybe-spam-stat-save 'ignore)
1001 (defalias 'spam-stat-register-ham-routine 'ignore)
1002 (defalias 'spam-stat-register-spam-routine 'ignore)
1003 (defalias 'spam-stat-buffer-is-spam 'ignore)
1004 (defalias 'spam-stat-buffer-is-non-spam 'ignore)
1005 (defalias 'spam-stat-split-fancy 'ignore)
1006 (defalias 'spam-stat-load 'ignore)
1007 (defalias 'spam-stat-save 'ignore)
1008 (defalias 'spam-check-stat 'ignore))))
1012 ;;;; Blacklists and whitelists.
1014 (defvar spam-whitelist-cache nil)
1015 (defvar spam-blacklist-cache nil)
1017 (defun spam-enter-whitelist (address)
1018 "Enter ADDRESS into the whitelist."
1019 (interactive "sAddress: ")
1020 (spam-enter-list address spam-whitelist)
1021 (setq spam-whitelist-cache nil))
1023 (defun spam-enter-blacklist (address)
1024 "Enter ADDRESS into the blacklist."
1025 (interactive "sAddress: ")
1026 (spam-enter-list address spam-blacklist)
1027 (setq spam-blacklist-cache nil))
1029 (defun spam-enter-list (address file)
1030 "Enter ADDRESS into the given FILE, either the whitelist or the blacklist."
1031 (unless (file-exists-p (file-name-directory file))
1032 (make-directory (file-name-directory file) t))
1035 (find-file-noselect file))
1036 (goto-char (point-min))
1037 (unless (re-search-forward (regexp-quote address) nil t)
1038 (goto-char (point-max))
1041 (insert address "\n")
1044 ;;; returns t if the sender is in the whitelist, nil or spam-split-group otherwise
1045 (defun spam-check-whitelist ()
1046 ;; FIXME! Should it detect when file timestamps change?
1047 (unless spam-whitelist-cache
1048 (setq spam-whitelist-cache (spam-parse-list spam-whitelist)))
1049 (if (spam-from-listed-p spam-whitelist-cache)
1051 (if spam-use-whitelist-exclusive
1055 (defun spam-check-blacklist ()
1056 ;; FIXME! Should it detect when file timestamps change?
1057 (unless spam-blacklist-cache
1058 (setq spam-blacklist-cache (spam-parse-list spam-blacklist)))
1059 (and (spam-from-listed-p spam-blacklist-cache) spam-split-group))
1061 (defun spam-parse-list (file)
1062 (when (file-readable-p file)
1063 (let (contents address)
1065 (insert-file-contents file)
1067 (setq address (buffer-substring (point) (spam-point-at-eol)))
1069 ;; insert the e-mail address if detected, otherwise the raw data
1070 (unless (zerop (length address))
1071 (let ((pure-address (cadr (gnus-extract-address-components address))))
1072 (push (or pure-address address) contents)))))
1073 (nreverse contents))))
1075 (defun spam-from-listed-p (cache)
1076 (let ((from (nnmail-fetch-field "from"))
1079 (let ((address (pop cache)))
1080 (unless (zerop (length address)) ; 0 for a nil address too
1081 (setq address (regexp-quote address))
1082 ;; fix regexp-quote's treatment of user-intended regexes
1083 (while (string-match "\\\\\\*" address)
1084 (setq address (replace-match ".*" t t address))))
1085 (when (and address (string-match address from))
1090 (defun spam-blacklist-register-routine ()
1091 (spam-generic-register-routine
1092 ;; the spam function
1094 (let ((from (spam-fetch-field-from-fast article)))
1095 (when (stringp from)
1096 (spam-enter-blacklist from))))
1100 (defun spam-whitelist-register-routine ()
1101 (spam-generic-register-routine
1102 ;; the spam function
1106 (let ((from (spam-fetch-field-from-fast article)))
1107 (when (stringp from)
1108 (spam-enter-whitelist from))))))
1111 ;;;; Spam-report glue
1112 (defun spam-report-gmane-register-routine ()
1113 (spam-generic-register-routine
1119 (defun spam-check-bogofilter-headers (&optional score)
1120 (let ((header (nnmail-fetch-field spam-bogofilter-header)))
1121 (when header ; return nil when no header
1122 (if score ; scoring mode
1123 (if (string-match "spamicity=\\([0-9.]+\\)" header)
1124 (match-string 1 header)
1126 ;; spam detection mode
1127 (when (string-match spam-bogofilter-bogosity-positive-spam-header
1129 spam-split-group)))))
1131 ;; return something sensible if the score can't be determined
1132 (defun spam-bogofilter-score ()
1133 "Get the Bogofilter spamicity score"
1135 (save-window-excursion
1136 (gnus-summary-show-article t)
1137 (set-buffer gnus-article-buffer)
1138 (let ((score (or (spam-check-bogofilter-headers t)
1139 (spam-check-bogofilter t))))
1140 (message "Spamicity score %s" score)
1142 (gnus-summary-show-article)))
1144 (defun spam-check-bogofilter (&optional score)
1145 "Check the Bogofilter backend for the classification of this message"
1146 (let ((article-buffer-name (buffer-name))
1149 (let ((temp-buffer-name (buffer-name)))
1151 (set-buffer article-buffer-name)
1152 (if spam-bogofilter-database-directory
1153 (call-process-region (point-min) (point-max)
1154 spam-bogofilter-path
1155 nil temp-buffer-name nil "-v"
1156 "-d" spam-bogofilter-database-directory)
1157 (call-process-region (point-min) (point-max) spam-bogofilter-path
1158 nil temp-buffer-name nil "-v")))
1159 (setq return (spam-check-bogofilter-headers score))))
1162 (defun spam-bogofilter-register-with-bogofilter (article-string spam)
1163 "Register an article, given as a string, as spam or non-spam."
1164 (when (stringp article-string)
1165 (let ((switch (if spam spam-bogofilter-spam-switch
1166 spam-bogofilter-ham-switch)))
1168 (insert article-string)
1169 (if spam-bogofilter-database-directory
1170 (call-process-region (point-min) (point-max)
1171 spam-bogofilter-path
1172 nil nil nil "-v" switch
1173 "-d" spam-bogofilter-database-directory)
1174 (call-process-region (point-min) (point-max) spam-bogofilter-path
1175 nil nil nil "-v" switch))))))
1177 (defun spam-bogofilter-register-spam-routine ()
1178 (spam-generic-register-routine
1180 (spam-bogofilter-register-with-bogofilter
1181 (spam-get-article-as-string article) t))
1184 (defun spam-bogofilter-register-ham-routine ()
1185 (spam-generic-register-routine
1188 (spam-bogofilter-register-with-bogofilter
1189 (spam-get-article-as-string article) nil))))
1193 (defun spam-check-spamoracle ()
1194 "Run spamoracle on an article to determine whether it's spam."
1195 (let ((article-buffer-name (buffer-name)))
1197 (let ((temp-buffer-name (buffer-name)))
1199 (set-buffer article-buffer-name)
1201 (apply 'call-process-region
1202 (point-min) (point-max)
1203 spam-spamoracle-binary
1204 nil temp-buffer-name nil
1205 (if spam-spamoracle-database
1206 `("-f" ,spam-spamoracle-database "mark")
1210 (set-buffer temp-buffer-name)
1211 (goto-char (point-min))
1212 (when (re-search-forward "^X-Spam: yes;" nil t)
1214 (error "Error running spamoracle" status))))))))
1216 (defun spam-spamoracle-learn (article article-is-spam-p)
1217 "Run spamoracle in training mode."
1219 (let ((temp-buffer-name (buffer-name)))
1221 (goto-char (point-min))
1222 (insert (spam-get-article-as-string article))
1223 (let* ((arg (if article-is-spam-p "-spam" "-good"))
1225 (apply 'call-process-region
1226 (point-min) (point-max)
1227 spam-spamoracle-binary
1228 nil temp-buffer-name nil
1229 (if spam-spamoracle-database
1230 `("-f" ,spam-spamoracle-database
1233 (when (not (zerop status))
1234 (error "Error running spamoracle" status)))))))
1236 (defun spam-spamoracle-learn-ham ()
1237 (spam-generic-register-routine
1240 (spam-spamoracle-learn article nil))))
1242 (defun spam-spamoracle-learn-spam ()
1243 (spam-generic-register-routine
1245 (spam-spamoracle-learn article t))
1251 (defun spam-initialize ()
1252 "Install the spam.el hooks and do other initialization"
1254 (setq spam-install-hooks t)
1255 ;; TODO: How do we redo this every time spam-face is customized?
1256 (push '((eq mark gnus-spam-mark) . spam-face)
1257 gnus-summary-highlight)
1258 ;; Add hooks for loading and saving the spam stats
1260 (add-hook 'gnus-save-newsrc-hook 'spam-maybe-spam-stat-save)
1261 (add-hook 'gnus-get-top-new-news-hook 'spam-maybe-spam-stat-load)
1262 (add-hook 'gnus-startup-hook 'spam-maybe-spam-stat-load))
1263 (add-hook 'gnus-summary-prepare-exit-hook 'spam-summary-prepare-exit)
1264 (add-hook 'gnus-summary-prepare-hook 'spam-summary-prepare)
1265 (add-hook 'gnus-get-new-news-hook 'spam-setup-widening))
1267 (defun spam-unload-hook ()
1268 "Uninstall the spam.el hooks"
1270 (remove-hook 'gnus-save-newsrc-hook 'spam-maybe-spam-stat-save)
1271 (remove-hook 'gnus-get-top-new-news-hook 'spam-maybe-spam-stat-load)
1272 (remove-hook 'gnus-startup-hook 'spam-maybe-spam-stat-load)
1273 (remove-hook 'gnus-summary-prepare-exit-hook 'spam-summary-prepare-exit)
1274 (remove-hook 'gnus-summary-prepare-hook 'spam-summary-prepare)
1275 (remove-hook 'gnus-get-new-news-hook 'spam-setup-widening))
1277 (when spam-install-hooks
1282 ;;; spam.el ends here.