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@gnus.org>
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-cacheable-groups nil
54 "*Groups that match this regexp will be cached.
56 If you only want to cache your nntp groups, you could set this
57 variable to \"^nntp\".
59 If a group matches both gnus-cacheable-groups and gnus-uncacheable-groups
62 :type '(choice (const :tag "off" nil)
65 (defcustom gnus-uncacheable-groups nil
66 "*Groups that match this regexp will not be cached.
68 If you want to avoid caching your nnml groups, you could set this
69 variable to \"^nnml\".
71 If a group matches both gnus-cacheable-groups and gnus-uncacheable-groups
74 :type '(choice (const :tag "off" nil)
77 (defvar gnus-cache-overview-coding-system 'raw-text
78 "Coding system used on Gnus cache files.")
82 ;;; Internal variables.
84 (defvar gnus-cache-removable-articles nil)
85 (defvar gnus-cache-buffer nil)
86 (defvar gnus-cache-active-hashtb nil)
87 (defvar gnus-cache-active-altered nil)
88 (defvar gnus-cache-write-file-coding-system 'raw-text)
91 (autoload 'nnml-generate-nov-databases-1 "nnml")
92 (autoload 'nnvirtual-find-group-art "nnvirtual"))
96 ;;; Functions called from Gnus.
98 (defun gnus-cache-open ()
99 "Initialize the cache."
100 (when (or (file-exists-p gnus-cache-directory)
102 (not (eq gnus-use-cache 'passive))))
103 (gnus-cache-read-active)))
105 ;; Complexities of byte-compiling make this kludge necessary. Eeek.
107 (gnus-add-shutdown 'gnus-cache-close 'gnus))
109 (defun gnus-cache-close ()
110 "Shut down the cache."
111 (gnus-cache-write-active)
112 (gnus-cache-save-buffers)
113 (setq gnus-cache-active-hashtb nil))
115 (defun gnus-cache-save-buffers ()
116 ;; save the overview buffer if it exists and has been modified
117 ;; delete empty cache subdirectories
118 (when gnus-cache-buffer
119 (let ((buffer (cdr gnus-cache-buffer))
120 (overview-file (gnus-cache-file-name
121 (car gnus-cache-buffer) ".overview")))
122 ;; write the overview only if it was modified
123 (when (buffer-modified-p buffer)
126 (if (> (buffer-size) 0)
127 ;; Non-empty overview, write it to a file.
128 (let ((coding-system-for-write
129 gnus-cache-overview-coding-system))
130 (gnus-write-buffer overview-file))
131 ;; Empty overview file, remove it
132 (when (file-exists-p overview-file)
133 (delete-file overview-file))
134 ;; If possible, remove group's cache subdirectory.
136 ;; FIXME: we can detect the error type and warn the user
137 ;; of any inconsistencies (articles w/o nov entries?).
138 ;; for now, just be conservative...delete only if safe -- sj
139 (delete-directory (file-name-directory overview-file))
141 ;; Kill the buffer -- it's either unmodified or saved.
142 (gnus-kill-buffer buffer)
143 (setq gnus-cache-buffer nil))))
145 (defun gnus-cache-possibly-enter-article
146 (group article headers ticked dormant unread &optional force)
147 (when (and (or force (not (eq gnus-use-cache 'passive)))
150 (vectorp headers)) ; This might be a dummy article.
151 ;; If this is a virtual group, we find the real group.
152 (when (gnus-virtual-group-p group)
153 (let ((result (nnvirtual-find-group-art
154 (gnus-group-real-name group) article)))
155 (setq group (car result)
156 headers (copy-sequence headers))
157 (mail-header-set-number headers (cdr result))))
158 (let ((number (mail-header-number headers))
161 (> number 0) ; Reffed article.
163 (and (or (not gnus-cacheable-groups)
164 (string-match gnus-cacheable-groups group))
165 (or (not gnus-uncacheable-groups)
167 gnus-uncacheable-groups group)))
168 (gnus-cache-member-of-class
169 gnus-cache-enter-articles ticked dormant unread)))
170 (not (file-exists-p (setq file (gnus-cache-file-name
172 ;; Possibly create the cache directory.
173 (gnus-make-directory (file-name-directory file))
174 ;; Save the article in the cache.
175 (if (file-exists-p file)
176 t ; The article already is saved.
178 (set-buffer nntp-server-buffer)
179 (let ((gnus-use-cache nil))
180 (gnus-request-article-this-buffer number group))
181 (when (> (buffer-size) 0)
182 (let ((coding-system-for-write
183 gnus-cache-write-file-coding-system))
184 (gnus-write-buffer file))
185 (gnus-cache-change-buffer group)
186 (set-buffer (cdr gnus-cache-buffer))
187 (goto-char (point-max))
189 (while (condition-case ()
191 (> (read (current-buffer)) number))
193 ;; The line was malformed, so we just remove it!!
201 (when (< (read (current-buffer)) number)
206 ;; [number subject from date id references chars lines xref]
207 (insert (format "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t\n"
208 (mail-header-number headers)
209 (let ((subject (mail-header-subject headers)))
210 (or (get-text-property 0 'raw-text subject)
212 (let ((from (mail-header-from headers)))
213 (or (get-text-property 0 'raw-text from)
215 (mail-header-date headers)
216 (mail-header-id headers)
217 (or (mail-header-references headers) "")
218 (or (mail-header-chars headers) "")
219 (or (mail-header-lines headers) "")
220 (or (mail-header-xref headers) "")))
221 ;; Update the active info.
222 (set-buffer gnus-summary-buffer)
223 (gnus-cache-update-active group number)
224 (push article gnus-newsgroup-cached)
225 (gnus-summary-update-secondary-mark article))
228 (defun gnus-cache-enter-remove-article (article)
229 "Mark ARTICLE for later possible removal."
231 (push article gnus-cache-removable-articles)))
233 (defun gnus-cache-possibly-remove-articles ()
234 "Possibly remove some of the removable articles."
235 (if (not (gnus-virtual-group-p gnus-newsgroup-name))
236 (gnus-cache-possibly-remove-articles-1)
237 (let ((arts gnus-cache-removable-articles)
240 (when (setq ga (nnvirtual-find-group-art
241 (gnus-group-real-name gnus-newsgroup-name) (pop arts)))
242 (let ((gnus-cache-removable-articles (list (cdr ga)))
243 (gnus-newsgroup-name (car ga)))
244 (gnus-cache-possibly-remove-articles-1)))))
245 (setq gnus-cache-removable-articles nil)))
247 (defun gnus-cache-possibly-remove-articles-1 ()
248 "Possibly remove some of the removable articles."
249 (unless (eq gnus-use-cache 'passive)
250 (let ((articles gnus-cache-removable-articles)
251 (cache-articles gnus-newsgroup-cached)
253 (gnus-cache-change-buffer gnus-newsgroup-name)
255 (when (memq (setq article (pop articles)) cache-articles)
256 ;; The article was in the cache, so we see whether we are
257 ;; supposed to remove it from the cache.
258 (gnus-cache-possibly-remove-article
259 article (memq article gnus-newsgroup-marked)
260 (memq article gnus-newsgroup-dormant)
261 (or (memq article gnus-newsgroup-unreads)
262 (memq article gnus-newsgroup-unselected))))))
263 ;; The overview file might have been modified, save it
264 ;; safe because we're only called at group exit anyway.
265 (gnus-cache-save-buffers)))
267 (defun gnus-cache-request-article (article group)
268 "Retrieve ARTICLE in GROUP from the cache."
269 (let ((file (gnus-cache-file-name group article))
270 (buffer-read-only nil))
271 (when (file-exists-p file)
273 (gnus-kill-all-overlays)
274 (nnheader-insert-file-contents file)
277 (defun gnus-cache-possibly-alter-active (group active)
278 "Alter the ACTIVE info for GROUP to reflect the articles in the cache."
279 (when gnus-cache-active-hashtb
280 (let ((cache-active (gnus-gethash group gnus-cache-active-hashtb)))
282 (when (< (car cache-active) (car active))
283 (setcar active (car cache-active)))
284 (when (> (cdr cache-active) (cdr active))
285 (setcdr active (cdr cache-active)))))))
287 (defun gnus-cache-retrieve-headers (articles group &optional fetch-old)
288 "Retrieve the headers for ARTICLES in GROUP."
290 (setq gnus-newsgroup-cached (gnus-cache-articles-in-group group))))
292 ;; No cached articles here, so we just retrieve them
294 (let ((gnus-use-cache nil))
295 (gnus-retrieve-headers articles group fetch-old))
296 (let ((uncached-articles (gnus-sorted-intersection
297 (gnus-sorted-complement articles cached)
299 (cache-file (gnus-cache-file-name group ".overview"))
301 ;; We first retrieve all the headers that we don't have in
303 (let ((gnus-use-cache nil))
304 (when uncached-articles
305 (setq type (and articles
306 (gnus-retrieve-headers
307 uncached-articles group fetch-old)))))
308 (gnus-cache-save-buffers)
309 ;; Then we insert the cached headers.
312 ((not (file-exists-p cache-file))
313 ;; There are no cached headers.
316 ;; There were no uncached headers (or retrieval was
317 ;; unsuccessful), so we use the cached headers exclusively.
318 (set-buffer nntp-server-buffer)
320 (nnheader-insert-file-contents cache-file)
323 ;; We have both cached and uncached NOV headers, so we
325 (gnus-cache-braid-nov group cached)
329 (gnus-cache-braid-heads group (gnus-sorted-intersection
333 (defun gnus-cache-enter-article (&optional n)
334 "Enter the next N articles into the cache.
335 If not given a prefix, use the process marked articles instead.
336 Returns the list of articles entered."
338 (let ((articles (gnus-summary-work-articles n))
340 (while (setq article (pop articles))
341 (gnus-summary-remove-process-mark article)
342 (if (natnump article)
343 (when (gnus-cache-possibly-enter-article
344 gnus-newsgroup-name article
345 (gnus-summary-article-header article)
348 (gnus-message 2 "Can't cache article %d" article))
349 (gnus-summary-update-secondary-mark article))
350 (gnus-summary-next-subject 1)
351 (gnus-summary-position-point)
354 (defun gnus-cache-remove-article (n)
355 "Remove the next N articles from the cache.
356 If not given a prefix, use the process marked articles instead.
357 Returns the list of articles removed."
359 (gnus-cache-change-buffer gnus-newsgroup-name)
360 (let ((articles (gnus-summary-work-articles n))
363 (setq article (pop articles))
364 (gnus-summary-remove-process-mark article)
365 (when (gnus-cache-possibly-remove-article article nil nil nil t)
367 (gnus-summary-update-secondary-mark article))
368 (gnus-summary-next-subject 1)
369 (gnus-summary-position-point)
372 (defun gnus-cached-article-p (article)
373 "Say whether ARTICLE is cached in the current group."
374 (memq article gnus-newsgroup-cached))
376 (defun gnus-summary-insert-cached-articles ()
377 "Insert all the articles cached for this group into the current buffer."
379 (let ((cached (sort (copy-sequence gnus-newsgroup-cached) '<))
380 (gnus-verbose (max 6 gnus-verbose)))
382 (gnus-message 3 "No cached articles for this group"))
384 (gnus-summary-goto-subject (pop cached) t))))
386 (defalias 'gnus-summary-limit-include-cached
387 'gnus-summary-insert-cached-articles)
389 ;;; Internal functions.
391 (defun gnus-cache-change-buffer (group)
392 (and gnus-cache-buffer
393 ;; See if the current group's overview cache has been loaded.
394 (or (string= group (car gnus-cache-buffer))
395 ;; Another overview cache is current, save it.
396 (gnus-cache-save-buffers)))
397 ;; if gnus-cache buffer is nil, create it
398 (unless gnus-cache-buffer
399 ;; Create cache buffer
401 (setq gnus-cache-buffer
403 (set-buffer (gnus-get-buffer-create " *gnus-cache-overview*"))))
404 (buffer-disable-undo (current-buffer))
405 ;; Insert the contents of this group's cache overview.
407 (let ((file (gnus-cache-file-name group ".overview")))
408 (when (file-exists-p file)
409 (nnheader-insert-file-contents file)))
410 ;; We have a fresh (empty/just loaded) buffer,
411 ;; mark it as unmodified to save a redundant write later.
412 (set-buffer-modified-p nil))))
414 ;; Return whether an article is a member of a class.
415 (defun gnus-cache-member-of-class (class ticked dormant unread)
416 (or (and ticked (memq 'ticked class))
417 (and dormant (memq 'dormant class))
418 (and unread (memq 'unread class))
419 (and (not unread) (not ticked) (not dormant) (memq 'read class))))
421 (defun gnus-cache-file-name (group article)
422 (concat (file-name-as-directory gnus-cache-directory)
423 (file-name-as-directory
424 (nnheader-translate-file-chars
425 (if (gnus-use-long-file-name 'not-cache)
427 (let ((group (nnheader-replace-chars-in-string group ?/ ?_)))
428 ;; Translate the first colon into a slash.
429 (when (string-match ":" group)
430 (aset group (match-beginning 0) ?/))
431 (nnheader-replace-chars-in-string group ?. ?/)))
433 (if (stringp article) article (int-to-string article))))
435 (defun gnus-cache-update-article (group article)
436 "If ARTICLE is in the cache, remove it and re-enter it."
437 (gnus-cache-change-buffer group)
438 (when (gnus-cache-possibly-remove-article article nil nil nil t)
439 (let ((gnus-use-cache nil))
440 (gnus-cache-possibly-enter-article
441 gnus-newsgroup-name article (gnus-summary-article-header article)
444 (defun gnus-cache-possibly-remove-article (article ticked dormant unread
446 "Possibly remove ARTICLE from the cache."
447 (let ((group gnus-newsgroup-name)
450 ;; If this is a virtual group, we find the real group.
451 (when (gnus-virtual-group-p group)
452 (let ((result (nnvirtual-find-group-art
453 (gnus-group-real-name group) article)))
454 (setq group (car result)
455 number (cdr result))))
456 (setq file (gnus-cache-file-name group number))
457 (when (and (file-exists-p file)
459 (gnus-cache-member-of-class
460 gnus-cache-remove-articles ticked dormant unread)))
463 (set-buffer (cdr gnus-cache-buffer))
464 (goto-char (point-min))
465 (when (or (looking-at (concat (int-to-string number) "\t"))
466 (search-forward (concat "\n" (int-to-string number) "\t")
468 (delete-region (progn (beginning-of-line) (point))
469 (progn (forward-line 1) (point)))))
470 (setq gnus-newsgroup-cached
471 (delq article gnus-newsgroup-cached))
472 (gnus-summary-update-secondary-mark article)
475 (defun gnus-cache-articles-in-group (group)
476 "Return a sorted list of cached articles in GROUP."
477 (let ((dir (file-name-directory (gnus-cache-file-name group 1)))
479 (when (file-exists-p dir)
481 (sort (mapcar (lambda (name) (string-to-int name))
482 (directory-files dir nil "^[0-9]+$" t))
484 ;; Update the cache active file, just to synch more.
486 (gnus-cache-update-active group (car articles) t)
487 (gnus-cache-update-active group (car (last articles))))
490 (defun gnus-cache-braid-nov (group cached &optional file)
491 (let ((cache-buf (gnus-get-buffer-create " *gnus-cache*"))
493 (gnus-cache-save-buffers)
495 (set-buffer cache-buf)
496 (buffer-disable-undo (current-buffer))
498 (nnheader-insert-file-contents (or file (gnus-cache-file-name group ".overview")))
499 (goto-char (point-min))
501 (goto-char (point-min)))
502 (set-buffer nntp-server-buffer)
503 (goto-char (point-min))
505 (while (and (not (eobp))
506 (< (read (current-buffer)) (car cached)))
510 (set-buffer cache-buf)
511 (if (search-forward (concat "\n" (int-to-string (car cached)) "\t")
513 (setq beg (progn (beginning-of-line) (point))
514 end (progn (end-of-line) (point)))
517 (insert-buffer-substring cache-buf beg end)
519 (setq cached (cdr cached)))
520 (kill-buffer cache-buf)))
522 (defun gnus-cache-braid-heads (group cached)
523 (let ((cache-buf (gnus-get-buffer-create " *gnus-cache*")))
525 (set-buffer cache-buf)
526 (buffer-disable-undo (current-buffer))
528 (set-buffer nntp-server-buffer)
529 (goto-char (point-min))
531 (while (and (not (eobp))
532 (looking-at "2.. +\\([0-9]+\\) ")
533 (< (progn (goto-char (match-beginning 1))
534 (read (current-buffer)))
536 (search-forward "\n.\n" nil 'move))
539 (set-buffer cache-buf)
541 (nnheader-insert-file-contents (gnus-cache-file-name group (car cached)))
542 (goto-char (point-min))
544 (princ (car cached) (current-buffer))
545 (insert " Article retrieved.\n")
546 (search-forward "\n\n" nil 'move)
547 (delete-region (point) (point-max))
550 (insert-buffer-substring cache-buf)
551 (setq cached (cdr cached)))
552 (kill-buffer cache-buf)))
555 (defun gnus-jog-cache ()
556 "Go through all groups and put the articles into the cache.
559 $ emacs -batch -l ~/.emacs -l gnus -f gnus-jog-cache"
561 (let ((gnus-mark-article-hook nil)
563 (nnmail-spool-file nil)
564 (gnus-use-dribble-file nil)
565 (gnus-novice-user nil)
566 (gnus-large-newsgroup nil))
569 ;; Go through all groups...
570 (gnus-group-mark-buffer)
571 (gnus-group-iterate nil
573 (let (gnus-auto-select-next)
574 (gnus-summary-read-group group nil t)
575 ;; ... and enter the articles into the cache.
576 (when (eq major-mode 'gnus-summary-mode)
577 (gnus-uu-mark-buffer)
578 (gnus-cache-enter-article)
579 (kill-buffer (current-buffer))))))))
581 (defun gnus-cache-read-active (&optional force)
582 "Read the cache active file."
583 (gnus-make-directory gnus-cache-directory)
584 (if (or (not (file-exists-p gnus-cache-active-file))
585 (not (zerop (nth 7 (file-attributes gnus-cache-active-file))))
587 ;; There is no active file, so we generate one.
588 (gnus-cache-generate-active)
589 ;; We simply read the active file.
591 (gnus-set-work-buffer)
592 (nnheader-insert-file-contents gnus-cache-active-file)
593 (gnus-active-to-gnus-format
594 nil (setq gnus-cache-active-hashtb
596 (count-lines (point-min) (point-max)))))
597 (setq gnus-cache-active-altered nil))))
599 (defun gnus-cache-write-active (&optional force)
600 "Write the active hashtb to the active file."
602 (and gnus-cache-active-hashtb
603 gnus-cache-active-altered))
604 (nnheader-temp-write gnus-cache-active-file
607 (when (and sym (boundp sym))
608 (insert (format "%s %d %d y\n"
609 (symbol-name sym) (cdr (symbol-value sym))
610 (car (symbol-value sym))))))
611 gnus-cache-active-hashtb))
612 ;; Mark the active hashtb as unaltered.
613 (setq gnus-cache-active-altered nil)))
615 (defun gnus-cache-update-active (group number &optional low)
616 "Update the upper bound of the active info of GROUP to NUMBER.
617 If LOW, update the lower bound instead."
618 (let ((active (gnus-gethash group gnus-cache-active-hashtb)))
620 ;; We just create a new active entry for this group.
621 (gnus-sethash group (cons number number) gnus-cache-active-hashtb)
622 ;; Update the lower or upper bound.
624 (setcar active number)
625 (setcdr active number)))
626 ;; Mark the active hashtb as altered.
627 (setq gnus-cache-active-altered t)))
630 (defun gnus-cache-generate-active (&optional directory)
631 "Generate the cache active file."
633 (let* ((top (null directory))
634 (directory (expand-file-name (or directory gnus-cache-directory)))
635 (files (directory-files directory 'full))
640 (concat "^" (regexp-quote
641 (file-name-as-directory
642 (expand-file-name gnus-cache-directory))))
643 (directory-file-name directory))
644 (nnheader-replace-chars-in-string
645 (substring (directory-file-name directory) (match-end 0))
649 (gnus-message 5 "Generating the cache active file...")
650 (setq gnus-cache-active-hashtb (gnus-make-hashtable 123)))
651 ;; Separate articles from all other files and directories.
653 (if (string-match "^[0-9]+$" (file-name-nondirectory (car files)))
654 (push (string-to-int (file-name-nondirectory (pop files))) nums)
655 (push (pop files) alphs)))
656 ;; If we have nums, then this is probably a valid group.
657 (when (setq nums (sort nums '<))
658 (gnus-sethash group (cons (car nums) (gnus-last-element nums))
659 gnus-cache-active-hashtb))
660 ;; Go through all the other files.
662 (when (and (file-directory-p (car alphs))
663 (not (string-match "^\\.\\.?$"
664 (file-name-nondirectory (car alphs)))))
665 ;; We descend directories.
666 (gnus-cache-generate-active (car alphs)))
667 (setq alphs (cdr alphs)))
668 ;; Write the new active file.
670 (gnus-cache-write-active t)
671 (gnus-message 5 "Generating the cache active file...done"))))
674 (defun gnus-cache-generate-nov-databases (dir)
675 "Generate NOV files recursively starting in DIR."
676 (interactive (list gnus-cache-directory))
678 (let ((nnml-generate-active-function 'identity))
679 (nnml-generate-nov-databases-1 dir)))
681 (defun gnus-cache-move-cache (dir)
682 "Move the cache tree to somewhere else."
683 (interactive "FMove the cache tree to: ")
684 (rename-file gnus-cache-directory dir))
686 (provide 'gnus-cache)
688 ;;; gnus-cache.el ends here