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 ((vbuf (nnheader-set-temp-buffer
70 (get-buffer-create " *virtual headers*")))
71 (unfetched (mapcar (lambda (g) (list g))
72 nnvirtual-component-groups))
73 (system-name (system-name))
74 cgroup 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 nil))
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 (princ (car article) (current-buffer))
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 "Xref: " system-name " " cgroup ":")
115 (princ (caddr article) (current-buffer))
117 (insert "Xref: " system-name " " cgroup ":")
118 (princ (caddr article) (current-buffer))
120 (if (not (string= "" prefix))
121 (while (re-search-forward
123 (save-excursion (end-of-line) (point)) t)
125 (goto-char (match-beginning 0))
128 (or (= (char-after (1- (point))) ?\t)
132 (goto-char (point-max))
133 (insert-buffer-substring nntp-server-buffer)))))
135 ;; In case some of the articles have expired or been
136 ;; cancelled, we have to mark them as read in the
139 (when (cdar unfetched)
140 (gnus-group-make-articles-read
141 (caar unfetched) (sort (cdar unfetched) '<)))
142 (setq unfetched (cdr unfetched)))
144 ;; The headers are ready for reading, so they are inserted into
145 ;; the nntp-server-buffer, which is where Gnus expects to find
149 (set-buffer nntp-server-buffer)
151 (insert-buffer-substring vbuf)
153 (kill-buffer vbuf)))))))
155 (defun nnvirtual-open-server (server &optional something)
156 (nnheader-init-server-buffer))
158 (defun nnvirtual-close-server (&rest dum)
161 (defun nnvirtual-request-close ()
162 (setq nnvirtual-current-group nil
163 nnvirtual-component-groups nil
164 nnvirtual-mapping nil
165 nnvirtual-group-alist nil)
168 (defun nnvirtual-server-opened (&optional server)
169 (and nntp-server-buffer
170 (get-buffer nntp-server-buffer)))
172 (defun nnvirtual-status-message (&optional server)
173 nnvirtual-status-string)
175 (defun nnvirtual-request-article (article &optional group server buffer)
176 (when (and (nnvirtual-possibly-change-group group server t)
178 (let* ((amap (assq article nnvirtual-mapping))
179 (cgroup (cadr amap)))
182 (nnheader-report 'nnvirtual "No such article: %s" article))
183 ((not (gnus-check-group cgroup))
185 'nnvirtual "Can't open server where %s exists" cgroup))
186 ((not (gnus-request-group cgroup t))
187 (nnheader-report 'nnvirtual "Can't open component group %s" cgroup))
192 (gnus-request-article-this-buffer (caddr amap) cgroup))
193 (gnus-request-article (caddr amap) cgroup)))))))
195 (defun nnvirtual-request-group (group &optional server dont-check)
197 ((null (nnvirtual-possibly-change-group
199 (if nnvirtual-always-rescan nil (not dont-check))))
200 (setq nnvirtual-current-group nil)
201 (nnheader-report 'nnvirtual "No component groups in %s" group))
203 (let ((len (length nnvirtual-mapping)))
204 (nnheader-insert "211 %d 1 %d %s\n" len len group)))))
206 (defun nnvirtual-request-type (group &optional article)
207 (when (nnvirtual-possibly-change-group group nil t)
210 (let ((mart (assq article nnvirtual-mapping)))
212 (gnus-request-type (cadr mart) (car mart)))))))
214 (defun nnvirtual-request-update-mark (group article mark)
215 (when (nnvirtual-possibly-change-group group nil t)
216 (let* ((nart (assq article nnvirtual-mapping))
218 ;; The component group might be a virtual group.
219 (nmark (gnus-request-update-mark cgroup (caddr nart) mark)))
220 (when (and (= mark nmark)
221 (gnus-group-auto-expirable-p cgroup))
222 (setq mark gnus-expirable-mark))))
225 (defun nnvirtual-close-group (group &optional server)
226 (when (nnvirtual-possibly-change-group group server t)
227 ;; We copy the marks from this group to the component
229 (nnvirtual-update-marked)
230 ;; Reset all relevant variables.
231 (setq nnvirtual-current-group nil
232 nnvirtual-component-groups nil
233 nnvirtual-mapping nil)
234 (setq nnvirtual-group-alist
235 (delq (assoc group nnvirtual-group-alist) nnvirtual-group-alist)))
238 (defun nnvirtual-request-list (&optional server)
239 (nnheader-report 'nnvirtual "LIST is not implemented."))
241 (defun nnvirtual-request-newgroups (date &optional server)
242 (nnheader-report 'nnvirtual "NEWGROUPS is not supported."))
244 (defun nnvirtual-request-list-newsgroups (&optional server)
245 (nnheader-report 'nnvirtual "LIST NEWSGROUPS is not implemented."))
247 (defun nnvirtual-request-update-info (group info &optional server)
248 (when (nnvirtual-possibly-change-group group server)
249 (let ((map nnvirtual-mapping)
250 (marks (mapcar (lambda (m) (list (cdr m))) gnus-article-mark-lists))
255 (push (car m) reads))
256 (when (setq mr (nth 4 m))
258 (setcdr (setq op (assq (pop mr) marks)) (cons (car m) (cdr op))))))
261 (setcdr (car mr) (gnus-compress-sequence (sort (cdar mr) '<)))
263 (setcar (cddr info) (gnus-compress-sequence (nreverse reads)))
265 ;; Enter these new marks into the info of the group.
267 (setcar (nthcdr 3 info) marks)
268 ;; Add the marks lists to the end of the info.
270 (setcdr (nthcdr 2 info) (list marks))))
273 (defun nnvirtual-catchup-group (group &optional server all)
274 (nnvirtual-possibly-change-group group server t)
275 (let ((gnus-group-marked nnvirtual-component-groups)
276 (gnus-expert-user t))
277 ;; Make sure all groups are activated.
280 (when (not (numberp (car (gnus-gethash g gnus-newsrc-hashtb))))
281 (gnus-activate-group g)))
282 nnvirtual-component-groups)
284 (set-buffer gnus-group-buffer)
285 (gnus-group-catchup-current nil all))))
287 (defun nnvirtual-find-group-art (group article)
288 "Return the real group and article for virtual GROUP and ARTICLE."
289 (nnvirtual-possibly-change-group group nil t)
290 (let ((mart (assq article nnvirtual-mapping)))
292 (cons (cadr mart) (caddr mart)))))
295 ;;; Internal functions.
297 (defun nnvirtual-convert-headers ()
298 "Convert HEAD headers into NOV headers."
300 (set-buffer nntp-server-buffer)
301 (let* ((dependencies (make-vector 100 0))
302 (headers (gnus-get-newsgroup-headers dependencies))
305 (while (setq header (pop headers))
306 (insert (int-to-string (mail-header-number header)) "\t"
307 (or (mail-header-subject header) "") "\t"
308 (or (mail-header-from header) "") "\t"
309 (or (mail-header-date header) "") "\t"
310 (or (mail-header-id header) "") "\t"
311 (or (mail-header-references header) "") "\t"
312 (int-to-string (or (mail-header-chars header) 0)) "\t"
313 (int-to-string (or (mail-header-lines header) 0)) "\t"
314 (if (mail-header-xref header)
315 (concat "Xref: " (mail-header-xref header) "\t")
318 (defun nnvirtual-possibly-change-group (group regexp &optional check)
320 (when (or (not (equal group nnvirtual-current-group))
322 (and (setq inf (assoc group nnvirtual-group-alist))
324 (string= (nth 3 inf) regexp)
326 (setq nnvirtual-current-group (car inf))
327 (setq nnvirtual-component-groups (nth 1 inf))
328 (setq nnvirtual-mapping (nth 2 inf))))
329 (when (and regexp (not inf))
330 (and inf (setq nnvirtual-group-alist
331 (delq inf nnvirtual-group-alist)))
332 (setq nnvirtual-mapping nil)
333 (setq nnvirtual-current-group group)
334 (let ((newsrc gnus-newsrc-alist)
335 (virt-group (gnus-group-prefixed-name
336 nnvirtual-current-group '(nnvirtual ""))))
337 (setq nnvirtual-component-groups nil)
339 (and (string-match regexp (caar newsrc))
340 (not (string= (caar newsrc) virt-group))
341 (setq nnvirtual-component-groups
342 (cons (caar newsrc) nnvirtual-component-groups)))
343 (setq newsrc (cdr newsrc))))
344 (if nnvirtual-component-groups
346 (nnvirtual-create-mapping)
347 (setq nnvirtual-group-alist
348 (cons (list group nnvirtual-component-groups
349 nnvirtual-mapping regexp)
350 nnvirtual-group-alist)))
351 (nnheader-report 'nnvirtual "No component groups: %s" group)))))
352 nnvirtual-component-groups)
354 (defun nnvirtual-update-marked ()
355 "Copy marks from the virtual group to the component groups."
356 (let ((mark-lists gnus-article-mark-lists)
357 type list mart cgroups)
358 (when (and gnus-summary-buffer
359 (get-buffer gnus-summary-buffer)
360 (buffer-name (get-buffer gnus-summary-buffer)))
361 (set-buffer gnus-summary-buffer))
363 (setq type (cdar mark-lists))
364 (setq list (symbol-value (intern (format "gnus-newsgroup-%s"
365 (car (pop mark-lists))))))
367 (mapcar (lambda (g) (list g)) nnvirtual-component-groups))
369 (nconc (assoc (cadr (setq mart (assq (pop list) nnvirtual-mapping)))
371 (list (caddr mart))))
373 (gnus-add-marked-articles
374 (caar cgroups) type (cdar cgroups) nil t)
375 (gnus-group-update-group (car (pop cgroups)) t)))))
377 (defsubst nnvirtual-marks (article marks)
378 "Return a list of mark types for ARTICLE."
381 (when (memq article (cdar marks))
382 (push (caar marks) out))
383 (setq marks (cdr marks)))
386 (defun nnvirtual-create-mapping ()
387 "Create an article mapping for the current group."
388 (let* (div m marks list article
394 (let* ((active (or (gnus-active g) (gnus-activate-group g)))
395 (unreads (and active (gnus-list-of-unread-articles
397 (marks (gnus-uncompress-marks
398 (gnus-info-marks (gnus-get-info g)))))
401 (push (cons 'cache (gnus-cache-articles-in-group g))
404 (setq div (/ (float (car active))
405 (if (zerop (cdr active))
408 (list (* div (- n (car active)))
409 g n (and (memq n unreads) t)
410 (nnvirtual-marks n marks)))
411 (gnus-uncompress-range active))))))
412 nnvirtual-component-groups))
414 (< (car m1) (car m2)))))
416 (setq nnvirtual-mapping map)
417 ;; Nix out any old marks.
418 (let ((marks gnus-article-mark-lists))
419 (set (intern (format "gnus-newsgroup-%s" (car (pop marks)))) nil))
420 ;; Copy in all marks from the component groups.
421 (while (setq m (pop map))
422 (setcar m (setq article (incf i)))
423 (when (setq marks (nth 4 m))
426 (intern (concat "gnus-newsgroup-"
428 (car (rassq (pop marks)
429 gnus-article-mark-lists))))))
430 (cons article (symbol-value list))))))))
434 ;;; nnvirtual.el ends here