1 ;;; gnus-cache.el --- cache interface for Gnus
2 ;; Copyright (C) 1995,96,97 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 (defgroup gnus-cache nil
39 (defcustom gnus-cache-directory
40 (nnheader-concat gnus-directory
"cache/")
41 "*The directory where cached articles will be stored."
45 (defcustom gnus-cache-active-file
46 (concat (file-name-as-directory gnus-cache-directory
) "active")
47 "*The cache active file."
51 (defcustom gnus-cache-enter-articles
'(ticked dormant
)
52 "Classes of articles to enter into the cache."
54 :type
'(set (const ticked
) (const dormant
) (const unread
) (const read
)))
56 (defcustom gnus-cache-remove-articles
'(read)
57 "Classes of articles to remove from the cache."
59 :type
'(set (const ticked
) (const dormant
) (const unread
) (const read
)))
61 (defcustom gnus-uncacheable-groups nil
62 "*Groups that match this regexp will not be cached.
64 If you want to avoid caching your nnml groups, you could set this
65 variable to \"^nnml\"."
67 :type
'(choice (const :tag
"off" nil
)
72 ;;; Internal variables.
74 (defvar gnus-cache-removable-articles nil
)
75 (defvar gnus-cache-buffer nil
)
76 (defvar gnus-cache-active-hashtb nil
)
77 (defvar gnus-cache-active-altered nil
)
80 (autoload 'nnml-generate-nov-databases-1
"nnml")
81 (autoload 'nnvirtual-find-group-art
"nnvirtual"))
85 ;;; Functions called from Gnus.
87 (defun gnus-cache-open ()
88 "Initialize the cache."
89 (when (or (file-exists-p gnus-cache-directory
)
91 (not (eq gnus-use-cache
'passive
))))
92 (gnus-cache-read-active)))
94 ;; Complexities of byte-compiling make this kludge necessary. Eeek.
96 (gnus-add-shutdown 'gnus-cache-close
'gnus
))
98 (defun gnus-cache-close ()
99 "Shut down the cache."
100 (gnus-cache-write-active)
101 (gnus-cache-save-buffers)
102 (setq gnus-cache-active-hashtb nil
))
104 (defun gnus-cache-save-buffers ()
105 ;; save the overview buffer if it exists and has been modified
106 ;; delete empty cache subdirectories
107 (when gnus-cache-buffer
108 (let ((buffer (cdr gnus-cache-buffer
))
109 (overview-file (gnus-cache-file-name
110 (car gnus-cache-buffer
) ".overview")))
111 ;; write the overview only if it was modified
112 (when (buffer-modified-p buffer
)
115 (if (> (buffer-size) 0)
116 ;; Non-empty overview, write it to a file.
117 (gnus-write-buffer overview-file
)
118 ;; Empty overview file, remove it
119 (when (file-exists-p overview-file
)
120 (delete-file overview-file
))
121 ;; If possible, remove group's cache subdirectory.
123 ;; FIXME: we can detect the error type and warn the user
124 ;; of any inconsistencies (articles w/o nov entries?).
125 ;; for now, just be conservative...delete only if safe -- sj
126 (delete-directory (file-name-directory overview-file
))
128 ;; Kill the buffer -- it's either unmodified or saved.
129 (gnus-kill-buffer buffer
)
130 (setq gnus-cache-buffer nil
))))
132 (defun gnus-cache-possibly-enter-article
133 (group article headers ticked dormant unread
&optional force
)
134 (when (and (or force
(not (eq gnus-use-cache
'passive
)))
137 (vectorp headers
)) ; This might be a dummy article.
138 ;; If this is a virtual group, we find the real group.
139 (when (gnus-virtual-group-p group
)
140 (let ((result (nnvirtual-find-group-art
141 (gnus-group-real-name group
) article
)))
142 (setq group
(car result
)
143 headers
(copy-sequence headers
))
144 (mail-header-set-number headers
(cdr result
))))
145 (let ((number (mail-header-number headers
))
147 (when (and (> number
0) ; Reffed article.
149 (and (or (not gnus-uncacheable-groups
)
151 gnus-uncacheable-groups group
)))
152 (gnus-cache-member-of-class
153 gnus-cache-enter-articles ticked dormant unread
)))
154 (not (file-exists-p (setq file
(gnus-cache-file-name
156 ;; Possibly create the cache directory.
157 (gnus-make-directory (setq dir
(file-name-directory file
)))
158 ;; Save the article in the cache.
159 (if (file-exists-p file
)
160 t
; The article already is saved.
162 (set-buffer nntp-server-buffer
)
163 (let ((gnus-use-cache nil
))
164 (gnus-request-article-this-buffer number group
))
165 (when (> (buffer-size) 0)
166 (gnus-write-buffer file
)
167 (gnus-cache-change-buffer group
)
168 (set-buffer (cdr gnus-cache-buffer
))
169 (goto-char (point-max))
171 (while (condition-case ()
173 (> (read (current-buffer)) number
))
175 ;; The line was malformed, so we just remove it!!
183 (when (< (read (current-buffer)) number
)
188 ;; [number subject from date id references chars lines xref]
189 (insert (format "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t\n"
190 (mail-header-number headers
)
191 (mail-header-subject headers
)
192 (mail-header-from headers
)
193 (mail-header-date headers
)
194 (mail-header-id headers
)
195 (or (mail-header-references headers
) "")
196 (or (mail-header-chars headers
) "")
197 (or (mail-header-lines headers
) "")
198 (or (mail-header-xref headers
) "")))
199 ;; Update the active info.
200 (set-buffer gnus-summary-buffer
)
201 (gnus-cache-update-active group number
)
202 (push article gnus-newsgroup-cached
)
203 (gnus-summary-update-secondary-mark article
))
206 (defun gnus-cache-enter-remove-article (article)
207 "Mark ARTICLE for later possible removal."
209 (push article gnus-cache-removable-articles
)))
211 (defun gnus-cache-possibly-remove-articles ()
212 "Possibly remove some of the removable articles."
213 (if (not (gnus-virtual-group-p gnus-newsgroup-name
))
214 (gnus-cache-possibly-remove-articles-1)
215 (let ((arts gnus-cache-removable-articles
)
218 (when (setq ga
(nnvirtual-find-group-art
219 (gnus-group-real-name gnus-newsgroup-name
) (pop arts
)))
220 (let ((gnus-cache-removable-articles (list (cdr ga
)))
221 (gnus-newsgroup-name (car ga
)))
222 (gnus-cache-possibly-remove-articles-1)))))
223 (setq gnus-cache-removable-articles nil
)))
225 (defun gnus-cache-possibly-remove-articles-1 ()
226 "Possibly remove some of the removable articles."
227 (unless (eq gnus-use-cache
'passive
)
228 (let ((articles gnus-cache-removable-articles
)
229 (cache-articles gnus-newsgroup-cached
)
231 (gnus-cache-change-buffer gnus-newsgroup-name
)
233 (when (memq (setq article
(pop articles
)) cache-articles
)
234 ;; The article was in the cache, so we see whether we are
235 ;; supposed to remove it from the cache.
236 (gnus-cache-possibly-remove-article
237 article
(memq article gnus-newsgroup-marked
)
238 (memq article gnus-newsgroup-dormant
)
239 (or (memq article gnus-newsgroup-unreads
)
240 (memq article gnus-newsgroup-unselected
))))))
241 ;; The overview file might have been modified, save it
242 ;; safe because we're only called at group exit anyway.
243 (gnus-cache-save-buffers)))
245 (defun gnus-cache-request-article (article group
)
246 "Retrieve ARTICLE in GROUP from the cache."
247 (let ((file (gnus-cache-file-name group article
))
248 (buffer-read-only nil
))
249 (when (file-exists-p file
)
251 (gnus-kill-all-overlays)
252 (insert-file-contents file
)
255 (defun gnus-cache-possibly-alter-active (group active
)
256 "Alter the ACTIVE info for GROUP to reflect the articles in the cache."
257 (when (equal group
"no.norsk") (error "hie"))
258 (when gnus-cache-active-hashtb
259 (let ((cache-active (gnus-gethash group gnus-cache-active-hashtb
)))
261 (< (car cache-active
) (car active
))
262 (setcar active
(car cache-active
)))
264 (> (cdr cache-active
) (cdr active
))
265 (setcdr active
(cdr cache-active
))))))
267 (defun gnus-cache-retrieve-headers (articles group
&optional fetch-old
)
268 "Retrieve the headers for ARTICLES in GROUP."
270 (setq gnus-newsgroup-cached
(gnus-cache-articles-in-group group
))))
272 ;; No cached articles here, so we just retrieve them
274 (let ((gnus-use-cache nil
))
275 (gnus-retrieve-headers articles group fetch-old
))
276 (let ((uncached-articles (gnus-sorted-intersection
277 (gnus-sorted-complement articles cached
)
279 (cache-file (gnus-cache-file-name group
".overview"))
281 ;; We first retrieve all the headers that we don't have in
283 (let ((gnus-use-cache nil
))
284 (when uncached-articles
285 (setq type
(and articles
286 (gnus-retrieve-headers
287 uncached-articles group fetch-old
)))))
288 (gnus-cache-save-buffers)
289 ;; Then we insert the cached headers.
292 ((not (file-exists-p cache-file
))
293 ;; There are no cached headers.
296 ;; There were no uncached headers (or retrieval was
297 ;; unsuccessful), so we use the cached headers exclusively.
298 (set-buffer nntp-server-buffer
)
300 (insert-file-contents cache-file
)
303 ;; We have both cached and uncached NOV headers, so we
305 (gnus-cache-braid-nov group cached
)
309 (gnus-cache-braid-heads group
(gnus-sorted-intersection
313 (defun gnus-cache-enter-article (&optional n
)
314 "Enter the next N articles into the cache.
315 If not given a prefix, use the process marked articles instead.
316 Returns the list of articles entered."
318 (gnus-set-global-variables)
319 (let ((articles (gnus-summary-work-articles n
))
321 (while (setq article
(pop articles
))
322 (if (natnump article
)
323 (when (gnus-cache-possibly-enter-article
324 gnus-newsgroup-name article
325 (gnus-summary-article-header article
)
328 (gnus-message 2 "Can't cache article %d" article
))
329 (gnus-summary-remove-process-mark article
)
330 (gnus-summary-update-secondary-mark article
))
331 (gnus-summary-next-subject 1)
332 (gnus-summary-position-point)
335 (defun gnus-cache-remove-article (n)
336 "Remove the next N articles from the cache.
337 If not given a prefix, use the process marked articles instead.
338 Returns the list of articles removed."
340 (gnus-set-global-variables)
341 (gnus-cache-change-buffer gnus-newsgroup-name
)
342 (let ((articles (gnus-summary-work-articles n
))
345 (setq article
(pop articles
))
346 (when (gnus-cache-possibly-remove-article article nil nil nil t
)
348 (gnus-summary-remove-process-mark article
)
349 (gnus-summary-update-secondary-mark article
))
350 (gnus-summary-next-subject 1)
351 (gnus-summary-position-point)
354 (defun gnus-cached-article-p (article)
355 "Say whether ARTICLE is cached in the current group."
356 (memq article gnus-newsgroup-cached
))
358 (defun gnus-summary-insert-cached-articles ()
359 "Insert all the articles cached for this group into the current buffer."
361 (let ((cached gnus-newsgroup-cached
)
362 (gnus-verbose (max 6 gnus-verbose
)))
364 (error "No cached articles for this group"))
366 (gnus-summary-goto-subject (pop cached
) t
))))
368 ;;; Internal functions.
370 (defun gnus-cache-change-buffer (group)
371 (and gnus-cache-buffer
372 ;; See if the current group's overview cache has been loaded.
373 (or (string= group
(car gnus-cache-buffer
))
374 ;; Another overview cache is current, save it.
375 (gnus-cache-save-buffers)))
376 ;; if gnus-cache buffer is nil, create it
377 (unless gnus-cache-buffer
378 ;; Create cache buffer
380 (setq gnus-cache-buffer
382 (set-buffer (get-buffer-create " *gnus-cache-overview*"))))
383 (buffer-disable-undo (current-buffer))
384 ;; Insert the contents of this group's cache overview.
386 (let ((file (gnus-cache-file-name group
".overview")))
387 (when (file-exists-p file
)
388 (nnheader-insert-file-contents file
)))
389 ;; We have a fresh (empty/just loaded) buffer,
390 ;; mark it as unmodified to save a redundant write later.
391 (set-buffer-modified-p nil
))))
393 ;; Return whether an article is a member of a class.
394 (defun gnus-cache-member-of-class (class ticked dormant unread
)
395 (or (and ticked
(memq 'ticked class
))
396 (and dormant
(memq 'dormant class
))
397 (and unread
(memq 'unread class
))
398 (and (not unread
) (not ticked
) (not dormant
) (memq 'read class
))))
400 (defun gnus-cache-file-name (group article
)
401 (concat (file-name-as-directory gnus-cache-directory
)
402 (file-name-as-directory
403 (nnheader-translate-file-chars
404 (if (gnus-use-long-file-name 'not-cache
)
406 (let ((group (nnheader-replace-chars-in-string group ?
/ ?_
)))
407 ;; Translate the first colon into a slash.
408 (when (string-match ":" group
)
409 (aset group
(match-beginning 0) ?
/))
410 (nnheader-replace-chars-in-string group ?. ?
/)))))
411 (if (stringp article
) article
(int-to-string article
))))
413 (defun gnus-cache-update-article (group article
)
414 "If ARTICLE is in the cache, remove it and re-enter it."
415 (when (gnus-cache-possibly-remove-article article nil nil nil t
)
416 (let ((gnus-use-cache nil
))
417 (gnus-cache-possibly-enter-article
418 gnus-newsgroup-name article
(gnus-summary-article-header article
)
421 (defun gnus-cache-possibly-remove-article (article ticked dormant unread
423 "Possibly remove ARTICLE from the cache."
424 (let ((group gnus-newsgroup-name
)
427 ;; If this is a virtual group, we find the real group.
428 (when (gnus-virtual-group-p group
)
429 (let ((result (nnvirtual-find-group-art
430 (gnus-group-real-name group
) article
)))
431 (setq group
(car result
)
432 number
(cdr result
))))
433 (setq file
(gnus-cache-file-name group number
))
434 (when (and (file-exists-p file
)
436 (gnus-cache-member-of-class
437 gnus-cache-remove-articles ticked dormant unread
)))
440 (set-buffer (cdr gnus-cache-buffer
))
441 (goto-char (point-min))
442 (when (or (looking-at (concat (int-to-string number
) "\t"))
443 (search-forward (concat "\n" (int-to-string number
) "\t")
445 (delete-region (progn (beginning-of-line) (point))
446 (progn (forward-line 1) (point)))))
447 (setq gnus-newsgroup-cached
448 (delq article gnus-newsgroup-cached
))
449 (gnus-summary-update-secondary-mark article
)
452 (defun gnus-cache-articles-in-group (group)
453 "Return a sorted list of cached articles in GROUP."
454 (let ((dir (file-name-directory (gnus-cache-file-name group
1))))
455 (when (file-exists-p dir
)
456 (sort (mapcar (lambda (name) (string-to-int name
))
457 (directory-files dir nil
"^[0-9]+$" t
))
460 (defun gnus-cache-braid-nov (group cached
)
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 (insert-file-contents (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 (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-universal-argument
545 (gnus-summary-read-group (gnus-group-group-name) nil t
)
546 ;; ... and enter the articles into the cache.
547 (when (eq major-mode
'gnus-summary-mode
)
548 (gnus-uu-mark-buffer)
549 (gnus-cache-enter-article)
550 (kill-buffer (current-buffer)))))))
552 (defun gnus-cache-read-active (&optional force
)
553 "Read the cache active file."
554 (gnus-make-directory gnus-cache-directory
)
555 (if (not (and (file-exists-p gnus-cache-active-file
)
556 (or force
(not gnus-cache-active-hashtb
))))
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 (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 "DMove the cache tree to: ")
653 (rename-file gnus-cache-directory dir
))
655 (provide 'gnus-cache
)
657 ;;; gnus-cache.el ends here