Remove non-free old and crusty clearcase pkg
[packages] / xemacs-packages / gnus / lisp / gnus-nocem.el
1 ;;; gnus-nocem.el --- NoCeM pseudo-cancellation treatment
2
3 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;;   2005, 2006, 2007, 2008 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 3, 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 'pgg-verify
77   "*Function called to verify that the NoCeM message is valid.
78 One likely value is `pgg-verify'.  If the function in this variable
79 isn't bound, the message will be used unconditionally."
80   :group 'gnus-nocem
81   :type '(radio (function-item pgg-verify)
82                 (function-item mc-verify)
83                 (function :tag "other")))
84
85 (defcustom gnus-nocem-liberal-fetch nil
86   "*If t try to fetch all messages which have @@NCM in the subject.
87 Otherwise don't fetch messages which have references or whose message-id
88 matches a previously scanned and verified nocem message."
89   :group 'gnus-nocem
90   :type 'boolean)
91
92 (defcustom gnus-nocem-check-article-limit 500
93   "*If non-nil, the maximum number of articles to check in any NoCeM group."
94   :group 'gnus-nocem
95   :version "21.1"
96   :type '(choice (const :tag "unlimited" nil)
97                  (integer 1000)))
98
99 (defcustom gnus-nocem-check-from t
100   "Non-nil means check for valid issuers in message bodies.
101 Otherwise don't bother fetching articles unless their author matches a
102 valid issuer, which is much faster if you are selective about the issuers."
103   :group 'gnus-nocem
104   :version "21.1"
105   :type 'boolean)
106
107 ;;; Internal variables
108
109 (defvar gnus-nocem-active nil)
110 (defvar gnus-nocem-alist nil)
111 (defvar gnus-nocem-touched-alist nil)
112 (defvar gnus-nocem-hashtb nil)
113 (defvar gnus-nocem-seen-message-ids nil)
114
115 ;;; Functions
116
117 (defun gnus-nocem-active-file ()
118   (concat (file-name-as-directory gnus-nocem-directory) "active"))
119
120 (defun gnus-nocem-cache-file ()
121   (concat (file-name-as-directory gnus-nocem-directory) "cache"))
122
123 ;;
124 ;; faster lookups for group names:
125 ;;
126
127 (defvar gnus-nocem-real-group-hashtb nil
128   "Real-name mappings of subscribed groups.")
129
130 (defun gnus-fill-real-hashtb ()
131   "Fill up a hash table with the real-name mappings from the user's active file."
132   (setq gnus-nocem-real-group-hashtb (gnus-make-hashtable
133                                       (length gnus-newsrc-alist)))
134   (mapcar (lambda (group)
135             (setq group (gnus-group-real-name (car group)))
136             (gnus-sethash group t gnus-nocem-real-group-hashtb))
137           gnus-newsrc-alist))
138
139 ;;;###autoload
140 (defun gnus-nocem-scan-groups ()
141   "Scan all NoCeM groups for new NoCeM messages."
142   (interactive)
143   (let ((groups gnus-nocem-groups)
144         (gnus-inhibit-demon t)
145         group active gactive articles check-headers)
146     (gnus-make-directory gnus-nocem-directory)
147     ;; Load any previous NoCeM headers.
148     (gnus-nocem-load-cache)
149     ;; Get the group name mappings:
150     (gnus-fill-real-hashtb)
151     ;; Read the active file if it hasn't been read yet.
152     (and (file-exists-p (gnus-nocem-active-file))
153          (not gnus-nocem-active)
154          (ignore-errors
155            (load (gnus-nocem-active-file) t t t)))
156     ;; Go through all groups and see whether new articles have
157     ;; arrived.
158     (while (setq group (pop groups))
159       (if (not (setq gactive (gnus-activate-group group)))
160           ()                            ; This group doesn't exist.
161         (setq active (nth 1 (assoc group gnus-nocem-active)))
162         (when (and (not (< (cdr gactive) (car gactive))) ; Empty group.
163                    (or (not active)
164                        (< (cdr active) (cdr gactive))))
165           ;; Ok, there are new articles in this group, se we fetch the
166           ;; headers.
167           (save-excursion
168             (let ((dependencies (make-vector 10 nil))
169                   headers header)
170               (with-temp-buffer
171                 (setq headers
172                       (if (eq 'nov
173                               (gnus-retrieve-headers
174                                (setq articles
175                                      (gnus-uncompress-range
176                                       (cons
177                                        (if active (1+ (cdr active))
178                                          (car gactive))
179                                        (cdr gactive))))
180                                group))
181                           (gnus-get-newsgroup-headers-xover
182                            articles nil dependencies)
183                         (gnus-get-newsgroup-headers dependencies)))
184                 (while (setq header (pop headers))
185                   ;; We take a closer look on all articles that have
186                   ;; "@@NCM" in the subject.  Unless we already read
187                   ;; this cross posted message.  Nocem messages
188                   ;; are not allowed to have references, so we can
189                   ;; ignore scanning followups.
190                   (and (string-match "@@NCM" (mail-header-subject header))
191                        (and gnus-nocem-check-from
192                             (let ((case-fold-search t))
193                               (catch 'ok
194                                 (mapcar
195                                  (lambda (author)
196                                    (if (consp author)
197                                        (setq author (car author)))
198                                    (if (string-match
199                                         author (mail-header-from header))
200                                        (throw 'ok t)))
201                                  gnus-nocem-issuers)
202                                 nil)))
203                        (or gnus-nocem-liberal-fetch
204                            (and (or (string= "" (mail-header-references
205                                                  header))
206                                     (null (mail-header-references header)))
207                                 (not (member (mail-header-message-id header)
208                                              gnus-nocem-seen-message-ids))))
209                        (push header check-headers)))
210                 (setq check-headers (last (nreverse check-headers)
211                                           gnus-nocem-check-article-limit))
212                 (let ((i 0)
213                       (len (length check-headers)))
214                   (dolist (h check-headers)
215                     (gnus-message
216                      7 "Checking article %d in %s for NoCeM (%d of %d)..."
217                      (mail-header-number h) group (incf i) len)
218                     (gnus-nocem-check-article group h)))))))
219         (setq gnus-nocem-active
220               (cons (list group gactive)
221                     (delq (assoc group gnus-nocem-active)
222                           gnus-nocem-active)))))
223     ;; Save the results, if any.
224     (gnus-nocem-save-cache)
225     (gnus-nocem-save-active)))
226
227 (defun gnus-nocem-check-article (group header)
228   "Check whether the current article is an NCM article and that we want it."
229   ;; Get the article.
230   (let ((date (mail-header-date header))
231         (gnus-newsgroup-name group)
232         issuer b e type)
233     (when (or (not date)
234               (time-less-p
235                (time-since (date-to-time date))
236                (days-to-time gnus-nocem-expiry-wait)))
237       (gnus-request-article-this-buffer (mail-header-number header) group)
238       (goto-char (point-min))
239       (when (re-search-forward
240              "-----BEGIN PGP\\( SIGNED\\)? MESSAGE-----"
241              nil t)
242         (delete-region (point-min) (match-beginning 0)))
243       (when (re-search-forward
244              "-----END PGP \\(MESSAGE\\|SIGNATURE\\)-----\n?"
245              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 (let ((obarray gnus-nocem-real-group-hashtb))
308                                (read buf)))))
309           ;; An error.
310           )
311          ((not (symbolp group))
312           ;; Ignore invalid entries.
313           )
314          ((not (boundp group))
315           ;; Make sure all entries in the hashtb are bound.
316           (set group nil))
317          (t
318           (when (gnus-gethash (gnus-group-real-name (symbol-name group))
319                               gnus-nocem-real-group-hashtb)
320             ;; Valid group.
321             (beginning-of-line)
322             (while (eq (char-after) ?\t)
323               (forward-line -1))
324             (setq id (buffer-substring (point) (1- (search-forward "\t"))))
325             (unless (if gnus-nocem-hashtb
326                         (gnus-gethash id gnus-nocem-hashtb)
327                       (setq gnus-nocem-hashtb (gnus-make-hashtable))
328                       nil)
329               ;; only store if not already present
330               (gnus-sethash id t gnus-nocem-hashtb)
331               (push id ncm))
332             (forward-line 1)
333             (while (eq (char-after) ?\t)
334               (forward-line 1))))))
335       (when ncm
336         (setq gnus-nocem-touched-alist t)
337         (push (cons (let ((time (current-time))) (setcdr (cdr time) nil) time)
338                     ncm)
339               gnus-nocem-alist))
340       t)))
341
342 ;;;###autoload
343 (defun gnus-nocem-load-cache ()
344   "Load the NoCeM cache."
345   (interactive)
346   (unless gnus-nocem-alist
347     ;; The buffer doesn't exist, so we create it and load the NoCeM
348     ;; cache.
349     (when (file-exists-p (gnus-nocem-cache-file))
350       (load (gnus-nocem-cache-file) t t t)
351       (gnus-nocem-alist-to-hashtb))))
352
353 (defun gnus-nocem-save-cache ()
354   "Save the NoCeM cache."
355   (when (and gnus-nocem-alist
356              gnus-nocem-touched-alist)
357     (with-temp-file (gnus-nocem-cache-file)
358       (gnus-prin1 `(setq gnus-nocem-alist ',gnus-nocem-alist)))
359     (setq gnus-nocem-touched-alist nil)))
360
361 (defun gnus-nocem-save-active ()
362   "Save the NoCeM active file."
363   (with-temp-file (gnus-nocem-active-file)
364     (gnus-prin1 `(setq gnus-nocem-active ',gnus-nocem-active))))
365
366 (defun gnus-nocem-alist-to-hashtb ()
367   "Create a hashtable from the Message-IDs we have."
368   (let* ((alist gnus-nocem-alist)
369          (pprev (cons nil alist))
370          (prev pprev)
371          (expiry (days-to-time gnus-nocem-expiry-wait))
372          entry)
373     (setq gnus-nocem-hashtb (gnus-make-hashtable (* (length alist) 51)))
374     (while (setq entry (car alist))
375       (if (not (time-less-p (time-since (car entry)) expiry))
376           ;; This entry has expired, so we remove it.
377           (setcdr prev (cdr alist))
378         (setq prev alist)
379         ;; This is ok, so we enter it into the hashtable.
380         (setq entry (cdr entry))
381         (while entry
382           (gnus-sethash (car entry) t gnus-nocem-hashtb)
383           (setq entry (cdr entry))))
384       (setq alist (cdr alist)))))
385
386 (gnus-add-shutdown 'gnus-nocem-close 'gnus)
387
388 (defun gnus-nocem-close ()
389   "Clear internal NoCeM variables."
390   (setq gnus-nocem-alist nil
391         gnus-nocem-hashtb nil
392         gnus-nocem-active nil
393         gnus-nocem-touched-alist nil
394         gnus-nocem-seen-message-ids nil
395         gnus-nocem-real-group-hashtb nil))
396
397 (defun gnus-nocem-unwanted-article-p (id)
398   "Say whether article ID in the current group is wanted."
399   (and gnus-nocem-hashtb
400        (gnus-gethash id gnus-nocem-hashtb)))
401
402 (provide 'gnus-nocem)
403
404 ;;; arch-tag: 0e0c74ea-2f8e-4f3e-8fff-09f767c1adef
405 ;;; gnus-nocem.el ends here