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