1 ;;; spam.el --- Identifying spam
2 ;; Copyright (C) 2002 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 ;; autoload executable-find
45 ;; executable-find is not autoloaded in Emacs 20
46 (autoload 'executable-find "executable"))
50 (autoload 'query-dig "dig"))
54 (autoload 'query-dns "dns"))
59 "Spam configuration.")
61 (defcustom spam-directory "~/News/spam/"
62 "Directory for spam whitelists and blacklists."
66 (defcustom spam-move-spam-nonspam-groups-only t
67 "Whether spam should be moved in non-spam groups only.
68 When nil, only ham and unclassified groups will have their spam moved
69 to the spam-process-destination. When t, spam will also be moved from
74 (defcustom spam-whitelist (expand-file-name "whitelist" spam-directory)
75 "The location of the whitelist.
76 The file format is one regular expression per line.
77 The regular expression is matched against the address."
81 (defcustom spam-blacklist (expand-file-name "blacklist" spam-directory)
82 "The location of the blacklist.
83 The file format is one regular expression per line.
84 The regular expression is matched against the address."
88 (defcustom spam-use-dig t
89 "Whether query-dig should be used instead of query-dns."
93 (defcustom spam-use-blacklist nil
94 "Whether the blacklist should be used by spam-split."
98 (defcustom spam-use-whitelist nil
99 "Whether the whitelist should be used by spam-split."
103 (defcustom spam-use-blackholes nil
104 "Whether blackholes should be used by spam-split."
108 (defcustom spam-use-bogofilter-headers nil
109 "Whether bogofilter headers should be used by spam-split.
110 Enable this if you pre-process messages with Bogofilter BEFORE Gnus sees them."
114 (defcustom spam-use-bogofilter nil
115 "Whether bogofilter should be invoked by spam-split.
116 Enable this if you want Gnus to invoke Bogofilter on new messages."
120 (defcustom spam-use-BBDB nil
121 "Whether BBDB should be used by spam-split."
125 (defcustom spam-use-ifile nil
126 "Whether ifile should be used by spam-split."
130 (defcustom spam-use-stat nil
131 "Whether spam-stat should be used by spam-split."
135 (defcustom spam-split-group "spam"
136 "Group name where incoming spam should be put by spam-split."
140 (defcustom spam-junk-mailgroups (cons spam-split-group '("mail.junk" "poste.pourriel"))
141 "Mailgroups with spam contents.
142 All unmarked article in such group receive the spam mark on group entry."
143 :type '(repeat (string :tag "Group"))
146 (defcustom spam-blackhole-servers '("bl.spamcop.net" "relays.ordb.org"
147 "dev.null.dk" "relays.visi.com")
148 "List of blackhole servers."
149 :type '(repeat (string :tag "Server"))
152 (defcustom spam-ham-marks (list 'gnus-del-mark 'gnus-read-mark
153 'gnus-killed-mark 'gnus-kill-file-mark
154 'gnus-low-score-mark)
155 "Marks considered as being ham (positively not spam).
156 Such articles will be processed as ham (non-spam) on group exit."
158 (variable-item gnus-del-mark)
159 (variable-item gnus-read-mark)
160 (variable-item gnus-killed-mark)
161 (variable-item gnus-kill-file-mark)
162 (variable-item gnus-low-score-mark))
165 (defcustom spam-spam-marks (list 'gnus-spam-mark)
166 "Marks considered as being spam (positively spam).
167 Such articles will be transmitted to `bogofilter -s' on group exit."
169 (variable-item gnus-spam-mark)
170 (variable-item gnus-killed-mark)
171 (variable-item gnus-kill-file-mark)
172 (variable-item gnus-low-score-mark))
175 (defcustom spam-face 'gnus-splash-face
176 "Face for spam-marked articles"
180 (defgroup spam-ifile nil
181 "Spam ifile configuration."
184 (defcustom spam-ifile-path (executable-find "ifile")
185 "File path of the ifile executable program."
186 :type '(choice (file :tag "Location of ifile")
187 (const :tag "ifile is not installed"))
190 (defcustom spam-ifile-database-path nil
191 "File path of the ifile database."
192 :type '(choice (file :tag "Location of the ifile database")
193 (const :tag "Use the default"))
196 (defcustom spam-ifile-spam-category "spam"
197 "Name of the spam ifile category."
201 (defcustom spam-ifile-ham-category nil
202 "Name of the ham ifile category. If nil, the current group name will
204 :type '(choice (string :tag "Use a fixed category")
205 (const :tag "Use the current group name"))
208 (defcustom spam-ifile-all-categories nil
209 "Whether the ifile check will return all categories, or just spam.
210 Set this to t if you want to use the spam-split invocation of ifile as
211 your main source of newsgroup names."
215 (defgroup spam-bogofilter nil
216 "Spam bogofilter configuration."
219 (defcustom spam-bogofilter-path (executable-find "bogofilter")
220 "File path of the Bogofilter executable program."
221 :type '(choice (file :tag "Location of bogofilter")
222 (const :tag "Bogofilter is not installed"))
223 :group 'spam-bogofilter)
225 (defcustom spam-bogofilter-header "X-Bogosity"
226 "The header that Bogofilter inserts in messages."
228 :group 'spam-bogofilter)
230 (defcustom spam-bogofilter-database-directory nil
231 "Directory path of the Bogofilter databases."
232 :type '(choice (directory :tag "Location of the Bogofilter database directory")
233 (const :tag "Use the default"))
236 ;;; Key bindings for spam control.
238 (gnus-define-keys gnus-summary-mode-map
239 "St" spam-bogofilter-score
240 "Sx" gnus-summary-mark-as-spam
241 "Mst" spam-bogofilter-score
242 "Msx" gnus-summary-mark-as-spam
243 "\M-d" gnus-summary-mark-as-spam)
245 ;;; How to highlight a spam summary line.
247 ;; TODO: How do we redo this every time spam-face is customized?
249 (push '((eq mark gnus-spam-mark) . spam-face)
250 gnus-summary-highlight)
252 ;; convenience functions
253 (defun spam-group-spam-contents-p (group)
255 (or (member group spam-junk-mailgroups)
256 (memq 'gnus-group-spam-classification-spam
257 (gnus-parameter-spam-contents group)))
260 (defun spam-group-ham-contents-p (group)
262 (memq 'gnus-group-spam-classification-ham
263 (gnus-parameter-spam-contents group))
266 (defun spam-group-processor-p (group processor)
267 (if (and (stringp group)
269 (member processor (car (gnus-parameter-spam-process group)))
272 (defun spam-group-spam-processor-bogofilter-p (group)
273 (spam-group-processor-p group 'gnus-group-spam-exit-processor-bogofilter))
275 (defun spam-group-spam-processor-blacklist-p (group)
276 (spam-group-processor-p group 'gnus-group-spam-exit-processor-blacklist))
278 (defun spam-group-spam-processor-ifile-p (group)
279 (spam-group-processor-p group 'gnus-group-spam-exit-processor-ifile))
281 (defun spam-group-ham-processor-ifile-p (group)
282 (spam-group-processor-p group 'gnus-group-ham-exit-processor-ifile))
284 (defun spam-group-ham-processor-bogofilter-p (group)
285 (spam-group-processor-p group 'gnus-group-ham-exit-processor-bogofilter))
287 (defun spam-group-spam-processor-stat-p (group)
288 (spam-group-processor-p group 'gnus-group-spam-exit-processor-stat))
290 (defun spam-group-ham-processor-stat-p (group)
291 (spam-group-processor-p group 'gnus-group-ham-exit-processor-stat))
293 (defun spam-group-ham-processor-whitelist-p (group)
294 (spam-group-processor-p group 'gnus-group-ham-exit-processor-whitelist))
296 (defun spam-group-ham-processor-BBDB-p (group)
297 (spam-group-processor-p group 'gnus-group-ham-exit-processor-BBDB))
299 ;;; Summary entry and exit processing.
301 (defun spam-summary-prepare ()
302 (spam-mark-junk-as-spam-routine))
304 (add-hook 'gnus-summary-prepare-hook 'spam-summary-prepare)
306 (defun spam-summary-prepare-exit ()
307 ;; The spam processors are invoked for any group, spam or ham or neither
308 (when (and spam-bogofilter-path
309 (spam-group-spam-processor-bogofilter-p gnus-newsgroup-name))
310 (spam-bogofilter-register-spam-routine))
312 (when (and spam-ifile-path
313 (spam-group-spam-processor-ifile-p gnus-newsgroup-name))
314 (spam-ifile-register-spam-routine))
316 (when (spam-group-spam-processor-stat-p gnus-newsgroup-name)
317 (spam-stat-register-spam-routine))
319 (when (spam-group-spam-processor-blacklist-p gnus-newsgroup-name)
320 (spam-blacklist-register-routine))
322 (if spam-move-spam-nonspam-groups-only
323 (when (not (spam-group-spam-contents-p gnus-newsgroup-name))
324 (spam-mark-spam-as-expired-and-move-routine
325 (gnus-parameter-spam-process-destination gnus-newsgroup-name)))
326 (spam-mark-spam-as-expired-and-move-routine
327 (gnus-parameter-spam-process-destination gnus-newsgroup-name)))
329 ;; now we redo spam-mark-spam-as-expired-and-move-routine to only
330 ;; expire spam, in case the above did not expire them
331 (spam-mark-spam-as-expired-and-move-routine nil)
333 (when (spam-group-ham-contents-p gnus-newsgroup-name)
334 (when (spam-group-ham-processor-whitelist-p gnus-newsgroup-name)
335 (spam-whitelist-register-routine))
336 (when (spam-group-ham-processor-ifile-p gnus-newsgroup-name)
337 (spam-ifile-register-ham-routine))
338 (when (spam-group-ham-processor-bogofilter-p gnus-newsgroup-name)
339 (spam-bogofilter-register-ham-routine))
340 (when (spam-group-ham-processor-stat-p gnus-newsgroup-name)
341 (spam-stat-register-ham-routine))
342 (when (spam-group-ham-processor-BBDB-p gnus-newsgroup-name)
343 (spam-BBDB-register-routine)))
345 ;; now move all ham articles out of spam groups
346 (when (spam-group-spam-contents-p gnus-newsgroup-name)
347 (spam-ham-move-routine
348 (gnus-parameter-ham-process-destination gnus-newsgroup-name))))
350 (add-hook 'gnus-summary-prepare-exit-hook 'spam-summary-prepare-exit)
352 (defun spam-mark-junk-as-spam-routine ()
353 ;; check the global list of group names spam-junk-mailgroups and the
355 (when (spam-group-spam-contents-p gnus-newsgroup-name)
356 (let ((articles gnus-newsgroup-articles)
359 (setq article (pop articles))
360 (when (eq (gnus-summary-article-mark article) gnus-unread-mark)
361 (gnus-summary-mark-article article gnus-spam-mark))))))
363 (defun spam-mark-spam-as-expired-and-move-routine (&optional group)
364 (let ((articles gnus-newsgroup-articles)
367 (setq article (pop articles))
368 (when (eq (gnus-summary-article-mark article) gnus-spam-mark)
369 (gnus-summary-mark-article article gnus-expirable-mark)
370 (when (stringp group)
371 (let ((gnus-current-article article))
372 (gnus-summary-move-article nil group)))))))
374 (defun spam-ham-move-routine (&optional group)
375 (let ((articles gnus-newsgroup-articles)
376 article ham-mark-values mark)
377 (dolist (mark spam-ham-marks)
378 (push (symbol-value mark) ham-mark-values))
381 (setq article (pop articles))
382 (when (and (memq mark ham-mark-values)
384 (let ((gnus-current-article article))
385 (gnus-summary-move-article nil group))))))
387 (defun spam-generic-register-routine (spam-func ham-func)
388 (let ((articles gnus-newsgroup-articles)
389 article mark ham-articles spam-articles spam-mark-values
392 ;; marks are stored as symbolic values, so we have to dereference
393 ;; them for memq to work. we wouldn't have to do this if
394 ;; gnus-summary-article-mark returned a symbol.
395 (dolist (mark spam-ham-marks)
396 (push (symbol-value mark) ham-mark-values))
398 (dolist (mark spam-spam-marks)
399 (push (symbol-value mark) spam-mark-values))
402 (setq article (pop articles)
403 mark (gnus-summary-article-mark article))
404 (cond ((memq mark spam-mark-values) (push article spam-articles))
405 ((memq article gnus-newsgroup-saved))
406 ((memq mark ham-mark-values) (push article ham-articles))))
407 (when (and ham-articles ham-func)
408 (mapc ham-func ham-articles)) ; we use mapc because unlike
409 ; mapcar it discards the
411 (when (and spam-articles spam-func)
412 (mapc spam-func spam-articles)))) ; we use mapc because unlike
413 ; mapcar it discards the
417 (defalias 'spam-point-at-eol (if (fboundp 'point-at-eol)
419 'line-end-position)))
421 (defun spam-get-article-as-string (article)
422 (let ((article-buffer (spam-get-article-as-buffer article))
425 (save-window-excursion
426 (set-buffer article-buffer)
427 (setq article-string (buffer-string))))
430 (defun spam-get-article-as-buffer (article)
431 (let ((article-buffer))
432 (when (numberp article)
433 (save-window-excursion
434 (gnus-summary-goto-subject article)
435 (gnus-summary-show-article t)
436 (setq article-buffer (get-buffer gnus-article-buffer))))
439 (defun spam-get-article-as-filename (article)
440 (let ((article-filename))
441 (when (numberp article)
442 (nnml-possibly-change-directory (gnus-group-real-name gnus-newsgroup-name))
443 (setq article-filename (expand-file-name (int-to-string article) nnml-current-directory)))
444 (if (file-exists-p article-filename)
448 (defun spam-fetch-field-from-fast (article)
449 "Fetch the `from' field quickly, using the internal gnus-data-list function"
450 (if (and (numberp article)
451 (assoc article (gnus-data-list nil)))
452 (mail-header-from (gnus-data-header (assoc article (gnus-data-list nil))))
455 (defun spam-fetch-field-subject-fast (article)
456 "Fetch the `subject' field quickly, using the internal gnus-data-list function"
457 (if (and (numberp article)
458 (assoc article (gnus-data-list nil)))
459 (mail-header-subject (gnus-data-header (assoc article (gnus-data-list nil))))
463 ;;;; Spam determination.
465 (defvar spam-list-of-checks
466 '((spam-use-blacklist . spam-check-blacklist)
467 (spam-use-whitelist . spam-check-whitelist)
468 (spam-use-BBDB . spam-check-BBDB)
469 (spam-use-ifile . spam-check-ifile)
470 (spam-use-stat . spam-check-stat)
471 (spam-use-blackholes . spam-check-blackholes)
472 (spam-use-bogofilter-headers . spam-check-bogofilter-headers)
473 (spam-use-bogofilter . spam-check-bogofilter))
474 "The spam-list-of-checks list contains pairs associating a parameter
475 variable with a spam checking function. If the parameter variable is
476 true, then the checking function is called, and its value decides what
477 happens. Each individual check may return `nil', `t', or a mailgroup
478 name. The value `nil' means that the check does not yield a decision,
479 and so, that further checks are needed. The value `t' means that the
480 message is definitely not spam, and that further spam checks should be
481 inhibited. Otherwise, a mailgroup name is returned where the mail
482 should go, and further checks are also inhibited. The usual mailgroup
483 name is the value of `spam-split-group', meaning that the message is
487 "Split this message into the `spam' group if it is spam.
488 This function can be used as an entry in `nnmail-split-fancy', for
489 example like this: (: spam-split)
491 See the Info node `(gnus)Fancy Mail Splitting' for more details."
494 ;; load the spam-stat tables if needed
495 (when spam-use-stat (spam-stat-load))
497 (let ((list-of-checks spam-list-of-checks)
499 (while (and list-of-checks (not decision))
500 (let ((pair (pop list-of-checks)))
501 (when (symbol-value (car pair))
502 (setq decision (funcall (cdr pair))))))
509 (defun spam-check-blackholes ()
510 "Check the Received headers for blackholed relays."
511 (let ((headers (message-fetch-field "received"))
516 (goto-char (point-min))
517 (while (re-search-forward
518 "\\[\\([0-9]+.[0-9]+.[0-9]+.[0-9]+\\)\\]" nil t)
519 (message "Blackhole search found host IP %s." (match-string 1))
520 (push (mapconcat 'identity
521 (nreverse (split-string (match-string 1) "\\."))
524 (dolist (server spam-blackhole-servers)
526 (let ((query-string (concat ip "." server)))
528 (let ((query-result (query-dig query-string)))
530 (message "spam: positive blackhole check '%s'" query-result)
531 (push (list ip server query-result)
533 ;; else, if not using dig.el
534 (when (query-dns query-string)
535 (push (list ip server (query-dns query-string 'TXT))
542 ;;; original idea for spam-check-BBDB from Alexander Kotelnikov
543 ;;; <sacha@giotto.sj.ru>
545 ;; all this is done inside a condition-case to trap errors
552 (defun spam-enter-ham-BBDB (from)
553 "Enter an address into the BBDB; implies ham (non-spam) sender"
555 (let* ((parsed-address (gnus-extract-address-components from))
556 (name (or (car parsed-address) "Ham Sender"))
557 (net-address (car (cdr parsed-address))))
558 (message "Adding address %s to BBDB" from)
559 (when (and net-address
560 (not (bbdb-search-simple nil net-address)))
561 (bbdb-create-internal name nil net-address nil nil
562 "ham sender added by spam.el")))))
564 (defun spam-BBDB-register-routine ()
565 (spam-generic-register-routine
570 (spam-enter-ham-BBDB (spam-fetch-field-from-fast article)))))
572 (defun spam-check-BBDB ()
573 "Mail from people in the BBDB is never considered spam"
574 (let ((who (message-fetch-field "from")))
576 (setq who (regexp-quote (cadr
577 (gnus-extract-address-components who))))
578 (if (bbdb-search-simple nil who)
579 nil spam-split-group)))))
582 (defalias 'bbdb-search-simple 'ignore)
583 (defalias 'spam-check-BBDB 'ignore)
584 (defalias 'spam-BBDB-register-routine 'ignore)
585 (defalias 'spam-enter-ham-BBDB 'ignore)
586 (defalias 'bbdb-create-internal 'ignore)
587 (defalias 'bbdb-records 'ignore))))
592 ;;; check the ifile backend; return nil if the mail was NOT classified
595 (defun spam-get-ifile-database-parameter ()
596 "Get the command-line parameter for ifile's database from spam-ifile-database-path."
597 (if spam-ifile-database-path
598 (format "--db-file=%s" spam-ifile-database-path)
601 (defun spam-check-ifile ()
602 "Check the ifile backend for the classification of this message"
603 (let ((article-buffer-name (buffer-name))
606 (let ((temp-buffer-name (buffer-name))
607 (db-param (spam-get-ifile-database-parameter)))
609 (set-buffer article-buffer-name)
611 (call-process-region (point-min) (point-max) spam-ifile-path
612 nil temp-buffer-name nil "-q" "-c" db-param)
613 (call-process-region (point-min) (point-max) spam-ifile-path
614 nil temp-buffer-name nil "-q" "-c")))
615 (goto-char (point-min))
617 (setq category (buffer-substring (point) (spam-point-at-eol))))
618 (when (not (zerop (length category))) ; we need a category here
619 (if spam-ifile-all-categories
620 (setq return category)
621 ;; else, if spam-ifile-all-categories is not set...
622 (when (string-equal spam-ifile-spam-category category)
623 (setq return spam-split-group))))))
626 (defun spam-ifile-register-with-ifile (article-string category)
627 "Register an article, given as a string, with a category.
628 Uses `gnus-newsgroup-name' if category is nil (for ham registration)."
629 (when (stringp article-string)
630 (let ((category (or category gnus-newsgroup-name))
631 (db-param (spam-get-ifile-database-parameter)))
633 (insert-string article-string)
635 (call-process-region (point-min) (point-max) spam-ifile-path
637 "-h" "-i" category db-param)
638 (call-process-region (point-min) (point-max) spam-ifile-path
640 "-h" "-i" category))))))
642 (defun spam-ifile-register-spam-routine ()
643 (spam-generic-register-routine
645 (spam-ifile-register-with-ifile
646 (spam-get-article-as-string article) spam-ifile-spam-category))
649 (defun spam-ifile-register-ham-routine ()
650 (spam-generic-register-routine
653 (spam-ifile-register-with-ifile
654 (spam-get-article-as-string article) spam-ifile-ham-category))))
661 (let ((spam-stat-install-hooks nil))
662 (require 'spam-stat))
664 (defun spam-check-stat ()
665 "Check the spam-stat backend for the classification of this message"
666 (let ((spam-stat-split-fancy-spam-group spam-split-group) ; override
667 (spam-stat-buffer (buffer-name)) ; stat the current buffer
669 (spam-stat-split-fancy)))
671 (defun spam-stat-register-spam-routine ()
672 (spam-generic-register-routine
674 (let ((article-string (spam-get-article-as-string article)))
676 (insert-string article-string)
677 (spam-stat-buffer-is-spam))))
681 (defun spam-stat-register-ham-routine ()
682 (spam-generic-register-routine
685 (let ((article-string (spam-get-article-as-string article)))
687 (insert-string article-string)
688 (spam-stat-buffer-is-non-spam)))))
692 (defalias 'spam-stat-register-ham-routine 'ignore)
693 (defalias 'spam-stat-register-spam-routine 'ignore)
694 (defalias 'spam-stat-buffer-is-spam 'ignore)
695 (defalias 'spam-stat-buffer-is-non-spam 'ignore)
696 (defalias 'spam-stat-split-fancy 'ignore)
697 (defalias 'spam-stat-load 'ignore)
698 (defalias 'spam-stat-save 'ignore)
699 (defalias 'spam-check-stat 'ignore))))
703 ;;;; Blacklists and whitelists.
705 (defvar spam-whitelist-cache nil)
706 (defvar spam-blacklist-cache nil)
708 (defun spam-enter-whitelist (address)
709 "Enter ADDRESS into the whitelist."
710 (interactive "sAddress: ")
711 (spam-enter-list address spam-whitelist)
712 (setq spam-whitelist-cache nil))
714 (defun spam-enter-blacklist (address)
715 "Enter ADDRESS into the blacklist."
716 (interactive "sAddress: ")
717 (spam-enter-list address spam-blacklist)
718 (setq spam-blacklist-cache nil))
720 (defun spam-enter-list (address file)
721 "Enter ADDRESS into the given FILE, either the whitelist or the blacklist."
722 (unless (file-exists-p (file-name-directory file))
723 (make-directory (file-name-directory file) t))
726 (find-file-noselect file))
727 (goto-char (point-max))
730 (insert address "\n")
733 ;;; returns nil if the sender is in the whitelist, spam-split-group otherwise
734 (defun spam-check-whitelist ()
735 ;; FIXME! Should it detect when file timestamps change?
736 (unless spam-whitelist-cache
737 (setq spam-whitelist-cache (spam-parse-list spam-whitelist)))
738 (if (spam-from-listed-p spam-whitelist-cache) nil spam-split-group))
740 (defun spam-check-blacklist ()
741 ;; FIXME! Should it detect when file timestamps change?
742 (unless spam-blacklist-cache
743 (setq spam-blacklist-cache (spam-parse-list spam-blacklist)))
744 (and (spam-from-listed-p spam-blacklist-cache) spam-split-group))
746 (defun spam-parse-list (file)
747 (when (file-readable-p file)
748 (let (contents address)
750 (insert-file-contents file)
752 (setq address (buffer-substring (point) (spam-point-at-eol)))
754 (unless (zerop (length address))
755 (setq address (regexp-quote address))
756 (while (string-match "\\\\\\*" address)
757 (setq address (replace-match ".*" t t address)))
758 (push address contents))))
759 (nreverse contents))))
761 (defun spam-from-listed-p (cache)
762 (let ((from (message-fetch-field "from"))
765 (when (string-match (pop cache) from)
770 (defun spam-blacklist-register-routine ()
771 (spam-generic-register-routine
774 (let ((from (spam-fetch-field-from-fast article)))
776 (spam-enter-blacklist from))))
780 (defun spam-whitelist-register-routine ()
781 (spam-generic-register-routine
786 (let ((from (spam-fetch-field-from-fast article)))
788 (spam-enter-whitelist from))))))
793 (defun spam-check-bogofilter-headers (&optional score)
794 (let ((header (message-fetch-field spam-bogofilter-header)))
796 (string-match "^Yes" header))
798 (when (string-match "spamicity=\\([0-9.]+\\)" header)
799 (match-string 1 header))
803 ;; return something sensible if the score can't be determined
804 (defun spam-bogofilter-score ()
805 "Get the Bogofilter spamicity score"
807 (save-window-excursion
808 (gnus-summary-show-article t)
809 (set-buffer gnus-article-buffer)
810 (let ((score (spam-check-bogofilter t)))
811 (message "Spamicity score %s" score)
814 (defun spam-check-bogofilter (&optional score)
815 "Check the Bogofilter backend for the classification of this message"
816 (let ((article-buffer-name (buffer-name))
819 (let ((temp-buffer-name (buffer-name)))
821 (set-buffer article-buffer-name)
822 (if spam-bogofilter-database-directory
823 (call-process-region (point-min) (point-max)
825 nil temp-buffer-name nil "-v"
826 "-d" spam-bogofilter-database-directory)
827 (call-process-region (point-min) (point-max) spam-bogofilter-path
828 nil temp-buffer-name nil "-v")))
829 (setq return (spam-check-bogofilter-headers score))))
832 (defun spam-bogofilter-register-with-bogofilter (article-string spam)
833 "Register an article, given as a string, as spam or non-spam."
834 (when (stringp article-string)
835 (let ((switch (if spam "-s" "-n")))
837 (insert-string article-string)
838 (if spam-bogofilter-database-directory
839 (call-process-region (point-min) (point-max)
841 nil nil nil "-v" switch
842 "-d" spam-bogofilter-database-directory)
843 (call-process-region (point-min) (point-max) spam-bogofilter-path
844 nil nil nil "-v" switch))))))
846 (defun spam-bogofilter-register-spam-routine ()
847 (spam-generic-register-routine
849 (spam-bogofilter-register-with-bogofilter
850 (spam-get-article-as-string article) t))
853 (defun spam-bogofilter-register-ham-routine ()
854 (spam-generic-register-routine
857 (spam-bogofilter-register-with-bogofilter
858 (spam-get-article-as-string article) nil))))
862 ;;; spam.el ends here.