*** empty log message ***
[gnus] / lisp / gnus-cache.el
1 ;;; gnus-cache.el --- cache interface for Gnus
2 ;; Copyright (C) 1995 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
5 ;; Keywords: news
6
7 ;; This file is part of GNU Emacs.
8
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)
12 ;; any later version.
13
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.
18
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.
22
23 ;;; Commentary:
24
25 ;;; Code:
26
27 (require 'gnus)
28
29 (defvar gnus-cache-directory (concat gnus-article-save-directory "cache/")
30   "*The directory where cached articles will be stored.")
31
32 (defvar gnus-cache-enter-articles '(ticked dormant)
33   "*Classes of articles to enter into the cache.")
34
35 (defvar gnus-cache-remove-articles '(read)
36   "*Classes of articles to remove from the cache.")
37
38 (defvar gnus-uncacheable-groups "^nnvirtual"
39   "*Groups that match this regexp will not be cached.
40
41 If you want to avoid caching your nnml groups, you could set this
42 variable to \"^nnml\".")
43
44 \f
45
46 (defvar gnus-cache-buffer nil)
47
48 \f
49
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
57   (or gnus-cache-buffer
58       ;; create cache buffer
59       (save-excursion
60         (setq gnus-cache-buffer
61               (cons group
62                     (set-buffer (get-buffer-create " *gnus-cache-overview*"))))
63         (buffer-disable-undo (current-buffer))
64         ;; insert the contents of this groups cache overview
65         (erase-buffer)
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))))
72
73
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)
78       ()
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)
84           (save-excursion
85             (set-buffer buffer)
86             (if (> (buffer-size) 0)
87                 ;; non-empty overview, write it out
88                 (progn
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
96               (condition-case nil
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))
101                 (error nil)))))
102       ;; kill the buffer, it's either unmodified or saved
103       (gnus-kill-buffer buffer)
104       (setq gnus-cache-buffer nil))))
105
106
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))))
113
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)
117               group 
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))))
123
124 (defun gnus-cache-possibly-enter-article 
125   (group article headers ticked dormant unread)
126   (let ((number (mail-header-number headers))
127         file dir)
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))))
135         ()                              ; Do nothing.
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))
144         (save-excursion
145           (set-buffer gnus-article-buffer)
146           (save-restriction
147             (widen)
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))
152           (forward-line -1)
153           (while (condition-case ()
154                      (and (not (bobp))
155                           (> (read (current-buffer)) number))
156                    (error
157                     ;; The line was malformed, so we just remove it!!
158                     (gnus-delete-line)
159                     t))
160             (forward-line -1))
161           (if (bobp) 
162               (if (not (eobp))
163                   (progn
164                     (beginning-of-line)
165                     (if (< (read (current-buffer)) number)
166                         (forward-line 1)))
167                 (beginning-of-line))
168             (forward-line 1))
169           (beginning-of-line)
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) ""))))
181         t))))
182
183 (defun gnus-cache-enter-remove-article (article)
184   (setq gnus-cache-removeable-articles
185         (cons article gnus-cache-removeable-articles)))
186
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)))
193         nil
194       (save-excursion
195         (delete-file file)
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")
200                                 (point-max) t))
201             (delete-region (progn (beginning-of-line) (point))
202                            (progn (forward-line 1) (point))))))))
203
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))
207         article)
208     (gnus-cache-change-buffer gnus-newsgroup-name)
209     (while articles
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))
223
224
225 (defun gnus-cache-request-article (article group)
226   (let ((file (gnus-cache-file-name group article)))
227     (if (not (file-exists-p file))
228         ()
229       (erase-buffer)
230       (insert-file-contents file)
231       t)))
232
233 (defun gnus-cache-articles-in-group (group)
234   (let ((dir (file-name-directory (gnus-cache-file-name group 1)))
235         articles)
236     (if (not (file-exists-p dir))
237         nil
238       (setq articles (directory-files dir nil "^[0-9]+$" t))
239       (if (not articles)
240           nil
241         (sort (mapcar (function (lambda (name)
242                                   (string-to-int name))) 
243                       articles)
244               '<)))))
245
246 (defun gnus-cache-active-articles (group)
247   (let ((articles (gnus-cache-articles-in-group group)))
248     (and articles
249          (cons (car articles) (gnus-last-element articles)))))
250
251 (defun gnus-cache-possibly-alter-active (group active)
252   (let ((cache-active (gnus-cache-active-articles group)))
253     (and cache-active (< (car cache-active) (car active))
254          (setcar active (car cache-active)))
255     (and cache-active (> (cdr cache-active) (cdr active))
256          (setcdr active (cdr cache-active)))))
257
258 (defun gnus-cache-retrieve-headers (articles group)
259   (let* ((cached (gnus-cache-articles-in-group group))
260          (articles (gnus-sorted-complement articles cached))
261          (cache-file (gnus-cache-file-name group ".overview"))
262          type)
263     (let ((gnus-use-cache nil))
264       (setq type (and articles (gnus-retrieve-headers articles group))))
265     (gnus-cache-save-buffers)
266     (save-excursion
267       (cond ((not (file-exists-p cache-file))
268              type)
269             ((null type)
270              (set-buffer nntp-server-buffer)
271              (erase-buffer)
272              (insert-file-contents cache-file)
273              'nov)
274             ((eq type 'nov)
275              (gnus-cache-braid-nov group cached)
276              type)
277             (t
278              (gnus-cache-braid-heads group cached)
279              type)))))
280
281 (defun gnus-cache-braid-nov (group cached)
282   (let ((cache-buf (get-buffer-create " *gnus-cache*"))
283         beg end)
284     (gnus-cache-save-buffers)
285     (save-excursion
286       (set-buffer cache-buf)
287       (buffer-disable-undo (current-buffer))
288       (erase-buffer)
289       (insert-file-contents (gnus-cache-file-name group ".overview"))
290       (goto-char (point-min))
291       (insert "\n")
292       (goto-char (point-min)))
293     (set-buffer nntp-server-buffer)
294     (goto-char (point-min))
295     (while cached
296       (while (and (not (eobp))
297                   (< (read (current-buffer)) (car cached)))
298         (forward-line 1))
299       (beginning-of-line)
300       (save-excursion
301         (set-buffer cache-buf)
302         (if (search-forward (concat "\n" (int-to-string (car cached)) "\t")
303                             nil t)
304             (setq beg (progn (beginning-of-line) (point))
305                   end (progn (end-of-line) (point)))
306           (setq beg nil)))
307       (if beg (progn (insert-buffer-substring cache-buf beg end)
308                      (insert "\n")))
309       (setq cached (cdr cached)))
310     (kill-buffer cache-buf)))
311
312 (defun gnus-cache-braid-heads (group cached)
313   (let ((cache-buf (get-buffer-create " *gnus-cache*")))
314     (save-excursion
315       (set-buffer cache-buf)
316       (buffer-disable-undo (current-buffer))
317       (erase-buffer))
318     (set-buffer nntp-server-buffer)
319     (goto-char (point-min))
320     (while cached
321       (while (and (not (eobp))
322                   (looking-at "2.. +\\([0-9]+\\) ")
323                   (< (progn (goto-char (match-beginning 1))
324                             (read (current-buffer)))
325                      (car cached)))
326         (search-forward "\n.\n" nil 'move))
327       (beginning-of-line)
328       (save-excursion
329         (set-buffer cache-buf)
330         (erase-buffer)
331         (insert-file-contents (gnus-cache-file-name group (car cached)))
332         (goto-char (point-min))
333         (insert "220 " (int-to-string (car cached)) " Article retrieved.\n")
334         (search-forward "\n\n" nil 'move)
335         (delete-region (point) (point-max))
336         (forward-char -1)
337         (insert "."))
338       (insert-buffer-substring cache-buf)
339       (setq cached (cdr cached)))
340     (kill-buffer cache-buf)))
341
342 (defun gnus-jog-cache ()
343   "Go through all groups and put the articles into the cache."
344   (interactive)
345   (let ((newsrc (cdr gnus-newsrc-alist))
346         (gnus-cache-enter-articles '(unread))
347         (gnus-mark-article-hook nil)
348         (gnus-expert-user t)
349         (gnus-large-newsgroup nil))
350     (while newsrc
351       (gnus-summary-read-group (car (car newsrc)))
352       (if (not (eq major-mode 'gnus-summary-mode))
353           ()
354         (while gnus-newsgroup-unreads
355           (gnus-summary-select-article t t nil (car gnus-newsgroup-unreads))
356           (setq gnus-newsgroup-unreads (cdr gnus-newsgroup-unreads)))
357         (kill-buffer (current-buffer)))
358       (setq newsrc (cdr newsrc)))))
359
360 (provide 'gnus-cache)
361               
362 ;;; gnus-cache.el ends here