gnus-cache.el 33 KB

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