Commit | Line | Data |
---|---|---|
eec82323 | 1 | ;;; gnus-cache.el --- cache interface for Gnus |
6748645f | 2 | ;; Copyright (C) 1995,96,97,98 Free Software Foundation, Inc. |
eec82323 | 3 | |
6748645f | 4 | ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org> |
eec82323 LMI |
5 | ;; Keywords: news |
6 | ||
7 | ;; This file is part of GNU Emacs. | |
8 | ||
9 | ;; GNU Emacs is free software; you can redistribute it and/or modify | |
10 | ;; it under the terms of the GNU General Public License as published by | |
11 | ;; the Free Software Foundation; either version 2, or (at your option) | |
12 | ;; any later version. | |
13 | ||
14 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
15 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | ;; GNU General Public License for more details. | |
18 | ||
19 | ;; You should have received a copy of the GNU General Public License | |
20 | ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
21 | ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
22 | ;; Boston, MA 02111-1307, USA. | |
23 | ||
24 | ;;; Commentary: | |
25 | ||
26 | ;;; Code: | |
27 | ||
7df7482d RS |
28 | (eval-when-compile (require 'cl)) |
29 | ||
6748645f LMI |
30 | (eval-when-compile (require 'cl)) |
31 | ||
eec82323 LMI |
32 | (require 'gnus) |
33 | (require 'gnus-int) | |
34 | (require 'gnus-range) | |
35 | (require 'gnus-start) | |
36 | (eval-when-compile | |
37 | (require 'gnus-sum)) | |
38 | ||
eec82323 LMI |
39 | (defcustom gnus-cache-active-file |
40 | (concat (file-name-as-directory gnus-cache-directory) "active") | |
41 | "*The cache active file." | |
42 | :group 'gnus-cache | |
43 | :type 'file) | |
44 | ||
45 | (defcustom gnus-cache-enter-articles '(ticked dormant) | |
46 | "Classes of articles to enter into the cache." | |
47 | :group 'gnus-cache | |
48 | :type '(set (const ticked) (const dormant) (const unread) (const read))) | |
49 | ||
50 | (defcustom gnus-cache-remove-articles '(read) | |
51 | "Classes of articles to remove from the cache." | |
52 | :group 'gnus-cache | |
53 | :type '(set (const ticked) (const dormant) (const unread) (const read))) | |
54 | ||
6748645f LMI |
55 | (defcustom gnus-cacheable-groups nil |
56 | "*Groups that match this regexp will be cached. | |
57 | ||
58 | If you only want to cache your nntp groups, you could set this | |
59 | variable to \"^nntp\". | |
60 | ||
61 | If a group matches both gnus-cacheable-groups and gnus-uncacheable-groups | |
62 | it's not cached." | |
63 | :group 'gnus-cache | |
64 | :type '(choice (const :tag "off" nil) | |
65 | regexp)) | |
66 | ||
eec82323 LMI |
67 | (defcustom gnus-uncacheable-groups nil |
68 | "*Groups that match this regexp will not be cached. | |
69 | ||
70 | If you want to avoid caching your nnml groups, you could set this | |
6748645f LMI |
71 | variable to \"^nnml\". |
72 | ||
73 | If a group matches both gnus-cacheable-groups and gnus-uncacheable-groups | |
74 | it's not cached." | |
eec82323 LMI |
75 | :group 'gnus-cache |
76 | :type '(choice (const :tag "off" nil) | |
77 | regexp)) | |
78 | ||
6748645f LMI |
79 | (defvar gnus-cache-overview-coding-system 'raw-text |
80 | "Coding system used on Gnus cache files.") | |
81 | ||
eec82323 LMI |
82 | \f |
83 | ||
84 | ;;; Internal variables. | |
85 | ||
86 | (defvar gnus-cache-removable-articles nil) | |
87 | (defvar gnus-cache-buffer nil) | |
88 | (defvar gnus-cache-active-hashtb nil) | |
89 | (defvar gnus-cache-active-altered nil) | |
90 | ||
91 | (eval-and-compile | |
92 | (autoload 'nnml-generate-nov-databases-1 "nnml") | |
93 | (autoload 'nnvirtual-find-group-art "nnvirtual")) | |
94 | ||
95 | \f | |
96 | ||
97 | ;;; Functions called from Gnus. | |
98 | ||
99 | (defun gnus-cache-open () | |
100 | "Initialize the cache." | |
101 | (when (or (file-exists-p gnus-cache-directory) | |
102 | (and gnus-use-cache | |
103 | (not (eq gnus-use-cache 'passive)))) | |
104 | (gnus-cache-read-active))) | |
105 | ||
106 | ;; Complexities of byte-compiling make this kludge necessary. Eeek. | |
107 | (ignore-errors | |
108 | (gnus-add-shutdown 'gnus-cache-close 'gnus)) | |
109 | ||
110 | (defun gnus-cache-close () | |
111 | "Shut down the cache." | |
112 | (gnus-cache-write-active) | |
113 | (gnus-cache-save-buffers) | |
114 | (setq gnus-cache-active-hashtb nil)) | |
115 | ||
116 | (defun gnus-cache-save-buffers () | |
117 | ;; save the overview buffer if it exists and has been modified | |
118 | ;; delete empty cache subdirectories | |
119 | (when gnus-cache-buffer | |
120 | (let ((buffer (cdr gnus-cache-buffer)) | |
121 | (overview-file (gnus-cache-file-name | |
122 | (car gnus-cache-buffer) ".overview"))) | |
123 | ;; write the overview only if it was modified | |
124 | (when (buffer-modified-p buffer) | |
125 | (save-excursion | |
126 | (set-buffer buffer) | |
127 | (if (> (buffer-size) 0) | |
128 | ;; Non-empty overview, write it to a file. | |
6748645f LMI |
129 | (let ((coding-system-for-write |
130 | gnus-cache-overview-coding-system)) | |
131 | (gnus-write-buffer overview-file)) | |
eec82323 LMI |
132 | ;; Empty overview file, remove it |
133 | (when (file-exists-p overview-file) | |
134 | (delete-file overview-file)) | |
135 | ;; If possible, remove group's cache subdirectory. | |
136 | (condition-case nil | |
137 | ;; FIXME: we can detect the error type and warn the user | |
138 | ;; of any inconsistencies (articles w/o nov entries?). | |
139 | ;; for now, just be conservative...delete only if safe -- sj | |
140 | (delete-directory (file-name-directory overview-file)) | |
141 | (error nil))))) | |
142 | ;; Kill the buffer -- it's either unmodified or saved. | |
143 | (gnus-kill-buffer buffer) | |
144 | (setq gnus-cache-buffer nil)))) | |
145 | ||
146 | (defun gnus-cache-possibly-enter-article | |
147 | (group article headers ticked dormant unread &optional force) | |
148 | (when (and (or force (not (eq gnus-use-cache 'passive))) | |
149 | (numberp article) | |
150 | (> article 0) | |
151 | (vectorp headers)) ; This might be a dummy article. | |
152 | ;; If this is a virtual group, we find the real group. | |
153 | (when (gnus-virtual-group-p group) | |
154 | (let ((result (nnvirtual-find-group-art | |
155 | (gnus-group-real-name group) article))) | |
156 | (setq group (car result) | |
157 | headers (copy-sequence headers)) | |
158 | (mail-header-set-number headers (cdr result)))) | |
159 | (let ((number (mail-header-number headers)) | |
6748645f | 160 | file) |
a8151ef7 LMI |
161 | (when (and number |
162 | (> number 0) ; Reffed article. | |
eec82323 | 163 | (or force |
6748645f LMI |
164 | (and (or (not gnus-cacheable-groups) |
165 | (string-match gnus-cacheable-groups group)) | |
166 | (or (not gnus-uncacheable-groups) | |
eec82323 LMI |
167 | (not (string-match |
168 | gnus-uncacheable-groups group))) | |
169 | (gnus-cache-member-of-class | |
170 | gnus-cache-enter-articles ticked dormant unread))) | |
171 | (not (file-exists-p (setq file (gnus-cache-file-name | |
172 | group number))))) | |
173 | ;; Possibly create the cache directory. | |
6748645f | 174 | (gnus-make-directory (file-name-directory file)) |
eec82323 LMI |
175 | ;; Save the article in the cache. |
176 | (if (file-exists-p file) | |
177 | t ; The article already is saved. | |
178 | (save-excursion | |
179 | (set-buffer nntp-server-buffer) | |
180 | (let ((gnus-use-cache nil)) | |
181 | (gnus-request-article-this-buffer number group)) | |
182 | (when (> (buffer-size) 0) | |
183 | (gnus-write-buffer file) | |
184 | (gnus-cache-change-buffer group) | |
185 | (set-buffer (cdr gnus-cache-buffer)) | |
186 | (goto-char (point-max)) | |
187 | (forward-line -1) | |
188 | (while (condition-case () | |
189 | (when (not (bobp)) | |
190 | (> (read (current-buffer)) number)) | |
191 | (error | |
192 | ;; The line was malformed, so we just remove it!! | |
193 | (gnus-delete-line) | |
194 | t)) | |
195 | (forward-line -1)) | |
196 | (if (bobp) | |
197 | (if (not (eobp)) | |
198 | (progn | |
199 | (beginning-of-line) | |
200 | (when (< (read (current-buffer)) number) | |
201 | (forward-line 1))) | |
202 | (beginning-of-line)) | |
203 | (forward-line 1)) | |
204 | (beginning-of-line) | |
205 | ;; [number subject from date id references chars lines xref] | |
206 | (insert (format "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t\n" | |
207 | (mail-header-number headers) | |
208 | (mail-header-subject headers) | |
209 | (mail-header-from headers) | |
210 | (mail-header-date headers) | |
211 | (mail-header-id headers) | |
212 | (or (mail-header-references headers) "") | |
213 | (or (mail-header-chars headers) "") | |
214 | (or (mail-header-lines headers) "") | |
215 | (or (mail-header-xref headers) ""))) | |
216 | ;; Update the active info. | |
217 | (set-buffer gnus-summary-buffer) | |
218 | (gnus-cache-update-active group number) | |
219 | (push article gnus-newsgroup-cached) | |
220 | (gnus-summary-update-secondary-mark article)) | |
221 | t)))))) | |
222 | ||
223 | (defun gnus-cache-enter-remove-article (article) | |
224 | "Mark ARTICLE for later possible removal." | |
225 | (when article | |
226 | (push article gnus-cache-removable-articles))) | |
227 | ||
228 | (defun gnus-cache-possibly-remove-articles () | |
229 | "Possibly remove some of the removable articles." | |
230 | (if (not (gnus-virtual-group-p gnus-newsgroup-name)) | |
231 | (gnus-cache-possibly-remove-articles-1) | |
232 | (let ((arts gnus-cache-removable-articles) | |
233 | ga) | |
234 | (while arts | |
235 | (when (setq ga (nnvirtual-find-group-art | |
236 | (gnus-group-real-name gnus-newsgroup-name) (pop arts))) | |
237 | (let ((gnus-cache-removable-articles (list (cdr ga))) | |
238 | (gnus-newsgroup-name (car ga))) | |
239 | (gnus-cache-possibly-remove-articles-1))))) | |
240 | (setq gnus-cache-removable-articles nil))) | |
241 | ||
242 | (defun gnus-cache-possibly-remove-articles-1 () | |
243 | "Possibly remove some of the removable articles." | |
244 | (unless (eq gnus-use-cache 'passive) | |
245 | (let ((articles gnus-cache-removable-articles) | |
246 | (cache-articles gnus-newsgroup-cached) | |
247 | article) | |
248 | (gnus-cache-change-buffer gnus-newsgroup-name) | |
249 | (while articles | |
250 | (when (memq (setq article (pop articles)) cache-articles) | |
251 | ;; The article was in the cache, so we see whether we are | |
252 | ;; supposed to remove it from the cache. | |
253 | (gnus-cache-possibly-remove-article | |
254 | article (memq article gnus-newsgroup-marked) | |
255 | (memq article gnus-newsgroup-dormant) | |
256 | (or (memq article gnus-newsgroup-unreads) | |
257 | (memq article gnus-newsgroup-unselected)))))) | |
258 | ;; The overview file might have been modified, save it | |
259 | ;; safe because we're only called at group exit anyway. | |
260 | (gnus-cache-save-buffers))) | |
261 | ||
262 | (defun gnus-cache-request-article (article group) | |
263 | "Retrieve ARTICLE in GROUP from the cache." | |
264 | (let ((file (gnus-cache-file-name group article)) | |
265 | (buffer-read-only nil)) | |
266 | (when (file-exists-p file) | |
267 | (erase-buffer) | |
268 | (gnus-kill-all-overlays) | |
269 | (insert-file-contents file) | |
270 | t))) | |
271 | ||
272 | (defun gnus-cache-possibly-alter-active (group active) | |
273 | "Alter the ACTIVE info for GROUP to reflect the articles in the cache." | |
eec82323 LMI |
274 | (when gnus-cache-active-hashtb |
275 | (let ((cache-active (gnus-gethash group gnus-cache-active-hashtb))) | |
a8151ef7 LMI |
276 | (when cache-active |
277 | (when (< (car cache-active) (car active)) | |
278 | (setcar active (car cache-active))) | |
279 | (when (> (cdr cache-active) (cdr active)) | |
280 | (setcdr active (cdr cache-active))))))) | |
eec82323 LMI |
281 | |
282 | (defun gnus-cache-retrieve-headers (articles group &optional fetch-old) | |
283 | "Retrieve the headers for ARTICLES in GROUP." | |
284 | (let ((cached | |
285 | (setq gnus-newsgroup-cached (gnus-cache-articles-in-group group)))) | |
286 | (if (not cached) | |
287 | ;; No cached articles here, so we just retrieve them | |
288 | ;; the normal way. | |
289 | (let ((gnus-use-cache nil)) | |
290 | (gnus-retrieve-headers articles group fetch-old)) | |
291 | (let ((uncached-articles (gnus-sorted-intersection | |
292 | (gnus-sorted-complement articles cached) | |
293 | articles)) | |
294 | (cache-file (gnus-cache-file-name group ".overview")) | |
295 | type) | |
296 | ;; We first retrieve all the headers that we don't have in | |
297 | ;; the cache. | |
298 | (let ((gnus-use-cache nil)) | |
299 | (when uncached-articles | |
300 | (setq type (and articles | |
301 | (gnus-retrieve-headers | |
302 | uncached-articles group fetch-old))))) | |
303 | (gnus-cache-save-buffers) | |
304 | ;; Then we insert the cached headers. | |
305 | (save-excursion | |
306 | (cond | |
307 | ((not (file-exists-p cache-file)) | |
308 | ;; There are no cached headers. | |
309 | type) | |
310 | ((null type) | |
311 | ;; There were no uncached headers (or retrieval was | |
312 | ;; unsuccessful), so we use the cached headers exclusively. | |
313 | (set-buffer nntp-server-buffer) | |
314 | (erase-buffer) | |
315 | (insert-file-contents cache-file) | |
316 | 'nov) | |
317 | ((eq type 'nov) | |
318 | ;; We have both cached and uncached NOV headers, so we | |
319 | ;; braid them. | |
320 | (gnus-cache-braid-nov group cached) | |
321 | type) | |
322 | (t | |
323 | ;; We braid HEADs. | |
324 | (gnus-cache-braid-heads group (gnus-sorted-intersection | |
325 | cached articles)) | |
326 | type))))))) | |
327 | ||
328 | (defun gnus-cache-enter-article (&optional n) | |
329 | "Enter the next N articles into the cache. | |
330 | If not given a prefix, use the process marked articles instead. | |
331 | Returns the list of articles entered." | |
332 | (interactive "P") | |
eec82323 LMI |
333 | (let ((articles (gnus-summary-work-articles n)) |
334 | article out) | |
335 | (while (setq article (pop articles)) | |
6748645f | 336 | (gnus-summary-remove-process-mark article) |
eec82323 LMI |
337 | (if (natnump article) |
338 | (when (gnus-cache-possibly-enter-article | |
339 | gnus-newsgroup-name article | |
340 | (gnus-summary-article-header article) | |
341 | nil nil nil t) | |
342 | (push article out)) | |
343 | (gnus-message 2 "Can't cache article %d" article)) | |
eec82323 LMI |
344 | (gnus-summary-update-secondary-mark article)) |
345 | (gnus-summary-next-subject 1) | |
346 | (gnus-summary-position-point) | |
347 | (nreverse out))) | |
348 | ||
349 | (defun gnus-cache-remove-article (n) | |
350 | "Remove the next N articles from the cache. | |
351 | If not given a prefix, use the process marked articles instead. | |
352 | Returns the list of articles removed." | |
353 | (interactive "P") | |
eec82323 LMI |
354 | (gnus-cache-change-buffer gnus-newsgroup-name) |
355 | (let ((articles (gnus-summary-work-articles n)) | |
356 | article out) | |
357 | (while articles | |
358 | (setq article (pop articles)) | |
6748645f | 359 | (gnus-summary-remove-process-mark article) |
eec82323 LMI |
360 | (when (gnus-cache-possibly-remove-article article nil nil nil t) |
361 | (push article out)) | |
eec82323 LMI |
362 | (gnus-summary-update-secondary-mark article)) |
363 | (gnus-summary-next-subject 1) | |
364 | (gnus-summary-position-point) | |
365 | (nreverse out))) | |
366 | ||
367 | (defun gnus-cached-article-p (article) | |
368 | "Say whether ARTICLE is cached in the current group." | |
369 | (memq article gnus-newsgroup-cached)) | |
370 | ||
371 | (defun gnus-summary-insert-cached-articles () | |
372 | "Insert all the articles cached for this group into the current buffer." | |
373 | (interactive) | |
6748645f | 374 | (let ((cached (sort (copy-sequence gnus-newsgroup-cached) '<)) |
eec82323 LMI |
375 | (gnus-verbose (max 6 gnus-verbose))) |
376 | (unless cached | |
6748645f | 377 | (gnus-message 3 "No cached articles for this group")) |
eec82323 LMI |
378 | (while cached |
379 | (gnus-summary-goto-subject (pop cached) t)))) | |
380 | ||
6748645f LMI |
381 | (defalias 'gnus-summary-limit-include-cached |
382 | 'gnus-summary-insert-cached-articles) | |
383 | ||
eec82323 LMI |
384 | ;;; Internal functions. |
385 | ||
386 | (defun gnus-cache-change-buffer (group) | |
387 | (and gnus-cache-buffer | |
388 | ;; See if the current group's overview cache has been loaded. | |
389 | (or (string= group (car gnus-cache-buffer)) | |
390 | ;; Another overview cache is current, save it. | |
391 | (gnus-cache-save-buffers))) | |
392 | ;; if gnus-cache buffer is nil, create it | |
393 | (unless gnus-cache-buffer | |
394 | ;; Create cache buffer | |
395 | (save-excursion | |
396 | (setq gnus-cache-buffer | |
397 | (cons group | |
6748645f LMI |
398 | (set-buffer (gnus-get-buffer-create |
399 | " *gnus-cache-overview*")))) | |
eec82323 LMI |
400 | (buffer-disable-undo (current-buffer)) |
401 | ;; Insert the contents of this group's cache overview. | |
402 | (erase-buffer) | |
403 | (let ((file (gnus-cache-file-name group ".overview"))) | |
404 | (when (file-exists-p file) | |
405 | (nnheader-insert-file-contents file))) | |
406 | ;; We have a fresh (empty/just loaded) buffer, | |
407 | ;; mark it as unmodified to save a redundant write later. | |
408 | (set-buffer-modified-p nil)))) | |
409 | ||
410 | ;; Return whether an article is a member of a class. | |
411 | (defun gnus-cache-member-of-class (class ticked dormant unread) | |
412 | (or (and ticked (memq 'ticked class)) | |
413 | (and dormant (memq 'dormant class)) | |
414 | (and unread (memq 'unread class)) | |
415 | (and (not unread) (not ticked) (not dormant) (memq 'read class)))) | |
416 | ||
417 | (defun gnus-cache-file-name (group article) | |
418 | (concat (file-name-as-directory gnus-cache-directory) | |
419 | (file-name-as-directory | |
420 | (nnheader-translate-file-chars | |
421 | (if (gnus-use-long-file-name 'not-cache) | |
422 | group | |
423 | (let ((group (nnheader-replace-chars-in-string group ?/ ?_))) | |
424 | ;; Translate the first colon into a slash. | |
425 | (when (string-match ":" group) | |
426 | (aset group (match-beginning 0) ?/)) | |
6748645f LMI |
427 | (nnheader-replace-chars-in-string group ?. ?/))) |
428 | t)) | |
eec82323 LMI |
429 | (if (stringp article) article (int-to-string article)))) |
430 | ||
431 | (defun gnus-cache-update-article (group article) | |
432 | "If ARTICLE is in the cache, remove it and re-enter it." | |
6748645f LMI |
433 | (gnus-cache-change-buffer group) |
434 | (when (gnus-cache-possibly-remove-article article nil nil nil t) | |
eec82323 LMI |
435 | (let ((gnus-use-cache nil)) |
436 | (gnus-cache-possibly-enter-article | |
437 | gnus-newsgroup-name article (gnus-summary-article-header article) | |
438 | nil nil nil t)))) | |
439 | ||
440 | (defun gnus-cache-possibly-remove-article (article ticked dormant unread | |
441 | &optional force) | |
442 | "Possibly remove ARTICLE from the cache." | |
443 | (let ((group gnus-newsgroup-name) | |
444 | (number article) | |
445 | file) | |
446 | ;; If this is a virtual group, we find the real group. | |
447 | (when (gnus-virtual-group-p group) | |
448 | (let ((result (nnvirtual-find-group-art | |
449 | (gnus-group-real-name group) article))) | |
450 | (setq group (car result) | |
451 | number (cdr result)))) | |
452 | (setq file (gnus-cache-file-name group number)) | |
453 | (when (and (file-exists-p file) | |
454 | (or force | |
455 | (gnus-cache-member-of-class | |
456 | gnus-cache-remove-articles ticked dormant unread))) | |
457 | (save-excursion | |
458 | (delete-file file) | |
459 | (set-buffer (cdr gnus-cache-buffer)) | |
460 | (goto-char (point-min)) | |
461 | (when (or (looking-at (concat (int-to-string number) "\t")) | |
462 | (search-forward (concat "\n" (int-to-string number) "\t") | |
463 | (point-max) t)) | |
464 | (delete-region (progn (beginning-of-line) (point)) | |
465 | (progn (forward-line 1) (point))))) | |
466 | (setq gnus-newsgroup-cached | |
467 | (delq article gnus-newsgroup-cached)) | |
468 | (gnus-summary-update-secondary-mark article) | |
469 | t))) | |
470 | ||
471 | (defun gnus-cache-articles-in-group (group) | |
472 | "Return a sorted list of cached articles in GROUP." | |
a8151ef7 LMI |
473 | (let ((dir (file-name-directory (gnus-cache-file-name group 1))) |
474 | articles) | |
eec82323 | 475 | (when (file-exists-p dir) |
a8151ef7 LMI |
476 | (setq articles |
477 | (sort (mapcar (lambda (name) (string-to-int name)) | |
478 | (directory-files dir nil "^[0-9]+$" t)) | |
479 | '<)) | |
480 | ;; Update the cache active file, just to synch more. | |
481 | (when articles | |
482 | (gnus-cache-update-active group (car articles) t) | |
483 | (gnus-cache-update-active group (car (last articles)))) | |
484 | articles))) | |
485 | ||
486 | (defun gnus-cache-braid-nov (group cached &optional file) | |
6748645f | 487 | (let ((cache-buf (gnus-get-buffer-create " *gnus-cache*")) |
eec82323 LMI |
488 | beg end) |
489 | (gnus-cache-save-buffers) | |
490 | (save-excursion | |
491 | (set-buffer cache-buf) | |
492 | (buffer-disable-undo (current-buffer)) | |
493 | (erase-buffer) | |
a8151ef7 | 494 | (insert-file-contents (or file (gnus-cache-file-name group ".overview"))) |
eec82323 LMI |
495 | (goto-char (point-min)) |
496 | (insert "\n") | |
497 | (goto-char (point-min))) | |
498 | (set-buffer nntp-server-buffer) | |
499 | (goto-char (point-min)) | |
500 | (while cached | |
501 | (while (and (not (eobp)) | |
502 | (< (read (current-buffer)) (car cached))) | |
503 | (forward-line 1)) | |
504 | (beginning-of-line) | |
505 | (save-excursion | |
506 | (set-buffer cache-buf) | |
507 | (if (search-forward (concat "\n" (int-to-string (car cached)) "\t") | |
508 | nil t) | |
509 | (setq beg (progn (beginning-of-line) (point)) | |
510 | end (progn (end-of-line) (point))) | |
511 | (setq beg nil))) | |
512 | (when beg | |
513 | (insert-buffer-substring cache-buf beg end) | |
514 | (insert "\n")) | |
515 | (setq cached (cdr cached))) | |
516 | (kill-buffer cache-buf))) | |
517 | ||
518 | (defun gnus-cache-braid-heads (group cached) | |
6748645f | 519 | (let ((cache-buf (gnus-get-buffer-create " *gnus-cache*"))) |
eec82323 LMI |
520 | (save-excursion |
521 | (set-buffer cache-buf) | |
522 | (buffer-disable-undo (current-buffer)) | |
523 | (erase-buffer)) | |
524 | (set-buffer nntp-server-buffer) | |
525 | (goto-char (point-min)) | |
526 | (while cached | |
527 | (while (and (not (eobp)) | |
528 | (looking-at "2.. +\\([0-9]+\\) ") | |
529 | (< (progn (goto-char (match-beginning 1)) | |
530 | (read (current-buffer))) | |
531 | (car cached))) | |
532 | (search-forward "\n.\n" nil 'move)) | |
533 | (beginning-of-line) | |
534 | (save-excursion | |
535 | (set-buffer cache-buf) | |
536 | (erase-buffer) | |
537 | (insert-file-contents (gnus-cache-file-name group (car cached))) | |
538 | (goto-char (point-min)) | |
539 | (insert "220 ") | |
540 | (princ (car cached) (current-buffer)) | |
541 | (insert " Article retrieved.\n") | |
542 | (search-forward "\n\n" nil 'move) | |
543 | (delete-region (point) (point-max)) | |
544 | (forward-char -1) | |
545 | (insert ".")) | |
546 | (insert-buffer-substring cache-buf) | |
547 | (setq cached (cdr cached))) | |
548 | (kill-buffer cache-buf))) | |
549 | ||
550 | ;;;###autoload | |
551 | (defun gnus-jog-cache () | |
552 | "Go through all groups and put the articles into the cache. | |
553 | ||
554 | Usage: | |
555 | $ emacs -batch -l ~/.emacs -l gnus -f gnus-jog-cache" | |
556 | (interactive) | |
557 | (let ((gnus-mark-article-hook nil) | |
558 | (gnus-expert-user t) | |
559 | (nnmail-spool-file nil) | |
560 | (gnus-use-dribble-file nil) | |
561 | (gnus-novice-user nil) | |
562 | (gnus-large-newsgroup nil)) | |
563 | ;; Start Gnus. | |
564 | (gnus) | |
565 | ;; Go through all groups... | |
566 | (gnus-group-mark-buffer) | |
a8151ef7 LMI |
567 | (gnus-group-iterate nil |
568 | (lambda (group) | |
569 | (let (gnus-auto-select-next) | |
570 | (gnus-summary-read-group group nil t) | |
571 | ;; ... and enter the articles into the cache. | |
572 | (when (eq major-mode 'gnus-summary-mode) | |
573 | (gnus-uu-mark-buffer) | |
574 | (gnus-cache-enter-article) | |
575 | (kill-buffer (current-buffer)))))))) | |
eec82323 LMI |
576 | |
577 | (defun gnus-cache-read-active (&optional force) | |
578 | "Read the cache active file." | |
579 | (gnus-make-directory gnus-cache-directory) | |
a8151ef7 | 580 | (if (or (not (file-exists-p gnus-cache-active-file)) |
6748645f | 581 | (zerop (nth 7 (file-attributes gnus-cache-active-file))) |
a8151ef7 | 582 | force) |
eec82323 LMI |
583 | ;; There is no active file, so we generate one. |
584 | (gnus-cache-generate-active) | |
585 | ;; We simply read the active file. | |
586 | (save-excursion | |
587 | (gnus-set-work-buffer) | |
588 | (insert-file-contents gnus-cache-active-file) | |
589 | (gnus-active-to-gnus-format | |
590 | nil (setq gnus-cache-active-hashtb | |
591 | (gnus-make-hashtable | |
592 | (count-lines (point-min) (point-max))))) | |
593 | (setq gnus-cache-active-altered nil)))) | |
594 | ||
595 | (defun gnus-cache-write-active (&optional force) | |
596 | "Write the active hashtb to the active file." | |
597 | (when (or force | |
598 | (and gnus-cache-active-hashtb | |
599 | gnus-cache-active-altered)) | |
600 | (nnheader-temp-write gnus-cache-active-file | |
601 | (mapatoms | |
602 | (lambda (sym) | |
603 | (when (and sym (boundp sym)) | |
604 | (insert (format "%s %d %d y\n" | |
605 | (symbol-name sym) (cdr (symbol-value sym)) | |
606 | (car (symbol-value sym)))))) | |
607 | gnus-cache-active-hashtb)) | |
608 | ;; Mark the active hashtb as unaltered. | |
609 | (setq gnus-cache-active-altered nil))) | |
610 | ||
611 | (defun gnus-cache-update-active (group number &optional low) | |
612 | "Update the upper bound of the active info of GROUP to NUMBER. | |
613 | If LOW, update the lower bound instead." | |
614 | (let ((active (gnus-gethash group gnus-cache-active-hashtb))) | |
615 | (if (null active) | |
616 | ;; We just create a new active entry for this group. | |
617 | (gnus-sethash group (cons number number) gnus-cache-active-hashtb) | |
618 | ;; Update the lower or upper bound. | |
619 | (if low | |
620 | (setcar active number) | |
621 | (setcdr active number))) | |
622 | ;; Mark the active hashtb as altered. | |
623 | (setq gnus-cache-active-altered t))) | |
624 | ||
625 | ;;;###autoload | |
626 | (defun gnus-cache-generate-active (&optional directory) | |
627 | "Generate the cache active file." | |
628 | (interactive) | |
629 | (let* ((top (null directory)) | |
630 | (directory (expand-file-name (or directory gnus-cache-directory))) | |
631 | (files (directory-files directory 'full)) | |
632 | (group | |
633 | (if top | |
634 | "" | |
635 | (string-match | |
6748645f LMI |
636 | (concat "^" (regexp-quote |
637 | (file-name-as-directory | |
638 | (expand-file-name gnus-cache-directory)))) | |
eec82323 LMI |
639 | (directory-file-name directory)) |
640 | (nnheader-replace-chars-in-string | |
641 | (substring (directory-file-name directory) (match-end 0)) | |
642 | ?/ ?.))) | |
643 | nums alphs) | |
644 | (when top | |
645 | (gnus-message 5 "Generating the cache active file...") | |
646 | (setq gnus-cache-active-hashtb (gnus-make-hashtable 123))) | |
6748645f LMI |
647 | (when (string-match "^\\(nn[^_]+\\)_" group) |
648 | (setq group (replace-match "\\1:" t t group))) | |
eec82323 LMI |
649 | ;; Separate articles from all other files and directories. |
650 | (while files | |
651 | (if (string-match "^[0-9]+$" (file-name-nondirectory (car files))) | |
652 | (push (string-to-int (file-name-nondirectory (pop files))) nums) | |
653 | (push (pop files) alphs))) | |
654 | ;; If we have nums, then this is probably a valid group. | |
655 | (when (setq nums (sort nums '<)) | |
656 | (gnus-sethash group (cons (car nums) (gnus-last-element nums)) | |
657 | gnus-cache-active-hashtb)) | |
658 | ;; Go through all the other files. | |
659 | (while alphs | |
660 | (when (and (file-directory-p (car alphs)) | |
6748645f | 661 | (not (string-match "^\\." |
eec82323 LMI |
662 | (file-name-nondirectory (car alphs))))) |
663 | ;; We descend directories. | |
664 | (gnus-cache-generate-active (car alphs))) | |
665 | (setq alphs (cdr alphs))) | |
666 | ;; Write the new active file. | |
667 | (when top | |
668 | (gnus-cache-write-active t) | |
669 | (gnus-message 5 "Generating the cache active file...done")))) | |
670 | ||
671 | ;;;###autoload | |
672 | (defun gnus-cache-generate-nov-databases (dir) | |
673 | "Generate NOV files recursively starting in DIR." | |
674 | (interactive (list gnus-cache-directory)) | |
675 | (gnus-cache-close) | |
676 | (let ((nnml-generate-active-function 'identity)) | |
677 | (nnml-generate-nov-databases-1 dir))) | |
678 | ||
679 | (defun gnus-cache-move-cache (dir) | |
680 | "Move the cache tree to somewhere else." | |
a8151ef7 | 681 | (interactive "FMove the cache tree to: ") |
eec82323 LMI |
682 | (rename-file gnus-cache-directory dir)) |
683 | ||
684 | (provide 'gnus-cache) | |
685 | ||
686 | ;;; gnus-cache.el ends here |