Synch to Gnus 200311270654.
[elisp/gnus.git-] / lisp / gnus-cache.el
1 ;;; gnus-cache.el --- cache interface for Gnus
2 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;;         Tatsuya Ichikawa <t-ichi@po.shiojiri.ne.jp>
7 ;;         MORIOKA Tomohiko <morioka@jaist.ac.jp>
8 ;; Keywords: news
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
26
27 ;;; Commentary:
28
29 ;;; Code:
30
31 (eval-when-compile (require 'cl))
32 (eval-when-compile (require 'gnus-clfns))
33
34 (require 'gnus)
35 (require 'gnus-int)
36 (require 'gnus-range)
37 (require 'gnus-start)
38 (eval-when-compile
39   (if (not (fboundp 'gnus-agent-load-alist))
40       (defun gnus-agent-load-alist (group)))
41   (require 'gnus-sum))
42
43 (defcustom gnus-cache-active-file
44   (expand-file-name "active" gnus-cache-directory)
45   "*The cache active file."
46   :group 'gnus-cache
47   :type 'file)
48
49 (defcustom gnus-cache-enter-articles '(ticked dormant)
50   "Classes of articles to enter into the cache."
51   :group 'gnus-cache
52   :type '(set (const ticked) (const dormant) (const unread) (const read)))
53
54 (defcustom gnus-cache-remove-articles '(read)
55   "Classes of articles to remove from the cache."
56   :group 'gnus-cache
57   :type '(set (const ticked) (const dormant) (const unread) (const read)))
58
59 (defcustom gnus-cacheable-groups nil
60   "*Groups that match this regexp will be cached.
61
62 If you only want to cache your nntp groups, you could set this
63 variable to \"^nntp\".
64
65 If a group matches both gnus-cacheable-groups and gnus-uncacheable-groups
66 it's not cached."
67   :group 'gnus-cache
68   :type '(choice (const :tag "off" nil)
69                  regexp))
70
71 (defcustom gnus-uncacheable-groups nil
72   "*Groups that match this regexp will not be cached.
73
74 If you want to avoid caching your nnml groups, you could set this
75 variable to \"^nnml\".
76
77 If a group matches both gnus-cacheable-groups and gnus-uncacheable-groups
78 it's not cached."
79   :group 'gnus-cache
80   :type '(choice (const :tag "off" nil)
81                  regexp))
82
83 (defvar gnus-cache-overview-coding-system 'raw-text
84   "Coding system used on Gnus cache files.")
85
86 (defvar gnus-cache-coding-system 'raw-text
87   "Coding system used on Gnus cache files.")
88
89 \f
90
91 ;;; Internal variables.
92
93 (defvar gnus-cache-removable-articles nil)
94 (defvar gnus-cache-buffer nil)
95 (defvar gnus-cache-active-hashtb nil)
96 (defvar gnus-cache-active-altered nil)
97 (defvar gnus-cache-write-file-coding-system 'raw-text)
98
99 (eval-and-compile
100   (autoload 'nnml-generate-nov-databases-1 "nnml")
101   (autoload 'nnvirtual-find-group-art "nnvirtual"))
102
103 \f
104
105 ;;; Functions called from Gnus.
106
107 (defun gnus-cache-open ()
108   "Initialize the cache."
109   (when (or (file-exists-p gnus-cache-directory)
110             (and gnus-use-cache
111                  (not (eq gnus-use-cache 'passive))))
112     (gnus-cache-read-active)))
113
114 ;; Complexities of byte-compiling make this kludge necessary.  Eeek.
115 (ignore-errors
116   (gnus-add-shutdown 'gnus-cache-close 'gnus))
117
118 (defun gnus-cache-close ()
119   "Shut down the cache."
120   (gnus-cache-write-active)
121   (gnus-cache-save-buffers)
122   (setq gnus-cache-active-hashtb nil))
123
124 (defun gnus-cache-save-buffers ()
125   ;; save the overview buffer if it exists and has been modified
126   ;; delete empty cache subdirectories
127   (when gnus-cache-buffer
128     (let ((buffer (cdr gnus-cache-buffer))
129           (overview-file (gnus-cache-file-name
130                           (car gnus-cache-buffer) ".overview")))
131       ;; write the overview only if it was modified
132       (when (buffer-modified-p buffer)
133         (save-excursion
134           (set-buffer buffer)
135           (if (> (buffer-size) 0)
136               ;; Non-empty overview, write it to a file.
137               (gnus-write-buffer-as-coding-system
138                gnus-cache-overview-coding-system overview-file)
139             ;; Empty overview file, remove it
140             (when (file-exists-p overview-file)
141               (delete-file overview-file))
142             ;; If possible, remove group's cache subdirectory.
143             (condition-case nil
144                 ;; FIXME: we can detect the error type and warn the user
145                 ;; of any inconsistencies (articles w/o nov entries?).
146                 ;; for now, just be conservative...delete only if safe -- sj
147                 (delete-directory (file-name-directory overview-file))
148               (error nil)))))
149       ;; Kill the buffer -- it's either unmodified or saved.
150       (gnus-kill-buffer buffer)
151       (setq gnus-cache-buffer nil))))
152
153 (defun gnus-cache-possibly-enter-article
154   (group article headers ticked dormant unread &optional force)
155   (when (and (or force (not (eq gnus-use-cache 'passive)))
156              (numberp article)
157              (> article 0)              ; This might be a dummy article.
158              (vectorp headers))
159     (let ((number article) file)
160       ;; If this is a virtual group, we find the real group.
161       (when (gnus-virtual-group-p group)
162         (let ((result (nnvirtual-find-group-art
163                        (gnus-group-real-name group) article)))
164           (setq group (car result)
165                 number (cdr result))))
166       (when (and number
167                  (> number 0)           ; Reffed article.
168                  (or force
169                      (and (gnus-cache-fully-p group)
170                           (gnus-cache-member-of-class
171                            gnus-cache-enter-articles ticked dormant unread)))
172                  (not (file-exists-p (setq file (gnus-cache-file-name
173                                                  group number)))))
174         ;; Possibly create the cache directory.
175         (gnus-make-directory (file-name-directory file))
176         ;; Save the article in the cache.
177         (if (file-exists-p file)
178             t                           ; The article already is saved.
179           (save-excursion
180             (set-buffer nntp-server-buffer)
181             (require 'gnus-art)
182             (let ((gnus-use-cache nil)
183                   (gnus-article-decode-hook nil))
184               (gnus-request-article-this-buffer number group))
185             (when (> (buffer-size) 0)
186               (gnus-write-buffer-as-coding-system
187                gnus-cache-write-file-coding-system file)
188               (nnheader-remove-body)
189               (setq headers (nnheader-parse-naked-head))
190               (mail-header-set-number headers number)
191               (gnus-cache-change-buffer group)
192               (set-buffer (cdr gnus-cache-buffer))
193               (goto-char (point-max))
194               (forward-line -1)
195               (while (condition-case ()
196                          (when (not (bobp))
197                            (> (read (current-buffer)) number))
198                        (error
199                         ;; The line was malformed, so we just remove it!!
200                         (gnus-delete-line)
201                         t))
202                 (forward-line -1))
203               (if (bobp)
204                   (if (not (eobp))
205                       (progn
206                         (beginning-of-line)
207                         (when (< (read (current-buffer)) number)
208                           (forward-line 1)))
209                     (beginning-of-line))
210                 (forward-line 1))
211               (beginning-of-line)
212               (nnheader-insert-nov headers)
213               ;; Update the active info.
214               (set-buffer gnus-summary-buffer)
215               (gnus-cache-possibly-update-active group (cons number number))
216               (setq gnus-newsgroup-cached
217                     (gnus-add-to-sorted-list gnus-newsgroup-cached article))
218               (gnus-summary-update-secondary-mark article))
219             t))))))
220
221 (defun gnus-cache-enter-remove-article (article)
222   "Mark ARTICLE for later possible removal."
223   (when article
224     (push article gnus-cache-removable-articles)))
225
226 (defun gnus-cache-possibly-remove-articles ()
227   "Possibly remove some of the removable articles."
228   (if (not (gnus-virtual-group-p gnus-newsgroup-name))
229       (gnus-cache-possibly-remove-articles-1)
230     (let ((arts gnus-cache-removable-articles)
231           ga)
232       (while arts
233         (when (setq ga (nnvirtual-find-group-art
234                         (gnus-group-real-name gnus-newsgroup-name) (pop arts)))
235           (let ((gnus-cache-removable-articles (list (cdr ga)))
236                 (gnus-newsgroup-name (car ga)))
237             (gnus-cache-possibly-remove-articles-1)))))
238     (setq gnus-cache-removable-articles nil)))
239
240 (defun gnus-cache-possibly-remove-articles-1 ()
241   "Possibly remove some of the removable articles."
242   (when (gnus-cache-fully-p gnus-newsgroup-name)
243     (let ((articles gnus-cache-removable-articles)
244           (cache-articles gnus-newsgroup-cached)
245           article)
246       (gnus-cache-change-buffer gnus-newsgroup-name)
247       (while articles
248         (when (memq (setq article (pop articles)) cache-articles)
249           ;; The article was in the cache, so we see whether we are
250           ;; supposed to remove it from the cache.
251           (gnus-cache-possibly-remove-article
252            article (memq article gnus-newsgroup-marked)
253            (memq article gnus-newsgroup-dormant)
254            (or (memq article gnus-newsgroup-unreads)
255                (memq article gnus-newsgroup-unselected))))))
256     ;; The overview file might have been modified, save it
257     ;; safe because we're only called at group exit anyway.
258     (gnus-cache-save-buffers)))
259
260 (defun gnus-cache-request-article (article group)
261   "Retrieve ARTICLE in GROUP from the cache."
262   (let ((file (gnus-cache-file-name group article))
263         (buffer-read-only nil))
264     (when (file-exists-p file)
265       (erase-buffer)
266       (gnus-kill-all-overlays)
267       (let ((nnheader-file-coding-system gnus-cache-coding-system))
268         (nnheader-insert-file-contents file))
269       t)))
270
271 (defun gnus-cache-possibly-alter-active (group active)
272   "Alter the ACTIVE info for GROUP to reflect the articles in the cache."
273   (when gnus-cache-active-hashtb
274     (let ((cache-active (gnus-gethash group gnus-cache-active-hashtb)))
275       (when cache-active
276         (when (< (car cache-active) (car active))
277           (setcar active (car cache-active)))
278         (when (> (cdr cache-active) (cdr active))
279           (setcdr active (cdr cache-active)))))))
280
281 (defun gnus-cache-retrieve-headers (articles group &optional fetch-old)
282   "Retrieve the headers for ARTICLES in GROUP."
283   (let ((cached
284          (setq gnus-newsgroup-cached (gnus-cache-articles-in-group group))))
285     (if (not cached)
286         ;; No cached articles here, so we just retrieve them
287         ;; the normal way.
288         (let ((gnus-use-cache nil))
289           (gnus-retrieve-headers articles group fetch-old))
290       (let ((uncached-articles (gnus-sorted-difference articles cached))
291             (cache-file (gnus-cache-file-name group ".overview"))
292             type)
293         ;; We first retrieve all the headers that we don't have in
294         ;; the cache.
295         (let ((gnus-use-cache nil))
296           (when uncached-articles
297             (setq type (and articles
298                             (gnus-retrieve-headers
299                              uncached-articles group fetch-old)))))
300         (gnus-cache-save-buffers)
301         ;; Then we insert the cached headers.
302         (save-excursion
303           (cond
304            ((not (file-exists-p cache-file))
305             ;; There are no cached headers.
306             type)
307            ((null type)
308             ;; There were no uncached headers (or retrieval was
309             ;; unsuccessful), so we use the cached headers exclusively.
310             (set-buffer nntp-server-buffer)
311             (erase-buffer)
312             (let ((nnheader-file-coding-system
313                    gnus-cache-overview-coding-system))
314               (nnheader-insert-file-contents cache-file))
315             'nov)
316            ((eq type 'nov)
317             ;; We have both cached and uncached NOV headers, so we
318             ;; braid them.
319             (gnus-cache-braid-nov group cached)
320             type)
321            (t
322             ;; We braid HEADs.
323             (gnus-cache-braid-heads group (gnus-sorted-intersection
324                                            cached articles))
325             type)))))))
326
327 (defun gnus-cache-enter-article (&optional n)
328   "Enter the next N articles into the cache.
329 If not given a prefix, use the process marked articles instead.
330 Returns the list of articles entered."
331   (interactive "P")
332   (let ((articles (gnus-summary-work-articles n))
333         article out)
334     (while (setq article (pop articles))
335       (gnus-summary-remove-process-mark article)
336       (if (natnump article)
337           (when (gnus-cache-possibly-enter-article
338                  gnus-newsgroup-name article
339                  (gnus-summary-article-header article)
340                  nil nil nil t)
341             (setq gnus-newsgroup-undownloaded (delq article gnus-newsgroup-undownloaded))
342             (push article out))
343         (gnus-message 2 "Can't cache article %d" article))
344       (gnus-summary-update-download-mark article)
345       (gnus-summary-update-secondary-mark article))
346     (gnus-summary-next-subject 1)
347     (gnus-summary-position-point)
348     (nreverse out)))
349
350 (defun gnus-cache-remove-article (&optional n)
351   "Remove the next N articles from the cache.
352 If not given a prefix, use the process marked articles instead.
353 Returns the list of articles removed."
354   (interactive "P")
355   (gnus-cache-change-buffer gnus-newsgroup-name)
356   (let ((articles (gnus-summary-work-articles n))
357         article out)
358     (while articles
359       (setq article (pop articles))
360       (gnus-summary-remove-process-mark article)
361       (when (gnus-cache-possibly-remove-article article nil nil nil t)
362         (when gnus-newsgroup-agentized
363           (let ((alist (gnus-agent-load-alist gnus-newsgroup-name)))
364             (unless (cdr (assoc article alist))
365               (setq gnus-newsgroup-undownloaded
366                     (gnus-add-to-sorted-list 
367                      gnus-newsgroup-undownloaded article)))))
368         (push article out))
369       (gnus-summary-update-download-mark article)
370       (gnus-summary-update-secondary-mark article))
371     (gnus-summary-next-subject 1)
372     (gnus-summary-position-point)
373     (nreverse out)))
374
375 (defun gnus-cached-article-p (article)
376   "Say whether ARTICLE is cached in the current group."
377   (memq article gnus-newsgroup-cached))
378
379 (defun gnus-summary-insert-cached-articles ()
380   "Insert all the articles cached for this group into the current buffer."
381   (interactive)
382   (let ((gnus-verbose (max 6 gnus-verbose)))
383     (if (not gnus-newsgroup-cached)
384         (gnus-message 3 "No cached articles for this group")
385       (gnus-summary-goto-subjects gnus-newsgroup-cached))))
386
387 (defun gnus-summary-limit-include-cached ()
388   "Limit the summary buffer to articles that are cached."
389   (interactive)
390   (let ((gnus-verbose (max 6 gnus-verbose)))
391     (if gnus-newsgroup-cached
392         (progn
393           (gnus-summary-limit gnus-newsgroup-cached)
394           (gnus-summary-position-point))
395       (gnus-message 3 "No cached articles for this group"))))
396
397 ;;; Internal functions.
398
399 (defun gnus-cache-change-buffer (group)
400   (and gnus-cache-buffer
401        ;; See if the current group's overview cache has been loaded.
402        (or (string= group (car gnus-cache-buffer))
403            ;; Another overview cache is current, save it.
404            (gnus-cache-save-buffers)))
405   ;; if gnus-cache buffer is nil, create it
406   (unless gnus-cache-buffer
407     ;; Create cache buffer
408     (save-excursion
409       (setq gnus-cache-buffer
410             (cons group
411                   (set-buffer (gnus-get-buffer-create
412                                " *gnus-cache-overview*"))))
413       ;; Insert the contents of this group's cache overview.
414       (erase-buffer)
415       (let ((file (gnus-cache-file-name group ".overview")))
416         (when (file-exists-p file)
417           (nnheader-insert-file-contents file)))
418       ;; We have a fresh (empty/just loaded) buffer,
419       ;; mark it as unmodified to save a redundant write later.
420       (set-buffer-modified-p nil))))
421
422 ;; Return whether an article is a member of a class.
423 (defun gnus-cache-member-of-class (class ticked dormant unread)
424   (or (and ticked (memq 'ticked class))
425       (and dormant (memq 'dormant class))
426       (and unread (memq 'unread class))
427       (and (not unread) (not ticked) (not dormant) (memq 'read class))))
428
429 (defun gnus-cache-file-name (group article)
430   (expand-file-name
431    (if (stringp article) article (int-to-string article))
432    (file-name-as-directory
433     (expand-file-name
434      (nnheader-translate-file-chars
435       (if (gnus-use-long-file-name 'not-cache)
436           group
437         (let ((group (nnheader-replace-duplicate-chars-in-string
438                       (nnheader-replace-chars-in-string group ?/ ?_)
439                       ?. ?_)))
440           ;; Translate the first colon into a slash.
441           (when (string-match ":" group)
442             (setq group (concat (substring group 0 (match-beginning 0))
443                                 "/" (substring group (match-end 0)))))
444           (nnheader-replace-chars-in-string group ?. ?/)))
445       t)
446      gnus-cache-directory))))
447
448 (defun gnus-cache-update-article (group article)
449   "If ARTICLE is in the cache, remove it and re-enter it."
450   (gnus-cache-change-buffer group)
451   (when (gnus-cache-possibly-remove-article article nil nil nil t)
452     (let ((gnus-use-cache nil))
453       (gnus-cache-possibly-enter-article
454        gnus-newsgroup-name article (gnus-summary-article-header article)
455        nil nil nil t))))
456
457 (defun gnus-cache-possibly-remove-article (article ticked dormant unread
458                                                    &optional force)
459   "Possibly remove ARTICLE from the cache."
460   (let ((group gnus-newsgroup-name)
461         (number article)
462         file)
463     ;; If this is a virtual group, we find the real group.
464     (when (gnus-virtual-group-p group)
465       (let ((result (nnvirtual-find-group-art
466                      (gnus-group-real-name group) article)))
467         (setq group (car result)
468               number (cdr result))))
469     (setq file (gnus-cache-file-name group number))
470     (when (and (file-exists-p file)
471                (or force
472                    (gnus-cache-member-of-class
473                     gnus-cache-remove-articles ticked dormant unread)))
474       (save-excursion
475         (delete-file file)
476         (set-buffer (cdr gnus-cache-buffer))
477         (goto-char (point-min))
478         (when (or (looking-at (concat (int-to-string number) "\t"))
479                   (search-forward (concat "\n" (int-to-string number) "\t")
480                                   (point-max) t))
481           (gnus-delete-line)))
482       (unless (setq gnus-newsgroup-cached
483                     (delq article gnus-newsgroup-cached))
484         (gnus-sethash gnus-newsgroup-name nil gnus-cache-active-hashtb)
485         (setq gnus-cache-active-altered t))
486       (gnus-summary-update-secondary-mark article)
487       t)))
488
489 (defun gnus-cache-articles-in-group (group)
490   "Return a sorted list of cached articles in GROUP."
491   (let ((dir (file-name-directory (gnus-cache-file-name group 1)))
492         articles)
493     (when (file-exists-p dir)
494       (setq articles
495             (sort (mapcar (lambda (name) (string-to-int name))
496                           (directory-files dir nil "^[0-9]+$" t))
497                   '<))
498       ;; Update the cache active file, just to synch more.
499       (if articles
500           (progn
501             (gnus-cache-update-active group (car articles) t)
502             (gnus-cache-update-active group (car (last articles))))
503         (when (gnus-gethash group gnus-cache-active-hashtb)
504           (gnus-sethash group nil gnus-cache-active-hashtb)
505           (setq gnus-cache-active-altered t)))
506       articles)))
507
508 (defun gnus-cache-braid-nov (group cached &optional file)
509   (let ((cache-buf (gnus-get-buffer-create " *gnus-cache*"))
510         beg end)
511     (gnus-cache-save-buffers)
512     (save-excursion
513       (set-buffer cache-buf)
514       (erase-buffer)
515       (let ((nnheader-file-coding-system gnus-cache-overview-coding-system))
516         (nnheader-insert-file-contents
517          (or file (gnus-cache-file-name group ".overview"))))
518       (goto-char (point-min))
519       (insert "\n")
520       (goto-char (point-min)))
521     (set-buffer nntp-server-buffer)
522     (goto-char (point-min))
523     (while cached
524       (while (and (not (eobp))
525                   (< (read (current-buffer)) (car cached)))
526         (forward-line 1))
527       (beginning-of-line)
528       (set-buffer cache-buf)
529       (if (search-forward (concat "\n" (int-to-string (car cached)) "\t")
530                           nil t)
531           (setq beg (gnus-point-at-bol)
532                 end (progn (end-of-line) (point)))
533         (setq beg nil))
534       (set-buffer nntp-server-buffer)
535       (when beg
536         (insert-buffer-substring cache-buf beg end)
537         (insert "\n"))
538       (setq cached (cdr cached)))
539     (kill-buffer cache-buf)))
540
541 (defun gnus-cache-braid-heads (group cached)
542   (let ((cache-buf (gnus-get-buffer-create " *gnus-cache*")))
543     (save-excursion
544       (set-buffer cache-buf)
545       (erase-buffer))
546     (set-buffer nntp-server-buffer)
547     (goto-char (point-min))
548     (while cached
549       (while (and (not (eobp))
550                   (looking-at "2.. +\\([0-9]+\\) ")
551                   (< (progn (goto-char (match-beginning 1))
552                             (read (current-buffer)))
553                      (car cached)))
554         (search-forward "\n.\n" nil 'move))
555       (beginning-of-line)
556       (save-excursion
557         (set-buffer cache-buf)
558         (erase-buffer)
559         (let ((nnheader-file-coding-system gnus-cache-coding-system))
560           (nnheader-insert-file-contents
561            (gnus-cache-file-name group (car cached))))
562         (goto-char (point-min))
563         (insert "220 ")
564         (princ (car cached) (current-buffer))
565         (insert " Article retrieved.\n")
566         (search-forward "\n\n" nil 'move)
567         (delete-region (point) (point-max))
568         (forward-char -1)
569         (insert "."))
570       (insert-buffer-substring cache-buf)
571       (setq cached (cdr cached)))
572     (kill-buffer cache-buf)))
573
574 ;;;###autoload
575 (defun gnus-jog-cache ()
576   "Go through all groups and put the articles into the cache.
577
578 Usage:
579 $ emacs -batch -l ~/.emacs -l gnus -f gnus-jog-cache"
580   (interactive)
581   (let ((gnus-mark-article-hook nil)
582         (gnus-expert-user t)
583         (nnmail-spool-file nil)
584         (mail-sources nil)
585         (gnus-use-dribble-file nil)
586         (gnus-novice-user nil)
587         (gnus-large-newsgroup nil))
588     ;; Start Gnus.
589     (gnus)
590     ;; Go through all groups...
591     (gnus-group-mark-buffer)
592     (gnus-group-iterate nil
593       (lambda (group)
594         (let (gnus-auto-select-next)
595           (gnus-summary-read-group group nil t)
596           ;; ... and enter the articles into the cache.
597           (when (eq major-mode 'gnus-summary-mode)
598             (gnus-uu-mark-buffer)
599             (gnus-cache-enter-article)
600             (kill-buffer (current-buffer))))))))
601
602 (defun gnus-cache-read-active (&optional force)
603   "Read the cache active file."
604   (gnus-make-directory gnus-cache-directory)
605   (if (or (not (file-exists-p gnus-cache-active-file))
606           (zerop (nth 7 (file-attributes gnus-cache-active-file)))
607           force)
608       ;; There is no active file, so we generate one.
609       (gnus-cache-generate-active)
610     ;; We simply read the active file.
611     (save-excursion
612       (gnus-set-work-buffer)
613       (nnheader-insert-file-contents gnus-cache-active-file)
614       (gnus-active-to-gnus-format
615        nil (setq gnus-cache-active-hashtb
616                  (gnus-make-hashtable
617                   (count-lines (point-min) (point-max)))))
618       (setq gnus-cache-active-altered nil))))
619
620 (defun gnus-cache-write-active (&optional force)
621   "Write the active hashtb to the active file."
622   (when (or force
623             (and gnus-cache-active-hashtb
624                  gnus-cache-active-altered))
625     (gnus-write-active-file gnus-cache-active-file gnus-cache-active-hashtb t)
626     ;; Mark the active hashtb as unaltered.
627     (setq gnus-cache-active-altered nil)))
628
629 (defun gnus-cache-possibly-update-active (group active)
630   "Update active info bounds of GROUP with ACTIVE if necessary.
631 The update is performed if ACTIVE contains a higher or lower bound
632 than the current."
633   (let ((lower t) (higher t))
634     (if gnus-cache-active-hashtb
635         (let ((cache-active (gnus-gethash group gnus-cache-active-hashtb)))
636           (when cache-active
637             (unless (< (car active) (car cache-active))
638               (setq lower nil))
639             (unless (> (cdr active) (cdr cache-active))
640               (setq higher nil))))
641       (gnus-cache-read-active))
642     (when lower
643       (gnus-cache-update-active group (car active) t))
644     (when higher
645       (gnus-cache-update-active group (cdr active)))))
646
647 (defun gnus-cache-update-active (group number &optional low)
648   "Update the upper bound of the active info of GROUP to NUMBER.
649 If LOW, update the lower bound instead."
650   (let ((active (gnus-gethash group gnus-cache-active-hashtb)))
651     (if (null active)
652         ;; We just create a new active entry for this group.
653         (gnus-sethash group (cons number number) gnus-cache-active-hashtb)
654       ;; Update the lower or upper bound.
655       (if low
656           (setcar active number)
657         (setcdr active number)))
658     ;; Mark the active hashtb as altered.
659     (setq gnus-cache-active-altered t)))
660
661 ;;;###autoload
662 (defun gnus-cache-generate-active (&optional directory)
663   "Generate the cache active file."
664   (interactive)
665   (let* ((top (null directory))
666          (directory (expand-file-name (or directory gnus-cache-directory)))
667          (files (directory-files directory 'full))
668          (group
669           (if top
670               ""
671             (string-match
672              (concat "^" (regexp-quote
673                           (file-name-as-directory
674                            (expand-file-name gnus-cache-directory))))
675              (directory-file-name directory))
676             (nnheader-replace-chars-in-string
677              (substring (directory-file-name directory) (match-end 0))
678              ?/ ?.)))
679          nums alphs)
680     (when top
681       (gnus-message 5 "Generating the cache active file...")
682       (setq gnus-cache-active-hashtb (gnus-make-hashtable 123)))
683     (when (string-match "^\\(nn[^_]+\\)_" group)
684       (setq group (replace-match "\\1:" t nil group)))
685     ;; Separate articles from all other files and directories.
686     (while files
687       (if (string-match "^[0-9]+$" (file-name-nondirectory (car files)))
688           (push (string-to-int (file-name-nondirectory (pop files))) nums)
689         (push (pop files) alphs)))
690     ;; If we have nums, then this is probably a valid group.
691     (when (setq nums (sort nums '<))
692       (gnus-sethash group (cons (car nums) (gnus-last-element nums))
693                     gnus-cache-active-hashtb))
694     ;; Go through all the other files.
695     (while alphs
696       (when (and (file-directory-p (car alphs))
697                  (not (string-match "^\\."
698                                     (file-name-nondirectory (car alphs)))))
699         ;; We descend directories.
700         (gnus-cache-generate-active (car alphs)))
701       (setq alphs (cdr alphs)))
702     ;; Write the new active file.
703     (when top
704       (gnus-cache-write-active t)
705       (gnus-message 5 "Generating the cache active file...done"))))
706
707 ;;;###autoload
708 (defun gnus-cache-generate-nov-databases (dir)
709   "Generate NOV files recursively starting in DIR."
710   (interactive (list gnus-cache-directory))
711   (gnus-cache-close)
712   (let ((nnml-generate-active-function 'identity))
713     (nnml-generate-nov-databases-1 dir))
714   (gnus-cache-open))
715
716 (defun gnus-cache-move-cache (dir)
717   "Move the cache tree to somewhere else."
718   (interactive "FMove the cache tree to: ")
719   (rename-file gnus-cache-directory dir))
720
721 (defun gnus-cache-fully-p (&optional group)
722   "Returns non-nil if the cache should be fully used.
723 If GROUP is non-nil, also cater to `gnus-cacheable-groups' and
724 `gnus-uncacheable-groups'."
725   (and gnus-use-cache
726        (not (eq gnus-use-cache 'passive))
727        (if (null group)
728            t
729          (and (or (not gnus-cacheable-groups)
730                   (string-match gnus-cacheable-groups group))
731               (or (not gnus-uncacheable-groups)
732                   (not (string-match gnus-uncacheable-groups group)))))))
733
734 (provide 'gnus-cache)
735
736 ;;; gnus-cache.el ends here