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