1 ;;; spam.el --- Identifying spam
2 ;; Copyright (C) 2002, 2003 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
26 ;;; This module addresses a few aspects of spam control under Gnus. Page
27 ;;; breaks are used for grouping declarations and documentation relating to
28 ;;; each particular aspect.
30 ;;; The integration with Gnus is not yet complete. See various `FIXME'
31 ;;; comments, below, for supplementary explanations or discussions.
33 ;;; Several TODO items are marked as such
39 (require 'gnus-uu) ; because of key prefix issues
40 (require 'gnus) ; for the definitions of group content classification and spam processors
41 (require 'message) ;for the message-fetch-field functions
43 ;; for nnimap-split-download-body-default
44 (eval-when-compile (require 'nnimap))
46 ;; autoload executable-find
48 ;; executable-find is not autoloaded in Emacs 20
49 (autoload 'executable-find "executable"))
53 (autoload 'query-dig "dig"))
57 (autoload 'query-dns "dns"))
62 "Spam configuration.")
64 (defcustom spam-directory "~/News/spam/"
65 "Directory for spam whitelists and blacklists."
69 (defcustom spam-move-spam-nonspam-groups-only t
70 "Whether spam should be moved in non-spam groups only.
71 When nil, only ham and unclassified groups will have their spam moved
72 to the spam-process-destination. When t, spam will also be moved from
77 (defcustom spam-whitelist (expand-file-name "whitelist" spam-directory)
78 "The location of the whitelist.
79 The file format is one regular expression per line.
80 The regular expression is matched against the address."
84 (defcustom spam-blacklist (expand-file-name "blacklist" spam-directory)
85 "The location of the blacklist.
86 The file format is one regular expression per line.
87 The regular expression is matched against the address."
91 (defcustom spam-use-dig t
92 "Whether query-dig should be used instead of query-dns."
96 (defcustom spam-use-blacklist nil
97 "Whether the blacklist should be used by spam-split."
101 (defcustom spam-use-whitelist nil
102 "Whether the whitelist should be used by spam-split."
106 (defcustom spam-use-whitelist-exclusive nil
107 "Whether whitelist-exclusive should be used by spam-split.
108 Exclusive whitelisting means that all messages from senders not in the whitelist
109 are considered spam."
113 (defcustom spam-use-blackholes nil
114 "Whether blackholes should be used by spam-split."
118 (defcustom spam-use-hashcash nil
119 "Whether hashcash payments should be detected by spam-split."
123 (defcustom spam-use-regex-headers nil
124 "Whether a header regular expression match should be used by spam-split.
125 Also see the variable `spam-spam-regex-headers' and `spam-ham-regex-headers'."
129 (defcustom spam-use-bogofilter-headers nil
130 "Whether bogofilter headers should be used by spam-split.
131 Enable this if you pre-process messages with Bogofilter BEFORE Gnus sees them."
135 (defcustom spam-use-bogofilter nil
136 "Whether bogofilter should be invoked by spam-split.
137 Enable this if you want Gnus to invoke Bogofilter on new messages."
141 (defcustom spam-use-BBDB nil
142 "Whether BBDB should be used by spam-split."
146 (defcustom spam-use-BBDB-exclusive nil
147 "Whether BBDB-exclusive should be used by spam-split.
148 Exclusive BBDB means that all messages from senders not in the BBDB are
153 (defcustom spam-use-ifile nil
154 "Whether ifile should be used by spam-split."
158 (defcustom spam-use-stat nil
159 "Whether spam-stat should be used by spam-split."
163 (defcustom spam-split-group "spam"
164 "Group name where incoming spam should be put by spam-split."
168 (defcustom spam-junk-mailgroups (cons spam-split-group '("mail.junk" "poste.pourriel"))
169 "Mailgroups with spam contents.
170 All unmarked article in such group receive the spam mark on group entry."
171 :type '(repeat (string :tag "Group"))
174 (defcustom spam-blackhole-servers '("bl.spamcop.net" "relays.ordb.org"
175 "dev.null.dk" "relays.visi.com")
176 "List of blackhole servers."
177 :type '(repeat (string :tag "Server"))
180 (defcustom spam-blackhole-good-server-regex nil
181 "String matching IP addresses that should not be checked in the blackholes"
185 (defcustom spam-ham-marks (list 'gnus-del-mark 'gnus-read-mark
186 'gnus-killed-mark 'gnus-kill-file-mark
187 'gnus-low-score-mark)
188 "Marks considered as being ham (positively not spam).
189 Such articles will be processed as ham (non-spam) on group exit."
191 (variable-item gnus-del-mark)
192 (variable-item gnus-read-mark)
193 (variable-item gnus-killed-mark)
194 (variable-item gnus-kill-file-mark)
195 (variable-item gnus-low-score-mark))
198 (defcustom spam-spam-marks (list 'gnus-spam-mark)
199 "Marks considered as being spam (positively spam).
200 Such articles will be transmitted to `bogofilter -s' on group exit."
202 (variable-item gnus-spam-mark)
203 (variable-item gnus-killed-mark)
204 (variable-item gnus-kill-file-mark)
205 (variable-item gnus-low-score-mark))
208 (defcustom spam-face 'gnus-splash-face
209 "Face for spam-marked articles"
213 (defcustom spam-regex-headers-spam '("^X-Spam-Flag: YES")
214 "Regular expression for positive header spam matches"
215 :type '(repeat (regexp :tag "Regular expression to match spam header"))
218 (defcustom spam-regex-headers-ham '("^X-Spam-Flag: NO")
219 "Regular expression for positive header ham matches"
220 :type '(repeat (regexp :tag "Regular expression to match ham header"))
223 (defgroup spam-ifile nil
224 "Spam ifile configuration."
227 (defcustom spam-ifile-path (executable-find "ifile")
228 "File path of the ifile executable program."
229 :type '(choice (file :tag "Location of ifile")
230 (const :tag "ifile is not installed"))
233 (defcustom spam-ifile-database-path nil
234 "File path of the ifile database."
235 :type '(choice (file :tag "Location of the ifile database")
236 (const :tag "Use the default"))
239 (defcustom spam-ifile-spam-category "spam"
240 "Name of the spam ifile category."
244 (defcustom spam-ifile-ham-category nil
245 "Name of the ham ifile category. If nil, the current group name will
247 :type '(choice (string :tag "Use a fixed category")
248 (const :tag "Use the current group name"))
251 (defcustom spam-ifile-all-categories nil
252 "Whether the ifile check will return all categories, or just spam.
253 Set this to t if you want to use the spam-split invocation of ifile as
254 your main source of newsgroup names."
258 (defgroup spam-bogofilter nil
259 "Spam bogofilter configuration."
262 (defcustom spam-bogofilter-path (executable-find "bogofilter")
263 "File path of the Bogofilter executable program."
264 :type '(choice (file :tag "Location of bogofilter")
265 (const :tag "Bogofilter is not installed"))
266 :group 'spam-bogofilter)
268 (defcustom spam-bogofilter-header "X-Bogosity"
269 "The header that Bogofilter inserts in messages."
271 :group 'spam-bogofilter)
273 (defcustom spam-bogofilter-spam-switch "-s"
274 "The switch that Bogofilter uses to register spam messages."
276 :group 'spam-bogofilter)
278 (defcustom spam-bogofilter-ham-switch "-n"
279 "The switch that Bogofilter uses to register ham messages."
281 :group 'spam-bogofilter)
283 (defcustom spam-bogofilter-bogosity-positive-spam-header "^\\(Yes\\|Spam\\)"
284 "The regex on `spam-bogofilter-header' for positive spam identification."
286 :group 'spam-bogofilter)
288 (defcustom spam-bogofilter-database-directory nil
289 "Directory path of the Bogofilter databases."
290 :type '(choice (directory :tag "Location of the Bogofilter database directory")
291 (const :tag "Use the default"))
294 ;;; Key bindings for spam control.
296 (gnus-define-keys gnus-summary-mode-map
297 "St" spam-bogofilter-score
298 "Sx" gnus-summary-mark-as-spam
299 "Mst" spam-bogofilter-score
300 "Msx" gnus-summary-mark-as-spam
301 "\M-d" gnus-summary-mark-as-spam)
303 ;;; How to highlight a spam summary line.
305 ;; TODO: How do we redo this every time spam-face is customized?
307 (push '((eq mark gnus-spam-mark) . spam-face)
308 gnus-summary-highlight)
310 ;; convenience functions
311 (defun spam-group-spam-contents-p (group)
313 (or (member group spam-junk-mailgroups)
314 (memq 'gnus-group-spam-classification-spam
315 (gnus-parameter-spam-contents group)))
318 (defun spam-group-ham-contents-p (group)
320 (memq 'gnus-group-spam-classification-ham
321 (gnus-parameter-spam-contents group))
324 (defun spam-group-processor-p (group processor)
325 (if (and (stringp group)
327 (member processor (car (gnus-parameter-spam-process group)))
330 (defun spam-group-spam-processor-bogofilter-p (group)
331 (spam-group-processor-p group 'gnus-group-spam-exit-processor-bogofilter))
333 (defun spam-group-spam-processor-blacklist-p (group)
334 (spam-group-processor-p group 'gnus-group-spam-exit-processor-blacklist))
336 (defun spam-group-spam-processor-ifile-p (group)
337 (spam-group-processor-p group 'gnus-group-spam-exit-processor-ifile))
339 (defun spam-group-ham-processor-ifile-p (group)
340 (spam-group-processor-p group 'gnus-group-ham-exit-processor-ifile))
342 (defun spam-group-ham-processor-bogofilter-p (group)
343 (spam-group-processor-p group 'gnus-group-ham-exit-processor-bogofilter))
345 (defun spam-group-spam-processor-stat-p (group)
346 (spam-group-processor-p group 'gnus-group-spam-exit-processor-stat))
348 (defun spam-group-ham-processor-stat-p (group)
349 (spam-group-processor-p group 'gnus-group-ham-exit-processor-stat))
351 (defun spam-group-ham-processor-whitelist-p (group)
352 (spam-group-processor-p group 'gnus-group-ham-exit-processor-whitelist))
354 (defun spam-group-ham-processor-BBDB-p (group)
355 (spam-group-processor-p group 'gnus-group-ham-exit-processor-BBDB))
357 (defun spam-group-ham-processor-copy-p (group)
358 (spam-group-processor-p group 'gnus-group-ham-exit-processor-copy))
360 ;;; Summary entry and exit processing.
362 (defun spam-summary-prepare ()
363 (spam-mark-junk-as-spam-routine))
365 (add-hook 'gnus-summary-prepare-hook 'spam-summary-prepare)
367 ;; The spam processors are invoked for any group, spam or ham or neither
368 (defun spam-summary-prepare-exit ()
369 (unless gnus-group-is-exiting-without-update-p
370 (gnus-message 6 "Exiting summary buffer and applying spam rules")
371 (when (and spam-bogofilter-path
372 (spam-group-spam-processor-bogofilter-p gnus-newsgroup-name))
373 (gnus-message 5 "Registering spam with bogofilter")
374 (spam-bogofilter-register-spam-routine))
376 (when (and spam-ifile-path
377 (spam-group-spam-processor-ifile-p gnus-newsgroup-name))
378 (gnus-message 5 "Registering spam with ifile")
379 (spam-ifile-register-spam-routine))
381 (when (spam-group-spam-processor-stat-p gnus-newsgroup-name)
382 (gnus-message 5 "Registering spam with spam-stat")
383 (spam-stat-register-spam-routine))
385 (when (spam-group-spam-processor-blacklist-p gnus-newsgroup-name)
386 (gnus-message 5 "Registering spam with the blacklist")
387 (spam-blacklist-register-routine))
389 (if spam-move-spam-nonspam-groups-only
390 (when (not (spam-group-spam-contents-p gnus-newsgroup-name))
391 (spam-mark-spam-as-expired-and-move-routine
392 (gnus-parameter-spam-process-destination gnus-newsgroup-name)))
393 (gnus-message 5 "Marking spam as expired and moving it to %s" gnus-newsgroup-name)
394 (spam-mark-spam-as-expired-and-move-routine
395 (gnus-parameter-spam-process-destination gnus-newsgroup-name)))
397 ;; now we redo spam-mark-spam-as-expired-and-move-routine to only
398 ;; expire spam, in case the above did not expire them
399 (gnus-message 5 "Marking spam as expired without moving it")
400 (spam-mark-spam-as-expired-and-move-routine nil)
402 (when (spam-group-ham-contents-p gnus-newsgroup-name)
403 (when (spam-group-ham-processor-whitelist-p gnus-newsgroup-name)
404 (gnus-message 5 "Registering ham with the whitelist")
405 (spam-whitelist-register-routine))
406 (when (spam-group-ham-processor-ifile-p gnus-newsgroup-name)
407 (gnus-message 5 "Registering ham with ifile")
408 (spam-ifile-register-ham-routine))
409 (when (spam-group-ham-processor-bogofilter-p gnus-newsgroup-name)
410 (gnus-message 5 "Registering ham with Bogofilter")
411 (spam-bogofilter-register-ham-routine))
412 (when (spam-group-ham-processor-stat-p gnus-newsgroup-name)
413 (gnus-message 5 "Registering ham with spam-stat")
414 (spam-stat-register-ham-routine))
415 (when (spam-group-ham-processor-BBDB-p gnus-newsgroup-name)
416 (gnus-message 5 "Registering ham with the BBDB")
417 (spam-BBDB-register-routine)))
419 (when (spam-group-ham-processor-copy-p gnus-newsgroup-name)
420 (gnus-message 5 "Copying ham")
421 (spam-ham-move-routine
422 (gnus-parameter-ham-process-destination gnus-newsgroup-name) t))
424 ;; now move all ham articles out of spam groups
425 (when (spam-group-spam-contents-p gnus-newsgroup-name)
426 (gnus-message 5 "Moving ham messages from spam group")
427 (spam-ham-move-routine
428 (gnus-parameter-ham-process-destination gnus-newsgroup-name)))))
430 (add-hook 'gnus-summary-prepare-exit-hook 'spam-summary-prepare-exit)
432 (defun spam-mark-junk-as-spam-routine ()
433 ;; check the global list of group names spam-junk-mailgroups and the
435 (when (spam-group-spam-contents-p gnus-newsgroup-name)
436 (gnus-message 5 "Marking unread articles as spam")
437 (let ((articles gnus-newsgroup-articles)
440 (setq article (pop articles))
441 (when (eq (gnus-summary-article-mark article) gnus-unread-mark)
442 (gnus-summary-mark-article article gnus-spam-mark))))))
444 (defun spam-mark-spam-as-expired-and-move-routine (&optional group)
445 (gnus-summary-kill-process-mark)
446 (let ((articles gnus-newsgroup-articles)
448 (dolist (article articles)
449 (when (eq (gnus-summary-article-mark article) gnus-spam-mark)
450 (gnus-summary-mark-article article gnus-expirable-mark)
451 (push article tomove)))
453 ;; now do the actual move
456 (dolist (article tomove)
457 (gnus-summary-set-process-mark article))
458 (when tomove (gnus-summary-move-article nil group))))
459 (gnus-summary-yank-process-mark))
461 (defun spam-ham-move-routine (&optional group copy)
462 (gnus-summary-kill-process-mark)
463 (let ((articles gnus-newsgroup-articles)
464 article ham-mark-values mark tomove)
465 (when (stringp group) ; this routine will do nothing
466 ; without a valid group
467 (dolist (mark spam-ham-marks)
468 (push (symbol-value mark) ham-mark-values))
469 (dolist (article articles)
470 (when (memq (gnus-summary-article-mark article) ham-mark-values)
471 (push article tomove)))
473 ;; now do the actual move
475 (dolist (article tomove)
476 (gnus-summary-set-process-mark article))
478 (gnus-summary-copy-article nil group)
479 (gnus-summary-move-article nil group)))))
480 (gnus-summary-yank-process-mark))
482 (defun spam-generic-register-routine (spam-func ham-func)
483 (let ((articles gnus-newsgroup-articles)
484 article mark ham-articles spam-articles spam-mark-values
487 ;; marks are stored as symbolic values, so we have to dereference
488 ;; them for memq to work. we wouldn't have to do this if
489 ;; gnus-summary-article-mark returned a symbol.
490 (dolist (mark spam-ham-marks)
491 (push (symbol-value mark) ham-mark-values))
493 (dolist (mark spam-spam-marks)
494 (push (symbol-value mark) spam-mark-values))
497 (setq article (pop articles)
498 mark (gnus-summary-article-mark article))
499 (cond ((memq mark spam-mark-values) (push article spam-articles))
500 ((memq article gnus-newsgroup-saved))
501 ((memq mark ham-mark-values) (push article ham-articles))))
502 (when (and ham-articles ham-func)
503 (mapc ham-func ham-articles)) ; we use mapc because unlike
504 ; mapcar it discards the
506 (when (and spam-articles spam-func)
507 (mapc spam-func spam-articles)))) ; we use mapc because unlike
508 ; mapcar it discards the
512 (defalias 'spam-point-at-eol (if (fboundp 'point-at-eol)
514 'line-end-position)))
516 (defun spam-get-article-as-string (article)
517 (let ((article-buffer (spam-get-article-as-buffer article))
520 (save-window-excursion
521 (set-buffer article-buffer)
522 (setq article-string (buffer-string))))
525 (defun spam-get-article-as-buffer (article)
526 (let ((article-buffer))
527 (when (numberp article)
528 (save-window-excursion
529 (gnus-summary-goto-subject article)
530 (gnus-summary-show-article t)
531 (setq article-buffer (get-buffer gnus-article-buffer))))
535 ;; (defun spam-get-article-as-filename (article)
536 ;; (let ((article-filename))
537 ;; (when (numberp article)
538 ;; (nnml-possibly-change-directory (gnus-group-real-name gnus-newsgroup-name))
539 ;; (setq article-filename (expand-file-name (int-to-string article) nnml-current-directory)))
540 ;; (if (file-exists-p article-filename)
544 (defun spam-fetch-field-from-fast (article)
545 "Fetch the `from' field quickly, using the internal gnus-data-list function"
546 (if (and (numberp article)
547 (assoc article (gnus-data-list nil)))
548 (mail-header-from (gnus-data-header (assoc article (gnus-data-list nil))))
551 (defun spam-fetch-field-subject-fast (article)
552 "Fetch the `subject' field quickly, using the internal gnus-data-list function"
553 (if (and (numberp article)
554 (assoc article (gnus-data-list nil)))
555 (mail-header-subject (gnus-data-header (assoc article (gnus-data-list nil))))
559 ;;;; Spam determination.
561 (defvar spam-list-of-checks
562 '((spam-use-blacklist . spam-check-blacklist)
563 (spam-use-regex-headers . spam-check-regex-headers)
564 (spam-use-whitelist . spam-check-whitelist)
565 (spam-use-BBDB . spam-check-BBDB)
566 (spam-use-ifile . spam-check-ifile)
567 (spam-use-stat . spam-check-stat)
568 (spam-use-blackholes . spam-check-blackholes)
569 (spam-use-hashcash . spam-check-hashcash)
570 (spam-use-bogofilter-headers . spam-check-bogofilter-headers)
571 (spam-use-bogofilter . spam-check-bogofilter))
572 "The spam-list-of-checks list contains pairs associating a parameter
573 variable with a spam checking function. If the parameter variable is
574 true, then the checking function is called, and its value decides what
575 happens. Each individual check may return nil, t, or a mailgroup
576 name. The value nil means that the check does not yield a decision,
577 and so, that further checks are needed. The value t means that the
578 message is definitely not spam, and that further spam checks should be
579 inhibited. Otherwise, a mailgroup name is returned where the mail
580 should go, and further checks are also inhibited. The usual mailgroup
581 name is the value of `spam-split-group', meaning that the message is
584 (defvar spam-list-of-statistical-checks
585 '(spam-use-ifile spam-use-stat spam-use-bogofilter)
586 "The spam-list-of-statistical-checks list contains all the mail
587 splitters that need to have the full message body available.")
590 "Split this message into the `spam' group if it is spam.
591 This function can be used as an entry in `nnmail-split-fancy', for
592 example like this: (: spam-split)
594 See the Info node `(gnus)Fancy Mail Splitting' for more details."
597 (dolist (check spam-list-of-statistical-checks)
598 (when (symbol-value check)
600 (gnus-message 8 "spam-split: widening the buffer (%s requires it)"
603 ;; (progn (widen) (debug (buffer-string)))
604 (let ((list-of-checks spam-list-of-checks)
606 (while (and list-of-checks (not decision))
607 (let ((pair (pop list-of-checks)))
608 (when (symbol-value (car pair))
609 (gnus-message 5 "spam-split: calling the %s function" (symbol-name (cdr pair)))
610 (setq decision (funcall (cdr pair))))))
615 (defun spam-setup-widening ()
616 (dolist (check spam-list-of-statistical-checks)
617 (when (symbol-value check)
618 (setq nnimap-split-download-body-default t))))
620 (add-hook 'gnus-get-new-news-hook 'spam-setup-widening)
625 (defun spam-check-regex-headers ()
627 (dolist (h-regex spam-regex-headers-ham)
629 (goto-char (point-min))
630 (when (re-search-forward h-regex nil t)
631 (message "Ham regex header search positive.")
633 (dolist (s-regex spam-regex-headers-spam)
635 (goto-char (point-min))
636 (when (re-search-forward s-regex nil t)
637 (message "Spam regex header search positive." (match-string 1))
639 (setq ret spam-split-group))))
645 (defun spam-check-blackholes ()
646 "Check the Received headers for blackholed relays."
647 (let ((headers (message-fetch-field "received"))
652 (goto-char (point-min))
653 (gnus-message 5 "Checking headers for relay addresses")
654 (while (re-search-forward
655 "\\[\\([0-9]+.[0-9]+.[0-9]+.[0-9]+\\)\\]" nil t)
656 (gnus-message 9 "Blackhole search found host IP %s." (match-string 1))
657 (push (mapconcat 'identity
658 (nreverse (split-string (match-string 1) "\\."))
661 (dolist (server spam-blackhole-servers)
663 (unless (and spam-blackhole-good-server-regex
664 (string-match spam-blackhole-good-server-regex ip))
665 (let ((query-string (concat ip "." server)))
667 (let ((query-result (query-dig query-string)))
669 (gnus-message 5 "(DIG): positive blackhole check '%s'"
671 (push (list ip server query-result)
673 ;; else, if not using dig.el
674 (when (query-dns query-string)
675 (gnus-message 5 "positive blackhole check")
676 (push (list ip server (query-dns query-string 'TXT))
687 (defun spam-check-hashcash ()
688 "Check the headers for hashcash payments."
689 (mail-check-payment))) ;mail-check-payment returns a boolean
692 (defalias 'mail-check-payment 'ignore)
693 (defalias 'spam-check-hashcash 'ignore))))
697 ;;; original idea for spam-check-BBDB from Alexander Kotelnikov
698 ;;; <sacha@giotto.sj.ru>
700 ;; all this is done inside a condition-case to trap errors
707 (defun spam-enter-ham-BBDB (from)
708 "Enter an address into the BBDB; implies ham (non-spam) sender"
710 (let* ((parsed-address (gnus-extract-address-components from))
711 (name (or (car parsed-address) "Ham Sender"))
712 (net-address (car (cdr parsed-address))))
713 (gnus-message 5 "Adding address %s to BBDB" from)
714 (when (and net-address
715 (not (bbdb-search-simple nil net-address)))
716 (bbdb-create-internal name nil net-address nil nil
717 "ham sender added by spam.el")))))
719 (defun spam-BBDB-register-routine ()
720 (spam-generic-register-routine
725 (spam-enter-ham-BBDB (spam-fetch-field-from-fast article)))))
727 (defun spam-check-BBDB ()
728 "Mail from people in the BBDB is classified as ham or non-spam"
729 (let ((who (message-fetch-field "from")))
731 (setq who (cadr (gnus-extract-address-components who)))
732 (if (bbdb-search-simple nil who)
734 (if spam-use-BBDB-exclusive
739 (defalias 'bbdb-search-simple 'ignore)
740 (defalias 'spam-check-BBDB 'ignore)
741 (defalias 'spam-BBDB-register-routine 'ignore)
742 (defalias 'spam-enter-ham-BBDB 'ignore)
743 (defalias 'bbdb-create-internal 'ignore)
744 (defalias 'bbdb-records 'ignore))))
749 ;;; check the ifile backend; return nil if the mail was NOT classified
752 (defun spam-get-ifile-database-parameter ()
753 "Get the command-line parameter for ifile's database from spam-ifile-database-path."
754 (if spam-ifile-database-path
755 (format "--db-file=%s" spam-ifile-database-path)
758 (defun spam-check-ifile ()
759 "Check the ifile backend for the classification of this message"
760 (let ((article-buffer-name (buffer-name))
763 (let ((temp-buffer-name (buffer-name))
764 (db-param (spam-get-ifile-database-parameter)))
766 (set-buffer article-buffer-name)
768 (call-process-region (point-min) (point-max) spam-ifile-path
769 nil temp-buffer-name nil "-q" "-c" db-param)
770 (call-process-region (point-min) (point-max) spam-ifile-path
771 nil temp-buffer-name nil "-q" "-c")))
772 (goto-char (point-min))
774 (setq category (buffer-substring (point) (spam-point-at-eol))))
775 (when (not (zerop (length category))) ; we need a category here
776 (if spam-ifile-all-categories
777 (setq return category)
778 ;; else, if spam-ifile-all-categories is not set...
779 (when (string-equal spam-ifile-spam-category category)
780 (setq return spam-split-group))))))
783 (defun spam-ifile-register-with-ifile (article-string category)
784 "Register an article, given as a string, with a category.
785 Uses `gnus-newsgroup-name' if category is nil (for ham registration)."
786 (when (stringp article-string)
787 (let ((category (or category gnus-newsgroup-name))
788 (db-param (spam-get-ifile-database-parameter)))
790 (insert article-string)
792 (call-process-region (point-min) (point-max) spam-ifile-path
794 "-h" "-i" category db-param)
795 (call-process-region (point-min) (point-max) spam-ifile-path
797 "-h" "-i" category))))))
799 (defun spam-ifile-register-spam-routine ()
800 (spam-generic-register-routine
802 (spam-ifile-register-with-ifile
803 (spam-get-article-as-string article) spam-ifile-spam-category))
806 (defun spam-ifile-register-ham-routine ()
807 (spam-generic-register-routine
810 (spam-ifile-register-with-ifile
811 (spam-get-article-as-string article) spam-ifile-ham-category))))
818 (let ((spam-stat-install-hooks nil))
819 (require 'spam-stat))
821 (defun spam-check-stat ()
822 "Check the spam-stat backend for the classification of this message"
823 (let ((spam-stat-split-fancy-spam-group spam-split-group) ; override
824 (spam-stat-buffer (buffer-name)) ; stat the current buffer
826 (spam-stat-split-fancy)))
828 (defun spam-stat-register-spam-routine ()
829 (spam-generic-register-routine
831 (let ((article-string (spam-get-article-as-string article)))
833 (insert article-string)
834 (spam-stat-buffer-is-spam))))
837 (defun spam-stat-register-ham-routine ()
838 (spam-generic-register-routine
841 (let ((article-string (spam-get-article-as-string article)))
843 (insert article-string)
844 (spam-stat-buffer-is-non-spam))))))
846 (defun spam-maybe-spam-stat-load ()
847 (when spam-use-stat (spam-stat-load)))
849 (defun spam-maybe-spam-stat-save ()
850 (when spam-use-stat (spam-stat-save)))
852 ;; Add hooks for loading and saving the spam stats
853 (add-hook 'gnus-save-newsrc-hook 'spam-maybe-spam-stat-save)
854 (add-hook 'gnus-get-top-new-news-hook 'spam-maybe-spam-stat-load)
855 (add-hook 'gnus-startup-hook 'spam-maybe-spam-stat-load))
858 (defalias 'spam-stat-register-ham-routine 'ignore)
859 (defalias 'spam-stat-register-spam-routine 'ignore)
860 (defalias 'spam-stat-buffer-is-spam 'ignore)
861 (defalias 'spam-stat-buffer-is-non-spam 'ignore)
862 (defalias 'spam-stat-split-fancy 'ignore)
863 (defalias 'spam-stat-load 'ignore)
864 (defalias 'spam-stat-save 'ignore)
865 (defalias 'spam-check-stat 'ignore))))
869 ;;;; Blacklists and whitelists.
871 (defvar spam-whitelist-cache nil)
872 (defvar spam-blacklist-cache nil)
874 (defun spam-enter-whitelist (address)
875 "Enter ADDRESS into the whitelist."
876 (interactive "sAddress: ")
877 (spam-enter-list address spam-whitelist)
878 (setq spam-whitelist-cache nil))
880 (defun spam-enter-blacklist (address)
881 "Enter ADDRESS into the blacklist."
882 (interactive "sAddress: ")
883 (spam-enter-list address spam-blacklist)
884 (setq spam-blacklist-cache nil))
886 (defun spam-enter-list (address file)
887 "Enter ADDRESS into the given FILE, either the whitelist or the blacklist."
888 (unless (file-exists-p (file-name-directory file))
889 (make-directory (file-name-directory file) t))
892 (find-file-noselect file))
893 (goto-char (point-max))
896 (insert address "\n")
899 ;;; returns t if the sender is in the whitelist, nil or spam-split-group otherwise
900 (defun spam-check-whitelist ()
901 ;; FIXME! Should it detect when file timestamps change?
902 (unless spam-whitelist-cache
903 (setq spam-whitelist-cache (spam-parse-list spam-whitelist)))
904 (if (spam-from-listed-p spam-whitelist-cache)
906 (if spam-use-whitelist-exclusive
910 (defun spam-check-blacklist ()
911 ;; FIXME! Should it detect when file timestamps change?
912 (unless spam-blacklist-cache
913 (setq spam-blacklist-cache (spam-parse-list spam-blacklist)))
914 (and (spam-from-listed-p spam-blacklist-cache) spam-split-group))
916 (defun spam-parse-list (file)
917 (when (file-readable-p file)
918 (let (contents address)
920 (insert-file-contents file)
922 (setq address (buffer-substring (point) (spam-point-at-eol)))
924 (unless (zerop (length address))
925 (setq address (regexp-quote address))
926 (while (string-match "\\\\\\*" address)
927 (setq address (replace-match ".*" t t address)))
928 (push address contents))))
929 (nreverse contents))))
931 (defun spam-from-listed-p (cache)
932 (let ((from (message-fetch-field "from"))
935 (when (string-match (pop cache) from)
940 (defun spam-blacklist-register-routine ()
941 (spam-generic-register-routine
944 (let ((from (spam-fetch-field-from-fast article)))
946 (spam-enter-blacklist from))))
950 (defun spam-whitelist-register-routine ()
951 (spam-generic-register-routine
956 (let ((from (spam-fetch-field-from-fast article)))
958 (spam-enter-whitelist from))))))
963 (defun spam-check-bogofilter-headers (&optional score)
964 (let ((header (message-fetch-field spam-bogofilter-header)))
966 (string-match spam-bogofilter-bogosity-positive-spam-header
969 (when (string-match "spamicity=\\([0-9.]+\\)" header)
970 (match-string 1 header))
973 ;; return something sensible if the score can't be determined
974 (defun spam-bogofilter-score ()
975 "Get the Bogofilter spamicity score"
977 (save-window-excursion
978 (gnus-summary-show-article t)
979 (set-buffer gnus-article-buffer)
980 (let ((score (spam-check-bogofilter t)))
981 (message "Spamicity score %s" score)
984 (defun spam-check-bogofilter (&optional score)
985 "Check the Bogofilter backend for the classification of this message"
986 (let ((article-buffer-name (buffer-name))
989 (let ((temp-buffer-name (buffer-name)))
991 (set-buffer article-buffer-name)
992 (if spam-bogofilter-database-directory
993 (call-process-region (point-min) (point-max)
995 nil temp-buffer-name nil "-v"
996 "-d" spam-bogofilter-database-directory)
997 (call-process-region (point-min) (point-max) spam-bogofilter-path
998 nil temp-buffer-name nil "-v")))
999 (setq return (spam-check-bogofilter-headers score))))
1002 (defun spam-bogofilter-register-with-bogofilter (article-string spam)
1003 "Register an article, given as a string, as spam or non-spam."
1004 (when (stringp article-string)
1005 (let ((switch (if spam spam-bogofilter-spam-switch
1006 spam-bogofilter-ham-switch)))
1008 (insert article-string)
1009 (if spam-bogofilter-database-directory
1010 (call-process-region (point-min) (point-max)
1011 spam-bogofilter-path
1012 nil nil nil "-v" switch
1013 "-d" spam-bogofilter-database-directory)
1014 (call-process-region (point-min) (point-max) spam-bogofilter-path
1015 nil nil nil "-v" switch))))))
1017 (defun spam-bogofilter-register-spam-routine ()
1018 (spam-generic-register-routine
1020 (spam-bogofilter-register-with-bogofilter
1021 (spam-get-article-as-string article) t))
1024 (defun spam-bogofilter-register-ham-routine ()
1025 (spam-generic-register-routine
1028 (spam-bogofilter-register-with-bogofilter
1029 (spam-get-article-as-string article) nil))))
1033 ;;; spam.el ends here.