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)
31 (eval-when-compile (require 'cl))
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" ())
41 (defcustom notmuch-mua-send-hook '(notmuch-mua-message-send-hook)
42 "Hook run before sending messages."
45 :group 'notmuch-hooks)
47 (defcustom notmuch-mua-compose-in 'current-window
49 "Where to create the mail buffer used to compose a new message.
50 Possible values are `current-window' (default), `new-window' and
51 `new-frame'. If set to `current-window', the mail buffer will be
52 displayed in the current window, so the old buffer will be
53 restored when the mail buffer is killed. If set to `new-window'
54 or `new-frame', the mail buffer will be displayed in a new
55 window/frame that will be destroyed when the buffer is killed.
56 You may want to customize `message-kill-buffer-on-exit'
58 (when (< emacs-major-version 24)
59 " Due to a known bug in Emacs 23, you should not set
60 this to `new-window' if `message-kill-buffer-on-exit' is
61 disabled: this would result in an incorrect behavior."))
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)))
67 (defcustom notmuch-mua-user-agent-function nil
68 "Function used to generate a `User-Agent:' string. If this is
69 `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))
78 (defcustom notmuch-mua-hidden-headers nil
79 "Headers that are added to the `message-mode' hidden headers
81 :type '(repeat string)
84 (defgroup notmuch-reply nil
85 "Replying to messages in notmuch"
88 (defcustom notmuch-mua-cite-function 'message-cite-original
89 "*Function for citing an original message.
90 Predefined functions include `message-cite-original' and
91 `message-cite-original-without-signature'.
92 Note that these functions use `mail-citation-hook' if that is non-nil."
93 :type '(radio (function-item message-cite-original)
94 (function-item message-cite-original-without-signature)
95 (function-item sc-cite-original)
96 (function :tag "Other"))
97 :link '(custom-manual "(message)Insertion Variables")
98 :group 'notmuch-reply)
100 (defcustom notmuch-mua-reply-insert-header-p-function
101 'notmuch-show-reply-insert-header-p-never
102 "Function to decide which parts get a header when replying.
104 This function specifies which parts of a mime message with
105 mutiple parts get a header."
106 :type '(radio (const :tag "No part headers"
107 notmuch-show-reply-insert-header-p-never)
108 (const :tag "All except multipart/* and hidden parts"
109 notmuch-show-reply-insert-header-p-trimmed)
110 (const :tag "Only for included text parts"
111 notmuch-show-reply-insert-header-p-minimal)
112 (const :tag "Exactly as in show view"
113 notmuch-show-insert-header-p)
114 (function :tag "Other"))
115 :group 'notmuch-reply)
119 (defun notmuch-mua-get-switch-function ()
120 "Get a switch function according to `notmuch-mua-compose-in'."
121 (cond ((eq notmuch-mua-compose-in 'current-window)
123 ((eq notmuch-mua-compose-in 'new-window)
124 'switch-to-buffer-other-window)
125 ((eq notmuch-mua-compose-in 'new-frame)
126 'switch-to-buffer-other-frame)
127 (t (error "Invalid value for `notmuch-mua-compose-in'"))))
129 (defun notmuch-mua-maybe-set-window-dedicated ()
130 "Set the selected window as dedicated according to
131 `notmuch-mua-compose-in'."
132 (when (or (eq notmuch-mua-compose-in 'new-frame)
133 (eq notmuch-mua-compose-in 'new-window))
134 (set-window-dedicated-p (selected-window) t)))
136 (defun notmuch-mua-user-agent-full ()
137 "Generate a `User-Agent:' string suitable for notmuch."
138 (concat (notmuch-mua-user-agent-notmuch)
140 (notmuch-mua-user-agent-emacs)))
142 (defun notmuch-mua-user-agent-notmuch ()
143 "Generate a `User-Agent:' string suitable for notmuch."
144 (let ((notmuch-version (if (string= notmuch-emacs-version "unknown")
145 (notmuch-cli-version)
146 notmuch-emacs-version)))
147 (concat "Notmuch/" notmuch-version " (https://notmuchmail.org)")))
149 (defun notmuch-mua-user-agent-emacs ()
150 "Generate a `User-Agent:' string suitable for notmuch."
151 (concat "Emacs/" emacs-version " (" system-configuration ")"))
153 (defun notmuch-mua-add-more-hidden-headers ()
154 "Add some headers to the list that are hidden by default."
155 (mapc (lambda (header)
156 (when (not (member header message-hidden-headers))
157 (push header message-hidden-headers)))
158 notmuch-mua-hidden-headers))
160 (defun notmuch-mua-reply-crypto (parts)
161 "Add mml sign-encrypt flag if any part of original message is encrypted."
162 (loop for part in parts
163 if (notmuch-match-content-type (plist-get part :content-type) "multipart/encrypted")
164 do (mml-secure-message-sign-encrypt)
165 else if (notmuch-match-content-type (plist-get part :content-type) "multipart/*")
166 do (notmuch-mua-reply-crypto (plist-get part :content))))
168 ;; There is a bug in emacs 23's message.el that results in a newline
169 ;; not being inserted after the References header, so the next header
170 ;; is concatenated to the end of it. This function fixes the problem,
171 ;; while guarding against the possibility that some current or future
172 ;; version of emacs has the bug fixed.
173 (defun notmuch-mua-insert-references (original-func header references)
174 (funcall original-func header references)
175 (unless (bolp) (insert "\n")))
177 (defun notmuch-mua-reply (query-string &optional sender reply-all)
178 (let ((args '("reply" "--format=sexp" "--format-version=1"))
179 (process-crypto notmuch-show-process-crypto)
183 (setq args (append args '("--decrypt"))))
186 (setq args (append args '("--reply-to=all")))
187 (setq args (append args '("--reply-to=sender"))))
188 (setq args (append args (list query-string)))
190 ;; Get the reply object as SEXP, and parse it into an elisp object.
191 (setq reply (apply #'notmuch-call-notmuch-sexp args))
193 ;; Extract the original message to simplify the following code.
194 (setq original (plist-get reply :original))
196 ;; Extract the headers of both the reply and the original message.
197 (let* ((original-headers (plist-get original :headers))
198 (reply-headers (plist-get reply :reply-headers)))
200 ;; If sender is non-nil, set the From: header to its value.
202 (plist-put reply-headers :From sender))
204 ;; Overlay the composition window on that being used to read
205 ;; the original message.
206 ((same-window-regexps '("\\*mail .*")))
208 ;; We modify message-header-format-alist to get around a bug in message.el.
209 ;; See the comment above on notmuch-mua-insert-references.
210 (let ((message-header-format-alist
211 (loop for pair in message-header-format-alist
212 if (eq (car pair) 'References)
213 collect (cons 'References
215 'notmuch-mua-insert-references
219 (notmuch-mua-mail (plist-get reply-headers :To)
220 (plist-get reply-headers :Subject)
221 (notmuch-headers-plist-to-alist reply-headers)
222 nil (notmuch-mua-get-switch-function))))
224 ;; Insert the message body - but put it in front of the signature
225 ;; if one is present, and after any other content
226 ;; message*setup-hooks may have added to the message body already.
229 (narrow-to-region (point) (point-max))
230 (goto-char (point-max))
231 (if (re-search-backward message-signature-separator nil t)
232 (if message-signature-insert-empty-line
234 (goto-char (point-max))))
236 (let ((from (plist-get original-headers :From))
237 (date (plist-get original-headers :Date))
240 ;; notmuch-mua-cite-function constructs a citation line based
241 ;; on the From and Date headers of the original message, which
242 ;; are assumed to be in the buffer.
243 (insert "From: " from "\n")
244 (insert "Date: " date "\n\n")
246 (insert (with-temp-buffer
248 ;; Don't attempt to clean up messages, excerpt
249 ;; citations, etc. in the original message before
251 ((notmuch-show-insert-text/plain-hook nil)
252 ;; Don't omit long parts.
253 (notmuch-show-max-text-part-size 0)
254 ;; Insert headers for parts as appropriate for replying.
255 (notmuch-show-insert-header-p-function notmuch-mua-reply-insert-header-p-function)
256 ;; Ensure that any encrypted parts are
257 ;; decrypted during the generation of the reply
259 (notmuch-show-process-crypto process-crypto)
260 ;; Don't indent multipart sub-parts.
261 (notmuch-show-indent-multipart nil))
262 ;; We don't want sigstatus buttons (an information leak and usually wrong anyway).
263 (letf (((symbol-function 'notmuch-crypto-insert-sigstatus-button) #'ignore)
264 ((symbol-function 'notmuch-crypto-insert-encstatus-button) #'ignore))
265 (notmuch-show-insert-body original (plist-get original :body) 0)
266 (buffer-substring-no-properties (point-min) (point-max))))))
270 ;; Quote the original message according to the user's configured style.
271 (funcall notmuch-mua-cite-function)))
273 ;; Crypto processing based crypto content of the original message
275 (notmuch-mua-reply-crypto (plist-get original :body))))
277 ;; Push mark right before signature, if any.
278 (message-goto-signature)
284 (set-buffer-modified-p nil))
286 (define-derived-mode notmuch-message-mode message-mode "Message[Notmuch]"
287 "Notmuch message composition mode. Mostly like `message-mode'"
288 (notmuch-address-setup))
290 (put 'notmuch-message-mode 'flyspell-mode-predicate 'mail-mode-flyspell-verify)
292 (define-key notmuch-message-mode-map (kbd "C-c C-c") #'notmuch-mua-send-and-exit)
293 (define-key notmuch-message-mode-map (kbd "C-c C-s") #'notmuch-mua-send)
294 (define-key notmuch-message-mode-map (kbd "C-c C-p") #'notmuch-draft-postpone)
295 (define-key notmuch-message-mode-map (kbd "C-x C-s") #'notmuch-draft-save)
297 (defun notmuch-mua-pop-to-buffer (name switch-function)
298 "Pop to buffer NAME, and warn if it already exists and is
299 modified. This function is notmuch addaptation of
300 `message-pop-to-buffer'."
301 (let ((buffer (get-buffer name)))
303 (buffer-name buffer))
304 (let ((window (get-buffer-window buffer 0)))
306 ;; Raise the frame already displaying the message buffer.
308 (gnus-select-frame-set-input-focus (window-frame window))
309 (select-window window))
310 (funcall switch-function buffer)
312 (when (and (buffer-modified-p)
315 "Message already being composed; erase? ")
317 (error "Message being composed")))
318 (funcall switch-function name)
321 (notmuch-message-mode)))
323 (defun notmuch-mua-mail (&optional to subject other-headers continue
324 switch-function yank-action send-actions
325 return-action &rest ignored)
326 "Invoke the notmuch mail composition window."
329 (when notmuch-mua-user-agent-function
330 (let ((user-agent (funcall notmuch-mua-user-agent-function)))
331 (when (not (string= "" user-agent))
332 (push (cons 'User-Agent user-agent) other-headers))))
334 (unless (assq 'From other-headers)
335 (push (cons 'From (message-make-from
336 (notmuch-user-name) (notmuch-user-primary-email))) other-headers))
338 (notmuch-mua-pop-to-buffer (message-buffer-name "mail" to)
339 (or switch-function (notmuch-mua-get-switch-function)))
342 ;; The following is copied from `message-mail'
343 `((To . ,(or to "")) (Subject . ,(or subject "")))
344 ;; C-h f compose-mail says that headers should be specified as
345 ;; (string . value); however all the rest of message expects
346 ;; headers to be symbols, not strings (eg message-header-format-alist).
347 ;; https://lists.gnu.org/archive/html/emacs-devel/2011-01/msg00337.html
348 ;; We need to convert any string input, eg from rmail-start-mail.
349 (dolist (h other-headers other-headers)
350 (if (stringp (car h)) (setcar h (intern (capitalize (car h))))))))
351 (args (list yank-action send-actions))
352 ;; Cause `message-setup-1' to do things relevant for mail,
353 ;; such as observe `message-default-mail-headers'.
354 (message-this-is-mail t))
355 ;; message-setup-1 in Emacs 23 does not accept return-action
356 ;; argument. Pass it only if it is supplied by the caller. This
357 ;; will never be the case when we're called by `compose-mail' in
359 (when return-action (nconc args '(return-action)))
360 (apply 'message-setup-1 headers args))
361 (notmuch-fcc-header-setup)
362 (message-sort-headers)
363 (message-hide-headers)
364 (set-buffer-modified-p nil)
365 (notmuch-mua-maybe-set-window-dedicated)
369 (defcustom notmuch-identities nil
370 "Identities that can be used as the From: address when composing a new message.
372 If this variable is left unset, then a list will be constructed from the
373 name and addresses configured in the notmuch configuration file."
374 :type '(repeat string)
375 :group 'notmuch-send)
377 (defcustom notmuch-always-prompt-for-sender nil
378 "Always prompt for the From: address when composing or forwarding a message.
380 This is not taken into account when replying to a message, because in that case
381 the From: header is already filled in by notmuch."
383 :group 'notmuch-send)
385 (defvar notmuch-mua-sender-history nil)
387 ;; Workaround: Running `ido-completing-read' in emacs 23.1, 23.2 and 23.3
388 ;; without some explicit initialization fill freeze the operation.
389 ;; Hence, we advice `ido-completing-read' to ensure required initialization
391 (if (and (= emacs-major-version 23) (< emacs-minor-version 4))
392 (defadvice ido-completing-read (before notmuch-ido-mode-init activate)
393 (ido-init-completion-maps)
394 (add-hook 'minibuffer-setup-hook 'ido-minibuffer-setup)
395 (add-hook 'choose-completion-string-functions
396 'ido-choose-completion-string)
397 (ad-disable-advice 'ido-completing-read 'before 'notmuch-ido-mode-init)
398 (ad-activate 'ido-completing-read)))
400 (defun notmuch-mua-prompt-for-sender ()
401 "Prompt for a sender from the user's configured identities."
402 (if notmuch-identities
403 (ido-completing-read "Send mail from: " notmuch-identities
404 nil nil nil 'notmuch-mua-sender-history
405 (car notmuch-identities))
406 (let* ((name (notmuch-user-name))
407 (addrs (cons (notmuch-user-primary-email)
408 (notmuch-user-other-email)))
410 (ido-completing-read (concat "Sender address for " name ": ") addrs
411 nil nil nil 'notmuch-mua-sender-history
413 (message-make-from name address))))
415 (put 'notmuch-mua-new-mail 'notmuch-prefix-doc "... and prompt for sender")
416 (defun notmuch-mua-new-mail (&optional prompt-for-sender)
419 If PROMPT-FOR-SENDER is non-nil, the user will be prompted for
420 the From: address first."
423 (when (or prompt-for-sender notmuch-always-prompt-for-sender)
424 (list (cons 'From (notmuch-mua-prompt-for-sender))))))
425 (notmuch-mua-mail nil nil other-headers nil (notmuch-mua-get-switch-function))))
427 (defun notmuch-mua-new-forward-messages (messages &optional prompt-for-sender)
428 "Compose a new message forwarding MESSAGES.
430 If PROMPT-FOR-SENDER is non-nil, the user will be prompteed for
432 (let* ((other-headers
433 (when (or prompt-for-sender notmuch-always-prompt-for-sender)
434 (list (cons 'From (notmuch-mua-prompt-for-sender)))))
435 forward-subject) ;; Comes from the first message and is
438 ;; Generate the template for the outgoing message.
439 (notmuch-mua-mail nil "" other-headers nil (notmuch-mua-get-switch-function))
442 ;; Insert all of the forwarded messages.
444 (let ((temp-buffer (get-buffer-create
445 (concat "*notmuch-fwd-raw-" id "*"))))
446 ;; Get the raw version of this message in the buffer.
447 (with-current-buffer temp-buffer
449 (let ((coding-system-for-read 'no-conversion))
450 (call-process notmuch-command nil t nil "show" "--format=raw" id))
451 ;; Because we process the messages in reverse order,
452 ;; always generate a forwarded subject, then use the
453 ;; last (i.e. first) one.
454 (setq forward-subject (message-make-forward-subject)))
455 ;; Make a copy ready to be forwarded in the
456 ;; composition buffer.
457 (message-forward-make-body temp-buffer)
458 ;; Kill the temporary buffer.
459 (kill-buffer temp-buffer)))
460 ;; `message-forward-make-body' always puts the message at
461 ;; the top, so do them in reverse order.
464 ;; Add in the appropriate subject.
466 (message-narrow-to-headers)
467 (message-remove-header "Subject")
468 (message-add-header (concat "Subject: " forward-subject)))
470 ;; `message-forward-make-body' shows the User-agent header. Hide
472 (message-hide-headers)
473 (set-buffer-modified-p nil))))
475 (defun notmuch-mua-new-reply (query-string &optional prompt-for-sender reply-all)
476 "Compose a reply to the message identified by QUERY-STRING.
478 If PROMPT-FOR-SENDER is non-nil, the user will be prompted for
479 the From: address first. If REPLY-ALL is non-nil, the message
480 will be addressed to all recipients of the source message."
482 ;; In current emacs (24.3) select-active-regions is set to t by
483 ;; default. The reply insertion code sets the region to the quoted
484 ;; message to make it easy to delete (kill-region or C-w). These two
485 ;; things combine to put the quoted message in the primary selection.
487 ;; This is not what the user wanted and is a privacy risk (accidental
488 ;; pasting of the quoted message). We can avoid some of the problems
489 ;; by let-binding select-active-regions to nil. This fixes if the
490 ;; primary selection was previously in a non-emacs window but not if
491 ;; it was in an emacs window. To avoid the problem in the latter case
492 ;; we deactivate mark.
495 (when prompt-for-sender
496 (notmuch-mua-prompt-for-sender)))
497 (select-active-regions nil))
498 (notmuch-mua-reply query-string sender reply-all)
501 (defun notmuch-mua-check-no-misplaced-secure-tag ()
502 "Query user if there is a misplaced secure mml tag.
504 Emacs message-send will (probably) ignore a secure mml tag unless
505 it is at the start of the body. Returns t if there is no such
506 tag, or the user confirms they mean it."
508 (let ((body-start (progn (message-goto-body) (point))))
509 (goto-char (point-max))
511 ;; We are always fine if there is no secure tag.
512 (not (search-backward "<#secure" nil 't))
513 ;; There is a secure tag, so it must be at the start of the
514 ;; body, with no secure tag earlier (i.e., in the headers).
515 (and (= (point) body-start)
516 (not (search-backward "<#secure" nil 't)))
517 ;; The user confirms they means it.
519 There is a <#secure> tag not at the start of the body. It is
520 likely that the message will be sent unsigned and unencrypted.
523 (defun notmuch-mua-check-secure-tag-has-newline ()
524 "Query if the secure mml tag has a newline following it.
526 Emacs message-send will (probably) ignore a correctly placed
527 secure mml tag unless it is followed by a newline. Returns t if
528 any secure tag is followed by a newline, or the user confirms
533 ;; There is no (correctly placed) secure tag.
534 (not (looking-at "<#secure"))
535 ;; The secure tag is followed by a newline.
536 (looking-at "<#secure[^\n>]*>\n")
537 ;; The user confirms they means it.
539 The <#secure> tag at the start of the body is not followed by a
540 newline. It is likely that the message will be sent unsigned and
541 unencrypted. Really send? "))))
543 (defun notmuch-mua-send-common (arg &optional exit)
545 (when (and (notmuch-mua-check-no-misplaced-secure-tag)
546 (notmuch-mua-check-secure-tag-has-newline))
547 (letf (((symbol-function 'message-do-fcc) #'notmuch-maildir-message-do-fcc))
549 (message-send-and-exit arg)
550 (message-send arg)))))
552 (defun notmuch-mua-send-and-exit (&optional arg)
554 (notmuch-mua-send-common arg 't))
556 (defun notmuch-mua-send (&optional arg)
558 (notmuch-mua-send-common arg))
560 (defun notmuch-mua-kill-buffer ()
562 (message-kill-buffer))
564 (defun notmuch-mua-message-send-hook ()
565 "The default function used for `notmuch-mua-send-hook', this
566 simply runs the corresponding `message-mode' hook functions."
567 (run-hooks 'message-send-hook))
571 (define-mail-user-agent 'notmuch-user-agent
572 'notmuch-mua-mail 'notmuch-mua-send-and-exit
573 'notmuch-mua-kill-buffer 'notmuch-mua-send-hook)
575 ;; Add some more headers to the list that `message-mode' hides when
576 ;; composing a message.
577 (notmuch-mua-add-more-hidden-headers)
581 (provide 'notmuch-mua)
583 ;;; notmuch-mua.el ends here