Trailing whitepace deleted.
[emacs.git] / lisp / gnus / nnkiboze.el
blob58157c0f3ae9b3f09375279151c869bbb7b9cb15
1 ;;; nnkiboze.el --- select virtual news access for Gnus
3 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000
4 ;; Free Software Foundation, Inc.
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; Keywords: news
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
26 ;;; Commentary:
28 ;; The other access methods (nntp, nnspool, etc) are general news
29 ;; access methods. This module relies on Gnus and can't be used
30 ;; separately.
32 ;;; Code:
34 (require 'nntp)
35 (require 'nnheader)
36 (require 'gnus)
37 (require 'gnus-score)
38 (require 'nnoo)
39 (require 'mm-util)
40 (eval-when-compile (require 'cl))
42 (nnoo-declare nnkiboze)
43 (defvoo nnkiboze-directory (nnheader-concat gnus-directory "kiboze/")
44 "nnkiboze will put its files in this directory.")
46 (defvoo nnkiboze-level 9
47 "The maximum level to be searched for articles.")
49 (defvoo nnkiboze-remove-read-articles t
50 "If non-nil, nnkiboze will remove read articles from the kiboze group.")
52 (defvoo nnkiboze-ephemeral nil
53 "If non-nil, don't store any data anywhere.")
55 (defvoo nnkiboze-scores nil
56 "Score rules for generating the nnkiboze group.")
58 (defvoo nnkiboze-regexp nil
59 "Regexp for matching component groups.")
61 (defvoo nnkiboze-file-coding-system mm-text-coding-system
62 "Coding system for nnkiboze files.")
66 (defconst nnkiboze-version "nnkiboze 1.0")
68 (defvoo nnkiboze-current-group nil)
69 (defvoo nnkiboze-status-string "")
71 (defvoo nnkiboze-headers nil)
75 ;;; Interface functions.
77 (nnoo-define-basics nnkiboze)
79 (deffoo nnkiboze-retrieve-headers (articles &optional group server fetch-old)
80 (nnkiboze-possibly-change-group group)
81 (unless gnus-nov-is-evil
82 (if (stringp (car articles))
83 'headers
84 (let ((nov (nnkiboze-nov-file-name)))
85 (when (file-exists-p nov)
86 (save-excursion
87 (set-buffer nntp-server-buffer)
88 (erase-buffer)
89 (let ((nnheader-file-coding-system nnkiboze-file-coding-system))
90 (nnheader-insert-file-contents nov))
91 (nnheader-nov-delete-outside-range
92 (car articles) (car (last articles)))
93 'nov))))))
95 (deffoo nnkiboze-request-article (article &optional newsgroup server buffer)
96 (nnkiboze-possibly-change-group newsgroup)
97 (if (not (numberp article))
98 ;; This is a real kludge. It might not work at times, but it
99 ;; does no harm I think. The only alternative is to offer no
100 ;; article fetching by message-id at all.
101 (nntp-request-article article newsgroup gnus-nntp-server buffer)
102 (let* ((header (gnus-summary-article-header article))
103 (xref (mail-header-xref header))
104 num group)
105 (unless xref
106 (error "nnkiboze: No xref"))
107 (unless (string-match " \\([^ ]+\\):\\([0-9]+\\)" xref)
108 (error "nnkiboze: Malformed xref"))
109 (setq num (string-to-int (match-string 2 xref))
110 group (match-string 1 xref))
111 (or (with-current-buffer buffer
112 (gnus-cache-request-article num group))
113 (gnus-request-article num group buffer)))))
115 (deffoo nnkiboze-request-scan (&optional group server)
116 (nnkiboze-generate-group (concat "nnkiboze:" group)))
118 (deffoo nnkiboze-request-group (group &optional server dont-check)
119 "Make GROUP the current newsgroup."
120 (nnkiboze-possibly-change-group group)
121 (if dont-check
123 (let ((nov-file (nnkiboze-nov-file-name))
124 beg end total)
125 (save-excursion
126 (set-buffer nntp-server-buffer)
127 (erase-buffer)
128 (unless (file-exists-p nov-file)
129 (nnkiboze-request-scan group))
130 (if (not (file-exists-p nov-file))
131 (nnheader-report 'nnkiboze "Can't select group %s" group)
132 (let ((nnheader-file-coding-system nnkiboze-file-coding-system))
133 (nnheader-insert-file-contents nov-file))
134 (if (zerop (buffer-size))
135 (nnheader-insert "211 0 0 0 %s\n" group)
136 (goto-char (point-min))
137 (when (looking-at "[0-9]+")
138 (setq beg (read (current-buffer))))
139 (goto-char (point-max))
140 (when (re-search-backward "^[0-9]" nil t)
141 (setq end (read (current-buffer))))
142 (setq total (count-lines (point-min) (point-max)))
143 (nnheader-insert "211 %d %d %d %s\n" total beg end group)))))))
145 (deffoo nnkiboze-close-group (group &optional server)
146 (nnkiboze-possibly-change-group group)
147 ;; Remove NOV lines of articles that are marked as read.
148 (when (and (file-exists-p (nnkiboze-nov-file-name))
149 nnkiboze-remove-read-articles)
150 (let ((coding-system-for-write nnkiboze-file-coding-system))
151 (with-temp-file (nnkiboze-nov-file-name)
152 (let ((cur (current-buffer))
153 (nnheader-file-coding-system nnkiboze-file-coding-system))
154 (nnheader-insert-file-contents (nnkiboze-nov-file-name))
155 (goto-char (point-min))
156 (while (not (eobp))
157 (if (not (gnus-article-read-p (read cur)))
158 (forward-line 1)
159 (gnus-delete-line))))))
160 (setq nnkiboze-current-group nil)))
162 (deffoo nnkiboze-open-server (server &optional defs)
163 (unless (assq 'nnkiboze-regexp defs)
164 (push `(nnkiboze-regexp ,server)
165 defs))
166 (nnoo-change-server 'nnkiboze server defs))
168 (deffoo nnkiboze-request-delete-group (group &optional force server)
169 (nnkiboze-possibly-change-group group)
170 (when force
171 (let ((files (nconc
172 (nnkiboze-score-file group)
173 (list (nnkiboze-nov-file-name)
174 (nnkiboze-nov-file-name ".newsrc")))))
175 (while files
176 (and (file-exists-p (car files))
177 (file-writable-p (car files))
178 (delete-file (car files)))
179 (setq files (cdr files)))))
180 (setq nnkiboze-current-group nil)
183 (nnoo-define-skeleton nnkiboze)
186 ;;; Internal functions.
188 (defun nnkiboze-possibly-change-group (group)
189 (setq nnkiboze-current-group group))
191 (defun nnkiboze-prefixed-name (group)
192 (gnus-group-prefixed-name group '(nnkiboze "")))
194 ;;;###autoload
195 (defun nnkiboze-generate-groups ()
196 "\"Usage: emacs -batch -l nnkiboze -f nnkiboze-generate-groups\".
197 Finds out what articles are to be part of the nnkiboze groups."
198 (interactive)
199 (let ((nnmail-spool-file nil)
200 (mail-sources nil)
201 (gnus-use-dribble-file nil)
202 (gnus-read-active-file t)
203 (gnus-expert-user t))
204 (gnus))
205 (let* ((gnus-newsrc-alist (gnus-copy-sequence gnus-newsrc-alist))
206 (newsrc (cdr gnus-newsrc-alist))
207 gnus-newsrc-hashtb info)
208 (gnus-make-hashtable-from-newsrc-alist)
209 ;; We have copied all the newsrc alist info over to local copies
210 ;; so that we can mess all we want with these lists.
211 (while (setq info (pop newsrc))
212 (when (string-match "nnkiboze" (gnus-info-group info))
213 ;; For each kiboze group, we call this function to generate
214 ;; it.
215 (nnkiboze-generate-group (gnus-info-group info) t))))
216 (save-excursion
217 (set-buffer gnus-group-buffer)
218 (gnus-group-list-groups)))
220 (defun nnkiboze-score-file (group)
221 (list (expand-file-name
222 (concat (file-name-as-directory gnus-kill-files-directory)
223 (nnheader-translate-file-chars
224 (concat (nnkiboze-prefixed-name nnkiboze-current-group)
225 "." gnus-score-file-suffix))))))
227 (defun nnkiboze-generate-group (group &optional inhibit-list-groups)
228 (let* ((info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
229 (newsrc-file (concat nnkiboze-directory
230 (nnheader-translate-file-chars
231 (concat group ".newsrc"))))
232 (nov-file (concat nnkiboze-directory
233 (nnheader-translate-file-chars
234 (concat group ".nov"))))
235 method nnkiboze-newsrc gname newsrc active
236 ginfo lowest glevel orig-info nov-buffer
237 ;; Bind various things to nil to make group entry faster.
238 (gnus-expert-user t)
239 (gnus-large-newsgroup nil)
240 (gnus-score-find-score-files-function 'nnkiboze-score-file)
241 ;; Use only nnkiboze-score-file!
242 (gnus-score-use-all-scores nil)
243 (gnus-use-scoring t)
244 (gnus-verbose (min gnus-verbose 3))
245 gnus-select-group-hook gnus-summary-prepare-hook
246 gnus-thread-sort-functions gnus-show-threads
247 gnus-visual gnus-suppress-duplicates num-unread)
248 (unless info
249 (error "No such group: %s" group))
250 ;; Load the kiboze newsrc file for this group.
251 (when (file-exists-p newsrc-file)
252 (load newsrc-file))
253 (let ((coding-system-for-write nnkiboze-file-coding-system))
254 (with-temp-file nov-file
255 (when (file-exists-p nov-file)
256 (insert-file-contents nov-file))
257 (setq nov-buffer (current-buffer))
258 ;; Go through the active hashtb and add new all groups that match the
259 ;; kiboze regexp.
260 (mapatoms
261 (lambda (group)
262 (and (string-match nnkiboze-regexp
263 (setq gname (symbol-name group))) ; Match
264 (not (assoc gname nnkiboze-newsrc)) ; It isn't registered
265 (numberp (car (symbol-value group))) ; It is active
266 (or (> nnkiboze-level 7)
267 (and (setq glevel (nth 1 (nth 2 (gnus-gethash
268 gname gnus-newsrc-hashtb))))
269 (>= nnkiboze-level glevel)))
270 (not (string-match "^nnkiboze:" gname)) ; Exclude kibozes
271 (push (cons gname (1- (car (symbol-value group))))
272 nnkiboze-newsrc)))
273 gnus-active-hashtb)
274 ;; `newsrc' is set to the list of groups that possibly are
275 ;; component groups to this kiboze group. This list has elements
276 ;; on the form `(GROUP . NUMBER)', where NUMBER is the highest
277 ;; number that has been kibozed in GROUP in this kiboze group.
278 (setq newsrc nnkiboze-newsrc)
279 (while newsrc
280 (if (not (setq active (gnus-gethash
281 (caar newsrc) gnus-active-hashtb)))
282 ;; This group isn't active after all, so we remove it from
283 ;; the list of component groups.
284 (setq nnkiboze-newsrc (delq (car newsrc) nnkiboze-newsrc))
285 (setq lowest (cdar newsrc))
286 ;; Ok, we have a valid component group, so we jump to it.
287 (switch-to-buffer gnus-group-buffer)
288 (gnus-group-jump-to-group (caar newsrc))
289 (gnus-message 3 "nnkiboze: Checking %s..." (caar newsrc))
290 (setq ginfo (gnus-get-info (gnus-group-group-name))
291 orig-info (gnus-copy-sequence ginfo)
292 num-unread (car (gnus-gethash (caar newsrc)
293 gnus-newsrc-hashtb)))
294 (unwind-protect
295 (progn
296 ;; We set all list of article marks to nil. Since we operate
297 ;; on copies of the real lists, we can destroy anything we
298 ;; want here.
299 (when (nth 3 ginfo)
300 (setcar (nthcdr 3 ginfo) nil))
301 ;; We set the list of read articles to be what we expect for
302 ;; this kiboze group -- either nil or `(1 . LOWEST)'.
303 (when ginfo
304 (setcar (nthcdr 2 ginfo)
305 (and (not (= lowest 1)) (cons 1 lowest))))
306 (when (and (or (not ginfo)
307 (> (length (gnus-list-of-unread-articles
308 (car ginfo)))
310 (progn
311 (ignore-errors
312 (gnus-group-select-group nil))
313 (eq major-mode 'gnus-summary-mode)))
314 ;; We are now in the group where we want to be.
315 (setq method (gnus-find-method-for-group
316 gnus-newsgroup-name))
317 (when (eq method gnus-select-method)
318 (setq method nil))
319 ;; We go through the list of scored articles.
320 (while gnus-newsgroup-scored
321 (when (> (caar gnus-newsgroup-scored) lowest)
322 ;; If it has a good score, then we enter this article
323 ;; into the kiboze group.
324 (nnkiboze-enter-nov
325 nov-buffer
326 (gnus-summary-article-header
327 (caar gnus-newsgroup-scored))
328 gnus-newsgroup-name))
329 (setq gnus-newsgroup-scored (cdr gnus-newsgroup-scored)))
330 ;; That's it. We exit this group.
331 (when (eq major-mode 'gnus-summary-mode)
332 (kill-buffer (current-buffer)))))
333 ;; Restore the proper info.
334 (when ginfo
335 (setcdr ginfo (cdr orig-info)))
336 (setcar (gnus-gethash (caar newsrc) gnus-newsrc-hashtb)
337 num-unread)))
338 (setcdr (car newsrc) (car active))
339 (gnus-message 3 "nnkiboze: Checking %s...done" (caar newsrc))
340 (setq newsrc (cdr newsrc)))))
341 ;; We save the kiboze newsrc for this group.
342 (with-temp-file newsrc-file
343 (insert "(setq nnkiboze-newsrc '")
344 (gnus-prin1 nnkiboze-newsrc)
345 (insert ")\n")))
346 (unless inhibit-list-groups
347 (save-excursion
348 (set-buffer gnus-group-buffer)
349 (gnus-group-list-groups)))
352 (defun nnkiboze-enter-nov (buffer header group)
353 (save-excursion
354 (set-buffer buffer)
355 (goto-char (point-max))
356 (let ((prefix (gnus-group-real-prefix group))
357 (oheader (copy-sequence header))
358 article)
359 (if (zerop (forward-line -1))
360 (progn
361 (setq article (1+ (read (current-buffer))))
362 (forward-line 1))
363 (setq article 1))
364 (mail-header-set-number oheader article)
365 (with-temp-buffer
366 (insert (or (mail-header-xref oheader) ""))
367 (goto-char (point-min))
368 (if (re-search-forward " [^ ]+:[0-9]+" nil t)
369 (goto-char (match-beginning 0))
370 (or (eobp) (forward-char 1)))
371 ;; The first Xref has to be the group this article
372 ;; really came for - this is the article nnkiboze
373 ;; will request when it is asked for the article.
374 (insert " " group ":"
375 (int-to-string (mail-header-number header)) " ")
376 (while (re-search-forward " [^ ]+:[0-9]+" nil t)
377 (goto-char (1+ (match-beginning 0)))
378 (insert prefix))
379 (mail-header-set-xref oheader (buffer-string)))
380 (nnheader-insert-nov oheader))))
382 (defun nnkiboze-nov-file-name (&optional suffix)
383 (concat (file-name-as-directory nnkiboze-directory)
384 (nnheader-translate-file-chars
385 (concat (nnkiboze-prefixed-name nnkiboze-current-group)
386 (or suffix ".nov")))))
388 (provide 'nnkiboze)
390 ;;; nnkiboze.el ends here