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
40 (eval-when-compile (require 'cl))
42 (nnoo-declare nnvirtual)
44 (defvoo nnvirtual-always-rescan nil
45 "*If non-nil, always scan groups for unread articles when entering a group.
46 If this variable is nil (which is the default) and you read articles
47 in a component group after the virtual group has been activated, the
48 read articles from the component group will show up when you enter the
51 (defvoo nnvirtual-component-regexp nil
52 "*Regexp to match component groups.")
56 (defconst nnvirtual-version "nnvirtual 1.0")
58 (defvoo nnvirtual-current-group nil)
59 (defvoo nnvirtual-component-groups nil)
60 (defvoo nnvirtual-mapping nil)
62 (defvoo nnvirtual-status-string "")
65 (autoload 'gnus-cache-articles-in-group "gnus-cache"))
69 ;;; Interface functions.
71 (nnoo-define-basics nnvirtual)
73 (deffoo nnvirtual-retrieve-headers (articles &optional newsgroup
75 (when (nnvirtual-possibly-change-server server)
77 (set-buffer nntp-server-buffer)
79 (if (stringp (car articles))
81 (let ((vbuf (nnheader-set-temp-buffer
82 (get-buffer-create " *virtual headers*")))
83 (unfetched (mapcar (lambda (g) (list g))
84 nnvirtual-component-groups))
85 (system-name (system-name))
86 cgroup article result prefix)
88 (setq article (assq (pop articles) nnvirtual-mapping))
89 (when (and (setq cgroup (cadr article))
91 (gnus-find-method-for-group cgroup) t)
92 (gnus-request-group cgroup t))
93 (setq prefix (gnus-group-real-prefix cgroup))
94 (when (setq result (gnus-retrieve-headers
95 (list (caddr article)) cgroup nil))
96 (set-buffer nntp-server-buffer)
97 (if (zerop (buffer-size))
98 (nconc (assq cgroup unfetched) (list (caddr article)))
99 ;; If we got HEAD headers, we convert them into NOV
100 ;; headers. This is slow, inefficient and, come to think
101 ;; of it, downright evil. So sue me. I couldn't be
102 ;; bothered to write a header parse routine that could
103 ;; parse a mixed HEAD/NOV buffer.
104 (when (eq result 'headers)
105 (nnvirtual-convert-headers))
106 (goto-char (point-min))
109 (point) (progn (read nntp-server-buffer) (point)))
110 (princ (car article) (current-buffer))
113 "[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t")
114 (goto-char (match-end 0))
116 "\t" (save-excursion (end-of-line) (point)) t)
118 (while (= (char-after (1- (point))) ? )
124 (or (= (char-after (1- (point))) ?\t)
126 (insert "Xref: " system-name " " cgroup ":")
127 (princ (caddr article) (current-buffer))
129 (insert "Xref: " system-name " " cgroup ":")
130 (princ (caddr article) (current-buffer))
132 (if (not (string= "" prefix))
133 (while (re-search-forward
135 (save-excursion (end-of-line) (point)) t)
137 (goto-char (match-beginning 0))
140 (or (= (char-after (1- (point))) ?\t)
144 (goto-char (point-max))
145 (insert-buffer-substring nntp-server-buffer)))))
147 ;; In case some of the articles have expired or been
148 ;; cancelled, we have to mark them as read in the
151 (when (cdar unfetched)
152 (gnus-group-make-articles-read
153 (caar unfetched) (sort (cdar unfetched) '<)))
154 (setq unfetched (cdr unfetched)))
156 ;; The headers are ready for reading, so they are inserted into
157 ;; the nntp-server-buffer, which is where Gnus expects to find
161 (set-buffer nntp-server-buffer)
163 (insert-buffer-substring vbuf)
165 (kill-buffer vbuf)))))))
167 (deffoo nnvirtual-request-article (article &optional group server buffer)
168 (when (and (nnvirtual-possibly-change-server server)
170 (let* ((amap (assq article nnvirtual-mapping))
171 (cgroup (cadr amap)))
174 (nnheader-report 'nnvirtual "No such article: %s" article))
175 ((not (gnus-check-group cgroup))
177 'nnvirtual "Can't open server where %s exists" cgroup))
178 ((not (gnus-request-group cgroup t))
179 (nnheader-report 'nnvirtual "Can't open component group %s" cgroup))
184 (gnus-request-article-this-buffer (caddr amap) cgroup))
185 (gnus-request-article (caddr amap) cgroup)))))))
187 (deffoo nnvirtual-open-server (server &optional defs)
188 (unless (assq 'nnvirtual-component-regexp defs)
189 (push `(nnvirtual-component-regexp ,server)
191 (nnoo-change-server 'nnvirtual server defs)
192 (if nnvirtual-component-groups
194 (setq nnvirtual-mapping nil)
195 ;; Go through the newsrc alist and find all component groups.
196 (let ((newsrc (cdr gnus-newsrc-alist))
198 (while (setq group (car (pop newsrc)))
199 (when (string-match nnvirtual-component-regexp group) ; Match
200 ;; Add this group to the list of component groups.
201 (setq nnvirtual-component-groups
202 (cons group (delete group nnvirtual-component-groups))))))
203 (if (not nnvirtual-component-groups)
204 (nnheader-report 'nnvirtual "No component groups: %s" server)
207 (deffoo nnvirtual-request-group (group &optional server dont-check)
208 (nnvirtual-possibly-change-server server)
209 (setq nnvirtual-component-groups
210 (delete (nnvirtual-current-group) nnvirtual-component-groups))
212 ((null nnvirtual-component-groups)
213 (setq nnvirtual-current-group nil)
214 (nnheader-report 'nnvirtual "No component groups in %s" group))
217 (nnvirtual-create-mapping))
218 (setq nnvirtual-current-group group)
219 (let ((len (length nnvirtual-mapping)))
220 (nnheader-insert "211 %d 1 %d %s\n" len len group)))))
222 (deffoo nnvirtual-request-type (group &optional article)
225 (let ((mart (assq article nnvirtual-mapping)))
227 (gnus-request-type (cadr mart) (car mart))))))
229 (deffoo nnvirtual-request-update-mark (group article mark)
230 (let* ((nart (assq article nnvirtual-mapping))
232 ;; The component group might be a virtual group.
233 (nmark (gnus-request-update-mark cgroup (caddr nart) mark)))
236 (gnus-group-auto-expirable-p cgroup))
237 (setq mark gnus-expirable-mark)))
240 (deffoo nnvirtual-close-group (group &optional server)
241 (when (nnvirtual-possibly-change-server server)
242 ;; Copy (un)read articles.
243 (nnvirtual-update-reads)
244 ;; We copy the marks from this group to the component
246 (nnvirtual-update-marked))
249 (deffoo nnvirtual-request-list (&optional server)
250 (nnheader-report 'nnvirtual "LIST is not implemented."))
252 (deffoo nnvirtual-request-newgroups (date &optional server)
253 (nnheader-report 'nnvirtual "NEWGROUPS is not supported."))
255 (deffoo nnvirtual-request-list-newsgroups (&optional server)
256 (nnheader-report 'nnvirtual "LIST NEWSGROUPS is not implemented."))
258 (deffoo nnvirtual-request-update-info (group info &optional server)
259 (when (nnvirtual-possibly-change-server server)
260 (let ((map nnvirtual-mapping)
261 (marks (mapcar (lambda (m) (list (cdr m))) gnus-article-mark-lists))
263 ;; Go through the mapping.
265 (unless (nth 3 (setq m (pop map)))
267 (push (car m) reads))
269 (when (setq mr (nth 4 m))
271 (setcdr (setq op (assq (pop mr) marks)) (cons (car m) (cdr op))))))
272 ;; Compress the marks and the reads.
275 (setcdr (car mr) (gnus-compress-sequence (sort (cdr (pop mr)) '<))))
276 (setcar (cddr info) (gnus-compress-sequence (nreverse reads)))
277 ;; Remove empty marks lists.
278 (while (and marks (not (cdar marks)))
279 (setq marks (cdr marks)))
284 (setcdr mr (cddr mr))))
286 ;; Enter these new marks into the info of the group.
288 (setcar (nthcdr 3 info) marks)
289 ;; Add the marks lists to the end of the info.
291 (setcdr (nthcdr 2 info) (list marks))))
294 (deffoo nnvirtual-catchup-group (group &optional server all)
295 (nnvirtual-possibly-change-server server)
296 (let ((gnus-group-marked (copy-sequence nnvirtual-component-groups))
297 (gnus-expert-user t))
298 ;; Make sure all groups are activated.
301 (when (not (numberp (car (gnus-gethash g gnus-newsrc-hashtb))))
302 (gnus-activate-group g)))
303 nnvirtual-component-groups)
305 (set-buffer gnus-group-buffer)
306 (gnus-group-catchup-current nil all))))
308 (deffoo nnvirtual-find-group-art (group article)
309 "Return the real group and article for virtual GROUP and ARTICLE."
310 (let ((mart (assq article nnvirtual-mapping)))
312 (cons (cadr mart) (caddr mart)))))
315 ;;; Internal functions.
317 (defun nnvirtual-convert-headers ()
318 "Convert HEAD headers into NOV headers."
320 (set-buffer nntp-server-buffer)
321 (let* ((dependencies (make-vector 100 0))
322 (headers (gnus-get-newsgroup-headers dependencies))
325 (while (setq header (pop headers))
326 (nnheader-insert-nov header)))))
328 (defun nnvirtual-possibly-change-server (server)
330 (nnoo-current-server-p 'nnvirtual server)
331 (nnvirtual-open-server server)))
333 (defun nnvirtual-update-marked ()
334 "Copy marks from the virtual group to the component groups."
335 (let ((mark-lists gnus-article-mark-lists)
336 (marks (gnus-info-marks (gnus-get-info (nnvirtual-current-group))))
337 type list mart cgroups)
338 (while (setq type (cdr (pop mark-lists)))
339 (setq list (gnus-uncompress-range (cdr (assq type marks))))
341 (mapcar (lambda (g) (list g)) nnvirtual-component-groups))
343 (nconc (assoc (cadr (setq mart (assq (pop list) nnvirtual-mapping)))
345 (list (caddr mart))))
347 (gnus-add-marked-articles
348 (caar cgroups) type (cdar cgroups) nil t)
349 (gnus-group-update-group (car (pop cgroups)) t)))))
351 (defun nnvirtual-update-reads ()
352 "Copy (un)reads from the current group to the component groups."
353 (let ((groups (mapcar (lambda (g) (list g)) nnvirtual-component-groups))
354 (articles (gnus-list-of-unread-articles
355 (nnvirtual-current-group)))
358 (setq m (assq (pop articles) nnvirtual-mapping))
359 (nconc (assoc (nth 1 m) groups) (list (nth 2 m))))
361 (gnus-update-read-articles (caar groups) (cdr (pop groups))))))
363 (defun nnvirtual-current-group ()
364 "Return the prefixed name of the current nnvirtual group."
365 (concat "nnvirtual:" nnvirtual-current-group))
367 (defsubst nnvirtual-marks (article marks)
368 "Return a list of mark types for ARTICLE."
371 (when (memq article (cdar marks))
372 (push (caar marks) out))
373 (setq marks (cdr marks)))
376 (defun nnvirtual-create-mapping ()
377 "Create an article mapping for the current group."
379 m marks list article unreads marks active
385 (when (and (setq active (gnus-activate-group g))
386 (> (cdr active) (car active)))
387 (setq unreads (gnus-list-of-unread-articles g)
388 marks (gnus-uncompress-marks
389 (gnus-info-marks (gnus-get-info g))))
391 (push (cons 'cache (gnus-cache-articles-in-group g))
393 (setq div (/ (float (car active))
394 (if (zerop (cdr active))
397 (list (* div (- n (car active)))
398 g n (and (memq n unreads) t)
399 (inline (nnvirtual-marks n marks))))
400 (gnus-uncompress-range active))))
401 nnvirtual-component-groups))
403 (< (car m1) (car m2)))))
405 (setq nnvirtual-mapping map)
406 ;; Set the virtual article numbers.
407 (while (setq m (pop map))
408 (setcar m (setq article (incf i))))))
412 ;;; nnvirtual.el ends here