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