0ae227c538c9d7f90210f68d69b79ab4190e78ce
[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   :version "21.1"
94   :type '(choice (const :tag "unlimited" nil)
95                  (integer 1000)))
96
97 (defcustom gnus-nocem-check-from t
98   "Non-nil means check for valid issuers in message bodies.
99 Otherwise don't bother fetching articles unless their author matches a
100 valid issuer, which is much faster if you are selective about the issuers."
101   :group 'gnus-nocem
102   :version "21.1"
103   :type 'boolean)
104
105 ;;; Internal variables
106
107 (defvar gnus-nocem-active nil)
108 (defvar gnus-nocem-alist nil)
109 (defvar gnus-nocem-touched-alist nil)
110 (defvar gnus-nocem-hashtb nil)
111 (defvar gnus-nocem-seen-message-ids nil)
112
113 ;;; Functions
114
115 (defun gnus-nocem-active-file ()
116   (concat (file-name-as-directory gnus-nocem-directory) "active"))
117
118 (defun gnus-nocem-cache-file ()
119   (concat (file-name-as-directory gnus-nocem-directory) "cache"))
120
121 ;;
122 ;; faster lookups for group names:
123 ;;
124
125 (defvar gnus-nocem-real-group-hashtb nil
126   "Real-name mappings of subscribed groups.")
127
128 (defun gnus-fill-real-hashtb ()
129   "Fill up a hash table with the real-name mappings from the user's active file."
130   (setq gnus-nocem-real-group-hashtb (gnus-make-hashtable
131                                       (length gnus-newsrc-alist)))
132   (mapcar (lambda (group)
133             (setq group (gnus-group-real-name (car group)))
134             (gnus-sethash group t gnus-nocem-real-group-hashtb))
135           gnus-newsrc-alist))
136
137 (defun gnus-nocem-scan-groups ()
138   "Scan all NoCeM groups for new NoCeM messages."
139   (interactive)
140   (let ((groups gnus-nocem-groups)
141         (gnus-inhibit-demon t)
142         group active gactive articles check-headers)
143     (gnus-make-directory gnus-nocem-directory)
144     ;; Load any previous NoCeM headers.
145     (gnus-nocem-load-cache)
146     ;; Get the group name mappings:
147     (gnus-fill-real-hashtb)
148     ;; Read the active file if it hasn't been read yet.
149     (and (file-exists-p (gnus-nocem-active-file))
150          (not gnus-nocem-active)
151          (ignore-errors
152            (load (gnus-nocem-active-file) t t t)))
153     ;; Go through all groups and see whether new articles have
154     ;; arrived.
155     (while (setq group (pop groups))
156       (if (not (setq gactive (gnus-activate-group group)))
157           ()                            ; This group doesn't exist.
158         (setq active (nth 1 (assoc group gnus-nocem-active)))
159         (when (and (not (< (cdr gactive) (car gactive))) ; Empty group.
160                    (or (not active)
161                        (< (cdr active) (cdr gactive))))
162           ;; Ok, there are new articles in this group, se we fetch the
163           ;; headers.
164           (save-excursion
165             (let ((dependencies (make-vector 10 nil))
166                   headers header)
167               (with-temp-buffer
168                 (setq headers
169                       (if (eq 'nov
170                               (gnus-retrieve-headers
171                                (setq articles
172                                      (gnus-uncompress-range
173                                       (cons
174                                        (if active (1+ (cdr active))
175                                          (car gactive))
176                                        (cdr gactive))))
177                                group))
178                           (gnus-get-newsgroup-headers-xover
179                            articles nil dependencies)
180                         (gnus-get-newsgroup-headers dependencies)))
181                 (while (setq header (pop headers))
182                   ;; We take a closer look on all articles that have
183                   ;; "@@NCM" in the subject.  Unless we already read
184                   ;; this cross posted message.  Nocem messages
185                   ;; are not allowed to have references, so we can
186                   ;; ignore scanning followups.
187                   (and (string-match "@@NCM" (mail-header-subject header))
188                        (and gnus-nocem-check-from
189                             (let ((case-fold-search t))
190                               (catch 'ok
191                                 (mapcar
192                                  (lambda (author)
193                                    (if (consp author)
194                                        (setq author (car author)))
195                                    (if (string-match
196                                         author (mail-header-from header))
197                                        (throw 'ok t)))
198                                  gnus-nocem-issuers)
199                                 nil)))
200                        (or gnus-nocem-liberal-fetch
201                            (and (or (string= "" (mail-header-references
202                                                  header))
203                                     (null (mail-header-references header)))
204                                 (not (member (mail-header-message-id header)
205                                              gnus-nocem-seen-message-ids))))
206                        (push header check-headers)))
207                 (let* ((i 0)
208                        (check-headers
209                         (last check-headers gnus-nocem-check-article-limit))
210                        (len (length check-headers)))
211                   (dolist (h check-headers)
212                     (gnus-message
213                      7 "Checking article %d in %s for NoCeM (%d of %d)..."
214                      (mail-header-number h) group (incf i) len)
215                     (gnus-nocem-check-article group h)))))))
216         (setq gnus-nocem-active
217               (cons (list group gactive)
218                     (delq (assoc group gnus-nocem-active)
219                           gnus-nocem-active)))))
220     ;; Save the results, if any.
221     (gnus-nocem-save-cache)
222     (gnus-nocem-save-active)))
223
224 (defun gnus-nocem-check-article (group header)
225   "Check whether the current article is an NCM article and that we want it."
226   ;; Get the article.
227   (let ((date (mail-header-date header))
228         (gnus-newsgroup-name group)
229         issuer b e type)
230     (when (or (not date)
231               (time-less-p
232                (time-since (date-to-time date))
233                (days-to-time gnus-nocem-expiry-wait)))
234       (gnus-request-article-this-buffer (mail-header-number header) group)
235       (goto-char (point-min))
236       (when (re-search-forward "-----BEGIN PGP MESSAGE-----" nil t)
237         (delete-region (point-min) (match-beginning 0)))
238       (when (re-search-forward "-----END PGP MESSAGE-----\n?" nil t)
239         (delete-region (match-end 0) (point-max)))
240       (goto-char (point-min))
241       ;; The article has to have proper NoCeM headers.
242       (when (and (setq b (search-forward "\n@@BEGIN NCM HEADERS\n" nil t))
243                  (setq e (search-forward "\n@@BEGIN NCM BODY\n" nil t)))
244         ;; We get the name of the issuer.
245         (narrow-to-region b e)
246         (setq issuer (mail-fetch-field "issuer")
247               type (mail-fetch-field "issuer"))
248         (widen)
249         (if (not (gnus-nocem-message-wanted-p issuer type))
250             (message "invalid NoCeM issuer: %s" issuer)
251           (and (gnus-nocem-verify-issuer issuer) ; She is who she says she is.
252                (gnus-nocem-enter-article) ; We gobble the message.
253                (push (mail-header-message-id header) ; But don't come back for
254                      gnus-nocem-seen-message-ids))))))) ; second helpings.
255
256 (defun gnus-nocem-message-wanted-p (issuer type)
257   (let ((issuers gnus-nocem-issuers)
258         wanted conditions condition)
259     (cond
260      ;; Do the quick check first.
261      ((member issuer issuers)
262       t)
263      ((setq conditions (cdr (assoc issuer issuers)))
264       ;; Check whether we want this type.
265       (while (setq condition (pop conditions))
266         (cond
267          ((stringp condition)
268           (setq wanted (string-match condition type)))
269          ((and (consp condition)
270                (eq (car condition) 'not)
271                (stringp (cadr condition)))
272           (setq wanted (not (string-match (cadr condition) type))))
273          (t
274           (error "Invalid NoCeM condition: %S" condition))))
275       wanted))))
276
277 (defun gnus-nocem-verify-issuer (person)
278   "Verify using PGP that the canceler is who she says she is."
279   (if (fboundp gnus-nocem-verifyer)
280       (ignore-errors
281         (funcall gnus-nocem-verifyer))
282     ;; If we don't have Mailcrypt, then we use the message anyway.
283     t))
284
285 (defun gnus-nocem-enter-article ()
286   "Enter the current article into the NoCeM cache."
287   (goto-char (point-min))
288   (let ((b (search-forward "\n@@BEGIN NCM BODY\n" nil t))
289         (e (search-forward "\n@@END NCM BODY\n" nil t))
290         (buf (current-buffer))
291         ncm id group)
292     (when (and b e)
293       (narrow-to-region b (1+ (match-beginning 0)))
294       (goto-char (point-min))
295       (while (search-forward "\t" nil t)
296         (cond
297          ((not (ignore-errors
298                  (setq group (let ((obarray gnus-active-hashtb)) (read buf)))))
299           ;; An error.
300           )
301          ((not (symbolp group))
302           ;; Ignore invalid entries.
303           )
304          ((not (boundp group))
305           ;; Make sure all entries in the hashtb are bound.
306           (set group nil))
307          (t
308           (when (gnus-gethash (gnus-group-real-name (symbol-name group))
309                               gnus-nocem-real-group-hashtb)
310             ;; Valid group.
311             (beginning-of-line)
312             (while (eq (char-after) ?\t)
313               (forward-line -1))
314             (setq id (buffer-substring (point) (1- (search-forward "\t"))))
315             (unless (gnus-gethash id gnus-nocem-hashtb)
316               ;; only store if not already present
317               (gnus-sethash id t gnus-nocem-hashtb)
318               (push id ncm))
319             (forward-line 1)
320             (while (eq (char-after) ?\t)
321               (forward-line 1))))))
322       (when ncm
323         (setq gnus-nocem-touched-alist t)
324         (push (cons (let ((time (current-time))) (setcdr (cdr time) nil) time)
325                     ncm)
326               gnus-nocem-alist))
327       t)))
328
329 (defun gnus-nocem-load-cache ()
330   "Load the NoCeM cache."
331   (interactive)
332   (unless gnus-nocem-alist
333     ;; The buffer doesn't exist, so we create it and load the NoCeM
334     ;; cache.
335     (when (file-exists-p (gnus-nocem-cache-file))
336       (load (gnus-nocem-cache-file) t t t)
337       (gnus-nocem-alist-to-hashtb))))
338
339 (defun gnus-nocem-save-cache ()
340   "Save the NoCeM cache."
341   (when (and gnus-nocem-alist
342              gnus-nocem-touched-alist)
343     (with-temp-file (gnus-nocem-cache-file)
344       (gnus-prin1 `(setq gnus-nocem-alist ',gnus-nocem-alist)))
345     (setq gnus-nocem-touched-alist nil)))
346
347 (defun gnus-nocem-save-active ()
348   "Save the NoCeM active file."
349   (with-temp-file (gnus-nocem-active-file)
350     (gnus-prin1 `(setq gnus-nocem-active ',gnus-nocem-active))))
351
352 (defun gnus-nocem-alist-to-hashtb ()
353   "Create a hashtable from the Message-IDs we have."
354   (let* ((alist gnus-nocem-alist)
355          (pprev (cons nil alist))
356          (prev pprev)
357          (expiry (days-to-time gnus-nocem-expiry-wait))
358          entry)
359     (setq gnus-nocem-hashtb (gnus-make-hashtable (* (length alist) 51)))
360     (while (setq entry (car alist))
361       (if (not (time-less-p (time-since (car entry)) expiry))
362           ;; This entry has expired, so we remove it.
363           (setcdr prev (cdr alist))
364         (setq prev alist)
365         ;; This is ok, so we enter it into the hashtable.
366         (setq entry (cdr entry))
367         (while entry
368           (gnus-sethash (car entry) t gnus-nocem-hashtb)
369           (setq entry (cdr entry))))
370       (setq alist (cdr alist)))))
371
372 (gnus-add-shutdown 'gnus-nocem-close 'gnus)
373
374 (defun gnus-nocem-close ()
375   "Clear internal NoCeM variables."
376   (setq gnus-nocem-alist nil
377         gnus-nocem-hashtb nil
378         gnus-nocem-active nil
379         gnus-nocem-touched-alist nil
380         gnus-nocem-seen-message-ids nil
381         gnus-nocem-real-group-hashtb nil))
382
383 (defun gnus-nocem-unwanted-article-p (id)
384   "Say whether article ID in the current group is wanted."
385   (and gnus-nocem-hashtb
386        (gnus-gethash id gnus-nocem-hashtb)))
387
388 (provide 'gnus-nocem)
389
390 ;;; gnus-nocem.el ends here