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)
67 (while (< (car (car map)) (car sequence))
68 (setq offset (car (car map)))
70 (setq top (car (car map)))
71 (setq group (nth 1 (car map)))
72 (setq prefix (gnus-group-real-prefix group))
73 (setq active (nth 2 (car map)))
75 (while (and sequence (<= (car sequence) top))
76 (setq articles (cons (- (+ active (car sequence)) offset)
78 (setq sequence (cdr sequence)))
79 (setq articles (nreverse articles))
81 (setq result (gnus-retrieve-headers articles group)))
83 (set-buffer nntp-server-buffer)
84 ;; If we got HEAD headers, we convert them into NOV
85 ;; headers. This is slow, inefficient and, come to think
86 ;; of it, downright evil. So sue me. I couldn't be
87 ;; bothered to write a header parse routine that could
88 ;; parse a mixed HEAD/NOV buffer.
89 (and (eq result 'headers) (nnvirtual-convert-headers))
90 (goto-char (point-min))
93 (setq article (read nntp-server-buffer))
94 (delete-region beg (point))
95 (insert (int-to-string (+ (- article active) offset)))
98 "[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t")
99 (goto-char (match-end 0))
101 "\t" (save-excursion (end-of-line) (point)) t)
103 (while (= (char-after (1- (point))) ? )
109 (or (= (char-after (1- (point))) ?\t)
111 (insert (format "Xref: %s %s:%d\t" (system-name)
113 (if (not (string= "" prefix))
114 (while (re-search-forward
116 (save-excursion (end-of-line) (point)) t)
118 (goto-char (match-beginning 0))
121 (or (= (char-after (1- (point))) ?\t)
124 (goto-char (point-max))
125 (insert-buffer-substring nntp-server-buffer))
126 ;; The headers are ready for reading, so they are inserted into
127 ;; the nntp-server-buffer, which is where Gnus expects to find
131 (set-buffer nntp-server-buffer)
133 (insert-buffer-substring "*virtual headers*")
135 (kill-buffer (current-buffer)))))))
137 (defun nnvirtual-open-server (newsgroups &optional something)
138 "Open a virtual newsgroup that contains NEWSGROUPS."
139 (nnheader-init-server-buffer))
141 (defun nnvirtual-close-server (&rest dum)
145 (defun nnvirtual-request-close ()
146 (setq nnvirtual-current-group nil
147 nnvirtual-current-groups nil
148 nnvirtual-current-mapping nil
149 nnvirtual-group-alist nil)
152 (defun nnvirtual-server-opened (&optional server)
153 "Return server process status, T or NIL.
154 If the stream is opened, return T, otherwise return NIL."
155 (and nntp-server-buffer
156 (get-buffer nntp-server-buffer)))
158 (defun nnvirtual-status-message (&optional server)
159 "Return server status response as string."
160 nnvirtual-status-string)
162 (defun nnvirtual-request-article (article &optional newsgroup server buffer)
163 "Select article by message number."
164 (nnvirtual-possibly-change-newsgroups newsgroup server t)
165 (and (numberp article)
166 (let ((map nnvirtual-current-mapping)
168 (while (< (car (car map)) article)
169 (setq offset (car (car map)))
170 (setq map (cdr map)))
171 (gnus-request-group (nth 1 (car map)) t)
172 (gnus-request-article (- (+ (nth 2 (car map)) article) offset)
173 (nth 1 (car map)) buffer))))
175 (defun nnvirtual-request-group (group &optional server dont-check)
176 "Make GROUP the current newsgroup."
177 (nnvirtual-possibly-change-newsgroups group server dont-check)
178 (let ((map nnvirtual-current-mapping))
180 (set-buffer nntp-server-buffer)
185 (setq map (cdr map)))
186 (insert (format "211 %d 1 %d %s\n" (car (car map))
187 (car (car map)) group))
189 (setq nnvirtual-status-string "No component groups")
190 (setq nnvirtual-current-group nil)
193 (defun nnvirtual-close-group (group &optional server)
194 (if (not nnvirtual-current-group)
196 (nnvirtual-possibly-change-newsgroups group server t)
197 (nnvirtual-update-marked)
198 (setq nnvirtual-current-group nil
199 nnvirtual-current-groups nil
200 nnvirtual-current-mapping nil)
201 (setq nnvirtual-group-alist
202 (delq (assoc group nnvirtual-group-alist) nnvirtual-group-alist))))
204 (defun nnvirtual-request-list (&optional server)
205 (setq nnvirtual-status-string "nnvirtual: LIST is not implemented.")
208 (defun nnvirtual-request-newgroups (date &optional server)
210 (setq nnvirtual-status-string "NEWGROUPS is not supported.")
213 (defun nnvirtual-request-list-newsgroups (&optional server)
214 (setq nnvirtual-status-string
215 "nnvirtual: LIST NEWSGROUPS is not implemented.")
218 (defalias 'nnvirtual-request-post 'nntp-request-post)
220 (defun nnvirtual-request-post-buffer
221 (post group subject header article-buffer info follow-to respect-poster)
222 (nntp-request-post-buffer post "" subject header article-buffer
223 info follow-to respect-poster))
226 ;;; Internal functions.
228 ;; Convert HEAD headers into NOV headers.
229 (defun nnvirtual-convert-headers ()
231 (set-buffer nntp-server-buffer)
232 (let* ((gnus-newsgroup-dependencies (make-vector 100 0))
233 (headers (gnus-get-newsgroup-headers))
237 (setq header (car headers)
238 headers (cdr headers))
239 (insert (int-to-string (header-number header)) "\t"
240 (or (header-subject header) "") "\t"
241 (or (header-from header) "") "\t"
242 (or (header-date header) "") "\t"
243 (or (header-id header) "") "\t"
244 (or (header-references header) "") "\t"
245 (int-to-string (or (header-chars header) 0)) "\t"
246 (int-to-string (or (header-lines header) 0)) "\t"
247 (if (header-xref header)
248 (concat "Xref: " (header-xref header) "\t")
251 (defun nnvirtual-possibly-change-newsgroups (group regexp &optional check)
254 (and nnvirtual-current-group
255 (string= group nnvirtual-current-group))
256 (and (setq inf (assoc group nnvirtual-group-alist))
257 (string= (nth 3 inf) regexp)
259 (setq nnvirtual-current-group (car inf))
260 (setq nnvirtual-current-groups (nth 1 inf))
261 (setq nnvirtual-current-mapping (nth 2 inf)))))
262 (if (or (not check) (not inf))
264 (and inf (setq nnvirtual-group-alist
265 (delq inf nnvirtual-group-alist)))
266 (setq nnvirtual-current-mapping nil)
267 (setq nnvirtual-current-group group)
268 (let ((newsrc gnus-newsrc-alist))
269 (setq nnvirtual-current-groups nil)
271 (and (string-match regexp (car (car newsrc)))
272 (not (string= (gnus-group-real-name (car (car newsrc)))
273 nnvirtual-current-group))
274 (setq nnvirtual-current-groups
275 (cons (car (car newsrc)) nnvirtual-current-groups)))
276 (setq newsrc (cdr newsrc))))
277 (if nnvirtual-current-groups
279 (nnvirtual-create-mapping group)
280 (setq nnvirtual-group-alist
281 (cons (list group nnvirtual-current-groups
282 nnvirtual-current-mapping regexp)
283 nnvirtual-group-alist)))
284 (setq nnvirtual-status-string
286 "nnvirtual: No newsgroups for this virtual newsgroup"))))))
287 nnvirtual-current-groups)
289 (defun nnvirtual-create-mapping (group)
290 (let* ((group (gnus-group-prefixed-name group (list 'nnvirtual "")))
291 (info (nth 2 (gnus-gethash group gnus-newsrc-hashtb)))
292 (groups nnvirtual-current-groups)
296 reads unread igroup itotal itreads ireads)
297 ;; The virtual group doesn't exist. (?)
298 (or info (error "No such group: %s" group))
299 (setq nnvirtual-current-mapping nil)
301 ;; Added by Sudish Joseph <joseph@cis.ohio-state.edu>.
302 (setq igroup (car groups))
303 (let ((info (nth 2 (gnus-gethash igroup gnus-newsrc-hashtb)))
304 (active (gnus-gethash igroup gnus-active-hashtb)))
305 ;; See if the group has had its active list read this session
306 ;; if not, we do it now.
308 (if (gnus-activate-newsgroup igroup)
310 (gnus-get-unread-articles-in-group
311 info (gnus-gethash igroup gnus-active-hashtb))
312 (setq active (gnus-gethash igroup gnus-active-hashtb)))
313 (message "Couldn't open component group %s" igroup)
317 ;; And then we do the mapping for this component group. If
318 ;; you feel tempte to cast your eyes to the soup below -
319 ;; don't. It'll hurt your soul. Suffice to say that it
320 ;; assigns ranges of nnvirtual article numbers to the
321 ;; different component groups. To get the article number
322 ;; from the nnvirtual number, one does something like
323 ;; (+ (- number offset) (car active)), where `offset' is the
324 ;; slice the mess below assigns, and active is the lowest
325 ;; active article in the component group.
326 (setq itotal (1+ (- (cdr active) (car active))))
327 (if (setq ireads (nth 2 info))
329 (if (not (listp (cdr ireads)))
330 (setq ireads (list (cons (car ireads) (cdr ireads))))
331 (setq ireads (copy-alist ireads)))))
332 (if (< (or (and (numberp (car ireads)) (car ireads))
333 (cdr (car ireads))) (car active))
334 (setq ireads (setq itreads (cdr ireads))))
335 (if (and ireads (< (or (and (numberp (car ireads))
337 (car (car ireads))) (car active)))
338 (setcar (or (and (numberp (car ireads)) ireads)
339 (car ireads)) (1+ (car active))))
341 (setcar (or (and (numberp (car itreads)) itreads)
343 (+ (- (or (and (numberp (car itreads)) (car itreads))
344 (car (car itreads))) (car active)) offset))
345 (if (not (numberp (car itreads)))
346 (setcdr (car itreads)
347 (+ (- (cdr (car itreads)) (car active)) offset)))
348 (setq itreads (cdr itreads)))
349 (setq reads (nconc reads ireads))))
350 (setq offset (+ offset (1- itotal)))
351 (setq nnvirtual-current-mapping
352 (cons (list offset igroup (car active))
353 nnvirtual-current-mapping)))
354 (setq groups (cdr groups))))
355 (setq nnvirtual-current-mapping
356 (nreverse nnvirtual-current-mapping))
357 (gnus-sethash group (cons 1 offset) gnus-active-hashtb)
358 (setcar (nthcdr 2 info) reads)
360 ;; Then we deal with the marks.
361 (let ((map nnvirtual-current-mapping)
362 (marks '(tick dormant reply expire score))
364 tick dormant reply expire score marked active)
366 (setq igroup (nth 1 (car map)))
367 (setq active (nth 2 (car map)))
368 (setq marked (nth 3 (nth 2 (gnus-gethash igroup gnus-newsrc-hashtb))))
371 (and (assq (car m) marked)
377 0 (+ (- art active) offset))
378 (cons (+ (- (car art) active) offset)
380 (cdr (assq (car m) marked)))
381 (symbol-value (car m)))))
383 (setq offset (car (car map)))
384 (setq map (cdr map)))
385 ;; Put the list of marked articles in the info of the virtual group.
389 (and (symbol-value (car m))
390 (setq marked (cons (cons (car m) (symbol-value (car m)))
394 (setcar (nthcdr 3 info) marked)
395 (setcdr (nthcdr 2 info) (list marked)))))))
397 (defun nnvirtual-update-marked ()
398 (let ((mark-lists '((gnus-newsgroup-marked . tick)
399 (gnus-newsgroup-dormant . dormant)
400 (gnus-newsgroup-expirable . expire)
401 (gnus-newsgroup-replied . reply)))
402 marks art-group group-alist g)
404 (setq marks (symbol-value (car (car mark-lists))))
406 (setq art-group (nnvirtual-art-group (car marks)))
407 (if (setq g (assoc (car art-group) group-alist))
408 (nconc g (list (cdr art-group)))
409 (setq group-alist (cons (list (car art-group) (cdr art-group))
411 (setq marks (cdr marks)))
413 (gnus-add-marked-articles
414 (car (car group-alist)) (cdr (car mark-lists))
415 (cdr (car group-alist)) nil t)
416 (gnus-group-update-group (car (car group-alist)) t)
417 (setq group-alist (cdr group-alist)))
418 (setq mark-lists (cdr mark-lists)))))
420 (defun nnvirtual-art-group (article)
421 (let ((map nnvirtual-current-mapping)
423 (while (< (car (car map)) (if (numberp article) article (car article)))
424 (setq offset (car (car map))
426 (cons (nth 1 (car map))
427 (if (numberp article)
428 (- (+ article (nth 2 (car map))) offset)
429 (cons (- (+ (car article) (nth 2 (car map))) offset)
432 (defun nnvirtual-catchup-group (group &optional server all)
433 (nnvirtual-possibly-change-newsgroups group server)
434 (let ((gnus-group-marked nnvirtual-current-groups)
435 (gnus-expert-user t))
436 (set-buffer gnus-group-buffer)
437 (gnus-group-catchup-current nil all)))
441 ;;; nnvirtual.el ends here