1 ;;; gnus-cache.el --- cache interface for Gnus
2 ;; Copyright (C) 1995,96,97,98 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
7 ;; This file is part of GNU Emacs.
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)
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.
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.
28 (eval-when-compile (require 'cl))
37 (defcustom gnus-cache-active-file
38 (concat (file-name-as-directory gnus-cache-directory) "active")
39 "*The cache active file."
43 (defcustom gnus-cache-enter-articles '(ticked dormant)
44 "Classes of articles to enter into the cache."
46 :type '(set (const ticked) (const dormant) (const unread) (const read)))
48 (defcustom gnus-cache-remove-articles '(read)
49 "Classes of articles to remove from the cache."
51 :type '(set (const ticked) (const dormant) (const unread) (const read)))
53 (defcustom gnus-uncacheable-groups nil
54 "*Groups that match this regexp will not be cached.
56 If you want to avoid caching your nnml groups, you could set this
57 variable to \"^nnml\"."
59 :type '(choice (const :tag "off" nil)
64 ;;; Internal variables.
66 (defvar gnus-cache-removable-articles nil)
67 (defvar gnus-cache-buffer nil)
68 (defvar gnus-cache-active-hashtb nil)
69 (defvar gnus-cache-active-altered nil)
72 (autoload 'nnml-generate-nov-databases-1 "nnml")
73 (autoload 'nnvirtual-find-group-art "nnvirtual"))
77 ;;; Functions called from Gnus.
79 (defun gnus-cache-open ()
80 "Initialize the cache."
81 (when (or (file-exists-p gnus-cache-directory)
83 (not (eq gnus-use-cache 'passive))))
84 (gnus-cache-read-active)))
86 ;; Complexities of byte-compiling make this kludge necessary. Eeek.
88 (gnus-add-shutdown 'gnus-cache-close 'gnus))
90 (defun gnus-cache-close ()
91 "Shut down the cache."
92 (gnus-cache-write-active)
93 (gnus-cache-save-buffers)
94 (setq gnus-cache-active-hashtb nil))
96 (defun gnus-cache-save-buffers ()
97 ;; save the overview buffer if it exists and has been modified
98 ;; delete empty cache subdirectories
99 (when gnus-cache-buffer
100 (let ((buffer (cdr gnus-cache-buffer))
101 (overview-file (gnus-cache-file-name
102 (car gnus-cache-buffer) ".overview")))
103 ;; write the overview only if it was modified
104 (when (buffer-modified-p buffer)
107 (if (> (buffer-size) 0)
108 ;; Non-empty overview, write it to a file.
109 (gnus-write-buffer overview-file)
110 ;; Empty overview file, remove it
111 (when (file-exists-p overview-file)
112 (delete-file overview-file))
113 ;; If possible, remove group's cache subdirectory.
115 ;; FIXME: we can detect the error type and warn the user
116 ;; of any inconsistencies (articles w/o nov entries?).
117 ;; for now, just be conservative...delete only if safe -- sj
118 (delete-directory (file-name-directory overview-file))
120 ;; Kill the buffer -- it's either unmodified or saved.
121 (gnus-kill-buffer buffer)
122 (setq gnus-cache-buffer nil))))
124 (defun gnus-cache-possibly-enter-article
125 (group article headers ticked dormant unread &optional force)
126 (when (and (or force (not (eq gnus-use-cache 'passive)))
129 (vectorp headers)) ; This might be a dummy article.
130 ;; If this is a virtual group, we find the real group.
131 (when (gnus-virtual-group-p group)
132 (let ((result (nnvirtual-find-group-art
133 (gnus-group-real-name group) article)))
134 (setq group (car result)
135 headers (copy-sequence headers))
136 (mail-header-set-number headers (cdr result))))
137 (let ((number (mail-header-number headers))
140 (> number 0) ; Reffed article.
142 (and (or (not gnus-uncacheable-groups)
144 gnus-uncacheable-groups group)))
145 (gnus-cache-member-of-class
146 gnus-cache-enter-articles ticked dormant unread)))
147 (not (file-exists-p (setq file (gnus-cache-file-name
149 ;; Possibly create the cache directory.
150 (gnus-make-directory (setq dir (file-name-directory file)))
151 ;; Save the article in the cache.
152 (if (file-exists-p file)
153 t ; The article already is saved.
155 (set-buffer nntp-server-buffer)
156 (let ((gnus-use-cache nil))
157 (gnus-request-article-this-buffer number group))
158 (when (> (buffer-size) 0)
159 (gnus-write-buffer file)
160 (gnus-cache-change-buffer group)
161 (set-buffer (cdr gnus-cache-buffer))
162 (goto-char (point-max))
164 (while (condition-case ()
166 (> (read (current-buffer)) number))
168 ;; The line was malformed, so we just remove it!!
176 (when (< (read (current-buffer)) number)
181 ;; [number subject from date id references chars lines xref]
182 (insert (format "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t\n"
183 (mail-header-number headers)
184 (mail-header-subject headers)
185 (mail-header-from headers)
186 (mail-header-date headers)
187 (mail-header-id headers)
188 (or (mail-header-references headers) "")
189 (or (mail-header-chars headers) "")
190 (or (mail-header-lines headers) "")
191 (or (mail-header-xref headers) "")))
192 ;; Update the active info.
193 (set-buffer gnus-summary-buffer)
194 (gnus-cache-update-active group number)
195 (push article gnus-newsgroup-cached)
196 (gnus-summary-update-secondary-mark article))
199 (defun gnus-cache-enter-remove-article (article)
200 "Mark ARTICLE for later possible removal."
202 (push article gnus-cache-removable-articles)))
204 (defun gnus-cache-possibly-remove-articles ()
205 "Possibly remove some of the removable articles."
206 (if (not (gnus-virtual-group-p gnus-newsgroup-name))
207 (gnus-cache-possibly-remove-articles-1)
208 (let ((arts gnus-cache-removable-articles)
211 (when (setq ga (nnvirtual-find-group-art
212 (gnus-group-real-name gnus-newsgroup-name) (pop arts)))
213 (let ((gnus-cache-removable-articles (list (cdr ga)))
214 (gnus-newsgroup-name (car ga)))
215 (gnus-cache-possibly-remove-articles-1)))))
216 (setq gnus-cache-removable-articles nil)))
218 (defun gnus-cache-possibly-remove-articles-1 ()
219 "Possibly remove some of the removable articles."
220 (unless (eq gnus-use-cache 'passive)
221 (let ((articles gnus-cache-removable-articles)
222 (cache-articles gnus-newsgroup-cached)
224 (gnus-cache-change-buffer gnus-newsgroup-name)
226 (when (memq (setq article (pop articles)) cache-articles)
227 ;; The article was in the cache, so we see whether we are
228 ;; supposed to remove it from the cache.
229 (gnus-cache-possibly-remove-article
230 article (memq article gnus-newsgroup-marked)
231 (memq article gnus-newsgroup-dormant)
232 (or (memq article gnus-newsgroup-unreads)
233 (memq article gnus-newsgroup-unselected))))))
234 ;; The overview file might have been modified, save it
235 ;; safe because we're only called at group exit anyway.
236 (gnus-cache-save-buffers)))
238 (defun gnus-cache-request-article (article group)
239 "Retrieve ARTICLE in GROUP from the cache."
240 (let ((file (gnus-cache-file-name group article))
241 (buffer-read-only nil))
242 (when (file-exists-p file)
244 (gnus-kill-all-overlays)
245 (nnheader-insert-file-contents file)
248 (defun gnus-cache-possibly-alter-active (group active)
249 "Alter the ACTIVE info for GROUP to reflect the articles in the cache."
250 (when gnus-cache-active-hashtb
251 (let ((cache-active (gnus-gethash group gnus-cache-active-hashtb)))
253 (when (< (car cache-active) (car active))
254 (setcar active (car cache-active)))
255 (when (> (cdr cache-active) (cdr active))
256 (setcdr active (cdr cache-active)))))))
258 (defun gnus-cache-retrieve-headers (articles group &optional fetch-old)
259 "Retrieve the headers for ARTICLES in GROUP."
261 (setq gnus-newsgroup-cached (gnus-cache-articles-in-group group))))
263 ;; No cached articles here, so we just retrieve them
265 (let ((gnus-use-cache nil))
266 (gnus-retrieve-headers articles group fetch-old))
267 (let ((uncached-articles (gnus-sorted-intersection
268 (gnus-sorted-complement articles cached)
270 (cache-file (gnus-cache-file-name group ".overview"))
272 ;; We first retrieve all the headers that we don't have in
274 (let ((gnus-use-cache nil))
275 (when uncached-articles
276 (setq type (and articles
277 (gnus-retrieve-headers
278 uncached-articles group fetch-old)))))
279 (gnus-cache-save-buffers)
280 ;; Then we insert the cached headers.
283 ((not (file-exists-p cache-file))
284 ;; There are no cached headers.
287 ;; There were no uncached headers (or retrieval was
288 ;; unsuccessful), so we use the cached headers exclusively.
289 (set-buffer nntp-server-buffer)
291 (nnheader-insert-file-contents cache-file)
294 ;; We have both cached and uncached NOV headers, so we
296 (gnus-cache-braid-nov group cached)
300 (gnus-cache-braid-heads group (gnus-sorted-intersection
304 (defun gnus-cache-enter-article (&optional n)
305 "Enter the next N articles into the cache.
306 If not given a prefix, use the process marked articles instead.
307 Returns the list of articles entered."
309 (let ((articles (gnus-summary-work-articles n))
311 (while (setq article (pop articles))
312 (gnus-summary-remove-process-mark article)
313 (if (natnump article)
314 (when (gnus-cache-possibly-enter-article
315 gnus-newsgroup-name article
316 (gnus-summary-article-header article)
319 (gnus-message 2 "Can't cache article %d" article))
320 (gnus-summary-update-secondary-mark article))
321 (gnus-summary-next-subject 1)
322 (gnus-summary-position-point)
325 (defun gnus-cache-remove-article (n)
326 "Remove the next N articles from the cache.
327 If not given a prefix, use the process marked articles instead.
328 Returns the list of articles removed."
330 (gnus-cache-change-buffer gnus-newsgroup-name)
331 (let ((articles (gnus-summary-work-articles n))
334 (setq article (pop articles))
335 (gnus-summary-remove-process-mark article)
336 (when (gnus-cache-possibly-remove-article article nil nil nil t)
338 (gnus-summary-update-secondary-mark article))
339 (gnus-summary-next-subject 1)
340 (gnus-summary-position-point)
343 (defun gnus-cached-article-p (article)
344 "Say whether ARTICLE is cached in the current group."
345 (memq article gnus-newsgroup-cached))
347 (defun gnus-summary-insert-cached-articles ()
348 "Insert all the articles cached for this group into the current buffer."
350 (let ((cached gnus-newsgroup-cached)
351 (gnus-verbose (max 6 gnus-verbose)))
353 (gnus-message 3 "No cached articles for this group"))
355 (gnus-summary-goto-subject (pop cached) t))))
357 (defalias 'gnus-summary-limit-include-cached
358 'gnus-summary-insert-cached-articles)
360 ;;; Internal functions.
362 (defun gnus-cache-change-buffer (group)
363 (and gnus-cache-buffer
364 ;; See if the current group's overview cache has been loaded.
365 (or (string= group (car gnus-cache-buffer))
366 ;; Another overview cache is current, save it.
367 (gnus-cache-save-buffers)))
368 ;; if gnus-cache buffer is nil, create it
369 (unless gnus-cache-buffer
370 ;; Create cache buffer
372 (setq gnus-cache-buffer
374 (set-buffer (get-buffer-create " *gnus-cache-overview*"))))
375 (buffer-disable-undo (current-buffer))
376 ;; Insert the contents of this group's cache overview.
378 (let ((file (gnus-cache-file-name group ".overview")))
379 (when (file-exists-p file)
380 (nnheader-insert-file-contents file)))
381 ;; We have a fresh (empty/just loaded) buffer,
382 ;; mark it as unmodified to save a redundant write later.
383 (set-buffer-modified-p nil))))
385 ;; Return whether an article is a member of a class.
386 (defun gnus-cache-member-of-class (class ticked dormant unread)
387 (or (and ticked (memq 'ticked class))
388 (and dormant (memq 'dormant class))
389 (and unread (memq 'unread class))
390 (and (not unread) (not ticked) (not dormant) (memq 'read class))))
392 (defun gnus-cache-file-name (group article)
393 (concat (file-name-as-directory gnus-cache-directory)
394 (file-name-as-directory
395 (nnheader-translate-file-chars
396 (if (gnus-use-long-file-name 'not-cache)
398 (let ((group (nnheader-replace-chars-in-string group ?/ ?_)))
399 ;; Translate the first colon into a slash.
400 (when (string-match ":" group)
401 (aset group (match-beginning 0) ?/))
402 (nnheader-replace-chars-in-string group ?. ?/)))
404 (if (stringp article) article (int-to-string article))))
406 (defun gnus-cache-update-article (group article)
407 "If ARTICLE is in the cache, remove it and re-enter it."
408 (when (gnus-cache-possibly-remove-article article nil nil nil t)
409 (let ((gnus-use-cache nil))
410 (gnus-cache-possibly-enter-article
411 gnus-newsgroup-name article (gnus-summary-article-header article)
414 (defun gnus-cache-possibly-remove-article (article ticked dormant unread
416 "Possibly remove ARTICLE from the cache."
417 (let ((group gnus-newsgroup-name)
420 ;; If this is a virtual group, we find the real group.
421 (when (gnus-virtual-group-p group)
422 (let ((result (nnvirtual-find-group-art
423 (gnus-group-real-name group) article)))
424 (setq group (car result)
425 number (cdr result))))
426 (setq file (gnus-cache-file-name group number))
427 (when (and (file-exists-p file)
429 (gnus-cache-member-of-class
430 gnus-cache-remove-articles ticked dormant unread)))
433 (set-buffer (cdr gnus-cache-buffer))
434 (goto-char (point-min))
435 (when (or (looking-at (concat (int-to-string number) "\t"))
436 (search-forward (concat "\n" (int-to-string number) "\t")
438 (delete-region (progn (beginning-of-line) (point))
439 (progn (forward-line 1) (point)))))
440 (setq gnus-newsgroup-cached
441 (delq article gnus-newsgroup-cached))
442 (gnus-summary-update-secondary-mark article)
445 (defun gnus-cache-articles-in-group (group)
446 "Return a sorted list of cached articles in GROUP."
447 (let ((dir (file-name-directory (gnus-cache-file-name group 1)))
449 (when (file-exists-p dir)
451 (sort (mapcar (lambda (name) (string-to-int name))
452 (directory-files dir nil "^[0-9]+$" t))
454 ;; Update the cache active file, just to synch more.
456 (gnus-cache-update-active group (car articles) t)
457 (gnus-cache-update-active group (car (last articles))))
460 (defun gnus-cache-braid-nov (group cached &optional file)
461 (let ((cache-buf (get-buffer-create " *gnus-cache*"))
463 (gnus-cache-save-buffers)
465 (set-buffer cache-buf)
466 (buffer-disable-undo (current-buffer))
468 (nnheader-insert-file-contents (or file (gnus-cache-file-name group ".overview")))
469 (goto-char (point-min))
471 (goto-char (point-min)))
472 (set-buffer nntp-server-buffer)
473 (goto-char (point-min))
475 (while (and (not (eobp))
476 (< (read (current-buffer)) (car cached)))
480 (set-buffer cache-buf)
481 (if (search-forward (concat "\n" (int-to-string (car cached)) "\t")
483 (setq beg (progn (beginning-of-line) (point))
484 end (progn (end-of-line) (point)))
487 (insert-buffer-substring cache-buf beg end)
489 (setq cached (cdr cached)))
490 (kill-buffer cache-buf)))
492 (defun gnus-cache-braid-heads (group cached)
493 (let ((cache-buf (get-buffer-create " *gnus-cache*")))
495 (set-buffer cache-buf)
496 (buffer-disable-undo (current-buffer))
498 (set-buffer nntp-server-buffer)
499 (goto-char (point-min))
501 (while (and (not (eobp))
502 (looking-at "2.. +\\([0-9]+\\) ")
503 (< (progn (goto-char (match-beginning 1))
504 (read (current-buffer)))
506 (search-forward "\n.\n" nil 'move))
509 (set-buffer cache-buf)
511 (nnheader-insert-file-contents (gnus-cache-file-name group (car cached)))
512 (goto-char (point-min))
514 (princ (car cached) (current-buffer))
515 (insert " Article retrieved.\n")
516 (search-forward "\n\n" nil 'move)
517 (delete-region (point) (point-max))
520 (insert-buffer-substring cache-buf)
521 (setq cached (cdr cached)))
522 (kill-buffer cache-buf)))
525 (defun gnus-jog-cache ()
526 "Go through all groups and put the articles into the cache.
529 $ emacs -batch -l ~/.emacs -l gnus -f gnus-jog-cache"
531 (let ((gnus-mark-article-hook nil)
533 (nnmail-spool-file nil)
534 (gnus-use-dribble-file nil)
535 (gnus-novice-user nil)
536 (gnus-large-newsgroup nil))
539 ;; Go through all groups...
540 (gnus-group-mark-buffer)
541 (gnus-group-iterate nil
543 (let (gnus-auto-select-next)
544 (gnus-summary-read-group group nil t)
545 ;; ... and enter the articles into the cache.
546 (when (eq major-mode 'gnus-summary-mode)
547 (gnus-uu-mark-buffer)
548 (gnus-cache-enter-article)
549 (kill-buffer (current-buffer))))))))
551 (defun gnus-cache-read-active (&optional force)
552 "Read the cache active file."
553 (gnus-make-directory gnus-cache-directory)
554 (if (or (not (file-exists-p gnus-cache-active-file))
555 (not (zerop (nth 7 (file-attributes gnus-cache-active-file))))
557 ;; There is no active file, so we generate one.
558 (gnus-cache-generate-active)
559 ;; We simply read the active file.
561 (gnus-set-work-buffer)
562 (nnheader-insert-file-contents gnus-cache-active-file)
563 (gnus-active-to-gnus-format
564 nil (setq gnus-cache-active-hashtb
566 (count-lines (point-min) (point-max)))))
567 (setq gnus-cache-active-altered nil))))
569 (defun gnus-cache-write-active (&optional force)
570 "Write the active hashtb to the active file."
572 (and gnus-cache-active-hashtb
573 gnus-cache-active-altered))
574 (nnheader-temp-write gnus-cache-active-file
577 (when (and sym (boundp sym))
578 (insert (format "%s %d %d y\n"
579 (symbol-name sym) (cdr (symbol-value sym))
580 (car (symbol-value sym))))))
581 gnus-cache-active-hashtb))
582 ;; Mark the active hashtb as unaltered.
583 (setq gnus-cache-active-altered nil)))
585 (defun gnus-cache-update-active (group number &optional low)
586 "Update the upper bound of the active info of GROUP to NUMBER.
587 If LOW, update the lower bound instead."
588 (let ((active (gnus-gethash group gnus-cache-active-hashtb)))
590 ;; We just create a new active entry for this group.
591 (gnus-sethash group (cons number number) gnus-cache-active-hashtb)
592 ;; Update the lower or upper bound.
594 (setcar active number)
595 (setcdr active number)))
596 ;; Mark the active hashtb as altered.
597 (setq gnus-cache-active-altered t)))
600 (defun gnus-cache-generate-active (&optional directory)
601 "Generate the cache active file."
603 (let* ((top (null directory))
604 (directory (expand-file-name (or directory gnus-cache-directory)))
605 (files (directory-files directory 'full))
610 (concat "^" (file-name-as-directory
611 (expand-file-name gnus-cache-directory)))
612 (directory-file-name directory))
613 (nnheader-replace-chars-in-string
614 (substring (directory-file-name directory) (match-end 0))
618 (gnus-message 5 "Generating the cache active file...")
619 (setq gnus-cache-active-hashtb (gnus-make-hashtable 123)))
620 ;; Separate articles from all other files and directories.
622 (if (string-match "^[0-9]+$" (file-name-nondirectory (car files)))
623 (push (string-to-int (file-name-nondirectory (pop files))) nums)
624 (push (pop files) alphs)))
625 ;; If we have nums, then this is probably a valid group.
626 (when (setq nums (sort nums '<))
627 (gnus-sethash group (cons (car nums) (gnus-last-element nums))
628 gnus-cache-active-hashtb))
629 ;; Go through all the other files.
631 (when (and (file-directory-p (car alphs))
632 (not (string-match "^\\.\\.?$"
633 (file-name-nondirectory (car alphs)))))
634 ;; We descend directories.
635 (gnus-cache-generate-active (car alphs)))
636 (setq alphs (cdr alphs)))
637 ;; Write the new active file.
639 (gnus-cache-write-active t)
640 (gnus-message 5 "Generating the cache active file...done"))))
643 (defun gnus-cache-generate-nov-databases (dir)
644 "Generate NOV files recursively starting in DIR."
645 (interactive (list gnus-cache-directory))
647 (let ((nnml-generate-active-function 'identity))
648 (nnml-generate-nov-databases-1 dir)))
650 (defun gnus-cache-move-cache (dir)
651 "Move the cache tree to somewhere else."
652 (interactive "FMove the cache tree to: ")
653 (rename-file gnus-cache-directory dir))
655 (provide 'gnus-cache)
657 ;;; gnus-cache.el ends here