1 ;;; spam.el --- Identifying spam
2 ;; Copyright (C) 2002, 2003, 2004 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
35 ;; TODO: spam scores, detection of spam in newsgroups, cross-server splitting,
36 ;; remote processing, training through files
40 (eval-when-compile (require 'cl))
44 (require 'gnus-uu) ; because of key prefix issues
45 ;;; for the definitions of group content classification and spam processors
47 (require 'message) ;for the message-fetch-field functions
49 ;; for nnimap-split-download-body-default
50 (eval-when-compile (require 'nnimap))
54 (autoload 'query-dig "dig"))
56 ;; autoload spam-report
58 (autoload 'spam-report-gmane "spam-report"))
60 ;; autoload gnus-registry
62 (autoload 'gnus-registry-group-count "gnus-registry")
63 (autoload 'gnus-registry-add-group "gnus-registry")
64 (autoload 'gnus-registry-store-extra-entry "gnus-registry")
65 (autoload 'gnus-registry-fetch-extra "gnus-registry"))
69 (autoload 'query-dns "dns"))
74 "Spam configuration.")
76 (defcustom spam-directory "~/News/spam/"
77 "Directory for spam whitelists and blacklists."
81 (defcustom spam-move-spam-nonspam-groups-only t
82 "Whether spam should be moved in non-spam groups only.
83 When t, only ham and unclassified groups will have their spam moved
84 to the spam-process-destination. When nil, spam will also be moved from
89 (defcustom spam-process-ham-in-nonham-groups nil
90 "Whether ham should be processed in non-ham groups."
94 (defcustom spam-log-to-registry nil
95 "Whether spam/ham processing should be logged in the registry."
99 (defcustom spam-split-symbolic-return nil
100 "Whether `spam-split' should work with symbols or group names."
104 (defcustom spam-split-symbolic-return-positive nil
105 "Whether `spam-split' should ALWAYS work with symbols or group names.
106 Do not set this if you use `spam-split' in a fancy split
111 (defcustom spam-process-ham-in-spam-groups nil
112 "Whether ham should be processed in spam groups."
116 (defcustom spam-mark-only-unseen-as-spam t
117 "Whether only unseen articles should be marked as spam in spam groups.
118 When nil, all unread articles in a spam group are marked as
119 spam. Set this if you want to leave an article unread in a spam group
120 without losing it to the automatic spam-marking process."
124 (defcustom spam-mark-ham-unread-before-move-from-spam-group nil
125 "Whether ham should be marked unread before it's moved.
126 The article is moved out of a spam group according to ham-process-destination.
127 This variable is an official entry in the international Longest Variable Name
132 (defcustom spam-disable-spam-split-during-ham-respool nil
133 "Whether `spam-split' should be ignored while resplitting ham.
134 This is useful to prevent ham from ending up in the same spam
135 group after the resplit. Don't set this to t if you have `spam-split' as the
136 last rule in your split configuration."
140 (defcustom spam-autodetect-recheck-messages nil
141 "Should spam.el recheck all meessages when autodetecting?
142 Normally this is nil, so only unseen messages will be checked."
146 (defcustom spam-whitelist (expand-file-name "whitelist" spam-directory)
147 "The location of the whitelist.
148 The file format is one regular expression per line.
149 The regular expression is matched against the address."
153 (defcustom spam-blacklist (expand-file-name "blacklist" spam-directory)
154 "The location of the blacklist.
155 The file format is one regular expression per line.
156 The regular expression is matched against the address."
160 (defcustom spam-use-dig t
161 "Whether `query-dig' should be used instead of `query-dns'."
165 (defcustom spam-use-blacklist nil
166 "Whether the blacklist should be used by `spam-split'."
170 (defcustom spam-blacklist-ignored-regexes nil
171 "Regular expressions that the blacklist should ignore."
172 :type '(repeat (regexp :tag "Regular expression to ignore when blacklisting"))
175 (defcustom spam-use-whitelist nil
176 "Whether the whitelist should be used by `spam-split'."
180 (defcustom spam-use-whitelist-exclusive nil
181 "Whether whitelist-exclusive should be used by `spam-split'.
182 Exclusive whitelisting means that all messages from senders not in the whitelist
183 are considered spam."
187 (defcustom spam-use-blackholes nil
188 "Whether blackholes should be used by `spam-split'."
192 (defcustom spam-use-hashcash nil
193 "Whether hashcash payments should be detected by `spam-split'."
197 (defcustom spam-use-regex-headers nil
198 "Whether a header regular expression match should be used by `spam-split'.
199 Also see the variables `spam-regex-headers-spam' and `spam-regex-headers-ham'."
203 (defcustom spam-use-regex-body nil
204 "Whether a body regular expression match should be used by `spam-split'.
205 Also see the variables `spam-regex-body-spam' and `spam-regex-body-ham'."
209 (defcustom spam-use-bogofilter-headers nil
210 "Whether bogofilter headers should be used by `spam-split'.
211 Enable this if you pre-process messages with Bogofilter BEFORE Gnus sees them."
215 (defcustom spam-use-bogofilter nil
216 "Whether bogofilter should be invoked by `spam-split'.
217 Enable this if you want Gnus to invoke Bogofilter on new messages."
221 (defcustom spam-use-BBDB nil
222 "Whether BBDB should be used by `spam-split'."
226 (defcustom spam-use-BBDB-exclusive nil
227 "Whether BBDB-exclusive should be used by `spam-split'.
228 Exclusive BBDB means that all messages from senders not in the BBDB are
233 (defcustom spam-use-ifile nil
234 "Whether ifile should be used by `spam-split'."
238 (defcustom spam-use-stat nil
239 "Whether `spam-stat' should be used by `spam-split'."
243 (defcustom spam-use-spamoracle nil
244 "Whether spamoracle should be used by `spam-split'."
248 (defcustom spam-use-spamassassin nil
249 "Whether spamassassin should be invoked by `spam-split'.
250 Enable this if you want Gnus to invoke SpamAssassin on new messages."
254 (defcustom spam-use-spamassassin-headers nil
255 "Whether spamassassin headers should be checked by `spam-split'.
256 Enable this if you pre-process messages with SpamAssassin BEFORE Gnus sees
261 (defcustom spam-install-hooks (or
265 spam-use-whitelist-exclusive
268 spam-use-regex-headers
271 spam-use-bogofilter-headers
272 spam-use-spamassassin
273 spam-use-spamassassin-headers
275 spam-use-BBDB-exclusive
279 "Whether the spam hooks should be installed.
280 Default to t if one of the spam-use-* variables is set."
284 (defcustom spam-split-group "spam"
285 "Group name where incoming spam should be put by `spam-split'."
289 ;;; TODO: deprecate this variable, it's confusing since it's a list of strings,
290 ;;; not regular expressions
291 (defcustom spam-junk-mailgroups (cons
293 '("mail.junk" "poste.pourriel"))
294 "Mailgroups with spam contents.
295 All unmarked article in such group receive the spam mark on group entry."
296 :type '(repeat (string :tag "Group"))
299 (defcustom spam-blackhole-servers '("bl.spamcop.net" "relays.ordb.org"
300 "dev.null.dk" "relays.visi.com")
301 "List of blackhole servers."
302 :type '(repeat (string :tag "Server"))
305 (defcustom spam-blackhole-good-server-regex nil
306 "String matching IP addresses that should not be checked in the blackholes."
307 :type '(radio (const nil)
308 (regexp :format "%t: %v\n" :size 0))
311 (defcustom spam-face 'gnus-splash-face
312 "Face for spam-marked articles."
316 (defcustom spam-regex-headers-spam '("^X-Spam-Flag: YES")
317 "Regular expression for positive header spam matches."
318 :type '(repeat (regexp :tag "Regular expression to match spam header"))
321 (defcustom spam-regex-headers-ham '("^X-Spam-Flag: NO")
322 "Regular expression for positive header ham matches."
323 :type '(repeat (regexp :tag "Regular expression to match ham header"))
326 (defcustom spam-regex-body-spam '()
327 "Regular expression for positive body spam matches."
328 :type '(repeat (regexp :tag "Regular expression to match spam body"))
331 (defcustom spam-regex-body-ham '()
332 "Regular expression for positive body ham matches."
333 :type '(repeat (regexp :tag "Regular expression to match ham body"))
336 (defgroup spam-ifile nil
337 "Spam ifile configuration."
340 (defcustom spam-ifile-path (exec-installed-p "ifile")
341 "File path of the ifile executable program."
342 :type '(choice (file :tag "Location of ifile")
343 (const :tag "ifile is not installed"))
346 (defcustom spam-ifile-database-path nil
347 "File path of the ifile database."
348 :type '(choice (file :tag "Location of the ifile database")
349 (const :tag "Use the default"))
352 (defcustom spam-ifile-spam-category "spam"
353 "Name of the spam ifile category."
357 (defcustom spam-ifile-ham-category nil
358 "Name of the ham ifile category.
359 If nil, the current group name will be used."
360 :type '(choice (string :tag "Use a fixed category")
361 (const :tag "Use the current group name"))
364 (defcustom spam-ifile-all-categories nil
365 "Whether the ifile check will return all categories, or just spam.
366 Set this to t if you want to use the `spam-split' invocation of ifile as
367 your main source of newsgroup names."
371 (defgroup spam-bogofilter nil
372 "Spam bogofilter configuration."
375 (defcustom spam-bogofilter-path (exec-installed-p "bogofilter")
376 "File path of the Bogofilter executable program."
377 :type '(choice (file :tag "Location of bogofilter")
378 (const :tag "Bogofilter is not installed"))
379 :group 'spam-bogofilter)
381 (defcustom spam-bogofilter-header "X-Bogosity"
382 "The header that Bogofilter inserts in messages."
384 :group 'spam-bogofilter)
386 (defcustom spam-bogofilter-spam-switch "-s"
387 "The switch that Bogofilter uses to register spam messages."
389 :group 'spam-bogofilter)
391 (defcustom spam-bogofilter-ham-switch "-n"
392 "The switch that Bogofilter uses to register ham messages."
394 :group 'spam-bogofilter)
396 (defcustom spam-bogofilter-spam-strong-switch "-S"
397 "The switch that Bogofilter uses to unregister ham messages."
399 :group 'spam-bogofilter)
401 (defcustom spam-bogofilter-ham-strong-switch "-N"
402 "The switch that Bogofilter uses to unregister spam messages."
404 :group 'spam-bogofilter)
406 (defcustom spam-bogofilter-bogosity-positive-spam-header "^\\(Yes\\|Spam\\)"
407 "The regex on `spam-bogofilter-header' for positive spam identification."
409 :group 'spam-bogofilter)
411 (defcustom spam-bogofilter-database-directory nil
412 "Directory path of the Bogofilter databases."
413 :type '(choice (directory
414 :tag "Location of the Bogofilter database directory")
415 (const :tag "Use the default"))
416 :group 'spam-bogofilter)
418 (defgroup spam-spamoracle nil
419 "Spam spamoracle configuration."
422 (defcustom spam-spamoracle-database nil
423 "Location of spamoracle database file.
424 When nil, use the default spamoracle database."
425 :type '(choice (directory :tag "Location of spamoracle database file.")
426 (const :tag "Use the default"))
427 :group 'spam-spamoracle)
429 (defcustom spam-spamoracle-binary (executable-find "spamoracle")
430 "Location of the spamoracle binary."
431 :type '(choice (directory :tag "Location of the spamoracle binary")
432 (const :tag "Use the default"))
433 :group 'spam-spamoracle)
435 (defgroup spam-spamassassin nil
436 "Spam SpamAssassin configuration."
439 (defcustom spam-spamassassin-path (executable-find "spamassassin")
440 "File path of the spamassassin executable program.
441 Hint: set this to \"spamc\" if you have spamd running. See the spamc and
442 spamd man pages for more information on these programs."
443 :type '(choice (file :tag "Location of spamc")
444 (const :tag "spamassassin is not installed"))
445 :group 'spam-spamassassin)
447 (defcustom spam-spamassassin-arguments ()
448 "Arguments to pass to the spamassassin executable.
449 This must be a list. For example, `(\"-C\" \"configfile\")'."
450 :type '(restricted-sexp :match-alternatives (listp))
451 :group 'spam-spamassassin)
453 (defcustom spam-spamassassin-spam-flag-header "X-Spam-Flag"
454 "The header inserted by SpamAssassin to flag spam."
456 :group 'spam-spamassassin)
458 (defcustom spam-spamassassin-positive-spam-flag-header "YES"
459 "The regex on `spam-spamassassin-spam-flag-header' for positive spam
462 :group 'spam-spamassassin)
464 (defcustom spam-spamassassin-spam-status-header "X-Spam-Status"
465 "The header inserted by SpamAssassin, giving extended scoring information"
467 :group 'spam-spamassassin)
469 (defcustom spam-sa-learn-path (executable-find "sa-learn")
470 "File path of the sa-learn executable program."
471 :type '(choice (file :tag "Location of spamassassin")
472 (const :tag "spamassassin is not installed"))
473 :group 'spam-spamassassin)
475 (defcustom spam-sa-learn-rebuild t
476 "Whether sa-learn should rebuild the database every time it is called
477 Enable this if you want sa-learn to rebuild the database automatically. Doing
478 this will slightly increase the running time of the spam registration process.
479 If you choose not to do this, you will have to run \"sa-learn --rebuild\" in
480 order for SpamAssassin to recognize the new registered spam."
482 :group 'spam-spamassassin)
484 (defcustom spam-sa-learn-spam-switch "--spam"
485 "The switch that sa-learn uses to register spam messages"
487 :group 'spam-spamassassin)
489 (defcustom spam-sa-learn-ham-switch "--ham"
490 "The switch that sa-learn uses to register ham messages"
492 :group 'spam-spamassassin)
494 (defcustom spam-sa-learn-unregister-switch "--forget"
495 "The switch that sa-learn uses to unregister messages messages"
497 :group 'spam-spamassassin)
499 ;;; Key bindings for spam control.
501 (gnus-define-keys gnus-summary-mode-map
502 "St" spam-generic-score
503 "Sx" gnus-summary-mark-as-spam
504 "Mst" spam-generic-score
505 "Msx" gnus-summary-mark-as-spam
506 "\M-d" gnus-summary-mark-as-spam)
508 (defvar spam-cache-lookups t
509 "Whether spam.el will try to cache lookups using `spam-caches'.")
511 (defvar spam-caches (make-hash-table
514 "Cache of spam detection entries.")
516 (defvar spam-old-ham-articles nil
517 "List of old ham articles, generated when a group is entered.")
519 (defvar spam-old-spam-articles nil
520 "List of old spam articles, generated when a group is entered.")
522 (defvar spam-split-disabled nil
523 "If non-nil, `spam-split' is disabled, and always returns nil.")
525 (defvar spam-split-last-successful-check nil
527 `spam-split' will set this to nil or a spam-use-XYZ check if it
530 ;; convenience functions
531 (defun spam-clear-cache (symbol)
532 "Clear the spam-caches entry for a check."
533 (remhash symbol spam-caches))
535 (defun spam-xor (a b)
537 (and (or a b) (not (and a b))))
539 (defun spam-group-ham-mark-p (group mark &optional spam)
540 "Checks if MARK is considered a ham mark in GROUP."
541 (when (stringp group)
542 (let* ((marks (spam-group-ham-marks group spam))
543 (marks (if (symbolp mark)
545 (mapcar 'symbol-value marks))))
548 (defun spam-group-spam-mark-p (group mark)
549 "Checks if MARK is considered a spam mark in GROUP."
550 (spam-group-ham-mark-p group mark t))
552 (defun spam-group-ham-marks (group &optional spam)
553 "In GROUP, get all the ham marks."
554 (when (stringp group)
555 (let* ((marks (if spam
556 (gnus-parameter-spam-marks group)
557 (gnus-parameter-ham-marks group)))
559 (marks (if (listp (car marks)) (car marks) marks)))
562 (defun spam-group-spam-marks (group)
563 "In GROUP, get all the spam marks."
564 (spam-group-ham-marks group t))
566 (defun spam-group-spam-contents-p (group)
567 "Is GROUP a spam group?"
569 (or (member group spam-junk-mailgroups)
570 (memq 'gnus-group-spam-classification-spam
571 (gnus-parameter-spam-contents group)))
574 (defun spam-group-ham-contents-p (group)
575 "Is GROUP a ham group?"
577 (memq 'gnus-group-spam-classification-ham
578 (gnus-parameter-spam-contents group))
581 (defvar spam-list-of-processors
582 '((gnus-group-spam-exit-processor-report-gmane spam spam-use-gmane)
583 (gnus-group-spam-exit-processor-bogofilter spam spam-use-bogofilter)
584 (gnus-group-spam-exit-processor-blacklist spam spam-use-blacklist)
585 (gnus-group-spam-exit-processor-ifile spam spam-use-ifile)
586 (gnus-group-spam-exit-processor-stat spam spam-use-stat)
587 (gnus-group-spam-exit-processor-spamoracle spam spam-use-spamoracle)
588 (gnus-group-spam-exit-processor-spamassassin spam spam-use-spam-spamassassin)
589 (gnus-group-ham-exit-processor-ifile ham spam-use-ifile)
590 (gnus-group-ham-exit-processor-bogofilter ham spam-use-bogofilter)
591 (gnus-group-ham-exit-processor-stat ham spam-use-stat)
592 (gnus-group-ham-exit-processor-whitelist ham spam-use-whitelist)
593 (gnus-group-ham-exit-processor-BBDB ham spam-use-BBDB)
594 (gnus-group-ham-exit-processor-copy ham spam-use-ham-copy)
595 (gnus-group-ham-exit-processor-spamassassin ham spam-use-ham-spamassassin)
596 (gnus-group-ham-exit-processor-spamoracle ham spam-use-spamoracle))
597 "The `spam-list-of-processors' list.
598 This list contains pairs associating a ham/spam exit processor
599 variable with a classification and a spam-use-* variable.")
601 (defun spam-group-processor-p (group processor)
602 (if (and (stringp group)
604 (or (member processor (nth 0 (gnus-parameter-spam-process group)))
605 (spam-group-processor-multiple-p
607 (cdr-safe (assoc processor spam-list-of-processors))))
610 (defun spam-group-processor-multiple-p (group processor-info)
611 (let* ((classification (nth 0 processor-info))
612 (check (nth 1 processor-info))
613 (parameters (nth 0 (gnus-parameter-spam-process group)))
615 (dolist (parameter parameters)
616 (when (and (null found)
618 (eq classification (nth 0 parameter))
619 (eq check (nth 1 parameter)))
623 (defun spam-group-spam-processor-report-gmane-p (group)
624 (spam-group-processor-p group 'gnus-group-spam-exit-processor-report-gmane))
626 (defun spam-group-spam-processor-bogofilter-p (group)
627 (spam-group-processor-p group 'gnus-group-spam-exit-processor-bogofilter))
629 (defun spam-group-spam-processor-blacklist-p (group)
630 (spam-group-processor-p group 'gnus-group-spam-exit-processor-blacklist))
632 (defun spam-group-spam-processor-ifile-p (group)
633 (spam-group-processor-p group 'gnus-group-spam-exit-processor-ifile))
635 (defun spam-group-ham-processor-ifile-p (group)
636 (spam-group-processor-p group 'gnus-group-ham-exit-processor-ifile))
638 (defun spam-group-spam-processor-spamoracle-p (group)
639 (spam-group-processor-p group 'gnus-group-spam-exit-processor-spamoracle))
641 (defun spam-group-ham-processor-bogofilter-p (group)
642 (spam-group-processor-p group 'gnus-group-ham-exit-processor-bogofilter))
644 (defun spam-group-spam-processor-stat-p (group)
645 (spam-group-processor-p group 'gnus-group-spam-exit-processor-stat))
647 (defun spam-group-ham-processor-stat-p (group)
648 (spam-group-processor-p group 'gnus-group-ham-exit-processor-stat))
650 (defun spam-group-ham-processor-whitelist-p (group)
651 (spam-group-processor-p group 'gnus-group-ham-exit-processor-whitelist))
653 (defun spam-group-ham-processor-BBDB-p (group)
654 (spam-group-processor-p group 'gnus-group-ham-exit-processor-BBDB))
656 (defun spam-group-ham-processor-copy-p (group)
657 (spam-group-processor-p group 'gnus-group-ham-exit-processor-copy))
659 (defun spam-group-ham-processor-spamoracle-p (group)
660 (spam-group-processor-p group 'gnus-group-ham-exit-processor-spamoracle))
662 (defun spam-report-articles-gmane (n)
663 "Report the current message as spam.
664 Respects the process/prefix convention."
666 (dolist (article (gnus-summary-work-articles n))
667 (gnus-summary-remove-process-mark article)
668 (spam-report-gmane article)))
670 (defun spam-generic-score ()
672 "Invoke whatever scoring method we can."
674 spam-use-spamassassin
675 spam-use-spamassassin-headers)
676 (spam-spamassassin-score)
677 (spam-bogofilter-score)))
679 ;;; Summary entry and exit processing.
681 (defun spam-summary-prepare ()
682 (setq spam-old-ham-articles
683 (spam-list-articles gnus-newsgroup-articles 'ham))
684 (setq spam-old-spam-articles
685 (spam-list-articles gnus-newsgroup-articles 'spam))
686 (spam-mark-junk-as-spam-routine))
688 ;; The spam processors are invoked for any group, spam or ham or neither
689 (defun spam-summary-prepare-exit ()
690 (unless gnus-group-is-exiting-without-update-p
691 (gnus-message 6 "Exiting summary buffer and applying spam rules")
693 ;; first of all, unregister any articles that are no longer ham or spam
694 ;; we have to iterate over the processors, or else we'll be too slow
695 (dolist (classification '(spam ham))
696 (let* ((old-articles (if (eq classification 'spam)
697 spam-old-spam-articles
698 spam-old-ham-articles))
699 (new-articles (spam-list-articles
700 gnus-newsgroup-articles
702 (changed-articles (gnus-set-difference old-articles new-articles)))
703 ;; now that we have the changed articles, we go through the processors
704 (dolist (processor-param spam-list-of-processors)
705 (let ((processor (nth 0 processor-param))
706 (processor-classification (nth 1 processor-param))
707 (check (nth 2 processor-param))
709 (dolist (article changed-articles)
710 (let ((id (spam-fetch-field-message-id-fast article)))
711 (when (spam-log-unregistration-needed-p
712 id 'process classification check)
713 (push article unregister-list))))
714 ;; call spam-register-routine with specific articles to unregister,
715 ;; when there are articles to unregister and the check is enabled
716 (when (and unregister-list (symbol-value check))
717 (spam-register-routine classification check t unregister-list))))))
719 ;; find all the spam processors applicable to this group
720 (dolist (processor-param spam-list-of-processors)
721 (let ((processor (nth 0 processor-param))
722 (classification (nth 1 processor-param))
723 (check (nth 2 processor-param)))
724 (when (and (eq 'spam classification)
725 (spam-group-processor-p gnus-newsgroup-name processor))
726 (spam-register-routine classification check))))
728 (if spam-move-spam-nonspam-groups-only
729 (when (not (spam-group-spam-contents-p gnus-newsgroup-name))
730 (spam-mark-spam-as-expired-and-move-routine
731 (gnus-parameter-spam-process-destination gnus-newsgroup-name)))
732 (gnus-message 5 "Marking spam as expired and moving it to %s"
734 (spam-mark-spam-as-expired-and-move-routine
735 (gnus-parameter-spam-process-destination gnus-newsgroup-name)))
737 ;; now we redo spam-mark-spam-as-expired-and-move-routine to only
738 ;; expire spam, in case the above did not expire them
739 (gnus-message 5 "Marking spam as expired without moving it")
740 (spam-mark-spam-as-expired-and-move-routine nil)
742 (when (or (spam-group-ham-contents-p gnus-newsgroup-name)
743 (and (spam-group-spam-contents-p gnus-newsgroup-name)
744 spam-process-ham-in-spam-groups)
745 spam-process-ham-in-nonham-groups)
746 ;; find all the ham processors applicable to this group
747 (dolist (processor-param spam-list-of-processors)
748 (let ((processor (nth 0 processor-param))
749 (classification (nth 1 processor-param))
750 (check (nth 2 processor-param)))
751 (when (and (eq 'ham classification)
752 (spam-group-processor-p gnus-newsgroup-name processor))
753 (spam-register-routine classification check)))))
755 (when (spam-group-ham-processor-copy-p gnus-newsgroup-name)
756 (gnus-message 5 "Copying ham")
757 (spam-ham-copy-routine
758 (gnus-parameter-ham-process-destination gnus-newsgroup-name)))
760 ;; now move all ham articles out of spam groups
761 (when (spam-group-spam-contents-p gnus-newsgroup-name)
762 (gnus-message 5 "Moving ham messages from spam group")
763 (spam-ham-move-routine
764 (gnus-parameter-ham-process-destination gnus-newsgroup-name))))
766 (setq spam-old-ham-articles nil)
767 (setq spam-old-spam-articles nil))
769 (defun spam-mark-junk-as-spam-routine ()
770 ;; check the global list of group names spam-junk-mailgroups and the
772 (when (spam-group-spam-contents-p gnus-newsgroup-name)
773 (gnus-message 5 "Marking %s articles as spam"
774 (if spam-mark-only-unseen-as-spam
777 (let ((articles (if spam-mark-only-unseen-as-spam
778 gnus-newsgroup-unseen
779 gnus-newsgroup-unreads)))
780 (dolist (article articles)
781 (gnus-summary-mark-article article gnus-spam-mark)))))
783 (defun spam-mark-spam-as-expired-and-move-routine (&rest groups)
784 (if (and (car-safe groups) (listp (car-safe groups)))
785 (apply 'spam-mark-spam-as-expired-and-move-routine (car groups))
786 (gnus-summary-kill-process-mark)
787 (let ((articles gnus-newsgroup-articles)
788 (backend-supports-deletions
789 (gnus-check-backend-function
790 'request-move-article gnus-newsgroup-name))
791 article tomove deletep)
792 (dolist (article articles)
793 (when (eq (gnus-summary-article-mark article) gnus-spam-mark)
794 (gnus-summary-mark-article article gnus-expirable-mark)
795 (push article tomove)))
797 ;; now do the actual copies
798 (dolist (group groups)
801 (dolist (article tomove)
802 (gnus-summary-set-process-mark article))
804 (if (or (not backend-supports-deletions)
805 (> (length groups) 1))
807 (gnus-summary-copy-article nil group)
809 (gnus-summary-move-article nil group)))))
811 ;; now delete the articles, if there was a copy done, and the
813 (when (and deletep backend-supports-deletions)
814 (dolist (article tomove)
815 (gnus-summary-set-process-mark article))
817 (let ((gnus-novice-user nil)) ; don't ask me if I'm sure
818 (gnus-summary-delete-article nil))))
820 (gnus-summary-yank-process-mark))))
822 (defun spam-ham-copy-or-move-routine (copy groups)
823 (gnus-summary-kill-process-mark)
824 (let ((todo (spam-list-articles gnus-newsgroup-articles 'ham))
825 (backend-supports-deletions
826 (gnus-check-backend-function
827 'request-move-article gnus-newsgroup-name))
828 (respool-method (gnus-find-method-for-group gnus-newsgroup-name))
829 article mark todo deletep respool)
831 (when (member 'respool groups)
832 (setq respool t) ; boolean for later
833 (setq groups '("fake"))) ; when respooling, groups are dynamic so fake it
835 ;; now do the actual move
836 (dolist (group groups)
837 (when (and todo (stringp group))
838 (dolist (article todo)
839 (when spam-mark-ham-unread-before-move-from-spam-group
840 (gnus-summary-mark-article article gnus-unread-mark))
841 (gnus-summary-set-process-mark article))
843 (if respool ; respooling is with a "fake" group
844 (let ((spam-split-disabled
845 (or spam-split-disabled
846 spam-disable-spam-split-during-ham-respool)))
847 (gnus-summary-respool-article nil respool-method))
848 (if (or (not backend-supports-deletions) ; else, we are not respooling
849 (> (length groups) 1))
850 (progn ; if copying, copy and set deletep
851 (gnus-summary-copy-article nil group)
853 (gnus-summary-move-article nil group))))) ; else move articles
855 ;; now delete the articles, unless a) copy is t, and there was a copy done
856 ;; b) a move was done to a single group
857 ;; c) backend-supports-deletions is nil
859 (when (and deletep backend-supports-deletions)
860 (dolist (article todo)
861 (gnus-summary-set-process-mark article))
863 (let ((gnus-novice-user nil)) ; don't ask me if I'm sure
864 (gnus-summary-delete-article nil))))))
866 (gnus-summary-yank-process-mark))
868 (defun spam-ham-copy-routine (&rest groups)
869 (if (and (car-safe groups) (listp (car-safe groups)))
870 (apply 'spam-ham-copy-routine (car groups))
871 (spam-ham-copy-or-move-routine t groups)))
873 (defun spam-ham-move-routine (&rest groups)
874 (if (and (car-safe groups) (listp (car-safe groups)))
875 (apply 'spam-ham-move-routine (car groups))
876 (spam-ham-copy-or-move-routine nil groups)))
878 (defun spam-get-article-as-string (article)
879 (when (numberp article)
881 (gnus-request-article-this-buffer
887 ;; (defun spam-get-article-as-filename (article)
888 ;; (let ((article-filename))
889 ;; (when (numberp article)
890 ;; (nnml-possibly-change-directory
891 ;; (gnus-group-real-name gnus-newsgroup-name))
892 ;; (setq article-filename (expand-file-name
893 ;; (int-to-string article) nnml-current-directory)))
894 ;; (if (file-exists-p article-filename)
898 (defun spam-fetch-field-fast (article field &optional prepared-data-header)
899 "Fetch a field quickly, using the internal gnus-data-list function"
900 (when (numberp article)
901 (let* ((data-header (or prepared-data-header
902 (spam-fetch-article-header article))))
903 (if (arrayp data-header)
906 (mail-header-from data-header))
907 ((equal field 'message-id)
908 (mail-header-message-id data-header))
909 ((equal field 'subject)
910 (mail-header-subject data-header))
911 ((equal field 'references)
912 (mail-header-references data-header))
914 (mail-header-date data-header))
916 (mail-header-xref data-header))
917 ((equal field 'extra)
918 (mail-header-extra data-header))
921 (gnus-error 5 "Article %d has a nil data header" article)))))
923 (defun spam-fetch-field-from-fast (article &optional prepared-data-header)
924 (spam-fetch-field-fast article 'from prepared-data-header))
926 (defun spam-fetch-field-subject-fast (article &optional prepared-data-header)
927 (spam-fetch-field-fast article 'subject prepared-data-header))
929 (defun spam-fetch-field-message-id-fast (article &optional prepared-data-header)
930 (spam-fetch-field-fast article 'message-id prepared-data-header))
932 (defun spam-generate-fake-headers (article)
933 (let ((dh (spam-fetch-article-header article)))
937 (concat "From: %s\nSubject: %s\nMessage-ID: %s\n"
938 "Date: %s\nReferences: %s\nXref: %s\n")
939 (spam-fetch-field-fast article 'from dh)
940 (spam-fetch-field-fast article 'subject dh)
941 (spam-fetch-field-fast article 'message-id dh)
942 (spam-fetch-field-fast article 'date dh)
943 (spam-fetch-field-fast article 'references dh)
944 (spam-fetch-field-fast article 'xref dh))
945 (when (spam-fetch-field-fast article 'extra dh)
946 (format "%s\n" (spam-fetch-field-fast article 'extra dh))))
949 "spam-generate-fake-headers: article %d didn't have a valid header"
952 (defun spam-fetch-article-header (article)
954 (set-buffer gnus-summary-buffer)
955 (nth 3 (assq article gnus-newsgroup-data))))
958 ;;;; Spam determination.
960 (defvar spam-list-of-checks
961 '((spam-use-blacklist . spam-check-blacklist)
962 (spam-use-regex-headers . spam-check-regex-headers)
963 (spam-use-regex-body . spam-check-regex-body)
964 (spam-use-whitelist . spam-check-whitelist)
965 (spam-use-BBDB . spam-check-BBDB)
966 (spam-use-ifile . spam-check-ifile)
967 (spam-use-spamoracle . spam-check-spamoracle)
968 (spam-use-stat . spam-check-stat)
969 (spam-use-blackholes . spam-check-blackholes)
970 (spam-use-hashcash . spam-check-hashcash)
971 (spam-use-spamassassin-headers . spam-check-spamassassin-headers)
972 (spam-use-spamassassin . spam-check-spamassassin)
973 (spam-use-bogofilter-headers . spam-check-bogofilter-headers)
974 (spam-use-bogofilter . spam-check-bogofilter))
975 "The spam-list-of-checks list contains pairs associating a
976 parameter variable with a spam checking function. If the
977 parameter variable is true, then the checking function is called,
978 and its value decides what happens. Each individual check may
979 return nil, t, or a mailgroup name. The value nil means that the
980 check does not yield a decision, and so, that further checks are
981 needed. The value t means that the message is definitely not
982 spam, and that further spam checks should be inhibited.
983 Otherwise, a mailgroup name or the symbol 'spam (depending on
984 spam-split-symbolic-return) is returned where the mail should go,
985 and further checks are also inhibited. The usual mailgroup name
986 is the value of `spam-split-group', meaning that the message is
989 (defvar spam-list-of-statistical-checks
995 spam-use-spamassassin
997 "The spam-list-of-statistical-checks list contains all the mail
998 splitters that need to have the full message body available.
999 Note that you should fetch extra headers if you don't like this,
1000 e.g. fetch the 'Received' header for spam-use-blackholes.")
1002 (defun spam-split (&rest specific-checks)
1003 "Split this message into the `spam' group if it is spam.
1004 This function can be used as an entry in the variable `nnmail-split-fancy',
1005 for example like this: (: spam-split). It can take checks as
1006 parameters. A string as a parameter will set the
1007 spam-split-group to that string.
1009 See the Info node `(gnus)Fancy Mail Splitting' for more details."
1011 (setq spam-split-last-successful-check nil)
1012 (unless spam-split-disabled
1013 (let ((spam-split-group-choice spam-split-group))
1014 (dolist (check specific-checks)
1015 (when (stringp check)
1016 (setq spam-split-group-choice check)
1017 (setq specific-checks (delq check specific-checks))))
1019 (let ((spam-split-group spam-split-group-choice))
1022 (dolist (check spam-list-of-statistical-checks)
1023 (when (and (symbolp check)
1024 (or (symbol-value check)
1025 (memq check specific-checks)))
1027 (gnus-message 8 "spam-split: widening the buffer (%s requires it)"
1028 (symbol-name check))
1030 ;; (progn (widen) (debug (buffer-string)))
1031 (let ((list-of-checks spam-list-of-checks)
1033 (while (and list-of-checks (not decision))
1034 (let ((pair (pop list-of-checks)))
1036 ;; either, given specific checks, this is one of them
1037 (and specific-checks (memq (car pair) specific-checks))
1038 ;; or, given no specific checks, spam-use-CHECK is set
1039 (and (null specific-checks) (symbol-value (car pair))))
1040 (gnus-message 5 "spam-split: calling the %s function"
1041 (symbol-name (cdr pair)))
1042 (setq decision (funcall (cdr pair)))
1043 ;; if we got a decision at all, save the current check
1045 (setq spam-split-last-successful-check (car pair)))
1047 (when (eq decision 'spam)
1048 (unless spam-split-symbolic-return
1051 (format "spam-split got %s but %s is nil"
1052 (symbol-name decision)
1053 (symbol-name spam-split-symbolic-return))))))))
1055 (if spam-split-symbolic-return-positive 'ham nil)
1058 (defun spam-find-spam ()
1059 "This function will detect spam in the current newsgroup using spam-split."
1062 (let* ((group gnus-newsgroup-name)
1063 (autodetect (gnus-parameter-spam-autodetect group))
1064 (methods (gnus-parameter-spam-autodetect-methods group))
1065 (first-method (nth 0 methods))
1066 (articles (if spam-autodetect-recheck-messages
1067 gnus-newsgroup-articles
1068 gnus-newsgroup-unseen))
1069 article-cannot-be-faked)
1071 (dolist (check spam-list-of-statistical-checks)
1072 (when (and (symbolp check)
1073 (memq check methods))
1074 (setq article-cannot-be-faked t)
1077 (when (memq 'default methods)
1078 (setq article-cannot-be-faked t))
1080 (when (and autodetect
1081 (not (equal first-method 'none)))
1084 (let ((id (spam-fetch-field-message-id-fast article))
1085 (subject (spam-fetch-field-subject-fast article))
1086 (sender (spam-fetch-field-from-fast article))
1090 (gnus-error 5 "Article %d has no message ID!" article))
1092 (when (and id spam-log-to-registry)
1093 (setq registry-lookup (spam-log-registration-type id 'incoming))
1094 (when registry-lookup
1097 "spam-find-spam: message %s was already registered incoming"
1100 (let* ((spam-split-symbolic-return t)
1101 (spam-split-symbolic-return-positive t)
1102 (fake-headers (spam-generate-fake-headers article))
1106 (if article-cannot-be-faked
1107 (gnus-request-article-this-buffer
1110 ;; else, we fake the article
1111 (when fake-headers (insert fake-headers)))
1112 (if (or (null first-method)
1113 (equal first-method 'default))
1115 (apply 'spam-split methods))))))
1116 (if (equal split-return 'spam)
1117 (gnus-summary-mark-article article gnus-spam-mark))
1119 (when (and id split-return spam-log-to-registry)
1120 (when (zerop (gnus-registry-group-count id))
1121 (gnus-registry-add-group
1122 id group subject sender))
1124 (unless registry-lookup
1125 (spam-log-processing-to-registry
1129 spam-split-last-successful-check
1133 (defvar spam-registration-functions
1134 ;; first the ham register, second the spam register function
1135 ;; third the ham unregister, fourth the spam unregister function
1136 '((spam-use-blacklist nil
1137 spam-blacklist-register-routine
1139 spam-blacklist-unregister-routine)
1140 (spam-use-whitelist spam-whitelist-register-routine
1142 spam-whitelist-unregister-routine
1144 (spam-use-BBDB spam-BBDB-register-routine
1146 spam-BBDB-unregister-routine
1148 (spam-use-ifile spam-ifile-register-ham-routine
1149 spam-ifile-register-spam-routine
1150 spam-ifile-unregister-ham-routine
1151 spam-ifile-unregister-spam-routine)
1152 (spam-use-spamoracle spam-spamoracle-learn-ham
1153 spam-spamoracle-learn-spam
1154 spam-spamoracle-unlearn-ham
1155 spam-spamoracle-unlearn-spam)
1156 (spam-use-stat spam-stat-register-ham-routine
1157 spam-stat-register-spam-routine
1158 spam-stat-unregister-ham-routine
1159 spam-stat-unregister-spam-routine)
1160 ;; note that spam-use-gmane is not a legitimate check
1162 spam-report-gmane-register-routine
1163 ;; does Gmane support unregistration?
1166 (spam-use-spamassassin spam-spamassassin-register-ham-routine
1167 spam-spamassassin-register-spam-routine
1168 spam-spamassassin-unregister-ham-routine
1169 spam-spamassassin-unregister-spam-routine)
1170 (spam-use-bogofilter spam-bogofilter-register-ham-routine
1171 spam-bogofilter-register-spam-routine
1172 spam-bogofilter-unregister-ham-routine
1173 spam-bogofilter-unregister-spam-routine))
1174 "The spam-registration-functions list contains pairs
1175 associating a parameter variable with the ham and spam
1176 registration functions, and the ham and spam unregistration
1179 (defun spam-classification-valid-p (classification)
1180 (or (eq classification 'spam)
1181 (eq classification 'ham)))
1183 (defun spam-process-type-valid-p (process-type)
1184 (or (eq process-type 'incoming)
1185 (eq process-type 'process)))
1187 (defun spam-registration-check-valid-p (check)
1188 (assoc check spam-registration-functions))
1190 (defun spam-unregistration-check-valid-p (check)
1191 (assoc check spam-registration-functions))
1193 (defun spam-registration-function (classification check)
1194 (let ((flist (cdr-safe (assoc check spam-registration-functions))))
1195 (if (eq classification 'spam)
1199 (defun spam-unregistration-function (classification check)
1200 (let ((flist (cdr-safe (assoc check spam-registration-functions))))
1201 (if (eq classification 'spam)
1205 (defun spam-list-articles (articles classification)
1206 (let ((mark-check (if (eq classification 'spam)
1207 'spam-group-spam-mark-p
1208 'spam-group-ham-mark-p))
1209 list mark-cache-yes mark-cache-no)
1210 (dolist (article articles)
1211 (let ((mark (gnus-summary-article-mark article)))
1212 (unless (memq mark mark-cache-no)
1213 (if (memq mark mark-cache-yes)
1215 ;; else, we have to actually check the mark
1216 (if (funcall mark-check
1221 (push mark mark-cache-yes))
1222 (push mark mark-cache-no))))))
1225 (defun spam-register-routine (classification
1227 &optional unregister
1229 (when (and (spam-classification-valid-p classification)
1230 (spam-registration-check-valid-p check))
1231 (let* ((register-function
1232 (spam-registration-function classification check))
1233 (unregister-function
1234 (spam-unregistration-function classification check))
1235 (run-function (if unregister
1238 (log-function (if unregister
1239 'spam-log-undo-registration
1240 'spam-log-processing-to-registry))
1244 ;; make list of articles, using specific-articles if given
1245 (setq articles (or specific-articles
1247 gnus-newsgroup-articles
1250 (gnus-message 5 "%s %d %s articles with classification %s, check %s"
1251 (if unregister "Unregistering" "Registering")
1253 (if specific-articles "specific" "")
1254 (symbol-name classification)
1255 (symbol-name check))
1256 (funcall run-function articles)
1257 ;; now log all the registrations (or undo them, depending on unregister)
1258 (dolist (article articles)
1259 (funcall log-function
1260 (spam-fetch-field-message-id-fast article)
1264 gnus-newsgroup-name))))))
1266 ;;; log a ham- or spam-processor invocation to the registry
1267 (defun spam-log-processing-to-registry (id type classification check group)
1268 (when spam-log-to-registry
1269 (if (and (stringp id)
1271 (spam-process-type-valid-p type)
1272 (spam-classification-valid-p classification)
1273 (spam-registration-check-valid-p check))
1274 (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type)))
1275 (cell (list classification check group)))
1276 (push cell cell-list)
1277 (gnus-registry-store-extra-entry
1282 (gnus-error 5 (format "%s called with bad ID, type, classification, check, or group"
1283 "spam-log-processing-to-registry")))))
1285 ;;; check if a ham- or spam-processor registration has been done
1286 (defun spam-log-registered-p (id type)
1287 (when spam-log-to-registry
1288 (if (and (stringp id)
1289 (spam-process-type-valid-p type))
1290 (cdr-safe (gnus-registry-fetch-extra id type))
1292 (gnus-error 5 (format "%s called with bad ID, type, classification, or check"
1293 "spam-log-registered-p"))
1296 ;;; check what a ham- or spam-processor registration says
1297 ;;; returns nil if conflicting registrations are found
1298 (defun spam-log-registration-type (id type)
1301 (dolist (reg (spam-log-registered-p id type))
1302 (let ((classification (nth 0 reg)))
1303 (when (spam-classification-valid-p classification)
1305 (not (eq classification decision)))
1306 (setq count (+ 1 count)))
1307 (setq decision classification))))
1312 ;;; check if a ham- or spam-processor registration needs to be undone
1313 (defun spam-log-unregistration-needed-p (id type classification check)
1314 (when spam-log-to-registry
1315 (if (and (stringp id)
1316 (spam-process-type-valid-p type)
1317 (spam-classification-valid-p classification)
1318 (spam-registration-check-valid-p check))
1319 (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type)))
1321 (dolist (cell cell-list)
1323 (when (and (eq classification (nth 0 cell))
1324 (eq check (nth 1 cell)))
1328 (gnus-error 5 (format "%s called with bad ID, type, classification, or check"
1329 "spam-log-unregistration-needed-p"))
1333 ;;; undo a ham- or spam-processor registration (the group is not used)
1334 (defun spam-log-undo-registration (id type classification check &optional group)
1335 (when (and spam-log-to-registry
1336 (spam-log-unregistration-needed-p id type classification check))
1337 (if (and (stringp id)
1338 (spam-process-type-valid-p type)
1339 (spam-classification-valid-p classification)
1340 (spam-registration-check-valid-p check))
1341 (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type)))
1342 new-cell-list found)
1343 (dolist (cell cell-list)
1344 (unless (and (eq classification (nth 0 cell))
1345 (eq check (nth 1 cell)))
1346 (push cell new-cell-list)))
1347 (gnus-registry-store-extra-entry
1352 (gnus-error 5 (format "%s called with bad ID, type, check, or group"
1353 "spam-log-undo-registration"))
1356 ;;; set up IMAP widening if it's necessary
1357 (defun spam-setup-widening ()
1358 (dolist (check spam-list-of-statistical-checks)
1359 (when (symbol-value check)
1360 (setq nnimap-split-download-body-default t))))
1365 (defun spam-check-regex-body ()
1366 (let ((spam-regex-headers-ham spam-regex-body-ham)
1367 (spam-regex-headers-spam spam-regex-body-spam))
1368 (spam-check-regex-headers t)))
1373 (defun spam-check-regex-headers (&optional body)
1374 (let ((type (if body "body" "header"))
1375 (spam-split-group (if spam-split-symbolic-return
1379 (dolist (h-regex spam-regex-headers-ham)
1381 (goto-char (point-min))
1382 (when (re-search-forward h-regex nil t)
1383 (message "Ham regex %s search positive." type)
1385 (dolist (s-regex spam-regex-headers-spam)
1387 (goto-char (point-min))
1388 (when (re-search-forward s-regex nil t)
1389 (message "Spam regex %s search positive." type)
1391 (setq ret spam-split-group))))
1397 (defun spam-reverse-ip-string (ip)
1399 (mapconcat 'identity
1400 (nreverse (split-string ip "\\."))
1403 (defun spam-check-blackholes ()
1404 "Check the Received headers for blackholed relays."
1405 (let ((headers (message-fetch-field "received"))
1406 (spam-split-group (if spam-split-symbolic-return
1413 (goto-char (point-min))
1414 (gnus-message 5 "Checking headers for relay addresses")
1415 (while (re-search-forward
1416 "\\([0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+\\)" nil t)
1417 (gnus-message 9 "Blackhole search found host IP %s." (match-string 1))
1418 (push (spam-reverse-ip-string (match-string 1))
1420 (dolist (server spam-blackhole-servers)
1422 (unless (and spam-blackhole-good-server-regex
1423 ;; match the good-server-regex against the reversed (again) IP string
1425 spam-blackhole-good-server-regex
1426 (spam-reverse-ip-string ip)))
1428 (let ((query-string (concat ip "." server)))
1430 (let ((query-result (query-dig query-string)))
1432 (gnus-message 5 "(DIG): positive blackhole check '%s'"
1434 (push (list ip server query-result)
1436 ;; else, if not using dig.el
1437 (when (query-dns query-string)
1438 (gnus-message 5 "positive blackhole check")
1439 (push (list ip server (query-dns query-string 'TXT))
1450 (defun spam-check-hashcash ()
1451 "Check the headers for hashcash payments."
1452 (mail-check-payment))) ;mail-check-payment returns a boolean
1455 (defalias 'mail-check-payment 'ignore)
1456 (defalias 'spam-check-hashcash 'ignore))))
1460 ;;; original idea for spam-check-BBDB from Alexander Kotelnikov
1461 ;;; <sacha@giotto.sj.ru>
1463 ;; all this is done inside a condition-case to trap errors
1470 ;; when the BBDB changes, we want to clear out our cache
1471 (defun spam-clear-cache-BBDB (&rest immaterial)
1472 (spam-clear-cache 'spam-use-BBDB))
1474 (add-hook 'bbdb-change-hook 'spam-clear-cache-BBDB)
1476 (defun spam-enter-ham-BBDB (addresses &optional remove)
1477 "Enter an address into the BBDB; implies ham (non-spam) sender"
1478 (dolist (from addresses)
1479 (when (stringp from)
1480 (let* ((parsed-address (gnus-extract-address-components from))
1481 (name (or (nth 0 parsed-address) "Ham Sender"))
1482 (remove-function (if remove
1483 'bbdb-delete-record-internal
1485 (net-address (nth 1 parsed-address))
1486 (record (and net-address
1487 (bbdb-search-simple nil net-address))))
1489 (gnus-message 5 "%s address %s %s BBDB"
1490 (if remove "Deleting" "Adding")
1492 (if remove "from" "to"))
1494 (funcall remove-function record)
1495 (bbdb-create-internal name nil net-address nil nil
1496 "ham sender added by spam.el")))))))
1498 (defun spam-BBDB-register-routine (articles &optional unregister)
1500 (dolist (article articles)
1501 (when (stringp (spam-fetch-field-from-fast article))
1502 (push (spam-fetch-field-from-fast article) addresses)))
1503 ;; now do the register/unregister action
1504 (spam-enter-ham-BBDB addresses unregister)))
1506 (defun spam-BBDB-unregister-routine (articles)
1507 (spam-BBDB-register-routine articles t))
1509 (defun spam-check-BBDB ()
1510 "Mail from people in the BBDB is classified as ham or non-spam"
1511 (let ((who (message-fetch-field "from"))
1512 (spam-split-group (if spam-split-symbolic-return
1515 bbdb-cache bbdb-hashtable)
1516 (when spam-cache-lookups
1517 (setq bbdb-cache (gethash 'spam-use-BBDB spam-caches))
1520 ;; this is the expanded (bbdb-hashtable) macro
1521 ;; without the debugging support
1522 (with-current-buffer (bbdb-buffer)
1524 (save-window-excursion
1525 (bbdb-records nil t)
1527 (puthash 'spam-use-BBDB bbdb-cache spam-caches)))
1529 (setq who (nth 1 (gnus-extract-address-components who)))
1531 (if spam-cache-lookups
1533 (intern-soft who bbdb-cache))
1534 (bbdb-search-simple nil who))
1536 (if spam-use-BBDB-exclusive
1541 (defalias 'bbdb-search-simple 'ignore)
1542 (defalias 'bbdb-records 'ignore)
1543 (defalias 'bbdb-buffer 'ignore)
1544 (defalias 'spam-check-BBDB 'ignore)
1545 (defalias 'spam-BBDB-register-routine 'ignore)
1546 (defalias 'spam-enter-ham-BBDB 'ignore)
1547 (defalias 'bbdb-create-internal 'ignore)
1548 (defalias 'bbdb-delete-record-internal 'ignore)
1549 (defalias 'bbdb-records 'ignore))))
1554 ;;; check the ifile backend; return nil if the mail was NOT classified
1557 (defun spam-get-ifile-database-parameter ()
1558 "Get the command-line parameter for ifile's database from
1559 spam-ifile-database-path."
1560 (if spam-ifile-database-path
1561 (format "--db-file=%s" spam-ifile-database-path)
1564 (defun spam-check-ifile ()
1565 "Check the ifile backend for the classification of this message."
1566 (let ((article-buffer-name (buffer-name))
1567 (spam-split-group (if spam-split-symbolic-return
1572 (let ((temp-buffer-name (buffer-name))
1573 (db-param (spam-get-ifile-database-parameter)))
1575 (set-buffer article-buffer-name)
1576 (apply 'call-process-region
1577 (point-min) (point-max) spam-ifile-path
1578 nil temp-buffer-name nil "-c"
1579 (if db-param `(,db-param "-q") `("-q"))))
1580 ;; check the return now (we're back in the temp buffer)
1581 (goto-char (point-min))
1583 (setq category (buffer-substring (point) (point-at-eol))))
1584 (when (not (zerop (length category))) ; we need a category here
1585 (if spam-ifile-all-categories
1586 (setq return category)
1587 ;; else, if spam-ifile-all-categories is not set...
1588 (when (string-equal spam-ifile-spam-category category)
1589 (setq return spam-split-group)))))) ; note return is nil otherwise
1592 (defun spam-ifile-register-with-ifile (articles category &optional unregister)
1593 "Register an article, given as a string, with a category.
1594 Uses `gnus-newsgroup-name' if category is nil (for ham registration)."
1595 (let ((category (or category gnus-newsgroup-name))
1596 (add-or-delete-option (if unregister "-d" "-i"))
1597 (db (spam-get-ifile-database-parameter))
1600 (dolist (article articles)
1601 (let ((article-string (spam-get-article-as-string article)))
1602 (when (stringp article-string)
1603 (insert article-string))))
1604 (apply 'call-process-region
1605 (point-min) (point-max) spam-ifile-path
1607 add-or-delete-option category
1608 (if db `(,db "-h") `("-h"))))))
1610 (defun spam-ifile-register-spam-routine (articles &optional unregister)
1611 (spam-ifile-register-with-ifile articles spam-ifile-spam-category unregister))
1613 (defun spam-ifile-unregister-spam-routine (articles)
1614 (spam-ifile-register-spam-routine articles t))
1616 (defun spam-ifile-register-ham-routine (articles &optional unregister)
1617 (spam-ifile-register-with-ifile articles spam-ifile-ham-category unregister))
1619 (defun spam-ifile-unregister-ham-routine (articles)
1620 (spam-ifile-register-ham-routine articles t))
1627 (let ((spam-stat-install-hooks nil))
1628 (require 'spam-stat))
1630 (defun spam-check-stat ()
1631 "Check the spam-stat backend for the classification of this message"
1632 (let ((spam-split-group (if spam-split-symbolic-return
1635 (spam-stat-split-fancy-spam-group spam-split-group) ; override
1636 (spam-stat-buffer (buffer-name)) ; stat the current buffer
1638 (spam-stat-split-fancy)))
1640 (defun spam-stat-register-spam-routine (articles &optional unregister)
1641 (dolist (article articles)
1642 (let ((article-string (spam-get-article-as-string article)))
1644 (insert article-string)
1646 (spam-stat-buffer-change-to-non-spam)
1647 (spam-stat-buffer-is-spam))))))
1649 (defun spam-stat-unregister-spam-routine (articles)
1650 (spam-stat-register-spam-routine articles t))
1652 (defun spam-stat-register-ham-routine (articles &optional unregister)
1653 (dolist (article articles)
1654 (let ((article-string (spam-get-article-as-string article)))
1656 (insert article-string)
1658 (spam-stat-buffer-change-to-spam)
1659 (spam-stat-buffer-is-non-spam))))))
1661 (defun spam-stat-unregister-ham-routine (articles)
1662 (spam-stat-register-ham-routine articles t))
1664 (defun spam-maybe-spam-stat-load ()
1665 (when spam-use-stat (spam-stat-load)))
1667 (defun spam-maybe-spam-stat-save ()
1668 (when spam-use-stat (spam-stat-save))))
1671 (defalias 'spam-stat-load 'ignore)
1672 (defalias 'spam-stat-save 'ignore)
1673 (defalias 'spam-maybe-spam-stat-load 'ignore)
1674 (defalias 'spam-maybe-spam-stat-save 'ignore)
1675 (defalias 'spam-stat-register-ham-routine 'ignore)
1676 (defalias 'spam-stat-unregister-ham-routine 'ignore)
1677 (defalias 'spam-stat-register-spam-routine 'ignore)
1678 (defalias 'spam-stat-unregister-spam-routine 'ignore)
1679 (defalias 'spam-stat-buffer-is-spam 'ignore)
1680 (defalias 'spam-stat-buffer-change-to-spam 'ignore)
1681 (defalias 'spam-stat-buffer-is-non-spam 'ignore)
1682 (defalias 'spam-stat-buffer-change-to-non-spam 'ignore)
1683 (defalias 'spam-stat-split-fancy 'ignore)
1684 (defalias 'spam-check-stat 'ignore))))
1688 ;;;; Blacklists and whitelists.
1690 (defvar spam-whitelist-cache nil)
1691 (defvar spam-blacklist-cache nil)
1693 (defun spam-kill-whole-line ()
1695 (let ((kill-whole-line t))
1698 ;;; address can be a list, too
1699 (defun spam-enter-whitelist (address &optional remove)
1700 "Enter ADDRESS (list or single) into the whitelist.
1701 With a non-nil REMOVE, remove them."
1702 (interactive "sAddress: ")
1703 (spam-enter-list address spam-whitelist remove)
1704 (setq spam-whitelist-cache nil)
1705 (spam-clear-cache 'spam-use-whitelist))
1707 ;;; address can be a list, too
1708 (defun spam-enter-blacklist (address &optional remove)
1709 "Enter ADDRESS (list or single) into the blacklist.
1710 With a non-nil REMOVE, remove them."
1711 (interactive "sAddress: ")
1712 (spam-enter-list address spam-blacklist remove)
1713 (setq spam-blacklist-cache nil)
1714 (spam-clear-cache 'spam-use-whitelist))
1716 (defun spam-enter-list (addresses file &optional remove)
1717 "Enter ADDRESSES into the given FILE.
1718 Either the whitelist or the blacklist files can be used. With
1719 REMOVE not nil, remove the ADDRESSES."
1720 (if (stringp addresses)
1721 (spam-enter-list (list addresses) file remove)
1722 ;; else, we have a list of addresses here
1723 (unless (file-exists-p (file-name-directory file))
1724 (make-directory (file-name-directory file) t))
1727 (find-file-noselect file))
1728 (dolist (a addresses)
1730 (goto-char (point-min))
1731 (if (re-search-forward (regexp-quote a) nil t)
1732 ;; found the address
1734 (spam-kill-whole-line))
1735 ;; else, the address was not found
1737 (goto-char (point-max))
1743 (defun spam-filelist-build-cache (type)
1744 (let ((cache (if (eq type 'spam-use-blacklist)
1745 spam-blacklist-cache
1746 spam-whitelist-cache))
1748 (unless (gethash type spam-caches)
1750 (let ((address (pop cache)))
1751 (unless (zerop (length address)) ; 0 for a nil address too
1752 (setq address (regexp-quote address))
1753 ;; fix regexp-quote's treatment of user-intended regexes
1754 (while (string-match "\\\\\\*" address)
1755 (setq address (replace-match ".*" t t address))))
1756 (push address parsed-cache)))
1757 (puthash type parsed-cache spam-caches))))
1759 (defun spam-filelist-check-cache (type from)
1760 (when (stringp from)
1761 (spam-filelist-build-cache type)
1763 (dolist (address (gethash type spam-caches))
1764 (when (and address (string-match address from))
1769 ;;; returns t if the sender is in the whitelist, nil or
1770 ;;; spam-split-group otherwise
1771 (defun spam-check-whitelist ()
1772 ;; FIXME! Should it detect when file timestamps change?
1773 (let ((spam-split-group (if spam-split-symbolic-return
1776 (unless spam-whitelist-cache
1777 (setq spam-whitelist-cache (spam-parse-list spam-whitelist)))
1778 (if (spam-from-listed-p 'spam-use-whitelist)
1780 (if spam-use-whitelist-exclusive
1784 (defun spam-check-blacklist ()
1785 ;; FIXME! Should it detect when file timestamps change?
1786 (let ((spam-split-group (if spam-split-symbolic-return
1789 (unless spam-blacklist-cache
1790 (setq spam-blacklist-cache (spam-parse-list spam-blacklist)))
1791 (and (spam-from-listed-p 'spam-use-blacklist) spam-split-group)))
1793 (defun spam-parse-list (file)
1794 (when (file-readable-p file)
1795 (let (contents address)
1797 (insert-file-contents file)
1799 (setq address (buffer-substring (point) (point-at-eol)))
1801 ;; insert the e-mail address if detected, otherwise the raw data
1802 (unless (zerop (length address))
1803 (let ((pure-address (nth 1 (gnus-extract-address-components address))))
1804 (push (or pure-address address) contents)))))
1805 (nreverse contents))))
1807 (defun spam-from-listed-p (type)
1808 (let ((from (message-fetch-field "from"))
1810 (spam-filelist-check-cache type from)))
1812 (defun spam-filelist-register-routine (articles blacklist &optional unregister)
1813 (let ((de-symbol (if blacklist 'spam-use-whitelist 'spam-use-blacklist))
1814 (declassification (if blacklist 'ham 'spam))
1816 (if blacklist 'spam-enter-blacklist 'spam-enter-whitelist))
1818 (if blacklist 'spam-enter-whitelist 'spam-enter-blacklist))
1819 from addresses unregister-list)
1820 (dolist (article articles)
1821 (let ((from (spam-fetch-field-from-fast article))
1822 (id (spam-fetch-field-message-id-fast article))
1824 (when (stringp from)
1825 (dolist (ignore-regex spam-blacklist-ignored-regexes)
1826 (when (and (not sender-ignored)
1827 (stringp ignore-regex)
1828 (string-match ignore-regex from))
1829 (setq sender-ignored t)))
1830 ;; remember the messages we need to unregister, unless remove is set
1833 (spam-log-unregistration-needed-p
1834 id 'process declassification de-symbol))
1835 (push from unregister-list))
1836 (unless sender-ignored
1837 (push from addresses)))))
1840 (funcall enter-function addresses t) ; unregister all these addresses
1841 ;; else, register normally and unregister what we need to
1842 (funcall remove-function unregister-list t)
1843 (dolist (article unregister-list)
1844 (spam-log-undo-registration
1845 (spam-fetch-field-message-id-fast article)
1849 (funcall enter-function addresses nil))))
1851 (defun spam-blacklist-unregister-routine (articles)
1852 (spam-blacklist-register-routine articles t))
1854 (defun spam-blacklist-register-routine (articles &optional unregister)
1855 (spam-filelist-register-routine articles t unregister))
1857 (defun spam-whitelist-unregister-routine (articles)
1858 (spam-whitelist-register-routine articles t))
1860 (defun spam-whitelist-register-routine (articles &optional unregister)
1861 (spam-filelist-register-routine articles nil unregister))
1864 ;;;; Spam-report glue
1865 (defun spam-report-gmane-register-routine (articles)
1867 (apply 'spam-report-gmane articles)))
1871 (defun spam-check-bogofilter-headers (&optional score)
1872 (let ((header (message-fetch-field spam-bogofilter-header))
1873 (spam-split-group (if spam-split-symbolic-return
1876 (when header ; return nil when no header
1877 (if score ; scoring mode
1878 (if (string-match "spamicity=\\([0-9.]+\\)" header)
1879 (match-string 1 header)
1881 ;; spam detection mode
1882 (when (string-match spam-bogofilter-bogosity-positive-spam-header
1884 spam-split-group)))))
1886 ;; return something sensible if the score can't be determined
1887 (defun spam-bogofilter-score ()
1888 "Get the Bogofilter spamicity score"
1890 (save-window-excursion
1891 (gnus-summary-show-article t)
1892 (set-buffer gnus-article-buffer)
1893 (let ((score (or (spam-check-bogofilter-headers t)
1894 (spam-check-bogofilter t))))
1895 (gnus-summary-show-article)
1896 (message "Spamicity score %s" score)
1899 (defun spam-check-bogofilter (&optional score)
1900 "Check the Bogofilter backend for the classification of this message"
1901 (let ((article-buffer-name (buffer-name))
1902 (db spam-bogofilter-database-directory)
1905 (let ((temp-buffer-name (buffer-name)))
1907 (set-buffer article-buffer-name)
1908 (apply 'call-process-region
1909 (point-min) (point-max)
1910 spam-bogofilter-path
1911 nil temp-buffer-name nil
1912 (if db `("-d" ,db "-v") `("-v"))))
1913 (setq return (spam-check-bogofilter-headers score))))
1916 (defun spam-bogofilter-register-with-bogofilter (articles
1918 &optional unregister)
1919 "Register an article, given as a string, as spam or non-spam."
1920 (dolist (article articles)
1921 (let ((article-string (spam-get-article-as-string article))
1922 (db spam-bogofilter-database-directory)
1923 (switch (if unregister
1925 spam-bogofilter-spam-strong-switch
1926 spam-bogofilter-ham-strong-switch)
1928 spam-bogofilter-spam-switch
1929 spam-bogofilter-ham-switch))))
1930 (when (stringp article-string)
1932 (insert article-string)
1934 (apply 'call-process-region
1935 (point-min) (point-max)
1936 spam-bogofilter-path
1938 (if db `("-d" ,db "-v") `("-v"))))))))
1940 (defun spam-bogofilter-register-spam-routine (articles &optional unregister)
1941 (spam-bogofilter-register-with-bogofilter articles t unregister))
1943 (defun spam-bogofilter-unregister-spam-routine (articles)
1944 (spam-bogofilter-register-spam-routine articles t))
1946 (defun spam-bogofilter-register-ham-routine (articles &optional unregister)
1947 (spam-bogofilter-register-with-bogofilter articles nil unregister))
1949 (defun spam-bogofilter-unregister-ham-routine (articles)
1950 (spam-bogofilter-register-ham-routine articles t))
1955 (defun spam-check-spamoracle ()
1956 "Run spamoracle on an article to determine whether it's spam."
1957 (let ((article-buffer-name (buffer-name))
1958 (spam-split-group (if spam-split-symbolic-return
1962 (let ((temp-buffer-name (buffer-name)))
1964 (set-buffer article-buffer-name)
1966 (apply 'call-process-region
1967 (point-min) (point-max)
1968 spam-spamoracle-binary
1969 nil temp-buffer-name nil
1970 (if spam-spamoracle-database
1971 `("-f" ,spam-spamoracle-database "mark")
1975 (set-buffer temp-buffer-name)
1976 (goto-char (point-min))
1977 (when (re-search-forward "^X-Spam: yes;" nil t)
1979 (error "Error running spamoracle" status))))))))
1981 (defun spam-spamoracle-learn (articles article-is-spam-p &optional unregister)
1982 "Run spamoracle in training mode."
1984 (let ((temp-buffer-name (buffer-name)))
1986 (goto-char (point-min))
1987 (dolist (article articles)
1988 (insert (spam-get-article-as-string article)))
1989 (let* ((arg (if (spam-xor unregister article-is-spam-p)
1993 (apply 'call-process-region
1994 (point-min) (point-max)
1995 spam-spamoracle-binary
1996 nil temp-buffer-name nil
1997 (if spam-spamoracle-database
1998 `("-f" ,spam-spamoracle-database
2001 (when (not (eq 0 status))
2002 (error "Error running spamoracle" status)))))))
2004 (defun spam-spamoracle-learn-ham (articles &optional unregister)
2005 (spam-spamoracle-learn articles nil unregister))
2007 (defun spam-spamoracle-unlearn-ham (articles &optional unregister)
2008 (spam-spamoracle-learn-ham articles t))
2010 (defun spam-spamoracle-learn-spam (articles &optional unregister)
2011 (spam-spamoracle-learn articles t unregister))
2013 (defun spam-spamoracle-unlearn-spam (articles &optional unregister)
2014 (spam-spamoracle-learn-spam articles t))
2018 ;;; based mostly on the bogofilter code
2019 (defun spam-check-spamassassin-headers (&optional score)
2020 "Check the SpamAssassin headers for the classification of this message."
2021 (if score ; scoring mode
2022 (let ((header (message-fetch-field spam-spamassassin-spam-status-header)))
2024 (if (string-match "hits=\\(-?[0-9.]+\\)" header)
2025 (match-string 1 header)
2027 ;; spam detection mode
2028 (let ((header (message-fetch-field spam-spamassassin-spam-flag-header))
2029 (spam-split-group (if spam-split-symbolic-return
2032 (when header ; return nil when no header
2033 (when (string-match spam-spamassassin-positive-spam-flag-header
2035 spam-split-group)))))
2037 (defun spam-check-spamassassin (&optional score)
2038 "Check the SpamAssassin backend for the classification of this message."
2039 (let ((article-buffer-name (buffer-name)))
2041 (let ((temp-buffer-name (buffer-name)))
2043 (set-buffer article-buffer-name)
2044 (apply 'call-process-region
2045 (point-min) (point-max) spam-spamassassin-path
2046 nil temp-buffer-name nil spam-spamassassin-arguments))
2047 ;; check the return now (we're back in the temp buffer)
2048 (goto-char (point-min))
2049 (spam-check-spamassassin-headers score)))))
2051 ;; return something sensible if the score can't be determined
2052 (defun spam-spamassassin-score ()
2053 "Get the SpamAssassin score"
2055 (save-window-excursion
2056 (gnus-summary-show-article t)
2057 (set-buffer gnus-article-buffer)
2058 (let ((score (or (spam-check-spamassassin-headers t)
2059 (spam-check-spamassassin t))))
2060 (gnus-summary-show-article)
2061 (message "SpamAssassin score %s" score)
2064 (defun spam-spamassassin-register-with-sa-learn (articles spam
2065 &optional unregister)
2066 "Register articles with spamassassin's sa-learn as spam or non-spam."
2068 (let ((action (if unregister spam-sa-learn-unregister-switch
2069 (if spam spam-sa-learn-spam-switch
2070 spam-sa-learn-ham-switch)))
2071 (summary-buffer-name (buffer-name)))
2073 ;; group the articles into mbox format
2074 (dolist (article articles)
2075 (let (article-string)
2077 (set-buffer summary-buffer-name)
2078 (setq article-string (spam-get-article-as-string article)))
2079 (when (stringp article-string)
2080 (insert "From \n") ; mbox separator (sa-learn only checks the
2081 ; first five chars, so we can get away with
2083 (insert article-string)
2085 ;; call sa-learn on all messages at the same time
2086 (apply 'call-process-region
2087 (point-min) (point-max)
2089 nil nil nil "--mbox"
2090 (if spam-sa-learn-rebuild
2092 `("--no-rebuild" ,action)))))))
2094 (defun spam-spamassassin-register-spam-routine (articles &optional unregister)
2095 (spam-spamassassin-register-with-sa-learn articles t unregister))
2097 (defun spam-spamassassin-register-ham-routine (articles &optional unregister)
2098 (spam-spamassassin-register-with-sa-learn articles nil unregister))
2100 (defun spam-spamassassin-unregister-spam-routine (articles)
2101 (spam-spamassassin-register-with-sa-learn articles t t))
2103 (defun spam-spamassassin-unregister-ham-routine (articles)
2104 (spam-spamassassin-register-with-sa-learn articles nil t))
2109 (defun spam-initialize ()
2110 "Install the spam.el hooks and do other initialization"
2112 (setq spam-install-hooks t)
2113 ;; TODO: How do we redo this every time spam-face is customized?
2114 (push '((eq mark gnus-spam-mark) . spam-face)
2115 gnus-summary-highlight)
2116 ;; Add hooks for loading and saving the spam stats
2117 (add-hook 'gnus-save-newsrc-hook 'spam-maybe-spam-stat-save)
2118 (add-hook 'gnus-get-top-new-news-hook 'spam-maybe-spam-stat-load)
2119 (add-hook 'gnus-startup-hook 'spam-maybe-spam-stat-load)
2120 (add-hook 'gnus-summary-prepare-exit-hook 'spam-summary-prepare-exit)
2121 (add-hook 'gnus-summary-prepare-hook 'spam-summary-prepare)
2122 (add-hook 'gnus-get-new-news-hook 'spam-setup-widening)
2123 (add-hook 'gnus-summary-prepared-hook 'spam-find-spam))
2125 (defun spam-unload-hook ()
2126 "Uninstall the spam.el hooks"
2128 (remove-hook 'gnus-save-newsrc-hook 'spam-maybe-spam-stat-save)
2129 (remove-hook 'gnus-get-top-new-news-hook 'spam-maybe-spam-stat-load)
2130 (remove-hook 'gnus-startup-hook 'spam-maybe-spam-stat-load)
2131 (remove-hook 'gnus-summary-prepare-exit-hook 'spam-summary-prepare-exit)
2132 (remove-hook 'gnus-summary-prepare-hook 'spam-summary-prepare)
2133 (remove-hook 'gnus-get-new-news-hook 'spam-setup-widening)
2134 (remove-hook 'gnus-summary-prepare-hook 'spam-find-spam))
2136 (when spam-install-hooks
2141 ;;; spam.el ends here