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 <http://www.gnu.org/licenses/>.
20 ;; Authors: David Edmondson <dme@dme.org>
24 (require 'format-spec)
26 (require 'notmuch-lib)
27 (require 'notmuch-address)
29 (eval-when-compile (require 'cl))
33 (defcustom notmuch-mua-send-hook '(notmuch-mua-message-send-hook)
34 "Hook run before sending messages."
37 :group 'notmuch-hooks)
39 (defcustom notmuch-mua-user-agent-function 'notmuch-mua-user-agent-full
40 "Function used to generate a `User-Agent:' string. If this is
41 `nil' then no `User-Agent:' will be generated."
42 :type '(choice (const :tag "No user agent string" nil)
43 (const :tag "Full" notmuch-mua-user-agent-full)
44 (const :tag "Notmuch" notmuch-mua-user-agent-notmuch)
45 (const :tag "Emacs" notmuch-mua-user-agent-emacs)
46 (function :tag "Custom user agent function"
47 :value notmuch-mua-user-agent-full))
50 (defcustom notmuch-mua-hidden-headers '("^User-Agent:")
51 "Headers that are added to the `message-mode' hidden headers
53 :type '(repeat string)
58 (defun notmuch-mua-user-agent-full ()
59 "Generate a `User-Agent:' string suitable for notmuch."
60 (concat (notmuch-mua-user-agent-notmuch)
62 (notmuch-mua-user-agent-emacs)))
64 (defun notmuch-mua-user-agent-notmuch ()
65 "Generate a `User-Agent:' string suitable for notmuch."
66 (concat "Notmuch/" (notmuch-version) " (http://notmuchmail.org)"))
68 (defun notmuch-mua-user-agent-emacs ()
69 "Generate a `User-Agent:' string suitable for notmuch."
70 (concat "Emacs/" emacs-version " (" system-configuration ")"))
72 (defun notmuch-mua-add-more-hidden-headers ()
73 "Add some headers to the list that are hidden by default."
74 (mapc (lambda (header)
75 (when (not (member header message-hidden-headers))
76 (push header message-hidden-headers)))
77 notmuch-mua-hidden-headers))
79 (defun notmuch-mua-get-quotable-parts (parts)
80 (loop for part in parts
81 if (notmuch-match-content-type (plist-get part :content-type) "multipart/alternative")
82 collect (let* ((subparts (plist-get part :content))
83 (types (mapcar (lambda (part) (plist-get part :content-type)) subparts))
84 (chosen-type (car (notmuch-multipart/alternative-choose types))))
85 (loop for part in (reverse subparts)
86 if (notmuch-match-content-type (plist-get part :content-type) chosen-type)
88 else if (notmuch-match-content-type (plist-get part :content-type) "multipart/*")
89 append (notmuch-mua-get-quotable-parts (plist-get part :content))
90 else if (notmuch-match-content-type (plist-get part :content-type) "text/*")
93 (defun notmuch-mua-reply (query-string &optional sender reply-all)
94 (let ((args '("reply" "--format=json"))
97 (when notmuch-show-process-crypto
98 (setq args (append args '("--decrypt"))))
101 (setq args (append args '("--reply-to=all")))
102 (setq args (append args '("--reply-to=sender"))))
103 (setq args (append args (list query-string)))
105 ;; Get the reply object as JSON, and parse it into an elisp object.
107 (apply 'call-process (append (list notmuch-command nil (list t t) nil) args))
108 (goto-char (point-min))
109 (let ((json-object-type 'plist)
110 (json-array-type 'list)
112 (setq reply (json-read))))
114 ;; Extract the original message to simplify the following code.
115 (setq original (plist-get reply :original))
117 ;; Extract the headers of both the reply and the original message.
118 (let* ((original-headers (plist-get original :headers))
119 (reply-headers (plist-get reply :reply-headers)))
121 ;; If sender is non-nil, set the From: header to its value.
123 (plist-put reply-headers :From sender))
125 ;; Overlay the composition window on that being used to read
126 ;; the original message.
127 ((same-window-regexps '("\\*mail .*")))
128 (notmuch-mua-mail (plist-get reply-headers :To)
129 (plist-get reply-headers :Subject)
130 (notmuch-plist-to-alist reply-headers)))
131 ;; Insert the message body - but put it in front of the signature
133 (goto-char (point-max))
134 (if (re-search-backward message-signature-separator nil t)
136 (goto-char (point-max)))
138 (let ((from (plist-get original-headers :From))
139 (date (plist-get original-headers :Date))
142 ;; message-cite-original constructs a citation line based on the From and Date
143 ;; headers of the original message, which are assumed to be in the buffer.
144 (insert "From: " from "\n")
145 (insert "Date: " date "\n\n")
147 ;; Get the parts of the original message that should be quoted; this includes
148 ;; all the text parts, except the non-preferred ones in a multipart/alternative.
149 (let ((quotable-parts (notmuch-mua-get-quotable-parts (plist-get original :body))))
151 (insert (notmuch-get-bodypart-content original part
153 notmuch-show-process-crypto)))
158 ;; Quote the original message according to the user's configured style.
159 (message-cite-original))))
161 (goto-char (point-max))
164 (set-buffer-modified-p nil))
166 (defun notmuch-mua-forward-message ()
169 (when notmuch-mua-user-agent-function
170 (let ((user-agent (funcall notmuch-mua-user-agent-function)))
171 (when (not (string= "" user-agent))
172 (message-add-header (format "User-Agent: %s" user-agent)))))
173 (message-sort-headers)
174 (message-hide-headers)
175 (set-buffer-modified-p nil)
179 (defun notmuch-mua-mail (&optional to subject other-headers &rest other-args)
180 "Invoke the notmuch mail composition window.
182 OTHER-ARGS are passed through to `message-mail'."
185 (when notmuch-mua-user-agent-function
186 (let ((user-agent (funcall notmuch-mua-user-agent-function)))
187 (when (not (string= "" user-agent))
188 (push (cons "User-Agent" user-agent) other-headers))))
190 (unless (mail-header 'From other-headers)
191 (push (cons "From" (concat
192 (notmuch-user-name) " <" (notmuch-user-primary-email) ">")) other-headers))
194 (apply #'message-mail to subject other-headers other-args)
195 (message-sort-headers)
196 (message-hide-headers)
197 (set-buffer-modified-p nil)
201 (defcustom notmuch-identities nil
202 "Identities that can be used as the From: address when composing a new message.
204 If this variable is left unset, then a list will be constructed from the
205 name and addresses configured in the notmuch configuration file."
206 :type '(repeat string)
207 :group 'notmuch-send)
209 (defcustom notmuch-always-prompt-for-sender nil
210 "Always prompt for the From: address when composing or forwarding a message.
212 This is not taken into account when replying to a message, because in that case
213 the From: header is already filled in by notmuch."
215 :group 'notmuch-send)
217 (defvar notmuch-mua-sender-history nil)
219 (defun notmuch-mua-prompt-for-sender ()
221 (let (name addresses one-name-only)
222 ;; If notmuch-identities is non-nil, check if there is a fixed user name.
223 (if notmuch-identities
224 (let ((components (mapcar 'mail-extract-address-components notmuch-identities)))
225 (setq name (caar components)
226 addresses (mapcar 'cadr components)
229 (mapcar (lambda (identity)
230 (string-equal name (car identity)))
232 ;; If notmuch-identities is nil, use values from the notmuch configuration file.
233 (setq name (notmuch-user-name)
234 addresses (cons (notmuch-user-primary-email) (notmuch-user-other-email))
236 ;; Now prompt the user, either for an email address only or for a full identity.
239 (ido-completing-read (concat "Sender address for " name ": ") addresses
240 nil nil nil 'notmuch-mua-sender-history (car addresses))))
241 (concat name " <" address ">"))
242 (ido-completing-read "Send mail From: " notmuch-identities
243 nil nil nil 'notmuch-mua-sender-history (car notmuch-identities)))))
245 (defun notmuch-mua-new-mail (&optional prompt-for-sender)
246 "Invoke the notmuch mail composition window.
248 If PROMPT-FOR-SENDER is non-nil, the user will be prompted for
249 the From: address first."
252 (when (or prompt-for-sender notmuch-always-prompt-for-sender)
253 (list (cons 'From (notmuch-mua-prompt-for-sender))))))
254 (notmuch-mua-mail nil nil other-headers)))
256 (defun notmuch-mua-new-forward-message (&optional prompt-for-sender)
257 "Invoke the notmuch message forwarding window.
259 If PROMPT-FOR-SENDER is non-nil, the user will be prompted for
260 the From: address first."
262 (if (or prompt-for-sender notmuch-always-prompt-for-sender)
263 (let* ((sender (notmuch-mua-prompt-for-sender))
264 (address-components (mail-extract-address-components sender))
265 (user-full-name (car address-components))
266 (user-mail-address (cadr address-components)))
267 (notmuch-mua-forward-message))
268 (notmuch-mua-forward-message)))
270 (defun notmuch-mua-new-reply (query-string &optional prompt-for-sender reply-all)
271 "Invoke the notmuch reply window."
274 (when prompt-for-sender
275 (notmuch-mua-prompt-for-sender))))
276 (notmuch-mua-reply query-string sender reply-all)))
278 (defun notmuch-mua-send-and-exit (&optional arg)
280 (message-send-and-exit arg))
282 (defun notmuch-mua-kill-buffer ()
284 (message-kill-buffer))
286 (defun notmuch-mua-message-send-hook ()
287 "The default function used for `notmuch-mua-send-hook', this
288 simply runs the corresponding `message-mode' hook functions."
289 (run-hooks 'message-send-hook))
293 (define-mail-user-agent 'notmuch-user-agent
294 'notmuch-mua-mail 'notmuch-mua-send-and-exit
295 'notmuch-mua-kill-buffer 'notmuch-mua-send-hook)
297 ;; Add some more headers to the list that `message-mode' hides when
298 ;; composing a message.
299 (notmuch-mua-add-more-hidden-headers)
303 (provide 'notmuch-mua)