*** 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 (eval-when-compile (require 'cl))
31
32 (defvar gnus-nocem-groups 
33   '("alt.nocem.misc" "news.admin.net-abuse.announce")
34   "*List of groups that will be searched for NoCeM messages.")
35
36 (defvar gnus-nocem-issuers 
37   '("Automoose-1" ; The CancelMoose[tm] on autopilot.
38     "clewis@ferret.ocunix.on.ca;" ; Chris Lewis -- Canadian angel & despammer.
39     "jem@xpat.com;"  ; Jem -- Korean despammer.
40     "red@redpoll.mrfs.oh.us (Richard E. Depew)" ; Spew/bincancel guy.
41     )
42   "*List of NoCeM issuers to pay attention to.")
43
44 (defvar gnus-nocem-directory 
45   (concat (file-name-as-directory gnus-article-save-directory) "NoCeM/")
46   "*Directory where NoCeM files will be stored.")
47
48 (defvar gnus-nocem-expiry-wait 15
49   "*Number of days to keep NoCeM headers in the cache.")
50
51 ;;; Internal variables
52
53 (defvar gnus-nocem-active nil)
54 (defvar gnus-nocem-alist nil)
55 (defvar gnus-nocem-touched-alist nil)
56 (defvar gnus-nocem-hashtb nil)
57
58 ;;; Functions
59
60 (defun gnus-nocem-active-file ()
61   (concat (file-name-as-directory gnus-nocem-directory) "active"))
62
63 (defun gnus-nocem-cache-file ()
64   (concat (file-name-as-directory gnus-nocem-directory) "cache"))
65
66 (defun gnus-nocem-scan-groups ()
67   "Scan all NoCeM groups for new NoCeM messages."
68   (interactive)
69   (let ((groups gnus-nocem-groups)
70         group active gactive articles)
71     (or (file-exists-p gnus-nocem-directory)
72         (make-directory gnus-nocem-directory t))
73     ;; Load any previous NoCeM headers.
74     (gnus-nocem-load-cache)
75     ;; Read the active file if it hasn't been read yet.
76     (and (file-exists-p (gnus-nocem-active-file))
77          (not gnus-nocem-active)
78          (condition-case ()
79              (load (gnus-nocem-active-file) t t t)
80            (error nil)))
81     ;; Go through all groups and see whether new articles have
82     ;; arrived.  
83     (while (setq group (pop groups))
84       (if (not (setq gactive (gnus-activate-group group)))
85           () ; This group doesn't exist.
86         (setq active (nth 1 (assoc group gnus-nocem-active)))
87         (when (and (not (< (cdr gactive) (car gactive))) ; Empty group.
88                    (or (not active)
89                        (< (cdr active) 
90                           (cdr (setq gactive (gnus-gethash 
91                                               group gnus-newsrc-hashtb))))))
92           ;; Ok, there are new articles in this group, se we fetch the
93           ;; headers.
94           (let ((gnus-newsgroup-dependencies (make-vector 10 nil))
95                 headers)
96             (setq headers
97                   (if (eq 'nov
98                           (gnus-retrieve-headers 
99                            (setq articles
100                                  (gnus-uncompress-range
101                                   (cons 
102                                    (if active (1+ (cdr active)) (car gactive))
103                                    (cdr gactive))))
104                            group))
105                       (gnus-get-newsgroup-headers-xover articles)
106                     (gnus-get-newsgroup-headers)))
107             (while headers
108               ;; We take a closer look on all articles that have
109               ;; "@@NCM" in the subject.  
110               (when (string-match "@@NCM" (mail-header-subject (car headers)))
111                 (gnus-nocem-check-article
112                  (mail-header-number (car headers)) group))
113               (setq headers (cdr headers)))))))
114     ;; Save the results, if any.
115     (gnus-nocem-save-cache)))
116
117 (defun gnus-nocem-check-article (number group)
118   "Check whether the current article is an NCM article and that we want it."
119   (nnheader-temp-write nil
120     ;; Get the article.
121     (gnus-request-article-this-buffer number group)
122     (nnheader-narrow-to-headers)
123     (let ((date (mail-fetch-field "date"))
124           issuer b)
125       (widen)
126       ;; The article has to have proper NoCeM headers.
127       (when (and (setq b (search-forward "\n@@BEGIN NCM HEADERS\n" nil t))
128                  (search-forward "\n@@BEGIN NCM BODY\n" nil t)
129                  (or (not date)
130                      (nnmail-time-less 
131                       (nnmail-time-since (nnmail-date-to-time date))
132                       (nnmail-days-to-time gnus-nocem-expiry-wait))))
133         ;; We get the name of the issuer.
134         (narrow-to-region b (match-beginning 0))
135         (setq issuer (mail-fetch-field "issuer"))
136         (and (member issuer gnus-nocem-issuers) ; We like her...
137              (gnus-nocem-verify-issuer issuer) ; She is who she says she is...
138              (gnus-nocem-enter-article)))))) ; We gobble the message.
139
140 (defun gnus-nocem-verify-issuer (person)
141   "Verify using PGP that the canceler is who she says she is."
142   t)
143
144 (defun gnus-nocem-enter-article ()
145   "Enter the current article into the NoCeM cache."
146   (widen)
147   (goto-char (point-min))
148   (let ((b (search-forward "\n@@BEGIN NCM BODY\n" nil t))
149         (e (search-forward "\n@@END NCM BODY\n" nil t))
150         (buf (current-buffer))
151         ncm id)
152     (when (and b e)
153       (narrow-to-region b (1+ (match-beginning 0)))
154       (goto-char (point-min))
155       (while (search-forward "\t" nil t)
156         (when (boundp (let ((obarray gnus-newsrc-hashtb)) (read buf)))
157           (beginning-of-line)
158           (while (= (following-char) ?\t)
159             (forward-line -1))
160           (setq id (buffer-substring (point) (1- (search-forward "\t"))))
161           (push id ncm)
162           (gnus-sethash id t gnus-nocem-hashtb)
163           (forward-line 1)
164           (while (= (following-char) ?\t)
165             (forward-line 1))))
166       (when ncm
167         (setq gnus-nocem-touched-alist t)
168         (push (cons (let ((time (current-time))) (setcdr (cdr time) nil) time)
169                     ncm) 
170               gnus-nocem-alist)))))
171
172 (defun gnus-nocem-load-cache ()
173   "Load the NoCeM cache."
174   (unless gnus-nocem-alist
175     ;; The buffer doesn't exist, so we create it and load the NoCeM
176     ;; cache.  
177     (when (file-exists-p (gnus-nocem-cache-file))
178       (load (gnus-nocem-cache-file) t t t)
179       (gnus-nocem-alist-to-hashtb))))
180       
181 (defun gnus-nocem-save-cache ()
182   "Save the NoCeM cache."
183   (when (and gnus-nocem-alist
184              gnus-nocem-touched-alist)
185     (nnheader-temp-write (gnus-nocem-cache-file)
186       (insert (prin1-to-string
187                (list 'setq 'gnus-nocem-alist gnus-nocem-alist))))
188     (setq gnus-nocem-touched-alist nil)))
189
190 (defun gnus-nocem-alist-to-hashtb ()
191   "Create a hashtable from the Message-IDs we have."
192   (let* ((alist gnus-nocem-alist)
193          (pprev (cons nil alist))
194          (prev pprev)
195          (expiry (nnmail-days-to-time gnus-nocem-expiry-wait))
196          entry)
197     (setq gnus-nocem-hashtb (gnus-make-hashtable (* (length alist) 51)))
198     (while (setq entry (car alist))
199       (if (not (nnmail-time-less (nnmail-time-since (car entry)) expiry))
200           ;; This entry has expired, so we remove it.
201           (setcdr prev (cdr alist))
202         (setq prev alist)
203         ;; This is ok, so we enter it into the hashtable.
204         (setq entry (cdr entry))
205         (while entry
206           (gnus-sethash (car entry) t gnus-nocem-hashtb)
207           (setq entry (cdr entry))))
208       (setq alist (cdr alist)))))
209
210 (defun gnus-nocem-close ()
211   "Clear internal NoCeM variables."
212   (setq gnus-nocem-alist nil
213         gnus-nocem-hashtb nil
214         gnus-nocem-active nil
215         gnus-nocem-touched-alist nil))
216
217 (defun gnus-nocem-unwanted-article-p (id)
218   "Say whether article ID in the current group is wanted."
219   (gnus-gethash id gnus-nocem-hashtb))
220
221 (provide 'gnus-nocem)
222
223 ;;; gnus-nocem.el ends here