gnus-draft.el 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. ;;; gnus-draft.el --- draft message support for Gnus
  2. ;; Copyright (C) 1997-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. (require 'gnus)
  19. (require 'gnus-sum)
  20. (require 'message)
  21. (require 'gnus-msg)
  22. (require 'nndraft)
  23. (require 'gnus-agent)
  24. (eval-when-compile (require 'cl))
  25. (eval-when-compile
  26. (when (featurep 'xemacs)
  27. (require 'easy-mmode))) ; for `define-minor-mode'
  28. ;;; Draft minor mode
  29. (defvar gnus-draft-mode-map
  30. (let ((map (make-sparse-keymap)))
  31. (gnus-define-keys map
  32. "Dt" gnus-draft-toggle-sending
  33. "e" gnus-draft-edit-message ;; Use `B w' for `gnus-summary-edit-article'
  34. "De" gnus-draft-edit-message
  35. "Ds" gnus-draft-send-message
  36. "DS" gnus-draft-send-all-messages)
  37. map))
  38. (defun gnus-draft-make-menu-bar ()
  39. (unless (boundp 'gnus-draft-menu)
  40. (easy-menu-define
  41. gnus-draft-menu gnus-draft-mode-map ""
  42. '("Drafts"
  43. ["Toggle whether to send" gnus-draft-toggle-sending t]
  44. ["Edit" gnus-draft-edit-message t]
  45. ["Send selected message(s)" gnus-draft-send-message t]
  46. ["Send all messages" gnus-draft-send-all-messages t]
  47. ["Delete draft" gnus-summary-delete-article t]))))
  48. (define-minor-mode gnus-draft-mode
  49. "Minor mode for providing a draft summary buffers.
  50. \\{gnus-draft-mode-map}"
  51. :lighter " Draft" :keymap gnus-draft-mode-map
  52. (cond
  53. ((not (derived-mode-p 'gnus-summary-mode)) (setq gnus-draft-mode nil))
  54. (gnus-draft-mode
  55. ;; Set up the menu.
  56. (when (gnus-visual-p 'draft-menu 'menu)
  57. (gnus-draft-make-menu-bar))
  58. (add-hook 'gnus-summary-prepare-exit-hook 'gnus-draft-clear-marks t t))))
  59. ;;; Commands
  60. (defun gnus-draft-toggle-sending (article)
  61. "Toggle whether to send an article or not."
  62. (interactive (list (gnus-summary-article-number)))
  63. (if (gnus-draft-article-sendable-p article)
  64. (progn
  65. (push article gnus-newsgroup-unsendable)
  66. (gnus-summary-mark-article article gnus-unsendable-mark))
  67. (setq gnus-newsgroup-unsendable
  68. (delq article gnus-newsgroup-unsendable))
  69. (gnus-summary-mark-article article gnus-unread-mark))
  70. (gnus-summary-position-point))
  71. (defun gnus-draft-edit-message ()
  72. "Enter a mail/post buffer to edit and send the draft."
  73. (interactive)
  74. (let ((article (gnus-summary-article-number))
  75. (group gnus-newsgroup-name))
  76. (gnus-draft-check-draft-articles (list article))
  77. (gnus-summary-mark-as-read article gnus-canceled-mark)
  78. (gnus-draft-setup article group t)
  79. (set-buffer-modified-p t)
  80. (save-excursion
  81. (save-restriction
  82. (message-narrow-to-headers)
  83. (message-remove-header "date")))
  84. (let ((message-draft-headers
  85. (delq 'Date (copy-sequence message-draft-headers))))
  86. (save-buffer))
  87. (let ((gnus-verbose-backends nil))
  88. (gnus-request-expire-articles (list article) group t))
  89. (push
  90. `((lambda ()
  91. (when (gnus-buffer-exists-p ,gnus-summary-buffer)
  92. (save-excursion
  93. (set-buffer ,gnus-summary-buffer)
  94. (gnus-cache-possibly-remove-article ,article nil nil nil t)))))
  95. message-send-actions)))
  96. (defun gnus-draft-send-message (&optional n)
  97. "Send the current draft(s).
  98. Obeys the standard process/prefix convention."
  99. (interactive "P")
  100. (let* ((articles (gnus-summary-work-articles n))
  101. (total (length articles))
  102. article)
  103. (gnus-draft-check-draft-articles articles)
  104. (while (setq article (pop articles))
  105. (gnus-summary-remove-process-mark article)
  106. (unless (memq article gnus-newsgroup-unsendable)
  107. (let ((message-sending-message
  108. (format "Sending message %d of %d..."
  109. (- total (length articles)) total)))
  110. (gnus-draft-send article gnus-newsgroup-name t))
  111. (gnus-summary-mark-article article gnus-canceled-mark)))))
  112. (defun gnus-draft-send (article &optional group interactive)
  113. "Send message ARTICLE."
  114. (let* ((is-queue (or (not group)
  115. (equal group "nndraft:queue")))
  116. (message-syntax-checks (if interactive message-syntax-checks
  117. 'dont-check-for-anything-just-trust-me))
  118. (message-hidden-headers nil)
  119. (message-inhibit-body-encoding (or is-queue
  120. message-inhibit-body-encoding))
  121. (message-send-hook (and (not is-queue)
  122. message-send-hook))
  123. (message-setup-hook (and (not is-queue)
  124. message-setup-hook))
  125. (gnus-message-setup-hook (and (not is-queue)
  126. gnus-message-setup-hook))
  127. (message-signature (and (not is-queue)
  128. message-signature))
  129. (gnus-agent-queue-mail (and (not is-queue)
  130. gnus-agent-queue-mail))
  131. (rfc2047-encode-encoded-words nil)
  132. type method move-to)
  133. (gnus-draft-setup article (or group "nndraft:queue") nil 'dont-pop)
  134. ;; We read the meta-information that says how and where
  135. ;; this message is to be sent.
  136. (save-restriction
  137. (message-narrow-to-headers)
  138. (when (re-search-forward
  139. (concat "^" (regexp-quote gnus-agent-target-move-group-header)
  140. ":") nil t)
  141. (skip-syntax-forward "-")
  142. (setq move-to (buffer-substring (point) (point-at-eol)))
  143. (message-remove-header gnus-agent-target-move-group-header))
  144. (goto-char (point-min))
  145. (when (re-search-forward
  146. (concat "^" (regexp-quote gnus-agent-meta-information-header) ":")
  147. nil t)
  148. (setq type (ignore-errors (read (current-buffer)))
  149. method (ignore-errors (read (current-buffer))))
  150. (message-remove-header gnus-agent-meta-information-header)))
  151. ;; Let Agent restore any GCC lines and have message.el perform them.
  152. (gnus-agent-restore-gcc)
  153. ;; Then we send it. If we have no meta-information, we just send
  154. ;; it and let Message figure out how.
  155. (when (and (or (null method)
  156. (gnus-server-opened method)
  157. (gnus-open-server method))
  158. (if type
  159. (let ((message-this-is-news (eq type 'news))
  160. (message-this-is-mail (eq type 'mail))
  161. (gnus-post-method method)
  162. (message-post-method method))
  163. (if move-to
  164. (gnus-inews-do-gcc move-to)
  165. (message-send-and-exit)))
  166. (if move-to
  167. (gnus-inews-do-gcc move-to)
  168. (message-send-and-exit))))
  169. (let ((gnus-verbose-backends nil))
  170. (gnus-request-expire-articles
  171. (list article) (or group "nndraft:queue") t)))))
  172. (defun gnus-draft-send-all-messages ()
  173. "Send all the sendable drafts."
  174. (interactive)
  175. (when (or
  176. gnus-expert-user
  177. (gnus-y-or-n-p
  178. "Send all drafts? "))
  179. (gnus-uu-mark-buffer)
  180. (gnus-draft-send-message)))
  181. (defun gnus-group-send-queue ()
  182. "Send all sendable articles from the queue group."
  183. (interactive)
  184. (when (or gnus-plugged
  185. (not gnus-agent-prompt-send-queue)
  186. (gnus-y-or-n-p "Gnus is unplugged; really send queue? "))
  187. (gnus-activate-group "nndraft:queue")
  188. (save-excursion
  189. (let* ((articles (nndraft-articles))
  190. (unsendable (gnus-uncompress-range
  191. (cdr (assq 'unsend
  192. (gnus-info-marks
  193. (gnus-get-info "nndraft:queue"))))))
  194. (gnus-posting-styles nil)
  195. message-send-mail-partially-limit
  196. (total (length articles))
  197. article)
  198. (while (setq article (pop articles))
  199. (unless (memq article unsendable)
  200. (let ((message-sending-message
  201. (format "Sending message %d of %d..."
  202. (- total (length articles)) total)))
  203. (gnus-draft-send article))))))
  204. (gnus-group-refresh-group "nndraft:queue")))
  205. ;;;###autoload
  206. (defun gnus-draft-reminder ()
  207. "Reminder user if there are unsent drafts."
  208. (interactive)
  209. (if (gnus-alive-p)
  210. (let (active)
  211. (catch 'continue
  212. (dolist (group '("nndraft:drafts" "nndraft:queue"))
  213. (setq active (gnus-activate-group group))
  214. (if (and active (>= (cdr active) (car active)))
  215. (if (y-or-n-p "There are unsent drafts. Confirm to exit? ")
  216. (throw 'continue t)
  217. (error "Stop!"))))))))
  218. (defcustom gnus-draft-setup-hook nil
  219. "Hook run after setting up a draft buffer."
  220. :group 'gnus-message
  221. :version "23.1" ;; No Gnus
  222. :type 'hook)
  223. (defun gnus-draft-setup (narticle group &optional restore dont-pop)
  224. "Setup a mail draft buffer.
  225. If DONT-POP is nil, display the buffer after setting it up."
  226. (let (ga)
  227. (gnus-setup-message 'forward
  228. (let ((article narticle))
  229. (message-mail nil nil nil nil
  230. (if dont-pop
  231. (lambda (buf) (set-buffer (get-buffer-create buf)))))
  232. (let ((inhibit-read-only t))
  233. (erase-buffer))
  234. (if (not (gnus-request-restore-buffer article group))
  235. (error "Couldn't restore the article")
  236. (when (and restore
  237. (equal group "nndraft:queue"))
  238. (mime-to-mml))
  239. ;; Insert the separator.
  240. (goto-char (point-min))
  241. (search-forward "\n\n")
  242. (forward-char -1)
  243. (save-restriction
  244. (narrow-to-region (point-min) (point))
  245. (setq ga
  246. (message-fetch-field gnus-draft-meta-information-header)))
  247. (insert mail-header-separator)
  248. (forward-line 1)
  249. (message-set-auto-save-file-name))))
  250. (gnus-backlog-remove-article group narticle)
  251. (when (and ga
  252. (ignore-errors (setq ga (car (read-from-string ga)))))
  253. (setq gnus-newsgroup-name
  254. (if (equal (car ga) "") nil (car ga)))
  255. (gnus-configure-posting-styles)
  256. (setq gnus-message-group-art (cons gnus-newsgroup-name (cadr ga)))
  257. (setq message-post-method
  258. `(lambda (arg)
  259. (gnus-post-method arg ,(car ga))))
  260. (unless (equal (cadr ga) "")
  261. (dolist (article (cdr ga))
  262. (message-add-action
  263. `(progn
  264. (gnus-add-mark ,(car ga) 'replied ,article)
  265. (gnus-request-set-mark ,(car ga) (list (list (list ,article)
  266. 'add '(reply)))))
  267. 'send))))
  268. (run-hooks 'gnus-draft-setup-hook)))
  269. (defun gnus-draft-article-sendable-p (article)
  270. "Say whether ARTICLE is sendable."
  271. (not (memq article gnus-newsgroup-unsendable)))
  272. (defun gnus-draft-check-draft-articles (articles)
  273. "Check whether the draft articles ARTICLES are under edit."
  274. (when (equal gnus-newsgroup-name "nndraft:drafts")
  275. (let ((buffers (buffer-list))
  276. file buffs buff)
  277. (save-current-buffer
  278. (while (and articles
  279. (not buff))
  280. (setq file (nndraft-article-filename (pop articles))
  281. buffs buffers)
  282. (while buffs
  283. (set-buffer (setq buff (pop buffs)))
  284. (if (and buffer-file-name
  285. (equal (file-remote-p file)
  286. (file-remote-p buffer-file-name))
  287. (string-equal (file-truename buffer-file-name)
  288. (file-truename file))
  289. (buffer-modified-p))
  290. (setq buffs nil)
  291. (setq buff nil)))))
  292. (when buff
  293. (let* ((window (get-buffer-window buff t))
  294. (frame (and window (window-frame window))))
  295. (if frame
  296. (gnus-select-frame-set-input-focus frame)
  297. (pop-to-buffer buff t)))
  298. (error "The draft %s is under edit" file)))))
  299. (defun gnus-draft-clear-marks ()
  300. (setq gnus-newsgroup-reads nil
  301. gnus-newsgroup-marked nil
  302. gnus-newsgroup-unreads (nndraft-articles)))
  303. (provide 'gnus-draft)
  304. ;;; gnus-draft.el ends here