nndraft.el 12 KB

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