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