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
39 (require 'gnus-uu) ; because of key prefix issues
40 (require 'gnus) ; for the definitions of group content classification and spam processors
41 (require 'message) ;for the message-fetch-field functions
43 ;; for nnimap-split-download-body-default
44 (eval-when-compile (require 'nnimap))
46 ;; autoload executable-find
48 ;; executable-find is not autoloaded in Emacs 20
49 (autoload 'executable-find "executable"))
53 (autoload 'query-dig "dig"))
55 ;; autoload spam-report
57 (autoload 'spam-report-gmane "spam-report"))
61 (autoload 'query-dns "dns"))
66 "Spam configuration.")
68 (defcustom spam-directory "~/News/spam/"
69 "Directory for spam whitelists and blacklists."
73 (defcustom spam-move-spam-nonspam-groups-only t
74 "Whether spam should be moved in non-spam groups only.
75 When nil, only ham and unclassified groups will have their spam moved
76 to the spam-process-destination. When t, spam will also be moved from
81 (defcustom spam-mark-ham-unread-before-move-from-spam-group nil
82 "Whether ham should be marked unread before it's moved out of a spam
83 group according to ham-process-destination. This variable is an
84 official entry in the international Longest Variable Name
89 (defcustom spam-whitelist (expand-file-name "whitelist" spam-directory)
90 "The location of the whitelist.
91 The file format is one regular expression per line.
92 The regular expression is matched against the address."
96 (defcustom spam-blacklist (expand-file-name "blacklist" spam-directory)
97 "The location of the blacklist.
98 The file format is one regular expression per line.
99 The regular expression is matched against the address."
103 (defcustom spam-use-dig t
104 "Whether query-dig should be used instead of query-dns."
108 (defcustom spam-use-blacklist nil
109 "Whether the blacklist should be used by spam-split."
113 (defcustom spam-use-whitelist nil
114 "Whether the whitelist should be used by spam-split."
118 (defcustom spam-use-whitelist-exclusive nil
119 "Whether whitelist-exclusive should be used by spam-split.
120 Exclusive whitelisting means that all messages from senders not in the whitelist
121 are considered spam."
125 (defcustom spam-use-blackholes nil
126 "Whether blackholes should be used by spam-split."
130 (defcustom spam-use-hashcash nil
131 "Whether hashcash payments should be detected by spam-split."
135 (defcustom spam-use-regex-headers nil
136 "Whether a header regular expression match should be used by spam-split.
137 Also see the variable `spam-spam-regex-headers' and `spam-ham-regex-headers'."
141 (defcustom spam-use-bogofilter-headers nil
142 "Whether bogofilter headers should be used by spam-split.
143 Enable this if you pre-process messages with Bogofilter BEFORE Gnus sees them."
147 (defcustom spam-use-bogofilter nil
148 "Whether bogofilter should be invoked by spam-split.
149 Enable this if you want Gnus to invoke Bogofilter on new messages."
153 (defcustom spam-use-BBDB nil
154 "Whether BBDB should be used by spam-split."
158 (defcustom spam-use-BBDB-exclusive nil
159 "Whether BBDB-exclusive should be used by spam-split.
160 Exclusive BBDB means that all messages from senders not in the BBDB are
165 (defcustom spam-use-ifile nil
166 "Whether ifile should be used by spam-split."
170 (defcustom spam-use-stat nil
171 "Whether spam-stat should be used by spam-split."
175 (defcustom spam-split-group "spam"
176 "Group name where incoming spam should be put by spam-split."
180 (defcustom spam-junk-mailgroups (cons spam-split-group '("mail.junk" "poste.pourriel"))
181 "Mailgroups with spam contents.
182 All unmarked article in such group receive the spam mark on group entry."
183 :type '(repeat (string :tag "Group"))
186 (defcustom spam-blackhole-servers '("bl.spamcop.net" "relays.ordb.org"
187 "dev.null.dk" "relays.visi.com")
188 "List of blackhole servers."
189 :type '(repeat (string :tag "Server"))
192 (defcustom spam-blackhole-good-server-regex nil
193 "String matching IP addresses that should not be checked in the blackholes"
197 (defcustom spam-face 'gnus-splash-face
198 "Face for spam-marked articles"
202 (defcustom spam-regex-headers-spam '("^X-Spam-Flag: YES")
203 "Regular expression for positive header spam matches"
204 :type '(repeat (regexp :tag "Regular expression to match spam header"))
207 (defcustom spam-regex-headers-ham '("^X-Spam-Flag: NO")
208 "Regular expression for positive header ham matches"
209 :type '(repeat (regexp :tag "Regular expression to match ham header"))
212 (defgroup spam-ifile nil
213 "Spam ifile configuration."
216 (defcustom spam-ifile-path (executable-find "ifile")
217 "File path of the ifile executable program."
218 :type '(choice (file :tag "Location of ifile")
219 (const :tag "ifile is not installed"))
222 (defcustom spam-ifile-database-path nil
223 "File path of the ifile database."
224 :type '(choice (file :tag "Location of the ifile database")
225 (const :tag "Use the default"))
228 (defcustom spam-ifile-spam-category "spam"
229 "Name of the spam ifile category."
233 (defcustom spam-ifile-ham-category nil
234 "Name of the ham ifile category. If nil, the current group name will
236 :type '(choice (string :tag "Use a fixed category")
237 (const :tag "Use the current group name"))
240 (defcustom spam-ifile-all-categories nil
241 "Whether the ifile check will return all categories, or just spam.
242 Set this to t if you want to use the spam-split invocation of ifile as
243 your main source of newsgroup names."
247 (defgroup spam-bogofilter nil
248 "Spam bogofilter configuration."
251 (defcustom spam-bogofilter-path (executable-find "bogofilter")
252 "File path of the Bogofilter executable program."
253 :type '(choice (file :tag "Location of bogofilter")
254 (const :tag "Bogofilter is not installed"))
255 :group 'spam-bogofilter)
257 (defcustom spam-bogofilter-header "X-Bogosity"
258 "The header that Bogofilter inserts in messages."
260 :group 'spam-bogofilter)
262 (defcustom spam-bogofilter-spam-switch "-s"
263 "The switch that Bogofilter uses to register spam messages."
265 :group 'spam-bogofilter)
267 (defcustom spam-bogofilter-ham-switch "-n"
268 "The switch that Bogofilter uses to register ham messages."
270 :group 'spam-bogofilter)
272 (defcustom spam-bogofilter-bogosity-positive-spam-header "^\\(Yes\\|Spam\\)"
273 "The regex on `spam-bogofilter-header' for positive spam identification."
275 :group 'spam-bogofilter)
277 (defcustom spam-bogofilter-database-directory nil
278 "Directory path of the Bogofilter databases."
279 :type '(choice (directory :tag "Location of the Bogofilter database directory")
280 (const :tag "Use the default"))
283 ;;; Key bindings for spam control.
285 (gnus-define-keys gnus-summary-mode-map
286 "St" spam-bogofilter-score
287 "Sx" gnus-summary-mark-as-spam
288 "Mst" spam-bogofilter-score
289 "Msx" gnus-summary-mark-as-spam
290 "\M-d" gnus-summary-mark-as-spam)
292 ;;; How to highlight a spam summary line.
294 ;; TODO: How do we redo this every time spam-face is customized?
296 (push '((eq mark gnus-spam-mark) . spam-face)
297 gnus-summary-highlight)
299 ;; convenience functions
300 (defun spam-group-ham-mark-p (group mark &optional spam)
301 (when (stringp group)
302 (let* ((marks (spam-group-ham-marks group spam))
303 (marks (if (symbolp mark)
305 (mapcar 'symbol-value marks))))
308 (defun spam-group-spam-mark-p (group mark)
309 (spam-group-ham-mark-p group mark t))
311 (defun spam-group-ham-marks (group &optional spam)
312 (when (stringp group)
313 (let* ((marks (if spam
314 (gnus-parameter-spam-marks group)
315 (gnus-parameter-ham-marks group)))
317 (marks (if (listp (car marks)) (car marks) marks)))
320 (defun spam-group-spam-marks (group)
321 (spam-group-ham-marks group t))
323 (defun spam-group-spam-contents-p (group)
325 (or (member group spam-junk-mailgroups)
326 (memq 'gnus-group-spam-classification-spam
327 (gnus-parameter-spam-contents group)))
330 (defun spam-group-ham-contents-p (group)
332 (memq 'gnus-group-spam-classification-ham
333 (gnus-parameter-spam-contents group))
336 (defun spam-group-processor-p (group processor)
337 (if (and (stringp group)
339 (member processor (car (gnus-parameter-spam-process group)))
342 (defun spam-group-spam-processor-report-gmane-p (group)
343 (spam-group-processor-p group 'gnus-group-spam-exit-processor-report-gmane))
345 (defun spam-group-spam-processor-bogofilter-p (group)
346 (spam-group-processor-p group 'gnus-group-spam-exit-processor-bogofilter))
348 (defun spam-group-spam-processor-blacklist-p (group)
349 (spam-group-processor-p group 'gnus-group-spam-exit-processor-blacklist))
351 (defun spam-group-spam-processor-ifile-p (group)
352 (spam-group-processor-p group 'gnus-group-spam-exit-processor-ifile))
354 (defun spam-group-ham-processor-ifile-p (group)
355 (spam-group-processor-p group 'gnus-group-ham-exit-processor-ifile))
357 (defun spam-group-ham-processor-bogofilter-p (group)
358 (spam-group-processor-p group 'gnus-group-ham-exit-processor-bogofilter))
360 (defun spam-group-spam-processor-stat-p (group)
361 (spam-group-processor-p group 'gnus-group-spam-exit-processor-stat))
363 (defun spam-group-ham-processor-stat-p (group)
364 (spam-group-processor-p group 'gnus-group-ham-exit-processor-stat))
366 (defun spam-group-ham-processor-whitelist-p (group)
367 (spam-group-processor-p group 'gnus-group-ham-exit-processor-whitelist))
369 (defun spam-group-ham-processor-BBDB-p (group)
370 (spam-group-processor-p group 'gnus-group-ham-exit-processor-BBDB))
372 (defun spam-group-ham-processor-copy-p (group)
373 (spam-group-processor-p group 'gnus-group-ham-exit-processor-copy))
375 ;;; Summary entry and exit processing.
377 (defun spam-summary-prepare ()
378 (spam-mark-junk-as-spam-routine))
380 (add-hook 'gnus-summary-prepare-hook 'spam-summary-prepare)
382 ;; The spam processors are invoked for any group, spam or ham or neither
383 (defun spam-summary-prepare-exit ()
384 (unless gnus-group-is-exiting-without-update-p
385 (gnus-message 6 "Exiting summary buffer and applying spam rules")
386 (when (and spam-bogofilter-path
387 (spam-group-spam-processor-bogofilter-p gnus-newsgroup-name))
388 (gnus-message 5 "Registering spam with bogofilter")
389 (spam-bogofilter-register-spam-routine))
391 (when (and spam-ifile-path
392 (spam-group-spam-processor-ifile-p gnus-newsgroup-name))
393 (gnus-message 5 "Registering spam with ifile")
394 (spam-ifile-register-spam-routine))
396 (when (spam-group-spam-processor-stat-p gnus-newsgroup-name)
397 (gnus-message 5 "Registering spam with spam-stat")
398 (spam-stat-register-spam-routine))
400 (when (spam-group-spam-processor-blacklist-p gnus-newsgroup-name)
401 (gnus-message 5 "Registering spam with the blacklist")
402 (spam-blacklist-register-routine))
404 (when (spam-group-spam-processor-report-gmane-p gnus-newsgroup-name)
405 (gnus-message 5 "Registering spam with the Gmane report")
406 (spam-report-gmane-register-routine))
408 (if spam-move-spam-nonspam-groups-only
409 (when (not (spam-group-spam-contents-p gnus-newsgroup-name))
410 (spam-mark-spam-as-expired-and-move-routine
411 (gnus-parameter-spam-process-destination gnus-newsgroup-name)))
412 (gnus-message 5 "Marking spam as expired and moving it to %s" gnus-newsgroup-name)
413 (spam-mark-spam-as-expired-and-move-routine
414 (gnus-parameter-spam-process-destination gnus-newsgroup-name)))
416 ;; now we redo spam-mark-spam-as-expired-and-move-routine to only
417 ;; expire spam, in case the above did not expire them
418 (gnus-message 5 "Marking spam as expired without moving it")
419 (spam-mark-spam-as-expired-and-move-routine nil)
421 (when (spam-group-ham-contents-p gnus-newsgroup-name)
422 (when (spam-group-ham-processor-whitelist-p gnus-newsgroup-name)
423 (gnus-message 5 "Registering ham with the whitelist")
424 (spam-whitelist-register-routine))
425 (when (spam-group-ham-processor-ifile-p gnus-newsgroup-name)
426 (gnus-message 5 "Registering ham with ifile")
427 (spam-ifile-register-ham-routine))
428 (when (spam-group-ham-processor-bogofilter-p gnus-newsgroup-name)
429 (gnus-message 5 "Registering ham with Bogofilter")
430 (spam-bogofilter-register-ham-routine))
431 (when (spam-group-ham-processor-stat-p gnus-newsgroup-name)
432 (gnus-message 5 "Registering ham with spam-stat")
433 (spam-stat-register-ham-routine))
434 (when (spam-group-ham-processor-BBDB-p gnus-newsgroup-name)
435 (gnus-message 5 "Registering ham with the BBDB")
436 (spam-BBDB-register-routine)))
438 (when (spam-group-ham-processor-copy-p gnus-newsgroup-name)
439 (gnus-message 5 "Copying ham")
440 (spam-ham-move-routine
441 (gnus-parameter-ham-process-destination gnus-newsgroup-name) t))
443 ;; now move all ham articles out of spam groups
444 (when (spam-group-spam-contents-p gnus-newsgroup-name)
445 (gnus-message 5 "Moving ham messages from spam group")
446 (spam-ham-move-routine
447 (gnus-parameter-ham-process-destination gnus-newsgroup-name)))))
449 (add-hook 'gnus-summary-prepare-exit-hook 'spam-summary-prepare-exit)
451 (defun spam-mark-junk-as-spam-routine ()
452 ;; check the global list of group names spam-junk-mailgroups and the
454 (when (spam-group-spam-contents-p gnus-newsgroup-name)
455 (gnus-message 5 "Marking unread articles as spam")
456 (let ((articles gnus-newsgroup-articles)
459 (setq article (pop articles))
460 (when (eq (gnus-summary-article-mark article) gnus-unread-mark)
461 (gnus-summary-mark-article article gnus-spam-mark))))))
463 (defun spam-mark-spam-as-expired-and-move-routine (&optional group)
464 (gnus-summary-kill-process-mark)
465 (let ((articles gnus-newsgroup-articles)
467 (dolist (article articles)
468 (when (eq (gnus-summary-article-mark article) gnus-spam-mark)
469 (gnus-summary-mark-article article gnus-expirable-mark)
470 (push article tomove)))
472 ;; now do the actual move
475 (dolist (article tomove)
476 (gnus-summary-set-process-mark article))
477 (when tomove (gnus-summary-move-article nil group))))
478 (gnus-summary-yank-process-mark))
480 (defun spam-ham-move-routine (&optional group copy)
481 (gnus-summary-kill-process-mark)
482 (let ((articles gnus-newsgroup-articles)
484 (when (stringp group) ; this routine will do nothing
485 ; without a valid group
486 (dolist (article articles)
487 (when (spam-group-ham-mark-p gnus-newsgroup-name
488 (gnus-summary-article-mark article))
489 (push article tomove)))
491 ;; now do the actual move
493 (dolist (article tomove)
494 (when spam-mark-ham-unread-before-move-from-spam-group
495 (gnus-summary-mark-article article gnus-unread-mark))
496 (gnus-summary-set-process-mark article))
498 (gnus-summary-copy-article nil group)
499 (gnus-summary-move-article nil group)))))
500 (gnus-summary-yank-process-mark))
502 (defun spam-generic-register-routine (spam-func ham-func)
503 (let ((articles gnus-newsgroup-articles)
504 article mark ham-articles spam-articles)
507 (setq article (pop articles)
508 mark (gnus-summary-article-mark article))
509 (cond ((spam-group-spam-mark-p gnus-newsgroup-name mark)
510 (push article spam-articles))
511 ((memq article gnus-newsgroup-saved))
512 ((spam-group-ham-mark-p gnus-newsgroup-name mark)
513 (push article ham-articles))))
515 (when (and ham-articles ham-func)
516 (mapc ham-func ham-articles)) ; we use mapc because unlike
517 ; mapcar it discards the
519 (when (and spam-articles spam-func)
520 (mapc spam-func spam-articles)))) ; we use mapc because unlike
521 ; mapcar it discards the
525 (defalias 'spam-point-at-eol (if (fboundp 'point-at-eol)
527 'line-end-position)))
529 (defun spam-get-article-as-string (article)
530 (let ((article-buffer (spam-get-article-as-buffer article))
533 (save-window-excursion
534 (set-buffer article-buffer)
535 (setq article-string (buffer-string))))
538 (defun spam-get-article-as-buffer (article)
539 (let ((article-buffer))
540 (when (numberp article)
541 (save-window-excursion
542 (gnus-summary-goto-subject article)
543 (gnus-summary-show-article t)
544 (setq article-buffer (get-buffer gnus-article-buffer))))
548 ;; (defun spam-get-article-as-filename (article)
549 ;; (let ((article-filename))
550 ;; (when (numberp article)
551 ;; (nnml-possibly-change-directory (gnus-group-real-name gnus-newsgroup-name))
552 ;; (setq article-filename (expand-file-name (int-to-string article) nnml-current-directory)))
553 ;; (if (file-exists-p article-filename)
557 (defun spam-fetch-field-from-fast (article)
558 "Fetch the `from' field quickly, using the internal gnus-data-list function"
559 (if (and (numberp article)
560 (assoc article (gnus-data-list nil)))
561 (mail-header-from (gnus-data-header (assoc article (gnus-data-list nil))))
564 (defun spam-fetch-field-subject-fast (article)
565 "Fetch the `subject' field quickly, using the internal gnus-data-list function"
566 (if (and (numberp article)
567 (assoc article (gnus-data-list nil)))
568 (mail-header-subject (gnus-data-header (assoc article (gnus-data-list nil))))
572 ;;;; Spam determination.
574 (defvar spam-list-of-checks
575 '((spam-use-blacklist . spam-check-blacklist)
576 (spam-use-regex-headers . spam-check-regex-headers)
577 (spam-use-whitelist . spam-check-whitelist)
578 (spam-use-BBDB . spam-check-BBDB)
579 (spam-use-ifile . spam-check-ifile)
580 (spam-use-stat . spam-check-stat)
581 (spam-use-blackholes . spam-check-blackholes)
582 (spam-use-hashcash . spam-check-hashcash)
583 (spam-use-bogofilter-headers . spam-check-bogofilter-headers)
584 (spam-use-bogofilter . spam-check-bogofilter))
585 "The spam-list-of-checks list contains pairs associating a parameter
586 variable with a spam checking function. If the parameter variable is
587 true, then the checking function is called, and its value decides what
588 happens. Each individual check may return nil, t, or a mailgroup
589 name. The value nil means that the check does not yield a decision,
590 and so, that further checks are needed. The value t means that the
591 message is definitely not spam, and that further spam checks should be
592 inhibited. Otherwise, a mailgroup name is returned where the mail
593 should go, and further checks are also inhibited. The usual mailgroup
594 name is the value of `spam-split-group', meaning that the message is
597 (defvar spam-list-of-statistical-checks
598 '(spam-use-ifile spam-use-stat spam-use-bogofilter)
599 "The spam-list-of-statistical-checks list contains all the mail
600 splitters that need to have the full message body available.")
603 "Split this message into the `spam' group if it is spam.
604 This function can be used as an entry in `nnmail-split-fancy', for
605 example like this: (: spam-split)
607 See the Info node `(gnus)Fancy Mail Splitting' for more details."
611 (dolist (check spam-list-of-statistical-checks)
612 (when (symbol-value check)
614 (gnus-message 8 "spam-split: widening the buffer (%s requires it)"
617 ;; (progn (widen) (debug (buffer-string)))
618 (let ((list-of-checks spam-list-of-checks)
620 (while (and list-of-checks (not decision))
621 (let ((pair (pop list-of-checks)))
622 (when (symbol-value (car pair))
623 (gnus-message 5 "spam-split: calling the %s function" (symbol-name (cdr pair)))
624 (setq decision (funcall (cdr pair))))))
629 (defun spam-setup-widening ()
630 (dolist (check spam-list-of-statistical-checks)
631 (when (symbol-value check)
632 (setq nnimap-split-download-body-default t))))
634 (add-hook 'gnus-get-new-news-hook 'spam-setup-widening)
639 (defun spam-check-regex-headers ()
641 (dolist (h-regex spam-regex-headers-ham)
643 (goto-char (point-min))
644 (when (re-search-forward h-regex nil t)
645 (message "Ham regex header search positive.")
647 (dolist (s-regex spam-regex-headers-spam)
649 (goto-char (point-min))
650 (when (re-search-forward s-regex nil t)
651 (message "Spam regex header search positive." (match-string 1))
653 (setq ret spam-split-group))))
659 (defun spam-check-blackholes ()
660 "Check the Received headers for blackholed relays."
661 (let ((headers (message-fetch-field "received"))
666 (goto-char (point-min))
667 (gnus-message 5 "Checking headers for relay addresses")
668 (while (re-search-forward
669 "\\[\\([0-9]+.[0-9]+.[0-9]+.[0-9]+\\)\\]" nil t)
670 (gnus-message 9 "Blackhole search found host IP %s." (match-string 1))
671 (push (mapconcat 'identity
672 (nreverse (split-string (match-string 1) "\\."))
675 (dolist (server spam-blackhole-servers)
677 (unless (and spam-blackhole-good-server-regex
678 (string-match spam-blackhole-good-server-regex ip))
679 (let ((query-string (concat ip "." server)))
681 (let ((query-result (query-dig query-string)))
683 (gnus-message 5 "(DIG): positive blackhole check '%s'"
685 (push (list ip server query-result)
687 ;; else, if not using dig.el
688 (when (query-dns query-string)
689 (gnus-message 5 "positive blackhole check")
690 (push (list ip server (query-dns query-string 'TXT))
701 (defun spam-check-hashcash ()
702 "Check the headers for hashcash payments."
703 (mail-check-payment))) ;mail-check-payment returns a boolean
706 (defalias 'mail-check-payment 'ignore)
707 (defalias 'spam-check-hashcash 'ignore))))
711 ;;; original idea for spam-check-BBDB from Alexander Kotelnikov
712 ;;; <sacha@giotto.sj.ru>
714 ;; all this is done inside a condition-case to trap errors
721 (defun spam-enter-ham-BBDB (from)
722 "Enter an address into the BBDB; implies ham (non-spam) sender"
724 (let* ((parsed-address (gnus-extract-address-components from))
725 (name (or (car parsed-address) "Ham Sender"))
726 (net-address (car (cdr parsed-address))))
727 (gnus-message 5 "Adding address %s to BBDB" from)
728 (when (and net-address
729 (not (bbdb-search-simple nil net-address)))
730 (bbdb-create-internal name nil net-address nil nil
731 "ham sender added by spam.el")))))
733 (defun spam-BBDB-register-routine ()
734 (spam-generic-register-routine
739 (spam-enter-ham-BBDB (spam-fetch-field-from-fast article)))))
741 (defun spam-check-BBDB ()
742 "Mail from people in the BBDB is classified as ham or non-spam"
743 (let ((who (message-fetch-field "from")))
745 (setq who (cadr (gnus-extract-address-components who)))
746 (if (bbdb-search-simple nil who)
748 (if spam-use-BBDB-exclusive
753 (defalias 'bbdb-search-simple 'ignore)
754 (defalias 'spam-check-BBDB 'ignore)
755 (defalias 'spam-BBDB-register-routine 'ignore)
756 (defalias 'spam-enter-ham-BBDB 'ignore)
757 (defalias 'bbdb-create-internal 'ignore)
758 (defalias 'bbdb-records 'ignore))))
763 ;;; check the ifile backend; return nil if the mail was NOT classified
766 (defun spam-get-ifile-database-parameter ()
767 "Get the command-line parameter for ifile's database from spam-ifile-database-path."
768 (if spam-ifile-database-path
769 (format "--db-file=%s" spam-ifile-database-path)
772 (defun spam-check-ifile ()
773 "Check the ifile backend for the classification of this message"
774 (let ((article-buffer-name (buffer-name))
777 (let ((temp-buffer-name (buffer-name))
778 (db-param (spam-get-ifile-database-parameter)))
780 (set-buffer article-buffer-name)
782 (call-process-region (point-min) (point-max) spam-ifile-path
783 nil temp-buffer-name nil "-q" "-c" db-param)
784 (call-process-region (point-min) (point-max) spam-ifile-path
785 nil temp-buffer-name nil "-q" "-c")))
786 (goto-char (point-min))
788 (setq category (buffer-substring (point) (spam-point-at-eol))))
789 (when (not (zerop (length category))) ; we need a category here
790 (if spam-ifile-all-categories
791 (setq return category)
792 ;; else, if spam-ifile-all-categories is not set...
793 (when (string-equal spam-ifile-spam-category category)
794 (setq return spam-split-group))))))
797 (defun spam-ifile-register-with-ifile (article-string category)
798 "Register an article, given as a string, with a category.
799 Uses `gnus-newsgroup-name' if category is nil (for ham registration)."
800 (when (stringp article-string)
801 (let ((category (or category gnus-newsgroup-name))
802 (db-param (spam-get-ifile-database-parameter)))
804 (insert article-string)
806 (call-process-region (point-min) (point-max) spam-ifile-path
808 "-h" "-i" category db-param)
809 (call-process-region (point-min) (point-max) spam-ifile-path
811 "-h" "-i" category))))))
813 (defun spam-ifile-register-spam-routine ()
814 (spam-generic-register-routine
816 (spam-ifile-register-with-ifile
817 (spam-get-article-as-string article) spam-ifile-spam-category))
820 (defun spam-ifile-register-ham-routine ()
821 (spam-generic-register-routine
824 (spam-ifile-register-with-ifile
825 (spam-get-article-as-string article) spam-ifile-ham-category))))
832 (let ((spam-stat-install-hooks nil))
833 (require 'spam-stat))
835 (defun spam-check-stat ()
836 "Check the spam-stat backend for the classification of this message"
837 (let ((spam-stat-split-fancy-spam-group spam-split-group) ; override
838 (spam-stat-buffer (buffer-name)) ; stat the current buffer
840 (spam-stat-split-fancy)))
842 (defun spam-stat-register-spam-routine ()
843 (spam-generic-register-routine
845 (let ((article-string (spam-get-article-as-string article)))
847 (insert article-string)
848 (spam-stat-buffer-is-spam))))
851 (defun spam-stat-register-ham-routine ()
852 (spam-generic-register-routine
855 (let ((article-string (spam-get-article-as-string article)))
857 (insert article-string)
858 (spam-stat-buffer-is-non-spam))))))
860 (defun spam-maybe-spam-stat-load ()
861 (when spam-use-stat (spam-stat-load)))
863 (defun spam-maybe-spam-stat-save ()
864 (when spam-use-stat (spam-stat-save)))
866 ;; Add hooks for loading and saving the spam stats
867 (add-hook 'gnus-save-newsrc-hook 'spam-maybe-spam-stat-save)
868 (add-hook 'gnus-get-top-new-news-hook 'spam-maybe-spam-stat-load)
869 (add-hook 'gnus-startup-hook 'spam-maybe-spam-stat-load))
872 (defalias 'spam-stat-register-ham-routine 'ignore)
873 (defalias 'spam-stat-register-spam-routine 'ignore)
874 (defalias 'spam-stat-buffer-is-spam 'ignore)
875 (defalias 'spam-stat-buffer-is-non-spam 'ignore)
876 (defalias 'spam-stat-split-fancy 'ignore)
877 (defalias 'spam-stat-load 'ignore)
878 (defalias 'spam-stat-save 'ignore)
879 (defalias 'spam-check-stat 'ignore))))
883 ;;;; Blacklists and whitelists.
885 (defvar spam-whitelist-cache nil)
886 (defvar spam-blacklist-cache nil)
888 (defun spam-enter-whitelist (address)
889 "Enter ADDRESS into the whitelist."
890 (interactive "sAddress: ")
891 (spam-enter-list address spam-whitelist)
892 (setq spam-whitelist-cache nil))
894 (defun spam-enter-blacklist (address)
895 "Enter ADDRESS into the blacklist."
896 (interactive "sAddress: ")
897 (spam-enter-list address spam-blacklist)
898 (setq spam-blacklist-cache nil))
900 (defun spam-enter-list (address file)
901 "Enter ADDRESS into the given FILE, either the whitelist or the blacklist."
902 (unless (file-exists-p (file-name-directory file))
903 (make-directory (file-name-directory file) t))
906 (find-file-noselect file))
907 (goto-char (point-max))
910 (insert address "\n")
913 ;;; returns t if the sender is in the whitelist, nil or spam-split-group otherwise
914 (defun spam-check-whitelist ()
915 ;; FIXME! Should it detect when file timestamps change?
916 (unless spam-whitelist-cache
917 (setq spam-whitelist-cache (spam-parse-list spam-whitelist)))
918 (if (spam-from-listed-p spam-whitelist-cache)
920 (if spam-use-whitelist-exclusive
924 (defun spam-check-blacklist ()
925 ;; FIXME! Should it detect when file timestamps change?
926 (unless spam-blacklist-cache
927 (setq spam-blacklist-cache (spam-parse-list spam-blacklist)))
928 (and (spam-from-listed-p spam-blacklist-cache) spam-split-group))
930 (defun spam-parse-list (file)
931 (when (file-readable-p file)
932 (let (contents address)
934 (insert-file-contents file)
936 (setq address (buffer-substring (point) (spam-point-at-eol)))
938 (unless (zerop (length address))
939 (setq address (regexp-quote address))
940 (while (string-match "\\\\\\*" address)
941 (setq address (replace-match ".*" t t address)))
942 (push address contents))))
943 (nreverse contents))))
945 (defun spam-from-listed-p (cache)
946 (let ((from (message-fetch-field "from"))
949 (when (string-match (pop cache) from)
954 (defun spam-blacklist-register-routine ()
955 (spam-generic-register-routine
958 (let ((from (spam-fetch-field-from-fast article)))
960 (spam-enter-blacklist from))))
964 (defun spam-whitelist-register-routine ()
965 (spam-generic-register-routine
970 (let ((from (spam-fetch-field-from-fast article)))
972 (spam-enter-whitelist from))))))
975 ;;;; Spam-report glue
976 (defun spam-report-gmane-register-routine ()
977 (spam-generic-register-routine
983 (defun spam-check-bogofilter-headers (&optional score)
984 (let ((header (message-fetch-field spam-bogofilter-header)))
986 (string-match spam-bogofilter-bogosity-positive-spam-header
989 (when (string-match "spamicity=\\([0-9.]+\\)" header)
990 (match-string 1 header))
993 ;; return something sensible if the score can't be determined
994 (defun spam-bogofilter-score ()
995 "Get the Bogofilter spamicity score"
997 (save-window-excursion
998 (gnus-summary-show-article t)
999 (set-buffer gnus-article-buffer)
1000 (let ((score (or (spam-check-bogofilter-headers t)
1001 (spam-check-bogofilter t))))
1002 (message "Spamicity score %s" score)
1005 (defun spam-check-bogofilter (&optional score)
1006 "Check the Bogofilter backend for the classification of this message"
1007 (let ((article-buffer-name (buffer-name))
1010 (let ((temp-buffer-name (buffer-name)))
1012 (set-buffer article-buffer-name)
1013 (if spam-bogofilter-database-directory
1014 (call-process-region (point-min) (point-max)
1015 spam-bogofilter-path
1016 nil temp-buffer-name nil "-v"
1017 "-d" spam-bogofilter-database-directory)
1018 (call-process-region (point-min) (point-max) spam-bogofilter-path
1019 nil temp-buffer-name nil "-v")))
1020 (setq return (spam-check-bogofilter-headers score))))
1023 (defun spam-bogofilter-register-with-bogofilter (article-string spam)
1024 "Register an article, given as a string, as spam or non-spam."
1025 (when (stringp article-string)
1026 (let ((switch (if spam spam-bogofilter-spam-switch
1027 spam-bogofilter-ham-switch)))
1029 (insert article-string)
1030 (if spam-bogofilter-database-directory
1031 (call-process-region (point-min) (point-max)
1032 spam-bogofilter-path
1033 nil nil nil "-v" switch
1034 "-d" spam-bogofilter-database-directory)
1035 (call-process-region (point-min) (point-max) spam-bogofilter-path
1036 nil nil nil "-v" switch))))))
1038 (defun spam-bogofilter-register-spam-routine ()
1039 (spam-generic-register-routine
1041 (spam-bogofilter-register-with-bogofilter
1042 (spam-get-article-as-string article) t))
1045 (defun spam-bogofilter-register-ham-routine ()
1046 (spam-generic-register-routine
1049 (spam-bogofilter-register-with-bogofilter
1050 (spam-get-article-as-string article) nil))))
1054 ;;; spam.el ends here.