]> git.notmuchmail.org Git - notmuch/blob - emacs/notmuch-crypto.el
f4585d5e927bafaa50b5957be6c10bb35c1811e8
[notmuch] / emacs / notmuch-crypto.el
1 ;;; notmuch-crypto.el --- functions for handling display of cryptographic metadata.
2 ;;
3 ;; Copyright © Jameson Rollins
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 <https://www.gnu.org/licenses/>.
19 ;;
20 ;; Authors: Jameson Rollins <jrollins@finestructure.net>
21
22 ;;; Code:
23
24 (require 'epg)
25 (require 'notmuch-lib)
26
27 (defcustom notmuch-crypto-process-mime t
28   "Should cryptographic MIME parts be processed?
29
30 If this variable is non-nil signatures in multipart/signed
31 messages will be verified and multipart/encrypted parts will be
32 decrypted.  The result of the crypto operation will be displayed
33 in a specially colored header button at the top of the processed
34 part.  Signed parts will have variously colored headers depending
35 on the success or failure of the verification process and on the
36 validity of user ID of the signer.
37
38 The effect of setting this variable can be seen temporarily by
39 providing a prefix when viewing a signed or encrypted message, or
40 by providing a prefix when reloading the message in notmuch-show
41 mode."
42   :type 'boolean
43   :package-version '(notmuch . "0.25")
44   :group 'notmuch-crypto)
45
46 (defcustom notmuch-crypto-get-keys-asynchronously t
47   "Retrieve gpg keys asynchronously."
48   :type 'boolean
49   :group 'notmuch-crypto)
50
51 (defcustom notmuch-crypto-gpg-program epg-gpg-program
52   "The gpg executable."
53   :type 'string
54   :group 'notmuch-crypto)
55
56 (defface notmuch-crypto-part-header
57   '((((class color)
58       (background dark))
59      (:foreground "LightBlue1"))
60     (((class color)
61       (background light))
62      (:foreground "blue")))
63   "Face used for crypto parts headers."
64   :group 'notmuch-crypto
65   :group 'notmuch-faces)
66
67 (defface notmuch-crypto-signature-good
68   '((t (:background "green" :foreground "black")))
69   "Face used for good signatures."
70   :group 'notmuch-crypto
71   :group 'notmuch-faces)
72
73 (defface notmuch-crypto-signature-good-key
74   '((t (:background "orange" :foreground "black")))
75   "Face used for good signatures."
76   :group 'notmuch-crypto
77   :group 'notmuch-faces)
78
79 (defface notmuch-crypto-signature-bad
80   '((t (:background "red" :foreground "black")))
81   "Face used for bad signatures."
82   :group 'notmuch-crypto
83   :group 'notmuch-faces)
84
85 (defface notmuch-crypto-signature-unknown
86   '((t (:background "red" :foreground "black")))
87   "Face used for signatures of unknown status."
88   :group 'notmuch-crypto
89   :group 'notmuch-faces)
90
91 (defface notmuch-crypto-decryption
92   '((t (:background "purple" :foreground "black")))
93   "Face used for encryption/decryption status messages."
94   :group 'notmuch-crypto
95   :group 'notmuch-faces)
96
97 (define-button-type 'notmuch-crypto-status-button-type
98   'action (lambda (button) (message (button-get button 'help-echo)))
99   'follow-link t
100   'help-echo "Set notmuch-crypto-process-mime to process cryptographic mime parts."
101   :supertype 'notmuch-button-type)
102
103 (defun notmuch-crypto-insert-sigstatus-button (sigstatus from)
104   "Insert a button describing the signature status SIGSTATUS sent
105 by user FROM."
106   (let* ((status (plist-get sigstatus :status))
107          (show-button t)
108          (face 'notmuch-crypto-signature-unknown)
109          (button-action (lambda (button) (message (button-get button 'help-echo))))
110          (keyid (concat "0x" (plist-get sigstatus :keyid)))
111          label help-msg)
112     (cond
113      ((string= status "good")
114       (let ((fingerprint (concat "0x" (plist-get sigstatus :fingerprint)))
115             (userid (plist-get sigstatus :userid)))
116         ;; If userid is present it has full or greater validity.
117         (if userid
118             (setq label (concat "Good signature by: " userid)
119                   face 'notmuch-crypto-signature-good)
120           (setq label (concat "Good signature by key: " fingerprint)
121                 face 'notmuch-crypto-signature-good-key))
122         (setq button-action 'notmuch-crypto-sigstatus-good-callback
123               help-msg (concat "Click to list key ID 0x" fingerprint "."))))
124
125      ((string= status "error")
126       (setq label (concat "Unknown key ID " keyid " or unsupported algorithm")
127             button-action 'notmuch-crypto-sigstatus-error-callback
128             help-msg (concat "Click to retrieve key ID " keyid
129                              " from keyserver.")))
130
131      ((string= status "bad")
132       (setq label (concat "Bad signature (claimed key ID " keyid ")")
133             face 'notmuch-crypto-signature-bad))
134
135      (status
136       (setq label (concat "Unknown signature status: " status)))
137      (t
138       (setq show-button nil)))
139     (when show-button
140       (insert-button
141        (concat "[ " label " ]")
142        :type 'notmuch-crypto-status-button-type
143        'help-echo help-msg
144        'face face
145        'mouse-face face
146        'action button-action
147        :notmuch-sigstatus sigstatus
148        :notmuch-from from)
149       (insert "\n"))))
150
151 (defun notmuch-crypto-sigstatus-good-callback (button)
152   (let* ((id (notmuch-show-get-message-id))
153          (sigstatus (button-get button :notmuch-sigstatus))
154          (fingerprint (concat "0x" (plist-get sigstatus :fingerprint)))
155          (buffer (get-buffer-create "*notmuch-crypto-gpg-out*"))
156          (window (display-buffer buffer)))
157     (with-selected-window window
158       (with-current-buffer buffer
159         (goto-char (point-max))
160         (insert (format "-- Key %s in message %s:\n"
161                         fingerprint id))
162         (call-process notmuch-crypto-gpg-program nil t t "--batch" "--no-tty" "--list-keys" fingerprint))
163       (recenter -1))))
164
165 (declare-function notmuch-show-refresh-view "notmuch-show" (&optional reset-state))
166
167 (defun notmuch-crypto--async-key-sentinel (process event)
168   "When the user asks for a GPG key to be retrieved
169 asynchronously, handle completion of that task.
170
171 If the retrieval is successful, the thread where the retrieval
172 was initiated is still displayed and the cursor has not moved,
173 redisplay the thread."
174   (let ((status (process-status process))
175         (exit-status (process-exit-status process))
176         (keyid (process-get process :gpg-key-id)))
177     (when (memq status '(exit signal))
178       (message "Getting the GPG key %s asynchronously...%s."
179                keyid
180                (if (= exit-status 0)
181                    "completed"
182                  "failed"))
183       ;; If the original buffer is still alive and point didn't move
184       ;; (i.e. the user didn't move on or away), refresh the buffer to
185       ;; show the updated signature status.
186       (let ((show-buffer (process-get process :notmuch-show-buffer))
187             (show-point (process-get process :notmuch-show-point)))
188         (when (and (bufferp show-buffer)
189                    (buffer-live-p show-buffer)
190                    (= show-point
191                       (with-current-buffer show-buffer
192                         (point))))
193           (with-current-buffer show-buffer
194             (notmuch-show-refresh-view)))))))
195
196 (defun notmuch-crypto--set-button-label (button label)
197   "Set the text displayed in BUTTON to LABEL."
198   (save-excursion
199     (let ((inhibit-read-only t))
200       ;; This knows rather too much about how we typically format
201       ;; buttons.
202       (goto-char (button-start button))
203       (forward-char 2)
204       (delete-region (point) (- (button-end button) 2))
205       (insert label))))
206
207 (defun notmuch-crypto-sigstatus-error-callback (button)
208   "When signature validation has failed, try to retrieve the
209 corresponding key when the status button is pressed."
210   (let* ((sigstatus (button-get button :notmuch-sigstatus))
211          (keyid (concat "0x" (plist-get sigstatus :keyid)))
212          (buffer (get-buffer-create "*notmuch-crypto-gpg-out*")))
213     (if notmuch-crypto-get-keys-asynchronously
214         (progn
215           (notmuch-crypto--set-button-label
216            button (format "Retrieving key %s asynchronously..." keyid))
217           (with-current-buffer buffer
218             (goto-char (point-max))
219             (insert (format "--- Retrieving key %s:\n" keyid)))
220           (let ((p (make-process :name "notmuch GPG key retrieval"
221                                  :connection-type 'pipe
222                                  :buffer buffer
223                                  :stderr buffer
224                                  :command (list notmuch-crypto-gpg-program "--recv-keys" keyid)
225                                  :sentinel #'notmuch-crypto--async-key-sentinel
226                                  ;; Create the process stopped so that
227                                  ;; we have time to store the key id,
228                                  ;; etc. on it.
229                                  :stop t)))
230             (process-put p :gpg-key-id keyid)
231             (process-put p :notmuch-show-buffer (current-buffer))
232             (process-put p :notmuch-show-point (point))
233             (message "Getting the GPG key %s asynchronously..." keyid)
234             (continue-process p)))
235
236       (let ((window (display-buffer buffer)))
237         (with-selected-window window
238           (with-current-buffer buffer
239             (goto-char (point-max))
240             (insert (format "--- Retrieving key %s:\n" keyid))
241             (call-process notmuch-crypto-gpg-program nil t t "--recv-keys" keyid)
242             (insert "\n")
243             (call-process notmuch-crypto-gpg-program nil t t "--list-keys" keyid))
244           (recenter -1))
245         (notmuch-show-refresh-view)))))
246
247 (defun notmuch-crypto-insert-encstatus-button (encstatus)
248   "Insert a button describing the encryption status ENCSTATUS."
249   (insert-button
250    (concat "[ "
251            (let ((status (plist-get encstatus :status)))
252              (cond
253               ((string= status "good")
254                "Decryption successful")
255               ((string= status "bad")
256                "Decryption error")
257               (t
258                (concat "Unknown encryption status"
259                        (if status (concat ": " status))))))
260            " ]")
261    :type 'notmuch-crypto-status-button-type
262    'face 'notmuch-crypto-decryption
263    'mouse-face 'notmuch-crypto-decryption)
264   (insert "\n"))
265
266 ;;
267
268 (provide 'notmuch-crypto)
269
270 ;;; notmuch-crypto.el ends here