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