*** 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 \f
39
40 (defvar gnus-cache-buffer nil)
41
42 \f
43
44 (defun gnus-cache-change-buffer (group)
45   (and gnus-cache-buffer
46        ;; see if the current group's overview cache has been loaded 
47        (or (string= group (car gnus-cache-buffer))
48            ;; another overview cache is current, save it
49            (gnus-cache-save-buffers)))
50   ;; if gnus-cache buffer is nil, create it
51   (or gnus-cache-buffer
52       ;; create cache buffer
53       (save-excursion
54         (setq gnus-cache-buffer
55               (cons group
56                     (set-buffer (get-buffer-create " *gnus-cache-overview*"))))
57         (buffer-disable-undo (current-buffer))
58         ;; insert the contents of this groups cache overview
59         (erase-buffer)
60         (let ((file (gnus-cache-file-name group ".overview")))
61           (and (file-exists-p file)
62                (insert-file-contents file)))
63         ;; we have a fresh (empty/just loaded) buffer, 
64         ;; mark it as unmodified to save a redundant write later.
65         (set-buffer-modified-p nil))))
66
67
68 (defun gnus-cache-save-buffers ()
69   ;; save the overview buffer if it exists and has been modified
70   ;; delete empty cache subdirectories
71   (if (null gnus-cache-buffer)
72       ()
73     (let ((buffer (cdr gnus-cache-buffer))
74           (overview-file (gnus-cache-file-name
75                           (car gnus-cache-buffer) ".overview")))
76       ;; write the overview only if it was modified
77       (if (buffer-modified-p buffer)
78           (save-excursion
79             (set-buffer buffer)
80             (if (> (buffer-size) 0)
81                 ;; non-empty overview, write it out
82                 (progn
83                   (gnus-make-directory (file-name-directory overview-file))
84                   (write-region (point-min) (point-max)
85                                 overview-file nil 'quietly))
86               ;; empty overview file, remove it
87               (and (file-exists-p overview-file)
88                    (delete-file overview-file))
89               ;; if possible, remove group's cache subdirectory
90               (condition-case nil
91                   ;; FIXME: we can detect the error type and warn the user
92                   ;; of any inconsistencies (articles w/o nov entries?).
93                   ;; for now, just be conservative...delete only if safe -- sj
94                   (delete-directory (file-name-directory overview-file))
95                 (error nil)))))
96       ;; kill the buffer, it's either unmodified or saved
97       (gnus-kill-buffer buffer)
98       (setq gnus-cache-buffer nil))))
99
100
101 ;; Return whether an article is a member of a class.
102 (defun gnus-cache-member-of-class (class ticked dormant unread)
103   (or (and ticked (memq 'ticked class))
104       (and dormant (memq 'dormant class))
105       (and unread (memq 'unread class))
106       (and (not unread) (memq 'read class))))
107
108 (defun gnus-cache-file-name (group article)
109   (concat (file-name-as-directory gnus-cache-directory)
110           (if (gnus-use-long-file-name 'not-cache)
111               group 
112             (let ((group (concat group "")))
113               (if (string-match ":" group)
114                   (aset group (match-beginning 0) ?/))
115               (gnus-replace-chars-in-string group ?. ?/)))
116           "/" (if (stringp article) article (int-to-string article))))
117
118 (defun gnus-cache-possibly-enter-article 
119   (group article headers ticked dormant unread)
120   (let ((number (mail-header-number headers))
121         file dir)
122     (if (or (not (vectorp headers))     ; This might be a dummy article.
123             (not (gnus-cache-member-of-class
124                   gnus-cache-enter-articles ticked dormant unread))
125             (file-exists-p (setq file (gnus-cache-file-name group article))))
126         ()                              ; Do nothing.
127       ;; Possibly create the cache directory.
128       (or (file-exists-p (setq dir (file-name-directory file)))
129           (gnus-make-directory dir))
130       ;; Save the article in the cache.
131       (if (file-exists-p file)
132           t                             ; The article already is saved, so we end here.
133         (let ((gnus-use-cache nil))
134           (gnus-summary-select-article))
135         (save-excursion
136           (set-buffer gnus-article-buffer)
137           (save-restriction
138             (widen)
139             (write-region (point-min) (point-max) file nil 'quiet))
140           (gnus-cache-change-buffer group)
141           (set-buffer (cdr gnus-cache-buffer))
142           (goto-char (point-max))
143           (forward-line -1)
144           (while (and (not (bobp))
145                       (> (read (current-buffer)) number))
146             (forward-line -1))
147           (if (bobp) 
148               (if (not (eobp))
149                   (progn
150                     (beginning-of-line)
151                     (if (< (read (current-buffer)) number)
152                         (forward-line 1)))
153                 (beginning-of-line))
154             (forward-line 1))
155           (beginning-of-line)
156           ;; [number subject from date id references chars lines xref]
157           (insert (format "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t\n"
158                           (mail-header-number headers)
159                           (mail-header-subject headers)
160                           (mail-header-from headers)
161                           (mail-header-date headers)
162                           (mail-header-id headers)
163                           (or (mail-header-references headers) "")
164                           (or (mail-header-chars headers) "")
165                           (or (mail-header-lines headers) "")
166                           (or (mail-header-xref headers) ""))))
167         t))))
168
169 (defun gnus-cache-enter-remove-article (article)
170   (setq gnus-cache-removeable-articles
171         (cons article gnus-cache-removeable-articles)))
172
173 (defsubst gnus-cache-possibly-remove-article 
174   (article ticked dormant unread)
175   (let ((file (gnus-cache-file-name gnus-newsgroup-name article)))
176     (if (or (not (file-exists-p file))
177             (not (gnus-cache-member-of-class
178                   gnus-cache-remove-articles ticked dormant unread)))
179         nil
180       (save-excursion
181         (delete-file file)
182         (set-buffer (cdr gnus-cache-buffer))
183         (goto-char (point-min))
184         (if (or (looking-at (concat (int-to-string article) "\t"))
185                 (search-forward (concat "\n" (int-to-string article) "\t")
186                                 (point-max) t))
187             (delete-region (progn (beginning-of-line) (point))
188                            (progn (forward-line 1) (point))))))))
189
190 (defun gnus-cache-possibly-remove-articles ()
191   (let ((articles gnus-cache-removeable-articles)
192         (cache-articles (gnus-cache-articles-in-group gnus-newsgroup-name))
193         article)
194     (gnus-cache-change-buffer gnus-newsgroup-name)
195     (while articles
196       (setq article (car articles)
197             articles (cdr articles))
198       (if (memq article cache-articles)
199           ;; The article was in the cache, so we see whether we are
200           ;; supposed to remove it from the cache.
201           (gnus-cache-possibly-remove-article
202            article (memq article gnus-newsgroup-marked)
203            (memq article gnus-newsgroup-dormant)
204            (or (memq article gnus-newsgroup-unreads)
205                (memq article gnus-newsgroup-unselected))))))
206   ;; the overview file might have been modified, save it
207   ;; safe because we're only called at group exit anyway
208   (gnus-cache-save-buffers))
209
210
211 (defun gnus-cache-request-article (article group)
212   (let ((file (gnus-cache-file-name group article)))
213     (if (not (file-exists-p file))
214         ()
215       (erase-buffer)
216       (insert-file-contents file)
217       t)))
218
219 (defun gnus-cache-articles-in-group (group)
220   (let ((dir (file-name-directory (gnus-cache-file-name group 1)))
221         articles)
222     (if (not (file-exists-p dir))
223         nil
224       (setq articles (directory-files dir nil "^[0-9]+$" t))
225       (if (not articles)
226           nil
227         (sort (mapcar (function (lambda (name)
228                                   (string-to-int name))) 
229                       articles)
230               '<)))))
231
232 (defun gnus-cache-active-articles (group)
233   (let ((articles (gnus-cache-articles-in-group group)))
234     (and articles
235          (cons (car articles) (gnus-last-element articles)))))
236
237 (defun gnus-cache-possibly-alter-active (group active)
238   (let ((cache-active (gnus-cache-active-articles group)))
239     (and cache-active (< (car cache-active) (car active))
240          (setcar active (car cache-active)))
241     (and cache-active (> (cdr cache-active) (cdr active))
242          (setcdr active (cdr cache-active)))))
243
244 (defun gnus-cache-retrieve-headers (articles group)
245   (let* ((cached (gnus-cache-articles-in-group group))
246          (articles (gnus-sorted-complement articles cached))
247          (cache-file (gnus-cache-file-name group ".overview"))
248          type)
249     (let ((gnus-use-cache nil))
250       (setq type (and articles (gnus-retrieve-headers articles group))))
251     (gnus-cache-save-buffers)
252     (save-excursion
253       (cond ((not (file-exists-p cache-file))
254              type)
255             ((null type)
256              (set-buffer nntp-server-buffer)
257              (erase-buffer)
258              (insert-file-contents cache-file)
259              'nov)
260             ((eq type 'nov)
261              (gnus-cache-braid-nov group cached)
262              type)
263             (t
264              (gnus-cache-braid-heads group cached)
265              type)))))
266
267 (defun gnus-cache-braid-nov (group cached)
268   (let ((cache-buf (get-buffer-create " *gnus-cache*"))
269         beg end)
270     (gnus-cache-save-buffers)
271     (save-excursion
272       (set-buffer cache-buf)
273       (buffer-disable-undo (current-buffer))
274       (erase-buffer)
275       (insert-file-contents (gnus-cache-file-name group ".overview"))
276       (goto-char (point-min))
277       (insert "\n")
278       (goto-char (point-min)))
279     (set-buffer nntp-server-buffer)
280     (goto-char (point-min))
281     (while cached
282       (while (and (not (eobp))
283                   (< (read (current-buffer)) (car cached)))
284         (forward-line 1))
285       (beginning-of-line)
286       (save-excursion
287         (set-buffer cache-buf)
288         (if (search-forward (concat "\n" (int-to-string (car cached)) "\t")
289                             nil t)
290             (setq beg (progn (beginning-of-line) (point))
291                   end (progn (end-of-line) (point)))
292           (setq beg nil)))
293       (if beg (progn (insert-buffer-substring cache-buf beg end)
294                      (insert "\n")))
295       (setq cached (cdr cached)))
296     (kill-buffer cache-buf)))
297
298 (defun gnus-cache-braid-heads (group cached)
299   (let ((cache-buf (get-buffer-create " *gnus-cache*")))
300     (save-excursion
301       (set-buffer cache-buf)
302       (buffer-disable-undo (current-buffer))
303       (erase-buffer))
304     (set-buffer nntp-server-buffer)
305     (goto-char (point-min))
306     (while cached
307       (while (and (not (eobp))
308                   (looking-at "2.. +\\([0-9]+\\) ")
309                   (< (progn (goto-char (match-beginning 1))
310                             (read (current-buffer)))
311                      (car cached)))
312         (search-forward "\n.\n" nil 'move))
313       (beginning-of-line)
314       (save-excursion
315         (set-buffer cache-buf)
316         (erase-buffer)
317         (insert-file-contents (gnus-cache-file-name group (car cached)))
318         (goto-char (point-min))
319         (insert "220 " (int-to-string (car cached)) " Article retrieved.\n")
320         (search-forward "\n\n" nil 'move)
321         (delete-region (point) (point-max))
322         (forward-char -1)
323         (insert "."))
324       (insert-buffer-substring cache-buf)
325       (setq cached (cdr cached)))
326     (kill-buffer cache-buf)))
327
328 (defun gnus-jog-cache ()
329   "Go through all groups and put the articles into the cache."
330   (interactive)
331   (let ((newsrc (cdr gnus-newsrc-alist))
332         (gnus-cache-enter-articles '(unread))
333         (gnus-mark-article-hook nil)
334         (gnus-expert-user t)
335         (gnus-large-newsgroup nil))
336     (while newsrc
337       (gnus-summary-read-group (car (car newsrc)))
338       (if (not (eq major-mode 'gnus-summary-mode))
339           ()
340         (while gnus-newsgroup-unreads
341           (gnus-summary-select-article t t nil (car gnus-newsgroup-unreads))
342           (setq gnus-newsgroup-unreads (cdr gnus-newsgroup-unreads)))
343         (kill-buffer (current-buffer)))
344       (setq newsrc (cdr newsrc)))))
345
346 (provide 'gnus-cache)
347               
348 ;;; gnus-cache.el ends here