]> git.notmuchmail.org Git - notmuch/blob - emacs/notmuch-mua.el
emacs: sign/encrypt replies to signed/encrypted messages
[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 'message)
23 (require 'mm-view)
24 (require 'format-spec)
25
26 (require 'notmuch-lib)
27 (require 'notmuch-address)
28
29 (eval-when-compile (require 'cl))
30
31 (declare-function notmuch-show-insert-bodypart "notmuch-show" (msg part depth &optional hide))
32
33 ;;
34
35 (defcustom notmuch-mua-send-hook '(notmuch-mua-message-send-hook)
36   "Hook run before sending messages."
37   :type 'hook
38   :group 'notmuch-send
39   :group 'notmuch-hooks)
40
41 (defcustom notmuch-mua-compose-in 'current-window
42   (concat
43    "Where to create the mail buffer used to compose a new message.
44 Possible values are `current-window' (default), `new-window' and
45 `new-frame'. If set to `current-window', the mail buffer will be
46 displayed in the current window, so the old buffer will be
47 restored when the mail buffer is killed. If set to `new-window'
48 or `new-frame', the mail buffer will be displayed in a new
49 window/frame that will be destroyed when the buffer is killed.
50 You may want to customize `message-kill-buffer-on-exit'
51 accordingly."
52    (when (< emacs-major-version 24)
53            " Due to a known bug in Emacs 23, you should not set
54 this to `new-window' if `message-kill-buffer-on-exit' is
55 disabled: this would result in an incorrect behavior."))
56   :group 'notmuch-send
57   :type '(choice (const :tag "Compose in the current window" current-window)
58                  (const :tag "Compose mail in a new window"  new-window)
59                  (const :tag "Compose mail in a new frame"   new-frame)))
60
61 (defcustom notmuch-mua-user-agent-function 'notmuch-mua-user-agent-full
62   "Function used to generate a `User-Agent:' string. If this is
63 `nil' then no `User-Agent:' will be generated."
64   :type '(choice (const :tag "No user agent string" nil)
65                  (const :tag "Full" notmuch-mua-user-agent-full)
66                  (const :tag "Notmuch" notmuch-mua-user-agent-notmuch)
67                  (const :tag "Emacs" notmuch-mua-user-agent-emacs)
68                  (function :tag "Custom user agent function"
69                            :value notmuch-mua-user-agent-full))
70   :group 'notmuch-send)
71
72 (defcustom notmuch-mua-hidden-headers '("^User-Agent:")
73   "Headers that are added to the `message-mode' hidden headers
74 list."
75   :type '(repeat string)
76   :group 'notmuch-send)
77
78 ;;
79
80 (defun notmuch-mua-get-switch-function ()
81   "Get a switch function according to `notmuch-mua-compose-in'."
82   (cond ((eq notmuch-mua-compose-in 'current-window)
83          'switch-to-buffer)
84         ((eq notmuch-mua-compose-in 'new-window)
85          'switch-to-buffer-other-window)
86         ((eq notmuch-mua-compose-in 'new-frame)
87          'switch-to-buffer-other-frame)
88         (t (error "Invalid value for `notmuch-mua-compose-in'"))))
89
90 (defun notmuch-mua-maybe-set-window-dedicated ()
91   "Set the selected window as dedicated according to
92 `notmuch-mua-compose-in'."
93   (when (or (eq notmuch-mua-compose-in 'new-frame)
94             (eq notmuch-mua-compose-in 'new-window))
95     (set-window-dedicated-p (selected-window) t)))
96
97 (defun notmuch-mua-user-agent-full ()
98   "Generate a `User-Agent:' string suitable for notmuch."
99   (concat (notmuch-mua-user-agent-notmuch)
100           " "
101           (notmuch-mua-user-agent-emacs)))
102
103 (defun notmuch-mua-user-agent-notmuch ()
104   "Generate a `User-Agent:' string suitable for notmuch."
105   (concat "Notmuch/" (notmuch-version) " (http://notmuchmail.org)"))
106
107 (defun notmuch-mua-user-agent-emacs ()
108   "Generate a `User-Agent:' string suitable for notmuch."
109   (concat "Emacs/" emacs-version " (" system-configuration ")"))
110
111 (defun notmuch-mua-add-more-hidden-headers ()
112   "Add some headers to the list that are hidden by default."
113   (mapc (lambda (header)
114           (when (not (member header message-hidden-headers))
115             (push header message-hidden-headers)))
116         notmuch-mua-hidden-headers))
117
118 (defun notmuch-mua-reply-crypto (parts)
119   (loop for part in parts
120         if (notmuch-match-content-type (plist-get part :content-type) "multipart/signed")
121           do (mml-secure-message-sign)
122         else if (notmuch-match-content-type (plist-get part :content-type) "multipart/encrypted")
123           do (mml-secure-message-sign-encrypt)
124         else if (notmuch-match-content-type (plist-get part :content-type) "multipart/*")
125           do (notmuch-mua-reply-crypto (plist-get part :content))))
126
127 (defun notmuch-mua-get-quotable-parts (parts)
128   (loop for part in parts
129         if (notmuch-match-content-type (plist-get part :content-type) "multipart/alternative")
130           collect (let* ((subparts (plist-get part :content))
131                         (types (mapcar (lambda (part) (plist-get part :content-type)) subparts))
132                         (chosen-type (car (notmuch-multipart/alternative-choose types))))
133                    (loop for part in (reverse subparts)
134                          if (notmuch-match-content-type (plist-get part :content-type) chosen-type)
135                          return part))
136         else if (notmuch-match-content-type (plist-get part :content-type) "multipart/*")
137           append (notmuch-mua-get-quotable-parts (plist-get part :content))
138         else if (notmuch-match-content-type (plist-get part :content-type) "text/*")
139           collect part))
140
141 (defun notmuch-mua-insert-quotable-part (message part)
142   ;; We don't want text properties leaking from the show renderer into
143   ;; the reply so we use a temp buffer. Also we don't want hooks, such
144   ;; as notmuch-wash-*, to be run on the quotable part so we set
145   ;; notmuch-show-insert-text/plain-hook to nil.
146   (insert (with-temp-buffer
147             (let ((notmuch-show-insert-text/plain-hook nil))
148               ;; Show the part but do not add buttons.
149               (notmuch-show-insert-bodypart message part 0 'no-buttons))
150             (buffer-substring-no-properties (point-min) (point-max)))))
151
152 ;; There is a bug in emacs 23's message.el that results in a newline
153 ;; not being inserted after the References header, so the next header
154 ;; is concatenated to the end of it. This function fixes the problem,
155 ;; while guarding against the possibility that some current or future
156 ;; version of emacs has the bug fixed.
157 (defun notmuch-mua-insert-references (original-func header references)
158   (funcall original-func header references)
159   (unless (bolp) (insert "\n")))
160
161 (defun notmuch-mua-reply (query-string &optional sender reply-all)
162   (let ((args '("reply" "--format=sexp" "--format-version=1"))
163         (process-crypto notmuch-show-process-crypto)
164         reply
165         original)
166     (when process-crypto
167       (setq args (append args '("--decrypt"))))
168
169     (if reply-all
170         (setq args (append args '("--reply-to=all")))
171       (setq args (append args '("--reply-to=sender"))))
172     (setq args (append args (list query-string)))
173
174     ;; Get the reply object as SEXP, and parse it into an elisp object.
175     (setq reply (apply #'notmuch-call-notmuch-sexp args))
176
177     ;; Extract the original message to simplify the following code.
178     (setq original (plist-get reply :original))
179
180     ;; Extract the headers of both the reply and the original message.
181     (let* ((original-headers (plist-get original :headers))
182            (reply-headers (plist-get reply :reply-headers)))
183
184       ;; If sender is non-nil, set the From: header to its value.
185       (when sender
186         (plist-put reply-headers :From sender))
187       (let
188           ;; Overlay the composition window on that being used to read
189           ;; the original message.
190           ((same-window-regexps '("\\*mail .*")))
191
192         ;; We modify message-header-format-alist to get around a bug in message.el.
193         ;; See the comment above on notmuch-mua-insert-references.
194         (let ((message-header-format-alist
195                (loop for pair in message-header-format-alist
196                      if (eq (car pair) 'References)
197                      collect (cons 'References
198                                    (apply-partially
199                                     'notmuch-mua-insert-references
200                                     (cdr pair)))
201                      else
202                      collect pair)))
203           (notmuch-mua-mail (plist-get reply-headers :To)
204                             (plist-get reply-headers :Subject)
205                             (notmuch-headers-plist-to-alist reply-headers)
206                             nil (notmuch-mua-get-switch-function))))
207
208       ;; Insert the message body - but put it in front of the signature
209       ;; if one is present, and after any other content
210       ;; message*setup-hooks may have added to the message body already.
211       (save-restriction
212         (message-goto-body)
213         (narrow-to-region (point) (point-max))
214         (goto-char (point-max))
215         (if (re-search-backward message-signature-separator nil t)
216             (if message-signature-insert-empty-line
217                 (forward-line -1))
218           (goto-char (point-max))))
219
220       (let ((from (plist-get original-headers :From))
221             (date (plist-get original-headers :Date))
222             (start (point)))
223
224         ;; message-cite-original constructs a citation line based on the From and Date
225         ;; headers of the original message, which are assumed to be in the buffer.
226         (insert "From: " from "\n")
227         (insert "Date: " date "\n\n")
228
229         ;; Get the parts of the original message that should be quoted; this includes
230         ;; all the text parts, except the non-preferred ones in a multipart/alternative.
231         (let ((quotable-parts (notmuch-mua-get-quotable-parts (plist-get original :body))))
232           (mapc (apply-partially 'notmuch-mua-insert-quotable-part original) quotable-parts))
233
234         (set-mark (point))
235         (goto-char start)
236         ;; Quote the original message according to the user's configured style.
237         (message-cite-original)))
238
239     ;; Sign and/or encrypt replies to signed and/or encrypted messages.
240     (when process-crypto
241       (notmuch-mua-reply-crypto (plist-get original :body))))
242
243   ;; Push mark right before signature, if any.
244   (message-goto-signature)
245   (unless (eobp)
246     (end-of-line -1))
247   (push-mark)
248
249   (message-goto-body)
250   (set-buffer-modified-p nil))
251
252 (defun notmuch-mua-mail (&optional to subject other-headers &rest other-args)
253   "Invoke the notmuch mail composition window.
254
255 OTHER-ARGS are passed through to `message-mail'."
256   (interactive)
257
258   (when notmuch-mua-user-agent-function
259     (let ((user-agent (funcall notmuch-mua-user-agent-function)))
260       (when (not (string= "" user-agent))
261         (push (cons 'User-Agent user-agent) other-headers))))
262
263   (unless (assq 'From other-headers)
264     (push (cons 'From (concat
265                        (notmuch-user-name) " <" (notmuch-user-primary-email) ">")) other-headers))
266
267   (apply #'message-mail to subject other-headers other-args)
268   (message-sort-headers)
269   (message-hide-headers)
270   (set-buffer-modified-p nil)
271   (notmuch-mua-maybe-set-window-dedicated)
272
273   (message-goto-to))
274
275 (defcustom notmuch-identities nil
276   "Identities that can be used as the From: address when composing a new message.
277
278 If this variable is left unset, then a list will be constructed from the
279 name and addresses configured in the notmuch configuration file."
280   :type '(repeat string)
281   :group 'notmuch-send)
282
283 (defcustom notmuch-always-prompt-for-sender nil
284   "Always prompt for the From: address when composing or forwarding a message.
285
286 This is not taken into account when replying to a message, because in that case
287 the From: header is already filled in by notmuch."
288   :type 'boolean
289   :group 'notmuch-send)
290
291 (defvar notmuch-mua-sender-history nil)
292
293 ;; Workaround: Running `ido-completing-read' in emacs 23.1, 23.2 and 23.3
294 ;; without some explicit initialization fill freeze the operation.
295 ;; Hence, we advice `ido-completing-read' to ensure required initialization
296 ;; is done.
297 (if (and (= emacs-major-version 23) (< emacs-minor-version 4))
298     (defadvice ido-completing-read (before notmuch-ido-mode-init activate)
299       (ido-init-completion-maps)
300       (add-hook 'minibuffer-setup-hook 'ido-minibuffer-setup)
301       (add-hook 'choose-completion-string-functions
302                 'ido-choose-completion-string)
303       (ad-disable-advice 'ido-completing-read 'before 'notmuch-ido-mode-init)
304       (ad-activate 'ido-completing-read)))
305
306 (defun notmuch-mua-prompt-for-sender ()
307   "Prompt for a sender from the user's configured identities."
308   (if notmuch-identities
309       (ido-completing-read "Send mail from: " notmuch-identities
310                            nil nil nil 'notmuch-mua-sender-history
311                            (car notmuch-identities))
312     (let* ((name (notmuch-user-name))
313            (addrs (cons (notmuch-user-primary-email)
314                         (notmuch-user-other-email)))
315            (address
316             (ido-completing-read (concat "Sender address for " name ": ") addrs
317                                  nil nil nil 'notmuch-mua-sender-history
318                                  (car addrs))))
319       (concat name " <" address ">"))))
320
321 (put 'notmuch-mua-new-mail 'notmuch-prefix-doc "... and prompt for sender")
322 (defun notmuch-mua-new-mail (&optional prompt-for-sender)
323   "Compose new mail.
324
325 If PROMPT-FOR-SENDER is non-nil, the user will be prompted for
326 the From: address first."
327   (interactive "P")
328   (let ((other-headers
329          (when (or prompt-for-sender notmuch-always-prompt-for-sender)
330            (list (cons 'From (notmuch-mua-prompt-for-sender))))))
331     (notmuch-mua-mail nil nil other-headers nil (notmuch-mua-get-switch-function))))
332
333 (defun notmuch-mua-new-forward-message (&optional prompt-for-sender)
334   "Invoke the notmuch message forwarding window.
335
336 The current buffer must contain an RFC2822 message to forward.
337
338 If PROMPT-FOR-SENDER is non-nil, the user will be prompted for
339 the From: address first."
340   (let* ((cur (current-buffer))
341          (message-forward-decoded-p nil)
342          (subject (message-make-forward-subject))
343          (other-headers
344           (when (or prompt-for-sender notmuch-always-prompt-for-sender)
345             (list (cons 'From (notmuch-mua-prompt-for-sender))))))
346     (notmuch-mua-mail nil subject other-headers nil (notmuch-mua-get-switch-function))
347     (message-forward-make-body cur)
348     ;; `message-forward-make-body' shows the User-agent header.  Hide
349     ;; it again.
350     (message-hide-headers)))
351
352 (defun notmuch-mua-new-reply (query-string &optional prompt-for-sender reply-all)
353   "Compose a reply to the message identified by QUERY-STRING.
354
355 If PROMPT-FOR-SENDER is non-nil, the user will be prompted for
356 the From: address first.  If REPLY-ALL is non-nil, the message
357 will be addressed to all recipients of the source message."
358
359 ;; In current emacs (24.3) select-active-regions is set to t by
360 ;; default. The reply insertion code sets the region to the quoted
361 ;; message to make it easy to delete (kill-region or C-w). These two
362 ;; things combine to put the quoted message in the primary selection.
363 ;;
364 ;; This is not what the user wanted and is a privacy risk (accidental
365 ;; pasting of the quoted message). We can avoid some of the problems
366 ;; by let-binding select-active-regions to nil. This fixes if the
367 ;; primary selection was previously in a non-emacs window but not if
368 ;; it was in an emacs window. To avoid the problem in the latter case
369 ;; we deactivate mark.
370
371   (let ((sender
372          (when prompt-for-sender
373            (notmuch-mua-prompt-for-sender)))
374         (select-active-regions nil))
375     (notmuch-mua-reply query-string sender reply-all)
376     (deactivate-mark)))
377
378 (defun notmuch-mua-send-and-exit (&optional arg)
379   (interactive "P")
380   (message-send-and-exit arg))
381
382 (defun notmuch-mua-kill-buffer ()
383   (interactive)
384   (message-kill-buffer))
385
386 (defun notmuch-mua-message-send-hook ()
387   "The default function used for `notmuch-mua-send-hook', this
388 simply runs the corresponding `message-mode' hook functions."
389   (run-hooks 'message-send-hook))
390
391 ;;
392
393 (define-mail-user-agent 'notmuch-user-agent
394   'notmuch-mua-mail 'notmuch-mua-send-and-exit
395   'notmuch-mua-kill-buffer 'notmuch-mua-send-hook)
396
397 ;; Add some more headers to the list that `message-mode' hides when
398 ;; composing a message.
399 (notmuch-mua-add-more-hidden-headers)
400
401 ;;
402
403 (provide 'notmuch-mua)