]> git.notmuchmail.org Git - notmuch/blob - emacs/notmuch-draft.el
504b33be44bcee8248ff2595d578103d51b23d34
[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 notmuch-draft-id nil
93   "Message-id of the most recent saved draft of this message")
94 (make-variable-buffer-local 'notmuch-draft-id)
95
96 (defun notmuch-draft--mark-deleted ()
97   "Tag the last saved draft deleted.
98
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"))))
102
103 (defun notmuch-draft-quote-some-mml ()
104   "Quote the mml tags in `notmuch-draft-quoted-tags`."
105   (save-excursion
106     ;; First we deal with any secure tag separately.
107     (message-goto-body)
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
113     ;; specified tags.
114     (when notmuch-draft-quoted-tags
115       (let ((re (concat "<#!*/?\\("
116                         (mapconcat 'regexp-quote notmuch-draft-quoted-tags "\\|")
117                         "\\)")))
118         (message-goto-body)
119         (while (re-search-forward re nil t)
120           ;; Insert ! after the #.
121           (goto-char (+ (match-beginning 0) 2))
122           (insert "!"))))))
123
124 (defun notmuch-draft-unquote-some-mml ()
125   "Unquote the mml tags in `notmuch-draft-quoted-tags`."
126   (save-excursion
127     (when notmuch-draft-quoted-tags
128       (let ((re (concat "<#!+/?\\("
129                         (mapconcat 'regexp-quote notmuch-draft-quoted-tags "\\|")
130                         "\\)")))
131         (message-goto-body)
132         (while (re-search-forward re nil t)
133           ;; Remove one ! from after the #.
134           (goto-char (+ (match-beginning 0) 2))
135           (delete-char 1))))
136     (let (secure-tag)
137       (save-restriction
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"))
141       (message-goto-body)
142       (when secure-tag
143         (insert secure-tag "\n")))))
144
145 (defun notmuch-draft--has-encryption-tag ()
146   "Returns t if there is an mml secure tag."
147   (save-excursion
148     (message-goto-body)
149     (re-search-forward notmuch-draft-encryption-tag-regex nil 't)))
150
151 (defun notmuch-draft--query-encryption ()
152   "Checks if we should save a message that should be encrypted.
153
154 `notmuch-draft-save-plaintext' controls the behaviour."
155   (cl-case notmuch-draft-save-plaintext
156         ((ask)
157          (unless (yes-or-no-p "(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 `notmuch-draft-save-plaintext')"))
163         ((t)
164          (ignore))))
165
166 (defun notmuch-draft--make-message-id ()
167   ;; message-make-message-id gives the id inside a "<" ">" pair,
168   ;; but notmuch doesn't want that form, so remove them.
169   (concat "draft-" (substring (message-make-message-id) 1 -1)))
170
171 (defun notmuch-draft-save ()
172   "Save the current draft message in the notmuch database.
173
174 This saves the current message in the database with tags
175 `notmuch-draft-tags` (in addition to any default tags
176 applied to newly inserted messages)."
177   (interactive)
178   (when (notmuch-draft--has-encryption-tag)
179     (notmuch-draft--query-encryption))
180   (let ((id (notmuch-draft--make-message-id)))
181     (with-temporary-notmuch-message-buffer
182      ;; We insert a Date header and a Message-ID header, the former
183      ;; so that it is easier to search for the message, and the
184      ;; latter so we have a way of accessing the saved message (for
185      ;; example to delete it at a later time). We check that the
186      ;; user has these in `message-deletable-headers` (the default)
187      ;; as otherwise they are doing something strange and we
188      ;; shouldn't interfere. Note, since we are doing this in a new
189      ;; buffer we don't change the version in the compose buffer.
190      (cond
191       ((member 'Message-ID message-deletable-headers)
192        (message-remove-header "Message-ID")
193        (message-add-header (concat "Message-ID: <" id ">")))
194       (t
195        (message "You have customized emacs so Message-ID is not a deletable header, so not changing it")
196        (setq id nil)))
197      (cond
198       ((member 'Date message-deletable-headers)
199        (message-remove-header "Date")
200        (message-add-header (concat "Date: " (message-make-date))))
201       (t
202        (message "You have customized emacs so Date is not a deletable header, so not changing it")))
203      (message-add-header "X-Notmuch-Emacs-Draft: True")
204      (notmuch-draft-quote-some-mml)
205      (notmuch-maildir-setup-message-for-saving)
206      (notmuch-maildir-notmuch-insert-current-buffer
207       notmuch-draft-folder 't notmuch-draft-tags))
208     ;; We are now back in the original compose buffer. Note the
209     ;; function notmuch-call-notmuch-process (called by
210     ;; notmuch-maildir-notmuch-insert-current-buffer) signals an error
211     ;; on failure, so to get to this point it must have
212     ;; succeeded. Also, notmuch-draft-id is still the id of the
213     ;; previous draft, so it is safe to mark it deleted.
214     (notmuch-draft--mark-deleted)
215     (setq notmuch-draft-id (concat "id:" id))
216     (set-buffer-modified-p nil)))
217
218 (defun notmuch-draft-postpone ()
219   "Save the draft message in the notmuch database and exit buffer."
220   (interactive)
221   (notmuch-draft-save)
222   (kill-buffer))
223
224 (defun notmuch-draft-resume (id)
225   "Resume editing of message with id ID."
226   (let* ((tags (process-lines notmuch-command "search" "--output=tags"
227                               "--exclude=false" id))
228          (draft (equal tags (notmuch-update-tags tags notmuch-draft-tags))))
229     (when (or draft
230               (yes-or-no-p "Message does not appear to be a draft: edit as new? "))
231       (switch-to-buffer (get-buffer-create (concat "*notmuch-draft-" id "*")))
232       (setq buffer-read-only nil)
233       (erase-buffer)
234       (let ((coding-system-for-read 'no-conversion))
235         (call-process notmuch-command nil t nil "show" "--format=raw" id))
236       (mime-to-mml)
237       (goto-char (point-min))
238       (when (re-search-forward "^$" nil t)
239         (replace-match mail-header-separator t t))
240       ;; Remove the Date and Message-ID headers (unless the user has
241       ;; explicitly customized emacs to tell us not to) as they will
242       ;; be replaced when the message is sent.
243       (save-restriction
244         (message-narrow-to-headers)
245         (when (member 'Message-ID message-deletable-headers)
246           (message-remove-header "Message-ID"))
247         (when (member 'Date message-deletable-headers)
248           (message-remove-header "Date"))
249         (unless draft (notmuch-fcc-header-setup))
250         ;; The X-Notmuch-Emacs-Draft header is a more reliable
251         ;; indication of whether the message really is a draft.
252         (setq draft (> (message-remove-header "X-Notmuch-Emacs-Draft") 0)))
253       ;; If the message is not a draft we should not unquote any mml.
254       (when draft
255         (notmuch-draft-unquote-some-mml))
256       (notmuch-message-mode)
257       (message-goto-body)
258       (set-buffer-modified-p nil)
259       ;; If the resumed message was a draft then set the draft
260       ;; message-id so that we can delete the current saved draft if the
261       ;; message is resaved or sent.
262       (setq notmuch-draft-id (when draft id)))))
263
264
265 (add-hook 'message-send-hook 'notmuch-draft--mark-deleted)
266
267
268 (provide 'notmuch-draft)
269
270 ;;; notmuch-draft.el ends here