]> git.notmuchmail.org Git - notmuch/blob - emacs/notmuch-draft.el
f928be876a906c013f9ec13d345e9d95211461ba
[notmuch] / emacs / notmuch-draft.el
1 ;;; notmuch-draft.el --- functions for postponing and editing drafts
2 ;;
3 ;; Copyright © Mark Walters
4 ;; Copyright © David Bremner
5 ;; Copyright © Leo Gaspard
6 ;;
7 ;; This file is part of Notmuch.
8 ;;
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.
13 ;;
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.
18 ;;
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/>.
21 ;;
22 ;; Authors: Mark Walters <markwalters1009@gmail.com>
23 ;;          David Bremner <david@tethera.net>
24 ;;          Leo Gaspard <leo@gaspard.io>
25
26 ;;; Code:
27
28 (require 'notmuch-maildir-fcc)
29 (require 'notmuch-tag)
30
31 (declare-function notmuch-show-get-message-id "notmuch-show" (&optional bare))
32 (declare-function notmuch-message-mode "notmuch-mua")
33
34 (defgroup notmuch-draft nil
35   "Saving and editing drafts in Notmuch."
36   :group 'notmuch)
37
38 (defcustom notmuch-draft-tags '("+draft")
39   "List of tags changes to apply to a draft message when it is saved in the database.
40
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.
44
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
47 set:
48     (\"+draft\" \"-inbox\")"
49   :type '(repeat string)
50   :group 'notmuch-draft)
51
52 (defcustom notmuch-draft-folder "drafts"
53   "Folder to save draft messages in.
54
55 This should be specified relative to the root of the notmuch
56 database. It will be created if necessary."
57   :type 'string
58   :group 'notmuch-draft)
59
60 (defcustom notmuch-draft-quoted-tags '()
61   "Mml tags to quote.
62
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.
65
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.
71
72 Note you may get strange results if you change this between
73 postponing and resuming a message."
74   :type '(repeat string)
75   :group 'notmuch-send)
76
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)."
81   :type '(radio
82           (const :tag "Never" nil)
83           (const :tag "Ask every time" ask)
84           (const :tag "Always" t))
85   :group 'notmuch-draft
86   :group 'notmuch-crypto)
87
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.")
91
92 (defvar-local notmuch-draft-id nil
93   "Message-id of the most recent saved draft of this message.")
94
95 (defun notmuch-draft--mark-deleted ()
96   "Tag the last saved draft deleted.
97
98 Used when a new version is saved, or the message is sent."
99   (when notmuch-draft-id
100     (notmuch-tag notmuch-draft-id '("+deleted"))))
101
102 (defun notmuch-draft-quote-some-mml ()
103   "Quote the mml tags in `notmuch-draft-quoted-tags'."
104   (save-excursion
105     ;; First we deal with any secure tag separately.
106     (message-goto-body)
107     (when (looking-at "<#secure[^\n]*>\n")
108       (let ((secure-tag (match-string 0)))
109         (delete-region (match-beginning 0) (match-end 0))
110         (message-add-header (concat "X-Notmuch-Emacs-Secure: " secure-tag))))
111     ;; This is copied from mml-quote-region but only quotes the
112     ;; specified tags.
113     (when notmuch-draft-quoted-tags
114       (let ((re (concat "<#!*/?\\("
115                         (mapconcat 'regexp-quote notmuch-draft-quoted-tags "\\|")
116                         "\\)")))
117         (message-goto-body)
118         (while (re-search-forward re nil t)
119           ;; Insert ! after the #.
120           (goto-char (+ (match-beginning 0) 2))
121           (insert "!"))))))
122
123 (defun notmuch-draft-unquote-some-mml ()
124   "Unquote the mml tags in `notmuch-draft-quoted-tags'."
125   (save-excursion
126     (when notmuch-draft-quoted-tags
127       (let ((re (concat "<#!+/?\\("
128                         (mapconcat 'regexp-quote notmuch-draft-quoted-tags "\\|")
129                         "\\)")))
130         (message-goto-body)
131         (while (re-search-forward re nil t)
132           ;; Remove one ! from after the #.
133           (goto-char (+ (match-beginning 0) 2))
134           (delete-char 1))))
135     (let (secure-tag)
136       (save-restriction
137         (message-narrow-to-headers)
138         (setq secure-tag (message-fetch-field "X-Notmuch-Emacs-Secure" t))
139         (message-remove-header "X-Notmuch-Emacs-Secure"))
140       (message-goto-body)
141       (when secure-tag
142         (insert secure-tag "\n")))))
143
144 (defun notmuch-draft--has-encryption-tag ()
145   "Returns t if there is an mml secure tag."
146   (save-excursion
147     (message-goto-body)
148     (re-search-forward notmuch-draft-encryption-tag-regex nil t)))
149
150 (defun notmuch-draft--query-encryption ()
151   "Checks if we should save a message that should be encrypted.
152
153 `notmuch-draft-save-plaintext' controls the behaviour."
154   (cl-case notmuch-draft-save-plaintext
155     ((ask)
156      (unless (yes-or-no-p
157               "(Customize `notmuch-draft-save-plaintext' to avoid this warning)
158 This message contains mml tags that suggest it is intended to be encrypted.
159 Really save and index an unencrypted copy? ")
160        (error "Save aborted")))
161     ((nil)
162      (error "Refusing to save draft with encryption tags (see `%s')"
163             'notmuch-draft-save-plaintext))
164     ((t)
165      (ignore))))
166
167 (defun notmuch-draft--make-message-id ()
168   ;; message-make-message-id gives the id inside a "<" ">" pair,
169   ;; but notmuch doesn't want that form, so remove them.
170   (concat "draft-" (substring (message-make-message-id) 1 -1)))
171
172 (defun notmuch-draft-save ()
173   "Save the current draft message in the notmuch database.
174
175 This saves the current message in the database with tags
176 `notmuch-draft-tags' (in addition to any default tags
177 applied to newly inserted messages)."
178   (interactive)
179   (when (notmuch-draft--has-encryption-tag)
180     (notmuch-draft--query-encryption))
181   (let ((id (notmuch-draft--make-message-id)))
182     (with-temporary-notmuch-message-buffer
183      ;; We insert a Date header and a Message-ID header, the former
184      ;; so that it is easier to search for the message, and the
185      ;; latter so we have a way of accessing the saved message (for
186      ;; example to delete it at a later time). We check that the
187      ;; user has these in `message-deletable-headers' (the default)
188      ;; as otherwise they are doing something strange and we
189      ;; shouldn't interfere. Note, since we are doing this in a new
190      ;; buffer we don't change the version in the compose buffer.
191      (cond
192       ((member 'Message-ID message-deletable-headers)
193        (message-remove-header "Message-ID")
194        (message-add-header (concat "Message-ID: <" id ">")))
195       (t
196        (message "You have customized emacs so Message-ID is not a %s"
197                 "deletable header, so not changing it")
198        (setq id nil)))
199      (cond
200       ((member 'Date message-deletable-headers)
201        (message-remove-header "Date")
202        (message-add-header (concat "Date: " (message-make-date))))
203       (t
204        (message "You have customized emacs so Date is not a deletable %s"
205                 "header, so not changing it")))
206      (message-add-header "X-Notmuch-Emacs-Draft: True")
207      (notmuch-draft-quote-some-mml)
208      (notmuch-maildir-setup-message-for-saving)
209      (notmuch-maildir-notmuch-insert-current-buffer
210       notmuch-draft-folder t notmuch-draft-tags))
211     ;; We are now back in the original compose buffer. Note the
212     ;; function notmuch-call-notmuch-process (called by
213     ;; notmuch-maildir-notmuch-insert-current-buffer) signals an error
214     ;; on failure, so to get to this point it must have
215     ;; succeeded. Also, notmuch-draft-id is still the id of the
216     ;; previous draft, so it is safe to mark it deleted.
217     (notmuch-draft--mark-deleted)
218     (setq notmuch-draft-id (concat "id:" id))
219     (set-buffer-modified-p nil)))
220
221 (defun notmuch-draft-postpone ()
222   "Save the draft message in the notmuch database and exit buffer."
223   (interactive)
224   (notmuch-draft-save)
225   (kill-buffer))
226
227 (defun notmuch-draft-resume (id)
228   "Resume editing of message with id ID."
229   (let* ((tags (process-lines notmuch-command "search" "--output=tags"
230                               "--exclude=false" id))
231          (draft (equal tags (notmuch-update-tags tags notmuch-draft-tags))))
232     (when (or draft
233               (yes-or-no-p "Message does not appear to be a draft: edit as new? "))
234       (pop-to-buffer-same-window
235        (get-buffer-create (concat "*notmuch-draft-" id "*")))
236       (setq buffer-read-only nil)
237       (erase-buffer)
238       (let ((coding-system-for-read 'no-conversion))
239         (call-process notmuch-command nil t nil "show" "--format=raw" id))
240       (mime-to-mml)
241       (goto-char (point-min))
242       (when (re-search-forward "^$" nil t)
243         (replace-match mail-header-separator t t))
244       ;; Remove the Date and Message-ID headers (unless the user has
245       ;; explicitly customized emacs to tell us not to) as they will
246       ;; be replaced when the message is sent.
247       (save-restriction
248         (message-narrow-to-headers)
249         (when (member 'Message-ID message-deletable-headers)
250           (message-remove-header "Message-ID"))
251         (when (member 'Date message-deletable-headers)
252           (message-remove-header "Date"))
253         (unless draft (notmuch-fcc-header-setup))
254         ;; The X-Notmuch-Emacs-Draft header is a more reliable
255         ;; indication of whether the message really is a draft.
256         (setq draft (> (message-remove-header "X-Notmuch-Emacs-Draft") 0)))
257       ;; If the message is not a draft we should not unquote any mml.
258       (when draft
259         (notmuch-draft-unquote-some-mml))
260       (notmuch-message-mode)
261       (message-goto-body)
262       (set-buffer-modified-p nil)
263       ;; If the resumed message was a draft then set the draft
264       ;; message-id so that we can delete the current saved draft if the
265       ;; message is resaved or sent.
266       (setq notmuch-draft-id (and draft id)))))
267
268
269 (add-hook 'message-send-hook 'notmuch-draft--mark-deleted)
270
271
272 (provide 'notmuch-draft)
273
274 ;;; notmuch-draft.el ends here