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-regex-headers nil
109 "Whether a header regular expression match should be used by spam-split.
110 Also see the variable `spam-spam-regex-headers' and `spam-ham-regex-headers'."
114 (defcustom spam-use-bogofilter-headers nil
115 "Whether bogofilter headers should be used by spam-split.
116 Enable this if you pre-process messages with Bogofilter BEFORE Gnus sees them."
120 (defcustom spam-use-bogofilter nil
121 "Whether bogofilter should be invoked by spam-split.
122 Enable this if you want Gnus to invoke Bogofilter on new messages."
126 (defcustom spam-use-BBDB nil
127 "Whether BBDB should be used by spam-split."
131 (defcustom spam-use-ifile nil
132 "Whether ifile should be used by spam-split."
136 (defcustom spam-use-stat nil
137 "Whether spam-stat should be used by spam-split."
141 (defcustom spam-split-group "spam"
142 "Group name where incoming spam should be put by spam-split."
146 (defcustom spam-junk-mailgroups (cons spam-split-group '("mail.junk" "poste.pourriel"))
147 "Mailgroups with spam contents.
148 All unmarked article in such group receive the spam mark on group entry."
149 :type '(repeat (string :tag "Group"))
152 (defcustom spam-blackhole-servers '("bl.spamcop.net" "relays.ordb.org"
153 "dev.null.dk" "relays.visi.com")
154 "List of blackhole servers."
155 :type '(repeat (string :tag "Server"))
158 (defcustom spam-ham-marks (list 'gnus-del-mark 'gnus-read-mark
159 'gnus-killed-mark 'gnus-kill-file-mark
160 'gnus-low-score-mark)
161 "Marks considered as being ham (positively not spam).
162 Such articles will be processed as ham (non-spam) on group exit."
164 (variable-item gnus-del-mark)
165 (variable-item gnus-read-mark)
166 (variable-item gnus-killed-mark)
167 (variable-item gnus-kill-file-mark)
168 (variable-item gnus-low-score-mark))
171 (defcustom spam-spam-marks (list 'gnus-spam-mark)
172 "Marks considered as being spam (positively spam).
173 Such articles will be transmitted to `bogofilter -s' on group exit."
175 (variable-item gnus-spam-mark)
176 (variable-item gnus-killed-mark)
177 (variable-item gnus-kill-file-mark)
178 (variable-item gnus-low-score-mark))
181 (defcustom spam-face 'gnus-splash-face
182 "Face for spam-marked articles"
186 (defcustom spam-regex-headers-spam '("^X-Spam-Flag: YES")
187 "Regular expression for positive header spam matches"
188 :type '(repeat (regexp :tag "Regular expression to match spam header"))
191 (defcustom spam-regex-headers-ham '("^X-Spam-Flag: NO")
192 "Regular expression for positive header ham matches"
193 :type '(repeat (regexp :tag "Regular expression to match ham header"))
196 (defgroup spam-ifile nil
197 "Spam ifile configuration."
200 (defcustom spam-ifile-path (executable-find "ifile")
201 "File path of the ifile executable program."
202 :type '(choice (file :tag "Location of ifile")
203 (const :tag "ifile is not installed"))
206 (defcustom spam-ifile-database-path nil
207 "File path of the ifile database."
208 :type '(choice (file :tag "Location of the ifile database")
209 (const :tag "Use the default"))
212 (defcustom spam-ifile-spam-category "spam"
213 "Name of the spam ifile category."
217 (defcustom spam-ifile-ham-category nil
218 "Name of the ham ifile category. If nil, the current group name will
220 :type '(choice (string :tag "Use a fixed category")
221 (const :tag "Use the current group name"))
224 (defcustom spam-ifile-all-categories nil
225 "Whether the ifile check will return all categories, or just spam.
226 Set this to t if you want to use the spam-split invocation of ifile as
227 your main source of newsgroup names."
231 (defgroup spam-bogofilter nil
232 "Spam bogofilter configuration."
235 (defcustom spam-bogofilter-path (executable-find "bogofilter")
236 "File path of the Bogofilter executable program."
237 :type '(choice (file :tag "Location of bogofilter")
238 (const :tag "Bogofilter is not installed"))
239 :group 'spam-bogofilter)
241 (defcustom spam-bogofilter-header "X-Bogosity"
242 "The header that Bogofilter inserts in messages."
244 :group 'spam-bogofilter)
246 (defcustom spam-bogofilter-database-directory nil
247 "Directory path of the Bogofilter databases."
248 :type '(choice (directory :tag "Location of the Bogofilter database directory")
249 (const :tag "Use the default"))
252 ;;; Key bindings for spam control.
254 (gnus-define-keys gnus-summary-mode-map
255 "St" spam-bogofilter-score
256 "Sx" gnus-summary-mark-as-spam
257 "Mst" spam-bogofilter-score
258 "Msx" gnus-summary-mark-as-spam
259 "\M-d" gnus-summary-mark-as-spam)
261 ;;; How to highlight a spam summary line.
263 ;; TODO: How do we redo this every time spam-face is customized?
265 (push '((eq mark gnus-spam-mark) . spam-face)
266 gnus-summary-highlight)
268 ;; convenience functions
269 (defun spam-group-spam-contents-p (group)
271 (or (member group spam-junk-mailgroups)
272 (memq 'gnus-group-spam-classification-spam
273 (gnus-parameter-spam-contents group)))
276 (defun spam-group-ham-contents-p (group)
278 (memq 'gnus-group-spam-classification-ham
279 (gnus-parameter-spam-contents group))
282 (defun spam-group-processor-p (group processor)
283 (if (and (stringp group)
285 (member processor (car (gnus-parameter-spam-process group)))
288 (defun spam-group-spam-processor-bogofilter-p (group)
289 (spam-group-processor-p group 'gnus-group-spam-exit-processor-bogofilter))
291 (defun spam-group-spam-processor-blacklist-p (group)
292 (spam-group-processor-p group 'gnus-group-spam-exit-processor-blacklist))
294 (defun spam-group-spam-processor-ifile-p (group)
295 (spam-group-processor-p group 'gnus-group-spam-exit-processor-ifile))
297 (defun spam-group-ham-processor-ifile-p (group)
298 (spam-group-processor-p group 'gnus-group-ham-exit-processor-ifile))
300 (defun spam-group-ham-processor-bogofilter-p (group)
301 (spam-group-processor-p group 'gnus-group-ham-exit-processor-bogofilter))
303 (defun spam-group-spam-processor-stat-p (group)
304 (spam-group-processor-p group 'gnus-group-spam-exit-processor-stat))
306 (defun spam-group-ham-processor-stat-p (group)
307 (spam-group-processor-p group 'gnus-group-ham-exit-processor-stat))
309 (defun spam-group-ham-processor-whitelist-p (group)
310 (spam-group-processor-p group 'gnus-group-ham-exit-processor-whitelist))
312 (defun spam-group-ham-processor-BBDB-p (group)
313 (spam-group-processor-p group 'gnus-group-ham-exit-processor-BBDB))
315 ;;; Summary entry and exit processing.
317 (defun spam-summary-prepare ()
318 (spam-mark-junk-as-spam-routine))
320 (add-hook 'gnus-summary-prepare-hook 'spam-summary-prepare)
322 (defun spam-summary-prepare-exit ()
323 ;; The spam processors are invoked for any group, spam or ham or neither
324 (when (and spam-bogofilter-path
325 (spam-group-spam-processor-bogofilter-p gnus-newsgroup-name))
326 (spam-bogofilter-register-spam-routine))
328 (when (and spam-ifile-path
329 (spam-group-spam-processor-ifile-p gnus-newsgroup-name))
330 (spam-ifile-register-spam-routine))
332 (when (spam-group-spam-processor-stat-p gnus-newsgroup-name)
333 (spam-stat-register-spam-routine))
335 (when (spam-group-spam-processor-blacklist-p gnus-newsgroup-name)
336 (spam-blacklist-register-routine))
338 (if spam-move-spam-nonspam-groups-only
339 (when (not (spam-group-spam-contents-p gnus-newsgroup-name))
340 (spam-mark-spam-as-expired-and-move-routine
341 (gnus-parameter-spam-process-destination gnus-newsgroup-name)))
342 (spam-mark-spam-as-expired-and-move-routine
343 (gnus-parameter-spam-process-destination gnus-newsgroup-name)))
345 ;; now we redo spam-mark-spam-as-expired-and-move-routine to only
346 ;; expire spam, in case the above did not expire them
347 (spam-mark-spam-as-expired-and-move-routine nil)
349 (when (spam-group-ham-contents-p gnus-newsgroup-name)
350 (when (spam-group-ham-processor-whitelist-p gnus-newsgroup-name)
351 (spam-whitelist-register-routine))
352 (when (spam-group-ham-processor-ifile-p gnus-newsgroup-name)
353 (spam-ifile-register-ham-routine))
354 (when (spam-group-ham-processor-bogofilter-p gnus-newsgroup-name)
355 (spam-bogofilter-register-ham-routine))
356 (when (spam-group-ham-processor-stat-p gnus-newsgroup-name)
357 (spam-stat-register-ham-routine))
358 (when (spam-group-ham-processor-BBDB-p gnus-newsgroup-name)
359 (spam-BBDB-register-routine)))
361 ;; now move all ham articles out of spam groups
362 (when (spam-group-spam-contents-p gnus-newsgroup-name)
363 (spam-ham-move-routine
364 (gnus-parameter-ham-process-destination gnus-newsgroup-name))))
366 (add-hook 'gnus-summary-prepare-exit-hook 'spam-summary-prepare-exit)
368 (defun spam-mark-junk-as-spam-routine ()
369 ;; check the global list of group names spam-junk-mailgroups and the
371 (when (spam-group-spam-contents-p gnus-newsgroup-name)
372 (let ((articles gnus-newsgroup-articles)
375 (setq article (pop articles))
376 (when (eq (gnus-summary-article-mark article) gnus-unread-mark)
377 (gnus-summary-mark-article article gnus-spam-mark))))))
379 (defun spam-mark-spam-as-expired-and-move-routine (&optional group)
380 (let ((articles gnus-newsgroup-articles)
383 (setq article (pop articles))
384 (when (eq (gnus-summary-article-mark article) gnus-spam-mark)
385 (gnus-summary-mark-article article gnus-expirable-mark)
386 (when (stringp group)
387 (let ((gnus-current-article article))
388 (gnus-summary-move-article nil group)))))))
390 (defun spam-ham-move-routine (&optional group)
391 (let ((articles gnus-newsgroup-articles)
392 article ham-mark-values mark)
393 (dolist (mark spam-ham-marks)
394 (push (symbol-value mark) ham-mark-values))
397 (setq article (pop articles))
398 (when (and (memq mark ham-mark-values)
400 (let ((gnus-current-article article))
401 (gnus-summary-move-article nil group))))))
403 (defun spam-generic-register-routine (spam-func ham-func)
404 (let ((articles gnus-newsgroup-articles)
405 article mark ham-articles spam-articles spam-mark-values
408 ;; marks are stored as symbolic values, so we have to dereference
409 ;; them for memq to work. we wouldn't have to do this if
410 ;; gnus-summary-article-mark returned a symbol.
411 (dolist (mark spam-ham-marks)
412 (push (symbol-value mark) ham-mark-values))
414 (dolist (mark spam-spam-marks)
415 (push (symbol-value mark) spam-mark-values))
418 (setq article (pop articles)
419 mark (gnus-summary-article-mark article))
420 (cond ((memq mark spam-mark-values) (push article spam-articles))
421 ((memq article gnus-newsgroup-saved))
422 ((memq mark ham-mark-values) (push article ham-articles))))
423 (when (and ham-articles ham-func)
424 (mapc ham-func ham-articles)) ; we use mapc because unlike
425 ; mapcar it discards the
427 (when (and spam-articles spam-func)
428 (mapc spam-func spam-articles)))) ; we use mapc because unlike
429 ; mapcar it discards the
433 (defalias 'spam-point-at-eol (if (fboundp 'point-at-eol)
435 'line-end-position)))
437 (defun spam-get-article-as-string (article)
438 (let ((article-buffer (spam-get-article-as-buffer article))
441 (save-window-excursion
442 (set-buffer article-buffer)
443 (setq article-string (buffer-string))))
446 (defun spam-get-article-as-buffer (article)
447 (let ((article-buffer))
448 (when (numberp article)
449 (save-window-excursion
450 (gnus-summary-goto-subject article)
451 (gnus-summary-show-article t)
452 (setq article-buffer (get-buffer gnus-article-buffer))))
455 (defun spam-get-article-as-filename (article)
456 (let ((article-filename))
457 (when (numberp article)
458 (nnml-possibly-change-directory (gnus-group-real-name gnus-newsgroup-name))
459 (setq article-filename (expand-file-name (int-to-string article) nnml-current-directory)))
460 (if (file-exists-p article-filename)
464 (defun spam-fetch-field-from-fast (article)
465 "Fetch the `from' field quickly, using the internal gnus-data-list function"
466 (if (and (numberp article)
467 (assoc article (gnus-data-list nil)))
468 (mail-header-from (gnus-data-header (assoc article (gnus-data-list nil))))
471 (defun spam-fetch-field-subject-fast (article)
472 "Fetch the `subject' field quickly, using the internal gnus-data-list function"
473 (if (and (numberp article)
474 (assoc article (gnus-data-list nil)))
475 (mail-header-subject (gnus-data-header (assoc article (gnus-data-list nil))))
479 ;;;; Spam determination.
481 (defvar spam-list-of-checks
482 '((spam-use-blacklist . spam-check-blacklist)
483 (spam-use-regex-headers . spam-check-regex-headers)
484 (spam-use-whitelist . spam-check-whitelist)
485 (spam-use-BBDB . spam-check-BBDB)
486 (spam-use-ifile . spam-check-ifile)
487 (spam-use-stat . spam-check-stat)
488 (spam-use-blackholes . spam-check-blackholes)
489 (spam-use-bogofilter-headers . spam-check-bogofilter-headers)
490 (spam-use-bogofilter . spam-check-bogofilter))
491 "The spam-list-of-checks list contains pairs associating a parameter
492 variable with a spam checking function. If the parameter variable is
493 true, then the checking function is called, and its value decides what
494 happens. Each individual check may return `nil', `t', or a mailgroup
495 name. The value `nil' means that the check does not yield a decision,
496 and so, that further checks are needed. The value `t' means that the
497 message is definitely not spam, and that further spam checks should be
498 inhibited. Otherwise, a mailgroup name is returned where the mail
499 should go, and further checks are also inhibited. The usual mailgroup
500 name is the value of `spam-split-group', meaning that the message is
504 "Split this message into the `spam' group if it is spam.
505 This function can be used as an entry in `nnmail-split-fancy', for
506 example like this: (: spam-split)
508 See the Info node `(gnus)Fancy Mail Splitting' for more details."
511 ;; load the spam-stat tables if needed
512 (when spam-use-stat (spam-stat-load))
514 (let ((list-of-checks spam-list-of-checks)
516 (while (and list-of-checks (not decision))
517 (let ((pair (pop list-of-checks)))
518 (when (symbol-value (car pair))
519 (setq decision (funcall (cdr pair))))))
526 (defun spam-check-regex-headers ()
528 (dolist (h-regex spam-regex-headers-ham)
530 (goto-char (point-min))
531 (when (re-search-forward h-regex nil t)
532 (message "Ham regex header search positive.")
534 (dolist (s-regex spam-regex-headers-spam)
536 (goto-char (point-min))
537 (when (re-search-forward s-regex nil t)
538 (message "Spam regex header search positive." (match-string 1))
540 (setq ret spam-split-group))))
546 (defun spam-check-blackholes ()
547 "Check the Received headers for blackholed relays."
548 (let ((headers (message-fetch-field "received"))
553 (goto-char (point-min))
554 (while (re-search-forward
555 "\\[\\([0-9]+.[0-9]+.[0-9]+.[0-9]+\\)\\]" nil t)
556 (message "Blackhole search found host IP %s." (match-string 1))
557 (push (mapconcat 'identity
558 (nreverse (split-string (match-string 1) "\\."))
561 (dolist (server spam-blackhole-servers)
563 (let ((query-string (concat ip "." server)))
565 (let ((query-result (query-dig query-string)))
567 (message "spam: positive blackhole check '%s'" query-result)
568 (push (list ip server query-result)
570 ;; else, if not using dig.el
571 (when (query-dns query-string)
572 (push (list ip server (query-dns query-string 'TXT))
579 ;;; original idea for spam-check-BBDB from Alexander Kotelnikov
580 ;;; <sacha@giotto.sj.ru>
582 ;; all this is done inside a condition-case to trap errors
589 (defun spam-enter-ham-BBDB (from)
590 "Enter an address into the BBDB; implies ham (non-spam) sender"
592 (let* ((parsed-address (gnus-extract-address-components from))
593 (name (or (car parsed-address) "Ham Sender"))
594 (net-address (car (cdr parsed-address))))
595 (message "Adding address %s to BBDB" from)
596 (when (and net-address
597 (not (bbdb-search-simple nil net-address)))
598 (bbdb-create-internal name nil net-address nil nil
599 "ham sender added by spam.el")))))
601 (defun spam-BBDB-register-routine ()
602 (spam-generic-register-routine
607 (spam-enter-ham-BBDB (spam-fetch-field-from-fast article)))))
609 (defun spam-check-BBDB ()
610 "Mail from people in the BBDB is never considered spam"
611 (let ((who (message-fetch-field "from")))
613 (setq who (regexp-quote (cadr
614 (gnus-extract-address-components who))))
615 (if (bbdb-search-simple nil who)
616 nil spam-split-group)))))
619 (defalias 'bbdb-search-simple 'ignore)
620 (defalias 'spam-check-BBDB 'ignore)
621 (defalias 'spam-BBDB-register-routine 'ignore)
622 (defalias 'spam-enter-ham-BBDB 'ignore)
623 (defalias 'bbdb-create-internal 'ignore)
624 (defalias 'bbdb-records 'ignore))))
629 ;;; check the ifile backend; return nil if the mail was NOT classified
632 (defun spam-get-ifile-database-parameter ()
633 "Get the command-line parameter for ifile's database from spam-ifile-database-path."
634 (if spam-ifile-database-path
635 (format "--db-file=%s" spam-ifile-database-path)
638 (defun spam-check-ifile ()
639 "Check the ifile backend for the classification of this message"
640 (let ((article-buffer-name (buffer-name))
643 (let ((temp-buffer-name (buffer-name))
644 (db-param (spam-get-ifile-database-parameter)))
646 (set-buffer article-buffer-name)
648 (call-process-region (point-min) (point-max) spam-ifile-path
649 nil temp-buffer-name nil "-q" "-c" db-param)
650 (call-process-region (point-min) (point-max) spam-ifile-path
651 nil temp-buffer-name nil "-q" "-c")))
652 (goto-char (point-min))
654 (setq category (buffer-substring (point) (spam-point-at-eol))))
655 (when (not (zerop (length category))) ; we need a category here
656 (if spam-ifile-all-categories
657 (setq return category)
658 ;; else, if spam-ifile-all-categories is not set...
659 (when (string-equal spam-ifile-spam-category category)
660 (setq return spam-split-group))))))
663 (defun spam-ifile-register-with-ifile (article-string category)
664 "Register an article, given as a string, with a category.
665 Uses `gnus-newsgroup-name' if category is nil (for ham registration)."
666 (when (stringp article-string)
667 (let ((category (or category gnus-newsgroup-name))
668 (db-param (spam-get-ifile-database-parameter)))
670 (insert-string article-string)
672 (call-process-region (point-min) (point-max) spam-ifile-path
674 "-h" "-i" category db-param)
675 (call-process-region (point-min) (point-max) spam-ifile-path
677 "-h" "-i" category))))))
679 (defun spam-ifile-register-spam-routine ()
680 (spam-generic-register-routine
682 (spam-ifile-register-with-ifile
683 (spam-get-article-as-string article) spam-ifile-spam-category))
686 (defun spam-ifile-register-ham-routine ()
687 (spam-generic-register-routine
690 (spam-ifile-register-with-ifile
691 (spam-get-article-as-string article) spam-ifile-ham-category))))
698 (let ((spam-stat-install-hooks nil))
699 (require 'spam-stat))
701 (defun spam-check-stat ()
702 "Check the spam-stat backend for the classification of this message"
703 (let ((spam-stat-split-fancy-spam-group spam-split-group) ; override
704 (spam-stat-buffer (buffer-name)) ; stat the current buffer
706 (spam-stat-split-fancy)))
708 (defun spam-stat-register-spam-routine ()
709 (spam-generic-register-routine
711 (let ((article-string (spam-get-article-as-string article)))
713 (insert-string article-string)
714 (spam-stat-buffer-is-spam))))
718 (defun spam-stat-register-ham-routine ()
719 (spam-generic-register-routine
722 (let ((article-string (spam-get-article-as-string article)))
724 (insert-string article-string)
725 (spam-stat-buffer-is-non-spam)))))
729 (defalias 'spam-stat-register-ham-routine 'ignore)
730 (defalias 'spam-stat-register-spam-routine 'ignore)
731 (defalias 'spam-stat-buffer-is-spam 'ignore)
732 (defalias 'spam-stat-buffer-is-non-spam 'ignore)
733 (defalias 'spam-stat-split-fancy 'ignore)
734 (defalias 'spam-stat-load 'ignore)
735 (defalias 'spam-stat-save 'ignore)
736 (defalias 'spam-check-stat 'ignore))))
740 ;;;; Blacklists and whitelists.
742 (defvar spam-whitelist-cache nil)
743 (defvar spam-blacklist-cache nil)
745 (defun spam-enter-whitelist (address)
746 "Enter ADDRESS into the whitelist."
747 (interactive "sAddress: ")
748 (spam-enter-list address spam-whitelist)
749 (setq spam-whitelist-cache nil))
751 (defun spam-enter-blacklist (address)
752 "Enter ADDRESS into the blacklist."
753 (interactive "sAddress: ")
754 (spam-enter-list address spam-blacklist)
755 (setq spam-blacklist-cache nil))
757 (defun spam-enter-list (address file)
758 "Enter ADDRESS into the given FILE, either the whitelist or the blacklist."
759 (unless (file-exists-p (file-name-directory file))
760 (make-directory (file-name-directory file) t))
763 (find-file-noselect file))
764 (goto-char (point-max))
767 (insert address "\n")
770 ;;; returns nil if the sender is in the whitelist, spam-split-group otherwise
771 (defun spam-check-whitelist ()
772 ;; FIXME! Should it detect when file timestamps change?
773 (unless spam-whitelist-cache
774 (setq spam-whitelist-cache (spam-parse-list spam-whitelist)))
775 (if (spam-from-listed-p spam-whitelist-cache) nil spam-split-group))
777 (defun spam-check-blacklist ()
778 ;; FIXME! Should it detect when file timestamps change?
779 (unless spam-blacklist-cache
780 (setq spam-blacklist-cache (spam-parse-list spam-blacklist)))
781 (and (spam-from-listed-p spam-blacklist-cache) spam-split-group))
783 (defun spam-parse-list (file)
784 (when (file-readable-p file)
785 (let (contents address)
787 (insert-file-contents file)
789 (setq address (buffer-substring (point) (spam-point-at-eol)))
791 (unless (zerop (length address))
792 (setq address (regexp-quote address))
793 (while (string-match "\\\\\\*" address)
794 (setq address (replace-match ".*" t t address)))
795 (push address contents))))
796 (nreverse contents))))
798 (defun spam-from-listed-p (cache)
799 (let ((from (message-fetch-field "from"))
802 (when (string-match (pop cache) from)
807 (defun spam-blacklist-register-routine ()
808 (spam-generic-register-routine
811 (let ((from (spam-fetch-field-from-fast article)))
813 (spam-enter-blacklist from))))
817 (defun spam-whitelist-register-routine ()
818 (spam-generic-register-routine
823 (let ((from (spam-fetch-field-from-fast article)))
825 (spam-enter-whitelist from))))))
830 (defun spam-check-bogofilter-headers (&optional score)
831 (let ((header (message-fetch-field spam-bogofilter-header)))
833 (string-match "^Yes" header))
835 (when (string-match "spamicity=\\([0-9.]+\\)" header)
836 (match-string 1 header))
840 ;; return something sensible if the score can't be determined
841 (defun spam-bogofilter-score ()
842 "Get the Bogofilter spamicity score"
844 (save-window-excursion
845 (gnus-summary-show-article t)
846 (set-buffer gnus-article-buffer)
847 (let ((score (spam-check-bogofilter t)))
848 (message "Spamicity score %s" score)
851 (defun spam-check-bogofilter (&optional score)
852 "Check the Bogofilter backend for the classification of this message"
853 (let ((article-buffer-name (buffer-name))
856 (let ((temp-buffer-name (buffer-name)))
858 (set-buffer article-buffer-name)
859 (if spam-bogofilter-database-directory
860 (call-process-region (point-min) (point-max)
862 nil temp-buffer-name nil "-v"
863 "-d" spam-bogofilter-database-directory)
864 (call-process-region (point-min) (point-max) spam-bogofilter-path
865 nil temp-buffer-name nil "-v")))
866 (setq return (spam-check-bogofilter-headers score))))
869 (defun spam-bogofilter-register-with-bogofilter (article-string spam)
870 "Register an article, given as a string, as spam or non-spam."
871 (when (stringp article-string)
872 (let ((switch (if spam "-s" "-n")))
874 (insert-string article-string)
875 (if spam-bogofilter-database-directory
876 (call-process-region (point-min) (point-max)
878 nil nil nil "-v" switch
879 "-d" spam-bogofilter-database-directory)
880 (call-process-region (point-min) (point-max) spam-bogofilter-path
881 nil nil nil "-v" switch))))))
883 (defun spam-bogofilter-register-spam-routine ()
884 (spam-generic-register-routine
886 (spam-bogofilter-register-with-bogofilter
887 (spam-get-article-as-string article) t))
890 (defun spam-bogofilter-register-ham-routine ()
891 (spam-generic-register-routine
894 (spam-bogofilter-register-with-bogofilter
895 (spam-get-article-as-string article) nil))))
899 ;;; spam.el ends here.