nnfolder.el 46 KB

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