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 (let ((map nnvirtual-current-mapping)
63 articles beg group active top article result prefix)
65 (while (< (car (car map)) (car sequence))
66 (setq offset (car (car map)))
68 (setq top (car (car map)))
69 (setq group (nth 1 (car map)))
70 (setq prefix (gnus-group-real-prefix group))
71 (setq active (nth 2 (car map)))
73 (while (and sequence (<= (car sequence) top))
74 (setq articles (cons (- (+ active (car sequence)) offset) articles))
75 (setq sequence (cdr sequence)))
76 (setq articles (nreverse articles))
78 (setq result (gnus-retrieve-headers articles group)))
80 (set-buffer nntp-server-buffer)
81 ;; If we got HEAD headers, we convert them into NOV
82 ;; headers. This is slow, inefficient and, come to think
83 ;; of it, downright evil. So sue me. I couldn't be
84 ;; bothered to write a header parse routine that could
85 ;; parse a mixed HEAD/NOV buffer.
86 (and (eq result 'headers) (nnvirtual-convert-headers))
87 (goto-char (point-min))
90 (setq article (read nntp-server-buffer))
91 (delete-region beg (point))
92 (insert (int-to-string (+ (- article active) offset)))
94 (looking-at "[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t")
95 (goto-char (match-end 0))
97 "\t" (save-excursion (end-of-line) (point)) t)
99 (while (= (char-after (1- (point))) ? )
105 (or (= (char-after (1- (point))) ?\t)
107 (insert (format "Xref: %s %s:%d\t" (system-name)
109 (if (not (string= "" prefix))
110 (while (re-search-forward
112 (save-excursion (end-of-line) (point)) t)
114 (goto-char (match-beginning 0))
117 (or (= (char-after (1- (point))) ?\t)
120 (goto-char (point-max))
121 (insert-buffer-substring nntp-server-buffer))
122 ;; The headers are ready for reading, so they are inserted into
123 ;; the nntp-server-buffer, which is where Gnus expects to find
127 (set-buffer nntp-server-buffer)
129 (insert-buffer-substring "*virtual headers*")
131 (kill-buffer (current-buffer))))))
133 (defun nnvirtual-open-server (newsgroups &optional something)
134 "Open a virtual newsgroup that contains NEWSGROUPS."
135 (nnheader-init-server-buffer))
137 (defun nnvirtual-close-server (&rest dum)
141 (defun nnvirtual-server-opened (&optional server)
142 "Return server process status, T or NIL.
143 If the stream is opened, return T, otherwise return NIL."
144 (and nntp-server-buffer
145 (get-buffer nntp-server-buffer)))
147 (defun nnvirtual-status-message (&optional server)
148 "Return server status response as string."
149 nnvirtual-status-string)
151 (defun nnvirtual-request-article (article &optional newsgroup server buffer)
152 "Select article by message number."
153 (nnvirtual-possibly-change-newsgroups newsgroup server t)
154 (and (numberp article)
155 (let ((map nnvirtual-current-mapping)
157 (while (< (car (car map)) article)
158 (setq offset (car (car map)))
159 (setq map (cdr map)))
160 (gnus-request-group (nth 1 (car map)) t)
161 (gnus-request-article (- (+ (nth 2 (car map)) article) offset)
162 (nth 1 (car map)) buffer))))
164 (defun nnvirtual-request-group (group &optional server dont-check)
165 "Make GROUP the current newsgroup."
166 (nnvirtual-possibly-change-newsgroups group server dont-check)
167 (let ((map nnvirtual-current-mapping))
169 (set-buffer nntp-server-buffer)
174 (setq map (cdr map)))
175 (insert (format "211 %d 1 %d %s\n" (car (car map))
176 (car (car map)) group))
178 (setq nnvirtual-status-string "No component groups")
179 (setq nnvirtual-current-group nil)
182 (defun nnvirtual-close-group (group &optional server)
183 (if (not nnvirtual-current-group)
185 (nnvirtual-possibly-change-newsgroups group server t)
186 (nnvirtual-update-marked)
187 (setq nnvirtual-current-group nil
188 nnvirtual-current-groups nil
189 nnvirtual-current-mapping nil)
190 (setq nnvirtual-group-alist
191 (delq (assoc group nnvirtual-group-alist) nnvirtual-group-alist))))
193 (defun nnvirtual-request-list (&optional server)
194 (setq nnvirtual-status-string "nnvirtual: LIST is not implemented.")
197 (defun nnvirtual-request-newgroups (date &optional server)
199 (setq nnvirtual-status-string "NEWGROUPS is not supported.")
202 (defun nnvirtual-request-list-newsgroups (&optional server)
203 (setq nnvirtual-status-string
204 "nnvirtual: LIST NEWSGROUPS is not implemented.")
207 (fset 'nnvirtual-request-post 'nntp-request-post)
209 (defun nnvirtual-request-post-buffer
210 (post group subject header article-buffer info follow-to respect-poster)
211 (nntp-request-post-buffer post "" subject header article-buffer
212 info follow-to respect-poster))
215 ;;; Internal functions.
217 ;; Convert HEAD headers into NOV headers.
218 (defun nnvirtual-convert-headers ()
220 (set-buffer nntp-server-buffer)
221 (let* ((gnus-newsgroup-dependencies (make-vector 100 0))
222 (headers (gnus-get-newsgroup-headers))
226 (setq header (car headers)
227 headers (cdr headers))
228 (insert (int-to-string (header-number header)) "\t"
229 (or (header-subject header) "") "\t"
230 (or (header-from header) "") "\t"
231 (or (header-date header) "") "\t"
232 (or (header-id header) "") "\t"
233 (or (header-references header) "") "\t"
234 (int-to-string (or (header-chars header) 0)) "\t"
235 (int-to-string (or (header-lines header) 0)) "\t"
236 (if (header-xref header)
237 (concat "Xref: " (header-xref header) "\t")
240 (defun nnvirtual-possibly-change-newsgroups (group regexp &optional check)
243 (and nnvirtual-current-group
244 (string= group nnvirtual-current-group))
245 (and (setq inf (assoc group nnvirtual-group-alist))
246 (string= (nth 3 inf) regexp)
248 (setq nnvirtual-current-group (car inf))
249 (setq nnvirtual-current-groups (nth 1 inf))
250 (setq nnvirtual-current-mapping (nth 2 inf)))))
251 (if (or (not check) (not inf))
253 (and inf (setq nnvirtual-group-alist
254 (delq inf nnvirtual-group-alist)))
255 (setq nnvirtual-current-mapping nil)
256 (setq nnvirtual-current-group group)
257 (let ((newsrc gnus-newsrc-alist))
258 (setq nnvirtual-current-groups nil)
260 (and (string-match regexp (car (car newsrc)))
261 (setq nnvirtual-current-groups
262 (cons (car (car newsrc)) nnvirtual-current-groups)))
263 (setq newsrc (cdr newsrc))))
264 (if nnvirtual-current-groups
266 (nnvirtual-create-mapping group)
267 (setq nnvirtual-group-alist
268 (cons (list group nnvirtual-current-groups
269 nnvirtual-current-mapping regexp)
270 nnvirtual-group-alist)))
271 (setq nnvirtual-status-string
273 "nnvirtual: No newsgroups for this virtual newsgroup"))))))
274 nnvirtual-current-groups)
276 (defun nnvirtual-create-mapping (group)
277 (let* ((group (gnus-group-prefixed-name group (list 'nnvirtual "")))
278 (info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
279 (groups nnvirtual-current-groups)
283 reads unread igroup itotal itreads ireads)
284 ;; The virtual group doesn't exist. (?)
285 (or info (error "No such group: %s" group))
286 (setq nnvirtual-current-mapping nil)
288 ;; Added by Sudish Joseph <joseph@cis.ohio-state.edu>.
289 (setq igroup (car groups))
290 (let ((info (nth 2 (gnus-gethash igroup gnus-newsrc-hashtb)))
291 (active (gnus-gethash igroup gnus-active-hashtb)))
292 ;; See if the group has had its active list read this session
293 ;; if not, we do it now.
295 (if (gnus-activate-newsgroup igroup)
297 (gnus-get-unread-articles-in-group
298 info (gnus-gethash igroup gnus-active-hashtb))
299 (setq active (gnus-gethash igroup gnus-active-hashtb)))
300 (message "Couldn't open component group %s" igroup)
304 ;; And then we do the mapping for this component group. If
305 ;; you feel tempte to cast your eyes to the soup below -
306 ;; don't. It'll hurt your soul. Suffice to say that it
307 ;; assigns ranges of nnvirtual article numbers to the
308 ;; different component groups. To get the article number
309 ;; from the nnvirtual number, one does something like
310 ;; (+ (- number offset) (car active)), where `offset' is the
311 ;; slice the mess below assigns, and active is the lowest
312 ;; active article in the component group.
313 (setq itotal (1+ (- (cdr active) (car active))))
314 (if (setq ireads (nth 2 info))
316 (if (not (listp (cdr ireads)))
317 (setq ireads (list (cons (car ireads) (cdr ireads))))
318 (setq ireads (copy-alist ireads)))))
319 (if (< (or (and (numberp (car ireads)) (car ireads))
320 (cdr (car ireads))) (car active))
321 (setq ireads (setq itreads (cdr ireads))))
322 (if (and ireads (< (or (and (numberp (car ireads))
324 (car (car ireads))) (car active)))
325 (setcar (or (and (numberp (car ireads)) ireads)
326 (car ireads)) (1+ (car active))))
328 (setcar (or (and (numberp (car itreads)) itreads)
330 (+ (- (or (and (numberp (car itreads)) (car itreads))
331 (car (car itreads))) (car active)) offset))
332 (if (not (numberp (car itreads)))
333 (setcdr (car itreads)
334 (+ (- (cdr (car itreads)) (car active)) offset)))
335 (setq itreads (cdr itreads)))
336 (setq reads (nconc reads ireads))))
337 (setq offset (+ offset (1- itotal)))
338 (setq nnvirtual-current-mapping
339 (cons (list offset igroup (car active))
340 nnvirtual-current-mapping)))
341 (setq groups (cdr groups))))
342 (setq nnvirtual-current-mapping
343 (nreverse nnvirtual-current-mapping))
344 (gnus-sethash group (cons 1 offset) gnus-active-hashtb)
345 (setcar (nthcdr 2 info) reads)
347 ;; Then we deal with the marks.
348 (let ((map nnvirtual-current-mapping)
349 (marks '(tick dormant reply expire score))
351 tick dormant reply expire score marked active)
353 (setq igroup (nth 1 (car map)))
354 (setq active (nth 2 (car map)))
355 (setq marked (nth 3 (nth 2 (gnus-gethash igroup gnus-newsrc-hashtb))))
358 (and (assq (car m) marked)
364 0 (+ (- art active) offset))
365 (cons (+ (- (car art) active) offset)
367 (cdr (assq (car m) marked)))
368 (symbol-value (car m)))))
370 (setq offset (car (car map)))
371 (setq map (cdr map)))
372 ;; Put the list of marked articles in the info of the virtual group.
376 (and (symbol-value (car m))
377 (setq marked (cons (cons (car m) (symbol-value (car m)))
381 (setcar (nthcdr 3 info) marked)
382 (setcdr (nthcdr 2 info) (list marked)))))))
384 (defun nnvirtual-update-marked ()
385 (let ((mark-lists '((gnus-newsgroup-marked . tick)
386 (gnus-newsgroup-dormant . dormant)
387 (gnus-newsgroup-expirable . expire)
388 (gnus-newsgroup-replied . reply)))
389 marks art-group group-alist g)
391 (setq marks (symbol-value (car (car mark-lists))))
393 (setq art-group (nnvirtual-art-group (car marks)))
394 (if (setq g (assoc (car art-group) group-alist))
395 (nconc g (list (cdr art-group)))
396 (setq group-alist (cons (list (car art-group) (cdr art-group))
398 (setq marks (cdr marks)))
400 (gnus-add-marked-articles
401 (car (car group-alist)) (cdr (car mark-lists))
402 (cdr (car group-alist)) nil t)
403 (gnus-group-update-group (car (car group-alist)) t)
404 (setq group-alist (cdr group-alist)))
405 (setq mark-lists (cdr mark-lists)))))
407 (defun nnvirtual-art-group (article)
408 (let ((map nnvirtual-current-mapping)
410 (while (< (car (car map)) (if (numberp article) article (car article)))
411 (setq offset (car (car map))
413 (cons (nth 1 (car map))
414 (if (numberp article)
415 (- (+ article (nth 2 (car map))) offset)
416 (cons (- (+ (car article) (nth 2 (car map))) offset)
421 ;;; nnvirtual.el ends here