6ac55c6694269872de5458d2a6964af8881fdf7b
[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           (file-name-as-directory
117            (if (gnus-use-long-file-name 'not-cache)
118                group 
119              (let ((group (concat group "")))
120                (if (string-match ":" group)
121                    (aset group (match-beginning 0) ?/))
122                (gnus-replace-chars-in-string group ?. ?/))))
123           (if (stringp article) article (int-to-string article))))
124
125 (defun gnus-cache-possibly-enter-article 
126   (group article headers ticked dormant unread)
127   (let ((number (mail-header-number headers))
128         file dir)
129     (if (or (not (vectorp headers))     ; This might be a dummy article.
130             (< number 0)                ; Reffed article.
131             (and gnus-uncacheable-groups
132                  (string-match gnus-uncacheable-groups group))
133             (not (gnus-cache-member-of-class
134                   gnus-cache-enter-articles ticked dormant unread))
135             (file-exists-p (setq file (gnus-cache-file-name group article))))
136         ()                              ; Do nothing.
137       ;; Possibly create the cache directory.
138       (or (file-exists-p (setq dir (file-name-directory file)))
139           (gnus-make-directory dir))
140       ;; Save the article in the cache.
141       (if (file-exists-p file)
142           t                             ; The article already is saved.
143         (let ((gnus-use-cache nil))
144           (gnus-summary-select-article))
145         (save-excursion
146           (set-buffer gnus-article-buffer)
147           (save-restriction
148             (widen)
149             (write-region (point-min) (point-max) file nil 'quiet))
150           (gnus-cache-change-buffer group)
151           (set-buffer (cdr gnus-cache-buffer))
152           (goto-char (point-max))
153           (forward-line -1)
154           (while (condition-case ()
155                      (and (not (bobp))
156                           (> (read (current-buffer)) number))
157                    (error
158                     ;; The line was malformed, so we just remove it!!
159                     (gnus-delete-line)
160                     t))
161             (forward-line -1))
162           (if (bobp) 
163               (if (not (eobp))
164                   (progn
165                     (beginning-of-line)
166                     (if (< (read (current-buffer)) number)
167                         (forward-line 1)))
168                 (beginning-of-line))
169             (forward-line 1))
170           (beginning-of-line)
171           ;; [number subject from date id references chars lines xref]
172           (insert (format "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t\n"
173                           (mail-header-number headers)
174                           (mail-header-subject headers)
175                           (mail-header-from headers)
176                           (mail-header-date headers)
177                           (mail-header-id headers)
178                           (or (mail-header-references headers) "")
179                           (or (mail-header-chars headers) "")
180                           (or (mail-header-lines headers) "")
181                           (or (mail-header-xref headers) ""))))
182         t))))
183
184 (defun gnus-cache-enter-remove-article (article)
185   (setq gnus-cache-removeable-articles
186         (cons article gnus-cache-removeable-articles)))
187
188 (defsubst gnus-cache-possibly-remove-article 
189   (article ticked dormant unread)
190   (let ((file (gnus-cache-file-name gnus-newsgroup-name article)))
191     (if (or (not (file-exists-p file))
192             (not (gnus-cache-member-of-class
193                   gnus-cache-remove-articles ticked dormant unread)))
194         nil
195       (save-excursion
196         (delete-file file)
197         (set-buffer (cdr gnus-cache-buffer))
198         (goto-char (point-min))
199         (if (or (looking-at (concat (int-to-string article) "\t"))
200                 (search-forward (concat "\n" (int-to-string article) "\t")
201                                 (point-max) t))
202             (delete-region (progn (beginning-of-line) (point))
203                            (progn (forward-line 1) (point))))))))
204
205 (defun gnus-cache-possibly-remove-articles ()
206   (let ((articles gnus-cache-removeable-articles)
207         (cache-articles (gnus-cache-articles-in-group gnus-newsgroup-name))
208         article)
209     (gnus-cache-change-buffer gnus-newsgroup-name)
210     (while articles
211       (setq article (car articles)
212             articles (cdr articles))
213       (if (memq article cache-articles)
214           ;; The article was in the cache, so we see whether we are
215           ;; supposed to remove it from the cache.
216           (gnus-cache-possibly-remove-article
217            article (memq article gnus-newsgroup-marked)
218            (memq article gnus-newsgroup-dormant)
219            (or (memq article gnus-newsgroup-unreads)
220                (memq article gnus-newsgroup-unselected))))))
221   ;; the overview file might have been modified, save it
222   ;; safe because we're only called at group exit anyway
223   (gnus-cache-save-buffers))
224
225
226 (defun gnus-cache-request-article (article group)
227   (let ((file (gnus-cache-file-name group article))
228         (buffer-read-only nil))
229     (if (not (file-exists-p file))
230         ()
231       (erase-buffer)
232       ;; There may be some overlays that we have to kill...
233       (insert "i")
234       (let ((overlays (and (fboundp 'overlays-at)
235                            (overlays-at (point-min)))))
236         (while overlays
237           (delete-overlay (car overlays))
238           (setq overlays (cdr overlays))))
239       (erase-buffer)      
240       (insert-file-contents file)
241       t)))
242
243 (defun gnus-cache-articles-in-group (group)
244   (let ((dir (file-name-directory (gnus-cache-file-name group 1)))
245         articles)
246     (if (not (file-exists-p dir))
247         nil
248       (setq articles (directory-files dir nil "^[0-9]+$" t))
249       (if (not articles)
250           nil
251         (sort (mapcar (function (lambda (name)
252                                   (string-to-int name))) 
253                       articles)
254               '<)))))
255
256 (defun gnus-cache-active-articles (group)
257   (let ((articles (gnus-cache-articles-in-group group)))
258     (and articles
259          (cons (car articles) (gnus-last-element articles)))))
260
261 (defun gnus-cache-possibly-alter-active (group active)
262   (let ((cache-active (gnus-cache-active-articles group)))
263     (and cache-active (< (car cache-active) (car active))
264          (setcar active (car cache-active)))
265     (and cache-active (> (cdr cache-active) (cdr active))
266          (setcdr active (cdr cache-active)))))
267
268 (defun gnus-cache-retrieve-headers (articles group)
269   (let* ((cached (gnus-cache-articles-in-group group))
270          (articles (gnus-sorted-complement articles cached))
271          (cache-file (gnus-cache-file-name group ".overview"))
272          type)
273     (let ((gnus-use-cache nil))
274       (setq type (and articles (gnus-retrieve-headers articles group))))
275     (gnus-cache-save-buffers)
276     (save-excursion
277       (cond ((not (file-exists-p cache-file))
278              type)
279             ((null type)
280              (set-buffer nntp-server-buffer)
281              (erase-buffer)
282              (insert-file-contents cache-file)
283              'nov)
284             ((eq type 'nov)
285              (gnus-cache-braid-nov group cached)
286              type)
287             (t
288              (gnus-cache-braid-heads group cached)
289              type)))))
290
291 (defun gnus-cache-braid-nov (group cached)
292   (let ((cache-buf (get-buffer-create " *gnus-cache*"))
293         beg end)
294     (gnus-cache-save-buffers)
295     (save-excursion
296       (set-buffer cache-buf)
297       (buffer-disable-undo (current-buffer))
298       (erase-buffer)
299       (insert-file-contents (gnus-cache-file-name group ".overview"))
300       (goto-char (point-min))
301       (insert "\n")
302       (goto-char (point-min)))
303     (set-buffer nntp-server-buffer)
304     (goto-char (point-min))
305     (while cached
306       (while (and (not (eobp))
307                   (< (read (current-buffer)) (car cached)))
308         (forward-line 1))
309       (beginning-of-line)
310       (save-excursion
311         (set-buffer cache-buf)
312         (if (search-forward (concat "\n" (int-to-string (car cached)) "\t")
313                             nil t)
314             (setq beg (progn (beginning-of-line) (point))
315                   end (progn (end-of-line) (point)))
316           (setq beg nil)))
317       (if beg (progn (insert-buffer-substring cache-buf beg end)
318                      (insert "\n")))
319       (setq cached (cdr cached)))
320     (kill-buffer cache-buf)))
321
322 (defun gnus-cache-braid-heads (group cached)
323   (let ((cache-buf (get-buffer-create " *gnus-cache*")))
324     (save-excursion
325       (set-buffer cache-buf)
326       (buffer-disable-undo (current-buffer))
327       (erase-buffer))
328     (set-buffer nntp-server-buffer)
329     (goto-char (point-min))
330     (while cached
331       (while (and (not (eobp))
332                   (looking-at "2.. +\\([0-9]+\\) ")
333                   (< (progn (goto-char (match-beginning 1))
334                             (read (current-buffer)))
335                      (car cached)))
336         (search-forward "\n.\n" nil 'move))
337       (beginning-of-line)
338       (save-excursion
339         (set-buffer cache-buf)
340         (erase-buffer)
341         (insert-file-contents (gnus-cache-file-name group (car cached)))
342         (goto-char (point-min))
343         (insert "220 " (int-to-string (car cached)) " Article retrieved.\n")
344         (search-forward "\n\n" nil 'move)
345         (delete-region (point) (point-max))
346         (forward-char -1)
347         (insert "."))
348       (insert-buffer-substring cache-buf)
349       (setq cached (cdr cached)))
350     (kill-buffer cache-buf)))
351
352 (defun gnus-jog-cache ()
353   "Go through all groups and put the articles into the cache."
354   (interactive)
355   (let ((newsrc (cdr gnus-newsrc-alist))
356         (gnus-cache-enter-articles '(unread))
357         (gnus-mark-article-hook nil)
358         (gnus-expert-user t)
359         (gnus-large-newsgroup nil))
360     (while newsrc
361       (gnus-summary-read-group (car (car newsrc)))
362       (if (not (eq major-mode 'gnus-summary-mode))
363           ()
364         (while gnus-newsgroup-unreads
365           (gnus-summary-select-article t t nil (car gnus-newsgroup-unreads))
366           (setq gnus-newsgroup-unreads (cdr gnus-newsgroup-unreads)))
367         (kill-buffer (current-buffer)))
368       (setq newsrc (cdr newsrc)))))
369
370 (provide 'gnus-cache)
371               
372 ;;; gnus-cache.el ends here