(gnus-inews-make-draft): Quote article-reply.
[gnus] / lisp / gnus-cache.el
1 ;;; gnus-cache.el --- cache interface for Gnus
2 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (eval-when-compile (require 'cl))
30
31 (require 'gnus)
32 (require 'gnus-int)
33 (require 'gnus-range)
34 (require 'gnus-start)
35 (eval-when-compile
36   (require 'gnus-sum))
37
38 (defcustom gnus-cache-active-file
39   (expand-file-name "active" gnus-cache-directory)
40   "*The cache active file."
41   :group 'gnus-cache
42   :type 'file)
43
44 (defcustom gnus-cache-enter-articles '(ticked dormant)
45   "Classes of articles to enter into the cache."
46   :group 'gnus-cache
47   :type '(set (const ticked) (const dormant) (const unread) (const read)))
48
49 (defcustom gnus-cache-remove-articles '(read)
50   "Classes of articles to remove from the cache."
51   :group 'gnus-cache
52   :type '(set (const ticked) (const dormant) (const unread) (const read)))
53
54 (defcustom gnus-cacheable-groups nil
55   "*Groups that match this regexp will be cached.
56
57 If you only want to cache your nntp groups, you could set this
58 variable to \"^nntp\".
59
60 If a group matches both gnus-cacheable-groups and gnus-uncacheable-groups
61 it's not cached."
62   :group 'gnus-cache
63   :type '(choice (const :tag "off" nil)
64                  regexp))
65
66 (defcustom gnus-uncacheable-groups nil
67   "*Groups that match this regexp will not be cached.
68
69 If you want to avoid caching your nnml groups, you could set this
70 variable to \"^nnml\".
71
72 If a group matches both gnus-cacheable-groups and gnus-uncacheable-groups
73 it's not cached."
74   :group 'gnus-cache
75   :type '(choice (const :tag "off" nil)
76                  regexp))
77
78 (defvar gnus-cache-overview-coding-system 'raw-text
79   "Coding system used on Gnus cache files.")
80
81 (defvar gnus-cache-coding-system 'raw-text
82   "Coding system used on Gnus cache files.")
83
84 \f
85
86 ;;; Internal variables.
87
88 (defvar gnus-cache-removable-articles nil)
89 (defvar gnus-cache-buffer nil)
90 (defvar gnus-cache-active-hashtb nil)
91 (defvar gnus-cache-active-altered nil)
92
93 (eval-and-compile
94   (autoload 'nnml-generate-nov-databases-1 "nnml")
95   (autoload 'nnvirtual-find-group-art "nnvirtual"))
96
97 \f
98
99 ;;; Functions called from Gnus.
100
101 (defun gnus-cache-open ()
102   "Initialize the cache."
103   (when (or (file-exists-p gnus-cache-directory)
104             (and gnus-use-cache
105                  (not (eq gnus-use-cache 'passive))))
106     (gnus-cache-read-active)))
107
108 ;; Complexities of byte-compiling make this kludge necessary.  Eeek.
109 (ignore-errors
110   (gnus-add-shutdown 'gnus-cache-close 'gnus))
111
112 (defun gnus-cache-close ()
113   "Shut down the cache."
114   (gnus-cache-write-active)
115   (gnus-cache-save-buffers)
116   (setq gnus-cache-active-hashtb nil))
117
118 (defun gnus-cache-save-buffers ()
119   ;; save the overview buffer if it exists and has been modified
120   ;; delete empty cache subdirectories
121   (when gnus-cache-buffer
122     (let ((buffer (cdr gnus-cache-buffer))
123           (overview-file (gnus-cache-file-name
124                           (car gnus-cache-buffer) ".overview")))
125       ;; write the overview only if it was modified
126       (when (buffer-modified-p buffer)
127         (save-excursion
128           (set-buffer buffer)
129           (if (> (buffer-size) 0)
130               ;; Non-empty overview, write it to a file.
131               (let ((coding-system-for-write
132                      gnus-cache-overview-coding-system))
133                 (gnus-write-buffer overview-file))
134             ;; Empty overview file, remove it
135             (when (file-exists-p overview-file)
136               (delete-file overview-file))
137             ;; If possible, remove group's cache subdirectory.
138             (condition-case nil
139                 ;; FIXME: we can detect the error type and warn the user
140                 ;; of any inconsistencies (articles w/o nov entries?).
141                 ;; for now, just be conservative...delete only if safe -- sj
142                 (delete-directory (file-name-directory overview-file))
143               (error nil)))))
144       ;; Kill the buffer -- it's either unmodified or saved.
145       (gnus-kill-buffer buffer)
146       (setq gnus-cache-buffer nil))))
147
148 (defun gnus-cache-possibly-enter-article
149   (group article ticked dormant unread &optional force)
150   (when (and (or force (not (eq gnus-use-cache 'passive)))
151              (numberp article)
152              (> article 0))             ; This might be a dummy article.
153     (let ((number article) file headers)
154       ;; If this is a virtual group, we find the real group.
155       (when (gnus-virtual-group-p group)
156         (let ((result (nnvirtual-find-group-art
157                        (gnus-group-real-name group) article)))
158           (setq group (car result)
159                 number (cdr result))))
160       (when (and number
161                  (> number 0)           ; Reffed article.
162                  (or force
163                      (and (gnus-cache-fully-p group)
164                           (gnus-cache-member-of-class
165                            gnus-cache-enter-articles ticked dormant unread)))
166                  (not (file-exists-p (setq file (gnus-cache-file-name
167                                                  group number)))))
168         ;; Possibly create the cache directory.
169         (gnus-make-directory (file-name-directory file))
170         ;; Save the article in the cache.
171         (if (file-exists-p file)
172             t                           ; The article already is saved.
173           (save-excursion
174             (set-buffer nntp-server-buffer)
175             (require 'gnus-art)
176             (let ((gnus-use-cache nil)
177                   (gnus-article-decode-hook nil))
178               (gnus-request-article-this-buffer number group))
179             (when (> (buffer-size) 0)
180               (let ((coding-system-for-write gnus-cache-coding-system))
181                 (gnus-write-buffer file))
182               (nnheader-remove-body)
183               (setq headers (nnheader-parse-naked-head))
184               (mail-header-set-number headers number)
185               (gnus-cache-change-buffer group)
186               (set-buffer (cdr gnus-cache-buffer))
187               (goto-char (point-max))
188               (forward-line -1)
189               (while (condition-case ()
190                          (when (not (bobp))
191                            (> (read (current-buffer)) number))
192                        (error
193                         ;; The line was malformed, so we just remove it!!
194                         (gnus-delete-line)
195                         t))
196                 (forward-line -1))
197               (if (bobp)
198                   (if (not (eobp))
199                       (progn
200                         (beginning-of-line)
201                         (when (< (read (current-buffer)) number)
202                           (forward-line 1)))
203                     (beginning-of-line))
204                 (forward-line 1))
205               (beginning-of-line)
206               (nnheader-insert-nov headers)
207               ;; Update the active info.
208               (set-buffer gnus-summary-buffer)
209               (gnus-cache-possibly-update-active group (cons number number))
210               (setq gnus-newsgroup-cached
211                     (gnus-add-to-sorted-list gnus-newsgroup-cached article))
212               (gnus-summary-update-secondary-mark article))
213             t))))))
214
215 (defun gnus-cache-enter-remove-article (article)
216   "Mark ARTICLE for later possible removal."
217   (when article
218     (push article gnus-cache-removable-articles)))
219
220 (defun gnus-cache-possibly-remove-articles ()
221   "Possibly remove some of the removable articles."
222   (if (not (gnus-virtual-group-p gnus-newsgroup-name))
223       (gnus-cache-possibly-remove-articles-1)
224     (let ((arts gnus-cache-removable-articles)
225           ga)
226       (while arts
227         (when (setq ga (nnvirtual-find-group-art
228                         (gnus-group-real-name gnus-newsgroup-name) (pop arts)))
229           (let ((gnus-cache-removable-articles (list (cdr ga)))
230                 (gnus-newsgroup-name (car ga)))
231             (gnus-cache-possibly-remove-articles-1)))))
232     (setq gnus-cache-removable-articles nil)))
233
234 (defun gnus-cache-possibly-remove-articles-1 ()
235   "Possibly remove some of the removable articles."
236   (when (gnus-cache-fully-p gnus-newsgroup-name)
237     (let ((articles gnus-cache-removable-articles)
238           (cache-articles gnus-newsgroup-cached)
239           article)
240       (gnus-cache-change-buffer gnus-newsgroup-name)
241       (while articles
242         (when (memq (setq article (pop articles)) cache-articles)
243           ;; The article was in the cache, so we see whether we are
244           ;; supposed to remove it from the cache.
245           (gnus-cache-possibly-remove-article
246            article (memq article gnus-newsgroup-marked)
247            (memq article gnus-newsgroup-dormant)
248            (or (memq article gnus-newsgroup-unreads)
249                (memq article gnus-newsgroup-unselected))))))
250     ;; The overview file might have been modified, save it
251     ;; safe because we're only called at group exit anyway.
252     (gnus-cache-save-buffers)))
253
254 (defun gnus-cache-request-article (article group)
255   "Retrieve ARTICLE in GROUP from the cache."
256   (let ((file (gnus-cache-file-name group article))
257         (buffer-read-only nil))
258     (when (file-exists-p file)
259       (erase-buffer)
260       (gnus-kill-all-overlays)
261       (let ((coding-system-for-read gnus-cache-coding-system))
262         (insert-file-contents file))
263       t)))
264
265 (defun gnus-cache-possibly-alter-active (group active)
266   "Alter the ACTIVE info for GROUP to reflect the articles in the cache."
267   (when gnus-cache-active-hashtb
268     (let ((cache-active (gnus-gethash group gnus-cache-active-hashtb)))
269       (when cache-active
270         (when (< (car cache-active) (car active))
271           (setcar active (car cache-active)))
272         (when (> (cdr cache-active) (cdr active))
273           (setcdr active (cdr cache-active)))))))
274
275 (defun gnus-cache-retrieve-headers (articles group &optional fetch-old)
276   "Retrieve the headers for ARTICLES in GROUP."
277   (let ((cached
278          (setq gnus-newsgroup-cached (gnus-cache-articles-in-group group))))
279     (if (not cached)
280         ;; No cached articles here, so we just retrieve them
281         ;; the normal way.
282         (let ((gnus-use-cache nil))
283           (gnus-retrieve-headers articles group fetch-old))
284       (let ((uncached-articles (gnus-sorted-difference articles cached))
285             (cache-file (gnus-cache-file-name group ".overview"))
286             type)
287         ;; We first retrieve all the headers that we don't have in
288         ;; the cache.
289         (let ((gnus-use-cache nil))
290           (when uncached-articles
291             (setq type (and articles
292                             (gnus-retrieve-headers
293                              uncached-articles group fetch-old)))))
294         (gnus-cache-save-buffers)
295         ;; Then we insert the cached headers.
296         (save-excursion
297           (cond
298            ((not (file-exists-p cache-file))
299             ;; There are no cached headers.
300             type)
301            ((null type)
302             ;; There were no uncached headers (or retrieval was
303             ;; unsuccessful), so we use the cached headers exclusively.
304             (set-buffer nntp-server-buffer)
305             (erase-buffer)
306             (let ((coding-system-for-read
307                    gnus-cache-overview-coding-system))
308               (insert-file-contents cache-file))
309             'nov)
310            ((eq type 'nov)
311             ;; We have both cached and uncached NOV headers, so we
312             ;; braid them.
313             (gnus-cache-braid-nov group cached)
314             type)
315            (t
316             ;; We braid HEADs.
317             (gnus-cache-braid-heads group (gnus-sorted-intersection
318                                            cached articles))
319             type)))))))
320
321 (defun gnus-cache-enter-article (&optional n)
322   "Enter the next N articles into the cache.
323 If not given a prefix, use the process marked articles instead.
324 Returns the list of articles entered."
325   (interactive "P")
326   (let ((articles (gnus-summary-work-articles n))
327         article out)
328     (while (setq article (pop articles))
329       (gnus-summary-remove-process-mark article)
330       (if (natnump article)
331           (when (gnus-cache-possibly-enter-article
332                  gnus-newsgroup-name article
333                  nil nil nil t)
334             (push article out))
335         (gnus-message 2 "Can't cache article %d" article))
336       (gnus-summary-update-secondary-mark article))
337     (gnus-summary-next-subject 1)
338     (gnus-summary-position-point)
339     (nreverse out)))
340
341 (defun gnus-cache-remove-article (&optional n)
342   "Remove the next N articles from the cache.
343 If not given a prefix, use the process marked articles instead.
344 Returns the list of articles removed."
345   (interactive "P")
346   (gnus-cache-change-buffer gnus-newsgroup-name)
347   (let ((articles (gnus-summary-work-articles n))
348         article out)
349     (while articles
350       (setq article (pop articles))
351       (gnus-summary-remove-process-mark article)
352       (when (gnus-cache-possibly-remove-article article nil nil nil t)
353         (push article out))
354       (gnus-summary-update-secondary-mark article))
355     (gnus-summary-next-subject 1)
356     (gnus-summary-position-point)
357     (nreverse out)))
358
359 (defun gnus-cached-article-p (article)
360   "Say whether ARTICLE is cached in the current group."
361   (memq article gnus-newsgroup-cached))
362
363 (defun gnus-summary-insert-cached-articles ()
364   "Insert all the articles cached for this group into the current buffer."
365   (interactive)
366   (let ((gnus-verbose (max 6 gnus-verbose)))
367     (if (not gnus-newsgroup-cached)
368         (gnus-message 3 "No cached articles for this group")
369       (gnus-summary-goto-subjects gnus-newsgroup-cached))))
370
371 (defun gnus-summary-limit-include-cached ()
372   "Limit the summary buffer to articles that are cached."
373   (interactive)
374   (let ((gnus-verbose (max 6 gnus-verbose)))
375     (if gnus-newsgroup-cached
376         (progn
377           (gnus-summary-limit gnus-newsgroup-cached)
378           (gnus-summary-position-point))
379       (gnus-message 3 "No cached articles for this group"))))
380
381 ;;; Internal functions.
382
383 (defun gnus-cache-change-buffer (group)
384   (and gnus-cache-buffer
385        ;; See if the current group's overview cache has been loaded.
386        (or (string= group (car gnus-cache-buffer))
387            ;; Another overview cache is current, save it.
388            (gnus-cache-save-buffers)))
389   ;; if gnus-cache buffer is nil, create it
390   (unless gnus-cache-buffer
391     ;; Create cache buffer
392     (save-excursion
393       (setq gnus-cache-buffer
394             (cons group
395                   (set-buffer (gnus-get-buffer-create
396                                " *gnus-cache-overview*"))))
397       ;; Insert the contents of this group's cache overview.
398       (erase-buffer)
399       (let ((file (gnus-cache-file-name group ".overview")))
400         (when (file-exists-p file)
401           (nnheader-insert-file-contents file)))
402       ;; We have a fresh (empty/just loaded) buffer,
403       ;; mark it as unmodified to save a redundant write later.
404       (set-buffer-modified-p nil))))
405
406 ;; Return whether an article is a member of a class.
407 (defun gnus-cache-member-of-class (class ticked dormant unread)
408   (or (and ticked (memq 'ticked class))
409       (and dormant (memq 'dormant class))
410       (and unread (memq 'unread class))
411       (and (not unread) (not ticked) (not dormant) (memq 'read class))))
412
413 (defun gnus-cache-file-name (group article)
414   (expand-file-name
415    (if (stringp article) article (int-to-string article))
416    (file-name-as-directory
417     (expand-file-name
418      (nnheader-translate-file-chars
419       (if (gnus-use-long-file-name 'not-cache)
420           group
421         (let ((group (nnheader-replace-duplicate-chars-in-string
422                       (nnheader-replace-chars-in-string group ?/ ?_)
423                       ?. ?_)))
424           ;; Translate the first colon into a slash.
425           (when (string-match ":" group)
426                   (setq group (concat (substring group 0 (match-beginning 0))
427                                       "/" (substring group (match-end 0)))))
428           (nnheader-replace-chars-in-string group ?. ?/)))
429       t)
430      gnus-cache-directory))))
431
432 (defun gnus-cache-update-article (group article)
433   "If ARTICLE is in the cache, remove it and re-enter it."
434   (gnus-cache-change-buffer group)
435   (when (gnus-cache-possibly-remove-article article nil nil nil t)
436     (let ((gnus-use-cache nil))
437       (gnus-cache-possibly-enter-article
438        gnus-newsgroup-name article
439        nil nil nil t))))
440
441 (defun gnus-cache-possibly-remove-article (article ticked dormant unread
442                                                    &optional force)
443   "Possibly remove ARTICLE from the cache."
444   (let ((group gnus-newsgroup-name)
445         (number article)
446         file)
447     ;; If this is a virtual group, we find the real group.
448     (when (gnus-virtual-group-p group)
449       (let ((result (nnvirtual-find-group-art
450                      (gnus-group-real-name group) article)))
451         (setq group (car result)
452               number (cdr result))))
453     (setq file (gnus-cache-file-name group number))
454     (when (and (file-exists-p file)
455                (or force