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