1 ;;; nnvirtual.el --- virtual newsgroups access for (ding) 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
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))
82 (setq result (gnus-retrieve-headers articles group)))
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)
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)))
101 "[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t")
102 (goto-char (match-end 0))
104 "\t" (save-excursion (end-of-line) (point)) t)
106 (while (= (char-after (1- (point))) ? )
112 (or (= (char-after (1- (point))) ?\t)
114 (insert (format "Xref: %s %s:%d\t" (system-name)
116 (if (not (string= "" prefix))
117 (while (re-search-forward
119 (save-excursion (end-of-line) (point)) t)
121 (goto-char (match-beginning 0))
124 (or (= (char-after (1- (point))) ?\t)
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
133 (let ((unfetched (gnus-sorted-complement
134 articles (nreverse fetched-articles))))
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
142 (set-buffer nntp-server-buffer)
144 (insert-buffer-substring "*virtual headers*")
146 (kill-buffer (current-buffer)))))))
148 (defun nnvirtual-open-server (newsgroups &optional something)
149 "Open a virtual newsgroup that contains NEWSGROUPS."
150 (nnheader-init-server-buffer))
152 (defun nnvirtual-close-server (&rest dum)
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)
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)))
169 (defun nnvirtual-status-message (&optional server)
170 "Return server status response as string."
171 nnvirtual-status-string)
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)
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))))
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))
191 (set-buffer nntp-server-buffer)
196 (setq map (cdr map)))
197 (insert (format "211 %d 1 %d %s\n" (car (car map))
198 (car (car map)) group))
200 (setq nnvirtual-status-string "No component groups")
201 (setq nnvirtual-current-group nil)
204 (defun nnvirtual-close-group (group &optional server)
205 (if (not nnvirtual-current-group)
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))))
215 (defun nnvirtual-request-list (&optional server)
216 (setq nnvirtual-status-string "nnvirtual: LIST is not implemented.")
219 (defun nnvirtual-request-newgroups (date &optional server)
221 (setq nnvirtual-status-string "NEWGROUPS is not supported.")
224 (defun nnvirtual-request-list-newsgroups (&optional server)
225 (setq nnvirtual-status-string
226 "nnvirtual: LIST NEWSGROUPS is not implemented.")
229 (defalias 'nnvirtual-request-post 'nntp-request-post)
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))
237 ;;; Internal functions.
239 ;; Convert HEAD headers into NOV headers.
240 (defun nnvirtual-convert-headers ()
242 (set-buffer nntp-server-buffer)
243 (let* ((gnus-newsgroup-dependencies (make-vector 100 0))
244 (headers (gnus-get-newsgroup-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")
262 (defun nnvirtual-possibly-change-newsgroups (group regexp &optional check)
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)
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))
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)
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
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
297 "nnvirtual: No newsgroups for this virtual newsgroup"))))))
298 nnvirtual-current-groups)
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)
305 reads unread igroup itotal ireads)
306 ;; The virtual group doesn't exist. (?)
307 (or info (error "No such group: %s" group))
308 (setq nnvirtual-current-mapping nil)
310 ;; Added by Sudish Joseph <joseph@cis.ohio-state.edu>.
311 (setq igroup (car groups))
312 (let ((info (nth 2 (gnus-gethash igroup gnus-newsrc-hashtb)))
313 (active (gnus-gethash igroup gnus-active-hashtb)))
314 ;; See if the group has had its active list read this session
315 ;; if not, we do it now.
317 (if (gnus-activate-newsgroup igroup)
319 (gnus-get-unread-articles-in-group
320 info (gnus-gethash igroup gnus-active-hashtb))
321 (setq active (gnus-gethash igroup gnus-active-hashtb)))
322 (message "Couldn't open component group %s" igroup)))
325 ;; And then we do the mapping for this component group. If
326 ;; you feel tempte to cast your eyes to the soup below -
327 ;; don't. It'll hurt your soul. Suffice to say that it
328 ;; assigns ranges of nnvirtual article numbers to the
329 ;; different component groups. To get the article number
330 ;; from the nnvirtual number, one does something like
331 ;; (+ (- number offset) (car active)), where `offset' is the
332 ;; slice the mess below assigns, and active is the lowest
333 ;; active article in the component group.
334 (setq itotal (1+ (- (cdr active) (car active))))
335 (if (setq ireads (nth 2 info))
337 (if (not (listp (cdr ireads)))
338 (setq ireads (list (cons (car ireads) (cdr ireads))))
339 (setq ireads (copy-alist ireads)))))
340 (if (< (or (and (numberp (car ireads)) (car ireads))
341 (cdr (car ireads))) (car active))
342 (setq ireads (setq itreads (cdr ireads))))
343 (if (and ireads (< (or (and (numberp (car ireads))
345 (car (car ireads))) (car active)))
346 (setcar (or (and (numberp (car ireads)) ireads)
347 (car ireads)) (1+ (car active))))
349 (setcar (or (and (numberp (car itreads)) itreads)
351 (+ (- (or (and (numberp (car itreads)) (car itreads))
352 (car (car itreads))) (car active)) offset))
353 (if (not (numberp (car itreads)))
354 (setcdr (car itreads)
355 (+ (- (cdr (car itreads)) (car active)) offset)))
356 (setq itreads (cdr itreads)))
357 (setq reads (nconc reads ireads))))
358 (setq offset (+ offset (1- itotal)))
359 (setq nnvirtual-current-mapping
360 (cons (list offset igroup (car active))
361 nnvirtual-current-mapping)))
362 (setq groups (cdr groups))))
363 (setq nnvirtual-current-mapping
364 (nreverse nnvirtual-current-mapping))
365 (gnus-sethash group (cons 1 offset) gnus-active-hashtb)
366 (setcar (nthcdr 2 info) reads)
368 ;; Then we deal with the marks.
369 (let ((map nnvirtual-current-mapping)
370 (marks '(tick dormant reply expire score))
372 tick dormant reply expire score marked active)
374 (setq igroup (nth 1 (car map)))
375 (setq active (nth 2 (car map)))
376 (setq marked (nth 3 (nth 2 (gnus-gethash igroup gnus-newsrc-hashtb))))
379 (and (assq (car m) marked)
385 0 (+ (- art active) offset))
386 (cons (+ (- (car art) active) offset)
388 (cdr (assq (car m) marked)))
389 (symbol-value (car m)))))
391 (setq offset (car (car map)))
392 (setq map (cdr map)))
393 ;; Put the list of marked articles in the info of the virtual group.
397 (and (symbol-value (car m))
398 (setq marked (cons (cons (car m) (symbol-value (car m)))
402 (setcar (nthcdr 3 info) marked)
403 (setcdr (nthcdr 2 info) (list marked)))))))
405 (defun nnvirtual-update-marked ()
406 (let ((mark-lists '((gnus-newsgroup-marked . tick)
407 (gnus-newsgroup-dormant . dormant)
408 (gnus-newsgroup-expirable . expire)
409 (gnus-newsgroup-replied . reply)))
410 marks art-group group-alist g)
412 (setq marks (symbol-value (car (car mark-lists))))
413 ;; Find out what groups the mark belong to.
415 (setq art-group (nnvirtual-art-group (car marks)))
416 (if (setq g (assoc (car art-group) group-alist))
417 (nconc g (list (cdr art-group)))
418 (setq group-alist (cons (list (car art-group) (cdr art-group))
420 (setq marks (cdr marks)))
421 ;; The groups that don't have marks must have no marks. (Yup.)
422 (let ((groups nnvirtual-current-groups))
424 (or (assoc (car groups) group-alist)
425 (setq group-alist (cons (list (car groups)) group-alist)))
426 (setq groups (cdr groups))))
427 ;; The we update the list of marks.
429 (gnus-add-marked-articles
430 (car (car group-alist)) (cdr (car mark-lists))
431 (cdr (car group-alist)) nil t)
432 (gnus-group-update-group (car (car group-alist)) t)
433 (setq group-alist (cdr group-alist)))
434 (setq mark-lists (cdr mark-lists)))))
436 (defun nnvirtual-art-group (article)
437 (let ((map nnvirtual-current-mapping)
439 (while (< (car (car map)) (if (numberp article) article (car article)))
440 (setq offset (car (car map))
442 (cons (nth 1 (car map))
443 (if (numberp article)
444 (- (+ article (nth 2 (car map))) offset)
445 (cons (- (+ (car article) (nth 2 (car map))) offset)
448 (defun nnvirtual-catchup-group (group &optional server all)
449 (nnvirtual-possibly-change-newsgroups group server)
450 (let ((gnus-group-marked nnvirtual-current-groups)
451 (gnus-expert-user t))
453 (set-buffer gnus-group-buffer)
454 (gnus-group-catchup-current nil all))))
458 ;;; nnvirtual.el ends here