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.
33 (defvar spam-blackhole-servers
34 '("bl.spamcop.net" "relays.ordb.org" "dev.null.dk"
35 "relays.visi.com" "rbl.maps.vix.com")
36 "List of blackhole servers.")
38 (defvar spam-split-group "spam" "Default group name for spam-split.")
40 (defun spam-check-blackholes ()
41 "Check the Receieved headers for blackholed relays."
42 (let ((headers (message-fetch-field "received"))
47 (goto-char (point-min))
48 (while (re-search-forward
49 "\\[\\([0-9]+.[0-9]+.[0-9]+.[0-9]+\\)\\]" nil t)
50 (message "blackhole search found host IP %s" (match-string 1))
51 (push (mapconcat 'identity
52 (nreverse (split-string (match-string 1) "\\."))
55 (dolist (server spam-blackhole-servers)
57 (when (query-dns (concat ip "." server))
58 (push (list ip server (query-dns (concat ip "." server) 'TXT))
62 ;;; Black- and white-lists
64 (defvar spam-directory "~/News/spam/"
65 "When spam files are kept.")
67 (defvar spam-whitelist (expand-file-name "whitelist" spam-directory)
68 "The location of the whitelist.
69 The file format is one regular expression per line.
70 The regular expression is matched against the address.")
72 (defvar spam-blacklist (expand-file-name "blacklist" spam-directory)
73 "The location of the blacklist.
74 The file format is one regular expression per line.
75 The regular expression is matched against the address.")
77 (defvar spam-whitelist-cache nil)
78 (defvar spam-blacklist-cache nil)
80 (defun spam-enter-whitelist (address &optional blacklist)
81 "Enter ADDRESS into the whitelist.
82 Optional arg BLACKLIST, if non-nil, means to enter in the blacklist instead."
83 (interactive "sAddress: ")
84 (let ((file (if blacklist spam-blacklist spam-whitelist)))
85 (unless (file-exists-p (file-name-directory file))
86 (make-directory (file-name-directory file) t))
89 (find-file-noselect file))
90 (goto-char (point-max))
95 (spam-refresh-list-cache))))
97 (defun spam-enter-blacklist (address)
98 "Enter ADDRESS into the blacklist."
99 (interactive "sAddress: ")
100 (spam-enter-whitelist address t))
103 (defalias 'spam-point-at-eol (if (fboundp 'point-at-eol)
105 'line-end-position)))
107 (defun spam-parse-whitelist (&optional blacklist)
108 (let ((file (if blacklist spam-blacklist spam-whitelist))
110 (when (file-exists-p file)
112 (insert-file-contents file)
114 (setq address (buffer-substring (point) (spam-point-at-eol)))
116 (unless (zerop (length address))
117 (setq address (regexp-quote address))
118 (while (string-match "\\\\\\*" address)
119 (setq address (replace-match ".*" t t address)))
120 (push address contents))))
121 (nreverse contents))))
123 (defun spam-refresh-list-cache ()
124 (setq spam-whitelist-cache (spam-parse-whitelist))
125 (setq spam-blacklist-cache (spam-parse-whitelist t)))
127 (defun spam-address-whitelisted-p (address &optional blacklist)
128 (let ((cache (if blacklist spam-blacklist-cache spam-whitelist-cache))
130 (while (and (not found)
132 (when (string-match (pop cache) address)
136 (defun spam-address-blacklisted-p (address &optional blacklist)
138 (spam-address-whitelisted-p address t)
141 ;; Function for nnmail-split-fancy: returns 'spam' if an article is deemed to be spam
143 "Split this message into the `spam' group if it is spam.
144 This function can be used as an entry in `nnmail-split-fancy', for
145 example like this: (: spam-split)
147 See the Info node `(gnus)Fancy Mail Splitting' for more details."
150 ;; refresh the cache if it's necessary
151 (unless spam-whitelist-cache (spam-refresh-list-cache))
152 (unless spam-blacklist-cache (spam-refresh-list-cache))
154 (let* ((from (message-fetch-field "from"))
156 (when (spam-check-blackholes)
157 (setq group spam-split-group))
158 (unless (spam-address-whitelisted-p from) ; unless the address is whitelisted,
159 (when (spam-address-blacklisted-p from) ; check if it's blacklisted,
160 (setq group spam-split-group)) ; and if so, set the group to spam-split-group
165 ;;; spam.el ends here