1 ;;; spam.el --- Identifying spam
2 ;; Copyright (C) 2002, 2003 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
26 ;;; This module addresses a few aspects of spam control under Gnus. Page
27 ;;; breaks are used for grouping declarations and documentation relating to
28 ;;; each particular aspect.
30 ;;; The integration with Gnus is not yet complete. See various `FIXME'
31 ;;; comments, below, for supplementary explanations or discussions.
33 ;;; Several TODO items are marked as such
37 (eval-when-compile (require 'cl))
41 (require 'gnus-uu) ; because of key prefix issues
42 (require 'gnus) ; for the definitions of group content classification and spam processors
43 (require 'message) ;for the message-fetch-field functions
45 ;; for nnimap-split-download-body-default
46 (eval-when-compile (require 'nnimap))
48 ;; autoload executable-find
50 ;; executable-find is not autoloaded in Emacs 20
51 (autoload 'executable-find "executable"))
55 (autoload 'query-dig "dig"))
57 ;; autoload spam-report
59 (autoload 'spam-report-gmane "spam-report"))
63 (autoload 'query-dns "dns"))
68 "Spam configuration.")
70 (defcustom spam-directory "~/News/spam/"
71 "Directory for spam whitelists and blacklists."
75 (defcustom spam-move-spam-nonspam-groups-only t
76 "Whether spam should be moved in non-spam groups only.
77 When nil, only ham and unclassified groups will have their spam moved
78 to the spam-process-destination. When t, spam will also be moved from
83 (defcustom spam-process-ham-in-nonham-groups nil
84 "Whether ham should be processed in non-ham groups."
88 (defcustom spam-process-ham-in-spam-groups nil
89 "Whether ham should be processed in spam groups."
93 (defcustom spam-mark-only-unseen-as-spam t
94 "Whether only unseen articles should be marked as spam in spam
95 groups. When nil, all unread articles in a spam group are marked as
96 spam. Set this if you want to leave an article unread in a spam group
97 without losing it to the automatic spam-marking process."
101 (defcustom spam-mark-ham-unread-before-move-from-spam-group nil
102 "Whether ham should be marked unread before it's moved out of a spam
103 group according to ham-process-destination. This variable is an
104 official entry in the international Longest Variable Name
109 (defcustom spam-whitelist (expand-file-name "whitelist" spam-directory)
110 "The location of the whitelist.
111 The file format is one regular expression per line.
112 The regular expression is matched against the address."
116 (defcustom spam-blacklist (expand-file-name "blacklist" spam-directory)
117 "The location of the blacklist.
118 The file format is one regular expression per line.
119 The regular expression is matched against the address."
123 (defcustom spam-use-dig t
124 "Whether query-dig should be used instead of query-dns."
128 (defcustom spam-use-blacklist nil
129 "Whether the blacklist should be used by spam-split."
133 (defcustom spam-use-whitelist nil
134 "Whether the whitelist should be used by spam-split."
138 (defcustom spam-use-whitelist-exclusive nil
139 "Whether whitelist-exclusive should be used by spam-split.
140 Exclusive whitelisting means that all messages from senders not in the whitelist
141 are considered spam."
145 (defcustom spam-use-blackholes nil
146 "Whether blackholes should be used by spam-split."
150 (defcustom spam-use-hashcash nil
151 "Whether hashcash payments should be detected by spam-split."
155 (defcustom spam-use-regex-headers nil
156 "Whether a header regular expression match should be used by spam-split.
157 Also see the variables `spam-regex-headers-spam' and `spam-regex-headers-ham'."
161 (defcustom spam-use-bogofilter-headers nil
162 "Whether bogofilter headers should be used by spam-split.
163 Enable this if you pre-process messages with Bogofilter BEFORE Gnus sees them."
167 (defcustom spam-use-bogofilter nil
168 "Whether bogofilter should be invoked by spam-split.
169 Enable this if you want Gnus to invoke Bogofilter on new messages."
173 (defcustom spam-use-BBDB nil
174 "Whether BBDB should be used by spam-split."
178 (defcustom spam-use-BBDB-exclusive nil
179 "Whether BBDB-exclusive should be used by spam-split.
180 Exclusive BBDB means that all messages from senders not in the BBDB are
185 (defcustom spam-use-ifile nil
186 "Whether ifile should be used by spam-split."
190 (defcustom spam-use-stat nil
191 "Whether spam-stat should be used by spam-split."
195 (defcustom spam-use-spamoracle nil
196 "Whether spamoracle should be used by spam-split."
200 (defcustom spam-install-hooks (or
204 spam-use-whitelist-exclusive
207 spam-use-regex-headers
208 spam-use-bogofilter-headers
211 spam-use-BBDB-exclusive
215 "Whether the spam hooks should be installed, default to t if one of
216 the spam-use-* variables is set."
217 :group 'gnus-registry
220 (defcustom spam-split-group "spam"
221 "Group name where incoming spam should be put by spam-split."
225 ;;; TODO: deprecate this variable, it's confusing since it's a list of strings, not regular expressions
226 (defcustom spam-junk-mailgroups (cons spam-split-group '("mail.junk" "poste.pourriel"))
227 "Mailgroups with spam contents.
228 All unmarked article in such group receive the spam mark on group entry."
229 :type '(repeat (string :tag "Group"))
232 (defcustom spam-blackhole-servers '("bl.spamcop.net" "relays.ordb.org"
233 "dev.null.dk" "relays.visi.com")
234 "List of blackhole servers."
235 :type '(repeat (string :tag "Server"))
238 (defcustom spam-blackhole-good-server-regex nil
239 "String matching IP addresses that should not be checked in the blackholes"
243 (defcustom spam-face 'gnus-splash-face
244 "Face for spam-marked articles"
248 (defcustom spam-regex-headers-spam '("^X-Spam-Flag: YES")
249 "Regular expression for positive header spam matches"
250 :type '(repeat (regexp :tag "Regular expression to match spam header"))
253 (defcustom spam-regex-headers-ham '("^X-Spam-Flag: NO")
254 "Regular expression for positive header ham matches"
255 :type '(repeat (regexp :tag "Regular expression to match ham header"))
258 (defgroup spam-ifile nil
259 "Spam ifile configuration."
262 (defcustom spam-ifile-path (executable-find "ifile")
263 "File path of the ifile executable program."
264 :type '(choice (file :tag "Location of ifile")
265 (const :tag "ifile is not installed"))
268 (defcustom spam-ifile-database-path nil
269 "File path of the ifile database."
270 :type '(choice (file :tag "Location of the ifile database")
271 (const :tag "Use the default"))
274 (defcustom spam-ifile-spam-category "spam"
275 "Name of the spam ifile category."
279 (defcustom spam-ifile-ham-category nil
280 "Name of the ham ifile category. If nil, the current group name will
282 :type '(choice (string :tag "Use a fixed category")
283 (const :tag "Use the current group name"))
286 (defcustom spam-ifile-all-categories nil
287 "Whether the ifile check will return all categories, or just spam.
288 Set this to t if you want to use the spam-split invocation of ifile as
289 your main source of newsgroup names."
293 (defgroup spam-bogofilter nil
294 "Spam bogofilter configuration."
297 (defcustom spam-bogofilter-path (executable-find "bogofilter")
298 "File path of the Bogofilter executable program."
299 :type '(choice (file :tag "Location of bogofilter")
300 (const :tag "Bogofilter is not installed"))
301 :group 'spam-bogofilter)
303 (defcustom spam-bogofilter-header "X-Bogosity"
304 "The header that Bogofilter inserts in messages."
306 :group 'spam-bogofilter)
308 (defcustom spam-bogofilter-spam-switch "-s"
309 "The switch that Bogofilter uses to register spam messages."
311 :group 'spam-bogofilter)
313 (defcustom spam-bogofilter-ham-switch "-n"
314 "The switch that Bogofilter uses to register ham messages."
316 :group 'spam-bogofilter)
318 (defcustom spam-bogofilter-bogosity-positive-spam-header "^\\(Yes\\|Spam\\)"
319 "The regex on `spam-bogofilter-header' for positive spam identification."
321 :group 'spam-bogofilter)
323 (defcustom spam-bogofilter-database-directory nil
324 "Directory path of the Bogofilter databases."
325 :type '(choice (directory :tag "Location of the Bogofilter database directory")
326 (const :tag "Use the default"))
329 (defgroup spam-spamoracle nil
330 "Spam ifile configuration."
333 (defcustom spam-spamoracle-database nil
334 "Location of spamoracle database file. When nil, use the default
335 spamoracle database."
336 :type '(choice (directory :tag "Location of spamoracle database file.")
337 (const :tag "Use the default"))
338 :group 'spam-spamoracle)
340 (defcustom spam-spamoracle-binary (executable-find "spamoracle")
341 "Location of the spamoracle binary."
342 :type '(choice (directory :tag "Location of the spamoracle binary")
343 (const :tag "Use the default"))
344 :group 'spam-spamoracle)
346 ;;; Key bindings for spam control.
348 (gnus-define-keys gnus-summary-mode-map
349 "St" spam-bogofilter-score
350 "Sx" gnus-summary-mark-as-spam
351 "Mst" spam-bogofilter-score
352 "Msx" gnus-summary-mark-as-spam
353 "\M-d" gnus-summary-mark-as-spam)
355 ;;; How to highlight a spam summary line.
357 ;; TODO: How do we redo this every time spam-face is customized?
359 (push '((eq mark gnus-spam-mark) . spam-face)
360 gnus-summary-highlight)
362 ;; convenience functions
363 (defun spam-group-ham-mark-p (group mark &optional spam)
364 (when (stringp group)
365 (let* ((marks (spam-group-ham-marks group spam))
366 (marks (if (symbolp mark)
368 (mapcar 'symbol-value marks))))
371 (defun spam-group-spam-mark-p (group mark)
372 (spam-group-ham-mark-p group mark t))
374 (defun spam-group-ham-marks (group &optional spam)
375 (when (stringp group)
376 (let* ((marks (if spam
377 (gnus-parameter-spam-marks group)
378 (gnus-parameter-ham-marks group)))
380 (marks (if (listp (car marks)) (car marks) marks)))
383 (defun spam-group-spam-marks (group)
384 (spam-group-ham-marks group t))
386 (defun spam-group-spam-contents-p (group)
388 (or (member group spam-junk-mailgroups)
389 (memq 'gnus-group-spam-classification-spam
390 (gnus-parameter-spam-contents group)))
393 (defun spam-group-ham-contents-p (group)
395 (memq 'gnus-group-spam-classification-ham
396 (gnus-parameter-spam-contents group))
399 (defun spam-group-processor-p (group processor)
400 (if (and (stringp group)
402 (member processor (car (gnus-parameter-spam-process group)))
405 (defun spam-group-spam-processor-report-gmane-p (group)
406 (spam-group-processor-p group 'gnus-group-spam-exit-processor-report-gmane))
408 (defun spam-group-spam-processor-bogofilter-p (group)
409 (spam-group-processor-p group 'gnus-group-spam-exit-processor-bogofilter))
411 (defun spam-group-spam-processor-blacklist-p (group)
412 (spam-group-processor-p group 'gnus-group-spam-exit-processor-blacklist))
414 (defun spam-group-spam-processor-ifile-p (group)
415 (spam-group-processor-p group 'gnus-group-spam-exit-processor-ifile))
417 (defun spam-group-ham-processor-ifile-p (group)
418 (spam-group-processor-p group 'gnus-group-ham-exit-processor-ifile))
420 (defun spam-group-spam-processor-spamoracle-p (group)
421 (spam-group-processor-p group 'gnus-group-spam-exit-processor-spamoracle))
423 (defun spam-group-ham-processor-bogofilter-p (group)
424 (spam-group-processor-p group 'gnus-group-ham-exit-processor-bogofilter))
426 (defun spam-group-spam-processor-stat-p (group)
427 (spam-group-processor-p group 'gnus-group-spam-exit-processor-stat))
429 (defun spam-group-ham-processor-stat-p (group)
430 (spam-group-processor-p group 'gnus-group-ham-exit-processor-stat))
432 (defun spam-group-ham-processor-whitelist-p (group)
433 (spam-group-processor-p group 'gnus-group-ham-exit-processor-whitelist))
435 (defun spam-group-ham-processor-BBDB-p (group)
436 (spam-group-processor-p group 'gnus-group-ham-exit-processor-BBDB))
438 (defun spam-group-ham-processor-copy-p (group)
439 (spam-group-processor-p group 'gnus-group-ham-exit-processor-copy))
441 (defun spam-group-ham-processor-spamoracle-p (group)
442 (spam-group-processor-p group 'gnus-group-ham-exit-processor-spamoracle))
444 ;;; Summary entry and exit processing.
446 (defun spam-summary-prepare ()
447 (spam-mark-junk-as-spam-routine))
449 ;; The spam processors are invoked for any group, spam or ham or neither
450 (defun spam-summary-prepare-exit ()
451 (unless gnus-group-is-exiting-without-update-p
452 (gnus-message 6 "Exiting summary buffer and applying spam rules")
453 (when (and spam-bogofilter-path
454 (spam-group-spam-processor-bogofilter-p gnus-newsgroup-name))
455 (gnus-message 5 "Registering spam with bogofilter")
456 (spam-bogofilter-register-spam-routine))
458 (when (and spam-ifile-path
459 (spam-group-spam-processor-ifile-p gnus-newsgroup-name))
460 (gnus-message 5 "Registering spam with ifile")
461 (spam-ifile-register-spam-routine))
463 (when (spam-group-spam-processor-spamoracle-p gnus-newsgroup-name)
464 (gnus-message 5 "Registering spam with spamoracle")
465 (spam-spamoracle-learn-spam))
467 (when (spam-group-spam-processor-stat-p gnus-newsgroup-name)
468 (gnus-message 5 "Registering spam with spam-stat")
469 (spam-stat-register-spam-routine))
471 (when (spam-group-spam-processor-blacklist-p gnus-newsgroup-name)
472 (gnus-message 5 "Registering spam with the blacklist")
473 (spam-blacklist-register-routine))
475 (when (spam-group-spam-processor-report-gmane-p gnus-newsgroup-name)
476 (gnus-message 5 "Registering spam with the Gmane report")
477 (spam-report-gmane-register-routine))
479 (if spam-move-spam-nonspam-groups-only
480 (when (not (spam-group-spam-contents-p gnus-newsgroup-name))
481 (spam-mark-spam-as-expired-and-move-routine
482 (gnus-parameter-spam-process-destination gnus-newsgroup-name)))
483 (gnus-message 5 "Marking spam as expired and moving it to %s" gnus-newsgroup-name)
484 (spam-mark-spam-as-expired-and-move-routine
485 (gnus-parameter-spam-process-destination gnus-newsgroup-name)))
487 ;; now we redo spam-mark-spam-as-expired-and-move-routine to only
488 ;; expire spam, in case the above did not expire them
489 (gnus-message 5 "Marking spam as expired without moving it")
490 (spam-mark-spam-as-expired-and-move-routine nil)
492 (when (or (spam-group-ham-contents-p gnus-newsgroup-name)
493 (and (spam-group-spam-contents-p gnus-newsgroup-name)
494 spam-process-ham-in-spam-groups)
495 spam-process-ham-in-nonham-groups)
496 (when (spam-group-ham-processor-whitelist-p gnus-newsgroup-name)
497 (gnus-message 5 "Registering ham with the whitelist")
498 (spam-whitelist-register-routine))
499 (when (spam-group-ham-processor-ifile-p gnus-newsgroup-name)
500 (gnus-message 5 "Registering ham with ifile")
501 (spam-ifile-register-ham-routine))
502 (when (spam-group-ham-processor-bogofilter-p gnus-newsgroup-name)
503 (gnus-message 5 "Registering ham with Bogofilter")
504 (spam-bogofilter-register-ham-routine))
505 (when (spam-group-ham-processor-stat-p gnus-newsgroup-name)
506 (gnus-message 5 "Registering ham with spam-stat")
507 (spam-stat-register-ham-routine))
508 (when (spam-group-ham-processor-BBDB-p gnus-newsgroup-name)
509 (gnus-message 5 "Registering ham with the BBDB")
510 (spam-BBDB-register-routine))
511 (when (spam-group-ham-processor-spamoracle-p gnus-newsgroup-name)
512 (gnus-message 5 "Registering ham with spamoracle")
513 (spam-spamoracle-learn-ham)))
515 (when (spam-group-ham-processor-copy-p gnus-newsgroup-name)
516 (gnus-message 5 "Copying ham")
517 (spam-ham-move-routine
518 (gnus-parameter-ham-process-destination gnus-newsgroup-name) t))
520 ;; now move all ham articles out of spam groups
521 (when (spam-group-spam-contents-p gnus-newsgroup-name)
522 (gnus-message 5 "Moving ham messages from spam group")
523 (spam-ham-move-routine
524 (gnus-parameter-ham-process-destination gnus-newsgroup-name)))))
526 (defun spam-mark-junk-as-spam-routine ()
527 ;; check the global list of group names spam-junk-mailgroups and the
529 (when (spam-group-spam-contents-p gnus-newsgroup-name)
530 (gnus-message 5 "Marking %s articles as spam"
531 (if spam-mark-only-unseen-as-spam
534 (let ((articles (if spam-mark-only-unseen-as-spam
535 gnus-newsgroup-unseen
536 gnus-newsgroup-unreads)))
537 (dolist (article articles)
538 (gnus-summary-mark-article article gnus-spam-mark)))))
540 (defun spam-mark-spam-as-expired-and-move-routine (&optional group)
541 (gnus-summary-kill-process-mark)
542 (let ((articles gnus-newsgroup-articles)
544 (dolist (article articles)
545 (when (eq (gnus-summary-article-mark article) gnus-spam-mark)
546 (gnus-summary-mark-article article gnus-expirable-mark)
547 (push article tomove)))
549 ;; now do the actual move
552 (dolist (article tomove)
553 (gnus-summary-set-process-mark article))
554 (when tomove (gnus-summary-move-article nil group))))
555 (gnus-summary-yank-process-mark))
557 (defun spam-ham-move-routine (&optional group copy)
558 (gnus-summary-kill-process-mark)
559 (let ((articles gnus-newsgroup-articles)
561 (when (stringp group) ; this routine will do nothing
562 ; without a valid group
563 (dolist (article articles)
564 (when (spam-group-ham-mark-p gnus-newsgroup-name
565 (gnus-summary-article-mark article))
566 (push article tomove)))
568 ;; now do the actual move
570 (dolist (article tomove)
571 (when spam-mark-ham-unread-before-move-from-spam-group
572 (gnus-summary-mark-article article gnus-unread-mark))
573 (gnus-summary-set-process-mark article))
575 (gnus-summary-copy-article nil group)
576 (gnus-summary-move-article nil group)))))
577 (gnus-summary-yank-process-mark))
579 (defun spam-generic-register-routine (spam-func ham-func)
580 (let ((articles gnus-newsgroup-articles)
581 article mark ham-articles spam-articles)
584 (setq article (pop articles)
585 mark (gnus-summary-article-mark article))
586 (cond ((spam-group-spam-mark-p gnus-newsgroup-name mark)
587 (push article spam-articles))
588 ((memq article gnus-newsgroup-saved))
589 ((spam-group-ham-mark-p gnus-newsgroup-name mark)
590 (push article ham-articles))))
592 (when (and ham-articles ham-func)
593 (mapc ham-func ham-articles)) ; we use mapc because unlike
594 ; mapcar it discards the
596 (when (and spam-articles spam-func)
597 (mapc spam-func spam-articles)))) ; we use mapc because unlike
598 ; mapcar it discards the
602 (defalias 'spam-point-at-eol (if (fboundp 'point-at-eol)
604 'line-end-position)))
606 (defun spam-get-article-as-string (article)
607 (let ((article-buffer (spam-get-article-as-buffer article))
610 (save-window-excursion
611 (set-buffer article-buffer)
612 (setq article-string (buffer-string))))
615 (defun spam-get-article-as-buffer (article)
616 (let ((article-buffer))
617 (when (numberp article)
618 (save-window-excursion
619 (gnus-summary-goto-subject article)
620 (gnus-summary-show-article t)
621 (setq article-buffer (get-buffer gnus-article-buffer))))
625 ;; (defun spam-get-article-as-filename (article)
626 ;; (let ((article-filename))
627 ;; (when (numberp article)
628 ;; (nnml-possibly-change-directory (gnus-group-real-name gnus-newsgroup-name))
629 ;; (setq article-filename (expand-file-name (int-to-string article) nnml-current-directory)))
630 ;; (if (file-exists-p article-filename)
634 (defun spam-fetch-field-from-fast (article)
635 "Fetch the `from' field quickly, using the internal gnus-data-list function"
636 (if (and (numberp article)
637 (assoc article (gnus-data-list nil)))
638 (mail-header-from (gnus-data-header (assoc article (gnus-data-list nil))))
641 (defun spam-fetch-field-subject-fast (article)
642 "Fetch the `subject' field quickly, using the internal gnus-data-list function"
643 (if (and (numberp article)
644 (assoc article (gnus-data-list nil)))
645 (mail-header-subject (gnus-data-header (assoc article (gnus-data-list nil))))
649 ;;;; Spam determination.
651 (defvar spam-list-of-checks
652 '((spam-use-blacklist . spam-check-blacklist)
653 (spam-use-regex-headers . spam-check-regex-headers)
654 (spam-use-whitelist . spam-check-whitelist)
655 (spam-use-BBDB . spam-check-BBDB)
656 (spam-use-ifile . spam-check-ifile)
657 (spam-use-spamoracle . spam-check-spamoracle)
658 (spam-use-stat . spam-check-stat)
659 (spam-use-blackholes . spam-check-blackholes)
660 (spam-use-hashcash . spam-check-hashcash)
661 (spam-use-bogofilter-headers . spam-check-bogofilter-headers)
662 (spam-use-bogofilter . spam-check-bogofilter))
663 "The spam-list-of-checks list contains pairs associating a parameter
664 variable with a spam checking function. If the parameter variable is
665 true, then the checking function is called, and its value decides what
666 happens. Each individual check may return nil, t, or a mailgroup
667 name. The value nil means that the check does not yield a decision,
668 and so, that further checks are needed. The value t means that the
669 message is definitely not spam, and that further spam checks should be
670 inhibited. Otherwise, a mailgroup name is returned where the mail
671 should go, and further checks are also inhibited. The usual mailgroup
672 name is the value of `spam-split-group', meaning that the message is
675 (defvar spam-list-of-statistical-checks
676 '(spam-use-ifile spam-use-stat spam-use-bogofilter spam-use-spamoracle)
677 "The spam-list-of-statistical-checks list contains all the mail
678 splitters that need to have the full message body available.")
680 (defun spam-split (&rest specific-checks)
681 "Split this message into the `spam' group if it is spam.
682 This function can be used as an entry in `nnmail-split-fancy', for
683 example like this: (: spam-split). It can take checks as parameters.
685 See the Info node `(gnus)Fancy Mail Splitting' for more details."
689 (dolist (check spam-list-of-statistical-checks)
690 (when (symbol-value check)
692 (gnus-message 8 "spam-split: widening the buffer (%s requires it)"
695 ;; (progn (widen) (debug (buffer-string)))
696 (let ((list-of-checks spam-list-of-checks)
698 (while (and list-of-checks (not decision))
699 (let ((pair (pop list-of-checks)))
700 (when (and (symbol-value (car pair))
701 (or (null specific-checks)
702 (memq (car pair) specific-checks)))
703 (gnus-message 5 "spam-split: calling the %s function" (symbol-name (cdr pair)))
704 (setq decision (funcall (cdr pair))))))
709 (defun spam-setup-widening ()
710 (dolist (check spam-list-of-statistical-checks)
711 (when (symbol-value check)
712 (setq nnimap-split-download-body-default t))))
717 (defun spam-check-regex-headers ()
719 (dolist (h-regex spam-regex-headers-ham)
721 (goto-char (point-min))
722 (when (re-search-forward h-regex nil t)
723 (message "Ham regex header search positive.")
725 (dolist (s-regex spam-regex-headers-spam)
727 (goto-char (point-min))
728 (when (re-search-forward s-regex nil t)
729 (message "Spam regex header search positive." (match-string 1))
731 (setq ret spam-split-group))))
737 (defun spam-check-blackholes ()
738 "Check the Received headers for blackholed relays."
739 (let ((headers (message-fetch-field "received"))
744 (goto-char (point-min))
745 (gnus-message 5 "Checking headers for relay addresses")
746 (while (re-search-forward
747 "\\[\\([0-9]+.[0-9]+.[0-9]+.[0-9]+\\)\\]" nil t)
748 (gnus-message 9 "Blackhole search found host IP %s." (match-string 1))
749 (push (mapconcat 'identity
750 (nreverse (split-string (match-string 1) "\\."))
753 (dolist (server spam-blackhole-servers)
755 (unless (and spam-blackhole-good-server-regex
756 (string-match spam-blackhole-good-server-regex ip))
757 (let ((query-string (concat ip "." server)))
759 (let ((query-result (query-dig query-string)))
761 (gnus-message 5 "(DIG): positive blackhole check '%s'"
763 (push (list ip server query-result)
765 ;; else, if not using dig.el
766 (when (query-dns query-string)
767 (gnus-message 5 "positive blackhole check")
768 (push (list ip server (query-dns query-string 'TXT))
779 (defun spam-check-hashcash ()
780 "Check the headers for hashcash payments."
781 (mail-check-payment))) ;mail-check-payment returns a boolean
784 (defalias 'mail-check-payment 'ignore)
785 (defalias 'spam-check-hashcash 'ignore))))
789 ;;; original idea for spam-check-BBDB from Alexander Kotelnikov
790 ;;; <sacha@giotto.sj.ru>
792 ;; all this is done inside a condition-case to trap errors
799 (defun spam-enter-ham-BBDB (from)
800 "Enter an address into the BBDB; implies ham (non-spam) sender"
802 (let* ((parsed-address (gnus-extract-address-components from))
803 (name (or (car parsed-address) "Ham Sender"))
804 (net-address (car (cdr parsed-address))))
805 (gnus-message 5 "Adding address %s to BBDB" from)
806 (when (and net-address
807 (not (bbdb-search-simple nil net-address)))
808 (bbdb-create-internal name nil net-address nil nil
809 "ham sender added by spam.el")))))
811 (defun spam-BBDB-register-routine ()
812 (spam-generic-register-routine
817 (spam-enter-ham-BBDB (spam-fetch-field-from-fast article)))))
819 (defun spam-check-BBDB ()
820 "Mail from people in the BBDB is classified as ham or non-spam"
821 (let ((who (message-fetch-field "from")))
823 (setq who (cadr (gnus-extract-address-components who)))
824 (if (bbdb-search-simple nil who)
826 (if spam-use-BBDB-exclusive
831 (defalias 'bbdb-search-simple 'ignore)
832 (defalias 'spam-check-BBDB 'ignore)
833 (defalias 'spam-BBDB-register-routine 'ignore)
834 (defalias 'spam-enter-ham-BBDB 'ignore)
835 (defalias 'bbdb-create-internal 'ignore)
836 (defalias 'bbdb-records 'ignore))))
841 ;;; check the ifile backend; return nil if the mail was NOT classified
844 (defun spam-get-ifile-database-parameter ()
845 "Get the command-line parameter for ifile's database from spam-ifile-database-path."
846 (if spam-ifile-database-path
847 (format "--db-file=%s" spam-ifile-database-path)
850 (defun spam-check-ifile ()
851 "Check the ifile backend for the classification of this message"
852 (let ((article-buffer-name (buffer-name))
855 (let ((temp-buffer-name (buffer-name))
856 (db-param (spam-get-ifile-database-parameter)))
858 (set-buffer article-buffer-name)
860 (call-process-region (point-min) (point-max) spam-ifile-path
861 nil temp-buffer-name nil "-q" "-c" db-param)
862 (call-process-region (point-min) (point-max) spam-ifile-path
863 nil temp-buffer-name nil "-q" "-c")))
864 (goto-char (point-min))
866 (setq category (buffer-substring (point) (spam-point-at-eol))))
867 (when (not (zerop (length category))) ; we need a category here
868 (if spam-ifile-all-categories
869 (setq return category)
870 ;; else, if spam-ifile-all-categories is not set...
871 (when (string-equal spam-ifile-spam-category category)
872 (setq return spam-split-group))))))
875 (defun spam-ifile-register-with-ifile (article-string category)
876 "Register an article, given as a string, with a category.
877 Uses `gnus-newsgroup-name' if category is nil (for ham registration)."
878 (when (stringp article-string)
879 (let ((category (or category gnus-newsgroup-name))
880 (db-param (spam-get-ifile-database-parameter)))
882 (insert article-string)
884 (call-process-region (point-min) (point-max) spam-ifile-path
886 "-h" "-i" category db-param)
887 (call-process-region (point-min) (point-max) spam-ifile-path
889 "-h" "-i" category))))))
891 (defun spam-ifile-register-spam-routine ()
892 (spam-generic-register-routine
894 (spam-ifile-register-with-ifile
895 (spam-get-article-as-string article) spam-ifile-spam-category))
898 (defun spam-ifile-register-ham-routine ()
899 (spam-generic-register-routine
902 (spam-ifile-register-with-ifile
903 (spam-get-article-as-string article) spam-ifile-ham-category))))
910 (let ((spam-stat-install-hooks nil))
911 (require 'spam-stat))
913 (defun spam-check-stat ()
914 "Check the spam-stat backend for the classification of this message"
915 (let ((spam-stat-split-fancy-spam-group spam-split-group) ; override
916 (spam-stat-buffer (buffer-name)) ; stat the current buffer
918 (spam-stat-split-fancy)))
920 (defun spam-stat-register-spam-routine ()
921 (spam-generic-register-routine
923 (let ((article-string (spam-get-article-as-string article)))
925 (insert article-string)
926 (spam-stat-buffer-is-spam))))
929 (defun spam-stat-register-ham-routine ()
930 (spam-generic-register-routine
933 (let ((article-string (spam-get-article-as-string article)))
935 (insert article-string)
936 (spam-stat-buffer-is-non-spam))))))
938 (defun spam-maybe-spam-stat-load ()
939 (when spam-use-stat (spam-stat-load)))
941 (defun spam-maybe-spam-stat-save ()
942 (when spam-use-stat (spam-stat-save))))
945 (defalias 'spam-maybe-spam-stat-load 'ignore)
946 (defalias 'spam-maybe-spam-stat-save 'ignore)
947 (defalias 'spam-stat-register-ham-routine 'ignore)
948 (defalias 'spam-stat-register-spam-routine 'ignore)
949 (defalias 'spam-stat-buffer-is-spam 'ignore)
950 (defalias 'spam-stat-buffer-is-non-spam 'ignore)
951 (defalias 'spam-stat-split-fancy 'ignore)
952 (defalias 'spam-stat-load 'ignore)
953 (defalias 'spam-stat-save 'ignore)
954 (defalias 'spam-check-stat 'ignore))))
958 ;;;; Blacklists and whitelists.
960 (defvar spam-whitelist-cache nil)
961 (defvar spam-blacklist-cache nil)
963 (defun spam-enter-whitelist (address)
964 "Enter ADDRESS into the whitelist."
965 (interactive "sAddress: ")
966 (spam-enter-list address spam-whitelist)
967 (setq spam-whitelist-cache nil))
969 (defun spam-enter-blacklist (address)
970 "Enter ADDRESS into the blacklist."
971 (interactive "sAddress: ")
972 (spam-enter-list address spam-blacklist)
973 (setq spam-blacklist-cache nil))
975 (defun spam-enter-list (address file)
976 "Enter ADDRESS into the given FILE, either the whitelist or the blacklist."
977 (unless (file-exists-p (file-name-directory file))
978 (make-directory (file-name-directory file) t))
981 (find-file-noselect file))
982 (goto-char (point-min))
983 (unless (re-search-forward (regexp-quote address) nil t)
984 (goto-char (point-max))
987 (insert address "\n")
990 ;;; returns t if the sender is in the whitelist, nil or spam-split-group otherwise
991 (defun spam-check-whitelist ()
992 ;; FIXME! Should it detect when file timestamps change?
993 (unless spam-whitelist-cache
994 (setq spam-whitelist-cache (spam-parse-list spam-whitelist)))
995 (if (spam-from-listed-p spam-whitelist-cache)
997 (if spam-use-whitelist-exclusive
1001 (defun spam-check-blacklist ()
1002 ;; FIXME! Should it detect when file timestamps change?
1003 (unless spam-blacklist-cache
1004 (setq spam-blacklist-cache (spam-parse-list spam-blacklist)))
1005 (and (spam-from-listed-p spam-blacklist-cache) spam-split-group))
1007 (defun spam-parse-list (file)
1008 (when (file-readable-p file)
1009 (let (contents address)
1011 (insert-file-contents file)
1013 (setq address (buffer-substring (point) (spam-point-at-eol)))
1015 ;; insert the e-mail address if detected, otherwise the raw data
1016 (unless (zerop (length address))
1017 (let ((pure-address (cadr (gnus-extract-address-components address))))
1018 (push (or pure-address address) contents)))))
1019 (nreverse contents))))
1021 (defun spam-from-listed-p (cache)
1022 (let ((from (message-fetch-field "from"))
1025 (let ((address (pop cache)))
1026 (unless (zerop (length address)) ; 0 for a nil address too
1027 (setq address (regexp-quote address))
1028 ;; fix regexp-quote's treatment of user-intended regexes
1029 (while (string-match "\\\\\\*" address)
1030 (setq address (replace-match ".*" t t address))))
1031 (when (and address (string-match address from))
1036 (defun spam-blacklist-register-routine ()
1037 (spam-generic-register-routine
1038 ;; the spam function
1040 (let ((from (spam-fetch-field-from-fast article)))
1041 (when (stringp from)
1042 (spam-enter-blacklist from))))
1046 (defun spam-whitelist-register-routine ()
1047 (spam-generic-register-routine
1048 ;; the spam function
1052 (let ((from (spam-fetch-field-from-fast article)))
1053 (when (stringp from)
1054 (spam-enter-whitelist from))))))
1057 ;;;; Spam-report glue
1058 (defun spam-report-gmane-register-routine ()
1059 (spam-generic-register-routine
1065 (defun spam-check-bogofilter-headers (&optional score)
1066 (let ((header (message-fetch-field spam-bogofilter-header)))
1067 (when header ; return nil when no header
1068 (if score ; scoring mode
1069 (if (string-match "spamicity=\\([0-9.]+\\)" header)
1070 (match-string 1 header)
1072 ;; spam detection mode
1073 (when (string-match spam-bogofilter-bogosity-positive-spam-header
1075 spam-split-group)))))
1077 ;; return something sensible if the score can't be determined
1078 (defun spam-bogofilter-score ()
1079 "Get the Bogofilter spamicity score"
1081 (save-window-excursion
1082 (gnus-summary-show-article t)
1083 (set-buffer gnus-article-buffer)
1084 (let ((score (or (spam-check-bogofilter-headers t)
1085 (spam-check-bogofilter t))))
1086 (message "Spamicity score %s" score)
1088 (gnus-summary-show-article)))
1090 (defun spam-check-bogofilter (&optional score)
1091 "Check the Bogofilter backend for the classification of this message"
1092 (let ((article-buffer-name (buffer-name))
1095 (let ((temp-buffer-name (buffer-name)))
1097 (set-buffer article-buffer-name)
1098 (if spam-bogofilter-database-directory
1099 (call-process-region (point-min) (point-max)
1100 spam-bogofilter-path
1101 nil temp-buffer-name nil "-v"
1102 "-d" spam-bogofilter-database-directory)
1103 (call-process-region (point-min) (point-max) spam-bogofilter-path
1104 nil temp-buffer-name nil "-v")))
1105 (setq return (spam-check-bogofilter-headers score))))
1108 (defun spam-bogofilter-register-with-bogofilter (article-string spam)
1109 "Register an article, given as a string, as spam or non-spam."
1110 (when (stringp article-string)
1111 (let ((switch (if spam spam-bogofilter-spam-switch
1112 spam-bogofilter-ham-switch)))
1114 (insert article-string)
1115 (if spam-bogofilter-database-directory
1116 (call-process-region (point-min) (point-max)
1117 spam-bogofilter-path
1118 nil nil nil "-v" switch
1119 "-d" spam-bogofilter-database-directory)
1120 (call-process-region (point-min) (point-max) spam-bogofilter-path
1121 nil nil nil "-v" switch))))))
1123 (defun spam-bogofilter-register-spam-routine ()
1124 (spam-generic-register-routine
1126 (spam-bogofilter-register-with-bogofilter
1127 (spam-get-article-as-string article) t))
1130 (defun spam-bogofilter-register-ham-routine ()
1131 (spam-generic-register-routine
1134 (spam-bogofilter-register-with-bogofilter
1135 (spam-get-article-as-string article) nil))))
1139 (defun spam-check-spamoracle ()
1140 "Run spamoracle on an article to determine whether it's spam."
1141 (let ((article-buffer-name (buffer-name)))
1143 (let ((temp-buffer-name (buffer-name)))
1145 (set-buffer article-buffer-name)
1147 (apply 'call-process-region
1148 (point-min) (point-max)
1149 spam-spamoracle-binary
1150 nil temp-buffer-name nil
1151 (if spam-spamoracle-database
1152 `("-f" ,spam-spamoracle-database "mark")
1156 (set-buffer temp-buffer-name)
1157 (goto-char (point-min))
1158 (when (re-search-forward "^X-Spam: yes;" nil t)
1160 (error "Error running spamoracle" status))))))))
1162 (defun spam-spamoracle-learn (article article-is-spam-p)
1163 "Run spamoracle in training mode."
1165 (let ((temp-buffer-name (buffer-name)))
1167 (goto-char (point-min))
1168 (insert (spam-get-article-as-string article))
1169 (let* ((arg (if article-is-spam-p "-spam" "-good"))
1171 (apply 'call-process-region
1172 (point-min) (point-max)
1173 spam-spamoracle-binary
1174 nil temp-buffer-name nil
1175 (if spam-spamoracle-database
1176 `("-f" ,spam-spamoracle-database
1179 (when (not (zerop status))
1180 (error "Error running spamoracle" status)))))))
1182 (defun spam-spamoracle-learn-ham ()
1183 (spam-generic-register-routine
1186 (spam-spamoracle-learn article nil))))
1188 (defun spam-spamoracle-learn-spam ()
1189 (spam-generic-register-routine
1191 (spam-spamoracle-learn article t))
1196 (defun spam-install-hooks-function ()
1197 "Install the spam.el hooks"
1199 ;; Add hooks for loading and saving the spam stats
1201 (add-hook 'gnus-save-newsrc-hook 'spam-maybe-spam-stat-save)
1202 (add-hook 'gnus-get-top-new-news-hook 'spam-maybe-spam-stat-load)
1203 (add-hook 'gnus-startup-hook 'spam-maybe-spam-stat-load))
1204 (add-hook 'gnus-summary-prepare-exit-hook 'spam-summary-prepare-exit)
1205 (add-hook 'gnus-summary-prepare-hook 'spam-summary-prepare)
1206 (add-hook 'gnus-get-new-news-hook 'spam-setup-widening))
1208 (defun spam-unload-hook ()
1209 "Uninstall the spam.el hooks"
1211 (remove-hook 'gnus-save-newsrc-hook 'spam-maybe-spam-stat-save)
1212 (remove-hook 'gnus-get-top-new-news-hook 'spam-maybe-spam-stat-load)
1213 (remove-hook 'gnus-startup-hook 'spam-maybe-spam-stat-load)
1214 (remove-hook 'gnus-summary-prepare-exit-hook 'spam-summary-prepare-exit)
1215 (remove-hook 'gnus-summary-prepare-hook 'spam-summary-prepare)
1216 (remove-hook 'gnus-get-new-news-hook 'spam-setup-widening))
1218 (when spam-install-hooks
1219 (spam-install-hooks-function))
1223 ;;; spam.el ends here.