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