*** 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                    (not (string= (gnus-group-real-name (car (car newsrc)))
273                                  nnvirtual-current-group))
274                    (setq nnvirtual-current-groups
275                          (cons (car (car newsrc)) nnvirtual-current-groups)))
276               (setq newsrc (cdr newsrc))))
277           (if nnvirtual-current-groups
278               (progn
279                 (nnvirtual-create-mapping group)
280                 (setq nnvirtual-group-alist
281                       (cons (list group nnvirtual-current-groups 
282                                   nnvirtual-current-mapping regexp)
283                             nnvirtual-group-alist)))
284             (setq nnvirtual-status-string 
285                   (format 
286                    "nnvirtual: No newsgroups for this virtual newsgroup"))))))
287   nnvirtual-current-groups)
288
289 (defun nnvirtual-create-mapping (group)
290   (let* ((group (gnus-group-prefixed-name group (list 'nnvirtual "")))
291          (info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
292          (groups nnvirtual-current-groups)
293          (i 1)
294          (total 0)
295          (offset 0)
296          reads unread igroup itotal itreads ireads)
297     ;; The virtual group doesn't exist. (?)
298     (or info (error "No such group: %s" group))
299     (setq nnvirtual-current-mapping nil)
300     (while groups
301       ;; Added by Sudish Joseph <joseph@cis.ohio-state.edu>.
302       (setq igroup (car groups))
303       (let ((info (nth 2 (gnus-gethash igroup gnus-newsrc-hashtb)))
304             (active (gnus-gethash igroup gnus-active-hashtb)))
305         ;; See if the group has had its active list read this session
306         ;; if not, we do it now.
307         (if (null active)
308             (if (gnus-activate-newsgroup igroup)
309                 (progn
310                   (gnus-get-unread-articles-in-group
311                    info (gnus-gethash igroup gnus-active-hashtb))
312                   (setq active (gnus-gethash igroup gnus-active-hashtb)))
313               (message "Couldn't open component group %s" igroup)
314               (ding)))
315         (if (null active)
316             ()
317           ;; And then we do the mapping for this component group. If
318           ;; you feel tempte to cast your eyes to the soup below -
319           ;; don't. It'll hurt your soul. Suffice to say that it
320           ;; assigns ranges of nnvirtual article numbers to the
321           ;; different component groups. To get the article number
322           ;; from the nnvirtual number, one does something like
323           ;; (+ (- number offset) (car active)), where `offset' is the
324           ;; slice the mess below assigns, and active is the lowest
325           ;; active article in the component group. 
326           (setq itotal (1+ (- (cdr active) (car active))))
327           (if (setq ireads (nth 2 info))
328               (let ((itreads
329                      (if (not (listp (cdr ireads)))
330                          (setq ireads (list (cons (car ireads) (cdr ireads))))
331                        (setq ireads (copy-alist ireads)))))
332                 (if (< (or (and (numberp (car ireads)) (car ireads))
333                            (cdr (car ireads))) (car active))
334                     (setq ireads (setq itreads (cdr ireads))))
335                 (if (and ireads (< (or (and (numberp (car ireads))
336                                             (car ireads))
337                                        (car (car ireads))) (car active)))
338                     (setcar (or (and (numberp (car ireads)) ireads)
339                                 (car ireads)) (1+ (car active))))
340                 (while itreads
341                   (setcar (or (and (numberp (car itreads)) itreads)
342                               (car itreads))
343                           (+ (- (or (and (numberp (car itreads)) (car itreads))
344                                     (car (car itreads))) (car active)) offset))
345                   (if (not (numberp (car itreads)))
346                       (setcdr (car itreads)
347                               (+ (- (cdr (car itreads)) (car active)) offset)))
348                   (setq itreads (cdr itreads)))
349                 (setq reads (nconc reads ireads))))
350           (setq offset (+ offset (1- itotal)))
351           (setq nnvirtual-current-mapping
352                 (cons (list offset igroup (car active)) 
353                       nnvirtual-current-mapping)))
354         (setq groups (cdr groups))))
355     (setq nnvirtual-current-mapping
356           (nreverse nnvirtual-current-mapping))
357     (gnus-sethash group (cons 1 offset) gnus-active-hashtb)
358     (setcar (nthcdr 2 info) reads)
359
360     ;; Then we deal with the marks.
361     (let ((map nnvirtual-current-mapping)
362           (marks '(tick dormant reply expire score))
363           (offset 0)
364           tick dormant reply expire score marked active)
365       (while map
366         (setq igroup (nth 1 (car map)))
367         (setq active (nth 2 (car map)))
368         (setq marked (nth 3 (nth 2 (gnus-gethash igroup gnus-newsrc-hashtb))))
369         (let ((m marks))
370           (while m
371             (and (assq (car m) marked)
372                  (set (car m) 
373                       (nconc (mapcar 
374                               (lambda (art) 
375                                 (if (numberp art)
376                                     (if (< art active)
377                                         0 (+ (- art active) offset))
378                                   (cons (+ (- (car art) active) offset)
379                                         (cdr art))))
380                               (cdr (assq (car m) marked)))
381                              (symbol-value (car m)))))
382             (setq m (cdr m))))
383         (setq offset (car (car map)))
384         (setq map (cdr map)))
385       ;; Put the list of marked articles in the info of the virtual group.
386       (let ((m marks)
387             marked)
388         (while m
389           (and (symbol-value (car m))
390                (setq marked (cons (cons (car m) (symbol-value (car m)))
391                                   marked)))
392           (setq m (cdr m)))
393         (if (nthcdr 3 info)
394             (setcar (nthcdr 3 info) marked)
395           (setcdr (nthcdr 2 info) (list marked)))))))
396
397 (defun nnvirtual-update-marked ()
398   (let ((mark-lists '((gnus-newsgroup-marked . tick)
399                       (gnus-newsgroup-dormant . dormant)
400                       (gnus-newsgroup-expirable . expire)
401                       (gnus-newsgroup-replied . reply)))
402         marks art-group group-alist g)
403     (while mark-lists
404       (setq marks (symbol-value (car (car mark-lists))))
405       (while marks
406         (setq art-group (nnvirtual-art-group (car marks)))
407         (if (setq g (assoc (car art-group) group-alist))
408             (nconc g (list (cdr art-group)))
409           (setq group-alist (cons (list (car art-group) (cdr art-group)) 
410                                   group-alist)))
411         (setq marks (cdr marks)))
412       (while group-alist
413         (gnus-add-marked-articles 
414          (car (car group-alist)) (cdr (car mark-lists)) 
415          (cdr (car group-alist)) nil t)
416         (gnus-group-update-group (car (car group-alist)) t)
417         (setq group-alist (cdr group-alist)))
418       (setq mark-lists (cdr mark-lists)))))
419
420 (defun nnvirtual-art-group (article) 
421   (let ((map nnvirtual-current-mapping)
422         (offset 0))
423     (while (< (car (car map)) (if (numberp article) article (car article)))
424       (setq offset (car (car map))
425             map (cdr map)))
426     (cons (nth 1 (car map))
427           (if (numberp article)
428               (- (+ article (nth 2 (car map))) offset)
429             (cons (- (+ (car article) (nth 2 (car map))) offset)
430                   (cdr article))))))
431
432 (defun nnvirtual-catchup-group (group &optional server all)
433   (nnvirtual-possibly-change-newsgroups group server)
434   (let ((gnus-group-marked nnvirtual-current-groups)
435         (gnus-expert-user t))
436     (set-buffer gnus-group-buffer)
437     (gnus-group-catchup-current nil all)))
438
439 (provide 'nnvirtual)
440
441 ;;; nnvirtual.el ends here