1 ;;; gnus-cache.el --- cache interface for Gnus
2 ;; Copyright (C) 1995,96 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.
35 (defvar gnus-cache-directory
36 (nnheader-concat gnus-directory "cache/")
37 "*The directory where cached articles will be stored.")
39 (defvar gnus-cache-active-file
40 (concat (file-name-as-directory gnus-cache-directory) "active")
41 "*The cache active file.")
43 (defvar gnus-cache-enter-articles '(ticked dormant)
44 "*Classes of articles to enter into the cache.")
46 (defvar gnus-cache-remove-articles '(read)
47 "*Classes of articles to remove from the cache.")
49 (defvar gnus-uncacheable-groups nil
50 "*Groups that match this regexp will not be cached.
52 If you want to avoid caching your nnml groups, you could set this
53 variable to \"^nnml\".")
57 ;;; Internal variables.
59 (defvar gnus-cache-removable-articles nil)
60 (defvar gnus-cache-buffer nil)
61 (defvar gnus-cache-active-hashtb nil)
62 (defvar gnus-cache-active-altered nil)
65 (autoload 'nnml-generate-nov-databases-1 "nnml")
66 (autoload 'nnvirtual-find-group-art "nnvirtual"))
70 ;;; Functions called from Gnus.
72 (defun gnus-cache-open ()
73 "Initialize the cache."
74 (when (or (file-exists-p gnus-cache-directory)
76 (not (eq gnus-use-cache 'passive))))
77 (gnus-cache-read-active)))
80 (gnus-add-shutdown 'gnus-cache-close 'gnus)
81 ;; Complexities of byte-compiling makes this kludge necessary. Eeek.
84 (defun gnus-cache-close ()
85 "Shut down the cache."
86 (gnus-cache-write-active)
87 (gnus-cache-save-buffers)
88 (setq gnus-cache-active-hashtb nil))
90 (defun gnus-cache-save-buffers ()
91 ;; save the overview buffer if it exists and has been modified
92 ;; delete empty cache subdirectories
93 (if (null gnus-cache-buffer)
95 (let ((buffer (cdr gnus-cache-buffer))
96 (overview-file (gnus-cache-file-name
97 (car gnus-cache-buffer) ".overview")))
98 ;; write the overview only if it was modified
99 (if (buffer-modified-p buffer)
102 (if (> (buffer-size) 0)
103 ;; non-empty overview, write it out
105 (gnus-make-directory (file-name-directory overview-file))
106 (write-region (point-min) (point-max)
107 overview-file nil 'quietly))
108 ;; empty overview file, remove it
109 (and (file-exists-p overview-file)
110 (delete-file overview-file))
111 ;; if possible, remove group's cache subdirectory
113 ;; FIXME: we can detect the error type and warn the user
114 ;; of any inconsistencies (articles w/o nov entries?).
115 ;; for now, just be conservative...delete only if safe -- sj
116 (delete-directory (file-name-directory overview-file))
118 ;; kill the buffer, it's either unmodified or saved
119 (gnus-kill-buffer buffer)
120 (setq gnus-cache-buffer nil))))
122 (defun gnus-cache-possibly-enter-article
123 (group article headers ticked dormant unread &optional force)
124 (when (and (or force (not (eq gnus-use-cache 'passive)))
127 (vectorp headers)) ; This might be a dummy article.
128 ;; If this is a virtual group, we find the real group.
129 (when (gnus-virtual-group-p group)
130 (let ((result (nnvirtual-find-group-art
131 (gnus-group-real-name group) article)))
132 (setq group (car result)
133 headers (copy-sequence headers))
134 (mail-header-set-number headers (cdr result))))
135 (let ((number (mail-header-number headers))
137 (when (and (> number 0) ; Reffed article.
138 (or (not gnus-uncacheable-groups)
139 (not (string-match gnus-uncacheable-groups group)))
141 (gnus-cache-member-of-class
142 gnus-cache-enter-articles ticked dormant unread))
143 (not (file-exists-p (setq file (gnus-cache-file-name
145 ;; Possibly create the cache directory.
146 (gnus-make-directory (setq dir (file-name-directory file)))
147 ;; Save the article in the cache.
148 (if (file-exists-p file)
149 t ; The article already is saved.
151 (set-buffer nntp-server-buffer)
152 (let ((gnus-use-cache nil))
153 (gnus-request-article-this-buffer number group))
154 (when (> (buffer-size) 0)
155 (write-region (point-min) (point-max) file nil 'quiet)
156 (gnus-cache-change-buffer group)
157 (set-buffer (cdr gnus-cache-buffer))
158 (goto-char (point-max))
160 (while (condition-case ()
162 (> (read (current-buffer)) number))
164 ;; The line was malformed, so we just remove it!!
172 (if (< (read (current-buffer)) number)
177 ;; [number subject from date id references chars lines xref]
178 (insert (format "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t\n"
179 (mail-header-number headers)
180 (mail-header-subject headers)
181 (mail-header-from headers)
182 (mail-header-date headers)
183 (mail-header-id headers)
184 (or (mail-header-references headers) "")
185 (or (mail-header-chars headers) "")
186 (or (mail-header-lines headers) "")
187 (or (mail-header-xref headers) "")))
188 ;; Update the active info.
189 (set-buffer gnus-summary-buffer)
190 (gnus-cache-update-active group number)
191 (push article gnus-newsgroup-cached)
192 (gnus-summary-update-secondary-mark article))
195 (defun gnus-cache-enter-remove-article (article)
196 "Mark ARTICLE for later possible removal."
198 (push article gnus-cache-removable-articles)))
200 (defun gnus-cache-possibly-remove-articles ()
201 "Possibly remove some of the removable articles."
202 (if (not (gnus-virtual-group-p gnus-newsgroup-name))
203 (gnus-cache-possibly-remove-articles-1)
204 (let ((arts gnus-cache-removable-articles)
207 (when (setq ga (nnvirtual-find-group-art
208 (gnus-group-real-name gnus-newsgroup-name) (pop arts)))
209 (let ((gnus-cache-removable-articles (list (cdr ga)))
210 (gnus-newsgroup-name (car ga)))
211 (gnus-cache-possibly-remove-articles-1)))))
212 (setq gnus-cache-removable-articles nil)))
214 (defun gnus-cache-possibly-remove-articles-1 ()
215 "Possibly remove some of the removable articles."
216 (unless (eq gnus-use-cache 'passive)
217 (let ((articles gnus-cache-removable-articles)
218 (cache-articles gnus-newsgroup-cached)
220 (gnus-cache-change-buffer gnus-newsgroup-name)
222 (if (memq (setq article (pop articles)) cache-articles)
223 ;; The article was in the cache, so we see whether we are
224 ;; supposed to remove it from the cache.
225 (gnus-cache-possibly-remove-article
226 article (memq article gnus-newsgroup-marked)
227 (memq article gnus-newsgroup-dormant)
228 (or (memq article gnus-newsgroup-unreads)
229 (memq article gnus-newsgroup-unselected))))))
230 ;; The overview file might have been modified, save it
231 ;; safe because we're only called at group exit anyway.
232 (gnus-cache-save-buffers)))
234 (defun gnus-cache-request-article (article group)
235 "Retrieve ARTICLE in GROUP from the cache."
236 (let ((file (gnus-cache-file-name group article))
237 (buffer-read-only nil))
238 (when (file-exists-p file)
240 (gnus-kill-all-overlays)
241 (insert-file-contents file)
244 (defun gnus-cache-possibly-alter-active (group active)
245 "Alter the ACTIVE info for GROUP to reflect the articles in the cache."
246 (when gnus-cache-active-hashtb
247 (let ((cache-active (gnus-gethash group gnus-cache-active-hashtb)))
249 (< (car cache-active) (car active))
250 (setcar active (car cache-active)))
252 (> (cdr cache-active) (cdr active))
253 (setcdr active (cdr cache-active))))))
255 (defun gnus-cache-retrieve-headers (articles group &optional fetch-old)
256 "Retrieve the headers for ARTICLES in GROUP."
258 (setq gnus-newsgroup-cached (gnus-cache-articles-in-group group))))
260 ;; No cached articles here, so we just retrieve them
262 (let ((gnus-use-cache nil))
263 (gnus-retrieve-headers articles group fetch-old))
264 (let ((uncached-articles (gnus-sorted-intersection
265 (gnus-sorted-complement articles cached)
267 (cache-file (gnus-cache-file-name group ".overview"))
269 ;; We first retrieve all the headers that we don't have in
271 (let ((gnus-use-cache nil))
272 (when uncached-articles
273 (setq type (and articles
274 (gnus-retrieve-headers
275 uncached-articles group fetch-old)))))
276 (gnus-cache-save-buffers)
277 ;; Then we insert the cached headers.
280 ((not (file-exists-p cache-file))
281 ;; There are no cached headers.
284 ;; There were no uncached headers (or retrieval was
285 ;; unsuccessful), so we use the cached headers exclusively.
286 (set-buffer nntp-server-buffer)
288 (insert-file-contents cache-file)
291 ;; We have both cached and uncached NOV headers, so we
293 (gnus-cache-braid-nov group cached)
297 (gnus-cache-braid-heads group (gnus-sorted-intersection
301 (defun gnus-cache-enter-article (&optional n)
302 "Enter the next N articles into the cache.
303 If not given a prefix, use the process marked articles instead.
304 Returns the list of articles entered."
306 (gnus-set-global-variables)
307 (let ((articles (gnus-summary-work-articles n))
310 (setq article (pop articles))
311 (when (gnus-cache-possibly-enter-article
312 gnus-newsgroup-name article (gnus-summary-article-header article)
315 (gnus-summary-remove-process-mark article)
316 (gnus-summary-update-secondary-mark article))
317 (gnus-summary-next-subject 1)
318 (gnus-summary-position-point)
321 (defun gnus-cache-remove-article (n)
322 "Remove the next N articles from the cache.
323 If not given a prefix, use the process marked articles instead.
324 Returns the list of articles removed."
326 (gnus-set-global-variables)
327 (gnus-cache-change-buffer gnus-newsgroup-name)
328 (let ((articles (gnus-summary-work-articles n))
331 (setq article (pop articles))
332 (when (gnus-cache-possibly-remove-article article nil nil nil t)
334 (gnus-summary-remove-process-mark article)
335 (gnus-summary-update-secondary-mark article))
336 (gnus-summary-next-subject 1)
337 (gnus-summary-position-point)
340 (defun gnus-cached-article-p (article)
341 "Say whether ARTICLE is cached in the current group."
342 (memq article gnus-newsgroup-cached))
344 (defun gnus-summary-insert-cached-articles ()
345 "Insert all the articles cached for this group into the current buffer."
347 (let ((cached gnus-newsgroup-cached))
349 (error "No cached articles for this group"))
351 (gnus-summary-goto-subject (pop cached) t))))
353 ;;; Internal functions.
355 (defun gnus-cache-change-buffer (group)
356 (and gnus-cache-buffer
357 ;; See if the current group's overview cache has been loaded.
358 (or (string= group (car gnus-cache-buffer))
359 ;; Another overview cache is current, save it.
360 (gnus-cache-save-buffers)))
361 ;; if gnus-cache buffer is nil, create it
362 (or gnus-cache-buffer
363 ;; Create cache buffer
365 (setq gnus-cache-buffer
367 (set-buffer (get-buffer-create " *gnus-cache-overview*"))))
368 (buffer-disable-undo (current-buffer))
369 ;; Insert the contents of this group's cache overview.
371 (let ((file (gnus-cache-file-name group ".overview")))
372 (and (file-exists-p file)
373 (insert-file-contents file)))
374 ;; We have a fresh (empty/just loaded) buffer,
375 ;; mark it as unmodified to save a redundant write later.
376 (set-buffer-modified-p nil))))
378 ;; Return whether an article is a member of a class.
379 (defun gnus-cache-member-of-class (class ticked dormant unread)
380 (or (and ticked (memq 'ticked class))
381 (and dormant (memq 'dormant class))
382 (and unread (memq 'unread class))
383 (and (not unread) (not ticked) (not dormant) (memq 'read class))))
385 (defun gnus-cache-file-name (group article)
386 (concat (file-name-as-directory gnus-cache-directory)
387 (file-name-as-directory
388 (if (gnus-use-long-file-name 'not-cache)
390 (let ((group (nnheader-replace-chars-in-string group ?/ ?_)))
391 ;; Translate the first colon into a slash.
392 (when (string-match ":" group)
393 (aset group (match-beginning 0) ?/))
394 (nnheader-replace-chars-in-string group ?. ?/))))
395 (if (stringp article) article (int-to-string article))))
397 (defun gnus-cache-update-article (group article)
398 "If ARTICLE is in the cache, remove it and re-enter it."
399 (when (gnus-cache-possibly-remove-article article nil nil nil t)
400 (let ((gnus-use-cache nil))
401 (gnus-cache-possibly-enter-article
402 gnus-newsgroup-name article (gnus-summary-article-header article)
405 (defun gnus-cache-possibly-remove-article (article ticked dormant unread
407 "Possibly remove ARTICLE from the cache."
408 (let ((group gnus-newsgroup-name)
411 ;; If this is a virtual group, we find the real group.
412 (when (gnus-virtual-group-p group)
413 (let ((result (nnvirtual-find-group-art
414 (gnus-group-real-name group) article)))
415 (setq group (car result)
416 number (cdr result))))
417 (setq file (gnus-cache-file-name group number))
418 (when (and (file-exists-p file)
420 (gnus-cache-member-of-class
421 gnus-cache-remove-articles ticked dormant unread)))
424 (set-buffer (cdr gnus-cache-buffer))
425 (goto-char (point-min))
426 (if (or (looking-at (concat (int-to-string number) "\t"))
427 (search-forward (concat "\n" (int-to-string number) "\t")
429 (delete-region (progn (beginning-of-line) (point))
430 (progn (forward-line 1) (point)))))
431 (setq gnus-newsgroup-cached
432 (delq article gnus-newsgroup-cached))
433 (gnus-summary-update-secondary-mark article)
436 (defun gnus-cache-articles-in-group (group)
437 "Return a sorted list of cached articles in GROUP."
438 (let ((dir (file-name-directory (gnus-cache-file-name group 1))))
439 (when (file-exists-p dir)
440 (sort (mapcar (lambda (name) (string-to-int name))
441 (directory-files dir nil "^[0-9]+$" t))
444 (defun gnus-cache-braid-nov (group cached)
445 (let ((cache-buf (get-buffer-create " *gnus-cache*"))
447 (gnus-cache-save-buffers)
449 (set-buffer cache-buf)
450 (buffer-disable-undo (current-buffer))
452 (insert-file-contents (gnus-cache-file-name group ".overview"))
453 (goto-char (point-min))
455 (goto-char (point-min)))
456 (set-buffer nntp-server-buffer)
457 (goto-char (point-min))
459 (while (and (not (eobp))
460 (< (read (current-buffer)) (car cached)))
464 (set-buffer cache-buf)
465 (if (search-forward (concat "\n" (int-to-string (car cached)) "\t")
467 (setq beg (progn (beginning-of-line) (point))
468 end (progn (end-of-line) (point)))
470 (if beg (progn (insert-buffer-substring cache-buf beg end)
472 (setq cached (cdr cached)))
473 (kill-buffer cache-buf)))
475 (defun gnus-cache-braid-heads (group cached)
476 (let ((cache-buf (get-buffer-create " *gnus-cache*")))
478 (set-buffer cache-buf)
479 (buffer-disable-undo (current-buffer))
481 (set-buffer nntp-server-buffer)
482 (goto-char (point-min))
484 (while (and (not (eobp))
485 (looking-at "2.. +\\([0-9]+\\) ")
486 (< (progn (goto-char (match-beginning 1))
487 (read (current-buffer)))
489 (search-forward "\n.\n" nil 'move))
492 (set-buffer cache-buf)
494 (insert-file-contents (gnus-cache-file-name group (car cached)))
495 (goto-char (point-min))
497 (princ (car cached) (current-buffer))
498 (insert " Article retrieved.\n")
499 (search-forward "\n\n" nil 'move)
500 (delete-region (point) (point-max))
503 (insert-buffer-substring cache-buf)
504 (setq cached (cdr cached)))
505 (kill-buffer cache-buf)))
508 (defun gnus-jog-cache ()
509 "Go through all groups and put the articles into the cache."
511 (let ((gnus-mark-article-hook nil)
513 (nnmail-spool-file nil)
514 (gnus-use-dribble-file nil)
515 (gnus-novice-user nil)
516 (gnus-large-newsgroup nil))
519 ;; Go through all groups...
520 (gnus-group-mark-buffer)
521 (gnus-group-universal-argument
524 (gnus-summary-read-group nil nil t)
525 ;; ... and enter the articles into the cache.
526 (when (eq major-mode 'gnus-summary-mode)
527 (gnus-uu-mark-buffer)
528 (gnus-cache-enter-article)
529 (kill-buffer (current-buffer)))))))
531 (defun gnus-cache-read-active (&optional force)
532 "Read the cache active file."
533 (gnus-make-directory gnus-cache-directory)
534 (if (not (and (file-exists-p gnus-cache-active-file)
535 (or force (not gnus-cache-active-hashtb))))
536 ;; There is no active file, so we generate one.
537 (gnus-cache-generate-active)
538 ;; We simply read the active file.
540 (gnus-set-work-buffer)
541 (insert-file-contents gnus-cache-active-file)
542 (gnus-active-to-gnus-format
543 nil (setq gnus-cache-active-hashtb
545 (count-lines (point-min) (point-max)))))
546 (setq gnus-cache-active-altered nil))))
548 (defun gnus-cache-write-active (&optional force)
549 "Write the active hashtb to the active file."
551 (and gnus-cache-active-hashtb
552 gnus-cache-active-altered))
554 (gnus-set-work-buffer)
557 (when (and sym (boundp sym))
558 (insert (format "%s %d %d y\n"
559 (symbol-name sym) (cdr (symbol-value sym))
560 (car (symbol-value sym))))))
561 gnus-cache-active-hashtb)
562 (gnus-make-directory (file-name-directory gnus-cache-active-file))
564 (point-min) (point-max) gnus-cache-active-file nil 'silent))
565 ;; Mark the active hashtb as unaltered.
566 (setq gnus-cache-active-altered nil)))
568 (defun gnus-cache-update-active (group number &optional low)
569 "Update the upper bound of the active info of GROUP to NUMBER.
570 If LOW, update the lower bound instead."
571 (let ((active (gnus-gethash group gnus-cache-active-hashtb)))
573 ;; We just create a new active entry for this group.
574 (gnus-sethash group (cons number number) gnus-cache-active-hashtb)
575 ;; Update the lower or upper bound.
577 (setcar active number)
578 (setcdr active number))
579 ;; Mark the active hashtb as altered.
580 (setq gnus-cache-active-altered t))))
583 (defun gnus-cache-generate-active (&optional directory)
584 "Generate the cache active file."
586 (let* ((top (null directory))
587 (directory (expand-file-name (or directory gnus-cache-directory)))
588 (files (directory-files directory 'full))
593 (concat "^" (file-name-as-directory
594 (expand-file-name gnus-cache-directory)))
595 (directory-file-name directory))
596 (nnheader-replace-chars-in-string
597 (substring (directory-file-name directory) (match-end 0))
601 (gnus-message 5 "Generating the cache active file...")
602 (setq gnus-cache-active-hashtb (gnus-make-hashtable 123)))
603 ;; Separate articles from all other files and directories.
605 (if (string-match "^[0-9]+$" (file-name-nondirectory (car files)))
606 (push (string-to-int (file-name-nondirectory (pop files))) nums)
607 (push (pop files) alphs)))
608 ;; If we have nums, then this is probably a valid group.
609 (when (setq nums (sort nums '<))
610 (gnus-sethash group (cons (car nums) (gnus-last-element nums))
611 gnus-cache-active-hashtb))
612 ;; Go through all the other files.
614 (when (and (file-directory-p (car alphs))
615 (not (string-match "^\\.\\.?$"
616 (file-name-nondirectory (car alphs)))))
617 ;; We descend directories.
618 (gnus-cache-generate-active (car alphs)))
619 (setq alphs (cdr alphs)))
620 ;; Write the new active file.
622 (gnus-cache-write-active t)
623 (gnus-message 5 "Generating the cache active file...done"))))
626 (defun gnus-cache-generate-nov-databases (dir)
627 "Generate NOV files recursively starting in DIR."
628 (interactive (list gnus-cache-directory))
630 (let ((nnml-generate-active-function 'identity))
631 (nnml-generate-nov-databases-1 dir)))
633 (provide 'gnus-cache)
635 ;;; gnus-cache.el ends here