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.
29 (eval-when-compile (require 'cl))
31 (defvar gnus-cache-directory
32 (concat (file-name-as-directory gnus-article-save-directory) "cache/")
33 "*The directory where cached articles will be stored.")
35 (defvar gnus-cache-active-file
36 (concat (file-name-as-directory gnus-cache-directory) "active")
37 "*The cache active file.")
39 (defvar gnus-cache-enter-articles '(ticked dormant)
40 "*Classes of articles to enter into the cache.")
42 (defvar gnus-cache-remove-articles '(read)
43 "*Classes of articles to remove from the cache.")
45 (defvar gnus-uncacheable-groups nil
46 "*Groups that match this regexp will not be cached.
48 If you want to avoid caching your nnml groups, you could set this
49 variable to \"^nnml\".")
53 ;;; Internal variables.
55 (defvar gnus-cache-buffer nil)
56 (defvar gnus-cache-active-hashtb nil)
57 (defvar gnus-cache-active-altered nil)
60 (autoload 'nnml-generate-nov-databases-1 "nnml")
61 (autoload 'nnvirtual-find-group-art "nnvirtual"))
65 ;;; Functions called from Gnus.
67 (defun gnus-cache-open ()
68 "Initialize the cache."
69 (gnus-cache-read-active))
71 (defun gnus-cache-close ()
72 "Shut down the cache."
73 (gnus-cache-write-active)
74 (gnus-cache-save-buffers)
75 (setq gnus-cache-active-hashtb nil))
77 (defun gnus-cache-save-buffers ()
78 ;; save the overview buffer if it exists and has been modified
79 ;; delete empty cache subdirectories
80 (if (null gnus-cache-buffer)
82 (let ((buffer (cdr gnus-cache-buffer))
83 (overview-file (gnus-cache-file-name
84 (car gnus-cache-buffer) ".overview")))
85 ;; write the overview only if it was modified
86 (if (buffer-modified-p buffer)
89 (if (> (buffer-size) 0)
90 ;; non-empty overview, write it out
92 (gnus-make-directory (file-name-directory overview-file))
93 (write-region (point-min) (point-max)
94 overview-file nil 'quietly))
95 ;; empty overview file, remove it
96 (and (file-exists-p overview-file)
97 (delete-file overview-file))
98 ;; if possible, remove group's cache subdirectory
100 ;; FIXME: we can detect the error type and warn the user
101 ;; of any inconsistencies (articles w/o nov entries?).
102 ;; for now, just be conservative...delete only if safe -- sj
103 (delete-directory (file-name-directory overview-file))
105 ;; kill the buffer, it's either unmodified or saved
106 (gnus-kill-buffer buffer)
107 (setq gnus-cache-buffer nil))))
109 (defun gnus-cache-possibly-enter-article
110 (group article headers ticked dormant unread &optional force)
111 (when (and (or force (not (eq gnus-use-cache 'passive)))
112 (vectorp headers)) ; This might be a dummy article.
113 ;; If this is a virtual group, we find the real group.
114 (when (gnus-virtual-group-p group)
115 (let ((result (nnvirtual-find-group-art group article)))
116 (setq group (car result)
118 headers (copy-sequence headers))
119 (aset headers 0 article)))
120 (let ((number (mail-header-number headers))
122 (when (and (> number 0) ; Reffed article.
123 (or (not gnus-uncacheable-groups)
124 (not (string-match gnus-uncacheable-groups group)))
126 (gnus-cache-member-of-class
127 gnus-cache-enter-articles ticked dormant unread))
128 (not (file-exists-p (setq file (gnus-cache-file-name
130 ;; Possibly create the cache directory.
131 (or (file-exists-p (setq dir (file-name-directory file)))
132 (gnus-make-directory dir))
133 ;; Save the article in the cache.
134 (if (file-exists-p file)
135 t ; The article already is saved.
136 (let ((gnus-use-cache nil))
137 (gnus-summary-select-article))
139 (set-buffer gnus-original-article-buffer)
140 (when (> (buffer-size) 0)
141 (write-region (point-min) (point-max) file nil 'quiet)
142 (gnus-cache-change-buffer group)
143 (set-buffer (cdr gnus-cache-buffer))
144 (goto-char (point-max))
146 (while (condition-case ()
148 (> (read (current-buffer)) number))
150 ;; The line was malformed, so we just remove it!!
158 (if (< (read (current-buffer)) number)
163 ;; [number subject from date id references chars lines xref]
164 (insert (format "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t\n"
165 (mail-header-number headers)
166 (mail-header-subject headers)
167 (mail-header-from headers)
168 (mail-header-date headers)
169 (mail-header-id headers)
170 (or (mail-header-references headers) "")
171 (or (mail-header-chars headers) "")
172 (or (mail-header-lines headers) "")
173 (or (mail-header-xref headers) "")))
174 ;; Update the active info.
175 (set-buffer gnus-summary-buffer)
176 (gnus-cache-update-active group number)
177 (push number gnus-newsgroup-cached)
178 (gnus-summary-update-secondary-mark article))
181 (defun gnus-cache-enter-remove-article (article)
182 "Mark ARTICLE for later possible removal."
183 (push article gnus-cache-removable-articles))
185 (defun gnus-cache-possibly-remove-articles ()
186 "Possibly remove some of the removable articles."
187 (if (not (gnus-virtual-group-p gnus-newsgroup-name))
188 (gnus-cache-possibly-remove-articles-1)
189 (let ((arts gnus-cache-removable-articles)
192 (when (setq ga (nnvirtual-find-group-art
193 gnus-newsgroup-name (pop arts)))
194 (let ((gnus-cache-removable-articles (list (cdr ga)))
195 (gnus-newsgroup-name (car ga)))
196 (gnus-cache-possibly-remove-articles-1)))))
197 (setq gnus-cache-removable-articles nil)))
199 (defun gnus-cache-possibly-remove-articles-1 ()
200 "Possibly remove some of the removable articles."
201 (unless (eq gnus-use-cache 'passive)
202 (let ((articles gnus-cache-removable-articles)
203 (cache-articles gnus-newsgroup-cached)
205 (gnus-cache-change-buffer gnus-newsgroup-name)
207 (if (memq (setq article (pop articles)) cache-articles)
208 ;; The article was in the cache, so we see whether we are
209 ;; supposed to remove it from the cache.
210 (gnus-cache-possibly-remove-article
211 article (memq article gnus-newsgroup-marked)
212 (memq article gnus-newsgroup-dormant)
213 (or (memq article gnus-newsgroup-unreads)
214 (memq article gnus-newsgroup-unselected))))))
215 ;; The overview file might have been modified, save it
216 ;; safe because we're only called at group exit anyway.
217 (gnus-cache-save-buffers)))
219 (defun gnus-cache-request-article (article group)
220 "Retrieve ARTICLE in GROUP from the cache."
221 (let ((file (gnus-cache-file-name group article))
222 (buffer-read-only nil))
223 (when (file-exists-p file)
225 (gnus-kill-all-overlays)
226 (insert-file-contents file)
229 (defun gnus-cache-possibly-alter-active (group active)
230 "Alter the ACTIVE info for GROUP to reflect the articles in the cache."
231 (let ((cache-active (gnus-gethash group gnus-cache-active-hashtb)))
233 (< (car cache-active) (car active))
234 (setcar active (car cache-active)))
236 (> (cdr cache-active) (cdr active))
237 (setcdr active (cdr cache-active)))))
239 (defun gnus-cache-retrieve-headers (articles group &optional fetch-old)
240 "Retrieve the headers for ARTICLES in GROUP."
242 (setq gnus-newsgroup-cached (gnus-cache-articles-in-group group)))
243 (articles (gnus-sorted-complement articles cached))
244 (cache-file (gnus-cache-file-name group ".overview"))
246 ;; We first retrieve all the headers that we don't have in
248 (let ((gnus-use-cache nil))
249 (setq type (and articles
250 (gnus-retrieve-headers articles group fetch-old))))
251 (gnus-cache-save-buffers)
252 ;; Then we insert the cached headers.
255 ((not (file-exists-p cache-file))
256 ;; There are no cached headers.
259 ;; There were no uncached headers (or retrieval was
260 ;; unsuccessful), so we use the cached headers exclusively.
261 (set-buffer nntp-server-buffer)
263 (insert-file-contents cache-file)
266 ;; We have both cached and uncached NOV headers, so we
268 (gnus-cache-braid-nov group cached)
272 (gnus-cache-braid-heads group cached)
275 (defun gnus-cache-enter-article (&optional n)
276 "Enter the next N articles into the cache.
277 If not given a prefix, use the process marked articles instead.
278 Returns the list of articles entered."
280 (gnus-set-global-variables)
281 (let ((articles (gnus-summary-work-articles n))
284 (setq article (pop articles))
285 (when (gnus-cache-possibly-enter-article
286 gnus-newsgroup-name article (gnus-summary-article-header article)
289 (gnus-summary-remove-process-mark article)
290 (gnus-summary-update-secondary-mark article))
291 (gnus-summary-position-point)
294 (defun gnus-cache-remove-article (n)
295 "Remove the next N articles from the cache.
296 If not given a prefix, use the process marked articles instead.
297 Returns the list of articles removed."
299 (gnus-set-global-variables)
300 (let ((articles (gnus-summary-work-articles n))
303 (setq article (pop articles))
304 (when (gnus-cache-possibly-remove-article article nil nil nil t)
306 (gnus-summary-remove-process-mark article)
307 (gnus-summary-update-secondary-mark article))
308 (gnus-summary-position-point)
311 (defun gnus-cached-article-p (article)
312 "Say whether ARTICLE is cached in the current group."
313 (memq article gnus-newsgroup-cached))
315 ;;; Internal functions.
317 (defun gnus-cache-change-buffer (group)
318 (and gnus-cache-buffer
319 ;; See if the current group's overview cache has been loaded.
320 (or (string= group (car gnus-cache-buffer))
321 ;; Another overview cache is current, save it.
322 (gnus-cache-save-buffers)))
323 ;; if gnus-cache buffer is nil, create it
324 (or gnus-cache-buffer
325 ;; Create cache buffer
327 (setq gnus-cache-buffer
329 (set-buffer (get-buffer-create " *gnus-cache-overview*"))))
330 (buffer-disable-undo (current-buffer))
331 ;; Insert the contents of this group's cache overview.
333 (let ((file (gnus-cache-file-name group ".overview")))
334 (and (file-exists-p file)
335 (insert-file-contents file)))
336 ;; We have a fresh (empty/just loaded) buffer,
337 ;; mark it as unmodified to save a redundant write later.
338 (set-buffer-modified-p nil))))
340 ;; Return whether an article is a member of a class.
341 (defun gnus-cache-member-of-class (class ticked dormant unread)
342 (or (and ticked (memq 'ticked class))
343 (and dormant (memq 'dormant class))
344 (and unread (memq 'unread class))
345 (and (not unread) (memq 'read class))))
347 (defun gnus-cache-file-name (group article)
348 (concat (file-name-as-directory gnus-cache-directory)
349 (file-name-as-directory
350 (if (gnus-use-long-file-name 'not-cache)
352 (let ((group (concat group "")))
353 (if (string-match ":" group)
354 (aset group (match-beginning 0) ?/))
355 (nnheader-replace-chars-in-string group ?. ?/))))
356 (if (stringp article) article (int-to-string article))))
358 (defun gnus-cache-possibly-remove-article
359 (article ticked dormant unread &optional force)
360 "Possibly remove ARTICLE from the cache."
361 (let ((file (gnus-cache-file-name gnus-newsgroup-name article)))
362 (when (and (file-exists-p file)
364 (gnus-cache-member-of-class
365 gnus-cache-remove-articles ticked dormant unread)))
368 (set-buffer (cdr gnus-cache-buffer))
369 (goto-char (point-min))
370 (if (or (looking-at (concat (int-to-string article) "\t"))
371 (search-forward (concat "\n" (int-to-string article) "\t")
373 (delete-region (progn (beginning-of-line) (point))
374 (progn (forward-line 1) (point)))))
375 (setq gnus-newsgroup-cached
376 (delq article gnus-newsgroup-cached))
377 (gnus-summary-update-secondary-mark article)
380 (defun gnus-cache-articles-in-group (group)
381 "Return a sorted list of cached articles in GROUP."
382 (let ((dir (file-name-directory (gnus-cache-file-name group 1)))
384 (when (file-exists-p dir)
385 (sort (mapcar (lambda (name) (string-to-int name))
386 (directory-files dir nil "^[0-9]+$" t))
389 (defun gnus-cache-braid-nov (group cached)
390 (let ((cache-buf (get-buffer-create " *gnus-cache*"))
392 (gnus-cache-save-buffers)
394 (set-buffer cache-buf)
395 (buffer-disable-undo (current-buffer))
397 (insert-file-contents (gnus-cache-file-name group ".overview"))
398 (goto-char (point-min))
400 (goto-char (point-min)))
401 (set-buffer nntp-server-buffer)
402 (goto-char (point-min))
404 (while (and (not (eobp))
405 (< (read (current-buffer)) (car cached)))
409 (set-buffer cache-buf)
410 (if (search-forward (concat "\n" (int-to-string (car cached)) "\t")
412 (setq beg (progn (beginning-of-line) (point))
413 end (progn (end-of-line) (point)))
415 (if beg (progn (insert-buffer-substring cache-buf beg end)
417 (setq cached (cdr cached)))
418 (kill-buffer cache-buf)))
420 (defun gnus-cache-braid-heads (group cached)
421 (let ((cache-buf (get-buffer-create " *gnus-cache*")))
423 (set-buffer cache-buf)
424 (buffer-disable-undo (current-buffer))
426 (set-buffer nntp-server-buffer)
427 (goto-char (point-min))
429 (while (and (not (eobp))
430 (looking-at "2.. +\\([0-9]+\\) ")
431 (< (progn (goto-char (match-beginning 1))
432 (read (current-buffer)))
434 (search-forward "\n.\n" nil 'move))
437 (set-buffer cache-buf)
439 (insert-file-contents (gnus-cache-file-name group (car cached)))
440 (goto-char (point-min))
441 (insert "220 " (int-to-string (car cached)) " Article retrieved.\n")
442 (search-forward "\n\n" nil 'move)
443 (delete-region (point) (point-max))
446 (insert-buffer-substring cache-buf)
447 (setq cached (cdr cached)))
448 (kill-buffer cache-buf)))
451 (defun gnus-jog-cache ()
452 "Go through all groups and put the articles into the cache."
454 (let ((gnus-mark-article-hook nil)
456 (nnmail-spool-file nil)
457 (gnus-use-dribble-file nil)
458 (gnus-novice-user nil)
459 (gnus-large-newsgroup nil))
462 ;; Go through all groups...
463 (gnus-group-mark-buffer)
464 (gnus-group-universal-argument
467 (gnus-summary-read-group nil nil t)
468 ;; ... and enter the articles into the cache.
469 (when (eq major-mode 'gnus-summary-mode)
470 (gnus-uu-mark-buffer)
471 (gnus-cache-enter-article)
472 (kill-buffer (current-buffer)))))))
474 (defun gnus-cache-read-active (&optional force)
475 "Read the cache active file."
476 (if (not (and (file-exists-p gnus-cache-active-file)
477 (or force (not gnus-cache-active-hashtb))))
478 ;; There is no active file, so we generate one.
479 (gnus-cache-generate-active)
480 ;; We simply read the active file.
482 (gnus-set-work-buffer)
483 (insert-file-contents gnus-cache-active-file)
484 (gnus-active-to-gnus-format
485 nil (setq gnus-cache-active-hashtb
487 (count-lines (point-min) (point-max)))))
488 (setq gnus-cache-active-altered nil))))
490 (defun gnus-cache-write-active (&optional force)
491 "Write the active hashtb to the active file."
493 (and gnus-cache-active-hashtb
494 gnus-cache-active-altered))
496 (gnus-set-work-buffer)
499 (when (and sym (boundp sym))
500 (insert (format "%s %d %d y\n"
501 (symbol-name sym) (cdr (symbol-value sym))
502 (car (symbol-value sym))))))
503 gnus-cache-active-hashtb)
504 (gnus-make-directory (file-name-directory gnus-cache-active-file))
506 (point-min) (point-max) gnus-cache-active-file nil 'silent))
507 ;; Mark the active hashtb as unaltered.
508 (setq gnus-cache-active-altered nil)))
510 (defun gnus-cache-update-active (group number &optional low)
511 "Update the upper bound of the active info of GROUP to NUMBER.
512 If LOW, update the lower bound instead."
513 (let ((active (gnus-gethash group gnus-cache-active-hashtb)))
515 ;; We just create a new active entry for this group.
516 (gnus-sethash group (cons number number) gnus-cache-active-hashtb)
517 ;; Update the lower or upper bound.
519 (setcar active number)
520 (setcdr active number))
521 ;; Mark the active hashtb as altered.
522 (setq gnus-cache-active-altered t))))
525 (defun gnus-cache-generate-active (&optional directory)
526 "Generate the cache active file."
528 (let* ((top (null directory))
529 (directory (expand-file-name (or directory gnus-cache-directory)))
530 (files (directory-files directory 'full))
535 (concat "^" (file-name-as-directory
536 (expand-file-name gnus-cache-directory)))
537 (directory-file-name directory))
538 (nnheader-replace-chars-in-string
539 (substring (directory-file-name directory) (match-end 0))
543 (gnus-message 5 "Generating the cache active file...")
544 (setq gnus-cache-active-hashtb (gnus-make-hashtable 123)))
545 ;; Separate articles from all other files and directories.
547 (if (string-match "^[0-9]+$" (file-name-nondirectory (car files)))
548 (push (string-to-int (file-name-nondirectory (pop files))) nums)
549 (push (pop files) alphs)))
550 ;; If we have nums, then this is probably a valid group.
551 (setq nums (sort nums '<))
553 (gnus-sethash group (cons (car nums) (gnus-last-element nums))
554 gnus-cache-active-hashtb))
555 ;; Go through all the other files.
557 (when (and (file-directory-p (car alphs))
558 (not (string-match "^\\.\\.?$"
559 (file-name-nondirectory (car alphs)))))
560 ;; We descend directories.
561 (gnus-cache-generate-active (car alphs)))
562 (setq alphs (cdr alphs)))
563 ;; Write the new active file.
565 (gnus-cache-write-active t)
566 (gnus-message 5 "Generating the cache active file...done"))))
569 (defun gnus-cache-generate-nov-databases (dir)
570 "Generate NOV files recursively starting in DIR."
571 (interactive (list gnus-cache-directory))
573 (let ((nnml-generate-active-function 'identity))
574 (nnml-generate-nov-databases-1 dir)))
576 (provide 'gnus-cache)
578 ;;; gnus-cache.el ends here