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 (when (and (setq cgroup (cadr article))
79 (gnus-find-method-for-group cgroup) t)
80 (gnus-request-group cgroup t))
81 (setq prefix (gnus-group-real-prefix cgroup))
82 (when (setq result (gnus-retrieve-headers
83 (list (caddr article)) cgroup fetch-old))
84 (set-buffer nntp-server-buffer)
85 (if (zerop (buffer-size))
86 (nconc (assq cgroup unfetched) (list (caddr article)))
87 ;; If we got HEAD headers, we convert them into NOV
88 ;; headers. This is slow, inefficient and, come to think
89 ;; of it, downright evil. So sue me. I couldn't be
90 ;; bothered to write a header parse routine that could
91 ;; parse a mixed HEAD/NOV buffer.
92 (when (eq result 'headers)
93 (nnvirtual-convert-headers))
94 (goto-char (point-min))
97 (point) (progn (read nntp-server-buffer) (point)))
98 (insert (int-to-string (car article)))
101 "[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t")
102 (goto-char (match-end 0))
104 "\t" (save-excursion (end-of-line) (point)) t)
106 (while (= (char-after (1- (point))) ? )
112 (or (= (char-after (1- (point))) ?\t)
114 (insert (format "Xref: %s %s:%d\t" (system-name)
115 cgroup (caddr article))))
116 (insert (format "Xref: %s %s:%d " (system-name)
117 cgroup (caddr article)))
118 (if (not (string= "" prefix))
119 (while (re-search-forward
121 (save-excursion (end-of-line) (point)) t)
123 (goto-char (match-beginning 0))
126 (or (= (char-after (1- (point))) ?\t)
130 (goto-char (point-max))
131 (insert-buffer-substring nntp-server-buffer)))))
133 ;; In case some of the articles have expired or been
134 ;; cancelled, we have to mark them as read in the
137 (when (cdar unfetched)
138 (gnus-group-make-articles-read
139 (caar unfetched) (sort (cdar unfetched) '<)))
140 (setq unfetched (cdr unfetched)))
142 ;; The headers are ready for reading, so they are inserted into
143 ;; the nntp-server-buffer, which is where Gnus expects to find
147 (set-buffer nntp-server-buffer)
149 (insert-buffer-substring vbuf)
151 (kill-buffer vbuf)))))))
153 (defun nnvirtual-open-server (server &optional something)
154 (nnheader-init-server-buffer))
156 (defun nnvirtual-close-server (&rest dum)
159 (defun nnvirtual-request-close ()
160 (setq nnvirtual-current-group nil
161 nnvirtual-component-groups nil
162 nnvirtual-mapping nil
163 nnvirtual-group-alist nil)
166 (defun nnvirtual-server-opened (&optional server)
167 (and nntp-server-buffer
168 (get-buffer nntp-server-buffer)))
170 (defun nnvirtual-status-message (&optional server)
171 nnvirtual-status-string)
173 (defun nnvirtual-request-article (article &optional group server buffer)
174 (when (and (nnvirtual-possibly-change-group group server t)
176 (let* ((amap (assq article nnvirtual-mapping))
177 (cgroup (cadr amap)))
180 (nnheader-report 'nnvirtual "No such article: %s" article))
181 ((not (gnus-check-group cgroup))
183 'nnvirtual "Can't open server where %s exists" cgroup))
184 ((not (gnus-request-group cgroup t))
185 (nnheader-report 'nnvirtual "Can't open component group %s" cgroup))
190 (gnus-request-article-this-buffer (caddr amap) cgroup))
191 (gnus-request-article (caddr amap) cgroup)))))))
193 (defun nnvirtual-request-group (group &optional server dont-check)
195 ((null (nnvirtual-possibly-change-group
197 (if nnvirtual-always-rescan nil dont-check)))
198 (setq nnvirtual-current-group nil)
199 (nnheader-report 'nnvirtual "No component groups in %s" group))
201 (let ((len (length nnvirtual-mapping)))
202 (nnheader-insert "211 %d 1 %d %s\n" len len group)))))
204 (defun nnvirtual-request-type (group &optional article)
205 (when (nnvirtual-possibly-change-group group nil t)
208 (let ((mart (assq article nnvirtual-mapping)))
210 (gnus-request-type (cadr mart) (car mart)))))))
212 (defun nnvirtual-request-update-mark (group article mark)
213 (when (nnvirtual-possibly-change-group group nil t)
214 (let* ((nart (assq article nnvirtual-mapping))
216 ;; The component group might be a virtual group.
217 (nmark (gnus-request-update-mark cgroup (caddr nart) mark)))
218 (when (and (= mark nmark)
219 (gnus-group-auto-expirable-p cgroup))
220 (setq mark gnus-expirable-mark))))
223 (defun nnvirtual-close-group (group &optional server)
224 (when (nnvirtual-possibly-change-group group server t)
225 ;; We copy the marks from this group to the component
227 (nnvirtual-update-marked)
228 ;; Reset all relevant variables.
229 (setq nnvirtual-current-group nil
230 nnvirtual-component-groups nil
231 nnvirtual-mapping nil)
232 (setq nnvirtual-group-alist
233 (delq (assoc group nnvirtual-group-alist) nnvirtual-group-alist)))
236 (defun nnvirtual-request-list (&optional server)
237 (nnheader-report 'nnvirtual "LIST is not implemented."))
239 (defun nnvirtual-request-newgroups (date &optional server)
240 (nnheader-report 'nnvirtual "NEWGROUPS is not supported."))
242 (defun nnvirtual-request-list-newsgroups (&optional server)
243 (nnheader-report 'nnvirtual "LIST NEWSGROUPS is not implemented."))
245 (defun nnvirtual-request-update-info (group info &optional server)
246 (when (nnvirtual-possibly-change-group group server)
247 (let ((map nnvirtual-mapping)
248 (marks (mapcar (lambda (m) (list (cdr m))) gnus-article-mark-lists))
253 (push (car m) reads))
254 (when (setq mr (nth 4 m))
256 (setcdr (setq op (assq (pop mr) marks)) (cons (car m) (cdr op))))))
259 (setcdr (car mr) (gnus-compress-sequence (sort (cdar mr) '<)))
261 (setcar (cddr info) (gnus-compress-sequence (nreverse reads)))
263 ;; Enter these new marks into the info of the group.
265 (setcar (nthcdr 3 info) marks)
266 ;; Add the marks lists to the end of the info.
268 (setcdr (nthcdr 2 info) (list marks))))
271 (defun nnvirtual-catchup-group (group &optional server all)
272 (nnvirtual-possibly-change-group group server t)
273 (let ((gnus-group-marked nnvirtual-component-groups)
274 (gnus-expert-user t))
276 (set-buffer gnus-group-buffer)
277 (gnus-group-catchup-current nil all))))
279 (defun nnvirtual-find-group-art (group article)
280 "Return the real group and article for virtual GROUP and ARTICLE."
281 (nnvirtual-possibly-change-group group nil t)
282 (let ((mart (assq article nnvirtual-mapping)))
284 (cons (cadr mart) (caddr mart)))))
287 ;;; Internal functions.
289 (defun nnvirtual-convert-headers ()
290 "Convert HEAD headers into NOV headers."
292 (set-buffer nntp-server-buffer)
293 (let* ((dependencies (make-vector 100 0))
294 (headers (gnus-get-newsgroup-headers dependencies))
297 (while (setq header (pop headers))
298 (insert (int-to-string (mail-header-number header)) "\t"
299 (or (mail-header-subject header) "") "\t"
300 (or (mail-header-from header) "") "\t"
301 (or (mail-header-date header) "") "\t"
302 (or (mail-header-id header) "") "\t"
303 (or (mail-header-references header) "") "\t"
304 (int-to-string (or (mail-header-chars header) 0)) "\t"
305 (int-to-string (or (mail-header-lines header) 0)) "\t"
306 (if (mail-header-xref header)
307 (concat "Xref: " (mail-header-xref header) "\t")
310 (defun nnvirtual-possibly-change-group (group regexp &optional dont-check)
312 (unless (equal group nnvirtual-current-group)
313 (and (setq inf (assoc group nnvirtual-group-alist))
315 (string= (nth 3 inf) regexp)
317 (setq nnvirtual-current-group (car inf))
318 (setq nnvirtual-component-groups (nth 1 inf))
319 (setq nnvirtual-mapping (nth 2 inf)))))
323 (and inf (setq nnvirtual-group-alist
324 (delq inf nnvirtual-group-alist)))
325 (setq nnvirtual-mapping nil)
326 (setq nnvirtual-current-group group)
327 (let ((newsrc gnus-newsrc-alist)
328 (virt-group (gnus-group-prefixed-name
329 nnvirtual-current-group '(nnvirtual ""))))
330 (setq nnvirtual-component-groups nil)
332 (and (string-match regexp (caar newsrc))
333 (not (string= (caar newsrc) virt-group))
334 (setq nnvirtual-component-groups
335 (cons (caar newsrc) nnvirtual-component-groups)))
336 (setq newsrc (cdr newsrc))))
337 (if nnvirtual-component-groups
339 (nnvirtual-create-mapping)
340 (setq nnvirtual-group-alist
341 (cons (list group nnvirtual-component-groups
342 nnvirtual-mapping regexp)
343 nnvirtual-group-alist)))
344 (nnheader-report 'nnvirtual "No component groups: %s" group))))
345 nnvirtual-component-groups)
347 (defun nnvirtual-update-marked ()
348 "Copy marks from the virtual group to the component groups."
349 (let ((mark-lists gnus-article-mark-lists)
350 (uncompressed '(score bookmark))
351 type list calist mart cgroups)
353 (setq type (cdar mark-lists))
354 (setq list (symbol-value (intern (format "gnus-newsgroup-%s"
355 (car (pop mark-lists))))))
357 (mapcar (lambda (g) (list g)) nnvirtual-component-groups))
359 (nconc (assoc (cadr (setq mart (assq (pop list) nnvirtual-mapping)))
361 (list (caddr mart))))
363 (gnus-add-marked-articles
364 (caar cgroups) type (cdar cgroups) nil t)
365 (gnus-group-update-group (car (pop cgroups)) t)))))
367 (defun 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."
378 (let* (div m marks list article
384 (let* ((active (or (gnus-active g) (gnus-activate-group g)))
385 (unreads (and active (gnus-list-of-unread-articles
387 (marks (gnus-uncompress-marks
388 (gnus-info-marks (gnus-get-info g)))))
391 (push (cons 'cache (gnus-cache-articles-in-group g))
394 (setq div (/ (float (car active))
395 (if (zerop (cdr active))
398 (list (* div (- n (car active)))
399 g n (and (memq n unreads) t)
400 (nnvirtual-marks n marks)))
401 (gnus-uncompress-range active))))))
402 nnvirtual-component-groups))
404 (< (car m1) (car m2)))))
406 (setq nnvirtual-mapping map)
407 (while (setq m (pop map))
408 (setcar m (setq article (incf i)))
409 (when (setq marks (nth 4 m))
412 (intern (concat "gnus-newsgroup-"
414 (car (rassq (pop marks)
415 gnus-article-mark-lists))))))
416 (cons article (symbol-value list))))))))
422 ;;; nnvirtual.el ends here