*** 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 (nnvirtual-possibly-change-server server)
245     ;; Copy (un)read articles.
246     (nnvirtual-update-reads)
247     ;; We copy the marks from this group to the component
248     ;; groups here.
249     (nnvirtual-update-marked))
250   t)
251     
252 (deffoo nnvirtual-request-list (&optional server) 
253   (nnheader-report 'nnvirtual "LIST is not implemented."))
254
255 (deffoo nnvirtual-request-newgroups (date &optional server)
256   (nnheader-report 'nnvirtual "NEWGROUPS is not supported."))
257
258 (deffoo nnvirtual-request-list-newsgroups (&optional server)
259   (nnheader-report 'nnvirtual "LIST NEWSGROUPS is not implemented."))
260
261 (deffoo nnvirtual-request-update-info (group info &optional server)
262   (when (nnvirtual-possibly-change-server server)
263     (let ((map nnvirtual-mapping)
264           (marks (mapcar (lambda (m) (list (cdr m))) gnus-article-mark-lists))
265           reads mr m op)
266       ;; Go through the mapping.
267       (while map
268         (unless (nth 3 (setq m (pop map)))
269           ;; Read article.
270           (push (car m) reads))
271         ;; Copy marks.
272         (when (setq mr (nth 4 m))
273           (while mr
274             (setcdr (setq op (assq (pop mr) marks)) (cons (car m) (cdr op))))))
275       ;; Compress the marks and the reads.
276       (setq mr marks)
277       (while mr
278         (setcdr (car mr) (gnus-compress-sequence (sort (cdr (pop mr)) '<))))
279       (setcar (cddr info) (gnus-compress-sequence (nreverse reads)))
280       ;; Remove empty marks lists.
281       (while (and marks (not (cdar marks)))
282         (setq marks (cdr marks)))
283       (setq mr marks)
284       (while (cdr mr)
285         (if (cdadr mr)
286             (setq mr (cdr mr))
287           (setcdr mr (cddr mr))))
288
289       ;; Enter these new marks into the info of the group.
290       (if (nthcdr 3 info)
291           (setcar (nthcdr 3 info) marks)
292         ;; Add the marks lists to the end of the info.
293         (when marks
294           (setcdr (nthcdr 2 info) (list marks))))
295       t)))
296
297 (deffoo nnvirtual-catchup-group (group &optional server all)
298   (nnvirtual-possibly-change-server server)
299   (let ((gnus-group-marked (copy-sequence nnvirtual-component-groups))
300         (gnus-expert-user t))
301     ;; Make sure all groups are activated.
302     (mapcar
303      (lambda (g)
304        (when (not (numberp (car (gnus-gethash g gnus-newsrc-hashtb))))
305          (gnus-activate-group g)))
306      nnvirtual-component-groups)
307     (save-excursion
308       (set-buffer gnus-group-buffer)
309       (gnus-group-catchup-current nil all))))
310
311 (deffoo nnvirtual-find-group-art (group article)
312   "Return the real group and article for virtual GROUP and ARTICLE."
313   (let ((mart (assq article nnvirtual-mapping)))
314     (when mart
315       (cons (cadr mart) (caddr mart)))))
316
317 \f
318 ;;; Internal functions.
319
320 (defun nnvirtual-convert-headers ()
321   "Convert HEAD headers into NOV headers."
322   (save-excursion
323     (set-buffer nntp-server-buffer)
324     (let* ((dependencies (make-vector 100 0))
325            (headers (gnus-get-newsgroup-headers dependencies))
326            header)
327       (erase-buffer)
328       (while (setq header (pop headers))
329         (nnheader-insert-nov header)))))
330
331 (defun nnvirtual-possibly-change-server (server)
332   (or (not server)
333       (nnoo-current-server-p 'nnvirtual server)
334       (nnvirtual-open-server server)))
335
336 (defun nnvirtual-update-marked ()
337   "Copy marks from the virtual group to the component groups."
338   (let ((mark-lists gnus-article-mark-lists)
339         (marks (gnus-info-marks (gnus-get-info (nnvirtual-current-group))))
340         type list mart cgroups)
341     (while (setq type (cdr (pop mark-lists)))
342       (setq list (gnus-uncompress-range (cdr (assq type marks))))
343       (setq cgroups 
344             (mapcar (lambda (g) (list g)) nnvirtual-component-groups))
345       (while list
346         (nconc (assoc (cadr (setq mart (assq (pop list) nnvirtual-mapping)))
347                       cgroups)
348                (list (caddr mart))))
349       (while cgroups
350         (gnus-add-marked-articles 
351          (caar cgroups) type (cdar cgroups) nil t)
352         (gnus-group-update-group (car (pop cgroups)) t)))))
353
354 (defun nnvirtual-update-reads ()
355   "Copy (un)reads from the current group to the component groups."
356   (let ((groups (mapcar (lambda (g) (list g)) nnvirtual-component-groups))
357         (articles (gnus-list-of-unread-articles
358                    (nnvirtual-current-group)))
359         m)
360     (while articles
361       (setq m (assq (pop articles) nnvirtual-mapping))
362       (nconc (assoc (nth 1 m) groups) (list (nth 2 m))))
363     (while groups
364       (gnus-update-read-articles (caar groups) (cdr (pop groups))))))
365
366 (defun nnvirtual-current-group ()
367   "Return the prefixed name of the current nnvirtual group."
368   (concat "nnvirtual:" nnvirtual-current-group))
369
370 (defsubst nnvirtual-marks (article marks)
371   "Return a list of mark types for ARTICLE."
372   (let (out)
373     (while marks
374       (when (memq article (cdar marks))
375         (push (caar marks) out))
376       (setq marks (cdr marks)))
377     out))
378
379 (defun nnvirtual-create-mapping ()
380   "Create an article mapping for the current group."
381   (let* ((div nil)
382          m marks list article unreads marks active
383          (map (sort
384                (apply 
385                 'nconc
386                 (mapcar
387                  (lambda (g)
388                    (when (and (setq active (gnus-activate-group g))
389                               (> (cdr active) (car active)))
390                      (setq unreads (gnus-list-of-unread-articles g)
391                            marks (gnus-uncompress-marks
392                                   (gnus-info-marks (gnus-get-info g))))
393                      (when gnus-use-cache
394                        (push (cons 'cache (gnus-cache-articles-in-group g))
395                              marks))
396                      (setq div (/ (float (car active)) 
397                                   (if (zerop (cdr active))
398                                       1 (cdr active))))
399                      (mapcar (lambda (n) 
400                                (list (* div (- n (car active)))
401                                      g n (and (memq n unreads) t)
402                                      (inline (nnvirtual-marks n marks))))
403                              (gnus-uncompress-range active))))
404                  nnvirtual-component-groups))
405                (lambda (m1 m2)
406                  (< (car m1) (car m2)))))
407          (i 0))
408     (setq nnvirtual-mapping map)
409     ;; Set the virtual article numbers.
410     (while (setq m (pop map))
411       (setcar m (setq article (incf i))))))
412
413 (provide 'nnvirtual)
414
415 ;;; nnvirtual.el ends here