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