nnfolder.el 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182
  1. ;;; nnfolder.el --- mail folder access for Gnus
  2. ;; Copyright (C) 1995-2015 Free Software Foundation, Inc.
  3. ;; Author: Simon Josefsson <simon@josefsson.org>
  4. ;; ShengHuo Zhu <zsh@cs.rochester.edu> (adding NOV)
  5. ;; Scott Byer <byer@mv.us.adobe.com>
  6. ;; Lars Magne Ingebrigtsen <larsi@gnus.org>
  7. ;; Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
  8. ;; Keywords: mail
  9. ;; This file is part of GNU Emacs.
  10. ;; GNU Emacs is free software: you can redistribute it and/or modify
  11. ;; it under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation, either version 3 of the License, or
  13. ;; (at your option) any later version.
  14. ;; GNU Emacs is distributed in the hope that it will be useful,
  15. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ;; GNU General Public License for more details.
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  20. ;;; Commentary:
  21. ;;; Code:
  22. (require 'nnheader)
  23. (require 'message)
  24. (require 'nnmail)
  25. (require 'nnoo)
  26. (eval-when-compile (require 'cl))
  27. (require 'gnus)
  28. (require 'gnus-util)
  29. (require 'gnus-range)
  30. ;; FIXME not explicitly used in this file.
  31. (autoload 'gnus-article-unpropagatable-p "gnus-sum")
  32. (nnoo-declare nnfolder)
  33. (defvoo nnfolder-directory (expand-file-name message-directory)
  34. "The name of the nnfolder directory.")
  35. (defvoo nnfolder-nov-directory nil
  36. "The name of the nnfolder NOV directory.
  37. If nil, `nnfolder-directory' is used.")
  38. (defvoo nnfolder-active-file
  39. (nnheader-concat nnfolder-directory "active")
  40. "The name of the active file.")
  41. ;; I renamed this variable to something more in keeping with the general GNU
  42. ;; style. -SLB
  43. (defvoo nnfolder-ignore-active-file nil
  44. "If non-nil, the active file is ignored.
  45. This causes nnfolder to do some extra work in order to determine the
  46. true active ranges of an mbox file. Note that the active file is
  47. still saved, but its values are not used. This costs some extra time
  48. when scanning an mbox when opening it.")
  49. (defvoo nnfolder-distrust-mbox nil
  50. "If non-nil, the folder will be distrusted.
  51. This means that nnfolder will not trust the user with respect to
  52. inserting unaccounted for mail in the middle of an mbox file. This
  53. can greatly slow down scans, which now must scan the entire file for
  54. unmarked messages. When nil, scans occur forward from the last marked
  55. message, a huge time saver for large mailboxes.")
  56. (defvoo nnfolder-newsgroups-file
  57. (concat (file-name-as-directory nnfolder-directory) "newsgroups")
  58. "Mail newsgroups description file.")
  59. (defvoo nnfolder-get-new-mail t
  60. "If non-nil, nnfolder will check the incoming mail file and split the mail.")
  61. (defvoo nnfolder-prepare-save-mail-hook nil
  62. "Hook run narrowed to an article before saving.")
  63. (defvoo nnfolder-save-buffer-hook nil
  64. "Hook run before saving the nnfolder mbox buffer.")
  65. (defvoo nnfolder-inhibit-expiry nil
  66. "If non-nil, inhibit expiry.")
  67. (defconst nnfolder-version "nnfolder 2.0"
  68. "nnfolder version.")
  69. (defconst nnfolder-article-marker "X-Gnus-Article-Number: "
  70. "String used to demarcate what the article number for a message is.")
  71. (defvoo nnfolder-current-group nil)
  72. (defvoo nnfolder-current-buffer nil)
  73. (defvoo nnfolder-status-string "")
  74. (defvoo nnfolder-group-alist nil)
  75. (defvoo nnfolder-buffer-alist nil)
  76. (defvoo nnfolder-scantime-alist nil)
  77. (defvoo nnfolder-active-timestamp nil)
  78. (defvoo nnfolder-active-file-coding-system mm-text-coding-system)
  79. (defvoo nnfolder-active-file-coding-system-for-write
  80. nnmail-active-file-coding-system)
  81. (defvoo nnfolder-file-coding-system mm-text-coding-system)
  82. (defvoo nnfolder-file-coding-system-for-write nnheader-file-coding-system
  83. "Coding system for save nnfolder file.
  84. if nil, `nnfolder-file-coding-system' is used.") ; FIXME: fill-in the doc-string of this variable
  85. (defvoo nnfolder-nov-is-evil nil
  86. "If non-nil, Gnus will never generate and use nov databases for mail groups.
  87. Using nov databases will speed up header fetching considerably.
  88. This variable shouldn't be flipped much. If you have, for some reason,
  89. set this to t, and want to set it to nil again, you should always run
  90. the `nnfolder-generate-active-file' command. The function will go
  91. through all nnfolder directories and generate nov databases for them
  92. all. This may very well take some time.")
  93. (defvoo nnfolder-nov-file-suffix ".nov")
  94. (defvoo nnfolder-nov-buffer-alist nil)
  95. (defvar nnfolder-nov-buffer-file-name nil)
  96. ;;; Interface functions
  97. (nnoo-define-basics nnfolder)
  98. (deffoo nnfolder-retrieve-headers (articles &optional group server fetch-old)
  99. (with-current-buffer nntp-server-buffer
  100. (erase-buffer)
  101. (let (article start stop num)
  102. (nnfolder-possibly-change-group group server)
  103. (when nnfolder-current-buffer
  104. (set-buffer nnfolder-current-buffer)
  105. (goto-char (point-min))
  106. (if (stringp (car articles))
  107. 'headers
  108. (if (nnfolder-retrieve-headers-with-nov articles fetch-old)
  109. 'nov
  110. (setq articles (gnus-sorted-intersection
  111. ;; Is ARTICLES sorted?
  112. (sort articles '<)
  113. (nnfolder-existing-articles)))
  114. (while (setq article (pop articles))
  115. (set-buffer nnfolder-current-buffer)
  116. (cond ((nnfolder-goto-article article)
  117. (setq start (point))
  118. (setq stop (if (search-forward "\n\n" nil t)
  119. (1- (point))
  120. (point-max)))
  121. (set-buffer nntp-server-buffer)
  122. (insert (format "221 %d Article retrieved.\n" article))
  123. (insert-buffer-substring nnfolder-current-buffer
  124. start stop)
  125. (goto-char (point-max))
  126. (insert ".\n"))
  127. ;; If we couldn't find this article, skip over ranges
  128. ;; of missing articles so we don't search the whole file
  129. ;; for each of them.
  130. ((numberp article)
  131. (setq start (point))
  132. (and
  133. ;; Check that we are either at BOF or after an
  134. ;; article with a lower number. We do this so we
  135. ;; won't be confused by out-of-order article numbers,
  136. ;; as caused by active file bogosity.
  137. (cond
  138. ((bobp))
  139. ((search-backward (concat "\n" nnfolder-article-marker)
  140. nil t)
  141. (goto-char (match-end 0))
  142. (setq num (string-to-number
  143. (buffer-substring
  144. (point) (point-at-eol))))
  145. (goto-char start)
  146. (< num article)))
  147. ;; Check that we are before an article with a
  148. ;; higher number.
  149. (search-forward (concat "\n" nnfolder-article-marker)
  150. nil t)
  151. (progn
  152. (setq num (string-to-number
  153. (buffer-substring
  154. (point) (point-at-eol))))
  155. (> num article))
  156. ;; Discard any article numbers before the one we're
  157. ;; now looking at.
  158. (while (and articles
  159. (< (car articles) num))
  160. (setq articles (cdr articles))))
  161. (goto-char start))))
  162. (set-buffer nntp-server-buffer)
  163. (nnheader-fold-continuation-lines)
  164. 'headers))))))
  165. (deffoo nnfolder-open-server (server &optional defs)
  166. (nnoo-change-server 'nnfolder server defs)
  167. (nnmail-activate 'nnfolder t)
  168. (gnus-make-directory nnfolder-directory)
  169. (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
  170. (and nnfolder-nov-directory
  171. (gnus-make-directory nnfolder-nov-directory)))
  172. (cond
  173. ((not (file-exists-p nnfolder-directory))
  174. (nnfolder-close-server)
  175. (nnheader-report 'nnfolder "Couldn't create directory: %s"
  176. nnfolder-directory))
  177. ((not (file-directory-p (file-truename nnfolder-directory)))
  178. (nnfolder-close-server)
  179. (nnheader-report 'nnfolder "Not a directory: %s" nnfolder-directory))
  180. (t
  181. (nnmail-activate 'nnfolder)
  182. (nnheader-report 'nnfolder "Opened server %s using directory %s"
  183. server nnfolder-directory)
  184. t)))
  185. (deffoo nnfolder-request-close ()
  186. (let ((alist nnfolder-buffer-alist))
  187. (while alist
  188. (nnfolder-close-group (caar alist) nil t)
  189. (setq alist (cdr alist))))
  190. (nnoo-close-server 'nnfolder)
  191. (setq nnfolder-buffer-alist nil
  192. nnfolder-group-alist nil))
  193. (deffoo nnfolder-request-article (article &optional group server buffer)
  194. (nnfolder-possibly-change-group group server)
  195. (with-current-buffer nnfolder-current-buffer
  196. (goto-char (point-min))
  197. (when (nnfolder-goto-article article)
  198. (let (start stop)
  199. (setq start (point))
  200. (forward-line 1)
  201. (unless (and (nnmail-search-unix-mail-delim)
  202. (forward-line -1))
  203. (goto-char (point-max)))
  204. (setq stop (point))
  205. (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
  206. (set-buffer nntp-server-buffer)
  207. (erase-buffer)
  208. (insert-buffer-substring nnfolder-current-buffer start stop)
  209. (goto-char (point-min))
  210. (while (looking-at "From ")
  211. (delete-char 5)
  212. (insert "X-From-Line: ")
  213. (forward-line 1))
  214. (if (numberp article)
  215. (cons nnfolder-current-group article)
  216. (goto-char (point-min))
  217. (cons nnfolder-current-group
  218. (if (search-forward (concat "\n" nnfolder-article-marker)
  219. nil t)
  220. (string-to-number (buffer-substring
  221. (point) (point-at-eol)))
  222. -1))))))))
  223. (deffoo nnfolder-request-group (group &optional server dont-check info)
  224. (nnfolder-possibly-change-group group server t)
  225. (save-excursion
  226. (cond ((not (assoc group nnfolder-group-alist))
  227. (nnheader-report 'nnfolder "No such group: %s" group))
  228. ((file-directory-p (nnfolder-group-pathname group))
  229. (nnheader-report 'nnfolder "%s is a directory"
  230. (file-name-as-directory
  231. (let ((nnmail-pathname-coding-system nil))
  232. (nnfolder-group-pathname group)))))
  233. (dont-check
  234. (nnheader-report 'nnfolder "Selected group %s" group)
  235. t)
  236. (t
  237. (let* ((active (assoc group nnfolder-group-alist))
  238. (group (car active))
  239. (range (cadr active)))
  240. (cond
  241. ((null active)
  242. (nnheader-report 'nnfolder "No such group: %s" group))
  243. ((null nnfolder-current-group)
  244. (nnheader-report 'nnfolder "Empty group: %s" group))
  245. (t
  246. (nnheader-report 'nnfolder "Selected group %s" group)
  247. (nnheader-insert "211 %d %d %d %s\n"
  248. (1+ (- (cdr range) (car range)))
  249. (car range) (cdr range) group))))))))
  250. (deffoo nnfolder-request-scan (&optional group server)
  251. (nnfolder-possibly-change-group nil server)
  252. (when nnfolder-get-new-mail
  253. (nnfolder-possibly-change-group group server)
  254. (nnmail-get-new-mail
  255. 'nnfolder 'nnfolder-save-all-buffers
  256. nnfolder-directory group)))
  257. (defun nnfolder-save-all-buffers ()
  258. (let ((bufs nnfolder-buffer-alist))
  259. (save-excursion
  260. (while bufs
  261. (if (not (gnus-buffer-live-p (nth 1 (car bufs))))
  262. (setq nnfolder-buffer-alist
  263. (delq (car bufs) nnfolder-buffer-alist))
  264. (set-buffer (nth 1 (car bufs)))
  265. (nnfolder-save-buffer)
  266. (kill-buffer (current-buffer)))
  267. (setq bufs (cdr bufs))))))
  268. ;; Don't close the buffer if we're not shutting down the server. This way,
  269. ;; we can keep the buffer in the group buffer cache, and not have to grovel
  270. ;; over the buffer again unless we add new mail to it or modify it in some
  271. ;; way.
  272. (deffoo nnfolder-close-group (group &optional server force)
  273. ;; Make sure we _had_ the group open.
  274. (when (or (assoc group nnfolder-buffer-alist)
  275. (equal group nnfolder-current-group))
  276. (let ((inf (assoc group nnfolder-buffer-alist)))
  277. (when inf
  278. (when (and nnfolder-current-group
  279. nnfolder-current-buffer)
  280. (push (list nnfolder-current-group nnfolder-current-buffer)
  281. nnfolder-buffer-alist))
  282. (setq nnfolder-buffer-alist
  283. (delq inf nnfolder-buffer-alist))
  284. (setq nnfolder-current-buffer (cadr inf)
  285. nnfolder-current-group (car inf))))
  286. (when (and nnfolder-current-buffer
  287. (buffer-name nnfolder-current-buffer))
  288. (with-current-buffer nnfolder-current-buffer
  289. ;; If the buffer was modified, write the file out now.
  290. (nnfolder-save-buffer)
  291. ;; If we're shutting the server down, we need to kill the
  292. ;; buffer and remove it from the open buffer list. Or, of
  293. ;; course, if we're trying to minimize our space impact.
  294. (kill-buffer (current-buffer))
  295. (setq nnfolder-buffer-alist (delq (assoc group nnfolder-buffer-alist)
  296. nnfolder-buffer-alist)))))
  297. (setq nnfolder-current-group nil
  298. nnfolder-current-buffer nil)
  299. t)
  300. (deffoo nnfolder-request-create-group (group &optional server args)
  301. (nnfolder-possibly-change-group nil server)
  302. (nnmail-activate 'nnfolder)
  303. (cond ((zerop (length group))
  304. (nnheader-report 'nnfolder "Invalid (empty) group name"))
  305. ((file-directory-p (nnfolder-group-pathname group))
  306. (nnheader-report 'nnfolder "%s is a directory"
  307. (file-name-as-directory
  308. (let ((nnmail-pathname-coding-system nil))
  309. (nnfolder-group-pathname group)))))
  310. ((assoc group nnfolder-group-alist)
  311. t)
  312. (t
  313. (push (list group (cons 1 0)) nnfolder-group-alist)
  314. (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
  315. (save-current-buffer
  316. (nnfolder-read-folder group))
  317. t)))
  318. (deffoo nnfolder-request-list (&optional server)
  319. (nnfolder-possibly-change-group nil server)
  320. (save-excursion
  321. (let ((nnmail-file-coding-system nnfolder-active-file-coding-system))
  322. (nnmail-find-file nnfolder-active-file)
  323. (setq nnfolder-group-alist (nnmail-get-active)))
  324. t))
  325. (deffoo nnfolder-request-newgroups (date &optional server)
  326. (nnfolder-possibly-change-group nil server)
  327. (nnfolder-request-list server))
  328. (deffoo nnfolder-request-list-newsgroups (&optional server)
  329. (nnfolder-possibly-change-group nil server)
  330. (save-excursion
  331. (let ((nnmail-file-coding-system nnfolder-file-coding-system))
  332. (nnmail-find-file nnfolder-newsgroups-file))))
  333. ;; Return a list consisting of all article numbers existing in the
  334. ;; current folder.
  335. (defun nnfolder-existing-articles ()
  336. (save-excursion
  337. (when nnfolder-current-buffer
  338. (set-buffer nnfolder-current-buffer)
  339. (goto-char (point-min))
  340. (let ((marker (concat "\n" nnfolder-article-marker))
  341. (number "[0-9]+")
  342. numbers)
  343. (while (and (search-forward marker nil t)
  344. (re-search-forward number nil t))
  345. (let ((newnum (string-to-number (match-string 0))))
  346. (if (nnmail-within-headers-p)
  347. (push newnum numbers))))
  348. ;; The article numbers are increasing, so this result is sorted.
  349. (nreverse numbers)))))
  350. (autoload 'gnus-request-group "gnus-int")
  351. (declare-function gnus-request-create-group "gnus-int"
  352. (group &optional gnus-command-method args))
  353. (deffoo nnfolder-request-expire-articles (articles newsgroup
  354. &optional server force)
  355. (nnfolder-possibly-change-group newsgroup server)
  356. (let ((is-old t)
  357. ;; The articles we have deleted so far.
  358. (deleted-articles nil)
  359. ;; The articles that really exist and will
  360. ;; be expired if they are old enough.
  361. (maybe-expirable
  362. (gnus-sorted-intersection articles (nnfolder-existing-articles)))
  363. target)
  364. (nnmail-activate 'nnfolder)
  365. (with-current-buffer nnfolder-current-buffer
  366. ;; Since messages are sorted in arrival order and expired in the
  367. ;; same order, we can stop as soon as we find a message that is
  368. ;; too old.
  369. (while (and maybe-expirable is-old)
  370. (goto-char (point-min))
  371. (when (and (nnfolder-goto-article (car maybe-expirable))
  372. (search-forward (concat "\n" nnfolder-article-marker)
  373. nil t))
  374. (forward-sexp)
  375. (when (setq is-old
  376. (nnmail-expired-article-p
  377. newsgroup
  378. (buffer-substring
  379. (point) (progn (end-of-line) (point)))
  380. force nnfolder-inhibit-expiry))
  381. (setq target nnmail-expiry-target)
  382. (unless (eq target 'delete)
  383. (with-temp-buffer
  384. (nnfolder-request-article (car maybe-expirable)
  385. newsgroup server (current-buffer))
  386. (let ((nnfolder-current-directory nil))
  387. (when (functionp target)
  388. (setq target (funcall target newsgroup)))
  389. (when (and target (not (eq target 'delete)))
  390. (if (or (gnus-request-group target)
  391. (gnus-request-create-group target))
  392. (nnmail-expiry-target-group target newsgroup)
  393. (setq target nil)))))
  394. (nnfolder-possibly-change-group newsgroup server))
  395. (when target
  396. (nnheader-message 5 "Deleting article %d in %s..."
  397. (car maybe-expirable) newsgroup)
  398. (nnfolder-delete-mail)
  399. (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
  400. (nnfolder-nov-delete-article newsgroup (car maybe-expirable)))
  401. ;; Must remember which articles were actually deleted
  402. (push (car maybe-expirable) deleted-articles))))
  403. (setq maybe-expirable (cdr maybe-expirable)))
  404. (unless nnfolder-inhibit-expiry
  405. (nnheader-message 5 "Deleting articles...done"))
  406. (nnfolder-save-buffer)
  407. (nnfolder-adjust-min-active newsgroup)
  408. (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
  409. (nnfolder-save-all-buffers)
  410. (gnus-sorted-difference articles (nreverse deleted-articles)))))
  411. (deffoo nnfolder-request-move-article (article group server accept-form
  412. &optional last move-is-internal)
  413. (save-excursion
  414. (let ((buf (get-buffer-create " *nnfolder move*"))
  415. result)
  416. (and
  417. (nnfolder-request-article article group server)
  418. (with-current-buffer buf
  419. (erase-buffer)
  420. (insert-buffer-substring nntp-server-buffer)
  421. (goto-char (point-min))
  422. (while (re-search-forward
  423. (concat "^" nnfolder-article-marker)
  424. (save-excursion (and (search-forward "\n\n" nil t) (point)))
  425. t)
  426. (gnus-delete-line))
  427. (setq result (eval accept-form))
  428. (kill-buffer buf)
  429. result)
  430. (save-excursion
  431. (nnfolder-possibly-change-group group server)
  432. (set-buffer nnfolder-current-buffer)
  433. (goto-char (point-min))
  434. (when (nnfolder-goto-article article)
  435. (nnfolder-delete-mail))
  436. (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
  437. (nnfolder-nov-delete-article group article))
  438. (when last
  439. (nnfolder-save-buffer)
  440. (nnfolder-adjust-min-active group)
  441. (nnfolder-save-active nnfolder-group-alist nnfolder-active-file))))
  442. result)))
  443. (deffoo nnfolder-request-accept-article (group &optional server last)
  444. (save-excursion
  445. (nnfolder-possibly-change-group group server)
  446. (nnmail-check-syntax)
  447. (let ((buf (current-buffer))
  448. result art-group)
  449. (goto-char (point-min))
  450. (when (looking-at "X-From-Line: ")
  451. (replace-match "From ")
  452. (while (progn (forward-line) (looking-at "[ \t]"))
  453. (delete-char -1)))
  454. (with-temp-buffer
  455. (let ((nnmail-file-coding-system nnfolder-active-file-coding-system)
  456. (nntp-server-buffer (current-buffer)))
  457. (nnmail-find-file nnfolder-active-file)
  458. (setq nnfolder-group-alist (nnmail-parse-active))))
  459. (save-excursion
  460. (goto-char (point-min))
  461. (if (search-forward "\n\n" nil t)
  462. (forward-line -1)
  463. (goto-char (point-max)))
  464. (while (re-search-backward (concat "^" nnfolder-article-marker) nil t)
  465. (delete-region (point) (progn (forward-line 1) (point))))
  466. (when nnmail-cache-accepted-message-ids
  467. (nnmail-cache-insert (nnmail-fetch-field "message-id")
  468. group
  469. (nnmail-fetch-field "subject")
  470. (nnmail-fetch-field "from")))
  471. (setq result (if (stringp group)
  472. (list (cons group (nnfolder-active-number group)))
  473. (setq art-group
  474. (nnmail-article-group 'nnfolder-active-number))))
  475. (if (and (null result)
  476. (yes-or-no-p "Moved to `junk' group; delete article? "))
  477. (setq result 'junk)
  478. (setq result
  479. (car (nnfolder-save-mail result)))))
  480. (when last
  481. (save-excursion
  482. (nnfolder-possibly-change-folder (or (caar art-group) group))
  483. (nnfolder-save-buffer)
  484. (when nnmail-cache-accepted-message-ids
  485. (nnmail-cache-close))))
  486. (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
  487. (unless result
  488. (nnheader-report 'nnfolder "Couldn't store article"))
  489. result)))
  490. (deffoo nnfolder-request-replace-article (article group buffer)
  491. (nnfolder-possibly-change-group group)
  492. (with-current-buffer buffer
  493. (goto-char (point-min))
  494. (if (not (looking-at "X-From-Line: "))
  495. (insert "From nobody " (current-time-string) "\n")
  496. (replace-match "From ")
  497. (forward-line 1)
  498. (while (looking-at "[ \t]")
  499. (delete-char -1)
  500. (forward-line 1)))
  501. (nnfolder-normalize-buffer)
  502. (set-buffer nnfolder-current-buffer)
  503. (goto-char (point-min))
  504. (if (not (nnfolder-goto-article article))
  505. nil
  506. (nnfolder-delete-mail)
  507. (insert-buffer-substring buffer)
  508. (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
  509. (with-current-buffer buffer
  510. (let ((headers (nnfolder-parse-head article
  511. (point-min) (point-max))))
  512. (with-current-buffer (nnfolder-open-nov group)
  513. (if (nnheader-find-nov-line article)
  514. (delete-region (point) (progn (forward-line 1) (point))))
  515. (nnheader-insert-nov headers)))))
  516. (nnfolder-save-buffer)
  517. t)))
  518. (deffoo nnfolder-request-delete-group (group &optional force server)
  519. (nnfolder-close-group group server t)
  520. ;; Delete all articles in GROUP.
  521. (if (not force)
  522. () ; Don't delete the articles.
  523. ;; Delete the file that holds the group.
  524. (let ((data (nnfolder-group-pathname group))
  525. (nov (nnfolder-group-nov-pathname group)))
  526. (ignore-errors (delete-file data))
  527. (ignore-errors (delete-file nov))))
  528. ;; Remove the group from all structures.
  529. (setq nnfolder-group-alist
  530. (delq (assoc group nnfolder-group-alist) nnfolder-group-alist)
  531. nnfolder-current-group nil
  532. nnfolder-current-buffer nil)
  533. ;; Save the active file.
  534. (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
  535. t)
  536. (deffoo nnfolder-request-rename-group (group new-name &optional server)
  537. (nnfolder-possibly-change-group group server)
  538. (with-current-buffer nnfolder-current-buffer
  539. (and (file-writable-p buffer-file-name)
  540. (ignore-errors
  541. (let ((new-file (nnfolder-group-pathname new-name)))
  542. (gnus-make-directory (file-name-directory new-file))
  543. (rename-file buffer-file-name new-file)
  544. (when (file-exists-p (nnfolder-group-nov-pathname group))
  545. (setq new-file (nnfolder-group-nov-pathname new-name))
  546. (gnus-make-directory (file-name-directory new-file))
  547. (rename-file (nnfolder-group-nov-pathname group) new-file)))
  548. t)
  549. ;; That went ok, so we change the internal structures.
  550. (let ((entry (assoc group nnfolder-group-alist)))
  551. (and entry (setcar entry new-name))
  552. (setq nnfolder-current-buffer nil
  553. nnfolder-current-group nil)
  554. ;; Save the new group alist.
  555. (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
  556. ;; We kill the buffer instead of renaming it and stuff.
  557. (kill-buffer (current-buffer))
  558. t))))
  559. (deffoo nnfolder-request-regenerate (server)
  560. (nnfolder-possibly-change-group nil server)
  561. (nnfolder-generate-active-file)
  562. t)
  563. ;;; Internal functions.
  564. (defun nnfolder-adjust-min-active (group)
  565. ;; Find the lowest active article in this group.
  566. (let* ((active (cadr (assoc group nnfolder-group-alist)))
  567. (marker (concat "\n" nnfolder-article-marker))
  568. (number "[0-9]+")
  569. (activemin (cdr active)))
  570. (with-current-buffer nnfolder-current-buffer
  571. (goto-char (point-min))
  572. (while (and (search-forward marker nil t)
  573. (re-search-forward number nil t))
  574. (let ((newnum (string-to-number (match-string 0))))
  575. (if (nnmail-within-headers-p)
  576. (setq activemin (min activemin newnum)))))
  577. (setcar active activemin))))
  578. (defun nnfolder-article-string (article)
  579. (if (numberp article)
  580. (concat "\n" nnfolder-article-marker (int-to-string article) " ")
  581. (concat "\nMessage-ID: " article)))
  582. (defun nnfolder-goto-article (article)
  583. "Place point at the start of the headers of ARTICLE.
  584. ARTICLE can be an article number or a Message-ID.
  585. Returns t if successful, nil otherwise."
  586. (let ((art-string (nnfolder-article-string article))
  587. start found)
  588. ;; It is likely that we are at or before the delimiter line.
  589. ;; We therefore go to the end of the previous line, and start
  590. ;; searching from there.
  591. (beginning-of-line)
  592. (unless (bobp)
  593. (forward-char -1))
  594. (setq start (point))
  595. ;; First search forward.
  596. (while (and (setq found (search-forward art-string nil t))
  597. (not (nnmail-within-headers-p))))
  598. ;; If unsuccessful, search backward from where we started,
  599. (unless found
  600. (goto-char start)
  601. (while (and (setq found (search-backward art-string nil t))
  602. (not (nnmail-within-headers-p)))))
  603. (when found
  604. (nnmail-search-unix-mail-delim-backward))))
  605. (defun nnfolder-delete-mail (&optional leave-delim)
  606. "Delete the message that point is in.
  607. If optional argument LEAVE-DELIM is t, then mailbox delimiter is not
  608. deleted. Point is left where the deleted region was."
  609. (save-restriction
  610. (narrow-to-region
  611. (save-excursion
  612. ;; In case point is at the beginning of the message already.
  613. (forward-line 1)
  614. (nnmail-search-unix-mail-delim-backward)
  615. (if leave-delim (progn (forward-line 1) (point))
  616. (point)))
  617. (progn
  618. (forward-line 1)
  619. (if (nnmail-search-unix-mail-delim)
  620. (point)
  621. (point-max))))
  622. (run-hooks 'nnfolder-delete-mail-hook)
  623. (delete-region (point-min) (point-max))))
  624. (defun nnfolder-possibly-change-group (group &optional server dont-check)
  625. ;; Change servers.
  626. (when (and server
  627. (not (nnfolder-server-opened server)))
  628. (nnfolder-open-server server))
  629. (unless (gnus-buffer-live-p nnfolder-current-buffer)
  630. (setq nnfolder-current-buffer nil
  631. nnfolder-current-group nil))
  632. ;; Change group.
  633. (let ((file-name-coding-system nnmail-pathname-coding-system))
  634. (when (and group
  635. (not (equal group nnfolder-current-group))
  636. (progn
  637. (nnmail-activate 'nnfolder)
  638. (and (assoc group nnfolder-group-alist)
  639. (file-exists-p (nnfolder-group-pathname group)))))
  640. (if dont-check
  641. (setq nnfolder-current-group group
  642. nnfolder-current-buffer nil)
  643. (let (inf file)
  644. ;; If we have to change groups, see if we don't already have
  645. ;; the folder in memory. If we do, verify the modtime and
  646. ;; destroy the folder if needed so we can rescan it.
  647. (setq nnfolder-current-buffer
  648. (nth 1 (assoc group nnfolder-buffer-alist)))
  649. ;; If the buffer is not live, make sure it isn't in the
  650. ;; alist. If it is live, verify that nobody else has
  651. ;; touched the file since last time.
  652. (when (and nnfolder-current-buffer
  653. (not (gnus-buffer-live-p nnfolder-current-buffer)))
  654. (setq nnfolder-buffer-alist (delq inf nnfolder-buffer-alist)
  655. nnfolder-current-buffer nil))
  656. (setq nnfolder-current-group group)
  657. (when (or (not nnfolder-current-buffer)
  658. (not (verify-visited-file-modtime
  659. nnfolder-current-buffer)))
  660. (save-excursion
  661. (setq file (nnfolder-group-pathname group))
  662. ;; See whether we need to create the new file.
  663. (unless (file-exists-p file)
  664. (gnus-make-directory (file-name-directory file))
  665. (let ((nnmail-file-coding-system
  666. (or nnfolder-file-coding-system-for-write
  667. nnfolder-file-coding-system-for-write)))
  668. (nnmail-write-region (point-min) (point-min)
  669. file t 'nomesg)))
  670. (when (setq nnfolder-current-buffer (nnfolder-read-folder group))
  671. (set-buffer nnfolder-current-buffer)
  672. (push (list group nnfolder-current-buffer)
  673. nnfolder-buffer-alist)))))))))
  674. (defun nnfolder-save-mail (group-art-list)
  675. "Called narrowed to an article."
  676. (let* (save-list group-art)
  677. (goto-char (point-min))
  678. ;; The From line may have been quoted by movemail.
  679. (when (looking-at ">From")
  680. (delete-char 1))
  681. ;; This might come from somewhere else.
  682. (unless (looking-at "From ")
  683. (insert "From nobody " (current-time-string) "\n")
  684. (goto-char (point-min)))
  685. ;; Quote all "From " lines in the article.
  686. (forward-line 1)
  687. (let (case-fold-search)
  688. (while (re-search-forward "^From " nil t)
  689. (beginning-of-line)
  690. (insert "> ")))
  691. (setq save-list group-art-list)
  692. (nnmail-insert-lines)
  693. (nnmail-insert-xref group-art-list)
  694. (run-hooks 'nnmail-prepare-save-mail-hook)
  695. (run-hooks 'nnfolder-prepare-save-mail-hook)
  696. ;; Insert the mail into each of the destination groups.
  697. (while (setq group-art (pop group-art-list))
  698. ;; Kill any previous newsgroup markers.
  699. (goto-char (point-min))
  700. (if (search-forward "\n\n" nil t)
  701. (forward-line -1)
  702. (goto-char (point-max)))
  703. (while (search-backward (concat "\n" nnfolder-article-marker) nil t)
  704. (delete-region (1+ (point)) (progn (forward-line 2) (point))))
  705. ;; Insert the new newsgroup marker.
  706. (nnfolder-insert-newsgroup-line group-art)
  707. (save-excursion
  708. (let ((beg (point-min))
  709. (end (point-max))
  710. (obuf (current-buffer)))
  711. (nnfolder-possibly-change-folder (car group-art))
  712. (let ((buffer-read-only nil))
  713. (nnfolder-normalize-buffer)
  714. (insert-buffer-substring obuf beg end))
  715. (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
  716. (set-buffer obuf)
  717. (nnfolder-add-nov (car group-art) (cdr group-art)
  718. (nnfolder-parse-head nil beg end))))))
  719. ;; Did we save it anywhere?
  720. save-list))
  721. (defun nnfolder-normalize-buffer ()
  722. "Make sure there are two newlines at the end of the buffer."
  723. (goto-char (point-max))
  724. (skip-chars-backward "\n")
  725. (delete-region (point) (point-max))
  726. (unless (bobp)
  727. (insert "\n\n")))
  728. (defun nnfolder-insert-newsgroup-line (group-art)
  729. (save-excursion
  730. (goto-char (point-min))
  731. (unless (search-forward "\n\n" nil t)
  732. (goto-char (point-max))
  733. (insert "\n"))
  734. (forward-char -1)
  735. (insert (format (concat nnfolder-article-marker "%d %s\n")
  736. (cdr group-art) (message-make-date)))))
  737. (defun nnfolder-active-number (group)
  738. ;; Find the next article number in GROUP.
  739. (let ((active (cadr (assoc group nnfolder-group-alist))))
  740. (if active
  741. (setcdr active (1+ (cdr active)))
  742. ;; This group is new, so we create a new entry for it.
  743. ;; This might be a bit naughty... creating groups on the drop of
  744. ;; a hat, but I don't know...
  745. (push (list group (setq active (cons 1 1)))
  746. nnfolder-group-alist))
  747. (cdr active)))
  748. (defun nnfolder-possibly-change-folder (group)
  749. (let ((inf (assoc group nnfolder-buffer-alist)))
  750. (if (and inf
  751. (gnus-buffer-live-p (cadr inf)))
  752. (set-buffer (cadr inf))
  753. (when inf
  754. (setq nnfolder-buffer-alist (delq inf nnfolder-buffer-alist)))
  755. (when nnfolder-group-alist
  756. (nnfolder-save-active nnfolder-group-alist nnfolder-active-file))
  757. (push (list group (nnfolder-read-folder group))
  758. nnfolder-buffer-alist))))
  759. ;; This method has a problem if you've accidentally let the active
  760. ;; list get out of sync with the files. This could happen, say, if
  761. ;; you've accidentally gotten new mail with something other than Gnus
  762. ;; (but why would _that_ ever happen? :-). In that case, we will be
  763. ;; in the middle of processing the file, ready to add new X-Gnus
  764. ;; article number markers, and we'll run across a message with no ID
  765. ;; yet - the active list _may_not_ be ready for us yet.
  766. ;; To handle this, I'm modifying this routine to maintain the maximum
  767. ;; ID seen so far, and when we hit a message with no ID, we will
  768. ;; _manually_ scan the rest of the message looking for any more,
  769. ;; possibly higher IDs. We'll assume the maximum that we find is the
  770. ;; highest active. Note that this shouldn't cost us much extra time
  771. ;; at all, but will be a lot less vulnerable to glitches between the
  772. ;; mbox and the active file.
  773. (defun nnfolder-read-folder (group)
  774. (let* ((file (nnfolder-group-pathname group))
  775. (nov (nnfolder-group-nov-pathname group))
  776. (buffer (set-buffer
  777. (let ((nnheader-file-coding-system
  778. nnfolder-file-coding-system))
  779. (nnheader-find-file-noselect file t)))))
  780. (mm-enable-multibyte) ;; Use multibyte buffer for future copying.
  781. (buffer-disable-undo)
  782. (if (equal (cadr (assoc group nnfolder-scantime-alist))
  783. (nth 5 (file-attributes file)))
  784. ;; This looks up-to-date, so we don't do any scanning.
  785. (if (file-exists-p file)
  786. buffer
  787. (push (list group buffer) nnfolder-buffer-alist)
  788. (set-buffer-modified-p t)
  789. (nnfolder-save-buffer))
  790. ;; Parse the damn thing.
  791. (save-excursion
  792. (goto-char (point-min))
  793. ;; Remove any blank lines at the start.
  794. (while (eq (following-char) ?\n)
  795. (delete-char 1))
  796. (nnmail-activate 'nnfolder)
  797. ;; Read in the file.
  798. (let ((delim "^From ")
  799. (marker (concat "\n" nnfolder-article-marker))
  800. (number "[0-9]+")
  801. (active (or (cadr (assoc group nnfolder-group-alist))
  802. (cons 1 0)))
  803. (scantime (assoc group nnfolder-scantime-alist))
  804. (minid (or (and (boundp 'most-positive-fixnum)
  805. most-positive-fixnum)
  806. (lsh -1 -1)))
  807. maxid start end newscantime
  808. novbuf articles newnum
  809. buffer-read-only)
  810. (setq maxid (cdr active))
  811. (unless (or gnus-nov-is-evil nnfolder-nov-is-evil
  812. (and (file-exists-p nov)
  813. (file-newer-than-file-p nov file)))
  814. (unless (file-exists-p nov)
  815. (gnus-make-directory (file-name-directory nov)))
  816. (with-current-buffer
  817. (setq novbuf (nnfolder-open-nov group))
  818. (goto-char (point-min))
  819. (while (not (eobp))
  820. (push (read novbuf) articles)
  821. (forward-line 1))
  822. (setq articles (nreverse articles))))
  823. (goto-char (point-min))
  824. ;; Anytime the active number is 1 or 0, it is suspect. In
  825. ;; that case, search the file manually to find the active
  826. ;; number. Or, of course, if we're being paranoid. (This
  827. ;; would also be the place to build other lists from the
  828. ;; header markers, such as expunge lists, etc., if we ever
  829. ;; desired to abandon the active file entirely for mboxes.)
  830. (when (or nnfolder-ignore-active-file
  831. novbuf
  832. (< maxid 2))
  833. (while (and (search-forward marker nil t)
  834. (looking-at number))
  835. (setq newnum (string-to-number (match-string 0)))
  836. (when (nnmail-within-headers-p)
  837. (setq maxid (max maxid newnum)
  838. minid (min minid newnum))
  839. (when novbuf
  840. (if (memq newnum articles)
  841. (setq articles (delq newnum articles))
  842. (let ((headers (nnfolder-parse-head newnum)))
  843. (with-current-buffer novbuf
  844. (nnheader-find-nov-line newnum)
  845. (nnheader-insert-nov headers)))))))
  846. (when (and novbuf articles)
  847. (with-current-buffer novbuf
  848. (dolist (article articles)
  849. (when (nnheader-find-nov-line article)
  850. (delete-region (point)
  851. (progn (forward-line 1) (point)))))))
  852. (setcar active (max 1 (min minid maxid)))
  853. (setcdr active (max maxid (cdr active)))
  854. (goto-char (point-min)))
  855. ;; As long as we trust that the user will only insert
  856. ;; unmarked mail at the end, go to the end and search
  857. ;; backwards for the last marker. Find the start of that
  858. ;; message, and begin to search for unmarked messages from
  859. ;; there.
  860. (when (not (or nnfolder-distrust-mbox
  861. (< maxid 2)))
  862. (goto-char (point-max))
  863. (unless (re-search-backward marker nil t)
  864. (goto-char (point-min)))
  865. ;;(when (nnmail-search-unix-mail-delim)
  866. ;; (goto-char (point-min)))
  867. )
  868. ;; Keep track of the active number on our own, and insert it
  869. ;; back into the active list when we're done. Also, prime
  870. ;; the pump to cut down on the number of searches we do.
  871. (unless (nnmail-search-unix-mail-delim)
  872. (goto-char (point-max)))
  873. (setq end (point-marker))
  874. (while (not (= end (point-max)))
  875. (setq start (marker-position end))
  876. (goto-char end)
  877. ;; There may be more than one "From " line, so we skip past
  878. ;; them.
  879. (while (looking-at delim)
  880. (forward-line 1))
  881. (set-marker end (if (nnmail-search-unix-mail-delim)
  882. (point)
  883. (point-max)))
  884. (goto-char start)
  885. (when (not (search-forward marker end t))
  886. (narrow-to-region start end)
  887. (nnmail-insert-lines)
  888. (nnfolder-insert-newsgroup-line
  889. (cons nil
  890. (setq newnum
  891. (nnfolder-active-number group))))
  892. (when novbuf
  893. (let ((headers (nnfolder-parse-head newnum (point-min)
  894. (point-max))))
  895. (with-current-buffer novbuf
  896. (goto-char (point-max))
  897. (nnheader-insert-nov headers))))
  898. (widen)))
  899. (set-marker end nil)
  900. ;; Make absolutely sure that the active list reflects
  901. ;; reality!
  902. (nnfolder-save-active nnfolder-group-alist nnfolder-active-file)
  903. ;; Set the scantime for this group.
  904. (setq newscantime (visited-file-modtime))
  905. (if scantime
  906. (setcdr scantime (list newscantime))
  907. (push (list group newscantime)
  908. nnfolder-scantime-alist))
  909. ;; Save nov.
  910. (when novbuf
  911. (nnfolder-save-nov))
  912. (current-buffer))))))
  913. (defun nnfolder-recursive-directory-files (dir prefix)
  914. (let ((files nil))
  915. (dolist (file (directory-files dir))
  916. (cond
  917. ((or (file-symlink-p (expand-file-name file dir))
  918. (member file '("." "..")))
  919. ;; Ignore
  920. )
  921. ((file-directory-p (expand-file-name file dir))
  922. (setq files (nconc (nnfolder-recursive-directory-files
  923. (expand-file-name file dir)
  924. (if prefix
  925. (concat prefix "." (directory-file-name file))
  926. (file-name-nondirectory file)))
  927. files)))
  928. ((file-regular-p (expand-file-name file dir))
  929. (push (if prefix
  930. (concat prefix "." file)
  931. file)
  932. files))))
  933. files))
  934. ;;;###autoload
  935. (defun nnfolder-generate-active-file ()
  936. "Look for mbox folders in the nnfolder directory and make them into groups.
  937. This command does not work if you use short group names."
  938. (interactive)
  939. (nnmail-activate 'nnfolder)
  940. (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
  941. (dolist (file (directory-files (or nnfolder-nov-directory
  942. nnfolder-directory)
  943. t
  944. (concat
  945. (regexp-quote nnfolder-nov-file-suffix)
  946. "$")))
  947. (when (not (message-mail-file-mbox-p file))
  948. (ignore-errors
  949. (delete-file file)))))
  950. (dolist (file (if nnmail-use-long-file-names
  951. (directory-files nnfolder-directory)
  952. (nnfolder-recursive-directory-files
  953. nnfolder-directory nil)))
  954. (when (and (not (backup-file-name-p file))
  955. (message-mail-file-mbox-p
  956. (nnfolder-group-pathname file)))
  957. (let ((oldgroup (assoc file nnfolder-group-alist)))
  958. (if oldgroup
  959. (nnheader-message 5 "Refreshing group %s..." file)
  960. (nnheader-message 5 "Adding group %s..." file))
  961. (if oldgroup
  962. (setq nnfolder-group-alist
  963. (delq oldgroup (copy-sequence nnfolder-group-alist))))
  964. (push (list file (cons 1 0)) nnfolder-group-alist)
  965. (nnfolder-possibly-change-folder file)
  966. (nnfolder-possibly-change-group file)
  967. (nnfolder-close-group file))))
  968. (nnheader-message 5 ""))
  969. (defun nnfolder-group-pathname (group)
  970. "Make file name for GROUP."
  971. (setq group
  972. (mm-encode-coding-string group nnmail-pathname-coding-system))
  973. (let ((dir (file-name-as-directory (expand-file-name nnfolder-directory))))
  974. ;; If this file exists, we use it directly.
  975. (if (or nnmail-use-long-file-names
  976. (file-exists-p (concat dir group)))
  977. (concat dir group)
  978. ;; If not, we translate dots into slashes.
  979. (concat dir (nnheader-replace-chars-in-string group ?. ?/)))))
  980. (defun nnfolder-group-nov-pathname (group)
  981. "Make pathname for GROUP NOV."
  982. (let ((nnfolder-directory
  983. (or nnfolder-nov-directory nnfolder-directory)))
  984. (concat (nnfolder-group-pathname group) nnfolder-nov-file-suffix)))
  985. (defvar copyright-update)
  986. (defun nnfolder-save-buffer ()
  987. "Save the buffer."
  988. (let ((delete-old-versions t))
  989. (when (buffer-modified-p)
  990. (run-hooks 'nnfolder-save-buffer-hook)
  991. (gnus-make-directory (file-name-directory (buffer-file-name)))
  992. (let ((coding-system-for-write
  993. (or nnfolder-file-coding-system-for-write
  994. nnfolder-file-coding-system)))
  995. (set (make-local-variable 'copyright-update) nil)
  996. (save-buffer)))
  997. (unless (or gnus-nov-is-evil nnfolder-nov-is-evil)
  998. (nnfolder-save-nov))))
  999. (defun nnfolder-save-active (group-alist active-file)
  1000. (let ((nnmail-active-file-coding-system
  1001. (or nnfolder-active-file-coding-system-for-write
  1002. nnfolder-active-file-coding-system)))
  1003. (nnmail-save-active group-alist active-file)))
  1004. (defun nnfolder-open-nov (group)
  1005. (or (cdr (assoc group nnfolder-nov-buffer-alist))
  1006. (let ((buffer (get-buffer-create (format " *nnfolder overview %s*" group))))
  1007. (with-current-buffer buffer
  1008. (set (make-local-variable 'nnfolder-nov-buffer-file-name)
  1009. (nnfolder-group-nov-pathname group))
  1010. (erase-buffer)
  1011. (when (file-exists-p nnfolder-nov-buffer-file-name)
  1012. (nnheader-insert-file-contents nnfolder-nov-buffer-file-name)))
  1013. (push (cons group buffer) nnfolder-nov-buffer-alist)
  1014. buffer)))
  1015. (defun nnfolder-save-nov ()
  1016. (save-excursion
  1017. (while nnfolder-nov-buffer-alist
  1018. (when (buffer-name (cdar nnfolder-nov-buffer-alist))
  1019. (set-buffer (cdar nnfolder-nov-buffer-alist))
  1020. (when (buffer-modified-p)
  1021. (gnus-make-directory (file-name-directory
  1022. nnfolder-nov-buffer-file-name))
  1023. (nnmail-write-region 1 (point-max) nnfolder-nov-buffer-file-name
  1024. nil 'nomesg))
  1025. (set-buffer-modified-p nil)
  1026. (kill-buffer (current-buffer)))
  1027. (setq nnfolder-nov-buffer-alist (cdr nnfolder-nov-buffer-alist)))))
  1028. (defun nnfolder-nov-delete-article (group article)
  1029. (with-current-buffer (nnfolder-open-nov group)
  1030. (when (nnheader-find-nov-line article)
  1031. (delete-region (point) (progn (forward-line 1) (point))))
  1032. t))
  1033. (defun nnfolder-retrieve-headers-with-nov (articles &optional fetch-old)
  1034. (if (or gnus-nov-is-evil nnfolder-nov-is-evil)
  1035. nil
  1036. (let ((nov (nnfolder-group-nov-pathname nnfolder-current-group)))
  1037. (when (file-exists-p nov)
  1038. (with-current-buffer nntp-server-buffer
  1039. (erase-buffer)
  1040. (nnheader-insert-file-contents nov)
  1041. (if (and fetch-old
  1042. (not (numberp fetch-old)))
  1043. t ; Don't remove anything.
  1044. (nnheader-nov-delete-outside-range
  1045. (if fetch-old (max 1 (- (car articles) fetch-old))
  1046. (car articles))
  1047. (car (last articles)))
  1048. t))))))
  1049. (defun nnfolder-parse-head (&optional number b e)
  1050. "Parse the head of the current buffer."
  1051. (let ((buf (current-buffer))
  1052. chars)
  1053. (save-excursion
  1054. (unless b
  1055. (setq b (if (nnmail-search-unix-mail-delim-backward)
  1056. (point) (point-min)))
  1057. (forward-line 1)
  1058. (setq e (if (nnmail-search-unix-mail-delim)
  1059. (point) (point-max))))
  1060. (setq chars (- e b))
  1061. (unless (zerop chars)
  1062. (goto-char b)
  1063. (if (search-forward "\n\n" e t) (setq e (1- (point)))))
  1064. (with-temp-buffer
  1065. (insert-buffer-substring buf b e)
  1066. (let ((headers (nnheader-parse-naked-head)))
  1067. (mail-header-set-chars headers chars)
  1068. (mail-header-set-number headers number)
  1069. headers)))))
  1070. (defun nnfolder-add-nov (group article headers)
  1071. "Add a nov line for the GROUP base."
  1072. (with-current-buffer (nnfolder-open-nov group)
  1073. (goto-char (point-max))
  1074. (mail-header-set-number headers article)
  1075. (nnheader-insert-nov headers)))
  1076. (provide 'nnfolder)
  1077. ;;; nnfolder.el ends here