]> git.notmuchmail.org Git - notmuch/blob - emacs/notmuch-mua.el
emacs: Correctly quote non-text/plain parts in reply
[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 <http://www.gnu.org/licenses/>.
19 ;;
20 ;; Authors: David Edmondson <dme@dme.org>
21
22 (require 'json)
23 (require 'message)
24 (require 'mm-view)
25 (require 'format-spec)
26
27 (require 'notmuch-lib)
28 (require 'notmuch-address)
29
30 (eval-when-compile (require 'cl))
31
32 ;;
33
34 (defcustom notmuch-mua-send-hook '(notmuch-mua-message-send-hook)
35   "Hook run before sending messages."
36   :type 'hook
37   :group 'notmuch-send
38   :group 'notmuch-hooks)
39
40 (defcustom notmuch-mua-user-agent-function 'notmuch-mua-user-agent-full
41   "Function used to generate a `User-Agent:' string. If this is
42 `nil' then no `User-Agent:' will be generated."
43   :type '(choice (const :tag "No user agent string" nil)
44                  (const :tag "Full" notmuch-mua-user-agent-full)
45                  (const :tag "Notmuch" notmuch-mua-user-agent-notmuch)
46                  (const :tag "Emacs" notmuch-mua-user-agent-emacs)
47                  (function :tag "Custom user agent function"
48                            :value notmuch-mua-user-agent-full))
49   :group 'notmuch-send)
50
51 (defcustom notmuch-mua-hidden-headers '("^User-Agent:")
52   "Headers that are added to the `message-mode' hidden headers
53 list."
54   :type '(repeat string)
55   :group 'notmuch-send)
56
57 ;;
58
59 (defun notmuch-mua-user-agent-full ()
60   "Generate a `User-Agent:' string suitable for notmuch."
61   (concat (notmuch-mua-user-agent-notmuch)
62           " "
63           (notmuch-mua-user-agent-emacs)))
64
65 (defun notmuch-mua-user-agent-notmuch ()
66   "Generate a `User-Agent:' string suitable for notmuch."
67   (concat "Notmuch/" (notmuch-version) " (http://notmuchmail.org)"))
68
69 (defun notmuch-mua-user-agent-emacs ()
70   "Generate a `User-Agent:' string suitable for notmuch."
71   (concat "Emacs/" emacs-version " (" system-configuration ")"))
72
73 (defun notmuch-mua-add-more-hidden-headers ()
74   "Add some headers to the list that are hidden by default."
75   (mapc (lambda (header)
76           (when (not (member header message-hidden-headers))
77             (push header message-hidden-headers)))
78         notmuch-mua-hidden-headers))
79
80 (defun notmuch-mua-get-quotable-parts (parts)
81   (loop for part in parts
82         if (notmuch-match-content-type (plist-get part :content-type) "multipart/alternative")
83           collect (let* ((subparts (plist-get part :content))
84                         (types (mapcar (lambda (part) (plist-get part :content-type)) subparts))
85                         (chosen-type (car (notmuch-multipart/alternative-choose types))))
86                    (loop for part in (reverse subparts)
87                          if (notmuch-match-content-type (plist-get part :content-type) chosen-type)
88                          return part))
89         else if (notmuch-match-content-type (plist-get part :content-type) "multipart/*")
90           append (notmuch-mua-get-quotable-parts (plist-get part :content))
91         else if (notmuch-match-content-type (plist-get part :content-type) "text/*")
92           collect part))
93
94 (defun notmuch-mua-insert-quotable-part (message part)
95   (save-restriction
96     (narrow-to-region (point) (point))
97     (notmuch-mm-display-part-inline message part (plist-get part :id)
98                                     (plist-get part :content-type)
99                                     notmuch-show-process-crypto)
100     (goto-char (point-max))))
101
102 ;; There is a bug in emacs 23's message.el that results in a newline
103 ;; not being inserted after the References header, so the next header
104 ;; is concatenated to the end of it. This function fixes the problem,
105 ;; while guarding against the possibility that some current or future
106 ;; version of emacs has the bug fixed.
107 (defun notmuch-mua-insert-references (original-func header references)
108   (funcall original-func header references)
109   (unless (bolp) (insert "\n")))
110
111 (defun notmuch-mua-reply (query-string &optional sender reply-all)
112   (let ((args '("reply" "--format=json"))
113         reply
114         original)
115     (when notmuch-show-process-crypto
116       (setq args (append args '("--decrypt"))))
117
118     (if reply-all
119         (setq args (append args '("--reply-to=all")))
120       (setq args (append args '("--reply-to=sender"))))
121     (setq args (append args (list query-string)))
122
123     ;; Get the reply object as JSON, and parse it into an elisp object.
124     (with-temp-buffer
125       (apply 'call-process (append (list notmuch-command nil (list t t) nil) args))
126       (goto-char (point-min))
127       (let ((json-object-type 'plist)
128             (json-array-type 'list)
129             (json-false 'nil))
130         (setq reply (json-read))))
131
132     ;; Extract the original message to simplify the following code.
133     (setq original (plist-get reply :original))
134
135     ;; Extract the headers of both the reply and the original message.
136     (let* ((original-headers (plist-get original :headers))
137            (reply-headers (plist-get reply :reply-headers)))
138
139       ;; If sender is non-nil, set the From: header to its value.
140       (when sender
141         (plist-put reply-headers :From sender))
142       (let
143           ;; Overlay the composition window on that being used to read
144           ;; the original message.
145           ((same-window-regexps '("\\*mail .*")))
146
147         ;; We modify message-header-format-alist to get around a bug in message.el.
148         ;; See the comment above on notmuch-mua-insert-references.
149         (let ((message-header-format-alist
150                (loop for pair in message-header-format-alist
151                      if (eq (car pair) 'References)
152                      collect (cons 'References
153                                    (apply-partially
154                                     'notmuch-mua-insert-references
155                                     (cdr pair)))
156                      else
157                      collect pair)))
158           (notmuch-mua-mail (plist-get reply-headers :To)
159                             (plist-get reply-headers :Subject)
160                             (notmuch-headers-plist-to-alist reply-headers))))
161
162       ;; Insert the message body - but put it in front of the signature
163       ;; if one is present
164       (goto-char (point-max))
165       (if (re-search-backward message-signature-separator nil t)
166           (forward-line -1)
167         (goto-char (point-max)))
168
169       (let ((from (plist-get original-headers :From))
170             (date (plist-get original-headers :Date))
171             (start (point)))
172
173         ;; message-cite-original constructs a citation line based on the From and Date
174         ;; headers of the original message, which are assumed to be in the buffer.
175         (insert "From: " from "\n")
176         (insert "Date: " date "\n\n")
177
178         ;; Get the parts of the original message that should be quoted; this includes
179         ;; all the text parts, except the non-preferred ones in a multipart/alternative.
180         (let ((quotable-parts (notmuch-mua-get-quotable-parts (plist-get original :body))))
181           (mapc (apply-partially 'notmuch-mua-insert-quotable-part original) quotable-parts))
182
183         (set-mark (point))
184         (goto-char start)
185         ;; Quote the original message according to the user's configured style.
186         (message-cite-original))))
187
188   (goto-char (point-max))
189   (push-mark)
190   (message-goto-body)
191   (set-buffer-modified-p nil))
192
193 (defun notmuch-mua-forward-message ()
194   (message-forward)
195
196   (when notmuch-mua-user-agent-function
197     (let ((user-agent (funcall notmuch-mua-user-agent-function)))
198       (when (not (string= "" user-agent))
199         (message-add-header (format "User-Agent: %s" user-agent)))))
200   (message-sort-headers)
201   (message-hide-headers)
202   (set-buffer-modified-p nil)
203
204   (message-goto-to))
205
206 (defun notmuch-mua-mail (&optional to subject other-headers &rest other-args)
207   "Invoke the notmuch mail composition window.
208
209 OTHER-ARGS are passed through to `message-mail'."
210   (interactive)
211
212   (when notmuch-mua-user-agent-function
213     (let ((user-agent (funcall notmuch-mua-user-agent-function)))
214       (when (not (string= "" user-agent))
215         (push (cons 'User-Agent user-agent) other-headers))))
216
217   (unless (assq 'From other-headers)
218     (push (cons 'From (concat
219                        (notmuch-user-name) " <" (notmuch-user-primary-email) ">")) other-headers))
220
221   (apply #'message-mail to subject other-headers other-args)
222   (message-sort-headers)
223   (message-hide-headers)
224   (set-buffer-modified-p nil)
225
226   (message-goto-to))
227
228 (defcustom notmuch-identities nil
229   "Identities that can be used as the From: address when composing a new message.
230
231 If this variable is left unset, then a list will be constructed from the
232 name and addresses configured in the notmuch configuration file."
233   :type '(repeat string)
234   :group 'notmuch-send)
235
236 (defcustom notmuch-always-prompt-for-sender nil
237   "Always prompt for the From: address when composing or forwarding a message.
238
239 This is not taken into account when replying to a message, because in that case
240 the From: header is already filled in by notmuch."
241   :type 'boolean
242   :group 'notmuch-send)
243
244 (defvar notmuch-mua-sender-history nil)
245
246 (defun notmuch-mua-prompt-for-sender ()
247   (interactive)
248   (let (name addresses one-name-only)
249     ;; If notmuch-identities is non-nil, check if there is a fixed user name.
250     (if notmuch-identities
251         (let ((components (mapcar 'mail-extract-address-components notmuch-identities)))
252           (setq name          (caar components)
253                 addresses     (mapcar 'cadr components)
254                 one-name-only (eval
255                                (cons 'and
256                                      (mapcar (lambda (identity)
257                                                (string-equal name (car identity)))
258                                              components)))))
259       ;; If notmuch-identities is nil, use values from the notmuch configuration file.
260       (setq name          (notmuch-user-name)
261             addresses     (cons (notmuch-user-primary-email) (notmuch-user-other-email))
262             one-name-only t))
263     ;; Now prompt the user, either for an email address only or for a full identity.
264     (if one-name-only
265         (let ((address
266                (ido-completing-read (concat "Sender address for " name ": ") addresses
267                                     nil nil nil 'notmuch-mua-sender-history (car addresses))))
268           (concat name " <" address ">"))
269       (ido-completing-read "Send mail From: " notmuch-identities
270                            nil nil nil 'notmuch-mua-sender-history (car notmuch-identities)))))
271
272 (defun notmuch-mua-new-mail (&optional prompt-for-sender)
273   "Invoke the notmuch mail composition window.
274
275 If PROMPT-FOR-SENDER is non-nil, the user will be prompted for
276 the From: address first."
277   (interactive "P")
278   (let ((other-headers
279          (when (or prompt-for-sender notmuch-always-prompt-for-sender)
280            (list (cons 'From (notmuch-mua-prompt-for-sender))))))
281     (notmuch-mua-mail nil nil other-headers)))
282
283 (defun notmuch-mua-new-forward-message (&optional prompt-for-sender)
284   "Invoke the notmuch message forwarding window.
285
286 If PROMPT-FOR-SENDER is non-nil, the user will be prompted for
287 the From: address first."
288   (interactive "P")
289   (if (or prompt-for-sender notmuch-always-prompt-for-sender)
290       (let* ((sender (notmuch-mua-prompt-for-sender))
291              (address-components (mail-extract-address-components sender))
292              (user-full-name (car address-components))
293              (user-mail-address (cadr address-components)))
294         (notmuch-mua-forward-message))
295     (notmuch-mua-forward-message)))
296
297 (defun notmuch-mua-new-reply (query-string &optional prompt-for-sender reply-all)
298   "Invoke the notmuch reply window."
299   (interactive "P")
300   (let ((sender
301          (when prompt-for-sender
302            (notmuch-mua-prompt-for-sender))))
303     (notmuch-mua-reply query-string sender reply-all)))
304
305 (defun notmuch-mua-send-and-exit (&optional arg)
306   (interactive "P")
307   (message-send-and-exit arg))
308
309 (defun notmuch-mua-kill-buffer ()
310   (interactive)
311   (message-kill-buffer))
312
313 (defun notmuch-mua-message-send-hook ()
314   "The default function used for `notmuch-mua-send-hook', this
315 simply runs the corresponding `message-mode' hook functions."
316   (run-hooks 'message-send-hook))
317
318 ;;
319
320 (define-mail-user-agent 'notmuch-user-agent
321   'notmuch-mua-mail 'notmuch-mua-send-and-exit
322   'notmuch-mua-kill-buffer 'notmuch-mua-send-hook)
323
324 ;; Add some more headers to the list that `message-mode' hides when
325 ;; composing a message.
326 (notmuch-mua-add-more-hidden-headers)
327
328 ;;
329
330 (provide 'notmuch-mua)