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