]> git.notmuchmail.org Git - notmuch/blob - emacs/notmuch-mua.el
Merge tag '0.12'
[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 'format-spec)
25
26 (require 'notmuch-lib)
27 (require 'notmuch-address)
28
29 (eval-when-compile (require 'cl))
30
31 ;;
32
33 (defcustom notmuch-mua-send-hook '(notmuch-mua-message-send-hook)
34   "Hook run before sending messages."
35   :type 'hook
36   :group 'notmuch-send
37   :group 'notmuch-hooks)
38
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))
48   :group 'notmuch-send)
49
50 (defcustom notmuch-mua-hidden-headers '("^User-Agent:")
51   "Headers that are added to the `message-mode' hidden headers
52 list."
53   :type '(repeat string)
54   :group 'notmuch-send)
55
56 ;;
57
58 (defun notmuch-mua-user-agent-full ()
59   "Generate a `User-Agent:' string suitable for notmuch."
60   (concat (notmuch-mua-user-agent-notmuch)
61           " "
62           (notmuch-mua-user-agent-emacs)))
63
64 (defun notmuch-mua-user-agent-notmuch ()
65   "Generate a `User-Agent:' string suitable for notmuch."
66   (concat "Notmuch/" (notmuch-version) " (http://notmuchmail.org)"))
67
68 (defun notmuch-mua-user-agent-emacs ()
69   "Generate a `User-Agent:' string suitable for notmuch."
70   (concat "Emacs/" emacs-version " (" system-configuration ")"))
71
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))
78
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)
87                          return part))
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/*")
91           collect part))
92
93 (defun notmuch-mua-reply (query-string &optional sender reply-all)
94   (let ((args '("reply" "--format=json"))
95         reply
96         original)
97     (when notmuch-show-process-crypto
98       (setq args (append args '("--decrypt"))))
99
100     (if reply-all
101         (setq args (append args '("--reply-to=all")))
102       (setq args (append args '("--reply-to=sender"))))
103     (setq args (append args (list query-string)))
104
105     ;; Get the reply object as JSON, and parse it into an elisp object.
106     (with-temp-buffer
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)
111             (json-false 'nil))
112         (setq reply (json-read))))
113
114     ;; Extract the original message to simplify the following code.
115     (setq original (plist-get reply :original))
116
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)))
120
121       ;; If sender is non-nil, set the From: header to its value.
122       (when sender
123         (plist-put reply-headers :From sender))
124       (let
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
132       ;; if one is present
133       (goto-char (point-max))
134       (if (re-search-backward message-signature-separator nil t)
135           (forward-line -1)
136         (goto-char (point-max)))
137
138       (let ((from (plist-get original-headers :From))
139             (date (plist-get original-headers :Date))
140             (start (point)))
141
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")
146
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))))
150           (mapc (lambda (part)
151                   (insert (notmuch-get-bodypart-content original part
152                                                         (plist-get part :id)
153                                                         notmuch-show-process-crypto)))
154                 quotable-parts))
155
156         (set-mark (point))
157         (goto-char start)
158         ;; Quote the original message according to the user's configured style.
159         (message-cite-original))))
160
161   (goto-char (point-max))
162   (push-mark)
163   (message-goto-body)
164   (set-buffer-modified-p nil))
165
166 (defun notmuch-mua-forward-message ()
167   (message-forward)
168
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)
176
177   (message-goto-to))
178
179 (defun notmuch-mua-mail (&optional to subject other-headers &rest other-args)
180   "Invoke the notmuch mail composition window.
181
182 OTHER-ARGS are passed through to `message-mail'."
183   (interactive)
184
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))))
189
190   (unless (mail-header 'From other-headers)
191     (push (cons "From" (concat
192                         (notmuch-user-name) " <" (notmuch-user-primary-email) ">")) other-headers))
193
194   (apply #'message-mail to subject other-headers other-args)
195   (message-sort-headers)
196   (message-hide-headers)
197   (set-buffer-modified-p nil)
198
199   (message-goto-to))
200
201 (defcustom notmuch-identities nil
202   "Identities that can be used as the From: address when composing a new message.
203
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)
208
209 (defcustom notmuch-always-prompt-for-sender nil
210   "Always prompt for the From: address when composing or forwarding a message.
211
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."
214   :type 'boolean
215   :group 'notmuch-send)
216
217 (defvar notmuch-mua-sender-history nil)
218
219 (defun notmuch-mua-prompt-for-sender ()
220   (interactive)
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)
227                 one-name-only (eval
228                                (cons 'and
229                                      (mapcar (lambda (identity)
230                                                (string-equal name (car identity)))
231                                              components)))))
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))
235             one-name-only t))
236     ;; Now prompt the user, either for an email address only or for a full identity.
237     (if one-name-only
238         (let ((address
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)))))
244
245 (defun notmuch-mua-new-mail (&optional prompt-for-sender)
246   "Invoke the notmuch mail composition window.
247
248 If PROMPT-FOR-SENDER is non-nil, the user will be prompted for
249 the From: address first."
250   (interactive "P")
251   (let ((other-headers
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)))
255
256 (defun notmuch-mua-new-forward-message (&optional prompt-for-sender)
257   "Invoke the notmuch message forwarding window.
258
259 If PROMPT-FOR-SENDER is non-nil, the user will be prompted for
260 the From: address first."
261   (interactive "P")
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)))
269
270 (defun notmuch-mua-new-reply (query-string &optional prompt-for-sender reply-all)
271   "Invoke the notmuch reply window."
272   (interactive "P")
273   (let ((sender
274          (when prompt-for-sender
275            (notmuch-mua-prompt-for-sender))))
276     (notmuch-mua-reply query-string sender reply-all)))
277
278 (defun notmuch-mua-send-and-exit (&optional arg)
279   (interactive "P")
280   (message-send-and-exit arg))
281
282 (defun notmuch-mua-kill-buffer ()
283   (interactive)
284   (message-kill-buffer))
285
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))
290
291 ;;
292
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)
296
297 ;; Add some more headers to the list that `message-mode' hides when
298 ;; composing a message.
299 (notmuch-mua-add-more-hidden-headers)
300
301 ;;
302
303 (provide 'notmuch-mua)