Update copyright year to 2014 by running admin/update-copyright.
[emacs.git] / lisp / gnus / gnus-cache.el
blobd58acbd18cabade4aac69144cd8119a30a702d74
1 ;;; gnus-cache.el --- cache interface for Gnus
3 ;; Copyright (C) 1995-2014 Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news
8 ;; This file is part of GNU Emacs.
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 3 of the License, or
13 ;; (at your option) any later version.
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.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;; Commentary:
25 ;;; Code:
27 ;; For Emacs <22.2 and XEmacs.
28 (eval-and-compile
29 (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
31 (eval-when-compile (require 'cl))
33 (require 'gnus)
34 (require 'gnus-sum)
36 (eval-when-compile
37 (unless (fboundp 'gnus-agent-load-alist)
38 (defun gnus-agent-load-alist (group))))
40 (defcustom gnus-cache-active-file
41 (expand-file-name "active" gnus-cache-directory)
42 "*The cache active file."
43 :group 'gnus-cache
44 :type 'file)
46 (defcustom gnus-cache-enter-articles '(ticked dormant)
47 "Classes of articles to enter into the cache."
48 :group 'gnus-cache
49 :type '(set (const ticked) (const dormant) (const unread) (const read)))
51 (defcustom gnus-cache-remove-articles '(read)
52 "Classes of articles to remove from the cache."
53 :group 'gnus-cache
54 :type '(set (const ticked) (const dormant) (const unread) (const read)))
56 (defcustom gnus-cacheable-groups nil
57 "*Groups that match this regexp will be cached.
59 If you only want to cache your nntp groups, you could set this
60 variable to \"^nntp\".
62 If a group matches both gnus-cacheable-groups and gnus-uncacheable-groups
63 it's not cached."
64 :group 'gnus-cache
65 :type '(choice (const :tag "off" nil)
66 regexp))
68 (defcustom gnus-uncacheable-groups nil
69 "*Groups that match this regexp will not be cached.
71 If you want to avoid caching your nnml groups, you could set this
72 variable to \"^nnml\".
74 If a group matches both gnus-cacheable-groups and gnus-uncacheable-groups
75 it's not cached."
76 :group 'gnus-cache
77 :type '(choice (const :tag "off" nil)
78 regexp))
80 (defvar gnus-cache-overview-coding-system 'raw-text
81 "Coding system used on Gnus cache files.")
83 (defvar gnus-cache-coding-system 'raw-text
84 "Coding system used on Gnus cache files.")
88 ;;; Internal variables.
90 (defvar gnus-cache-removable-articles nil)
91 (defvar gnus-cache-buffer nil)
92 (defvar gnus-cache-active-hashtb nil)
93 (defvar gnus-cache-active-altered nil)
94 (defvar gnus-cache-total-fetched-hashtb nil)
96 (declare-function nnvirtual-find-group-art "nnvirtual" (group article))
98 (autoload 'nnml-generate-nov-databases-directory "nnml")
99 (autoload 'nnvirtual-find-group-art "nnvirtual")
103 ;;; Functions called from Gnus.
105 (defun gnus-cache-open ()
106 "Initialize the cache."
107 (when (or (file-exists-p gnus-cache-directory)
108 (and gnus-use-cache
109 (not (eq gnus-use-cache 'passive))))
110 (gnus-cache-read-active)))
112 ;; Complexities of byte-compiling make this kludge necessary. Eeek.
113 (ignore-errors
114 (gnus-add-shutdown 'gnus-cache-close 'gnus))
116 (defun gnus-cache-close ()
117 "Shut down the cache."
118 (gnus-cache-write-active)
119 (gnus-cache-save-buffers)
120 (setq gnus-cache-active-hashtb nil))
122 (defun gnus-cache-save-buffers ()
123 ;; save the overview buffer if it exists and has been modified
124 ;; delete empty cache subdirectories
125 (when gnus-cache-buffer
126 (let ((buffer (cdr gnus-cache-buffer))
127 (overview-file (gnus-cache-file-name
128 (car gnus-cache-buffer) ".overview")))
129 ;; write the overview only if it was modified
130 (when (and (buffer-live-p buffer) (buffer-modified-p buffer))
131 (with-current-buffer buffer
132 (if (> (buffer-size) 0)
133 ;; Non-empty overview, write it to a file.
134 (let ((coding-system-for-write
135 gnus-cache-overview-coding-system))
136 (gnus-write-buffer overview-file))
137 (let ((file-name-coding-system nnmail-pathname-coding-system))
138 ;; Empty overview file, remove it
139 (when (file-exists-p overview-file)
140 (delete-file overview-file))
141 ;; If possible, remove group's cache subdirectory.
142 (condition-case nil
143 ;; FIXME: we can detect the error type and warn the user
144 ;; of any inconsistencies (articles w/o nov entries?).
145 ;; for now, just be conservative...delete only if safe -- sj
146 (delete-directory (file-name-directory overview-file))
147 (error))))
149 (gnus-cache-update-overview-total-fetched-for
150 (car gnus-cache-buffer) overview-file)))
151 ;; Kill the buffer -- it's either unmodified or saved.
152 (gnus-kill-buffer buffer)
153 (setq gnus-cache-buffer nil))))
155 (defun gnus-cache-possibly-enter-article
156 (group article ticked dormant unread &optional force)
157 (when (and (or force (not (eq gnus-use-cache 'passive)))
158 (numberp article)
159 (> article 0)) ; This might be a dummy article.
160 (let ((number article)
161 file headers lines-chars
162 (file-name-coding-system nnmail-pathname-coding-system))
163 ;; If this is a virtual group, we find the real group.
164 (when (gnus-virtual-group-p group)
165 (let ((result (nnvirtual-find-group-art
166 (gnus-group-real-name group) article)))
167 (setq group (car result)
168 number (cdr result))))
169 (when (and number
170 (> number 0) ; Reffed article.
171 (or force
172 (and (gnus-cache-fully-p group)
173 (gnus-cache-member-of-class
174 gnus-cache-enter-articles ticked dormant unread)))
175 (not (file-exists-p (setq file (gnus-cache-file-name
176 group number)))))
177 ;; Possibly create the cache directory.
178 (gnus-make-directory (file-name-directory file))
179 ;; Save the article in the cache.
180 (if (file-exists-p file)
181 t ; The article already is saved.
182 (with-current-buffer nntp-server-buffer
183 (require 'gnus-art)
184 (let ((gnus-use-cache nil)
185 (gnus-article-decode-hook nil))
186 (gnus-request-article-this-buffer number group))
187 (when (> (buffer-size) 0)
188 (let ((coding-system-for-write gnus-cache-coding-system))
189 (gnus-write-buffer file)
190 (gnus-cache-update-file-total-fetched-for group file))
191 (setq lines-chars (nnheader-get-lines-and-char))
192 (nnheader-remove-body)
193 (setq headers (nnheader-parse-naked-head))
194 (mail-header-set-number headers number)
195 (mail-header-set-lines headers (car lines-chars))
196 (mail-header-set-chars headers (cadr lines-chars))
197 (gnus-cache-change-buffer group)
198 (set-buffer (cdr gnus-cache-buffer))
199 (goto-char (point-max))
200 (forward-line -1)
201 (while (condition-case ()
202 (when (not (bobp))
203 (> (read (current-buffer)) number))
204 (error
205 ;; The line was malformed, so we just remove it!!
206 (gnus-delete-line)
208 (forward-line -1))
209 (if (bobp)
210 (if (not (eobp))
211 (progn
212 (beginning-of-line)
213 (when (< (read (current-buffer)) number)
214 (forward-line 1)))
215 (beginning-of-line))
216 (forward-line 1))
217 (beginning-of-line)
218 (nnheader-insert-nov headers)
219 ;; Update the active info.
220 (set-buffer gnus-summary-buffer)
221 (gnus-cache-possibly-update-active group (cons number number))
222 (setq gnus-newsgroup-cached
223 (gnus-add-to-sorted-list gnus-newsgroup-cached article))
224 (gnus-summary-update-secondary-mark article))
225 t))))))
227 (defun gnus-cache-enter-remove-article (article)
228 "Mark ARTICLE for later possible removal."
229 (when article
230 (push article gnus-cache-removable-articles)))
232 (defun gnus-cache-possibly-remove-articles ()
233 "Possibly remove some of the removable articles."
234 (if (not (gnus-virtual-group-p gnus-newsgroup-name))
235 (gnus-cache-possibly-remove-articles-1)
236 (let ((arts gnus-cache-removable-articles)
238 (while arts
239 (when (setq ga (nnvirtual-find-group-art
240 (gnus-group-real-name gnus-newsgroup-name) (pop arts)))
241 (let ((gnus-cache-removable-articles (list (cdr ga)))
242 (gnus-newsgroup-name (car ga)))
243 (gnus-cache-possibly-remove-articles-1)))))
244 (setq gnus-cache-removable-articles nil)))
246 (defun gnus-cache-possibly-remove-articles-1 ()
247 "Possibly remove some of the removable articles."
248 (when (gnus-cache-fully-p gnus-newsgroup-name)
249 (let ((cache-articles gnus-newsgroup-cached))
250 (gnus-cache-change-buffer gnus-newsgroup-name)
251 (dolist (article gnus-cache-removable-articles)
252 (when (memq article cache-articles)
253 ;; The article was in the cache, so we see whether we are
254 ;; supposed to remove it from the cache.
255 (gnus-cache-possibly-remove-article
256 article (memq article gnus-newsgroup-marked)
257 (memq article gnus-newsgroup-dormant)
258 (or (memq article gnus-newsgroup-unreads)
259 (memq article gnus-newsgroup-unselected))))))
260 ;; The overview file might have been modified, save it
261 ;; safe because we're only called at group exit anyway.
262 (gnus-cache-save-buffers)))
264 (defun gnus-cache-request-article (article group)
265 "Retrieve ARTICLE in GROUP from the cache."
266 (let ((file (gnus-cache-file-name group article))
267 (buffer-read-only nil)
268 (file-name-coding-system nnmail-pathname-coding-system))
269 (when (file-exists-p file)
270 (erase-buffer)
271 (gnus-kill-all-overlays)
272 (let ((coding-system-for-read gnus-cache-coding-system))
273 (insert-file-contents file))
274 t)))
276 (defun gnus-cache-possibly-alter-active (group active)
277 "Alter the ACTIVE info for GROUP to reflect the articles in the cache."
278 (when gnus-cache-active-hashtb
279 (let ((cache-active (gnus-gethash group gnus-cache-active-hashtb)))
280 (when cache-active
281 (when (< (car cache-active) (car active))
282 (setcar active (car cache-active)))
283 (when (> (cdr cache-active) (cdr active))
284 (setcdr active (cdr cache-active)))))))
286 (defun gnus-cache-retrieve-headers (articles group &optional fetch-old)
287 "Retrieve the headers for ARTICLES in GROUP."
288 (let ((cached
289 (setq gnus-newsgroup-cached (gnus-cache-articles-in-group group))))
290 (if (not cached)
291 ;; No cached articles here, so we just retrieve them
292 ;; the normal way.
293 (let ((gnus-use-cache nil))
294 (gnus-retrieve-headers articles group fetch-old))
295 (let ((uncached-articles (gnus-sorted-difference articles cached))
296 (cache-file (gnus-cache-file-name group ".overview"))
297 type
298 (file-name-coding-system nnmail-pathname-coding-system))
299 ;; We first retrieve all the headers that we don't have in
300 ;; the cache.
301 (let ((gnus-use-cache nil))
302 (when uncached-articles
303 (setq type (and articles
304 (gnus-retrieve-headers
305 uncached-articles group fetch-old)))))
306 (gnus-cache-save-buffers)
307 ;; Then we insert the cached headers.
308 (save-excursion
309 (cond
310 ((not (file-exists-p cache-file))
311 ;; There are no cached headers.
312 type)
313 ((null type)
314 ;; There were no uncached headers (or retrieval was
315 ;; unsuccessful), so we use the cached headers exclusively.
316 (set-buffer nntp-server-buffer)
317 (erase-buffer)
318 (let ((coding-system-for-read
319 gnus-cache-overview-coding-system))
320 (insert-file-contents cache-file))
321 'nov)
322 ((eq type 'nov)
323 ;; We have both cached and uncached NOV headers, so we
324 ;; braid them.
325 (gnus-cache-braid-nov group cached)
326 type)
328 ;; We braid HEADs.
329 (gnus-cache-braid-heads group (gnus-sorted-intersection
330 cached articles))
331 type)))))))
333 (defun gnus-cache-enter-article (&optional n)
334 "Enter the next N articles into the cache.
335 If not given a prefix, use the process marked articles instead.
336 Returns the list of articles entered."
337 (interactive "P")
338 (let (out)
339 (dolist (article (gnus-summary-work-articles n))
340 (gnus-summary-remove-process-mark article)
341 (if (natnump article)
342 (when (gnus-cache-possibly-enter-article
343 gnus-newsgroup-name article
344 nil nil nil t)
345 (setq gnus-newsgroup-undownloaded (delq article gnus-newsgroup-undownloaded))
346 (push article out))
347 (gnus-message 2 "Can't cache article %d" article))
348 (gnus-summary-update-download-mark article)
349 (gnus-summary-update-secondary-mark article))
350 (gnus-summary-next-subject 1)
351 (gnus-summary-position-point)
352 (nreverse out)))
354 (defun gnus-cache-remove-article (&optional n)
355 "Remove the next N articles from the cache.
356 If not given a prefix, use the process marked articles instead.
357 Returns the list of articles removed."
358 (interactive "P")
359 (gnus-cache-change-buffer gnus-newsgroup-name)
360 (let (out)
361 (dolist (article (gnus-summary-work-articles n))
362 (gnus-summary-remove-process-mark article)
363 (when (gnus-cache-possibly-remove-article article nil nil nil t)
364 (when gnus-newsgroup-agentized
365 (let ((alist (gnus-agent-load-alist gnus-newsgroup-name)))
366 (unless (cdr (assoc article alist))
367 (setq gnus-newsgroup-undownloaded
368 (gnus-add-to-sorted-list
369 gnus-newsgroup-undownloaded article)))))
370 (push article out))
371 (gnus-summary-update-download-mark article)
372 (gnus-summary-update-secondary-mark article))
373 (gnus-summary-next-subject 1)
374 (gnus-summary-position-point)
375 (nreverse out)))
377 (defun gnus-cached-article-p (article)
378 "Say whether ARTICLE is cached in the current group."
379 (memq article gnus-newsgroup-cached))
381 (defun gnus-summary-insert-cached-articles ()
382 "Insert all the articles cached for this group into the current buffer."
383 (interactive)
384 (let ((gnus-verbose (max 6 gnus-verbose)))
385 (cond
386 ((not gnus-newsgroup-cached)
387 (gnus-message 3 "No cached articles for this group"))
388 ;; This is faster if there are few articles to insert.
389 ((< (length gnus-newsgroup-cached) 20)
390 (gnus-summary-goto-subjects gnus-newsgroup-cached))
392 (gnus-summary-include-articles gnus-newsgroup-cached)))))
394 (defun gnus-summary-limit-include-cached ()
395 "Limit the summary buffer to articles that are cached."
396 (interactive)
397 (let ((gnus-verbose (max 6 gnus-verbose)))
398 (if gnus-newsgroup-cached
399 (progn
400 (gnus-summary-limit gnus-newsgroup-cached)
401 (gnus-summary-position-point))
402 (gnus-message 3 "No cached articles for this group"))))
404 ;;; Internal functions.
406 (defun gnus-cache-change-buffer (group)
407 (and gnus-cache-buffer
408 ;; See if the current group's overview cache has been loaded.
409 (or (string= group (car gnus-cache-buffer))
410 ;; Another overview cache is current, save it.
411 (gnus-cache-save-buffers)))
412 ;; if gnus-cache buffer is nil, create it
413 (unless gnus-cache-buffer
414 ;; Create cache buffer
415 (save-excursion
416 (setq gnus-cache-buffer
417 (cons group
418 (set-buffer (gnus-get-buffer-create
419 " *gnus-cache-overview*"))))
420 ;; Insert the contents of this group's cache overview.
421 (erase-buffer)
422 (let ((file (gnus-cache-file-name group ".overview"))
423 (file-name-coding-system nnmail-pathname-coding-system))
424 (when (file-exists-p file)
425 (nnheader-insert-file-contents file)))
426 ;; We have a fresh (empty/just loaded) buffer,
427 ;; mark it as unmodified to save a redundant write later.
428 (set-buffer-modified-p nil))))
430 ;; Return whether an article is a member of a class.
431 (defun gnus-cache-member-of-class (class ticked dormant unread)
432 (or (and ticked (memq 'ticked class))
433 (and dormant (memq 'dormant class))
434 (and unread (memq 'unread class))
435 (and (not unread) (not ticked) (not dormant) (memq 'read class))))
437 (defvar gnus-cache-decoded-group-names nil
438 "Alist of original group names and decoded group names.
439 Decoding is done according to `gnus-group-name-charset-method-alist'
440 or `gnus-group-name-charset-group-alist'.")
442 (defvar gnus-cache-unified-group-names nil
443 "Alist of unified decoded group names and original group names.
444 A group name is decoded according to
445 `gnus-group-name-charset-method-alist' or
446 `gnus-group-name-charset-group-alist' first, and is encoded and
447 decoded again according to `nnmail-pathname-coding-system',
448 `file-name-coding-system', or `default-file-name-coding-system'.
450 It is used when asking for a original group name from a cache
451 directory name, in which non-ASCII characters might have been unified
452 into the ones of a certain charset particularly if the `utf-8' coding
453 system for example was used.")
455 (defun gnus-cache-decoded-group-name (group)
456 "Return a decoded group name of GROUP."
457 (or (cdr (assoc group gnus-cache-decoded-group-names))
458 (let ((decoded (gnus-group-decoded-name group))
459 (coding (or nnmail-pathname-coding-system
460 (and (boundp 'file-name-coding-system)
461 file-name-coding-system)
462 (and (boundp 'default-file-name-coding-system)
463 default-file-name-coding-system))))
464 (push (cons group decoded) gnus-cache-decoded-group-names)
465 (push (cons (mm-decode-coding-string
466 (mm-encode-coding-string decoded coding)
467 coding)
468 group)
469 gnus-cache-unified-group-names)
470 decoded)))
472 (defun gnus-cache-file-name (group article)
473 (setq group (gnus-cache-decoded-group-name group))
474 (expand-file-name
475 (if (stringp article) article (int-to-string article))
476 (file-name-as-directory
477 (expand-file-name
478 (nnheader-translate-file-chars
479 (if (gnus-use-long-file-name 'not-cache)
480 group
481 (let ((group (nnheader-replace-duplicate-chars-in-string
482 (nnheader-replace-chars-in-string group ?/ ?_)
483 ?. ?_)))
484 ;; Translate the first colon into a slash.
485 (when (string-match ":" group)
486 (setq group (concat (substring group 0 (match-beginning 0))
487 "/" (substring group (match-end 0)))))
488 (nnheader-replace-chars-in-string group ?. ?/)))
490 gnus-cache-directory))))
492 (defun gnus-cache-update-article (group article)
493 "If ARTICLE is in the cache, remove it and re-enter it."
494 (gnus-cache-change-buffer group)
495 (when (gnus-cache-possibly-remove-article article nil nil nil t)
496 (let ((gnus-use-cache nil))
497 (gnus-cache-possibly-enter-article
498 gnus-newsgroup-name article
499 nil nil nil t))))
501 (defun gnus-cache-possibly-remove-article (article ticked dormant unread
502 &optional force)
503 "Possibly remove ARTICLE from the cache."
504 (let ((group gnus-newsgroup-name)
505 (number article)
506 file
507 (file-name-coding-system nnmail-pathname-coding-system))
508 ;; If this is a virtual group, we find the real group.
509 (when (gnus-virtual-group-p group)
510 (let ((result (nnvirtual-find-group-art
511 (gnus-group-real-name group) article)))
512 (setq group (car result)
513 number (cdr result))))
514 (setq file (gnus-cache-file-name group number))
515 (when (and (file-exists-p file)
516 (or force
517 (gnus-cache-member-of-class
518 gnus-cache-remove-articles ticked dormant unread)))
519 (save-excursion
520 (gnus-cache-update-file-total-fetched-for group file t)
521 (delete-file file)
523 (set-buffer (cdr gnus-cache-buffer))
524 (goto-char (point-min))
525 (when (or (looking-at (concat (int-to-string number) "\t"))
526 (search-forward (concat "\n" (int-to-string number) "\t")
527 (point-max) t))
528 (gnus-delete-line)))
529 (unless (setq gnus-newsgroup-cached
530 (delq article gnus-newsgroup-cached))
531 (gnus-sethash gnus-newsgroup-name nil gnus-cache-active-hashtb)
532 (setq gnus-cache-active-altered t))
533 (gnus-summary-update-secondary-mark article)
534 t)))
536 (defun gnus-cache-articles-in-group (group)
537 "Return a sorted list of cached articles in GROUP."
538 (let ((dir (file-name-directory (gnus-cache-file-name group 1)))
539 articles
540 (file-name-coding-system nnmail-pathname-coding-system))
541 (when (file-exists-p dir)
542 (setq articles
543 (sort (mapcar (lambda (name) (string-to-number name))
544 (directory-files dir nil "^[0-9]+$" t))
545 '<))
546 ;; Update the cache active file, just to synch more.
547 (if articles
548 (progn
549 (gnus-cache-update-active group (car articles) t)
550 (gnus-cache-update-active group (car (last articles))))
551 (when (gnus-gethash group gnus-cache-active-hashtb)
552 (gnus-sethash group nil gnus-cache-active-hashtb)
553 (setq gnus-cache-active-altered t)))
554 articles)))
556 (defun gnus-cache-braid-nov (group cached &optional file)
557 (let ((cache-buf (gnus-get-buffer-create " *gnus-cache*"))
558 beg end)
559 (gnus-cache-save-buffers)
560 (with-current-buffer cache-buf
561 (erase-buffer)
562 (let ((coding-system-for-read gnus-cache-overview-coding-system)
563 (file-name-coding-system nnmail-pathname-coding-system))
564 (insert-file-contents
565 (or file (gnus-cache-file-name group ".overview"))))
566 (goto-char (point-min))
567 (insert "\n")
568 (goto-char (point-min)))
569 (set-buffer nntp-server-buffer)
570 (goto-char (point-min))
571 (while cached
572 (while (and (not (eobp))
573 (< (read (current-buffer)) (car cached)))
574 (forward-line 1))
575 (beginning-of-line)
576 (set-buffer cache-buf)
577 (if (search-forward (concat "\n" (int-to-string (car cached)) "\t")
578 nil t)
579 (setq beg (point-at-bol)
580 end (progn (end-of-line) (point)))
581 (setq beg nil))
582 (set-buffer nntp-server-buffer)
583 (when beg
584 (insert-buffer-substring cache-buf beg end)
585 (insert "\n"))
586 (setq cached (cdr cached)))
587 (kill-buffer cache-buf)))
589 (defun gnus-cache-braid-heads (group cached)
590 (let ((cache-buf (gnus-get-buffer-create " *gnus-cache*")))
591 (with-current-buffer cache-buf
592 (erase-buffer))
593 (set-buffer nntp-server-buffer)
594 (goto-char (point-min))
595 (dolist (entry cached)
596 (while (and (not (eobp))
597 (looking-at "2.. +\\([0-9]+\\) ")
598 (< (progn (goto-char (match-beginning 1))
599 (read (current-buffer)))
600 entry))
601 (search-forward "\n.\n" nil 'move))
602 (beginning-of-line)
603 (set-buffer cache-buf)
604 (erase-buffer)
605 (let ((coding-system-for-read gnus-cache-coding-system)
606 (file-name-coding-system nnmail-pathname-coding-system))
607 (insert-file-contents (gnus-cache-file-name group entry)))
608 (goto-char (point-min))
609 (insert "220 ")
610 (princ (pop cached) (current-buffer))
611 (insert " Article retrieved.\n")
612 (search-forward "\n\n" nil 'move)
613 (delete-region (point) (point-max))
614 (forward-char -1)
615 (insert ".")
616 (set-buffer nntp-server-buffer)
617 (insert-buffer-substring cache-buf))
618 (kill-buffer cache-buf)))
620 ;;;###autoload
621 (defun gnus-jog-cache ()
622 "Go through all groups and put the articles into the cache.
624 Usage:
625 $ emacs -batch -l ~/.emacs -l gnus -f gnus-jog-cache"
626 (interactive)
627 (let ((gnus-mark-article-hook nil)
628 (gnus-expert-user t)
629 (mail-sources nil)
630 (gnus-use-dribble-file nil)
631 (gnus-novice-user nil)
632 (gnus-large-newsgroup nil))
633 ;; Start Gnus.
634 (gnus)
635 ;; Go through all groups...
636 (gnus-group-mark-buffer)
637 (gnus-group-iterate nil
638 (lambda (group)
639 (let (gnus-auto-select-next)
640 (gnus-summary-read-group group nil t)
641 ;; ... and enter the articles into the cache.
642 (when (eq major-mode 'gnus-summary-mode)
643 (gnus-uu-mark-buffer)
644 (gnus-cache-enter-article)
645 (kill-buffer (current-buffer))))))))
647 (defun gnus-cache-read-active (&optional force)
648 "Read the cache active file."
649 (gnus-make-directory gnus-cache-directory)
650 (if (or (not (file-exists-p gnus-cache-active-file))
651 (zerop (nth 7 (file-attributes gnus-cache-active-file)))
652 force)
653 ;; There is no active file, so we generate one.
654 (gnus-cache-generate-active)
655 ;; We simply read the active file.
656 (save-excursion
657 (gnus-set-work-buffer)
658 (nnheader-insert-file-contents gnus-cache-active-file)
659 (gnus-active-to-gnus-format
660 nil (setq gnus-cache-active-hashtb
661 (gnus-make-hashtable
662 (count-lines (point-min) (point-max)))))
663 (setq gnus-cache-active-altered nil))))
665 (defun gnus-cache-write-active (&optional force)
666 "Write the active hashtb to the active file."
667 (when (or force
668 (and gnus-cache-active-hashtb
669 gnus-cache-active-altered))
670 (gnus-write-active-file gnus-cache-active-file gnus-cache-active-hashtb t)
671 ;; Mark the active hashtb as unaltered.
672 (setq gnus-cache-active-altered nil)))
674 (defun gnus-cache-possibly-update-active (group active)
675 "Update active info bounds of GROUP with ACTIVE if necessary.
676 The update is performed if ACTIVE contains a higher or lower bound
677 than the current."
678 (let ((lower t) (higher t))
679 (if gnus-cache-active-hashtb
680 (let ((cache-active (gnus-gethash group gnus-cache-active-hashtb)))
681 (when cache-active
682 (unless (< (car active) (car cache-active))
683 (setq lower nil))
684 (unless (> (cdr active) (cdr cache-active))
685 (setq higher nil))))
686 (gnus-cache-read-active))
687 (when lower
688 (gnus-cache-update-active group (car active) t))
689 (when higher
690 (gnus-cache-update-active group (cdr active)))))
692 (defun gnus-cache-update-active (group number &optional low)
693 "Update the upper bound of the active info of GROUP to NUMBER.
694 If LOW, update the lower bound instead."
695 (let ((active (gnus-gethash group gnus-cache-active-hashtb)))
696 (if (null active)
697 ;; We just create a new active entry for this group.
698 (gnus-sethash group (cons number number) gnus-cache-active-hashtb)
699 ;; Update the lower or upper bound.
700 (if low
701 (setcar active number)
702 (setcdr active number)))
703 ;; Mark the active hashtb as altered.
704 (setq gnus-cache-active-altered t)))
706 ;;;###autoload
707 (defun gnus-cache-generate-active (&optional directory)
708 "Generate the cache active file."
709 (interactive)
710 (let* ((top (null directory))
711 (directory (expand-file-name (or directory gnus-cache-directory)))
712 (file-name-coding-system nnmail-pathname-coding-system)
713 (files (directory-files directory 'full))
714 (group
715 (if top
717 (string-match
718 (concat "^" (regexp-quote
719 (file-name-as-directory
720 (expand-file-name gnus-cache-directory))))
721 (directory-file-name directory))
722 (nnheader-replace-chars-in-string
723 (substring (directory-file-name directory) (match-end 0))
724 ?/ ?.)))
725 nums alphs)
726 (when top
727 (gnus-message 5 "Generating the cache active file...")
728 (setq gnus-cache-active-hashtb (gnus-make-hashtable 123)))
729 (when (string-match "^\\(nn[^_]+\\)_" group)
730 (setq group (replace-match "\\1:" t nil group)))
731 ;; Separate articles from all other files and directories.
732 (while files
733 (if (string-match "^[0-9]+$" (file-name-nondirectory (car files)))
734 (push (string-to-number (file-name-nondirectory (pop files))) nums)
735 (push (pop files) alphs)))
736 ;; If we have nums, then this is probably a valid group.
737 (when (setq nums (sort nums '<))
738 ;; Use non-decoded group name.
739 ;; FIXME: this is kind of a workaround. The active file should
740 ;; be updated at the time articles are cached. It will make
741 ;; `gnus-cache-unified-group-names' needless.
742 (gnus-sethash (or (cdr (assoc group gnus-cache-unified-group-names))
743 group)
744 (cons (car nums) (gnus-last-element nums))
745 gnus-cache-active-hashtb))
746 ;; Go through all the other files.
747 (dolist (file alphs)
748 (when (and (file-directory-p file)
749 (not (string-match "^\\."
750 (file-name-nondirectory file))))
751 ;; We descend directories.
752 (gnus-cache-generate-active file)))
753 ;; Write the new active file.
754 (when top
755 (gnus-cache-write-active t)
756 (gnus-message 5 "Generating the cache active file...done"))))
758 ;;;###autoload
759 (defun gnus-cache-generate-nov-databases (dir)
760 "Generate NOV files recursively starting in DIR."
761 (interactive (list gnus-cache-directory))
762 (gnus-cache-close)
763 (let ((nnml-generate-active-function 'identity))
764 (nnml-generate-nov-databases-directory dir))
766 (setq gnus-cache-total-fetched-hashtb nil)
768 (gnus-cache-open))
770 (defun gnus-cache-move-cache (dir)
771 "Move the cache tree to somewhere else."
772 (interactive "FMove the cache tree to: ")
773 (rename-file gnus-cache-directory dir))
775 (defun gnus-cache-fully-p (&optional group)
776 "Returns non-nil if the cache should be fully used.
777 If GROUP is non-nil, also cater to `gnus-cacheable-groups' and
778 `gnus-uncacheable-groups'."
779 (and gnus-use-cache
780 (not (eq gnus-use-cache 'passive))
781 (if (null group)
783 (and (or (not gnus-cacheable-groups)
784 (string-match gnus-cacheable-groups group))
785 (or (not gnus-uncacheable-groups)
786 (not (string-match gnus-uncacheable-groups group)))))))
788 ;;;###autoload
789 (defun gnus-cache-rename-group (old-group new-group)
790 "Rename OLD-GROUP as NEW-GROUP.
791 Always updates the cache, even when disabled, as the old cache
792 files would corrupt Gnus when the cache was next enabled. It
793 depends on the caller to determine whether group renaming is
794 supported."
795 (let ((old-dir (gnus-cache-file-name old-group ""))
796 (new-dir (gnus-cache-file-name new-group ""))
797 (file-name-coding-system nnmail-pathname-coding-system))
798 (gnus-rename-file old-dir new-dir t))
800 (gnus-cache-rename-group-total-fetched-for old-group new-group)
802 (let ((no-save gnus-cache-active-hashtb))
803 (unless gnus-cache-active-hashtb
804 (gnus-cache-read-active))
805 (let* ((old-group-hash-value
806 (gnus-gethash old-group gnus-cache-active-hashtb))
807 (new-group-hash-value
808 (gnus-gethash new-group gnus-cache-active-hashtb))
809 (delta
810 (or old-group-hash-value new-group-hash-value)))
811 (gnus-sethash new-group old-group-hash-value gnus-cache-active-hashtb)
812 (gnus-sethash old-group nil gnus-cache-active-hashtb)
814 (if no-save
815 (setq gnus-cache-active-altered delta)
816 (gnus-cache-write-active delta)))))
818 ;;;###autoload
819 (defun gnus-cache-delete-group (group)
820 "Delete GROUP from the cache.
821 Always updates the cache, even when disabled, as the old cache
822 files would corrupt gnus when the cache was next enabled.
823 Depends upon the caller to determine whether group deletion is
824 supported."
825 (let ((dir (gnus-cache-file-name group ""))
826 (file-name-coding-system nnmail-pathname-coding-system))
827 (gnus-delete-directory dir))
829 (gnus-cache-delete-group-total-fetched-for group)
831 (let ((no-save gnus-cache-active-hashtb))
832 (unless gnus-cache-active-hashtb
833 (gnus-cache-read-active))
834 (let* ((group-hash-value (gnus-gethash group gnus-cache-active-hashtb)))
835 (gnus-sethash group nil gnus-cache-active-hashtb)
837 (if no-save
838 (setq gnus-cache-active-altered group-hash-value)
839 (gnus-cache-write-active group-hash-value)))))
841 (defvar gnus-cache-inhibit-update-total-fetched-for nil)
842 (defvar gnus-cache-need-update-total-fetched-for nil)
844 (defmacro gnus-cache-with-refreshed-group (group &rest body)
845 `(prog1 (let ((gnus-cache-inhibit-update-total-fetched-for t))
846 ,@body)
847 (when (and gnus-cache-need-update-total-fetched-for
848 (not gnus-cache-inhibit-update-total-fetched-for))
849 (with-current-buffer gnus-group-buffer
850 (setq gnus-cache-need-update-total-fetched-for nil)
851 (gnus-group-update-group ,group t)))))
853 (defun gnus-cache-update-file-total-fetched-for (group file &optional subtract)
854 (when gnus-cache-total-fetched-hashtb
855 (gnus-cache-with-refreshed-group
856 group
857 (let* ((entry (or (gnus-gethash group gnus-cache-total-fetched-hashtb)
858 (gnus-sethash group (make-vector 2 0)
859 gnus-cache-total-fetched-hashtb)))
860 size)
862 (if file
863 (setq size (or (nth 7 (file-attributes file)) 0))
864 (let* ((file-name-coding-system nnmail-pathname-coding-system)
865 (files (directory-files (gnus-cache-file-name group "")
866 t nil t))
867 file attrs)
868 (setq size 0.0)
869 (while (setq file (pop files))
870 (setq attrs (file-attributes file))
871 (unless (nth 0 attrs)
872 (incf size (float (nth 7 attrs)))))))
874 (setq gnus-cache-need-update-total-fetched-for t)
876 (incf (nth 1 entry) (if subtract (- size) size))))))
878 (defun gnus-cache-update-overview-total-fetched-for (group file)
879 (when gnus-cache-total-fetched-hashtb
880 (gnus-cache-with-refreshed-group
881 group
882 (let* ((entry (or (gnus-gethash group gnus-cache-total-fetched-hashtb)
883 (gnus-sethash group (make-list 2 0)
884 gnus-cache-total-fetched-hashtb)))
885 (file-name-coding-system nnmail-pathname-coding-system)
886 (size (or (nth 7 (file-attributes
887 (or file
888 (gnus-cache-file-name group ".overview"))))
889 0)))
890 (setq gnus-cache-need-update-total-fetched-for t)
891 (setf (nth 0 entry) size)))))
893 (defun gnus-cache-rename-group-total-fetched-for (old-group new-group)
894 "Record of disk space used by OLD-GROUP now associated with NEW-GROUP."
895 (when gnus-cache-total-fetched-hashtb
896 (let ((entry (gnus-gethash old-group gnus-cache-total-fetched-hashtb)))
897 (gnus-sethash new-group entry gnus-cache-total-fetched-hashtb)
898 (gnus-sethash old-group nil gnus-cache-total-fetched-hashtb))))
900 (defun gnus-cache-delete-group-total-fetched-for (group)
901 "Delete record of disk space used by GROUP being deleted."
902 (when gnus-cache-total-fetched-hashtb
903 (gnus-sethash group nil gnus-cache-total-fetched-hashtb)))
905 (defun gnus-cache-total-fetched-for (group &optional no-inhibit)
906 "Get total disk space used by the cache for the specified GROUP."
907 (unless (equal group "dummy.group")
908 (unless gnus-cache-total-fetched-hashtb
909 (setq gnus-cache-total-fetched-hashtb (gnus-make-hashtable 1024)))
911 (let* ((entry (gnus-gethash group gnus-cache-total-fetched-hashtb)))
912 (if entry
913 (apply '+ entry)
914 (let ((gnus-cache-inhibit-update-total-fetched-for (not no-inhibit)))
916 (gnus-cache-update-overview-total-fetched-for group nil)
917 (gnus-cache-update-file-total-fetched-for group nil)))))))
919 (provide 'gnus-cache)
921 ;;; gnus-cache.el ends here