*** 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-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   '("alt.nocem.misc" "news.admin.net-abuse.announce")
40   "List of groups that will be searched for NoCeM messages."
41   :group 'gnus-nocem
42   :type '(repeat (string :tag "Group")))
43
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)"
51    )
52   "List of NoCeM issuers to pay attention to."
53   :group 'gnus-nocem
54   :type '(repeat string))
55
56 (defcustom gnus-nocem-directory 
57   (nnheader-concat gnus-article-save-directory "NoCeM/")
58   "*Directory where NoCeM files will be stored."
59   :group 'gnus-nocem
60   :type 'directory)
61
62 (defcustom gnus-nocem-expiry-wait 15
63   "*Number of days to keep NoCeM headers in the cache."
64   :group 'gnus-nocem
65   :type 'integer)
66
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."
71   :group 'gnus-nocem
72   :type '(radio (function-item mc-verify)
73                 (function :tag "other")))
74
75 (defcustom gnus-nocem-liberal-fetch nil
76   "*If t try to fetch all messages which have @@NCM in the subject.
77 Otherwise don't fetch messages which have references or whose messsage-id
78 matches an previously scanned and verified nocem message."
79   :group 'gnus-nocem
80   :type 'boolean)
81
82 ;;; Internal variables
83
84 (defvar gnus-nocem-active nil)
85 (defvar gnus-nocem-alist nil)
86 (defvar gnus-nocem-touched-alist nil)
87 (defvar gnus-nocem-hashtb nil)
88 (defvar gnus-nocem-seen-message-ids nil)
89
90 ;;; Functions
91
92 (defun gnus-nocem-active-file ()
93   (concat (file-name-as-directory gnus-nocem-directory) "active"))
94
95 (defun gnus-nocem-cache-file ()
96   (concat (file-name-as-directory gnus-nocem-directory) "cache"))
97
98 (defun gnus-nocem-scan-groups ()
99   "Scan all NoCeM groups for new NoCeM messages."
100   (interactive)
101   (let ((groups gnus-nocem-groups)
102         group active gactive articles)
103     (gnus-make-directory gnus-nocem-directory)
104     ;; Load any previous NoCeM headers.
105     (gnus-nocem-load-cache)
106     ;; Read the active file if it hasn't been read yet.
107     (and (file-exists-p (gnus-nocem-active-file))
108          (not gnus-nocem-active)
109          (ignore-errors
110            (load (gnus-nocem-active-file) t t t)))
111     ;; Go through all groups and see whether new articles have
112     ;; arrived.  
113     (while (setq group (pop groups))
114       (if (not (setq gactive (gnus-activate-group group)))
115           ()                            ; This group doesn't exist.
116         (setq active (nth 1 (assoc group gnus-nocem-active)))
117         (when (and (not (< (cdr gactive) (car gactive))) ; Empty group.
118                    (or (not active)
119                        (< (cdr active) (cdr gactive))))
120           ;; Ok, there are new articles in this group, se we fetch the
121           ;; headers.
122           (save-excursion
123             (let ((dependencies (make-vector 10 nil))
124                   headers header)
125               (nnheader-temp-write nil
126                 (setq headers
127                       (if (eq 'nov
128                               (gnus-retrieve-headers 
129                                (setq articles
130                                      (gnus-uncompress-range
131                                       (cons 
132                                        (if active (1+ (cdr active))
133                                          (car gactive))
134                                        (cdr gactive))))
135                                group))
136                           (gnus-get-newsgroup-headers-xover 
137                            articles nil dependencies)
138                         (gnus-get-newsgroup-headers dependencies)))
139                 (while (setq header (pop headers))
140                   ;; We take a closer look on all articles that have
141                   ;; "@@NCM" in the subject.  Unless we already read
142                   ;; this cross posted message.  Nocem messages
143                   ;; are not allowed to have references, so we can
144                   ;; ignore scanning followups.
145                   (and (string-match "@@NCM" (mail-header-subject header))
146                        (or gnus-nocem-liberal-fetch
147                            (and (string= "" (mail-header-references header))
148                                 (not (member (mail-header-message-id header)
149                                              gnus-nocem-seen-message-ids))))
150                        (gnus-nocem-check-article group header)))))))
151         (setq gnus-nocem-active
152               (cons (list group gactive)
153                     (delq (assoc group gnus-nocem-active)
154                           gnus-nocem-active)))))
155     ;; Save the results, if any.
156     (gnus-nocem-save-cache)
157     (gnus-nocem-save-active)))
158
159 (defun gnus-nocem-check-article (group header)
160   "Check whether the current article is an NCM article and that we want it."
161   ;; Get the article.
162   (gnus-message 7 "Checking article %d in %s for NoCeM..."
163                 (mail-header-number header) group)
164   (let ((date (mail-header-date header))
165         issuer b e)
166     (when (or (not date)
167               (nnmail-time-less 
168                (nnmail-time-since (nnmail-date-to-time date))
169                (nnmail-days-to-time gnus-nocem-expiry-wait)))
170       (gnus-request-article-this-buffer (mail-header-number header) group)
171       (goto-char (point-min))
172       (when (re-search-forward "-----BEGIN PGP MESSAGE-----" nil t)
173         (delete-region (point-min) (match-beginning 0)))
174       (when (re-search-forward "-----END PGP MESSAGE-----\n?" nil t)
175         (delete-region (match-end 0) (point-max)))
176       (goto-char (point-min))
177       ;; The article has to have proper NoCeM headers.
178       (when (and (setq b (search-forward "\n@@BEGIN NCM HEADERS\n" nil t))
179                  (setq e (search-forward "\n@@BEGIN NCM BODY\n" nil t)))
180         ;; We get the name of the issuer.
181         (narrow-to-region b e)
182         (setq issuer (mail-fetch-field "issuer"))
183         (widen)
184         (and (member issuer gnus-nocem-issuers) ; We like her....
185              (gnus-nocem-verify-issuer issuer) ; She is who she says she is...
186              (gnus-nocem-enter-article) ; We gobble the message..
187              (push (mail-header-message-id header) ; But don't come back for
188                    gnus-nocem-seen-message-ids)))))) ; second helpings.
189
190   
191 (defun gnus-nocem-verify-issuer (person)
192   "Verify using PGP that the canceler is who she says she is."
193   (if (fboundp gnus-nocem-verifyer)
194       (funcall gnus-nocem-verifyer)
195     ;; If we don't have Mailcrypt, then we use the message anyway.
196     t))
197
198 (defun gnus-nocem-enter-article ()
199   "Enter the current article into the NoCeM cache."
200   (goto-char (point-min))
201   (let ((b (search-forward "\n@@BEGIN NCM BODY\n" nil t))
202         (e (search-forward "\n@@END NCM BODY\n" nil t))
203         (buf (current-buffer))
204         ncm id group)
205     (when (and b e)
206       (narrow-to-region b (1+ (match-beginning 0)))
207       (goto-char (point-min))
208       (while (search-forward "\t" nil t)
209         (if (ignore-errors
210               (setq group (let ((obarray gnus-active-hashtb)) (read buf))))
211             (if (not (boundp group))
212                 ;; Make sure all entries in the hashtb are bound.
213                 (set group nil)
214               (when (gnus-gethash (symbol-name group) gnus-newsrc-hashtb)
215                 ;; Valid group.
216                 (beginning-of-line)
217                 (while (= (following-char) ?\t)
218                   (forward-line -1))
219                 (setq id (buffer-substring (point) (1- (search-forward "\t"))))
220                 (unless (gnus-gethash id gnus-nocem-hashtb)
221                   ;; only store if not already present
222                   (gnus-sethash id t gnus-nocem-hashtb)
223                   (push id ncm))
224                 (forward-line 1)
225                 (while (= (following-char) ?\t)
226                   (forward-line 1))))))
227       (when ncm
228         (setq gnus-nocem-touched-alist t)
229         (push (cons (let ((time (current-time))) (setcdr (cdr time) nil) time)
230                     ncm)
231               gnus-nocem-alist))
232       t)))
233
234 (defun gnus-nocem-load-cache ()
235   "Load the NoCeM cache."
236   (unless gnus-nocem-alist
237     ;; The buffer doesn't exist, so we create it and load the NoCeM
238     ;; cache.  
239     (when (file-exists-p (gnus-nocem-cache-file))
240       (load (gnus-nocem-cache-file) t t t)
241       (gnus-nocem-alist-to-hashtb))))
242       
243 (defun gnus-nocem-save-cache ()
244   "Save the NoCeM cache."
245   (when (and gnus-nocem-alist
246              gnus-nocem-touched-alist)
247     (nnheader-temp-write (gnus-nocem-cache-file)
248       (gnus-prin1 `(setq gnus-nocem-alist ',gnus-nocem-alist)))
249     (setq gnus-nocem-touched-alist nil)))
250
251 (defun gnus-nocem-save-active ()
252   "Save the NoCeM active file."
253   (nnheader-temp-write (gnus-nocem-active-file)
254     (gnus-prin1 `(setq gnus-nocem-active ',gnus-nocem-active))))
255
256 (defun gnus-nocem-alist-to-hashtb ()
257   "Create a hashtable from the Message-IDs we have."
258   (let* ((alist gnus-nocem-alist)
259          (pprev (cons nil alist))
260          (prev pprev)
261          (expiry (nnmail-days-to-time gnus-nocem-expiry-wait))
262          entry)
263     (setq gnus-nocem-hashtb (gnus-make-hashtable (* (length alist) 51)))
264     (while (setq entry (car alist))
265       (if (not (nnmail-time-less (nnmail-time-since (car entry)) expiry))
266           ;; This entry has expired, so we remove it.
267           (setcdr prev (cdr alist))
268         (setq prev alist)
269         ;; This is ok, so we enter it into the hashtable.
270         (setq entry (cdr entry))
271         (while entry
272           (gnus-sethash (car entry) t gnus-nocem-hashtb)
273           (setq entry (cdr entry))))
274       (setq alist (cdr alist)))))
275
276 (gnus-add-shutdown 'gnus-nocem-close 'gnus)
277
278 (defun gnus-nocem-close ()
279   "Clear internal NoCeM variables."
280   (setq gnus-nocem-alist nil
281         gnus-nocem-hashtb nil
282         gnus-nocem-active nil
283         gnus-nocem-touched-alist nil
284         gnus-nocem-seen-message-ids nil))
285
286 (defun gnus-nocem-unwanted-article-p (id)
287   "Say whether article ID in the current group is wanted."
288   (gnus-gethash id gnus-nocem-hashtb))
289
290 (provide 'gnus-nocem)
291
292 ;;; gnus-nocem.el ends here