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))
52 ;; autoload executable-find
54 ;; executable-find is not autoloaded in Emacs 20
55 (autoload 'executable-find "executable"))
59 (autoload 'query-dig "dig"))
61 ;; autoload spam-report
63 (autoload 'spam-report-gmane "spam-report"))
65 ;; autoload gnus-registry
67 (autoload 'gnus-registry-group-count "gnus-registry")
68 (autoload 'gnus-registry-add-group "gnus-registry")
69 (autoload 'gnus-registry-store-extra-entry "gnus-registry")
70 (autoload 'gnus-registry-fetch-extra "gnus-registry"))
74 (autoload 'query-dns "dns"))
79 "Spam configuration.")
81 (defcustom spam-directory "~/News/spam/"
82 "Directory for spam whitelists and blacklists."
86 (defcustom spam-move-spam-nonspam-groups-only t
87 "Whether spam should be moved in non-spam groups only.
88 When t, only ham and unclassified groups will have their spam moved
89 to the spam-process-destination. When nil, spam will also be moved from
94 (defcustom spam-process-ham-in-nonham-groups nil
95 "Whether ham should be processed in non-ham groups."
99 (defcustom spam-log-to-registry nil
100 "Whether spam/ham processing should be logged in the registry."
104 (defcustom spam-split-symbolic-return nil
105 "Whether `spam-split' should work with symbols or group names."
109 (defcustom spam-split-symbolic-return-positive nil
110 "Whether `spam-split' should ALWAYS work with symbols or group names.
111 Do not set this if you use `spam-split' in a fancy split
116 (defcustom spam-process-ham-in-spam-groups nil
117 "Whether ham should be processed in spam groups."
121 (defcustom spam-mark-only-unseen-as-spam t
122 "Whether only unseen articles should be marked as spam in spam groups.
123 When nil, all unread articles in a spam group are marked as
124 spam. Set this if you want to leave an article unread in a spam group
125 without losing it to the automatic spam-marking process."
129 (defcustom spam-mark-ham-unread-before-move-from-spam-group nil
130 "Whether ham should be marked unread before it's moved.
131 The article is moved out of a spam group according to ham-process-destination.
132 This variable is an official entry in the international Longest Variable Name
137 (defcustom spam-disable-spam-split-during-ham-respool nil
138 "Whether `spam-split' should be ignored while resplitting ham in a process
139 destination. This is useful to prevent ham from ending up in the same spam
140 group after the resplit. Don't set this to t if you have spam-split as the
141 last rule in your split configuration."
145 (defcustom spam-autodetect-recheck-messages nil
146 "Should spam.el recheck all meessages when autodetecting?
147 Normally this is nil, so only unseen messages will be checked."
151 (defcustom spam-whitelist (expand-file-name "whitelist" spam-directory)
152 "The location of the whitelist.
153 The file format is one regular expression per line.
154 The regular expression is matched against the address."
158 (defcustom spam-blacklist (expand-file-name "blacklist" spam-directory)
159 "The location of the blacklist.
160 The file format is one regular expression per line.
161 The regular expression is matched against the address."
165 (defcustom spam-use-dig t
166 "Whether `query-dig' should be used instead of `query-dns'."
170 (defcustom spam-use-blacklist nil
171 "Whether the blacklist should be used by `spam-split'."
175 (defcustom spam-blacklist-ignored-regexes nil
176 "Regular expressions that the blacklist should ignore."
177 :type '(repeat (regexp :tag "Regular expression to ignore when blacklisting"))
180 (defcustom spam-use-whitelist nil
181 "Whether the whitelist should be used by `spam-split'."
185 (defcustom spam-use-whitelist-exclusive nil
186 "Whether whitelist-exclusive should be used by `spam-split'.
187 Exclusive whitelisting means that all messages from senders not in the whitelist
188 are considered spam."
192 (defcustom spam-use-blackholes nil
193 "Whether blackholes should be used by `spam-split'."
197 (defcustom spam-use-hashcash nil
198 "Whether hashcash payments should be detected by `spam-split'."
202 (defcustom spam-use-regex-headers nil
203 "Whether a header regular expression match should be used by `spam-split'.
204 Also see the variables `spam-regex-headers-spam' and `spam-regex-headers-ham'."
208 (defcustom spam-use-regex-body nil
209 "Whether a body regular expression match should be used by `spam-split'.
210 Also see the variables `spam-regex-body-spam' and `spam-regex-body-ham'."
214 (defcustom spam-use-bogofilter-headers nil
215 "Whether bogofilter headers should be used by `spam-split'.
216 Enable this if you pre-process messages with Bogofilter BEFORE Gnus sees them."
220 (defcustom spam-use-bogofilter nil
221 "Whether bogofilter should be invoked by `spam-split'.
222 Enable this if you want Gnus to invoke Bogofilter on new messages."
226 (defcustom spam-use-BBDB nil
227 "Whether BBDB should be used by `spam-split'."
231 (defcustom spam-use-BBDB-exclusive nil
232 "Whether BBDB-exclusive should be used by `spam-split'.
233 Exclusive BBDB means that all messages from senders not in the BBDB are
238 (defcustom spam-use-ifile nil
239 "Whether ifile should be used by `spam-split'."
243 (defcustom spam-use-stat nil
244 "Whether `spam-stat' should be used by `spam-split'."
248 (defcustom spam-use-spamoracle nil
249 "Whether spamoracle should be used by `spam-split'."
253 (defcustom spam-install-hooks (or
257 spam-use-whitelist-exclusive
260 spam-use-regex-headers
262 spam-use-bogofilter-headers
265 spam-use-BBDB-exclusive
269 "Whether the spam hooks should be installed.
270 Default to t if one of the spam-use-* variables is set."
274 (defcustom spam-split-group "spam"
275 "Group name where incoming spam should be put by `spam-split'."
279 ;;; TODO: deprecate this variable, it's confusing since it's a list of strings,
280 ;;; not regular expressions
281 (defcustom spam-junk-mailgroups (cons
283 '("mail.junk" "poste.pourriel"))
284 "Mailgroups with spam contents.
285 All unmarked article in such group receive the spam mark on group entry."
286 :type '(repeat (string :tag "Group"))
289 (defcustom spam-blackhole-servers '("bl.spamcop.net" "relays.ordb.org"
290 "dev.null.dk" "relays.visi.com")
291 "List of blackhole servers."
292 :type '(repeat (string :tag "Server"))
295 (defcustom spam-blackhole-good-server-regex nil
296 "String matching IP addresses that should not be checked in the blackholes."
297 :type '(radio (const nil)
298 (regexp :format "%t: %v\n" :size 0))
301 (defcustom spam-face 'gnus-splash-face
302 "Face for spam-marked articles."
306 (defcustom spam-regex-headers-spam '("^X-Spam-Flag: YES")
307 "Regular expression for positive header spam matches."
308 :type '(repeat (regexp :tag "Regular expression to match spam header"))
311 (defcustom spam-regex-headers-ham '("^X-Spam-Flag: NO")
312 "Regular expression for positive header ham matches."
313 :type '(repeat (regexp :tag "Regular expression to match ham header"))
316 (defcustom spam-regex-body-spam '()
317 "Regular expression for positive body spam matches."
318 :type '(repeat (regexp :tag "Regular expression to match spam body"))
321 (defcustom spam-regex-body-ham '()
322 "Regular expression for positive body ham matches."
323 :type '(repeat (regexp :tag "Regular expression to match ham body"))
326 (defgroup spam-ifile nil
327 "Spam ifile configuration."
330 (defcustom spam-ifile-path (executable-find "ifile")
331 "File path of the ifile executable program."
332 :type '(choice (file :tag "Location of ifile")
333 (const :tag "ifile is not installed"))
336 (defcustom spam-ifile-database-path nil
337 "File path of the ifile database."
338 :type '(choice (file :tag "Location of the ifile database")
339 (const :tag "Use the default"))
342 (defcustom spam-ifile-spam-category "spam"
343 "Name of the spam ifile category."
347 (defcustom spam-ifile-ham-category nil
348 "Name of the ham ifile category.
349 If nil, the current group name will be used."
350 :type '(choice (string :tag "Use a fixed category")
351 (const :tag "Use the current group name"))
354 (defcustom spam-ifile-all-categories nil
355 "Whether the ifile check will return all categories, or just spam.
356 Set this to t if you want to use the `spam-split' invocation of ifile as
357 your main source of newsgroup names."
361 (defgroup spam-bogofilter nil
362 "Spam bogofilter configuration."
365 (defcustom spam-bogofilter-path (executable-find "bogofilter")
366 "File path of the Bogofilter executable program."
367 :type '(choice (file :tag "Location of bogofilter")
368 (const :tag "Bogofilter is not installed"))
369 :group 'spam-bogofilter)
371 (defcustom spam-bogofilter-header "X-Bogosity"
372 "The header that Bogofilter inserts in messages."
374 :group 'spam-bogofilter)
376 (defcustom spam-bogofilter-spam-switch "-s"
377 "The switch that Bogofilter uses to register spam messages."
379 :group 'spam-bogofilter)
381 (defcustom spam-bogofilter-ham-switch "-n"
382 "The switch that Bogofilter uses to register ham messages."
384 :group 'spam-bogofilter)
386 (defcustom spam-bogofilter-spam-strong-switch "-S"
387 "The switch that Bogofilter uses to unregister ham messages."
389 :group 'spam-bogofilter)
391 (defcustom spam-bogofilter-ham-strong-switch "-N"
392 "The switch that Bogofilter uses to unregister spam messages."
394 :group 'spam-bogofilter)
396 (defcustom spam-bogofilter-bogosity-positive-spam-header "^\\(Yes\\|Spam\\)"
397 "The regex on `spam-bogofilter-header' for positive spam identification."
399 :group 'spam-bogofilter)
401 (defcustom spam-bogofilter-database-directory nil
402 "Directory path of the Bogofilter databases."
403 :type '(choice (directory
404 :tag "Location of the Bogofilter database directory")
405 (const :tag "Use the default"))
406 :group 'spam-bogofilter)
408 (defgroup spam-spamoracle nil
409 "Spam spamoracle configuration."
412 (defcustom spam-spamoracle-database nil
413 "Location of spamoracle database file. When nil, use the default
414 spamoracle database."
415 :type '(choice (directory :tag "Location of spamoracle database file.")
416 (const :tag "Use the default"))
417 :group 'spam-spamoracle)
419 (defcustom spam-spamoracle-binary (executable-find "spamoracle")
420 "Location of the spamoracle binary."
421 :type '(choice (directory :tag "Location of the spamoracle binary")
422 (const :tag "Use the default"))
423 :group 'spam-spamoracle)
425 ;;; Key bindings for spam control.
427 (gnus-define-keys gnus-summary-mode-map
428 "St" spam-bogofilter-score
429 "Sx" gnus-summary-mark-as-spam
430 "Mst" spam-bogofilter-score
431 "Msx" gnus-summary-mark-as-spam
432 "\M-d" gnus-summary-mark-as-spam)
434 (defvar spam-old-ham-articles nil
435 "List of old ham articles, generated when a group is entered.")
437 (defvar spam-old-spam-articles nil
438 "List of old spam articles, generated when a group is entered.")
440 (defvar spam-split-disabled nil
441 "If non-nil, `spam-split' is disabled, and always returns nil.")
443 (defvar spam-split-last-successful-check nil
444 "`spam-split' will set this to nil or a spam-use-XYZ check if it
447 ;; convenience functions
448 (defun spam-xor (a b)
449 "Logical exclusive `or'."
450 (and (or a b) (not (and a b))))
452 (defun spam-group-ham-mark-p (group mark &optional spam)
453 (when (stringp group)
454 (let* ((marks (spam-group-ham-marks group spam))
455 (marks (if (symbolp mark)
457 (mapcar 'symbol-value marks))))
460 (defun spam-group-spam-mark-p (group mark)
461 (spam-group-ham-mark-p group mark t))
463 (defun spam-group-ham-marks (group &optional spam)
464 (when (stringp group)
465 (let* ((marks (if spam
466 (gnus-parameter-spam-marks group)
467 (gnus-parameter-ham-marks group)))
469 (marks (if (listp (car marks)) (car marks) marks)))
472 (defun spam-group-spam-marks (group)
473 (spam-group-ham-marks group t))
475 (defun spam-group-spam-contents-p (group)
477 (or (member group spam-junk-mailgroups)
478 (memq 'gnus-group-spam-classification-spam
479 (gnus-parameter-spam-contents group)))
482 (defun spam-group-ham-contents-p (group)
484 (memq 'gnus-group-spam-classification-ham
485 (gnus-parameter-spam-contents group))
488 (defvar spam-list-of-processors
489 '((gnus-group-spam-exit-processor-report-gmane spam spam-use-gmane)
490 (gnus-group-spam-exit-processor-bogofilter spam spam-use-bogofilter)
491 (gnus-group-spam-exit-processor-blacklist spam spam-use-blacklist)
492 (gnus-group-spam-exit-processor-ifile spam spam-use-ifile)
493 (gnus-group-spam-exit-processor-stat spam spam-use-stat)
494 (gnus-group-spam-exit-processor-spamoracle spam spam-use-spamoracle)
495 (gnus-group-ham-exit-processor-ifile ham spam-use-ifile)
496 (gnus-group-ham-exit-processor-bogofilter ham spam-use-bogofilter)
497 (gnus-group-ham-exit-processor-stat ham spam-use-stat)
498 (gnus-group-ham-exit-processor-whitelist ham spam-use-whitelist)
499 (gnus-group-ham-exit-processor-BBDB ham spam-use-BBDB)
500 (gnus-group-ham-exit-processor-copy ham spam-use-ham-copy)
501 (gnus-group-ham-exit-processor-spamoracle ham spam-use-spamoracle))
502 "The spam-list-of-processors list contains pairs associating a
503 ham/spam exit processor variable with a classification and a
504 spam-use-* variable.")
506 (defun spam-group-processor-p (group processor)
507 (if (and (stringp group)
509 (or (member processor (nth 0 (gnus-parameter-spam-process group)))
510 (spam-group-processor-multiple-p
512 (cdr-safe (assoc processor spam-list-of-processors))))
515 (defun spam-group-processor-multiple-p (group processor-info)
516 (let* ((classification (nth 0 processor-info))
517 (check (nth 1 processor-info))
518 (parameters (nth 0 (gnus-parameter-spam-process group)))
520 (dolist (parameter parameters)
521 (when (and (null found)
523 (eq classification (nth 0 parameter))
524 (eq check (nth 1 parameter)))
528 (defun spam-group-spam-processor-report-gmane-p (group)
529 (spam-group-processor-p group 'gnus-group-spam-exit-processor-report-gmane))
531 (defun spam-group-spam-processor-bogofilter-p (group)
532 (spam-group-processor-p group 'gnus-group-spam-exit-processor-bogofilter))
534 (defun spam-group-spam-processor-blacklist-p (group)
535 (spam-group-processor-p group 'gnus-group-spam-exit-processor-blacklist))
537 (defun spam-group-spam-processor-ifile-p (group)
538 (spam-group-processor-p group 'gnus-group-spam-exit-processor-ifile))
540 (defun spam-group-ham-processor-ifile-p (group)
541 (spam-group-processor-p group 'gnus-group-ham-exit-processor-ifile))
543 (defun spam-group-spam-processor-spamoracle-p (group)
544 (spam-group-processor-p group 'gnus-group-spam-exit-processor-spamoracle))
546 (defun spam-group-ham-processor-bogofilter-p (group)
547 (spam-group-processor-p group 'gnus-group-ham-exit-processor-bogofilter))
549 (defun spam-group-spam-processor-stat-p (group)
550 (spam-group-processor-p group 'gnus-group-spam-exit-processor-stat))
552 (defun spam-group-ham-processor-stat-p (group)
553 (spam-group-processor-p group 'gnus-group-ham-exit-processor-stat))
555 (defun spam-group-ham-processor-whitelist-p (group)
556 (spam-group-processor-p group 'gnus-group-ham-exit-processor-whitelist))
558 (defun spam-group-ham-processor-BBDB-p (group)
559 (spam-group-processor-p group 'gnus-group-ham-exit-processor-BBDB))
561 (defun spam-group-ham-processor-copy-p (group)
562 (spam-group-processor-p group 'gnus-group-ham-exit-processor-copy))
564 (defun spam-group-ham-processor-spamoracle-p (group)
565 (spam-group-processor-p group 'gnus-group-ham-exit-processor-spamoracle))
567 ;;; Summary entry and exit processing.
569 (defun spam-summary-prepare ()
570 (setq spam-old-ham-articles
571 (spam-list-articles gnus-newsgroup-articles 'ham))
572 (setq spam-old-spam-articles
573 (spam-list-articles gnus-newsgroup-articles 'spam))
574 (spam-mark-junk-as-spam-routine))
576 ;; The spam processors are invoked for any group, spam or ham or neither
577 (defun spam-summary-prepare-exit ()
578 (unless gnus-group-is-exiting-without-update-p
579 (gnus-message 6 "Exiting summary buffer and applying spam rules")
581 ;; first of all, unregister any articles that are no longer ham or spam
582 ;; we have to iterate over the processors, or else we'll be too slow
583 (dolist (classification '(spam ham))
584 (let* ((old-articles (if (eq classification 'spam)
585 spam-old-spam-articles
586 spam-old-ham-articles))
587 (new-articles (spam-list-articles
588 gnus-newsgroup-articles
590 (changed-articles (gnus-set-difference old-articles new-articles)))
591 ;; now that we have the changed articles, we go through the processors
592 (dolist (processor-param spam-list-of-processors)
593 (let ((processor (nth 0 processor-param))
594 (processor-classification (nth 1 processor-param))
595 (check (nth 2 processor-param))
597 (dolist (article changed-articles)
598 (let ((id (spam-fetch-field-message-id-fast article)))
599 (when (spam-log-unregistration-needed-p
600 id 'process classification check)
601 (push article unregister-list))))
602 ;; call spam-register-routine with specific articles to unregister,
603 ;; when there are articles to unregister and the check is enabled
604 (when (and unregister-list (symbol-value check))
605 (spam-register-routine classification check t unregister-list))))))
607 ;; find all the spam processors applicable to this group
608 (dolist (processor-param spam-list-of-processors)
609 (let ((processor (nth 0 processor-param))
610 (classification (nth 1 processor-param))
611 (check (nth 2 processor-param)))
612 (when (and (eq 'spam classification)
613 (spam-group-processor-p gnus-newsgroup-name processor))
614 (spam-register-routine classification check))))
616 (if spam-move-spam-nonspam-groups-only
617 (when (not (spam-group-spam-contents-p gnus-newsgroup-name))
618 (spam-mark-spam-as-expired-and-move-routine
619 (gnus-parameter-spam-process-destination gnus-newsgroup-name)))
620 (gnus-message 5 "Marking spam as expired and moving it to %s"
622 (spam-mark-spam-as-expired-and-move-routine
623 (gnus-parameter-spam-process-destination gnus-newsgroup-name)))
625 ;; now we redo spam-mark-spam-as-expired-and-move-routine to only
626 ;; expire spam, in case the above did not expire them
627 (gnus-message 5 "Marking spam as expired without moving it")
628 (spam-mark-spam-as-expired-and-move-routine nil)
630 (when (or (spam-group-ham-contents-p gnus-newsgroup-name)
631 (and (spam-group-spam-contents-p gnus-newsgroup-name)
632 spam-process-ham-in-spam-groups)
633 spam-process-ham-in-nonham-groups)
634 ;; find all the ham processors applicable to this group
635 (dolist (processor-param spam-list-of-processors)
636 (let ((processor (nth 0 processor-param))
637 (classification (nth 1 processor-param))
638 (check (nth 2 processor-param)))
639 (when (and (eq 'ham classification)
640 (spam-group-processor-p gnus-newsgroup-name processor))
641 (spam-register-routine classification check)))))
643 (when (spam-group-ham-processor-copy-p gnus-newsgroup-name)
644 (gnus-message 5 "Copying ham")
645 (spam-ham-copy-routine
646 (gnus-parameter-ham-process-destination gnus-newsgroup-name)))
648 ;; now move all ham articles out of spam groups
649 (when (spam-group-spam-contents-p gnus-newsgroup-name)
650 (gnus-message 5 "Moving ham messages from spam group")
651 (spam-ham-move-routine
652 (gnus-parameter-ham-process-destination gnus-newsgroup-name))))
654 (setq spam-old-ham-articles nil)
655 (setq spam-old-spam-articles nil))
657 (defun spam-mark-junk-as-spam-routine ()
658 ;; check the global list of group names spam-junk-mailgroups and the
660 (when (spam-group-spam-contents-p gnus-newsgroup-name)
661 (gnus-message 5 "Marking %s articles as spam"
662 (if spam-mark-only-unseen-as-spam
665 (let ((articles (if spam-mark-only-unseen-as-spam
666 gnus-newsgroup-unseen
667 gnus-newsgroup-unreads)))
668 (dolist (article articles)
669 (gnus-summary-mark-article article gnus-spam-mark)))))
671 (defun spam-mark-spam-as-expired-and-move-routine (&rest groups)
672 (if (and (car-safe groups) (listp (car-safe groups)))
673 (apply 'spam-mark-spam-as-expired-and-move-routine (car groups))
674 (gnus-summary-kill-process-mark)
675 (let ((articles gnus-newsgroup-articles)
676 (backend-supports-deletions
677 (gnus-check-backend-function
678 'request-move-article gnus-newsgroup-name))
679 article tomove deletep)
680 (dolist (article articles)
681 (when (eq (gnus-summary-article-mark article) gnus-spam-mark)
682 (gnus-summary-mark-article article gnus-expirable-mark)
683 (push article tomove)))
685 ;; now do the actual copies
686 (dolist (group groups)
689 (dolist (article tomove)
690 (gnus-summary-set-process-mark article))
692 (if (or (not backend-supports-deletions)
693 (> (length groups) 1))
695 (gnus-summary-copy-article nil group)
697 (gnus-summary-move-article nil group)))))
699 ;; now delete the articles, if there was a copy done, and the
701 (when (and deletep backend-supports-deletions)
702 (dolist (article tomove)
703 (gnus-summary-set-process-mark article))
705 (let ((gnus-novice-user nil)) ; don't ask me if I'm sure
706 (gnus-summary-delete-article nil))))
708 (gnus-summary-yank-process-mark))))
710 (defun spam-ham-copy-or-move-routine (copy groups)
711 (gnus-summary-kill-process-mark)
712 (let ((todo (spam-list-articles gnus-newsgroup-articles 'ham))
713 (backend-supports-deletions
714 (gnus-check-backend-function
715 'request-move-article gnus-newsgroup-name))
716 (respool-method (gnus-find-method-for-group gnus-newsgroup-name))
717 article mark todo deletep respool)
719 (when (member 'respool groups)
720 (setq respool t) ; boolean for later
721 (setq groups '("fake"))) ; when respooling, groups are dynamic so fake it
723 ;; now do the actual move
724 (dolist (group groups)
725 (when (and todo (stringp group))
726 (dolist (article todo)
727 (when spam-mark-ham-unread-before-move-from-spam-group
728 (gnus-summary-mark-article article gnus-unread-mark))
729 (gnus-summary-set-process-mark article))
731 (if respool ; respooling is with a "fake" group
732 (let ((spam-split-disabled
733 (or spam-split-disabled
734 spam-disable-spam-split-during-ham-respool)))
735 (gnus-summary-respool-article nil respool-method))
736 (if (or (not backend-supports-deletions) ; else, we are not respooling
737 (> (length groups) 1))
738 (progn ; if copying, copy and set deletep
739 (gnus-summary-copy-article nil group)
741 (gnus-summary-move-article nil group))))) ; else move articles
743 ;; now delete the articles, unless a) copy is t, and there was a copy done
744 ;; b) a move was done to a single group
745 ;; c) backend-supports-deletions is nil
747 (when (and deletep backend-supports-deletions)
748 (dolist (article todo)
749 (gnus-summary-set-process-mark article))
751 (let ((gnus-novice-user nil)) ; don't ask me if I'm sure
752 (gnus-summary-delete-article nil))))))
754 (gnus-summary-yank-process-mark))
756 (defun spam-ham-copy-routine (&rest groups)
757 (if (and (car-safe groups) (listp (car-safe groups)))
758 (apply 'spam-ham-copy-routine (car groups))
759 (spam-ham-copy-or-move-routine t groups)))
761 (defun spam-ham-move-routine (&rest groups)
762 (if (and (car-safe groups) (listp (car-safe groups)))
763 (apply 'spam-ham-move-routine (car groups))
764 (spam-ham-copy-or-move-routine nil groups)))
766 (defun spam-get-article-as-string (article)
767 (let ((article-buffer (spam-get-article-as-buffer article))
770 (save-window-excursion
771 (set-buffer article-buffer)
772 (setq article-string (buffer-string))))
775 (defun spam-get-article-as-buffer (article)
776 (let ((article-buffer))
777 (when (numberp article)
778 (save-window-excursion
779 (gnus-summary-goto-subject article)
780 (gnus-summary-show-article t)
781 (setq article-buffer (get-buffer gnus-article-buffer))))
785 ;; (defun spam-get-article-as-filename (article)
786 ;; (let ((article-filename))
787 ;; (when (numberp article)
788 ;; (nnml-possibly-change-directory
789 ;; (gnus-group-real-name gnus-newsgroup-name))
790 ;; (setq article-filename (expand-file-name
791 ;; (int-to-string article) nnml-current-directory)))
792 ;; (if (file-exists-p article-filename)
796 (defun spam-fetch-field-from-fast (article)
797 "Fetch the `from' field quickly, using the internal gnus-data-list function"
798 (if (and (numberp article)
799 (assoc article (gnus-data-list nil)))
801 (gnus-data-header (assoc article (gnus-data-list nil))))
804 (defun spam-fetch-field-subject-fast (article)
805 "Fetch the `subject' field quickly, using the internal
806 gnus-data-list function"
807 (if (and (numberp article)
808 (assoc article (gnus-data-list nil)))
810 (gnus-data-header (assoc article (gnus-data-list nil))))
813 (defun spam-fetch-field-message-id-fast (article)
814 "Fetch the `Message-ID' field quickly, using the internal
815 gnus-data-list function"
816 (if (and (numberp article)
817 (assoc article (gnus-data-list nil)))
818 (mail-header-message-id
819 (gnus-data-header (assoc article (gnus-data-list nil))))
823 ;;;; Spam determination.
825 (defvar spam-list-of-checks
826 '((spam-use-blacklist . spam-check-blacklist)
827 (spam-use-regex-headers . spam-check-regex-headers)
828 (spam-use-regex-body . spam-check-regex-body)
829 (spam-use-whitelist . spam-check-whitelist)
830 (spam-use-BBDB . spam-check-BBDB)
831 (spam-use-ifile . spam-check-ifile)
832 (spam-use-spamoracle . spam-check-spamoracle)
833 (spam-use-stat . spam-check-stat)
834 (spam-use-blackholes . spam-check-blackholes)
835 (spam-use-hashcash . spam-check-hashcash)
836 (spam-use-bogofilter-headers . spam-check-bogofilter-headers)
837 (spam-use-bogofilter . spam-check-bogofilter))
838 "The spam-list-of-checks list contains pairs associating a
839 parameter variable with a spam checking function. If the
840 parameter variable is true, then the checking function is called,
841 and its value decides what happens. Each individual check may
842 return nil, t, or a mailgroup name. The value nil means that the
843 check does not yield a decision, and so, that further checks are
844 needed. The value t means that the message is definitely not
845 spam, and that further spam checks should be inhibited.
846 Otherwise, a mailgroup name or the symbol 'spam (depending on
847 spam-split-symbolic-return) is returned where the mail should go,
848 and further checks are also inhibited. The usual mailgroup name
849 is the value of `spam-split-group', meaning that the message is
852 (defvar spam-list-of-statistical-checks
858 "The spam-list-of-statistical-checks list contains all the mail
859 splitters that need to have the full message body available.")
861 ;;;TODO: modify to invoke self with each check if invoked without specifics
862 (defun spam-split (&rest specific-checks)
863 "Split this message into the `spam' group if it is spam.
864 This function can be used as an entry in the variable `nnmail-split-fancy',
865 for example like this: (: spam-split). It can take checks as
866 parameters. A string as a parameter will set the
867 spam-split-group to that string.
869 See the Info node `(gnus)Fancy Mail Splitting' for more details."
871 (setq spam-split-last-successful-check nil)
872 (unless spam-split-disabled
873 (let ((spam-split-group-choice spam-split-group))
874 (dolist (check specific-checks)
875 (when (stringp check)
876 (setq spam-split-group-choice check)
877 (setq specific-checks (delq check specific-checks))))
879 (let ((spam-split-group spam-split-group-choice))
882 (dolist (check spam-list-of-statistical-checks)
883 (when (and (symbolp check) (symbol-value check))
885 (gnus-message 8 "spam-split: widening the buffer (%s requires it)"
888 ;; (progn (widen) (debug (buffer-string)))
889 (let ((list-of-checks spam-list-of-checks)
891 (while (and list-of-checks (not decision))
892 (let ((pair (pop list-of-checks)))
893 (when (and (symbol-value (car pair))
894 (or (null specific-checks)
895 (memq (car pair) specific-checks)))
896 (gnus-message 5 "spam-split: calling the %s function"
897 (symbol-name (cdr pair)))
898 (setq decision (funcall (cdr pair)))
899 ;; if we got a decision at all, save the current check
901 (setq spam-split-last-successful-check (car pair)))
903 (when (eq decision 'spam)
904 (if spam-split-symbolic-return
905 (setq decision spam-split-group)
908 (format "spam-split got %s but %s is nil"
909 (symbol-name decision)
910 (symbol-name spam-split-symbolic-return))))))))
912 (if spam-split-symbolic-return-positive 'ham nil)
915 (defun spam-find-spam ()
916 "This function will detect spam in the current newsgroup using spam-split."
919 (let* ((group gnus-newsgroup-name)
920 (autodetect (gnus-parameter-spam-autodetect group))
921 (methods (gnus-parameter-spam-autodetect-methods group))
922 (first-method (nth 0 methods)))
923 (when (and autodetect
924 (not (equal first-method 'none)))
927 (let ((id (spam-fetch-field-message-id-fast article))
928 (subject (spam-fetch-field-subject-fast article))
929 (sender (spam-fetch-field-from-fast article)))
930 (unless (and spam-log-to-registry
931 (spam-log-registered-p id 'incoming))
932 (let* ((spam-split-symbolic-return t)
933 (spam-split-symbolic-return-positive t)
936 (gnus-request-article-this-buffer
939 (if (or (null first-method)
940 (equal first-method 'default))
942 (apply 'spam-split methods)))))
943 (if (equal split-return 'spam)
944 (gnus-summary-mark-article article gnus-spam-mark))
946 (when (and split-return spam-log-to-registry)
947 (when (zerop (gnus-registry-group-count id))
948 (gnus-registry-add-group
949 id group subject sender))
951 (spam-log-processing-to-registry
955 spam-split-last-successful-check
957 (if spam-autodetect-recheck-messages
958 gnus-newsgroup-articles
959 gnus-newsgroup-unseen)))))
961 (defvar spam-registration-functions
962 ;; first the ham register, second the spam register function
963 ;; third the ham unregister, fourth the spam unregister function
964 '((spam-use-blacklist nil
965 spam-blacklist-register-routine
967 spam-blacklist-unregister-routine)
968 (spam-use-whitelist spam-whitelist-register-routine
970 spam-whitelist-unregister-routine
972 (spam-use-BBDB spam-BBDB-register-routine
974 spam-BBDB-unregister-routine
976 (spam-use-ifile spam-ifile-register-ham-routine
977 spam-ifile-register-spam-routine
978 spam-ifile-unregister-ham-routine
979 spam-ifile-unregister-spam-routine)
980 (spam-use-spamoracle spam-spamoracle-learn-ham
981 spam-spamoracle-learn-spam
982 spam-spamoracle-unlearn-ham
983 spam-spamoracle-unlearn-spam)
984 (spam-use-stat spam-stat-register-ham-routine
985 spam-stat-register-spam-routine
986 spam-stat-unregister-ham-routine
987 spam-stat-unregister-spam-routine)
988 ;; note that spam-use-gmane is not a legitimate check
990 spam-report-gmane-register-routine
991 ;; does Gmane support unregistration?
994 (spam-use-bogofilter spam-bogofilter-register-ham-routine
995 spam-bogofilter-register-spam-routine
996 spam-bogofilter-unregister-ham-routine
997 spam-bogofilter-unregister-spam-routine))
998 "The spam-registration-functions list contains pairs
999 associating a parameter variable with the ham and spam
1000 registration functions, and the ham and spam unregistration
1003 (defun spam-classification-valid-p (classification)
1004 (or (eq classification 'spam)
1005 (eq classification 'ham)))
1007 (defun spam-process-type-valid-p (process-type)
1008 (or (eq process-type 'incoming)
1009 (eq process-type 'process)))
1011 (defun spam-registration-check-valid-p (check)
1012 (assoc check spam-registration-functions))
1014 (defun spam-unregistration-check-valid-p (check)
1015 (assoc check spam-registration-functions))
1017 (defun spam-registration-function (classification check)
1018 (let ((flist (cdr-safe (assoc check spam-registration-functions))))
1019 (if (eq classification 'spam)
1023 (defun spam-unregistration-function (classification check)
1024 (let ((flist (cdr-safe (assoc check spam-registration-functions))))
1025 (if (eq classification 'spam)
1029 (defun spam-list-articles (articles classification)
1030 (let ((mark-check (if (eq classification 'spam)
1031 'spam-group-spam-mark-p
1032 'spam-group-ham-mark-p))
1033 list mark-cache-yes mark-cache-no)
1034 (dolist (article articles)
1035 (let ((mark (gnus-summary-article-mark article)))
1036 (unless (memq mark mark-cache-no)
1037 (if (memq mark mark-cache-yes)
1039 ;; else, we have to actually check the mark
1040 (if (funcall mark-check
1045 (push mark mark-cache-yes))
1046 (push mark mark-cache-no))))))
1049 (defun spam-register-routine (classification
1051 &optional unregister
1053 (when (and (spam-classification-valid-p classification)
1054 (spam-registration-check-valid-p check))
1055 (let* ((register-function
1056 (spam-registration-function classification check))
1057 (unregister-function
1058 (spam-unregistration-function classification check))
1059 (run-function (if unregister
1062 (log-function (if unregister
1063 'spam-log-undo-registration
1064 'spam-log-processing-to-registry))
1068 ;; make list of articles, using specific-articles if given
1069 (setq articles (or specific-articles
1071 gnus-newsgroup-articles
1074 (gnus-message 5 "%s %d %s articles with classification %s, check %s"
1075 (if unregister "Unregistering" "Registering")
1077 (if specific-articles "specific" "")
1078 (symbol-name classification)
1079 (symbol-name check))
1080 (funcall run-function articles)
1081 ;; now log all the registrations (or undo them, depending on unregister)
1082 (dolist (article articles)
1083 (funcall log-function
1084 (spam-fetch-field-message-id-fast article)
1088 gnus-newsgroup-name))))))
1090 ;;; log a ham- or spam-processor invocation to the registry
1091 (defun spam-log-processing-to-registry (id type classification check group)
1092 (when spam-log-to-registry
1093 (if (and (stringp id)
1095 (spam-process-type-valid-p type)
1096 (spam-classification-valid-p classification)
1097 (spam-registration-check-valid-p check))
1098 (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type)))
1099 (cell (list classification check group)))
1100 (push cell cell-list)
1101 (gnus-registry-store-extra-entry
1106 (gnus-message 5 (format "%s called with bad ID, type, classification, check, or group"
1107 "spam-log-processing-to-registry")))))
1109 ;;; check if a ham- or spam-processor registration has been done
1110 (defun spam-log-registered-p (id type)
1111 (when spam-log-to-registry
1112 (if (and (stringp id)
1113 (spam-process-type-valid-p type))
1114 (cdr-safe (gnus-registry-fetch-extra id type))
1116 (gnus-message 5 (format "%s called with bad ID, type, classification, or check"
1117 "spam-log-registered-p"))
1120 ;;; check if a ham- or spam-processor registration needs to be undone
1121 (defun spam-log-unregistration-needed-p (id type classification check)
1122 (when spam-log-to-registry
1123 (if (and (stringp id)
1124 (spam-process-type-valid-p type)
1125 (spam-classification-valid-p classification)
1126 (spam-registration-check-valid-p check))
1127 (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type)))
1129 (dolist (cell cell-list)
1131 (when (and (eq classification (nth 0 cell))
1132 (eq check (nth 1 cell)))
1136 (gnus-message 5 (format "%s called with bad ID, type, classification, or check"
1137 "spam-log-unregistration-needed-p"))
1141 ;;; undo a ham- or spam-processor registration (the group is not used)
1142 (defun spam-log-undo-registration (id type classification check &optional group)
1143 (when (and spam-log-to-registry
1144 (spam-log-unregistration-needed-p id type classification check))
1145 (if (and (stringp id)
1146 (spam-process-type-valid-p type)
1147 (spam-classification-valid-p classification)
1148 (spam-registration-check-valid-p check))
1149 (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type)))
1150 new-cell-list found)
1151 (dolist (cell cell-list)
1152 (unless (and (eq classification (nth 0 cell))
1153 (eq check (nth 1 cell)))
1154 (push cell new-cell-list)))
1155 (gnus-registry-store-extra-entry
1160 (gnus-message 5 (format "%s called with bad ID, type, check, or group"
1161 "spam-log-undo-registration"))
1164 ;;; set up IMAP widening if it's necessary
1165 (defun spam-setup-widening ()
1166 (dolist (check spam-list-of-statistical-checks)
1167 (when (symbol-value check)
1168 (setq nnimap-split-download-body-default t))))
1173 (defun spam-check-regex-body ()
1174 (let ((spam-regex-headers-ham spam-regex-body-ham)
1175 (spam-regex-headers-spam spam-regex-body-spam))
1176 (spam-check-regex-headers t)))
1181 (defun spam-check-regex-headers (&optional body)
1182 (let ((type (if body "body" "header"))
1183 (spam-split-group (if spam-split-symbolic-return
1187 (dolist (h-regex spam-regex-headers-ham)
1189 (goto-char (point-min))
1190 (when (re-search-forward h-regex nil t)
1191 (message "Ham regex %s search positive." type)
1193 (dolist (s-regex spam-regex-headers-spam)
1195 (goto-char (point-min))
1196 (when (re-search-forward s-regex nil t)
1197 (message "Spam regex %s search positive." type)
1199 (setq ret spam-split-group))))
1205 (defun spam-reverse-ip-string (ip)
1207 (mapconcat 'identity
1208 (nreverse (split-string ip "\\."))
1211 (defun spam-check-blackholes ()
1212 "Check the Received headers for blackholed relays."
1213 (let ((headers (nnmail-fetch-field "received"))
1214 (spam-split-group (if spam-split-symbolic-return
1221 (goto-char (point-min))
1222 (gnus-message 5 "Checking headers for relay addresses")
1223 (while (re-search-forward
1224 "\\([0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+\\)" nil t)
1225 (gnus-message 9 "Blackhole search found host IP %s." (match-string 1))
1226 (push (spam-reverse-ip-string (match-string 1))
1228 (dolist (server spam-blackhole-servers)
1230 (unless (and spam-blackhole-good-server-regex
1231 ;; match the good-server-regex against the reversed (again) IP string
1233 spam-blackhole-good-server-regex
1234 (spam-reverse-ip-string ip)))
1236 (let ((query-string (concat ip "." server)))
1238 (let ((query-result (query-dig query-string)))
1240 (gnus-message 5 "(DIG): positive blackhole check '%s'"
1242 (push (list ip server query-result)
1244 ;; else, if not using dig.el
1245 (when (query-dns query-string)
1246 (gnus-message 5 "positive blackhole check")
1247 (push (list ip server (query-dns query-string 'TXT))
1258 (defun spam-check-hashcash ()
1259 "Check the headers for hashcash payments."
1260 (mail-check-payment))) ;mail-check-payment returns a boolean
1263 (defalias 'mail-check-payment 'ignore)
1264 (defalias 'spam-check-hashcash 'ignore))))
1268 ;;; original idea for spam-check-BBDB from Alexander Kotelnikov
1269 ;;; <sacha@giotto.sj.ru>
1271 ;; all this is done inside a condition-case to trap errors
1278 (defun spam-enter-ham-BBDB (addresses &optional remove)
1279 "Enter an address into the BBDB; implies ham (non-spam) sender"
1280 (dolist (from addresses)
1281 (when (stringp from)
1282 (let* ((parsed-address (gnus-extract-address-components from))
1283 (name (or (nth 0 parsed-address) "Ham Sender"))
1284 (remove-function (if remove
1285 'bbdb-delete-record-internal
1287 (net-address (nth 1 parsed-address))
1288 (record (and net-address
1289 (bbdb-search-simple nil net-address))))
1291 (gnus-message 5 "%s address %s %s BBDB"
1292 (if remove "Deleting" "Adding")
1294 (if remove "from" "to"))
1296 (funcall remove-function record)
1297 (bbdb-create-internal name nil net-address nil nil
1298 "ham sender added by spam.el")))))))
1300 (defun spam-BBDB-register-routine (articles &optional unregister)
1302 (dolist (article articles)
1303 (when (stringp (spam-fetch-field-from-fast article))
1304 (push (spam-fetch-field-from-fast article) addresses)))
1305 ;; now do the register/unregister action
1306 (spam-enter-ham-BBDB addresses unregister)))
1308 (defun spam-BBDB-unregister-routine (articles)
1309 (spam-BBDB-register-routine articles t))
1311 (defun spam-check-BBDB ()
1312 "Mail from people in the BBDB is classified as ham or non-spam"
1313 (let ((who (nnmail-fetch-field "from"))
1314 (spam-split-group (if spam-split-symbolic-return
1318 (setq who (nth 1 (gnus-extract-address-components who)))
1319 (if (bbdb-search-simple nil who)
1321 (if spam-use-BBDB-exclusive
1326 (defalias 'bbdb-search-simple 'ignore)
1327 (defalias 'spam-check-BBDB 'ignore)
1328 (defalias 'spam-BBDB-register-routine 'ignore)
1329 (defalias 'spam-enter-ham-BBDB 'ignore)
1330 (defalias 'bbdb-create-internal 'ignore)
1331 (defalias 'bbdb-delete-record-internal 'ignore)
1332 (defalias 'bbdb-records 'ignore))))
1337 ;;; check the ifile backend; return nil if the mail was NOT classified
1340 (defun spam-get-ifile-database-parameter ()
1341 "Get the command-line parameter for ifile's database from
1342 spam-ifile-database-path."
1343 (if spam-ifile-database-path
1344 (format "--db-file=%s" spam-ifile-database-path)
1347 (defun spam-check-ifile ()
1348 "Check the ifile backend for the classification of this message."
1349 (let ((article-buffer-name (buffer-name))
1350 (spam-split-group (if spam-split-symbolic-return
1355 (let ((temp-buffer-name (buffer-name))
1356 (db-param (spam-get-ifile-database-parameter)))
1358 (set-buffer article-buffer-name)
1359 (apply 'call-process-region
1360 (point-min) (point-max) spam-ifile-path
1361 nil temp-buffer-name nil "-c"
1362 (if db-param `(,db-param "-q") `("-q"))))
1363 ;; check the return now (we're back in the temp buffer)
1364 (goto-char (point-min))
1366 (setq category (buffer-substring (point) (point-at-eol))))
1367 (when (not (zerop (length category))) ; we need a category here
1368 (if spam-ifile-all-categories
1369 (setq return category)
1370 ;; else, if spam-ifile-all-categories is not set...
1371 (when (string-equal spam-ifile-spam-category category)
1372 (setq return spam-split-group)))))) ; note return is nil otherwise
1375 (defun spam-ifile-register-with-ifile (articles category &optional unregister)
1376 "Register an article, given as a string, with a category.
1377 Uses `gnus-newsgroup-name' if category is nil (for ham registration)."
1378 (let ((category (or category gnus-newsgroup-name))
1379 (add-or-delete-option (if unregister "-d" "-i"))
1380 (db (spam-get-ifile-database-parameter))
1383 (dolist (article articles)
1384 (let ((article-string (spam-get-article-as-string article)))
1385 (when (stringp article-string)
1386 (insert article-string))))
1387 (apply 'call-process-region
1388 (point-min) (point-max) spam-ifile-path
1390 add-or-delete-option category
1391 (if db `(,db "-h") `("-h"))))))
1393 (defun spam-ifile-register-spam-routine (articles &optional unregister)
1394 (spam-ifile-register-with-ifile articles spam-ifile-spam-category unregister))
1396 (defun spam-ifile-unregister-spam-routine (articles)
1397 (spam-ifile-register-spam-routine articles t))
1399 (defun spam-ifile-register-ham-routine (articles &optional unregister)
1400 (spam-ifile-register-with-ifile articles spam-ifile-ham-category unregister))
1402 (defun spam-ifile-unregister-ham-routine (articles)
1403 (spam-ifile-register-ham-routine articles t))
1410 (let ((spam-stat-install-hooks nil))
1411 (require 'spam-stat))
1413 (defun spam-check-stat ()
1414 "Check the spam-stat backend for the classification of this message"
1415 (let ((spam-split-group (if spam-split-symbolic-return
1418 (spam-stat-split-fancy-spam-group spam-split-group) ; override
1419 (spam-stat-buffer (buffer-name)) ; stat the current buffer
1421 (spam-stat-split-fancy)))
1423 (defun spam-stat-register-spam-routine (articles &optional unregister)
1424 (dolist (article articles)
1425 (let ((article-string (spam-get-article-as-string article)))
1427 (insert article-string)
1429 (spam-stat-buffer-change-to-non-spam)
1430 (spam-stat-buffer-is-spam))))))
1432 (defun spam-stat-unregister-spam-routine (articles)
1433 (spam-stat-register-spam-routine articles t))
1435 (defun spam-stat-register-ham-routine (articles &optional unregister)
1436 (dolist (article articles)
1437 (let ((article-string (spam-get-article-as-string article)))
1439 (insert article-string)
1441 (spam-stat-buffer-change-to-spam)
1442 (spam-stat-buffer-is-non-spam))))))
1444 (defun spam-stat-unregister-ham-routine (articles)
1445 (spam-stat-register-ham-routine articles t))
1447 (defun spam-maybe-spam-stat-load ()
1448 (when spam-use-stat (spam-stat-load)))
1450 (defun spam-maybe-spam-stat-save ()
1451 (when spam-use-stat (spam-stat-save))))
1454 (defalias 'spam-stat-load 'ignore)
1455 (defalias 'spam-stat-save 'ignore)
1456 (defalias 'spam-maybe-spam-stat-load 'ignore)
1457 (defalias 'spam-maybe-spam-stat-save 'ignore)
1458 (defalias 'spam-stat-register-ham-routine 'ignore)
1459 (defalias 'spam-stat-unregister-ham-routine 'ignore)
1460 (defalias 'spam-stat-register-spam-routine 'ignore)
1461 (defalias 'spam-stat-unregister-spam-routine 'ignore)
1462 (defalias 'spam-stat-buffer-is-spam 'ignore)
1463 (defalias 'spam-stat-buffer-change-to-spam 'ignore)
1464 (defalias 'spam-stat-buffer-is-non-spam 'ignore)
1465 (defalias 'spam-stat-buffer-change-to-non-spam 'ignore)
1466 (defalias 'spam-stat-split-fancy 'ignore)
1467 (defalias 'spam-check-stat 'ignore))))
1471 ;;;; Blacklists and whitelists.
1473 (defvar spam-whitelist-cache nil)
1474 (defvar spam-blacklist-cache nil)
1476 (defun spam-kill-whole-line ()
1478 (let ((kill-whole-line t))
1481 ;;; address can be a list, too
1482 (defun spam-enter-whitelist (address &optional remove)
1483 "Enter ADDRESS (list or single) into the whitelist.
1484 With a non-nil REMOVE, remove them."
1485 (interactive "sAddress: ")
1486 (spam-enter-list address spam-whitelist remove)
1487 (setq spam-whitelist-cache nil))
1489 ;;; address can be a list, too
1490 (defun spam-enter-blacklist (address &optional remove)
1491 "Enter ADDRESS (list or single) into the blacklist.
1492 With a non-nil REMOVE, remove them."
1493 (interactive "sAddress: ")
1494 (spam-enter-list address spam-blacklist remove)
1495 (setq spam-blacklist-cache nil))
1497 (defun spam-enter-list (addresses file &optional remove)
1498 "Enter ADDRESSES into the given FILE.
1499 Either the whitelist or the blacklist files can be used. With
1500 REMOVE not nil, remove the ADDRESSES."
1501 (if (stringp addresses)
1502 (spam-enter-list (list addresses) file remove)
1503 ;; else, we have a list of addresses here
1504 (unless (file-exists-p (file-name-directory file))
1505 (make-directory (file-name-directory file) t))
1508 (find-file-noselect file))
1509 (dolist (a addresses)
1511 (goto-char (point-min))
1512 (if (re-search-forward (regexp-quote a) nil t)
1513 ;; found the address
1515 (spam-kill-whole-line))
1516 ;; else, the address was not found
1518 (goto-char (point-max))
1524 ;;; returns t if the sender is in the whitelist, nil or
1525 ;;; spam-split-group otherwise
1526 (defun spam-check-whitelist ()
1527 ;; FIXME! Should it detect when file timestamps change?
1528 (let ((spam-split-group (if spam-split-symbolic-return
1531 (unless spam-whitelist-cache
1532 (setq spam-whitelist-cache (spam-parse-list spam-whitelist)))
1533 (if (spam-from-listed-p spam-whitelist-cache)
1535 (if spam-use-whitelist-exclusive
1539 (defun spam-check-blacklist ()
1540 ;; FIXME! Should it detect when file timestamps change?
1541 (let ((spam-split-group (if spam-split-symbolic-return
1544 (unless spam-blacklist-cache
1545 (setq spam-blacklist-cache (spam-parse-list spam-blacklist)))
1546 (and (spam-from-listed-p spam-blacklist-cache) spam-split-group)))
1548 (defun spam-parse-list (file)
1549 (when (file-readable-p file)
1550 (let (contents address)
1552 (insert-file-contents file)
1554 (setq address (buffer-substring (point) (point-at-eol)))
1556 ;; insert the e-mail address if detected, otherwise the raw data
1557 (unless (zerop (length address))
1558 (let ((pure-address (nth 1 (gnus-extract-address-components address))))
1559 (push (or pure-address address) contents)))))
1560 (nreverse contents))))
1562 (defun spam-from-listed-p (cache)
1563 (let ((from (nnmail-fetch-field "from"))
1566 (let ((address (pop cache)))
1567 (unless (zerop (length address)) ; 0 for a nil address too
1568 (setq address (regexp-quote address))
1569 ;; fix regexp-quote's treatment of user-intended regexes
1570 (while (string-match "\\\\\\*" address)
1571 (setq address (replace-match ".*" t t address))))
1572 (when (and address (string-match address from))
1577 (defun spam-filelist-register-routine (articles blacklist &optional unregister)
1578 (let ((de-symbol (if blacklist 'spam-use-whitelist 'spam-use-blacklist))
1579 (declassification (if blacklist 'ham 'spam))
1581 (if blacklist 'spam-enter-blacklist 'spam-enter-whitelist))
1583 (if blacklist 'spam-enter-whitelist 'spam-enter-blacklist))
1584 from addresses unregister-list)
1585 (dolist (article articles)
1586 (let ((from (spam-fetch-field-from-fast article))
1587 (id (spam-fetch-field-message-id-fast article))
1589 (when (stringp from)
1590 (dolist (ignore-regex spam-blacklist-ignored-regexes)
1591 (when (and (not sender-ignored)
1592 (stringp ignore-regex)
1593 (string-match ignore-regex from))
1594 (setq sender-ignored t)))
1595 ;; remember the messages we need to unregister, unless remove is set
1598 (spam-log-unregistration-needed-p
1599 id 'process declassification de-symbol))
1600 (push from unregister-list))
1601 (unless sender-ignored
1602 (push from addresses)))))
1605 (funcall enter-function addresses t) ; unregister all these addresses
1606 ;; else, register normally and unregister what we need to
1607 (funcall remove-function unregister-list t)
1608 (dolist (article unregister-list)
1609 (spam-log-undo-registration
1610 (spam-fetch-field-message-id-fast article)
1614 (funcall enter-function addresses nil))))
1616 (defun spam-blacklist-unregister-routine (articles)
1617 (spam-blacklist-register-routine articles t))
1619 (defun spam-blacklist-register-routine (articles &optional unregister)
1620 (spam-filelist-register-routine articles t unregister))
1622 (defun spam-whitelist-unregister-routine (articles)
1623 (spam-whitelist-register-routine articles t))
1625 (defun spam-whitelist-register-routine (articles &optional unregister)
1626 (spam-filelist-register-routine articles nil unregister))
1629 ;;;; Spam-report glue
1630 (defun spam-report-gmane-register-routine (articles)
1632 (apply 'spam-report-gmane articles)))
1636 (defun spam-check-bogofilter-headers (&optional score)
1637 (let ((header (nnmail-fetch-field spam-bogofilter-header))
1638 (spam-split-group (if spam-split-symbolic-return
1641 (when header ; return nil when no header
1642 (if score ; scoring mode
1643 (if (string-match "spamicity=\\([0-9.]+\\)" header)
1644 (match-string 1 header)
1646 ;; spam detection mode
1647 (when (string-match spam-bogofilter-bogosity-positive-spam-header
1649 spam-split-group)))))
1651 ;; return something sensible if the score can't be determined
1652 (defun spam-bogofilter-score ()
1653 "Get the Bogofilter spamicity score"
1655 (save-window-excursion
1656 (gnus-summary-show-article t)
1657 (set-buffer gnus-article-buffer)
1658 (let ((score (or (spam-check-bogofilter-headers t)
1659 (spam-check-bogofilter t))))
1660 (message "Spamicity score %s" score)
1662 (gnus-summary-show-article)))
1664 (defun spam-check-bogofilter (&optional score)
1665 "Check the Bogofilter backend for the classification of this message"
1666 (let ((article-buffer-name (buffer-name))
1667 (db spam-bogofilter-database-directory)
1670 (let ((temp-buffer-name (buffer-name)))
1672 (set-buffer article-buffer-name)
1673 (apply 'call-process-region
1674 (point-min) (point-max)
1675 spam-bogofilter-path
1676 nil temp-buffer-name nil
1677 (if db `("-d" ,db "-v") `("-v"))))
1678 (setq return (spam-check-bogofilter-headers score))))
1681 (defun spam-bogofilter-register-with-bogofilter (articles
1683 &optional unregister)
1684 "Register an article, given as a string, as spam or non-spam."
1685 (dolist (article articles)
1686 (let ((article-string (spam-get-article-as-string article))
1687 (db spam-bogofilter-database-directory)
1688 (switch (if unregister
1690 spam-bogofilter-spam-strong-switch
1691 spam-bogofilter-ham-strong-switch)
1693 spam-bogofilter-spam-switch
1694 spam-bogofilter-ham-switch))))
1695 (when (stringp article-string)
1697 (insert article-string)
1699 (apply 'call-process-region
1700 (point-min) (point-max)
1701 spam-bogofilter-path
1703 (if db `("-d" ,db "-v") `("-v"))))))))
1705 (defun spam-bogofilter-register-spam-routine (articles &optional unregister)
1706 (spam-bogofilter-register-with-bogofilter articles t unregister))
1708 (defun spam-bogofilter-unregister-spam-routine (articles)
1709 (spam-bogofilter-register-spam-routine articles t))
1711 (defun spam-bogofilter-register-ham-routine (articles &optional unregister)
1712 (spam-bogofilter-register-with-bogofilter articles nil unregister))
1714 (defun spam-bogofilter-unregister-ham-routine (articles)
1715 (spam-bogofilter-register-ham-routine articles t))
1720 (defun spam-check-spamoracle ()
1721 "Run spamoracle on an article to determine whether it's spam."
1722 (let ((article-buffer-name (buffer-name))
1723 (spam-split-group (if spam-split-symbolic-return
1727 (let ((temp-buffer-name (buffer-name)))
1729 (set-buffer article-buffer-name)
1731 (apply 'call-process-region
1732 (point-min) (point-max)
1733 spam-spamoracle-binary
1734 nil temp-buffer-name nil
1735 (if spam-spamoracle-database
1736 `("-f" ,spam-spamoracle-database "mark")
1740 (set-buffer temp-buffer-name)
1741 (goto-char (point-min))
1742 (when (re-search-forward "^X-Spam: yes;" nil t)
1744 (error "Error running spamoracle" status))))))))
1746 (defun spam-spamoracle-learn (articles article-is-spam-p &optional unregister)
1747 "Run spamoracle in training mode."
1749 (let ((temp-buffer-name (buffer-name)))
1751 (goto-char (point-min))
1752 (dolist (article articles)
1753 (insert (spam-get-article-as-string article)))
1754 (let* ((arg (if (spam-xor unregister article-is-spam-p)
1758 (apply 'call-process-region
1759 (point-min) (point-max)
1760 spam-spamoracle-binary
1761 nil temp-buffer-name nil
1762 (if spam-spamoracle-database
1763 `("-f" ,spam-spamoracle-database
1766 (when (not (eq 0 status))
1767 (error "Error running spamoracle" status)))))))
1769 (defun spam-spamoracle-learn-ham (articles &optional unregister)
1770 (spam-spamoracle-learn articles nil unregister))
1772 (defun spam-spamoracle-unlearn-ham (articles &optional unregister)
1773 (spam-spamoracle-learn-ham articles t))
1775 (defun spam-spamoracle-learn-spam (articles &optional unregister)
1776 (spam-spamoracle-learn articles t unregister))
1778 (defun spam-spamoracle-unlearn-spam (articles &optional unregister)
1779 (spam-spamoracle-learn-spam articles t))
1785 (defun spam-initialize ()
1786 "Install the spam.el hooks and do other initialization"
1788 (setq spam-install-hooks t)
1789 ;; TODO: How do we redo this every time spam-face is customized?
1790 (push '((eq mark gnus-spam-mark) . spam-face)
1791 gnus-summary-highlight)
1792 ;; Add hooks for loading and saving the spam stats
1793 (add-hook 'gnus-save-newsrc-hook 'spam-maybe-spam-stat-save)
1794 (add-hook 'gnus-get-top-new-news-hook 'spam-maybe-spam-stat-load)
1795 (add-hook 'gnus-startup-hook 'spam-maybe-spam-stat-load)
1796 (add-hook 'gnus-summary-prepare-exit-hook 'spam-summary-prepare-exit)
1797 (add-hook 'gnus-summary-prepare-hook 'spam-summary-prepare)
1798 (add-hook 'gnus-get-new-news-hook 'spam-setup-widening)
1799 (add-hook 'gnus-summary-prepare-hook 'spam-find-spam))
1801 (defun spam-unload-hook ()
1802 "Uninstall the spam.el hooks"
1804 (remove-hook 'gnus-save-newsrc-hook 'spam-maybe-spam-stat-save)
1805 (remove-hook 'gnus-get-top-new-news-hook 'spam-maybe-spam-stat-load)
1806 (remove-hook 'gnus-startup-hook 'spam-maybe-spam-stat-load)
1807 (remove-hook 'gnus-summary-prepare-exit-hook 'spam-summary-prepare-exit)
1808 (remove-hook 'gnus-summary-prepare-hook 'spam-summary-prepare)
1809 (remove-hook 'gnus-get-new-news-hook 'spam-setup-widening)
1810 (remove-hook 'gnus-summary-prepare-hook 'spam-find-spam))
1812 (when spam-install-hooks
1817 ;;; spam.el ends here.
1821 ;;; spam.el ends here