* gnus.el (gnus-other-frame-function): New user option.
[gnus] / lisp / spam.el
1 ;;; spam.el --- Identifying spam
2 ;; Copyright (C) 2002 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; Keywords: network
6
7 ;; This file is part of GNU Emacs.
8
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)
12 ;; any later version.
13
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.
18
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.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 (require 'dns)
29 (require 'message)
30
31 ;;; Blackholes
32
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.")
37
38 (defvar spam-split-group "spam" "Default group name for spam-split.")
39
40 (defun spam-check-blackholes ()
41   "Check the Receieved headers for blackholed relays."
42   (let ((headers (message-fetch-field "received"))
43         ips matches)
44     (when headers
45       (with-temp-buffer
46         (insert headers)
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) "\\."))
53                            ".")
54                 ips)))
55       (dolist (server spam-blackhole-servers)
56         (dolist (ip ips)
57           (when (query-dns (concat ip "." server))
58             (push (list ip server (query-dns (concat ip "." server) 'TXT))
59                   matches))))
60       matches)))
61
62 ;;; Black- and white-lists
63
64 (defvar spam-directory "~/News/spam/"
65   "When spam files are kept.")
66
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.")
71
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.")
76
77 (defvar spam-whitelist-cache nil)
78 (defvar spam-blacklist-cache nil)
79
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))
87     (save-excursion
88       (set-buffer
89        (find-file-noselect file))
90       (goto-char (point-max))
91       (unless (bobp)
92         (insert "\n"))
93       (insert address "\n")
94       (save-buffer)
95       (spam-refresh-list-cache))))
96
97 (defun spam-enter-blacklist (address)
98   "Enter ADDRESS into the blacklist."
99   (interactive "sAddress: ")
100   (spam-enter-whitelist address t))
101
102 (eval-and-compile
103   (defalias 'spam-point-at-eol (if (fboundp 'point-at-eol)
104                                    'point-at-eol
105                                  'line-end-position)))
106
107 (defun spam-parse-whitelist (&optional blacklist)
108   (let ((file (if blacklist spam-blacklist spam-whitelist))
109         contents address)
110     (when (file-exists-p file)
111       (with-temp-buffer
112         (insert-file-contents file)
113         (while (not (eobp))
114           (setq address (buffer-substring (point) (spam-point-at-eol)))
115           (forward-line 1)
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))))
122
123 (defun spam-refresh-list-cache ()
124   (setq spam-whitelist-cache (spam-parse-whitelist))
125   (setq spam-blacklist-cache (spam-parse-whitelist t)))
126
127 (defun spam-address-whitelisted-p (address &optional blacklist)
128   (let ((cache (if blacklist spam-blacklist-cache spam-whitelist-cache))
129         found)
130     (while (and (not found)
131                 cache)
132       (when (string-match (pop cache) address)
133         (setq found t)))
134     found))
135
136 (defun spam-address-blacklisted-p (address &optional blacklist)
137   (if address
138       (spam-address-whitelisted-p address t)
139     nil))
140
141 ;; Function for nnmail-split-fancy: returns 'spam' if an article is deemed to be spam
142 (defun spam-split ()
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)
146
147 See the Info node `(gnus)Fancy Mail Splitting' for more details."
148   (interactive)
149
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))
153
154   (let* ((from (message-fetch-field "from"))
155          (group nil))
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
161       group)))
162
163 (provide 'spam)
164
165 ;;; spam.el ends here