nnmh.el 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  1. ;;; nnmh.el --- mhspool 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. ;; Based on nnspool.el by Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>.
  19. ;; For an overview of what the interface functions do, please see the
  20. ;; Gnus sources.
  21. ;;; Code:
  22. (require 'nnheader)
  23. (require 'nnmail)
  24. (require 'gnus-start)
  25. (require 'nnoo)
  26. (eval-when-compile (require 'cl))
  27. (nnoo-declare nnmh)
  28. (defvoo nnmh-directory message-directory
  29. "Mail spool directory.")
  30. (defvoo nnmh-get-new-mail t
  31. "If non-nil, nnmh will check the incoming mail file and split the mail.")
  32. (defvoo nnmh-prepare-save-mail-hook nil
  33. "Hook run narrowed to an article before saving.")
  34. (defvoo nnmh-be-safe nil
  35. "If non-nil, nnmh will check all articles to make sure whether they are new or not.
  36. Go through the .nnmh-articles file and compare with the actual
  37. articles in this folder. The articles that are \"new\" will be marked
  38. as unread by Gnus.")
  39. (defconst nnmh-version "nnmh 1.0"
  40. "nnmh version.")
  41. (defvoo nnmh-current-directory nil
  42. "Current news group directory.")
  43. (defvoo nnmh-status-string "")
  44. (defvoo nnmh-group-alist nil)
  45. ;; Don't even think about setting this variable. It does not exist.
  46. ;; Forget about it. Uh-huh. Nope. Nobody here. It's only bound
  47. ;; dynamically by certain functions in nndraft.
  48. (defvar nnmh-allow-delete-final nil)
  49. ;;; Interface functions.
  50. (nnoo-define-basics nnmh)
  51. (deffoo nnmh-retrieve-headers (articles &optional newsgroup server fetch-old)
  52. (with-current-buffer nntp-server-buffer
  53. (erase-buffer)
  54. (let* ((file nil)
  55. (number (length articles))
  56. (large (and (numberp nnmail-large-newsgroup)
  57. (> number nnmail-large-newsgroup)))
  58. (count 0)
  59. (file-name-coding-system nnmail-pathname-coding-system)
  60. beg article)
  61. (nnmh-possibly-change-directory newsgroup server)
  62. ;; We don't support fetching by Message-ID.
  63. (if (stringp (car articles))
  64. 'headers
  65. (while articles
  66. (when (and (file-exists-p
  67. (setq file (concat (file-name-as-directory
  68. nnmh-current-directory)
  69. (int-to-string
  70. (setq article (pop articles))))))
  71. (not (file-directory-p file)))
  72. (insert (format "221 %d Article retrieved.\n" article))
  73. (setq beg (point))
  74. (nnheader-insert-head file)
  75. (goto-char beg)
  76. (if (search-forward "\n\n" nil t)
  77. (forward-char -1)
  78. (goto-char (point-max))
  79. (insert "\n\n"))
  80. (insert ".\n")
  81. (delete-region (point) (point-max)))
  82. (setq count (1+ count))
  83. (and large
  84. (zerop (% count 20))
  85. (nnheader-message 5 "nnmh: Receiving headers... %d%%"
  86. (floor (* count 100.0) number))))
  87. (when large
  88. (nnheader-message 5 "nnmh: Receiving headers...done"))
  89. (nnheader-fold-continuation-lines)
  90. 'headers))))
  91. (deffoo nnmh-open-server (server &optional defs)
  92. (nnoo-change-server 'nnmh server defs)
  93. (when (not (file-exists-p nnmh-directory))
  94. (condition-case ()
  95. (make-directory nnmh-directory t)
  96. (error t)))
  97. (cond
  98. ((not (file-exists-p nnmh-directory))
  99. (nnmh-close-server)
  100. (nnheader-report 'nnmh "Couldn't create directory: %s" nnmh-directory))
  101. ((not (file-directory-p (file-truename nnmh-directory)))
  102. (nnmh-close-server)
  103. (nnheader-report 'nnmh "Not a directory: %s" nnmh-directory))
  104. (t
  105. (nnheader-report 'nnmh "Opened server %s using directory %s"
  106. server nnmh-directory)
  107. t)))
  108. (deffoo nnmh-request-article (id &optional newsgroup server buffer)
  109. (nnmh-possibly-change-directory newsgroup server)
  110. (let ((file (if (stringp id)
  111. nil
  112. (concat nnmh-current-directory (int-to-string id))))
  113. (file-name-coding-system nnmail-pathname-coding-system)
  114. (nntp-server-buffer (or buffer nntp-server-buffer)))
  115. (and (stringp file)
  116. (file-exists-p file)
  117. (not (file-directory-p file))
  118. (save-excursion (nnmail-find-file file))
  119. (string-to-number (file-name-nondirectory file)))))
  120. (deffoo nnmh-request-group (group &optional server dont-check info)
  121. (nnheader-init-server-buffer)
  122. (nnmh-possibly-change-directory group server)
  123. (let ((pathname (nnmail-group-pathname group nnmh-directory))
  124. (file-name-coding-system nnmail-pathname-coding-system)
  125. dir)
  126. (cond
  127. ((not (file-directory-p pathname))
  128. (nnheader-report
  129. 'nnmh "Can't select group (no such directory): %s" group))
  130. (t
  131. (setq nnmh-current-directory pathname)
  132. (and nnmh-get-new-mail
  133. nnmh-be-safe
  134. (nnmh-update-gnus-unreads group))
  135. (cond
  136. (dont-check
  137. (nnheader-report 'nnmh "Selected group %s" group)
  138. t)
  139. (t
  140. ;; Re-scan the directory if it's on a foreign system.
  141. (nnheader-re-read-dir pathname)
  142. (setq dir
  143. (sort
  144. (mapcar 'string-to-number
  145. (directory-files pathname nil "^[0-9]+$" t))
  146. '<))
  147. (cond
  148. (dir
  149. (setq nnmh-group-alist
  150. (delq (assoc group nnmh-group-alist) nnmh-group-alist))
  151. (push (list group (cons (car dir) (car (last dir))))
  152. nnmh-group-alist)
  153. (nnheader-report 'nnmh "Selected group %s" group)
  154. (nnheader-insert
  155. "211 %d %d %d %s\n" (length dir) (car dir)
  156. (car (last dir)) group))
  157. (t
  158. (nnheader-report 'nnmh "Empty group %s" group)
  159. (nnheader-insert (format "211 0 1 0 %s\n" group))))))))))
  160. (deffoo nnmh-request-scan (&optional group server)
  161. (nnmail-get-new-mail 'nnmh nil nnmh-directory group))
  162. (deffoo nnmh-request-list (&optional server dir)
  163. (nnheader-insert "")
  164. (nnmh-possibly-change-directory nil server)
  165. (let ((file-name-coding-system nnmail-pathname-coding-system)
  166. (nnmh-toplev
  167. (file-truename (or dir (file-name-as-directory nnmh-directory)))))
  168. (nnmh-request-list-1 nnmh-toplev))
  169. (setq nnmh-group-alist (nnmail-get-active))
  170. t)
  171. (defvar nnmh-toplev)
  172. (defun nnmh-request-list-1 (dir)
  173. (setq dir (expand-file-name dir))
  174. ;; Recurse down all directories.
  175. (let ((files (nnheader-directory-files dir t nil t))
  176. (max 0)
  177. min rdir num subdirectoriesp file)
  178. ;; Recurse down directories.
  179. (setq subdirectoriesp
  180. ;; nth 1 of file-attributes always 1 on MS Windows :(
  181. (/= (nth 1 (file-attributes (file-truename dir))) 2))
  182. (dolist (rdir files)
  183. (if (or (not subdirectoriesp)
  184. (file-regular-p rdir))
  185. (progn
  186. (setq file (file-name-nondirectory rdir))
  187. (when (string-match "^[0-9]+$" file)
  188. (setq num (string-to-number file))
  189. (setq max (max max num))
  190. (when (or (null min)
  191. (< num min))
  192. (setq min num))))
  193. ;; This is a directory.
  194. (when (and (file-readable-p rdir)
  195. (not (equal (file-truename rdir)
  196. (file-truename dir))))
  197. (nnmh-request-list-1 rdir))))
  198. ;; For each directory, generate an active file line.
  199. (unless (string= (expand-file-name nnmh-toplev) dir)
  200. (with-current-buffer nntp-server-buffer
  201. (goto-char (point-max))
  202. (insert
  203. (format
  204. "%s %.0f %.0f y\n"
  205. (progn
  206. (string-match
  207. (regexp-quote
  208. (file-truename (file-name-as-directory
  209. (expand-file-name nnmh-toplev))))
  210. dir)
  211. (mm-string-to-multibyte ;Why? Isn't it multibyte already?
  212. (mm-encode-coding-string
  213. (nnheader-replace-chars-in-string
  214. (substring dir (match-end 0))
  215. ?/ ?.)
  216. nnmail-pathname-coding-system)))
  217. (or max 0)
  218. (or min 1))))))
  219. t)
  220. (deffoo nnmh-request-newgroups (date &optional server)
  221. (nnmh-request-list server))
  222. (deffoo nnmh-request-expire-articles (articles newsgroup
  223. &optional server force)
  224. (nnmh-possibly-change-directory newsgroup server)
  225. (let ((is-old t)
  226. (dir nnmh-current-directory)
  227. article rest mod-time)
  228. (nnheader-init-server-buffer)
  229. (while (and articles is-old)
  230. (setq article (concat dir (int-to-string (car articles))))
  231. (when (setq mod-time (nth 5 (file-attributes article)))
  232. (if (and (nnmh-deletable-article-p newsgroup (car articles))
  233. (setq is-old
  234. (nnmail-expired-article-p newsgroup mod-time force)))
  235. (progn
  236. ;; Allow a special target group. -- jcn
  237. (unless (eq nnmail-expiry-target 'delete)
  238. (with-temp-buffer
  239. (nnmh-request-article (car articles)
  240. newsgroup server (current-buffer))
  241. (nnmail-expiry-target-group
  242. nnmail-expiry-target newsgroup)))
  243. (nnheader-message 5 "Deleting article %s in %s..."
  244. article newsgroup)
  245. (condition-case ()
  246. (funcall nnmail-delete-file-function article)
  247. (file-error
  248. (nnheader-message 1 "Couldn't delete article %s in %s"
  249. article newsgroup)
  250. (push (car articles) rest))))
  251. (push (car articles) rest)))
  252. (setq articles (cdr articles)))
  253. (nnheader-message 5 "")
  254. (nconc rest articles)))
  255. (deffoo nnmh-close-group (group &optional server)
  256. t)
  257. (deffoo nnmh-request-move-article (article group server accept-form
  258. &optional last move-is-internal)
  259. (let ((buf (get-buffer-create " *nnmh move*"))
  260. result)
  261. (and
  262. (nnmh-deletable-article-p group article)
  263. (nnmh-request-article article group server)
  264. (with-current-buffer buf
  265. (erase-buffer)
  266. (insert-buffer-substring nntp-server-buffer)
  267. (setq result (eval accept-form))
  268. (kill-buffer (current-buffer))
  269. result)
  270. (progn
  271. (nnmh-possibly-change-directory group server)
  272. (condition-case ()
  273. (funcall nnmail-delete-file-function
  274. (concat nnmh-current-directory (int-to-string article)))
  275. (file-error nil))))
  276. result))
  277. (deffoo nnmh-request-accept-article (group &optional server last noinsert)
  278. (nnmh-possibly-change-directory group server)
  279. (nnmail-check-syntax)
  280. (when nnmail-cache-accepted-message-ids
  281. (nnmail-cache-insert (nnmail-fetch-field "message-id")
  282. group
  283. (nnmail-fetch-field "subject")
  284. (nnmail-fetch-field "from")))
  285. (nnheader-init-server-buffer)
  286. (prog1
  287. (if (stringp group)
  288. (if noinsert
  289. (nnmh-active-number group)
  290. (car (nnmh-save-mail
  291. (list (cons group (nnmh-active-number group)))
  292. noinsert)))
  293. (let ((res (nnmail-article-group 'nnmh-active-number)))
  294. (if (and (null res)
  295. (yes-or-no-p "Moved to `junk' group; delete article? "))
  296. 'junk
  297. (car (nnmh-save-mail res noinsert)))))
  298. (when (and last nnmail-cache-accepted-message-ids)
  299. (nnmail-cache-close))))
  300. (deffoo nnmh-request-replace-article (article group buffer)
  301. (nnmh-possibly-change-directory group)
  302. (with-current-buffer buffer
  303. (nnmh-possibly-create-directory group)
  304. (ignore-errors
  305. (nnmail-write-region
  306. (point-min) (point-max)
  307. (concat nnmh-current-directory (int-to-string article))
  308. nil (if (nnheader-be-verbose 5) nil 'nomesg))
  309. t)))
  310. (deffoo nnmh-request-create-group (group &optional server args)
  311. (nnheader-init-server-buffer)
  312. (unless (assoc group nnmh-group-alist)
  313. (let (active)
  314. (push (list group (setq active (cons 1 0)))
  315. nnmh-group-alist)
  316. (nnmh-possibly-create-directory group)
  317. (nnmh-possibly-change-directory group server)
  318. (let ((articles (mapcar 'string-to-number
  319. (directory-files
  320. nnmh-current-directory nil "^[0-9]+$"))))
  321. (when articles
  322. (setcar active (apply 'min articles))
  323. (setcdr active (apply 'max articles))))))
  324. t)
  325. (deffoo nnmh-request-delete-group (group &optional force server)
  326. (nnmh-possibly-change-directory group server)
  327. ;; Delete all articles in GROUP.
  328. (if (not force)
  329. () ; Don't delete the articles.
  330. (let ((articles (directory-files nnmh-current-directory t "^[0-9]+$")))
  331. (while articles
  332. (when (file-writable-p (car articles))
  333. (nnheader-message 5 "Deleting article %s in %s..."
  334. (car articles) group)
  335. (funcall nnmail-delete-file-function (car articles)))
  336. (setq articles (cdr articles))))
  337. ;; Try to delete the directory itself.
  338. (ignore-errors
  339. (delete-directory nnmh-current-directory)))
  340. ;; Remove the group from all structures.
  341. (setq nnmh-group-alist
  342. (delq (assoc group nnmh-group-alist) nnmh-group-alist)
  343. nnmh-current-directory nil)
  344. t)
  345. (deffoo nnmh-request-rename-group (group new-name &optional server)
  346. (nnmh-possibly-change-directory group server)
  347. (let ((new-dir (nnmail-group-pathname new-name nnmh-directory))
  348. (old-dir (nnmail-group-pathname group nnmh-directory)))
  349. (when (ignore-errors
  350. (make-directory new-dir t)
  351. t)
  352. ;; We move the articles file by file instead of renaming
  353. ;; the directory -- there may be subgroups in this group.
  354. ;; One might be more clever, I guess.
  355. (let ((files (nnheader-article-to-file-alist old-dir)))
  356. (while files
  357. (rename-file
  358. (concat old-dir (cdar files))
  359. (concat new-dir (cdar files)))
  360. (pop files)))
  361. (when (<= (length (directory-files old-dir)) 2)
  362. (ignore-errors
  363. (delete-directory old-dir)))
  364. ;; That went ok, so we change the internal structures.
  365. (let ((entry (assoc group nnmh-group-alist)))
  366. (when entry
  367. (setcar entry new-name))
  368. (setq nnmh-current-directory nil)
  369. t))))
  370. (nnoo-define-skeleton nnmh)
  371. ;;; Internal functions.
  372. (defun nnmh-possibly-change-directory (newsgroup &optional server)
  373. (when (and server
  374. (not (nnmh-server-opened server)))
  375. (nnmh-open-server server))
  376. (when newsgroup
  377. (let ((pathname (nnmail-group-pathname newsgroup nnmh-directory))
  378. (file-name-coding-system nnmail-pathname-coding-system))
  379. (if (file-directory-p pathname)
  380. (setq nnmh-current-directory pathname)
  381. (nnheader-report 'nnmh "Not a directory: %s" nnmh-directory)))))
  382. (defun nnmh-possibly-create-directory (group)
  383. (let (dir dirs)
  384. (setq dir (nnmail-group-pathname group nnmh-directory))
  385. (while (not (file-directory-p dir))
  386. (push dir dirs)
  387. (setq dir (file-name-directory (directory-file-name dir))))
  388. (while dirs
  389. (when (make-directory (directory-file-name (car dirs)))
  390. (error "Could not create directory %s" (car dirs)))
  391. (nnheader-message 5 "Creating mail directory %s" (car dirs))
  392. (setq dirs (cdr dirs)))))
  393. (defun nnmh-save-mail (group-art &optional noinsert)
  394. "Called narrowed to an article."
  395. (unless noinsert
  396. (nnmail-insert-lines)
  397. (nnmail-insert-xref group-art))
  398. (run-hooks 'nnmail-prepare-save-mail-hook)
  399. (run-hooks 'nnmh-prepare-save-mail-hook)
  400. (goto-char (point-min))
  401. (while (looking-at "From ")
  402. (replace-match "X-From-Line: ")
  403. (forward-line 1))
  404. ;; We save the article in all the newsgroups it belongs in.
  405. (let ((ga group-art)
  406. first)
  407. (while ga
  408. (nnmh-possibly-create-directory (caar ga))
  409. (let ((file (concat (nnmail-group-pathname
  410. (caar ga) nnmh-directory)
  411. (int-to-string (cdar ga)))))
  412. (if first
  413. ;; It was already saved, so we just make a hard link.
  414. (funcall nnmail-crosspost-link-function first file t)
  415. ;; Save the article.
  416. (nnmail-write-region (point-min) (point-max) file nil nil)
  417. (setq first file)))
  418. (setq ga (cdr ga))))
  419. group-art)
  420. (defun nnmh-active-number (group)
  421. "Compute the next article number in GROUP."
  422. (let ((active (cadr (assoc group nnmh-group-alist)))
  423. (dir (nnmail-group-pathname group nnmh-directory))
  424. (file-name-coding-system nnmail-pathname-coding-system)
  425. file)
  426. (unless active
  427. ;; The group wasn't known to nnmh, so we just create an active
  428. ;; entry for it.
  429. (setq active (cons 1 0))
  430. (push (list group active) nnmh-group-alist)
  431. (unless (file-exists-p dir)
  432. (gnus-make-directory dir))
  433. ;; Find the highest number in the group.
  434. (let ((files (sort
  435. (mapcar 'string-to-number
  436. (directory-files dir nil "^[0-9]+$"))
  437. '>)))
  438. (when files
  439. (setcdr active (car files)))))
  440. (setcdr active (1+ (cdr active)))
  441. (while (or
  442. ;; See whether the file exists...
  443. (file-exists-p
  444. (setq file (concat (nnmail-group-pathname group nnmh-directory)
  445. (int-to-string (cdr active)))))
  446. ;; ... or there is a buffer that will make that file exist
  447. ;; in the future.
  448. (get-file-buffer file))
  449. ;; Skip past that file.
  450. (setcdr active (1+ (cdr active))))
  451. (cdr active)))
  452. (defun nnmh-update-gnus-unreads (group)
  453. ;; Go through the .nnmh-articles file and compare with the actual
  454. ;; articles in this folder. The articles that are "new" will be
  455. ;; marked as unread by Gnus.
  456. (let* ((dir nnmh-current-directory)
  457. (files (sort (mapcar 'string-to-number
  458. (directory-files nnmh-current-directory
  459. nil "^[0-9]+$" t))
  460. '<))
  461. (nnmh-file (concat dir ".nnmh-articles"))
  462. new articles)
  463. ;; Load the .nnmh-articles file.
  464. (when (file-exists-p nnmh-file)
  465. (setq articles
  466. (let (nnmh-newsgroup-articles)
  467. (ignore-errors (load nnmh-file nil t t))
  468. nnmh-newsgroup-articles)))
  469. ;; Add all new articles to the `new' list.
  470. (let ((art files))
  471. (while art
  472. (unless (assq (car art) articles)
  473. (push (car art) new))
  474. (setq art (cdr art))))
  475. ;; Remove all deleted articles.
  476. (let ((art articles))
  477. (while art
  478. (unless (memq (caar art) files)
  479. (setq articles (delq (car art) articles)))
  480. (setq art (cdr art))))
  481. ;; Check whether the articles really are the ones that Gnus thinks
  482. ;; they are by looking at the time-stamps.
  483. (let ((arts articles)
  484. art)
  485. (while (setq art (pop arts))
  486. (when (not (equal
  487. (nth 5 (file-attributes
  488. (concat dir (int-to-string (car art)))))
  489. (cdr art)))
  490. (setq articles (delq art articles))
  491. (push (car art) new))))
  492. ;; Go through all the new articles and add them, and their
  493. ;; time-stamps, to the list.
  494. (setq articles
  495. (nconc articles
  496. (mapcar
  497. (lambda (art)
  498. (cons art
  499. (nth 5 (file-attributes
  500. (concat dir (int-to-string art))))))
  501. new)))
  502. ;; Make Gnus mark all new articles as unread.
  503. (when new
  504. (gnus-make-articles-unread
  505. (gnus-group-prefixed-name group (list 'nnmh ""))
  506. (setq new (sort new '<))))
  507. ;; Sort the article list with highest numbers first.
  508. (setq articles (sort articles (lambda (art1 art2)
  509. (> (car art1) (car art2)))))
  510. ;; Finally write this list back to the .nnmh-articles file.
  511. (with-temp-file nnmh-file
  512. (insert ";; Gnus article active file for " group "\n\n")
  513. (insert "(setq nnmh-newsgroup-articles '")
  514. (gnus-prin1 articles)
  515. (insert ")\n"))))
  516. (defun nnmh-deletable-article-p (group article)
  517. "Say whether ARTICLE in GROUP can be deleted."
  518. (let ((path (concat nnmh-current-directory (int-to-string article))))
  519. ;; Writable.
  520. (and (file-writable-p path)
  521. (or
  522. ;; We can never delete the last article in the group.
  523. (not (eq (cdr (nth 1 (assoc group nnmh-group-alist)))
  524. article))
  525. ;; Well, we can.
  526. nnmh-allow-delete-final))))
  527. (provide 'nnmh)
  528. ;;; nnmh.el ends here