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