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)
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-request-group group t)
89 (gnus-retrieve-headers articles group)))))
91 (set-buffer nntp-server-buffer)
92 ;; If we got HEAD headers, we convert them into NOV
93 ;; headers. This is slow, inefficient and, come to think
94 ;; of it, downright evil. So sue me. I couldn't be
95 ;; bothered to write a header parse routine that could
96 ;; parse a mixed HEAD/NOV buffer.
97 (and (eq result 'headers) (nnvirtual-convert-headers))
98 (goto-char (point-min))
99 (setq fetched-articles nil)
102 article (read nntp-server-buffer)
103 fetched-articles (cons article fetched-articles))
104 (delete-region beg (point))
105 (insert (int-to-string (+ (- article active) offset)))
108 "[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t")
109 (goto-char (match-end 0))
111 "\t" (save-excursion (end-of-line) (point)) t)
113 (while (= (char-after (1- (point))) ? )
119 (or (= (char-after (1- (point))) ?\t)
121 (insert (format "Xref: %s %s:%d\t" (system-name)
123 (if (not (string= "" prefix))
124 (while (re-search-forward
126 (save-excursion (end-of-line) (point)) t)
128 (goto-char (match-beginning 0))
131 (or (= (char-after (1- (point))) ?\t)
134 (goto-char (point-max))
135 (insert-buffer-substring nntp-server-buffer)
136 ;; We have now massaged and inserted the headers from one
137 ;; group. In case some of the articles have expired or been
138 ;; cancelled, we have to mark them as read in the component
140 (let ((unfetched (gnus-sorted-complement
141 articles (nreverse fetched-articles))))
143 (gnus-group-make-articles-read group unfetched nil))))
144 ;; The headers are ready for reading, so they are inserted into
145 ;; the nntp-server-buffer, which is where Gnus expects to find
149 (set-buffer nntp-server-buffer)
151 (insert-buffer-substring "*virtual headers*")
153 (kill-buffer (current-buffer)))))))
155 (defun nnvirtual-open-server (newsgroups &optional something)
156 "Open a virtual newsgroup that contains NEWSGROUPS."
157 (nnheader-init-server-buffer))
159 (defun nnvirtual-close-server (&rest dum)
163 (defun nnvirtual-request-close ()
164 (setq nnvirtual-current-group nil
165 nnvirtual-current-groups nil
166 nnvirtual-current-mapping nil
167 nnvirtual-group-alist nil)
170 (defun nnvirtual-server-opened (&optional server)
171 "Return server process status, T or NIL.
172 If the stream is opened, return T, otherwise return NIL."
173 (and nntp-server-buffer
174 (get-buffer nntp-server-buffer)))
176 (defun nnvirtual-status-message (&optional server)
177 "Return server status response as string."
178 nnvirtual-status-string)
180 (defun nnvirtual-request-article (article &optional newsgroup server buffer)
181 "Select article by message number."
182 (nnvirtual-possibly-change-newsgroups newsgroup server t)
183 (and (numberp article)
184 (let ((map nnvirtual-current-mapping)
187 (while (< (car (car map)) article)
188 (setq offset (car (car map)))
189 (setq map (cdr map)))
190 (setq group-method (gnus-find-method-for-group (nth 1 (car map))))
191 (or (gnus-server-opened group-method)
192 (gnus-open-server group-method))
193 (gnus-request-group (nth 1 (car map)) t)
194 (gnus-request-article (- (+ (nth 2 (car map)) article) offset)
195 (nth 1 (car map)) buffer))))
197 (defun nnvirtual-request-group (group &optional server dont-check)
198 "Make GROUP the current newsgroup."
199 (nnvirtual-possibly-change-newsgroups group server dont-check)
200 (let ((map nnvirtual-current-mapping))
202 (set-buffer nntp-server-buffer)
207 (setq map (cdr map)))
208 (insert (format "211 %d 1 %d %s\n" (car (car map))
209 (car (car map)) group))
211 (setq nnvirtual-status-string "No component groups")
212 (setq nnvirtual-current-group nil)
215 (defun nnvirtual-close-group (group &optional server)
216 (if (not nnvirtual-current-group)
218 (nnvirtual-possibly-change-newsgroups group server t)
219 (nnvirtual-update-marked)
220 (setq nnvirtual-current-group nil
221 nnvirtual-current-groups nil
222 nnvirtual-current-mapping nil)
223 (setq nnvirtual-group-alist
224 (delq (assoc group nnvirtual-group-alist) nnvirtual-group-alist))))
226 (defun nnvirtual-request-list (&optional server)
227 (setq nnvirtual-status-string "nnvirtual: LIST is not implemented.")
230 (defun nnvirtual-request-newgroups (date &optional server)
232 (setq nnvirtual-status-string "NEWGROUPS is not supported.")
235 (defun nnvirtual-request-list-newsgroups (&optional server)
236 (setq nnvirtual-status-string
237 "nnvirtual: LIST NEWSGROUPS is not implemented.")
240 (defalias 'nnvirtual-request-post 'nntp-request-post)
242 (defun nnvirtual-request-post-buffer
243 (post group subject header article-buffer info follow-to respect-poster)
244 (nntp-request-post-buffer post "" subject header article-buffer
245 info follow-to respect-poster))
248 ;;; Internal functions.
250 ;; Convert HEAD headers into NOV headers.
251 (defun nnvirtual-convert-headers ()
253 (set-buffer nntp-server-buffer)
254 (let* ((gnus-newsgroup-dependencies (make-vector 100 0))
255 (headers (gnus-get-newsgroup-headers))
259 (setq header (car headers)
260 headers (cdr headers))
261 (insert (int-to-string (mail-header-number header)) "\t"
262 (or (mail-header-subject header) "") "\t"
263 (or (mail-header-from header) "") "\t"
264 (or (mail-header-date header) "") "\t"
265 (or (mail-header-id header) "") "\t"
266 (or (mail-header-references header) "") "\t"
267 (int-to-string (or (mail-header-chars header) 0)) "\t"
268 (int-to-string (or (mail-header-lines header) 0)) "\t"
269 (if (mail-header-xref header)
270 (concat "Xref: " (mail-header-xref header) "\t")
273 (defun nnvirtual-possibly-change-newsgroups (group regexp &optional check)
276 (and nnvirtual-current-group
277 (string= group nnvirtual-current-group))
278 (and (setq inf (assoc group nnvirtual-group-alist))
279 (string= (nth 3 inf) regexp)
281 (setq nnvirtual-current-group (car inf))
282 (setq nnvirtual-current-groups (nth 1 inf))
283 (setq nnvirtual-current-mapping (nth 2 inf)))))
284 (if (or (not check) (not inf))
286 (and inf (setq nnvirtual-group-alist
287 (delq inf nnvirtual-group-alist)))
288 (setq nnvirtual-current-mapping nil)
289 (setq nnvirtual-current-group group)
290 (let ((newsrc gnus-newsrc-alist)
291 (virt-group (gnus-group-prefixed-name
292 nnvirtual-current-group '(nnvirtual ""))))
293 (setq nnvirtual-current-groups nil)
295 (and (string-match regexp (car (car newsrc)))
296 (not (string= (car (car newsrc)) virt-group))
297 (setq nnvirtual-current-groups
298 (cons (car (car newsrc)) nnvirtual-current-groups)))
299 (setq newsrc (cdr newsrc))))
300 (if nnvirtual-current-groups
302 (nnvirtual-create-mapping group)
303 (setq nnvirtual-group-alist
304 (cons (list group nnvirtual-current-groups
305 nnvirtual-current-mapping regexp)
306 nnvirtual-group-alist)))
307 (setq nnvirtual-status-string
309 "nnvirtual: No newsgroups for this virtual newsgroup"))))))
310 nnvirtual-current-groups)
312 (defun nnvirtual-create-mapping (group)
313 (let* ((group (gnus-group-prefixed-name group (list 'nnvirtual "")))
314 (info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
315 (groups nnvirtual-current-groups)
317 reads unread igroup itotal ireads)
318 ;; The virtual group doesn't exist. (?)
319 (or info (error "No such group: %s" group))
320 (setq nnvirtual-current-mapping nil)
322 ;; Added by Sudish Joseph <joseph@cis.ohio-state.edu>.
323 (setq igroup (car groups))
324 (let ((info (nth 2 (gnus-gethash igroup gnus-newsrc-hashtb)))
325 (active (gnus-gethash igroup gnus-active-hashtb)))
326 ;; See if the group has had its active list read this session
327 ;; if not, we do it now.
329 (if (gnus-activate-group igroup)
331 (gnus-get-unread-articles-in-group
332 info (gnus-gethash igroup gnus-active-hashtb))
333 (setq active (gnus-gethash igroup gnus-active-hashtb)))
334 (message "Couldn't open component group %s" igroup)))
337 ;; And then we do the mapping for this component group. If
338 ;; you feel tempted to cast your eyes to the soup below -
339 ;; don't. It'll hurt your soul. Suffice to say that it
340 ;; assigns ranges of nnvirtual article numbers to the
341 ;; different component groups. To get the article number
342 ;; from the nnvirtual number, one does something like
343 ;; (+ (- number offset) (car active)), where `offset' is the
344 ;; slice the mess below assigns, and active is the lowest
345 ;; active article in the component group.
346 (setq itotal (1+ (- (cdr active) (car active))))
347 (if (setq ireads (nth 2 info))
349 (if (not (listp (cdr ireads)))
350 (setq ireads (list (cons (car ireads) (cdr ireads))))
351 (setq ireads (copy-alist ireads)))))
352 (if (< (or (and (numberp (car ireads)) (car ireads))
353 (cdr (car ireads))) (car active))
354 (setq ireads (setq itreads (cdr ireads))))
355 (if (and ireads (< (or (and (numberp (car ireads))
357 (car (car ireads))) (car active)))
358 (setcar (or (and (numberp (car ireads)) ireads)
359 (car ireads)) (1+ (car active))))
361 (setcar (or (and (numberp (car itreads)) itreads)
364 1 (- (if (numberp (car itreads))
369 (if (not (numberp (car itreads)))
370 (setcdr (car itreads)
371 (+ (- (cdr (car itreads)) (car active)) offset)))
372 (setq itreads (cdr itreads)))
373 (setq reads (nconc reads ireads))))
374 (setq offset (+ offset (1- itotal)))
375 (setq nnvirtual-current-mapping
376 (cons (list offset igroup (car active))
377 nnvirtual-current-mapping)))
378 (setq groups (cdr groups))))
379 (setq nnvirtual-current-mapping
380 (nreverse nnvirtual-current-mapping))
381 ;; Set Gnus active info.
382 (gnus-sethash group (cons 1 (1- offset)) gnus-active-hashtb)
383 ;; Set Gnus read info.
384 (setcar (nthcdr 2 info) reads)
386 ;; Then we deal with the marks.
387 (let ((map nnvirtual-current-mapping)
388 (marks '(tick dormant reply expire score))
390 tick dormant reply expire score marked active)
392 (setq igroup (nth 1 (car map)))
393 (setq active (nth 2 (car map)))
394 (setq marked (nth 3 (nth 2 (gnus-gethash igroup gnus-newsrc-hashtb))))
397 (and (assq (car m) marked)
403 0 (+ (- art active) offset))
404 (cons (+ (- (car art) active) offset)
406 (cdr (assq (car m) marked)))
407 (symbol-value (car m)))))
409 (setq offset (car (car map)))
410 (setq map (cdr map)))
411 ;; Put the list of marked articles in the info of the virtual group.
415 (and (symbol-value (car m))
416 (setq marked (cons (cons (car m) (symbol-value (car m)))
420 (setcar (nthcdr 3 info) marked)
421 (setcdr (nthcdr 2 info) (list marked)))))))
423 (defun nnvirtual-update-marked ()
424 (let ((mark-lists '((gnus-newsgroup-marked . tick)
425 (gnus-newsgroup-dormant . dormant)
426 (gnus-newsgroup-expirable . expire)
427 (gnus-newsgroup-replied . reply)))
428 marks art-group group-alist g)
430 (setq marks (symbol-value (car (car mark-lists))))
431 ;; Find out what groups the mark belong to.
433 (setq art-group (nnvirtual-art-group (car marks)))
434 (if (setq g (assoc (car art-group) group-alist))
435 (nconc g (list (cdr art-group)))
436 (setq group-alist (cons (list (car art-group) (cdr art-group))
438 (setq marks (cdr marks)))
439 ;; The groups that don't have marks must have no marks. (Yup.)
440 (let ((groups nnvirtual-current-groups))
442 (or (assoc (car groups) group-alist)
443 (setq group-alist (cons (list (car groups)) group-alist)))
444 (setq groups (cdr groups))))
445 ;; The we update the list of marks.
447 (gnus-add-marked-articles
448 (car (car group-alist)) (cdr (car mark-lists))
449 (cdr (car group-alist)) nil t)
450 (gnus-group-update-group (car (car group-alist)) t)
451 (setq group-alist (cdr group-alist)))
452 (setq mark-lists (cdr mark-lists)))))
454 (defun nnvirtual-art-group (article)
455 (let ((map nnvirtual-current-mapping)
457 (while (< (car (car map)) (if (numberp article) article (car article)))
458 (setq offset (car (car map))
460 (cons (nth 1 (car map))
461 (if (numberp article)
462 (- (+ article (nth 2 (car map))) offset)
463 (cons (- (+ (car article) (nth 2 (car map))) offset)
466 (defun nnvirtual-catchup-group (group &optional server all)
467 (nnvirtual-possibly-change-newsgroups group server)
468 (let ((gnus-group-marked nnvirtual-current-groups)
469 (gnus-expert-user t))
471 (set-buffer gnus-group-buffer)
472 (gnus-group-catchup-current nil all))))
476 ;;; nnvirtual.el ends here