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 ;; 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-whitelist-exclusive nil
104 "Whether whitelist-exclusive should be used by spam-split.
105 Exclusive whitelisting means that all messages from senders not in the whitelist
106 are considered spam."
110 (defcustom spam-use-blackholes nil
111 "Whether blackholes should be used by spam-split."
115 (defcustom spam-use-regex-headers nil
116 "Whether a header regular expression match should be used by spam-split.
117 Also see the variable `spam-spam-regex-headers' and `spam-ham-regex-headers'."
121 (defcustom spam-use-bogofilter-headers nil
122 "Whether bogofilter headers should be used by spam-split.
123 Enable this if you pre-process messages with Bogofilter BEFORE Gnus sees them."
127 (defcustom spam-use-bogofilter nil
128 "Whether bogofilter should be invoked by spam-split.
129 Enable this if you want Gnus to invoke Bogofilter on new messages."
133 (defcustom spam-use-BBDB nil
134 "Whether BBDB should be used by spam-split."
138 (defcustom spam-use-BBDB-exclusive nil
139 "Whether BBDB-exclusive should be used by spam-split.
140 Exclusive BBDB means that all messages from senders not in the BBDB are
145 (defcustom spam-use-ifile nil
146 "Whether ifile should be used by spam-split."
150 (defcustom spam-use-stat nil
151 "Whether spam-stat should be used by spam-split."
155 (defcustom spam-split-group "spam"
156 "Group name where incoming spam should be put by spam-split."
160 (defcustom spam-junk-mailgroups (cons spam-split-group '("mail.junk" "poste.pourriel"))
161 "Mailgroups with spam contents.
162 All unmarked article in such group receive the spam mark on group entry."
163 :type '(repeat (string :tag "Group"))
166 (defcustom spam-blackhole-servers '("bl.spamcop.net" "relays.ordb.org"
167 "dev.null.dk" "relays.visi.com")
168 "List of blackhole servers."
169 :type '(repeat (string :tag "Server"))
172 (defcustom spam-blackhole-good-server-regex nil
173 "String matching IP addresses that should not be checked in the blackholes"
177 (defcustom spam-ham-marks (list 'gnus-del-mark 'gnus-read-mark
178 'gnus-killed-mark 'gnus-kill-file-mark
179 'gnus-low-score-mark)
180 "Marks considered as being ham (positively not spam).
181 Such articles will be processed as ham (non-spam) on group exit."
183 (variable-item gnus-del-mark)
184 (variable-item gnus-read-mark)
185 (variable-item gnus-killed-mark)
186 (variable-item gnus-kill-file-mark)
187 (variable-item gnus-low-score-mark))
190 (defcustom spam-spam-marks (list 'gnus-spam-mark)
191 "Marks considered as being spam (positively spam).
192 Such articles will be transmitted to `bogofilter -s' on group exit."
194 (variable-item gnus-spam-mark)
195 (variable-item gnus-killed-mark)
196 (variable-item gnus-kill-file-mark)
197 (variable-item gnus-low-score-mark))
200 (defcustom spam-face 'gnus-splash-face
201 "Face for spam-marked articles"
205 (defcustom spam-regex-headers-spam '("^X-Spam-Flag: YES")
206 "Regular expression for positive header spam matches"
207 :type '(repeat (regexp :tag "Regular expression to match spam header"))
210 (defcustom spam-regex-headers-ham '("^X-Spam-Flag: NO")
211 "Regular expression for positive header ham matches"
212 :type '(repeat (regexp :tag "Regular expression to match ham header"))
215 (defgroup spam-ifile nil
216 "Spam ifile configuration."
219 (defcustom spam-ifile-path (executable-find "ifile")
220 "File path of the ifile executable program."
221 :type '(choice (file :tag "Location of ifile")
222 (const :tag "ifile is not installed"))
225 (defcustom spam-ifile-database-path nil
226 "File path of the ifile database."
227 :type '(choice (file :tag "Location of the ifile database")
228 (const :tag "Use the default"))
231 (defcustom spam-ifile-spam-category "spam"
232 "Name of the spam ifile category."
236 (defcustom spam-ifile-ham-category nil
237 "Name of the ham ifile category. If nil, the current group name will
239 :type '(choice (string :tag "Use a fixed category")
240 (const :tag "Use the current group name"))
243 (defcustom spam-ifile-all-categories nil
244 "Whether the ifile check will return all categories, or just spam.
245 Set this to t if you want to use the spam-split invocation of ifile as
246 your main source of newsgroup names."
250 (defgroup spam-bogofilter nil
251 "Spam bogofilter configuration."
254 (defcustom spam-bogofilter-path (executable-find "bogofilter")
255 "File path of the Bogofilter executable program."
256 :type '(choice (file :tag "Location of bogofilter")
257 (const :tag "Bogofilter is not installed"))
258 :group 'spam-bogofilter)
260 (defcustom spam-bogofilter-header "X-Bogosity"
261 "The header that Bogofilter inserts in messages."
263 :group 'spam-bogofilter)
265 (defcustom spam-bogofilter-bogosity-positive-spam-header "^\\(Yes\\|Spam\\)"
266 "The regex on `spam-bogofilter-header' for positive spam identification."
268 :group 'spam-bogofilter)
270 (defcustom spam-bogofilter-database-directory nil
271 "Directory path of the Bogofilter databases."
272 :type '(choice (directory :tag "Location of the Bogofilter database directory")
273 (const :tag "Use the default"))
276 ;;; Key bindings for spam control.
278 (gnus-define-keys gnus-summary-mode-map
279 "St" spam-bogofilter-score
280 "Sx" gnus-summary-mark-as-spam
281 "Mst" spam-bogofilter-score
282 "Msx" gnus-summary-mark-as-spam
283 "\M-d" gnus-summary-mark-as-spam)
285 ;;; How to highlight a spam summary line.
287 ;; TODO: How do we redo this every time spam-face is customized?
289 (push '((eq mark gnus-spam-mark) . spam-face)
290 gnus-summary-highlight)
292 ;; convenience functions
293 (defun spam-group-spam-contents-p (group)
295 (or (member group spam-junk-mailgroups)
296 (memq 'gnus-group-spam-classification-spam
297 (gnus-parameter-spam-contents group)))
300 (defun spam-group-ham-contents-p (group)
302 (memq 'gnus-group-spam-classification-ham
303 (gnus-parameter-spam-contents group))
306 (defun spam-group-processor-p (group processor)
307 (if (and (stringp group)
309 (member processor (car (gnus-parameter-spam-process group)))
312 (defun spam-group-spam-processor-bogofilter-p (group)
313 (spam-group-processor-p group 'gnus-group-spam-exit-processor-bogofilter))
315 (defun spam-group-spam-processor-blacklist-p (group)
316 (spam-group-processor-p group 'gnus-group-spam-exit-processor-blacklist))
318 (defun spam-group-spam-processor-ifile-p (group)
319 (spam-group-processor-p group 'gnus-group-spam-exit-processor-ifile))
321 (defun spam-group-ham-processor-ifile-p (group)
322 (spam-group-processor-p group 'gnus-group-ham-exit-processor-ifile))
324 (defun spam-group-ham-processor-bogofilter-p (group)
325 (spam-group-processor-p group 'gnus-group-ham-exit-processor-bogofilter))
327 (defun spam-group-spam-processor-stat-p (group)
328 (spam-group-processor-p group 'gnus-group-spam-exit-processor-stat))
330 (defun spam-group-ham-processor-stat-p (group)
331 (spam-group-processor-p group 'gnus-group-ham-exit-processor-stat))
333 (defun spam-group-ham-processor-whitelist-p (group)
334 (spam-group-processor-p group 'gnus-group-ham-exit-processor-whitelist))
336 (defun spam-group-ham-processor-BBDB-p (group)
337 (spam-group-processor-p group 'gnus-group-ham-exit-processor-BBDB))
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 (defun spam-summary-prepare-exit ()
347 ;; The spam processors are invoked for any group, spam or ham or neither
348 (gnus-message 6 "Exiting summary buffer and applying spam rules")
349 (when (and spam-bogofilter-path
350 (spam-group-spam-processor-bogofilter-p gnus-newsgroup-name))
351 (gnus-message 5 "Registering spam with bogofilter")
352 (spam-bogofilter-register-spam-routine))
354 (when (and spam-ifile-path
355 (spam-group-spam-processor-ifile-p gnus-newsgroup-name))
356 (gnus-message 5 "Registering spam with ifile")
357 (spam-ifile-register-spam-routine))
359 (when (spam-group-spam-processor-stat-p gnus-newsgroup-name)
360 (gnus-message 5 "Registering spam with spam-stat")
361 (spam-stat-register-spam-routine))
363 (when (spam-group-spam-processor-blacklist-p gnus-newsgroup-name)
364 (gnus-message 5 "Registering spam with the blacklist")
365 (spam-blacklist-register-routine))
367 (if spam-move-spam-nonspam-groups-only
368 (when (not (spam-group-spam-contents-p gnus-newsgroup-name))
369 (spam-mark-spam-as-expired-and-move-routine
370 (gnus-parameter-spam-process-destination gnus-newsgroup-name)))
371 (gnus-message 5 "Marking spam as expired and moving it")
372 (spam-mark-spam-as-expired-and-move-routine
373 (gnus-parameter-spam-process-destination gnus-newsgroup-name)))
375 ;; now we redo spam-mark-spam-as-expired-and-move-routine to only
376 ;; expire spam, in case the above did not expire them
377 (spam-mark-spam-as-expired-and-move-routine nil)
379 (when (spam-group-ham-contents-p gnus-newsgroup-name)
380 (when (spam-group-ham-processor-whitelist-p gnus-newsgroup-name)
381 (gnus-message 5 "Registering ham with the whitelist")
382 (spam-whitelist-register-routine))
383 (when (spam-group-ham-processor-ifile-p gnus-newsgroup-name)
384 (gnus-message 5 "Registering ham with ifile")
385 (spam-ifile-register-ham-routine))
386 (when (spam-group-ham-processor-bogofilter-p gnus-newsgroup-name)
387 (gnus-message 5 "Registering ham with Bogofilter")
388 (spam-bogofilter-register-ham-routine))
389 (when (spam-group-ham-processor-stat-p gnus-newsgroup-name)
390 (gnus-message 5 "Registering ham with spam-stat")
391 (spam-stat-register-ham-routine))
392 (when (spam-group-ham-processor-BBDB-p gnus-newsgroup-name)
393 (gnus-message 5 "Registering ham with the BBDB")
394 (spam-BBDB-register-routine)))
396 ;; now move all ham articles out of spam groups
397 (when (spam-group-spam-contents-p gnus-newsgroup-name)
398 (gnus-message 5 "Moving ham messages from spam group")
399 (spam-ham-move-routine
400 (gnus-parameter-ham-process-destination gnus-newsgroup-name))))
402 (add-hook 'gnus-summary-prepare-exit-hook 'spam-summary-prepare-exit)
404 (defun spam-mark-junk-as-spam-routine ()
405 ;; check the global list of group names spam-junk-mailgroups and the
407 (when (spam-group-spam-contents-p gnus-newsgroup-name)
408 (gnus-message 5 "Marking unread articles as spam")
409 (let ((articles gnus-newsgroup-articles)
412 (setq article (pop articles))
413 (when (eq (gnus-summary-article-mark article) gnus-unread-mark)
414 (gnus-summary-mark-article article gnus-spam-mark))))))
416 (defun spam-mark-spam-as-expired-and-move-routine (&optional group)
417 (let ((articles gnus-newsgroup-articles)
419 (dolist (article articles)
420 (gnus-summary-remove-process-mark article)
421 (when (eq (gnus-summary-article-mark article) gnus-spam-mark)
422 (gnus-summary-mark-article article gnus-expirable-mark)
423 (push article tomove)))
425 ;; now do the actual move
426 (when (stringp group)
427 (dolist (article tomove)
428 (gnus-summary-set-process-mark article))
429 (when tomove (gnus-summary-move-article nil group)))))
431 (defun spam-ham-move-routine (&optional group)
432 (let ((articles gnus-newsgroup-articles)
433 article ham-mark-values mark tomove)
434 (when (stringp group) ; this routine will do nothing
435 ; without a valid group
436 (dolist (mark spam-ham-marks)
437 (push (symbol-value mark) ham-mark-values))
438 (dolist (article articles)
439 (gnus-summary-remove-process-mark article)
440 (when (memq (gnus-summary-article-mark article) ham-mark-values)
441 (push article tomove)))
443 ;; now do the actual move
444 (dolist (article tomove)
445 (gnus-summary-set-process-mark article))
446 (when tomove (gnus-summary-move-article nil group)))))
448 (defun spam-generic-register-routine (spam-func ham-func)
449 (let ((articles gnus-newsgroup-articles)
450 article mark ham-articles spam-articles spam-mark-values
453 ;; marks are stored as symbolic values, so we have to dereference
454 ;; them for memq to work. we wouldn't have to do this if
455 ;; gnus-summary-article-mark returned a symbol.
456 (dolist (mark spam-ham-marks)
457 (push (symbol-value mark) ham-mark-values))
459 (dolist (mark spam-spam-marks)
460 (push (symbol-value mark) spam-mark-values))
463 (setq article (pop articles)
464 mark (gnus-summary-article-mark article))
465 (cond ((memq mark spam-mark-values) (push article spam-articles))
466 ((memq article gnus-newsgroup-saved))
467 ((memq mark ham-mark-values) (push article ham-articles))))
468 (when (and ham-articles ham-func)
469 (mapc ham-func ham-articles)) ; we use mapc because unlike
470 ; mapcar it discards the
472 (when (and spam-articles spam-func)
473 (mapc spam-func spam-articles)))) ; we use mapc because unlike
474 ; mapcar it discards the
478 (defalias 'spam-point-at-eol (if (fboundp 'point-at-eol)
480 'line-end-position)))
482 (defun spam-get-article-as-string (article)
483 (let ((article-buffer (spam-get-article-as-buffer article))
486 (save-window-excursion
487 (set-buffer article-buffer)
488 (setq article-string (buffer-string))))
491 (defun spam-get-article-as-buffer (article)
492 (let ((article-buffer))
493 (when (numberp article)
494 (save-window-excursion
495 (gnus-summary-goto-subject article)
496 (gnus-summary-show-article t)
497 (setq article-buffer (get-buffer gnus-article-buffer))))
500 (defun spam-get-article-as-filename (article)
501 (let ((article-filename))
502 (when (numberp article)
503 (nnml-possibly-change-directory (gnus-group-real-name gnus-newsgroup-name))
504 (setq article-filename (expand-file-name (int-to-string article) nnml-current-directory)))
505 (if (file-exists-p article-filename)
509 (defun spam-fetch-field-from-fast (article)
510 "Fetch the `from' field quickly, using the internal gnus-data-list function"
511 (if (and (numberp article)
512 (assoc article (gnus-data-list nil)))
513 (mail-header-from (gnus-data-header (assoc article (gnus-data-list nil))))
516 (defun spam-fetch-field-subject-fast (article)
517 "Fetch the `subject' field quickly, using the internal gnus-data-list function"
518 (if (and (numberp article)
519 (assoc article (gnus-data-list nil)))
520 (mail-header-subject (gnus-data-header (assoc article (gnus-data-list nil))))
524 ;;;; Spam determination.
526 (defvar spam-list-of-checks
527 '((spam-use-blacklist . spam-check-blacklist)
528 (spam-use-regex-headers . spam-check-regex-headers)
529 (spam-use-whitelist . spam-check-whitelist)
530 (spam-use-BBDB . spam-check-BBDB)
531 (spam-use-ifile . spam-check-ifile)
532 (spam-use-stat . spam-check-stat)
533 (spam-use-blackholes . spam-check-blackholes)
534 (spam-use-bogofilter-headers . spam-check-bogofilter-headers)
535 (spam-use-bogofilter . spam-check-bogofilter))
536 "The spam-list-of-checks list contains pairs associating a parameter
537 variable with a spam checking function. If the parameter variable is
538 true, then the checking function is called, and its value decides what
539 happens. Each individual check may return nil, t, or a mailgroup
540 name. The value nil means that the check does not yield a decision,
541 and so, that further checks are needed. The value t means that the
542 message is definitely not spam, and that further spam checks should be
543 inhibited. Otherwise, a mailgroup name is returned where the mail
544 should go, and further checks are also inhibited. The usual mailgroup
545 name is the value of `spam-split-group', meaning that the message is
549 "Split this message into the `spam' group if it is spam.
550 This function can be used as an entry in `nnmail-split-fancy', for
551 example like this: (: spam-split)
553 See the Info node `(gnus)Fancy Mail Splitting' for more details."
556 ;; load the spam-stat tables if needed
557 (when spam-use-stat (spam-stat-load))
559 (let ((list-of-checks spam-list-of-checks)
561 (while (and list-of-checks (not decision))
562 (let ((pair (pop list-of-checks)))
563 (when (symbol-value (car pair))
564 (gnus-message 5 "spam-split: calling the %s function" (symbol-name (cdr pair)))
565 (setq decision (funcall (cdr pair))))))
572 (defun spam-check-regex-headers ()
574 (dolist (h-regex spam-regex-headers-ham)
576 (goto-char (point-min))
577 (when (re-search-forward h-regex nil t)
578 (message "Ham regex header search positive.")
580 (dolist (s-regex spam-regex-headers-spam)
582 (goto-char (point-min))
583 (when (re-search-forward s-regex nil t)
584 (message "Spam regex header search positive." (match-string 1))
586 (setq ret spam-split-group))))
592 (defun spam-check-blackholes ()
593 "Check the Received headers for blackholed relays."
594 (let ((headers (message-fetch-field "received"))
599 (goto-char (point-min))
600 (gnus-message 5 "Checking headers for relay addresses")
601 (while (re-search-forward
602 "\\[\\([0-9]+.[0-9]+.[0-9]+.[0-9]+\\)\\]" nil t)
603 (gnus-message 9 "Blackhole search found host IP %s." (match-string 1))
604 (push (mapconcat 'identity
605 (nreverse (split-string (match-string 1) "\\."))
608 (dolist (server spam-blackhole-servers)
610 (unless (and spam-blackhole-good-server-regex
611 (string-match spam-blackhole-good-server-regex ip))
612 (let ((query-string (concat ip "." server)))
614 (let ((query-result (query-dig query-string)))
616 (gnus-message 5 "(DIG): positive blackhole check '%s'" query-result)
617 (push (list ip server query-result)
619 ;; else, if not using dig.el
620 (when (query-dns query-string)
621 (gnus-message 5 "positive blackhole check")
622 (push (list ip server (query-dns query-string 'TXT))
629 ;;; original idea for spam-check-BBDB from Alexander Kotelnikov
630 ;;; <sacha@giotto.sj.ru>
632 ;; all this is done inside a condition-case to trap errors
639 (defun spam-enter-ham-BBDB (from)
640 "Enter an address into the BBDB; implies ham (non-spam) sender"
642 (let* ((parsed-address (gnus-extract-address-components from))
643 (name (or (car parsed-address) "Ham Sender"))
644 (net-address (car (cdr parsed-address))))
645 (gnus-message 5 "Adding address %s to BBDB" from)
646 (when (and net-address
647 (not (bbdb-search-simple nil net-address)))
648 (bbdb-create-internal name nil net-address nil nil
649 "ham sender added by spam.el")))))
651 (defun spam-BBDB-register-routine ()
652 (spam-generic-register-routine
657 (spam-enter-ham-BBDB (spam-fetch-field-from-fast article)))))
659 (defun spam-check-BBDB ()
660 "Mail from people in the BBDB is classified as ham or non-spam"
661 (let ((who (message-fetch-field "from")))
663 (setq who (cadr (gnus-extract-address-components who)))
664 (if (bbdb-search-simple nil who)
666 (if spam-use-BBDB-exclusive
671 (defalias 'bbdb-search-simple 'ignore)
672 (defalias 'spam-check-BBDB 'ignore)
673 (defalias 'spam-BBDB-register-routine 'ignore)
674 (defalias 'spam-enter-ham-BBDB 'ignore)
675 (defalias 'bbdb-create-internal 'ignore)
676 (defalias 'bbdb-records 'ignore))))
681 ;;; check the ifile backend; return nil if the mail was NOT classified
684 (defun spam-get-ifile-database-parameter ()
685 "Get the command-line parameter for ifile's database from spam-ifile-database-path."
686 (if spam-ifile-database-path
687 (format "--db-file=%s" spam-ifile-database-path)
690 (defun spam-check-ifile ()
691 "Check the ifile backend for the classification of this message"
692 (let ((article-buffer-name (buffer-name))
695 (let ((temp-buffer-name (buffer-name))
696 (db-param (spam-get-ifile-database-parameter)))
698 (set-buffer article-buffer-name)
700 (call-process-region (point-min) (point-max) spam-ifile-path
701 nil temp-buffer-name nil "-q" "-c" db-param)
702 (call-process-region (point-min) (point-max) spam-ifile-path
703 nil temp-buffer-name nil "-q" "-c")))
704 (goto-char (point-min))
706 (setq category (buffer-substring (point) (spam-point-at-eol))))
707 (when (not (zerop (length category))) ; we need a category here
708 (if spam-ifile-all-categories
709 (setq return category)
710 ;; else, if spam-ifile-all-categories is not set...
711 (when (string-equal spam-ifile-spam-category category)
712 (setq return spam-split-group))))))
715 (defun spam-ifile-register-with-ifile (article-string category)
716 "Register an article, given as a string, with a category.
717 Uses `gnus-newsgroup-name' if category is nil (for ham registration)."
718 (when (stringp article-string)
719 (let ((category (or category gnus-newsgroup-name))
720 (db-param (spam-get-ifile-database-parameter)))
722 (insert-string article-string)
724 (call-process-region (point-min) (point-max) spam-ifile-path
726 "-h" "-i" category db-param)
727 (call-process-region (point-min) (point-max) spam-ifile-path
729 "-h" "-i" category))))))
731 (defun spam-ifile-register-spam-routine ()
732 (spam-generic-register-routine
734 (spam-ifile-register-with-ifile
735 (spam-get-article-as-string article) spam-ifile-spam-category))
738 (defun spam-ifile-register-ham-routine ()
739 (spam-generic-register-routine
742 (spam-ifile-register-with-ifile
743 (spam-get-article-as-string article) spam-ifile-ham-category))))
750 (let ((spam-stat-install-hooks nil))
751 (require 'spam-stat))
753 (defun spam-check-stat ()
754 "Check the spam-stat backend for the classification of this message"
755 (let ((spam-stat-split-fancy-spam-group spam-split-group) ; override
756 (spam-stat-buffer (buffer-name)) ; stat the current buffer
758 (spam-stat-split-fancy)))
760 (defun spam-stat-register-spam-routine ()
761 (spam-generic-register-routine
763 (let ((article-string (spam-get-article-as-string article)))
765 (insert-string article-string)
766 (spam-stat-buffer-is-spam))))
770 (defun spam-stat-register-ham-routine ()
771 (spam-generic-register-routine
774 (let ((article-string (spam-get-article-as-string article)))
776 (insert-string article-string)
777 (spam-stat-buffer-is-non-spam)))))
781 (defalias 'spam-stat-register-ham-routine 'ignore)
782 (defalias 'spam-stat-register-spam-routine 'ignore)
783 (defalias 'spam-stat-buffer-is-spam 'ignore)
784 (defalias 'spam-stat-buffer-is-non-spam 'ignore)
785 (defalias 'spam-stat-split-fancy 'ignore)
786 (defalias 'spam-stat-load 'ignore)
787 (defalias 'spam-stat-save 'ignore)
788 (defalias 'spam-check-stat 'ignore))))
792 ;;;; Blacklists and whitelists.
794 (defvar spam-whitelist-cache nil)
795 (defvar spam-blacklist-cache nil)
797 (defun spam-enter-whitelist (address)
798 "Enter ADDRESS into the whitelist."
799 (interactive "sAddress: ")
800 (spam-enter-list address spam-whitelist)
801 (setq spam-whitelist-cache nil))
803 (defun spam-enter-blacklist (address)
804 "Enter ADDRESS into the blacklist."
805 (interactive "sAddress: ")
806 (spam-enter-list address spam-blacklist)
807 (setq spam-blacklist-cache nil))
809 (defun spam-enter-list (address file)
810 "Enter ADDRESS into the given FILE, either the whitelist or the blacklist."
811 (unless (file-exists-p (file-name-directory file))
812 (make-directory (file-name-directory file) t))
815 (find-file-noselect file))
816 (goto-char (point-max))
819 (insert address "\n")
822 ;;; returns t if the sender is in the whitelist, nil or spam-split-group otherwise
823 (defun spam-check-whitelist ()
824 ;; FIXME! Should it detect when file timestamps change?
825 (unless spam-whitelist-cache
826 (setq spam-whitelist-cache (spam-parse-list spam-whitelist)))
827 (if (spam-from-listed-p spam-whitelist-cache)
829 (if spam-use-whitelist-exclusive
833 (defun spam-check-blacklist ()
834 ;; FIXME! Should it detect when file timestamps change?
835 (unless spam-blacklist-cache
836 (setq spam-blacklist-cache (spam-parse-list spam-blacklist)))
837 (and (spam-from-listed-p spam-blacklist-cache) spam-split-group))
839 (defun spam-parse-list (file)
840 (when (file-readable-p file)
841 (let (contents address)
843 (insert-file-contents file)
845 (setq address (buffer-substring (point) (spam-point-at-eol)))
847 (unless (zerop (length address))
848 (setq address (regexp-quote address))
849 (while (string-match "\\\\\\*" address)
850 (setq address (replace-match ".*" t t address)))
851 (push address contents))))
852 (nreverse contents))))
854 (defun spam-from-listed-p (cache)
855 (let ((from (message-fetch-field "from"))
858 (when (string-match (pop cache) from)
863 (defun spam-blacklist-register-routine ()
864 (spam-generic-register-routine
867 (let ((from (spam-fetch-field-from-fast article)))
869 (spam-enter-blacklist from))))
873 (defun spam-whitelist-register-routine ()
874 (spam-generic-register-routine
879 (let ((from (spam-fetch-field-from-fast article)))
881 (spam-enter-whitelist from))))))
886 (defun spam-check-bogofilter-headers (&optional score)
887 (let ((header (message-fetch-field spam-bogofilter-header)))
889 (string-match spam-bogofilter-bogosity-positive-spam-header
892 (when (string-match "spamicity=\\([0-9.]+\\)" header)
893 (match-string 1 header))
896 ;; return something sensible if the score can't be determined
897 (defun spam-bogofilter-score ()
898 "Get the Bogofilter spamicity score"
900 (save-window-excursion
901 (gnus-summary-show-article t)
902 (set-buffer gnus-article-buffer)
903 (let ((score (spam-check-bogofilter t)))
904 (message "Spamicity score %s" score)
907 (defun spam-check-bogofilter (&optional score)
908 "Check the Bogofilter backend for the classification of this message"
909 (let ((article-buffer-name (buffer-name))
912 (let ((temp-buffer-name (buffer-name)))
914 (set-buffer article-buffer-name)
915 (if spam-bogofilter-database-directory
916 (call-process-region (point-min) (point-max)
918 nil temp-buffer-name nil "-v"
919 "-d" spam-bogofilter-database-directory)
920 (call-process-region (point-min) (point-max) spam-bogofilter-path
921 nil temp-buffer-name nil "-v")))
922 (setq return (spam-check-bogofilter-headers score))))
925 (defun spam-bogofilter-register-with-bogofilter (article-string spam)
926 "Register an article, given as a string, as spam or non-spam."
927 (when (stringp article-string)
928 (let ((switch (if spam "-s" "-n")))
930 (insert-string article-string)
931 (if spam-bogofilter-database-directory
932 (call-process-region (point-min) (point-max)
934 nil nil nil "-v" switch
935 "-d" spam-bogofilter-database-directory)
936 (call-process-region (point-min) (point-max) spam-bogofilter-path
937 nil nil nil "-v" switch))))))
939 (defun spam-bogofilter-register-spam-routine ()
940 (spam-generic-register-routine
942 (spam-bogofilter-register-with-bogofilter
943 (spam-get-article-as-string article) t))
946 (defun spam-bogofilter-register-ham-routine ()
947 (spam-generic-register-routine
950 (spam-bogofilter-register-with-bogofilter
951 (spam-get-article-as-string article) nil))))
955 ;;; spam.el ends here.