*** 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 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 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 (defun nnvirtual-request-post-buffer 
242   (post group subject header article-buffer info follow-to respect-poster)
243   (nntp-request-post-buffer post "" subject header article-buffer
244                             info follow-to respect-poster))
245
246 \f
247 ;;; Internal functions.
248
249 ;; Convert HEAD headers into NOV headers.
250 (defun nnvirtual-convert-headers ()
251   (save-excursion
252     (set-buffer nntp-server-buffer)
253     (let* ((gnus-newsgroup-dependencies (make-vector 100 0))
254            (headers (gnus-get-newsgroup-headers))
255            header)
256       (erase-buffer)
257       (while headers
258         (setq header (car headers)
259               headers (cdr headers))
260         (insert (int-to-string (mail-header-number header)) "\t"
261                 (or (mail-header-subject header) "") "\t"
262                 (or (mail-header-from header) "") "\t"
263                 (or (mail-header-date header) "") "\t"
264                 (or (mail-header-id header) "") "\t"
265                 (or (mail-header-references header) "") "\t"
266                 (int-to-string (or (mail-header-chars header) 0)) "\t"
267                 (int-to-string (or (mail-header-lines header) 0)) "\t"
268                 (if (mail-header-xref header) 
269                     (concat "Xref: " (mail-header-xref header) "\t")
270                   "") "\n")))))
271
272 (defun nnvirtual-possibly-change-newsgroups (group regexp &optional check)
273   (let ((inf t))
274     (or (not group)
275         (and nnvirtual-current-group
276              (string= group nnvirtual-current-group))
277         (and (setq inf (assoc group nnvirtual-group-alist))
278              (string= (nth 3 inf) regexp)
279              (progn
280                (setq nnvirtual-current-group (car inf))
281                (setq nnvirtual-current-groups (nth 1 inf))
282                (setq nnvirtual-current-mapping (nth 2 inf)))))
283     (if (or (not check) (not inf))
284         (progn
285           (and inf (setq nnvirtual-group-alist 
286                          (delq inf nnvirtual-group-alist)))
287           (setq nnvirtual-current-mapping nil)
288           (setq nnvirtual-current-group group)
289           (let ((newsrc gnus-newsrc-alist)
290                 (virt-group (gnus-group-prefixed-name 
291                              nnvirtual-current-group '(nnvirtual ""))))
292             (setq nnvirtual-current-groups nil)
293             (while newsrc
294               (and (string-match regexp (car (car newsrc)))
295                    (not (string= (car (car newsrc)) virt-group))
296                    (setq nnvirtual-current-groups
297                          (cons (car (car newsrc)) nnvirtual-current-groups)))
298               (setq newsrc (cdr newsrc))))
299           (if nnvirtual-current-groups
300               (progn
301                 (nnvirtual-create-mapping group)
302                 (setq nnvirtual-group-alist
303                       (cons (list group nnvirtual-current-groups 
304                                   nnvirtual-current-mapping regexp)
305                             nnvirtual-group-alist)))
306             (setq nnvirtual-status-string 
307                   (format 
308                    "nnvirtual: No newsgroups for this virtual newsgroup"))))))
309   nnvirtual-current-groups)
310
311 (defun nnvirtual-create-mapping (group)
312   (let* ((group (gnus-group-prefixed-name group (list 'nnvirtual "")))
313          (info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
314          (groups nnvirtual-current-groups)
315          (offset 0)
316          reads unread igroup itotal ireads)
317     ;; The virtual group doesn't exist. (?)
318     (or info (error "No such group: %s" group))
319     (setq nnvirtual-current-mapping nil)
320     (while groups
321       ;; Added by Sudish Joseph <joseph@cis.ohio-state.edu>.
322       (setq igroup (car groups))
323       (let ((info (nth 2 (gnus-gethash igroup gnus-newsrc-hashtb)))
324             (active (gnus-gethash igroup gnus-active-hashtb)))
325         ;; See if the group has had its active list read this session
326         ;; if not, we do it now.
327         (if (null active)
328             (if (gnus-activate-group igroup)
329                 (progn
330                   (gnus-get-unread-articles-in-group
331                    info (gnus-gethash igroup gnus-active-hashtb))
332                   (setq active (gnus-gethash igroup gnus-active-hashtb)))
333               (message "Couldn't open component group %s" igroup)))
334         (if (null active)
335             ()
336           ;; And then we do the mapping for this component group. If
337           ;; you feel tempted to cast your eyes to the soup below -
338           ;; don't. It'll hurt your soul. Suffice to say that it
339           ;; assigns ranges of nnvirtual article numbers to the
340           ;; different component groups. To get the article number
341           ;; from the nnvirtual number, one does something like
342           ;; (+ (- number offset) (car active)), where `offset' is the
343           ;; slice the mess below assigns, and active is the lowest
344           ;; active article in the component group. 
345           (setq itotal (1+ (- (cdr active) (car active))))
346           (if (setq ireads (nth 2 info))
347               (let ((itreads
348                      (if (not (listp (cdr ireads)))
349                          (setq ireads (list (cons (car ireads) (cdr ireads))))
350                        (setq ireads (copy-alist ireads)))))
351                 (if (< (or (and (numberp (car ireads)) (car ireads))
352                            (cdr (car ireads))) (car active))
353                     (setq ireads (setq itreads (cdr ireads))))
354                 (if (and ireads (< (or (and (numberp (car ireads))
355                                             (car ireads))
356                                        (car (car ireads))) (car active)))
357                     (setcar (or (and (numberp (car ireads)) ireads)
358                                 (car ireads)) (1+ (car active))))
359                 (while itreads
360                   (setcar (or (and (numberp (car itreads)) itreads)
361                               (car itreads))
362                           (+ (max 
363                               1 (- (if (numberp (car itreads)) 
364                                        (car itreads)
365                                      (car (car itreads)))
366                                    (car active)))
367                              offset))
368                   (if (not (numberp (car itreads)))
369                       (setcdr (car itreads)
370                               (+ (- (cdr (car itreads)) (car active)) offset)))
371                   (setq itreads (cdr itreads)))
372                 (setq reads (nconc reads ireads))))
373           (setq offset (+ offset (1- itotal)))
374           (setq nnvirtual-current-mapping
375                 (cons (list offset igroup (car active)) 
376                       nnvirtual-current-mapping)))
377         (setq groups (cdr groups))))
378     (setq nnvirtual-current-mapping
379           (nreverse nnvirtual-current-mapping))
380     ;; Set Gnus active info.
381     (gnus-sethash group (cons 1 (1- offset)) gnus-active-hashtb)
382     ;; Set Gnus read info.
383     (setcar (nthcdr 2 info) reads)
384
385     ;; Then we deal with the marks.
386     (let ((map nnvirtual-current-mapping)
387           (marks '(tick dormant reply expire score))
388           (offset 0)
389           tick dormant reply expire score marked active)
390       (while map
391         (setq igroup (nth 1 (car map)))
392         (setq active (nth 2 (car map)))
393         (setq marked (nth 3 (nth 2 (gnus-gethash igroup gnus-newsrc-hashtb))))
394         (let ((m marks))
395           (while m
396             (and (assq (car m) marked)
397                  (set (car m) 
398                       (nconc (mapcar 
399                               (lambda (art) 
400                                 (if (numberp art)
401                                     (if (< art active)
402                                         0 (+ (- art active) offset))
403                                   (cons (+ (- (car art) active) offset)
404                                         (cdr art))))
405                               (cdr (assq (car m) marked)))
406                              (symbol-value (car m)))))
407             (setq m (cdr m))))
408         (setq offset (car (car map)))
409         (setq map (cdr map)))
410       ;; Put the list of marked articles in the info of the virtual group.
411       (let ((m marks)
412             marked)
413         (while m
414           (and (symbol-value (car m))
415                (setq marked (cons (cons (car m) (symbol-value (car m)))
416                                   marked)))
417           (setq m (cdr m)))
418         (if (nthcdr 3 info)
419             (setcar (nthcdr 3 info) marked)
420           (setcdr (nthcdr 2 info) (list marked)))))))
421
422 (defun nnvirtual-update-marked ()
423   (let ((mark-lists '((gnus-newsgroup-marked . tick)
424                       (gnus-newsgroup-dormant . dormant)
425                       (gnus-newsgroup-expirable . expire)
426                       (gnus-newsgroup-replied . reply)))
427         marks art-group group-alist g)
428     (while mark-lists
429       (setq marks (symbol-value (car (car mark-lists))))
430       ;; Find out what groups the mark belong to.
431       (while marks
432         (setq art-group (nnvirtual-art-group (car marks)))
433         (if (setq g (assoc (car art-group) group-alist))
434             (nconc g (list (cdr art-group)))
435           (setq group-alist (cons (list (car art-group) (cdr art-group)) 
436                                   group-alist)))
437         (setq marks (cdr marks)))
438       ;; The groups that don't have marks must have no marks. (Yup.)
439       (let ((groups nnvirtual-current-groups))
440         (while groups
441           (or (assoc (car groups) group-alist)
442               (setq group-alist (cons (list (car groups)) group-alist)))
443           (setq groups (cdr groups))))
444       ;; The we update the list of marks.
445       (while group-alist
446         (gnus-add-marked-articles 
447          (car (car group-alist)) (cdr (car mark-lists)) 
448          (cdr (car group-alist)) nil t)
449         (gnus-group-update-group (car (car group-alist)) t)
450         (setq group-alist (cdr group-alist)))
451       (setq mark-lists (cdr mark-lists)))))
452
453 (defun nnvirtual-art-group (article) 
454   (let ((map nnvirtual-current-mapping)
455         (offset 0))
456     (while (< (car (car map)) (if (numberp article) article (car article)))
457       (setq offset (car (car map))
458             map (cdr map)))
459     (cons (nth 1 (car map))
460           (if (numberp article)
461               (- (+ article (nth 2 (car map))) offset)
462             (cons (- (+ (car article) (nth 2 (car map))) offset)
463                   (cdr article))))))
464
465 (defun nnvirtual-catchup-group (group &optional server all)
466   (nnvirtual-possibly-change-newsgroups group server)
467   (let ((gnus-group-marked nnvirtual-current-groups)
468         (gnus-expert-user t))
469     (save-excursion
470       (set-buffer gnus-group-buffer)
471       (gnus-group-catchup-current nil all))))
472
473 (provide 'nnvirtual)
474
475 ;;; nnvirtual.el ends here