1 ;;; spam.el --- Identifying spam
3 ;; Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008 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 3, 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 (make-obsolete-variable 'spam-ifile-path
'spam-ifile-program
)
355 ;; "22.1" ;; Gnus 5.10.9
356 (defcustom spam-ifile-program
(executable-find "ifile")
357 "Name of the ifile program."
358 :type
'(choice (file :tag
"Location of ifile")
359 (const :tag
"ifile is not installed"))
362 (make-obsolete-variable 'spam-ifile-database-path
'spam-ifile-database
)
363 ;; "22.1" ;; Gnus 5.10.9
364 (defcustom spam-ifile-database nil
365 "File name of the ifile database."
366 :type
'(choice (file :tag
"Location of the ifile database")
367 (const :tag
"Use the default"))
370 (defcustom spam-ifile-spam-category
"spam"
371 "Name of the spam ifile category."
375 (defcustom spam-ifile-ham-category nil
376 "Name of the ham ifile category.
377 If nil, the current group name will be used."
378 :type
'(choice (string :tag
"Use a fixed category")
379 (const :tag
"Use the current group name"))
382 (defcustom spam-ifile-all-categories nil
383 "Whether the ifile check will return all categories, or just spam.
384 Set this to t if you want to use the `spam-split' invocation of ifile as
385 your main source of newsgroup names."
389 (defgroup spam-bogofilter nil
390 "Spam bogofilter configuration."
393 (make-obsolete-variable 'spam-bogofilter-path
'spam-bogofilter-program
)
394 ;; "22.1" ;; Gnus 5.10.9
395 (defcustom spam-bogofilter-program
(executable-find "bogofilter")
396 "Name of the Bogofilter program."
397 :type
'(choice (file :tag
"Location of bogofilter")
398 (const :tag
"Bogofilter is not installed"))
399 :group
'spam-bogofilter
)
401 (defcustom spam-bogofilter-header
"X-Bogosity"
402 "The header that Bogofilter inserts in messages."
404 :group
'spam-bogofilter
)
406 (defcustom spam-bogofilter-spam-switch
"-s"
407 "The switch that Bogofilter uses to register spam messages."
409 :group
'spam-bogofilter
)
411 (defcustom spam-bogofilter-ham-switch
"-n"
412 "The switch that Bogofilter uses to register ham messages."
414 :group
'spam-bogofilter
)
416 (defcustom spam-bogofilter-spam-strong-switch
"-S"
417 "The switch that Bogofilter uses to unregister ham messages."
419 :group
'spam-bogofilter
)
421 (defcustom spam-bogofilter-ham-strong-switch
"-N"
422 "The switch that Bogofilter uses to unregister spam messages."
424 :group
'spam-bogofilter
)
426 (defcustom spam-bogofilter-bogosity-positive-spam-header
"^\\(Yes\\|Spam\\)"
427 "The regex on `spam-bogofilter-header' for positive spam identification."
429 :group
'spam-bogofilter
)
431 (defcustom spam-bogofilter-database-directory nil
432 "Location of the Bogofilter database.
433 When nil, use the default location."
434 :type
'(choice (directory
435 :tag
"Location of the Bogofilter database directory")
436 (const :tag
"Use the default"))
437 :group
'spam-bogofilter
)
439 (defgroup spam-spamoracle nil
440 "Spam spamoracle configuration."
443 (defcustom spam-spamoracle-database nil
444 "Location of spamoracle database file.
445 When nil, use the default spamoracle database."
446 :type
'(choice (directory :tag
"Location of spamoracle database file.")
447 (const :tag
"Use the default"))
448 :group
'spam-spamoracle
)
450 (defcustom spam-spamoracle-binary
(executable-find "spamoracle")
451 "Location of the spamoracle binary."
452 :type
'(choice (directory :tag
"Location of the spamoracle binary")
453 (const :tag
"Use the default"))
454 :group
'spam-spamoracle
)
456 ;;; Key bindings for spam control.
458 (gnus-define-keys gnus-summary-mode-map
459 "St" spam-bogofilter-score
460 "Sx" gnus-summary-mark-as-spam
461 "Mst" spam-bogofilter-score
462 "Msx" gnus-summary-mark-as-spam
463 "\M-d" gnus-summary-mark-as-spam
)
465 (defvar spam-old-ham-articles nil
466 "List of old ham articles, generated when a group is entered.")
468 (defvar spam-old-spam-articles nil
469 "List of old spam articles, generated when a group is entered.")
471 (defvar spam-split-disabled nil
472 "If non-nil, `spam-split' is disabled, and always returns nil.")
474 (defvar spam-split-last-successful-check nil
475 "`spam-split' will set this to nil or a spam-use-XYZ check if it
478 ;; convenience functions
479 (defun spam-xor (a b
)
480 "Logical exclusive `or'."
481 (and (or a b
) (not (and a b
))))
483 (defun spam-group-ham-mark-p (group mark
&optional spam
)
484 (when (stringp group
)
485 (let* ((marks (spam-group-ham-marks group spam
))
486 (marks (if (symbolp mark
)
488 (mapcar 'symbol-value marks
))))
491 (defun spam-group-spam-mark-p (group mark
)
492 (spam-group-ham-mark-p group mark t
))
494 (defun spam-group-ham-marks (group &optional spam
)
495 (when (stringp group
)
496 (let* ((marks (if spam
497 (gnus-parameter-spam-marks group
)
498 (gnus-parameter-ham-marks group
)))
500 (marks (if (listp (car marks
)) (car marks
) marks
)))
503 (defun spam-group-spam-marks (group)
504 (spam-group-ham-marks group t
))
506 (defun spam-group-spam-contents-p (group)
508 (or (member group spam-junk-mailgroups
)
509 (memq 'gnus-group-spam-classification-spam
510 (gnus-parameter-spam-contents group
)))
513 (defun spam-group-ham-contents-p (group)
515 (memq 'gnus-group-spam-classification-ham
516 (gnus-parameter-spam-contents group
))
519 (defvar spam-list-of-processors
520 '((gnus-group-spam-exit-processor-report-gmane spam spam-use-gmane
)
521 (gnus-group-spam-exit-processor-bogofilter spam spam-use-bogofilter
)
522 (gnus-group-spam-exit-processor-blacklist spam spam-use-blacklist
)
523 (gnus-group-spam-exit-processor-ifile spam spam-use-ifile
)
524 (gnus-group-spam-exit-processor-stat spam spam-use-stat
)
525 (gnus-group-spam-exit-processor-spamoracle spam spam-use-spamoracle
)
526 (gnus-group-ham-exit-processor-ifile ham spam-use-ifile
)
527 (gnus-group-ham-exit-processor-bogofilter ham spam-use-bogofilter
)
528 (gnus-group-ham-exit-processor-stat ham spam-use-stat
)
529 (gnus-group-ham-exit-processor-whitelist ham spam-use-whitelist
)
530 (gnus-group-ham-exit-processor-BBDB ham spam-use-BBDB
)
531 (gnus-group-ham-exit-processor-copy ham spam-use-ham-copy
)
532 (gnus-group-ham-exit-processor-spamoracle ham spam-use-spamoracle
))
533 "The spam-list-of-processors list contains pairs associating a
534 ham/spam exit processor variable with a classification and a
535 spam-use-* variable.")
537 (defun spam-group-processor-p (group processor
)
538 (if (and (stringp group
)
540 (or (member processor
(nth 0 (gnus-parameter-spam-process group
)))
541 (spam-group-processor-multiple-p
543 (cdr-safe (assoc processor spam-list-of-processors
))))
546 (defun spam-group-processor-multiple-p (group processor-info
)
547 (let* ((classification (nth 0 processor-info
))
548 (check (nth 1 processor-info
))
549 (parameters (nth 0 (gnus-parameter-spam-process group
)))
551 (dolist (parameter parameters
)
552 (when (and (null found
)
554 (eq classification
(nth 0 parameter
))
555 (eq check
(nth 1 parameter
)))
559 (defun spam-group-spam-processor-report-gmane-p (group)
560 (spam-group-processor-p group
'gnus-group-spam-exit-processor-report-gmane
))
562 (defun spam-group-spam-processor-bogofilter-p (group)
563 (spam-group-processor-p group
'gnus-group-spam-exit-processor-bogofilter
))
565 (defun spam-group-spam-processor-blacklist-p (group)
566 (spam-group-processor-p group
'gnus-group-spam-exit-processor-blacklist
))
568 (defun spam-group-spam-processor-ifile-p (group)
569 (spam-group-processor-p group
'gnus-group-spam-exit-processor-ifile
))
571 (defun spam-group-ham-processor-ifile-p (group)
572 (spam-group-processor-p group
'gnus-group-ham-exit-processor-ifile
))
574 (defun spam-group-spam-processor-spamoracle-p (group)
575 (spam-group-processor-p group
'gnus-group-spam-exit-processor-spamoracle
))
577 (defun spam-group-ham-processor-bogofilter-p (group)
578 (spam-group-processor-p group
'gnus-group-ham-exit-processor-bogofilter
))
580 (defun spam-group-spam-processor-stat-p (group)
581 (spam-group-processor-p group
'gnus-group-spam-exit-processor-stat
))
583 (defun spam-group-ham-processor-stat-p (group)
584 (spam-group-processor-p group
'gnus-group-ham-exit-processor-stat
))
586 (defun spam-group-ham-processor-whitelist-p (group)
587 (spam-group-processor-p group
'gnus-group-ham-exit-processor-whitelist
))
589 (defun spam-group-ham-processor-BBDB-p (group)
590 (spam-group-processor-p group
'gnus-group-ham-exit-processor-BBDB
))
592 (defun spam-group-ham-processor-copy-p (group)
593 (spam-group-processor-p group
'gnus-group-ham-exit-processor-copy
))
595 (defun spam-group-ham-processor-spamoracle-p (group)
596 (spam-group-processor-p group
'gnus-group-ham-exit-processor-spamoracle
))
598 ;;; Summary entry and exit processing.
600 (defun spam-summary-prepare ()
601 (setq spam-old-ham-articles
602 (spam-list-articles gnus-newsgroup-articles
'ham
))
603 (setq spam-old-spam-articles
604 (spam-list-articles gnus-newsgroup-articles
'spam
))
605 (spam-mark-junk-as-spam-routine))
607 ;; The spam processors are invoked for any group, spam or ham or neither
608 (defun spam-summary-prepare-exit ()
609 (unless gnus-group-is-exiting-without-update-p
610 (gnus-message 6 "Exiting summary buffer and applying spam rules")
612 ;; first of all, unregister any articles that are no longer ham or spam
613 ;; we have to iterate over the processors, or else we'll be too slow
614 (dolist (classification '(spam ham
))
615 (let* ((old-articles (if (eq classification
'spam
)
616 spam-old-spam-articles
617 spam-old-ham-articles
))
618 (new-articles (spam-list-articles
619 gnus-newsgroup-articles
621 (changed-articles (gnus-set-difference old-articles new-articles
)))
622 ;; now that we have the changed articles, we go through the processors
623 (dolist (processor-param spam-list-of-processors
)
624 (let ((processor (nth 0 processor-param
))
625 (processor-classification (nth 1 processor-param
))
626 (check (nth 2 processor-param
))
628 (dolist (article changed-articles
)
629 (let ((id (spam-fetch-field-message-id-fast article
)))
630 (when (spam-log-unregistration-needed-p
631 id
'process classification check
)
632 (push article unregister-list
))))
633 ;; call spam-register-routine with specific articles to unregister,
634 ;; when there are articles to unregister and the check is enabled
635 (when (and unregister-list
(symbol-value check
))
636 (spam-register-routine classification check t unregister-list
))))))
638 ;; find all the spam processors applicable to this group
639 (dolist (processor-param spam-list-of-processors
)
640 (let ((processor (nth 0 processor-param
))
641 (classification (nth 1 processor-param
))
642 (check (nth 2 processor-param
)))
643 (when (and (eq 'spam classification
)
644 (spam-group-processor-p gnus-newsgroup-name processor
))
645 (spam-register-routine classification check
))))
647 (if spam-move-spam-nonspam-groups-only
648 (when (not (spam-group-spam-contents-p gnus-newsgroup-name
))
649 (spam-mark-spam-as-expired-and-move-routine
650 (gnus-parameter-spam-process-destination gnus-newsgroup-name
)))
651 (gnus-message 5 "Marking spam as expired and moving it to %s"
653 (spam-mark-spam-as-expired-and-move-routine
654 (gnus-parameter-spam-process-destination gnus-newsgroup-name
)))
656 ;; now we redo spam-mark-spam-as-expired-and-move-routine to only
657 ;; expire spam, in case the above did not expire them
658 (gnus-message 5 "Marking spam as expired without moving it")
659 (spam-mark-spam-as-expired-and-move-routine nil
)
661 (when (or (spam-group-ham-contents-p gnus-newsgroup-name
)
662 (and (spam-group-spam-contents-p gnus-newsgroup-name
)
663 spam-process-ham-in-spam-groups
)
664 spam-process-ham-in-nonham-groups
)
665 ;; find all the ham processors applicable to this group
666 (dolist (processor-param spam-list-of-processors
)
667 (let ((processor (nth 0 processor-param
))
668 (classification (nth 1 processor-param
))
669 (check (nth 2 processor-param
)))
670 (when (and (eq 'ham classification
)
671 (spam-group-processor-p gnus-newsgroup-name processor
))
672 (spam-register-routine classification check
)))))
674 (when (spam-group-ham-processor-copy-p gnus-newsgroup-name
)
675 (gnus-message 5 "Copying ham")
676 (spam-ham-copy-routine
677 (gnus-parameter-ham-process-destination gnus-newsgroup-name
)))
679 ;; now move all ham articles out of spam groups
680 (when (spam-group-spam-contents-p gnus-newsgroup-name
)
681 (gnus-message 5 "Moving ham messages from spam group")
682 (spam-ham-move-routine
683 (gnus-parameter-ham-process-destination gnus-newsgroup-name
))))
685 (setq spam-old-ham-articles nil
)
686 (setq spam-old-spam-articles nil
))
688 (defun spam-mark-junk-as-spam-routine ()
689 ;; check the global list of group names spam-junk-mailgroups and the
691 (when (spam-group-spam-contents-p gnus-newsgroup-name
)
692 (gnus-message 6 "Marking %s articles as spam"
693 (if spam-mark-only-unseen-as-spam
696 (let ((articles (if spam-mark-only-unseen-as-spam
697 gnus-newsgroup-unseen
698 gnus-newsgroup-unreads
)))
699 (if spam-mark-new-messages-in-spam-group-as-spam
700 (dolist (article articles
)
701 (gnus-summary-mark-article article gnus-spam-mark
))
702 (gnus-message 9 "Did not mark new messages as spam.")))))
704 (defun spam-mark-spam-as-expired-and-move-routine (&rest groups
)
705 (if (and (car-safe groups
) (listp (car-safe groups
)))
706 (apply 'spam-mark-spam-as-expired-and-move-routine
(car groups
))
707 (gnus-summary-kill-process-mark)
708 (let ((articles gnus-newsgroup-articles
)
709 (backend-supports-deletions
710 (gnus-check-backend-function
711 'request-move-article gnus-newsgroup-name
))
712 article tomove deletep
)
713 (dolist (article articles
)
714 (when (eq (gnus-summary-article-mark article
) gnus-spam-mark
)
715 (gnus-summary-mark-article article gnus-expirable-mark
)
716 (push article tomove
)))
718 ;; now do the actual copies
719 (dolist (group groups
)
722 (dolist (article tomove
)
723 (gnus-summary-set-process-mark article
))
725 (if (or (not backend-supports-deletions
)
726 (> (length groups
) 1))
728 (gnus-summary-copy-article nil group
)
730 (gnus-summary-move-article nil group
)))))
732 ;; now delete the articles, if there was a copy done, and the
734 (when (and deletep backend-supports-deletions
)
735 (dolist (article tomove
)
736 (gnus-summary-set-process-mark article
))
738 (let ((gnus-novice-user nil
)) ; don't ask me if I'm sure
739 (gnus-summary-delete-article nil
))))
741 (gnus-summary-yank-process-mark))))
743 (defun spam-ham-copy-or-move-routine (copy groups
)
744 (gnus-summary-kill-process-mark)
745 (let ((todo (spam-list-articles gnus-newsgroup-articles
'ham
))
746 (backend-supports-deletions
747 (gnus-check-backend-function
748 'request-move-article gnus-newsgroup-name
))
749 (respool-method (gnus-find-method-for-group gnus-newsgroup-name
))
750 article mark deletep respool
)
752 (when (member 'respool groups
)
753 (setq respool t
) ; boolean for later
754 (setq groups
'("fake"))) ; when respooling, groups are dynamic so fake it
756 ;; now do the actual move
757 (dolist (group groups
)
758 (when (and todo
(stringp group
))
759 (dolist (article todo
)
760 (when spam-mark-ham-unread-before-move-from-spam-group
761 (gnus-summary-mark-article article gnus-unread-mark
))
762 (gnus-summary-set-process-mark article
))
764 (if respool
; respooling is with a "fake" group
765 (let ((spam-split-disabled
766 (or spam-split-disabled
767 spam-disable-spam-split-during-ham-respool
)))
768 (gnus-summary-respool-article nil respool-method
))
769 (if (or (not backend-supports-deletions
) ; else, we are not respooling
770 (> (length groups
) 1))
771 (progn ; if copying, copy and set deletep
772 (gnus-summary-copy-article nil group
)
774 (gnus-summary-move-article nil group
))))) ; else move articles
776 ;; now delete the articles, unless a) copy is t, and there was a copy done
777 ;; b) a move was done to a single group
778 ;; c) backend-supports-deletions is nil
780 (when (and deletep backend-supports-deletions
)
781 (dolist (article todo
)
782 (gnus-summary-set-process-mark article
))
784 (let ((gnus-novice-user nil
)) ; don't ask me if I'm sure
785 (gnus-summary-delete-article nil
))))))
787 (gnus-summary-yank-process-mark))
789 (defun spam-ham-copy-routine (&rest groups
)
790 (if (and (car-safe groups
) (listp (car-safe groups
)))
791 (apply 'spam-ham-copy-routine
(car groups
))
792 (spam-ham-copy-or-move-routine t groups
)))
794 (defun spam-ham-move-routine (&rest groups
)
795 (if (and (car-safe groups
) (listp (car-safe groups
)))
796 (apply 'spam-ham-move-routine
(car groups
))
797 (spam-ham-copy-or-move-routine nil groups
)))
800 (defalias 'spam-point-at-eol
(if (fboundp 'point-at-eol
)
802 'line-end-position
)))
804 (defun spam-get-article-as-string (article)
805 (let ((article-buffer (spam-get-article-as-buffer article
))
808 (save-window-excursion
809 (set-buffer article-buffer
)
810 (setq article-string
(buffer-string))))
813 (defun spam-get-article-as-buffer (article)
814 (let ((article-buffer))
815 (when (numberp article
)
816 (save-window-excursion
817 (gnus-summary-goto-subject article
)
818 (gnus-summary-show-article t
)
819 (setq article-buffer
(get-buffer gnus-article-buffer
))))
823 ;; (defun spam-get-article-as-filename (article)
824 ;; (let ((article-filename))
825 ;; (when (numberp article)
826 ;; (nnml-possibly-change-directory
827 ;; (gnus-group-real-name gnus-newsgroup-name))
828 ;; (setq article-filename (expand-file-name
829 ;; (int-to-string article) nnml-current-directory)))
830 ;; (if (file-exists-p article-filename)
834 (defun spam-fetch-field-from-fast (article)
835 "Fetch the `from' field quickly, using the internal gnus-data-list function"
836 (if (and (numberp article
)
837 (assoc article
(gnus-data-list nil
)))
839 (gnus-data-header (assoc article
(gnus-data-list nil
))))
842 (defun spam-fetch-field-subject-fast (article)
843 "Fetch the `subject' field quickly, using the internal
844 gnus-data-list function"
845 (if (and (numberp article
)
846 (assoc article
(gnus-data-list nil
)))
848 (gnus-data-header (assoc article
(gnus-data-list nil
))))
851 (defun spam-fetch-field-message-id-fast (article)
852 "Fetch the `Message-ID' field quickly, using the internal
853 gnus-data-list function"
854 (if (and (numberp article
)
855 (assoc article
(gnus-data-list nil
)))
856 (mail-header-message-id
857 (gnus-data-header (assoc article
(gnus-data-list nil
))))
861 ;;;; Spam determination.
863 (defvar spam-list-of-checks
864 '((spam-use-blacklist . spam-check-blacklist
)
865 (spam-use-regex-headers . spam-check-regex-headers
)
866 (spam-use-regex-body . spam-check-regex-body
)
867 (spam-use-whitelist . spam-check-whitelist
)
868 (spam-use-BBDB . spam-check-BBDB
)
869 (spam-use-ifile . spam-check-ifile
)
870 (spam-use-spamoracle . spam-check-spamoracle
)
871 (spam-use-stat . spam-check-stat
)
872 (spam-use-blackholes . spam-check-blackholes
)
873 (spam-use-hashcash . spam-check-hashcash
)
874 (spam-use-bogofilter-headers . spam-check-bogofilter-headers
)
875 (spam-use-bogofilter . spam-check-bogofilter
))
876 "The spam-list-of-checks list contains pairs associating a
877 parameter variable with a spam checking function. If the
878 parameter variable is true, then the checking function is called,
879 and its value decides what happens. Each individual check may
880 return nil, t, or a mailgroup name. The value nil means that the
881 check does not yield a decision, and so, that further checks are
882 needed. The value t means that the message is definitely not
883 spam, and that further spam checks should be inhibited.
884 Otherwise, a mailgroup name or the symbol 'spam (depending on
885 spam-split-symbolic-return) is returned where the mail should go,
886 and further checks are also inhibited. The usual mailgroup name
887 is the value of `spam-split-group', meaning that the message is
890 (defvar spam-list-of-statistical-checks
896 "The spam-list-of-statistical-checks list contains all the mail
897 splitters that need to have the full message body available.")
899 ;;;TODO: modify to invoke self with each check if invoked without specifics
900 (defun spam-split (&rest specific-checks
)
901 "Split this message into the `spam' group if it is spam.
902 This function can be used as an entry in the variable `nnmail-split-fancy',
903 for example like this: (: spam-split). It can take checks as
904 parameters. A string as a parameter will set the
905 spam-split-group to that string.
907 See the Info node `(gnus)Fancy Mail Splitting' for more details."
909 (setq spam-split-last-successful-check nil
)
910 (unless spam-split-disabled
911 (let ((spam-split-group-choice spam-split-group
))
912 (dolist (check specific-checks
)
913 (when (stringp check
)
914 (setq spam-split-group-choice check
)
915 (setq specific-checks
(delq check specific-checks
))))
917 (let ((spam-split-group spam-split-group-choice
))
920 (dolist (check spam-list-of-statistical-checks
)
921 (when (and (symbolp check
) (symbol-value check
))
923 (gnus-message 8 "spam-split: widening the buffer (%s requires it)"
926 ;; (progn (widen) (debug (buffer-string)))
927 (let ((list-of-checks spam-list-of-checks
)
929 (while (and list-of-checks
(not decision
))
930 (let ((pair (pop list-of-checks
)))
931 (when (and (symbol-value (car pair
))
932 (or (null specific-checks
)
933 (memq (car pair
) specific-checks
)))
934 (gnus-message 5 "spam-split: calling the %s function"
935 (symbol-name (cdr pair
)))
936 (setq decision
(funcall (cdr pair
)))
937 ;; if we got a decision at all, save the current check
939 (setq spam-split-last-successful-check
(car pair
)))
941 (when (eq decision
'spam
)
942 (if spam-split-symbolic-return
943 (setq decision spam-split-group
)
946 (format "spam-split got %s but %s is nil"
947 (symbol-name decision
)
948 (symbol-name spam-split-symbolic-return
))))))))
950 (if spam-split-symbolic-return-positive
'ham nil
)
953 (defun spam-find-spam ()
954 "This function will detect spam in the current newsgroup using spam-split."
957 (let* ((group gnus-newsgroup-name
)
958 (autodetect (gnus-parameter-spam-autodetect group
))
959 (methods (gnus-parameter-spam-autodetect-methods group
))
960 (first-method (nth 0 methods
)))
961 (when (and autodetect
962 (not (equal first-method
'none
)))
965 (let ((id (spam-fetch-field-message-id-fast article
))
966 (subject (spam-fetch-field-subject-fast article
))
967 (sender (spam-fetch-field-from-fast article
)))
968 (unless (and spam-log-to-registry
969 (spam-log-registered-p id
'incoming
))
970 (let* ((spam-split-symbolic-return t
)
971 (spam-split-symbolic-return-positive t
)
974 (gnus-request-article-this-buffer
977 (if (or (null first-method
)
978 (equal first-method
'default
))
980 (apply 'spam-split methods
)))))
981 (if (equal split-return
'spam
)
982 (gnus-summary-mark-article article gnus-spam-mark
))
984 (when (and split-return spam-log-to-registry
)
985 (when (zerop (gnus-registry-group-count id
))
986 (gnus-registry-add-group
987 id group subject sender
))
989 (spam-log-processing-to-registry
993 spam-split-last-successful-check
995 (if spam-autodetect-recheck-messages
996 gnus-newsgroup-articles
997 gnus-newsgroup-unseen
)))))
999 (defvar spam-registration-functions
1000 ;; first the ham register, second the spam register function
1001 ;; third the ham unregister, fourth the spam unregister function
1002 '((spam-use-blacklist nil
1003 spam-blacklist-register-routine
1005 spam-blacklist-unregister-routine
)
1006 (spam-use-whitelist spam-whitelist-register-routine
1008 spam-whitelist-unregister-routine
1010 (spam-use-BBDB spam-BBDB-register-routine
1012 spam-BBDB-unregister-routine
1014 (spam-use-ifile spam-ifile-register-ham-routine
1015 spam-ifile-register-spam-routine
1016 spam-ifile-unregister-ham-routine
1017 spam-ifile-unregister-spam-routine
)
1018 (spam-use-spamoracle spam-spamoracle-learn-ham
1019 spam-spamoracle-learn-spam
1020 spam-spamoracle-unlearn-ham
1021 spam-spamoracle-unlearn-spam
)
1022 (spam-use-stat spam-stat-register-ham-routine
1023 spam-stat-register-spam-routine
1024 spam-stat-unregister-ham-routine
1025 spam-stat-unregister-spam-routine
)
1026 ;; note that spam-use-gmane is not a legitimate check
1028 spam-report-gmane-register-routine
1029 ;; does Gmane support unregistration?
1032 (spam-use-bogofilter spam-bogofilter-register-ham-routine
1033 spam-bogofilter-register-spam-routine
1034 spam-bogofilter-unregister-ham-routine
1035 spam-bogofilter-unregister-spam-routine
))
1036 "The spam-registration-functions list contains pairs
1037 associating a parameter variable with the ham and spam
1038 registration functions, and the ham and spam unregistration
1041 (defun spam-classification-valid-p (classification)
1042 (or (eq classification
'spam
)
1043 (eq classification
'ham
)))
1045 (defun spam-process-type-valid-p (process-type)
1046 (or (eq process-type
'incoming
)
1047 (eq process-type
'process
)))
1049 (defun spam-registration-check-valid-p (check)
1050 (assoc check spam-registration-functions
))
1052 (defun spam-unregistration-check-valid-p (check)
1053 (assoc check spam-registration-functions
))
1055 (defun spam-registration-function (classification check
)
1056 (let ((flist (cdr-safe (assoc check spam-registration-functions
))))
1057 (if (eq classification
'spam
)
1061 (defun spam-unregistration-function (classification check
)
1062 (let ((flist (cdr-safe (assoc check spam-registration-functions
))))
1063 (if (eq classification
'spam
)
1067 (defun spam-list-articles (articles classification
)
1068 (let ((mark-check (if (eq classification
'spam
)
1069 'spam-group-spam-mark-p
1070 'spam-group-ham-mark-p
))
1071 list mark-cache-yes mark-cache-no
)
1072 (dolist (article articles
)
1073 (let ((mark (gnus-summary-article-mark article
)))
1074 (unless (memq mark mark-cache-no
)
1075 (if (memq mark mark-cache-yes
)
1077 ;; else, we have to actually check the mark
1078 (if (funcall mark-check
1083 (push mark mark-cache-yes
))
1084 (push mark mark-cache-no
))))))
1087 (defun spam-register-routine (classification
1089 &optional unregister
1091 (when (and (spam-classification-valid-p classification
)
1092 (spam-registration-check-valid-p check
))
1093 (let* ((register-function
1094 (spam-registration-function classification check
))
1095 (unregister-function
1096 (spam-unregistration-function classification check
))
1097 (run-function (if unregister
1100 (log-function (if unregister
1101 'spam-log-undo-registration
1102 'spam-log-processing-to-registry
))
1106 ;; make list of articles, using specific-articles if given
1107 (setq articles
(or specific-articles
1109 gnus-newsgroup-articles
1112 (gnus-message 5 "%s %d %s articles with classification %s, check %s"
1113 (if unregister
"Unregistering" "Registering")
1115 (if specific-articles
"specific" "")
1116 (symbol-name classification
)
1117 (symbol-name check
))
1118 (funcall run-function articles
)
1119 ;; now log all the registrations (or undo them, depending on unregister)
1120 (dolist (article articles
)
1121 (funcall log-function
1122 (spam-fetch-field-message-id-fast article
)
1126 gnus-newsgroup-name
))))))
1128 ;;; log a ham- or spam-processor invocation to the registry
1129 (defun spam-log-processing-to-registry (id type classification check group
)
1130 (when spam-log-to-registry
1131 (if (and (stringp id
)
1133 (spam-process-type-valid-p type
)
1134 (spam-classification-valid-p classification
)
1135 (spam-registration-check-valid-p check
))
1136 (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type
)))
1137 (cell (list classification check group
)))
1138 (push cell cell-list
)
1139 (gnus-registry-store-extra-entry
1144 (gnus-message 5 (format "%s called with bad ID, type, classification, check, or group"
1145 "spam-log-processing-to-registry")))))
1147 ;;; check if a ham- or spam-processor registration has been done
1148 (defun spam-log-registered-p (id type
)
1149 (when spam-log-to-registry
1150 (if (and (stringp id
)
1151 (spam-process-type-valid-p type
))
1152 (cdr-safe (gnus-registry-fetch-extra id type
))
1154 (gnus-message 5 (format "%s called with bad ID, type, classification, or check"
1155 "spam-log-registered-p"))
1158 ;;; check if a ham- or spam-processor registration needs to be undone
1159 (defun spam-log-unregistration-needed-p (id type classification check
)
1160 (when spam-log-to-registry
1161 (if (and (stringp id
)
1162 (spam-process-type-valid-p type
)
1163 (spam-classification-valid-p classification
)
1164 (spam-registration-check-valid-p check
))
1165 (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type
)))
1167 (dolist (cell cell-list
)
1169 (when (and (eq classification
(nth 0 cell
))
1170 (eq check
(nth 1 cell
)))
1174 (gnus-message 5 (format "%s called with bad ID, type, classification, or check"
1175 "spam-log-unregistration-needed-p"))
1179 ;;; undo a ham- or spam-processor registration (the group is not used)
1180 (defun spam-log-undo-registration (id type classification check
&optional group
)
1181 (when (and spam-log-to-registry
1182 (spam-log-unregistration-needed-p id type classification check
))
1183 (if (and (stringp id
)
1184 (spam-process-type-valid-p type
)
1185 (spam-classification-valid-p classification
)
1186 (spam-registration-check-valid-p check
))
1187 (let ((cell-list (cdr-safe (gnus-registry-fetch-extra id type
)))
1188 new-cell-list found
)
1189 (dolist (cell cell-list
)
1190 (unless (and (eq classification
(nth 0 cell
))
1191 (eq check
(nth 1 cell
)))
1192 (push cell new-cell-list
)))
1193 (gnus-registry-store-extra-entry
1198 (gnus-message 5 (format "%s called with bad ID, type, check, or group"
1199 "spam-log-undo-registration"))
1202 ;;; set up IMAP widening if it's necessary
1203 (defun spam-setup-widening ()
1204 (dolist (check spam-list-of-statistical-checks
)
1205 (when (symbol-value check
)
1206 (setq nnimap-split-download-body-default t
))))
1211 (defun spam-check-regex-body ()
1212 (let ((spam-regex-headers-ham spam-regex-body-ham
)
1213 (spam-regex-headers-spam spam-regex-body-spam
))
1214 (spam-check-regex-headers t
)))
1219 (defun spam-check-regex-headers (&optional body
)
1220 (let ((type (if body
"body" "header"))
1221 (spam-split-group (if spam-split-symbolic-return
1225 (dolist (h-regex spam-regex-headers-ham
)
1227 (goto-char (point-min))
1228 (when (re-search-forward h-regex nil t
)
1229 (message "Ham regex %s search positive." type
)
1231 (dolist (s-regex spam-regex-headers-spam
)
1233 (goto-char (point-min))
1234 (when (re-search-forward s-regex nil t
)
1235 (message "Spam regex %s search positive." type
)
1237 (setq ret spam-split-group
))))
1243 (defun spam-reverse-ip-string (ip)
1245 (mapconcat 'identity
1246 (nreverse (split-string ip
"\\."))
1249 (defun spam-check-blackholes ()
1250 "Check the Received headers for blackholed relays."
1251 (let ((headers (nnmail-fetch-field "received"))
1252 (spam-split-group (if spam-split-symbolic-return
1259 (goto-char (point-min))
1260 (gnus-message 5 "Checking headers for relay addresses")
1261 (while (re-search-forward
1262 "\\([0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+\\)" nil t
)
1263 (gnus-message 9 "Blackhole search found host IP %s." (match-string 1))
1264 (push (spam-reverse-ip-string (match-string 1))
1266 (dolist (server spam-blackhole-servers
)
1268 (unless (and spam-blackhole-good-server-regex
1269 ;; match the good-server-regex against the reversed (again) IP string
1271 spam-blackhole-good-server-regex
1272 (spam-reverse-ip-string ip
)))
1274 (let ((query-string (concat ip
"." server
)))
1276 (let ((query-result (query-dig query-string
)))
1278 (gnus-message 5 "(DIG): positive blackhole check '%s'"
1280 (push (list ip server query-result
)
1282 ;; else, if not using dig.el
1283 (when (query-dns query-string
)
1284 (gnus-message 5 "positive blackhole check")
1285 (push (list ip server
(query-dns query-string
'TXT
))
1293 (autoload 'mail-check-payment
"hashcash"))
1299 (defun spam-check-hashcash ()
1300 "Check the headers for hashcash payments."
1301 (mail-check-payment))) ;mail-check-payment returns a boolean
1307 ;;; original idea for spam-check-BBDB from Alexander Kotelnikov
1308 ;;; <sacha@giotto.sj.ru>
1310 ;; all this is done inside a condition-case to trap errors
1313 (autoload 'bbdb-buffer
"bbdb")
1314 (autoload 'bbdb-create-internal
"bbdb")
1315 (autoload 'bbdb-search-simple
"bbdb"))
1318 (when (condition-case nil
1321 (require 'bbdb-com
))
1323 (defalias 'spam-BBDB-register-routine
'ignore
)
1324 (defalias 'spam-enter-ham-BBDB
'ignore
)
1327 (defun spam-enter-ham-BBDB (addresses &optional remove
)
1328 "Enter an address into the BBDB; implies ham (non-spam) sender"
1329 (dolist (from addresses
)
1330 (when (stringp from
)
1331 (let* ((parsed-address (gnus-extract-address-components from
))
1332 (name (or (nth 0 parsed-address
) "Ham Sender"))
1333 (remove-function (if remove
1334 'bbdb-delete-record-internal
1336 (net-address (nth 1 parsed-address
))
1337 (record (and net-address
1338 (bbdb-search-simple nil net-address
))))
1340 (gnus-message 5 "%s address %s %s BBDB"
1341 (if remove
"Deleting" "Adding")
1343 (if remove
"from" "to"))
1345 (funcall remove-function record
)
1346 (bbdb-create-internal name nil net-address nil nil
1347 "ham sender added by spam.el")))))))
1349 (defun spam-BBDB-register-routine (articles &optional unregister
)
1351 (dolist (article articles
)
1352 (when (stringp (spam-fetch-field-from-fast article
))
1353 (push (spam-fetch-field-from-fast article
) addresses
)))
1354 ;; now do the register/unregister action
1355 (spam-enter-ham-BBDB addresses unregister
)))
1357 (defun spam-BBDB-unregister-routine (articles)
1358 (spam-BBDB-register-routine articles t
))
1360 (defun spam-check-BBDB ()
1361 "Mail from people in the BBDB is classified as ham or non-spam"
1362 (let ((who (nnmail-fetch-field "from"))
1363 (spam-split-group (if spam-split-symbolic-return
1367 (setq who
(nth 1 (gnus-extract-address-components who
)))
1368 (if (bbdb-search-simple nil who
)
1370 (if spam-use-BBDB-exclusive
1377 ;;; check the ifile backend; return nil if the mail was NOT classified
1381 (defun spam-get-ifile-database-parameter ()
1382 "Return the command-line parameter for ifile's database.
1383 See `spam-ifile-database'."
1384 (if spam-ifile-database
1385 (format "--db-file=%s" spam-ifile-database
)
1388 (defun spam-check-ifile ()
1389 "Check the ifile backend for the classification of this message."
1390 (let ((article-buffer-name (buffer-name))
1391 (spam-split-group (if spam-split-symbolic-return
1396 (let ((temp-buffer-name (buffer-name))
1397 (db-param (spam-get-ifile-database-parameter)))
1399 (set-buffer article-buffer-name
)
1400 (apply 'call-process-region
1401 (point-min) (point-max) spam-ifile-program
1402 nil temp-buffer-name nil
"-c"
1403 (if db-param
`(,db-param
"-q") `("-q"))))
1404 ;; check the return now (we're back in the temp buffer)
1405 (goto-char (point-min))
1407 (setq category
(buffer-substring (point) (spam-point-at-eol))))
1408 (when (not (zerop (length category
))) ; we need a category here
1409 (if spam-ifile-all-categories
1410 (setq return category
)
1411 ;; else, if spam-ifile-all-categories is not set...
1412 (when (string-equal spam-ifile-spam-category category
)
1413 (setq return spam-split-group
)))))) ; note return is nil otherwise
1416 (defun spam-ifile-register-with-ifile (articles category
&optional unregister
)
1417 "Register an article, given as a string, with a category.
1418 Uses `gnus-newsgroup-name' if category is nil (for ham registration)."
1419 (let ((category (or category gnus-newsgroup-name
))
1420 (add-or-delete-option (if unregister
"-d" "-i"))
1421 (db (spam-get-ifile-database-parameter))
1424 (dolist (article articles
)
1425 (let ((article-string (spam-get-article-as-string article
)))
1426 (when (stringp article-string
)
1427 (insert article-string
))))
1428 (apply 'call-process-region
1429 (point-min) (point-max) spam-ifile-program
1431 add-or-delete-option category
1432 (if db
`(,db
"-h") `("-h"))))))
1434 (defun spam-ifile-register-spam-routine (articles &optional unregister
)
1435 (spam-ifile-register-with-ifile articles spam-ifile-spam-category unregister
))
1437 (defun spam-ifile-unregister-spam-routine (articles)
1438 (spam-ifile-register-spam-routine articles t
))
1440 (defun spam-ifile-register-ham-routine (articles &optional unregister
)
1441 (spam-ifile-register-with-ifile articles spam-ifile-ham-category unregister
))
1443 (defun spam-ifile-unregister-ham-routine (articles)
1444 (spam-ifile-register-ham-routine articles t
))
1450 (autoload 'spam-stat-buffer-change-to-non-spam
"spam-stat")
1451 (autoload 'spam-stat-buffer-change-to-spam
"spam-stat")
1452 (autoload 'spam-stat-buffer-is-non-spam
"spam-stat")
1453 (autoload 'spam-stat-buffer-is-spam
"spam-stat")
1454 (autoload 'spam-stat-load
"spam-stat")
1455 (autoload 'spam-stat-save
"spam-stat")
1456 (autoload 'spam-stat-split-fancy
"spam-stat"))
1459 (when (condition-case nil
1460 (let ((spam-stat-install-hooks nil
))
1461 (require 'spam-stat
))
1463 (defalias 'spam-stat-register-ham-routine
'ignore
)
1464 (defalias 'spam-stat-register-spam-routine
'ignore
)
1467 (defun spam-check-stat ()
1468 "Check the spam-stat backend for the classification of this message"
1469 (let ((spam-split-group (if spam-split-symbolic-return
1472 (spam-stat-split-fancy-spam-group spam-split-group
) ; override
1473 (spam-stat-buffer (buffer-name)) ; stat the current buffer
1475 (spam-stat-split-fancy)))
1477 (defun spam-stat-register-spam-routine (articles &optional unregister
)
1478 (dolist (article articles
)
1479 (let ((article-string (spam-get-article-as-string article
)))
1481 (insert article-string
)
1483 (spam-stat-buffer-change-to-non-spam)
1484 (spam-stat-buffer-is-spam))))))
1486 (defun spam-stat-unregister-spam-routine (articles)
1487 (spam-stat-register-spam-routine articles t
))
1489 (defun spam-stat-register-ham-routine (articles &optional unregister
)
1490 (dolist (article articles
)
1491 (let ((article-string (spam-get-article-as-string article
)))
1493 (insert article-string
)
1495 (spam-stat-buffer-change-to-spam)
1496 (spam-stat-buffer-is-non-spam))))))
1498 (defun spam-stat-unregister-ham-routine (articles)
1499 (spam-stat-register-ham-routine articles t
))
1501 (defun spam-maybe-spam-stat-load ()
1502 (when spam-use-stat
(spam-stat-load)))
1504 (defun spam-maybe-spam-stat-save ()
1505 (when spam-use-stat
(spam-stat-save)))))
1509 ;;;; Blacklists and whitelists.
1511 (defvar spam-whitelist-cache nil
)
1512 (defvar spam-blacklist-cache nil
)
1514 (defun spam-kill-whole-line ()
1516 (let ((kill-whole-line t
))
1519 ;;; address can be a list, too
1520 (defun spam-enter-whitelist (address &optional remove
)
1521 "Enter ADDRESS (list or single) into the whitelist.
1522 With a non-nil REMOVE, remove them."
1523 (interactive "sAddress: ")
1524 (spam-enter-list address spam-whitelist remove
)
1525 (setq spam-whitelist-cache nil
))
1527 ;;; address can be a list, too
1528 (defun spam-enter-blacklist (address &optional remove
)
1529 "Enter ADDRESS (list or single) into the blacklist.
1530 With a non-nil REMOVE, remove them."
1531 (interactive "sAddress: ")
1532 (spam-enter-list address spam-blacklist remove
)
1533 (setq spam-blacklist-cache nil
))
1535 (defun spam-enter-list (addresses file
&optional remove
)
1536 "Enter ADDRESSES into the given FILE.
1537 Either the whitelist or the blacklist files can be used. With
1538 REMOVE not nil, remove the ADDRESSES."
1539 (if (stringp addresses
)
1540 (spam-enter-list (list addresses
) file remove
)
1541 ;; else, we have a list of addresses here
1542 (unless (file-exists-p (file-name-directory file
))
1543 (make-directory (file-name-directory file
) t
))
1546 (find-file-noselect file
))
1547 (dolist (a addresses
)
1549 (goto-char (point-min))
1550 (if (re-search-forward (regexp-quote a
) nil t
)
1551 ;; found the address
1553 (spam-kill-whole-line))
1554 ;; else, the address was not found
1556 (goto-char (point-max))
1562 ;;; returns t if the sender is in the whitelist, nil or
1563 ;;; spam-split-group otherwise
1564 (defun spam-check-whitelist ()
1565 ;; FIXME! Should it detect when file timestamps change?
1566 (let ((spam-split-group (if spam-split-symbolic-return
1569 (unless spam-whitelist-cache
1570 (setq spam-whitelist-cache
(spam-parse-list spam-whitelist
)))
1571 (if (spam-from-listed-p spam-whitelist-cache
)
1573 (if spam-use-whitelist-exclusive
1577 (defun spam-check-blacklist ()
1578 ;; FIXME! Should it detect when file timestamps change?
1579 (let ((spam-split-group (if spam-split-symbolic-return
1582 (unless spam-blacklist-cache
1583 (setq spam-blacklist-cache
(spam-parse-list spam-blacklist
)))
1584 (and (spam-from-listed-p spam-blacklist-cache
) spam-split-group
)))
1586 (defun spam-parse-list (file)
1587 (when (file-readable-p file
)
1588 (let (contents address
)
1590 (insert-file-contents file
)
1592 (setq address
(buffer-substring (point) (spam-point-at-eol)))
1594 ;; insert the e-mail address if detected, otherwise the raw data
1595 (unless (zerop (length address
))
1596 (let ((pure-address (nth 1 (gnus-extract-address-components address
))))
1597 (push (or pure-address address
) contents
)))))
1598 (nreverse contents
))))
1600 (defun spam-from-listed-p (cache)
1601 (let ((from (nnmail-fetch-field "from"))
1604 (let ((address (pop cache
)))
1605 (unless (zerop (length address
)) ; 0 for a nil address too
1606 (setq address
(regexp-quote address
))
1607 ;; fix regexp-quote's treatment of user-intended regexes
1608 (while (string-match "\\\\\\*" address
)
1609 (setq address
(replace-match ".*" t t address
))))
1610 (when (and address
(string-match address from
))
1615 (defun spam-filelist-register-routine (articles blacklist
&optional unregister
)
1616 (let ((de-symbol (if blacklist
'spam-use-whitelist
'spam-use-blacklist
))
1617 (declassification (if blacklist
'ham
'spam
))
1619 (if blacklist
'spam-enter-blacklist
'spam-enter-whitelist
))
1621 (if blacklist
'spam-enter-whitelist
'spam-enter-blacklist
))
1622 from addresses unregister-list
)
1623 (dolist (article articles
)
1624 (let ((from (spam-fetch-field-from-fast article
))
1625 (id (spam-fetch-field-message-id-fast article
))
1627 (when (stringp from
)
1628 (dolist (ignore-regex spam-blacklist-ignored-regexes
)
1629 (when (and (not sender-ignored
)
1630 (stringp ignore-regex
)
1631 (string-match ignore-regex from
))
1632 (setq sender-ignored t
)))
1633 ;; remember the messages we need to unregister, unless remove is set
1636 (spam-log-unregistration-needed-p
1637 id
'process declassification de-symbol
))
1638 (push from unregister-list
))
1639 (unless sender-ignored
1640 (push from addresses
)))))
1643 (funcall enter-function addresses t
) ; unregister all these addresses
1644 ;; else, register normally and unregister what we need to
1645 (funcall remove-function unregister-list t
)
1646 (dolist (article unregister-list
)
1647 (spam-log-undo-registration
1648 (spam-fetch-field-message-id-fast article
)
1652 (funcall enter-function addresses nil
))))
1654 (defun spam-blacklist-unregister-routine (articles)
1655 (spam-blacklist-register-routine articles t
))
1657 (defun spam-blacklist-register-routine (articles &optional unregister
)
1658 (spam-filelist-register-routine articles t unregister
))
1660 (defun spam-whitelist-unregister-routine (articles)
1661 (spam-whitelist-register-routine articles t
))
1663 (defun spam-whitelist-register-routine (articles &optional unregister
)
1664 (spam-filelist-register-routine articles nil unregister
))
1667 ;;;; Spam-report glue
1668 (defun spam-report-gmane-register-routine (articles)
1670 (apply 'spam-report-gmane articles
)))
1674 (defun spam-check-bogofilter-headers (&optional score
)
1675 (let ((header (nnmail-fetch-field spam-bogofilter-header
))
1676 (spam-split-group (if spam-split-symbolic-return
1679 (when header
; return nil when no header
1680 (if score
; scoring mode
1681 (if (string-match "spamicity=\\([0-9.]+\\)" header
)
1682 (match-string 1 header
)
1684 ;; spam detection mode
1685 (when (string-match spam-bogofilter-bogosity-positive-spam-header
1687 spam-split-group
)))))
1689 ;; return something sensible if the score can't be determined
1690 (defun spam-bogofilter-score ()
1691 "Get the Bogofilter spamicity score"
1693 (save-window-excursion
1694 (gnus-summary-show-article t
)
1695 (set-buffer gnus-article-buffer
)
1696 (let ((score (or (spam-check-bogofilter-headers t
)
1697 (spam-check-bogofilter t
))))
1698 (message "Spamicity score %s" score
)
1700 (gnus-summary-show-article)))
1702 (defun spam-check-bogofilter (&optional score
)
1703 "Check the Bogofilter backend for the classification of this message"
1704 (let ((article-buffer-name (buffer-name))
1705 (db spam-bogofilter-database-directory
)
1708 (let ((temp-buffer-name (buffer-name)))
1710 (set-buffer article-buffer-name
)
1711 (apply 'call-process-region
1712 (point-min) (point-max)
1713 spam-bogofilter-program
1714 nil temp-buffer-name nil
1715 (if db
`("-d" ,db
"-v") `("-v"))))
1716 (setq return
(spam-check-bogofilter-headers score
))))
1719 (defun spam-bogofilter-register-with-bogofilter (articles
1721 &optional unregister
)
1722 "Register an article, given as a string, as spam or non-spam."
1723 (dolist (article articles
)
1724 (let ((article-string (spam-get-article-as-string article
))
1725 (db spam-bogofilter-database-directory
)
1726 (switch (if unregister
1728 spam-bogofilter-spam-strong-switch
1729 spam-bogofilter-ham-strong-switch
)
1731 spam-bogofilter-spam-switch
1732 spam-bogofilter-ham-switch
))))
1733 (when (stringp article-string
)
1735 (insert article-string
)
1737 (apply 'call-process-region
1738 (point-min) (point-max)
1739 spam-bogofilter-program
1741 (if db
`("-d" ,db
"-v") `("-v"))))))))
1743 (defun spam-bogofilter-register-spam-routine (articles &optional unregister
)
1744 (spam-bogofilter-register-with-bogofilter articles t unregister
))
1746 (defun spam-bogofilter-unregister-spam-routine (articles)
1747 (spam-bogofilter-register-spam-routine articles t
))
1749 (defun spam-bogofilter-register-ham-routine (articles &optional unregister
)
1750 (spam-bogofilter-register-with-bogofilter articles nil unregister
))
1752 (defun spam-bogofilter-unregister-ham-routine (articles)
1753 (spam-bogofilter-register-ham-routine articles t
))
1758 (defun spam-check-spamoracle ()
1759 "Run spamoracle on an article to determine whether it's spam."
1760 (let ((article-buffer-name (buffer-name))
1761 (spam-split-group (if spam-split-symbolic-return
1765 (let ((temp-buffer-name (buffer-name)))
1767 (set-buffer article-buffer-name
)
1769 (apply 'call-process-region
1770 (point-min) (point-max)
1771 spam-spamoracle-binary
1772 nil temp-buffer-name nil
1773 (if spam-spamoracle-database
1774 `("-f" ,spam-spamoracle-database
"mark")
1778 (set-buffer temp-buffer-name
)
1779 (goto-char (point-min))
1780 (when (re-search-forward "^X-Spam: yes;" nil t
)
1782 (error "Error running spamoracle: %s" status
))))))))
1784 (defun spam-spamoracle-learn (articles article-is-spam-p
&optional unregister
)
1785 "Run spamoracle in training mode."
1787 (let ((temp-buffer-name (buffer-name)))
1789 (goto-char (point-min))
1790 (dolist (article articles
)
1791 (insert (spam-get-article-as-string article
)))
1792 (let* ((arg (if (spam-xor unregister article-is-spam-p
)
1796 (apply 'call-process-region
1797 (point-min) (point-max)
1798 spam-spamoracle-binary
1799 nil temp-buffer-name nil
1800 (if spam-spamoracle-database
1801 `("-f" ,spam-spamoracle-database
1804 (unless (eq 0 status
)
1805 (error "Error running spamoracle: %s" status
)))))))
1807 (defun spam-spamoracle-learn-ham (articles &optional unregister
)
1808 (spam-spamoracle-learn articles nil unregister
))
1810 (defun spam-spamoracle-unlearn-ham (articles &optional unregister
)
1811 (spam-spamoracle-learn-ham articles t
))
1813 (defun spam-spamoracle-learn-spam (articles &optional unregister
)
1814 (spam-spamoracle-learn articles t unregister
))
1816 (defun spam-spamoracle-unlearn-spam (articles &optional unregister
)
1817 (spam-spamoracle-learn-spam articles t
))
1823 (defun spam-initialize ()
1824 "Install the spam.el hooks and do other initialization"
1826 (setq spam-install-hooks t
)
1827 ;; TODO: How do we redo this every time the `spam' face is customized?
1828 (push '((eq mark gnus-spam-mark
) . spam
)
1829 gnus-summary-highlight
)
1830 ;; Add hooks for loading and saving the spam stats
1831 (add-hook 'gnus-save-newsrc-hook
'spam-maybe-spam-stat-save
)
1832 (add-hook 'gnus-get-top-new-news-hook
'spam-maybe-spam-stat-load
)
1833 (add-hook 'gnus-startup-hook
'spam-maybe-spam-stat-load
)
1834 (add-hook 'gnus-summary-prepare-exit-hook
'spam-summary-prepare-exit
)
1835 (add-hook 'gnus-summary-prepare-hook
'spam-summary-prepare
)
1836 (add-hook 'gnus-get-new-news-hook
'spam-setup-widening
)
1837 (add-hook 'gnus-summary-prepare-hook
'spam-find-spam
))
1839 (defun spam-unload-hook ()
1840 "Uninstall the spam.el hooks"
1842 (remove-hook 'gnus-save-newsrc-hook
'spam-maybe-spam-stat-save
)
1843 (remove-hook 'gnus-get-top-new-news-hook
'spam-maybe-spam-stat-load
)
1844 (remove-hook 'gnus-startup-hook
'spam-maybe-spam-stat-load
)
1845 (remove-hook 'gnus-summary-prepare-exit-hook
'spam-summary-prepare-exit
)
1846 (remove-hook 'gnus-summary-prepare-hook
'spam-summary-prepare
)
1847 (remove-hook 'gnus-get-new-news-hook
'spam-setup-widening
)
1848 (remove-hook 'gnus-summary-prepare-hook
'spam-find-spam
))
1850 (add-hook 'spam-unload-hook
'spam-unload-hook
)
1852 (when spam-install-hooks
1857 ;;; arch-tag: 07e6e0ca-ab0a-4412-b445-1f6c72a4f27f
1858 ;;; spam.el ends here