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-all-categories nil
202 "Whether the ifile check will return all categories, or just spam.
203 Set this to t if you want to use the spam-split invocation of ifile as
204 your main source of newsgroup names."
208 (defgroup spam-bogofilter nil
209 "Spam bogofilter configuration."
212 (defcustom spam-bogofilter-path (executable-find "bogofilter")
213 "File path of the Bogofilter executable program."
214 :type '(choice (file :tag "Location of bogofilter")
215 (const :tag "Bogofilter is not installed"))
216 :group 'spam-bogofilter)
218 (defcustom spam-bogofilter-header "X-Bogosity"
219 "The header that Bogofilter inserts in messages."
221 :group 'spam-bogofilter)
223 (defcustom spam-bogofilter-database-directory nil
224 "Directory path of the Bogofilter databases."
225 :type '(choice (directory :tag "Location of the Bogofilter database directory")
226 (const :tag "Use the default"))
229 ;;; Key bindings for spam control.
231 (gnus-define-keys gnus-summary-mode-map
232 "St" spam-bogofilter-score
233 "Sx" gnus-summary-mark-as-spam
234 "Mst" spam-bogofilter-score
235 "Msx" gnus-summary-mark-as-spam
236 "\M-d" gnus-summary-mark-as-spam)
238 ;;; How to highlight a spam summary line.
240 ;; TODO: How do we redo this every time spam-face is customized?
242 (push '((eq mark gnus-spam-mark) . spam-face)
243 gnus-summary-highlight)
245 ;; convenience functions
246 (defun spam-group-spam-contents-p (group)
248 (or (member group spam-junk-mailgroups)
249 (memq 'gnus-group-spam-classification-spam
250 (gnus-parameter-spam-contents group)))
253 (defun spam-group-ham-contents-p (group)
255 (memq 'gnus-group-spam-classification-ham
256 (gnus-parameter-spam-contents group))
259 (defun spam-group-processor-p (group processor)
260 (if (and (stringp group)
262 (member processor (car (gnus-parameter-spam-process group)))
265 (defun spam-group-spam-processor-bogofilter-p (group)
266 (spam-group-processor-p group 'gnus-group-spam-exit-processor-bogofilter))
268 (defun spam-group-spam-processor-blacklist-p (group)
269 (spam-group-processor-p group 'gnus-group-spam-exit-processor-blacklist))
271 (defun spam-group-spam-processor-ifile-p (group)
272 (spam-group-processor-p group 'gnus-group-spam-exit-processor-ifile))
274 (defun spam-group-ham-processor-ifile-p (group)
275 (spam-group-processor-p group 'gnus-group-ham-exit-processor-ifile))
277 (defun spam-group-ham-processor-bogofilter-p (group)
278 (spam-group-processor-p group 'gnus-group-ham-exit-processor-ifile))
280 (defun spam-group-spam-processor-stat-p (group)
281 (spam-group-processor-p group 'gnus-group-spam-exit-processor-stat))
283 (defun spam-group-ham-processor-stat-p (group)
284 (spam-group-processor-p group 'gnus-group-ham-exit-processor-stat))
286 (defun spam-group-ham-processor-whitelist-p (group)
287 (spam-group-processor-p group 'gnus-group-ham-exit-processor-whitelist))
289 (defun spam-group-ham-processor-BBDB-p (group)
290 (spam-group-processor-p group 'gnus-group-ham-exit-processor-BBDB))
292 ;;; Summary entry and exit processing.
294 (defun spam-summary-prepare ()
295 (spam-mark-junk-as-spam-routine))
297 (add-hook 'gnus-summary-prepare-hook 'spam-summary-prepare)
299 (defun spam-summary-prepare-exit ()
300 ;; The spam processors are invoked for any group, spam or ham or neither
301 (when (and spam-bogofilter-path
302 (spam-group-spam-processor-bogofilter-p gnus-newsgroup-name))
303 (spam-bogofilter-register-spam-routine))
305 (when (and spam-ifile-path
306 (spam-group-spam-processor-ifile-p gnus-newsgroup-name))
307 (spam-ifile-register-spam-routine))
309 (when (spam-group-spam-processor-stat-p gnus-newsgroup-name)
310 (spam-stat-register-spam-routine))
312 (when (spam-group-spam-processor-blacklist-p gnus-newsgroup-name)
313 (spam-blacklist-register-routine))
315 (if spam-move-spam-nonspam-groups-only
316 (when (not (spam-group-spam-contents-p gnus-newsgroup-name))
317 (spam-mark-spam-as-expired-and-move-routine
318 (gnus-parameter-spam-process-destination gnus-newsgroup-name)))
319 (spam-mark-spam-as-expired-and-move-routine
320 (gnus-parameter-spam-process-destination gnus-newsgroup-name)))
322 ;; now we redo spam-mark-spam-as-expired-and-move-routine to only
323 ;; expire spam, in case the above did not expire them
324 (spam-mark-spam-as-expired-and-move-routine nil)
326 (when (spam-group-ham-contents-p gnus-newsgroup-name)
327 (when (spam-group-ham-processor-whitelist-p gnus-newsgroup-name)
328 (spam-whitelist-register-routine))
329 (when (spam-group-ham-processor-ifile-p gnus-newsgroup-name)
330 (spam-ifile-register-ham-routine))
331 (when (spam-group-ham-processor-bogofilter-p gnus-newsgroup-name)
332 (spam-bogofilter-register-ham-routine))
333 (when (spam-group-ham-processor-stat-p gnus-newsgroup-name)
334 (spam-stat-register-ham-routine))
335 (when (spam-group-ham-processor-BBDB-p gnus-newsgroup-name)
336 (spam-BBDB-register-routine)))
338 ;; now move all ham articles out of spam groups
339 (when (spam-group-spam-contents-p gnus-newsgroup-name)
340 (spam-ham-move-routine
341 (gnus-parameter-ham-process-destination gnus-newsgroup-name))))
343 (add-hook 'gnus-summary-prepare-exit-hook 'spam-summary-prepare-exit)
345 (defun spam-mark-junk-as-spam-routine ()
346 ;; check the global list of group names spam-junk-mailgroups and the
348 (when (spam-group-spam-contents-p gnus-newsgroup-name)
349 (let ((articles gnus-newsgroup-articles)
352 (setq article (pop articles))
353 (when (eq (gnus-summary-article-mark article) gnus-unread-mark)
354 (gnus-summary-mark-article article gnus-spam-mark))))))
356 (defun spam-mark-spam-as-expired-and-move-routine (&optional group)
357 (let ((articles gnus-newsgroup-articles)
360 (setq article (pop articles))
361 (when (eq (gnus-summary-article-mark article) gnus-spam-mark)
362 (gnus-summary-mark-article article gnus-expirable-mark)
363 (when (stringp group)
364 (let ((gnus-current-article article))
365 (gnus-summary-move-article nil group)))))))
367 (defun spam-ham-move-routine (&optional group)
368 (let ((articles gnus-newsgroup-articles)
369 article ham-mark-values mark)
370 (dolist (mark spam-ham-marks)
371 (push (symbol-value mark) ham-mark-values))
374 (setq article (pop articles))
375 (when (and (memq mark ham-mark-values)
377 (let ((gnus-current-article article))
378 (gnus-summary-move-article nil group))))))
380 (defun spam-generic-register-routine (spam-func ham-func)
381 (let ((articles gnus-newsgroup-articles)
382 article mark ham-articles spam-articles spam-mark-values
385 ;; marks are stored as symbolic values, so we have to dereference
386 ;; them for memq to work. we wouldn't have to do this if
387 ;; gnus-summary-article-mark returned a symbol.
388 (dolist (mark spam-ham-marks)
389 (push (symbol-value mark) ham-mark-values))
391 (dolist (mark spam-spam-marks)
392 (push (symbol-value mark) spam-mark-values))
395 (setq article (pop articles)
396 mark (gnus-summary-article-mark article))
397 (cond ((memq mark spam-mark-values) (push article spam-articles))
398 ((memq article gnus-newsgroup-saved))
399 ((memq mark ham-mark-values) (push article ham-articles))))
400 (when (and ham-articles ham-func)
401 (mapc ham-func ham-articles)) ; we use mapc because unlike
402 ; mapcar it discards the
404 (when (and spam-articles spam-func)
405 (mapc spam-func spam-articles)))) ; we use mapc because unlike
406 ; mapcar it discards the
410 (defalias 'spam-point-at-eol (if (fboundp 'point-at-eol)
412 'line-end-position)))
414 (defun spam-get-article-as-string (article)
415 (let ((article-buffer (spam-get-article-as-buffer article))
418 (save-window-excursion
419 (set-buffer article-buffer)
420 (setq article-string (buffer-string))))
423 (defun spam-get-article-as-buffer (article)
424 (let ((article-buffer))
425 (when (numberp article)
426 (save-window-excursion
427 (gnus-summary-goto-subject article)
428 (gnus-summary-show-article t)
429 (setq article-buffer (get-buffer gnus-article-buffer))))
432 (defun spam-get-article-as-filename (article)
433 (let ((article-filename))
434 (when (numberp article)
435 (nnml-possibly-change-directory (gnus-group-real-name gnus-newsgroup-name))
436 (setq article-filename (expand-file-name (int-to-string article) nnml-current-directory)))
437 (if (file-exists-p article-filename)
441 (defun spam-fetch-field-from-fast (article)
442 "Fetch the `from' field quickly, using the internal gnus-data-list function"
443 (if (and (numberp article)
444 (assoc article (gnus-data-list nil)))
445 (mail-header-from (gnus-data-header (assoc article (gnus-data-list nil))))
448 (defun spam-fetch-field-subject-fast (article)
449 "Fetch the `subject' field quickly, using the internal gnus-data-list function"
450 (if (and (numberp article)
451 (assoc article (gnus-data-list nil)))
452 (mail-header-subject (gnus-data-header (assoc article (gnus-data-list nil))))
456 ;;;; Spam determination.
458 (defvar spam-list-of-checks
459 '((spam-use-blacklist . spam-check-blacklist)
460 (spam-use-whitelist . spam-check-whitelist)
461 (spam-use-BBDB . spam-check-BBDB)
462 (spam-use-ifile . spam-check-ifile)
463 (spam-use-stat . spam-check-stat)
464 (spam-use-blackholes . spam-check-blackholes)
465 (spam-use-bogofilter-headers . spam-check-bogofilter-headers)
466 (spam-use-bogofilter . spam-check-bogofilter))
467 "The spam-list-of-checks list contains pairs associating a parameter
468 variable with a spam checking function. If the parameter variable is
469 true, then the checking function is called, and its value decides what
470 happens. Each individual check may return `nil', `t', or a mailgroup
471 name. The value `nil' means that the check does not yield a decision,
472 and so, that further checks are needed. The value `t' means that the
473 message is definitely not spam, and that further spam checks should be
474 inhibited. Otherwise, a mailgroup name is returned where the mail
475 should go, and further checks are also inhibited. The usual mailgroup
476 name is the value of `spam-split-group', meaning that the message is
480 "Split this message into the `spam' group if it is spam.
481 This function can be used as an entry in `nnmail-split-fancy', for
482 example like this: (: spam-split)
484 See the Info node `(gnus)Fancy Mail Splitting' for more details."
487 ;; load the spam-stat tables if needed
488 (when spam-use-stat (spam-stat-load))
490 (let ((list-of-checks spam-list-of-checks)
492 (while (and list-of-checks (not decision))
493 (let ((pair (pop list-of-checks)))
494 (when (symbol-value (car pair))
495 (setq decision (funcall (cdr pair))))))
502 (defun spam-check-blackholes ()
503 "Check the Received headers for blackholed relays."
504 (let ((headers (message-fetch-field "received"))
509 (goto-char (point-min))
510 (while (re-search-forward
511 "\\[\\([0-9]+.[0-9]+.[0-9]+.[0-9]+\\)\\]" nil t)
512 (message "Blackhole search found host IP %s." (match-string 1))
513 (push (mapconcat 'identity
514 (nreverse (split-string (match-string 1) "\\."))
517 (dolist (server spam-blackhole-servers)
519 (let ((query-string (concat ip "." server)))
521 (let ((query-result (query-dig query-string)))
523 (message "spam: positive blackhole check '%s'" query-result)
524 (push (list ip server query-result)
526 ;; else, if not using dig.el
527 (when (query-dns query-string)
528 (push (list ip server (query-dns query-string 'TXT))
535 ;;; original idea for spam-check-BBDB from Alexander Kotelnikov
536 ;;; <sacha@giotto.sj.ru>
538 ;; all this is done inside a condition-case to trap errors
545 (defun spam-enter-ham-BBDB (from)
546 "Enter an address into the BBDB; implies ham (non-spam) sender"
548 (let* ((parsed-address (gnus-extract-address-components from))
549 (name (or (car parsed-address) "Ham Sender"))
550 (net-address (car (cdr parsed-address))))
551 (message "Adding address %s to BBDB" from)
552 (when (and net-address
553 (not (bbdb-search-simple nil net-address)))
554 (bbdb-create-internal name nil net-address nil nil
555 "ham sender added by spam.el")))))
557 (defun spam-BBDB-register-routine ()
558 (spam-generic-register-routine
563 (spam-enter-ham-BBDB (spam-fetch-field-from-fast article)))))
565 (defun spam-check-BBDB ()
566 "Mail from people in the BBDB is never considered spam"
567 (let ((who (message-fetch-field "from")))
569 (setq who (regexp-quote (cadr
570 (gnus-extract-address-components who))))
571 (if (bbdb-search-simple nil who)
572 nil spam-split-group)))))
575 (defalias 'bbdb-search-simple 'ignore)
576 (defalias 'spam-check-BBDB 'ignore)
577 (defalias 'spam-BBDB-register-routine 'ignore)
578 (defalias 'spam-enter-ham-BBDB 'ignore)
579 (defalias 'bbdb-create-internal 'ignore)
580 (defalias 'bbdb-records 'ignore))))
585 ;;; check the ifile backend; return nil if the mail was NOT classified
588 (defun spam-get-ifile-database-parameter ()
589 "Get the command-line parameter for ifile's database from spam-ifile-database-path."
590 (if spam-ifile-database-path
591 (format "--db-file=%s" spam-ifile-database-path)
594 (defun spam-check-ifile ()
595 "Check the ifile backend for the classification of this message"
596 (let ((article-buffer-name (buffer-name))
599 (let ((temp-buffer-name (buffer-name))
600 (db-param (spam-get-ifile-database-parameter)))
602 (set-buffer article-buffer-name)
604 (call-process-region (point-min) (point-max) spam-ifile-path
605 nil temp-buffer-name nil "-q" "-c" db-param)
606 (call-process-region (point-min) (point-max) spam-ifile-path
607 nil temp-buffer-name nil "-q" "-c")))
608 (goto-char (point-min))
610 (setq category (buffer-substring (point) (spam-point-at-eol))))
611 (when (not (zerop (length category))) ; we need a category here
612 (if spam-ifile-all-categories
613 (setq return category)
614 ;; else, if spam-ifile-all-categories is not set...
615 (when (string-equal spam-ifile-spam-category category)
616 (setq return spam-split-group))))))
619 (defun spam-ifile-register-with-ifile (article-string category)
620 "Register an article, given as a string, with a category.
621 Uses `gnus-newsgroup-name' if category is nil (for ham registration)."
622 (when (stringp article-string)
623 (let ((category (or category gnus-newsgroup-name))
624 (db-param (spam-get-ifile-database-parameter)))
626 (insert-string article-string)
628 (call-process-region (point-min) (point-max) spam-ifile-path
630 "-h" "-i" category db-param)
631 (call-process-region (point-min) (point-max) spam-ifile-path
633 "-h" "-i" category))))))
635 (defun spam-ifile-register-spam-routine ()
636 (spam-generic-register-routine
638 (spam-ifile-register-with-ifile
639 (spam-get-article-as-string article) spam-ifile-spam-category))
642 (defun spam-ifile-register-ham-routine ()
643 (spam-generic-register-routine
646 (spam-ifile-register-with-ifile
647 (spam-get-article-as-string article) nil))))
654 (let ((spam-stat-install-hooks nil))
655 (require 'spam-stat))
657 (defun spam-check-stat ()
658 "Check the spam-stat backend for the classification of this message"
659 (let ((spam-stat-split-fancy-spam-group spam-split-group) ; override
660 (spam-stat-buffer (buffer-name)) ; stat the current buffer
662 (spam-stat-split-fancy)))
664 (defun spam-stat-register-spam-routine ()
665 (spam-generic-register-routine
667 (let ((article-string (spam-get-article-as-string article)))
669 (insert-string article-string)
670 (spam-stat-buffer-is-spam))))
674 (defun spam-stat-register-ham-routine ()
675 (spam-generic-register-routine
678 (let ((article-string (spam-get-article-as-string article)))
680 (insert-string article-string)
681 (spam-stat-buffer-is-non-spam)))))
685 (defalias 'spam-stat-register-ham-routine 'ignore)
686 (defalias 'spam-stat-register-spam-routine 'ignore)
687 (defalias 'spam-stat-buffer-is-spam 'ignore)
688 (defalias 'spam-stat-buffer-is-non-spam 'ignore)
689 (defalias 'spam-stat-split-fancy 'ignore)
690 (defalias 'spam-stat-load 'ignore)
691 (defalias 'spam-stat-save 'ignore)
692 (defalias 'spam-check-stat 'ignore))))
696 ;;;; Blacklists and whitelists.
698 (defvar spam-whitelist-cache nil)
699 (defvar spam-blacklist-cache nil)
701 (defun spam-enter-whitelist (address)
702 "Enter ADDRESS into the whitelist."
703 (interactive "sAddress: ")
704 (spam-enter-list address spam-whitelist)
705 (setq spam-whitelist-cache nil))
707 (defun spam-enter-blacklist (address)
708 "Enter ADDRESS into the blacklist."
709 (interactive "sAddress: ")
710 (spam-enter-list address spam-blacklist)
711 (setq spam-blacklist-cache nil))
713 (defun spam-enter-list (address file)
714 "Enter ADDRESS into the given FILE, either the whitelist or the blacklist."
715 (unless (file-exists-p (file-name-directory file))
716 (make-directory (file-name-directory file) t))
719 (find-file-noselect file))
720 (goto-char (point-max))
723 (insert address "\n")
726 ;;; returns nil if the sender is in the whitelist, spam-split-group otherwise
727 (defun spam-check-whitelist ()
728 ;; FIXME! Should it detect when file timestamps change?
729 (unless spam-whitelist-cache
730 (setq spam-whitelist-cache (spam-parse-list spam-whitelist)))
731 (if (spam-from-listed-p spam-whitelist-cache) nil spam-split-group))
733 (defun spam-check-blacklist ()
734 ;; FIXME! Should it detect when file timestamps change?
735 (unless spam-blacklist-cache
736 (setq spam-blacklist-cache (spam-parse-list spam-blacklist)))
737 (and (spam-from-listed-p spam-blacklist-cache) spam-split-group))
739 (defun spam-parse-list (file)
740 (when (file-readable-p file)
741 (let (contents address)
743 (insert-file-contents file)
745 (setq address (buffer-substring (point) (spam-point-at-eol)))
747 (unless (zerop (length address))
748 (setq address (regexp-quote address))
749 (while (string-match "\\\\\\*" address)
750 (setq address (replace-match ".*" t t address)))
751 (push address contents))))
752 (nreverse contents))))
754 (defun spam-from-listed-p (cache)
755 (let ((from (message-fetch-field "from"))
758 (when (string-match (pop cache) from)
763 (defun spam-blacklist-register-routine ()
764 (spam-generic-register-routine
767 (let ((from (spam-fetch-field-from-fast article)))
769 (spam-enter-blacklist from))))
773 (defun spam-whitelist-register-routine ()
774 (spam-generic-register-routine
779 (let ((from (spam-fetch-field-from-fast article)))
781 (spam-enter-whitelist from))))))
786 (defun spam-check-bogofilter-headers (&optional score)
787 (let ((header (message-fetch-field spam-bogofilter-header)))
789 (string-match "^Yes" header))
791 (when (string-match "spamicity=\\([0-9.]+\\)" header)
792 (match-string 1 header))
796 ;; return something sensible if the score can't be determined
797 (defun spam-bogofilter-score ()
798 "Get the Bogofilter spamicity score"
800 (save-window-excursion
801 (gnus-summary-show-article t)
802 (set-buffer gnus-article-buffer)
803 (let ((score (spam-check-bogofilter t)))
804 (message "Spamicity score %s" score)
807 (defun spam-check-bogofilter (&optional score)
808 "Check the Bogofilter backend for the classification of this message"
809 (let ((article-buffer-name (buffer-name))
812 (let ((temp-buffer-name (buffer-name)))
814 (set-buffer article-buffer-name)
815 (if spam-bogofilter-database-directory
816 (call-process-region (point-min) (point-max)
818 nil temp-buffer-name nil "-v"
819 "-d" spam-bogofilter-database-directory)
820 (call-process-region (point-min) (point-max) spam-bogofilter-path
821 nil temp-buffer-name nil "-v")))
822 (setq return (spam-check-bogofilter-headers score))))
825 (defun spam-bogofilter-register-with-bogofilter (article-string spam)
826 "Register an article, given as a string, as spam or non-spam."
827 (when (stringp article-string)
828 (let ((switch (if spam "-s" "-n")))
830 (insert-string article-string)
831 (if spam-bogofilter-database-directory
832 (call-process-region (point-min) (point-max)
834 nil nil nil "-v" switch
835 "-d" spam-bogofilter-database-directory)
836 (call-process-region (point-min) (point-max) spam-bogofilter-path
837 nil nil nil "-v" switch))))))
839 (defun spam-bogofilter-register-spam-routine ()
840 (spam-generic-register-routine
842 (spam-bogofilter-register-with-bogofilter
843 (spam-get-article-as-string article) t))
846 (defun spam-bogofilter-register-ham-routine ()
847 (spam-generic-register-routine
850 (spam-bogofilter-register-with-bogofilter
851 (spam-get-article-as-string article) nil))))
855 ;;; spam.el ends here.