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