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