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
41 (require 'gnus-uu) ; because of key prefix issues
42 (require 'gnus) ; for the definitions of group content classification and spam processors
43 (require 'message) ;for the message-fetch-field functions
47 (autoload 'query-dig "dig"))
51 (autoload 'query-dns "dns"))
56 "Spam configuration.")
58 (defcustom spam-directory "~/News/spam/"
59 "Directory for spam whitelists and blacklists."
63 (defcustom spam-move-spam-nonspam-groups-only t
64 "Whether spam should be moved in non-spam groups only.
65 When nil, only ham and unclassified groups will have their spam moved
66 to the spam-process-destination. When t, spam will also be moved from
71 (defcustom spam-whitelist (expand-file-name "whitelist" spam-directory)
72 "The location of the whitelist.
73 The file format is one regular expression per line.
74 The regular expression is matched against the address."
78 (defcustom spam-blacklist (expand-file-name "blacklist" spam-directory)
79 "The location of the blacklist.
80 The file format is one regular expression per line.
81 The regular expression is matched against the address."
85 (defcustom spam-use-dig t
86 "Whether query-dig should be used instead of query-dns."
90 (defcustom spam-use-blacklist nil
91 "Whether the blacklist should be used by spam-split."
95 (defcustom spam-use-whitelist nil
96 "Whether the whitelist should be used by spam-split."
100 (defcustom spam-use-whitelist-exclusive nil
101 "Whether whitelist-exclusive should be used by spam-split.
102 Exclusive whitelisting means that all messages from senders not in the whitelist
103 are considered spam."
107 (defcustom spam-use-blackholes nil
108 "Whether blackholes should be used by spam-split."
112 (defcustom spam-use-regex-headers nil
113 "Whether a header regular expression match should be used by spam-split.
114 Also see the variable `spam-spam-regex-headers' and `spam-ham-regex-headers'."
118 (defcustom spam-use-bogofilter-headers nil
119 "Whether bogofilter headers should be used by spam-split.
120 Enable this if you pre-process messages with Bogofilter BEFORE Gnus sees them."
124 (defcustom spam-use-bogofilter nil
125 "Whether bogofilter should be invoked by spam-split.
126 Enable this if you want Gnus to invoke Bogofilter on new messages."
130 (defcustom spam-use-BBDB nil
131 "Whether BBDB should be used by spam-split."
135 (defcustom spam-use-BBDB-exclusive nil
136 "Whether BBDB-exclusive should be used by spam-split.
137 Exclusive BBDB means that all messages from senders not in the BBDB are
142 (defcustom spam-use-ifile nil
143 "Whether ifile should be used by spam-split."
147 (defcustom spam-use-stat nil
148 "Whether spam-stat should be used by spam-split."
152 (defcustom spam-split-group "spam"
153 "Group name where incoming spam should be put by spam-split."
157 (defcustom spam-junk-mailgroups (cons spam-split-group '("mail.junk" "poste.pourriel"))
158 "Mailgroups with spam contents.
159 All unmarked article in such group receive the spam mark on group entry."
160 :type '(repeat (string :tag "Group"))
163 (defcustom spam-blackhole-servers '("bl.spamcop.net" "relays.ordb.org"
164 "dev.null.dk" "relays.visi.com")
165 "List of blackhole servers."
166 :type '(repeat (string :tag "Server"))
169 (defcustom spam-blackhole-good-server-regex nil
170 "String matching IP addresses that should not be checked in the blackholes"
174 (defcustom spam-ham-marks (list 'gnus-del-mark 'gnus-read-mark
175 'gnus-killed-mark 'gnus-kill-file-mark
176 'gnus-low-score-mark)
177 "Marks considered as being ham (positively not spam).
178 Such articles will be processed as ham (non-spam) on group exit."
180 (variable-item gnus-del-mark)
181 (variable-item gnus-read-mark)
182 (variable-item gnus-killed-mark)
183 (variable-item gnus-kill-file-mark)
184 (variable-item gnus-low-score-mark))
187 (defcustom spam-spam-marks (list 'gnus-spam-mark)
188 "Marks considered as being spam (positively spam).
189 Such articles will be transmitted to `bogofilter -s' on group exit."
191 (variable-item gnus-spam-mark)
192 (variable-item gnus-killed-mark)
193 (variable-item gnus-kill-file-mark)
194 (variable-item gnus-low-score-mark))
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 (exec-installed-p "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 (exec-installed-p "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-bogosity-positive-spam-header "^\\(Yes\\|Spam\\)"
263 "The regex on `spam-bogofilter-header' for positive spam identification."
265 :group 'spam-bogofilter)
267 (defcustom spam-bogofilter-database-directory nil
268 "Directory path of the Bogofilter databases."
269 :type '(choice (directory :tag "Location of the Bogofilter database directory")
270 (const :tag "Use the default"))
273 ;;; Key bindings for spam control.
275 (gnus-define-keys gnus-summary-mode-map
276 "St" spam-bogofilter-score
277 "Sx" gnus-summary-mark-as-spam
278 "Mst" spam-bogofilter-score
279 "Msx" gnus-summary-mark-as-spam
280 "\M-d" gnus-summary-mark-as-spam)
282 ;;; How to highlight a spam summary line.
284 ;; TODO: How do we redo this every time spam-face is customized?
286 (push '((eq mark gnus-spam-mark) . spam-face)
287 gnus-summary-highlight)
289 ;; convenience functions
290 (defun spam-group-spam-contents-p (group)
292 (or (member group spam-junk-mailgroups)
293 (memq 'gnus-group-spam-classification-spam
294 (gnus-parameter-spam-contents group)))
297 (defun spam-group-ham-contents-p (group)
299 (memq 'gnus-group-spam-classification-ham
300 (gnus-parameter-spam-contents group))
303 (defun spam-group-processor-p (group processor)
304 (if (and (stringp group)
306 (member processor (car (gnus-parameter-spam-process group)))
309 (defun spam-group-spam-processor-bogofilter-p (group)
310 (spam-group-processor-p group 'gnus-group-spam-exit-processor-bogofilter))
312 (defun spam-group-spam-processor-blacklist-p (group)
313 (spam-group-processor-p group 'gnus-group-spam-exit-processor-blacklist))
315 (defun spam-group-spam-processor-ifile-p (group)
316 (spam-group-processor-p group 'gnus-group-spam-exit-processor-ifile))
318 (defun spam-group-ham-processor-ifile-p (group)
319 (spam-group-processor-p group 'gnus-group-ham-exit-processor-ifile))
321 (defun spam-group-ham-processor-bogofilter-p (group)
322 (spam-group-processor-p group 'gnus-group-ham-exit-processor-bogofilter))
324 (defun spam-group-spam-processor-stat-p (group)
325 (spam-group-processor-p group 'gnus-group-spam-exit-processor-stat))
327 (defun spam-group-ham-processor-stat-p (group)
328 (spam-group-processor-p group 'gnus-group-ham-exit-processor-stat))
330 (defun spam-group-ham-processor-whitelist-p (group)
331 (spam-group-processor-p group 'gnus-group-ham-exit-processor-whitelist))
333 (defun spam-group-ham-processor-BBDB-p (group)
334 (spam-group-processor-p group 'gnus-group-ham-exit-processor-BBDB))
336 (defun spam-group-ham-processor-copy-p (group)
337 (spam-group-processor-p group 'gnus-group-ham-exit-processor-copy))
339 ;;; Summary entry and exit processing.
341 (defun spam-summary-prepare ()
342 (spam-mark-junk-as-spam-routine))
344 (add-hook 'gnus-summary-prepare-hook 'spam-summary-prepare)
346 ;; The spam processors are invoked for any group, spam or ham or neither
347 (defun spam-summary-prepare-exit ()
348 (unless gnus-group-is-exiting-without-update-p
349 (gnus-message 6 "Exiting summary buffer and applying spam rules")
350 (when (and spam-bogofilter-path
351 (spam-group-spam-processor-bogofilter-p gnus-newsgroup-name))
352 (gnus-message 5 "Registering spam with bogofilter")
353 (spam-bogofilter-register-spam-routine))
355 (when (and spam-ifile-path
356 (spam-group-spam-processor-ifile-p gnus-newsgroup-name))
357 (gnus-message 5 "Registering spam with ifile")
358 (spam-ifile-register-spam-routine))
360 (when (spam-group-spam-processor-stat-p gnus-newsgroup-name)
361 (gnus-message 5 "Registering spam with spam-stat")
362 (spam-stat-register-spam-routine))
364 (when (spam-group-spam-processor-blacklist-p gnus-newsgroup-name)
365 (gnus-message 5 "Registering spam with the blacklist")
366 (spam-blacklist-register-routine))
368 (if spam-move-spam-nonspam-groups-only
369 (when (not (spam-group-spam-contents-p gnus-newsgroup-name))
370 (spam-mark-spam-as-expired-and-move-routine
371 (gnus-parameter-spam-process-destination gnus-newsgroup-name)))
372 (gnus-message 5 "Marking spam as expired and moving it to %s" gnus-newsgroup-name)
373 (spam-mark-spam-as-expired-and-move-routine
374 (gnus-parameter-spam-process-destination gnus-newsgroup-name)))
376 ;; now we redo spam-mark-spam-as-expired-and-move-routine to only
377 ;; expire spam, in case the above did not expire them
378 (gnus-message 5 "Marking spam as expired without moving it")
379 (spam-mark-spam-as-expired-and-move-routine nil)
381 (when (spam-group-ham-contents-p gnus-newsgroup-name)
382 (when (spam-group-ham-processor-whitelist-p gnus-newsgroup-name)
383 (gnus-message 5 "Registering ham with the whitelist")
384 (spam-whitelist-register-routine))
385 (when (spam-group-ham-processor-ifile-p gnus-newsgroup-name)
386 (gnus-message 5 "Registering ham with ifile")
387 (spam-ifile-register-ham-routine))
388 (when (spam-group-ham-processor-bogofilter-p gnus-newsgroup-name)
389 (gnus-message 5 "Registering ham with Bogofilter")
390 (spam-bogofilter-register-ham-routine))
391 (when (spam-group-ham-processor-stat-p gnus-newsgroup-name)
392 (gnus-message 5 "Registering ham with spam-stat")
393 (spam-stat-register-ham-routine))
394 (when (spam-group-ham-processor-BBDB-p gnus-newsgroup-name)
395 (gnus-message 5 "Registering ham with the BBDB")
396 (spam-BBDB-register-routine)))
398 (when (spam-group-ham-processor-copy-p gnus-newsgroup-name)
399 (gnus-message 5 "Copying ham")
400 (spam-ham-move-routine
401 (gnus-parameter-ham-process-destination gnus-newsgroup-name) t))
403 ;; now move all ham articles out of spam groups
404 (when (spam-group-spam-contents-p gnus-newsgroup-name)
405 (gnus-message 5 "Moving ham messages from spam group")
406 (spam-ham-move-routine
407 (gnus-parameter-ham-process-destination gnus-newsgroup-name)))))
409 (add-hook 'gnus-summary-prepare-exit-hook 'spam-summary-prepare-exit)
411 (defun spam-mark-junk-as-spam-routine ()
412 ;; check the global list of group names spam-junk-mailgroups and the
414 (when (spam-group-spam-contents-p gnus-newsgroup-name)
415 (gnus-message 5 "Marking unread articles as spam")
416 (let ((articles gnus-newsgroup-articles)
419 (setq article (pop articles))
420 (when (eq (gnus-summary-article-mark article) gnus-unread-mark)
421 (gnus-summary-mark-article article gnus-spam-mark))))))
423 (defun spam-mark-spam-as-expired-and-move-routine (&optional group)
424 (gnus-summary-kill-process-mark)
425 (let ((articles gnus-newsgroup-articles)
427 (dolist (article articles)
428 (when (eq (gnus-summary-article-mark article) gnus-spam-mark)
429 (gnus-summary-mark-article article gnus-expirable-mark)
430 (push article tomove)))
432 ;; now do the actual move
435 (dolist (article tomove)
436 (gnus-summary-set-process-mark article))
437 (when tomove (gnus-summary-move-article nil group))))
438 (gnus-summary-yank-process-mark))
440 (defun spam-ham-move-routine (&optional group copy)
441 (gnus-summary-kill-process-mark)
442 (let ((articles gnus-newsgroup-articles)
443 article ham-mark-values mark tomove)
444 (when (stringp group) ; this routine will do nothing
445 ; without a valid group
446 (dolist (mark spam-ham-marks)
447 (push (symbol-value mark) ham-mark-values))
448 (dolist (article articles)
449 (when (memq (gnus-summary-article-mark article) ham-mark-values)
450 (push article tomove)))
452 ;; now do the actual move
454 (dolist (article tomove)
455 (gnus-summary-set-process-mark article))
457 (gnus-summary-copy-article nil group)
458 (gnus-summary-move-article nil group)))))
459 (gnus-summary-yank-process-mark))
461 (defun spam-generic-register-routine (spam-func ham-func)
462 (let ((articles gnus-newsgroup-articles)
463 article mark ham-articles spam-articles spam-mark-values
466 ;; marks are stored as symbolic values, so we have to dereference
467 ;; them for memq to work. we wouldn't have to do this if
468 ;; gnus-summary-article-mark returned a symbol.
469 (dolist (mark spam-ham-marks)
470 (push (symbol-value mark) ham-mark-values))
472 (dolist (mark spam-spam-marks)
473 (push (symbol-value mark) spam-mark-values))
476 (setq article (pop articles)
477 mark (gnus-summary-article-mark article))
478 (cond ((memq mark spam-mark-values) (push article spam-articles))
479 ((memq article gnus-newsgroup-saved))
480 ((memq mark ham-mark-values) (push article ham-articles))))
481 (when (and ham-articles ham-func)
482 (mapc ham-func ham-articles)) ; we use mapc because unlike
483 ; mapcar it discards the
485 (when (and spam-articles spam-func)
486 (mapc spam-func spam-articles)))) ; we use mapc because unlike
487 ; mapcar it discards the
491 (defalias 'spam-point-at-eol (if (fboundp 'point-at-eol)
493 'line-end-position)))
495 (defun spam-get-article-as-string (article)
496 (let ((article-buffer (spam-get-article-as-buffer article))
499 (save-window-excursion
500 (set-buffer article-buffer)
501 (setq article-string (buffer-string))))
504 (defun spam-get-article-as-buffer (article)
505 (let ((article-buffer))
506 (when (numberp article)
507 (save-window-excursion
508 (gnus-summary-goto-subject article)
509 (gnus-summary-show-article t)
510 (setq article-buffer (get-buffer gnus-article-buffer))))
514 ;; (defun spam-get-article-as-filename (article)
515 ;; (let ((article-filename))
516 ;; (when (numberp article)
517 ;; (nnml-possibly-change-directory (gnus-group-real-name gnus-newsgroup-name))
518 ;; (setq article-filename (expand-file-name (int-to-string article) nnml-current-directory)))
519 ;; (if (file-exists-p article-filename)
523 (defun spam-fetch-field-from-fast (article)
524 "Fetch the `from' field quickly, using the internal gnus-data-list function"
525 (if (and (numberp article)
526 (assoc article (gnus-data-list nil)))
527 (mail-header-from (gnus-data-header (assoc article (gnus-data-list nil))))
530 (defun spam-fetch-field-subject-fast (article)
531 "Fetch the `subject' field quickly, using the internal gnus-data-list function"
532 (if (and (numberp article)
533 (assoc article (gnus-data-list nil)))
534 (mail-header-subject (gnus-data-header (assoc article (gnus-data-list nil))))
538 ;;;; Spam determination.
540 (defvar spam-list-of-checks
541 '((spam-use-blacklist . spam-check-blacklist)
542 (spam-use-regex-headers . spam-check-regex-headers)
543 (spam-use-whitelist . spam-check-whitelist)
544 (spam-use-BBDB . spam-check-BBDB)
545 (spam-use-ifile . spam-check-ifile)
546 (spam-use-stat . spam-check-stat)
547 (spam-use-blackholes . spam-check-blackholes)
548 (spam-use-bogofilter-headers . spam-check-bogofilter-headers)
549 (spam-use-bogofilter . spam-check-bogofilter))
550 "The spam-list-of-checks list contains pairs associating a parameter
551 variable with a spam checking function. If the parameter variable is
552 true, then the checking function is called, and its value decides what
553 happens. Each individual check may return nil, t, or a mailgroup
554 name. The value nil means that the check does not yield a decision,
555 and so, that further checks are needed. The value t means that the
556 message is definitely not spam, and that further spam checks should be
557 inhibited. Otherwise, a mailgroup name is returned where the mail
558 should go, and further checks are also inhibited. The usual mailgroup
559 name is the value of `spam-split-group', meaning that the message is
563 "Split this message into the `spam' group if it is spam.
564 This function can be used as an entry in `nnmail-split-fancy', for
565 example like this: (: spam-split)
567 See the Info node `(gnus)Fancy Mail Splitting' for more details."
570 (let ((list-of-checks spam-list-of-checks)
572 (while (and list-of-checks (not decision))
573 (let ((pair (pop list-of-checks)))
574 (when (symbol-value (car pair))
575 (gnus-message 5 "spam-split: calling the %s function" (symbol-name (cdr pair)))
576 (setq decision (funcall (cdr pair))))))
583 (defun spam-check-regex-headers ()
585 (dolist (h-regex spam-regex-headers-ham)
587 (goto-char (point-min))
588 (when (re-search-forward h-regex nil t)
589 (message "Ham regex header search positive.")
591 (dolist (s-regex spam-regex-headers-spam)
593 (goto-char (point-min))
594 (when (re-search-forward s-regex nil t)
595 (message "Spam regex header search positive." (match-string 1))
597 (setq ret spam-split-group))))
603 (defun spam-check-blackholes ()
604 "Check the Received headers for blackholed relays."
605 (let ((headers (message-fetch-field "received"))
610 (goto-char (point-min))
611 (gnus-message 5 "Checking headers for relay addresses")
612 (while (re-search-forward
613 "\\[\\([0-9]+.[0-9]+.[0-9]+.[0-9]+\\)\\]" nil t)
614 (gnus-message 9 "Blackhole search found host IP %s." (match-string 1))
615 (push (mapconcat 'identity
616 (nreverse (split-string (match-string 1) "\\."))
619 (dolist (server spam-blackhole-servers)
621 (unless (and spam-blackhole-good-server-regex
622 (string-match spam-blackhole-good-server-regex ip))
623 (let ((query-string (concat ip "." server)))
625 (let ((query-result (query-dig query-string)))
627 (gnus-message 5 "(DIG): positive blackhole check '%s'" query-result)
628 (push (list ip server query-result)
630 ;; else, if not using dig.el
631 (when (query-dns query-string)
632 (gnus-message 5 "positive blackhole check")
633 (push (list ip server (query-dns query-string 'TXT))
640 ;;; original idea for spam-check-BBDB from Alexander Kotelnikov
641 ;;; <sacha@giotto.sj.ru>
643 ;; all this is done inside a condition-case to trap errors
650 (defun spam-enter-ham-BBDB (from)
651 "Enter an address into the BBDB; implies ham (non-spam) sender"
653 (let* ((parsed-address (gnus-extract-address-components from))
654 (name (or (car parsed-address) "Ham Sender"))
655 (net-address (car (cdr parsed-address))))
656 (gnus-message 5 "Adding address %s to BBDB" from)
657 (when (and net-address
658 (not (bbdb-search-simple nil net-address)))
659 (bbdb-create-internal name nil net-address nil nil
660 "ham sender added by spam.el")))))
662 (defun spam-BBDB-register-routine ()
663 (spam-generic-register-routine
668 (spam-enter-ham-BBDB (spam-fetch-field-from-fast article)))))
670 (defun spam-check-BBDB ()
671 "Mail from people in the BBDB is classified as ham or non-spam"
672 (let ((who (message-fetch-field "from")))
674 (setq who (cadr (gnus-extract-address-components who)))
675 (if (bbdb-search-simple nil who)
677 (if spam-use-BBDB-exclusive
682 (defalias 'bbdb-search-simple 'ignore)
683 (defalias 'spam-check-BBDB 'ignore)
684 (defalias 'spam-BBDB-register-routine 'ignore)
685 (defalias 'spam-enter-ham-BBDB 'ignore)
686 (defalias 'bbdb-create-internal 'ignore)
687 (defalias 'bbdb-records 'ignore))))
692 ;;; check the ifile backend; return nil if the mail was NOT classified
695 (defun spam-get-ifile-database-parameter ()
696 "Get the command-line parameter for ifile's database from spam-ifile-database-path."
697 (if spam-ifile-database-path
698 (format "--db-file=%s" spam-ifile-database-path)
701 (defun spam-check-ifile ()
702 "Check the ifile backend for the classification of this message"
703 (let ((article-buffer-name (buffer-name))
706 (let ((temp-buffer-name (buffer-name))
707 (db-param (spam-get-ifile-database-parameter)))
709 (set-buffer article-buffer-name)
711 (call-process-region (point-min) (point-max) spam-ifile-path
712 nil temp-buffer-name nil "-q" "-c" db-param)
713 (call-process-region (point-min) (point-max) spam-ifile-path
714 nil temp-buffer-name nil "-q" "-c")))
715 (goto-char (point-min))
717 (setq category (buffer-substring (point) (spam-point-at-eol))))
718 (when (not (zerop (length category))) ; we need a category here
719 (if spam-ifile-all-categories
720 (setq return category)
721 ;; else, if spam-ifile-all-categories is not set...
722 (when (string-equal spam-ifile-spam-category category)
723 (setq return spam-split-group))))))
726 (defun spam-ifile-register-with-ifile (article-string category)
727 "Register an article, given as a string, with a category.
728 Uses `gnus-newsgroup-name' if category is nil (for ham registration)."
729 (when (stringp article-string)
730 (let ((category (or category gnus-newsgroup-name))
731 (db-param (spam-get-ifile-database-parameter)))
733 (insert article-string)
735 (call-process-region (point-min) (point-max) spam-ifile-path
737 "-h" "-i" category db-param)
738 (call-process-region (point-min) (point-max) spam-ifile-path
740 "-h" "-i" category))))))
742 (defun spam-ifile-register-spam-routine ()
743 (spam-generic-register-routine
745 (spam-ifile-register-with-ifile
746 (spam-get-article-as-string article) spam-ifile-spam-category))
749 (defun spam-ifile-register-ham-routine ()
750 (spam-generic-register-routine
753 (spam-ifile-register-with-ifile
754 (spam-get-article-as-string article) spam-ifile-ham-category))))
761 (let ((spam-stat-install-hooks nil))
762 (require 'spam-stat))
764 (defun spam-check-stat ()
765 "Check the spam-stat backend for the classification of this message"
766 (let ((spam-stat-split-fancy-spam-group spam-split-group) ; override
767 (spam-stat-buffer (buffer-name)) ; stat the current buffer
769 (spam-stat-split-fancy)))
771 (defun spam-stat-register-spam-routine ()
772 (spam-generic-register-routine
774 (let ((article-string (spam-get-article-as-string article)))
776 (insert article-string)
777 (spam-stat-buffer-is-spam))))
780 (defun spam-stat-register-ham-routine ()
781 (spam-generic-register-routine
784 (let ((article-string (spam-get-article-as-string article)))
786 (insert article-string)
787 (spam-stat-buffer-is-non-spam))))))
789 ;; Add hooks for loading and saving the spam stats
791 (add-hook 'gnus-save-newsrc-hook 'spam-stat-save)
792 (add-hook 'gnus-get-new-news-hook 'spam-stat-load)))
795 (defalias 'spam-stat-register-ham-routine 'ignore)
796 (defalias 'spam-stat-register-spam-routine 'ignore)
797 (defalias 'spam-stat-buffer-is-spam 'ignore)
798 (defalias 'spam-stat-buffer-is-non-spam 'ignore)
799 (defalias 'spam-stat-split-fancy 'ignore)
800 (defalias 'spam-stat-load 'ignore)
801 (defalias 'spam-stat-save 'ignore)
802 (defalias 'spam-check-stat 'ignore))))
806 ;;;; Blacklists and whitelists.
808 (defvar spam-whitelist-cache nil)
809 (defvar spam-blacklist-cache nil)
811 (defun spam-enter-whitelist (address)
812 "Enter ADDRESS into the whitelist."
813 (interactive "sAddress: ")
814 (spam-enter-list address spam-whitelist)
815 (setq spam-whitelist-cache nil))
817 (defun spam-enter-blacklist (address)
818 "Enter ADDRESS into the blacklist."
819 (interactive "sAddress: ")
820 (spam-enter-list address spam-blacklist)
821 (setq spam-blacklist-cache nil))
823 (defun spam-enter-list (address file)
824 "Enter ADDRESS into the given FILE, either the whitelist or the blacklist."
825 (unless (file-exists-p (file-name-directory file))
826 (make-directory (file-name-directory file) t))
829 (find-file-noselect file))
830 (goto-char (point-max))
833 (insert address "\n")
836 ;;; returns t if the sender is in the whitelist, nil or spam-split-group otherwise
837 (defun spam-check-whitelist ()
838 ;; FIXME! Should it detect when file timestamps change?
839 (unless spam-whitelist-cache
840 (setq spam-whitelist-cache (spam-parse-list spam-whitelist)))
841 (if (spam-from-listed-p spam-whitelist-cache)
843 (if spam-use-whitelist-exclusive
847 (defun spam-check-blacklist ()
848 ;; FIXME! Should it detect when file timestamps change?
849 (unless spam-blacklist-cache
850 (setq spam-blacklist-cache (spam-parse-list spam-blacklist)))
851 (and (spam-from-listed-p spam-blacklist-cache) spam-split-group))
853 (defun spam-parse-list (file)
854 (when (file-readable-p file)
855 (let (contents address)
857 (insert-file-contents file)
859 (setq address (buffer-substring (point) (spam-point-at-eol)))
861 (unless (zerop (length address))
862 (setq address (regexp-quote address))
863 (while (string-match "\\\\\\*" address)
864 (setq address (replace-match ".*" t t address)))
865 (push address contents))))
866 (nreverse contents))))
868 (defun spam-from-listed-p (cache)
869 (let ((from (message-fetch-field "from"))
872 (when (string-match (pop cache) from)
877 (defun spam-blacklist-register-routine ()
878 (spam-generic-register-routine
881 (let ((from (spam-fetch-field-from-fast article)))
883 (spam-enter-blacklist from))))
887 (defun spam-whitelist-register-routine ()
888 (spam-generic-register-routine
893 (let ((from (spam-fetch-field-from-fast article)))
895 (spam-enter-whitelist from))))))
900 (defun spam-check-bogofilter-headers (&optional score)
901 (let ((header (message-fetch-field spam-bogofilter-header)))
903 (string-match spam-bogofilter-bogosity-positive-spam-header
906 (when (string-match "spamicity=\\([0-9.]+\\)" header)
907 (match-string 1 header))
910 ;; return something sensible if the score can't be determined
911 (defun spam-bogofilter-score ()
912 "Get the Bogofilter spamicity score"
914 (save-window-excursion
915 (gnus-summary-show-article t)
916 (set-buffer gnus-article-buffer)
917 (let ((score (spam-check-bogofilter t)))
918 (message "Spamicity score %s" score)
921 (defun spam-check-bogofilter (&optional score)
922 "Check the Bogofilter backend for the classification of this message"
923 (let ((article-buffer-name (buffer-name))
926 (let ((temp-buffer-name (buffer-name)))
928 (set-buffer article-buffer-name)
929 (if spam-bogofilter-database-directory
930 (call-process-region (point-min) (point-max)
932 nil temp-buffer-name nil "-v"
933 "-d" spam-bogofilter-database-directory)
934 (call-process-region (point-min) (point-max) spam-bogofilter-path
935 nil temp-buffer-name nil "-v")))
936 (setq return (spam-check-bogofilter-headers score))))
939 (defun spam-bogofilter-register-with-bogofilter (article-string spam)
940 "Register an article, given as a string, as spam or non-spam."
941 (when (stringp article-string)
942 (let ((switch (if spam "-s" "-n")))
944 (insert article-string)
945 (if spam-bogofilter-database-directory
946 (call-process-region (point-min) (point-max)
948 nil nil nil "-v" switch
949 "-d" spam-bogofilter-database-directory)
950 (call-process-region (point-min) (point-max) spam-bogofilter-path
951 nil nil nil "-v" switch))))))
953 (defun spam-bogofilter-register-spam-routine ()
954 (spam-generic-register-routine
956 (spam-bogofilter-register-with-bogofilter
957 (spam-get-article-as-string article) t))
960 (defun spam-bogofilter-register-ham-routine ()
961 (spam-generic-register-routine
964 (spam-bogofilter-register-with-bogofilter
965 (spam-get-article-as-string article) nil))))
969 ;;; spam.el ends here.