(file-remote-p): Docstring fix.
[bpt/emacs.git] / lisp / gnus / gnus-nocem.el
CommitLineData
eec82323 1;;; gnus-nocem.el --- NoCeM pseudo-cancellation treatment
16409b0b 2
b6c2d8c6 3;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
e3fe4da0 4;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
eec82323 5
6748645f 6;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
eec82323
LMI
7;; Keywords: news
8
9;; This file is part of GNU Emacs.
10
11;; GNU Emacs is free software; you can redistribute it and/or modify
12;; it under the terms of the GNU General Public License as published by
5a9dffec 13;; the Free Software Foundation; either version 3, or (at your option)
eec82323
LMI
14;; any later version.
15
16;; GNU Emacs is distributed in the hope that it will be useful,
17;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19;; GNU General Public License for more details.
20
21;; You should have received a copy of the GNU General Public License
22;; along with GNU Emacs; see the file COPYING. If not, write to the
3a35cf56
LK
23;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24;; Boston, MA 02110-1301, USA.
eec82323
LMI
25
26;;; Commentary:
27
28;;; Code:
29
0b9d8890
RS
30(eval-when-compile (require 'cl))
31
eec82323
LMI
32(require 'gnus)
33(require 'nnmail)
34(require 'gnus-art)
35(require 'gnus-sum)
36(require 'gnus-range)
37
38(defgroup gnus-nocem nil
80a14ae0 39 "NoCeM pseudo-cancellation treatment."
eec82323
LMI
40 :group 'gnus-score)
41
42(defcustom gnus-nocem-groups
43 '("news.lists.filters" "news.admin.net-abuse.bulletins"
44 "alt.nocem.misc" "news.admin.net-abuse.announce")
6748645f 45 "*List of groups that will be searched for NoCeM messages."
eec82323
LMI
46 :group 'gnus-nocem
47 :type '(repeat (string :tag "Group")))
48
49(defcustom gnus-nocem-issuers
f49c4ef7
DL
50 '("AutoMoose-1" ; CancelMoose[tm]
51 "clewis@ferret.ocunix" ; Chris Lewis
52 "cosmo.roadkill"
53 "SpamHippo"
54 "hweede@snafu.de")
6748645f
LMI
55 "*List of NoCeM issuers to pay attention to.
56
f49c4ef7
DL
57This can also be a list of `(ISSUER CONDITION ...)' elements.
58
59See <URL:http://www.xs4all.nl/~rosalind/nocemreg/nocemreg.html> for an
60issuer registry."
eec82323 61 :group 'gnus-nocem
23f87bed 62 :link '(url-link "http://www.xs4all.nl/~rosalind/nocemreg/nocemreg.html")
6748645f 63 :type '(repeat (choice string sexp)))
eec82323
LMI
64
65(defcustom gnus-nocem-directory
66 (nnheader-concat gnus-article-save-directory "NoCeM/")
67 "*Directory where NoCeM files will be stored."
68 :group 'gnus-nocem
69 :type 'directory)
70
71(defcustom gnus-nocem-expiry-wait 15
72 "*Number of days to keep NoCeM headers in the cache."
73 :group 'gnus-nocem
74 :type 'integer)
75
84861437 76(defcustom gnus-nocem-verifyer 'pgg-verify
eec82323 77 "*Function called to verify that the NoCeM message is valid.
84861437 78One likely value is `pgg-verify'. If the function in this variable
eec82323
LMI
79isn't bound, the message will be used unconditionally."
80 :group 'gnus-nocem
84861437
MB
81 :type '(radio (function-item pgg-verify)
82 (function-item mc-verify)
eec82323
LMI
83 (function :tag "other")))
84
85(defcustom gnus-nocem-liberal-fetch nil
86 "*If t try to fetch all messages which have @@NCM in the subject.
87Otherwise don't fetch messages which have references or whose message-id
1dda7cc8 88matches a previously scanned and verified nocem message."
eec82323
LMI
89 :group 'gnus-nocem
90 :type 'boolean)
91
fd1d9d98 92(defcustom gnus-nocem-check-article-limit 500
e76ef45b
MB
93 "*If non-nil, the maximum number of articles to check in any NoCeM group."
94 :group 'gnus-nocem
f49c4ef7 95 :version "21.1"
e76ef45b
MB
96 :type '(choice (const :tag "unlimited" nil)
97 (integer 1000)))
98
f49c4ef7
DL
99(defcustom gnus-nocem-check-from t
100 "Non-nil means check for valid issuers in message bodies.
101Otherwise don't bother fetching articles unless their author matches a
102valid issuer, which is much faster if you are selective about the issuers."
103 :group 'gnus-nocem
104 :version "21.1"
105 :type 'boolean)
106
eec82323
LMI
107;;; Internal variables
108
109(defvar gnus-nocem-active nil)
110(defvar gnus-nocem-alist nil)
111(defvar gnus-nocem-touched-alist nil)
112(defvar gnus-nocem-hashtb nil)
113(defvar gnus-nocem-seen-message-ids nil)
114
115;;; Functions
116
117(defun gnus-nocem-active-file ()
118 (concat (file-name-as-directory gnus-nocem-directory) "active"))
119
120(defun gnus-nocem-cache-file ()
121 (concat (file-name-as-directory gnus-nocem-directory) "cache"))
122
a8151ef7
LMI
123;;
124;; faster lookups for group names:
125;;
126
127(defvar gnus-nocem-real-group-hashtb nil
128 "Real-name mappings of subscribed groups.")
129
130(defun gnus-fill-real-hashtb ()
6748645f 131 "Fill up a hash table with the real-name mappings from the user's active file."
01c52d31
MB
132 (if (hash-table-p gnus-nocem-real-group-hashtb)
133 (clrhash gnus-nocem-real-group-hashtb)
134 (setq gnus-nocem-real-group-hashtb (make-hash-table :test 'equal)))
a8151ef7
LMI
135 (mapcar (lambda (group)
136 (setq group (gnus-group-real-name (car group)))
01c52d31 137 (puthash group t gnus-nocem-real-group-hashtb))
a8151ef7
LMI
138 gnus-newsrc-alist))
139
ea8ae765 140;;;###autoload
eec82323
LMI
141(defun gnus-nocem-scan-groups ()
142 "Scan all NoCeM groups for new NoCeM messages."
143 (interactive)
144 (let ((groups gnus-nocem-groups)
145 (gnus-inhibit-demon t)
16409b0b 146 group active gactive articles check-headers)
eec82323
LMI
147 (gnus-make-directory gnus-nocem-directory)
148 ;; Load any previous NoCeM headers.
149 (gnus-nocem-load-cache)
a8151ef7
LMI
150 ;; Get the group name mappings:
151 (gnus-fill-real-hashtb)
eec82323
LMI
152 ;; Read the active file if it hasn't been read yet.
153 (and (file-exists-p (gnus-nocem-active-file))
154 (not gnus-nocem-active)
155 (ignore-errors
156 (load (gnus-nocem-active-file) t t t)))
157 ;; Go through all groups and see whether new articles have
158 ;; arrived.
159 (while (setq group (pop groups))
160 (if (not (setq gactive (gnus-activate-group group)))
161 () ; This group doesn't exist.
162 (setq active (nth 1 (assoc group gnus-nocem-active)))
163 (when (and (not (< (cdr gactive) (car gactive))) ; Empty group.
164 (or (not active)
165 (< (cdr active) (cdr gactive))))
166 ;; Ok, there are new articles in this group, se we fetch the
167 ;; headers.
168 (save-excursion
169 (let ((dependencies (make-vector 10 nil))
170 headers header)
16409b0b 171 (with-temp-buffer
eec82323
LMI
172 (setq headers
173 (if (eq 'nov
174 (gnus-retrieve-headers
175 (setq articles
176 (gnus-uncompress-range
177 (cons
178 (if active (1+ (cdr active))
179 (car gactive))
180 (cdr gactive))))
181 group))
182 (gnus-get-newsgroup-headers-xover
183 articles nil dependencies)
184 (gnus-get-newsgroup-headers dependencies)))
185 (while (setq header (pop headers))
186 ;; We take a closer look on all articles that have
187 ;; "@@NCM" in the subject. Unless we already read
188 ;; this cross posted message. Nocem messages
189 ;; are not allowed to have references, so we can
190 ;; ignore scanning followups.
191 (and (string-match "@@NCM" (mail-header-subject header))
f49c4ef7
DL
192 (and gnus-nocem-check-from
193 (let ((case-fold-search t))
194 (catch 'ok
01c52d31 195 (mapc
f49c4ef7
DL
196 (lambda (author)
197 (if (consp author)
198 (setq author (car author)))
199 (if (string-match
200 author (mail-header-from header))
201 (throw 'ok t)))
202 gnus-nocem-issuers)
203 nil)))
eec82323
LMI
204 (or gnus-nocem-liberal-fetch
205 (and (or (string= "" (mail-header-references
206 header))
207 (null (mail-header-references header)))
208 (not (member (mail-header-message-id header)
209 gnus-nocem-seen-message-ids))))
16409b0b 210 (push header check-headers)))
ea8ae765
MB
211 (setq check-headers (last (nreverse check-headers)
212 gnus-nocem-check-article-limit))
213 (let ((i 0)
214 (len (length check-headers)))
16409b0b
GM
215 (dolist (h check-headers)
216 (gnus-message
217 7 "Checking article %d in %s for NoCeM (%d of %d)..."
218 (mail-header-number h) group (incf i) len)
219 (gnus-nocem-check-article group h)))))))
eec82323
LMI
220 (setq gnus-nocem-active
221 (cons (list group gactive)
222 (delq (assoc group gnus-nocem-active)
223 gnus-nocem-active)))))
224 ;; Save the results, if any.
225 (gnus-nocem-save-cache)
226 (gnus-nocem-save-active)))
227
228(defun gnus-nocem-check-article (group header)
229 "Check whether the current article is an NCM article and that we want it."
230 ;; Get the article.
eec82323 231 (let ((date (mail-header-date header))
f49c4ef7 232 (gnus-newsgroup-name group)
6748645f 233 issuer b e type)
eec82323 234 (when (or (not date)
16409b0b
GM
235 (time-less-p
236 (time-since (date-to-time date))
237 (days-to-time gnus-nocem-expiry-wait)))
eec82323
LMI
238 (gnus-request-article-this-buffer (mail-header-number header) group)
239 (goto-char (point-min))
ea8ae765 240 (when (re-search-forward
01c52d31 241 "-----BEGIN PGP\\(?: SIGNED\\)? MESSAGE-----"
ea8ae765 242 nil t)
eec82323 243 (delete-region (point-min) (match-beginning 0)))
ea8ae765 244 (when (re-search-forward
01c52d31 245 "-----END PGP \\(?:MESSAGE\\|SIGNATURE\\)-----\n?"
ea8ae765 246 nil t)
eec82323
LMI
247 (delete-region (match-end 0) (point-max)))
248 (goto-char (point-min))
249 ;; The article has to have proper NoCeM headers.
250 (when (and (setq b (search-forward "\n@@BEGIN NCM HEADERS\n" nil t))
251 (setq e (search-forward "\n@@BEGIN NCM BODY\n" nil t)))
252 ;; We get the name of the issuer.
253 (narrow-to-region b e)
6748645f 254 (setq issuer (mail-fetch-field "issuer")
84861437 255 type (mail-fetch-field "type"))
eec82323 256 (widen)
6748645f
LMI
257 (if (not (gnus-nocem-message-wanted-p issuer type))
258 (message "invalid NoCeM issuer: %s" issuer)
259 (and (gnus-nocem-verify-issuer issuer) ; She is who she says she is.
260 (gnus-nocem-enter-article) ; We gobble the message.
261 (push (mail-header-message-id header) ; But don't come back for
262 gnus-nocem-seen-message-ids))))))) ; second helpings.
263
264(defun gnus-nocem-message-wanted-p (issuer type)
265 (let ((issuers gnus-nocem-issuers)
266 wanted conditions condition)
267 (cond
268 ;; Do the quick check first.
269 ((member issuer issuers)
270 t)
271 ((setq conditions (cdr (assoc issuer issuers)))
272 ;; Check whether we want this type.
273 (while (setq condition (pop conditions))
274 (cond
275 ((stringp condition)
84861437
MB
276 (when (string-match condition type)
277 (setq wanted t)))
6748645f
LMI
278 ((and (consp condition)
279 (eq (car condition) 'not)
280 (stringp (cadr condition)))
84861437
MB
281 (when (string-match (cadr condition) type)
282 (setq wanted nil)))
6748645f
LMI
283 (t
284 (error "Invalid NoCeM condition: %S" condition))))
285 wanted))))
eec82323
LMI
286
287(defun gnus-nocem-verify-issuer (person)
288 "Verify using PGP that the canceler is who she says she is."
84861437 289 (if (functionp gnus-nocem-verifyer)
a8151ef7
LMI
290 (ignore-errors
291 (funcall gnus-nocem-verifyer))
eec82323
LMI
292 ;; If we don't have Mailcrypt, then we use the message anyway.
293 t))
294
295(defun gnus-nocem-enter-article ()
296 "Enter the current article into the NoCeM cache."
297 (goto-char (point-min))
298 (let ((b (search-forward "\n@@BEGIN NCM BODY\n" nil t))
299 (e (search-forward "\n@@END NCM BODY\n" nil t))
300 (buf (current-buffer))
301 ncm id group)
302 (when (and b e)
303 (narrow-to-region b (1+ (match-beginning 0)))
304 (goto-char (point-min))
305 (while (search-forward "\t" nil t)
306 (cond
307 ((not (ignore-errors
01c52d31
MB
308 (setq group (gnus-group-real-name (symbol-name (read buf))))
309 (gethash group gnus-nocem-real-group-hashtb)))
eec82323
LMI
310 ;; An error.
311 )
eec82323 312 (t
01c52d31
MB
313 ;; Valid group.
314 (beginning-of-line)
315 (while (eq (char-after) ?\t)
316 (forward-line -1))
317 (setq id (buffer-substring (point) (1- (search-forward "\t"))))
318 (unless (if (hash-table-p gnus-nocem-hashtb)
319 (gethash id gnus-nocem-hashtb)
320 (setq gnus-nocem-hashtb (make-hash-table :test 'equal))
321 nil)
322 ;; only store if not already present
323 (puthash id t gnus-nocem-hashtb)
324 (push id ncm))
325 (forward-line 1)
326 (while (eq (char-after) ?\t)
327 (forward-line 1)))))
eec82323
LMI
328 (when ncm
329 (setq gnus-nocem-touched-alist t)
330 (push (cons (let ((time (current-time))) (setcdr (cdr time) nil) time)
331 ncm)
332 gnus-nocem-alist))
333 t)))
334
ea8ae765 335;;;###autoload
eec82323
LMI
336(defun gnus-nocem-load-cache ()
337 "Load the NoCeM cache."
338 (interactive)
339 (unless gnus-nocem-alist
340 ;; The buffer doesn't exist, so we create it and load the NoCeM
341 ;; cache.
342 (when (file-exists-p (gnus-nocem-cache-file))
343 (load (gnus-nocem-cache-file) t t t)
344 (gnus-nocem-alist-to-hashtb))))
345
346(defun gnus-nocem-save-cache ()
347 "Save the NoCeM cache."
348 (when (and gnus-nocem-alist
349 gnus-nocem-touched-alist)
16409b0b 350 (with-temp-file (gnus-nocem-cache-file)
eec82323
LMI
351 (gnus-prin1 `(setq gnus-nocem-alist ',gnus-nocem-alist)))
352 (setq gnus-nocem-touched-alist nil)))
353
354(defun gnus-nocem-save-active ()
355 "Save the NoCeM active file."
16409b0b 356 (with-temp-file (gnus-nocem-active-file)
eec82323
LMI
357 (gnus-prin1 `(setq gnus-nocem-active ',gnus-nocem-active))))
358
359(defun gnus-nocem-alist-to-hashtb ()
360 "Create a hashtable from the Message-IDs we have."
361 (let* ((alist gnus-nocem-alist)
362 (pprev (cons nil alist))
363 (prev pprev)
16409b0b 364 (expiry (days-to-time gnus-nocem-expiry-wait))
eec82323 365 entry)
01c52d31
MB
366 (if (hash-table-p gnus-nocem-hashtb)
367 (clrhash gnus-nocem-hashtb)
368 (setq gnus-nocem-hashtb (make-hash-table :test 'equal)))
eec82323 369 (while (setq entry (car alist))
16409b0b 370 (if (not (time-less-p (time-since (car entry)) expiry))
eec82323
LMI
371 ;; This entry has expired, so we remove it.
372 (setcdr prev (cdr alist))
373 (setq prev alist)
374 ;; This is ok, so we enter it into the hashtable.
375 (setq entry (cdr entry))
376 (while entry
01c52d31 377 (puthash (car entry) t gnus-nocem-hashtb)
eec82323
LMI
378 (setq entry (cdr entry))))
379 (setq alist (cdr alist)))))
380
381(gnus-add-shutdown 'gnus-nocem-close 'gnus)
382
383(defun gnus-nocem-close ()
384 "Clear internal NoCeM variables."
385 (setq gnus-nocem-alist nil
386 gnus-nocem-hashtb nil
387 gnus-nocem-active nil
388 gnus-nocem-touched-alist nil
a8151ef7
LMI
389 gnus-nocem-seen-message-ids nil
390 gnus-nocem-real-group-hashtb nil))
eec82323
LMI
391
392(defun gnus-nocem-unwanted-article-p (id)
393 "Say whether article ID in the current group is wanted."
6748645f 394 (and gnus-nocem-hashtb
01c52d31 395 (gethash id gnus-nocem-hashtb)))
eec82323
LMI
396
397(provide 'gnus-nocem)
398
ab5796a9 399;;; arch-tag: 0e0c74ea-2f8e-4f3e-8fff-09f767c1adef
eec82323 400;;; gnus-nocem.el ends here