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
42 (eval-when-compile (require 'cl))
46 (require 'gnus-uu) ; because of key prefix issues
47 ;;; for the definitions of group content classification and spam processors
49 (require 'message) ;for the message-fetch-field functions
51 ;; for nnimap-split-download-body-default
52 (eval-when-compile (require 'nnimap))
56 (autoload 'query-dig "dig"))
58 ;; autoload spam-report
60 (autoload 'spam-report-gmane "spam-report"))
62 ;; autoload gnus-registry
64 (autoload 'gnus-registry-group-count "gnus-registry")
65 (autoload 'gnus-registry-add-group "gnus-registry")
66 (autoload 'gnus-registry-store-extra-entry "gnus-registry")
67 (autoload 'gnus-registry-fetch-extra "gnus-registry"))
71 (autoload 'query-dns "dns"))
76 "Spam configuration.")
78 (defcustom spam-directory "~/News/spam/"
79 "Directory for spam whitelists and blacklists."
83 (defcustom spam-move-spam-nonspam-groups-only t
84 "Whether spam should be moved in non-spam groups only.
85 When t, only ham and unclassified groups will have their spam moved
86 to the spam-process-destination. When nil, spam will also be moved from
91 (defcustom spam-process-ham-in-nonham-groups nil
92 "Whether ham should be processed in non-ham groups."
96 (defcustom spam-log-to-registry nil
97 "Whether spam/ham processing should be logged in the registry."
101 (defcustom spam-split-symbolic-return nil
102 "Whether `spam-split' should work with symbols or group names."
106 (defcustom spam-split-symbolic-return-positive nil
107 "Whether `spam-split' should ALWAYS work with symbols or group names.
108 Do not set this if you use `spam-split' in a fancy split
113 (defcustom spam-process-ham-in-spam-groups nil
114 "Whether ham should be processed in spam groups."
118 (defcustom spam-mark-only-unseen-as-spam t
119 "Whether only unseen articles should be marked as spam in spam groups.
120 When nil, all unread articles in a spam group are marked as
121 spam. Set this if you want to leave an article unread in a spam group
122 without losing it to the automatic spam-marking process."
126 (defcustom spam-mark-ham-unread-before-move-from-spam-group nil
127 "Whether ham should be marked unread before it's moved.
128 The article is moved out of a spam group according to ham-process-destination.
129 This variable is an official entry in the international Longest Variable Name
134 (defcustom spam-disable-spam-split-during-ham-respool nil
135 "Whether `spam-split' should be ignored while resplitting ham.
136 This is useful to prevent ham from ending up in the same spam
137 group after the resplit. Don't set this to t if you have `spam-split' as the
138 last rule in your split configuration."
142 (defcustom spam-autodetect-recheck-messages nil
143 "Should spam.el recheck all meessages when autodetecting?
144 Normally this is nil, so only unseen messages will be checked."
148 (defcustom spam-whitelist (expand-file-name "whitelist" spam-directory)
149 "The location of the whitelist.
150 The file format is one regular expression per line.
151 The regular expression is matched against the address."
155 (defcustom spam-blacklist (expand-file-name "blacklist" spam-directory)
156 "The location of the blacklist.
157 The file format is one regular expression per line.
158 The regular expression is matched against the address."
162 (defcustom spam-use-dig t
163 "Whether `query-dig' should be used instead of `query-dns'."
167 (defcustom spam-use-blacklist nil
168 "Whether the blacklist should be used by `spam-split'."
172 (defcustom spam-blacklist-ignored-regexes nil
173 "Regular expressions that the blacklist should ignore."
174 :type '(repeat (regexp :tag "Regular expression to ignore when blacklisting"))
177 (defcustom spam-use-whitelist nil
178 "Whether the whitelist should be used by `spam-split'."
182 (defcustom spam-use-whitelist-exclusive nil
183 "Whether whitelist-exclusive should be used by `spam-split'.
184 Exclusive whitelisting means that all messages from senders not in the whitelist
185 are considered spam."
189 (defcustom spam-use-blackholes nil
190 "Whether blackholes should be used by `spam-split'."
194 (defcustom spam-use-hashcash nil
195 "Whether hashcash payments should be detected by `spam-split'."
199 (defcustom spam-use-regex-headers nil
200 "Whether a header regular expression match should be used by `spam-split'.
201 Also see the variables `spam-regex-headers-spam' and `spam-regex-headers-ham'."
205 (defcustom spam-use-regex-body nil
206 "Whether a body regular expression match should be used by `spam-split'.
207 Also see the variables `spam-regex-body-spam' and `spam-regex-body-ham'."
211 (defcustom spam-use-bogofilter-headers nil
212 "Whether bogofilter headers should be used by `spam-split'.
213 Enable this if you pre-process messages with Bogofilter BEFORE Gnus sees them."
217 (defcustom spam-use-bogofilter nil
218 "Whether bogofilter should be invoked by `spam-split'.
219 Enable this if you want Gnus to invoke Bogofilter on new messages."
223 (defcustom spam-use-BBDB nil
224 "Whether BBDB should be used by `spam-split'."
228 (defcustom spam-use-BBDB-exclusive nil
229 "Whether BBDB-exclusive should be used by `spam-split'.
230 Exclusive BBDB means that all messages from senders not in the BBDB are
235 (defcustom spam-use-ifile nil
236 "Whether ifile should be used by `spam-split'."
240 (defcustom spam-use-stat nil
241 "Whether `spam-stat' should be used by `spam-split'."
245 (defcustom spam-use-spamoracle nil
246 "Whether spamoracle should be used by `spam-split'."
250 (defcustom spam-install-hooks (or
254 spam-use-whitelist-exclusive
257 spam-use-regex-headers
259 spam-use-bogofilter-headers
262 spam-use-BBDB-exclusive
266 "Whether the spam hooks should be installed.
267 Default to t if one of the spam-use-* variables is set."
271 (defcustom spam-split-group "spam"
272 "Group name where incoming spam should be put by `spam-split'."
276 ;;; TODO: deprecate this variable, it's confusing since it's a list of strings,
277 ;;; not regular expressions
278 (defcustom spam-junk-mailgroups (cons
280 '("mail.junk" "poste.pourriel"))
281 "Mailgroups with spam contents.
282 All unmarked article in such group receive the spam mark on group entry."
283 :type '(repeat (string :tag "Group"))
286 (defcustom spam-blackhole-servers '("bl.spamcop.net" "relays.ordb.org"
287 "dev.null.dk" "relays.visi.com")
288 "List of blackhole servers."
289 :type '(repeat (string :tag "Server"))
292 (defcustom spam-blackhole-good-server-regex nil
293 "String matching IP addresses that should not be checked in the blackholes."
294 :type '(radio (const nil)
295 (regexp :format "%t: %v\n" :size 0))
298 (defcustom spam-face 'gnus-splash-face
299 "Face for spam-marked articles."
303 (defcustom spam-regex-headers-spam '("^X-Spam-Flag: YES")
304 "Regular expression for positive header spam matches."
305 :type '(repeat (regexp :tag "Regular expression to match spam header"))
308 (defcustom spam-regex-headers-ham '("^X-Spam-Flag: NO")
309 "Regular expression for positive header ham matches."
310 :type '(repeat (regexp :tag "Regular expression to match ham header"))
313 (defcustom spam-regex-body-spam '()
314 "Regular expression for positive body spam matches."
315 :type '(repeat (regexp :tag "Regular expression to match spam body"))
318 (defcustom spam-regex-body-ham '()
319 "Regular expression for positive body ham matches."
320 :type '(repeat (regexp :tag "Regular expression to match ham body"))
323 (defgroup spam-ifile nil
324 "Spam ifile configuration."
327 (defcustom spam-ifile-path (exec-installed-p "ifile")
328 "File path of the ifile executable program."
329 :type '(choice (file :tag "Location of ifile")
330 (const :tag "ifile is not installed"))
333 (defcustom spam-ifile-database-path nil
334 "File path of the ifile database."
335 :type '(choice (file :tag "Location of the ifile database")
336 (const :tag "Use the default"))
339 (defcustom spam-ifile-spam-category "spam"
340 "Name of the spam ifile category."
344 (defcustom spam-ifile-ham-category nil
345 "Name of the ham ifile category.
346 If nil, the current group name will be used."
347 :type '(choice (string :tag "Use a fixed category")
348 (const :tag "Use the current group name"))
351 (defcustom spam-ifile-all-categories nil
352 "Whether the ifile check will return all categories, or just spam.
353 Set this to t if you want to use the `spam-split' invocation of ifile as
354 your main source of newsgroup names."
358 (defgroup spam-bogofilter nil
359 "Spam bogofilter configuration."
362 (defcustom spam-bogofilter-path (exec-installed-p "bogofilter")
363 "File path of the Bogofilter executable program."
364 :type '(choice (file :tag "Location of bogofilter")
365 (const :tag "Bogofilter is not installed"))
366 :group 'spam-bogofilter)
368 (defcustom spam-bogofilter-header "X-Bogosity"
369 "The header that Bogofilter inserts in messages."
371 :group 'spam-bogofilter)
373 (defcustom spam-bogofilter-spam-switch "-s"
374 "The switch that Bogofilter uses to register spam messages."
376 :group 'spam-bogofilter)
378 (defcustom spam-bogofilter-ham-switch "-n"
379 "The switch that Bogofilter uses to register ham messages."
381 :group 'spam-bogofilter)
383 (defcustom spam-bogofilter-spam-strong-switch "-S"
384 "The switch that Bogofilter uses to unregister ham messages."
386 :group 'spam-bogofilter)
388 (defcustom spam-bogofilter-ham-strong-switch "-N"
389 "The switch that Bogofilter uses to unregister spam messages."
391 :group 'spam-bogofilter)
393 (defcustom spam-bogofilter-bogosity-positive-spam-header "^\\(Yes\\|Spam\\)"
394 "The regex on `spam-bogofilter-header' for positive spam identification."
396 :group 'spam-bogofilter)
398 (defcustom spam-bogofilter-database-directory nil
399 "Directory path of the Bogofilter databases."
400 :type '(choice (directory
401 :tag "Location of the Bogofilter database directory")
402 (const :tag "Use the default"))
403 :group 'spam-bogofilter)
405 (defgroup spam-spamoracle nil
406 "Spam spamoracle configuration."
409 (defcustom spam-spamoracle-database nil
410 "Location of spamoracle database file.
411 When nil, use the default spamoracle database."
412 :type '(choice (directory :tag "Location of spamoracle database file.")
413 (const :tag "Use the default"))
414 :group 'spam-spamoracle)
416 (defcustom spam-spamoracle-binary (executable-find "spamoracle")
417 "Location of the spamoracle binary."
418 :type '(choice (directory :tag "Location of the spamoracle binary")
419 (const :tag "Use the default"))
420 :group 'spam-spamoracle)
422 ;;; Key bindings for spam control.
424 (gnus-define-keys gnus-summary-mode-map
425 "St" spam-bogofilter-score
426 "Sx" gnus-summary-mark-as-spam
427 "Mst" spam-bogofilter-score
428 "Msx" gnus-summary-mark-as-spam
429 "\M-d" gnus-summary-mark-as-spam)
431 (defvar spam-cache-lookups t
432 "Whether spam.el will try to cache lookups using `spam-caches'.")
434 (defvar spam-caches (make-hash-table
437 "Cache of spam detection entries.")
439 (defvar spam-old-ham-articles nil
440 "List of old ham articles, generated when a group is entered.")
442 (defvar spam-old-spam-articles nil
443 "List of old spam articles, generated when a group is entered.")
445 (defvar spam-split-disabled nil
446 "If non-nil, `spam-split' is disabled, and always returns nil.")
448 (defvar spam-split-last-successful-check nil
450 `spam-split' will set this to nil or a spam-use-XYZ check if it
453 ;; convenience functions
454 (defun spam-clear-cache (symbol)
455 "Clear the spam-caches entry for a check."
456 (remhash symbol spam-caches))
458 (defun spam-xor (a b)
460 (and (or a b) (not (and a b))))
462 (defun spam-group-ham-mark-p (group mark &optional spam)
463 "Checks if MARK is considered a ham mark in GROUP."
464 (when (stringp group)
465 (let* ((marks (spam-group-ham-marks group spam))
466 (marks (if (symbolp mark)
468 (mapcar 'symbol-value marks))))
471 (defun spam-group-spam-mark-p (group mark)
472 "Checks if MARK is considered a spam mark in GROUP."
473 (spam-group-ham-mark-p group mark t))
475 (defun spam-group-ham-marks (group &optional spam)
476 "In GROUP, get all the ham marks."
477 (when (stringp group)
478 (let* ((marks (if spam
479 (gnus-parameter-spam-marks group)
480 (gnus-parameter-ham-marks group)))
482 (marks (if (listp (car marks)) (car marks) marks)))
485 (defun spam-group-spam-marks (group)
486 "In GROUP, get all the spam marks."
487 (spam-group-ham-marks group t))
489 (defun spam-group-spam-contents-p (group)
490 "Is GROUP a spam group?"
492 (or (member group spam-junk-mailgroups)
493 (memq 'gnus-group-spam-classification-spam
494 (gnus-parameter-spam-contents group)))
497 (defun spam-group-ham-contents-p (group)
498 "Is GROUP a ham group?"
500 (memq 'gnus-group-spam-classification-ham
501 (gnus-parameter-spam-contents group))
504 (defvar spam-list-of-processors
505 '((gnus-group-spam-exit-processor-report-gmane spam spam-use-gmane)
506 (gnus-group-spam-exit-processor-bogofilter spam spam-use-bogofilter)
507 (gnus-group-spam-exit-processor-blacklist spam spam-use-blacklist)
508 (gnus-group-spam-exit-processor-ifile spam spam-use-ifile)
509 (gnus-group-spam-exit-processor-stat spam spam-use-stat)
510 (gnus-group-spam-exit-processor-spamoracle spam spam-use-spamoracle)
511 (gnus-group-ham-exit-processor-ifile ham spam-use-ifile)
512 (gnus-group-ham-exit-processor-bogofilter ham spam-use-bogofilter)
513 (gnus-group-ham-exit-processor-stat ham spam-use-stat)
514 (gnus-group-ham-exit-processor-whitelist ham spam-use-whitelist)
515 (gnus-group-ham-exit-processor-BBDB ham spam-use-BBDB)
516 (gnus-group-ham-exit-processor-copy ham spam-use-ham-copy)
517 (gnus-group-ham-exit-processor-spamoracle ham spam-use-spamoracle))
518 "The `spam-list-of-processors' list.
519 This list contains pairs associating a ham/spam exit processor
520 variable with a classification and a spam-use-* variable.")
522 (defun spam-group-processor-p (group processor)
523 (if (and (stringp group)
525 (or (member processor (nth 0 (gnus-parameter-spam-process group)))
526 (spam-group-processor-multiple-p
528 (cdr-safe (assoc processor spam-list-of-processors))))
531 (defun spam-group-processor-multiple-p (group processor-info)
532 (let* ((classification (nth 0 processor-info))
533 (check (nth 1 processor-info))
534 (parameters (nth 0 (gnus-parameter-spam-process group)))
536 (dolist (parameter parameters)
537 (when (and (null found)
539 (eq classification (nth 0 parameter))
540 (eq check (nth 1 parameter)))
544 (defun spam-group-spam-processor-report-gmane-p (group)
545 (spam-group-processor-p group 'gnus-group-spam-exit-processor-report-gmane))
547 (defun spam-group-spam-processor-bogofilter-p (group)
548 (spam-group-processor-p group 'gnus-group-spam-exit-processor-bogofilter))
550 (defun spam-group-spam-processor-blacklist-p (group)
551 (spam-group-processor-p group 'gnus-group-spam-exit-processor-blacklist))
553 (defun spam-group-spam-processor-ifile-p (group)
554 (spam-group-processor-p group 'gnus-group-spam-exit-processor-ifile))
556 (defun spam-group-ham-processor-ifile-p (group)
557 (spam-group-processor-p group 'gnus-group-ham-exit-processor-ifile))
559 (defun spam-group-spam-processor-spamoracle-p (group)
560 (spam-group-processor-p group 'gnus-group-spam-exit-processor-spamoracle))
562 (defun spam-group-ham-processor-bogofilter-p (group)
563 (spam-group-processor-p group 'gnus-group-ham-exit-processor-bogofilter))
565 (defun spam-group-spam-processor-stat-p (group)
566 (spam-group-processor-p group 'gnus-group-spam-exit-processor-stat))
568 (defun spam-group-ham-processor-stat-p (group)
569 (spam-group-processor-p group 'gnus-group-ham-exit-processor-stat))
571 (defun spam-group-ham-processor-whitelist-p (group)
572 (spam-group-processor-p group 'gnus-group-ham-exit-processor-whitelist))
574 (defun spam-group-ham-processor-BBDB-p (group)
575 (spam-group-processor-p group 'gnus-group-ham-exit-processor-BBDB))
577 (defun spam-group-ham-processor-copy-p (group)
578 (spam-group-processor-p group 'gnus-group-ham-exit-processor-copy))
580 (defun spam-group-ham-processor-spamoracle-p (group)
581 (spam-group-processor-p group 'gnus-group-ham-exit-processor-spamoracle))
583 (defun spam-report-articles-gmane (n)
584 "Report the current message as spam.
585 Respects the process/prefix convention."
587 (dolist (article (gnus-summary-work-articles n))
588 (gnus-summary-remove-process-mark article)
589 (spam-report-gmane article)))
591 ;;; Summary entry and exit processing.
593 (defun spam-summary-prepare ()
594 (setq spam-old-ham-articles
595 (spam-list-articles gnus-newsgroup-articles 'ham))
596 (setq spam-old-spam-articles
597 (spam-list-articles gnus-newsgroup-articles 'spam))
598 (spam-mark-junk-as-spam-routine))
600 ;; The spam processors are invoked for any group, spam or ham or neither
601 (defun spam-summary-prepare-exit ()
602 (unless gnus-group-is-exiting-without-update-p
603 (gnus-message 6 "Exiting summary buffer and applying spam rules")
605 ;; first of all, unregister any articles that are no longer ham or spam
606 ;; we have to iterate over the processors, or else we'll be too slow
607 (dolist (classification '(spam ham))
608 (let* ((old-articles (if (eq classification 'spam)
609 spam-old-spam-articles
610 spam-old-ham-articles))
611 (new-articles (spam-list-articles
612 gnus-newsgroup-articles
614 (changed-articles (gnus-set-difference old-articles new-articles)))
615 ;; now that we have the changed articles, we go through the processors
616 (dolist (processor-param spam-list-of-processors)
617 (let ((processor (nth 0 processor-param))
618 (processor-classification (nth 1 processor-param))
619 (check (nth 2 processor-param))
621 (dolist (article changed-articles)
622 (let ((id (spam-fetch-field-message-id-fast article)))
623 (when (spam-log-unregistration-needed-p
624 id 'process classification check)
625 (push article unregister-list))))
626 ;; call spam-register-routine with specific articles to unregister,
627 ;; when there are articles to unregister and the check is enabled
628 (when (and unregister-list (symbol-value check))
629 (spam-register-routine classification check t unregister-list))))))
631 ;; find all the spam processors applicable to this group
632 (dolist (processor-param spam-list-of-processors)
633 (let ((processor (nth 0 processor-param))
634 (classification (nth 1 processor-param))
635 (check (nth 2 processor-param)))
636 (when (and (eq 'spam classification)
637 (spam-group-processor-p gnus-newsgroup-name processor))
638 (spam-register-routine classification check))))
640 (if spam-move-spam-nonspam-groups-only
641 (when (not (spam-group-spam-contents-p gnus-newsgroup-name))
642 (spam-mark-spam-as-expired-and-move-routine
643 (gnus-parameter-spam-process-destination gnus-newsgroup-name)))
644 (gnus-message 5 "Marking spam as expired and moving it to %s"
646 (spam-mark-spam-as-expired-and-move-routine
647 (gnus-parameter-spam-process-destination gnus-newsgroup-name)))
649 ;; now we redo spam-mark-spam-as-expired-and-move-routine to only
650 ;; expire spam, in case the above did not expire them
651 (gnus-message 5 "Marking spam as expired without moving it")
652 (spam-mark-spam-as-expired-and-move-routine nil)
654 (when (or (spam-group-ham-contents-p gnus-newsgroup-name)
655 (and (spam-group-spam-contents-p gnus-newsgroup-name)
656 spam-process-ham-in-spam-groups)
657 spam-process-ham-in-nonham-groups)
658 ;; find all the ham processors applicable to this group
659 (dolist (processor-param spam-list-of-processors)
660 (let ((processor (nth 0 processor-param))
661 (classification (nth 1 processor-param))
662 (check (nth 2 processor-param)))
663 (when (and (eq 'ham classification)
664 (spam-group-processor-p gnus-newsgroup-name processor))
665 (spam-register-routine classification check)))))
667 (when (spam-group-ham-processor-copy-p gnus-newsgroup-name)
668 (gnus-message 5 "Copying ham")
669 (spam-ham-copy-routine
670 (gnus-parameter-ham-process-destination gnus-newsgroup-name)))
672 ;; now move all ham articles out of spam groups
673 (when (spam-group-spam-contents-p gnus-newsgroup-name)
674 (gnus-message 5 "Moving ham messages from spam group")
675 (spam-ham-move-routine
676 (gnus-parameter-ham-process-destination gnus-newsgroup-name))))
678 (setq spam-old-ham-articles nil)
679 (setq spam-old-spam-articles nil))
681 (defun spam-mark-junk-as-spam-routine ()
682 ;; check the global list of group names spam-junk-mailgroups and the
684 (when (spam-group-spam-contents-p gnus-newsgroup-name)
685 (gnus-message 5 "Marking %s articles as spam"
686 (if spam-mark-only-unseen-as-spam
689 (let ((articles (if spam-mark-only-unseen-as-spam
690 gnus-newsgroup-unseen
691 gnus-newsgroup-unreads)))
692 (dolist (article articles)
693 (gnus-summary-mark-article article gnus-spam-mark)))))
695 (defun spam-mark-spam-as-expired-and-move-routine (&rest groups)
696 (if (and (car-safe groups) (listp (car-safe groups)))
697 (apply 'spam-mark-spam-as-expired-and-move-routine (car groups))
698 (gnus-summary-kill-process-mark)
699 (let ((articles gnus-newsgroup-articles)
700 (backend-supports-deletions
701 (gnus-check-backend-function
702 'request-move-article gnus-newsgroup-name))
703 article tomove deletep)
704 (dolist (article articles)
705 (when (eq (gnus-summary-article-mark article) gnus-spam-mark)
706 (gnus-summary-mark-article article gnus-expirable-mark)
707 (push article tomove)))
709 ;; now do the actual copies
710 (dolist (group groups)
713 (dolist (article tomove)
714 (gnus-summary-set-process-mark article))
716 (if (or (not backend-supports-deletions)
717 (> (length groups) 1))
719 (gnus-summary-copy-article nil group)
721 (gnus-summary-move-article nil group)))))
723 ;; now delete the articles, if there was a copy done, and the
725 (when (and deletep backend-supports-deletions)
726 (dolist (article tomove)
727 (gnus-summary-set-process-mark article))
729 (let ((gnus-novice-user nil)) ; don't ask me if I'm sure
730 (gnus-summary-delete-article nil))))
732 (gnus-summary-yank-process-mark))))
734 (defun spam-ham-copy-or-move-routine (copy groups)
735 (gnus-summary-kill-process-mark)
736 (let ((todo (spam-list-articles gnus-newsgroup-articles 'ham))
737 (backend-supports-deletions
738 (gnus-check-backend-function
739 'request-move-article gnus-newsgroup-name))
740 (respool-method (gnus-find-method-for-group gnus-newsgroup-name))
741 article mark todo deletep respool)
743 (when (member 'respool groups)
744 (setq respool t) ; boolean for later
745 (setq groups '("fake"))) ; when respooling, groups are dynamic so fake it
747 ;; now do the actual move
748 (dolist (group groups)
749 (when (and todo (stringp group))
750 (dolist (article todo)
751 (when spam-mark-ham-unread-before-move-from-spam-group
752 (gnus-summary-mark-article article gnus-unread-mark))
753 (gnus-summary-set-process-mark article))
755 (if respool ; respooling is with a "fake" group
756 (let ((spam-split-disabled
757 (or spam-split-disabled
758 spam-disable-spam-split-during-ham-respool)))
759 (gnus-summary-respool-article nil respool-method))
760 (if (or (not backend-supports-deletions) ; else, we are not respooling
761 (> (length groups) 1))
762 (progn ; if copying, copy and set deletep
763 (gnus-summary-copy-article nil group)
765 (gnus-summary-move-article nil group))))) ; else move articles
767 ;; now delete the articles, unless a) copy is t, and there was a copy done
768 ;; b) a move was done to a single group
769 ;; c) backend-supports-deletions is nil
771 (when (and deletep backend-supports-deletions)
772 (dolist (article todo)
773 (gnus-summary-set-process-mark article))
775 (let ((gnus-novice-user nil)) ; don't ask me if I'm sure
776 (gnus-summary-delete-article nil))))))
778 (gnus-summary-yank-process-mark))
780 (defun spam-ham-copy-routine (&rest groups)
781 (if (and (car-safe groups) (listp (car-safe groups)))
782 (apply 'spam-ham-copy-routine (car groups))
783 (spam-ham-copy-or-move-routine t groups)))
785 (defun spam-ham-move-routine (&rest groups)
786 (if (and (car-safe groups) (listp (car-safe groups)))
787 (apply 'spam-ham-move-routine (car groups))
788 (spam-ham-copy-or-move-routine nil groups)))
790 (defun spam-get-article-as-string (article)
791 (when (numberp article)
793 (gnus-request-article-this-buffer
799 ;; (defun spam-get-article-as-filename (article)
800 ;; (let ((article-filename))
801 ;; (when (numberp article)
802 ;; (nnml-possibly-change-directory
803 ;; (gnus-group-real-name gnus-newsgroup-name))
804 ;; (setq article-filename (expand-file-name
805 ;; (int-to-string article) nnml-current-directory)))
806 ;; (if (file-exists-p article-filename)
810 (defun spam-fetch-field-fast (article field &optional prepared-data-header)
811 "Fetch a field quickly, using the internal gnus-data-list function"
812 (when (numberp article)
813 (let* ((data-header (or prepared-data-header
814 (spam-fetch-article-header article))))
815 (if (arrayp data-header)
818 (mail-header-from data-header))
819 ((equal field 'message-id)
820 (mail-header-message-id data-header))
821 ((equal field 'subject)
822 (mail-header-subject data-header))
823 ((equal field 'references)
824 (mail-header-references data-header))
826 (mail-header-date data-header))
828 (mail-header-xref data-header))
829 ((equal field 'extra)
830 (mail-header-extra data-header))
833 (gnus-error 5 "Article %d has a nil data header" article)))))
835 (defun spam-fetch-field-from-fast (article &optional prepared-data-header)
836 (spam-fetch-field-fast article 'from prepared-data-header))
838 (defun spam-fetch-field-subject-fast (article &optional prepared-data-header)
839 (spam-fetch-field-fast article 'subject prepared-data-header))
841 (defun spam-fetch-field-message-id-fast (article &optional prepared-data-header)
842 (spam-fetch-field-fast article 'message-id prepared-data-header))
844 (defun spam-generate-fake-headers (article)
845 (let ((dh (spam-fetch-article-header article)))
849 (concat "From: %s\nSubject: %s\nMessage-ID: %s\n"
850 "Date: %s\nReferences: %s\nXref: %s\n")
851 (spam-fetch-field-fast article 'from dh)
852 (spam-fetch-field-fast article 'subject dh)
853 (spam-fetch-field-fast article 'message-id dh)
854 (spam-fetch-field-fast article 'date dh)
855 (spam-fetch-field-fast article 'references dh)
856 (spam-fetch-field-fast article 'xref dh))
857 (when (spam-fetch-field-fast article 'extra dh)
858 (format "%s\n" (spam-fetch-field-fast article 'extra dh))))
861 "spam-generate-fake-headers: article %d didn't have a valid header"
864 (defun spam-fetch-article-header (article)
866 (set-buffer gnus-summary-buffer)
867 (nth 3 (assq article gnus-newsgroup-data))))
870 ;;;; Spam determination.
872 (defvar spam-list-of-checks
873 '((spam-use-blacklist . spam-check-blacklist)
874 (spam-use-regex-headers . spam-check-regex-headers)
875 (spam-use-regex-body . spam-check-regex-body)
876 (spam-use-whitelist . spam-check-whitelist)
877 (spam-use-BBDB . spam-check-BBDB)
878 (spam-use-ifile . spam-check-ifile)
879 (spam-use-spamoracle . spam-check-spamoracle)
880 (spam-use-stat . spam-check-stat)
881 (spam-use-blackholes . spam-check-blackholes)
882 (spam-use-hashcash . spam-check-hashcash)
883 (spam-use-bogofilter-headers . spam-check-bogofilter-headers)
884 (spam-use-bogofilter . spam-check-bogofilter))
885 "The spam-list-of-checks list contains pairs associating a
886 parameter variable with a spam checking function. If the
887 parameter variable is true, then the checking function is called,
888 and its value decides what happens. Each individual check may
889 return nil, t, or a mailgroup name. The value nil means that the
890 check does not yield a decision, and so, that further checks are
891 needed. The value t means that the message is definitely not
892 spam, and that further spam checks should be inhibited.
893 Otherwise, a mailgroup name or the symbol 'spam (depending on
894 spam-split-symbolic-return) is returned where the mail should go,
895 and further checks are also inhibited. The usual mailgroup name
896 is the value of `spam-split-group', meaning that the message is
899 (defvar spam-list-of-statistical-checks
906 "The spam-list-of-statistical-checks list contains all the mail
907 splitters that need to have the full message body available.
908 Note that you should fetch extra headers if you don't like this,
909 e.g. fetch the 'Received' header for spam-use-blackholes.")
911 (defun spam-split (&rest specific-checks)
912 "Split this message into the `spam' group if it is spam.
913 This function can be used as an entry in the variable `nnmail-split-fancy',
914 for example like this: (: spam-split). It can take checks as
915 parameters. A string as a parameter will set the
916 spam-split-group to that string.
918 See the Info node `(gnus)Fancy Mail Splitting' for more details."
920 (setq spam-split-last-successful-check nil)
921 (unless spam-split-disabled
922 (let ((spam-split-group-choice spam-split-group))
923 (dolist (check specific-checks)
924 (when (stringp check)
925 (setq spam-split-group-choice check)
926 (setq specific-checks (delq check specific-checks))))
928 (let ((spam-split-group spam-split-group-choice))
931 (dolist (check spam-list-of-statistical-checks)
932 (when (and (symbolp check)
933 (or (symbol-value check)
934 (memq check specific-checks)))
936 (gnus-message 8 "spam-split: widening the buffer (%s requires it)"
939 ;; (progn (widen) (debug (buffer-string)))
940 (let ((list-of-checks spam-list-of-checks)
942 (while (and list-of-checks (not decision))
943 (let ((pair (pop list-of-checks)))
945 ;; either, given specific checks, this is one of them
946 (and specific-checks (memq (car pair) specific-checks))
947 ;; or, given no specific checks, spam-use-CHECK is set
948 (and (null specific-checks) (symbol-value (car pair))))
949 (gnus-message 5 "spam-split: calling the %s function"
950 (symbol-name (cdr pair)))
951 (setq decision (funcall (cdr pair)))
952 ;; if we got a decision at all, save the current check
954 (setq spam-split-last-successful-check (car pair)))
956 (when (eq decision 'spam)
957 (unless spam-split-symbolic-return
960 (format "spam-split got %s but %s is nil"
961 (symbol-name decision)
962 (symbol-name spam-split-symbolic-return))))))))
964 (if spam-split-symbolic-return-positive 'ham nil)
967 (defun spam-find-spam ()
968 "This function will detect spam in the current newsgroup using spam-split."
971 (let* ((group gnus-newsgroup-name)
972 (autodetect (gnus-parameter-spam-autodetect group))
973 (methods (gnus-parameter-spam-autodetect-methods group))
974 (first-method (nth 0 methods))
975 (articles (if spam-autodetect-recheck-messages
976 gnus-newsgroup-articles
977 gnus-newsgroup-unseen))
978 article-cannot-be-faked)
980 (dolist (check spam-list-of-statistical-checks)
981 (when (and (symbolp check)
982 (memq check methods))
983 (setq article-cannot-be-faked t)
986 (when (memq 'default methods)
987 (setq article-cannot-be-faked t))
989 (when (and autodetect
990 (not (equal first-method 'none)))
993 (let ((id (spam-fetch-field-message-id-fast article))
994 (subject (spam-fetch-field-subject-fast article))
995 (sender (spam-fetch-field-from-fast article))
999 (gnus-error 5 "Article %d has no message ID!" article))
1001 (when (and id spam-log-to-registry)
1002 (setq registry-lookup (spam-log-registration-type id 'incoming))
1003 (when registry-lookup
1006 "spam-find-spam: message %s was already registered incoming"
1009 (let* ((spam-split-symbolic-return t)
1010 (spam-split-symbolic-return-positive t)
1011 (fake-headers (spam-generate-fake-headers article))
1015 (if article-cannot-be-faked
1016 (gnus-request-article-this-buffer
1019 ;; else, we fake the article
1020 (when fake-headers (insert fake-headers)))
1021 (if (or (null first-method)
1022 (equal first-method 'default))
1024 (apply 'spam-split methods))))))
1025 (if (equal split-return 'spam)
1026 (gnus-summary-mark-article article gnus-spam-mark))
1028 (when (and id split-return spam-log-to-registry)
1029 (when (zerop (gnus-registry-group-count id))
1030 (gnus-registry-add-group
1031 id group subject sender))
1033 (unless registry-lookup
1034 (spam-log-processing-to-registry
1038 spam-split-last-successful-check
1042 (defvar spam-registration-functions
1043 ;; first the ham register, second the spam register function
1044 ;; third the ham unregister, fourth the spam unregister function
1045 '((spam-use-blacklist nil
1046 spam-blacklist-register-routine
1048 spam-blacklist-unregister-routine)
1049 (spam-use-whitelist spam-whitelist-register-routine
1051 spam-whitelist-unregister-routine
1053 (spam-use-BBDB spam-BBDB-register-routine
1055 spam-BBDB-unregister-routine
1057 (spam-use-ifile spam-ifile-register-ham-routine
1058 spam-ifile-register-spam-routine
1059 spam-ifile-unregister-ham-routine
1060 spam-ifile-unregister-spam-routine)
1061 (spam-use-spamoracle spam-spamoracle-learn-ham
1062 spam-spamoracle-learn-spam
1063 spam-spamoracle-unlearn-ham
1064 spam-spamoracle-unlearn-spam)
1065 (spam-use-stat spam-stat-register-ham-routine
1066 spam-stat-register-spam-routine
1067 spam-stat-unregister-ham-routine
1068 spam-stat-unregister-spam-routine)
1069 ;; note that spam-use-gmane is not a legitimate check
1071 spam-report-gmane-register-routine
1072 ;; does Gmane support unregistration?
1075 (spam-use-bogofilter spam-bogofilter-register-ham-routine
1076 spam-bogofilter-register-spam-routine
1077 spam-bogofilter-unregister-ham-routine
1078 spam-bogofilter-unregister-spam-routine))
1079 "The spam-registration-functions list contains pairs
1080 associating a parameter variable with the ham and spam
1081 registration functions, and the ham and spam unregistration
1084 (defun spam-classification-valid-p (classification)
1085 (or (eq classification 'spam)
1086 (eq classification 'ham)))
1088 (defun spam-process-type-valid-p (process-type)
1089 (or (eq process-type 'incoming)
1090 (eq process-type 'process)))
1092 (defun spam-registration-check-valid-p (check)
1093 (assoc check spam-registration-functions))
1095 (defun spam-unregistration-check-valid-p (check)
1096 (assoc check spam-registration-functions))
1098 (defun spam-registration-function (classification check)
1099 (let ((flist (cdr-safe (assoc check spam-registration-functions))))
1100 (if (eq classification 'spam)
1104 (defun spam-unregistration-function (classification check)
1105 (let ((flist (cdr-safe (assoc check spam-registration-functions))))
1106 (if (eq classification 'spam)
1110 (defun spam-list-articles (articles classification)
1111 (let ((mark-check (if (eq classification 'spam)
1112 'spam-group-spam-mark-p
1113 'spam-group-ham-mark-p))
1114 list mark-cache-yes mark-cache-no)
1115 (dolist (article articles)
1116 (let ((mark (gnus-summary-article-mark article)))
1117 (unless (memq mark mark-cache-no)
1118 (if (memq mark mark-cache-yes)
1120 ;; else, we have to actually check the mark
1121 (if (funcall mark-check
1126 (push mark mark-cache-yes))
1127 (push mark mark-cache-no))))))
1130 (defun spam-register-routine (classification
1132 &optional unregister
1134 (when (and (spam-classification-valid-p classification)
1135 (spam-registration-check-valid-p check))
1136 (let* ((register-function
1137 (spam-registration-function classification check))
1138 (unregister-function
1139 (spam-unregistration-function classification check))
1140 (run-function (if unregister
1143 (log-function (if unregister
1144 'spam-log-undo-registration
1145 'spam-log-processing-to-registry))
1149 ;; make list of articles, using specific-articles if given
1150 (setq articles (or specific-articles
1152 gnus-newsgroup-articles
1155 (gnus-message 5 "%s %d %s articles with classification %s, check %s"
1156 (if unregister "Unregistering" "Registering")
1158 (if specific-articles "specific" "")
1159 (symbol-name classification)
1160 (symbol-name check))
1161 (funcall run-function articles)
1162 ;; now log all the registrations (or undo them, depending on unregister)
1163 (dolist (article articles)
1164 (funcall log-function
1165 (spam-fetch-field-message-id-fast article)
1169 gnus-newsgroup-name))))))
1171 ;;; log a ham- or spam-processor invocation to the registry
1172 (defun spam-log-processing-to-registry (id type classification check group)
1173 (when spam-log-to-registry
1174 (if (and (stringp id)
1176 (spam-process-type-valid-p type)
1177 (spam-classification-valid-p classification)
1178 (spam-registration-check-valid-p check))
1179 (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type)))
1180 (cell (list classification check group)))
1181 (push cell cell-list)
1182 (gnus-registry-store-extra-entry
1187 (gnus-error 5 (format "%s called with bad ID, type, classification, check, or group"
1188 "spam-log-processing-to-registry")))))
1190 ;;; check if a ham- or spam-processor registration has been done
1191 (defun spam-log-registered-p (id type)
1192 (when spam-log-to-registry
1193 (if (and (stringp id)
1194 (spam-process-type-valid-p type))
1195 (cdr-safe (gnus-registry-fetch-extra id type))
1197 (gnus-error 5 (format "%s called with bad ID, type, classification, or check"
1198 "spam-log-registered-p"))
1201 ;;; check what a ham- or spam-processor registration says
1202 ;;; returns nil if conflicting registrations are found
1203 (defun spam-log-registration-type (id type)
1206 (dolist (reg (spam-log-registered-p id type))
1207 (let ((classification (nth 0 reg)))
1208 (when (spam-classification-valid-p classification)
1210 (not (eq classification decision)))
1211 (setq count (+ 1 count)))
1212 (setq decision classification))))
1217 ;;; check if a ham- or spam-processor registration needs to be undone
1218 (defun spam-log-unregistration-needed-p (id type classification check)
1219 (when spam-log-to-registry
1220 (if (and (stringp id)
1221 (spam-process-type-valid-p type)
1222 (spam-classification-valid-p classification)
1223 (spam-registration-check-valid-p check))
1224 (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type)))
1226 (dolist (cell cell-list)
1228 (when (and (eq classification (nth 0 cell))
1229 (eq check (nth 1 cell)))
1233 (gnus-error 5 (format "%s called with bad ID, type, classification, or check"
1234 "spam-log-unregistration-needed-p"))
1238 ;;; undo a ham- or spam-processor registration (the group is not used)
1239 (defun spam-log-undo-registration (id type classification check &optional group)
1240 (when (and spam-log-to-registry
1241 (spam-log-unregistration-needed-p id type classification check))
1242 (if (and (stringp id)
1243 (spam-process-type-valid-p type)
1244 (spam-classification-valid-p classification)
1245 (spam-registration-check-valid-p check))
1246 (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type)))
1247 new-cell-list found)
1248 (dolist (cell cell-list)
1249 (unless (and (eq classification (nth 0 cell))
1250 (eq check (nth 1 cell)))
1251 (push cell new-cell-list)))
1252 (gnus-registry-store-extra-entry
1257 (gnus-error 5 (format "%s called with bad ID, type, check, or group"
1258 "spam-log-undo-registration"))
1261 ;;; set up IMAP widening if it's necessary
1262 (defun spam-setup-widening ()
1263 (dolist (check spam-list-of-statistical-checks)
1264 (when (symbol-value check)
1265 (setq nnimap-split-download-body-default t))))
1270 (defun spam-check-regex-body ()
1271 (let ((spam-regex-headers-ham spam-regex-body-ham)
1272 (spam-regex-headers-spam spam-regex-body-spam))
1273 (spam-check-regex-headers t)))
1278 (defun spam-check-regex-headers (&optional body)
1279 (let ((type (if body "body" "header"))
1280 (spam-split-group (if spam-split-symbolic-return
1284 (dolist (h-regex spam-regex-headers-ham)
1286 (goto-char (point-min))
1287 (when (re-search-forward h-regex nil t)
1288 (message "Ham regex %s search positive." type)
1290 (dolist (s-regex spam-regex-headers-spam)
1292 (goto-char (point-min))
1293 (when (re-search-forward s-regex nil t)
1294 (message "Spam regex %s search positive." type)
1296 (setq ret spam-split-group))))
1302 (defun spam-reverse-ip-string (ip)
1304 (mapconcat 'identity
1305 (nreverse (split-string ip "\\."))
1308 (defun spam-check-blackholes ()
1309 "Check the Received headers for blackholed relays."
1310 (let ((headers (message-fetch-field "received"))
1311 (spam-split-group (if spam-split-symbolic-return
1318 (goto-char (point-min))
1319 (gnus-message 5 "Checking headers for relay addresses")
1320 (while (re-search-forward
1321 "\\([0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+\\)" nil t)
1322 (gnus-message 9 "Blackhole search found host IP %s." (match-string 1))
1323 (push (spam-reverse-ip-string (match-string 1))
1325 (dolist (server spam-blackhole-servers)
1327 (unless (and spam-blackhole-good-server-regex
1328 ;; match the good-server-regex against the reversed (again) IP string
1330 spam-blackhole-good-server-regex
1331 (spam-reverse-ip-string ip)))
1333 (let ((query-string (concat ip "." server)))
1335 (let ((query-result (query-dig query-string)))
1337 (gnus-message 5 "(DIG): positive blackhole check '%s'"
1339 (push (list ip server query-result)
1341 ;; else, if not using dig.el
1342 (when (query-dns query-string)
1343 (gnus-message 5 "positive blackhole check")
1344 (push (list ip server (query-dns query-string 'TXT))
1355 (defun spam-check-hashcash ()
1356 "Check the headers for hashcash payments."
1357 (mail-check-payment))) ;mail-check-payment returns a boolean
1360 (defalias 'mail-check-payment 'ignore)
1361 (defalias 'spam-check-hashcash 'ignore))))
1365 ;;; original idea for spam-check-BBDB from Alexander Kotelnikov
1366 ;;; <sacha@giotto.sj.ru>
1368 ;; all this is done inside a condition-case to trap errors
1375 ;; when the BBDB changes, we want to clear out our cache
1376 (defun spam-clear-cache-BBDB (&rest immaterial)
1377 (spam-clear-cache 'spam-use-BBDB))
1379 (add-hook 'bbdb-change-hook 'spam-clear-cache-BBDB)
1381 (defun spam-enter-ham-BBDB (addresses &optional remove)
1382 "Enter an address into the BBDB; implies ham (non-spam) sender"
1383 (dolist (from addresses)
1384 (when (stringp from)
1385 (let* ((parsed-address (gnus-extract-address-components from))
1386 (name (or (nth 0 parsed-address) "Ham Sender"))
1387 (remove-function (if remove
1388 'bbdb-delete-record-internal
1390 (net-address (nth 1 parsed-address))
1391 (record (and net-address
1392 (bbdb-search-simple nil net-address))))
1394 (gnus-message 5 "%s address %s %s BBDB"
1395 (if remove "Deleting" "Adding")
1397 (if remove "from" "to"))
1399 (funcall remove-function record)
1400 (bbdb-create-internal name nil net-address nil nil
1401 "ham sender added by spam.el")))))))
1403 (defun spam-BBDB-register-routine (articles &optional unregister)
1405 (dolist (article articles)
1406 (when (stringp (spam-fetch-field-from-fast article))
1407 (push (spam-fetch-field-from-fast article) addresses)))
1408 ;; now do the register/unregister action
1409 (spam-enter-ham-BBDB addresses unregister)))
1411 (defun spam-BBDB-unregister-routine (articles)
1412 (spam-BBDB-register-routine articles t))
1414 (defun spam-check-BBDB ()
1415 "Mail from people in the BBDB is classified as ham or non-spam"
1416 (let ((who (message-fetch-field "from"))
1417 (spam-split-group (if spam-split-symbolic-return
1420 bbdb-cache bbdb-hashtable)
1421 (when spam-cache-lookups
1422 (setq bbdb-cache (gethash 'spam-use-BBDB spam-caches))
1425 ;; this is the expanded (bbdb-hashtable) macro
1426 ;; without the debugging support
1427 (with-current-buffer (bbdb-buffer)
1429 (save-window-excursion
1430 (bbdb-records nil t)
1432 (puthash 'spam-use-BBDB bbdb-cache spam-caches)))
1434 (setq who (nth 1 (gnus-extract-address-components who)))
1436 (if spam-cache-lookups
1438 (intern-soft who bbdb-cache))
1439 (bbdb-search-simple nil who))
1441 (if spam-use-BBDB-exclusive
1446 (defalias 'bbdb-search-simple 'ignore)
1447 (defalias 'bbdb-records 'ignore)
1448 (defalias 'bbdb-buffer 'ignore)
1449 (defalias 'spam-check-BBDB 'ignore)
1450 (defalias 'spam-BBDB-register-routine 'ignore)
1451 (defalias 'spam-enter-ham-BBDB 'ignore)
1452 (defalias 'bbdb-create-internal 'ignore)
1453 (defalias 'bbdb-delete-record-internal 'ignore)
1454 (defalias 'bbdb-records 'ignore))))
1459 ;;; check the ifile backend; return nil if the mail was NOT classified
1462 (defun spam-get-ifile-database-parameter ()
1463 "Get the command-line parameter for ifile's database from
1464 spam-ifile-database-path."
1465 (if spam-ifile-database-path
1466 (format "--db-file=%s" spam-ifile-database-path)
1469 (defun spam-check-ifile ()
1470 "Check the ifile backend for the classification of this message."
1471 (let ((article-buffer-name (buffer-name))
1472 (spam-split-group (if spam-split-symbolic-return
1477 (let ((temp-buffer-name (buffer-name))
1478 (db-param (spam-get-ifile-database-parameter)))
1480 (set-buffer article-buffer-name)
1481 (apply 'call-process-region
1482 (point-min) (point-max) spam-ifile-path
1483 nil temp-buffer-name nil "-c"
1484 (if db-param `(,db-param "-q") `("-q"))))
1485 ;; check the return now (we're back in the temp buffer)
1486 (goto-char (point-min))
1488 (setq category (buffer-substring (point) (point-at-eol))))
1489 (when (not (zerop (length category))) ; we need a category here
1490 (if spam-ifile-all-categories
1491 (setq return category)
1492 ;; else, if spam-ifile-all-categories is not set...
1493 (when (string-equal spam-ifile-spam-category category)
1494 (setq return spam-split-group)))))) ; note return is nil otherwise
1497 (defun spam-ifile-register-with-ifile (articles category &optional unregister)
1498 "Register an article, given as a string, with a category.
1499 Uses `gnus-newsgroup-name' if category is nil (for ham registration)."
1500 (let ((category (or category gnus-newsgroup-name))
1501 (add-or-delete-option (if unregister "-d" "-i"))
1502 (db (spam-get-ifile-database-parameter))
1505 (dolist (article articles)
1506 (let ((article-string (spam-get-article-as-string article)))
1507 (when (stringp article-string)
1508 (insert article-string))))
1509 (apply 'call-process-region
1510 (point-min) (point-max) spam-ifile-path
1512 add-or-delete-option category
1513 (if db `(,db "-h") `("-h"))))))
1515 (defun spam-ifile-register-spam-routine (articles &optional unregister)
1516 (spam-ifile-register-with-ifile articles spam-ifile-spam-category unregister))
1518 (defun spam-ifile-unregister-spam-routine (articles)
1519 (spam-ifile-register-spam-routine articles t))
1521 (defun spam-ifile-register-ham-routine (articles &optional unregister)
1522 (spam-ifile-register-with-ifile articles spam-ifile-ham-category unregister))
1524 (defun spam-ifile-unregister-ham-routine (articles)
1525 (spam-ifile-register-ham-routine articles t))
1532 (let ((spam-stat-install-hooks nil))
1533 (require 'spam-stat))
1535 (defun spam-check-stat ()
1536 "Check the spam-stat backend for the classification of this message"
1537 (let ((spam-split-group (if spam-split-symbolic-return
1540 (spam-stat-split-fancy-spam-group spam-split-group) ; override
1541 (spam-stat-buffer (buffer-name)) ; stat the current buffer
1543 (spam-stat-split-fancy)))
1545 (defun spam-stat-register-spam-routine (articles &optional unregister)
1546 (dolist (article articles)
1547 (let ((article-string (spam-get-article-as-string article)))
1549 (insert article-string)
1551 (spam-stat-buffer-change-to-non-spam)
1552 (spam-stat-buffer-is-spam))))))
1554 (defun spam-stat-unregister-spam-routine (articles)
1555 (spam-stat-register-spam-routine articles t))
1557 (defun spam-stat-register-ham-routine (articles &optional unregister)
1558 (dolist (article articles)
1559 (let ((article-string (spam-get-article-as-string article)))
1561 (insert article-string)
1563 (spam-stat-buffer-change-to-spam)
1564 (spam-stat-buffer-is-non-spam))))))
1566 (defun spam-stat-unregister-ham-routine (articles)
1567 (spam-stat-register-ham-routine articles t))
1569 (defun spam-maybe-spam-stat-load ()
1570 (when spam-use-stat (spam-stat-load)))
1572 (defun spam-maybe-spam-stat-save ()
1573 (when spam-use-stat (spam-stat-save))))
1576 (defalias 'spam-stat-load 'ignore)
1577 (defalias 'spam-stat-save 'ignore)
1578 (defalias 'spam-maybe-spam-stat-load 'ignore)
1579 (defalias 'spam-maybe-spam-stat-save 'ignore)
1580 (defalias 'spam-stat-register-ham-routine 'ignore)
1581 (defalias 'spam-stat-unregister-ham-routine 'ignore)
1582 (defalias 'spam-stat-register-spam-routine 'ignore)
1583 (defalias 'spam-stat-unregister-spam-routine 'ignore)
1584 (defalias 'spam-stat-buffer-is-spam 'ignore)
1585 (defalias 'spam-stat-buffer-change-to-spam 'ignore)
1586 (defalias 'spam-stat-buffer-is-non-spam 'ignore)
1587 (defalias 'spam-stat-buffer-change-to-non-spam 'ignore)
1588 (defalias 'spam-stat-split-fancy 'ignore)
1589 (defalias 'spam-check-stat 'ignore))))
1593 ;;;; Blacklists and whitelists.
1595 (defvar spam-whitelist-cache nil)
1596 (defvar spam-blacklist-cache nil)
1598 (defun spam-kill-whole-line ()
1600 (let ((kill-whole-line t))
1603 ;;; address can be a list, too
1604 (defun spam-enter-whitelist (address &optional remove)
1605 "Enter ADDRESS (list or single) into the whitelist.
1606 With a non-nil REMOVE, remove them."
1607 (interactive "sAddress: ")
1608 (spam-enter-list address spam-whitelist remove)
1609 (setq spam-whitelist-cache nil)
1610 (spam-clear-cache 'spam-use-whitelist))
1612 ;;; address can be a list, too
1613 (defun spam-enter-blacklist (address &optional remove)
1614 "Enter ADDRESS (list or single) into the blacklist.
1615 With a non-nil REMOVE, remove them."
1616 (interactive "sAddress: ")
1617 (spam-enter-list address spam-blacklist remove)
1618 (setq spam-blacklist-cache nil)
1619 (spam-clear-cache 'spam-use-whitelist))
1621 (defun spam-enter-list (addresses file &optional remove)
1622 "Enter ADDRESSES into the given FILE.
1623 Either the whitelist or the blacklist files can be used. With
1624 REMOVE not nil, remove the ADDRESSES."
1625 (if (stringp addresses)
1626 (spam-enter-list (list addresses) file remove)
1627 ;; else, we have a list of addresses here
1628 (unless (file-exists-p (file-name-directory file))
1629 (make-directory (file-name-directory file) t))
1632 (find-file-noselect file))
1633 (dolist (a addresses)
1635 (goto-char (point-min))
1636 (if (re-search-forward (regexp-quote a) nil t)
1637 ;; found the address
1639 (spam-kill-whole-line))
1640 ;; else, the address was not found
1642 (goto-char (point-max))
1648 (defun spam-filelist-build-cache (type)
1649 (let ((cache (if (eq type 'spam-use-blacklist)
1650 spam-blacklist-cache
1651 spam-whitelist-cache))
1653 (unless (gethash type spam-caches)
1655 (let ((address (pop cache)))
1656 (unless (zerop (length address)) ; 0 for a nil address too
1657 (setq address (regexp-quote address))
1658 ;; fix regexp-quote's treatment of user-intended regexes
1659 (while (string-match "\\\\\\*" address)
1660 (setq address (replace-match ".*" t t address))))
1661 (push address parsed-cache)))
1662 (puthash type parsed-cache spam-caches))))
1664 (defun spam-filelist-check-cache (type from)
1665 (when (stringp from)
1666 (spam-filelist-build-cache type)
1668 (dolist (address (gethash type spam-caches))
1669 (when (and address (string-match address from))
1674 ;;; returns t if the sender is in the whitelist, nil or
1675 ;;; spam-split-group otherwise
1676 (defun spam-check-whitelist ()
1677 ;; FIXME! Should it detect when file timestamps change?
1678 (let ((spam-split-group (if spam-split-symbolic-return
1681 (unless spam-whitelist-cache
1682 (setq spam-whitelist-cache (spam-parse-list spam-whitelist)))
1683 (if (spam-from-listed-p 'spam-use-whitelist)
1685 (if spam-use-whitelist-exclusive
1689 (defun spam-check-blacklist ()
1690 ;; FIXME! Should it detect when file timestamps change?
1691 (let ((spam-split-group (if spam-split-symbolic-return
1694 (unless spam-blacklist-cache
1695 (setq spam-blacklist-cache (spam-parse-list spam-blacklist)))
1696 (and (spam-from-listed-p 'spam-use-blacklist) spam-split-group)))
1698 (defun spam-parse-list (file)
1699 (when (file-readable-p file)
1700 (let (contents address)
1702 (insert-file-contents file)
1704 (setq address (buffer-substring (point) (point-at-eol)))
1706 ;; insert the e-mail address if detected, otherwise the raw data
1707 (unless (zerop (length address))
1708 (let ((pure-address (nth 1 (gnus-extract-address-components address))))
1709 (push (or pure-address address) contents)))))
1710 (nreverse contents))))
1712 (defun spam-from-listed-p (type)
1713 (let ((from (message-fetch-field "from"))
1715 (spam-filelist-check-cache type from)))
1717 (defun spam-filelist-register-routine (articles blacklist &optional unregister)
1718 (let ((de-symbol (if blacklist 'spam-use-whitelist 'spam-use-blacklist))
1719 (declassification (if blacklist 'ham 'spam))
1721 (if blacklist 'spam-enter-blacklist 'spam-enter-whitelist))
1723 (if blacklist 'spam-enter-whitelist 'spam-enter-blacklist))
1724 from addresses unregister-list)
1725 (dolist (article articles)
1726 (let ((from (spam-fetch-field-from-fast article))
1727 (id (spam-fetch-field-message-id-fast article))
1729 (when (stringp from)
1730 (dolist (ignore-regex spam-blacklist-ignored-regexes)
1731 (when (and (not sender-ignored)
1732 (stringp ignore-regex)
1733 (string-match ignore-regex from))
1734 (setq sender-ignored t)))
1735 ;; remember the messages we need to unregister, unless remove is set
1738 (spam-log-unregistration-needed-p
1739 id 'process declassification de-symbol))
1740 (push from unregister-list))
1741 (unless sender-ignored
1742 (push from addresses)))))
1745 (funcall enter-function addresses t) ; unregister all these addresses
1746 ;; else, register normally and unregister what we need to
1747 (funcall remove-function unregister-list t)
1748 (dolist (article unregister-list)
1749 (spam-log-undo-registration
1750 (spam-fetch-field-message-id-fast article)
1754 (funcall enter-function addresses nil))))
1756 (defun spam-blacklist-unregister-routine (articles)
1757 (spam-blacklist-register-routine articles t))
1759 (defun spam-blacklist-register-routine (articles &optional unregister)
1760 (spam-filelist-register-routine articles t unregister))
1762 (defun spam-whitelist-unregister-routine (articles)
1763 (spam-whitelist-register-routine articles t))
1765 (defun spam-whitelist-register-routine (articles &optional unregister)
1766 (spam-filelist-register-routine articles nil unregister))
1769 ;;;; Spam-report glue
1770 (defun spam-report-gmane-register-routine (articles)
1772 (apply 'spam-report-gmane articles)))
1776 (defun spam-check-bogofilter-headers (&optional score)
1777 (let ((header (message-fetch-field spam-bogofilter-header))
1778 (spam-split-group (if spam-split-symbolic-return
1781 (when header ; return nil when no header
1782 (if score ; scoring mode
1783 (if (string-match "spamicity=\\([0-9.]+\\)" header)
1784 (match-string 1 header)
1786 ;; spam detection mode
1787 (when (string-match spam-bogofilter-bogosity-positive-spam-header
1789 spam-split-group)))))
1791 ;; return something sensible if the score can't be determined
1792 (defun spam-bogofilter-score ()
1793 "Get the Bogofilter spamicity score"
1795 (save-window-excursion
1796 (gnus-summary-show-article t)
1797 (set-buffer gnus-article-buffer)
1798 (let ((score (or (spam-check-bogofilter-headers t)
1799 (spam-check-bogofilter t))))
1800 (message "Spamicity score %s" score)
1802 (gnus-summary-show-article)))
1804 (defun spam-check-bogofilter (&optional score)
1805 "Check the Bogofilter backend for the classification of this message"
1806 (let ((article-buffer-name (buffer-name))
1807 (db spam-bogofilter-database-directory)
1810 (let ((temp-buffer-name (buffer-name)))
1812 (set-buffer article-buffer-name)
1813 (apply 'call-process-region
1814 (point-min) (point-max)
1815 spam-bogofilter-path
1816 nil temp-buffer-name nil
1817 (if db `("-d" ,db "-v") `("-v"))))
1818 (setq return (spam-check-bogofilter-headers score))))
1821 (defun spam-bogofilter-register-with-bogofilter (articles
1823 &optional unregister)
1824 "Register an article, given as a string, as spam or non-spam."
1825 (dolist (article articles)
1826 (let ((article-string (spam-get-article-as-string article))
1827 (db spam-bogofilter-database-directory)
1828 (switch (if unregister
1830 spam-bogofilter-spam-strong-switch
1831 spam-bogofilter-ham-strong-switch)
1833 spam-bogofilter-spam-switch
1834 spam-bogofilter-ham-switch))))
1835 (when (stringp article-string)
1837 (insert article-string)
1839 (apply 'call-process-region
1840 (point-min) (point-max)
1841 spam-bogofilter-path
1843 (if db `("-d" ,db "-v") `("-v"))))))))
1845 (defun spam-bogofilter-register-spam-routine (articles &optional unregister)
1846 (spam-bogofilter-register-with-bogofilter articles t unregister))
1848 (defun spam-bogofilter-unregister-spam-routine (articles)
1849 (spam-bogofilter-register-spam-routine articles t))
1851 (defun spam-bogofilter-register-ham-routine (articles &optional unregister)
1852 (spam-bogofilter-register-with-bogofilter articles nil unregister))
1854 (defun spam-bogofilter-unregister-ham-routine (articles)
1855 (spam-bogofilter-register-ham-routine articles t))
1860 (defun spam-check-spamoracle ()
1861 "Run spamoracle on an article to determine whether it's spam."
1862 (let ((article-buffer-name (buffer-name))
1863 (spam-split-group (if spam-split-symbolic-return
1867 (let ((temp-buffer-name (buffer-name)))
1869 (set-buffer article-buffer-name)
1871 (apply 'call-process-region
1872 (point-min) (point-max)
1873 spam-spamoracle-binary
1874 nil temp-buffer-name nil
1875 (if spam-spamoracle-database
1876 `("-f" ,spam-spamoracle-database "mark")
1880 (set-buffer temp-buffer-name)
1881 (goto-char (point-min))
1882 (when (re-search-forward "^X-Spam: yes;" nil t)
1884 (error "Error running spamoracle" status))))))))
1886 (defun spam-spamoracle-learn (articles article-is-spam-p &optional unregister)
1887 "Run spamoracle in training mode."
1889 (let ((temp-buffer-name (buffer-name)))
1891 (goto-char (point-min))
1892 (dolist (article articles)
1893 (insert (spam-get-article-as-string article)))
1894 (let* ((arg (if (spam-xor unregister article-is-spam-p)
1898 (apply 'call-process-region
1899 (point-min) (point-max)
1900 spam-spamoracle-binary
1901 nil temp-buffer-name nil
1902 (if spam-spamoracle-database
1903 `("-f" ,spam-spamoracle-database
1906 (when (not (eq 0 status))
1907 (error "Error running spamoracle" status)))))))
1909 (defun spam-spamoracle-learn-ham (articles &optional unregister)
1910 (spam-spamoracle-learn articles nil unregister))
1912 (defun spam-spamoracle-unlearn-ham (articles &optional unregister)
1913 (spam-spamoracle-learn-ham articles t))
1915 (defun spam-spamoracle-learn-spam (articles &optional unregister)
1916 (spam-spamoracle-learn articles t unregister))
1918 (defun spam-spamoracle-unlearn-spam (articles &optional unregister)
1919 (spam-spamoracle-learn-spam articles t))
1925 (defun spam-initialize ()
1926 "Install the spam.el hooks and do other initialization"
1928 (setq spam-install-hooks t)
1929 ;; TODO: How do we redo this every time spam-face is customized?
1930 (push '((eq mark gnus-spam-mark) . spam-face)
1931 gnus-summary-highlight)
1932 ;; Add hooks for loading and saving the spam stats
1933 (add-hook 'gnus-save-newsrc-hook 'spam-maybe-spam-stat-save)
1934 (add-hook 'gnus-get-top-new-news-hook 'spam-maybe-spam-stat-load)
1935 (add-hook 'gnus-startup-hook 'spam-maybe-spam-stat-load)
1936 (add-hook 'gnus-summary-prepare-exit-hook 'spam-summary-prepare-exit)
1937 (add-hook 'gnus-summary-prepare-hook 'spam-summary-prepare)
1938 (add-hook 'gnus-get-new-news-hook 'spam-setup-widening)
1939 (add-hook 'gnus-summary-prepared-hook 'spam-find-spam))
1941 (defun spam-unload-hook ()
1942 "Uninstall the spam.el hooks"
1944 (remove-hook 'gnus-save-newsrc-hook 'spam-maybe-spam-stat-save)
1945 (remove-hook 'gnus-get-top-new-news-hook 'spam-maybe-spam-stat-load)
1946 (remove-hook 'gnus-startup-hook 'spam-maybe-spam-stat-load)
1947 (remove-hook 'gnus-summary-prepare-exit-hook 'spam-summary-prepare-exit)
1948 (remove-hook 'gnus-summary-prepare-hook 'spam-summary-prepare)
1949 (remove-hook 'gnus-get-new-news-hook 'spam-setup-widening)
1950 (remove-hook 'gnus-summary-prepare-hook 'spam-find-spam))
1952 (when spam-install-hooks
1957 ;;; spam.el ends here