nnmbox.el 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  1. ;;; nnmbox.el --- mail mbox access for Gnus
  2. ;; Copyright (C) 1995-2012 Free Software Foundation, Inc.
  3. ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
  4. ;; Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
  5. ;; Keywords: news, mail
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; For an overview of what the interface functions do, please see the
  19. ;; Gnus sources.
  20. ;;; Code:
  21. (require 'nnheader)
  22. (require 'message)
  23. (require 'nnmail)
  24. (require 'nnoo)
  25. (require 'gnus-range)
  26. (eval-when-compile (require 'cl))
  27. (nnoo-declare nnmbox)
  28. (defvoo nnmbox-mbox-file (expand-file-name "~/mbox")
  29. "The name of the mail box file in the user's home directory.")
  30. (defvoo nnmbox-active-file (expand-file-name "~/.mbox-active")
  31. "The name of the active file for the mail box.")
  32. (defvoo nnmbox-get-new-mail t
  33. "If non-nil, nnmbox will check the incoming mail file and split the mail.")
  34. (defvoo nnmbox-prepare-save-mail-hook nil
  35. "Hook run narrowed to an article before saving.")
  36. (defconst nnmbox-version "nnmbox 1.0"
  37. "nnmbox version.")
  38. (defvoo nnmbox-current-group nil
  39. "Current nnmbox news group directory.")
  40. (defvar nnmbox-mbox-buffer nil)
  41. (defvoo nnmbox-status-string "")
  42. (defvoo nnmbox-group-alist nil)
  43. (defvoo nnmbox-active-timestamp nil)
  44. (defvoo nnmbox-file-coding-system mm-binary-coding-system)
  45. (defvoo nnmbox-file-coding-system-for-write nil)
  46. (defvoo nnmbox-active-file-coding-system mm-binary-coding-system)
  47. (defvoo nnmbox-active-file-coding-system-for-write nil)
  48. (defvar nnmbox-group-building-active-articles nil)
  49. (defvar nnmbox-group-active-articles nil)
  50. ;;; Interface functions
  51. (nnoo-define-basics nnmbox)
  52. (deffoo nnmbox-retrieve-headers (sequence &optional newsgroup server fetch-old)
  53. (with-current-buffer nntp-server-buffer
  54. (erase-buffer)
  55. (let ((number (length sequence))
  56. (count 0)
  57. article start stop)
  58. (nnmbox-possibly-change-newsgroup newsgroup server)
  59. (while sequence
  60. (setq article (car sequence))
  61. (set-buffer nnmbox-mbox-buffer)
  62. (when (nnmbox-find-article article)
  63. (setq start
  64. (save-excursion
  65. (re-search-backward
  66. (concat "^" message-unix-mail-delimiter) nil t)
  67. (point)))
  68. (search-forward "\n\n" nil t)
  69. (setq stop (1- (point)))
  70. (set-buffer nntp-server-buffer)
  71. (insert (format "221 %d Article retrieved.\n" article))
  72. (insert-buffer-substring nnmbox-mbox-buffer start stop)
  73. (goto-char (point-max))
  74. (insert ".\n"))
  75. (setq sequence (cdr sequence))
  76. (setq count (1+ count))
  77. (and (numberp nnmail-large-newsgroup)
  78. (> number nnmail-large-newsgroup)
  79. (zerop (% count 20))
  80. (nnheader-message 5 "nnmbox: Receiving headers... %d%%"
  81. (/ (* count 100) number))))
  82. (and (numberp nnmail-large-newsgroup)
  83. (> number nnmail-large-newsgroup)
  84. (nnheader-message 5 "nnmbox: Receiving headers...done"))
  85. (set-buffer nntp-server-buffer)
  86. (nnheader-fold-continuation-lines)
  87. 'headers)))
  88. (deffoo nnmbox-open-server (server &optional defs)
  89. (nnoo-change-server 'nnmbox server defs)
  90. (nnmbox-create-mbox)
  91. (cond
  92. ((not (file-exists-p nnmbox-mbox-file))
  93. (nnmbox-close-server)
  94. (nnheader-report 'nnmbox "No such file: %s" nnmbox-mbox-file))
  95. ((file-directory-p nnmbox-mbox-file)
  96. (nnmbox-close-server)
  97. (nnheader-report 'nnmbox "Not a regular file: %s" nnmbox-mbox-file))
  98. (t
  99. (nnheader-report 'nnmbox "Opened server %s using mbox %s" server
  100. nnmbox-mbox-file)
  101. t)))
  102. (deffoo nnmbox-close-server (&optional server)
  103. (when (and nnmbox-mbox-buffer
  104. (buffer-name nnmbox-mbox-buffer))
  105. (kill-buffer nnmbox-mbox-buffer))
  106. (nnoo-close-server 'nnmbox server)
  107. t)
  108. (deffoo nnmbox-server-opened (&optional server)
  109. (and (nnoo-current-server-p 'nnmbox server)
  110. nnmbox-mbox-buffer
  111. (buffer-name nnmbox-mbox-buffer)
  112. nntp-server-buffer
  113. (buffer-name nntp-server-buffer)))
  114. (deffoo nnmbox-request-article (article &optional newsgroup server buffer)
  115. (nnmbox-possibly-change-newsgroup newsgroup server)
  116. (with-current-buffer nnmbox-mbox-buffer
  117. (when (nnmbox-find-article article)
  118. (let (start stop)
  119. (re-search-backward (concat "^" message-unix-mail-delimiter) nil t)
  120. (setq start (point))
  121. (forward-line 1)
  122. (setq stop (if (re-search-forward (concat "^"
  123. message-unix-mail-delimiter)
  124. nil 'move)
  125. (match-beginning 0)
  126. (point)))
  127. (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
  128. (set-buffer nntp-server-buffer)
  129. (erase-buffer)
  130. (insert-buffer-substring nnmbox-mbox-buffer start stop)
  131. (goto-char (point-min))
  132. (while (looking-at "From ")
  133. (delete-char 5)
  134. (insert "X-From-Line: ")
  135. (forward-line 1))
  136. (if (numberp article)
  137. (cons nnmbox-current-group article)
  138. (nnmbox-article-group-number nil)))))))
  139. (deffoo nnmbox-request-group (group &optional server dont-check info)
  140. (nnmbox-possibly-change-newsgroup nil server)
  141. (let ((active (cadr (assoc group nnmbox-group-alist))))
  142. (cond
  143. ((or (null active)
  144. (null (nnmbox-possibly-change-newsgroup group server)))
  145. (nnheader-report 'nnmbox "No such group: %s" group))
  146. (dont-check
  147. (nnheader-report 'nnmbox "Selected group %s" group)
  148. (nnheader-insert ""))
  149. (t
  150. (nnheader-report 'nnmbox "Selected group %s" group)
  151. (nnheader-insert "211 %d %d %d %s\n"
  152. (1+ (- (cdr active) (car active)))
  153. (car active) (cdr active) group)))))
  154. (defun nnmbox-save-buffer ()
  155. (let ((coding-system-for-write
  156. (or nnmbox-file-coding-system-for-write
  157. nnmbox-file-coding-system)))
  158. (save-buffer)))
  159. (defun nnmbox-save-active (group-alist active-file)
  160. (let ((nnmail-active-file-coding-system
  161. (or nnmbox-active-file-coding-system-for-write
  162. nnmbox-active-file-coding-system)))
  163. (nnmail-save-active group-alist active-file)))
  164. (deffoo nnmbox-request-scan (&optional group server)
  165. (nnmbox-possibly-change-newsgroup group server)
  166. (nnmbox-read-mbox)
  167. (nnmail-get-new-mail
  168. 'nnmbox
  169. (lambda ()
  170. (with-current-buffer nnmbox-mbox-buffer
  171. (nnmbox-save-buffer)))
  172. (file-name-directory nnmbox-mbox-file)
  173. group
  174. (lambda ()
  175. (save-excursion
  176. (let ((in-buf (current-buffer)))
  177. (set-buffer nnmbox-mbox-buffer)
  178. (goto-char (point-max))
  179. (insert-buffer-substring in-buf)))
  180. (nnmbox-save-active nnmbox-group-alist nnmbox-active-file))))
  181. (deffoo nnmbox-close-group (group &optional server)
  182. t)
  183. (deffoo nnmbox-request-create-group (group &optional server args)
  184. (nnmail-activate 'nnmbox)
  185. (unless (assoc group nnmbox-group-alist)
  186. (push (list group (cons 1 0))
  187. nnmbox-group-alist)
  188. (nnmbox-save-active nnmbox-group-alist nnmbox-active-file))
  189. t)
  190. (deffoo nnmbox-request-list (&optional server)
  191. (save-excursion
  192. (let ((nnmail-file-coding-system
  193. nnmbox-active-file-coding-system))
  194. (nnmail-find-file nnmbox-active-file))
  195. (setq nnmbox-group-alist (nnmail-get-active))
  196. t))
  197. (deffoo nnmbox-request-newgroups (date &optional server)
  198. (nnmbox-request-list server))
  199. (deffoo nnmbox-request-list-newsgroups (&optional server)
  200. (nnheader-report 'nnmbox "LIST NEWSGROUPS is not implemented."))
  201. (deffoo nnmbox-request-expire-articles
  202. (articles newsgroup &optional server force)
  203. (nnmbox-possibly-change-newsgroup newsgroup server)
  204. (let* ((is-old t)
  205. rest)
  206. (nnmail-activate 'nnmbox)
  207. (with-current-buffer nnmbox-mbox-buffer
  208. (while (and articles is-old)
  209. (when (nnmbox-find-article (car articles))
  210. (if (setq is-old
  211. (nnmail-expired-article-p
  212. newsgroup
  213. (buffer-substring
  214. (point) (progn (end-of-line) (point))) force))
  215. (progn
  216. (unless (eq nnmail-expiry-target 'delete)
  217. (with-temp-buffer
  218. (nnmbox-request-article (car articles)
  219. newsgroup server
  220. (current-buffer))
  221. (let ((nnml-current-directory nil))
  222. (nnmail-expiry-target-group
  223. nnmail-expiry-target newsgroup)))
  224. (nnmbox-possibly-change-newsgroup newsgroup server))
  225. (nnheader-message 5 "Deleting article %d in %s..."
  226. (car articles) newsgroup)
  227. (nnmbox-delete-mail))
  228. (push (car articles) rest)))
  229. (setq articles (cdr articles)))
  230. (nnmbox-save-buffer)
  231. ;; Find the lowest active article in this group.
  232. (let ((active (nth 1 (assoc newsgroup nnmbox-group-alist))))
  233. (while (and (not (nnmbox-find-article (car active)))
  234. (<= (car active) (cdr active)))
  235. (setcar active (1+ (car active)))))
  236. (nnmbox-save-active nnmbox-group-alist nnmbox-active-file)
  237. (nconc rest articles))))
  238. (deffoo nnmbox-request-move-article
  239. (article group server accept-form &optional last move-is-internal)
  240. (let ((buf (get-buffer-create " *nnmbox move*"))
  241. result)
  242. (and
  243. (nnmbox-request-article article group server)
  244. (with-current-buffer buf
  245. (erase-buffer)
  246. (insert-buffer-substring nntp-server-buffer)
  247. (goto-char (point-min))
  248. (while (re-search-forward
  249. "^X-Gnus-Newsgroup:"
  250. (save-excursion (search-forward "\n\n" nil t) (point)) t)
  251. (gnus-delete-line))
  252. (setq result (eval accept-form))
  253. (kill-buffer buf)
  254. result)
  255. (save-excursion
  256. (nnmbox-possibly-change-newsgroup group server)
  257. (set-buffer nnmbox-mbox-buffer)
  258. (when (nnmbox-find-article article)
  259. (nnmbox-delete-mail))
  260. (and last (nnmbox-save-buffer))))
  261. result))
  262. (deffoo nnmbox-request-accept-article (group &optional server last)
  263. (nnmbox-possibly-change-newsgroup group server)
  264. (nnmail-check-syntax)
  265. (let ((buf (current-buffer))
  266. result cont)
  267. (and
  268. (nnmail-activate 'nnmbox)
  269. (with-temp-buffer
  270. (insert-buffer-substring buf)
  271. (goto-char (point-min))
  272. (cond (;; The From line may have been quoted by movemail.
  273. (looking-at (concat ">" message-unix-mail-delimiter))
  274. (delete-char 1)
  275. (forward-line 1))
  276. ((looking-at "X-From-Line: ")
  277. (replace-match "From ")
  278. (forward-line 1))
  279. (t
  280. (insert "From nobody " (current-time-string) "\n")))
  281. (narrow-to-region (point)
  282. (if (search-forward "\n\n" nil 'move)
  283. (1- (point))
  284. (point)))
  285. (while (re-search-backward "^X-Gnus-Newsgroup: " nil t)
  286. (delete-region (point) (progn (forward-line 1) (point))))
  287. (when nnmail-cache-accepted-message-ids
  288. (nnmail-cache-insert (message-fetch-field "message-id")
  289. group
  290. (message-fetch-field "subject")
  291. (message-fetch-field "from")))
  292. (widen)
  293. (setq result (if (stringp group)
  294. (list (cons group (nnmbox-active-number group)))
  295. (nnmail-article-group 'nnmbox-active-number)))
  296. (prog1
  297. (if (and (null result)
  298. (yes-or-no-p "Moved to `junk' group; delete article? "))
  299. (setq result 'junk)
  300. (setq result (car (nnmbox-save-mail result))))
  301. (setq cont (buffer-string))))
  302. (with-current-buffer nnmbox-mbox-buffer
  303. (goto-char (point-max))
  304. (insert cont)
  305. (when last
  306. (when nnmail-cache-accepted-message-ids
  307. (nnmail-cache-close))
  308. (nnmbox-save-active nnmbox-group-alist nnmbox-active-file)
  309. (nnmbox-save-buffer))))
  310. result))
  311. (deffoo nnmbox-request-replace-article (article group buffer)
  312. (nnmbox-possibly-change-newsgroup group)
  313. (with-current-buffer nnmbox-mbox-buffer
  314. (if (not (nnmbox-find-article article))
  315. nil
  316. (nnmbox-delete-mail t t)
  317. (insert
  318. (with-temp-buffer
  319. (insert-buffer-substring buffer)
  320. (goto-char (point-min))
  321. (when (looking-at "X-From-Line:")
  322. (delete-region (point) (progn (forward-line 1) (point))))
  323. (while (re-search-forward (concat "^" message-unix-mail-delimiter)
  324. nil t)
  325. (goto-char (match-beginning 0))
  326. (insert ">"))
  327. (goto-char (point-max))
  328. (unless (bolp)
  329. (insert "\n"))
  330. (buffer-string)))
  331. (nnmbox-save-buffer)
  332. t)))
  333. (deffoo nnmbox-request-delete-group (group &optional force server)
  334. (nnmbox-possibly-change-newsgroup group server)
  335. ;; Delete all articles in GROUP.
  336. (if (not force)
  337. () ; Don't delete the articles.
  338. (with-current-buffer nnmbox-mbox-buffer
  339. (goto-char (point-min))
  340. ;; Delete all articles in this group.
  341. (let ((ident (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":"))
  342. found)
  343. (while (search-forward ident nil t)
  344. (setq found t)
  345. (nnmbox-delete-mail))
  346. (when found
  347. (nnmbox-save-buffer)))))
  348. ;; Remove the group from all structures.
  349. (setq nnmbox-group-alist
  350. (delq (assoc group nnmbox-group-alist) nnmbox-group-alist)
  351. nnmbox-current-group nil)
  352. ;; Save the active file.
  353. (nnmbox-save-active nnmbox-group-alist nnmbox-active-file)
  354. t)
  355. (deffoo nnmbox-request-rename-group (group new-name &optional server)
  356. (nnmbox-possibly-change-newsgroup group server)
  357. (with-current-buffer nnmbox-mbox-buffer
  358. (goto-char (point-min))
  359. (let ((ident (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":"))
  360. (new-ident (concat "\nX-Gnus-Newsgroup: " new-name ":"))
  361. found)
  362. (while (search-forward ident nil t)
  363. (replace-match new-ident t t)
  364. (setq found t))
  365. (when found
  366. (nnmbox-save-buffer))))
  367. (let ((entry (assoc group nnmbox-group-active-articles)))
  368. (when entry
  369. (setcar entry new-name)))
  370. (let ((entry (assoc group nnmbox-group-alist)))
  371. (when entry
  372. (setcar entry new-name))
  373. (setq nnmbox-current-group nil)
  374. ;; Save the new group alist.
  375. (nnmbox-save-active nnmbox-group-alist nnmbox-active-file)
  376. t))
  377. ;;; Internal functions.
  378. ;; If FORCE, delete article no matter how many X-Gnus-Newsgroup
  379. ;; headers there are. If LEAVE-DELIM, don't delete the Unix mbox
  380. ;; delimiter line.
  381. (defun nnmbox-delete-mail (&optional force leave-delim)
  382. ;; Delete the current X-Gnus-Newsgroup line.
  383. ;; First delete record of active article, unless the article is being
  384. ;; replaced, indicated by FORCE being non-nil.
  385. (if (not force)
  386. (nnmbox-record-deleted-article (nnmbox-article-group-number t)))
  387. (or force
  388. (gnus-delete-line))
  389. ;; Beginning of the article.
  390. (save-excursion
  391. (save-restriction
  392. (narrow-to-region
  393. (prog2
  394. (re-search-backward (concat "^" message-unix-mail-delimiter) nil t)
  395. (if leave-delim (progn (forward-line 1) (point))
  396. (match-beginning 0))
  397. (forward-line 1))
  398. (or (and (re-search-forward (concat "^" message-unix-mail-delimiter)
  399. nil t)
  400. (match-beginning 0))
  401. (point-max)))
  402. (goto-char (point-min))
  403. ;; Only delete the article if no other group owns it as well.
  404. (when (or force
  405. (not (re-search-forward "^X-Gnus-Newsgroup: " nil t))
  406. (search-backward "\n\n" nil t))
  407. (delete-region (point-min) (point-max))))))
  408. (defun nnmbox-possibly-change-newsgroup (newsgroup &optional server)
  409. (when (and server
  410. (not (nnmbox-server-opened server)))
  411. (nnmbox-open-server server))
  412. (when (or (not nnmbox-mbox-buffer)
  413. (not (buffer-name nnmbox-mbox-buffer)))
  414. (nnmbox-read-mbox))
  415. (when (not nnmbox-group-alist)
  416. (nnmail-activate 'nnmbox))
  417. (if newsgroup
  418. (when (assoc newsgroup nnmbox-group-alist)
  419. (setq nnmbox-current-group newsgroup))
  420. t))
  421. (defun nnmbox-article-string (article)
  422. (if (numberp article)
  423. (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":"
  424. (int-to-string article) " ")
  425. (concat "\nMessage-ID: " article)))
  426. (defun nnmbox-article-group-number (this-line)
  427. (save-excursion
  428. (if this-line
  429. (beginning-of-line)
  430. (goto-char (point-min)))
  431. (when (re-search-forward "^X-Gnus-Newsgroup: +\\([^:]+\\):\\([0-9]+\\) "
  432. nil t)
  433. (cons (buffer-substring (match-beginning 1) (match-end 1))
  434. (string-to-number
  435. (buffer-substring (match-beginning 2) (match-end 2)))))))
  436. (defun nnmbox-in-header-p (pos)
  437. "Return non-nil if POS is in the header of an article."
  438. (save-excursion
  439. (goto-char pos)
  440. (re-search-backward (concat "^" message-unix-mail-delimiter) nil t)
  441. (search-forward "\n\n" nil t)
  442. (< pos (point))))
  443. (defun nnmbox-find-article (article)
  444. "Leaves point on the relevant X-Gnus-Newsgroup line if found."
  445. ;; Check that article is in the active range first, to avoid an
  446. ;; expensive exhaustive search if it isn't.
  447. (if (and (numberp article)
  448. (not (nnmbox-is-article-active-p article)))
  449. nil
  450. (let ((art-string (nnmbox-article-string article))
  451. (found nil))
  452. ;; There is the possibility that the X-Gnus-Newsgroup line appears
  453. ;; in the body of an article (for instance, if an article has been
  454. ;; forwarded from someone using Gnus as their mailer), so check
  455. ;; that the line is actually part of the article header.
  456. (or (and (search-forward art-string nil t)
  457. (nnmbox-in-header-p (point)))
  458. (progn
  459. (goto-char (point-min))
  460. (while (and (not found)
  461. (search-forward art-string nil t))
  462. (setq found (nnmbox-in-header-p (point))))
  463. found)))))
  464. (defun nnmbox-record-active-article (group-art)
  465. (let* ((group (car group-art))
  466. (article (cdr group-art))
  467. (entry
  468. (or (assoc group nnmbox-group-active-articles)
  469. (progn
  470. (push (list group)
  471. nnmbox-group-active-articles)
  472. (car nnmbox-group-active-articles)))))
  473. ;; add article to index, either by building complete list
  474. ;; in reverse order, or as a list of ranges.
  475. (if (not nnmbox-group-building-active-articles)
  476. (setcdr entry (gnus-add-to-range (cdr entry) (list article)))
  477. (when (memq article (cdr entry))
  478. (switch-to-buffer nnmbox-mbox-buffer)
  479. (error "Article %s:%d already exists!" group article))
  480. (when (and (cadr entry) (< article (cadr entry)))
  481. (switch-to-buffer nnmbox-mbox-buffer)
  482. (error "Article %s:%d out of order" group article))
  483. (setcdr entry (cons article (cdr entry))))))
  484. (defun nnmbox-record-deleted-article (group-art)
  485. (let* ((group (car group-art))
  486. (article (cdr group-art))
  487. (entry
  488. (or (assoc group nnmbox-group-active-articles)
  489. (progn
  490. (push (list group)
  491. nnmbox-group-active-articles)
  492. (car nnmbox-group-active-articles)))))
  493. ;; remove article from index
  494. (setcdr entry (gnus-remove-from-range (cdr entry) (list article)))))
  495. (defun nnmbox-is-article-active-p (article)
  496. (gnus-member-of-range
  497. article
  498. (cdr (assoc nnmbox-current-group
  499. nnmbox-group-active-articles))))
  500. (defun nnmbox-save-mail (group-art)
  501. "Called narrowed to an article."
  502. (let ((delim (concat "^" message-unix-mail-delimiter)))
  503. (goto-char (point-min))
  504. ;; This might come from somewhere else.
  505. (if (looking-at delim)
  506. (forward-line 1)
  507. (insert "From nobody " (current-time-string) "\n"))
  508. ;; Quote all "From " lines in the article.
  509. (while (re-search-forward delim nil t)
  510. (goto-char (match-beginning 0))
  511. (insert ">")))
  512. (goto-char (point-max))
  513. (unless (bolp)
  514. (insert "\n"))
  515. (nnmail-insert-lines)
  516. (nnmail-insert-xref group-art)
  517. (nnmbox-insert-newsgroup-line group-art)
  518. (let ((alist group-art))
  519. (while alist
  520. (nnmbox-record-active-article (car alist))
  521. (setq alist (cdr alist))))
  522. (run-hooks 'nnmail-prepare-save-mail-hook)
  523. (run-hooks 'nnmbox-prepare-save-mail-hook)
  524. group-art)
  525. (defun nnmbox-insert-newsgroup-line (group-art)
  526. (save-excursion
  527. (goto-char (point-min))
  528. (when (search-forward "\n\n" nil t)
  529. (forward-char -1)
  530. (while group-art
  531. (insert (format "X-Gnus-Newsgroup: %s:%d %s\n"
  532. (caar group-art) (cdar group-art)
  533. (current-time-string)))
  534. (setq group-art (cdr group-art))))
  535. t))
  536. (defun nnmbox-active-number (group)
  537. ;; Find the next article number in GROUP.
  538. (let ((active (cadr (assoc group nnmbox-group-alist))))
  539. (if active
  540. (setcdr active (1+ (cdr active)))
  541. ;; This group is new, so we create a new entry for it.
  542. ;; This might be a bit naughty... creating groups on the drop of
  543. ;; a hat, but I don't know...
  544. (push (list group (setq active (cons 1 1)))
  545. nnmbox-group-alist))
  546. (cdr active)))
  547. (defun nnmbox-create-mbox ()
  548. (when (not (file-exists-p nnmbox-mbox-file))
  549. (let ((nnmail-file-coding-system
  550. (or nnmbox-file-coding-system-for-write
  551. nnmbox-file-coding-system))
  552. (dir (file-name-directory nnmbox-mbox-file)))
  553. (and dir (gnus-make-directory dir))
  554. (nnmail-write-region (point-min) (point-min)
  555. nnmbox-mbox-file t 'nomesg))))
  556. (defun nnmbox-read-mbox ()
  557. (nnmail-activate 'nnmbox)
  558. (nnmbox-create-mbox)
  559. (if (and nnmbox-mbox-buffer
  560. (buffer-name nnmbox-mbox-buffer)
  561. (with-current-buffer nnmbox-mbox-buffer
  562. (= (buffer-size) (nnheader-file-size nnmbox-mbox-file))))
  563. ()
  564. (save-excursion
  565. (let ((delim (concat "^" message-unix-mail-delimiter))
  566. (alist nnmbox-group-alist)
  567. (nnmbox-group-building-active-articles t)
  568. start end end-header number)
  569. (set-buffer (setq nnmbox-mbox-buffer
  570. (let ((nnheader-file-coding-system
  571. nnmbox-file-coding-system))
  572. (nnheader-find-file-noselect
  573. nnmbox-mbox-file t t))))
  574. (mm-enable-multibyte)
  575. (buffer-disable-undo)
  576. (gnus-add-buffer)
  577. ;; Go through the group alist and compare against the mbox file.
  578. (while alist
  579. (goto-char (point-max))
  580. (when (and (re-search-backward
  581. (format "^X-Gnus-Newsgroup: %s:\\([0-9]+\\) "
  582. (caar alist)) nil t)
  583. (> (setq number
  584. (string-to-number
  585. (buffer-substring
  586. (match-beginning 1) (match-end 1))))
  587. (cdadar alist)))
  588. (setcdr (cadar alist) number))
  589. (setq alist (cdr alist)))
  590. ;; Examine all articles for our private X-Gnus-Newsgroup
  591. ;; headers. This is done primarily as a consistency check, but
  592. ;; it is convenient for building an index of the articles
  593. ;; present, to avoid costly searches for missing articles
  594. ;; (eg. when expiring articles).
  595. (goto-char (point-min))
  596. (setq nnmbox-group-active-articles nil)
  597. (while (re-search-forward delim nil t)
  598. (setq start (match-beginning 0))
  599. (save-excursion
  600. (search-forward "\n\n" nil t)
  601. (setq end-header (point))
  602. (setq end (or (and
  603. (re-search-forward delim nil t)
  604. (match-beginning 0))
  605. (point-max))))
  606. (if (search-forward "\nX-Gnus-Newsgroup: " end-header t)
  607. ;; Build a list of articles in each group, remembering
  608. ;; that each article may be in more than one group.
  609. (progn
  610. (nnmbox-record-active-article (nnmbox-article-group-number t))
  611. (while (search-forward "\nX-Gnus-Newsgroup: " end-header t)
  612. (nnmbox-record-active-article (nnmbox-article-group-number t))))
  613. ;; The article is either new, or for some other reason
  614. ;; hasn't got our private headers, so add them now. The
  615. ;; only situation I've encountered when the X-Gnus-Newsgroup
  616. ;; header is missing is if the article contains a forwarded
  617. ;; message which does contain that header line (earlier
  618. ;; versions of Gnus didn't restrict their search to the
  619. ;; headers). In this case, there is an Xref line which
  620. ;; provides the relevant information to construct the
  621. ;; missing header(s).
  622. (save-excursion
  623. (save-restriction
  624. (narrow-to-region start end)
  625. (if (re-search-forward "\nXref: [^ ]+" end-header t)
  626. ;; generate headers from Xref:
  627. (let (alist)
  628. (while (re-search-forward " \\([^:]+\\):\\([0-9]+\\)" end-header t)
  629. (push (cons (match-string 1)
  630. (string-to-number (match-string 2))) alist))
  631. (nnmbox-insert-newsgroup-line alist))
  632. ;; this is really a new article
  633. (nnmbox-save-mail
  634. (nnmail-article-group 'nnmbox-active-number))))))
  635. (goto-char end))
  636. ;; put article lists in order
  637. (setq alist nnmbox-group-active-articles)
  638. (while alist
  639. (setcdr (car alist) (gnus-compress-sequence (nreverse (cdar alist))))
  640. (setq alist (cdr alist)))))))
  641. (provide 'nnmbox)
  642. ;;; nnmbox.el ends here