1 ;;; spam.el --- Identifying spam
3 ;; Copyright (C) 2002, 2003, 2004, 2005 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-log-to-registry nil
104 "Whether spam/ham processing should be logged in the registry."
108 (defcustom spam-split-symbolic-return nil
109 "Whether `spam-split' should work with symbols or group names."
113 (defcustom spam-split-symbolic-return-positive nil
114 "Whether `spam-split' should ALWAYS work with symbols or group names.
115 Do not set this if you use `spam-split' in a fancy split
120 (defcustom spam-process-ham-in-spam-groups nil
121 "Whether ham should be processed in spam groups."
125 (defcustom spam-mark-only-unseen-as-spam t
126 "Whether only unseen articles should be marked as spam in spam groups.
127 When nil, all unread articles in a spam group are marked as
128 spam. Set this if you want to leave an article unread in a spam group
129 without losing it to the automatic spam-marking process."
133 (defcustom spam-mark-ham-unread-before-move-from-spam-group nil
134 "Whether ham should be marked unread before it's moved.
135 The article is moved out of a spam group according to ham-process-destination.
136 This variable is an official entry in the international Longest Variable Name
141 (defcustom spam-disable-spam-split-during-ham-respool nil
142 "Whether `spam-split' should be ignored while resplitting ham in a process
143 destination. This is useful to prevent ham from ending up in the same spam
144 group after the resplit. Don't set this to t if you have spam-split as the
145 last rule in your split configuration."
149 (defcustom spam-autodetect-recheck-messages nil
150 "Should spam.el recheck all meessages when autodetecting?
151 Normally this is nil, so only unseen messages will be checked."
155 (defcustom spam-whitelist
(expand-file-name "whitelist" spam-directory
)
156 "The location of the whitelist.
157 The file format is one regular expression per line.
158 The regular expression is matched against the address."
162 (defcustom spam-blacklist
(expand-file-name "blacklist" spam-directory
)
163 "The location of the blacklist.
164 The file format is one regular expression per line.
165 The regular expression is matched against the address."
169 (defcustom spam-use-dig t
170 "Whether `query-dig' should be used instead of `query-dns'."
174 (defcustom spam-use-blacklist nil
175 "Whether the blacklist should be used by `spam-split'."
179 (defcustom spam-blacklist-ignored-regexes nil
180 "Regular expressions that the blacklist should ignore."
181 :type
'(repeat (regexp :tag
"Regular expression to ignore when blacklisting"))
184 (defcustom spam-use-whitelist nil
185 "Whether the whitelist should be used by `spam-split'."
189 (defcustom spam-use-whitelist-exclusive nil
190 "Whether whitelist-exclusive should be used by `spam-split'.
191 Exclusive whitelisting means that all messages from senders not in the whitelist
192 are considered spam."
196 (defcustom spam-use-blackholes nil
197 "Whether blackholes should be used by `spam-split'."
201 (defcustom spam-use-hashcash nil
202 "Whether hashcash payments should be detected by `spam-split'."
206 (defcustom spam-use-regex-headers nil
207 "Whether a header regular expression match should be used by `spam-split'.
208 Also see the variables `spam-regex-headers-spam' and `spam-regex-headers-ham'."
212 (defcustom spam-use-regex-body nil
213 "Whether a body regular expression match should be used by `spam-split'.
214 Also see the variables `spam-regex-body-spam' and `spam-regex-body-ham'."
218 (defcustom spam-use-bogofilter-headers nil
219 "Whether bogofilter headers should be used by `spam-split'.
220 Enable this if you pre-process messages with Bogofilter BEFORE Gnus sees them."
224 (defcustom spam-use-bogofilter nil
225 "Whether bogofilter should be invoked by `spam-split'.
226 Enable this if you want Gnus to invoke Bogofilter on new messages."
230 (defcustom spam-use-BBDB nil
231 "Whether BBDB should be used by `spam-split'."
235 (defcustom spam-use-BBDB-exclusive nil
236 "Whether BBDB-exclusive should be used by `spam-split'.
237 Exclusive BBDB means that all messages from senders not in the BBDB are
242 (defcustom spam-use-ifile nil
243 "Whether ifile should be used by `spam-split'."
247 (defcustom spam-use-stat nil
248 "Whether `spam-stat' should be used by `spam-split'."
252 (defcustom spam-use-spamoracle nil
253 "Whether spamoracle should be used by `spam-split'."
257 (defcustom spam-install-hooks
(or
261 spam-use-whitelist-exclusive
264 spam-use-regex-headers
266 spam-use-bogofilter-headers
269 spam-use-BBDB-exclusive
273 "Whether the spam hooks should be installed.
274 Default to t if one of the spam-use-* variables is set."
278 (defcustom spam-split-group
"spam"
279 "Group name where incoming spam should be put by `spam-split'."
283 ;;; TODO: deprecate this variable, it's confusing since it's a list of strings,
284 ;;; not regular expressions
285 (defcustom spam-junk-mailgroups
(cons
287 '("mail.junk" "poste.pourriel"))
288 "Mailgroups with spam contents.
289 All unmarked article in such group receive the spam mark on group entry."
290 :type
'(repeat (string :tag
"Group"))
293 (defcustom spam-blackhole-servers
'("bl.spamcop.net" "relays.ordb.org"
294 "dev.null.dk" "relays.visi.com")
295 "List of blackhole servers."
296 :type
'(repeat (string :tag
"Server"))
299 (defcustom spam-blackhole-good-server-regex nil
300 "String matching IP addresses that should not be checked in the blackholes."
301 :type
'(radio (const nil
) regexp
)
305 '((((class color
) (type tty
) (background dark
))
306 (:foreground
"gray80" :background
"gray50"))
307 (((class color
) (type tty
) (background light
))
308 (:foreground
"gray50" :background
"gray80"))
309 (((class color
) (background dark
))
310 (:foreground
"ivory2"))
311 (((class color
) (background light
))
312 (:foreground
"ivory4"))
313 (t :inverse-video t
))
314 "Face for spam-marked articles."
316 ;; backward-compatibility alias
317 (put 'spam-face
'face-alias
'spam
)
319 (defcustom spam-face
'spam
320 "Face for spam-marked articles."
324 (defcustom spam-regex-headers-spam
'("^X-Spam-Flag: YES")
325 "Regular expression for positive header spam matches."
326 :type
'(repeat (regexp :tag
"Regular expression to match spam header"))
329 (defcustom spam-regex-headers-ham
'("^X-Spam-Flag: NO")
330 "Regular expression for positive header ham matches."
331 :type
'(repeat (regexp :tag
"Regular expression to match ham header"))
334 (defcustom spam-regex-body-spam
'()
335 "Regular expression for positive body spam matches."
336 :type
'(repeat (regexp :tag
"Regular expression to match spam body"))
339 (defcustom spam-regex-body-ham
'()
340 "Regular expression for positive body ham matches."
341 :type
'(repeat (regexp :tag
"Regular expression to match ham body"))
344 (defgroup spam-ifile nil
345 "Spam ifile configuration."
348 (defcustom spam-ifile-path
(executable-find "ifile")
349 "File path of the ifile executable program."
350 :type
'(choice (file :tag
"Location of ifile")
351 (const :tag
"ifile is not installed"))
354 (defcustom spam-ifile-database-path nil
355 "File path of the ifile database."
356 :type
'(choice (file :tag
"Location of the ifile database")
357 (const :tag
"Use the default"))
360 (defcustom spam-ifile-spam-category
"spam"
361 "Name of the spam ifile category."
365 (defcustom spam-ifile-ham-category nil
366 "Name of the ham ifile category.
367 If nil, the current group name will be used."
368 :type
'(choice (string :tag
"Use a fixed category")
369 (const :tag
"Use the current group name"))
372 (defcustom spam-ifile-all-categories nil
373 "Whether the ifile check will return all categories, or just spam.
374 Set this to t if you want to use the `spam-split' invocation of ifile as
375 your main source of newsgroup names."
379 (defgroup spam-bogofilter nil
380 "Spam bogofilter configuration."
383 (defcustom spam-bogofilter-path
(executable-find "bogofilter")
384 "File path of the Bogofilter executable program."
385 :type
'(choice (file :tag
"Location of bogofilter")
386 (const :tag
"Bogofilter is not installed"))
387 :group
'spam-bogofilter
)
389 (defcustom spam-bogofilter-header
"X-Bogosity"
390 "The header that Bogofilter inserts in messages."
392 :group
'spam-bogofilter
)
394 (defcustom spam-bogofilter-spam-switch
"-s"
395 "The switch that Bogofilter uses to register spam messages."
397 :group
'spam-bogofilter
)
399 (defcustom spam-bogofilter-ham-switch
"-n"
400 "The switch that Bogofilter uses to register ham messages."
402 :group
'spam-bogofilter
)
404 (defcustom spam-bogofilter-spam-strong-switch
"-S"
405 "The switch that Bogofilter uses to unregister ham messages."
407 :group
'spam-bogofilter
)
409 (defcustom spam-bogofilter-ham-strong-switch
"-N"
410 "The switch that Bogofilter uses to unregister spam messages."
412 :group
'spam-bogofilter
)
414 (defcustom spam-bogofilter-bogosity-positive-spam-header
"^\\(Yes\\|Spam\\)"
415 "The regex on `spam-bogofilter-header' for positive spam identification."
417 :group
'spam-bogofilter
)
419 (defcustom spam-bogofilter-database-directory nil
420 "Directory path of the Bogofilter databases."
421 :type
'(choice (directory
422 :tag
"Location of the Bogofilter database directory")
423 (const :tag
"Use the default"))
424 :group
'spam-bogofilter
)
426 (defgroup spam-spamoracle nil
427 "Spam spamoracle configuration."
430 (defcustom spam-spamoracle-database nil
431 "Location of spamoracle database file. When nil, use the default
432 spamoracle database."
433 :type
'(choice (directory :tag
"Location of spamoracle database file.")
434 (const :tag
"Use the default"))
435 :group
'spam-spamoracle
)
437 (defcustom spam-spamoracle-binary
(executable-find "spamoracle")
438 "Location of the spamoracle binary."
439 :type
'(choice (directory :tag
"Location of the spamoracle binary")
440 (const :tag
"Use the default"))
441 :group
'spam-spamoracle
)
443 ;;; Key bindings for spam control.
445 (gnus-define-keys gnus-summary-mode-map
446 "St" spam-bogofilter-score
447 "Sx" gnus-summary-mark-as-spam
448 "Mst" spam-bogofilter-score
449 "Msx" gnus-summary-mark-as-spam
450 "\M-d" gnus-summary-mark-as-spam
)
452 (defvar spam-old-ham-articles nil
453 "List of old ham articles, generated when a group is entered.")
455 (defvar spam-old-spam-articles nil
456 "List of old spam articles, generated when a group is entered.")
458 (defvar spam-split-disabled nil
459 "If non-nil, `spam-split' is disabled, and always returns nil.")
461 (defvar spam-split-last-successful-check nil
462 "`spam-split' will set this to nil or a spam-use-XYZ check if it
465 ;; convenience functions
466 (defun spam-xor (a b
)
467 "Logical exclusive `or'."
468 (and (or a b
) (not (and a b
))))
470 (defun spam-group-ham-mark-p (group mark
&optional spam
)
471 (when (stringp group
)
472 (let* ((marks (spam-group-ham-marks group spam
))
473 (marks (if (symbolp mark
)
475 (mapcar 'symbol-value marks
))))
478 (defun spam-group-spam-mark-p (group mark
)
479 (spam-group-ham-mark-p group mark t
))
481 (defun spam-group-ham-marks (group &optional spam
)
482 (when (stringp group
)
483 (let* ((marks (if spam
484 (gnus-parameter-spam-marks group
)
485 (gnus-parameter-ham-marks group
)))
487 (marks (if (listp (car marks
)) (car marks
) marks
)))
490 (defun spam-group-spam-marks (group)
491 (spam-group-ham-marks group t
))
493 (defun spam-group-spam-contents-p (group)
495 (or (member group spam-junk-mailgroups
)
496 (memq 'gnus-group-spam-classification-spam
497 (gnus-parameter-spam-contents group
)))
500 (defun spam-group-ham-contents-p (group)
502 (memq 'gnus-group-spam-classification-ham
503 (gnus-parameter-spam-contents group
))
506 (defvar spam-list-of-processors
507 '((gnus-group-spam-exit-processor-report-gmane spam spam-use-gmane
)
508 (gnus-group-spam-exit-processor-bogofilter spam spam-use-bogofilter
)
509 (gnus-group-spam-exit-processor-blacklist spam spam-use-blacklist
)
510 (gnus-group-spam-exit-processor-ifile spam spam-use-ifile
)
511 (gnus-group-spam-exit-processor-stat spam spam-use-stat
)
512 (gnus-group-spam-exit-processor-spamoracle spam spam-use-spamoracle
)
513 (gnus-group-ham-exit-processor-ifile ham spam-use-ifile
)
514 (gnus-group-ham-exit-processor-bogofilter ham spam-use-bogofilter
)
515 (gnus-group-ham-exit-processor-stat ham spam-use-stat
)
516 (gnus-group-ham-exit-processor-whitelist ham spam-use-whitelist
)
517 (gnus-group-ham-exit-processor-BBDB ham spam-use-BBDB
)
518 (gnus-group-ham-exit-processor-copy ham spam-use-ham-copy
)
519 (gnus-group-ham-exit-processor-spamoracle ham spam-use-spamoracle
))
520 "The spam-list-of-processors list contains pairs associating a
521 ham/spam exit processor variable with a classification and a
522 spam-use-* variable.")
524 (defun spam-group-processor-p (group processor
)
525 (if (and (stringp group
)
527 (or (member processor
(nth 0 (gnus-parameter-spam-process group
)))
528 (spam-group-processor-multiple-p
530 (cdr-safe (assoc processor spam-list-of-processors
))))
533 (defun spam-group-processor-multiple-p (group processor-info
)
534 (let* ((classification (nth 0 processor-info
))
535 (check (nth 1 processor-info
))
536 (parameters (nth 0 (gnus-parameter-spam-process group
)))
538 (dolist (parameter parameters
)
539 (when (and (null found
)
541 (eq classification
(nth 0 parameter
))
542 (eq check
(nth 1 parameter
)))
546 (defun spam-group-spam-processor-report-gmane-p (group)
547 (spam-group-processor-p group
'gnus-group-spam-exit-processor-report-gmane
))
549 (defun spam-group-spam-processor-bogofilter-p (group)
550 (spam-group-processor-p group
'gnus-group-spam-exit-processor-bogofilter
))
552 (defun spam-group-spam-processor-blacklist-p (group)
553 (spam-group-processor-p group
'gnus-group-spam-exit-processor-blacklist
))
555 (defun spam-group-spam-processor-ifile-p (group)
556 (spam-group-processor-p group
'gnus-group-spam-exit-processor-ifile
))
558 (defun spam-group-ham-processor-ifile-p (group)
559 (spam-group-processor-p group
'gnus-group-ham-exit-processor-ifile
))
561 (defun spam-group-spam-processor-spamoracle-p (group)
562 (spam-group-processor-p group
'gnus-group-spam-exit-processor-spamoracle
))
564 (defun spam-group-ham-processor-bogofilter-p (group)
565 (spam-group-processor-p group
'gnus-group-ham-exit-processor-bogofilter
))
567 (defun spam-group-spam-processor-stat-p (group)
568 (spam-group-processor-p group
'gnus-group-spam-exit-processor-stat
))
570 (defun spam-group-ham-processor-stat-p (group)
571 (spam-group-processor-p group
'gnus-group-ham-exit-processor-stat
))
573 (defun spam-group-ham-processor-whitelist-p (group)
574 (spam-group-processor-p group
'gnus-group-ham-exit-processor-whitelist
))
576 (defun spam-group-ham-processor-BBDB-p (group)
577 (spam-group-processor-p group
'gnus-group-ham-exit-processor-BBDB
))
579 (defun spam-group-ham-processor-copy-p (group)
580 (spam-group-processor-p group
'gnus-group-ham-exit-processor-copy
))
582 (defun spam-group-ham-processor-spamoracle-p (group)
583 (spam-group-processor-p group
'gnus-group-ham-exit-processor-spamoracle
))
585 ;;; Summary entry and exit processing.
587 (defun spam-summary-prepare ()
588 (setq spam-old-ham-articles
589 (spam-list-articles gnus-newsgroup-articles
'ham
))
590 (setq spam-old-spam-articles
591 (spam-list-articles gnus-newsgroup-articles
'spam
))
592 (spam-mark-junk-as-spam-routine))
594 ;; The spam processors are invoked for any group, spam or ham or neither
595 (defun spam-summary-prepare-exit ()
596 (unless gnus-group-is-exiting-without-update-p
597 (gnus-message 6 "Exiting summary buffer and applying spam rules")
599 ;; first of all, unregister any articles that are no longer ham or spam
600 ;; we have to iterate over the processors, or else we'll be too slow
601 (dolist (classification '(spam ham
))
602 (let* ((old-articles (if (eq classification
'spam
)
603 spam-old-spam-articles
604 spam-old-ham-articles
))
605 (new-articles (spam-list-articles
606 gnus-newsgroup-articles
608 (changed-articles (gnus-set-difference old-articles new-articles
)))
609 ;; now that we have the changed articles, we go through the processors
610 (dolist (processor-param spam-list-of-processors
)
611 (let ((processor (nth 0 processor-param
))
612 (processor-classification (nth 1 processor-param
))
613 (check (nth 2 processor-param
))
615 (dolist (article changed-articles
)
616 (let ((id (spam-fetch-field-message-id-fast article
)))
617 (when (spam-log-unregistration-needed-p
618 id
'process classification check
)
619 (push article unregister-list
))))
620 ;; call spam-register-routine with specific articles to unregister,
621 ;; when there are articles to unregister and the check is enabled
622 (when (and unregister-list
(symbol-value check
))
623 (spam-register-routine classification check t unregister-list
))))))
625 ;; find all the spam processors applicable to this group
626 (dolist (processor-param spam-list-of-processors
)
627 (let ((processor (nth 0 processor-param
))
628 (classification (nth 1 processor-param
))
629 (check (nth 2 processor-param
)))
630 (when (and (eq 'spam classification
)
631 (spam-group-processor-p gnus-newsgroup-name processor
))
632 (spam-register-routine classification check
))))
634 (if spam-move-spam-nonspam-groups-only
635 (when (not (spam-group-spam-contents-p gnus-newsgroup-name
))
636 (spam-mark-spam-as-expired-and-move-routine
637 (gnus-parameter-spam-process-destination gnus-newsgroup-name
)))
638 (gnus-message 5 "Marking spam as expired and moving it to %s"
640 (spam-mark-spam-as-expired-and-move-routine
641 (gnus-parameter-spam-process-destination gnus-newsgroup-name
)))
643 ;; now we redo spam-mark-spam-as-expired-and-move-routine to only
644 ;; expire spam, in case the above did not expire them
645 (gnus-message 5 "Marking spam as expired without moving it")
646 (spam-mark-spam-as-expired-and-move-routine nil
)
648 (when (or (spam-group-ham-contents-p gnus-newsgroup-name
)
649 (and (spam-group-spam-contents-p gnus-newsgroup-name
)
650 spam-process-ham-in-spam-groups
)
651 spam-process-ham-in-nonham-groups
)
652 ;; find all the ham processors applicable to this group
653 (dolist (processor-param spam-list-of-processors
)
654 (let ((processor (nth 0 processor-param
))
655 (classification (nth 1 processor-param
))
656 (check (nth 2 processor-param
)))
657 (when (and (eq 'ham classification
)
658 (spam-group-processor-p gnus-newsgroup-name processor
))
659 (spam-register-routine classification check
)))))
661 (when (spam-group-ham-processor-copy-p gnus-newsgroup-name
)
662 (gnus-message 5 "Copying ham")
663 (spam-ham-copy-routine
664 (gnus-parameter-ham-process-destination gnus-newsgroup-name
)))
666 ;; now move all ham articles out of spam groups
667 (when (spam-group-spam-contents-p gnus-newsgroup-name
)
668 (gnus-message 5 "Moving ham messages from spam group")
669 (spam-ham-move-routine
670 (gnus-parameter-ham-process-destination gnus-newsgroup-name
))))
672 (setq spam-old-ham-articles nil
)
673 (setq spam-old-spam-articles nil
))
675 (defun spam-mark-junk-as-spam-routine ()
676 ;; check the global list of group names spam-junk-mailgroups and the
678 (when (spam-group-spam-contents-p gnus-newsgroup-name
)
679 (gnus-message 5 "Marking %s articles as spam"
680 (if spam-mark-only-unseen-as-spam
683 (let ((articles (if spam-mark-only-unseen-as-spam
684 gnus-newsgroup-unseen
685 gnus-newsgroup-unreads
)))
686 (dolist (article articles
)
687 (gnus-summary-mark-article article gnus-spam-mark
)))))
689 (defun spam-mark-spam-as-expired-and-move-routine (&rest groups
)
690 (if (and (car-safe groups
) (listp (car-safe groups
)))
691 (apply 'spam-mark-spam-as-expired-and-move-routine
(car groups
))
692 (gnus-summary-kill-process-mark)
693 (let ((articles gnus-newsgroup-articles
)
694 (backend-supports-deletions
695 (gnus-check-backend-function
696 'request-move-article gnus-newsgroup-name
))
697 article tomove deletep
)
698 (dolist (article articles
)
699 (when (eq (gnus-summary-article-mark article
) gnus-spam-mark
)
700 (gnus-summary-mark-article article gnus-expirable-mark
)
701 (push article tomove
)))
703 ;; now do the actual copies
704 (dolist (group groups
)
707 (dolist (article tomove
)
708 (gnus-summary-set-process-mark article
))
710 (if (or (not backend-supports-deletions
)
711 (> (length groups
) 1))
713 (gnus-summary-copy-article nil group
)
715 (gnus-summary-move-article nil group
)))))
717 ;; now delete the articles, if there was a copy done, and the
719 (when (and deletep backend-supports-deletions
)
720 (dolist (article tomove
)
721 (gnus-summary-set-process-mark article
))
723 (let ((gnus-novice-user nil
)) ; don't ask me if I'm sure
724 (gnus-summary-delete-article nil
))))
726 (gnus-summary-yank-process-mark))))
728 (defun spam-ham-copy-or-move-routine (copy groups
)
729 (gnus-summary-kill-process-mark)
730 (let ((todo (spam-list-articles gnus-newsgroup-articles
'ham
))
731 (backend-supports-deletions
732 (gnus-check-backend-function
733 'request-move-article gnus-newsgroup-name
))
734 (respool-method (gnus-find-method-for-group gnus-newsgroup-name
))
735 article mark todo deletep respool
)
737 (when (member 'respool groups
)
738 (setq respool t
) ; boolean for later
739 (setq groups
'("fake"))) ; when respooling, groups are dynamic so fake it
741 ;; now do the actual move
742 (dolist (group groups
)
743 (when (and todo
(stringp group
))
744 (dolist (article todo
)
745 (when spam-mark-ham-unread-before-move-from-spam-group
746 (gnus-summary-mark-article article gnus-unread-mark
))
747 (gnus-summary-set-process-mark article
))
749 (if respool
; respooling is with a "fake" group
750 (let ((spam-split-disabled
751 (or spam-split-disabled
752 spam-disable-spam-split-during-ham-respool
)))
753 (gnus-summary-respool-article nil respool-method
))
754 (if (or (not backend-supports-deletions
) ; else, we are not respooling
755 (> (length groups
) 1))
756 (progn ; if copying, copy and set deletep
757 (gnus-summary-copy-article nil group
)
759 (gnus-summary-move-article nil group
))))) ; else move articles
761 ;; now delete the articles, unless a) copy is t, and there was a copy done
762 ;; b) a move was done to a single group
763 ;; c) backend-supports-deletions is nil
765 (when (and deletep backend-supports-deletions
)
766 (dolist (article todo
)
767 (gnus-summary-set-process-mark article
))
769 (let ((gnus-novice-user nil
)) ; don't ask me if I'm sure
770 (gnus-summary-delete-article nil
))))))
772 (gnus-summary-yank-process-mark))
774 (defun spam-ham-copy-routine (&rest groups
)
775 (if (and (car-safe groups
) (listp (car-safe groups
)))
776 (apply 'spam-ham-copy-routine
(car groups
))
777 (spam-ham-copy-or-move-routine t groups
)))
779 (defun spam-ham-move-routine (&rest groups
)
780 (if (and (car-safe groups
) (listp (car-safe groups
)))
781 (apply 'spam-ham-move-routine
(car groups
))
782 (spam-ham-copy-or-move-routine nil groups
)))
785 (defalias 'spam-point-at-eol
(if (fboundp 'point-at-eol
)
787 'line-end-position
)))
789 (defun spam-get-article-as-string (article)
790 (let ((article-buffer (spam-get-article-as-buffer article
))
793 (save-window-excursion
794 (set-buffer article-buffer
)
795 (setq article-string
(buffer-string))))
798 (defun spam-get-article-as-buffer (article)
799 (let ((article-buffer))
800 (when (numberp article
)
801 (save-window-excursion
802 (gnus-summary-goto-subject article
)
803 (gnus-summary-show-article t
)
804 (setq article-buffer
(get-buffer gnus-article-buffer
))))
808 ;; (defun spam-get-article-as-filename (article)
809 ;; (let ((article-filename))
810 ;; (when (numberp article)
811 ;; (nnml-possibly-change-directory
812 ;; (gnus-group-real-name gnus-newsgroup-name))
813 ;; (setq article-filename (expand-file-name
814 ;; (int-to-string article) nnml-current-directory)))
815 ;; (if (file-exists-p article-filename)
819 (defun spam-fetch-field-from-fast (article)
820 "Fetch the `from' field quickly, using the internal gnus-data-list function"
821 (if (and (numberp article
)
822 (assoc article
(gnus-data-list nil
)))
824 (gnus-data-header (assoc article
(gnus-data-list nil
))))
827 (defun spam-fetch-field-subject-fast (article)
828 "Fetch the `subject' field quickly, using the internal
829 gnus-data-list function"
830 (if (and (numberp article
)
831 (assoc article
(gnus-data-list nil
)))
833 (gnus-data-header (assoc article
(gnus-data-list nil
))))
836 (defun spam-fetch-field-message-id-fast (article)
837 "Fetch the `Message-ID' field quickly, using the internal
838 gnus-data-list function"
839 (if (and (numberp article
)
840 (assoc article
(gnus-data-list nil
)))
841 (mail-header-message-id
842 (gnus-data-header (assoc article
(gnus-data-list nil
))))
846 ;;;; Spam determination.
848 (defvar spam-list-of-checks
849 '((spam-use-blacklist . spam-check-blacklist
)
850 (spam-use-regex-headers . spam-check-regex-headers
)
851 (spam-use-regex-body . spam-check-regex-body
)
852 (spam-use-whitelist . spam-check-whitelist
)
853 (spam-use-BBDB . spam-check-BBDB
)
854 (spam-use-ifile . spam-check-ifile
)
855 (spam-use-spamoracle . spam-check-spamoracle
)
856 (spam-use-stat . spam-check-stat
)
857 (spam-use-blackholes . spam-check-blackholes
)
858 (spam-use-hashcash . spam-check-hashcash
)
859 (spam-use-bogofilter-headers . spam-check-bogofilter-headers
)
860 (spam-use-bogofilter . spam-check-bogofilter
))
861 "The spam-list-of-checks list contains pairs associating a
862 parameter variable with a spam checking function. If the
863 parameter variable is true, then the checking function is called,
864 and its value decides what happens. Each individual check may
865 return nil, t, or a mailgroup name. The value nil means that the
866 check does not yield a decision, and so, that further checks are
867 needed. The value t means that the message is definitely not
868 spam, and that further spam checks should be inhibited.
869 Otherwise, a mailgroup name or the symbol 'spam (depending on
870 spam-split-symbolic-return) is returned where the mail should go,
871 and further checks are also inhibited. The usual mailgroup name
872 is the value of `spam-split-group', meaning that the message is
875 (defvar spam-list-of-statistical-checks
881 "The spam-list-of-statistical-checks list contains all the mail
882 splitters that need to have the full message body available.")
884 ;;;TODO: modify to invoke self with each check if invoked without specifics
885 (defun spam-split (&rest specific-checks
)
886 "Split this message into the `spam' group if it is spam.
887 This function can be used as an entry in the variable `nnmail-split-fancy',
888 for example like this: (: spam-split). It can take checks as
889 parameters. A string as a parameter will set the
890 spam-split-group to that string.
892 See the Info node `(gnus)Fancy Mail Splitting' for more details."
894 (setq spam-split-last-successful-check nil
)
895 (unless spam-split-disabled
896 (let ((spam-split-group-choice spam-split-group
))
897 (dolist (check specific-checks
)
898 (when (stringp check
)
899 (setq spam-split-group-choice check
)
900 (setq specific-checks
(delq check specific-checks
))))
902 (let ((spam-split-group spam-split-group-choice
))
905 (dolist (check spam-list-of-statistical-checks
)
906 (when (and (symbolp check
) (symbol-value check
))
908 (gnus-message 8 "spam-split: widening the buffer (%s requires it)"
911 ;; (progn (widen) (debug (buffer-string)))
912 (let ((list-of-checks spam-list-of-checks
)
914 (while (and list-of-checks
(not decision
))
915 (let ((pair (pop list-of-checks
)))
916 (when (and (symbol-value (car pair
))
917 (or (null specific-checks
)
918 (memq (car pair
) specific-checks
)))
919 (gnus-message 5 "spam-split: calling the %s function"
920 (symbol-name (cdr pair
)))
921 (setq decision
(funcall (cdr pair
)))
922 ;; if we got a decision at all, save the current check
924 (setq spam-split-last-successful-check
(car pair
)))
926 (when (eq decision
'spam
)
927 (if spam-split-symbolic-return
928 (setq decision spam-split-group
)
931 (format "spam-split got %s but %s is nil"
932 (symbol-name decision
)
933 (symbol-name spam-split-symbolic-return
))))))))
935 (if spam-split-symbolic-return-positive
'ham nil
)
938 (defun spam-find-spam ()
939 "This function will detect spam in the current newsgroup using spam-split."
942 (let* ((group gnus-newsgroup-name
)
943 (autodetect (gnus-parameter-spam-autodetect group
))
944 (methods (gnus-parameter-spam-autodetect-methods group
))
945 (first-method (nth 0 methods
)))
946 (when (and autodetect
947 (not (equal first-method
'none
)))
950 (let ((id (spam-fetch-field-message-id-fast article
))
951 (subject (spam-fetch-field-subject-fast article
))
952 (sender (spam-fetch-field-from-fast article
)))
953 (unless (and spam-log-to-registry
954 (spam-log-registered-p id
'incoming
))
955 (let* ((spam-split-symbolic-return t
)
956 (spam-split-symbolic-return-positive t
)
959 (gnus-request-article-this-buffer
962 (if (or (null first-method
)
963 (equal first-method
'default
))
965 (apply 'spam-split methods
)))))
966 (if (equal split-return
'spam
)
967 (gnus-summary-mark-article article gnus-spam-mark
))
969 (when (and split-return spam-log-to-registry
)
970 (when (zerop (gnus-registry-group-count id
))
971 (gnus-registry-add-group
972 id group subject sender
))
974 (spam-log-processing-to-registry
978 spam-split-last-successful-check
980 (if spam-autodetect-recheck-messages
981 gnus-newsgroup-articles
982 gnus-newsgroup-unseen
)))))
984 (defvar spam-registration-functions
985 ;; first the ham register, second the spam register function
986 ;; third the ham unregister, fourth the spam unregister function
987 '((spam-use-blacklist nil
988 spam-blacklist-register-routine
990 spam-blacklist-unregister-routine
)
991 (spam-use-whitelist spam-whitelist-register-routine
993 spam-whitelist-unregister-routine
995 (spam-use-BBDB spam-BBDB-register-routine
997 spam-BBDB-unregister-routine
999 (spam-use-ifile spam-ifile-register-ham-routine
1000 spam-ifile-register-spam-routine
1001 spam-ifile-unregister-ham-routine
1002 spam-ifile-unregister-spam-routine
)
1003 (spam-use-spamoracle spam-spamoracle-learn-ham
1004 spam-spamoracle-learn-spam
1005 spam-spamoracle-unlearn-ham
1006 spam-spamoracle-unlearn-spam
)
1007 (spam-use-stat spam-stat-register-ham-routine
1008 spam-stat-register-spam-routine
1009 spam-stat-unregister-ham-routine
1010 spam-stat-unregister-spam-routine
)
1011 ;; note that spam-use-gmane is not a legitimate check
1013 spam-report-gmane-register-routine
1014 ;; does Gmane support unregistration?
1017 (spam-use-bogofilter spam-bogofilter-register-ham-routine
1018 spam-bogofilter-register-spam-routine
1019 spam-bogofilter-unregister-ham-routine
1020 spam-bogofilter-unregister-spam-routine
))
1021 "The spam-registration-functions list contains pairs
1022 associating a parameter variable with the ham and spam
1023 registration functions, and the ham and spam unregistration
1026 (defun spam-classification-valid-p (classification)
1027 (or (eq classification
'spam
)
1028 (eq classification
'ham
)))
1030 (defun spam-process-type-valid-p (process-type)
1031 (or (eq process-type
'incoming
)
1032 (eq process-type
'process
)))
1034 (defun spam-registration-check-valid-p (check)
1035 (assoc check spam-registration-functions
))
1037 (defun spam-unregistration-check-valid-p (check)
1038 (assoc check spam-registration-functions
))
1040 (defun spam-registration-function (classification check
)
1041 (let ((flist (cdr-safe (assoc check spam-registration-functions
))))
1042 (if (eq classification
'spam
)
1046 (defun spam-unregistration-function (classification check
)
1047 (let ((flist (cdr-safe (assoc check spam-registration-functions
))))
1048 (if (eq classification
'spam
)
1052 (defun spam-list-articles (articles classification
)
1053 (let ((mark-check (if (eq classification
'spam
)
1054 'spam-group-spam-mark-p
1055 'spam-group-ham-mark-p
))
1056 list mark-cache-yes mark-cache-no
)
1057 (dolist (article articles
)
1058 (let ((mark (gnus-summary-article-mark article
)))
1059 (unless (memq mark mark-cache-no
)
1060 (if (memq mark mark-cache-yes
)
1062 ;; else, we have to actually check the mark
1063 (if (funcall mark-check
1068 (push mark mark-cache-yes
))
1069 (push mark mark-cache-no
))))))
1072 (defun spam-register-routine (classification
1074 &optional unregister
1076 (when (and (spam-classification-valid-p classification
)
1077 (spam-registration-check-valid-p check
))
1078 (let* ((register-function
1079 (spam-registration-function classification check
))
1080 (unregister-function
1081 (spam-unregistration-function classification check
))
1082 (run-function (if unregister
1085 (log-function (if unregister
1086 'spam-log-undo-registration
1087 'spam-log-processing-to-registry
))
1091 ;; make list of articles, using specific-articles if given
1092 (setq articles
(or specific-articles
1094 gnus-newsgroup-articles
1097 (gnus-message 5 "%s %d %s articles with classification %s, check %s"
1098 (if unregister
"Unregistering" "Registering")
1100 (if specific-articles
"specific" "")
1101 (symbol-name classification
)
1102 (symbol-name check
))
1103 (funcall run-function articles
)
1104 ;; now log all the registrations (or undo them, depending on unregister)
1105 (dolist (article articles
)
1106 (funcall log-function
1107 (spam-fetch-field-message-id-fast article
)
1111 gnus-newsgroup-name
))))))
1113 ;;; log a ham- or spam-processor invocation to the registry
1114 (defun spam-log-processing-to-registry (id type classification check group
)
1115 (when spam-log-to-registry
1116 (if (and (stringp id
)
1118 (spam-process-type-valid-p type
)
1119 (spam-classification-valid-p classification
)
1120 (spam-registration-check-valid-p check
))
1121 (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type
)))
1122 (cell (list classification check group
)))
1123 (push cell cell-list
)
1124 (gnus-registry-store-extra-entry
1129 (gnus-message 5 (format "%s called with bad ID, type, classification, check, or group"
1130 "spam-log-processing-to-registry")))))
1132 ;;; check if a ham- or spam-processor registration has been done
1133 (defun spam-log-registered-p (id type
)
1134 (when spam-log-to-registry
1135 (if (and (stringp id
)
1136 (spam-process-type-valid-p type
))
1137 (cdr-safe (gnus-registry-fetch-extra id type
))
1139 (gnus-message 5 (format "%s called with bad ID, type, classification, or check"
1140 "spam-log-registered-p"))
1143 ;;; check if a ham- or spam-processor registration needs to be undone
1144 (defun spam-log-unregistration-needed-p (id type classification check
)
1145 (when spam-log-to-registry
1146 (if (and (stringp id
)
1147 (spam-process-type-valid-p type
)
1148 (spam-classification-valid-p classification
)
1149 (spam-registration-check-valid-p check
))
1150 (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type
)))
1152 (dolist (cell cell-list
)
1154 (when (and (eq classification
(nth 0 cell
))
1155 (eq check
(nth 1 cell
)))
1159 (gnus-message 5 (format "%s called with bad ID, type, classification, or check"
1160 "spam-log-unregistration-needed-p"))
1164 ;;; undo a ham- or spam-processor registration (the group is not used)
1165 (defun spam-log-undo-registration (id type classification check
&optional group
)
1166 (when (and spam-log-to-registry
1167 (spam-log-unregistration-needed-p id type classification check
))
1168 (if (and (stringp id
)
1169 (spam-process-type-valid-p type
)
1170 (spam-classification-valid-p classification
)
1171 (spam-registration-check-valid-p check
))
1172 (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type
)))
1173 new-cell-list found
)
1174 (dolist (cell cell-list
)
1175 (unless (and (eq classification
(nth 0 cell
))
1176 (eq check
(nth 1 cell
)))
1177 (push cell new-cell-list
)))
1178 (gnus-registry-store-extra-entry
1183 (gnus-message 5 (format "%s called with bad ID, type, check, or group"
1184 "spam-log-undo-registration"))
1187 ;;; set up IMAP widening if it's necessary
1188 (defun spam-setup-widening ()
1189 (dolist (check spam-list-of-statistical-checks
)
1190 (when (symbol-value check
)
1191 (setq nnimap-split-download-body-default t
))))
1196 (defun spam-check-regex-body ()
1197 (let ((spam-regex-headers-ham spam-regex-body-ham
)
1198 (spam-regex-headers-spam spam-regex-body-spam
))
1199 (spam-check-regex-headers t
)))
1204 (defun spam-check-regex-headers (&optional body
)
1205 (let ((type (if body
"body" "header"))
1206 (spam-split-group (if spam-split-symbolic-return
1210 (dolist (h-regex spam-regex-headers-ham
)
1212 (goto-char (point-min))
1213 (when (re-search-forward h-regex nil t
)
1214 (message "Ham regex %s search positive." type
)
1216 (dolist (s-regex spam-regex-headers-spam
)
1218 (goto-char (point-min))
1219 (when (re-search-forward s-regex nil t
)
1220 (message "Spam regex %s search positive." type
)
1222 (setq ret spam-split-group
))))
1228 (defun spam-reverse-ip-string (ip)
1230 (mapconcat 'identity
1231 (nreverse (split-string ip
"\\."))
1234 (defun spam-check-blackholes ()
1235 "Check the Received headers for blackholed relays."
1236 (let ((headers (nnmail-fetch-field "received"))
1237 (spam-split-group (if spam-split-symbolic-return
1244 (goto-char (point-min))
1245 (gnus-message 5 "Checking headers for relay addresses")
1246 (while (re-search-forward
1247 "\\([0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+\\)" nil t
)
1248 (gnus-message 9 "Blackhole search found host IP %s." (match-string 1))
1249 (push (spam-reverse-ip-string (match-string 1))
1251 (dolist (server spam-blackhole-servers
)
1253 (unless (and spam-blackhole-good-server-regex
1254 ;; match the good-server-regex against the reversed (again) IP string
1256 spam-blackhole-good-server-regex
1257 (spam-reverse-ip-string ip
)))
1259 (let ((query-string (concat ip
"." server
)))
1261 (let ((query-result (query-dig query-string
)))
1263 (gnus-message 5 "(DIG): positive blackhole check '%s'"
1265 (push (list ip server query-result
)
1267 ;; else, if not using dig.el
1268 (when (query-dns query-string
)
1269 (gnus-message 5 "positive blackhole check")
1270 (push (list ip server
(query-dns query-string
'TXT
))
1278 (autoload 'mail-check-payment
"hashcash"))
1284 (defun spam-check-hashcash ()
1285 "Check the headers for hashcash payments."
1286 (mail-check-payment))) ;mail-check-payment returns a boolean
1292 ;;; original idea for spam-check-BBDB from Alexander Kotelnikov
1293 ;;; <sacha@giotto.sj.ru>
1295 ;; all this is done inside a condition-case to trap errors
1298 (autoload 'bbdb-buffer
"bbdb")
1299 (autoload 'bbdb-create-internal
"bbdb")
1300 (autoload 'bbdb-search-simple
"bbdb"))
1303 (when (condition-case nil
1306 (require 'bbdb-com
))
1308 (defalias 'spam-BBDB-register-routine
'ignore
)
1309 (defalias 'spam-enter-ham-BBDB
'ignore
)
1312 (defun spam-enter-ham-BBDB (addresses &optional remove
)
1313 "Enter an address into the BBDB; implies ham (non-spam) sender"
1314 (dolist (from addresses
)
1315 (when (stringp from
)
1316 (let* ((parsed-address (gnus-extract-address-components from
))
1317 (name (or (nth 0 parsed-address
) "Ham Sender"))
1318 (remove-function (if remove
1319 'bbdb-delete-record-internal
1321 (net-address (nth 1 parsed-address
))
1322 (record (and net-address
1323 (bbdb-search-simple nil net-address
))))
1325 (gnus-message 5 "%s address %s %s BBDB"
1326 (if remove
"Deleting" "Adding")
1328 (if remove
"from" "to"))
1330 (funcall remove-function record
)
1331 (bbdb-create-internal name nil net-address nil nil
1332 "ham sender added by spam.el")))))))
1334 (defun spam-BBDB-register-routine (articles &optional unregister
)
1336 (dolist (article articles
)
1337 (when (stringp (spam-fetch-field-from-fast article
))
1338 (push (spam-fetch-field-from-fast article
) addresses
)))
1339 ;; now do the register/unregister action
1340 (spam-enter-ham-BBDB addresses unregister
)))
1342 (defun spam-BBDB-unregister-routine (articles)
1343 (spam-BBDB-register-routine articles t
))
1345 (defun spam-check-BBDB ()
1346 "Mail from people in the BBDB is classified as ham or non-spam"
1347 (let ((who (nnmail-fetch-field "from"))
1348 (spam-split-group (if spam-split-symbolic-return
1352 (setq who
(nth 1 (gnus-extract-address-components who
)))
1353 (if (bbdb-search-simple nil who
)
1355 (if spam-use-BBDB-exclusive
1362 ;;; check the ifile backend; return nil if the mail was NOT classified
1365 (defun spam-get-ifile-database-parameter ()
1366 "Get the command-line parameter for ifile's database from
1367 spam-ifile-database-path."
1368 (if spam-ifile-database-path
1369 (format "--db-file=%s" spam-ifile-database-path
)
1372 (defun spam-check-ifile ()
1373 "Check the ifile backend for the classification of this message."
1374 (let ((article-buffer-name (buffer-name))
1375 (spam-split-group (if spam-split-symbolic-return
1380 (let ((temp-buffer-name (buffer-name))
1381 (db-param (spam-get-ifile-database-parameter)))
1383 (set-buffer article-buffer-name
)
1384 (apply 'call-process-region
1385 (point-min) (point-max) spam-ifile-path
1386 nil temp-buffer-name nil
"-c"
1387 (if db-param
`(,db-param
"-q") `("-q"))))
1388 ;; check the return now (we're back in the temp buffer)
1389 (goto-char (point-min))
1391 (setq category
(buffer-substring (point) (spam-point-at-eol))))
1392 (when (not (zerop (length category
))) ; we need a category here
1393 (if spam-ifile-all-categories
1394 (setq return category
)
1395 ;; else, if spam-ifile-all-categories is not set...
1396 (when (string-equal spam-ifile-spam-category category
)
1397 (setq return spam-split-group
)))))) ; note return is nil otherwise
1400 (defun spam-ifile-register-with-ifile (articles category
&optional unregister
)
1401 "Register an article, given as a string, with a category.
1402 Uses `gnus-newsgroup-name' if category is nil (for ham registration)."
1403 (let ((category (or category gnus-newsgroup-name
))
1404 (add-or-delete-option (if unregister
"-d" "-i"))
1405 (db (spam-get-ifile-database-parameter))
1408 (dolist (article articles
)
1409 (let ((article-string (spam-get-article-as-string article
)))
1410 (when (stringp article-string
)
1411 (insert article-string
))))
1412 (apply 'call-process-region
1413 (point-min) (point-max) spam-ifile-path
1415 add-or-delete-option category
1416 (if db
`(,db
"-h") `("-h"))))))
1418 (defun spam-ifile-register-spam-routine (articles &optional unregister
)
1419 (spam-ifile-register-with-ifile articles spam-ifile-spam-category unregister
))
1421 (defun spam-ifile-unregister-spam-routine (articles)
1422 (spam-ifile-register-spam-routine articles t
))
1424 (defun spam-ifile-register-ham-routine (articles &optional unregister
)
1425 (spam-ifile-register-with-ifile articles spam-ifile-ham-category unregister
))
1427 (defun spam-ifile-unregister-ham-routine (articles)
1428 (spam-ifile-register-ham-routine articles t
))
1434 (autoload 'spam-stat-buffer-change-to-non-spam
"spam-stat")
1435 (autoload 'spam-stat-buffer-change-to-spam
"spam-stat")
1436 (autoload 'spam-stat-buffer-is-non-spam
"spam-stat")
1437 (autoload 'spam-stat-buffer-is-spam
"spam-stat")
1438 (autoload 'spam-stat-load
"spam-stat")
1439 (autoload 'spam-stat-save
"spam-stat")
1440 (autoload 'spam-stat-split-fancy
"spam-stat"))
1443 (when (condition-case nil
1444 (let ((spam-stat-install-hooks nil
))
1445 (require 'spam-stat
))
1447 (defalias 'spam-stat-register-ham-routine
'ignore
)
1448 (defalias 'spam-stat-register-spam-routine
'ignore
)
1451 (defun spam-check-stat ()
1452 "Check the spam-stat backend for the classification of this message"
1453 (let ((spam-split-group (if spam-split-symbolic-return
1456 (spam-stat-split-fancy-spam-group spam-split-group
) ; override
1457 (spam-stat-buffer (buffer-name)) ; stat the current buffer
1459 (spam-stat-split-fancy)))
1461 (defun spam-stat-register-spam-routine (articles &optional unregister
)
1462 (dolist (article articles
)
1463 (let ((article-string (spam-get-article-as-string article
)))
1465 (insert article-string
)
1467 (spam-stat-buffer-change-to-non-spam)
1468 (spam-stat-buffer-is-spam))))))
1470 (defun spam-stat-unregister-spam-routine (articles)
1471 (spam-stat-register-spam-routine articles t
))
1473 (defun spam-stat-register-ham-routine (articles &optional unregister
)
1474 (dolist (article articles
)
1475 (let ((article-string (spam-get-article-as-string article
)))
1477 (insert article-string
)
1479 (spam-stat-buffer-change-to-spam)
1480 (spam-stat-buffer-is-non-spam))))))
1482 (defun spam-stat-unregister-ham-routine (articles)
1483 (spam-stat-register-ham-routine articles t
))
1485 (defun spam-maybe-spam-stat-load ()
1486 (when spam-use-stat
(spam-stat-load)))
1488 (defun spam-maybe-spam-stat-save ()
1489 (when spam-use-stat
(spam-stat-save)))))
1493 ;;;; Blacklists and whitelists.
1495 (defvar spam-whitelist-cache nil
)
1496 (defvar spam-blacklist-cache nil
)
1498 (defun spam-kill-whole-line ()
1500 (let ((kill-whole-line t
))
1503 ;;; address can be a list, too
1504 (defun spam-enter-whitelist (address &optional remove
)
1505 "Enter ADDRESS (list or single) into the whitelist.
1506 With a non-nil REMOVE, remove them."
1507 (interactive "sAddress: ")
1508 (spam-enter-list address spam-whitelist remove
)
1509 (setq spam-whitelist-cache nil
))
1511 ;;; address can be a list, too
1512 (defun spam-enter-blacklist (address &optional remove
)
1513 "Enter ADDRESS (list or single) into the blacklist.
1514 With a non-nil REMOVE, remove them."
1515 (interactive "sAddress: ")
1516 (spam-enter-list address spam-blacklist remove
)
1517 (setq spam-blacklist-cache nil
))
1519 (defun spam-enter-list (addresses file
&optional remove
)
1520 "Enter ADDRESSES into the given FILE.
1521 Either the whitelist or the blacklist files can be used. With
1522 REMOVE not nil, remove the ADDRESSES."
1523 (if (stringp addresses
)
1524 (spam-enter-list (list addresses
) file remove
)
1525 ;; else, we have a list of addresses here
1526 (unless (file-exists-p (file-name-directory file
))
1527 (make-directory (file-name-directory file
) t
))
1530 (find-file-noselect file
))
1531 (dolist (a addresses
)
1533 (goto-char (point-min))
1534 (if (re-search-forward (regexp-quote a
) nil t
)
1535 ;; found the address
1537 (spam-kill-whole-line))
1538 ;; else, the address was not found
1540 (goto-char (point-max))
1546 ;;; returns t if the sender is in the whitelist, nil or
1547 ;;; spam-split-group otherwise
1548 (defun spam-check-whitelist ()
1549 ;; FIXME! Should it detect when file timestamps change?
1550 (let ((spam-split-group (if spam-split-symbolic-return
1553 (unless spam-whitelist-cache
1554 (setq spam-whitelist-cache
(spam-parse-list spam-whitelist
)))
1555 (if (spam-from-listed-p spam-whitelist-cache
)
1557 (if spam-use-whitelist-exclusive
1561 (defun spam-check-blacklist ()
1562 ;; FIXME! Should it detect when file timestamps change?
1563 (let ((spam-split-group (if spam-split-symbolic-return
1566 (unless spam-blacklist-cache
1567 (setq spam-blacklist-cache
(spam-parse-list spam-blacklist
)))
1568 (and (spam-from-listed-p spam-blacklist-cache
) spam-split-group
)))
1570 (defun spam-parse-list (file)
1571 (when (file-readable-p file
)
1572 (let (contents address
)
1574 (insert-file-contents file
)
1576 (setq address
(buffer-substring (point) (spam-point-at-eol)))
1578 ;; insert the e-mail address if detected, otherwise the raw data
1579 (unless (zerop (length address
))
1580 (let ((pure-address (nth 1 (gnus-extract-address-components address
))))
1581 (push (or pure-address address
) contents
)))))
1582 (nreverse contents
))))
1584 (defun spam-from-listed-p (cache)
1585 (let ((from (nnmail-fetch-field "from"))
1588 (let ((address (pop cache
)))
1589 (unless (zerop (length address
)) ; 0 for a nil address too
1590 (setq address
(regexp-quote address
))
1591 ;; fix regexp-quote's treatment of user-intended regexes
1592 (while (string-match "\\\\\\*" address
)
1593 (setq address
(replace-match ".*" t t address
))))
1594 (when (and address
(string-match address from
))
1599 (defun spam-filelist-register-routine (articles blacklist
&optional unregister
)
1600 (let ((de-symbol (if blacklist
'spam-use-whitelist
'spam-use-blacklist
))
1601 (declassification (if blacklist
'ham
'spam
))
1603 (if blacklist
'spam-enter-blacklist
'spam-enter-whitelist
))
1605 (if blacklist
'spam-enter-whitelist
'spam-enter-blacklist
))
1606 from addresses unregister-list
)
1607 (dolist (article articles
)
1608 (let ((from (spam-fetch-field-from-fast article
))
1609 (id (spam-fetch-field-message-id-fast article
))
1611 (when (stringp from
)
1612 (dolist (ignore-regex spam-blacklist-ignored-regexes
)
1613 (when (and (not sender-ignored
)
1614 (stringp ignore-regex
)
1615 (string-match ignore-regex from
))
1616 (setq sender-ignored t
)))
1617 ;; remember the messages we need to unregister, unless remove is set
1620 (spam-log-unregistration-needed-p
1621 id
'process declassification de-symbol
))
1622 (push from unregister-list
))
1623 (unless sender-ignored
1624 (push from addresses
)))))
1627 (funcall enter-function addresses t
) ; unregister all these addresses
1628 ;; else, register normally and unregister what we need to
1629 (funcall remove-function unregister-list t
)
1630 (dolist (article unregister-list
)
1631 (spam-log-undo-registration
1632 (spam-fetch-field-message-id-fast article
)
1636 (funcall enter-function addresses nil
))))
1638 (defun spam-blacklist-unregister-routine (articles)
1639 (spam-blacklist-register-routine articles t
))
1641 (defun spam-blacklist-register-routine (articles &optional unregister
)
1642 (spam-filelist-register-routine articles t unregister
))
1644 (defun spam-whitelist-unregister-routine (articles)
1645 (spam-whitelist-register-routine articles t
))
1647 (defun spam-whitelist-register-routine (articles &optional unregister
)
1648 (spam-filelist-register-routine articles nil unregister
))
1651 ;;;; Spam-report glue
1652 (defun spam-report-gmane-register-routine (articles)
1654 (apply 'spam-report-gmane articles
)))
1658 (defun spam-check-bogofilter-headers (&optional score
)
1659 (let ((header (nnmail-fetch-field spam-bogofilter-header
))
1660 (spam-split-group (if spam-split-symbolic-return
1663 (when header
; return nil when no header
1664 (if score
; scoring mode
1665 (if (string-match "spamicity=\\([0-9.]+\\)" header
)
1666 (match-string 1 header
)
1668 ;; spam detection mode
1669 (when (string-match spam-bogofilter-bogosity-positive-spam-header
1671 spam-split-group
)))))
1673 ;; return something sensible if the score can't be determined
1674 (defun spam-bogofilter-score ()
1675 "Get the Bogofilter spamicity score"
1677 (save-window-excursion
1678 (gnus-summary-show-article t
)
1679 (set-buffer gnus-article-buffer
)
1680 (let ((score (or (spam-check-bogofilter-headers t
)
1681 (spam-check-bogofilter t
))))
1682 (message "Spamicity score %s" score
)
1684 (gnus-summary-show-article)))
1686 (defun spam-check-bogofilter (&optional score
)
1687 "Check the Bogofilter backend for the classification of this message"
1688 (let ((article-buffer-name (buffer-name))
1689 (db spam-bogofilter-database-directory
)
1692 (let ((temp-buffer-name (buffer-name)))
1694 (set-buffer article-buffer-name
)
1695 (apply 'call-process-region
1696 (point-min) (point-max)
1697 spam-bogofilter-path
1698 nil temp-buffer-name nil
1699 (if db
`("-d" ,db
"-v") `("-v"))))
1700 (setq return
(spam-check-bogofilter-headers score
))))
1703 (defun spam-bogofilter-register-with-bogofilter (articles
1705 &optional unregister
)
1706 "Register an article, given as a string, as spam or non-spam."
1707 (dolist (article articles
)
1708 (let ((article-string (spam-get-article-as-string article
))
1709 (db spam-bogofilter-database-directory
)
1710 (switch (if unregister
1712 spam-bogofilter-spam-strong-switch
1713 spam-bogofilter-ham-strong-switch
)
1715 spam-bogofilter-spam-switch
1716 spam-bogofilter-ham-switch
))))
1717 (when (stringp article-string
)
1719 (insert article-string
)
1721 (apply 'call-process-region
1722 (point-min) (point-max)
1723 spam-bogofilter-path
1725 (if db
`("-d" ,db
"-v") `("-v"))))))))
1727 (defun spam-bogofilter-register-spam-routine (articles &optional unregister
)
1728 (spam-bogofilter-register-with-bogofilter articles t unregister
))
1730 (defun spam-bogofilter-unregister-spam-routine (articles)
1731 (spam-bogofilter-register-spam-routine articles t
))
1733 (defun spam-bogofilter-register-ham-routine (articles &optional unregister
)
1734 (spam-bogofilter-register-with-bogofilter articles nil unregister
))
1736 (defun spam-bogofilter-unregister-ham-routine (articles)
1737 (spam-bogofilter-register-ham-routine articles t
))
1742 (defun spam-check-spamoracle ()
1743 "Run spamoracle on an article to determine whether it's spam."
1744 (let ((article-buffer-name (buffer-name))
1745 (spam-split-group (if spam-split-symbolic-return
1749 (let ((temp-buffer-name (buffer-name)))
1751 (set-buffer article-buffer-name
)
1753 (apply 'call-process-region
1754 (point-min) (point-max)
1755 spam-spamoracle-binary
1756 nil temp-buffer-name nil
1757 (if spam-spamoracle-database
1758 `("-f" ,spam-spamoracle-database
"mark")
1762 (set-buffer temp-buffer-name
)
1763 (goto-char (point-min))
1764 (when (re-search-forward "^X-Spam: yes;" nil t
)
1766 (error "Error running spamoracle: %s" status
))))))))
1768 (defun spam-spamoracle-learn (articles article-is-spam-p
&optional unregister
)
1769 "Run spamoracle in training mode."
1771 (let ((temp-buffer-name (buffer-name)))
1773 (goto-char (point-min))
1774 (dolist (article articles
)
1775 (insert (spam-get-article-as-string article
)))
1776 (let* ((arg (if (spam-xor unregister article-is-spam-p
)
1780 (apply 'call-process-region
1781 (point-min) (point-max)
1782 spam-spamoracle-binary
1783 nil temp-buffer-name nil
1784 (if spam-spamoracle-database
1785 `("-f" ,spam-spamoracle-database
1788 (unless (eq 0 status
)
1789 (error "Error running spamoracle: %s" status
)))))))
1791 (defun spam-spamoracle-learn-ham (articles &optional unregister
)
1792 (spam-spamoracle-learn articles nil unregister
))
1794 (defun spam-spamoracle-unlearn-ham (articles &optional unregister
)
1795 (spam-spamoracle-learn-ham articles t
))
1797 (defun spam-spamoracle-learn-spam (articles &optional unregister
)
1798 (spam-spamoracle-learn articles t unregister
))
1800 (defun spam-spamoracle-unlearn-spam (articles &optional unregister
)
1801 (spam-spamoracle-learn-spam articles t
))
1807 (defun spam-initialize ()
1808 "Install the spam.el hooks and do other initialization"
1810 (setq spam-install-hooks t
)
1811 ;; TODO: How do we redo this every time the `spam' face is customized?
1812 (push '((eq mark gnus-spam-mark
) . spam
)
1813 gnus-summary-highlight
)
1814 ;; Add hooks for loading and saving the spam stats
1815 (add-hook 'gnus-save-newsrc-hook
'spam-maybe-spam-stat-save
)
1816 (add-hook 'gnus-get-top-new-news-hook
'spam-maybe-spam-stat-load
)
1817 (add-hook 'gnus-startup-hook
'spam-maybe-spam-stat-load
)
1818 (add-hook 'gnus-summary-prepare-exit-hook
'spam-summary-prepare-exit
)
1819 (add-hook 'gnus-summary-prepare-hook
'spam-summary-prepare
)
1820 (add-hook 'gnus-get-new-news-hook
'spam-setup-widening
)
1821 (add-hook 'gnus-summary-prepare-hook
'spam-find-spam
))
1823 (defun spam-unload-hook ()
1824 "Uninstall the spam.el hooks"
1826 (remove-hook 'gnus-save-newsrc-hook
'spam-maybe-spam-stat-save
)
1827 (remove-hook 'gnus-get-top-new-news-hook
'spam-maybe-spam-stat-load
)
1828 (remove-hook 'gnus-startup-hook
'spam-maybe-spam-stat-load
)
1829 (remove-hook 'gnus-summary-prepare-exit-hook
'spam-summary-prepare-exit
)
1830 (remove-hook 'gnus-summary-prepare-hook
'spam-summary-prepare
)
1831 (remove-hook 'gnus-get-new-news-hook
'spam-setup-widening
)
1832 (remove-hook 'gnus-summary-prepare-hook
'spam-find-spam
))
1834 (add-hook 'spam-unload-hook
'spam-unload-hook
)
1836 (when spam-install-hooks
1841 ;;; arch-tag: 07e6e0ca-ab0a-4412-b445-1f6c72a4f27f
1842 ;;; spam.el ends here