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>
5 ;; Tatsuya Ichikawa <t-ichi@po.shiojiri.ne.jp>
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 (concat (file-name-as-directory gnus-cache-directory) "active")
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 'binary
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)
92 (defvar gnus-cache-write-file-coding-system 'raw-text)
95 (autoload 'nnml-generate-nov-databases-1 "nnml")
96 (autoload 'nnvirtual-find-group-art "nnvirtual"))
100 ;;; Functions called from Gnus.
102 (defun gnus-cache-open ()
103 "Initialize the cache."
104 (when (or (file-exists-p gnus-cache-directory)
106 (not (eq gnus-use-cache 'passive))))
107 (gnus-cache-read-active)))
109 ;; Complexities of byte-compiling make this kludge necessary. Eeek.
111 (gnus-add-shutdown 'gnus-cache-close 'gnus))
113 (defun gnus-cache-close ()
114 "Shut down the cache."
115 (gnus-cache-write-active)
116 (gnus-cache-save-buffers)
117 (setq gnus-cache-active-hashtb nil))
119 (defun gnus-cache-save-buffers ()
120 ;; save the overview buffer if it exists and has been modified
121 ;; delete empty cache subdirectories
122 (when gnus-cache-buffer
123 (let ((buffer (cdr gnus-cache-buffer))
124 (overview-file (gnus-cache-file-name
125 (car gnus-cache-buffer) ".overview")))
126 ;; write the overview only if it was modified
127 (when (buffer-modified-p buffer)
130 (if (> (buffer-size) 0)
131 ;; Non-empty overview, write it to a file.
132 (let ((coding-system-for-write
133 gnus-cache-overview-coding-system))
134 (gnus-write-buffer overview-file))
135 ;; Empty overview file, remove it
136 (when (file-exists-p overview-file)
137 (delete-file overview-file))
138 ;; If possible, remove group's cache subdirectory.
140 ;; FIXME: we can detect the error type and warn the user
141 ;; of any inconsistencies (articles w/o nov entries?).
142 ;; for now, just be conservative...delete only if safe -- sj
143 (delete-directory (file-name-directory overview-file))
145 ;; Kill the buffer -- it's either unmodified or saved.
146 (gnus-kill-buffer buffer)
147 (setq gnus-cache-buffer nil))))
149 (defun gnus-cache-possibly-enter-article
150 (group article headers ticked dormant unread &optional force)
151 (when (and (or force (not (eq gnus-use-cache 'passive)))
154 (vectorp headers)) ; This might be a dummy article.
155 ;; If this is a virtual group, we find the real group.
156 (when (gnus-virtual-group-p group)
157 (let ((result (nnvirtual-find-group-art
158 (gnus-group-real-name group) article)))
159 (setq group (car result)
160 headers (copy-sequence headers))
161 (mail-header-set-number headers (cdr result))))
162 (let ((number (mail-header-number headers))
165 (> number 0) ; Reffed article.
167 (and (or (not gnus-cacheable-groups)
168 (string-match gnus-cacheable-groups group))
169 (or (not gnus-uncacheable-groups)
171 gnus-uncacheable-groups group)))
172 (gnus-cache-member-of-class
173 gnus-cache-enter-articles ticked dormant unread)))
174 (not (file-exists-p (setq file (gnus-cache-file-name
176 ;; Possibly create the cache directory.
177 (gnus-make-directory (file-name-directory file))
178 ;; Save the article in the cache.
179 (if (file-exists-p file)
180 t ; The article already is saved.
182 (set-buffer nntp-server-buffer)
183 (let ((gnus-use-cache nil)
184 (gnus-article-decode-hook nil))
185 (gnus-request-article-this-buffer number group))
186 (when (> (buffer-size) 0)
187 (let ((coding-system-for-write
188 gnus-cache-write-file-coding-system))
189 (gnus-write-buffer file))
190 (gnus-cache-change-buffer group)
191 (set-buffer (cdr gnus-cache-buffer))
192 (goto-char (point-max))
194 (while (condition-case ()
196 (> (read (current-buffer)) number))
198 ;; The line was malformed, so we just remove it!!
206 (when (< (read (current-buffer)) number)
211 (nnheader-insert-nov headers)
212 ;; Update the active info.
213 (set-buffer gnus-summary-buffer)
214 (gnus-cache-update-active group number)
215 (push article gnus-newsgroup-cached)
216 (gnus-summary-update-secondary-mark article))
219 (defun gnus-cache-enter-remove-article (article)
220 "Mark ARTICLE for later possible removal."
222 (push article gnus-cache-removable-articles)))
224 (defun gnus-cache-possibly-remove-articles ()
225 "Possibly remove some of the removable articles."
226 (if (not (gnus-virtual-group-p gnus-newsgroup-name))
227 (gnus-cache-possibly-remove-articles-1)
228 (let ((arts gnus-cache-removable-articles)
231 (when (setq ga (nnvirtual-find-group-art
232 (gnus-group-real-name gnus-newsgroup-name) (pop arts)))
233 (let ((gnus-cache-removable-articles (list (cdr ga)))
234 (gnus-newsgroup-name (car ga)))
235 (gnus-cache-possibly-remove-articles-1)))))
236 (setq gnus-cache-removable-articles nil)))
238 (defun gnus-cache-possibly-remove-articles-1 ()
239 "Possibly remove some of the removable articles."
240 (unless (eq gnus-use-cache 'passive)
241 (let ((articles gnus-cache-removable-articles)
242 (cache-articles gnus-newsgroup-cached)
244 (gnus-cache-change-buffer gnus-newsgroup-name)
246 (when (memq (setq article (pop articles)) cache-articles)
247 ;; The article was in the cache, so we see whether we are
248 ;; supposed to remove it from the cache.
249 (gnus-cache-possibly-remove-article
250 article (memq article gnus-newsgroup-marked)
251 (memq article gnus-newsgroup-dormant)
252 (or (memq article gnus-newsgroup-unreads)
253 (memq article gnus-newsgroup-unselected))))))
254 ;; The overview file might have been modified, save it
255 ;; safe because we're only called at group exit anyway.
256 (gnus-cache-save-buffers)))
258 (defun gnus-cache-request-article (article group)
259 "Retrieve ARTICLE in GROUP from the cache."
260 (let ((file (gnus-cache-file-name group article))
261 (buffer-read-only nil))
262 (when (file-exists-p file)
264 (gnus-kill-all-overlays)
265 (let ((nnheader-file-coding-system gnus-cache-coding-system))
266 (nnheader-insert-file-contents file))
269 (defun gnus-cache-possibly-alter-active (group active)
270 "Alter the ACTIVE info for GROUP to reflect the articles in the cache."
271 (when gnus-cache-active-hashtb
272 (let ((cache-active (gnus-gethash group gnus-cache-active-hashtb)))
274 (when (< (car cache-active) (car active))
275 (setcar active (car cache-active)))
276 (when (> (cdr cache-active) (cdr active))
277 (setcdr active (cdr cache-active)))))))
279 (defun gnus-cache-retrieve-headers (articles group &optional fetch-old)
280 "Retrieve the headers for ARTICLES in GROUP."
282 (setq gnus-newsgroup-cached (gnus-cache-articles-in-group group))))
284 ;; No cached articles here, so we just retrieve them
286 (let ((gnus-use-cache nil))
287 (gnus-retrieve-headers articles group fetch-old))
288 (let ((uncached-articles (gnus-sorted-intersection
289 (gnus-sorted-complement articles cached)
291 (cache-file (gnus-cache-file-name group ".overview"))
293 ;; We first retrieve all the headers that we don't have in
295 (let ((gnus-use-cache nil))
296 (when uncached-articles
297 (setq type (and articles
298 (gnus-retrieve-headers
299 uncached-articles group fetch-old)))))
300 (gnus-cache-save-buffers)
301 ;; Then we insert the cached headers.
304 ((not (file-exists-p cache-file))
305 ;; There are no cached headers.
308 ;; There were no uncached headers (or retrieval was
309 ;; unsuccessful), so we use the cached headers exclusively.
310 (set-buffer nntp-server-buffer)
312 (nnheader-insert-file-contents cache-file)
315 ;; We have both cached and uncached NOV headers, so we
317 (gnus-cache-braid-nov group cached)
321 (gnus-cache-braid-heads group (gnus-sorted-intersection
325 (defun gnus-cache-enter-article (&optional n)
326 "Enter the next N articles into the cache.
327 If not given a prefix, use the process marked articles instead.
328 Returns the list of articles entered."
330 (let ((articles (gnus-summary-work-articles n))
332 (while (setq article (pop articles))
333 (gnus-summary-remove-process-mark article)
334 (if (natnump article)
335 (when (gnus-cache-possibly-enter-article
336 gnus-newsgroup-name article
337 (gnus-summary-article-header article)
340 (gnus-message 2 "Can't cache article %d" article))
341 (gnus-summary-update-secondary-mark article))
342 (gnus-summary-next-subject 1)
343 (gnus-summary-position-point)
346 (defun gnus-cache-remove-article (n)
347 "Remove the next N articles from the cache.
348 If not given a prefix, use the process marked articles instead.
349 Returns the list of articles removed."
351 (gnus-cache-change-buffer gnus-newsgroup-name)
352 (let ((articles (gnus-summary-work-articles n))
355 (setq article (pop articles))
356 (gnus-summary-remove-process-mark article)
357 (when (gnus-cache-possibly-remove-article article nil nil nil t)
359 (gnus-summary-update-secondary-mark article))
360 (gnus-summary-next-subject 1)
361 (gnus-summary-position-point)
364 (defun gnus-cached-article-p (article)
365 "Say whether ARTICLE is cached in the current group."
366 (memq article gnus-newsgroup-cached))
368 (defun gnus-summary-insert-cached-articles ()
369 "Insert all the articles cached for this group into the current buffer."
371 (let ((cached (sort (copy-sequence gnus-newsgroup-cached) '<))
372 (gnus-verbose (max 6 gnus-verbose)))
374 (gnus-message 3 "No cached articles for this group"))
376 (gnus-summary-goto-subject (pop cached) t))))
378 (defalias 'gnus-summary-limit-include-cached
379 'gnus-summary-insert-cached-articles)
381 ;;; Internal functions.
383 (defun gnus-cache-change-buffer (group)
384 (and gnus-cache-buffer
385 ;; See if the current group's overview cache has been loaded.
386 (or (string= group (car gnus-cache-buffer))
387 ;; Another overview cache is current, save it.
388 (gnus-cache-save-buffers)))
389 ;; if gnus-cache buffer is nil, create it
390 (unless gnus-cache-buffer
391 ;; Create cache buffer
393 (setq gnus-cache-buffer
395 (set-buffer (gnus-get-buffer-create
396 " *gnus-cache-overview*"))))
397 ;; Insert the contents of this group's cache overview.
399 (let ((file (gnus-cache-file-name group ".overview")))
400 (when (file-exists-p file)
401 (nnheader-insert-file-contents file)))
402 ;; We have a fresh (empty/just loaded) buffer,
403 ;; mark it as unmodified to save a redundant write later.
404 (set-buffer-modified-p nil))))
406 ;; Return whether an article is a member of a class.
407 (defun gnus-cache-member-of-class (class ticked dormant unread)
408 (or (and ticked (memq 'ticked class))
409 (and dormant (memq 'dormant class))
410 (and unread (memq 'unread class))
411 (and (not unread) (not ticked) (not dormant) (memq 'read class))))
413 (defun gnus-cache-file-name (group article)
414 (concat (file-name-as-directory gnus-cache-directory)
415 (file-name-as-directory
416 (nnheader-translate-file-chars
417 (if (gnus-use-long-file-name 'not-cache)
419 (let ((group (nnheader-replace-chars-in-string group ?/ ?_)))
420 ;; Translate the first colon into a slash.
421 (when (string-match ":" group)
422 (aset group (match-beginning 0) ?/))
423 (nnheader-replace-chars-in-string group ?. ?/)))
425 (if (stringp article) article (int-to-string article))))
427 (defun gnus-cache-update-article (group article)
428 "If ARTICLE is in the cache, remove it and re-enter it."
429 (gnus-cache-change-buffer group)
430 (when (gnus-cache-possibly-remove-article article nil nil nil t)
431 (let ((gnus-use-cache nil))
432 (gnus-cache-possibly-enter-article
433 gnus-newsgroup-name article (gnus-summary-article-header article)
436 (defun gnus-cache-possibly-remove-article (article ticked dormant unread
438 "Possibly remove ARTICLE from the cache."
439 (let ((group gnus-newsgroup-name)
442 ;; If this is a virtual group, we find the real group.
443 (when (gnus-virtual-group-p group)
444 (let ((result (nnvirtual-find-group-art
445 (gnus-group-real-name group) article)))
446 (setq group (car result)
447 number (cdr result))))
448 (setq file (gnus-cache-file-name group number))
449 (when (and (file-exists-p file)
451 (gnus-cache-member-of-class
452 gnus-cache-remove-articles ticked dormant unread)))
455 (set-buffer (cdr gnus-cache-buffer))
456 (goto-char (point-min))
457 (when (or (looking-at (concat (int-to-string number) "\t"))
458 (search-forward (concat "\n" (int-to-string number) "\t")
460 (delete-region (progn (beginning-of-line) (point))
461 (progn (forward-line 1) (point)))))
462 (setq gnus-newsgroup-cached
463 (delq article gnus-newsgroup-cached))
464 (gnus-summary-update-secondary-mark article)
467 (defun gnus-cache-articles-in-group (group)
468 "Return a sorted list of cached articles in GROUP."
469 (let ((dir (file-name-directory (gnus-cache-file-name group 1)))
471 (when (file-exists-p dir)
473 (sort (mapcar (lambda (name) (string-to-int name))
474 (directory-files dir nil "^[0-9]+$" t))
476 ;; Update the cache active file, just to synch more.
478 (gnus-cache-update-active group (car articles) t)
479 (gnus-cache-update-active group (car (last articles))))
482 (defun gnus-cache-braid-nov (group cached &optional file)
483 (let ((cache-buf (gnus-get-buffer-create " *gnus-cache*"))
485 (gnus-cache-save-buffers)
487 (set-buffer cache-buf)
489 (nnheader-insert-file-contents (or file (gnus-cache-file-name group ".overview")))
490 (goto-char (point-min))
492 (goto-char (point-min)))
493 (set-buffer nntp-server-buffer)
494 (goto-char (point-min))
496 (while (and (not (eobp))
497 (< (read (current-buffer)) (car cached)))
501 (set-buffer cache-buf)
502 (if (search-forward (concat "\n" (int-to-string (car cached)) "\t")
504 (setq beg (progn (beginning-of-line) (point))
505 end (progn (end-of-line) (point)))
508 (insert-buffer-substring cache-buf beg end)
510 (setq cached (cdr cached)))
511 (kill-buffer cache-buf)))
513 (defun gnus-cache-braid-heads (group cached)
514 (let ((cache-buf (gnus-get-buffer-create " *gnus-cache*")))
516 (set-buffer cache-buf)
518 (set-buffer nntp-server-buffer)
519 (goto-char (point-min))
521 (while (and (not (eobp))
522 (looking-at "2.. +\\([0-9]+\\) ")
523 (< (progn (goto-char (match-beginning 1))
524 (read (current-buffer)))
526 (search-forward "\n.\n" nil 'move))
529 (set-buffer cache-buf)
531 (nnheader-insert-file-contents (gnus-cache-file-name group (car cached)))
532 (goto-char (point-min))
534 (princ (car cached) (current-buffer))
535 (insert " Article retrieved.\n")
536 (search-forward "\n\n" nil 'move)
537 (delete-region (point) (point-max))
540 (insert-buffer-substring cache-buf)
541 (setq cached (cdr cached)))
542 (kill-buffer cache-buf)))
545 (defun gnus-jog-cache ()
546 "Go through all groups and put the articles into the cache.
549 $ emacs -batch -l ~/.emacs -l gnus -f gnus-jog-cache"
551 (let ((gnus-mark-article-hook nil)
553 (nnmail-spool-file nil)
554 (gnus-use-dribble-file nil)
555 (gnus-novice-user nil)
556 (gnus-large-newsgroup nil))
559 ;; Go through all groups...
560 (gnus-group-mark-buffer)
561 (gnus-group-iterate nil
563 (let (gnus-auto-select-next)
564 (gnus-summary-read-group group nil t)
565 ;; ... and enter the articles into the cache.
566 (when (eq major-mode 'gnus-summary-mode)
567 (gnus-uu-mark-buffer)
568 (gnus-cache-enter-article)
569 (kill-buffer (current-buffer))))))))
571 (defun gnus-cache-read-active (&optional force)
572 "Read the cache active file."
573 (gnus-make-directory gnus-cache-directory)
574 (if (or (not (file-exists-p gnus-cache-active-file))
575 (not (zerop (nth 7 (file-attributes gnus-cache-active-file))))
577 ;; There is no active file, so we generate one.
578 (gnus-cache-generate-active)
579 ;; We simply read the active file.
581 (gnus-set-work-buffer)
582 (nnheader-insert-file-contents gnus-cache-active-file)
583 (gnus-active-to-gnus-format
584 nil (setq gnus-cache-active-hashtb
586 (count-lines (point-min) (point-max)))))
587 (setq gnus-cache-active-altered nil))))
589 (defun gnus-cache-write-active (&optional force)
590 "Write the active hashtb to the active file."
592 (and gnus-cache-active-hashtb
593 gnus-cache-active-altered))
594 (with-temp-file gnus-cache-active-file
597 (when (and sym (boundp sym))
598 (insert (format "%s %d %d y\n"
599 (symbol-name sym) (cdr (symbol-value sym))
600 (car (symbol-value sym))))))
601 gnus-cache-active-hashtb))
602 ;; Mark the active hashtb as unaltered.
603 (setq gnus-cache-active-altered nil)))
605 (defun gnus-cache-update-active (group number &optional low)
606 "Update the upper bound of the active info of GROUP to NUMBER.
607 If LOW, update the lower bound instead."
608 (let ((active (gnus-gethash group gnus-cache-active-hashtb)))
610 ;; We just create a new active entry for this group.
611 (gnus-sethash group (cons number number) gnus-cache-active-hashtb)
612 ;; Update the lower or upper bound.
614 (setcar active number)
615 (setcdr active number)))
616 ;; Mark the active hashtb as altered.
617 (setq gnus-cache-active-altered t)))
620 (defun gnus-cache-generate-active (&optional directory)
621 "Generate the cache active file."
623 (let* ((top (null directory))
624 (directory (expand-file-name (or directory gnus-cache-directory)))
625 (files (directory-files directory 'full))
630 (concat "^" (regexp-quote
631 (file-name-as-directory
632 (expand-file-name gnus-cache-directory))))
633 (directory-file-name directory))
634 (nnheader-replace-chars-in-string
635 (substring (directory-file-name directory) (match-end 0))
639 (gnus-message 5 "Generating the cache active file...")
640 (setq gnus-cache-active-hashtb (gnus-make-hashtable 123)))
641 (when (string-match "^\\(nn[^_]+\\)_" group)
642 (setq group (replace-match "\\1:" t t group)))
643 ;; Separate articles from all other files and directories.
645 (if (string-match "^[0-9]+$" (file-name-nondirectory (car files)))
646 (push (string-to-int (file-name-nondirectory (pop files))) nums)
647 (push (pop files) alphs)))
648 ;; If we have nums, then this is probably a valid group.
649 (when (setq nums (sort nums '<))
650 (gnus-sethash group (cons (car nums) (gnus-last-element nums))
651 gnus-cache-active-hashtb))
652 ;; Go through all the other files.
654 (when (and (file-directory-p (car alphs))
655 (not (string-match "^\\."
656 (file-name-nondirectory (car alphs)))))
657 ;; We descend directories.
658 (gnus-cache-generate-active (car alphs)))
659 (setq alphs (cdr alphs)))
660 ;; Write the new active file.
662 (gnus-cache-write-active t)
663 (gnus-message 5 "Generating the cache active file...done"))))
666 (defun gnus-cache-generate-nov-databases (dir)
667 "Generate NOV files recursively starting in DIR."
668 (interactive (list gnus-cache-directory))
670 (let ((nnml-generate-active-function 'identity))
671 (nnml-generate-nov-databases-1 dir)))
673 (defun gnus-cache-move-cache (dir)
674 "Move the cache tree to somewhere else."
675 (interactive "FMove the cache tree to: ")
676 (rename-file gnus-cache-directory dir))
678 (provide 'gnus-cache)
680 ;;; gnus-cache.el ends here