(List Motion): Note optional args.
[emacs.git] / lisp / gnus / gnus-nocem.el
blob1020c72988002a5b58d27b0835c67d298ef0947d
1 ;;; gnus-nocem.el --- NoCeM pseudo-cancellation treatment
2 ;; Copyright (C) 1995,96,97,98 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; Keywords: news
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)
12 ;; any later version.
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.
24 ;;; Commentary:
26 ;;; Code:
28 (eval-when-compile (require 'cl))
30 (eval-when-compile (require 'cl))
32 (require 'gnus)
33 (require 'nnmail)
34 (require 'gnus-art)
35 (require 'gnus-sum)
36 (require 'gnus-range)
38 (defgroup gnus-nocem nil
39 "NoCeM pseudo-cancellation treatment"
40 :group 'gnus-score)
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")))
49 (defcustom gnus-nocem-issuers
50 '("AutoMoose-1" "Automoose-1" ; CancelMoose[tm]
51 "rbraver@ohww.norman.ok.us" ; Robert Braver
52 "clewis@ferret.ocunix.on.ca" ; Chris Lewis
53 "jem@xpat.com" ; Despammer from Korea
54 "snowhare@xmission.com" ; Benjamin "Snowhare" Franz
55 "red@redpoll.mrfs.oh.us (Richard E. Depew)" ; ARMM! ARMM!
57 "*List of NoCeM issuers to pay attention to.
59 This can also be a list of `(ISSUER CONDITIONS)' elements."
60 :group 'gnus-nocem
61 :type '(repeat (choice string sexp)))
63 (defcustom gnus-nocem-directory
64 (nnheader-concat gnus-article-save-directory "NoCeM/")
65 "*Directory where NoCeM files will be stored."
66 :group 'gnus-nocem
67 :type 'directory)
69 (defcustom gnus-nocem-expiry-wait 15
70 "*Number of days to keep NoCeM headers in the cache."
71 :group 'gnus-nocem
72 :type 'integer)
74 (defcustom gnus-nocem-verifyer 'mc-verify
75 "*Function called to verify that the NoCeM message is valid.
76 One likely value is `mc-verify'. If the function in this variable
77 isn't bound, the message will be used unconditionally."
78 :group 'gnus-nocem
79 :type '(radio (function-item mc-verify)
80 (function :tag "other")))
82 (defcustom gnus-nocem-liberal-fetch nil
83 "*If t try to fetch all messages which have @@NCM in the subject.
84 Otherwise don't fetch messages which have references or whose message-id
85 matches an previously scanned and verified nocem message."
86 :group 'gnus-nocem
87 :type 'boolean)
89 ;;; Internal variables
91 (defvar gnus-nocem-active nil)
92 (defvar gnus-nocem-alist nil)
93 (defvar gnus-nocem-touched-alist nil)
94 (defvar gnus-nocem-hashtb nil)
95 (defvar gnus-nocem-seen-message-ids nil)
97 ;;; Functions
99 (defun gnus-nocem-active-file ()
100 (concat (file-name-as-directory gnus-nocem-directory) "active"))
102 (defun gnus-nocem-cache-file ()
103 (concat (file-name-as-directory gnus-nocem-directory) "cache"))
106 ;; faster lookups for group names:
109 (defvar gnus-nocem-real-group-hashtb nil
110 "Real-name mappings of subscribed groups.")
112 (defun gnus-fill-real-hashtb ()
113 "Fill up a hash table with the real-name mappings from the user's active file."
114 (setq gnus-nocem-real-group-hashtb (gnus-make-hashtable
115 (length gnus-newsrc-alist)))
116 (mapcar (lambda (group)
117 (setq group (gnus-group-real-name (car group)))
118 (gnus-sethash group t gnus-nocem-real-group-hashtb))
119 gnus-newsrc-alist))
121 (defun gnus-nocem-scan-groups ()
122 "Scan all NoCeM groups for new NoCeM messages."
123 (interactive)
124 (let ((groups gnus-nocem-groups)
125 (gnus-inhibit-demon t)
126 group active gactive articles)
127 (gnus-make-directory gnus-nocem-directory)
128 ;; Load any previous NoCeM headers.
129 (gnus-nocem-load-cache)
130 ;; Get the group name mappings:
131 (gnus-fill-real-hashtb)
132 ;; Read the active file if it hasn't been read yet.
133 (and (file-exists-p (gnus-nocem-active-file))
134 (not gnus-nocem-active)
135 (ignore-errors
136 (load (gnus-nocem-active-file) t t t)))
137 ;; Go through all groups and see whether new articles have
138 ;; arrived.
139 (while (setq group (pop groups))
140 (if (not (setq gactive (gnus-activate-group group)))
141 () ; This group doesn't exist.
142 (setq active (nth 1 (assoc group gnus-nocem-active)))
143 (when (and (not (< (cdr gactive) (car gactive))) ; Empty group.
144 (or (not active)
145 (< (cdr active) (cdr gactive))))
146 ;; Ok, there are new articles in this group, se we fetch the
147 ;; headers.
148 (save-excursion
149 (let ((dependencies (make-vector 10 nil))
150 headers header)
151 (nnheader-temp-write nil
152 (setq headers
153 (if (eq 'nov
154 (gnus-retrieve-headers
155 (setq articles
156 (gnus-uncompress-range
157 (cons
158 (if active (1+ (cdr active))
159 (car gactive))
160 (cdr gactive))))
161 group))
162 (gnus-get-newsgroup-headers-xover
163 articles nil dependencies)
164 (gnus-get-newsgroup-headers dependencies)))
165 (while (setq header (pop headers))
166 ;; We take a closer look on all articles that have
167 ;; "@@NCM" in the subject. Unless we already read
168 ;; this cross posted message. Nocem messages
169 ;; are not allowed to have references, so we can
170 ;; ignore scanning followups.
171 (and (string-match "@@NCM" (mail-header-subject header))
172 (or gnus-nocem-liberal-fetch
173 (and (or (string= "" (mail-header-references
174 header))
175 (null (mail-header-references header)))
176 (not (member (mail-header-message-id header)
177 gnus-nocem-seen-message-ids))))
178 (gnus-nocem-check-article group header)))))))
179 (setq gnus-nocem-active
180 (cons (list group gactive)
181 (delq (assoc group gnus-nocem-active)
182 gnus-nocem-active)))))
183 ;; Save the results, if any.
184 (gnus-nocem-save-cache)
185 (gnus-nocem-save-active)))
187 (defun gnus-nocem-check-article (group header)
188 "Check whether the current article is an NCM article and that we want it."
189 ;; Get the article.
190 (gnus-message 7 "Checking article %d in %s for NoCeM..."
191 (mail-header-number header) group)
192 (let ((date (mail-header-date header))
193 issuer b e type)
194 (when (or (not date)
195 (nnmail-time-less
196 (nnmail-time-since (nnmail-date-to-time date))
197 (nnmail-days-to-time gnus-nocem-expiry-wait)))
198 (gnus-request-article-this-buffer (mail-header-number header) group)
199 (goto-char (point-min))
200 (when (re-search-forward "-----BEGIN PGP MESSAGE-----" nil t)
201 (delete-region (point-min) (match-beginning 0)))
202 (when (re-search-forward "-----END PGP MESSAGE-----\n?" nil t)
203 (delete-region (match-end 0) (point-max)))
204 (goto-char (point-min))
205 ;; The article has to have proper NoCeM headers.
206 (when (and (setq b (search-forward "\n@@BEGIN NCM HEADERS\n" nil t))
207 (setq e (search-forward "\n@@BEGIN NCM BODY\n" nil t)))
208 ;; We get the name of the issuer.
209 (narrow-to-region b e)
210 (setq issuer (mail-fetch-field "issuer")
211 type (mail-fetch-field "issuer"))
212 (widen)
213 (if (not (gnus-nocem-message-wanted-p issuer type))
214 (message "invalid NoCeM issuer: %s" issuer)
215 (and (gnus-nocem-verify-issuer issuer) ; She is who she says she is.
216 (gnus-nocem-enter-article) ; We gobble the message.
217 (push (mail-header-message-id header) ; But don't come back for
218 gnus-nocem-seen-message-ids))))))) ; second helpings.
220 (defun gnus-nocem-message-wanted-p (issuer type)
221 (let ((issuers gnus-nocem-issuers)
222 wanted conditions condition)
223 (cond
224 ;; Do the quick check first.
225 ((member issuer issuers)
227 ((setq conditions (cdr (assoc issuer issuers)))
228 ;; Check whether we want this type.
229 (while (setq condition (pop conditions))
230 (cond
231 ((stringp condition)
232 (setq wanted (string-match condition type)))
233 ((and (consp condition)
234 (eq (car condition) 'not)
235 (stringp (cadr condition)))
236 (setq wanted (not (string-match (cadr condition) type))))
238 (error "Invalid NoCeM condition: %S" condition))))
239 wanted))))
241 (defun gnus-nocem-verify-issuer (person)
242 "Verify using PGP that the canceler is who she says she is."
243 (if (fboundp gnus-nocem-verifyer)
244 (ignore-errors
245 (funcall gnus-nocem-verifyer))
246 ;; If we don't have Mailcrypt, then we use the message anyway.
249 (defun gnus-nocem-enter-article ()
250 "Enter the current article into the NoCeM cache."
251 (goto-char (point-min))
252 (let ((b (search-forward "\n@@BEGIN NCM BODY\n" nil t))
253 (e (search-forward "\n@@END NCM BODY\n" nil t))
254 (buf (current-buffer))
255 ncm id group)
256 (when (and b e)
257 (narrow-to-region b (1+ (match-beginning 0)))
258 (goto-char (point-min))
259 (while (search-forward "\t" nil t)
260 (cond
261 ((not (ignore-errors
262 (setq group (let ((obarray gnus-active-hashtb)) (read buf)))))
263 ;; An error.
265 ((not (symbolp group))
266 ;; Ignore invalid entries.
268 ((not (boundp group))
269 ;; Make sure all entries in the hashtb are bound.
270 (set group nil))
272 (when (gnus-gethash (gnus-group-real-name (symbol-name group))
273 gnus-nocem-real-group-hashtb)
274 ;; Valid group.
275 (beginning-of-line)
276 (while (= (following-char) ?\t)
277 (forward-line -1))
278 (setq id (buffer-substring (point) (1- (search-forward "\t"))))
279 (unless (gnus-gethash id gnus-nocem-hashtb)
280 ;; only store if not already present
281 (gnus-sethash id t gnus-nocem-hashtb)
282 (push id ncm))
283 (forward-line 1)
284 (while (= (following-char) ?\t)
285 (forward-line 1))))))
286 (when ncm
287 (setq gnus-nocem-touched-alist t)
288 (push (cons (let ((time (current-time))) (setcdr (cdr time) nil) time)
289 ncm)
290 gnus-nocem-alist))
291 t)))
293 (defun gnus-nocem-load-cache ()
294 "Load the NoCeM cache."
295 (interactive)
296 (unless gnus-nocem-alist
297 ;; The buffer doesn't exist, so we create it and load the NoCeM
298 ;; cache.
299 (when (file-exists-p (gnus-nocem-cache-file))
300 (load (gnus-nocem-cache-file) t t t)
301 (gnus-nocem-alist-to-hashtb))))
303 (defun gnus-nocem-save-cache ()
304 "Save the NoCeM cache."
305 (when (and gnus-nocem-alist
306 gnus-nocem-touched-alist)
307 (nnheader-temp-write (gnus-nocem-cache-file)
308 (gnus-prin1 `(setq gnus-nocem-alist ',gnus-nocem-alist)))
309 (setq gnus-nocem-touched-alist nil)))
311 (defun gnus-nocem-save-active ()
312 "Save the NoCeM active file."
313 (nnheader-temp-write (gnus-nocem-active-file)
314 (gnus-prin1 `(setq gnus-nocem-active ',gnus-nocem-active))))
316 (defun gnus-nocem-alist-to-hashtb ()
317 "Create a hashtable from the Message-IDs we have."
318 (let* ((alist gnus-nocem-alist)
319 (pprev (cons nil alist))
320 (prev pprev)
321 (expiry (nnmail-days-to-time gnus-nocem-expiry-wait))
322 entry)
323 (setq gnus-nocem-hashtb (gnus-make-hashtable (* (length alist) 51)))
324 (while (setq entry (car alist))
325 (if (not (nnmail-time-less (nnmail-time-since (car entry)) expiry))
326 ;; This entry has expired, so we remove it.
327 (setcdr prev (cdr alist))
328 (setq prev alist)
329 ;; This is ok, so we enter it into the hashtable.
330 (setq entry (cdr entry))
331 (while entry
332 (gnus-sethash (car entry) t gnus-nocem-hashtb)
333 (setq entry (cdr entry))))
334 (setq alist (cdr alist)))))
336 (gnus-add-shutdown 'gnus-nocem-close 'gnus)
338 (defun gnus-nocem-close ()
339 "Clear internal NoCeM variables."
340 (setq gnus-nocem-alist nil
341 gnus-nocem-hashtb nil
342 gnus-nocem-active nil
343 gnus-nocem-touched-alist nil
344 gnus-nocem-seen-message-ids nil
345 gnus-nocem-real-group-hashtb nil))
347 (defun gnus-nocem-unwanted-article-p (id)
348 "Say whether article ID in the current group is wanted."
349 (and gnus-nocem-hashtb
350 (gnus-gethash id gnus-nocem-hashtb)))
352 (provide 'gnus-nocem)
354 ;;; gnus-nocem.el ends here