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>
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)
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.
28 (eval-when-compile (require 'cl))
36 (defgroup gnus-nocem nil
37 "NoCeM pseudo-cancellation treatment"
40 (defcustom gnus-nocem-groups
41 '("news.lists.filters" "news.admin.net-abuse.bulletins"
42 "alt.nocem.misc" "news.admin.net-abuse.announce")
43 "*List of groups that will be searched for NoCeM messages."
45 :type '(repeat (string :tag "Group")))
47 (defcustom gnus-nocem-issuers
48 '("AutoMoose-1" "Automoose-1" ; CancelMoose[tm]
49 "rbraver@ohww.norman.ok.us" ; Robert Braver
50 "clewis@ferret.ocunix.on.ca" ; Chris Lewis
51 "jem@xpat.com" ; Despammer from Korea
52 "snowhare@xmission.com" ; Benjamin "Snowhare" Franz
53 "red@redpoll.mrfs.oh.us (Richard E. Depew)" ; ARMM! ARMM!
55 "*List of NoCeM issuers to pay attention to.
57 This can also be a list of `(ISSUER CONDITIONS)' elements."
59 :type '(repeat (choice string sexp)))
61 (defcustom gnus-nocem-directory
62 (nnheader-concat gnus-article-save-directory "NoCeM/")
63 "*Directory where NoCeM files will be stored."
67 (defcustom gnus-nocem-expiry-wait 15
68 "*Number of days to keep NoCeM headers in the cache."
72 (defcustom gnus-nocem-verifyer 'mc-verify
73 "*Function called to verify that the NoCeM message is valid.
74 One likely value is `mc-verify'. If the function in this variable
75 isn't bound, the message will be used unconditionally."
77 :type '(radio (function-item mc-verify)
78 (function :tag "other")))
80 (defcustom gnus-nocem-liberal-fetch nil
81 "*If t try to fetch all messages which have @@NCM in the subject.
82 Otherwise don't fetch messages which have references or whose message-id
83 matches an previously scanned and verified nocem message."
87 ;;; Internal variables
89 (defvar gnus-nocem-active nil)
90 (defvar gnus-nocem-alist nil)
91 (defvar gnus-nocem-touched-alist nil)
92 (defvar gnus-nocem-hashtb nil)
93 (defvar gnus-nocem-seen-message-ids nil)
97 (defun gnus-nocem-active-file ()
98 (concat (file-name-as-directory gnus-nocem-directory) "active"))
100 (defun gnus-nocem-cache-file ()
101 (concat (file-name-as-directory gnus-nocem-directory) "cache"))
104 ;; faster lookups for group names:
107 (defvar gnus-nocem-real-group-hashtb nil
108 "Real-name mappings of subscribed groups.")
110 (defun gnus-fill-real-hashtb ()
111 "Fill up a hash table with the real-name mappings from the user's active file."
112 (setq gnus-nocem-real-group-hashtb (gnus-make-hashtable
113 (length gnus-newsrc-alist)))
114 (mapcar (lambda (group)
115 (setq group (gnus-group-real-name (car group)))
116 (gnus-sethash group t gnus-nocem-real-group-hashtb))
119 (defun gnus-nocem-scan-groups ()
120 "Scan all NoCeM groups for new NoCeM messages."
122 (let ((groups gnus-nocem-groups)
123 (gnus-inhibit-demon t)
124 group active gactive articles)
125 (gnus-make-directory gnus-nocem-directory)
126 ;; Load any previous NoCeM headers.
127 (gnus-nocem-load-cache)
128 ;; Get the group name mappings:
129 (gnus-fill-real-hashtb)
130 ;; Read the active file if it hasn't been read yet.
131 (and (file-exists-p (gnus-nocem-active-file))
132 (not gnus-nocem-active)
134 (load (gnus-nocem-active-file) t t t)))
135 ;; Go through all groups and see whether new articles have
137 (while (setq group (pop groups))
138 (if (not (setq gactive (gnus-activate-group group)))
139 () ; This group doesn't exist.
140 (setq active (nth 1 (assoc group gnus-nocem-active)))
141 (when (and (not (< (cdr gactive) (car gactive))) ; Empty group.
143 (< (cdr active) (cdr gactive))))
144 ;; Ok, there are new articles in this group, se we fetch the
147 (let ((dependencies (make-vector 10 nil))
152 (gnus-retrieve-headers
154 (gnus-uncompress-range
156 (if active (1+ (cdr active))
160 (gnus-get-newsgroup-headers-xover
161 articles nil dependencies)
162 (gnus-get-newsgroup-headers dependencies)))
163 (while (setq header (pop headers))
164 ;; We take a closer look on all articles that have
165 ;; "@@NCM" in the subject. Unless we already read
166 ;; this cross posted message. Nocem messages
167 ;; are not allowed to have references, so we can
168 ;; ignore scanning followups.
169 (and (string-match "@@NCM" (mail-header-subject header))
170 (or gnus-nocem-liberal-fetch
171 (and (or (string= "" (mail-header-references
173 (null (mail-header-references header)))
174 (not (member (mail-header-message-id header)
175 gnus-nocem-seen-message-ids))))
176 (gnus-nocem-check-article group header)))))))
177 (setq gnus-nocem-active
178 (cons (list group gactive)
179 (delq (assoc group gnus-nocem-active)
180 gnus-nocem-active)))))
181 ;; Save the results, if any.
182 (gnus-nocem-save-cache)
183 (gnus-nocem-save-active)))
185 (defun gnus-nocem-check-article (group header)
186 "Check whether the current article is an NCM article and that we want it."
188 (gnus-message 7 "Checking article %d in %s for NoCeM..."
189 (mail-header-number header) group)
190 (let ((date (mail-header-date header))
194 (time-since (date-to-time date))
195 (days-to-time gnus-nocem-expiry-wait)))
196 (gnus-request-article-this-buffer (mail-header-number header) group)
197 (goto-char (point-min))
198 (when (re-search-forward "-----BEGIN PGP MESSAGE-----" nil t)
199 (delete-region (point-min) (match-beginning 0)))
200 (when (re-search-forward "-----END PGP MESSAGE-----\n?" nil t)
201 (delete-region (match-end 0) (point-max)))
202 (goto-char (point-min))
203 ;; The article has to have proper NoCeM headers.
204 (when (and (setq b (search-forward "\n@@BEGIN NCM HEADERS\n" nil t))
205 (setq e (search-forward "\n@@BEGIN NCM BODY\n" nil t)))
206 ;; We get the name of the issuer.
207 (narrow-to-region b e)
208 (setq issuer (mail-fetch-field "issuer")
209 type (mail-fetch-field "issuer"))
211 (if (not (gnus-nocem-message-wanted-p issuer type))
212 (message "invalid NoCeM issuer: %s" issuer)
213 (and (gnus-nocem-verify-issuer issuer) ; She is who she says she is.
214 (gnus-nocem-enter-article) ; We gobble the message.
215 (push (mail-header-message-id header) ; But don't come back for
216 gnus-nocem-seen-message-ids))))))) ; second helpings.
218 (defun gnus-nocem-message-wanted-p (issuer type)
219 (let ((issuers gnus-nocem-issuers)
220 wanted conditions condition)
222 ;; Do the quick check first.
223 ((member issuer issuers)
225 ((setq conditions (cdr (assoc issuer issuers)))
226 ;; Check whether we want this type.
227 (while (setq condition (pop conditions))
230 (setq wanted (string-match condition type)))
231 ((and (consp condition)
232 (eq (car condition) 'not)
233 (stringp (cadr condition)))
234 (setq wanted (not (string-match (cadr condition) type))))
236 (error "Invalid NoCeM condition: %S" condition))))
239 (defun gnus-nocem-verify-issuer (person)
240 "Verify using PGP that the canceler is who she says she is."
241 (if (fboundp gnus-nocem-verifyer)
243 (funcall gnus-nocem-verifyer))
244 ;; If we don't have Mailcrypt, then we use the message anyway.
247 (defun gnus-nocem-enter-article ()
248 "Enter the current article into the NoCeM cache."
249 (goto-char (point-min))
250 (let ((b (search-forward "\n@@BEGIN NCM BODY\n" nil t))
251 (e (search-forward "\n@@END NCM BODY\n" nil t))
252 (buf (current-buffer))
255 (narrow-to-region b (1+ (match-beginning 0)))
256 (goto-char (point-min))
257 (while (search-forward "\t" nil t)
260 (setq group (let ((obarray gnus-active-hashtb)) (read buf)))))
263 ((not (symbolp group))
264 ;; Ignore invalid entries.
266 ((not (boundp group))
267 ;; Make sure all entries in the hashtb are bound.
270 (when (gnus-gethash (gnus-group-real-name (symbol-name group))
271 gnus-nocem-real-group-hashtb)
274 (while (eq (char-after) ?\t)
276 (setq id (buffer-substring (point) (1- (search-forward "\t"))))
277 (unless (gnus-gethash id gnus-nocem-hashtb)
278 ;; only store if not already present
279 (gnus-sethash id t gnus-nocem-hashtb)
282 (while (eq (char-after) ?\t)
283 (forward-line 1))))))
285 (setq gnus-nocem-touched-alist t)
286 (push (cons (let ((time (current-time))) (setcdr (cdr time) nil) time)
291 (defun gnus-nocem-load-cache ()
292 "Load the NoCeM cache."
294 (unless gnus-nocem-alist
295 ;; The buffer doesn't exist, so we create it and load the NoCeM
297 (when (file-exists-p (gnus-nocem-cache-file))
298 (load (gnus-nocem-cache-file) t t t)
299 (gnus-nocem-alist-to-hashtb))))
301 (defun gnus-nocem-save-cache ()
302 "Save the NoCeM cache."
303 (when (and gnus-nocem-alist
304 gnus-nocem-touched-alist)
305 (with-temp-file (gnus-nocem-cache-file)
306 (gnus-prin1 `(setq gnus-nocem-alist ',gnus-nocem-alist)))
307 (setq gnus-nocem-touched-alist nil)))
309 (defun gnus-nocem-save-active ()
310 "Save the NoCeM active file."
311 (with-temp-file (gnus-nocem-active-file)
312 (gnus-prin1 `(setq gnus-nocem-active ',gnus-nocem-active))))
314 (defun gnus-nocem-alist-to-hashtb ()
315 "Create a hashtable from the Message-IDs we have."
316 (let* ((alist gnus-nocem-alist)
317 (pprev (cons nil alist))
319 (expiry (days-to-time gnus-nocem-expiry-wait))
321 (setq gnus-nocem-hashtb (gnus-make-hashtable (* (length alist) 51)))
322 (while (setq entry (car alist))
323 (if (not (time-less-p (time-since (car entry)) expiry))
324 ;; This entry has expired, so we remove it.
325 (setcdr prev (cdr alist))
327 ;; This is ok, so we enter it into the hashtable.
328 (setq entry (cdr entry))
330 (gnus-sethash (car entry) t gnus-nocem-hashtb)
331 (setq entry (cdr entry))))
332 (setq alist (cdr alist)))))
334 (gnus-add-shutdown 'gnus-nocem-close 'gnus)
336 (defun gnus-nocem-close ()
337 "Clear internal NoCeM variables."
338 (setq gnus-nocem-alist nil
339 gnus-nocem-hashtb nil
340 gnus-nocem-active nil
341 gnus-nocem-touched-alist nil
342 gnus-nocem-seen-message-ids nil
343 gnus-nocem-real-group-hashtb nil))
345 (defun gnus-nocem-unwanted-article-p (id)
346 "Say whether article ID in the current group is wanted."
347 (and gnus-nocem-hashtb
348 (gnus-gethash id gnus-nocem-hashtb)))
350 (provide 'gnus-nocem)
352 ;;; gnus-nocem.el ends here