1 ;;; nnvirtual.el --- virtual newsgroups access for Gnus
3 ;; Copyright (C) 1994-2011 Free Software Foundation, Inc.
5 ;; Author: David Moore <dmoore@ucsd.edu>
6 ;; Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;; The other access methods (nntp, nnspool, etc) are general news
28 ;; access methods. This module relies on Gnus and can not be used
41 (eval-when-compile (require 'cl))
43 (nnoo-declare nnvirtual)
45 (defvoo nnvirtual-always-rescan t
46 "If non-nil, always scan groups for unread articles when entering a group.
47 If this variable is nil and you read articles in a component group
48 after the virtual group has been activated, the read articles from the
49 component group will show up when you enter the virtual group.")
51 (defvoo nnvirtual-component-regexp nil
52 "Regexp to match component groups.")
54 (defvoo nnvirtual-component-groups nil
55 "Component group in this nnvirtual group.")
59 (defconst nnvirtual-version "nnvirtual 1.1")
61 (defvoo nnvirtual-current-group nil)
63 (defvoo nnvirtual-mapping-table nil
64 "Table of rules on how to map between component group and article number to virtual article number.")
66 (defvoo nnvirtual-mapping-offsets nil
67 "Table indexed by component group to an offset to be applied to article numbers in that group.")
69 (defvoo nnvirtual-mapping-len 0
70 "Number of articles in this virtual group.")
72 (defvoo nnvirtual-mapping-reads nil
73 "Compressed sequence of read articles on the virtual group as computed from the unread status of individual component groups.")
75 (defvoo nnvirtual-mapping-marks nil
76 "Compressed marks alist for the virtual group as computed from the marks of individual component groups.")
78 (defvoo nnvirtual-info-installed nil
79 "T if we have already installed the group info for this group, and shouldn't blast over it again.")
81 (defvoo nnvirtual-status-string "")
83 (autoload 'gnus-cache-articles-in-group "gnus-cache")
87 ;;; Interface functions.
89 (nnoo-define-basics nnvirtual)
92 (deffoo nnvirtual-retrieve-headers (articles &optional newsgroup
94 (when (nnvirtual-possibly-change-server server)
95 (with-current-buffer nntp-server-buffer
97 (if (stringp (car articles))
99 (let ((vbuf (nnheader-set-temp-buffer
100 (get-buffer-create " *virtual headers*")))
101 (carticles (nnvirtual-partition-sequence articles))
102 (system-name (system-name))
103 cgroup carticle article result prefix)
105 (setq cgroup (caar carticles))
106 (setq articles (cdar carticles))
110 (gnus-find-method-for-group cgroup) t)
111 (gnus-request-group cgroup t)
112 (setq prefix (gnus-group-real-prefix cgroup))
113 ;; FIX FIX FIX we want to check the cache!
114 ;; This is probably evil if people have set
115 ;; gnus-use-cache to nil themselves, but I
116 ;; have no way of finding the true value of it.
117 (let ((gnus-use-cache t))
118 (setq result (gnus-retrieve-headers
119 articles cgroup nil))))
120 (set-buffer nntp-server-buffer)
121 ;; If we got HEAD headers, we convert them into NOV
122 ;; headers. This is slow, inefficient and, come to think
123 ;; of it, downright evil. So sue me. I couldn't be
124 ;; bothered to write a header parse routine that could
125 ;; parse a mixed HEAD/NOV buffer.
126 (when (eq result 'headers)
127 (nnvirtual-convert-headers))
128 (goto-char (point-min))
130 (delete-region (point)
132 (setq carticle (read nntp-server-buffer))
135 ;; We remove this article from the articles list, if
136 ;; anything is left in the articles list after going through
137 ;; the entire buffer, then those articles have been
138 ;; expired or canceled, so we appropriately update the
139 ;; component group below. They should be coming up
140 ;; generally in order, so this shouldn't be slow.
141 (setq articles (delq carticle articles))
143 (setq article (nnvirtual-reverse-map-article cgroup carticle))
145 ;; This line has no reverse mapping, that means it
146 ;; was an extra article reference returned by nntp.
149 (delete-region (point) (progn (forward-line 1) (point))))
150 ;; Otherwise insert the virtual article number,
151 ;; and clean up the xrefs.
152 (princ article nntp-server-buffer)
153 (nnvirtual-update-xref-header cgroup carticle
159 (goto-char (point-max))
160 (insert-buffer-substring nntp-server-buffer))
161 ;; Anything left in articles is expired or canceled.
162 ;; Could be smart and not tell it about articles already known?
164 (gnus-group-make-articles-read cgroup articles))
167 ;; The headers are ready for reading, so they are inserted into
168 ;; the nntp-server-buffer, which is where Gnus expects to find
171 (with-current-buffer nntp-server-buffer
173 (insert-buffer-substring vbuf)
174 ;; FIX FIX FIX, we should be able to sort faster than
175 ;; this if needed, since each cgroup is sorted, we just
177 (sort-numeric-fields 1 (point-min) (point-max))
179 (kill-buffer vbuf)))))))
182 (defvoo nnvirtual-last-accessed-component-group nil)
184 (deffoo nnvirtual-request-article (article &optional group server buffer)
185 (when (nnvirtual-possibly-change-server server)
186 (if (stringp article)
187 ;; This is a fetch by Message-ID.
189 ((not nnvirtual-last-accessed-component-group)
191 'nnvirtual "Don't know what server to request from"))
196 (let* ((gnus-override-method nil)
197 (method (gnus-find-method-for-group
198 nnvirtual-last-accessed-component-group)))
199 (funcall (gnus-get-function method 'request-article)
200 article nil (nth 1 method) buffer)))))
201 ;; This is a fetch by number.
202 (let* ((amap (nnvirtual-map-article article))
206 (nnheader-report 'nnvirtual "No such article: %s" article))
207 ((not (gnus-check-group cgroup))
209 'nnvirtual "Can't open server where %s exists" cgroup))
210 ((not (gnus-request-group cgroup t))
211 (nnheader-report 'nnvirtual "Can't open component group %s" cgroup))
213 (setq nnvirtual-last-accessed-component-group cgroup)
215 (with-current-buffer buffer
216 ;; We bind this here to avoid double decoding.
217 (let ((gnus-article-decode-hook nil))
218 (gnus-request-article-this-buffer (cdr amap) cgroup)))
219 (gnus-request-article (cdr amap) cgroup))))))))
222 (deffoo nnvirtual-open-server (server &optional defs)
223 (unless (assq 'nnvirtual-component-regexp defs)
224 (push `(nnvirtual-component-regexp ,server)
226 (nnoo-change-server 'nnvirtual server defs)
227 (if nnvirtual-component-groups
229 (setq nnvirtual-mapping-table nil
230 nnvirtual-mapping-offsets nil
231 nnvirtual-mapping-len 0
232 nnvirtual-mapping-reads nil
233 nnvirtual-mapping-marks nil
234 nnvirtual-info-installed nil)
235 (when nnvirtual-component-regexp
236 ;; Go through the newsrc alist and find all component groups.
237 (let ((newsrc (cdr gnus-newsrc-alist))
239 (while (setq group (car (pop newsrc)))
240 (when (string-match nnvirtual-component-regexp group) ; Match
241 ;; Add this group to the list of component groups.
242 (setq nnvirtual-component-groups
243 (cons group (delete group nnvirtual-component-groups)))))))
244 (if (not nnvirtual-component-groups)
245 (nnheader-report 'nnvirtual "No component groups: %s" server)
249 (deffoo nnvirtual-request-group (group &optional server dont-check info)
250 (nnvirtual-possibly-change-server server)
251 (setq nnvirtual-component-groups
252 (delete (nnvirtual-current-group) nnvirtual-component-groups))
254 ((null nnvirtual-component-groups)
255 (setq nnvirtual-current-group nil)
256 (nnheader-report 'nnvirtual "No component groups in %s" group))
258 (setq nnvirtual-current-group group)
259 (nnvirtual-create-mapping dont-check)
260 (when nnvirtual-always-rescan
261 (nnvirtual-request-update-info
262 (nnvirtual-current-group)
263 (gnus-get-info (nnvirtual-current-group))))
264 (nnheader-insert "211 %d 1 %d %s\n"
265 nnvirtual-mapping-len nnvirtual-mapping-len group))))
268 (deffoo nnvirtual-request-type (group &optional article)
271 (if (numberp article)
272 (let ((mart (nnvirtual-map-article article)))
274 (gnus-request-type (car mart) (cdr mart))))
276 nnvirtual-last-accessed-component-group nil))))
278 (deffoo nnvirtual-request-update-mark (group article mark)
279 (let* ((nart (nnvirtual-map-article article))
282 (memq mark gnus-auto-expirable-marks)
283 ;; The component group might be a virtual group.
284 (= mark (gnus-request-update-mark cgroup (cdr nart) mark))
285 (gnus-group-auto-expirable-p cgroup))
286 (setq mark gnus-expirable-mark)))
290 (deffoo nnvirtual-close-group (group &optional server)
291 (when (and (nnvirtual-possibly-change-server server)
292 (not (gnus-ephemeral-group-p (nnvirtual-current-group))))
293 (nnvirtual-update-read-and-marked t t))
297 (deffoo nnvirtual-request-newgroups (date &optional server)
298 (nnheader-report 'nnvirtual "NEWGROUPS is not supported."))
301 (deffoo nnvirtual-request-list-newsgroups (&optional server)
302 (nnheader-report 'nnvirtual "LIST NEWSGROUPS is not implemented."))
305 (deffoo nnvirtual-request-update-info (group info &optional server)
306 (when (and (nnvirtual-possibly-change-server server)
307 (not nnvirtual-info-installed))
308 ;; Install the precomputed lists atomically, so the virtual group
309 ;; is not left in a half-way state in case of C-g.
311 (setcar (cddr info) nnvirtual-mapping-reads)
313 (setcar (nthcdr 3 info) nnvirtual-mapping-marks)
314 (when nnvirtual-mapping-marks
315 (setcdr (nthcdr 2 info) (list nnvirtual-mapping-marks))))
316 (setq nnvirtual-info-installed t))
320 (deffoo nnvirtual-catchup-group (group &optional server all)
321 (when (and (nnvirtual-possibly-change-server server)
322 (not (gnus-ephemeral-group-p (nnvirtual-current-group))))
323 ;; copy over existing marks first, in case they set anything
324 (nnvirtual-update-read-and-marked nil nil)
325 ;; do a catchup on all component groups
326 (let ((gnus-group-marked (copy-sequence nnvirtual-component-groups))
327 (gnus-expert-user t))
328 ;; Make sure all groups are activated.
331 (when (not (numberp (gnus-group-unread g)))
332 (gnus-activate-group g)))
333 nnvirtual-component-groups)
334 (with-current-buffer gnus-group-buffer
335 (gnus-group-catchup-current nil all)))))
338 (deffoo nnvirtual-find-group-art (group article)
339 "Return the real group and article for virtual GROUP and ARTICLE."
340 (nnvirtual-map-article article))
343 (deffoo nnvirtual-request-post (&optional server)
344 (if (not gnus-message-group-art)
345 (nnheader-report 'nnvirtual "Can't post to an nnvirtual group")
346 (let ((group (car (nnvirtual-find-group-art
347 (car gnus-message-group-art)
348 (cdr gnus-message-group-art)))))
349 (gnus-request-post (gnus-find-method-for-group group)))))
352 (deffoo nnvirtual-request-expire-articles (articles group
353 &optional server force)
354 (nnvirtual-possibly-change-server server)
355 (setq nnvirtual-component-groups
356 (delete (nnvirtual-current-group) nnvirtual-component-groups))
358 (dolist (group nnvirtual-component-groups)
359 (setq unexpired (nconc unexpired
362 (nnvirtual-reverse-map-article
364 (gnus-uncompress-range
365 (gnus-group-expire-articles-1 group))))))
366 (sort (delq nil unexpired) '<)))
369 ;;; Internal functions.
371 (defun nnvirtual-convert-headers ()
372 "Convert HEAD headers into NOV headers."
373 (with-current-buffer nntp-server-buffer
374 (let* ((dependencies (make-vector 100 0))
375 (headers (gnus-get-newsgroup-headers dependencies)))
377 (mapc 'nnheader-insert-nov headers))))
380 (defun nnvirtual-update-xref-header (group article prefix system-name)
381 "Edit current NOV header in current buffer to have an xref to the component group, and also server prefix any existing xref lines."
382 ;; Move to beginning of Xref field, creating a slot if needed.
385 "[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t")
386 (goto-char (match-end 0))
387 (unless (search-forward "\t" (point-at-eol) 'move)
390 ;; Remove any spaces at the beginning of the Xref field.
391 (while (eq (char-after (1- (point))) ? )
395 (insert "Xref: " system-name " " group ":")
396 (princ article (current-buffer))
399 ;; If there were existing xref lines, clean them up to have the correct
400 ;; component server prefix.
402 (narrow-to-region (point)
403 (or (search-forward "\t" (point-at-eol) t)
405 (goto-char (point-min))
406 (when (re-search-forward "Xref: *[^\n:0-9 ]+ *" nil t)
407 (replace-match "" t t))
408 (goto-char (point-min))
409 (when (re-search-forward
410 (concat (regexp-quote (gnus-group-real-name group)) ":[0-9]+")
412 (replace-match "" t t))
415 (when (not (string= "" prefix))
416 (while (re-search-forward "[^ ]+:[0-9]+" nil t)
418 (goto-char (match-beginning 0))
421 ;; Ensure a trailing \t.
423 (or (eq (char-after (1- (point))) ?\t)