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