*** 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 (defvoo nnvirtual-component-groups nil
55   "Component group in this nnvirtual group.")
56
57 \f
58
59 (defconst nnvirtual-version "nnvirtual 1.0")
60
61 (defvoo nnvirtual-current-group nil)
62 (defvoo nnvirtual-mapping nil)
63
64 (defvoo nnvirtual-status-string "")
65
66 (eval-and-compile
67   (autoload 'gnus-cache-articles-in-group "gnus-cache"))
68
69 \f
70
71 ;;; Interface functions.
72
73 (nnoo-define-basics nnvirtual)
74
75 (deffoo nnvirtual-retrieve-headers (articles &optional newsgroup
76                                              server fetch-old)
77   (when (nnvirtual-possibly-change-server server)
78     (save-excursion
79       (set-buffer nntp-server-buffer)
80       (erase-buffer)
81       (if (stringp (car articles))
82           'headers
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)
89           (while articles
90             (setq article (assq (pop articles) nnvirtual-mapping))
91             (when (and (setq cgroup (cadr article))
92                        (gnus-check-server
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))
109                   (while (not (eobp))
110                     (delete-region 
111                      (point) (progn (read nntp-server-buffer) (point)))
112                     (princ (car article) (current-buffer))
113                     (beginning-of-line)
114                     (looking-at 
115                      "[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t")
116                     (goto-char (match-end 0))
117                     (or (search-forward 
118                          "\t" (save-excursion (end-of-line) (point)) t)
119                         (end-of-line))
120                     (while (= (char-after (1- (point))) ? )
121                       (forward-char -1)
122                       (delete-char 1))
123                     (if (eolp)
124                         (progn
125                           (end-of-line)
126                           (or (= (char-after (1- (point))) ?\t)
127                               (insert ?\t))
128                           (insert "Xref: " system-name " " cgroup ":")
129                           (princ (caddr article) (current-buffer))
130                           (insert "\t"))
131                       (insert "Xref: " system-name " " cgroup ":")
132                       (princ (caddr article) (current-buffer))
133                       (insert " ")
134                       (if (not (string= "" prefix))
135                           (while (re-search-forward 
136                                   "[^ ]+:[0-9]+"
137                                   (save-excursion (end-of-line) (point)) t)
138                             (save-excursion
139                               (goto-char (match-beginning 0))
140                               (insert prefix))))
141                       (end-of-line)
142                       (or (= (char-after (1- (point))) ?\t)
143                           (insert ?\t)))
144                     (forward-line 1))
145                   (set-buffer vbuf)
146                   (goto-char (point-max))
147                   (insert-buffer-substring nntp-server-buffer)))))
148           
149           ;; In case some of the articles have expired or been
150           ;; cancelled, we have to mark them as read in the
151           ;; component group.
152           (while unfetched
153             (when (cdar unfetched)
154               (gnus-group-make-articles-read 
155                (caar unfetched) (sort (cdar unfetched) '<)))
156             (setq unfetched (cdr unfetched)))
157
158           ;; The headers are ready for reading, so they are inserted into
159           ;; the nntp-server-buffer, which is where Gnus expects to find
160           ;; them.
161           (prog1
162               (save-excursion
163                 (set-buffer nntp-server-buffer)
164                 (erase-buffer)
165                 (insert-buffer-substring vbuf)
166                 'nov)
167             (kill-buffer vbuf)))))))
168
169 (deffoo nnvirtual-request-article (article &optional group server buffer)
170   (when (and (nnvirtual-possibly-change-server server)
171              (numberp article))
172     (let* ((amap (assq article nnvirtual-mapping))
173            (cgroup (cadr amap)))
174       (cond
175        ((not amap)
176         (nnheader-report 'nnvirtual "No such article: %s" article))
177        ((not (gnus-check-group cgroup))
178         (nnheader-report
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))
182        (t
183         (if buffer 
184             (save-excursion
185               (set-buffer buffer)
186               (gnus-request-article-this-buffer (caddr amap) cgroup))
187           (gnus-request-article (caddr amap) cgroup)))))))
188
189 (deffoo nnvirtual-open-server (server &optional defs)
190   (unless (assq 'nnvirtual-component-regexp defs)
191     (push `(nnvirtual-component-regexp ,server)
192           defs))
193   (nnoo-change-server 'nnvirtual server defs)
194   (if nnvirtual-component-groups
195       t
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))
200             group)
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)
208       t)))
209
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))
214   (cond
215    ((null nnvirtual-component-groups)
216     (setq nnvirtual-current-group nil)
217     (nnheader-report 'nnvirtual "No component groups in %s" group))
218    (t
219     (unless dont-check
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)))))
224
225 (deffoo nnvirtual-request-type (group &optional article)
226   (if (not article)
227       'unknown
228     (let ((mart (assq article nnvirtual-mapping)))
229       (when mart
230         (gnus-request-type (cadr mart) (car mart))))))
231
232 (deffoo nnvirtual-request-update-mark (group article mark)
233   (let* ((nart (assq article nnvirtual-mapping))
234          (cgroup (cadr nart))
235          ;; The component group might be a virtual group.
236          (nmark (gnus-request-update-mark cgroup (caddr nart) mark)))
237     (when (and nart
238                (= mark nmark)
239                (gnus-group-auto-expirable-p cgroup))
240       (setq mark gnus-expirable-mark)))
241   mark)
242     
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
249     ;; groups here.
250     (nnvirtual-update-marked))
251   t)
252     
253 (deffoo nnvirtual-request-list (&optional server) 
254   (nnheader-report 'nnvirtual "LIST is not implemented."))
255
256 (deffoo nnvirtual-request-newgroups (date &optional server)
257   (nnheader-report 'nnvirtual "NEWGROUPS is not supported."))
258
259 (deffoo nnvirtual-request-list-newsgroups (&optional server)
260   (nnheader-report 'nnvirtual "LIST NEWSGROUPS is not implemented."))
261
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))
266           reads mr m op)
267       ;; Go through the mapping.
268       (while map
269         (unless (nth 3 (setq m (pop map)))
270           ;; Read article.
271           (push (car m) reads))
272         ;; Copy marks.
273         (when (setq mr (nth 4 m))
274           (while mr
275             (setcdr (setq op (assq (pop mr) marks)) (cons (car m) (cdr op))))))
276       ;; Compress the marks and the reads.
277       (setq mr marks)
278       (while mr
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)))
284       (setq mr marks)
285       (while (cdr mr)
286         (if (cdadr mr)
287             (setq mr (cdr mr))
288           (setcdr mr (cddr mr))))
289
290       ;; Enter these new marks into the info of the group.
291       (if (nthcdr 3 info)
292           (setcar (nthcdr 3 info) marks)
293         ;; Add the marks lists to the end of the info.
294         (when marks
295           (setcdr (nthcdr 2 info) (list marks))))
296       t)))
297
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.
303     (mapcar
304      (lambda (g)
305        (when (not (numberp (car (gnus-gethash g gnus-newsrc-hashtb))))
306          (gnus-activate-group g)))
307      nnvirtual-component-groups)
308     (save-excursion
309       (set-buffer gnus-group-buffer)
310       (gnus-group-catchup-current nil all))))
311
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)))
315     (when mart
316       (cons (cadr mart) (caddr mart)))))
317
318 \f
319 ;;; Internal functions.
320
321 (defun nnvirtual-convert-headers ()
322   "Convert HEAD headers into NOV headers."
323   (save-excursion
324     (set-buffer nntp-server-buffer)
325     (let* ((dependencies (make-vector 100 0))
326            (headers (gnus-get-newsgroup-headers dependencies))
327            header)
328       (erase-buffer)
329       (while (setq header (pop headers))
330         (nnheader-insert-nov header)))))
331
332 (defun nnvirtual-possibly-change-server (server)
333   (or (not server)
334       (nnoo-current-server-p 'nnvirtual server)
335       (nnvirtual-open-server server)))
336
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))))
344       (setq cgroups 
345             (mapcar (lambda (g) (list g)) nnvirtual-component-groups))
346       (while list
347         (nconc (assoc (cadr (setq mart (assq (pop list) nnvirtual-mapping)))
348                       cgroups)
349                (list (caddr mart))))
350       (while cgroups
351         (gnus-add-marked-articles 
352          (caar cgroups) type (cdar cgroups) nil t)
353         (gnus-group-update-group (car (pop cgroups)) t)))))
354
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)))
360         m)
361     (while articles
362       (setq m (assq (pop articles) nnvirtual-mapping))
363       (nconc (assoc (nth 1 m) groups) (list (nth 2 m))))
364     (while groups
365       (gnus-update-read-articles (caar groups) (cdr (pop groups))))))
366
367 (defun nnvirtual-current-group ()
368   "Return the prefixed name of the current nnvirtual group."
369   (concat "nnvirtual:" nnvirtual-current-group))
370
371 (defsubst nnvirtual-marks (article marks)
372   "Return a list of mark types for ARTICLE."
373   (let (out)
374     (while marks
375       (when (memq article (cdar marks))
376         (push (caar marks) out))
377       (setq marks (cdr marks)))
378     out))
379
380 (defun nnvirtual-create-mapping ()
381   "Create an article mapping for the current group."
382   (let* ((div nil)
383          m unreads marks active 
384          (map (sort
385                (apply 
386                 'nconc
387                 (mapcar
388                  (lambda (g)
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))))
394                      (when gnus-use-cache
395                        (push (cons 'cache (gnus-cache-articles-in-group g))
396                              marks))
397                      (setq div (/ (float (car active)) 
398                                   (if (zerop (cdr active))
399                                       1 (cdr active))))
400                      (mapcar (lambda (n) 
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))
406                (lambda (m1 m2)
407                  (< (car m1) (car m2)))))
408          (i 0))
409     (setq nnvirtual-mapping map)
410     ;; Set the virtual article numbers.
411     (while (setq m (pop map))
412       (setcar m (incf i)))))
413
414 (provide 'nnvirtual)
415
416 ;;; nnvirtual.el ends here