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