nndraft.el 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. ;;; nndraft.el --- draft article access for Gnus
  2. ;; Copyright (C) 1995-2015 Free Software Foundation, Inc.
  3. ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
  4. ;; Keywords: news
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;;; Code:
  18. (require 'nnheader)
  19. (require 'nnmail)
  20. (require 'gnus-start)
  21. (require 'gnus-group)
  22. (require 'nnmh)
  23. (require 'nnoo)
  24. (require 'mm-util)
  25. (eval-when-compile (require 'cl))
  26. ;; The nnoo-import at the end, I think.
  27. (declare-function nndraft-request-list "nndraft" (&rest args) t)
  28. (nnoo-declare nndraft
  29. nnmh)
  30. (defvoo nndraft-directory (nnheader-concat gnus-directory "drafts/")
  31. "Where nndraft will store its files."
  32. nnmh-directory)
  33. (defvar nndraft-required-headers '(Date)
  34. "*Headers to be generated when saving a draft message.
  35. The headers in this variable and the ones in `message-required-headers'
  36. are generated if and only if they are also in `message-draft-headers'.")
  37. (defvoo nndraft-current-group "" nil nnmh-current-group)
  38. (defvoo nndraft-get-new-mail nil nil nnmh-get-new-mail)
  39. (defvoo nndraft-current-directory nil nil nnmh-current-directory)
  40. (defconst nndraft-version "nndraft 1.0")
  41. (defvoo nndraft-status-string "" nil nnmh-status-string)
  42. ;;; Interface functions.
  43. (nnoo-define-basics nndraft)
  44. (deffoo nndraft-open-server (server &optional defs)
  45. (nnoo-change-server 'nndraft server defs)
  46. (cond
  47. ((not (file-exists-p nndraft-directory))
  48. (nndraft-close-server)
  49. (nnheader-report 'nndraft "No such file or directory: %s"
  50. nndraft-directory))
  51. ((not (file-directory-p (file-truename nndraft-directory)))
  52. (nndraft-close-server)
  53. (nnheader-report 'nndraft "Not a directory: %s" nndraft-directory))
  54. (t
  55. (nnheader-report 'nndraft "Opened server %s using directory %s"
  56. server nndraft-directory)
  57. t)))
  58. (deffoo nndraft-retrieve-headers (articles &optional group server fetch-old)
  59. (nndraft-possibly-change-group group)
  60. (with-current-buffer nntp-server-buffer
  61. (erase-buffer)
  62. (let (article lines chars)
  63. ;; We don't support fetching by Message-ID.
  64. (if (stringp (car articles))
  65. 'headers
  66. (while articles
  67. (narrow-to-region (point) (point))
  68. (when (nndraft-request-article
  69. (setq article (pop articles)) group server (current-buffer))
  70. (goto-char (point-min))
  71. (if (search-forward "\n\n" nil t)
  72. (forward-line -1)
  73. (goto-char (point-max)))
  74. (setq lines (count-lines (point) (point-max))
  75. chars (- (point-max) (point)))
  76. (delete-region (point) (point-max))
  77. (goto-char (point-min))
  78. (insert (format "221 %d Article retrieved.\n" article))
  79. (insert (format "Lines: %d\nChars: %d\n" lines chars))
  80. (widen)
  81. (goto-char (point-max))
  82. (insert ".\n")))
  83. (nnheader-fold-continuation-lines)
  84. 'headers))))
  85. (deffoo nndraft-request-article (id &optional group server buffer)
  86. (nndraft-possibly-change-group group)
  87. (when (numberp id)
  88. ;; We get the newest file of the auto-saved file and the
  89. ;; "real" file.
  90. (let* ((file (nndraft-article-filename id))
  91. (auto (nndraft-auto-save-file-name file))
  92. (newest (if (file-newer-than-file-p file auto) file auto))
  93. (nntp-server-buffer (or buffer nntp-server-buffer)))
  94. (when (and (file-exists-p newest)
  95. (let ((nnmail-file-coding-system
  96. (if (file-newer-than-file-p file auto)
  97. (if (member group '("drafts" "delayed"))
  98. message-draft-coding-system
  99. mm-text-coding-system)
  100. mm-auto-save-coding-system)))
  101. (nnmail-find-file newest)))
  102. (with-current-buffer nntp-server-buffer
  103. (goto-char (point-min))
  104. ;; If there's a mail header separator in this file,
  105. ;; we remove it.
  106. (when (re-search-forward
  107. (concat "^" (regexp-quote mail-header-separator) "$") nil t)
  108. (replace-match "" t t)))
  109. t))))
  110. (deffoo nndraft-request-restore-buffer (article &optional group server)
  111. "Request a new buffer that is restored to the state of ARTICLE."
  112. (nndraft-possibly-change-group group)
  113. (when (nndraft-request-article article group server (current-buffer))
  114. (message-remove-header "xref")
  115. (message-remove-header "lines")
  116. ;; Articles in nndraft:queue are considered as sent messages. The
  117. ;; Date field should be the time when they are sent.
  118. ;;(message-remove-header "date")
  119. t))
  120. (deffoo nndraft-request-update-info (group info &optional server)
  121. (nndraft-possibly-change-group group)
  122. (gnus-info-set-read
  123. info
  124. (gnus-update-read-articles (gnus-group-prefixed-name group '(nndraft ""))
  125. (nndraft-articles) t))
  126. (let ((marks (nth 3 info)))
  127. (when marks
  128. ;; Nix out all marks except the `unsend'-able article marks.
  129. (setcar (nthcdr 3 info)
  130. (if (assq 'unsend marks)
  131. (list (assq 'unsend marks))
  132. nil))))
  133. t)
  134. (defun nndraft-generate-headers ()
  135. (save-excursion
  136. (message-generate-headers
  137. (message-headers-to-generate
  138. nndraft-required-headers message-draft-headers nil))))
  139. (defun nndraft-update-unread-articles ()
  140. "Update groups' unread articles in the group buffer."
  141. (nndraft-request-list)
  142. (with-current-buffer gnus-group-buffer
  143. (let* ((groups (mapcar (lambda (elem)
  144. (gnus-group-prefixed-name (car elem)
  145. (list 'nndraft "")))
  146. (nnmail-get-active)))
  147. (gnus-group-marked (copy-sequence groups))
  148. ;; Don't send delayed articles.
  149. (gnus-get-new-news-hook nil)
  150. (inhibit-read-only t))
  151. (gnus-group-get-new-news-this-group nil t)
  152. (save-excursion
  153. (dolist (group groups)
  154. (unless (and gnus-permanently-visible-groups
  155. (string-match gnus-permanently-visible-groups
  156. group))
  157. (gnus-group-goto-group group)
  158. (when (zerop (gnus-group-group-unread))
  159. (gnus-delete-line))))))))
  160. (deffoo nndraft-request-associate-buffer (group)
  161. "Associate the current buffer with some article in the draft group."
  162. (nndraft-open-server "")
  163. (nndraft-request-group group)
  164. (nndraft-possibly-change-group group)
  165. (let ((gnus-verbose-backends nil)
  166. (buf (current-buffer))
  167. article file)
  168. (with-temp-buffer
  169. (insert-buffer-substring buf)
  170. (setq article (nndraft-request-accept-article
  171. group (nnoo-current-server 'nndraft) t 'noinsert)
  172. file (nndraft-article-filename article)))
  173. (setq buffer-file-name (expand-file-name file)
  174. buffer-auto-save-file-name (make-auto-save-file-name))
  175. (clear-visited-file-modtime)
  176. (let ((hook (if (boundp 'write-contents-functions)
  177. 'write-contents-functions
  178. 'write-contents-hooks)))
  179. (gnus-make-local-hook hook)
  180. (add-hook hook 'nndraft-generate-headers nil t))
  181. (gnus-make-local-hook 'after-save-hook)
  182. (add-hook 'after-save-hook 'nndraft-update-unread-articles nil t)
  183. (message-add-action '(nndraft-update-unread-articles)
  184. 'exit 'postpone 'kill)
  185. article))
  186. (deffoo nndraft-request-group (group &optional server dont-check info)
  187. (nndraft-possibly-change-group group)
  188. (unless dont-check
  189. (let* ((pathname (nnmail-group-pathname group nndraft-directory))
  190. (file-name-coding-system nnmail-pathname-coding-system)
  191. dir file)
  192. (nnheader-re-read-dir pathname)
  193. (setq dir (mapcar (lambda (name) (string-to-number (substring name 1)))
  194. (ignore-errors (directory-files
  195. pathname nil "^#[0-9]+#$" t))))
  196. (dolist (n dir)
  197. (unless (file-exists-p
  198. (setq file (expand-file-name (int-to-string n) pathname)))
  199. (rename-file (nndraft-auto-save-file-name file) file)))))
  200. (nnoo-parent-function 'nndraft
  201. 'nnmh-request-group
  202. (list group server dont-check)))
  203. (deffoo nndraft-request-move-article (article group server accept-form
  204. &optional last move-is-internal)
  205. (nndraft-possibly-change-group group)
  206. (let ((buf (get-buffer-create " *nndraft move*"))
  207. result)
  208. (and
  209. (nndraft-request-article article group server)
  210. (with-current-buffer buf
  211. (erase-buffer)
  212. (insert-buffer-substring nntp-server-buffer)
  213. (setq result (eval accept-form))
  214. (kill-buffer (current-buffer))
  215. result)
  216. (null (nndraft-request-expire-articles (list article) group server 'force))
  217. result)))
  218. (deffoo nndraft-request-expire-articles (articles group &optional server force)
  219. (nndraft-possibly-change-group group)
  220. (let* ((nnmh-allow-delete-final t)
  221. (nnmail-expiry-target 'delete)
  222. ;; FIXME: If we want to move a draft message to an expiry group,
  223. ;; there are things to have to improve:
  224. ;; - Remove a header separator.
  225. ;; - Encode it, including attachments, into a MIME message.
  226. ;;(nnmail-expiry-target
  227. ;; (or (gnus-group-find-parameter
  228. ;; (gnus-group-prefixed-name group (list 'nndraft server))
  229. ;; 'expiry-target t)
  230. ;; nnmail-expiry-target))
  231. (res (nnoo-parent-function 'nndraft
  232. 'nnmh-request-expire-articles
  233. (list articles group server force)))
  234. article)
  235. ;; Delete all the "state" files of articles that have been expired.
  236. (while articles
  237. (unless (memq (setq article (pop articles)) res)
  238. (let ((auto (nndraft-auto-save-file-name
  239. (nndraft-article-filename article))))
  240. (when (file-exists-p auto)
  241. (funcall nnmail-delete-file-function auto)))
  242. (dolist (backup
  243. (let ((kept-new-versions 1)
  244. (kept-old-versions 0))
  245. (find-backup-file-name
  246. (nndraft-article-filename article))))
  247. (when (file-exists-p backup)
  248. (funcall nnmail-delete-file-function backup)))))
  249. res))
  250. (deffoo nndraft-request-accept-article (group &optional server last noinsert)
  251. (nndraft-possibly-change-group group)
  252. (let ((gnus-verbose-backends nil))
  253. (nnoo-parent-function 'nndraft 'nnmh-request-accept-article
  254. (list group server last noinsert))))
  255. (deffoo nndraft-request-replace-article (article group buffer)
  256. (nndraft-possibly-change-group group)
  257. (let ((nnmail-file-coding-system
  258. (if (member group '("drafts" "delayed"))
  259. message-draft-coding-system
  260. mm-text-coding-system)))
  261. (nnoo-parent-function 'nndraft 'nnmh-request-replace-article
  262. (list article group buffer))))
  263. (deffoo nndraft-request-create-group (group &optional server args)
  264. (nndraft-possibly-change-group group)
  265. (if (file-exists-p nndraft-current-directory)
  266. (if (file-directory-p nndraft-current-directory)
  267. t
  268. nil)
  269. (condition-case ()
  270. (progn
  271. (gnus-make-directory nndraft-current-directory)
  272. t)
  273. (file-error nil))))
  274. ;;; Low-Level Interface
  275. (defun nndraft-possibly-change-group (group)
  276. (when (and group
  277. (not (equal group nndraft-current-group)))
  278. (nndraft-open-server "")
  279. (setq nndraft-current-group group)
  280. (setq nndraft-current-directory
  281. (nnheader-concat nndraft-directory group))))
  282. (defun nndraft-article-filename (article &rest args)
  283. (apply 'concat
  284. (file-name-as-directory nndraft-current-directory)
  285. (int-to-string article)
  286. args))
  287. (defun nndraft-auto-save-file-name (file)
  288. (save-excursion
  289. (prog1
  290. (progn
  291. (set-buffer (get-buffer-create " *draft tmp*"))
  292. (setq buffer-file-name file)
  293. (make-auto-save-file-name))
  294. (kill-buffer (current-buffer)))))
  295. (defun nndraft-articles ()
  296. "Return the list of messages in the group."
  297. (gnus-make-directory nndraft-current-directory)
  298. (sort
  299. (mapcar 'string-to-number
  300. (directory-files nndraft-current-directory nil "\\`[0-9]+\\'" t))
  301. '<))
  302. (nnoo-import nndraft
  303. (nnmh
  304. nnmh-retrieve-headers
  305. nnmh-request-group
  306. nnmh-close-group
  307. nnmh-request-list))
  308. (provide 'nndraft)
  309. ;;; nndraft.el ends here