*** 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             (when (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     (when mart
280       (cons (cadr mart) (caddr mart)))))
281
282 \f
283 ;;; Internal functions.
284
285 (defun nnvirtual-convert-headers ()
286   "Convert HEAD headers into NOV headers."
287   (save-excursion
288     (set-buffer nntp-server-buffer)
289     (let* ((dependencies (make-vector 100 0))
290            (headers (gnus-get-newsgroup-headers dependencies))
291            header)
292       (erase-buffer)
293       (while (setq header (pop headers))
294         (insert (int-to-string (mail-header-number header)) "\t"
295                 (or (mail-header-subject header) "") "\t"
296                 (or (mail-header-from header) "") "\t"
297                 (or (mail-header-date header) "") "\t"
298                 (or (mail-header-id header) "") "\t"
299                 (or (mail-header-references header) "") "\t"
300                 (int-to-string (or (mail-header-chars header) 0)) "\t"
301                 (int-to-string (or (mail-header-lines header) 0)) "\t"
302                 (if (mail-header-xref header) 
303                     (concat "Xref: " (mail-header-xref header) "\t")
304                   "") "\n")))))
305
306 (defun nnvirtual-possibly-change-group (group regexp &optional dont-check)
307   (let ((inf t))
308     (unless (equal group nnvirtual-current-group)
309       (and (setq inf (assoc group nnvirtual-group-alist))
310            regexp
311            (string= (nth 3 inf) regexp)
312            (progn
313              (setq nnvirtual-current-group (car inf))
314              (setq nnvirtual-component-groups (nth 1 inf))
315              (setq nnvirtual-mapping (nth 2 inf)))))
316     (when (and regexp
317                (or (not inf)
318                    (not dont-check)))
319       (and inf (setq nnvirtual-group-alist 
320                      (delq inf nnvirtual-group-alist)))
321       (setq nnvirtual-mapping nil)
322       (setq nnvirtual-current-group group)
323       (let ((newsrc gnus-newsrc-alist)
324             (virt-group (gnus-group-prefixed-name 
325                          nnvirtual-current-group '(nnvirtual ""))))
326         (setq nnvirtual-component-groups nil)
327         (while newsrc
328           (and (string-match regexp (car (car newsrc)))
329                (not (string= (car (car newsrc)) virt-group))
330                (setq nnvirtual-component-groups
331                      (cons (car (car newsrc)) nnvirtual-component-groups)))
332           (setq newsrc (cdr newsrc))))
333       (if nnvirtual-component-groups
334           (progn
335             (nnvirtual-create-mapping)
336             (setq nnvirtual-group-alist
337                   (cons (list group nnvirtual-component-groups 
338                               nnvirtual-mapping regexp)
339                         nnvirtual-group-alist)))
340         (nnheader-report 'nnvirtual "No component groups: %s" group))))
341   nnvirtual-component-groups)
342
343 (defun nnvirtual-update-marked ()
344   "Copy marks from the virtual group to the component groups."
345   (let ((mark-lists gnus-article-mark-lists)
346         (uncompressed '(score bookmark))
347         type list calist mart cgroups)
348     (while mark-lists
349       (setq type (cdar mark-lists))
350       (setq list (symbol-value (intern (format "gnus-newsgroup-%s"
351                                                (car (pop mark-lists))))))
352       (setq cgroups 
353             (mapcar (lambda (g) (list g)) nnvirtual-component-groups))
354       (while list
355         (nconc (assoc (cadr (setq mart (assq (pop list) nnvirtual-mapping)))
356                       cgroups)
357                (list (caddr mart))))
358       (while cgroups
359         (gnus-add-marked-articles 
360          (caar cgroups) type (cdar cgroups) nil t)
361         (gnus-group-update-group (car (pop cgroups)) t)))))
362
363 (defun nnvirtual-marks (article marks)
364   "Return a list of mark types for ARTICLE."
365   (let (out)
366     (while marks
367       (when (memq article (cdar marks))
368         (push (caar marks) out))
369       (setq marks (cdr marks)))
370     out))
371
372 (defun nnvirtual-create-mapping ()
373   "Create an article mapping for the current group."
374   (let* (div
375          (map (sort
376                (apply 
377                 'nconc
378                 (mapcar
379                  (lambda (g)
380                    (let* ((active (or (gnus-active g) (gnus-activate-group g)))
381                           (unreads (and active (gnus-list-of-unread-articles
382                                                 g)))
383                           (marks (gnus-uncompress-marks
384                                   (gnus-info-marks (gnus-get-info g)))))
385                      (when active
386                        (when gnus-use-cache
387                          (push (cons 'cache (gnus-cache-articles-in-group g))
388                                marks))
389                        (when active
390                          (setq div (/ (float (car active)) 
391                                       (if (zerop (cdr active))
392                                           1 (cdr active))))
393                          (mapcar (lambda (n) 
394                                    (list (* div (- n (car active)))
395                                          g n (and (memq n unreads) t)
396                                          (nnvirtual-marks n marks)))
397                                  (gnus-uncompress-range active))))))
398                  nnvirtual-component-groups))
399                (lambda (m1 m2)
400                  (< (car m1) (car m2)))))
401          (i 0))
402     (setq nnvirtual-mapping map)
403     (while map
404       (setcar (pop map) (incf i)))))
405
406 (provide 'nnvirtual)
407
408 ;;; nnvirtual.el ends here