1 ;;; nnvirtual.el --- virtual newsgroups access for Gnus
2 ;; Copyright (C) 1994,95 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
5 ;; Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
8 ;; This file is part of GNU Emacs.
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)
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.
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.
26 ;; The other access methods (nntp, nnspool, etc) are general news
27 ;; access methods. This module relies on Gnus and can not be used
38 (defconst nnvirtual-version "nnvirtual 1.0"
39 "Version number of this version of nnvirtual.")
41 (defvar nnvirtual-group-alist nil)
42 (defvar nnvirtual-current-group nil)
43 (defvar nnvirtual-current-groups nil)
44 (defvar nnvirtual-current-mapping nil)
46 (defvar nnvirtual-do-not-open nil)
48 (defvar nnvirtual-status-string "")
52 ;;; Interface functions.
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)
58 (set-buffer (get-buffer-create "*virtual headers*"))
59 (buffer-disable-undo (current-buffer))
61 (if (stringp (car sequence))
63 (let ((map nnvirtual-current-mapping)
65 articles beg group active top article result prefix
66 fetched-articles group-method)
68 (while (< (car (car map)) (car sequence))
69 (setq offset (car (car 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)))
76 (while (and sequence (<= (car sequence) top))
77 (setq articles (cons (- (+ active (car sequence)) offset)
79 (setq sequence (cdr sequence)))
80 (setq articles (nreverse articles))
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)))))
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)
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)))
107 "[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t")
108 (goto-char (match-end 0))
110 "\t" (save-excursion (end-of-line) (point)) t)
112 (while (= (char-after (1- (point))) ? )
118 (or (= (char-after (1- (point))) ?\t)
120 (insert (format "Xref: %s %s:%d\t" (system-name)
122 (if (not (string= "" prefix))
123 (while (re-search-forward
125 (save-excursion (end-of-line) (point)) t)
127 (goto-char (match-beginning 0))
130 (or (= (char-after (1- (point))) ?\t)
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
139 (let ((unfetched (gnus-sorted-complement
140 articles (nreverse fetched-articles))))
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
148 (set-buffer nntp-server-buffer)
150 (insert-buffer-substring "*virtual headers*")
152 (kill-buffer (current-buffer)))))))
154 (defun nnvirtual-open-server (newsgroups &optional something)
155 "Open a virtual newsgroup that contains NEWSGROUPS."
156 (nnheader-init-server-buffer))
158 (defun nnvirtual-close-server (&rest dum)
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)
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)))
175 (defun nnvirtual-status-message (&optional server)
176 "Return server status response as string."
177 nnvirtual-status-string)
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)
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))))
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))
201 (set-buffer nntp-server-buffer)
206 (setq map (cdr map)))
207 (insert (format "211 %d 1 %d %s\n" (car (car map))
208 (car (car map)) group))
210 (setq nnvirtual-status-string "No component groups")
211 (setq nnvirtual-current-group nil)
214 (defun nnvirtual-close-group (group &optional server)
215 (if (not nnvirtual-current-group)
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))))
225 (defun nnvirtual-request-list (&optional server)
226 (setq nnvirtual-status-string "nnvirtual: LIST is not implemented.")
229 (defun nnvirtual-request-newgroups (date &optional server)
231 (setq nnvirtual-status-string "NEWGROUPS is not supported.")
234 (defun nnvirtual-request-list-newsgroups (&optional server)
235 (setq nnvirtual-status-string
236 "nnvirtual: LIST NEWSGROUPS is not implemented.")
239 (defalias 'nnvirtual-request-post 'nntp-request-post)
242 ;;; Internal functions.
244 ;; Convert HEAD headers into NOV headers.
245 (defun nnvirtual-convert-headers ()
247 (set-buffer nntp-server-buffer)
248 (let* ((gnus-newsgroup-dependencies (make-vector 100 0))
249 (headers (gnus-get-newsgroup-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")
267 (defun nnvirtual-possibly-change-newsgroups (group regexp &optional check)
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)
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))
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)
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
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
303 "nnvirtual: No newsgroups for this virtual newsgroup"))))))
304 nnvirtual-current-groups)
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)
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)
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.
323 (if (gnus-activate-group igroup)
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)))
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))
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))
351 (car (car ireads))) (car active)))
352 (setcar (or (and (numberp (car ireads)) ireads)
353 (car ireads)) (1+ (car active))))
355 (setcar (or (and (numberp (car itreads)) itreads)
358 1 (- (if (numberp (car itreads))
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)
380 ;; Then we deal with the marks.
381 (let ((map nnvirtual-current-mapping)
382 (marks '(tick dormant reply expire score))
384 tick dormant reply expire score marked active)
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))))
391 (and (assq (car m) marked)
397 0 (+ (- art active) offset))
398 (cons (+ (- (car art) active) offset)
400 (cdr (assq (car m) marked)))
401 (symbol-value (car 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.
409 (and (symbol-value (car m))
410 (setq marked (cons (cons (car m) (symbol-value (car m)))
414 (setcar (nthcdr 3 info) marked)
415 (setcdr (nthcdr 2 info) (list marked)))))))
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)
424 (setq marks (symbol-value (car (car mark-lists))))
425 ;; Find out what groups the mark belong to.
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))
432 (setq marks (cdr marks)))
433 ;; The groups that don't have marks must have no marks. (Yup.)
434 (let ((groups nnvirtual-current-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.
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)))))
448 (defun nnvirtual-art-group (article)
449 (let ((map nnvirtual-current-mapping)
451 (while (< (car (car map)) (if (numberp article) article (car article)))
452 (setq offset (car (car 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)
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))
465 (set-buffer gnus-group-buffer)
466 (gnus-group-catchup-current nil all))))
470 ;;; nnvirtual.el ends here