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