1 ;;; gnus-draft.el --- draft message support for Gnus
3 ;; Copyright (C) 1997-2016 Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
33 (eval-when-compile (require 'cl
))
37 (defvar gnus-draft-mode-map
38 (let ((map (make-sparse-keymap)))
40 "Dt" gnus-draft-toggle-sending
41 "e" gnus-draft-edit-message
;; Use `B w' for `gnus-summary-edit-article'
42 "De" gnus-draft-edit-message
43 "Ds" gnus-draft-send-message
44 "DS" gnus-draft-send-all-messages
)
47 (defun gnus-draft-make-menu-bar ()
48 (unless (boundp 'gnus-draft-menu
)
50 gnus-draft-menu gnus-draft-mode-map
""
52 ["Toggle whether to send" gnus-draft-toggle-sending t
]
53 ["Edit" gnus-draft-edit-message t
]
54 ["Send selected message(s)" gnus-draft-send-message t
]
55 ["Send all messages" gnus-draft-send-all-messages t
]
56 ["Delete draft" gnus-summary-delete-article t
]))))
58 (define-minor-mode gnus-draft-mode
59 "Minor mode for providing a draft summary buffers.
61 \\{gnus-draft-mode-map}"
62 :lighter
" Draft" :keymap gnus-draft-mode-map
64 ((not (derived-mode-p 'gnus-summary-mode
)) (setq gnus-draft-mode nil
))
67 (when (gnus-visual-p 'draft-menu
'menu
)
68 (gnus-draft-make-menu-bar))
69 (add-hook 'gnus-summary-prepare-exit-hook
'gnus-draft-clear-marks t t
))))
73 (defun gnus-draft-toggle-sending (article)
74 "Toggle whether to send an article or not."
75 (interactive (list (gnus-summary-article-number)))
76 (if (gnus-draft-article-sendable-p article
)
78 (push article gnus-newsgroup-unsendable
)
79 (gnus-summary-mark-article article gnus-unsendable-mark
))
80 (setq gnus-newsgroup-unsendable
81 (delq article gnus-newsgroup-unsendable
))
82 (gnus-summary-mark-article article gnus-unread-mark
))
83 (gnus-summary-position-point))
85 (defun gnus-draft-edit-message ()
86 "Enter a mail/post buffer to edit and send the draft."
88 (let ((article (gnus-summary-article-number))
89 (group gnus-newsgroup-name
))
90 (gnus-draft-check-draft-articles (list article
))
91 (gnus-summary-mark-as-read article gnus-canceled-mark
)
92 (gnus-draft-setup article group t
)
93 (set-buffer-modified-p t
)
96 (message-narrow-to-headers)
97 (message-remove-header "date")))
98 (let ((message-draft-headers
99 (delq 'Date
(copy-sequence message-draft-headers
))))
101 (let ((gnus-verbose-backends nil
))
102 (gnus-request-expire-articles (list article
) group t
))
105 (when (gnus-buffer-exists-p ,gnus-summary-buffer
)
107 (set-buffer ,gnus-summary-buffer
)
108 (gnus-cache-possibly-remove-article ,article nil nil nil t
)))))
109 message-send-actions
)))
111 (defun gnus-draft-send-message (&optional n
)
112 "Send the current draft(s).
113 Obeys the standard process/prefix convention."
115 (let* ((articles (gnus-summary-work-articles n
))
116 (total (length articles
))
118 (gnus-draft-check-draft-articles articles
)
119 (while (setq article
(pop articles
))
120 (gnus-summary-remove-process-mark article
)
121 (unless (memq article gnus-newsgroup-unsendable
)
122 (let ((message-sending-message
123 (format "Sending message %d of %d..."
124 (- total
(length articles
)) total
)))
125 (gnus-draft-send article gnus-newsgroup-name t
))
126 (gnus-summary-mark-article article gnus-canceled-mark
)))))
128 (defun gnus-draft-send (article &optional group interactive
)
129 "Send message ARTICLE."
130 (let* ((is-queue (or (not group
)
131 (equal group
"nndraft:queue")))
132 (message-syntax-checks (if interactive message-syntax-checks
133 'dont-check-for-anything-just-trust-me
))
134 (message-hidden-headers nil
)
135 (message-inhibit-body-encoding (or is-queue
136 message-inhibit-body-encoding
))
137 (message-send-hook (and (not is-queue
)
139 (message-setup-hook (and (not is-queue
)
141 (gnus-message-setup-hook (and (not is-queue
)
142 gnus-message-setup-hook
))
143 (message-signature (and (not is-queue
)
145 (gnus-agent-queue-mail (and (not is-queue
)
146 gnus-agent-queue-mail
))
147 (rfc2047-encode-encoded-words nil
)
149 (gnus-draft-setup article
(or group
"nndraft:queue") nil
'dont-pop
)
150 ;; We read the meta-information that says how and where
151 ;; this message is to be sent.
153 (message-narrow-to-headers)
154 (when (re-search-forward
155 (concat "^" (regexp-quote gnus-agent-target-move-group-header
)
157 (skip-syntax-forward "-")
158 (setq move-to
(buffer-substring (point) (point-at-eol)))
159 (message-remove-header gnus-agent-target-move-group-header
))
160 (goto-char (point-min))
161 (when (re-search-forward
162 (concat "^" (regexp-quote gnus-agent-meta-information-header
) ":")
164 (setq type
(ignore-errors (read (current-buffer)))
165 method
(ignore-errors (read (current-buffer))))
166 (message-remove-header gnus-agent-meta-information-header
)))
167 ;; Let Agent restore any GCC lines and have message.el perform them.
168 (gnus-agent-restore-gcc)
169 ;; Then we send it. If we have no meta-information, we just send
170 ;; it and let Message figure out how.
171 (when (and (or (null method
)
172 (gnus-server-opened method
)
173 (gnus-open-server method
))
175 (let ((message-this-is-news (eq type
'news
))
176 (message-this-is-mail (eq type
'mail
))
177 (gnus-post-method method
)
178 (message-post-method method
))
180 (gnus-inews-do-gcc move-to
)
181 (message-send-and-exit)))
183 (gnus-inews-do-gcc move-to
)
184 (message-send-and-exit))))
185 (let ((gnus-verbose-backends nil
))
186 (gnus-request-expire-articles
187 (list article
) (or group
"nndraft:queue") t
)))))
189 (defun gnus-draft-send-all-messages ()
190 "Send all the sendable drafts."
195 "Send all drafts? "))
196 (gnus-uu-mark-buffer)
197 (gnus-draft-send-message)))
199 (defun gnus-group-send-queue ()
200 "Send all sendable articles from the queue group."
202 (when (or gnus-plugged
203 (not gnus-agent-prompt-send-queue
)
204 (gnus-y-or-n-p "Gnus is unplugged; really send queue? "))
205 (gnus-activate-group "nndraft:queue")
207 (let* ((articles (nndraft-articles))
208 (unsendable (gnus-uncompress-range
211 (gnus-get-info "nndraft:queue"))))))
212 (gnus-posting-styles nil
)
213 message-send-mail-partially-limit
214 (total (length articles
))
216 (while (setq article
(pop articles
))
217 (unless (memq article unsendable
)
218 (let ((message-sending-message
219 (format "Sending message %d of %d..."
220 (- total
(length articles
)) total
)))
221 (gnus-draft-send article
))))))
222 (gnus-group-refresh-group "nndraft:queue")))
225 (defun gnus-draft-reminder ()
226 "Reminder user if there are unsent drafts."
231 (dolist (group '("nndraft:drafts" "nndraft:queue"))
232 (setq active
(gnus-activate-group group
))
233 (if (and active
(>= (cdr active
) (car active
)))
234 (if (y-or-n-p "There are unsent drafts. Confirm to exit? ")
236 (error "Stop!"))))))))
238 (defcustom gnus-draft-setup-hook nil
239 "Hook run after setting up a draft buffer."
241 :version
"23.1" ;; No Gnus
245 (defun gnus-draft-setup (narticle group
&optional restore dont-pop
)
246 "Setup a mail draft buffer.
247 If DONT-POP is nil, display the buffer after setting it up."
249 (gnus-setup-message 'forward
250 (let ((article narticle
))
251 (message-mail nil nil nil nil
253 (lambda (buf) (set-buffer (get-buffer-create buf
)))))
254 (let ((inhibit-read-only t
))
256 (if (not (gnus-request-restore-buffer article group
))
257 (error "Couldn't restore the article")
259 (equal group
"nndraft:queue"))
261 ;; Insert the separator.
262 (goto-char (point-min))
263 (search-forward "\n\n")
266 (narrow-to-region (point-min) (point))
268 (message-fetch-field gnus-draft-meta-information-header
)))
269 (insert mail-header-separator
)
271 (message-set-auto-save-file-name))))
272 (gnus-backlog-remove-article group narticle
)
274 (ignore-errors (setq ga
(car (read-from-string ga
)))))
275 (setq gnus-newsgroup-name
276 (if (equal (car ga
) "") nil
(car ga
)))
277 (gnus-configure-posting-styles)
278 (setq gnus-message-group-art
(cons gnus-newsgroup-name
(cadr ga
)))
279 (setq message-post-method
281 (gnus-post-method arg
,(car ga
))))
282 (unless (equal (cadr ga
) "")
283 (dolist (article (cdr ga
))
286 (gnus-add-mark ,(car ga
) 'replied
,article
)
287 (gnus-request-set-mark ,(car ga
) (list (list (list ,article
)
290 (run-hooks 'gnus-draft-setup-hook
)))
292 (defun gnus-draft-article-sendable-p (article)
293 "Say whether ARTICLE is sendable."
294 (not (memq article gnus-newsgroup-unsendable
)))
296 (defun gnus-draft-check-draft-articles (articles)
297 "Check whether the draft articles ARTICLES are under edit."
298 (when (equal gnus-newsgroup-name
"nndraft:drafts")
299 (let ((buffers (buffer-list))
304 (setq file
(nndraft-article-filename (pop articles
))
307 (set-buffer (setq buff
(pop buffs
)))
308 (if (and buffer-file-name
309 (equal (file-remote-p file
)
310 (file-remote-p buffer-file-name
))
311 (string-equal (file-truename buffer-file-name
)
312 (file-truename file
))
317 (let* ((window (get-buffer-window buff t
))
318 (frame (and window
(window-frame window
))))
320 (select-frame-set-input-focus frame
)
321 (pop-to-buffer buff t
)))
322 (error "The draft %s is under edit" file
)))))
324 (defun gnus-draft-clear-marks ()
325 (setq gnus-newsgroup-reads nil
326 gnus-newsgroup-marked nil
327 gnus-newsgroup-unreads
(nndraft-articles)))
329 (provide 'gnus-draft
)
331 ;;; gnus-draft.el ends here