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