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