nnmbox.el 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  1. ;;; nnmbox.el --- mail mbox access for Gnus
  2. ;; Copyright (C) 1995-2015 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. (floor (* count 100.0) 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. (save-excursion
  118. (when (nnmbox-find-article article)
  119. (let (start stop)
  120. (re-search-backward (concat "^" message-unix-mail-delimiter) nil t)
  121. (setq start (point))
  122. (forward-line 1)
  123. (setq stop (if (re-search-forward (concat "^"
  124. message-unix-mail-delimiter)
  125. nil 'move)
  126. (match-beginning 0)
  127. (point)))
  128. (let ((nntp-server-buffer (or buffer nntp-server-buffer)))
  129. (set-buffer nntp-server-buffer)
  130. (erase-buffer)
  131. (insert-buffer-substring nnmbox-mbox-buffer start stop)
  132. (goto-char (point-min))
  133. (while (looking-at "From ")
  134. (delete-char 5)
  135. (insert "X-From-Line: ")
  136. (forward-line 1))
  137. (if (numberp article)
  138. (cons nnmbox-current-group article)
  139. (nnmbox-article-group-number nil))))))))
  140. (deffoo nnmbox-request-group (group &optional server dont-check info)
  141. (nnmbox-possibly-change-newsgroup nil server)
  142. (let ((active (cadr (assoc group nnmbox-group-alist))))
  143. (cond
  144. ((or (null active)
  145. (null (nnmbox-possibly-change-newsgroup group server)))
  146. (nnheader-report 'nnmbox "No such group: %s" group))
  147. (dont-check
  148. (nnheader-report 'nnmbox "Selected group %s" group)
  149. (nnheader-insert ""))
  150. (t
  151. (nnheader-report 'nnmbox "Selected group %s" group)
  152. (nnheader-insert "211 %d %d %d %s\n"
  153. (1+ (- (cdr active) (car active)))
  154. (car active) (cdr active) group)))))
  155. (defun nnmbox-save-buffer ()
  156. (let ((coding-system-for-write
  157. (or nnmbox-file-coding-system-for-write
  158. nnmbox-file-coding-system)))
  159. (save-buffer)))
  160. (defun nnmbox-save-active (group-alist active-file)
  161. (let ((nnmail-active-file-coding-system
  162. (or nnmbox-active-file-coding-system-for-write
  163. nnmbox-active-file-coding-system)))
  164. (nnmail-save-active group-alist active-file)))
  165. (deffoo nnmbox-request-scan (&optional group server)
  166. (nnmbox-possibly-change-newsgroup group server)
  167. (nnmbox-read-mbox)
  168. (nnmail-get-new-mail
  169. 'nnmbox
  170. (lambda ()
  171. (with-current-buffer nnmbox-mbox-buffer
  172. (nnmbox-save-buffer)))
  173. (file-name-directory nnmbox-mbox-file)
  174. group
  175. (lambda ()
  176. (save-excursion
  177. (let ((in-buf (current-buffer)))
  178. (set-buffer nnmbox-mbox-buffer)
  179. (goto-char (point-max))
  180. (insert-buffer-substring in-buf)))
  181. (nnmbox-save-active nnmbox-group-alist nnmbox-active-file))))
  182. (deffoo nnmbox-close-group (group &optional server)
  183. t)
  184. (deffoo nnmbox-request-create-group (group &optional server args)
  185. (nnmail-activate 'nnmbox)
  186. (unless (assoc group nnmbox-group-alist)
  187. (push (list group (cons 1 0))
  188. nnmbox-group-alist)
  189. (nnmbox-save-active nnmbox-group-alist nnmbox-active-file))
  190. t)
  191. (deffoo nnmbox-request-list (&optional server)
  192. (save-excursion
  193. (let ((nnmail-file-coding-system
  194. nnmbox-active-file-coding-system))
  195. (nnmail-find-file nnmbox-active-file))
  196. (setq nnmbox-group-alist (nnmail-get-active))
  197. t))
  198. (deffoo nnmbox-request-newgroups (date &optional server)
  199. (nnmbox-request-list server))
  200. (deffoo nnmbox-request-list-newsgroups (&optional server)
  201. (nnheader-report 'nnmbox "LIST NEWSGROUPS is not implemented."))
  202. (deffoo nnmbox-request-expire-articles
  203. (articles newsgroup &optional server force)
  204. (nnmbox-possibly-change-newsgroup newsgroup server)
  205. (let* ((is-old t)
  206. rest)
  207. (nnmail-activate 'nnmbox)
  208. (with-current-buffer nnmbox-mbox-buffer
  209. (while (and articles is-old)
  210. (when (nnmbox-find-article (car articles))
  211. (if (setq is-old
  212. (nnmail-expired-article-p
  213. newsgroup
  214. (buffer-substring (point) (line-end-position))
  215. force))
  216. (progn
  217. (unless (eq nnmail-expiry-target 'delete)
  218. (with-temp-buffer
  219. (nnmbox-request-article (car articles)
  220. newsgroup server
  221. (current-buffer))
  222. (let ((nnml-current-directory nil))
  223. (nnmail-expiry-target-group
  224. nnmail-expiry-target newsgroup)))
  225. (nnmbox-possibly-change-newsgroup newsgroup server))
  226. (nnheader-message 5 "Deleting article %d in %s..."
  227. (car articles) newsgroup)
  228. (nnmbox-delete-mail))
  229. (push (car articles) rest)))
  230. (setq articles (cdr articles)))
  231. (nnmbox-save-buffer)
  232. ;; Find the lowest active article in this group.
  233. (let ((active (nth 1 (assoc newsgroup nnmbox-group-alist))))
  234. (while (and (not (nnmbox-find-article (car active)))
  235. (<= (car active) (cdr active)))
  236. (setcar active (1+ (car active)))))
  237. (nnmbox-save-active nnmbox-group-alist nnmbox-active-file)
  238. (nconc rest articles))))
  239. (deffoo nnmbox-request-move-article
  240. (article group server accept-form &optional last move-is-internal)
  241. (let ((buf (get-buffer-create " *nnmbox move*"))
  242. result)
  243. (and
  244. (nnmbox-request-article article group server)
  245. (with-current-buffer buf
  246. (erase-buffer)
  247. (insert-buffer-substring nntp-server-buffer)
  248. (goto-char (point-min))
  249. (while (re-search-forward
  250. "^X-Gnus-Newsgroup:"
  251. (save-excursion (search-forward "\n\n" nil t) (point)) t)
  252. (gnus-delete-line))
  253. (setq result (eval accept-form))
  254. (kill-buffer buf)
  255. result)
  256. (save-excursion
  257. (nnmbox-possibly-change-newsgroup group server)
  258. (set-buffer nnmbox-mbox-buffer)
  259. (when (nnmbox-find-article article)
  260. (nnmbox-delete-mail))
  261. (and last (nnmbox-save-buffer))))
  262. result))
  263. (deffoo nnmbox-request-accept-article (group &optional server last)
  264. (nnmbox-possibly-change-newsgroup group server)
  265. (nnmail-check-syntax)
  266. (let ((buf (current-buffer))
  267. result cont)
  268. (and
  269. (nnmail-activate 'nnmbox)
  270. (with-temp-buffer
  271. (insert-buffer-substring buf)
  272. (goto-char (point-min))
  273. (cond (;; The From line may have been quoted by movemail.
  274. (looking-at (concat ">" message-unix-mail-delimiter))
  275. (delete-char 1)
  276. (forward-line 1))
  277. ((looking-at "X-From-Line: ")
  278. (replace-match "From ")
  279. (forward-line 1))
  280. (t
  281. (insert "From nobody " (current-time-string) "\n")))
  282. (narrow-to-region (point)
  283. (if (search-forward "\n\n" nil 'move)
  284. (1- (point))
  285. (point)))
  286. (while (re-search-backward "^X-Gnus-Newsgroup: " nil t)
  287. (delete-region (point) (progn (forward-line 1) (point))))
  288. (when nnmail-cache-accepted-message-ids
  289. (nnmail-cache-insert (message-fetch-field "message-id")
  290. group
  291. (message-fetch-field "subject")
  292. (message-fetch-field "from")))
  293. (widen)
  294. (setq result (if (stringp group)
  295. (list (cons group (nnmbox-active-number group)))
  296. (nnmail-article-group 'nnmbox-active-number)))
  297. (prog1
  298. (if (and (null result)
  299. (yes-or-no-p "Moved to `junk' group; delete article? "))
  300. (setq result 'junk)
  301. (setq result (car (nnmbox-save-mail result))))
  302. (setq cont (buffer-string))))
  303. (with-current-buffer nnmbox-mbox-buffer
  304. (goto-char (point-max))
  305. (insert cont)
  306. (when last
  307. (when nnmail-cache-accepted-message-ids
  308. (nnmail-cache-close))
  309. (nnmbox-save-active nnmbox-group-alist nnmbox-active-file)
  310. (nnmbox-save-buffer))))
  311. result))
  312. (deffoo nnmbox-request-replace-article (article group buffer)
  313. (nnmbox-possibly-change-newsgroup group)
  314. (with-current-buffer nnmbox-mbox-buffer
  315. (if (not (nnmbox-find-article article))
  316. nil
  317. (nnmbox-delete-mail t t)
  318. (insert
  319. (with-temp-buffer
  320. (insert-buffer-substring buffer)
  321. (goto-char (point-min))
  322. (when (looking-at "X-From-Line:")
  323. (delete-region (point) (progn (forward-line 1) (point))))
  324. (while (re-search-forward (concat "^" message-unix-mail-delimiter)
  325. nil t)
  326. (goto-char (match-beginning 0))
  327. (insert ">"))
  328. (goto-char (point-max))
  329. (unless (bolp)
  330. (insert "\n"))
  331. (buffer-string)))
  332. (nnmbox-save-buffer)
  333. t)))
  334. (deffoo nnmbox-request-delete-group (group &optional force server)
  335. (nnmbox-possibly-change-newsgroup group server)
  336. ;; Delete all articles in GROUP.
  337. (if (not force)
  338. () ; Don't delete the articles.
  339. (with-current-buffer nnmbox-mbox-buffer
  340. (goto-char (point-min))
  341. ;; Delete all articles in this group.
  342. (let ((ident (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":"))
  343. found)
  344. (while (search-forward ident nil t)
  345. (setq found t)
  346. (nnmbox-delete-mail))
  347. (when found
  348. (nnmbox-save-buffer)))))
  349. ;; Remove the group from all structures.
  350. (setq nnmbox-group-alist
  351. (delq (assoc group nnmbox-group-alist) nnmbox-group-alist)
  352. nnmbox-current-group nil)
  353. ;; Save the active file.
  354. (nnmbox-save-active nnmbox-group-alist nnmbox-active-file)
  355. t)
  356. (deffoo nnmbox-request-rename-group (group new-name &optional server)
  357. (nnmbox-possibly-change-newsgroup group server)
  358. (with-current-buffer nnmbox-mbox-buffer
  359. (goto-char (point-min))
  360. (let ((ident (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":"))
  361. (new-ident (concat "\nX-Gnus-Newsgroup: " new-name ":"))
  362. found)
  363. (while (search-forward ident nil t)
  364. (replace-match new-ident t t)
  365. (setq found t))
  366. (when found
  367. (nnmbox-save-buffer))))
  368. (let ((entry (assoc group nnmbox-group-active-articles)))
  369. (when entry
  370. (setcar entry new-name)))
  371. (let ((entry (assoc group nnmbox-group-alist)))
  372. (when entry
  373. (setcar entry new-name))
  374. (setq nnmbox-current-group nil)
  375. ;; Save the new group alist.
  376. (nnmbox-save-active nnmbox-group-alist nnmbox-active-file)
  377. t))
  378. ;;; Internal functions.
  379. ;; If FORCE, delete article no matter how many X-Gnus-Newsgroup
  380. ;; headers there are. If LEAVE-DELIM, don't delete the Unix mbox
  381. ;; delimiter line.
  382. (defun nnmbox-delete-mail (&optional force leave-delim)
  383. ;; Delete the current X-Gnus-Newsgroup line.
  384. ;; First delete record of active article, unless the article is being
  385. ;; replaced, indicated by FORCE being non-nil.
  386. (if (not force)
  387. (nnmbox-record-deleted-article (nnmbox-article-group-number t)))
  388. (or force
  389. (gnus-delete-line))
  390. ;; Beginning of the article.
  391. (save-excursion
  392. (save-restriction
  393. (narrow-to-region
  394. (prog2
  395. (re-search-backward (concat "^" message-unix-mail-delimiter) nil t)
  396. (if leave-delim (progn (forward-line 1) (point))
  397. (match-beginning 0))
  398. (forward-line 1))
  399. (or (and (re-search-forward (concat "^" message-unix-mail-delimiter)
  400. nil t)
  401. (match-beginning 0))
  402. (point-max)))
  403. (goto-char (point-min))
  404. ;; Only delete the article if no other group owns it as well.
  405. (when (or force
  406. (not (re-search-forward "^X-Gnus-Newsgroup: " nil t))
  407. (search-backward "\n\n" nil t))
  408. (delete-region (point-min) (point-max))))))
  409. (defun nnmbox-possibly-change-newsgroup (newsgroup &optional server)
  410. (when (and server
  411. (not (nnmbox-server-opened server)))
  412. (nnmbox-open-server server))
  413. (when (or (not nnmbox-mbox-buffer)
  414. (not (buffer-name nnmbox-mbox-buffer)))
  415. (nnmbox-read-mbox))
  416. (when (not nnmbox-group-alist)
  417. (nnmail-activate 'nnmbox))
  418. (if newsgroup
  419. (when (assoc newsgroup nnmbox-group-alist)
  420. (setq nnmbox-current-group newsgroup))
  421. t))
  422. (defun nnmbox-article-string (article)
  423. (if (numberp article)
  424. (concat "\nX-Gnus-Newsgroup: " nnmbox-current-group ":"
  425. (int-to-string article) " ")
  426. (concat "\nMessage-ID: " article)))
  427. (defun nnmbox-article-group-number (this-line)
  428. (save-excursion
  429. (if this-line
  430. (beginning-of-line)
  431. (goto-char (point-min)))
  432. (when (re-search-forward "^X-Gnus-Newsgroup: +\\([^:]+\\):\\([0-9]+\\) "
  433. nil t)
  434. (cons (buffer-substring (match-beginning 1) (match-end 1))
  435. (string-to-number
  436. (buffer-substring (match-beginning 2) (match-end 2)))))))
  437. (defun nnmbox-in-header-p (pos)
  438. "Return non-nil if POS is in the header of an article."
  439. (save-excursion
  440. (goto-char pos)
  441. (re-search-backward (concat "^" message-unix-mail-delimiter) nil t)
  442. (search-forward "\n\n" nil t)
  443. (< pos (point))))
  444. (defun nnmbox-find-article (article)
  445. "Leaves point on the relevant X-Gnus-Newsgroup line if found."
  446. ;; Check that article is in the active range first, to avoid an
  447. ;; expensive exhaustive search if it isn't.
  448. (if (and (numberp article)
  449. (not (nnmbox-is-article-active-p article)))
  450. nil
  451. (let ((art-string (nnmbox-article-string article))
  452. (found nil))
  453. ;; There is the possibility that the X-Gnus-Newsgroup line appears
  454. ;; in the body of an article (for instance, if an article has been
  455. ;; forwarded from someone using Gnus as their mailer), so check
  456. ;; that the line is actually part of the article header.
  457. (or (and (search-forward art-string nil t)
  458. (nnmbox-in-header-p (point)))
  459. (progn
  460. (goto-char (point-min))
  461. (while (and (not found)
  462. (search-forward art-string nil t))
  463. (setq found (nnmbox-in-header-p (point))))
  464. found)))))
  465. (defun nnmbox-record-active-article (group-art)
  466. (let* ((group (car group-art))
  467. (article (cdr group-art))
  468. (entry
  469. (or (assoc group nnmbox-group-active-articles)
  470. (progn
  471. (push (list group)
  472. nnmbox-group-active-articles)
  473. (car nnmbox-group-active-articles)))))
  474. ;; add article to index, either by building complete list
  475. ;; in reverse order, or as a list of ranges.
  476. (if (not nnmbox-group-building-active-articles)
  477. (setcdr entry (gnus-add-to-range (cdr entry) (list article)))
  478. (when (memq article (cdr entry))
  479. (switch-to-buffer nnmbox-mbox-buffer)
  480. (error "Article %s:%d already exists!" group article))
  481. (when (and (cadr entry) (< article (cadr entry)))
  482. (switch-to-buffer nnmbox-mbox-buffer)
  483. (error "Article %s:%d out of order" group article))
  484. (setcdr entry (cons article (cdr entry))))))
  485. (defun nnmbox-record-deleted-article (group-art)
  486. (let* ((group (car group-art))
  487. (article (cdr group-art))
  488. (entry
  489. (or (assoc group nnmbox-group-active-articles)
  490. (progn
  491. (push (list group)
  492. nnmbox-group-active-articles)
  493. (car nnmbox-group-active-articles)))))
  494. ;; remove article from index
  495. (setcdr entry (gnus-remove-from-range (cdr entry) (list article)))))
  496. (defun nnmbox-is-article-active-p (article)
  497. (gnus-member-of-range
  498. article
  499. (cdr (assoc nnmbox-current-group
  500. nnmbox-group-active-articles))))
  501. (defun nnmbox-save-mail (group-art)
  502. "Called narrowed to an article."
  503. (let ((delim (concat "^" message-unix-mail-delimiter)))
  504. (goto-char (point-min))
  505. ;; This might come from somewhere else.
  506. (if (looking-at delim)
  507. (forward-line 1)
  508. (insert "From nobody " (current-time-string) "\n"))
  509. ;; Quote all "From " lines in the article.
  510. (while (re-search-forward delim nil t)
  511. (goto-char (match-beginning 0))
  512. (insert ">")))
  513. (goto-char (point-max))
  514. (unless (bolp)
  515. (insert "\n"))
  516. (nnmail-insert-lines)
  517. (nnmail-insert-xref group-art)
  518. (nnmbox-insert-newsgroup-line group-art)
  519. (let ((alist group-art))
  520. (while alist
  521. (nnmbox-record-active-article (car alist))
  522. (setq alist (cdr alist))))
  523. (run-hooks 'nnmail-prepare-save-mail-hook)
  524. (run-hooks 'nnmbox-prepare-save-mail-hook)
  525. group-art)
  526. (defun nnmbox-insert-newsgroup-line (group-art)
  527. (save-excursion
  528. (goto-char (point-min))
  529. (when (search-forward "\n\n" nil t)
  530. (forward-char -1)
  531. (while group-art
  532. (insert (format "X-Gnus-Newsgroup: %s:%d %s\n"
  533. (caar group-art) (cdar group-art)
  534. (current-time-string)))
  535. (setq group-art (cdr group-art))))
  536. t))
  537. (defun nnmbox-active-number (group)
  538. ;; Find the next article number in GROUP.
  539. (let ((active (cadr (assoc group nnmbox-group-alist))))
  540. (if active
  541. (setcdr active (1+ (cdr active)))
  542. ;; This group is new, so we create a new entry for it.
  543. ;; This might be a bit naughty... creating groups on the drop of
  544. ;; a hat, but I don't know...
  545. (push (list group (setq active (cons 1 1)))
  546. nnmbox-group-alist))
  547. (cdr active)))
  548. (defun nnmbox-create-mbox ()
  549. (when (not (file-exists-p nnmbox-mbox-file))
  550. (let ((nnmail-file-coding-system
  551. (or nnmbox-file-coding-system-for-write
  552. nnmbox-file-coding-system))
  553. (dir (file-name-directory nnmbox-mbox-file)))
  554. (and dir (gnus-make-directory dir))
  555. (nnmail-write-region (point-min) (point-min)
  556. nnmbox-mbox-file t 'nomesg))))
  557. (defun nnmbox-read-mbox ()
  558. (nnmail-activate 'nnmbox)
  559. (nnmbox-create-mbox)
  560. (if (and nnmbox-mbox-buffer
  561. (buffer-name nnmbox-mbox-buffer)
  562. (with-current-buffer nnmbox-mbox-buffer
  563. (= (buffer-size) (nnheader-file-size nnmbox-mbox-file))))
  564. ()
  565. (save-excursion
  566. (let ((delim (concat "^" message-unix-mail-delimiter))
  567. (alist nnmbox-group-alist)
  568. (nnmbox-group-building-active-articles t)
  569. start end end-header number)
  570. (set-buffer (setq nnmbox-mbox-buffer
  571. (let ((nnheader-file-coding-system
  572. nnmbox-file-coding-system))
  573. (nnheader-find-file-noselect
  574. nnmbox-mbox-file t t))))
  575. (mm-enable-multibyte)
  576. (buffer-disable-undo)
  577. (gnus-add-buffer)
  578. ;; Go through the group alist and compare against the mbox file.
  579. (while alist
  580. (goto-char (point-max))
  581. (when (and (re-search-backward
  582. (format "^X-Gnus-Newsgroup: %s:\\([0-9]+\\) "
  583. (caar alist)) nil t)
  584. (> (setq number
  585. (string-to-number
  586. (buffer-substring
  587. (match-beginning 1) (match-end 1))))
  588. (cdadar alist)))
  589. (setcdr (cadar alist) number))
  590. (setq alist (cdr alist)))
  591. ;; Examine all articles for our private X-Gnus-Newsgroup
  592. ;; headers. This is done primarily as a consistency check, but
  593. ;; it is convenient for building an index of the articles
  594. ;; present, to avoid costly searches for missing articles
  595. ;; (eg. when expiring articles).
  596. (goto-char (point-min))
  597. (setq nnmbox-group-active-articles nil)
  598. (while (re-search-forward delim nil t)
  599. (setq start (match-beginning 0))
  600. (save-excursion
  601. (search-forward "\n\n" nil t)
  602. (setq end-header (point))
  603. (setq end (or (and
  604. (re-search-forward delim nil t)
  605. (match-beginning 0))
  606. (point-max))))
  607. (if (search-forward "\nX-Gnus-Newsgroup: " end-header t)
  608. ;; Build a list of articles in each group, remembering
  609. ;; that each article may be in more than one group.
  610. (progn
  611. (nnmbox-record-active-article (nnmbox-article-group-number t))
  612. (while (search-forward "\nX-Gnus-Newsgroup: " end-header t)
  613. (nnmbox-record-active-article (nnmbox-article-group-number t))))
  614. ;; The article is either new, or for some other reason
  615. ;; hasn't got our private headers, so add them now. The
  616. ;; only situation I've encountered when the X-Gnus-Newsgroup
  617. ;; header is missing is if the article contains a forwarded
  618. ;; message which does contain that header line (earlier
  619. ;; versions of Gnus didn't restrict their search to the
  620. ;; headers). In this case, there is an Xref line which
  621. ;; provides the relevant information to construct the
  622. ;; missing header(s).
  623. (save-excursion
  624. (save-restriction
  625. (narrow-to-region start end)
  626. (if (re-search-forward "\nXref: [^ ]+" end-header t)
  627. ;; generate headers from Xref:
  628. (let (alist)
  629. (while (re-search-forward " \\([^:]+\\):\\([0-9]+\\)" end-header t)
  630. (push (cons (match-string 1)
  631. (string-to-number (match-string 2))) alist))
  632. (nnmbox-insert-newsgroup-line alist))
  633. ;; this is really a new article
  634. (nnmbox-save-mail
  635. (nnmail-article-group 'nnmbox-active-number))))))
  636. (goto-char end))
  637. ;; put article lists in order
  638. (setq alist nnmbox-group-active-articles)
  639. (while alist
  640. (setcdr (car alist) (gnus-compress-sequence (nreverse (cdar alist))))
  641. (setq alist (cdr alist)))))))
  642. (provide 'nnmbox)
  643. ;;; nnmbox.el ends here