gnus-cache.el 33 KB

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