1 ;;; notmuch-crypto.el --- functions for handling display of cryptographic metadata.
3 ;; Copyright © Jameson Rollins
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 <https://www.gnu.org/licenses/>.
20 ;; Authors: Jameson Rollins <jrollins@finestructure.net>
25 (require 'notmuch-lib)
27 (defcustom notmuch-crypto-process-mime t
28 "Should cryptographic MIME parts be processed?
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.
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
43 :package-version '(notmuch . "0.25")
44 :group 'notmuch-crypto)
46 (defcustom notmuch-crypto-get-keys-asynchronously t
47 "Retrieve gpg keys asynchronously."
49 :group 'notmuch-crypto)
51 (defface notmuch-crypto-part-header
54 (:foreground "LightBlue1"))
57 (:foreground "blue")))
58 "Face used for crypto parts headers."
59 :group 'notmuch-crypto
60 :group 'notmuch-faces)
62 (defface notmuch-crypto-signature-good
63 '((t (:background "green" :foreground "black")))
64 "Face used for good signatures."
65 :group 'notmuch-crypto
66 :group 'notmuch-faces)
68 (defface notmuch-crypto-signature-good-key
69 '((t (:background "orange" :foreground "black")))
70 "Face used for good signatures."
71 :group 'notmuch-crypto
72 :group 'notmuch-faces)
74 (defface notmuch-crypto-signature-bad
75 '((t (:background "red" :foreground "black")))
76 "Face used for bad signatures."
77 :group 'notmuch-crypto
78 :group 'notmuch-faces)
80 (defface notmuch-crypto-signature-unknown
81 '((t (:background "red" :foreground "black")))
82 "Face used for signatures of unknown status."
83 :group 'notmuch-crypto
84 :group 'notmuch-faces)
86 (defface notmuch-crypto-decryption
87 '((t (:background "purple" :foreground "black")))
88 "Face used for encryption/decryption status messages."
89 :group 'notmuch-crypto
90 :group 'notmuch-faces)
92 (define-button-type 'notmuch-crypto-status-button-type
93 'action (lambda (button) (message (button-get button 'help-echo)))
95 'help-echo "Set notmuch-crypto-process-mime to process cryptographic mime parts."
96 :supertype 'notmuch-button-type)
98 (defun notmuch-crypto-insert-sigstatus-button (sigstatus from)
99 "Insert a button describing the signature status SIGSTATUS sent
101 (let* ((status (plist-get sigstatus :status))
103 (face 'notmuch-crypto-signature-unknown)
104 (button-action (lambda (button) (message (button-get button 'help-echo))))
105 (keyid (concat "0x" (plist-get sigstatus :keyid)))
108 ((string= status "good")
109 (let ((fingerprint (concat "0x" (plist-get sigstatus :fingerprint)))
110 (userid (plist-get sigstatus :userid)))
111 ;; If userid is present it has full or greater validity.
113 (setq label (concat "Good signature by: " userid)
114 face 'notmuch-crypto-signature-good)
115 (setq label (concat "Good signature by key: " fingerprint)
116 face 'notmuch-crypto-signature-good-key))
117 (setq button-action 'notmuch-crypto-sigstatus-good-callback
118 help-msg (concat "Click to list key ID 0x" fingerprint "."))))
120 ((string= status "error")
121 (setq label (concat "Unknown key ID " keyid " or unsupported algorithm")
122 button-action 'notmuch-crypto-sigstatus-error-callback
123 help-msg (concat "Click to retrieve key ID " keyid
124 " from keyserver.")))
126 ((string= status "bad")
127 (setq label (concat "Bad signature (claimed key ID " keyid ")")
128 face 'notmuch-crypto-signature-bad))
131 (setq label (concat "Unknown signature status: " status)))
133 (setq show-button nil)))
136 (concat "[ " label " ]")
137 :type 'notmuch-crypto-status-button-type
141 'action button-action
142 :notmuch-sigstatus sigstatus
146 (defun notmuch-crypto-sigstatus-good-callback (button)
147 (let* ((sigstatus (button-get button :notmuch-sigstatus))
148 (fingerprint (concat "0x" (plist-get sigstatus :fingerprint)))
149 (buffer (get-buffer-create "*notmuch-crypto-gpg-out*"))
150 (window (display-buffer buffer)))
151 (with-selected-window window
152 (with-current-buffer buffer
153 (goto-char (point-max))
154 (call-process epg-gpg-program nil t t "--batch" "--no-tty" "--list-keys" fingerprint))
157 (declare-function notmuch-show-refresh-view "notmuch-show" (&optional reset-state))
159 (defun notmuch-crypto--async-key-sentinel (process event)
160 "When the user asks for a GPG key to be retrieved
161 asynchronously, handle completion of that task.
163 If the retrieval is successful, the thread where the retrieval
164 was initiated is still displayed and the cursor has not moved,
165 redisplay the thread."
166 (let ((status (process-status process))
167 (exit-status (process-exit-status process))
168 (keyid (process-get process :gpg-key-id)))
169 (when (memq status '(exit signal))
170 (message "Getting the GPG key %s asynchronously...%s."
172 (if (= exit-status 0)
175 ;; If the original buffer is still alive and point didn't move
176 ;; (i.e. the user didn't move on or away), refresh the buffer to
177 ;; show the updated signature status.
178 (let ((show-buffer (process-get process :notmuch-show-buffer))
179 (show-point (process-get process :notmuch-show-point)))
180 (when (and (bufferp show-buffer)
181 (buffer-live-p show-buffer)
183 (with-current-buffer show-buffer
185 (with-current-buffer show-buffer
186 (notmuch-show-refresh-view)))))))
188 (defun notmuch-crypto--set-button-label (button label)
189 "Set the text displayed in BUTTON to LABEL."
191 (let ((inhibit-read-only t))
192 ;; This knows rather too much about how we typically format
194 (goto-char (button-start button))
196 (delete-region (point) (- (button-end button) 2))
199 (defun notmuch-crypto-sigstatus-error-callback (button)
200 "When signature validation has failed, try to retrieve the
201 corresponding key when the status button is pressed."
202 (let* ((sigstatus (button-get button :notmuch-sigstatus))
203 (keyid (concat "0x" (plist-get sigstatus :keyid)))
204 (buffer (get-buffer-create "*notmuch-crypto-gpg-out*")))
205 (if notmuch-crypto-get-keys-asynchronously
207 (notmuch-crypto--set-button-label
208 button (format "Retrieving key %s asynchronously..." keyid))
209 (let ((p (make-process :name "notmuch GPG key retrieval"
211 :command (list epg-gpg-program "--recv-keys" keyid)
212 :connection-type 'pipe
213 :sentinel #'notmuch-crypto--async-key-sentinel
214 ;; Create the process stopped so that
215 ;; we have time to store the key id,
218 (process-put p :gpg-key-id keyid)
219 (process-put p :notmuch-show-buffer (current-buffer))
220 (process-put p :notmuch-show-point (point))
221 (message "Getting the GPG key %s asynchronously..." keyid)
222 (continue-process p)))
223 (let ((window (display-buffer buffer)))
224 (with-selected-window window
225 (with-current-buffer buffer
226 (goto-char (point-max))
227 (call-process epg-gpg-program nil t t "--recv-keys" keyid)
229 (call-process epg-gpg-program nil t t "--list-keys" keyid))
231 (notmuch-show-refresh-view)))))
233 (defun notmuch-crypto-insert-encstatus-button (encstatus)
234 "Insert a button describing the encryption status ENCSTATUS."
237 (let ((status (plist-get encstatus :status)))
239 ((string= status "good")
240 "Decryption successful")
241 ((string= status "bad")
244 (concat "Unknown encryption status"
245 (if status (concat ": " status))))))
247 :type 'notmuch-crypto-status-button-type
248 'face 'notmuch-crypto-decryption
249 'mouse-face 'notmuch-crypto-decryption)
254 (provide 'notmuch-crypto)
256 ;;; notmuch-crypto.el ends here