*** empty log message ***
[gnus] / lisp / nnvirtual.el
1 ;;; nnvirtual.el --- virtual newsgroups access for Gnus
2 ;; Copyright (C) 1994,95,96 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
5 ;;      Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
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)
13 ;; any later version.
14
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.
19
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.
24
25 ;;; Commentary:
26
27 ;; The other access methods (nntp, nnspool, etc) are general news
28 ;; access methods. This module relies on Gnus and can not be used
29 ;; separately.
30
31 ;;; Code:
32
33 (require 'nntp)
34 (require 'nnheader)
35 (require 'gnus)
36 (require 'nnoo)
37 (require 'gnus-util)
38 (require 'gnus-start)
39 (require 'gnus-sum)
40 (eval-when-compile (require 'cl))
41
42 (nnoo-declare nnvirtual)
43
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
49 virtual group.")
50
51 (defvoo nnvirtual-component-regexp nil
52   "*Regexp to match component groups.")
53
54 \f
55
56 (defconst nnvirtual-version "nnvirtual 1.0")
57
58 (defvoo nnvirtual-current-group nil)
59 (defvoo nnvirtual-component-groups nil)
60 (defvoo nnvirtual-mapping nil)
61
62 (defvoo nnvirtual-status-string "")
63
64 (eval-and-compile
65   (autoload 'gnus-cache-articles-in-group "gnus-cache"))
66
67 \f
68
69 ;;; Interface functions.
70
71 (nnoo-define-basics nnvirtual)
72
73 (deffoo nnvirtual-retrieve-headers (articles &optional newsgroup
74                                              server fetch-old)
75   (when (nnvirtual-possibly-change-server server)
76     (save-excursion
77       (set-buffer nntp-server-buffer)
78       (erase-buffer)
79       (if (stringp (car articles))
80           'headers
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)
87           (while articles
88             (setq article (assq (pop articles) nnvirtual-mapping))
89             (when (and (setq cgroup (cadr article))
90                        (gnus-check-server
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))
107                   (while (not (eobp))
108                     (delete-region 
109                      (point) (progn (read nntp-server-buffer) (point)))
110                     (princ (car article) (current-buffer))
111                     (beginning-of-line)
112                     (looking-at 
113                      "[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t")
114                     (goto-char (match-end 0))
115                     (or (search-forward 
116                          "\t" (save-excursion (end-of-line) (point)) t)
117                         (end-of-line))
118                     (while (= (char-after (1- (point))) ? )
119                       (forward-char -1)
120                       (delete-char 1))
121                     (if (eolp)
122                         (progn
123                           (end-of-line)
124                           (or (= (char-after (1- (point))) ?\t)
125                               (insert ?\t))
126                           (insert "Xref: " system-name " " cgroup ":")
127                           (princ (caddr article) (current-buffer))
128                           (insert "\t"))
129                       (insert "Xref: " system-name " " cgroup ":")
130                       (princ (caddr article) (current-buffer))
131                       (insert " ")
132                       (if (not (string= "" prefix))
133                           (while (re-search-forward 
134                                   "[^ ]+:[0-9]+"
135                                   (save-excursion (end-of-line) (point)) t)
136                             (save-excursion
137                               (goto-char (match-beginning 0))
138                               (insert prefix))))
139                       (end-of-line)
140                       (or (= (char-after (1- (point))) ?\t)
141                           (insert ?\t)))
142                     (forward-line 1))
143                   (set-buffer vbuf)
144                   (goto-char (point-max))
145                   (insert-buffer-substring nntp-server-buffer)))))
146           
147           ;; In case some of the articles have expired or been
148           ;; cancelled, we have to mark them as read in the
149           ;; component group.
150           (while unfetched
151             (when (cdar unfetched)
152               (gnus-group-make-articles-read 
153                (caar unfetched) (sort (cdar unfetched) '<)))
154             (setq unfetched (cdr unfetched)))
155
156           ;; The headers are ready for reading, so they are inserted into
157           ;; the nntp-server-buffer, which is where Gnus expects to find
158           ;; them.
159           (prog1
160               (save-excursion
161                 (set-buffer nntp-server-buffer)
162                 (erase-buffer)
163                 (insert-buffer-substring vbuf)
164                 'nov)
165             (kill-buffer vbuf)))))))
166
167 (deffoo nnvirtual-request-article (article &optional group server buffer)
168   (when (and (nnvirtual-possibly-change-server server)
169              (numberp article))
170     (let* ((amap (assq article nnvirtual-mapping))
171            (cgroup (cadr amap)))
172       (cond
173        ((not amap)
174         (nnheader-report 'nnvirtual "No such article: %s" article))
175        ((not (gnus-check-group cgroup))
176         (nnheader-report
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))
180        (t
181         (if buffer 
182             (save-excursion
183               (set-buffer buffer)
184               (gnus-request-article-this-buffer (caddr amap) cgroup))
185           (gnus-request-article (caddr amap) cgroup)))))))
186
187 (deffoo nnvirtual-open-server (server &optional defs)
188   (unless (assq 'nnvirtual-component-regexp defs)
189     (push `(nnvirtual-component-regexp ,server)
190           defs))
191   (nnoo-change-server 'nnvirtual server defs)
192   (if nnvirtual-component-groups
193       t
194     (setq nnvirtual-mapping nil)
195     ;; Go through the newsrc alist and find all component groups.
196     (let ((newsrc (cdr gnus-newsrc-alist))
197           group)
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)
205       t)))
206
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))
211   (cond
212    ((null nnvirtual-component-groups)
213     (setq nnvirtual-current-group nil)
214     (nnheader-report 'nnvirtual "No component groups in %s" group))
215    (t
216     (unless dont-check
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)))))
221
222 (deffoo nnvirtual-request-type (group &optional article)
223   (if (not article)
224       'unknown
225     (let ((mart (assq article nnvirtual-mapping)))
226       (when mart
227         (gnus-request-type (cadr mart) (car mart))))))
228
229 (deffoo nnvirtual-request-update-mark (group article mark)
230   (let* ((nart (assq article nnvirtual-mapping))
231          (cgroup (cadr nart))
232          ;; The component group might be a virtual group.
233          (nmark (gnus-request-update-mark cgroup (caddr nart) mark)))
234     (when (and nart
235                (= mark nmark)
236                (gnus-group-auto-expirable-p cgroup))
237       (setq mark gnus-expirable-mark)))
238   mark)
239     
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
245     ;; groups here.
246     (nnvirtual-update-marked))
247   t)
248     
249 (deffoo nnvirtual-request-list (&optional server) 
250   (nnheader-report 'nnvirtual "LIST is not implemented."))
251
252 (deffoo nnvirtual-request-newgroups (date &optional server)
253   (nnheader-report 'nnvirtual "NEWGROUPS is not supported."))
254
255 (deffoo nnvirtual-request-list-newsgroups (&optional server)
256   (nnheader-report 'nnvirtual "LIST NEWSGROUPS is not implemented."))
257
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))
262           reads mr m op)
263       ;; Go through the mapping.
264       (while map
265         (unless (nth 3 (setq m (pop map)))
266           ;; Read article.
267           (push (car m) reads))
268         ;; Copy marks.
269         (when (setq mr (nth 4 m))
270           (while mr
271             (setcdr (setq op (assq (pop mr) marks)) (cons (car m) (cdr op))))))
272       ;; Compress the marks and the reads.
273       (setq mr marks)
274       (while mr
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)))
280       (setq mr marks)
281       (while (cdr mr)
282         (if (cdadr mr)
283             (setq mr (cdr mr))
284           (setcdr mr (cddr mr))))
285
286       ;; Enter these new marks into the info of the group.
287       (if (nthcdr 3 info)
288           (setcar (nthcdr 3 info) marks)
289         ;; Add the marks lists to the end of the info.
290         (when marks
291           (setcdr (nthcdr 2 info) (list marks))))
292       t)))
293
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.
299     (mapcar
300      (lambda (g)
301        (when (not (numberp (car (gnus-gethash g gnus-newsrc-hashtb))))
302          (gnus-activate-group g)))
303      nnvirtual-component-groups)
304     (save-excursion
305       (set-buffer gnus-group-buffer)
306       (gnus-group-catchup-current nil all))))
307
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)))
311     (when mart
312       (cons (cadr mart) (caddr mart)))))
313
314 \f
315 ;;; Internal functions.
316
317 (defun nnvirtual-convert-headers ()
318   "Convert HEAD headers into NOV headers."
319   (save-excursion
320     (set-buffer nntp-server-buffer)
321     (let* ((dependencies (make-vector 100 0))
322            (headers (gnus-get-newsgroup-headers dependencies))
323            header)
324       (erase-buffer)
325       (while (setq header (pop headers))
326         (nnheader-insert-nov header)))))
327
328 (defun nnvirtual-possibly-change-server (server)
329   (or (not server)
330       (nnoo-current-server-p 'nnvirtual server)
331       (nnvirtual-open-server server)))
332
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))))
340       (setq cgroups 
341             (mapcar (lambda (g) (list g)) nnvirtual-component-groups))
342       (while list
343         (nconc (assoc (cadr (setq mart (assq (pop list) nnvirtual-mapping)))
344                       cgroups)
345                (list (caddr mart))))
346       (while cgroups
347         (gnus-add-marked-articles 
348          (caar cgroups) type (cdar cgroups) nil t)
349         (gnus-group-update-group (car (pop cgroups)) t)))))
350
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)))
356         m)
357     (while articles
358       (setq m (assq (pop articles) nnvirtual-mapping))
359       (nconc (assoc (nth 1 m) groups) (list (nth 2 m))))
360     (while groups
361       (gnus-update-read-articles (caar groups) (cdr (pop groups))))))
362
363 (defun nnvirtual-current-group ()
364   "Return the prefixed name of the current nnvirtual group."
365   (concat "nnvirtual:" nnvirtual-current-group))
366
367 (defsubst nnvirtual-marks (article marks)
368   "Return a list of mark types for ARTICLE."
369   (let (out)
370     (while marks
371       (when (memq article (cdar marks))
372         (push (caar marks) out))
373       (setq marks (cdr marks)))
374     out))
375
376 (defun nnvirtual-create-mapping ()
377   "Create an article mapping for the current group."
378   (let* ((div nil)
379          m marks list article unreads marks active
380          (map (sort
381                (apply 
382                 'nconc
383                 (mapcar
384                  (lambda (g)
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))))
390                      (when gnus-use-cache
391                        (push (cons 'cache (gnus-cache-articles-in-group g))
392                              marks))
393                      (setq div (/ (float (car active)) 
394                                   (if (zerop (cdr active))
395                                       1 (cdr active))))
396                      (mapcar (lambda (n) 
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))
402                (lambda (m1 m2)
403                  (< (car m1) (car m2)))))
404          (i 0))
405     (setq nnvirtual-mapping map)
406     ;; Set the virtual article numbers.
407     (while (setq m (pop map))
408       (setcar m (setq article (incf i))))))
409
410 (provide 'nnvirtual)
411
412 ;;; nnvirtual.el ends here