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