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