1 ;;; gnus-cache.el --- cache interface for Gnus
2 ;; Copyright (C) 1995 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
21 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
29 (defvar gnus-cache-directory (concat gnus-article-save-directory "cache/")
30 "*The directory where cached articles will be stored.")
32 (defvar gnus-cache-enter-articles '(ticked dormant)
33 "*Classes of articles to enter into the cache.")
35 (defvar gnus-cache-remove-articles '(read)
36 "*Classes of articles to remove from the cache.")
38 (defvar gnus-uncacheable-groups "^nnvirtual"
39 "*Groups that match this regexp will not be cached.
41 If you want to avoid caching your nnml groups, you could set this
42 variable to \"^nnml\".")
46 (defvar gnus-cache-buffer nil)
50 (defun gnus-cache-change-buffer (group)
51 (and gnus-cache-buffer
52 ;; see if the current group's overview cache has been loaded
53 (or (string= group (car gnus-cache-buffer))
54 ;; another overview cache is current, save it
55 (gnus-cache-save-buffers)))
56 ;; if gnus-cache buffer is nil, create it
58 ;; create cache buffer
60 (setq gnus-cache-buffer
62 (set-buffer (get-buffer-create " *gnus-cache-overview*"))))
63 (buffer-disable-undo (current-buffer))
64 ;; insert the contents of this groups cache overview
66 (let ((file (gnus-cache-file-name group ".overview")))
67 (and (file-exists-p file)
68 (insert-file-contents file)))
69 ;; we have a fresh (empty/just loaded) buffer,
70 ;; mark it as unmodified to save a redundant write later.
71 (set-buffer-modified-p nil))))
74 (defun gnus-cache-save-buffers ()
75 ;; save the overview buffer if it exists and has been modified
76 ;; delete empty cache subdirectories
77 (if (null gnus-cache-buffer)
79 (let ((buffer (cdr gnus-cache-buffer))
80 (overview-file (gnus-cache-file-name
81 (car gnus-cache-buffer) ".overview")))
82 ;; write the overview only if it was modified
83 (if (buffer-modified-p buffer)
86 (if (> (buffer-size) 0)
87 ;; non-empty overview, write it out
89 (gnus-make-directory (file-name-directory overview-file))
90 (write-region (point-min) (point-max)
91 overview-file nil 'quietly))
92 ;; empty overview file, remove it
93 (and (file-exists-p overview-file)
94 (delete-file overview-file))
95 ;; if possible, remove group's cache subdirectory
97 ;; FIXME: we can detect the error type and warn the user
98 ;; of any inconsistencies (articles w/o nov entries?).
99 ;; for now, just be conservative...delete only if safe -- sj
100 (delete-directory (file-name-directory overview-file))
102 ;; kill the buffer, it's either unmodified or saved
103 (gnus-kill-buffer buffer)
104 (setq gnus-cache-buffer nil))))
107 ;; Return whether an article is a member of a class.
108 (defun gnus-cache-member-of-class (class ticked dormant unread)
109 (or (and ticked (memq 'ticked class))
110 (and dormant (memq 'dormant class))
111 (and unread (memq 'unread class))
112 (and (not unread) (memq 'read class))))
114 (defun gnus-cache-file-name (group article)
115 (concat (file-name-as-directory gnus-cache-directory)
116 (if (gnus-use-long-file-name 'not-cache)
118 (let ((group (concat group "")))
119 (if (string-match ":" group)
120 (aset group (match-beginning 0) ?/))
121 (gnus-replace-chars-in-string group ?. ?/)))
122 "/" (if (stringp article) article (int-to-string article))))
124 (defun gnus-cache-possibly-enter-article
125 (group article headers ticked dormant unread)
126 (let ((number (mail-header-number headers))
128 (if (or (not (vectorp headers)) ; This might be a dummy article.
129 (< number 0) ; Reffed article.
130 (and gnus-uncacheable-groups
131 (string-match gnus-uncacheable-groups group))
132 (not (gnus-cache-member-of-class
133 gnus-cache-enter-articles ticked dormant unread))
134 (file-exists-p (setq file (gnus-cache-file-name group article))))
136 ;; Possibly create the cache directory.
137 (or (file-exists-p (setq dir (file-name-directory file)))
138 (gnus-make-directory dir))
139 ;; Save the article in the cache.
140 (if (file-exists-p file)
141 t ; The article already is saved.
142 (let ((gnus-use-cache nil))
143 (gnus-summary-select-article))
145 (set-buffer gnus-article-buffer)
148 (write-region (point-min) (point-max) file nil 'quiet))
149 (gnus-cache-change-buffer group)
150 (set-buffer (cdr gnus-cache-buffer))
151 (goto-char (point-max))
153 (while (condition-case ()
155 (> (read (current-buffer)) number))
157 ;; The line was malformed, so we just remove it!!
165 (if (< (read (current-buffer)) number)
170 ;; [number subject from date id references chars lines xref]
171 (insert (format "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t\n"
172 (mail-header-number headers)
173 (mail-header-subject headers)
174 (mail-header-from headers)
175 (mail-header-date headers)
176 (mail-header-id headers)
177 (or (mail-header-references headers) "")
178 (or (mail-header-chars headers) "")
179 (or (mail-header-lines headers) "")
180 (or (mail-header-xref headers) ""))))
183 (defun gnus-cache-enter-remove-article (article)
184 (setq gnus-cache-removeable-articles
185 (cons article gnus-cache-removeable-articles)))
187 (defsubst gnus-cache-possibly-remove-article
188 (article ticked dormant unread)
189 (let ((file (gnus-cache-file-name gnus-newsgroup-name article)))
190 (if (or (not (file-exists-p file))
191 (not (gnus-cache-member-of-class
192 gnus-cache-remove-articles ticked dormant unread)))
196 (set-buffer (cdr gnus-cache-buffer))
197 (goto-char (point-min))
198 (if (or (looking-at (concat (int-to-string article) "\t"))
199 (search-forward (concat "\n" (int-to-string article) "\t")
201 (delete-region (progn (beginning-of-line) (point))
202 (progn (forward-line 1) (point))))))))
204 (defun gnus-cache-possibly-remove-articles ()
205 (let ((articles gnus-cache-removeable-articles)
206 (cache-articles (gnus-cache-articles-in-group gnus-newsgroup-name))
208 (gnus-cache-change-buffer gnus-newsgroup-name)
210 (setq article (car articles)
211 articles (cdr articles))
212 (if (memq article cache-articles)
213 ;; The article was in the cache, so we see whether we are
214 ;; supposed to remove it from the cache.
215 (gnus-cache-possibly-remove-article
216 article (memq article gnus-newsgroup-marked)
217 (memq article gnus-newsgroup-dormant)
218 (or (memq article gnus-newsgroup-unreads)
219 (memq article gnus-newsgroup-unselected))))))
220 ;; the overview file might have been modified, save it
221 ;; safe because we're only called at group exit anyway
222 (gnus-cache-save-buffers))
225 (defun gnus-cache-request-article (article group)
226 (let ((file (gnus-cache-file-name group article)))
227 (if (not (file-exists-p file))
230 ;; There may be some overlays that we have to kill...
232 (let ((overlays (and (fboundp 'overlays-at)
233 (overlays-at (point-min)))))
235 (delete-overlay (car overlays))
236 (setq overlays (cdr overlays))))
238 (insert-file-contents file)
241 (defun gnus-cache-articles-in-group (group)
242 (let ((dir (file-name-directory (gnus-cache-file-name group 1)))
244 (if (not (file-exists-p dir))
246 (setq articles (directory-files dir nil "^[0-9]+$" t))
249 (sort (mapcar (function (lambda (name)
250 (string-to-int name)))
254 (defun gnus-cache-active-articles (group)
255 (let ((articles (gnus-cache-articles-in-group group)))
257 (cons (car articles) (gnus-last-element articles)))))
259 (defun gnus-cache-possibly-alter-active (group active)
260 (let ((cache-active (gnus-cache-active-articles group)))
261 (and cache-active (< (car cache-active) (car active))
262 (setcar active (car cache-active)))
263 (and cache-active (> (cdr cache-active) (cdr active))
264 (setcdr active (cdr cache-active)))))
266 (defun gnus-cache-retrieve-headers (articles group)
267 (let* ((cached (gnus-cache-articles-in-group group))
268 (articles (gnus-sorted-complement articles cached))
269 (cache-file (gnus-cache-file-name group ".overview"))
271 (let ((gnus-use-cache nil))
272 (setq type (and articles (gnus-retrieve-headers articles group))))
273 (gnus-cache-save-buffers)
275 (cond ((not (file-exists-p cache-file))
278 (set-buffer nntp-server-buffer)
280 (insert-file-contents cache-file)
283 (gnus-cache-braid-nov group cached)
286 (gnus-cache-braid-heads group cached)
289 (defun gnus-cache-braid-nov (group cached)
290 (let ((cache-buf (get-buffer-create " *gnus-cache*"))
292 (gnus-cache-save-buffers)
294 (set-buffer cache-buf)
295 (buffer-disable-undo (current-buffer))
297 (insert-file-contents (gnus-cache-file-name group ".overview"))
298 (goto-char (point-min))
300 (goto-char (point-min)))
301 (set-buffer nntp-server-buffer)
302 (goto-char (point-min))
304 (while (and (not (eobp))
305 (< (read (current-buffer)) (car cached)))
309 (set-buffer cache-buf)
310 (if (search-forward (concat "\n" (int-to-string (car cached)) "\t")
312 (setq beg (progn (beginning-of-line) (point))
313 end (progn (end-of-line) (point)))
315 (if beg (progn (insert-buffer-substring cache-buf beg end)
317 (setq cached (cdr cached)))
318 (kill-buffer cache-buf)))
320 (defun gnus-cache-braid-heads (group cached)
321 (let ((cache-buf (get-buffer-create " *gnus-cache*")))
323 (set-buffer cache-buf)
324 (buffer-disable-undo (current-buffer))
326 (set-buffer nntp-server-buffer)
327 (goto-char (point-min))
329 (while (and (not (eobp))
330 (looking-at "2.. +\\([0-9]+\\) ")
331 (< (progn (goto-char (match-beginning 1))
332 (read (current-buffer)))
334 (search-forward "\n.\n" nil 'move))
337 (set-buffer cache-buf)
339 (insert-file-contents (gnus-cache-file-name group (car cached)))
340 (goto-char (point-min))
341 (insert "220 " (int-to-string (car cached)) " Article retrieved.\n")
342 (search-forward "\n\n" nil 'move)
343 (delete-region (point) (point-max))
346 (insert-buffer-substring cache-buf)
347 (setq cached (cdr cached)))
348 (kill-buffer cache-buf)))
350 (defun gnus-jog-cache ()
351 "Go through all groups and put the articles into the cache."
353 (let ((newsrc (cdr gnus-newsrc-alist))
354 (gnus-cache-enter-articles '(unread))
355 (gnus-mark-article-hook nil)
357 (gnus-large-newsgroup nil))
359 (gnus-summary-read-group (car (car newsrc)))
360 (if (not (eq major-mode 'gnus-summary-mode))
362 (while gnus-newsgroup-unreads
363 (gnus-summary-select-article t t nil (car gnus-newsgroup-unreads))
364 (setq gnus-newsgroup-unreads (cdr gnus-newsgroup-unreads)))
365 (kill-buffer (current-buffer)))
366 (setq newsrc (cdr newsrc)))))
368 (provide 'gnus-cache)
370 ;;; gnus-cache.el ends here