*** empty log message ***
[gnus] / lisp / nnvirtual.el
1 ;;; nnvirtual.el --- virtual newsgroups access for Gnus
2 ;; Copyright (C) 1994,95 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
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23
24 ;;; Commentary:
25
26 ;; The other access methods (nntp, nnspool, etc) are general news
27 ;; access methods. This module relies on Gnus and can not be used
28 ;; separately.
29
30 ;;; Code:
31
32 (require 'nntp)
33 (require 'nnheader)
34 (require 'gnus)
35
36 \f
37
38 (defconst nnvirtual-version "nnvirtual 1.0"
39   "Version number of this version of nnvirtual.")
40
41 (defvar nnvirtual-group-alist nil)
42 (defvar nnvirtual-current-group nil)
43 (defvar nnvirtual-current-groups nil)
44 (defvar nnvirtual-current-mapping nil)
45
46 (defvar nnvirtual-do-not-open nil)
47
48 (defvar nnvirtual-status-string "")
49
50 \f
51
52 ;;; Interface functions.
53
54 (defun nnvirtual-retrieve-headers (sequence &optional newsgroup server fetch-old)
55   "Retrieve the headers for the articles in SEQUENCE."
56   (nnvirtual-possibly-change-newsgroups newsgroup server t)
57   (save-excursion
58     (set-buffer (get-buffer-create "*virtual headers*"))
59     (buffer-disable-undo (current-buffer))
60     (erase-buffer)
61     (if (stringp (car sequence))
62         'headers
63       (let ((map nnvirtual-current-mapping)
64             (offset 0)
65             articles beg group active top article result prefix
66             fetched-articles group-method)
67         (while sequence
68           (while (< (car (car map)) (car sequence))
69             (setq offset (car (car map)))
70             (setq map (cdr map)))
71           (setq top (car (car map)))
72           (setq group (nth 1 (car map)))
73           (setq prefix (gnus-group-real-prefix group))
74           (setq active (nth 2 (car map)))
75           (setq articles nil)
76           (while (and sequence (<= (car sequence) top))
77             (setq articles (cons (- (+ active (car sequence)) offset) 
78                                  articles))
79             (setq sequence (cdr sequence)))
80           (setq articles (nreverse articles))
81           (if (and articles
82                    (setq result 
83                          (progn
84                            (setq group-method 
85                                  (gnus-find-method-for-group group))
86                            (and (or (gnus-server-opened group-method)
87                                     (gnus-open-server group-method))
88                                 (gnus-request-group group t)
89                                 (gnus-retrieve-headers articles group)))))
90               (save-excursion
91                 (set-buffer nntp-server-buffer)
92                 ;; If we got HEAD headers, we convert them into NOV
93                 ;; headers. This is slow, inefficient and, come to think
94                 ;; of it, downright evil. So sue me. I couldn't be
95                 ;; bothered to write a header parse routine that could
96                 ;; parse a mixed HEAD/NOV buffer.
97                 (and (eq result 'headers) (nnvirtual-convert-headers))
98                 (goto-char (point-min))
99                 (setq fetched-articles nil)
100                 (while (not (eobp))
101                   (setq beg (point)
102                         article (read nntp-server-buffer)
103                         fetched-articles (cons article fetched-articles))
104                   (delete-region beg (point))
105                   (insert (int-to-string (+ (- article active) offset)))
106                   (beginning-of-line)
107                   (looking-at 
108                    "[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t")
109                   (goto-char (match-end 0))
110                   (or (search-forward 
111                        "\t" (save-excursion (end-of-line) (point)) t)
112                       (end-of-line))
113                   (while (= (char-after (1- (point))) ? )
114                     (forward-char -1)
115                     (delete-char 1))
116                   (if (eolp)
117                       (progn
118                         (end-of-line)
119                         (or (= (char-after (1- (point))) ?\t)
120                             (insert ?\t))
121                         (insert (format "Xref: %s %s:%d\t" (system-name) 
122                                         group article)))
123                     (if (not (string= "" prefix))
124                         (while (re-search-forward 
125                                 "[^ ]+:[0-9]+"
126                                 (save-excursion (end-of-line) (point)) t)
127                           (save-excursion
128                             (goto-char (match-beginning 0))
129                             (insert prefix))))
130                     (end-of-line)
131                     (or (= (char-after (1- (point))) ?\t)
132                         (insert ?\t)))
133                   (forward-line 1))))
134           (goto-char (point-max))
135           (insert-buffer-substring nntp-server-buffer)
136           ;; We have now massaged and inserted the headers from one
137           ;; group. In case some of the articles have expired or been
138           ;; cancelled, we have to mark them as read in the component
139           ;; group. 
140           (let ((unfetched (gnus-sorted-complement 
141                             articles (nreverse fetched-articles))))
142             (and unfetched
143                  (gnus-group-make-articles-read group unfetched))))
144         ;; The headers are ready for reading, so they are inserted into
145         ;; the nntp-server-buffer, which is where Gnus expects to find
146         ;; them.
147         (prog1
148             (save-excursion
149               (set-buffer nntp-server-buffer)
150               (erase-buffer)
151               (insert-buffer-substring "*virtual headers*")
152               'nov)
153           (kill-buffer (current-buffer)))))))
154
155 (defun nnvirtual-open-server (newsgroups &optional something)
156   "Open a virtual newsgroup that contains NEWSGROUPS."
157   (nnheader-init-server-buffer))
158
159 (defun nnvirtual-close-server (&rest dum)
160   "Close news server."
161   t)
162
163 (defun nnvirtual-request-close ()
164   (setq nnvirtual-current-group nil
165         nnvirtual-current-groups nil
166         nnvirtual-current-mapping nil
167         nnvirtual-group-alist nil)
168   t)
169
170 (defun nnvirtual-server-opened (&optional server)
171   "Return server process status, T or NIL.
172 If the stream is opened, return T, otherwise return NIL."
173   (and nntp-server-buffer
174        (get-buffer nntp-server-buffer)))
175
176 (defun nnvirtual-status-message (&optional server)
177   "Return server status response as string."
178   nnvirtual-status-string)
179
180 (defun nnvirtual-request-article (article &optional newsgroup server buffer)
181   "Select article by message number."
182   (nnvirtual-possibly-change-newsgroups newsgroup server t)
183   (and (numberp article)
184        (let ((map nnvirtual-current-mapping)
185              (offset 0)
186              group-method)
187          (while (< (car (car map)) article)
188            (setq offset (car (car map)))
189            (setq map (cdr map)))
190          (setq group-method (gnus-find-method-for-group (nth 1 (car map))))
191          (or (gnus-server-opened group-method)
192              (gnus-open-server group-method))
193          (gnus-request-group (nth 1 (car map)) t)
194          (gnus-request-article (- (+ (nth 2 (car map)) article) offset)
195                                (nth 1 (car map)) buffer))))
196
197 (defun nnvirtual-request-group (group &optional server dont-check)
198   "Make GROUP the current newsgroup."
199   (nnvirtual-possibly-change-newsgroups group server dont-check)
200   (let ((map nnvirtual-current-mapping))
201     (save-excursion
202       (set-buffer nntp-server-buffer)
203       (erase-buffer)
204       (if map
205           (progn
206             (while (cdr map)
207               (setq map (cdr map)))
208             (insert (format "211 %d 1 %d %s\n" (car (car map)) 
209                             (car (car map)) group))
210             t)
211         (setq nnvirtual-status-string "No component groups")
212         (setq nnvirtual-current-group nil)
213         nil))))
214
215 (defun nnvirtual-request-type (group &optional article)
216   (nnvirtual-possibly-change-newsgroups group nil)
217   (if (not article)
218       'unknown
219     (gnus-request-type (car (nnvirtual-art-group article)))))
220     
221 (defun nnvirtual-close-group (group &optional server)
222   (if (not nnvirtual-current-group)
223       ()
224     (nnvirtual-possibly-change-newsgroups group server t)
225     (nnvirtual-update-marked)
226     (setq nnvirtual-current-group nil
227           nnvirtual-current-groups nil
228           nnvirtual-current-mapping nil)
229     (setq nnvirtual-group-alist 
230           (delq (assoc group nnvirtual-group-alist) nnvirtual-group-alist))))
231
232 (defun nnvirtual-request-list (&optional server) 
233   (setq nnvirtual-status-string "nnvirtual: LIST is not implemented.")
234   nil)
235
236 (defun nnvirtual-request-newgroups (date &optional server)
237   "List new groups."
238   (setq nnvirtual-status-string "NEWGROUPS is not supported.")
239   nil)
240
241 (defun nnvirtual-request-list-newsgroups (&optional server)
242   (setq nnvirtual-status-string
243         "nnvirtual: LIST NEWSGROUPS is not implemented.")
244   nil)
245
246 \f
247 ;;; Internal functions.
248
249 (defun nnvirtual-convert-headers ()
250   "Convert HEAD headers into NOV headers."
251   (save-excursion
252     (set-buffer nntp-server-buffer)
253     (let* ((dependencies (make-vector 100 0))
254            (headers (gnus-get-newsgroup-headers dependencies))
255            header)
256       (erase-buffer)
257       (while headers
258         (setq header (pop headers))
259         (insert (int-to-string (mail-header-number header)) "\t"
260                 (or (mail-header-subject header) "") "\t"
261                 (or (mail-header-from header) "") "\t"
262                 (or (mail-header-date header) "") "\t"
263                 (or (mail-header-id header) "") "\t"
264                 (or (mail-header-references header) "") "\t"
265                 (int-to-string (or (mail-header-chars header) 0)) "\t"
266                 (int-to-string (or (mail-header-lines header) 0)) "\t"
267                 (if (mail-header-xref header) 
268                     (concat "Xref: " (mail-header-xref header) "\t")
269                   "") "\n")))))
270
271 (defun nnvirtual-possibly-change-newsgroups (group regexp &optional check)
272   (let ((inf t))
273     (or (not group)
274         (and nnvirtual-current-group
275              (string= group nnvirtual-current-group))
276         (and (setq inf (assoc group nnvirtual-group-alist))
277              (string= (nth 3 inf) regexp)
278              (progn
279                (setq nnvirtual-current-group (car inf))
280                (setq nnvirtual-current-groups (nth 1 inf))
281                (setq nnvirtual-current-mapping (nth 2 inf)))))
282     (if (or (not check) (not inf))
283         (progn
284           (and inf (setq nnvirtual-group-alist 
285                          (delq inf nnvirtual-group-alist)))
286           (setq nnvirtual-current-mapping nil)
287           (setq nnvirtual-current-group group)
288           (let ((newsrc gnus-newsrc-alist)
289                 (virt-group (gnus-group-prefixed-name 
290                              nnvirtual-current-group '(nnvirtual ""))))
291             (setq nnvirtual-current-groups nil)
292             (while newsrc
293               (and (string-match regexp (car (car newsrc)))
294                    (not (string= (car (car newsrc)) virt-group))
295                    (setq nnvirtual-current-groups
296                          (cons (car (car newsrc)) nnvirtual-current-groups)))
297               (setq newsrc (cdr newsrc))))
298           (if nnvirtual-current-groups
299               (progn
300                 (nnvirtual-create-mapping group)
301                 (setq nnvirtual-group-alist
302                       (cons (list group nnvirtual-current-groups 
303                                   nnvirtual-current-mapping regexp)
304                             nnvirtual-group-alist)))
305             (setq nnvirtual-status-string 
306                   (format 
307                    "nnvirtual: No newsgroups for this virtual newsgroup"))))))
308   nnvirtual-current-groups)
309
310 (defun nnvirtual-create-mapping (group)
311   (let* ((group (gnus-group-prefixed-name group (list 'nnvirtual "")))
312          (info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
313          (groups nnvirtual-current-groups)
314          (offset 0)
315          reads unread igroup itotal ireads)
316     ;; The virtual group doesn't exist. (?)
317     (or info (error "No such group: %s" group))
318     (setq nnvirtual-current-mapping nil)
319     (while groups
320       ;; Added by Sudish Joseph <joseph@cis.ohio-state.edu>.
321       (setq igroup (car groups))
322       (let ((info (nth 2 (gnus-gethash igroup gnus-newsrc-hashtb)))
323             (active (gnus-gethash igroup gnus-active-hashtb)))
324         ;; See if the group has had its active list read this session
325         ;; if not, we do it now.
326         (if (null active)
327             (if (gnus-activate-group igroup)
328                 (progn
329                   (gnus-get-unread-articles-in-group
330                    info (gnus-gethash igroup gnus-active-hashtb))
331                   (setq active (gnus-gethash igroup gnus-active-hashtb)))
332               (message "Couldn't open component group %s" igroup)))
333         (if (null active)
334             ()
335           ;; And then we do the mapping for this component group. If
336           ;; you feel tempted to cast your eyes to the soup below -
337           ;; don't.  It'll hurt your soul.  Suffice to say that it
338           ;; assigns ranges of nnvirtual article numbers to the
339           ;; different component groups.  To get the article number
340           ;; from the nnvirtual number, one does something like
341           ;; (+ (- number offset) (car active)), where `offset' is the
342           ;; slice the mess below assigns, and active is the lowest
343           ;; active article in the component group. 
344           (setq itotal (1+ (- (cdr active) (car active))))
345           (if (setq ireads (nth 2 info))
346               (let ((itreads
347                      (if (not (listp (cdr ireads)))
348                          (setq ireads (list (cons (car ireads) (cdr ireads))))
349                        (setq ireads (copy-alist ireads)))))
350                 (if (< (or (and (numberp (car ireads)) (car ireads))
351                            (cdr (car ireads))) (car active))
352                     (setq ireads (setq itreads (cdr ireads))))
353                 (if (and ireads (< (or (and (numberp (car ireads))
354                                             (car ireads))
355                                        (car (car ireads))) (car active)))
356                     (setcar (or (and (numberp (car ireads)) ireads)
357                                 (car ireads)) (1+ (car active))))
358                 (while itreads
359                   (setcar (or (and (numberp (car itreads)) itreads)
360                               (car itreads))
361                           (+ (max 
362                               1 (- (if (numberp (car itreads)) 
363                                        (car itreads)
364                                      (car (car itreads)))
365                                    (car active)))
366                              offset))
367                   (if (not (numberp (car itreads)))
368                       (setcdr (car itreads)
369                               (+ (- (cdr (car itreads)) (car active)) offset)))
370                   (setq itreads (cdr itreads)))
371                 (setq reads (nconc reads ireads))))
372           (setq offset (+ offset (1- itotal)))
373           (setq nnvirtual-current-mapping
374                 (cons (list offset igroup (car active)) 
375                       nnvirtual-current-mapping)))
376         (setq groups (cdr groups))))
377     (setq nnvirtual-current-mapping
378           (nreverse nnvirtual-current-mapping))
379     ;; Set Gnus active info.
380     (gnus-sethash group (cons 1 (1- offset)) gnus-active-hashtb)
381     ;; Set Gnus read info.
382     (setcar (nthcdr 2 info) reads)
383
384     ;; Then we deal with the marks.
385     (let ((map nnvirtual-current-mapping)
386           (marks '(tick dormant reply expire score))
387           (offset 0)
388           tick dormant reply expire score marked active)
389       (while map
390         (setq igroup (nth 1 (car map)))
391         (setq active (nth 2 (car map)))
392         (setq marked (nth 3 (nth 2 (gnus-gethash igroup gnus-newsrc-hashtb))))
393         (let ((m marks))
394           (while m
395             (and (assq (car m) marked)
396                  (set (car m) 
397                       (nconc (mapcar 
398                               (lambda (art) 
399                                 (if (numberp art)
400                                     (if (< art active)
401                                         0 (+ (- art active) offset))
402                                   (cons (+ (- (car art) active) offset)
403                                         (cdr art))))
404                               (cdr (assq (car m) marked)))
405                              (symbol-value (car m)))))
406             (setq m (cdr m))))
407         (setq offset (car (car map)))
408         (setq map (cdr map)))
409       ;; Put the list of marked articles in the info of the virtual group.
410       (let ((m marks)
411             marked)
412         (while m
413           (and (symbol-value (car m))
414                (setq marked (cons (cons (car m) (symbol-value (car m)))
415                                   marked)))
416           (setq m (cdr m)))
417         (if (nthcdr 3 info)
418             (setcar (nthcdr 3 info) marked)
419           (setcdr (nthcdr 2 info) (list marked)))))))
420
421 (defun nnvirtual-update-marked ()
422   (let ((mark-lists '((gnus-newsgroup-marked . tick)
423                       (gnus-newsgroup-dormant . dormant)
424                       (gnus-newsgroup-expirable . expire)
425                       (gnus-newsgroup-replied . reply)))
426         marks art-group group-alist g)
427     (while mark-lists
428       (setq marks (symbol-value (car (car mark-lists))))
429       ;; Find out what groups the mark belong to.
430       (while marks
431         (setq art-group (nnvirtual-art-group (car marks)))
432         (if (setq g (assoc (car art-group) group-alist))
433             (nconc g (list (cdr art-group)))
434           (setq group-alist (cons (list (car art-group) (cdr art-group)) 
435                                   group-alist)))
436         (setq marks (cdr marks)))
437       ;; The groups that don't have marks must have no marks. (Yup.)
438       (let ((groups nnvirtual-current-groups))
439         (while groups
440           (or (assoc (car groups) group-alist)
441               (setq group-alist (cons (list (car groups)) group-alist)))
442           (setq groups (cdr groups))))
443       ;; The we update the list of marks.
444       (while group-alist
445         (gnus-add-marked-articles 
446          (car (car group-alist)) (cdr (car mark-lists)) 
447          (cdr (car group-alist)) nil t)
448         (gnus-group-update-group (car (car group-alist)) t)
449         (setq group-alist (cdr group-alist)))
450       (setq mark-lists (cdr mark-lists)))))
451
452 (defun nnvirtual-art-group (article) 
453   (let ((map nnvirtual-current-mapping)
454         (offset 0))
455     (while (< (car (car map)) (if (numberp article) article (car article)))
456       (setq offset (car (car map))
457             map (cdr map)))
458     (cons (nth 1 (car map))
459           (if (numberp article)
460               (- (+ article (nth 2 (car map))) offset)
461             (cons (- (+ (car article) (nth 2 (car map))) offset)
462                   (cdr article))))))
463
464 (defun nnvirtual-catchup-group (group &optional server all)
465   (nnvirtual-possibly-change-newsgroups group server)
466   (let ((gnus-group-marked nnvirtual-current-groups)
467         (gnus-expert-user t))
468     (save-excursion
469       (set-buffer gnus-group-buffer)
470       (gnus-group-catchup-current nil all))))
471
472 (provide 'nnvirtual)
473
474 ;;; nnvirtual.el ends here