Indent.
[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
456                    (gnus-cache-member-of-class
457                     gnus-cache-remove-articles ticked dormant unread)))
458       (save-excursion
459         (delete-file file)
460         (set-buffer (cdr gnus-cache-buffer))
461         (goto-char (point-min))
462         (when (or (looking-at (concat (int-to-string number) "\t"))
463                   (search-forward (concat "\n" (int-to-string number) "\t")
464                                   (point-max) t))
465           (delete-region (progn (beginning-of-line) (point))
466                          (progn (forward-line 1) (point)))))
467       (unless (setq gnus-newsgroup-cached
468                     (delq article gnus-newsgroup-cached))
469         (gnus-sethash gnus-newsgroup-name nil gnus-cache-active-hashtb)
470         (setq gnus-cache-active-altered t))
471       (gnus-summary-update-secondary-mark article)
472       t)))
473
474 (defun gnus-cache-articles-in-group (group)
475   "Return a sorted list of cached articles in GROUP."
476   (let ((dir (file-name-directory (gnus-cache-file-name group 1)))
477         articles)
478     (when (file-exists-p dir)
479       (setq articles
480             (sort (mapcar (lambda (name) (string-to-int name))
481                           (directory-files dir nil "^[0-9]+$" t))
482                   '<))
483       ;; Update the cache active file, just to synch more.
484       (if articles
485           (progn
486             (gnus-cache-update-active group (car articles) t)
487             (gnus-cache-update-active group (car (last articles))))
488         (when (gnus-gethash group gnus-cache-active-hashtb)
489           (gnus-sethash group nil gnus-cache-active-hashtb)
490           (setq gnus-cache-active-altered t)))
491       articles)))
492
493 (defun gnus-cache-braid-nov (group cached &optional file)
494   (let ((cache-buf (gnus-get-buffer-create " *gnus-cache*"))
495         beg end)
496     (gnus-cache-save-buffers)
497     (save-excursion
498       (set-buffer cache-buf)
499       (erase-buffer)
500       (let ((coding-system-for-read
501              gnus-cache-overview-coding-system))
502         (insert-file-contents
503          (or file (gnus-cache-file-name group ".overview"))))
504       (goto-char (point-min))
505       (insert "\n")
506       (goto-char (point-min)))
507     (set-buffer nntp-server-buffer)
508     (goto-char (point-min))
509     (while cached
510       (while (and (not (eobp))
511                   (< (read (current-buffer)) (car cached)))
512         (forward-line 1))
513       (beginning-of-line)
514       (set-buffer cache-buf)
515       (if (search-forward (concat "\n" (int-to-string (car cached)) "\t")
516                           nil t)
517           (setq beg (progn (beginning-of-line) (point))
518                 end (progn (end-of-line) (point)))
519         (setq beg nil))
520       (set-buffer nntp-server-buffer)
521       (when beg
522         (insert-buffer-substring cache-buf beg end)
523         (insert "\n"))
524       (setq cached (cdr cached)))
525     (kill-buffer cache-buf)))
526
527 (defun gnus-cache-braid-heads (group cached)
528   (let ((cache-buf (gnus-get-buffer-create " *gnus-cache*")))
529     (save-excursion
530       (set-buffer cache-buf)
531       (erase-buffer))
532     (set-buffer nntp-server-buffer)
533     (goto-char (point-min))
534     (while cached
535       (while (and (not (eobp))
536                   (looking-at "2.. +\\([0-9]+\\) ")
537                   (< (progn (goto-char (match-beginning 1))
538                             (read (current-buffer)))
539                      (car cached)))
540         (search-forward "\n.\n" nil 'move))
541       (beginning-of-line)
542       (set-buffer cache-buf)
543       (erase-buffer)
544       (let ((coding-system-for-read
545              gnus-cache-coding-system))
546         (insert-file-contents (gnus-cache-file-name group (car cached))))
547       (goto-char (point-min))
548       (insert "220 ")
549       (princ (car cached) (current-buffer))
550       (insert " Article retrieved.\n")
551       (search-forward "\n\n" nil 'move)
552       (delete-region (point) (point-max))
553       (forward-char -1)
554       (insert ".")
555       (set-buffer nntp-server-buffer)
556       (insert-buffer-substring cache-buf)
557       (setq cached (cdr cached)))
558     (kill-buffer cache-buf)))
559
560 ;;;###autoload
561 (defun gnus-jog-cache ()
562   "Go through all groups and put the articles into the cache.
563
564 Usage:
565 $ emacs -batch -l ~/.emacs -l gnus -f gnus-jog-cache"
566   (interactive)
567   (let ((gnus-mark-article-hook nil)
568         (gnus-expert-user t)
569         (nnmail-spool-file nil)
570         (mail-sources nil)
571         (gnus-use-dribble-file nil)
572         (gnus-novice-user nil)
573         (gnus-large-newsgroup nil))
574     ;; Start Gnus.
575     (gnus)
576     ;; Go through all groups...
577     (gnus-group-mark-buffer)
578     (gnus-group-iterate nil
579       (lambda (group)
580         (let (gnus-auto-select-next)
581           (gnus-summary-read-group group nil t)
582           ;; ... and enter the articles into the cache.
583           (when (eq major-mode 'gnus-summary-mode)
584             (gnus-uu-mark-buffer)
585             (gnus-cache-enter-article)
586             (kill-buffer (current-buffer))))))))
587
588 (defun gnus-cache-read-active (&optional force)
589   "Read the cache active file."
590   (gnus-make-directory gnus-cache-directory)
591   (if (or (not (file-exists-p gnus-cache-active-file))
592           (zerop (nth 7 (file-attributes gnus-cache-active-file)))
593           force)
594       ;; There is no active file, so we generate one.
595       (gnus-cache-generate-active)
596     ;; We simply read the active file.
597     (save-excursion
598       (gnus-set-work-buffer)
599       (nnheader-insert-file-contents gnus-cache-active-file)
600       (gnus-active-to-gnus-format
601        nil (setq gnus-cache-active-hashtb
602                  (gnus-make-hashtable
603                   (count-lines (point-min) (point-max)))))
604       (setq gnus-cache-active-altered nil))))
605
606 (defun gnus-cache-write-active (&optional force)
607   "Write the active hashtb to the active file."
608   (when (or force
609             (and gnus-cache-active-hashtb
610                  gnus-cache-active-altered))
611     (gnus-write-active-file gnus-cache-active-file gnus-cache-active-hashtb t)
612     ;; Mark the active hashtb as unaltered.
613     (setq gnus-cache-active-altered nil)))
614
615 (defun gnus-cache-possibly-update-active (group active)
616   "Update active info bounds of GROUP with ACTIVE if necessary.
617 The update is performed if ACTIVE contains a higher or lower bound
618 than the current."
619   (let ((lower t) (higher t))
620     (if gnus-cache-active-hashtb
621         (let ((cache-active (gnus-gethash group gnus-cache-active-hashtb)))
622           (when cache-active
623             (unless (< (car active) (car cache-active))
624               (setq lower nil))
625             (unless (> (cdr active) (cdr cache-active))
626               (setq higher nil))))
627       (gnus-cache-read-active))
628     (when lower
629       (gnus-cache-update-active group (car active) t))
630     (when higher
631       (gnus-cache-update-active group (cdr active)))))
632
633 (defun gnus-cache-update-active (group number &optional low)
634   "Update the upper bound of the active info of GROUP to NUMBER.
635 If LOW, update the lower bound instead."
636   (let ((active (gnus-gethash group gnus-cache-active-hashtb)))
637     (if (null active)
638         ;; We just create a new active entry for this group.
639         (gnus-sethash group (cons number number) gnus-cache-active-hashtb)
640       ;; Update the lower or upper bound.
641       (if low
642           (setcar active number)
643         (setcdr active number)))
644     ;; Mark the active hashtb as altered.
645     (setq gnus-cache-active-altered t)))
646
647 ;;;###autoload
648 (defun gnus-cache-generate-active (&optional directory)
649   "Generate the cache active file."
650   (interactive)
651   (let* ((top (null directory))
652          (directory (expand-file-name (or directory gnus-cache-directory)))
653          (files (directory-files directory 'full))
654          (group
655           (if top
656               ""
657             (string-match
658              (concat "^" (regexp-quote
659                           (file-name-as-directory
660                            (expand-file-name gnus-cache-directory))))
661              (directory-file-name directory))
662             (nnheader-replace-chars-in-string
663              (substring (directory-file-name directory) (match-end 0))
664              ?/ ?.)))
665          nums alphs)
666     (when top
667       (gnus-message 5 "Generating the cache active file...")
668       (setq gnus-cache-active-hashtb (gnus-make-hashtable 123)))
669     (when (string-match "^\\(nn[^_]+\\)_" group)
670       (setq group (replace-match "\\1:" t t group)))
671     ;; Separate articles from all other files and directories.
672     (while files
673       (if (string-match "^[0-9]+$" (file-name-nondirectory (car files)))
674           (push (string-to-int (file-name-nondirectory (pop files))) nums)
675         (push (pop files) alphs)))
676     ;; If we have nums, then this is probably a valid group.
677     (when (setq nums (sort nums '<))
678       (gnus-sethash group (cons (car nums) (gnus-last-element nums))
679                     gnus-cache-active-hashtb))
680     ;; Go through all the other files.
681     (while alphs
682       (when (and (file-directory-p (car alphs))
683                  (not (string-match "^\\."
684                                     (file-name-nondirectory (car alphs)))))
685         ;; We descend directories.
686         (gnus-cache-generate-active (car alphs)))
687       (setq alphs (cdr alphs)))
688     ;; Write the new active file.
689     (when top
690       (gnus-cache-write-active t)
691       (gnus-message 5 "Generating the cache active file...done"))))
692
693 ;;;###autoload
694 (defun gnus-cache-generate-nov-databases (dir)
695   "Generate NOV files recursively starting in DIR."
696   (interactive (list gnus-cache-directory))
697   (gnus-cache-close)
698   (let ((nnml-generate-active-function 'identity))
699     (nnml-generate-nov-databases-1 dir))
700   (gnus-cache-open))
701
702 (defun gnus-cache-move-cache (dir)
703   "Move the cache tree to somewhere else."
704   (interactive "FMove the cache tree to: ")
705   (rename-file gnus-cache-directory dir))
706
707 (defun gnus-cache-fully-p (&optional group)
708   "Returns non-nil if the cache should be fully used.
709 If GROUP is non-nil, also cater to `gnus-cacheable-groups' and
710 `gnus-uncacheable-groups'."
711   (and gnus-use-cache
712        (not (eq gnus-use-cache 'passive))
713        (if (null group)
714            t
715          (and (or (not gnus-cacheable-groups)
716                   (string-match gnus-cacheable-groups group))
717               (or (not gnus-uncacheable-groups)
718                   (not (string-match gnus-uncacheable-groups group)))))))
719
720 (provide 'gnus-cache)
721
722 ;;; gnus-cache.el ends here