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