1 ;;; notmuch-draft.el --- functions for postponing and editing drafts
3 ;; Copyright © Mark Walters
4 ;; Copyright © David Bremner
5 ;; Copyright © Leo Gaspard
7 ;; This file is part of Notmuch.
9 ;; Notmuch is free software: you can redistribute it and/or modify it
10 ;; under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
14 ;; Notmuch is distributed in the hope that it will be useful, but
15 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 ;; General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with Notmuch. If not, see <https://www.gnu.org/licenses/>.
22 ;; Authors: Mark Walters <markwalters1009@gmail.com>
23 ;; David Bremner <david@tethera.net>
24 ;; Leo Gaspard <leo@gaspard.io>
28 (require 'notmuch-maildir-fcc)
29 (require 'notmuch-tag)
31 (declare-function notmuch-show-get-message-id "notmuch-show" (&optional bare))
32 (declare-function notmuch-message-mode "notmuch-mua")
34 (defgroup notmuch-draft nil
35 "Saving and editing drafts in Notmuch."
38 (defcustom notmuch-draft-tags '("+draft")
39 "List of tags changes to apply to a draft message when it is saved in the database.
41 Tags starting with \"+\" (or not starting with either \"+\" or
42 \"-\") in the list will be added, and tags starting with \"-\"
43 will be removed from the message being stored.
45 For example, if you wanted to give the message a \"draft\" tag
46 but not the (normally added by default) \"inbox\" tag, you would
48 (\"+draft\" \"-inbox\")"
49 :type '(repeat string)
50 :group 'notmuch-draft)
52 (defcustom notmuch-draft-folder "drafts"
53 "Folder to save draft messages in.
55 This should be specified relative to the root of the notmuch
56 database. It will be created if necessary."
58 :group 'notmuch-draft)
60 (defcustom notmuch-draft-quoted-tags '()
63 This should be a list of mml tags to quote before saving. You do
64 not need to include \"secure\" as that is handled separately.
66 If you include \"part\" then attachments will not be saved with
67 the draft -- if not then they will be saved with the draft. The
68 former means the attachments may not still exist when you resume
69 the message, the latter means that the attachments as they were
70 when you postponed will be sent with the resumed message.
72 Note you may get strange results if you change this between
73 postponing and resuming a message."
74 :type '(repeat string)
77 (defcustom notmuch-draft-save-plaintext 'ask
78 "Should notmuch save/postpone in plaintext messages that seem
79 like they are intended to be sent encrypted
80 (i.e with an mml encryption tag in it)."
82 (const :tag "Never" nil)
83 (const :tag "Ask every time" ask)
84 (const :tag "Always" t))
86 :group 'notmuch-crypto)
88 (defvar notmuch-draft-encryption-tag-regex
89 "<#\\(part encrypt\\|secure.*mode=.*encrypt>\\)"
90 "Regular expression matching mml tags indicating encryption of part or message.")
92 (defvar notmuch-draft-id nil
93 "Message-id of the most recent saved draft of this message.")
94 (make-variable-buffer-local 'notmuch-draft-id)
96 (defun notmuch-draft--mark-deleted ()
97 "Tag the last saved draft deleted.
99 Used when a new version is saved, or the message is sent."
100 (when notmuch-draft-id
101 (notmuch-tag notmuch-draft-id '("+deleted"))))
103 (defun notmuch-draft-quote-some-mml ()
104 "Quote the mml tags in `notmuch-draft-quoted-tags`."
106 ;; First we deal with any secure tag separately.
108 (when (looking-at "<#secure[^\n]*>\n")
109 (let ((secure-tag (match-string 0)))
110 (delete-region (match-beginning 0) (match-end 0))
111 (message-add-header (concat "X-Notmuch-Emacs-Secure: " secure-tag))))
112 ;; This is copied from mml-quote-region but only quotes the
114 (when notmuch-draft-quoted-tags
115 (let ((re (concat "<#!*/?\\("
116 (mapconcat 'regexp-quote notmuch-draft-quoted-tags "\\|")
119 (while (re-search-forward re nil t)
120 ;; Insert ! after the #.
121 (goto-char (+ (match-beginning 0) 2))
124 (defun notmuch-draft-unquote-some-mml ()
125 "Unquote the mml tags in `notmuch-draft-quoted-tags`."
127 (when notmuch-draft-quoted-tags
128 (let ((re (concat "<#!+/?\\("
129 (mapconcat 'regexp-quote notmuch-draft-quoted-tags "\\|")
132 (while (re-search-forward re nil t)
133 ;; Remove one ! from after the #.
134 (goto-char (+ (match-beginning 0) 2))
138 (message-narrow-to-headers)
139 (setq secure-tag (message-fetch-field "X-Notmuch-Emacs-Secure" 't))
140 (message-remove-header "X-Notmuch-Emacs-Secure"))
143 (insert secure-tag "\n")))))
145 (defun notmuch-draft--has-encryption-tag ()
146 "Returns t if there is an mml secure tag."
149 (re-search-forward notmuch-draft-encryption-tag-regex nil 't)))
151 (defun notmuch-draft--query-encryption ()
152 "Checks if we should save a message that should be encrypted.
154 `notmuch-draft-save-plaintext' controls the behaviour."
155 (cl-case notmuch-draft-save-plaintext
158 "(Customize `notmuch-draft-save-plaintext' to avoid this warning)
159 This message contains mml tags that suggest it is intended to be encrypted.
160 Really save and index an unencrypted copy? ")
161 (error "Save aborted")))
163 (error "Refusing to save draft with encryption tags (see `%s')"
164 'notmuch-draft-save-plaintext))
168 (defun notmuch-draft--make-message-id ()
169 ;; message-make-message-id gives the id inside a "<" ">" pair,
170 ;; but notmuch doesn't want that form, so remove them.
171 (concat "draft-" (substring (message-make-message-id) 1 -1)))
173 (defun notmuch-draft-save ()
174 "Save the current draft message in the notmuch database.
176 This saves the current message in the database with tags
177 `notmuch-draft-tags` (in addition to any default tags
178 applied to newly inserted messages)."
180 (when (notmuch-draft--has-encryption-tag)
181 (notmuch-draft--query-encryption))
182 (let ((id (notmuch-draft--make-message-id)))
183 (with-temporary-notmuch-message-buffer
184 ;; We insert a Date header and a Message-ID header, the former
185 ;; so that it is easier to search for the message, and the
186 ;; latter so we have a way of accessing the saved message (for
187 ;; example to delete it at a later time). We check that the
188 ;; user has these in `message-deletable-headers` (the default)
189 ;; as otherwise they are doing something strange and we
190 ;; shouldn't interfere. Note, since we are doing this in a new
191 ;; buffer we don't change the version in the compose buffer.
193 ((member 'Message-ID message-deletable-headers)
194 (message-remove-header "Message-ID")
195 (message-add-header (concat "Message-ID: <" id ">")))
197 (message "You have customized emacs so Message-ID is not a %s"
198 "deletable header, so not changing it")
201 ((member 'Date message-deletable-headers)
202 (message-remove-header "Date")
203 (message-add-header (concat "Date: " (message-make-date))))
205 (message "You have customized emacs so Date is not a deletable %s"
206 "header, so not changing it")))
207 (message-add-header "X-Notmuch-Emacs-Draft: True")
208 (notmuch-draft-quote-some-mml)
209 (notmuch-maildir-setup-message-for-saving)
210 (notmuch-maildir-notmuch-insert-current-buffer
211 notmuch-draft-folder 't notmuch-draft-tags))
212 ;; We are now back in the original compose buffer. Note the
213 ;; function notmuch-call-notmuch-process (called by
214 ;; notmuch-maildir-notmuch-insert-current-buffer) signals an error
215 ;; on failure, so to get to this point it must have
216 ;; succeeded. Also, notmuch-draft-id is still the id of the
217 ;; previous draft, so it is safe to mark it deleted.
218 (notmuch-draft--mark-deleted)
219 (setq notmuch-draft-id (concat "id:" id))
220 (set-buffer-modified-p nil)))
222 (defun notmuch-draft-postpone ()
223 "Save the draft message in the notmuch database and exit buffer."
228 (defun notmuch-draft-resume (id)
229 "Resume editing of message with id ID."
230 (let* ((tags (process-lines notmuch-command "search" "--output=tags"
231 "--exclude=false" id))
232 (draft (equal tags (notmuch-update-tags tags notmuch-draft-tags))))
234 (yes-or-no-p "Message does not appear to be a draft: edit as new? "))
235 (pop-to-buffer-same-window
236 (get-buffer-create (concat "*notmuch-draft-" id "*")))
237 (setq buffer-read-only nil)
239 (let ((coding-system-for-read 'no-conversion))
240 (call-process notmuch-command nil t nil "show" "--format=raw" id))
242 (goto-char (point-min))
243 (when (re-search-forward "^$" nil t)
244 (replace-match mail-header-separator t t))
245 ;; Remove the Date and Message-ID headers (unless the user has
246 ;; explicitly customized emacs to tell us not to) as they will
247 ;; be replaced when the message is sent.
249 (message-narrow-to-headers)
250 (when (member 'Message-ID message-deletable-headers)
251 (message-remove-header "Message-ID"))
252 (when (member 'Date message-deletable-headers)
253 (message-remove-header "Date"))
254 (unless draft (notmuch-fcc-header-setup))
255 ;; The X-Notmuch-Emacs-Draft header is a more reliable
256 ;; indication of whether the message really is a draft.
257 (setq draft (> (message-remove-header "X-Notmuch-Emacs-Draft") 0)))
258 ;; If the message is not a draft we should not unquote any mml.
260 (notmuch-draft-unquote-some-mml))
261 (notmuch-message-mode)
263 (set-buffer-modified-p nil)
264 ;; If the resumed message was a draft then set the draft
265 ;; message-id so that we can delete the current saved draft if the
266 ;; message is resaved or sent.
267 (setq notmuch-draft-id (and draft id)))))
270 (add-hook 'message-send-hook 'notmuch-draft--mark-deleted)
273 (provide 'notmuch-draft)
275 ;;; notmuch-draft.el ends here