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