nnml.el 44 KB

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