1 ;;; nnvirtual.el --- virtual newsgroups access for Gnus
2 ;; Copyright (C) 1994,95,96 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
5 ;; Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
27 ;; The other access methods (nntp, nnspool, etc) are general news
28 ;; access methods. This module relies on Gnus and can not be used
36 (eval-when-compile (require 'cl))
38 (defvar nnvirtual-always-rescan nil
39 "*If non-nil, always scan groups for unread articles when entering a group.
40 If this variable is nil (which is the default) and you read articles
41 in a component group after the virtual group has been activated, the
42 read articles from the component group will show up when you enter the
47 (defconst nnvirtual-version "nnvirtual 1.0"
48 "Version number of this version of nnvirtual.")
50 (defvar nnvirtual-group-alist nil)
51 (defvar nnvirtual-current-group nil)
52 (defvar nnvirtual-component-groups nil)
53 (defvar nnvirtual-mapping nil)
55 (defvar nnvirtual-status-string "")
58 (autoload 'gnus-cache-articles-in-group "gnus-cache"))
62 ;;; Interface functions.
64 (defun nnvirtual-retrieve-headers (articles &optional newsgroup server fetch-old)
65 (when (nnvirtual-possibly-change-group newsgroup server t)
67 (if (stringp (car articles))
69 (let ((map nnvirtual-mapping)
70 (vbuf (nnheader-set-temp-buffer
71 (get-buffer-create " *virtual headers*")))
72 (unfetched (mapcar (lambda (g) (list g))
73 nnvirtual-component-groups))
74 beg cgroup active article result prefix)
76 (setq article (assq (pop articles) nnvirtual-mapping))
77 (setq cgroup (cadr article))
78 (gnus-request-group cgroup t)
79 (setq prefix (gnus-group-real-prefix cgroup))
80 (when (setq result (gnus-retrieve-headers
81 (list (caddr article)) cgroup))
82 (set-buffer nntp-server-buffer)
83 (if (zerop (buffer-size))
84 (nconc (assq cgroup unfetched) (list (caddr article)))
85 ;; If we got HEAD headers, we convert them into NOV
86 ;; headers. This is slow, inefficient and, come to think
87 ;; of it, downright evil. So sue me. I couldn't be
88 ;; bothered to write a header parse routine that could
89 ;; parse a mixed HEAD/NOV buffer.
90 (when (eq result 'headers)
91 (nnvirtual-convert-headers))
92 (goto-char (point-min))
95 (point) (progn (read nntp-server-buffer) (point)))
96 (insert (int-to-string (car article)))
99 "[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t")
100 (goto-char (match-end 0))
102 "\t" (save-excursion (end-of-line) (point)) t)
104 (while (= (char-after (1- (point))) ? )
110 (or (= (char-after (1- (point))) ?\t)
112 (insert (format "Xref: %s %s:%d\t" (system-name)
113 cgroup (caddr article))))
114 (if (not (string= "" prefix))
115 (while (re-search-forward
117 (save-excursion (end-of-line) (point)) t)
119 (goto-char (match-beginning 0))
122 (or (= (char-after (1- (point))) ?\t)
126 (goto-char (point-max))
127 (insert-buffer-substring nntp-server-buffer))))
129 ;; In case some of the articles have expired or been
130 ;; cancelled, we have to mark them as read in the
133 (when (cdar unfetched)
134 (gnus-group-make-articles-read
135 (caar unfetched) (sort (cdar unfetched) '<)))
136 (setq unfetched (cdr unfetched)))
138 ;; The headers are ready for reading, so they are inserted into
139 ;; the nntp-server-buffer, which is where Gnus expects to find
143 (set-buffer nntp-server-buffer)
145 (insert-buffer-substring vbuf)
147 (kill-buffer vbuf)))))))
149 (defun nnvirtual-open-server (server &optional something)
150 (nnheader-init-server-buffer))
152 (defun nnvirtual-close-server (&rest dum)
155 (defun nnvirtual-request-close ()
156 (setq nnvirtual-current-group nil
157 nnvirtual-component-groups nil
158 nnvirtual-mapping nil
159 nnvirtual-group-alist nil)
162 (defun nnvirtual-server-opened (&optional server)
163 (and nntp-server-buffer
164 (get-buffer nntp-server-buffer)))
166 (defun nnvirtual-status-message (&optional server)
167 nnvirtual-status-string)
169 (defun nnvirtual-request-article (article &optional group server buffer)
170 (when (and (nnvirtual-possibly-change-group group server t)
172 (let* ((amap (assq article nnvirtual-mapping))
173 (cgroup (cadr amap)))
176 (nnheader-report 'nnvirtual "No such article: %s" article))
177 ((not (gnus-check-group cgroup))
179 'nnvirtual "Can't open server where %s exists" cgroup))
180 ((not (gnus-request-group cgroup t))
181 (nnheader-report 'nnvirtual "Can't open component group %s" cgroup))
186 (gnus-request-article-this-buffer (caddr amap) cgroup))
187 (gnus-request-article (caddr amap) cgroup)))))))
189 (defun nnvirtual-request-group (group &optional server dont-check)
191 ((null (nnvirtual-possibly-change-group
193 (if nnvirtual-always-rescan nil dont-check)))
194 (setq nnvirtual-current-group nil)
195 (nnheader-report 'nnvirtual "No component groups in %s" group))
197 (let ((len (length nnvirtual-mapping)))
198 (nnheader-insert "211 %d 1 %d %s\n" len len group)))))
200 (defun nnvirtual-request-type (group &optional article)
201 (when (nnvirtual-possibly-change-group group nil t)
204 (let ((mart (assq article nnvirtual-mapping)))
206 (gnus-request-type (cadr mart) (car mart)))))))
208 (defun nnvirtual-request-update-mark (group article mark)
209 (when (nnvirtual-possibly-change-group group nil t)
210 (let* ((nart (assq article nnvirtual-mapping))
212 ;; The component group might be a virtual group.
213 (nmark (gnus-request-update-mark cgroup (caddr nart) mark)))
214 (when (and (= mark nmark)
215 (gnus-group-auto-expirable-p cgroup))
216 (setq mark gnus-expirable-mark))))
219 (defun nnvirtual-close-group (group &optional server)
220 (when (nnvirtual-possibly-change-group group server t)
221 ;; We copy the marks from this group to the component
223 (nnvirtual-update-marked)
224 ;; Reset all relevant variables.
225 (setq nnvirtual-current-group nil
226 nnvirtual-component-groups nil
227 nnvirtual-mapping nil)
228 (setq nnvirtual-group-alist
229 (delq (assoc group nnvirtual-group-alist) nnvirtual-group-alist)))
232 (defun nnvirtual-request-list (&optional server)
233 (nnheader-report 'nnvirtual "LIST is not implemented."))
235 (defun nnvirtual-request-newgroups (date &optional server)
236 (nnheader-report 'nnvirtual "NEWGROUPS is not supported."))
238 (defun nnvirtual-request-list-newsgroups (&optional server)
239 (nnheader-report 'nnvirtual "LIST NEWSGROUPS is not implemented."))
241 (defun nnvirtual-request-update-info (group info &optional server)
242 (when (nnvirtual-possibly-change-group group server)
243 (let ((map nnvirtual-mapping)
244 (marks (mapcar (lambda (m) (list (cdr m))) gnus-article-mark-lists))
249 (push (car m) reads))
250 (when (setq mr (nth 4 m))
252 (setcdr (setq op (assq (pop mr) marks)) (cons (car m) (cdr op))))))
255 (setcdr (car mr) (gnus-compress-sequence (sort (cdar mr) '<)))
257 (setcar (cddr info) (gnus-compress-sequence (nreverse reads)))
259 ;; Enter these new marks into the info of the group.
261 (setcar (nthcdr 3 info) marks)
262 ;; Add the marks lists to the end of the info.
264 (setcdr (nthcdr 2 info) (list marks))))
267 (defun nnvirtual-catchup-group (group &optional server all)
268 (nnvirtual-possibly-change-group group server t)
269 (let ((gnus-group-marked nnvirtual-component-groups)
270 (gnus-expert-user t))
272 (set-buffer gnus-group-buffer)
273 (gnus-group-catchup-current nil all))))
275 (defun nnvirtual-find-group-art (group article)
276 "Return the real group and article for virtual GROUP and ARTICLE."
277 (nnvirtual-possibly-change-group group nil t)
278 (let ((mart (assq article nnvirtual-mapping)))
279 (cons (cadr mart) (caddr mart))))
282 ;;; Internal functions.
284 (defun nnvirtual-convert-headers ()
285 "Convert HEAD headers into NOV headers."
287 (set-buffer nntp-server-buffer)
288 (let* ((dependencies (make-vector 100 0))
289 (headers (gnus-get-newsgroup-headers dependencies))
292 (while (setq header (pop headers))
293 (insert (int-to-string (mail-header-number header)) "\t"
294 (or (mail-header-subject header) "") "\t"
295 (or (mail-header-from header) "") "\t"
296 (or (mail-header-date header) "") "\t"
297 (or (mail-header-id header) "") "\t"
298 (or (mail-header-references header) "") "\t"
299 (int-to-string (or (mail-header-chars header) 0)) "\t"
300 (int-to-string (or (mail-header-lines header) 0)) "\t"
301 (if (mail-header-xref header)
302 (concat "Xref: " (mail-header-xref header) "\t")
305 (defun nnvirtual-possibly-change-group (group regexp &optional dont-check)
307 (unless (equal group nnvirtual-current-group)
308 (and (setq inf (assoc group nnvirtual-group-alist))
310 (string= (nth 3 inf) regexp)
312 (setq nnvirtual-current-group (car inf))
313 (setq nnvirtual-component-groups (nth 1 inf))
314 (setq nnvirtual-mapping (nth 2 inf)))))
318 (and inf (setq nnvirtual-group-alist
319 (delq inf nnvirtual-group-alist)))
320 (setq nnvirtual-mapping nil)
321 (setq nnvirtual-current-group group)
322 (let ((newsrc gnus-newsrc-alist)
323 (virt-group (gnus-group-prefixed-name
324 nnvirtual-current-group '(nnvirtual ""))))
325 (setq nnvirtual-component-groups nil)
327 (and (string-match regexp (car (car newsrc)))
328 (not (string= (car (car newsrc)) virt-group))
329 (setq nnvirtual-component-groups
330 (cons (car (car newsrc)) nnvirtual-component-groups)))
331 (setq newsrc (cdr newsrc))))
332 (if nnvirtual-component-groups
334 (nnvirtual-create-mapping)
335 (setq nnvirtual-group-alist
336 (cons (list group nnvirtual-component-groups
337 nnvirtual-mapping regexp)
338 nnvirtual-group-alist)))
339 (nnheader-report 'nnvirtual "No component groups: %s" group))))
340 nnvirtual-component-groups)
342 (defun nnvirtual-update-marked ()
343 "Copy marks from the virtual group to the component groups."
344 (let ((mark-lists gnus-article-mark-lists)
345 (uncompressed '(score bookmark))
346 type list calist mart cgroups)
348 (setq type (cdar mark-lists))
349 (setq list (symbol-value (intern (format "gnus-newsgroup-%s"
350 (car (pop mark-lists))))))
352 (mapcar (lambda (g) (list g)) nnvirtual-component-groups))
354 (nconc (assoc (cadr (setq mart (assq (pop list) nnvirtual-mapping)))
356 (list (caddr mart))))
358 (gnus-add-marked-articles
359 (caar cgroups) type (cdar cgroups) nil t)
360 (gnus-group-update-group (car (pop cgroups)) t)))))
362 (defun nnvirtual-marks (article marks)
363 "Return a list of mark types for ARTICLE."
366 (when (memq article (cdar marks))
367 (push (caar marks) out))
368 (setq marks (cdr marks)))
371 (defun nnvirtual-create-mapping ()
372 "Create an article mapping for the current group."
379 (let* ((active (or (gnus-active g) (gnus-activate-group g)))
380 (unreads (and active (gnus-list-of-unread-articles
382 (marks (gnus-uncompress-marks
383 (gnus-info-marks (gnus-get-info g)))))
386 (push (cons 'cache (gnus-cache-articles-in-group g))
389 (setq div (/ (float (car active))
390 (if (zerop (cdr active))
393 (list (* div (- n (car active)))
394 g n (and (memq n unreads) t)
395 (nnvirtual-marks n marks)))
396 (gnus-uncompress-range active))))))
397 nnvirtual-component-groups))
399 (< (car m1) (car m2)))))
401 (setq nnvirtual-mapping map)
403 (setcar (pop map) (incf i)))))
407 ;;; nnvirtual.el ends here