1 ;;; spam.el --- Identifying spam
3 ;; Copyright (C) 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
27 ;;; This module addresses a few aspects of spam control under Gnus. Page
28 ;;; breaks are used for grouping declarations and documentation relating to
29 ;;; each particular aspect.
31 ;;; The integration with Gnus is not yet complete. See various `FIXME'
32 ;;; comments, below, for supplementary explanations or discussions.
34 ;;; Several TODO items are marked as such
36 ;; TODO: spam scores, detection of spam in newsgroups, cross-server splitting,
37 ;; remote processing, training through files
41 (eval-when-compile (require 'cl
))
45 (require 'gnus-uu
) ; because of key prefix issues
46 ;;; for the definitions of group content classification and spam processors
48 (require 'message
) ;for the message-fetch-field functions
50 ;; for nnimap-split-download-body-default
51 (eval-when-compile (require 'nnimap
))
53 ;; autoload executable-find
55 ;; executable-find is not autoloaded in Emacs 20
56 (autoload 'executable-find
"executable"))
60 (autoload 'query-dig
"dig"))
62 ;; autoload spam-report
64 (autoload 'spam-report-gmane
"spam-report"))
66 ;; autoload gnus-registry
68 (autoload 'gnus-registry-group-count
"gnus-registry")
69 (autoload 'gnus-registry-add-group
"gnus-registry")
70 (autoload 'gnus-registry-store-extra-entry
"gnus-registry")
71 (autoload 'gnus-registry-fetch-extra
"gnus-registry"))
75 (autoload 'query-dns
"dns"))
85 (defcustom spam-directory
(nnheader-concat gnus-directory
"spam/")
86 "Directory for spam whitelists and blacklists."
90 (defcustom spam-move-spam-nonspam-groups-only t
91 "Whether spam should be moved in non-spam groups only.
92 When t, only ham and unclassified groups will have their spam moved
93 to the spam-process-destination. When nil, spam will also be moved from
98 (defcustom spam-process-ham-in-nonham-groups nil
99 "Whether ham should be processed in non-ham groups."
103 (defcustom spam-mark-new-messages-in-spam-group-as-spam t
104 "Whether new messages in a spam group should get the spam-mark."
106 ;; :version "22.1" ;; Gnus 5.10.8 / No Gnus 0.3
109 (defcustom spam-log-to-registry nil
110 "Whether spam/ham processing should be logged in the registry."
114 (defcustom spam-split-symbolic-return nil
115 "Whether `spam-split' should work with symbols or group names."
119 (defcustom spam-split-symbolic-return-positive nil
120 "Whether `spam-split' should ALWAYS work with symbols or group names.
121 Do not set this if you use `spam-split' in a fancy split
126 (defcustom spam-process-ham-in-spam-groups nil
127 "Whether ham should be processed in spam groups."
131 (defcustom spam-mark-only-unseen-as-spam t
132 "Whether only unseen articles should be marked as spam in spam groups.
133 When nil, all unread articles in a spam group are marked as
134 spam. Set this if you want to leave an article unread in a spam group
135 without losing it to the automatic spam-marking process."
139 (defcustom spam-mark-ham-unread-before-move-from-spam-group nil
140 "Whether ham should be marked unread before it's moved.
141 The article is moved out of a spam group according to ham-process-destination.
142 This variable is an official entry in the international Longest Variable Name
147 (defcustom spam-disable-spam-split-during-ham-respool nil
148 "Whether `spam-split' should be ignored while resplitting ham in a process
149 destination. This is useful to prevent ham from ending up in the same spam
150 group after the resplit. Don't set this to t if you have spam-split as the
151 last rule in your split configuration."
155 (defcustom spam-autodetect-recheck-messages nil
156 "Should spam.el recheck all meessages when autodetecting?
157 Normally this is nil, so only unseen messages will be checked."
161 (defcustom spam-whitelist
(expand-file-name "whitelist" spam-directory
)
162 "The location of the whitelist.
163 The file format is one regular expression per line.
164 The regular expression is matched against the address."
168 (defcustom spam-blacklist
(expand-file-name "blacklist" spam-directory
)
169 "The location of the blacklist.
170 The file format is one regular expression per line.
171 The regular expression is matched against the address."
175 (defcustom spam-use-dig t
176 "Whether `query-dig' should be used instead of `query-dns'."
180 (defcustom spam-use-blacklist nil
181 "Whether the blacklist should be used by `spam-split'."
185 (defcustom spam-blacklist-ignored-regexes nil
186 "Regular expressions that the blacklist should ignore."
187 :type
'(repeat (regexp :tag
"Regular expression to ignore when blacklisting"))
190 (defcustom spam-use-whitelist nil
191 "Whether the whitelist should be used by `spam-split'."
195 (defcustom spam-use-whitelist-exclusive nil
196 "Whether whitelist-exclusive should be used by `spam-split'.
197 Exclusive whitelisting means that all messages from senders not in the whitelist
198 are considered spam."
202 (defcustom spam-use-blackholes nil
203 "Whether blackholes should be used by `spam-split'."
207 (defcustom spam-use-hashcash nil
208 "Whether hashcash payments should be detected by `spam-split'."
212 (defcustom spam-use-regex-headers nil
213 "Whether a header regular expression match should be used by `spam-split'.
214 Also see the variables `spam-regex-headers-spam' and `spam-regex-headers-ham'."
218 (defcustom spam-use-regex-body nil
219 "Whether a body regular expression match should be used by `spam-split'.
220 Also see the variables `spam-regex-body-spam' and `spam-regex-body-ham'."
224 (defcustom spam-use-bogofilter-headers nil
225 "Whether bogofilter headers should be used by `spam-split'.
226 Enable this if you pre-process messages with Bogofilter BEFORE Gnus sees them."
230 (defcustom spam-use-bogofilter nil
231 "Whether bogofilter should be invoked by `spam-split'.
232 Enable this if you want Gnus to invoke Bogofilter on new messages."
236 (defcustom spam-use-BBDB nil
237 "Whether BBDB should be used by `spam-split'."
241 (defcustom spam-use-BBDB-exclusive nil
242 "Whether BBDB-exclusive should be used by `spam-split'.
243 Exclusive BBDB means that all messages from senders not in the BBDB are
248 (defcustom spam-use-ifile nil
249 "Whether ifile should be used by `spam-split'."
253 (defcustom spam-use-stat nil
254 "Whether `spam-stat' should be used by `spam-split'."
258 (defcustom spam-use-spamoracle nil
259 "Whether spamoracle should be used by `spam-split'."
263 (defcustom spam-install-hooks
(or
267 spam-use-whitelist-exclusive
270 spam-use-regex-headers
272 spam-use-bogofilter-headers
275 spam-use-BBDB-exclusive
279 "Whether the spam hooks should be installed.
280 Default to t if one of the spam-use-* variables is set."
284 (defcustom spam-split-group
"spam"
285 "Group name where incoming spam should be put by `spam-split'."
289 ;;; TODO: deprecate this variable, it's confusing since it's a list of strings,
290 ;;; not regular expressions
291 (defcustom spam-junk-mailgroups
(cons
293 '("mail.junk" "poste.pourriel"))
294 "Mailgroups with spam contents.
295 All unmarked article in such group receive the spam mark on group entry."
296 :type
'(repeat (string :tag
"Group"))
299 (defcustom spam-blackhole-servers
'("bl.spamcop.net" "relays.ordb.org"
300 "dev.null.dk" "relays.visi.com")
301 "List of blackhole servers."
302 :type
'(repeat (string :tag
"Server"))
305 (defcustom spam-blackhole-good-server-regex nil
306 "String matching IP addresses that should not be checked in the blackholes."
307 :type
'(radio (const nil
) regexp
)
311 '((((class color
) (type tty
) (background dark
))
312 (:foreground
"gray80" :background
"gray50"))
313 (((class color
) (type tty
) (background light
))
314 (:foreground
"gray50" :background
"gray80"))
315 (((class color
) (background dark
))
316 (:foreground
"ivory2"))
317 (((class color
) (background light
))
318 (:foreground
"ivory4"))
319 (t :inverse-video t
))
320 "Face for spam-marked articles."
322 ;; backward-compatibility alias
323 (put 'spam-face
'face-alias
'spam
)
325 (defcustom spam-face
'spam
326 "Face for spam-marked articles."
330 (defcustom spam-regex-headers-spam
'("^X-Spam-Flag: YES")
331 "Regular expression for positive header spam matches."
332 :type
'(repeat (regexp :tag
"Regular expression to match spam header"))
335 (defcustom spam-regex-headers-ham
'("^X-Spam-Flag: NO")
336 "Regular expression for positive header ham matches."
337 :type
'(repeat (regexp :tag
"Regular expression to match ham header"))
340 (defcustom spam-regex-body-spam
'()
341 "Regular expression for positive body spam matches."
342 :type
'(repeat (regexp :tag
"Regular expression to match spam body"))
345 (defcustom spam-regex-body-ham
'()
346 "Regular expression for positive body ham matches."
347 :type
'(repeat (regexp :tag
"Regular expression to match ham body"))
350 (defgroup spam-ifile nil
351 "Spam ifile configuration."
354 (defcustom spam-ifile-path
(executable-find "ifile")
355 "File path of the ifile executable program."
356 :type
'(choice (file :tag
"Location of ifile")
357 (const :tag
"ifile is not installed"))
360 (defcustom spam-ifile-database-path nil
361 "File path of the ifile database."
362 :type
'(choice (file :tag
"Location of the ifile database")
363 (const :tag
"Use the default"))
366 (defcustom spam-ifile-spam-category
"spam"
367 "Name of the spam ifile category."
371 (defcustom spam-ifile-ham-category nil
372 "Name of the ham ifile category.
373 If nil, the current group name will be used."
374 :type
'(choice (string :tag
"Use a fixed category")
375 (const :tag
"Use the current group name"))
378 (defcustom spam-ifile-all-categories nil
379 "Whether the ifile check will return all categories, or just spam.
380 Set this to t if you want to use the `spam-split' invocation of ifile as
381 your main source of newsgroup names."
385 (defgroup spam-bogofilter nil
386 "Spam bogofilter configuration."
389 (defcustom spam-bogofilter-path
(executable-find "bogofilter")
390 "File path of the Bogofilter executable program."
391 :type
'(choice (file :tag
"Location of bogofilter")
392 (const :tag
"Bogofilter is not installed"))
393 :group
'spam-bogofilter
)
395 (defcustom spam-bogofilter-header
"X-Bogosity"
396 "The header that Bogofilter inserts in messages."
398 :group
'spam-bogofilter
)
400 (defcustom spam-bogofilter-spam-switch
"-s"
401 "The switch that Bogofilter uses to register spam messages."
403 :group
'spam-bogofilter
)
405 (defcustom spam-bogofilter-ham-switch
"-n"
406 "The switch that Bogofilter uses to register ham messages."
408 :group
'spam-bogofilter
)
410 (defcustom spam-bogofilter-spam-strong-switch
"-S"
411 "The switch that Bogofilter uses to unregister ham messages."
413 :group
'spam-bogofilter
)
415 (defcustom spam-bogofilter-ham-strong-switch
"-N"
416 "The switch that Bogofilter uses to unregister spam messages."
418 :group
'spam-bogofilter
)
420 (defcustom spam-bogofilter-bogosity-positive-spam-header
"^\\(Yes\\|Spam\\)"
421 "The regex on `spam-bogofilter-header' for positive spam identification."
423 :group
'spam-bogofilter
)
425 (defcustom spam-bogofilter-database-directory nil
426 "Directory path of the Bogofilter databases."
427 :type
'(choice (directory
428 :tag
"Location of the Bogofilter database directory")
429 (const :tag
"Use the default"))
430 :group
'spam-bogofilter
)
432 (defgroup spam-spamoracle nil
433 "Spam spamoracle configuration."
436 (defcustom spam-spamoracle-database nil
437 "Location of spamoracle database file. When nil, use the default
438 spamoracle database."
439 :type
'(choice (directory :tag
"Location of spamoracle database file.")
440 (const :tag
"Use the default"))
441 :group
'spam-spamoracle
)
443 (defcustom spam-spamoracle-binary
(executable-find "spamoracle")
444 "Location of the spamoracle binary."
445 :type
'(choice (directory :tag
"Location of the spamoracle binary")
446 (const :tag
"Use the default"))
447 :group
'spam-spamoracle
)
449 ;;; Key bindings for spam control.
451 (gnus-define-keys gnus-summary-mode-map
452 "St" spam-bogofilter-score
453 "Sx" gnus-summary-mark-as-spam
454 "Mst" spam-bogofilter-score
455 "Msx" gnus-summary-mark-as-spam
456 "\M-d" gnus-summary-mark-as-spam
)
458 (defvar spam-old-ham-articles nil
459 "List of old ham articles, generated when a group is entered.")
461 (defvar spam-old-spam-articles nil
462 "List of old spam articles, generated when a group is entered.")
464 (defvar spam-split-disabled nil
465 "If non-nil, `spam-split' is disabled, and always returns nil.")
467 (defvar spam-split-last-successful-check nil
468 "`spam-split' will set this to nil or a spam-use-XYZ check if it
471 ;; convenience functions
472 (defun spam-xor (a b
)
473 "Logical exclusive `or'."
474 (and (or a b
) (not (and a b
))))
476 (defun spam-group-ham-mark-p (group mark
&optional spam
)
477 (when (stringp group
)
478 (let* ((marks (spam-group-ham-marks group spam
))
479 (marks (if (symbolp mark
)
481 (mapcar 'symbol-value marks
))))
484 (defun spam-group-spam-mark-p (group mark
)
485 (spam-group-ham-mark-p group mark t
))
487 (defun spam-group-ham-marks (group &optional spam
)
488 (when (stringp group
)
489 (let* ((marks (if spam
490 (gnus-parameter-spam-marks group
)
491 (gnus-parameter-ham-marks group
)))
493 (marks (if (listp (car marks
)) (car marks
) marks
)))
496 (defun spam-group-spam-marks (group)
497 (spam-group-ham-marks group t
))
499 (defun spam-group-spam-contents-p (group)
501 (or (member group spam-junk-mailgroups
)
502 (memq 'gnus-group-spam-classification-spam
503 (gnus-parameter-spam-contents group
)))
506 (defun spam-group-ham-contents-p (group)
508 (memq 'gnus-group-spam-classification-ham
509 (gnus-parameter-spam-contents group
))
512 (defvar spam-list-of-processors
513 '((gnus-group-spam-exit-processor-report-gmane spam spam-use-gmane
)
514 (gnus-group-spam-exit-processor-bogofilter spam spam-use-bogofilter
)
515 (gnus-group-spam-exit-processor-blacklist spam spam-use-blacklist
)
516 (gnus-group-spam-exit-processor-ifile spam spam-use-ifile
)
517 (gnus-group-spam-exit-processor-stat spam spam-use-stat
)
518 (gnus-group-spam-exit-processor-spamoracle spam spam-use-spamoracle
)
519 (gnus-group-ham-exit-processor-ifile ham spam-use-ifile
)
520 (gnus-group-ham-exit-processor-bogofilter ham spam-use-bogofilter
)
521 (gnus-group-ham-exit-processor-stat ham spam-use-stat
)
522 (gnus-group-ham-exit-processor-whitelist ham spam-use-whitelist
)
523 (gnus-group-ham-exit-processor-BBDB ham spam-use-BBDB
)
524 (gnus-group-ham-exit-processor-copy ham spam-use-ham-copy
)
525 (gnus-group-ham-exit-processor-spamoracle ham spam-use-spamoracle
))
526 "The spam-list-of-processors list contains pairs associating a
527 ham/spam exit processor variable with a classification and a
528 spam-use-* variable.")
530 (defun spam-group-processor-p (group processor
)
531 (if (and (stringp group
)
533 (or (member processor
(nth 0 (gnus-parameter-spam-process group
)))
534 (spam-group-processor-multiple-p
536 (cdr-safe (assoc processor spam-list-of-processors
))))
539 (defun spam-group-processor-multiple-p (group processor-info
)
540 (let* ((classification (nth 0 processor-info
))
541 (check (nth 1 processor-info
))
542 (parameters (nth 0 (gnus-parameter-spam-process group
)))
544 (dolist (parameter parameters
)
545 (when (and (null found
)
547 (eq classification
(nth 0 parameter
))
548 (eq check
(nth 1 parameter
)))
552 (defun spam-group-spam-processor-report-gmane-p (group)
553 (spam-group-processor-p group
'gnus-group-spam-exit-processor-report-gmane
))
555 (defun spam-group-spam-processor-bogofilter-p (group)
556 (spam-group-processor-p group
'gnus-group-spam-exit-processor-bogofilter
))
558 (defun spam-group-spam-processor-blacklist-p (group)
559 (spam-group-processor-p group
'gnus-group-spam-exit-processor-blacklist
))
561 (defun spam-group-spam-processor-ifile-p (group)
562 (spam-group-processor-p group
'gnus-group-spam-exit-processor-ifile
))
564 (defun spam-group-ham-processor-ifile-p (group)
565 (spam-group-processor-p group
'gnus-group-ham-exit-processor-ifile
))
567 (defun spam-group-spam-processor-spamoracle-p (group)
568 (spam-group-processor-p group
'gnus-group-spam-exit-processor-spamoracle
))
570 (defun spam-group-ham-processor-bogofilter-p (group)
571 (spam-group-processor-p group
'gnus-group-ham-exit-processor-bogofilter
))
573 (defun spam-group-spam-processor-stat-p (group)
574 (spam-group-processor-p group
'gnus-group-spam-exit-processor-stat
))
576 (defun spam-group-ham-processor-stat-p (group)
577 (spam-group-processor-p group
'gnus-group-ham-exit-processor-stat
))
579 (defun spam-group-ham-processor-whitelist-p (group)
580 (spam-group-processor-p group
'gnus-group-ham-exit-processor-whitelist
))
582 (defun spam-group-ham-processor-BBDB-p (group)
583 (spam-group-processor-p group
'gnus-group-ham-exit-processor-BBDB
))
585 (defun spam-group-ham-processor-copy-p (group)
586 (spam-group-processor-p group
'gnus-group-ham-exit-processor-copy
))
588 (defun spam-group-ham-processor-spamoracle-p (group)
589 (spam-group-processor-p group
'gnus-group-ham-exit-processor-spamoracle
))
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 6 "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 (if spam-mark-new-messages-in-spam-group-as-spam
693 (dolist (article articles
)
694 (gnus-summary-mark-article article gnus-spam-mark
))
695 (gnus-message 9 "Did not mark new messages as spam.")))))
697 (defun spam-mark-spam-as-expired-and-move-routine (&rest groups
)
698 (if (and (car-safe groups
) (listp (car-safe groups
)))
699 (apply 'spam-mark-spam-as-expired-and-move-routine
(car groups
))
700 (gnus-summary-kill-process-mark)
701 (let ((articles gnus-newsgroup-articles
)
702 (backend-supports-deletions
703 (gnus-check-backend-function
704 'request-move-article gnus-newsgroup-name
))
705 article tomove deletep
)
706 (dolist (article articles
)
707 (when (eq (gnus-summary-article-mark article
) gnus-spam-mark
)
708 (gnus-summary-mark-article article gnus-expirable-mark
)
709 (push article tomove
)))
711 ;; now do the actual copies
712 (dolist (group groups
)
715 (dolist (article tomove
)
716 (gnus-summary-set-process-mark article
))
718 (if (or (not backend-supports-deletions
)
719 (> (length groups
) 1))
721 (gnus-summary-copy-article nil group
)
723 (gnus-summary-move-article nil group
)))))
725 ;; now delete the articles, if there was a copy done, and the
727 (when (and deletep backend-supports-deletions
)
728 (dolist (article tomove
)
729 (gnus-summary-set-process-mark article
))
731 (let ((gnus-novice-user nil
)) ; don't ask me if I'm sure
732 (gnus-summary-delete-article nil
))))
734 (gnus-summary-yank-process-mark))))
736 (defun spam-ham-copy-or-move-routine (copy groups
)
737 (gnus-summary-kill-process-mark)
738 (let ((todo (spam-list-articles gnus-newsgroup-articles
'ham
))
739 (backend-supports-deletions
740 (gnus-check-backend-function
741 'request-move-article gnus-newsgroup-name
))
742 (respool-method (gnus-find-method-for-group gnus-newsgroup-name
))
743 article mark deletep respool
)
745 (when (member 'respool groups
)
746 (setq respool t
) ; boolean for later
747 (setq groups
'("fake"))) ; when respooling, groups are dynamic so fake it
749 ;; now do the actual move
750 (dolist (group groups
)
751 (when (and todo
(stringp group
))
752 (dolist (article todo
)
753 (when spam-mark-ham-unread-before-move-from-spam-group
754 (gnus-summary-mark-article article gnus-unread-mark
))
755 (gnus-summary-set-process-mark article
))
757 (if respool
; respooling is with a "fake" group
758 (let ((spam-split-disabled
759 (or spam-split-disabled
760 spam-disable-spam-split-during-ham-respool
)))
761 (gnus-summary-respool-article nil respool-method
))
762 (if (or (not backend-supports-deletions
) ; else, we are not respooling
763 (> (length groups
) 1))
764 (progn ; if copying, copy and set deletep
765 (gnus-summary-copy-article nil group
)
767 (gnus-summary-move-article nil group
))))) ; else move articles
769 ;; now delete the articles, unless a) copy is t, and there was a copy done
770 ;; b) a move was done to a single group
771 ;; c) backend-supports-deletions is nil
773 (when (and deletep backend-supports-deletions
)
774 (dolist (article todo
)
775 (gnus-summary-set-process-mark article
))
777 (let ((gnus-novice-user nil
)) ; don't ask me if I'm sure
778 (gnus-summary-delete-article nil
))))))
780 (gnus-summary-yank-process-mark))
782 (defun spam-ham-copy-routine (&rest groups
)
783 (if (and (car-safe groups
) (listp (car-safe groups
)))
784 (apply 'spam-ham-copy-routine
(car groups
))
785 (spam-ham-copy-or-move-routine t groups
)))
787 (defun spam-ham-move-routine (&rest groups
)
788 (if (and (car-safe groups
) (listp (car-safe groups
)))
789 (apply 'spam-ham-move-routine
(car groups
))
790 (spam-ham-copy-or-move-routine nil groups
)))
793 (defalias 'spam-point-at-eol
(if (fboundp 'point-at-eol
)
795 'line-end-position
)))
797 (defun spam-get-article-as-string (article)
798 (let ((article-buffer (spam-get-article-as-buffer article
))
801 (save-window-excursion
802 (set-buffer article-buffer
)
803 (setq article-string
(buffer-string))))
806 (defun spam-get-article-as-buffer (article)
807 (let ((article-buffer))
808 (when (numberp article
)
809 (save-window-excursion
810 (gnus-summary-goto-subject article
)
811 (gnus-summary-show-article t
)
812 (setq article-buffer
(get-buffer gnus-article-buffer
))))
816 ;; (defun spam-get-article-as-filename (article)
817 ;; (let ((article-filename))
818 ;; (when (numberp article)
819 ;; (nnml-possibly-change-directory
820 ;; (gnus-group-real-name gnus-newsgroup-name))
821 ;; (setq article-filename (expand-file-name
822 ;; (int-to-string article) nnml-current-directory)))
823 ;; (if (file-exists-p article-filename)
827 (defun spam-fetch-field-from-fast (article)
828 "Fetch the `from' field quickly, using the internal gnus-data-list function"
829 (if (and (numberp article
)
830 (assoc article
(gnus-data-list nil
)))
832 (gnus-data-header (assoc article
(gnus-data-list nil
))))
835 (defun spam-fetch-field-subject-fast (article)
836 "Fetch the `subject' field quickly, using the internal
837 gnus-data-list function"
838 (if (and (numberp article
)
839 (assoc article
(gnus-data-list nil
)))
841 (gnus-data-header (assoc article
(gnus-data-list nil
))))
844 (defun spam-fetch-field-message-id-fast (article)
845 "Fetch the `Message-ID' field quickly, using the internal
846 gnus-data-list function"
847 (if (and (numberp article
)
848 (assoc article
(gnus-data-list nil
)))
849 (mail-header-message-id
850 (gnus-data-header (assoc article
(gnus-data-list nil
))))
854 ;;;; Spam determination.
856 (defvar spam-list-of-checks
857 '((spam-use-blacklist . spam-check-blacklist
)
858 (spam-use-regex-headers . spam-check-regex-headers
)
859 (spam-use-regex-body . spam-check-regex-body
)
860 (spam-use-whitelist . spam-check-whitelist
)
861 (spam-use-BBDB . spam-check-BBDB
)
862 (spam-use-ifile . spam-check-ifile
)
863 (spam-use-spamoracle . spam-check-spamoracle
)
864 (spam-use-stat . spam-check-stat
)
865 (spam-use-blackholes . spam-check-blackholes
)
866 (spam-use-hashcash . spam-check-hashcash
)
867 (spam-use-bogofilter-headers . spam-check-bogofilter-headers
)
868 (spam-use-bogofilter . spam-check-bogofilter
))
869 "The spam-list-of-checks list contains pairs associating a
870 parameter variable with a spam checking function. If the
871 parameter variable is true, then the checking function is called,
872 and its value decides what happens. Each individual check may
873 return nil, t, or a mailgroup name. The value nil means that the
874 check does not yield a decision, and so, that further checks are
875 needed. The value t means that the message is definitely not
876 spam, and that further spam checks should be inhibited.
877 Otherwise, a mailgroup name or the symbol 'spam (depending on
878 spam-split-symbolic-return) is returned where the mail should go,
879 and further checks are also inhibited. The usual mailgroup name
880 is the value of `spam-split-group', meaning that the message is
883 (defvar spam-list-of-statistical-checks
889 "The spam-list-of-statistical-checks list contains all the mail
890 splitters that need to have the full message body available.")
892 ;;;TODO: modify to invoke self with each check if invoked without specifics
893 (defun spam-split (&rest specific-checks
)
894 "Split this message into the `spam' group if it is spam.
895 This function can be used as an entry in the variable `nnmail-split-fancy',
896 for example like this: (: spam-split). It can take checks as
897 parameters. A string as a parameter will set the
898 spam-split-group to that string.
900 See the Info node `(gnus)Fancy Mail Splitting' for more details."
902 (setq spam-split-last-successful-check nil
)
903 (unless spam-split-disabled
904 (let ((spam-split-group-choice spam-split-group
))
905 (dolist (check specific-checks
)
906 (when (stringp check
)
907 (setq spam-split-group-choice check
)
908 (setq specific-checks
(delq check specific-checks
))))
910 (let ((spam-split-group spam-split-group-choice
))
913 (dolist (check spam-list-of-statistical-checks
)
914 (when (and (symbolp check
) (symbol-value check
))
916 (gnus-message 8 "spam-split: widening the buffer (%s requires it)"
919 ;; (progn (widen) (debug (buffer-string)))
920 (let ((list-of-checks spam-list-of-checks
)
922 (while (and list-of-checks
(not decision
))
923 (let ((pair (pop list-of-checks
)))
924 (when (and (symbol-value (car pair
))
925 (or (null specific-checks
)
926 (memq (car pair
) specific-checks
)))
927 (gnus-message 5 "spam-split: calling the %s function"
928 (symbol-name (cdr pair
)))
929 (setq decision
(funcall (cdr pair
)))
930 ;; if we got a decision at all, save the current check
932 (setq spam-split-last-successful-check
(car pair
)))
934 (when (eq decision
'spam
)
935 (if spam-split-symbolic-return
936 (setq decision spam-split-group
)
939 (format "spam-split got %s but %s is nil"
940 (symbol-name decision
)
941 (symbol-name spam-split-symbolic-return
))))))))
943 (if spam-split-symbolic-return-positive
'ham nil
)
946 (defun spam-find-spam ()
947 "This function will detect spam in the current newsgroup using spam-split."
950 (let* ((group gnus-newsgroup-name
)
951 (autodetect (gnus-parameter-spam-autodetect group
))
952 (methods (gnus-parameter-spam-autodetect-methods group
))
953 (first-method (nth 0 methods
)))
954 (when (and autodetect
955 (not (equal first-method
'none
)))
958 (let ((id (spam-fetch-field-message-id-fast article
))
959 (subject (spam-fetch-field-subject-fast article
))
960 (sender (spam-fetch-field-from-fast article
)))
961 (unless (and spam-log-to-registry
962 (spam-log-registered-p id
'incoming
))
963 (let* ((spam-split-symbolic-return t
)
964 (spam-split-symbolic-return-positive t
)
967 (gnus-request-article-this-buffer
970 (if (or (null first-method
)
971 (equal first-method
'default
))
973 (apply 'spam-split methods
)))))
974 (if (equal split-return
'spam
)
975 (gnus-summary-mark-article article gnus-spam-mark
))
977 (when (and split-return spam-log-to-registry
)
978 (when (zerop (gnus-registry-group-count id
))
979 (gnus-registry-add-group
980 id group subject sender
))
982 (spam-log-processing-to-registry
986 spam-split-last-successful-check
988 (if spam-autodetect-recheck-messages
989 gnus-newsgroup-articles
990 gnus-newsgroup-unseen
)))))
992 (defvar spam-registration-functions
993 ;; first the ham register, second the spam register function
994 ;; third the ham unregister, fourth the spam unregister function
995 '((spam-use-blacklist nil
996 spam-blacklist-register-routine
998 spam-blacklist-unregister-routine
)
999 (spam-use-whitelist spam-whitelist-register-routine
1001 spam-whitelist-unregister-routine
1003 (spam-use-BBDB spam-BBDB-register-routine
1005 spam-BBDB-unregister-routine
1007 (spam-use-ifile spam-ifile-register-ham-routine
1008 spam-ifile-register-spam-routine
1009 spam-ifile-unregister-ham-routine
1010 spam-ifile-unregister-spam-routine
)
1011 (spam-use-spamoracle spam-spamoracle-learn-ham
1012 spam-spamoracle-learn-spam
1013 spam-spamoracle-unlearn-ham
1014 spam-spamoracle-unlearn-spam
)
1015 (spam-use-stat spam-stat-register-ham-routine
1016 spam-stat-register-spam-routine
1017 spam-stat-unregister-ham-routine
1018 spam-stat-unregister-spam-routine
)
1019 ;; note that spam-use-gmane is not a legitimate check
1021 spam-report-gmane-register-routine
1022 ;; does Gmane support unregistration?
1025 (spam-use-bogofilter spam-bogofilter-register-ham-routine
1026 spam-bogofilter-register-spam-routine
1027 spam-bogofilter-unregister-ham-routine
1028 spam-bogofilter-unregister-spam-routine
))
1029 "The spam-registration-functions list contains pairs
1030 associating a parameter variable with the ham and spam
1031 registration functions, and the ham and spam unregistration
1034 (defun spam-classification-valid-p (classification)
1035 (or (eq classification
'spam
)
1036 (eq classification
'ham
)))
1038 (defun spam-process-type-valid-p (process-type)
1039 (or (eq process-type
'incoming
)
1040 (eq process-type
'process
)))
1042 (defun spam-registration-check-valid-p (check)
1043 (assoc check spam-registration-functions
))
1045 (defun spam-unregistration-check-valid-p (check)
1046 (assoc check spam-registration-functions
))
1048 (defun spam-registration-function (classification check
)
1049 (let ((flist (cdr-safe (assoc check spam-registration-functions
))))
1050 (if (eq classification
'spam
)
1054 (defun spam-unregistration-function (classification check
)
1055 (let ((flist (cdr-safe (assoc check spam-registration-functions
))))
1056 (if (eq classification
'spam
)
1060 (defun spam-list-articles (articles classification
)
1061 (let ((mark-check (if (eq classification
'spam
)
1062 'spam-group-spam-mark-p
1063 'spam-group-ham-mark-p
))
1064 list mark-cache-yes mark-cache-no
)
1065 (dolist (article articles
)
1066 (let ((mark (gnus-summary-article-mark article
)))
1067 (unless (memq mark mark-cache-no
)
1068 (if (memq mark mark-cache-yes
)
1070 ;; else, we have to actually check the mark
1071 (if (funcall mark-check
1076 (push mark mark-cache-yes
))
1077 (push mark mark-cache-no
))))))
1080 (defun spam-register-routine (classification
1082 &optional unregister
1084 (when (and (spam-classification-valid-p classification
)
1085 (spam-registration-check-valid-p check
))
1086 (let* ((register-function
1087 (spam-registration-function classification check
))
1088 (unregister-function
1089 (spam-unregistration-function classification check
))
1090 (run-function (if unregister
1093 (log-function (if unregister
1094 'spam-log-undo-registration
1095 'spam-log-processing-to-registry
))
1099 ;; make list of articles, using specific-articles if given
1100 (setq articles
(or specific-articles
1102 gnus-newsgroup-articles
1105 (gnus-message 5 "%s %d %s articles with classification %s, check %s"
1106 (if unregister
"Unregistering" "Registering")
1108 (if specific-articles
"specific" "")
1109 (symbol-name classification
)
1110 (symbol-name check
))
1111 (funcall run-function articles
)
1112 ;; now log all the registrations (or undo them, depending on unregister)
1113 (dolist (article articles
)
1114 (funcall log-function
1115 (spam-fetch-field-message-id-fast article
)
1119 gnus-newsgroup-name
))))))
1121 ;;; log a ham- or spam-processor invocation to the registry
1122 (defun spam-log-processing-to-registry (id type classification check group
)
1123 (when spam-log-to-registry
1124 (if (and (stringp id
)
1126 (spam-process-type-valid-p type
)
1127 (spam-classification-valid-p classification
)
1128 (spam-registration-check-valid-p check
))
1129 (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type
)))
1130 (cell (list classification check group
)))
1131 (push cell cell-list
)
1132 (gnus-registry-store-extra-entry
1137 (gnus-message 5 (format "%s called with bad ID, type, classification, check, or group"
1138 "spam-log-processing-to-registry")))))
1140 ;;; check if a ham- or spam-processor registration has been done
1141 (defun spam-log-registered-p (id type
)
1142 (when spam-log-to-registry
1143 (if (and (stringp id
)
1144 (spam-process-type-valid-p type
))
1145 (cdr-safe (gnus-registry-fetch-extra id type
))
1147 (gnus-message 5 (format "%s called with bad ID, type, classification, or check"
1148 "spam-log-registered-p"))
1151 ;;; check if a ham- or spam-processor registration needs to be undone
1152 (defun spam-log-unregistration-needed-p (id type classification check
)
1153 (when spam-log-to-registry
1154 (if (and (stringp id
)
1155 (spam-process-type-valid-p type
)
1156 (spam-classification-valid-p classification
)
1157 (spam-registration-check-valid-p check
))
1158 (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type
)))
1160 (dolist (cell cell-list
)
1162 (when (and (eq classification
(nth 0 cell
))
1163 (eq check
(nth 1 cell
)))
1167 (gnus-message 5 (format "%s called with bad ID, type, classification, or check"
1168 "spam-log-unregistration-needed-p"))
1172 ;;; undo a ham- or spam-processor registration (the group is not used)
1173 (defun spam-log-undo-registration (id type classification check
&optional group
)
1174 (when (and spam-log-to-registry
1175 (spam-log-unregistration-needed-p id type classification check
))
1176 (if (and (stringp id
)
1177 (spam-process-type-valid-p type
)
1178 (spam-classification-valid-p classification
)
1179 (spam-registration-check-valid-p check
))
1180 (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type
)))
1181 new-cell-list found
)
1182 (dolist (cell cell-list
)
1183 (unless (and (eq classification
(nth 0 cell
))
1184 (eq check
(nth 1 cell
)))
1185 (push cell new-cell-list
)))
1186 (gnus-registry-store-extra-entry
1191 (gnus-message 5 (format "%s called with bad ID, type, check, or group"
1192 "spam-log-undo-registration"))
1195 ;;; set up IMAP widening if it's necessary
1196 (defun spam-setup-widening ()
1197 (dolist (check spam-list-of-statistical-checks
)
1198 (when (symbol-value check
)
1199 (setq nnimap-split-download-body-default t
))))
1204 (defun spam-check-regex-body ()
1205 (let ((spam-regex-headers-ham spam-regex-body-ham
)
1206 (spam-regex-headers-spam spam-regex-body-spam
))
1207 (spam-check-regex-headers t
)))
1212 (defun spam-check-regex-headers (&optional body
)
1213 (let ((type (if body
"body" "header"))
1214 (spam-split-group (if spam-split-symbolic-return
1218 (dolist (h-regex spam-regex-headers-ham
)
1220 (goto-char (point-min))
1221 (when (re-search-forward h-regex nil t
)
1222 (message "Ham regex %s search positive." type
)
1224 (dolist (s-regex spam-regex-headers-spam
)
1226 (goto-char (point-min))
1227 (when (re-search-forward s-regex nil t
)
1228 (message "Spam regex %s search positive." type
)
1230 (setq ret spam-split-group
))))
1236 (defun spam-reverse-ip-string (ip)
1238 (mapconcat 'identity
1239 (nreverse (split-string ip
"\\."))
1242 (defun spam-check-blackholes ()
1243 "Check the Received headers for blackholed relays."
1244 (let ((headers (nnmail-fetch-field "received"))
1245 (spam-split-group (if spam-split-symbolic-return
1252 (goto-char (point-min))
1253 (gnus-message 5 "Checking headers for relay addresses")
1254 (while (re-search-forward
1255 "\\([0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+\\)" nil t
)
1256 (gnus-message 9 "Blackhole search found host IP %s." (match-string 1))
1257 (push (spam-reverse-ip-string (match-string 1))
1259 (dolist (server spam-blackhole-servers
)
1261 (unless (and spam-blackhole-good-server-regex
1262 ;; match the good-server-regex against the reversed (again) IP string
1264 spam-blackhole-good-server-regex
1265 (spam-reverse-ip-string ip
)))
1267 (let ((query-string (concat ip
"." server
)))
1269 (let ((query-result (query-dig query-string
)))
1271 (gnus-message 5 "(DIG): positive blackhole check '%s'"
1273 (push (list ip server query-result
)
1275 ;; else, if not using dig.el
1276 (when (query-dns query-string
)
1277 (gnus-message 5 "positive blackhole check")
1278 (push (list ip server
(query-dns query-string
'TXT
))
1286 (autoload 'mail-check-payment
"hashcash"))
1292 (defun spam-check-hashcash ()
1293 "Check the headers for hashcash payments."
1294 (mail-check-payment))) ;mail-check-payment returns a boolean
1300 ;;; original idea for spam-check-BBDB from Alexander Kotelnikov
1301 ;;; <sacha@giotto.sj.ru>
1303 ;; all this is done inside a condition-case to trap errors
1306 (autoload 'bbdb-buffer
"bbdb")
1307 (autoload 'bbdb-create-internal
"bbdb")
1308 (autoload 'bbdb-search-simple
"bbdb"))
1311 (when (condition-case nil
1314 (require 'bbdb-com
))
1316 (defalias 'spam-BBDB-register-routine
'ignore
)
1317 (defalias 'spam-enter-ham-BBDB
'ignore
)
1320 (defun spam-enter-ham-BBDB (addresses &optional remove
)
1321 "Enter an address into the BBDB; implies ham (non-spam) sender"
1322 (dolist (from addresses
)
1323 (when (stringp from
)
1324 (let* ((parsed-address (gnus-extract-address-components from
))
1325 (name (or (nth 0 parsed-address
) "Ham Sender"))
1326 (remove-function (if remove
1327 'bbdb-delete-record-internal
1329 (net-address (nth 1 parsed-address
))
1330 (record (and net-address
1331 (bbdb-search-simple nil net-address
))))
1333 (gnus-message 5 "%s address %s %s BBDB"
1334 (if remove
"Deleting" "Adding")
1336 (if remove
"from" "to"))
1338 (funcall remove-function record
)
1339 (bbdb-create-internal name nil net-address nil nil
1340 "ham sender added by spam.el")))))))
1342 (defun spam-BBDB-register-routine (articles &optional unregister
)
1344 (dolist (article articles
)
1345 (when (stringp (spam-fetch-field-from-fast article
))
1346 (push (spam-fetch-field-from-fast article
) addresses
)))
1347 ;; now do the register/unregister action
1348 (spam-enter-ham-BBDB addresses unregister
)))
1350 (defun spam-BBDB-unregister-routine (articles)
1351 (spam-BBDB-register-routine articles t
))
1353 (defun spam-check-BBDB ()
1354 "Mail from people in the BBDB is classified as ham or non-spam"
1355 (let ((who (nnmail-fetch-field "from"))
1356 (spam-split-group (if spam-split-symbolic-return
1360 (setq who
(nth 1 (gnus-extract-address-components who
)))
1361 (if (bbdb-search-simple nil who
)
1363 (if spam-use-BBDB-exclusive
1370 ;;; check the ifile backend; return nil if the mail was NOT classified
1373 (defun spam-get-ifile-database-parameter ()
1374 "Get the command-line parameter for ifile's database from
1375 spam-ifile-database-path."
1376 (if spam-ifile-database-path
1377 (format "--db-file=%s" spam-ifile-database-path
)
1380 (defun spam-check-ifile ()
1381 "Check the ifile backend for the classification of this message."
1382 (let ((article-buffer-name (buffer-name))
1383 (spam-split-group (if spam-split-symbolic-return
1388 (let ((temp-buffer-name (buffer-name))
1389 (db-param (spam-get-ifile-database-parameter)))
1391 (set-buffer article-buffer-name
)
1392 (apply 'call-process-region
1393 (point-min) (point-max) spam-ifile-path
1394 nil temp-buffer-name nil
"-c"
1395 (if db-param
`(,db-param
"-q") `("-q"))))
1396 ;; check the return now (we're back in the temp buffer)
1397 (goto-char (point-min))
1399 (setq category
(buffer-substring (point) (spam-point-at-eol))))
1400 (when (not (zerop (length category
))) ; we need a category here
1401 (if spam-ifile-all-categories
1402 (setq return category
)
1403 ;; else, if spam-ifile-all-categories is not set...
1404 (when (string-equal spam-ifile-spam-category category
)
1405 (setq return spam-split-group
)))))) ; note return is nil otherwise
1408 (defun spam-ifile-register-with-ifile (articles category
&optional unregister
)
1409 "Register an article, given as a string, with a category.
1410 Uses `gnus-newsgroup-name' if category is nil (for ham registration)."
1411 (let ((category (or category gnus-newsgroup-name
))
1412 (add-or-delete-option (if unregister
"-d" "-i"))
1413 (db (spam-get-ifile-database-parameter))
1416 (dolist (article articles
)
1417 (let ((article-string (spam-get-article-as-string article
)))
1418 (when (stringp article-string
)
1419 (insert article-string
))))
1420 (apply 'call-process-region
1421 (point-min) (point-max) spam-ifile-path
1423 add-or-delete-option category
1424 (if db
`(,db
"-h") `("-h"))))))
1426 (defun spam-ifile-register-spam-routine (articles &optional unregister
)
1427 (spam-ifile-register-with-ifile articles spam-ifile-spam-category unregister
))
1429 (defun spam-ifile-unregister-spam-routine (articles)
1430 (spam-ifile-register-spam-routine articles t
))
1432 (defun spam-ifile-register-ham-routine (articles &optional unregister
)
1433 (spam-ifile-register-with-ifile articles spam-ifile-ham-category unregister
))
1435 (defun spam-ifile-unregister-ham-routine (articles)
1436 (spam-ifile-register-ham-routine articles t
))
1442 (autoload 'spam-stat-buffer-change-to-non-spam
"spam-stat")
1443 (autoload 'spam-stat-buffer-change-to-spam
"spam-stat")
1444 (autoload 'spam-stat-buffer-is-non-spam
"spam-stat")
1445 (autoload 'spam-stat-buffer-is-spam
"spam-stat")
1446 (autoload 'spam-stat-load
"spam-stat")
1447 (autoload 'spam-stat-save
"spam-stat")
1448 (autoload 'spam-stat-split-fancy
"spam-stat"))
1451 (when (condition-case nil
1452 (let ((spam-stat-install-hooks nil
))
1453 (require 'spam-stat
))
1455 (defalias 'spam-stat-register-ham-routine
'ignore
)
1456 (defalias 'spam-stat-register-spam-routine
'ignore
)
1459 (defun spam-check-stat ()
1460 "Check the spam-stat backend for the classification of this message"
1461 (let ((spam-split-group (if spam-split-symbolic-return
1464 (spam-stat-split-fancy-spam-group spam-split-group
) ; override
1465 (spam-stat-buffer (buffer-name)) ; stat the current buffer
1467 (spam-stat-split-fancy)))
1469 (defun spam-stat-register-spam-routine (articles &optional unregister
)
1470 (dolist (article articles
)
1471 (let ((article-string (spam-get-article-as-string article
)))
1473 (insert article-string
)
1475 (spam-stat-buffer-change-to-non-spam)
1476 (spam-stat-buffer-is-spam))))))
1478 (defun spam-stat-unregister-spam-routine (articles)
1479 (spam-stat-register-spam-routine articles t
))
1481 (defun spam-stat-register-ham-routine (articles &optional unregister
)
1482 (dolist (article articles
)
1483 (let ((article-string (spam-get-article-as-string article
)))
1485 (insert article-string
)
1487 (spam-stat-buffer-change-to-spam)
1488 (spam-stat-buffer-is-non-spam))))))
1490 (defun spam-stat-unregister-ham-routine (articles)
1491 (spam-stat-register-ham-routine articles t
))
1493 (defun spam-maybe-spam-stat-load ()
1494 (when spam-use-stat
(spam-stat-load)))
1496 (defun spam-maybe-spam-stat-save ()
1497 (when spam-use-stat
(spam-stat-save)))))
1501 ;;;; Blacklists and whitelists.
1503 (defvar spam-whitelist-cache nil
)
1504 (defvar spam-blacklist-cache nil
)
1506 (defun spam-kill-whole-line ()
1508 (let ((kill-whole-line t
))
1511 ;;; address can be a list, too
1512 (defun spam-enter-whitelist (address &optional remove
)
1513 "Enter ADDRESS (list or single) into the whitelist.
1514 With a non-nil REMOVE, remove them."
1515 (interactive "sAddress: ")
1516 (spam-enter-list address spam-whitelist remove
)
1517 (setq spam-whitelist-cache nil
))
1519 ;;; address can be a list, too
1520 (defun spam-enter-blacklist (address &optional remove
)
1521 "Enter ADDRESS (list or single) into the blacklist.
1522 With a non-nil REMOVE, remove them."
1523 (interactive "sAddress: ")
1524 (spam-enter-list address spam-blacklist remove
)
1525 (setq spam-blacklist-cache nil
))
1527 (defun spam-enter-list (addresses file
&optional remove
)
1528 "Enter ADDRESSES into the given FILE.
1529 Either the whitelist or the blacklist files can be used. With
1530 REMOVE not nil, remove the ADDRESSES."
1531 (if (stringp addresses
)
1532 (spam-enter-list (list addresses
) file remove
)
1533 ;; else, we have a list of addresses here
1534 (unless (file-exists-p (file-name-directory file
))
1535 (make-directory (file-name-directory file
) t
))
1538 (find-file-noselect file
))
1539 (dolist (a addresses
)
1541 (goto-char (point-min))
1542 (if (re-search-forward (regexp-quote a
) nil t
)
1543 ;; found the address
1545 (spam-kill-whole-line))
1546 ;; else, the address was not found
1548 (goto-char (point-max))
1554 ;;; returns t if the sender is in the whitelist, nil or
1555 ;;; spam-split-group otherwise
1556 (defun spam-check-whitelist ()
1557 ;; FIXME! Should it detect when file timestamps change?
1558 (let ((spam-split-group (if spam-split-symbolic-return
1561 (unless spam-whitelist-cache
1562 (setq spam-whitelist-cache
(spam-parse-list spam-whitelist
)))
1563 (if (spam-from-listed-p spam-whitelist-cache
)
1565 (if spam-use-whitelist-exclusive
1569 (defun spam-check-blacklist ()
1570 ;; FIXME! Should it detect when file timestamps change?
1571 (let ((spam-split-group (if spam-split-symbolic-return
1574 (unless spam-blacklist-cache
1575 (setq spam-blacklist-cache
(spam-parse-list spam-blacklist
)))
1576 (and (spam-from-listed-p spam-blacklist-cache
) spam-split-group
)))
1578 (defun spam-parse-list (file)
1579 (when (file-readable-p file
)
1580 (let (contents address
)
1582 (insert-file-contents file
)
1584 (setq address
(buffer-substring (point) (spam-point-at-eol)))
1586 ;; insert the e-mail address if detected, otherwise the raw data
1587 (unless (zerop (length address
))
1588 (let ((pure-address (nth 1 (gnus-extract-address-components address
))))
1589 (push (or pure-address address
) contents
)))))
1590 (nreverse contents
))))
1592 (defun spam-from-listed-p (cache)
1593 (let ((from (nnmail-fetch-field "from"))
1596 (let ((address (pop cache
)))
1597 (unless (zerop (length address
)) ; 0 for a nil address too
1598 (setq address
(regexp-quote address
))
1599 ;; fix regexp-quote's treatment of user-intended regexes
1600 (while (string-match "\\\\\\*" address
)
1601 (setq address
(replace-match ".*" t t address
))))
1602 (when (and address
(string-match address from
))
1607 (defun spam-filelist-register-routine (articles blacklist
&optional unregister
)
1608 (let ((de-symbol (if blacklist
'spam-use-whitelist
'spam-use-blacklist
))
1609 (declassification (if blacklist
'ham
'spam
))
1611 (if blacklist
'spam-enter-blacklist
'spam-enter-whitelist
))
1613 (if blacklist
'spam-enter-whitelist
'spam-enter-blacklist
))
1614 from addresses unregister-list
)
1615 (dolist (article articles
)
1616 (let ((from (spam-fetch-field-from-fast article
))
1617 (id (spam-fetch-field-message-id-fast article
))
1619 (when (stringp from
)
1620 (dolist (ignore-regex spam-blacklist-ignored-regexes
)
1621 (when (and (not sender-ignored
)
1622 (stringp ignore-regex
)
1623 (string-match ignore-regex from
))
1624 (setq sender-ignored t
)))
1625 ;; remember the messages we need to unregister, unless remove is set
1628 (spam-log-unregistration-needed-p
1629 id
'process declassification de-symbol
))
1630 (push from unregister-list
))
1631 (unless sender-ignored
1632 (push from addresses
)))))
1635 (funcall enter-function addresses t
) ; unregister all these addresses
1636 ;; else, register normally and unregister what we need to
1637 (funcall remove-function unregister-list t
)
1638 (dolist (article unregister-list
)
1639 (spam-log-undo-registration
1640 (spam-fetch-field-message-id-fast article
)
1644 (funcall enter-function addresses nil
))))
1646 (defun spam-blacklist-unregister-routine (articles)
1647 (spam-blacklist-register-routine articles t
))
1649 (defun spam-blacklist-register-routine (articles &optional unregister
)
1650 (spam-filelist-register-routine articles t unregister
))
1652 (defun spam-whitelist-unregister-routine (articles)
1653 (spam-whitelist-register-routine articles t
))
1655 (defun spam-whitelist-register-routine (articles &optional unregister
)
1656 (spam-filelist-register-routine articles nil unregister
))
1659 ;;;; Spam-report glue
1660 (defun spam-report-gmane-register-routine (articles)
1662 (apply 'spam-report-gmane articles
)))
1666 (defun spam-check-bogofilter-headers (&optional score
)
1667 (let ((header (nnmail-fetch-field spam-bogofilter-header
))
1668 (spam-split-group (if spam-split-symbolic-return
1671 (when header
; return nil when no header
1672 (if score
; scoring mode
1673 (if (string-match "spamicity=\\([0-9.]+\\)" header
)
1674 (match-string 1 header
)
1676 ;; spam detection mode
1677 (when (string-match spam-bogofilter-bogosity-positive-spam-header
1679 spam-split-group
)))))
1681 ;; return something sensible if the score can't be determined
1682 (defun spam-bogofilter-score ()
1683 "Get the Bogofilter spamicity score"
1685 (save-window-excursion
1686 (gnus-summary-show-article t
)
1687 (set-buffer gnus-article-buffer
)
1688 (let ((score (or (spam-check-bogofilter-headers t
)
1689 (spam-check-bogofilter t
))))
1690 (message "Spamicity score %s" score
)
1692 (gnus-summary-show-article)))
1694 (defun spam-check-bogofilter (&optional score
)
1695 "Check the Bogofilter backend for the classification of this message"
1696 (let ((article-buffer-name (buffer-name))
1697 (db spam-bogofilter-database-directory
)
1700 (let ((temp-buffer-name (buffer-name)))
1702 (set-buffer article-buffer-name
)
1703 (apply 'call-process-region
1704 (point-min) (point-max)
1705 spam-bogofilter-path
1706 nil temp-buffer-name nil
1707 (if db
`("-d" ,db
"-v") `("-v"))))
1708 (setq return
(spam-check-bogofilter-headers score
))))
1711 (defun spam-bogofilter-register-with-bogofilter (articles
1713 &optional unregister
)
1714 "Register an article, given as a string, as spam or non-spam."
1715 (dolist (article articles
)
1716 (let ((article-string (spam-get-article-as-string article
))
1717 (db spam-bogofilter-database-directory
)
1718 (switch (if unregister
1720 spam-bogofilter-spam-strong-switch
1721 spam-bogofilter-ham-strong-switch
)
1723 spam-bogofilter-spam-switch
1724 spam-bogofilter-ham-switch
))))
1725 (when (stringp article-string
)
1727 (insert article-string
)
1729 (apply 'call-process-region
1730 (point-min) (point-max)
1731 spam-bogofilter-path
1733 (if db
`("-d" ,db
"-v") `("-v"))))))))
1735 (defun spam-bogofilter-register-spam-routine (articles &optional unregister
)
1736 (spam-bogofilter-register-with-bogofilter articles t unregister
))
1738 (defun spam-bogofilter-unregister-spam-routine (articles)
1739 (spam-bogofilter-register-spam-routine articles t
))
1741 (defun spam-bogofilter-register-ham-routine (articles &optional unregister
)
1742 (spam-bogofilter-register-with-bogofilter articles nil unregister
))
1744 (defun spam-bogofilter-unregister-ham-routine (articles)
1745 (spam-bogofilter-register-ham-routine articles t
))
1750 (defun spam-check-spamoracle ()
1751 "Run spamoracle on an article to determine whether it's spam."
1752 (let ((article-buffer-name (buffer-name))
1753 (spam-split-group (if spam-split-symbolic-return
1757 (let ((temp-buffer-name (buffer-name)))
1759 (set-buffer article-buffer-name
)
1761 (apply 'call-process-region
1762 (point-min) (point-max)
1763 spam-spamoracle-binary
1764 nil temp-buffer-name nil
1765 (if spam-spamoracle-database
1766 `("-f" ,spam-spamoracle-database
"mark")
1770 (set-buffer temp-buffer-name
)
1771 (goto-char (point-min))
1772 (when (re-search-forward "^X-Spam: yes;" nil t
)
1774 (error "Error running spamoracle: %s" status
))))))))
1776 (defun spam-spamoracle-learn (articles article-is-spam-p
&optional unregister
)
1777 "Run spamoracle in training mode."
1779 (let ((temp-buffer-name (buffer-name)))
1781 (goto-char (point-min))
1782 (dolist (article articles
)
1783 (insert (spam-get-article-as-string article
)))
1784 (let* ((arg (if (spam-xor unregister article-is-spam-p
)
1788 (apply 'call-process-region
1789 (point-min) (point-max)
1790 spam-spamoracle-binary
1791 nil temp-buffer-name nil
1792 (if spam-spamoracle-database
1793 `("-f" ,spam-spamoracle-database
1796 (unless (eq 0 status
)
1797 (error "Error running spamoracle: %s" status
)))))))
1799 (defun spam-spamoracle-learn-ham (articles &optional unregister
)
1800 (spam-spamoracle-learn articles nil unregister
))
1802 (defun spam-spamoracle-unlearn-ham (articles &optional unregister
)
1803 (spam-spamoracle-learn-ham articles t
))
1805 (defun spam-spamoracle-learn-spam (articles &optional unregister
)
1806 (spam-spamoracle-learn articles t unregister
))
1808 (defun spam-spamoracle-unlearn-spam (articles &optional unregister
)
1809 (spam-spamoracle-learn-spam articles t
))
1815 (defun spam-initialize ()
1816 "Install the spam.el hooks and do other initialization"
1818 (setq spam-install-hooks t
)
1819 ;; TODO: How do we redo this every time the `spam' face is customized?
1820 (push '((eq mark gnus-spam-mark
) . spam
)
1821 gnus-summary-highlight
)
1822 ;; Add hooks for loading and saving the spam stats
1823 (add-hook 'gnus-save-newsrc-hook
'spam-maybe-spam-stat-save
)
1824 (add-hook 'gnus-get-top-new-news-hook
'spam-maybe-spam-stat-load
)
1825 (add-hook 'gnus-startup-hook
'spam-maybe-spam-stat-load
)
1826 (add-hook 'gnus-summary-prepare-exit-hook
'spam-summary-prepare-exit
)
1827 (add-hook 'gnus-summary-prepare-hook
'spam-summary-prepare
)
1828 (add-hook 'gnus-get-new-news-hook
'spam-setup-widening
)
1829 (add-hook 'gnus-summary-prepare-hook
'spam-find-spam
))
1831 (defun spam-unload-hook ()
1832 "Uninstall the spam.el hooks"
1834 (remove-hook 'gnus-save-newsrc-hook
'spam-maybe-spam-stat-save
)
1835 (remove-hook 'gnus-get-top-new-news-hook
'spam-maybe-spam-stat-load
)
1836 (remove-hook 'gnus-startup-hook
'spam-maybe-spam-stat-load
)
1837 (remove-hook 'gnus-summary-prepare-exit-hook
'spam-summary-prepare-exit
)
1838 (remove-hook 'gnus-summary-prepare-hook
'spam-summary-prepare
)
1839 (remove-hook 'gnus-get-new-news-hook
'spam-setup-widening
)
1840 (remove-hook 'gnus-summary-prepare-hook
'spam-find-spam
))
1842 (add-hook 'spam-unload-hook
'spam-unload-hook
)
1844 (when spam-install-hooks
1849 ;;; arch-tag: 07e6e0ca-ab0a-4412-b445-1f6c72a4f27f
1850 ;;; spam.el ends here