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 0.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)
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)
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))
247 ;;; Internal functions.
249 ;; Convert HEAD headers into NOV headers.
250 (defun nnvirtual-convert-headers ()
252 (set-buffer nntp-server-buffer)
253 (let* ((gnus-newsgroup-dependencies (make-vector 100 0))
254 (headers (gnus-get-newsgroup-headers))
258 (setq header (car headers)
259 headers (cdr headers))
260 (insert (int-to-string (header-number header)) "\t"
261 (or (header-subject header) "") "\t"
262 (or (header-from header) "") "\t"
263 (or (header-date header) "") "\t"
264 (or (header-id header) "") "\t"
265 (or (header-references header) "") "\t"
266 (int-to-string (or (header-chars header) 0)) "\t"
267 (int-to-string (or (header-lines header) 0)) "\t"
268 (if (header-xref header)
269 (concat "Xref: " (header-xref header) "\t")
272 (defun nnvirtual-possibly-change-newsgroups (group regexp &optional check)
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)
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))
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)
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
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
308 "nnvirtual: No newsgroups for this virtual newsgroup"))))))
309 nnvirtual-current-groups)
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)
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)
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.
328 (if (gnus-activate-group igroup)
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)))
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))
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))
356 (car (car ireads))) (car active)))
357 (setcar (or (and (numberp (car ireads)) ireads)
358 (car ireads)) (1+ (car active))))
360 (setcar (or (and (numberp (car itreads)) itreads)
363 1 (- (if (numberp (car itreads))
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)
385 ;; Then we deal with the marks.
386 (let ((map nnvirtual-current-mapping)
387 (marks '(tick dormant reply expire score))
389 tick dormant reply expire score marked active)
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))))
396 (and (assq (car m) marked)
402 0 (+ (- art active) offset))
403 (cons (+ (- (car art) active) offset)
405 (cdr (assq (car m) marked)))
406 (symbol-value (car 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.
414 (and (symbol-value (car m))
415 (setq marked (cons (cons (car m) (symbol-value (car m)))
419 (setcar (nthcdr 3 info) marked)
420 (setcdr (nthcdr 2 info) (list marked)))))))
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)
429 (setq marks (symbol-value (car (car mark-lists))))
430 ;; Find out what groups the mark belong to.
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))
437 (setq marks (cdr marks)))
438 ;; The groups that don't have marks must have no marks. (Yup.)
439 (let ((groups nnvirtual-current-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.
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)))))
453 (defun nnvirtual-art-group (article)
454 (let ((map nnvirtual-current-mapping)
456 (while (< (car (car map)) (if (numberp article) article (car article)))
457 (setq offset (car (car 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)
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))
470 (set-buffer gnus-group-buffer)
471 (gnus-group-catchup-current nil all))))
475 ;;; nnvirtual.el ends here