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