nnml.el 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326
  1. ;;; nnml.el --- mail spool access for Gnus
  2. ;; Copyright (C) 1995-2012 Free Software
  3. ;; Foundation, Inc.
  4. ;; Authors: Didier Verna <didier@xemacs.org> (adding compaction)
  5. ;; Simon Josefsson <simon@josefsson.org> (adding MARKS)
  6. ;; Lars Magne Ingebrigtsen <larsi@gnus.org>
  7. ;; Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
  8. ;; Keywords: news, 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. ;; Based on nnspool.el by Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>.
  22. ;; For an overview of what the interface functions do, please see the
  23. ;; Gnus sources.
  24. ;;; Code:
  25. (require 'gnus)
  26. (require 'nnheader)
  27. (require 'nnmail)
  28. (require 'nnoo)
  29. (eval-when-compile (require 'cl))
  30. ;; FIXME first is unused in this file.
  31. (autoload 'gnus-article-unpropagatable-p "gnus-sum")
  32. (autoload 'gnus-backlog-remove-article "gnus-bcklg")
  33. (nnoo-declare nnml)
  34. (defvoo nnml-directory message-directory
  35. "Spool directory for the nnml mail backend.")
  36. (defvoo nnml-active-file
  37. (expand-file-name "active" nnml-directory)
  38. "Mail active file.")
  39. (defvoo nnml-newsgroups-file
  40. (expand-file-name "newsgroups" nnml-directory)
  41. "Mail newsgroups description file.")
  42. (defvoo nnml-get-new-mail t
  43. "If non-nil, nnml will check the incoming mail file and split the mail.")
  44. (defvoo nnml-nov-is-evil nil
  45. "If non-nil, Gnus will never generate and use nov databases for mail spools.
  46. Using nov databases will speed up header fetching considerably.
  47. This variable shouldn't be flipped much. If you have, for some reason,
  48. set this to t, and want to set it to nil again, you should always run
  49. the `nnml-generate-nov-databases' command. The function will go
  50. through all nnml directories and generate nov databases for them
  51. all. This may very well take some time.")
  52. (defvoo nnml-marks-is-evil nil
  53. "If non-nil, Gnus will never generate and use marks file for mail spools.
  54. Using marks files makes it possible to backup and restore mail groups
  55. separately from `.newsrc.eld'. If you have, for some reason, set this
  56. to t, and want to set it to nil again, you should always remove the
  57. corresponding marks file (usually named `.marks' in the nnml group
  58. directory, but see `nnml-marks-file-name') for the group. Then the
  59. marks file will be regenerated properly by Gnus.")
  60. (defvoo nnml-prepare-save-mail-hook nil
  61. "Hook run narrowed to an article before saving.")
  62. (defvoo nnml-inhibit-expiry nil
  63. "If non-nil, inhibit expiry.")
  64. (defvoo nnml-use-compressed-files nil
  65. "If non-nil, allow using compressed message files.
  66. If it is a string, use it as the file extension which specifies
  67. the compression program. You can set it to \".bz2\" if your Emacs
  68. supports auto-compression using the bzip2 program. A value of t
  69. is equivalent to \".gz\".")
  70. (defvoo nnml-compressed-files-size-threshold 1000
  71. "Default size threshold for compressed message files.
  72. Message files with bodies larger than that many characters will
  73. be automatically compressed if `nnml-use-compressed-files' is
  74. non-nil.")
  75. (defconst nnml-version "nnml 1.0"
  76. "nnml version.")
  77. (defvoo nnml-nov-file-name ".overview")
  78. (defvoo nnml-marks-file-name ".marks")
  79. (defvoo nnml-current-directory nil)
  80. (defvoo nnml-current-group nil)
  81. (defvoo nnml-status-string "")
  82. (defvoo nnml-nov-buffer-alist nil)
  83. (defvoo nnml-group-alist nil)
  84. (defvoo nnml-active-timestamp nil)
  85. (defvoo nnml-article-file-alist nil)
  86. (defvoo nnml-generate-active-function 'nnml-generate-active-info)
  87. (defvar nnml-nov-buffer-file-name nil)
  88. (defvoo nnml-file-coding-system nnmail-file-coding-system)
  89. (defvoo nnml-marks nil)
  90. (defvar nnml-marks-modtime (gnus-make-hashtable))
  91. ;;; Interface functions.
  92. (nnoo-define-basics nnml)
  93. (eval-when-compile
  94. (defsubst nnml-group-name-charset (group server-or-method)
  95. (gnus-group-name-charset
  96. (if (stringp server-or-method)
  97. (gnus-server-to-method
  98. (if (string-match "\\+" server-or-method)
  99. (concat (substring server-or-method 0 (match-beginning 0))
  100. ":" (substring server-or-method (match-end 0)))
  101. (concat "nnml:" server-or-method)))
  102. (or server-or-method gnus-command-method '(nnml "")))
  103. group)))
  104. (defun nnml-decoded-group-name (group &optional server-or-method)
  105. "Return a decoded group name of GROUP on SERVER-OR-METHOD."
  106. (if nnmail-group-names-not-encoded-p
  107. group
  108. (mm-decode-coding-string
  109. group
  110. (nnml-group-name-charset group server-or-method))))
  111. (defun nnml-encoded-group-name (group &optional server-or-method)
  112. "Return an encoded group name of GROUP on SERVER-OR-METHOD."
  113. (mm-encode-coding-string
  114. group
  115. (nnml-group-name-charset group server-or-method)))
  116. (defun nnml-group-pathname (group &optional file server)
  117. "Return an absolute file name of FILE for GROUP on SERVER."
  118. (nnmail-group-pathname (inline (nnml-decoded-group-name group server))
  119. nnml-directory file))
  120. (deffoo nnml-retrieve-headers (sequence &optional group server fetch-old)
  121. (when (nnml-possibly-change-directory group server)
  122. (with-current-buffer nntp-server-buffer
  123. (erase-buffer)
  124. (let* ((file nil)
  125. (number (length sequence))
  126. (count 0)
  127. (file-name-coding-system nnmail-pathname-coding-system)
  128. beg article)
  129. (if (stringp (car sequence))
  130. 'headers
  131. (if (nnml-retrieve-headers-with-nov sequence fetch-old)
  132. 'nov
  133. (while sequence
  134. (setq article (car sequence))
  135. (setq file (nnml-article-to-file article))
  136. (when (and file
  137. (file-exists-p file)
  138. (not (file-directory-p file)))
  139. (insert (format "221 %d Article retrieved.\n" article))
  140. (setq beg (point))
  141. (nnheader-insert-head file)
  142. (goto-char beg)
  143. (if (re-search-forward "\n\r?\n" nil t)
  144. (forward-char -1)
  145. (goto-char (point-max))
  146. (insert "\n\n"))
  147. (insert ".\n")
  148. (delete-region (point) (point-max)))
  149. (setq sequence (cdr sequence))
  150. (setq count (1+ count))
  151. (and (numberp nnmail-large-newsgroup)
  152. (> number nnmail-large-newsgroup)
  153. (zerop (% count 20))
  154. (nnheader-message 6 "nnml: Receiving headers... %d%%"
  155. (/ (* count 100) number))))
  156. (and (numberp nnmail-large-newsgroup)
  157. (> number nnmail-large-newsgroup)
  158. (nnheader-message 6 "nnml: Receiving headers...done"))
  159. (nnheader-fold-continuation-lines)
  160. 'headers))))))
  161. (deffoo nnml-open-server (server &optional defs)
  162. (nnoo-change-server 'nnml server defs)
  163. (when (not (file-exists-p nnml-directory))
  164. (ignore-errors (make-directory nnml-directory t)))
  165. (cond
  166. ((not (file-exists-p nnml-directory))
  167. (nnml-close-server)
  168. (nnheader-report 'nnml "Couldn't create directory: %s" nnml-directory))
  169. ((not (file-directory-p (file-truename nnml-directory)))
  170. (nnml-close-server)
  171. (nnheader-report 'nnml "Not a directory: %s" nnml-directory))
  172. (t
  173. (nnheader-report 'nnml "Opened server %s using directory %s"
  174. server nnml-directory)
  175. t)))
  176. (deffoo nnml-request-regenerate (server)
  177. (nnml-possibly-change-directory nil server)
  178. (nnml-generate-nov-databases server)
  179. t)
  180. (deffoo nnml-request-article (id &optional group server buffer)
  181. (nnml-possibly-change-directory group server)
  182. (let* ((nntp-server-buffer (or buffer nntp-server-buffer))
  183. (file-name-coding-system nnmail-pathname-coding-system)
  184. path gpath group-num)
  185. (if (stringp id)
  186. (when (and (setq group-num (nnml-find-group-number id server))
  187. (cdr
  188. (assq (cdr group-num)
  189. (nnheader-article-to-file-alist
  190. (setq gpath (nnml-group-pathname (car group-num)
  191. nil server))))))
  192. (nnml-update-file-alist)
  193. (setq path (concat gpath (if nnml-use-compressed-files
  194. (cdr (assq (cdr group-num)
  195. nnml-article-file-alist))
  196. (number-to-string (cdr group-num))))))
  197. (setq path (nnml-article-to-file id)))
  198. (cond
  199. ((not path)
  200. (nnheader-report 'nnml "No such article: %s" id))
  201. ((not (file-exists-p path))
  202. (nnheader-report 'nnml "No such file: %s" path))
  203. ((file-directory-p path)
  204. (nnheader-report 'nnml "File is a directory: %s" path))
  205. ((not (save-excursion (let ((nnmail-file-coding-system
  206. nnml-file-coding-system))
  207. (nnmail-find-file path))))
  208. (nnheader-report 'nnml "Couldn't read file: %s" path))
  209. (t
  210. (nnheader-report 'nnml "Article %s retrieved" id)
  211. ;; We return the article number.
  212. (cons (if group-num (car group-num) group)
  213. (string-to-number (file-name-nondirectory path)))))))
  214. (deffoo nnml-request-group (group &optional server dont-check info)
  215. (let ((file-name-coding-system nnmail-pathname-coding-system)
  216. (decoded (nnml-decoded-group-name group server)))
  217. (cond
  218. ((not (nnml-possibly-change-directory group server))
  219. (nnheader-report 'nnml "Invalid group (no such directory)"))
  220. ((not (file-exists-p nnml-current-directory))
  221. (nnheader-report 'nnml "Directory %s does not exist"
  222. nnml-current-directory))
  223. ((not (file-directory-p nnml-current-directory))
  224. (nnheader-report 'nnml "%s is not a directory" nnml-current-directory))
  225. (dont-check
  226. (nnheader-report 'nnml "Group %s selected" decoded)
  227. t)
  228. (t
  229. (nnheader-re-read-dir nnml-current-directory)
  230. (nnmail-activate 'nnml)
  231. (let ((active (nth 1 (assoc group nnml-group-alist))))
  232. (if (not active)
  233. (nnheader-report 'nnml "No such group: %s" decoded)
  234. (nnheader-report 'nnml "Selected group %s" decoded)
  235. (nnheader-insert "211 %d %d %d %s\n"
  236. (max (1+ (- (cdr active) (car active))) 0)
  237. (car active) (cdr active) group)))))))
  238. (deffoo nnml-request-scan (&optional group server)
  239. (setq nnml-article-file-alist nil)
  240. (nnml-possibly-change-directory group server)
  241. (nnmail-get-new-mail 'nnml 'nnml-save-incremental-nov nnml-directory group))
  242. (deffoo nnml-close-group (group &optional server)
  243. (setq nnml-article-file-alist nil)
  244. t)
  245. (deffoo nnml-request-create-group (group &optional server args)
  246. (nnml-possibly-change-directory nil server)
  247. (nnmail-activate 'nnml)
  248. (cond
  249. ((let ((file (directory-file-name (nnml-group-pathname group nil server)))
  250. (file-name-coding-system nnmail-pathname-coding-system))
  251. (and (file-exists-p file)
  252. (not (file-directory-p file))))
  253. (nnheader-report 'nnml "%s is a file"
  254. (directory-file-name (nnml-group-pathname group
  255. nil server))))
  256. ((assoc group nnml-group-alist)
  257. t)
  258. (t
  259. (let (active)
  260. (push (list group (setq active (cons 1 0)))
  261. nnml-group-alist)
  262. (nnml-possibly-create-directory group server)
  263. (nnml-possibly-change-directory group server)
  264. (let* ((file-name-coding-system nnmail-pathname-coding-system)
  265. (articles (nnml-directory-articles nnml-current-directory)))
  266. (when articles
  267. (setcar active (apply 'min articles))
  268. (setcdr active (apply 'max articles))))
  269. (nnmail-save-active nnml-group-alist nnml-active-file)
  270. t))))
  271. (deffoo nnml-request-list (&optional server)
  272. (save-excursion
  273. (let ((nnmail-file-coding-system nnmail-active-file-coding-system)
  274. (file-name-coding-system nnmail-pathname-coding-system))
  275. (nnmail-find-file nnml-active-file))
  276. (setq nnml-group-alist (nnmail-get-active))
  277. t))
  278. (deffoo nnml-request-newgroups (date &optional server)
  279. (nnml-request-list server))
  280. (deffoo nnml-request-list-newsgroups (&optional server)
  281. (save-excursion
  282. (nnmail-find-file nnml-newsgroups-file)))
  283. (deffoo nnml-request-expire-articles (articles group &optional server force)
  284. (nnml-possibly-change-directory group server)
  285. (let* ((file-name-coding-system nnmail-pathname-coding-system)
  286. (active-articles
  287. (nnml-directory-articles nnml-current-directory))
  288. (is-old t)
  289. (decoded (nnml-decoded-group-name group server))
  290. article rest mod-time number target)
  291. (nnmail-activate 'nnml)
  292. (setq active-articles (sort active-articles '<))
  293. ;; Articles not listed in active-articles are already gone,
  294. ;; so don't try to expire them.
  295. (setq articles (gnus-sorted-intersection articles active-articles))
  296. (while (and articles is-old)
  297. (if (and (setq article (nnml-article-to-file
  298. (setq number (pop articles))))
  299. (setq mod-time (nth 5 (file-attributes article)))
  300. (nnml-deletable-article-p group number)
  301. (setq is-old (nnmail-expired-article-p group mod-time force
  302. nnml-inhibit-expiry)))
  303. (progn
  304. ;; Allow a special target group.
  305. (setq target nnmail-expiry-target)
  306. (unless (eq target 'delete)
  307. (with-temp-buffer
  308. (nnml-request-article number group server (current-buffer))
  309. (let (nnml-current-directory
  310. nnml-current-group
  311. nnml-article-file-alist)
  312. (when (functionp target)
  313. (setq target (funcall target group)))
  314. (when (and target (not (eq target 'delete)))
  315. (if (or (gnus-request-group target)
  316. (gnus-request-create-group target))
  317. (nnmail-expiry-target-group target group)
  318. (setq target nil)))))
  319. ;; Maybe directory is changed during nnmail-expiry-target-group.
  320. (nnml-possibly-change-directory group server))
  321. (if target
  322. (progn
  323. (nnheader-message 5 "Deleting article %s in %s"
  324. number decoded)
  325. (condition-case ()
  326. (funcall nnmail-delete-file-function article)
  327. (file-error
  328. (push number rest)))
  329. (setq active-articles (delq number active-articles))
  330. (nnml-nov-delete-article group number))
  331. (push number rest)))
  332. (push number rest)))
  333. (let ((active (nth 1 (assoc group nnml-group-alist))))
  334. (when active
  335. (setcar active (or (and active-articles
  336. (apply 'min active-articles))
  337. (1+ (cdr active)))))
  338. (nnmail-save-active nnml-group-alist nnml-active-file))
  339. (nnml-save-nov)
  340. (nconc rest articles)))
  341. (deffoo nnml-request-move-article
  342. (article group server accept-form &optional last move-is-internal)
  343. (let ((buf (get-buffer-create " *nnml move*"))
  344. (file-name-coding-system nnmail-pathname-coding-system)
  345. result)
  346. (nnml-possibly-change-directory group server)
  347. (nnml-update-file-alist)
  348. (and
  349. (nnml-deletable-article-p group article)
  350. (nnml-request-article article group server)
  351. (let (nnml-current-directory
  352. nnml-current-group
  353. nnml-article-file-alist)
  354. (with-current-buffer buf
  355. (insert-buffer-substring nntp-server-buffer)
  356. (setq result (eval accept-form))
  357. (kill-buffer (current-buffer))
  358. result))
  359. (progn
  360. (nnml-possibly-change-directory group server)
  361. (condition-case ()
  362. (funcall nnmail-delete-file-function
  363. (nnml-article-to-file article))
  364. (file-error nil))
  365. (nnml-nov-delete-article group article)
  366. (when last
  367. (nnml-save-nov)
  368. (nnmail-save-active nnml-group-alist nnml-active-file))))
  369. result))
  370. (deffoo nnml-request-accept-article (group &optional server last)
  371. (nnml-possibly-change-directory group server)
  372. (nnmail-check-syntax)
  373. (let (result)
  374. (when nnmail-cache-accepted-message-ids
  375. (nnmail-cache-insert (nnmail-fetch-field "message-id")
  376. group
  377. (nnmail-fetch-field "subject")
  378. (nnmail-fetch-field "from")))
  379. (if (stringp group)
  380. (and
  381. (nnmail-activate 'nnml)
  382. (setq result (car (nnml-save-mail
  383. (list (cons group (nnml-active-number group
  384. server)))
  385. server t)))
  386. (progn
  387. (nnmail-save-active nnml-group-alist nnml-active-file)
  388. (and last (nnml-save-nov))))
  389. (and
  390. (nnmail-activate 'nnml)
  391. (if (and (not (setq result (nnmail-article-group
  392. `(lambda (group)
  393. (nnml-active-number group ,server)))))
  394. (yes-or-no-p "Moved to `junk' group; delete article? "))
  395. (setq result 'junk)
  396. (setq result (car (nnml-save-mail result server t))))
  397. (when last
  398. (nnmail-save-active nnml-group-alist nnml-active-file)
  399. (when nnmail-cache-accepted-message-ids
  400. (nnmail-cache-close))
  401. (nnml-save-nov))))
  402. result))
  403. (deffoo nnml-request-post (&optional server)
  404. (nnmail-do-request-post 'nnml-request-accept-article server))
  405. (deffoo nnml-request-replace-article (article group buffer)
  406. (nnml-possibly-change-directory group)
  407. (with-current-buffer buffer
  408. (nnml-possibly-create-directory group)
  409. (let ((chars (nnmail-insert-lines))
  410. (art (concat (int-to-string article) "\t"))
  411. headers)
  412. (when (ignore-errors
  413. (nnmail-write-region
  414. (point-min) (point-max)
  415. (or (nnml-article-to-file article)
  416. (expand-file-name (int-to-string article)
  417. nnml-current-directory))
  418. nil (if (nnheader-be-verbose 5) nil 'nomesg))
  419. t)
  420. (setq headers (nnml-parse-head chars article))
  421. ;; Replace the NOV line in the NOV file.
  422. (with-current-buffer (nnml-open-nov group)
  423. (goto-char (point-min))
  424. (if (or (looking-at art)
  425. (search-forward (concat "\n" art) nil t))
  426. ;; Delete the old NOV line.
  427. (gnus-delete-line)
  428. ;; The line isn't here, so we have to find out where
  429. ;; we should insert it. (This situation should never
  430. ;; occur, but one likes to make sure...)
  431. (while (and (looking-at "[0-9]+\t")
  432. (< (string-to-number
  433. (buffer-substring
  434. (match-beginning 0) (match-end 0)))
  435. article)
  436. (zerop (forward-line 1)))))
  437. (beginning-of-line)
  438. (nnheader-insert-nov headers)
  439. (nnml-save-nov)
  440. t)))))
  441. (deffoo nnml-request-delete-group (group &optional force server)
  442. (nnml-possibly-change-directory group server)
  443. (let ((file (directory-file-name nnml-current-directory))
  444. (file-name-coding-system nnmail-pathname-coding-system))
  445. (if (file-exists-p file)
  446. (if (file-directory-p file)
  447. (progn
  448. (when force
  449. ;; Delete all articles in GROUP.
  450. (let ((articles
  451. (directory-files
  452. nnml-current-directory t
  453. (concat
  454. nnheader-numerical-short-files
  455. "\\|" (regexp-quote nnml-nov-file-name) "$"
  456. "\\|" (regexp-quote nnml-marks-file-name) "$")))
  457. (decoded (nnml-decoded-group-name group server)))
  458. (dolist (article articles)
  459. (when (file-writable-p article)
  460. (nnheader-message 5 "Deleting article %s in %s..."
  461. (file-name-nondirectory article)
  462. decoded)
  463. (funcall nnmail-delete-file-function article))))
  464. ;; Try to delete the directory itself.
  465. (ignore-errors (delete-directory nnml-current-directory))))
  466. (nnheader-report 'nnml "%s is not a directory" file))
  467. (nnheader-report 'nnml "No such directory: %s/" file))
  468. ;; Remove the group from all structures.
  469. (setq nnml-group-alist
  470. (delq (assoc group nnml-group-alist) nnml-group-alist)
  471. nnml-current-group nil
  472. nnml-current-directory nil)
  473. ;; Save the active file.
  474. (nnmail-save-active nnml-group-alist nnml-active-file))
  475. t)
  476. (deffoo nnml-request-rename-group (group new-name &optional server)
  477. (nnml-possibly-change-directory group server)
  478. (let ((new-dir (nnml-group-pathname new-name nil server))
  479. (old-dir (nnml-group-pathname group nil server))
  480. (file-name-coding-system nnmail-pathname-coding-system))
  481. (when (ignore-errors
  482. (make-directory new-dir t)
  483. t)
  484. ;; We move the articles file by file instead of renaming
  485. ;; the directory -- there may be subgroups in this group.
  486. ;; One might be more clever, I guess.
  487. (dolist (file (nnheader-article-to-file-alist old-dir))
  488. (rename-file
  489. (concat old-dir (cdr file))
  490. (concat new-dir (cdr file))))
  491. ;; Move .overview file.
  492. (let ((overview (concat old-dir nnml-nov-file-name)))
  493. (when (file-exists-p overview)
  494. (rename-file overview (concat new-dir nnml-nov-file-name))))
  495. ;; Move .marks file.
  496. (let ((marks (concat old-dir nnml-marks-file-name)))
  497. (when (file-exists-p marks)
  498. (rename-file marks (concat new-dir nnml-marks-file-name))))
  499. (when (<= (length (directory-files old-dir)) 2)
  500. (ignore-errors (delete-directory old-dir)))
  501. ;; That went ok, so we change the internal structures.
  502. (let ((entry (assoc group nnml-group-alist)))
  503. (when entry
  504. (setcar entry new-name))
  505. (setq nnml-current-directory nil
  506. nnml-current-group nil)
  507. ;; Save the new group alist.
  508. (nnmail-save-active nnml-group-alist nnml-active-file)
  509. t))))
  510. (deffoo nnml-set-status (article name value &optional group server)
  511. (nnml-possibly-change-directory group server)
  512. (let ((file (nnml-article-to-file article)))
  513. (cond
  514. ((not (file-exists-p file))
  515. (nnheader-report 'nnml "File %s does not exist" file))
  516. (t
  517. (with-temp-file file
  518. (nnheader-insert-file-contents file)
  519. (nnmail-replace-status name value))
  520. t))))
  521. ;;; Internal functions.
  522. (defun nnml-article-to-file (article)
  523. (nnml-update-file-alist)
  524. (let (file)
  525. (if (setq file
  526. (if nnml-use-compressed-files
  527. (cdr (assq article nnml-article-file-alist))
  528. (number-to-string article)))
  529. (expand-file-name file nnml-current-directory)
  530. (when (not nnheader-directory-files-is-safe)
  531. ;; Just to make sure nothing went wrong when reading over NFS --
  532. ;; check once more.
  533. (when (file-exists-p
  534. (setq file (expand-file-name (number-to-string article)
  535. nnml-current-directory)))
  536. (nnml-update-file-alist t)
  537. file)))))
  538. (defun nnml-deletable-article-p (group article)
  539. "Say whether ARTICLE in GROUP can be deleted."
  540. (let ((file-name-coding-system nnmail-pathname-coding-system)
  541. path)
  542. (when (setq path (nnml-article-to-file article))
  543. (when (file-writable-p path)
  544. (or (not nnmail-keep-last-article)
  545. (not (eq (cdr (nth 1 (assoc group nnml-group-alist)))
  546. article)))))))
  547. ;; Find an article number in the current group given the Message-ID.
  548. (defun nnml-find-group-number (id server)
  549. (with-current-buffer (get-buffer-create " *nnml id*")
  550. (let ((alist nnml-group-alist)
  551. number)
  552. ;; We want to look through all .overview files, but we want to
  553. ;; start with the one in the current directory. It seems most
  554. ;; likely that the article we are looking for is in that group.
  555. (if (setq number (nnml-find-id nnml-current-group id server))
  556. (cons nnml-current-group number)
  557. ;; It wasn't there, so we look through the other groups as well.
  558. (while (and (not number)
  559. alist)
  560. (or (string= (caar alist) nnml-current-group)
  561. (setq number (nnml-find-id (caar alist) id server)))
  562. (or number
  563. (setq alist (cdr alist))))
  564. (and number
  565. (cons (caar alist) number))))))
  566. (defun nnml-find-id (group id server)
  567. (erase-buffer)
  568. (let ((nov (nnml-group-pathname group nnml-nov-file-name server))
  569. number found)
  570. (when (file-exists-p nov)
  571. (nnheader-insert-file-contents nov)
  572. (while (and (not found)
  573. (search-forward id nil t)) ; We find the ID.
  574. ;; And the id is in the fourth field.
  575. (if (not (and (search-backward "\t" nil t 4)
  576. (not (search-backward "\t" (point-at-bol) t))))
  577. (forward-line 1)
  578. (beginning-of-line)
  579. (setq found t)
  580. ;; We return the article number.
  581. (setq number
  582. (ignore-errors (read (current-buffer))))))
  583. number)))
  584. (defun nnml-retrieve-headers-with-nov (articles &optional fetch-old)
  585. (if (or gnus-nov-is-evil nnml-nov-is-evil)
  586. nil
  587. (let ((nov (expand-file-name nnml-nov-file-name nnml-current-directory)))
  588. (when (file-exists-p nov)
  589. (with-current-buffer nntp-server-buffer
  590. (erase-buffer)
  591. (nnheader-insert-file-contents nov)
  592. (if (and fetch-old
  593. (not (numberp fetch-old)))
  594. t ; Don't remove anything.
  595. (nnheader-nov-delete-outside-range
  596. (if fetch-old (max 1 (- (car articles) fetch-old))
  597. (car articles))
  598. (car (last articles)))
  599. t))))))
  600. (defun nnml-possibly-change-directory (group &optional server)
  601. (when (and server
  602. (not (nnml-server-opened server)))
  603. (nnml-open-server server))
  604. (if (not group)
  605. t
  606. (let ((pathname (nnml-group-pathname group nil server))
  607. (file-name-coding-system nnmail-pathname-coding-system))
  608. (when (not (equal pathname nnml-current-directory))
  609. (setq nnml-current-directory pathname
  610. nnml-current-group group
  611. nnml-article-file-alist nil))
  612. (file-exists-p nnml-current-directory))))
  613. (defun nnml-possibly-create-directory (group &optional server)
  614. (let ((dir (nnml-group-pathname group nil server))
  615. (file-name-coding-system nnmail-pathname-coding-system))
  616. (unless (file-exists-p dir)
  617. (make-directory (directory-file-name dir) t)
  618. (nnheader-message 5 "Creating mail directory %s" dir))))
  619. (defun nnml-save-mail (group-art &optional server full-nov)
  620. "Save a mail into the groups GROUP-ART in the nnml server SERVER.
  621. GROUP-ART is a list that each element is a cons of a group name and an
  622. article number. This function is called narrowed to an article."
  623. (let* ((chars (nnmail-insert-lines))
  624. (extension (and nnml-use-compressed-files
  625. (> chars nnml-compressed-files-size-threshold)
  626. (if (stringp nnml-use-compressed-files)
  627. nnml-use-compressed-files
  628. ".gz")))
  629. decoded dec file first headers)
  630. (when nnmail-group-names-not-encoded-p
  631. (dolist (ga (prog1 group-art (setq group-art nil)))
  632. (setq group-art (nconc group-art
  633. (list (cons (nnml-encoded-group-name (car ga)
  634. server)
  635. (cdr ga))))
  636. decoded (nconc decoded (list (car ga)))))
  637. (setq dec decoded))
  638. (nnmail-insert-xref group-art)
  639. (run-hooks 'nnmail-prepare-save-mail-hook)
  640. (run-hooks 'nnml-prepare-save-mail-hook)
  641. (goto-char (point-min))
  642. (while (looking-at "From ")
  643. (replace-match "X-From-Line: ")
  644. (forward-line 1))
  645. ;; We save the article in all the groups it belongs in.
  646. (dolist (ga group-art)
  647. (if nnmail-group-names-not-encoded-p
  648. (progn
  649. (nnml-possibly-create-directory (car decoded) server)
  650. (setq file (nnmail-group-pathname
  651. (pop decoded) nnml-directory
  652. (concat (number-to-string (cdr ga)) extension))))
  653. (nnml-possibly-create-directory (car ga) server)
  654. (setq file (nnml-group-pathname
  655. (car ga) (concat (number-to-string (cdr ga)) extension)
  656. server)))
  657. (if first
  658. ;; It was already saved, so we just make a hard link.
  659. (let ((file-name-coding-system nnmail-pathname-coding-system))
  660. (funcall nnmail-crosspost-link-function first file t))
  661. ;; Save the article.
  662. (nnmail-write-region (point-min) (point-max) file nil
  663. (if (nnheader-be-verbose 5) nil 'nomesg))
  664. (setq first file)))
  665. ;; Generate a nov line for this article. We generate the nov
  666. ;; line after saving, because nov generation destroys the
  667. ;; header.
  668. (setq headers (nnml-parse-head chars))
  669. ;; Output the nov line to all nov databases that should have it.
  670. (let ((func (if full-nov
  671. 'nnml-add-nov
  672. 'nnml-add-incremental-nov)))
  673. (if nnmail-group-names-not-encoded-p
  674. (dolist (ga group-art)
  675. (funcall func (pop dec) (cdr ga) headers))
  676. (dolist (ga group-art)
  677. (funcall func (car ga) (cdr ga) headers)))))
  678. group-art)
  679. (defun nnml-active-number (group &optional server)
  680. "Compute the next article number in GROUP on SERVER."
  681. (let* ((encoded (if nnmail-group-names-not-encoded-p
  682. (nnml-encoded-group-name group server)))
  683. (active (cadr (assoc (or encoded group) nnml-group-alist))))
  684. ;; The group wasn't known to nnml, so we just create an active
  685. ;; entry for it.
  686. (unless active
  687. ;; Perhaps the active file was corrupt? See whether
  688. ;; there are any articles in this group.
  689. (nnml-possibly-create-directory group server)
  690. (nnml-possibly-change-directory group server)
  691. (unless nnml-article-file-alist
  692. (setq nnml-article-file-alist
  693. (sort
  694. (nnml-current-group-article-to-file-alist)
  695. 'car-less-than-car)))
  696. (setq active
  697. (if nnml-article-file-alist
  698. (cons (caar nnml-article-file-alist)
  699. (caar (last nnml-article-file-alist)))
  700. (cons 1 0)))
  701. (push (list (or encoded group) active) nnml-group-alist))
  702. (setcdr active (1+ (cdr active)))
  703. (while (file-exists-p
  704. (nnml-group-pathname group (int-to-string (cdr active)) server))
  705. (setcdr active (1+ (cdr active))))
  706. (cdr active)))
  707. (defvar nnml-incremental-nov-buffer-alist nil)
  708. (defun nnml-save-incremental-nov ()
  709. (save-excursion
  710. (while nnml-incremental-nov-buffer-alist
  711. (when (buffer-name (cdar nnml-incremental-nov-buffer-alist))
  712. (set-buffer (cdar nnml-incremental-nov-buffer-alist))
  713. (when (buffer-modified-p)
  714. (nnmail-write-region (point-min) (point-max)
  715. nnml-nov-buffer-file-name t 'nomesg))
  716. (set-buffer-modified-p nil)
  717. (kill-buffer (current-buffer)))
  718. (setq nnml-incremental-nov-buffer-alist
  719. (cdr nnml-incremental-nov-buffer-alist)))))
  720. (defun nnml-open-incremental-nov (group)
  721. (or (cdr (assoc group nnml-incremental-nov-buffer-alist))
  722. (let ((buffer (nnml-get-nov-buffer group t)))
  723. (push (cons group buffer) nnml-incremental-nov-buffer-alist)
  724. buffer)))
  725. (defun nnml-add-incremental-nov (group article headers)
  726. "Add a nov line for the GROUP nov headers, incrementally."
  727. (with-current-buffer (nnml-open-incremental-nov group)
  728. (goto-char (point-max))
  729. (mail-header-set-number headers article)
  730. (nnheader-insert-nov headers)))
  731. (defun nnml-add-nov (group article headers)
  732. "Add a nov line for the GROUP base."
  733. (with-current-buffer (nnml-open-nov group)
  734. (goto-char (point-max))
  735. (mail-header-set-number headers article)
  736. (nnheader-insert-nov headers)))
  737. (defsubst nnml-header-value ()
  738. (buffer-substring (match-end 0) (point-at-eol)))
  739. (defun nnml-parse-head (chars &optional number)
  740. "Parse the head of the current buffer."
  741. (save-excursion
  742. (save-restriction
  743. (unless (zerop (buffer-size))
  744. (narrow-to-region
  745. (goto-char (point-min))
  746. (if (re-search-forward "\n\r?\n" nil t)
  747. (1- (point))
  748. (point-max))))
  749. (let ((headers (nnheader-parse-naked-head)))
  750. (mail-header-set-chars headers chars)
  751. (mail-header-set-number headers number)
  752. headers))))
  753. (defun nnml-get-nov-buffer (group &optional incrementalp)
  754. (let* ((decoded (nnml-decoded-group-name group))
  755. (buffer (get-buffer-create (format " *nnml %soverview %s*"
  756. (if incrementalp
  757. "incremental "
  758. "")
  759. decoded)))
  760. (file-name-coding-system nnmail-pathname-coding-system))
  761. (with-current-buffer buffer
  762. (set (make-local-variable 'nnml-nov-buffer-file-name)
  763. (nnmail-group-pathname decoded nnml-directory nnml-nov-file-name))
  764. (erase-buffer)
  765. (when (and (not incrementalp)
  766. (file-exists-p nnml-nov-buffer-file-name))
  767. (nnheader-insert-file-contents nnml-nov-buffer-file-name)))
  768. buffer))
  769. (defun nnml-open-nov (group)
  770. (or (let ((buffer (cdr (assoc group nnml-nov-buffer-alist))))
  771. (and (buffer-name buffer)
  772. buffer))
  773. (let ((buffer (nnml-get-nov-buffer group)))
  774. (push (cons group buffer) nnml-nov-buffer-alist)
  775. buffer)))
  776. (defun nnml-save-nov ()
  777. (save-excursion
  778. (while nnml-nov-buffer-alist
  779. (when (buffer-name (cdar nnml-nov-buffer-alist))
  780. (set-buffer (cdar nnml-nov-buffer-alist))
  781. (when (buffer-modified-p)
  782. (nnmail-write-region (point-min) (point-max)
  783. nnml-nov-buffer-file-name nil 'nomesg))
  784. (set-buffer-modified-p nil)
  785. (kill-buffer (current-buffer)))
  786. (setq nnml-nov-buffer-alist (cdr nnml-nov-buffer-alist)))))
  787. ;;;###autoload
  788. (defun nnml-generate-nov-databases (&optional server)
  789. "Generate NOV databases in all nnml directories."
  790. (interactive (list (or (nnoo-current-server 'nnml) "")))
  791. ;; Read the active file to make sure we don't re-use articles
  792. ;; numbers in empty groups.
  793. (nnmail-activate 'nnml)
  794. (unless (nnml-server-opened server)
  795. (nnml-open-server server))
  796. (setq nnml-directory (expand-file-name nnml-directory))
  797. ;; Recurse down the directories.
  798. (nnml-generate-nov-databases-directory nnml-directory nil t)
  799. ;; Save the active file.
  800. (nnmail-save-active nnml-group-alist nnml-active-file))
  801. (defvar nnml-files)
  802. (defun nnml-generate-nov-databases-directory (dir &optional seen no-active)
  803. "Regenerate the NOV database in DIR.
  804. Unless no-active is non-nil, update the active file too."
  805. (interactive (list (let ((file-name-coding-system
  806. nnmail-pathname-coding-system))
  807. (read-directory-name "Regenerate NOV in: "
  808. nnml-directory nil t))))
  809. (setq dir (file-name-as-directory dir))
  810. (let ((file-name-coding-system nnmail-pathname-coding-system))
  811. ;; Only scan this sub-tree if we haven't been here yet.
  812. (unless (member (file-truename dir) seen)
  813. (push (file-truename dir) seen)
  814. ;; We descend recursively
  815. (dolist (dir (directory-files dir t nil t))
  816. (when (and (not (string-match "^\\." (file-name-nondirectory dir)))
  817. (file-directory-p dir))
  818. (nnml-generate-nov-databases-directory dir seen)))
  819. ;; Do this directory.
  820. (let ((nnml-files (sort (nnheader-article-to-file-alist dir)
  821. 'car-less-than-car)))
  822. (if (not nnml-files)
  823. (let* ((group (nnheader-file-to-group
  824. (directory-file-name dir) nnml-directory))
  825. (info (cadr (assoc group nnml-group-alist))))
  826. (when info
  827. (setcar info (1+ (cdr info)))))
  828. (funcall nnml-generate-active-function dir)
  829. ;; Generate the nov file.
  830. (nnml-generate-nov-file dir nnml-files)
  831. (unless no-active
  832. (nnmail-save-active nnml-group-alist nnml-active-file)))))))
  833. (defun nnml-generate-active-info (dir)
  834. ;; Update the active info for this group.
  835. (let ((group (directory-file-name dir))
  836. entry last)
  837. (setq group (nnheader-file-to-group (nnml-encoded-group-name group)
  838. nnml-directory)
  839. entry (assoc group nnml-group-alist)
  840. last (or (caadr entry) 0)
  841. nnml-group-alist (delq entry nnml-group-alist))
  842. (push (list group
  843. (cons (or (caar nnml-files) (1+ last))
  844. (max last
  845. (or (caar (last nnml-files))
  846. 0))))
  847. nnml-group-alist)))
  848. (defun nnml-generate-nov-file (dir files)
  849. (let* ((dir (file-name-as-directory dir))
  850. (nov (concat dir nnml-nov-file-name))
  851. (nov-buffer (get-buffer-create " *nov*"))
  852. chars file headers)
  853. (with-current-buffer nov-buffer
  854. ;; Init the nov buffer.
  855. (buffer-disable-undo)
  856. (erase-buffer)
  857. (set-buffer nntp-server-buffer)
  858. ;; Delete the old NOV file.
  859. (when (file-exists-p nov)
  860. (funcall nnmail-delete-file-function nov))
  861. (dolist (file files)
  862. (let ((path (concat dir (cdr file))))
  863. (unless (file-directory-p path)
  864. (erase-buffer)
  865. (nnheader-insert-file-contents path)
  866. (narrow-to-region
  867. (goto-char (point-min))
  868. (progn
  869. (re-search-forward "\n\r?\n" nil t)
  870. (setq chars (- (point-max) (point)))
  871. (max (point-min) (1- (point)))))
  872. (unless (zerop (buffer-size))
  873. (goto-char (point-min))
  874. (setq headers (nnml-parse-head chars (car file)))
  875. (with-current-buffer nov-buffer
  876. (goto-char (point-max))
  877. (nnheader-insert-nov headers)))
  878. (widen))))
  879. (with-current-buffer nov-buffer
  880. (nnmail-write-region (point-min) (point-max) nov nil 'nomesg)
  881. (kill-buffer (current-buffer))))))
  882. (defun nnml-nov-delete-article (group article)
  883. (with-current-buffer (nnml-open-nov group)
  884. (when (nnheader-find-nov-line article)
  885. (delete-region (point) (progn (forward-line 1) (point)))
  886. (when (bobp)
  887. (let ((active (cadr (assoc group nnml-group-alist)))
  888. num)
  889. (when active
  890. (if (eobp)
  891. (setf (car active) (1+ (cdr active)))
  892. (when (and (setq num (ignore-errors (read (current-buffer))))
  893. (numberp num))
  894. (setf (car active) num)))))))
  895. t))
  896. (defun nnml-update-file-alist (&optional force)
  897. (when nnml-use-compressed-files
  898. (when (or (not nnml-article-file-alist)
  899. force)
  900. (setq nnml-article-file-alist
  901. (nnml-current-group-article-to-file-alist)))))
  902. (defun nnml-directory-articles (dir)
  903. "Return a list of all article files in a directory.
  904. Use the nov database for that directory if available."
  905. (if (or gnus-nov-is-evil nnml-nov-is-evil
  906. (not (file-exists-p
  907. (expand-file-name nnml-nov-file-name dir))))
  908. (nnheader-directory-articles dir)
  909. ;; build list from .overview if available
  910. ;; We would use nnml-open-nov, except that nnml-nov-buffer-alist is
  911. ;; defvoo'd, and we might get called when it hasn't been swapped in.
  912. (with-current-buffer (nnml-get-nov-buffer nnml-current-group)
  913. (let ((list nil)
  914. art)
  915. (goto-char (point-min))
  916. (while (not (eobp))
  917. (setq art (read (current-buffer)))
  918. (push art list)
  919. (forward-line 1))
  920. list))))
  921. (defun nnml-current-group-article-to-file-alist ()
  922. "Return an alist of article/file pairs in the current group.
  923. Use the nov database for the current group if available."
  924. (if (or nnml-use-compressed-files
  925. gnus-nov-is-evil
  926. nnml-nov-is-evil
  927. (not (file-exists-p
  928. (expand-file-name nnml-nov-file-name
  929. nnml-current-directory))))
  930. (nnheader-article-to-file-alist nnml-current-directory)
  931. ;; build list from .overview if available
  932. (with-current-buffer (nnml-get-nov-buffer nnml-current-group)
  933. (let ((alist nil)
  934. art)
  935. (goto-char (point-min))
  936. (while (not (eobp))
  937. (setq art (read (current-buffer)))
  938. ;; assume file name is unadorned (ie. not compressed etc)
  939. (push (cons art (int-to-string art)) alist)
  940. (forward-line 1))
  941. alist))))
  942. (deffoo nnml-request-set-mark (group actions &optional server)
  943. (nnml-possibly-change-directory group server)
  944. (unless nnml-marks-is-evil
  945. (nnml-open-marks group server)
  946. (setq nnml-marks (nnheader-update-marks-actions nnml-marks actions))
  947. (nnml-save-marks group server))
  948. nil)
  949. (deffoo nnml-request-marks (group info &optional server)
  950. (nnml-possibly-change-directory group server)
  951. (when (and (not nnml-marks-is-evil) (nnml-marks-changed-p group server))
  952. (nnheader-message 8 "Updating marks for %s..." group)
  953. (nnml-open-marks group server)
  954. ;; Update info using `nnml-marks'.
  955. (mapc (lambda (pred)
  956. (unless (memq (cdr pred) gnus-article-unpropagated-mark-lists)
  957. (gnus-info-set-marks
  958. info
  959. (gnus-update-alist-soft
  960. (cdr pred)
  961. (cdr (assq (cdr pred) nnml-marks))
  962. (gnus-info-marks info))
  963. t)))
  964. gnus-article-mark-lists)
  965. (let ((seen (cdr (assq 'read nnml-marks))))
  966. (gnus-info-set-read info
  967. (if (and (integerp (car seen))
  968. (null (cdr seen)))
  969. (list (cons (car seen) (car seen)))
  970. seen)))
  971. (nnheader-message 8 "Updating marks for %s...done" group))
  972. info)
  973. (defun nnml-marks-changed-p (group server)
  974. (let ((file (nnml-group-pathname group nnml-marks-file-name server)))
  975. (if (null (gnus-gethash file nnml-marks-modtime))
  976. t ;; never looked at marks file, assume it has changed
  977. (not (equal (gnus-gethash file nnml-marks-modtime)
  978. (nth 5 (file-attributes file)))))))
  979. (defun nnml-save-marks (group server)
  980. (let ((file-name-coding-system nnmail-pathname-coding-system)
  981. (file (nnml-group-pathname group nnml-marks-file-name server)))
  982. (condition-case err
  983. (progn
  984. (nnml-possibly-create-directory group server)
  985. (with-temp-file file
  986. (erase-buffer)
  987. (gnus-prin1 nnml-marks)
  988. (insert "\n"))
  989. (gnus-sethash file
  990. (nth 5 (file-attributes file))
  991. nnml-marks-modtime))
  992. (error (or (gnus-yes-or-no-p
  993. (format "Could not write to %s (%s). Continue? " file err))
  994. (error "Cannot write to %s (%s)" file err))))))
  995. (defun nnml-open-marks (group server)
  996. (let* ((decoded (nnml-decoded-group-name group server))
  997. (file (nnmail-group-pathname decoded nnml-directory
  998. nnml-marks-file-name))
  999. (file-name-coding-system nnmail-pathname-coding-system))
  1000. (if (file-exists-p file)
  1001. (condition-case err
  1002. (with-temp-buffer
  1003. (gnus-sethash file (nth 5 (file-attributes file))
  1004. nnml-marks-modtime)
  1005. (nnheader-insert-file-contents file)
  1006. (setq nnml-marks (read (current-buffer)))
  1007. (dolist (el gnus-article-unpropagated-mark-lists)
  1008. (setq nnml-marks (gnus-remassoc el nnml-marks))))
  1009. (error (or (gnus-yes-or-no-p
  1010. (format "Error reading nnml marks file %s (%s). Continuing will use marks from .newsrc.eld. Continue? " file err))
  1011. (error "Cannot read nnml marks file %s (%s)" file err))))
  1012. ;; User didn't have a .marks file. Probably first time
  1013. ;; user of the .marks stuff. Bootstrap it from .newsrc.eld.
  1014. (let ((info (gnus-get-info
  1015. (gnus-group-prefixed-name
  1016. group
  1017. (gnus-server-to-method
  1018. (format "nnml:%s" (or server "")))))))
  1019. (setq decoded (if (member server '(nil ""))
  1020. (concat "nnml:" decoded)
  1021. (format "nnml+%s:%s" server decoded)))
  1022. (nnheader-message 7 "Bootstrapping marks for %s..." decoded)
  1023. (setq nnml-marks (gnus-info-marks info))
  1024. (push (cons 'read (gnus-info-read info)) nnml-marks)
  1025. (dolist (el gnus-article-unpropagated-mark-lists)
  1026. (setq nnml-marks (gnus-remassoc el nnml-marks)))
  1027. (nnml-save-marks group server)
  1028. (nnheader-message 7 "Bootstrapping marks for %s...done" decoded)))))
  1029. ;;;
  1030. ;;; Group and server compaction. -- dvl
  1031. ;;;
  1032. ;; #### FIXME: this function handles self Xref: entry correctly, but I don't
  1033. ;; #### know how to handle external cross-references. I actually don't know if
  1034. ;; #### this is handled correctly elsewhere. For instance, what happens if you
  1035. ;; #### move all articles to a new group (that's what people do for manual
  1036. ;; #### compaction) ?
  1037. ;; #### NOTE: the function below handles the article backlog. This is
  1038. ;; #### conceptually the wrong place to do it because the backend is at a
  1039. ;; #### lower level. However, this is the only place where we have the needed
  1040. ;; #### information to do the job. Ideally, this function should not handle
  1041. ;; #### the backlog by itself, but return a list of moved groups / articles to
  1042. ;; #### the caller. This will become important to avoid code duplication when
  1043. ;; #### other backends get a compaction feature. Also, note that invalidating
  1044. ;; #### the "original article buffer" is already done at an upper level.
  1045. ;; Shouldn't `nnml-request-compact-group' be interactive? --rsteib
  1046. (defun nnml-request-compact-group (group &optional server save)
  1047. (nnml-possibly-change-directory group server)
  1048. (unless nnml-article-file-alist
  1049. (setq nnml-article-file-alist
  1050. (sort (nnml-current-group-article-to-file-alist)
  1051. 'car-less-than-car)))
  1052. (if (not nnml-article-file-alist)
  1053. ;; The group is empty: do nothing but return t
  1054. t
  1055. ;; The group is not empty:
  1056. (let* ((group-full-name
  1057. (gnus-group-prefixed-name
  1058. group
  1059. (gnus-server-to-method (format "nnml:%s" server))))
  1060. (info (gnus-get-info group-full-name))
  1061. (new-number 1)
  1062. compacted)
  1063. (let ((articles nnml-article-file-alist)
  1064. article)
  1065. (while (setq article (pop articles))
  1066. (let ((old-number (car article)))
  1067. (when (> old-number new-number)
  1068. ;; There is a gap here:
  1069. (let ((old-number-string (int-to-string old-number))
  1070. (new-number-string (int-to-string new-number)))
  1071. (setq compacted t)
  1072. ;; #### NOTE: `nnml-article-to-file' calls
  1073. ;; #### `nnml-update-file-alist' (which in turn calls
  1074. ;; #### `nnml-current-group-article-to-file-alist', which
  1075. ;; #### might use the NOV database). This might turn out to be
  1076. ;; #### inefficient. In that case, we will do the work
  1077. ;; #### manually.
  1078. ;; 1/ Move the article to a new file:
  1079. (let* ((oldfile (nnml-article-to-file old-number))
  1080. (newfile
  1081. (gnus-replace-in-string
  1082. oldfile
  1083. ;; nnml-use-compressed-files might be any string, but
  1084. ;; probably it's sufficient to take into account only
  1085. ;; "\\.[a-z0-9]+". Note that we can't only use the
  1086. ;; value of nnml-use-compressed-files because old
  1087. ;; articles might have been saved with a different
  1088. ;; value.
  1089. (concat
  1090. "\\(" old-number-string "\\)\\(\\(\\.[a-z0-9]+\\)?\\)$")
  1091. (concat new-number-string "\\2"))))
  1092. (with-current-buffer nntp-server-buffer
  1093. (nnmail-find-file oldfile)
  1094. ;; Update the Xref header in the article itself:
  1095. (when (and (re-search-forward "^Xref: [^ ]+ " nil t)
  1096. (re-search-forward
  1097. (concat "\\<"
  1098. (regexp-quote
  1099. (concat group ":" old-number-string))
  1100. "\\>")
  1101. (point-at-eol) t))
  1102. (replace-match
  1103. (concat group ":" new-number-string)))
  1104. ;; Save to the new file:
  1105. (nnmail-write-region (point-min) (point-max) newfile))
  1106. (funcall nnmail-delete-file-function oldfile))
  1107. ;; 2/ Update all marks for this article:
  1108. ;; #### NOTE: it is possible that the new article number
  1109. ;; #### already belongs to a range, whereas the corresponding
  1110. ;; #### article doesn't exist (for example, if you delete an
  1111. ;; #### article). For that reason, it is important to update
  1112. ;; #### the ranges (meaning remove nonexistent articles) before
  1113. ;; #### doing anything on them.
  1114. ;; 2 a/ read articles:
  1115. (let ((read (gnus-info-read info)))
  1116. (setq read (gnus-remove-from-range read (list new-number)))
  1117. (when (gnus-member-of-range old-number read)
  1118. (setq read (gnus-remove-from-range read (list old-number)))
  1119. (setq read (gnus-add-to-range read (list new-number))))
  1120. (gnus-info-set-read info read))
  1121. ;; 2 b/ marked articles:
  1122. (let ((oldmarks (gnus-info-marks info))
  1123. mark newmarks)
  1124. (while (setq mark (pop oldmarks))
  1125. (setcdr mark (gnus-remove-from-range (cdr mark)
  1126. (list new-number)))
  1127. (when (gnus-member-of-range old-number (cdr mark))
  1128. (setcdr mark (gnus-remove-from-range (cdr mark)
  1129. (list old-number)))
  1130. (setcdr mark (gnus-add-to-range (cdr mark)
  1131. (list new-number))))
  1132. (push mark newmarks))
  1133. (gnus-info-set-marks info newmarks))
  1134. ;; 3/ Update the NOV entry for this article:
  1135. (unless nnml-nov-is-evil
  1136. (with-current-buffer (nnml-open-nov group)
  1137. (when (nnheader-find-nov-line old-number)
  1138. ;; Replace the article number:
  1139. (looking-at old-number-string)
  1140. (replace-match new-number-string nil t)
  1141. ;; Update the Xref header:
  1142. (when (re-search-forward
  1143. (concat "\\(Xref:[^\t\n]* \\)\\<"
  1144. (regexp-quote
  1145. (concat group ":" old-number-string))
  1146. "\\>")
  1147. (point-at-eol) t)
  1148. (replace-match
  1149. (concat "\\1" group ":" new-number-string))))))
  1150. ;; 4/ Possibly remove the article from the backlog:
  1151. (when gnus-keep-backlog
  1152. ;; #### NOTE: instead of removing the article, we could
  1153. ;; #### modify the backlog to reflect the numbering change,
  1154. ;; #### but I don't think it's worth it.
  1155. (gnus-backlog-remove-article group-full-name old-number)
  1156. (gnus-backlog-remove-article group-full-name new-number))))
  1157. (setq new-number (1+ new-number)))))
  1158. (if (not compacted)
  1159. ;; No compaction had to be done:
  1160. t
  1161. ;; Some articles have actually been renamed:
  1162. ;; 1/ Rebuild active information:
  1163. (let ((entry (assoc group nnml-group-alist))
  1164. (active (cons 1 (1- new-number))))
  1165. (setq nnml-group-alist (delq entry nnml-group-alist))
  1166. (push (list group active) nnml-group-alist)
  1167. ;; Update the active hashtable to let the *Group* buffer display
  1168. ;; up-to-date lines. I don't think that either gnus-newsrc-hashtb or
  1169. ;; gnus-newwrc-alist are out of date, since all we did is to modify
  1170. ;; the info of the group internally.
  1171. (gnus-set-active group-full-name active))
  1172. ;; 1 bis/
  1173. ;; #### NOTE: normally, we should save the overview (NOV) file
  1174. ;; #### here, just like we save the marks file. However, there is no
  1175. ;; #### such function as nnml-save-nov for a single group. Only for
  1176. ;; #### all groups. Gnus inconsistency is getting worse every day...
  1177. ;; 2/ Rebuild marks file:
  1178. (unless nnml-marks-is-evil
  1179. ;; #### NOTE: this constant use of global variables everywhere is
  1180. ;; #### truly disgusting. Gnus really needs a *major* cleanup.
  1181. (setq nnml-marks (gnus-info-marks info))
  1182. (push (cons 'read (gnus-info-read info)) nnml-marks)
  1183. (dolist (el gnus-article-unpropagated-mark-lists)
  1184. (setq nnml-marks (gnus-remassoc el nnml-marks)))
  1185. (nnml-save-marks group server))
  1186. ;; 3/ Save everything if this was not part of a bigger operation:
  1187. (if (not save)
  1188. ;; Nothing to save (yet):
  1189. t
  1190. ;; Something to save:
  1191. ;; a/ Save the NOV databases:
  1192. ;; #### NOTE: this should be done directory per directory in 1bis
  1193. ;; #### above. See comment there.
  1194. (nnml-save-nov)
  1195. ;; b/ Save the active file:
  1196. (nnmail-save-active nnml-group-alist nnml-active-file)
  1197. (let ((marks (nnml-group-pathname group nnml-marks-file-name server)))
  1198. (when (file-exists-p marks)
  1199. (delete-file marks)))
  1200. t)))))
  1201. (defun nnml-request-compact (&optional server)
  1202. "Request compaction of all SERVER nnml groups."
  1203. (interactive (list (or (nnoo-current-server 'nnml) "")))
  1204. (nnmail-activate 'nnml)
  1205. (unless (nnml-server-opened server)
  1206. (nnml-open-server server))
  1207. (setq nnml-directory (expand-file-name nnml-directory))
  1208. (let* ((groups (gnus-groups-from-server
  1209. (gnus-server-to-method (format "nnml:%s" server))))
  1210. (first (pop groups))
  1211. group)
  1212. (when first
  1213. (while (setq group (pop groups))
  1214. (nnml-request-compact-group (gnus-group-real-name group) server))
  1215. (nnml-request-compact-group (gnus-group-real-name first) server t))))
  1216. (provide 'nnml)
  1217. ;;; nnml.el ends here