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