1 ;;; gnus-nocem.el --- NoCeM pseudo-cancellation treatment
2 ;; Copyright (C) 1995,96 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
7 ;; This file is part of GNU Emacs.
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)
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.
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.
30 (eval-when-compile (require 'cl))
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.")
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;" ; John Milburn -- despammer in Korea.
40 "red@redpoll.mrfs.oh.us (Richard E. Depew)" ; Spew/bincancel guy.
42 "*List of NoCeM issuers to pay attention to.")
44 (defvar gnus-nocem-directory
45 (concat (file-name-as-directory gnus-article-save-directory) "NoCeM/")
46 "*Directory where NoCeM files will be stored.")
48 (defvar gnus-nocem-expiry-wait 15
49 "*Number of days to keep NoCeM headers in the cache.")
51 ;;; Internal variables
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)
60 (defun gnus-nocem-active-file ()
61 (concat (file-name-as-directory gnus-nocem-directory) "active"))
63 (defun gnus-nocem-cache-file ()
64 (concat (file-name-as-directory gnus-nocem-directory) "cache"))
66 (defun gnus-nocem-scan-groups ()
67 "Scan all NoCeM groups for new NoCeM messages."
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)
79 (load (gnus-nocem-active-file) t t t)
81 ;; Go through all groups and see whether new articles have
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.
89 (< (cdr active) (cdr gactive))))
90 ;; Ok, there are new articles in this group, se we fetch the
93 (let ((dependencies (make-vector 10 nil))
94 (buffer (nnheader-set-temp-buffer " *Gnus NoCeM*"))
98 (gnus-retrieve-headers
100 (gnus-uncompress-range
102 (if active (1+ (cdr active))
106 (gnus-get-newsgroup-headers-xover
107 articles nil dependencies)
108 (gnus-get-newsgroup-headers dependencies)))
110 ;; We take a closer look on all articles that have
111 ;; "@@NCM" in the subject.
112 (when (string-match "@@NCM"
113 (mail-header-subject (car headers)))
114 (gnus-nocem-check-article group (car headers)))
115 (setq headers (cdr headers)))
116 (kill-buffer (current-buffer)))))
117 (setq gnus-nocem-active
118 (cons (list group gactive)
119 (delq (assoc group gnus-nocem-active)
120 gnus-nocem-active)))))
121 ;; Save the results, if any.
122 (gnus-nocem-save-cache)
123 (gnus-nocem-save-active)))
125 (defun gnus-nocem-check-article (group header)
126 "Check whether the current article is an NCM article and that we want it."
128 (gnus-message 7 "Checking article %d in %s for NoCeM..."
129 (mail-header-number header) group)
130 (let ((date (mail-header-date header))
134 (nnmail-time-since (nnmail-date-to-time date))
135 (nnmail-days-to-time gnus-nocem-expiry-wait)))
136 (gnus-request-article-this-buffer (mail-header-number header) group)
137 ;; The article has to have proper NoCeM headers.
138 (when (and (setq b (search-forward "\n@@BEGIN NCM HEADERS\n" nil t))
139 (setq e (search-forward "\n@@BEGIN NCM BODY\n" nil t)))
140 ;; We get the name of the issuer.
141 (narrow-to-region b e)
142 (setq issuer (mail-fetch-field "issuer"))
143 (and (member issuer gnus-nocem-issuers) ; We like her...
144 (gnus-nocem-verify-issuer issuer) ; She is who she says she is..
145 (gnus-nocem-enter-article)))))) ; We gobble the message.
147 (defun gnus-nocem-verify-issuer (person)
148 "Verify using PGP that the canceler is who she says she is."
151 (defun gnus-nocem-enter-article ()
152 "Enter the current article into the NoCeM cache."
154 (goto-char (point-min))
155 (let ((b (search-forward "\n@@BEGIN NCM BODY\n" nil t))
156 (e (search-forward "\n@@END NCM BODY\n" nil t))
157 (buf (current-buffer))
160 (narrow-to-region b (1+ (match-beginning 0)))
161 (goto-char (point-min))
162 (while (search-forward "\t" nil t)
163 (when (condition-case nil
164 (boundp (let ((obarray gnus-active-hashtb)) (read buf)))
167 (while (= (following-char) ?\t)
169 (setq id (buffer-substring (point) (1- (search-forward "\t"))))
171 (gnus-sethash id t gnus-nocem-hashtb)
173 (while (= (following-char) ?\t)
176 (setq gnus-nocem-touched-alist t)
177 (push (cons (let ((time (current-time))) (setcdr (cdr time) nil) time)
179 gnus-nocem-alist)))))
181 (defun gnus-nocem-load-cache ()
182 "Load the NoCeM cache."
183 (unless gnus-nocem-alist
184 ;; The buffer doesn't exist, so we create it and load the NoCeM
186 (when (file-exists-p (gnus-nocem-cache-file))
187 (load (gnus-nocem-cache-file) t t t)
188 (gnus-nocem-alist-to-hashtb))))
190 (defun gnus-nocem-save-cache ()
191 "Save the NoCeM cache."
192 (when (and gnus-nocem-alist
193 gnus-nocem-touched-alist)
194 (nnheader-temp-write (gnus-nocem-cache-file)
195 (prin1 `(setq gnus-nocem-alist ',gnus-nocem-alist) (current-buffer)))
196 (setq gnus-nocem-touched-alist nil)))
198 (defun gnus-nocem-save-active ()
199 "Save the NoCeM active file."
200 (nnheader-temp-write (gnus-nocem-active-file)
201 (prin1 `(setq gnus-nocem-active ',gnus-nocem-active) (current-buffer))))
203 (defun gnus-nocem-alist-to-hashtb ()
204 "Create a hashtable from the Message-IDs we have."
205 (let* ((alist gnus-nocem-alist)
206 (pprev (cons nil alist))
208 (expiry (nnmail-days-to-time gnus-nocem-expiry-wait))
210 (setq gnus-nocem-hashtb (gnus-make-hashtable (* (length alist) 51)))
211 (while (setq entry (car alist))
212 (if (not (nnmail-time-less (nnmail-time-since (car entry)) expiry))
213 ;; This entry has expired, so we remove it.
214 (setcdr prev (cdr alist))
216 ;; This is ok, so we enter it into the hashtable.
217 (setq entry (cdr entry))
219 (gnus-sethash (car entry) t gnus-nocem-hashtb)
220 (setq entry (cdr entry))))
221 (setq alist (cdr alist)))))
223 (gnus-add-shutdown 'gnus-nocem-close 'gnus)
225 (defun gnus-nocem-close ()
226 "Clear internal NoCeM variables."
227 (setq gnus-nocem-alist nil
228 gnus-nocem-hashtb nil
229 gnus-nocem-active nil
230 gnus-nocem-touched-alist nil))
232 (defun gnus-nocem-unwanted-article-p (id)
233 "Say whether article ID in the current group is wanted."
234 (gnus-gethash id gnus-nocem-hashtb))
236 (provide 'gnus-nocem)
238 ;;; gnus-nocem.el ends here