1 ;;; gnus-nocem.el --- NoCeM pseudo-cancellation treatment
3 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2002, 2003, 2004,
4 ;; 2005 Free Software Foundation, Inc.
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
9 ;; This file is part of GNU Emacs.
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)
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.
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.
30 (eval-when-compile (require 'cl))
38 (defgroup gnus-nocem nil
39 "NoCeM pseudo-cancellation treatment."
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."
47 :type '(repeat (string :tag "Group")))
49 (defcustom gnus-nocem-issuers
50 '("AutoMoose-1" ; CancelMoose[tm]
51 "clewis@ferret.ocunix" ; Chris Lewis
55 "*List of NoCeM issuers to pay attention to.
57 This can also be a list of `(ISSUER CONDITION ...)' elements.
59 See <URL:http://www.xs4all.nl/~rosalind/nocemreg/nocemreg.html> for an
62 :link '(url-link "http://www.xs4all.nl/~rosalind/nocemreg/nocemreg.html")
63 :type '(repeat (choice string sexp)))
65 (defcustom gnus-nocem-directory
66 (nnheader-concat gnus-article-save-directory "NoCeM/")
67 "*Directory where NoCeM files will be stored."
71 (defcustom gnus-nocem-expiry-wait 15
72 "*Number of days to keep NoCeM headers in the cache."
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."
81 :type '(radio (function-item pgg-verify)
82 (function-item mc-verify)
83 (function :tag "other")))
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."
92 (defcustom gnus-nocem-check-article-limit 500
93 "*If non-nil, the maximum number of articles to check in any NoCeM group."
96 :type '(choice (const :tag "unlimited" nil)
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."
107 ;;; Internal variables
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)
117 (defun gnus-nocem-active-file ()
118 (concat (file-name-as-directory gnus-nocem-directory) "active"))
120 (defun gnus-nocem-cache-file ()
121 (concat (file-name-as-directory gnus-nocem-directory) "cache"))
124 ;; faster lookups for group names:
127 (defvar gnus-nocem-real-group-hashtb nil
128 "Real-name mappings of subscribed groups.")
130 (defun gnus-fill-real-hashtb ()
131 "Fill up a hash table with the real-name mappings from the user's active file."
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)))
135 (mapcar (lambda (group)
136 (setq group (gnus-group-real-name (car group)))
137 (puthash group t gnus-nocem-real-group-hashtb))
140 (defun gnus-nocem-scan-groups ()
141 "Scan all NoCeM groups for new NoCeM messages."
143 (let ((groups gnus-nocem-groups)
144 (gnus-inhibit-demon t)
145 group active gactive articles check-headers)
146 (gnus-make-directory gnus-nocem-directory)
147 ;; Load any previous NoCeM headers.
148 (gnus-nocem-load-cache)
149 ;; Get the group name mappings:
150 (gnus-fill-real-hashtb)
151 ;; Read the active file if it hasn't been read yet.
152 (and (file-exists-p (gnus-nocem-active-file))
153 (not gnus-nocem-active)
155 (load (gnus-nocem-active-file) t t t)))
156 ;; Go through all groups and see whether new articles have
158 (while (setq group (pop groups))
159 (if (not (setq gactive (gnus-activate-group group)))
160 () ; This group doesn't exist.
161 (setq active (nth 1 (assoc group gnus-nocem-active)))
162 (when (and (not (< (cdr gactive) (car gactive))) ; Empty group.
164 (< (cdr active) (cdr gactive))))
165 ;; Ok, there are new articles in this group, se we fetch the
168 (let ((dependencies (make-vector 10 nil))
173 (gnus-retrieve-headers
175 (gnus-uncompress-range
177 (if active (1+ (cdr active))
181 (gnus-get-newsgroup-headers-xover
182 articles nil dependencies)
183 (gnus-get-newsgroup-headers dependencies)))
184 (while (setq header (pop headers))
185 ;; We take a closer look on all articles that have
186 ;; "@@NCM" in the subject. Unless we already read
187 ;; this cross posted message. Nocem messages
188 ;; are not allowed to have references, so we can
189 ;; ignore scanning followups.
190 (and (string-match "@@NCM" (mail-header-subject header))
191 (and gnus-nocem-check-from
192 (let ((case-fold-search t))
197 (setq author (car author)))
199 author (mail-header-from header))
203 (or gnus-nocem-liberal-fetch
204 (and (or (string= "" (mail-header-references
206 (null (mail-header-references header)))
207 (not (member (mail-header-message-id header)
208 gnus-nocem-seen-message-ids))))
209 (push header check-headers)))
212 (last check-headers gnus-nocem-check-article-limit))
213 (len (length check-headers)))
214 (dolist (h check-headers)
216 7 "Checking article %d in %s for NoCeM (%d of %d)..."
217 (mail-header-number h) group (incf i) len)
218 (gnus-nocem-check-article group h)))))))
219 (setq gnus-nocem-active
220 (cons (list group gactive)
221 (delq (assoc group gnus-nocem-active)
222 gnus-nocem-active)))))
223 ;; Save the results, if any.
224 (gnus-nocem-save-cache)
225 (gnus-nocem-save-active)))
227 (defun gnus-nocem-check-article (group header)
228 "Check whether the current article is an NCM article and that we want it."
230 (let ((date (mail-header-date header))
231 (gnus-newsgroup-name group)
235 (time-since (date-to-time date))
236 (days-to-time gnus-nocem-expiry-wait)))
237 (gnus-request-article-this-buffer (mail-header-number header) group)
238 (goto-char (point-min))
239 (when (re-search-forward "-----BEGIN PGP MESSAGE-----" nil t)
240 (delete-region (point-min) (match-beginning 0)))
241 (when (re-search-forward "-----END PGP MESSAGE-----\n?" nil t)
242 (delete-region (match-end 0) (point-max)))
243 (goto-char (point-min))
244 ;; The article has to have proper NoCeM headers.
245 (when (and (setq b (search-forward "\n@@BEGIN NCM HEADERS\n" nil t))
246 (setq e (search-forward "\n@@BEGIN NCM BODY\n" nil t)))
247 ;; We get the name of the issuer.
248 (narrow-to-region b e)
249 (setq issuer (mail-fetch-field "issuer")
250 type (mail-fetch-field "type"))
252 (if (not (gnus-nocem-message-wanted-p issuer type))
253 (message "invalid NoCeM issuer: %s" issuer)
254 (and (gnus-nocem-verify-issuer issuer) ; She is who she says she is.
255 (gnus-nocem-enter-article) ; We gobble the message.
256 (push (mail-header-message-id header) ; But don't come back for
257 gnus-nocem-seen-message-ids))))))) ; second helpings.
259 (defun gnus-nocem-message-wanted-p (issuer type)
260 (let ((issuers gnus-nocem-issuers)
261 wanted conditions condition)
263 ;; Do the quick check first.
264 ((member issuer issuers)
266 ((setq conditions (cdr (assoc issuer issuers)))
267 ;; Check whether we want this type.
268 (while (setq condition (pop conditions))
271 (when (string-match condition type)
273 ((and (consp condition)
274 (eq (car condition) 'not)
275 (stringp (cadr condition)))
276 (when (string-match (cadr condition) type)
279 (error "Invalid NoCeM condition: %S" condition))))
282 (defun gnus-nocem-verify-issuer (person)
283 "Verify using PGP that the canceler is who she says she is."
284 (if (functionp gnus-nocem-verifyer)
286 (funcall gnus-nocem-verifyer))
287 ;; If we don't have Mailcrypt, then we use the message anyway.
290 (defun gnus-nocem-enter-article ()
291 "Enter the current article into the NoCeM cache."
292 (goto-char (point-min))
293 (let ((b (search-forward "\n@@BEGIN NCM BODY\n" nil t))
294 (e (search-forward "\n@@END NCM BODY\n" nil t))
295 (buf (current-buffer))
298 (narrow-to-region b (1+ (match-beginning 0)))
299 (goto-char (point-min))
300 (while (search-forward "\t" nil t)
303 (setq group (gnus-group-real-name (symbol-name (read buf))))
304 (gethash group gnus-nocem-real-group-hashtb)))
310 (while (eq (char-after) ?\t)
312 (setq id (buffer-substring (point) (1- (search-forward "\t"))))
313 (unless (if (hash-table-p gnus-nocem-hashtb)
314 (gethash id gnus-nocem-hashtb)
315 (setq gnus-nocem-hashtb (make-hash-table :test 'equal))
317 ;; only store if not already present
318 (puthash id t gnus-nocem-hashtb)
321 (while (eq (char-after) ?\t)
324 (setq gnus-nocem-touched-alist t)
325 (push (cons (let ((time (current-time))) (setcdr (cdr time) nil) time)
330 (defun gnus-nocem-load-cache ()
331 "Load the NoCeM cache."
333 (unless gnus-nocem-alist
334 ;; The buffer doesn't exist, so we create it and load the NoCeM
336 (when (file-exists-p (gnus-nocem-cache-file))
337 (load (gnus-nocem-cache-file) t t t)
338 (gnus-nocem-alist-to-hashtb))))
340 (defun gnus-nocem-save-cache ()
341 "Save the NoCeM cache."
342 (when (and gnus-nocem-alist
343 gnus-nocem-touched-alist)
344 (with-temp-file (gnus-nocem-cache-file)
345 (gnus-prin1 `(setq gnus-nocem-alist ',gnus-nocem-alist)))
346 (setq gnus-nocem-touched-alist nil)))
348 (defun gnus-nocem-save-active ()
349 "Save the NoCeM active file."
350 (with-temp-file (gnus-nocem-active-file)
351 (gnus-prin1 `(setq gnus-nocem-active ',gnus-nocem-active))))
353 (defun gnus-nocem-alist-to-hashtb ()
354 "Create a hashtable from the Message-IDs we have."
355 (let* ((alist gnus-nocem-alist)
356 (pprev (cons nil alist))
358 (expiry (days-to-time gnus-nocem-expiry-wait))
360 (if (hash-table-p gnus-nocem-hashtb)
361 (clrhash gnus-nocem-hashtb)
362 (setq gnus-nocem-hashtb (make-hash-table :test 'equal)))
363 (while (setq entry (car alist))
364 (if (not (time-less-p (time-since (car entry)) expiry))
365 ;; This entry has expired, so we remove it.
366 (setcdr prev (cdr alist))
368 ;; This is ok, so we enter it into the hashtable.
369 (setq entry (cdr entry))
371 (puthash (car entry) t gnus-nocem-hashtb)
372 (setq entry (cdr entry))))
373 (setq alist (cdr alist)))))
375 (gnus-add-shutdown 'gnus-nocem-close 'gnus)
377 (defun gnus-nocem-close ()
378 "Clear internal NoCeM variables."
379 (setq gnus-nocem-alist nil
380 gnus-nocem-hashtb nil
381 gnus-nocem-active nil
382 gnus-nocem-touched-alist nil
383 gnus-nocem-seen-message-ids nil
384 gnus-nocem-real-group-hashtb nil))
386 (defun gnus-nocem-unwanted-article-p (id)
387 "Say whether article ID in the current group is wanted."
388 (and gnus-nocem-hashtb
389 (gethash id gnus-nocem-hashtb)))
391 (provide 'gnus-nocem)
393 ;;; arch-tag: 0e0c74ea-2f8e-4f3e-8fff-09f767c1adef
394 ;;; gnus-nocem.el ends here