]> git.notmuchmail.org Git - notmuch/blob - emacs/notmuch-mua.el
emacs: notmuch-mua-add-more-hidden-headers: use local binding
[notmuch] / emacs / notmuch-mua.el
1 ;;; notmuch-mua.el --- emacs style mail-user-agent  -*- lexical-binding: t -*-
2 ;;
3 ;; Copyright © David Edmondson
4 ;;
5 ;; This file is part of Notmuch.
6 ;;
7 ;; Notmuch is free software: you can redistribute it and/or modify it
8 ;; under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation, either version 3 of the License, or
10 ;; (at your option) any later version.
11 ;;
12 ;; Notmuch is distributed in the hope that it will be useful, but
13 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 ;; General Public License for more details.
16 ;;
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with Notmuch.  If not, see <https://www.gnu.org/licenses/>.
19 ;;
20 ;; Authors: David Edmondson <dme@dme.org>
21
22 ;;; Code:
23
24 (require 'message)
25 (require 'mm-view)
26 (require 'format-spec)
27
28 (require 'notmuch-lib)
29 (require 'notmuch-address)
30 (require 'notmuch-draft)
31 (require 'notmuch-message)
32
33 (declare-function notmuch-show-insert-body "notmuch-show" (msg body depth))
34 (declare-function notmuch-fcc-header-setup "notmuch-maildir-fcc" ())
35 (declare-function notmuch-maildir-message-do-fcc "notmuch-maildir-fcc" ())
36 (declare-function notmuch-draft-postpone "notmuch-draft" ())
37 (declare-function notmuch-draft-save "notmuch-draft" ())
38
39 (defvar notmuch-show-indent-multipart)
40 (defvar notmuch-show-insert-header-p-function)
41 (defvar notmuch-show-max-text-part-size)
42 (defvar notmuch-show-insert-text/plain-hook)
43
44 ;;; Options
45
46 (defcustom notmuch-mua-send-hook nil
47   "Hook run before sending messages."
48   :type 'hook
49   :group 'notmuch-send
50   :group 'notmuch-hooks)
51
52 (defcustom notmuch-mua-compose-in 'current-window
53   "Where to create the mail buffer used to compose a new message.
54 Possible values are `current-window' (default), `new-window' and
55 `new-frame'. If set to `current-window', the mail buffer will be
56 displayed in the current window, so the old buffer will be
57 restored when the mail buffer is killed. If set to `new-window'
58 or `new-frame', the mail buffer will be displayed in a new
59 window/frame that will be destroyed when the buffer is killed.
60 You may want to customize `message-kill-buffer-on-exit'
61 accordingly."
62   :group 'notmuch-send
63   :type '(choice (const :tag "Compose in the current window" current-window)
64                  (const :tag "Compose mail in a new window"  new-window)
65                  (const :tag "Compose mail in a new frame"   new-frame)))
66
67 (defcustom notmuch-mua-user-agent-function nil
68   "Function used to generate a `User-Agent:' string.
69 If this is `nil' then no `User-Agent:' will be generated."
70   :type '(choice (const :tag "No user agent string" nil)
71                  (const :tag "Full" notmuch-mua-user-agent-full)
72                  (const :tag "Notmuch" notmuch-mua-user-agent-notmuch)
73                  (const :tag "Emacs" notmuch-mua-user-agent-emacs)
74                  (function :tag "Custom user agent function"
75                            :value notmuch-mua-user-agent-full))
76   :group 'notmuch-send)
77
78 (defcustom notmuch-mua-hidden-headers nil
79   "Headers that are added to the `message-mode' hidden headers list."
80   :type '(repeat string)
81   :group 'notmuch-send)
82
83 (defcustom notmuch-identities nil
84   "Identities that can be used as the From: address when composing a new message.
85
86 If this variable is left unset, then a list will be constructed from the
87 name and addresses configured in the notmuch configuration file."
88   :type '(repeat string)
89   :group 'notmuch-send)
90
91 (defcustom notmuch-always-prompt-for-sender nil
92   "Always prompt for the From: address when composing or forwarding a message.
93
94 This is not taken into account when replying to a message, because in that case
95 the From: header is already filled in by notmuch."
96   :type 'boolean
97   :group 'notmuch-send)
98
99 (defgroup notmuch-reply nil
100   "Replying to messages in notmuch."
101   :group 'notmuch)
102
103 (defcustom notmuch-mua-cite-function 'message-cite-original
104   "Function for citing an original message.
105
106 Predefined functions include `message-cite-original' and
107 `message-cite-original-without-signature'.  Note that these
108 functions use `mail-citation-hook' if that is non-nil."
109   :type '(radio (function-item message-cite-original)
110                 (function-item message-cite-original-without-signature)
111                 (function-item sc-cite-original)
112                 (function :tag "Other"))
113   :link '(custom-manual "(message)Insertion Variables")
114   :group 'notmuch-reply)
115
116 (defcustom notmuch-mua-reply-insert-header-p-function
117   'notmuch-show-reply-insert-header-p-never
118   "Function to decide which parts get a header when replying.
119
120 This function specifies which parts of a mime message with
121 multiple parts get a header."
122   :type '(radio (const :tag "No part headers"
123                        notmuch-show-reply-insert-header-p-never)
124                 (const :tag "All except multipart/* and hidden parts"
125                        notmuch-show-reply-insert-header-p-trimmed)
126                 (const :tag "Only for included text parts"
127                        notmuch-show-reply-insert-header-p-minimal)
128                 (const :tag "Exactly as in show view"
129                        notmuch-show-insert-header-p)
130                 (function :tag "Other"))
131   :group 'notmuch-reply)
132
133 (defcustom notmuch-mua-attachment-regexp
134   "\\b\\(attache\?ment\\|attached\\|attach\\|pi[èe]ce\s+jointe?\\)\\b"
135   "Message body text indicating that an attachment is expected.
136
137 This is not used unless `notmuch-mua-attachment-check' is added
138 to `notmuch-mua-send-hook'."
139   :type 'regexp
140   :group 'notmuch-send)
141
142 ;;; Various functions
143
144 (defun notmuch-mua-attachment-check ()
145   "Signal an error an attachement is expected but missing.
146
147 Signal an error if the message text indicates that an attachment
148 is expected but no MML referencing an attachment is found.
149
150 Typically this is added to `notmuch-mua-send-hook'."
151   (when (and
152          ;; When the message mentions attachment...
153          (save-excursion
154            (message-goto-body)
155            ;; Limit search from reaching other possible parts of the message
156            (let ((search-limit (search-forward "\n<#" nil t)))
157              (message-goto-body)
158              (cl-loop while (re-search-forward notmuch-mua-attachment-regexp
159                                                search-limit t)
160                       ;; For every instance of the "attachment" string
161                       ;; found, examine the text properties. If the text
162                       ;; has either a `face' or `syntax-table' property
163                       ;; then it is quoted text and should *not* cause the
164                       ;; user to be asked about a missing attachment.
165                       if (let ((props (text-properties-at (match-beginning 0))))
166                            (not (or (memq 'syntax-table props)
167                                     (memq 'face props))))
168                       return t
169                       finally return nil)))
170          ;; ...but doesn't have a part with a filename...
171          (save-excursion
172            (message-goto-body)
173            (not (re-search-forward "^<#part [^>]*filename=" nil t)))
174          ;; ...and that's not okay...
175          (not (y-or-n-p "Attachment mentioned, but no attachment - is that okay?")))
176     ;; ...signal an error.
177     (error "Missing attachment")))
178
179 (defun notmuch-mua-get-switch-function ()
180   "Get a switch function according to `notmuch-mua-compose-in'."
181   (pcase notmuch-mua-compose-in
182     ('current-window 'switch-to-buffer)
183     ('new-window     'switch-to-buffer-other-window)
184     ('new-frame      'switch-to-buffer-other-frame)
185     (_ (error "Invalid value for `notmuch-mua-compose-in'"))))
186
187 (defun notmuch-mua-maybe-set-window-dedicated ()
188   "Set the selected window as dedicated according to `notmuch-mua-compose-in'."
189   (when (or (eq notmuch-mua-compose-in 'new-frame)
190             (eq notmuch-mua-compose-in 'new-window))
191     (set-window-dedicated-p (selected-window) t)))
192
193 (defun notmuch-mua-user-agent-full ()
194   "Generate a `User-Agent:' string suitable for notmuch."
195   (concat (notmuch-mua-user-agent-notmuch)
196           " "
197           (notmuch-mua-user-agent-emacs)))
198
199 (defun notmuch-mua-user-agent-notmuch ()
200   "Generate a `User-Agent:' string suitable for notmuch."
201   (let ((notmuch-version (if (string= notmuch-emacs-version "unknown")
202                              (notmuch-cli-version)
203                            notmuch-emacs-version)))
204     (concat "Notmuch/" notmuch-version " (https://notmuchmail.org)")))
205
206 (defun notmuch-mua-user-agent-emacs ()
207   "Generate a `User-Agent:' string suitable for notmuch."
208   (concat "Emacs/" emacs-version " (" system-configuration ")"))
209
210 (defun notmuch-mua-add-more-hidden-headers ()
211   "Add some headers to the list that are hidden by default."
212   (mapc (lambda (header)
213           (unless (member header message-hidden-headers)
214             (push header message-hidden-headers)))
215         notmuch-mua-hidden-headers))
216
217 (defun notmuch-mua-reply-crypto (parts)
218   "Add mml sign-encrypt flag if any part of original message is encrypted."
219   (cl-loop for part in parts
220            for type = (plist-get part :content-type)
221            if (notmuch-match-content-type type "multipart/encrypted")
222            do (mml-secure-message-sign-encrypt)
223            else if (notmuch-match-content-type type "multipart/*")
224            do (notmuch-mua-reply-crypto (plist-get part :content))))
225
226 ;; There is a bug in Emacs' message.el that results in a newline
227 ;; not being inserted after the References header, so the next header
228 ;; is concatenated to the end of it. This function fixes the problem,
229 ;; while guarding against the possibility that some current or future
230 ;; version of emacs has the bug fixed.
231 (defun notmuch-mua-insert-references (original-func header references)
232   (funcall original-func header references)
233   (unless (bolp) (insert "\n")))
234
235 ;;; Mua reply
236
237 (defun notmuch-mua-reply (query-string &optional sender reply-all)
238   (let ((args '("reply" "--format=sexp" "--format-version=4"))
239         (process-crypto notmuch-show-process-crypto)
240         reply
241         original)
242     (when process-crypto
243       (setq args (append args '("--decrypt=true"))))
244     (if reply-all
245         (setq args (append args '("--reply-to=all")))
246       (setq args (append args '("--reply-to=sender"))))
247     (setq args (append args (list query-string)))
248     ;; Get the reply object as SEXP, and parse it into an elisp object.
249     (setq reply (apply #'notmuch-call-notmuch-sexp args))
250     ;; Extract the original message to simplify the following code.
251     (setq original (plist-get reply :original))
252     ;; Extract the headers of both the reply and the original message.
253     (let* ((original-headers (plist-get original :headers))
254            (reply-headers (plist-get reply :reply-headers)))
255       ;; If sender is non-nil, set the From: header to its value.
256       (when sender
257         (plist-put reply-headers :From sender))
258       (let
259           ;; Overlay the composition window on that being used to read
260           ;; the original message.
261           ((same-window-regexps '("\\*mail .*")))
262         ;; We modify message-header-format-alist to get around
263         ;; a bug in message.el.  See the comment above on
264         ;; notmuch-mua-insert-references.
265         (let ((message-header-format-alist
266                (cl-loop for pair in message-header-format-alist
267                         if (eq (car pair) 'References)
268                         collect (cons 'References
269                                       (apply-partially
270                                        'notmuch-mua-insert-references
271                                        (cdr pair)))
272                         else
273                         collect pair)))
274           (notmuch-mua-mail (plist-get reply-headers :To)
275                             (notmuch-sanitize (plist-get reply-headers :Subject))
276                             (notmuch-headers-plist-to-alist reply-headers)
277                             nil (notmuch-mua-get-switch-function))))
278       ;; Create a buffer-local queue for tag changes triggered when
279       ;; sending the reply.
280       (when notmuch-message-replied-tags
281         (setq notmuch-message-queued-tag-changes
282               (list (cons query-string notmuch-message-replied-tags))))
283       ;; Insert the message body - but put it in front of the signature
284       ;; if one is present, and after any other content
285       ;; message*setup-hooks may have added to the message body already.
286       (save-restriction
287         (message-goto-body)
288         (narrow-to-region (point) (point-max))
289         (goto-char (point-max))
290         (if (re-search-backward message-signature-separator nil t)
291             (when message-signature-insert-empty-line
292               (forward-line -1))
293           (goto-char (point-max))))
294       (let ((from (plist-get original-headers :From))
295             (date (plist-get original-headers :Date))
296             (start (point)))
297         ;; notmuch-mua-cite-function constructs a citation line based
298         ;; on the From and Date headers of the original message, which
299         ;; are assumed to be in the buffer.
300         (insert "From: " from "\n")
301         (insert "Date: " date "\n\n")
302         (insert
303          (with-temp-buffer
304            (let
305                ;; Don't attempt to clean up messages, excerpt
306                ;; citations, etc. in the original message before
307                ;; quoting.
308                ((notmuch-show-insert-text/plain-hook nil)
309                 ;; Don't omit long parts.
310                 (notmuch-show-max-text-part-size 0)
311                 ;; Insert headers for parts as appropriate for replying.
312                 (notmuch-show-insert-header-p-function
313                  notmuch-mua-reply-insert-header-p-function)
314                 ;; Ensure that any encrypted parts are
315                 ;; decrypted during the generation of the reply
316                 ;; text.
317                 (notmuch-show-process-crypto process-crypto)
318                 ;; Don't indent multipart sub-parts.
319                 (notmuch-show-indent-multipart nil))
320              ;; We don't want sigstatus buttons (an information leak and usually wrong anyway).
321              (cl-letf (((symbol-function 'notmuch-crypto-insert-sigstatus-button) #'ignore)
322                        ((symbol-function 'notmuch-crypto-insert-encstatus-button) #'ignore))
323                (notmuch-show-insert-body original (plist-get original :body) 0)
324                (buffer-substring-no-properties (point-min) (point-max))))))
325         (set-mark (point))
326         (goto-char start)
327         ;; Quote the original message according to the user's configured style.
328         (funcall notmuch-mua-cite-function)))
329     ;; Crypto processing based crypto content of the original message
330     (when process-crypto
331       (notmuch-mua-reply-crypto (plist-get original :body))))
332   ;; Push mark right before signature, if any.
333   (message-goto-signature)
334   (unless (eobp)
335     (end-of-line -1))
336   (push-mark)
337   (message-goto-body)
338   (set-buffer-modified-p nil))
339
340 ;;; Mode and keymap
341
342 (defvar notmuch-message-mode-map
343   (let ((map (make-sparse-keymap)))
344     (define-key map (kbd "C-c C-c") #'notmuch-mua-send-and-exit)
345     (define-key map (kbd "C-c C-s") #'notmuch-mua-send)
346     (define-key map (kbd "C-c C-p") #'notmuch-draft-postpone)
347     (define-key map (kbd "C-x C-s") #'notmuch-draft-save)
348     map)
349   "Keymap for `notmuch-message-mode'.")
350
351 (define-derived-mode notmuch-message-mode message-mode "Message[Notmuch]"
352   "Notmuch message composition mode. Mostly like `message-mode'."
353   (notmuch-address-setup))
354
355 (put 'notmuch-message-mode 'flyspell-mode-predicate 'mail-mode-flyspell-verify)
356
357 ;;; New messages
358
359 (defun notmuch-mua-pop-to-buffer (name switch-function)
360   "Pop to buffer NAME, and warn if it already exists and is modified.
361 Like `message-pop-to-buffer' but enable `notmuch-message-mode'
362 instead of `message-mode' and SWITCH-FUNCTION is mandatory."
363   (let ((buffer (get-buffer name)))
364     (if (and buffer
365              (buffer-name buffer))
366         (let ((window (get-buffer-window buffer 0)))
367           (if window
368               ;; Raise the frame already displaying the message buffer.
369               (progn
370                 (select-frame-set-input-focus (window-frame window))
371                 (select-window window))
372             (funcall switch-function buffer)
373             (set-buffer buffer))
374           (when (buffer-modified-p)
375             (if (y-or-n-p "Message already being composed; erase? ")
376                 (message nil)
377               (error "Message being composed"))))
378       (funcall switch-function name)
379       (set-buffer name))
380     (erase-buffer)
381     (notmuch-message-mode)))
382
383 (defun notmuch-mua-mail (&optional to subject other-headers _continue
384                                    switch-function yank-action send-actions
385                                    return-action &rest ignored)
386   "Invoke the notmuch mail composition window."
387   (interactive)
388   (when notmuch-mua-user-agent-function
389     (let ((user-agent (funcall notmuch-mua-user-agent-function)))
390       (unless (string-empty-p user-agent)
391         (push (cons 'User-Agent user-agent) other-headers))))
392   (unless (assq 'From other-headers)
393     (push (cons 'From (message-make-from
394                        (notmuch-user-name)
395                        (notmuch-user-primary-email)))
396           other-headers))
397   (notmuch-mua-pop-to-buffer (message-buffer-name "mail" to)
398                              (or switch-function
399                                  (notmuch-mua-get-switch-function)))
400   (let ((headers
401          (append
402           ;; The following is copied from `message-mail'
403           `((To . ,(or to "")) (Subject . ,(or subject "")))
404           ;; C-h f compose-mail says that headers should be specified as
405           ;; (string . value); however all the rest of message expects
406           ;; headers to be symbols, not strings (eg message-header-format-alist).
407           ;; https://lists.gnu.org/archive/html/emacs-devel/2011-01/msg00337.html
408           ;; We need to convert any string input, eg from rmail-start-mail.
409           (dolist (h other-headers other-headers)
410             (when (stringp (car h))
411               (setcar h (intern (capitalize (car h))))))))
412         ;; Cause `message-setup-1' to do things relevant for mail,
413         ;; such as observe `message-default-mail-headers'.
414         (message-this-is-mail t))
415     (message-setup-1 headers yank-action send-actions return-action))
416   (notmuch-fcc-header-setup)
417   (message-sort-headers)
418   (message-hide-headers)
419   (set-buffer-modified-p nil)
420   (notmuch-mua-maybe-set-window-dedicated)
421   (message-goto-to))
422
423 (defvar notmuch-mua-sender-history nil)
424
425 (defun notmuch-mua-prompt-for-sender ()
426   "Prompt for a sender from the user's configured identities."
427   (if notmuch-identities
428       (completing-read "Send mail from: " notmuch-identities
429                        nil nil nil 'notmuch-mua-sender-history
430                        (car notmuch-identities))
431     (let* ((name (notmuch-user-name))
432            (addrs (cons (notmuch-user-primary-email)
433                         (notmuch-user-other-email)))
434            (address
435             (completing-read (concat "Sender address for " name ": ") addrs
436                              nil nil nil 'notmuch-mua-sender-history
437                              (car addrs))))
438       (message-make-from name address))))
439
440 (put 'notmuch-mua-new-mail 'notmuch-prefix-doc "... and prompt for sender")
441 (defun notmuch-mua-new-mail (&optional prompt-for-sender)
442   "Compose new mail.
443
444 If PROMPT-FOR-SENDER is non-nil, the user will be prompted for
445 the From: address first."
446   (interactive "P")
447   (let ((other-headers
448          (and (or prompt-for-sender notmuch-always-prompt-for-sender)
449               (list (cons 'From (notmuch-mua-prompt-for-sender))))))
450     (notmuch-mua-mail nil nil other-headers nil (notmuch-mua-get-switch-function))))
451
452 (defun notmuch-mua-new-forward-messages (messages &optional prompt-for-sender)
453   "Compose a new message forwarding MESSAGES.
454
455 If PROMPT-FOR-SENDER is non-nil, the user will be prompteed for
456 the From: address."
457   (let* ((other-headers
458           (and (or prompt-for-sender notmuch-always-prompt-for-sender)
459                (list (cons 'From (notmuch-mua-prompt-for-sender)))))
460          ;; Comes from the first message and is applied later.
461          forward-subject
462          ;; List of accumulated message-references of forwarded messages.
463          forward-references
464          ;; List of corresponding message-query.
465          forward-queries)
466     ;; Generate the template for the outgoing message.
467     (notmuch-mua-mail nil "" other-headers nil (notmuch-mua-get-switch-function))
468     (save-excursion
469       ;; Insert all of the forwarded messages.
470       (mapc (lambda (id)
471               (let ((temp-buffer (get-buffer-create
472                                   (concat "*notmuch-fwd-raw-" id "*"))))
473                 ;; Get the raw version of this message in the buffer.
474                 (with-current-buffer temp-buffer
475                   (erase-buffer)
476                   (let ((coding-system-for-read 'no-conversion))
477                     (call-process notmuch-command nil t nil
478                                   "show" "--format=raw" id))
479                   ;; Because we process the messages in reverse order,
480                   ;; always generate a forwarded subject, then use the
481                   ;; last (i.e. first) one.
482                   (setq forward-subject (message-make-forward-subject))
483                   (push (message-fetch-field "Message-ID") forward-references)
484                   (push id forward-queries))
485                 ;; Make a copy ready to be forwarded in the
486                 ;; composition buffer.
487                 (message-forward-make-body temp-buffer)
488                 ;; Kill the temporary buffer.
489                 (kill-buffer temp-buffer)))
490             ;; `message-forward-make-body' always puts the message at
491             ;; the top, so do them in reverse order.
492             (reverse messages))
493       ;; Add in the appropriate subject.
494       (save-restriction
495         (message-narrow-to-headers)
496         (message-remove-header "Subject")
497         (message-add-header (concat "Subject: " forward-subject))
498         (message-remove-header "References")
499         (message-add-header (concat "References: "
500                                     (mapconcat 'identity forward-references " "))))
501       ;; Create a buffer-local queue for tag changes triggered when
502       ;; sending the message.
503       (when notmuch-message-forwarded-tags
504         (setq notmuch-message-queued-tag-changes
505               (cl-loop for id in forward-queries
506                        collect
507                        (cons id notmuch-message-forwarded-tags))))
508       ;; `message-forward-make-body' shows the User-agent header.  Hide
509       ;; it again.
510       (message-hide-headers)
511       (set-buffer-modified-p nil))))
512
513 (defun notmuch-mua-new-reply (query-string &optional prompt-for-sender reply-all)
514   "Compose a reply to the message identified by QUERY-STRING.
515
516 If PROMPT-FOR-SENDER is non-nil, the user will be prompted for
517 the From: address first.  If REPLY-ALL is non-nil, the message
518 will be addressed to all recipients of the source message."
519   ;; `select-active-regions' is t by default. The reply insertion code
520   ;; sets the region to the quoted message to make it easy to delete
521   ;; (kill-region or C-w). These two things combine to put the quoted
522   ;; message in the primary selection.
523   ;;
524   ;; This is not what the user wanted and is a privacy risk (accidental
525   ;; pasting of the quoted message). We can avoid some of the problems
526   ;; by let-binding select-active-regions to nil. This fixes if the
527   ;; primary selection was previously in a non-emacs window but not if
528   ;; it was in an emacs window. To avoid the problem in the latter case
529   ;; we deactivate mark.
530   (let ((sender (and prompt-for-sender
531                      (notmuch-mua-prompt-for-sender)))
532         (select-active-regions nil))
533     (notmuch-mua-reply query-string sender reply-all)
534     (deactivate-mark)))
535
536 ;;; Checks
537
538 (defun notmuch-mua-check-no-misplaced-secure-tag ()
539   "Query user if there is a misplaced secure mml tag.
540
541 Emacs message-send will (probably) ignore a secure mml tag unless
542 it is at the start of the body. Returns t if there is no such
543 tag, or the user confirms they mean it."
544   (save-excursion
545     (let ((body-start (progn (message-goto-body) (point))))
546       (goto-char (point-max))
547       (or
548        ;; We are always fine if there is no secure tag.
549        (not (search-backward "<#secure" nil t))
550        ;; There is a secure tag, so it must be at the start of the
551        ;; body, with no secure tag earlier (i.e., in the headers).
552        (and (= (point) body-start)
553             (not (search-backward "<#secure" nil t)))
554        ;; The user confirms they means it.
555        (yes-or-no-p "\
556 There is a <#secure> tag not at the start of the body. It is
557 likely that the message will be sent unsigned and unencrypted.
558 Really send? ")))))
559
560 (defun notmuch-mua-check-secure-tag-has-newline ()
561   "Query if the secure mml tag has a newline following it.
562
563 Emacs message-send will (probably) ignore a correctly placed
564 secure mml tag unless it is followed by a newline. Returns t if
565 any secure tag is followed by a newline, or the user confirms
566 they mean it."
567   (save-excursion
568     (message-goto-body)
569     (or
570      ;; There is no (correctly placed) secure tag.
571      (not (looking-at "<#secure"))
572      ;; The secure tag is followed by a newline.
573      (looking-at "<#secure[^\n>]*>\n")
574      ;; The user confirms they means it.
575      (yes-or-no-p "\
576 The <#secure> tag at the start of the body is not followed by a
577 newline. It is likely that the message will be sent unsigned and
578 unencrypted.  Really send? "))))
579
580 ;;; Finishing commands
581
582 (defun notmuch-mua-send-common (arg &optional exit)
583   (interactive "P")
584   (run-hooks 'notmuch-mua-send-hook)
585   (when (and (notmuch-mua-check-no-misplaced-secure-tag)
586              (notmuch-mua-check-secure-tag-has-newline))
587     (cl-letf (((symbol-function 'message-do-fcc)
588                #'notmuch-maildir-message-do-fcc))
589       (if exit
590           (message-send-and-exit arg)
591         (message-send arg)))))
592
593 (defun notmuch-mua-send-and-exit (&optional arg)
594   (interactive "P")
595   (notmuch-mua-send-common arg t))
596
597 (defun notmuch-mua-send (&optional arg)
598   (interactive "P")
599   (notmuch-mua-send-common arg))
600
601 (defun notmuch-mua-kill-buffer ()
602   (interactive)
603   (message-kill-buffer))
604
605 ;;; _
606
607 (define-mail-user-agent 'notmuch-user-agent
608   'notmuch-mua-mail
609   'notmuch-mua-send-and-exit
610   'notmuch-mua-kill-buffer
611   'notmuch-mua-send-hook)
612
613 ;; Add some more headers to the list that `message-mode' hides when
614 ;; composing a message.
615 (notmuch-mua-add-more-hidden-headers)
616
617 (provide 'notmuch-mua)
618
619 ;;; notmuch-mua.el ends here