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" ())
39 (defcustom notmuch-mua-send-hook '(notmuch-mua-message-send-hook)
40 "Hook run before sending messages."
43 :group 'notmuch-hooks)
45 (defcustom notmuch-mua-compose-in 'current-window
47 "Where to create the mail buffer used to compose a new message.
48 Possible values are `current-window' (default), `new-window' and
49 `new-frame'. If set to `current-window', the mail buffer will be
50 displayed in the current window, so the old buffer will be
51 restored when the mail buffer is killed. If set to `new-window'
52 or `new-frame', the mail buffer will be displayed in a new
53 window/frame that will be destroyed when the buffer is killed.
54 You may want to customize `message-kill-buffer-on-exit'
56 (when (< emacs-major-version 24)
57 " Due to a known bug in Emacs 23, you should not set
58 this to `new-window' if `message-kill-buffer-on-exit' is
59 disabled: this would result in an incorrect behavior."))
61 :type '(choice (const :tag "Compose in the current window" current-window)
62 (const :tag "Compose mail in a new window" new-window)
63 (const :tag "Compose mail in a new frame" new-frame)))
65 (defcustom notmuch-mua-user-agent-function nil
66 "Function used to generate a `User-Agent:' string. If this is
67 `nil' then no `User-Agent:' will be generated."
68 :type '(choice (const :tag "No user agent string" nil)
69 (const :tag "Full" notmuch-mua-user-agent-full)
70 (const :tag "Notmuch" notmuch-mua-user-agent-notmuch)
71 (const :tag "Emacs" notmuch-mua-user-agent-emacs)
72 (function :tag "Custom user agent function"
73 :value notmuch-mua-user-agent-full))
76 (defcustom notmuch-mua-hidden-headers nil
77 "Headers that are added to the `message-mode' hidden headers
79 :type '(repeat string)
82 (defgroup notmuch-reply nil
83 "Replying to messages in notmuch"
86 (defcustom notmuch-mua-cite-function 'message-cite-original
87 "*Function for citing an original message.
88 Predefined functions include `message-cite-original' and
89 `message-cite-original-without-signature'.
90 Note that these functions use `mail-citation-hook' if that is non-nil."
91 :type '(radio (function-item message-cite-original)
92 (function-item message-cite-original-without-signature)
93 (function-item sc-cite-original)
94 (function :tag "Other"))
95 :link '(custom-manual "(message)Insertion Variables")
96 :group 'notmuch-reply)
98 (defcustom notmuch-mua-reply-insert-header-p-function
99 'notmuch-show-reply-insert-header-p-never
100 "Function to decide which parts get a header when replying.
102 This function specifies which parts of a mime message with
103 mutiple parts get a header."
104 :type '(radio (const :tag "No part headers"
105 notmuch-show-reply-insert-header-p-never)
106 (const :tag "All except multipart/* and hidden parts"
107 notmuch-show-reply-insert-header-p-trimmed)
108 (const :tag "Only for included text parts"
109 notmuch-show-reply-insert-header-p-minimal)
110 (const :tag "Exactly as in show view"
111 notmuch-show-insert-header-p)
112 (function :tag "Other"))
113 :group 'notmuch-reply)
117 (defun notmuch-mua-get-switch-function ()
118 "Get a switch function according to `notmuch-mua-compose-in'."
119 (cond ((eq notmuch-mua-compose-in 'current-window)
121 ((eq notmuch-mua-compose-in 'new-window)
122 'switch-to-buffer-other-window)
123 ((eq notmuch-mua-compose-in 'new-frame)
124 'switch-to-buffer-other-frame)
125 (t (error "Invalid value for `notmuch-mua-compose-in'"))))
127 (defun notmuch-mua-maybe-set-window-dedicated ()
128 "Set the selected window as dedicated according to
129 `notmuch-mua-compose-in'."
130 (when (or (eq notmuch-mua-compose-in 'new-frame)
131 (eq notmuch-mua-compose-in 'new-window))
132 (set-window-dedicated-p (selected-window) t)))
134 (defun notmuch-mua-user-agent-full ()
135 "Generate a `User-Agent:' string suitable for notmuch."
136 (concat (notmuch-mua-user-agent-notmuch)
138 (notmuch-mua-user-agent-emacs)))
140 (defun notmuch-mua-user-agent-notmuch ()
141 "Generate a `User-Agent:' string suitable for notmuch."
142 (let ((notmuch-version (if (string= notmuch-emacs-version "unknown")
143 (notmuch-cli-version)
144 notmuch-emacs-version)))
145 (concat "Notmuch/" notmuch-version " (https://notmuchmail.org)")))
147 (defun notmuch-mua-user-agent-emacs ()
148 "Generate a `User-Agent:' string suitable for notmuch."
149 (concat "Emacs/" emacs-version " (" system-configuration ")"))
151 (defun notmuch-mua-add-more-hidden-headers ()
152 "Add some headers to the list that are hidden by default."
153 (mapc (lambda (header)
154 (when (not (member header message-hidden-headers))
155 (push header message-hidden-headers)))
156 notmuch-mua-hidden-headers))
158 (defun notmuch-mua-reply-crypto (parts)
159 "Add mml sign-encrypt flag if any part of original message is encrypted."
160 (loop for part in parts
161 if (notmuch-match-content-type (plist-get part :content-type) "multipart/encrypted")
162 do (mml-secure-message-sign-encrypt)
163 else if (notmuch-match-content-type (plist-get part :content-type) "multipart/*")
164 do (notmuch-mua-reply-crypto (plist-get part :content))))
166 ;; There is a bug in emacs 23's message.el that results in a newline
167 ;; not being inserted after the References header, so the next header
168 ;; is concatenated to the end of it. This function fixes the problem,
169 ;; while guarding against the possibility that some current or future
170 ;; version of emacs has the bug fixed.
171 (defun notmuch-mua-insert-references (original-func header references)
172 (funcall original-func header references)
173 (unless (bolp) (insert "\n")))
175 (defun notmuch-mua-reply (query-string &optional sender reply-all)
176 (let ((args '("reply" "--format=sexp" "--format-version=1"))
177 (process-crypto notmuch-show-process-crypto)
181 (setq args (append args '("--decrypt"))))
184 (setq args (append args '("--reply-to=all")))
185 (setq args (append args '("--reply-to=sender"))))
186 (setq args (append args (list query-string)))
188 ;; Get the reply object as SEXP, and parse it into an elisp object.
189 (setq reply (apply #'notmuch-call-notmuch-sexp args))
191 ;; Extract the original message to simplify the following code.
192 (setq original (plist-get reply :original))
194 ;; Extract the headers of both the reply and the original message.
195 (let* ((original-headers (plist-get original :headers))
196 (reply-headers (plist-get reply :reply-headers)))
198 ;; If sender is non-nil, set the From: header to its value.
200 (plist-put reply-headers :From sender))
202 ;; Overlay the composition window on that being used to read
203 ;; the original message.
204 ((same-window-regexps '("\\*mail .*")))
206 ;; We modify message-header-format-alist to get around a bug in message.el.
207 ;; See the comment above on notmuch-mua-insert-references.
208 (let ((message-header-format-alist
209 (loop for pair in message-header-format-alist
210 if (eq (car pair) 'References)
211 collect (cons 'References
213 'notmuch-mua-insert-references
217 (notmuch-mua-mail (plist-get reply-headers :To)
218 (plist-get reply-headers :Subject)
219 (notmuch-headers-plist-to-alist reply-headers)
220 nil (notmuch-mua-get-switch-function))))
222 ;; Insert the message body - but put it in front of the signature
223 ;; if one is present, and after any other content
224 ;; message*setup-hooks may have added to the message body already.
227 (narrow-to-region (point) (point-max))
228 (goto-char (point-max))
229 (if (re-search-backward message-signature-separator nil t)
230 (if message-signature-insert-empty-line
232 (goto-char (point-max))))
234 (let ((from (plist-get original-headers :From))
235 (date (plist-get original-headers :Date))
238 ;; notmuch-mua-cite-function constructs a citation line based
239 ;; on the From and Date headers of the original message, which
240 ;; are assumed to be in the buffer.
241 (insert "From: " from "\n")
242 (insert "Date: " date "\n\n")
244 (insert (with-temp-buffer
246 ;; Don't attempt to clean up messages, excerpt
247 ;; citations, etc. in the original message before
249 ((notmuch-show-insert-text/plain-hook nil)
250 ;; Don't omit long parts.
251 (notmuch-show-max-text-part-size 0)
252 ;; Insert headers for parts as appropriate for replying.
253 (notmuch-show-insert-header-p-function notmuch-mua-reply-insert-header-p-function)
254 ;; Ensure that any encrypted parts are
255 ;; decrypted during the generation of the reply
257 (notmuch-show-process-crypto process-crypto)
258 ;; Don't indent multipart sub-parts.
259 (notmuch-show-indent-multipart nil))
260 ;; We don't want sigstatus buttons (an information leak and usually wrong anyway).
261 (letf (((symbol-function 'notmuch-crypto-insert-sigstatus-button) #'ignore)
262 ((symbol-function 'notmuch-crypto-insert-encstatus-button) #'ignore))
263 (notmuch-show-insert-body original (plist-get original :body) 0)
264 (buffer-substring-no-properties (point-min) (point-max))))))
268 ;; Quote the original message according to the user's configured style.
269 (funcall notmuch-mua-cite-function)))
271 ;; Crypto processing based crypto content of the original message
273 (notmuch-mua-reply-crypto (plist-get original :body))))
275 ;; Push mark right before signature, if any.
276 (message-goto-signature)
282 (set-buffer-modified-p nil))
284 (define-derived-mode notmuch-message-mode message-mode "Message[Notmuch]"
285 "Notmuch message composition mode. Mostly like `message-mode'"
286 (notmuch-address-setup))
288 (put 'notmuch-message-mode 'flyspell-mode-predicate 'mail-mode-flyspell-verify)
290 (define-key notmuch-message-mode-map (kbd "C-c C-c") #'notmuch-mua-send-and-exit)
291 (define-key notmuch-message-mode-map (kbd "C-c C-s") #'notmuch-mua-send)
293 (defun notmuch-mua-pop-to-buffer (name switch-function)
294 "Pop to buffer NAME, and warn if it already exists and is
295 modified. This function is notmuch addaptation of
296 `message-pop-to-buffer'."
297 (let ((buffer (get-buffer name)))
299 (buffer-name buffer))
300 (let ((window (get-buffer-window buffer 0)))
302 ;; Raise the frame already displaying the message buffer.
304 (gnus-select-frame-set-input-focus (window-frame window))
305 (select-window window))
306 (funcall switch-function buffer)
308 (when (and (buffer-modified-p)
311 "Message already being composed; erase? ")
313 (error "Message being composed")))
314 (funcall switch-function name)
317 (notmuch-message-mode)))
319 (defun notmuch-mua-mail (&optional to subject other-headers continue
320 switch-function yank-action send-actions
321 return-action &rest ignored)
322 "Invoke the notmuch mail composition window."
325 (when notmuch-mua-user-agent-function
326 (let ((user-agent (funcall notmuch-mua-user-agent-function)))
327 (when (not (string= "" user-agent))
328 (push (cons 'User-Agent user-agent) other-headers))))
330 (unless (assq 'From other-headers)
331 (push (cons 'From (message-make-from
332 (notmuch-user-name) (notmuch-user-primary-email))) other-headers))
334 (notmuch-mua-pop-to-buffer (message-buffer-name "mail" to)
335 (or switch-function (notmuch-mua-get-switch-function)))
338 ;; The following is copied from `message-mail'
339 `((To . ,(or to "")) (Subject . ,(or subject "")))
340 ;; C-h f compose-mail says that headers should be specified as
341 ;; (string . value); however all the rest of message expects
342 ;; headers to be symbols, not strings (eg message-header-format-alist).
343 ;; https://lists.gnu.org/archive/html/emacs-devel/2011-01/msg00337.html
344 ;; We need to convert any string input, eg from rmail-start-mail.
345 (dolist (h other-headers other-headers)
346 (if (stringp (car h)) (setcar h (intern (capitalize (car h))))))))
347 (args (list yank-action send-actions))
348 ;; Cause `message-setup-1' to do things relevant for mail,
349 ;; such as observe `message-default-mail-headers'.
350 (message-this-is-mail t))
351 ;; message-setup-1 in Emacs 23 does not accept return-action
352 ;; argument. Pass it only if it is supplied by the caller. This
353 ;; will never be the case when we're called by `compose-mail' in
355 (when return-action (nconc args '(return-action)))
356 (apply 'message-setup-1 headers args))
357 (notmuch-fcc-header-setup)
358 (message-sort-headers)
359 (message-hide-headers)
360 (set-buffer-modified-p nil)
361 (notmuch-mua-maybe-set-window-dedicated)
365 (defcustom notmuch-identities nil
366 "Identities that can be used as the From: address when composing a new message.
368 If this variable is left unset, then a list will be constructed from the
369 name and addresses configured in the notmuch configuration file."
370 :type '(repeat string)
371 :group 'notmuch-send)
373 (defcustom notmuch-always-prompt-for-sender nil
374 "Always prompt for the From: address when composing or forwarding a message.
376 This is not taken into account when replying to a message, because in that case
377 the From: header is already filled in by notmuch."
379 :group 'notmuch-send)
381 (defvar notmuch-mua-sender-history nil)
383 ;; Workaround: Running `ido-completing-read' in emacs 23.1, 23.2 and 23.3
384 ;; without some explicit initialization fill freeze the operation.
385 ;; Hence, we advice `ido-completing-read' to ensure required initialization
387 (if (and (= emacs-major-version 23) (< emacs-minor-version 4))
388 (defadvice ido-completing-read (before notmuch-ido-mode-init activate)
389 (ido-init-completion-maps)
390 (add-hook 'minibuffer-setup-hook 'ido-minibuffer-setup)
391 (add-hook 'choose-completion-string-functions
392 'ido-choose-completion-string)
393 (ad-disable-advice 'ido-completing-read 'before 'notmuch-ido-mode-init)
394 (ad-activate 'ido-completing-read)))
396 (defun notmuch-mua-prompt-for-sender ()
397 "Prompt for a sender from the user's configured identities."
398 (if notmuch-identities
399 (ido-completing-read "Send mail from: " notmuch-identities
400 nil nil nil 'notmuch-mua-sender-history
401 (car notmuch-identities))
402 (let* ((name (notmuch-user-name))
403 (addrs (cons (notmuch-user-primary-email)
404 (notmuch-user-other-email)))
406 (ido-completing-read (concat "Sender address for " name ": ") addrs
407 nil nil nil 'notmuch-mua-sender-history
409 (message-make-from name address))))
411 (put 'notmuch-mua-new-mail 'notmuch-prefix-doc "... and prompt for sender")
412 (defun notmuch-mua-new-mail (&optional prompt-for-sender)
415 If PROMPT-FOR-SENDER is non-nil, the user will be prompted for
416 the From: address first."
419 (when (or prompt-for-sender notmuch-always-prompt-for-sender)
420 (list (cons 'From (notmuch-mua-prompt-for-sender))))))
421 (notmuch-mua-mail nil nil other-headers nil (notmuch-mua-get-switch-function))))
423 (defun notmuch-mua-new-forward-messages (messages &optional prompt-for-sender)
424 "Compose a new message forwarding MESSAGES.
426 If PROMPT-FOR-SENDER is non-nil, the user will be prompteed for
428 (let* ((other-headers
429 (when (or prompt-for-sender notmuch-always-prompt-for-sender)
430 (list (cons 'From (notmuch-mua-prompt-for-sender)))))
431 forward-subject) ;; Comes from the first message and is
434 ;; Generate the template for the outgoing message.
435 (notmuch-mua-mail nil "" other-headers nil (notmuch-mua-get-switch-function))
438 ;; Insert all of the forwarded messages.
440 (let ((temp-buffer (get-buffer-create
441 (concat "*notmuch-fwd-raw-" id "*"))))
442 ;; Get the raw version of this message in the buffer.
443 (with-current-buffer temp-buffer
445 (let ((coding-system-for-read 'no-conversion))
446 (call-process notmuch-command nil t nil "show" "--format=raw" id))
447 ;; Because we process the messages in reverse order,
448 ;; always generate a forwarded subject, then use the
449 ;; last (i.e. first) one.
450 (setq forward-subject (message-make-forward-subject)))
451 ;; Make a copy ready to be forwarded in the
452 ;; composition buffer.
453 (message-forward-make-body temp-buffer)
454 ;; Kill the temporary buffer.
455 (kill-buffer temp-buffer)))
456 ;; `message-forward-make-body' always puts the message at
457 ;; the top, so do them in reverse order.
460 ;; Add in the appropriate subject.
462 (message-narrow-to-headers)
463 (message-remove-header "Subject")
464 (message-add-header (concat "Subject: " forward-subject)))
466 ;; `message-forward-make-body' shows the User-agent header. Hide
468 (message-hide-headers)
469 (set-buffer-modified-p nil))))
471 (defun notmuch-mua-new-reply (query-string &optional prompt-for-sender reply-all)
472 "Compose a reply to the message identified by QUERY-STRING.
474 If PROMPT-FOR-SENDER is non-nil, the user will be prompted for
475 the From: address first. If REPLY-ALL is non-nil, the message
476 will be addressed to all recipients of the source message."
478 ;; In current emacs (24.3) select-active-regions is set to t by
479 ;; default. The reply insertion code sets the region to the quoted
480 ;; message to make it easy to delete (kill-region or C-w). These two
481 ;; things combine to put the quoted message in the primary selection.
483 ;; This is not what the user wanted and is a privacy risk (accidental
484 ;; pasting of the quoted message). We can avoid some of the problems
485 ;; by let-binding select-active-regions to nil. This fixes if the
486 ;; primary selection was previously in a non-emacs window but not if
487 ;; it was in an emacs window. To avoid the problem in the latter case
488 ;; we deactivate mark.
491 (when prompt-for-sender
492 (notmuch-mua-prompt-for-sender)))
493 (select-active-regions nil))
494 (notmuch-mua-reply query-string sender reply-all)
497 (defun notmuch-mua-check-no-misplaced-secure-tag ()
498 "Query user if there is a misplaced secure mml tag.
500 Emacs message-send will (probably) ignore a secure mml tag unless
501 it is at the start of the body. Returns t if there is no such
502 tag, or the user confirms they mean it."
504 (let ((body-start (progn (message-goto-body) (point))))
505 (goto-char (point-max))
507 ;; We are always fine if there is no secure tag.
508 (not (search-backward "<#secure" nil 't))
509 ;; There is a secure tag, so it must be at the start of the
510 ;; body, with no secure tag earlier (i.e., in the headers).
511 (and (= (point) body-start)
512 (not (search-backward "<#secure" nil 't)))
513 ;; The user confirms they means it.
515 There is a <#secure> tag not at the start of the body. It is
516 likely that the message will be sent unsigned and unencrypted.
519 (defun notmuch-mua-check-secure-tag-has-newline ()
520 "Query if the secure mml tag has a newline following it.
522 Emacs message-send will (probably) ignore a correctly placed
523 secure mml tag unless it is followed by a newline. Returns t if
524 any secure tag is followed by a newline, or the user confirms
529 ;; There is no (correctly placed) secure tag.
530 (not (looking-at "<#secure"))
531 ;; The secure tag is followed by a newline.
532 (looking-at "<#secure[^\n>]*>\n")
533 ;; The user confirms they means it.
535 The <#secure> tag at the start of the body is not followed by a
536 newline. It is likely that the message will be sent unsigned and
537 unencrypted. Really send? "))))
539 (defun notmuch-mua-send-common (arg &optional exit)
541 (when (and (notmuch-mua-check-no-misplaced-secure-tag)
542 (notmuch-mua-check-secure-tag-has-newline))
543 (letf (((symbol-function 'message-do-fcc) #'notmuch-maildir-message-do-fcc))
545 (message-send-and-exit arg)
546 (message-send arg)))))
548 (defun notmuch-mua-send-and-exit (&optional arg)
550 (notmuch-mua-send-common arg 't))
552 (defun notmuch-mua-send (&optional arg)
554 (notmuch-mua-send-common arg))
556 (defun notmuch-mua-kill-buffer ()
558 (message-kill-buffer))
560 (defun notmuch-mua-message-send-hook ()
561 "The default function used for `notmuch-mua-send-hook', this
562 simply runs the corresponding `message-mode' hook functions."
563 (run-hooks 'message-send-hook))
567 (define-mail-user-agent 'notmuch-user-agent
568 'notmuch-mua-mail 'notmuch-mua-send-and-exit
569 'notmuch-mua-kill-buffer 'notmuch-mua-send-hook)
571 ;; Add some more headers to the list that `message-mode' hides when
572 ;; composing a message.
573 (notmuch-mua-add-more-hidden-headers)
577 (provide 'notmuch-mua)
579 ;;; notmuch-mua.el ends here