1 ;;; gnus-cache.el --- cache interface for Gnus
2 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001
3 ;; Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
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 the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
29 (eval-when-compile (require 'cl))
38 (defcustom gnus-cache-active-file
39 (expand-file-name "active" gnus-cache-directory)
40 "*The cache active file."
44 (defcustom gnus-cache-enter-articles '(ticked dormant)
45 "Classes of articles to enter into the cache."
47 :type '(set (const ticked) (const dormant) (const unread) (const read)))
49 (defcustom gnus-cache-remove-articles '(read)
50 "Classes of articles to remove from the cache."
52 :type '(set (const ticked) (const dormant) (const unread) (const read)))
54 (defcustom gnus-cacheable-groups nil
55 "*Groups that match this regexp will be cached.
57 If you only want to cache your nntp groups, you could set this
58 variable to \"^nntp\".
60 If a group matches both gnus-cacheable-groups and gnus-uncacheable-groups
63 :type '(choice (const :tag "off" nil)
66 (defcustom gnus-uncacheable-groups nil
67 "*Groups that match this regexp will not be cached.
69 If you want to avoid caching your nnml groups, you could set this
70 variable to \"^nnml\".
72 If a group matches both gnus-cacheable-groups and gnus-uncacheable-groups
75 :type '(choice (const :tag "off" nil)
78 (defvar gnus-cache-overview-coding-system 'raw-text
79 "Coding system used on Gnus cache files.")
81 (defvar gnus-cache-coding-system 'raw-text
82 "Coding system used on Gnus cache files.")
86 ;;; Internal variables.
88 (defvar gnus-cache-removable-articles nil)
89 (defvar gnus-cache-buffer nil)
90 (defvar gnus-cache-active-hashtb nil)
91 (defvar gnus-cache-active-altered nil)
94 (autoload 'nnml-generate-nov-databases-1 "nnml")
95 (autoload 'nnvirtual-find-group-art "nnvirtual"))
99 ;;; Functions called from Gnus.
101 (defun gnus-cache-open ()
102 "Initialize the cache."
103 (when (or (file-exists-p gnus-cache-directory)
105 (not (eq gnus-use-cache 'passive))))
106 (gnus-cache-read-active)))
108 ;; Complexities of byte-compiling make this kludge necessary. Eeek.
110 (gnus-add-shutdown 'gnus-cache-close 'gnus))
112 (defun gnus-cache-close ()
113 "Shut down the cache."
114 (gnus-cache-write-active)
115 (gnus-cache-save-buffers)
116 (setq gnus-cache-active-hashtb nil))
118 (defun gnus-cache-save-buffers ()
119 ;; save the overview buffer if it exists and has been modified
120 ;; delete empty cache subdirectories
121 (when gnus-cache-buffer
122 (let ((buffer (cdr gnus-cache-buffer))
123 (overview-file (gnus-cache-file-name
124 (car gnus-cache-buffer) ".overview")))
125 ;; write the overview only if it was modified
126 (when (buffer-modified-p buffer)
129 (if (> (buffer-size) 0)
130 ;; Non-empty overview, write it to a file.
131 (let ((coding-system-for-write
132 gnus-cache-overview-coding-system))
133 (gnus-write-buffer overview-file))
134 ;; Empty overview file, remove it
135 (when (file-exists-p overview-file)
136 (delete-file overview-file))
137 ;; If possible, remove group's cache subdirectory.
139 ;; FIXME: we can detect the error type and warn the user
140 ;; of any inconsistencies (articles w/o nov entries?).
141 ;; for now, just be conservative...delete only if safe -- sj
142 (delete-directory (file-name-directory overview-file))
144 ;; Kill the buffer -- it's either unmodified or saved.
145 (gnus-kill-buffer buffer)
146 (setq gnus-cache-buffer nil))))
148 (defun gnus-cache-possibly-enter-article
149 (group article ticked dormant unread &optional force)
150 (when (and (or force (not (eq gnus-use-cache 'passive)))
152 (> article 0)) ; This might be a dummy article.
153 (let ((number article) file headers)
154 ;; If this is a virtual group, we find the real group.
155 (when (gnus-virtual-group-p group)
156 (let ((result (nnvirtual-find-group-art
157 (gnus-group-real-name group) article)))
158 (setq group (car result)
159 number (cdr result))))
161 (> number 0) ; Reffed article.
163 (and (gnus-cache-fully-p group)
164 (gnus-cache-member-of-class
165 gnus-cache-enter-articles ticked dormant unread)))
166 (not (file-exists-p (setq file (gnus-cache-file-name
168 ;; Possibly create the cache directory.
169 (gnus-make-directory (file-name-directory file))
170 ;; Save the article in the cache.
171 (if (file-exists-p file)
172 t ; The article already is saved.
174 (set-buffer nntp-server-buffer)
176 (let ((gnus-use-cache nil)
177 (gnus-article-decode-hook nil))
178 (gnus-request-article-this-buffer number group))
179 (when (> (buffer-size) 0)
180 (let ((coding-system-for-write gnus-cache-coding-system))
181 (gnus-write-buffer file))
182 (setq headers (nnheader-parse-head t))
183 (mail-header-set-number headers number)
184 (gnus-cache-change-buffer group)
185 (set-buffer (cdr gnus-cache-buffer))
186 (goto-char (point-max))
188 (while (condition-case ()
190 (> (read (current-buffer)) number))
192 ;; The line was malformed, so we just remove it!!
200 (when (< (read (current-buffer)) number)
205 (nnheader-insert-nov headers)
206 ;; Update the active info.
207 (set-buffer gnus-summary-buffer)
208 (gnus-cache-possibly-update-active group (cons number number))
209 (push article gnus-newsgroup-cached)
210 (gnus-summary-update-secondary-mark article))
213 (defun gnus-cache-enter-remove-article (article)
214 "Mark ARTICLE for later possible removal."
216 (push article gnus-cache-removable-articles)))
218 (defun gnus-cache-possibly-remove-articles ()
219 "Possibly remove some of the removable articles."
220 (if (not (gnus-virtual-group-p gnus-newsgroup-name))
221 (gnus-cache-possibly-remove-articles-1)
222 (let ((arts gnus-cache-removable-articles)
225 (when (setq ga (nnvirtual-find-group-art
226 (gnus-group-real-name gnus-newsgroup-name) (pop arts)))
227 (let ((gnus-cache-removable-articles (list (cdr ga)))
228 (gnus-newsgroup-name (car ga)))
229 (gnus-cache-possibly-remove-articles-1)))))
230 (setq gnus-cache-removable-articles nil)))
232 (defun gnus-cache-possibly-remove-articles-1 ()
233 "Possibly remove some of the removable articles."
234 (when (gnus-cache-fully-p gnus-newsgroup-name)
235 (let ((articles gnus-cache-removable-articles)
236 (cache-articles gnus-newsgroup-cached)
238 (gnus-cache-change-buffer gnus-newsgroup-name)
240 (when (memq (setq article (pop articles)) cache-articles)
241 ;; The article was in the cache, so we see whether we are
242 ;; supposed to remove it from the cache.
243 (gnus-cache-possibly-remove-article
244 article (memq article gnus-newsgroup-marked)
245 (memq article gnus-newsgroup-dormant)
246 (or (memq article gnus-newsgroup-unreads)
247 (memq article gnus-newsgroup-unselected))))))
248 ;; The overview file might have been modified, save it
249 ;; safe because we're only called at group exit anyway.
250 (gnus-cache-save-buffers)))
252 (defun gnus-cache-request-article (article group)
253 "Retrieve ARTICLE in GROUP from the cache."
254 (let ((file (gnus-cache-file-name group article))
255 (buffer-read-only nil))
256 (when (file-exists-p file)
258 (gnus-kill-all-overlays)
259 (let ((coding-system-for-read gnus-cache-coding-system))
260 (insert-file-contents file))
263 (defun gnus-cache-possibly-alter-active (group active)
264 "Alter the ACTIVE info for GROUP to reflect the articles in the cache."
265 (when gnus-cache-active-hashtb
266 (let ((cache-active (gnus-gethash group gnus-cache-active-hashtb)))
268 (when (< (car cache-active) (car active))
269 (setcar active (car cache-active)))
270 (when (> (cdr cache-active) (cdr active))
271 (setcdr active (cdr cache-active)))))))
273 (defun gnus-cache-retrieve-headers (articles group &optional fetch-old)
274 "Retrieve the headers for ARTICLES in GROUP."
276 (setq gnus-newsgroup-cached (gnus-cache-articles-in-group group))))
278 ;; No cached articles here, so we just retrieve them
280 (let ((gnus-use-cache nil))
281 (gnus-retrieve-headers articles group fetch-old))
282 (let ((uncached-articles (gnus-sorted-intersection
283 (gnus-sorted-complement articles cached)
285 (cache-file (gnus-cache-file-name group ".overview"))
287 ;; We first retrieve all the headers that we don't have in
289 (let ((gnus-use-cache nil))
290 (when uncached-articles
291 (setq type (and articles
292 (gnus-retrieve-headers
293 uncached-articles group fetch-old)))))
294 (gnus-cache-save-buffers)
295 ;; Then we insert the cached headers.
298 ((not (file-exists-p cache-file))
299 ;; There are no cached headers.
302 ;; There were no uncached headers (or retrieval was
303 ;; unsuccessful), so we use the cached headers exclusively.
304 (set-buffer nntp-server-buffer)
306 (let ((coding-system-for-read
307 gnus-cache-overview-coding-system))
308 (insert-file-contents cache-file))
311 ;; We have both cached and uncached NOV headers, so we
313 (gnus-cache-braid-nov group cached)
317 (gnus-cache-braid-heads group (gnus-sorted-intersection
321 (defun gnus-cache-enter-article (&optional n)
322 "Enter the next N articles into the cache.
323 If not given a prefix, use the process marked articles instead.
324 Returns the list of articles entered."
326 (let ((articles (gnus-summary-work-articles n))
328 (while (setq article (pop articles))
329 (gnus-summary-remove-process-mark article)
330 (if (natnump article)
331 (when (gnus-cache-possibly-enter-article
332 gnus-newsgroup-name article
335 (gnus-message 2 "Can't cache article %d" article))
336 (gnus-summary-update-secondary-mark article))
337 (gnus-summary-next-subject 1)
338 (gnus-summary-position-point)
341 (defun gnus-cache-remove-article (n)
342 "Remove the next N articles from the cache.
343 If not given a prefix, use the process marked articles instead.
344 Returns the list of articles removed."
346 (gnus-cache-change-buffer gnus-newsgroup-name)
347 (let ((articles (gnus-summary-work-articles n))
350 (setq article (pop articles))
351 (gnus-summary-remove-process-mark article)
352 (when (gnus-cache-possibly-remove-article article nil nil nil t)
354 (gnus-summary-update-secondary-mark article))
355 (gnus-summary-next-subject 1)
356 (gnus-summary-position-point)
359 (defun gnus-cached-article-p (article)
360 "Say whether ARTICLE is cached in the current group."
361 (memq article gnus-newsgroup-cached))
363 (defun gnus-summary-insert-cached-articles ()
364 "Insert all the articles cached for this group into the current buffer."
366 (let ((cached (sort (copy-sequence gnus-newsgroup-cached) '>))
367 (gnus-verbose (max 6 gnus-verbose)))
369 (gnus-message 3 "No cached articles for this group"))
371 (gnus-summary-goto-subject (pop cached) t))))
373 (defun gnus-summary-limit-include-cached ()
374 "Limit the summary buffer to articles that are cached."
376 (let ((cached (sort (copy-sequence gnus-newsgroup-cached) '>))
377 (gnus-verbose (max 6 gnus-verbose)))
380 (gnus-summary-limit cached)
381 (gnus-summary-position-point))
382 (gnus-message 3 "No cached articles for this group"))))
384 ;;; Internal functions.
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
396 (setq gnus-cache-buffer
398 (set-buffer (gnus-get-buffer-create
399 " *gnus-cache-overview*"))))
400 ;; Insert the contents of this group's cache overview.
402 (let ((file (gnus-cache-file-name group ".overview")))
403 (when (file-exists-p file)
404 (nnheader-insert-file-contents file)))
405 ;; We have a fresh (empty/just loaded) buffer,
406 ;; mark it as unmodified to save a redundant write later.
407 (set-buffer-modified-p nil))))
409 ;; Return whether an article is a member of a class.
410 (defun gnus-cache-member-of-class (class ticked dormant unread)
411 (or (and ticked (memq 'ticked class))
412 (and dormant (memq 'dormant class))
413 (and unread (memq 'unread class))
414 (and (not unread) (not ticked) (not dormant) (memq 'read class))))
416 (defun gnus-cache-file-name (group article)
418 (if (stringp article) article (int-to-string article))
419 (file-name-as-directory
421 (nnheader-translate-file-chars
422 (if (gnus-use-long-file-name 'not-cache)
424 (let ((group (nnheader-replace-duplicate-chars-in-string
425 (nnheader-replace-chars-in-string group ?/ ?_)
427 ;; Translate the first colon into a slash.
428 (when (string-match ":" group)
429 (setq group (concat (substring group 0 (match-beginning 0))
430 "/" (substring group (match-end 0)))))
431 (nnheader-replace-chars-in-string group ?. ?/)))
433 gnus-cache-directory))))
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
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)
497 (let ((coding-system-for-read
498 gnus-cache-overview-coding-system))
499 (insert-file-contents
500 (or file (gnus-cache-file-name group ".overview"))))
501 (goto-char (point-min))
503 (goto-char (point-min)))
504 (set-buffer nntp-server-buffer)
505 (goto-char (point-min))
507 (while (and (not (eobp))
508 (< (read (current-buffer)) (car cached)))
512 (set-buffer cache-buf)
513 (if (search-forward (concat "\n" (int-to-string (car cached)) "\t")
515 (setq beg (progn (beginning-of-line) (point))
516 end (progn (end-of-line) (point)))
519 (insert-buffer-substring cache-buf beg end)
521 (setq cached (cdr cached)))
522 (kill-buffer cache-buf)))
524 (defun gnus-cache-braid-heads (group cached)
525 (let ((cache-buf (gnus-get-buffer-create " *gnus-cache*")))
527 (set-buffer cache-buf)
529 (set-buffer nntp-server-buffer)
530 (goto-char (point-min))
532 (while (and (not (eobp))
533 (looking-at "2.. +\\([0-9]+\\) ")
534 (< (progn (goto-char (match-beginning 1))
535 (read (current-buffer)))
537 (search-forward "\n.\n" nil 'move))
540 (set-buffer cache-buf)
542 (let ((coding-system-for-read
543 gnus-cache-coding-system))
544 (insert-file-contents (gnus-cache-file-name group (car cached))))
545 (goto-char (point-min))
547 (princ (car cached) (current-buffer))
548 (insert " Article retrieved.\n")
549 (search-forward "\n\n" nil 'move)
550 (delete-region (point) (point-max))
553 (insert-buffer-substring cache-buf)
554 (setq cached (cdr cached)))
555 (kill-buffer cache-buf)))
558 (defun gnus-jog-cache ()
559 "Go through all groups and put the articles into the cache.
562 $ emacs -batch -l ~/.emacs -l gnus -f gnus-jog-cache"
564 (let ((gnus-mark-article-hook nil)
566 (nnmail-spool-file nil)
568 (gnus-use-dribble-file nil)
569 (gnus-novice-user nil)
570 (gnus-large-newsgroup nil))
573 ;; Go through all groups...
574 (gnus-group-mark-buffer)
575 (gnus-group-iterate nil
577 (let (gnus-auto-select-next)
578 (gnus-summary-read-group group nil t)
579 ;; ... and enter the articles into the cache.
580 (when (eq major-mode 'gnus-summary-mode)
581 (gnus-uu-mark-buffer)
582 (gnus-cache-enter-article)
583 (kill-buffer (current-buffer))))))))
585 (defun gnus-cache-read-active (&optional force)
586 "Read the cache active file."
587 (gnus-make-directory gnus-cache-directory)
588 (if (or (not (file-exists-p gnus-cache-active-file))
589 (zerop (nth 7 (file-attributes gnus-cache-active-file)))
591 ;; There is no active file, so we generate one.
592 (gnus-cache-generate-active)
593 ;; We simply read the active file.
595 (gnus-set-work-buffer)
596 (nnheader-insert-file-contents gnus-cache-active-file)
597 (gnus-active-to-gnus-format
598 nil (setq gnus-cache-active-hashtb
600 (count-lines (point-min) (point-max)))))
601 (setq gnus-cache-active-altered nil))))
603 (defun gnus-cache-write-active (&optional force)
604 "Write the active hashtb to the active file."
606 (and gnus-cache-active-hashtb
607 gnus-cache-active-altered))
608 (gnus-write-active-file gnus-cache-active-file gnus-cache-active-hashtb t)
609 ;; Mark the active hashtb as unaltered.
610 (setq gnus-cache-active-altered nil)))
612 (defun gnus-cache-possibly-update-active (group active)
613 "Update active info bounds of GROUP with ACTIVE if necessary.
614 The update is performed if ACTIVE contains a higher or lower bound
616 (let ((lower t) (higher t))
617 (if gnus-cache-active-hashtb
618 (let ((cache-active (gnus-gethash group gnus-cache-active-hashtb)))
620 (unless (< (car active) (car cache-active))
622 (unless (> (cdr active) (cdr cache-active))
624 (gnus-cache-read-active))
626 (gnus-cache-update-active group (car active) t))
628 (gnus-cache-update-active group (cdr active)))))
630 (defun gnus-cache-update-active (group number &optional low)
631 "Update the upper bound of the active info of GROUP to NUMBER.
632 If LOW, update the lower bound instead."
633 (let ((active (gnus-gethash group gnus-cache-active-hashtb)))
635 ;; We just create a new active entry for this group.
636 (gnus-sethash group (cons number number) gnus-cache-active-hashtb)
637 ;; Update the lower or upper bound.
639 (setcar active number)
640 (setcdr active number)))
641 ;; Mark the active hashtb as altered.
642 (setq gnus-cache-active-altered t)))
645 (defun gnus-cache-generate-active (&optional directory)
646 "Generate the cache active file."
648 (let* ((top (null directory))
649 (directory (expand-file-name (or directory gnus-cache-directory)))
650 (files (directory-files directory 'full))
655 (concat "^" (regexp-quote
656 (file-name-as-directory
657 (expand-file-name gnus-cache-directory))))
658 (directory-file-name directory))
659 (nnheader-replace-chars-in-string
660 (substring (directory-file-name directory) (match-end 0))
664 (gnus-message 5 "Generating the cache active file...")
665 (setq gnus-cache-active-hashtb (gnus-make-hashtable 123)))
666 (when (string-match "^\\(nn[^_]+\\)_" group)
667 (setq group (replace-match "\\1:" t t group)))
668 ;; Separate articles from all other files and directories.
670 (if (string-match "^[0-9]+$" (file-name-nondirectory (car files)))
671 (push (string-to-int (file-name-nondirectory (pop files))) nums)
672 (push (pop files) alphs)))
673 ;; If we have nums, then this is probably a valid group.
674 (when (setq nums (sort nums '<))
675 (gnus-sethash group (cons (car nums) (gnus-last-element nums))
676 gnus-cache-active-hashtb))
677 ;; Go through all the other files.
679 (when (and (file-directory-p (car alphs))
680 (not (string-match "^\\."
681 (file-name-nondirectory (car alphs)))))
682 ;; We descend directories.
683 (gnus-cache-generate-active (car alphs)))
684 (setq alphs (cdr alphs)))
685 ;; Write the new active file.
687 (gnus-cache-write-active t)
688 (gnus-message 5 "Generating the cache active file...done"))))
691 (defun gnus-cache-generate-nov-databases (dir)
692 "Generate NOV files recursively starting in DIR."
693 (interactive (list gnus-cache-directory))
695 (let ((nnml-generate-active-function 'identity))
696 (nnml-generate-nov-databases-1 dir))
699 (defun gnus-cache-move-cache (dir)
700 "Move the cache tree to somewhere else."
701 (interactive "FMove the cache tree to: ")
702 (rename-file gnus-cache-directory dir))
704 (defun gnus-cache-fully-p (&optional group)
705 "Returns non-nil if the cache should be fully used.
706 If GROUP is non-nil, also cater to `gnus-cacheable-groups' and
707 `gnus-uncacheable-groups'."
709 (not (eq gnus-use-cache 'passive))
712 (and (or (not gnus-cacheable-groups)
713 (string-match gnus-cacheable-groups group))
714 (or (not gnus-uncacheable-groups)
715 (not (string-match gnus-uncacheable-groups group)))))))
718 (provide 'gnus-cache)
720 ;;; gnus-cache.el ends here