*** 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 (eval-when-compile (require 'cl))
37
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
43 virtual group.")
44
45 \f
46
47 (defconst nnvirtual-version "nnvirtual 1.0"
48   "Version number of this version of nnvirtual.")
49
50 (defvar nnvirtual-group-alist nil)
51 (defvar nnvirtual-current-group nil)
52 (defvar nnvirtual-component-groups nil)
53 (defvar nnvirtual-mapping nil)
54
55 (defvar nnvirtual-status-string "")
56
57 (eval-and-compile
58   (autoload 'gnus-cache-articles-in-group "gnus-cache"))
59
60 \f
61
62 ;;; Interface functions.
63
64 (defun nnvirtual-retrieve-headers (articles &optional newsgroup server fetch-old)
65   (when (nnvirtual-possibly-change-group newsgroup server t)
66     (save-excursion
67       (if (stringp (car articles))
68           'headers
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)
75           (while articles
76             (setq article (assq (pop articles) nnvirtual-mapping))
77             (setq cgroup (cadr article))
78             (gnus-request-group cgroup t)
79             (setq prefix (gnus-group-real-prefix cgroup))
80             (when (setq result (gnus-retrieve-headers 
81                                 (list (caddr article)) cgroup))
82               (set-buffer nntp-server-buffer)
83               (if (zerop (buffer-size))
84                   (nconc (assq cgroup unfetched) (list (caddr article)))
85                 ;; If we got HEAD headers, we convert them into NOV
86                 ;; headers.  This is slow, inefficient and, come to think
87                 ;; of it, downright evil.  So sue me.  I couldn't be
88                 ;; bothered to write a header parse routine that could
89                 ;; parse a mixed HEAD/NOV buffer.
90                 (when (eq result 'headers)
91                   (nnvirtual-convert-headers))
92                 (goto-char (point-min))
93                 (while (not (eobp))
94                   (delete-region 
95                    (point) (progn (read nntp-server-buffer) (point)))
96                   (insert (int-to-string (car article)))
97                   (beginning-of-line)
98                   (looking-at 
99                    "[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t")
100                   (goto-char (match-end 0))
101                   (or (search-forward 
102                        "\t" (save-excursion (end-of-line) (point)) t)
103                       (end-of-line))
104                   (while (= (char-after (1- (point))) ? )
105                     (forward-char -1)
106                     (delete-char 1))
107                   (if (eolp)
108                       (progn
109                         (end-of-line)
110                         (or (= (char-after (1- (point))) ?\t)
111                             (insert ?\t))
112                         (insert (format "Xref: %s %s:%d\t" (system-name) 
113                                         cgroup (caddr article))))
114                     (if (not (string= "" prefix))
115                         (while (re-search-forward 
116                                 "[^ ]+:[0-9]+"
117                                 (save-excursion (end-of-line) (point)) t)
118                           (save-excursion
119                             (goto-char (match-beginning 0))
120                             (insert prefix))))
121                     (end-of-line)
122                     (or (= (char-after (1- (point))) ?\t)
123                         (insert ?\t)))
124                   (forward-line 1))
125                 (set-buffer vbuf)
126                 (goto-char (point-max))
127                 (insert-buffer-substring nntp-server-buffer))))
128           
129           ;; In case some of the articles have expired or been
130           ;; cancelled, we have to mark them as read in the
131           ;; component group.
132           (while unfetched
133             (when (cdar unfetched)
134               (gnus-group-make-articles-read 
135                (caar unfetched) (sort (cdar unfetched) '<)))
136             (setq unfetched (cdr unfetched)))
137
138           ;; The headers are ready for reading, so they are inserted into
139           ;; the nntp-server-buffer, which is where Gnus expects to find
140           ;; them.
141           (prog1
142               (save-excursion
143                 (set-buffer nntp-server-buffer)
144                 (erase-buffer)
145                 (insert-buffer-substring vbuf)
146                 'nov)
147             (kill-buffer vbuf)))))))
148
149 (defun nnvirtual-open-server (server &optional something)
150   (nnheader-init-server-buffer))
151
152 (defun nnvirtual-close-server (&rest dum)
153   t)
154
155 (defun nnvirtual-request-close ()
156   (setq nnvirtual-current-group nil
157         nnvirtual-component-groups nil
158         nnvirtual-mapping nil
159         nnvirtual-group-alist nil)
160   t)
161
162 (defun nnvirtual-server-opened (&optional server)
163   (and nntp-server-buffer
164        (get-buffer nntp-server-buffer)))
165
166 (defun nnvirtual-status-message (&optional server)
167   nnvirtual-status-string)
168
169 (defun nnvirtual-request-article (article &optional group server buffer)
170   (when (and (nnvirtual-possibly-change-group group server t)
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 (defun nnvirtual-request-group (group &optional server dont-check)
190   (cond
191    ((null (nnvirtual-possibly-change-group
192            group server 
193            (if nnvirtual-always-rescan nil dont-check)))
194     (setq nnvirtual-current-group nil)
195     (nnheader-report 'nnvirtual "No component groups in %s" group))
196    (t
197     (let ((len (length nnvirtual-mapping)))
198       (nnheader-insert "211 %d 1 %d %s\n" len len group)))))
199
200 (defun nnvirtual-request-type (group &optional article)
201   (when (nnvirtual-possibly-change-group group nil t)
202     (if (not article)
203         'unknown
204       (let ((mart (assq article nnvirtual-mapping)))
205         (when mart
206           (gnus-request-type (cadr mart) (car mart)))))))
207
208 (defun nnvirtual-request-update-mark (group article mark)
209   (when (nnvirtual-possibly-change-group group nil t)
210     (let* ((nart (assq article nnvirtual-mapping))
211            (cgroup (cadr nart))
212            ;; The component group might be a virtual group.
213            (nmark (gnus-request-update-mark cgroup (caddr nart) mark)))
214       (when (and (= mark nmark)
215                  (gnus-group-auto-expirable-p cgroup))
216         (setq mark gnus-expirable-mark))))
217   mark)
218     
219 (defun nnvirtual-close-group (group &optional server)
220   (when (nnvirtual-possibly-change-group group server t)
221     ;; We copy the marks from this group to the component
222     ;; groups here.
223     (nnvirtual-update-marked)
224     ;; Reset all relevant variables.
225     (setq nnvirtual-current-group nil
226           nnvirtual-component-groups nil
227           nnvirtual-mapping nil)
228     (setq nnvirtual-group-alist 
229           (delq (assoc group nnvirtual-group-alist) nnvirtual-group-alist)))
230   t)
231     
232 (defun nnvirtual-request-list (&optional server) 
233   (nnheader-report 'nnvirtual "LIST is not implemented."))
234
235 (defun nnvirtual-request-newgroups (date &optional server)
236   (nnheader-report 'nnvirtual "NEWGROUPS is not supported."))
237
238 (defun nnvirtual-request-list-newsgroups (&optional server)
239   (nnheader-report 'nnvirtual "LIST NEWSGROUPS is not implemented."))
240
241 (defun nnvirtual-request-update-info (group info &optional server)
242   (when (nnvirtual-possibly-change-group group server)
243     (let ((map nnvirtual-mapping)
244           (marks (mapcar (lambda (m) (list (cdr m))) gnus-article-mark-lists))
245           reads marks mr m op)
246       (while map
247         (setq m (pop map))
248         (unless (nth 3 m)
249           (push (car m) reads))
250         (when (setq mr (nth 4 m))
251           (while mr
252             (setcdr (setq op (assq (pop mr) marks)) (cons (car m) (cdr op))))))
253       (setq mr marks)
254       (while mr
255         (setcdr (car mr) (gnus-compress-sequence (sort (cdar mr) '<)))
256         (setq mr (cdr mr)))
257       (setcar (cddr info) (gnus-compress-sequence (nreverse reads)))
258       
259       ;; Enter these new marks into the info of the group.
260       (if (nthcdr 3 info)
261           (setcar (nthcdr 3 info) marks)
262         ;; Add the marks lists to the end of the info.
263         (when marks
264           (setcdr (nthcdr 2 info) (list marks))))
265       t)))
266
267 (defun nnvirtual-catchup-group (group &optional server all)
268   (nnvirtual-possibly-change-group group server t)
269   (let ((gnus-group-marked nnvirtual-component-groups)
270         (gnus-expert-user t))
271     (save-excursion
272       (set-buffer gnus-group-buffer)
273       (gnus-group-catchup-current nil all))))
274
275 (defun nnvirtual-find-group-art (group article)
276   "Return the real group and article for virtual GROUP and ARTICLE."
277   (nnvirtual-possibly-change-group group nil t)
278   (let ((mart (assq article nnvirtual-mapping)))
279     (cons (cadr mart) (caddr mart))))
280
281 \f
282 ;;; Internal functions.
283
284 (defun nnvirtual-convert-headers ()
285   "Convert HEAD headers into NOV headers."
286   (save-excursion
287     (set-buffer nntp-server-buffer)
288     (let* ((dependencies (make-vector 100 0))
289            (headers (gnus-get-newsgroup-headers dependencies))
290            header)
291       (erase-buffer)
292       (while (setq header (pop headers))
293         (insert (int-to-string (mail-header-number header)) "\t"
294                 (or (mail-header-subject header) "") "\t"
295                 (or (mail-header-from header) "") "\t"
296                 (or (mail-header-date header) "") "\t"
297                 (or (mail-header-id header) "") "\t"
298                 (or (mail-header-references header) "") "\t"
299                 (int-to-string (or (mail-header-chars header) 0)) "\t"
300                 (int-to-string (or (mail-header-lines header) 0)) "\t"
301                 (if (mail-header-xref header) 
302                     (concat "Xref: " (mail-header-xref header) "\t")
303                   "") "\n")))))
304
305 (defun nnvirtual-possibly-change-group (group regexp &optional dont-check)
306   (let ((inf t))
307     (unless (equal group nnvirtual-current-group)
308       (and (setq inf (assoc group nnvirtual-group-alist))
309            regexp
310            (string= (nth 3 inf) regexp)
311            (progn
312              (setq nnvirtual-current-group (car inf))
313              (setq nnvirtual-component-groups (nth 1 inf))
314              (setq nnvirtual-mapping (nth 2 inf)))))
315     (when (and regexp
316                (or (not inf)
317                    (not dont-check)))
318       (and inf (setq nnvirtual-group-alist 
319                      (delq inf nnvirtual-group-alist)))
320       (setq nnvirtual-mapping nil)
321       (setq nnvirtual-current-group group)
322       (let ((newsrc gnus-newsrc-alist)
323             (virt-group (gnus-group-prefixed-name 
324                          nnvirtual-current-group '(nnvirtual ""))))
325         (setq nnvirtual-component-groups nil)
326         (while newsrc
327           (and (string-match regexp (car (car newsrc)))
328                (not (string= (car (car newsrc)) virt-group))
329                (setq nnvirtual-component-groups
330                      (cons (car (car newsrc)) nnvirtual-component-groups)))
331           (setq newsrc (cdr newsrc))))
332       (if nnvirtual-component-groups
333           (progn
334             (nnvirtual-create-mapping)
335             (setq nnvirtual-group-alist
336                   (cons (list group nnvirtual-component-groups 
337                               nnvirtual-mapping regexp)
338                         nnvirtual-group-alist)))
339         (nnheader-report 'nnvirtual "No component groups: %s" group))))
340   nnvirtual-component-groups)
341
342 (defun nnvirtual-update-marked ()
343   "Copy marks from the virtual group to the component groups."
344   (let ((mark-lists gnus-article-mark-lists)
345         (uncompressed '(score bookmark))
346         type list calist mart cgroups)
347     (while mark-lists
348       (setq type (cdar mark-lists))
349       (setq list (symbol-value (intern (format "gnus-newsgroup-%s"
350                                                (car (pop mark-lists))))))
351       (setq cgroups 
352             (mapcar (lambda (g) (list g)) nnvirtual-component-groups))
353       (while list
354         (nconc (assoc (cadr (setq mart (assq (pop list) nnvirtual-mapping)))
355                       cgroups)
356                (list (caddr mart))))
357       (while cgroups
358         (gnus-add-marked-articles 
359          (caar cgroups) type (cdar cgroups) nil t)
360         (gnus-group-update-group (car (pop cgroups)) t)))))
361
362 (defun nnvirtual-marks (article marks)
363   "Return a list of mark types for ARTICLE."
364   (let (out)
365     (while marks
366       (when (memq article (cdar marks))
367         (push (caar marks) out))
368       (setq marks (cdr marks)))
369     out))
370
371 (defun nnvirtual-create-mapping ()
372   "Create an article mapping for the current group."
373   (let* (div
374          (map (sort
375                (apply 
376                 'nconc
377                 (mapcar
378                  (lambda (g)
379                    (let* ((active (or (gnus-active g) (gnus-activate-group g)))
380                           (unreads (and active (gnus-list-of-unread-articles
381                                                 g)))
382                           (marks (gnus-uncompress-marks
383                                   (gnus-info-marks (gnus-get-info g)))))
384                      (when active
385                        (when gnus-use-cache
386                          (push (cons 'cache (gnus-cache-articles-in-group g))
387                                marks))
388                        (when active
389                          (setq div (/ (float (car active)) 
390                                       (if (zerop (cdr active))
391                                           1 (cdr active))))
392                          (mapcar (lambda (n) 
393                                    (list (* div (- n (car active)))
394                                          g n (and (memq n unreads) t)
395                                          (nnvirtual-marks n marks)))
396                                  (gnus-uncompress-range active))))))
397                  nnvirtual-component-groups))
398                (lambda (m1 m2)
399                  (< (car m1) (car m2)))))
400          (i 0))
401     (setq nnvirtual-mapping map)
402     (while map
403       (setcar (pop map) (incf i)))))
404
405 (provide 'nnvirtual)
406
407 ;;; nnvirtual.el ends here