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.")
54 (defvoo nnvirtual-component-groups nil
55 "Component group in this nnvirtual group.")
59 (defconst nnvirtual-version "nnvirtual 1.0")
61 (defvoo nnvirtual-current-group nil)
62 (defvoo nnvirtual-mapping nil)
64 (defvoo nnvirtual-status-string "")
67 (autoload 'gnus-cache-articles-in-group "gnus-cache"))
71 ;;; Interface functions.
73 (nnoo-define-basics nnvirtual)
75 (deffoo nnvirtual-retrieve-headers (articles &optional newsgroup
77 (when (nnvirtual-possibly-change-server server)
79 (set-buffer nntp-server-buffer)
81 (if (stringp (car articles))
83 (let ((vbuf (nnheader-set-temp-buffer
84 (get-buffer-create " *virtual headers*")))
85 (unfetched (mapcar (lambda (g) (list g))
86 nnvirtual-component-groups))
87 (system-name (system-name))
88 cgroup article result prefix)
90 (setq article (assq (pop articles) nnvirtual-mapping))
91 (when (and (setq cgroup (cadr article))
93 (gnus-find-method-for-group cgroup) t)
94 (gnus-request-group cgroup t))
95 (setq prefix (gnus-group-real-prefix cgroup))
96 (when (setq result (gnus-retrieve-headers
97 (list (caddr article)) cgroup nil))
98 (set-buffer nntp-server-buffer)
99 (if (zerop (buffer-size))
100 (nconc (assq cgroup unfetched) (list (caddr article)))
101 ;; If we got HEAD headers, we convert them into NOV
102 ;; headers. This is slow, inefficient and, come to think
103 ;; of it, downright evil. So sue me. I couldn't be
104 ;; bothered to write a header parse routine that could
105 ;; parse a mixed HEAD/NOV buffer.
106 (when (eq result 'headers)
107 (nnvirtual-convert-headers))
108 (goto-char (point-min))
111 (point) (progn (read nntp-server-buffer) (point)))
112 (princ (car article) (current-buffer))
115 "[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t")
116 (goto-char (match-end 0))
118 "\t" (save-excursion (end-of-line) (point)) t)
120 (while (= (char-after (1- (point))) ? )
126 (or (= (char-after (1- (point))) ?\t)
128 (insert "Xref: " system-name " " cgroup ":")
129 (princ (caddr article) (current-buffer))
131 (insert "Xref: " system-name " " cgroup ":")
132 (princ (caddr article) (current-buffer))
134 (when (not (string= "" prefix))
135 (while (re-search-forward
137 (save-excursion (end-of-line) (point)) t)
139 (goto-char (match-beginning 0))
142 (or (= (char-after (1- (point))) ?\t)
146 (goto-char (point-max))
147 (insert-buffer-substring nntp-server-buffer)))))
149 ;; In case some of the articles have expired or been
150 ;; cancelled, we have to mark them as read in the
153 (when (cdar unfetched)
154 (gnus-group-make-articles-read
155 (caar unfetched) (sort (cdar unfetched) '<)))
156 (setq unfetched (cdr unfetched)))
158 ;; The headers are ready for reading, so they are inserted into
159 ;; the nntp-server-buffer, which is where Gnus expects to find
163 (set-buffer nntp-server-buffer)
165 (insert-buffer-substring vbuf)
167 (kill-buffer vbuf)))))))
169 (deffoo nnvirtual-request-article (article &optional group server buffer)
170 (when (and (nnvirtual-possibly-change-server server)
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 (deffoo nnvirtual-open-server (server &optional defs)
190 (unless (assq 'nnvirtual-component-regexp defs)
191 (push `(nnvirtual-component-regexp ,server)
193 (nnoo-change-server 'nnvirtual server defs)
194 (if nnvirtual-component-groups
196 (setq nnvirtual-mapping nil)
197 (when nnvirtual-component-regexp
198 ;; Go through the newsrc alist and find all component groups.
199 (let ((newsrc (cdr gnus-newsrc-alist))
201 (while (setq group (car (pop newsrc)))
202 (when (string-match nnvirtual-component-regexp group) ; Match
203 ;; Add this group to the list of component groups.
204 (setq nnvirtual-component-groups
205 (cons group (delete group nnvirtual-component-groups)))))))
206 (if (not nnvirtual-component-groups)
207 (nnheader-report 'nnvirtual "No component groups: %s" server)
210 (deffoo nnvirtual-request-group (group &optional server dont-check)
211 (nnvirtual-possibly-change-server server)
212 (setq nnvirtual-component-groups
213 (delete (nnvirtual-current-group) nnvirtual-component-groups))
215 ((null nnvirtual-component-groups)
216 (setq nnvirtual-current-group nil)
217 (nnheader-report 'nnvirtual "No component groups in %s" group))
220 (nnvirtual-create-mapping))
221 (setq nnvirtual-current-group group)
222 (let ((len (length nnvirtual-mapping)))
223 (nnheader-insert "211 %d 1 %d %s\n" len len group)))))
225 (deffoo nnvirtual-request-type (group &optional article)
228 (let ((mart (assq article nnvirtual-mapping)))
230 (gnus-request-type (cadr mart) (car mart))))))
232 (deffoo nnvirtual-request-update-mark (group article mark)
233 (let* ((nart (assq article nnvirtual-mapping))
235 ;; The component group might be a virtual group.
236 (nmark (gnus-request-update-mark cgroup (caddr nart) mark)))
239 (gnus-group-auto-expirable-p cgroup))
240 (setq mark gnus-expirable-mark)))
243 (deffoo nnvirtual-close-group (group &optional server)
244 (when (and (nnvirtual-possibly-change-server server)
245 (not (gnus-ephemeral-group-p (nnvirtual-current-group))))
246 ;; Copy (un)read articles.
247 (nnvirtual-update-reads)
248 ;; We copy the marks from this group to the component
250 (nnvirtual-update-marked))
253 (deffoo nnvirtual-request-list (&optional server)
254 (nnheader-report 'nnvirtual "LIST is not implemented."))
256 (deffoo nnvirtual-request-newgroups (date &optional server)
257 (nnheader-report 'nnvirtual "NEWGROUPS is not supported."))
259 (deffoo nnvirtual-request-list-newsgroups (&optional server)
260 (nnheader-report 'nnvirtual "LIST NEWSGROUPS is not implemented."))
262 (deffoo nnvirtual-request-update-info (group info &optional server)
263 (when (nnvirtual-possibly-change-server server)
264 (let ((map nnvirtual-mapping)
265 (marks (mapcar (lambda (m) (list (cdr m))) gnus-article-mark-lists))
267 ;; Go through the mapping.
269 (unless (nth 3 (setq m (pop map)))
271 (push (car m) reads))
273 (when (setq mr (nth 4 m))
275 (setcdr (setq op (assq (pop mr) marks)) (cons (car m) (cdr op))))))
276 ;; Compress the marks and the reads.
279 (setcdr (car mr) (gnus-compress-sequence (sort (cdr (pop mr)) '<))))
280 (setcar (cddr info) (gnus-compress-sequence (nreverse reads)))
281 ;; Remove empty marks lists.
282 (while (and marks (not (cdar marks)))
283 (setq marks (cdr marks)))
288 (setcdr mr (cddr mr))))
290 ;; Enter these new marks into the info of the group.
292 (setcar (nthcdr 3 info) marks)
293 ;; Add the marks lists to the end of the info.
295 (setcdr (nthcdr 2 info) (list marks))))
298 (deffoo nnvirtual-catchup-group (group &optional server all)
299 (nnvirtual-possibly-change-server server)
300 (let ((gnus-group-marked (copy-sequence nnvirtual-component-groups))
301 (gnus-expert-user t))
302 ;; Make sure all groups are activated.
305 (when (not (numberp (car (gnus-gethash g gnus-newsrc-hashtb))))
306 (gnus-activate-group g)))
307 nnvirtual-component-groups)
309 (set-buffer gnus-group-buffer)
310 (gnus-group-catchup-current nil all))))
312 (deffoo nnvirtual-find-group-art (group article)
313 "Return the real group and article for virtual GROUP and ARTICLE."
314 (let ((mart (assq article nnvirtual-mapping)))
316 (cons (cadr mart) (caddr mart)))))
319 ;;; Internal functions.
321 (defun nnvirtual-convert-headers ()
322 "Convert HEAD headers into NOV headers."
324 (set-buffer nntp-server-buffer)
325 (let* ((dependencies (make-vector 100 0))
326 (headers (gnus-get-newsgroup-headers dependencies))
329 (while (setq header (pop headers))
330 (nnheader-insert-nov header)))))
332 (defun nnvirtual-possibly-change-server (server)
334 (nnoo-current-server-p 'nnvirtual server)
335 (nnvirtual-open-server server)))
337 (defun nnvirtual-update-marked ()
338 "Copy marks from the virtual group to the component groups."
339 (let ((mark-lists gnus-article-mark-lists)
340 (marks (gnus-info-marks (gnus-get-info (nnvirtual-current-group))))
341 type list mart cgroups)
342 (while (setq type (cdr (pop mark-lists)))
343 (setq list (gnus-uncompress-range (cdr (assq type marks))))
345 (mapcar (lambda (g) (list g)) nnvirtual-component-groups))
347 (nconc (assoc (cadr (setq mart (assq (pop list) nnvirtual-mapping)))
349 (list (caddr mart))))
351 (gnus-add-marked-articles
352 (caar cgroups) type (cdar cgroups) nil t)
353 (gnus-group-update-group (car (pop cgroups)) t)))))
355 (defun nnvirtual-update-reads ()
356 "Copy (un)reads from the current group to the component groups."
357 (let ((groups (mapcar (lambda (g) (list g)) nnvirtual-component-groups))
358 (articles (gnus-list-of-unread-articles
359 (nnvirtual-current-group)))
362 (setq m (assq (pop articles) nnvirtual-mapping))
363 (nconc (assoc (nth 1 m) groups) (list (nth 2 m))))
365 (gnus-update-read-articles (caar groups) (cdr (pop groups))))))
367 (defun nnvirtual-current-group ()
368 "Return the prefixed name of the current nnvirtual group."
369 (concat "nnvirtual:" nnvirtual-current-group))
371 (defsubst nnvirtual-marks (article marks)
372 "Return a list of mark types for ARTICLE."
375 (when (memq article (cdar marks))
376 (push (caar marks) out))
377 (setq marks (cdr marks)))
380 (defun nnvirtual-create-mapping ()
381 "Create an article mapping for the current group."
383 m unreads marks active
389 (when (and (setq active (gnus-activate-group g))
390 (> (cdr active) (car active)))
391 (setq unreads (gnus-list-of-unread-articles g)
392 marks (gnus-uncompress-marks
393 (gnus-info-marks (gnus-get-info g))))
395 (push (cons 'cache (gnus-cache-articles-in-group g))
397 (setq div (/ (float (car active))
398 (if (zerop (cdr active))
401 (list (* div (- n (car active)))
402 g n (and (memq n unreads) t)
403 (inline (nnvirtual-marks n marks))))
404 (gnus-uncompress-range active))))
405 nnvirtual-component-groups))
407 (< (car m1) (car m2)))))
409 (setq nnvirtual-mapping map)
410 ;; Set the virtual article numbers.
411 (while (setq m (pop map))
412 (setcar m (incf i)))))
416 ;;; nnvirtual.el ends here