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 (let* ((status (plist-get sigstatus :status))
103 (face 'notmuch-crypto-signature-unknown)
104 (button-action (lambda (button) (message (button-get button 'help-echo)))))
106 ((string= status "good")
107 (let ((fingerprint (concat "0x" (plist-get sigstatus :fingerprint))))
108 ;; if userid present, userid has full or greater validity
109 (if (plist-member sigstatus :userid)
110 (let ((userid (plist-get sigstatus :userid)))
111 (setq label (concat "Good signature by: " userid))
112 (setq face 'notmuch-crypto-signature-good))
114 (setq label (concat "Good signature by key: " fingerprint))
115 (setq face 'notmuch-crypto-signature-good-key)))
116 (setq button-action 'notmuch-crypto-sigstatus-good-callback)
117 (setq help-msg (concat "Click to list key ID 0x" fingerprint "."))))
118 ((string= status "error")
119 (let ((keyid (concat "0x" (plist-get sigstatus :keyid))))
120 (setq label (concat "Unknown key ID " keyid " or unsupported algorithm"))
121 (setq button-action 'notmuch-crypto-sigstatus-error-callback)
122 (setq help-msg (concat "Click to retrieve key ID " keyid " from keyserver."))))
123 ((string= status "bad")
124 (let ((keyid (concat "0x" (plist-get sigstatus :keyid))))
125 (setq label (concat "Bad signature (claimed key ID " keyid ")"))
126 (setq face 'notmuch-crypto-signature-bad)))
128 (setq label (concat "Unknown signature status: " status)))
130 (setq show-button nil)))
133 (concat "[ " label " ]")
134 :type 'notmuch-crypto-status-button-type
138 'action button-action
139 :notmuch-sigstatus sigstatus
143 (declare-function notmuch-show-refresh-view "notmuch-show" (&optional reset-state))
145 (defun notmuch-crypto-sigstatus-good-callback (button)
146 (let* ((sigstatus (button-get button :notmuch-sigstatus))
147 (fingerprint (concat "0x" (plist-get sigstatus :fingerprint)))
148 (buffer (get-buffer-create "*notmuch-crypto-gpg-out*"))
149 (window (display-buffer buffer t nil)))
150 (with-selected-window window
151 (with-current-buffer buffer
152 (goto-char (point-max))
153 (call-process epg-gpg-program nil t t "--batch" "--no-tty" "--list-keys" fingerprint))
156 (defun notmuch-crypto--async-key-sentinel (process event)
157 "When the user asks for a GPG key to be retrieved
158 asynchronously, handle completion of that task.
160 If the retrieval is successful, the thread where the retrieval
161 was initiated is still displayed and the cursor has not moved,
162 redisplay the thread."
163 (let ((status (process-status process))
164 (exit-status (process-exit-status process))
165 (keyid (process-get process :gpg-key-id)))
166 (when (memq status '(exit signal))
167 (message "Getting the GPG key %s asynchronously...%s."
169 (if (= exit-status 0)
172 ;; If the original buffer is still alive and point didn't move
173 ;; (i.e. the user didn't move on or away), refresh the buffer to
174 ;; show the updated signature status.
175 (let ((show-buffer (process-get process :notmuch-show-buffer))
176 (show-point (process-get process :notmuch-show-point)))
177 (when (and (bufferp show-buffer)
178 (buffer-live-p show-buffer)
180 (with-current-buffer show-buffer
182 (with-current-buffer show-buffer
183 (notmuch-show-refresh-view)))))))
185 (defun notmuch-crypto--set-button-label (button label)
186 "Set the text displayed in BUTTON to LABEL."
188 (let ((inhibit-read-only t))
189 ;; This knows rather too much about how we typically format
191 (goto-char (button-start button))
193 (delete-region (point) (- (button-end button) 2))
196 (defun notmuch-crypto-sigstatus-error-callback (button)
197 (let* ((sigstatus (button-get button :notmuch-sigstatus))
198 (keyid (concat "0x" (plist-get sigstatus :keyid)))
199 (buffer (get-buffer-create "*notmuch-crypto-gpg-out*")))
200 (if notmuch-crypto-get-keys-asynchronously
202 (notmuch-crypto--set-button-label
203 button (format "Retrieving key %s asynchronously..." keyid))
204 (let ((p (make-process :name "notmuch GPG key retrieval"
206 :command (list epg-gpg-program "--recv-keys" keyid)
207 :connection-type 'pipe
208 :sentinel #'notmuch-crypto--async-key-sentinel
209 ;; Create the process stopped so that
210 ;; we have time to store the key id,
213 (process-put p :gpg-key-id keyid)
214 (process-put p :notmuch-show-buffer (current-buffer))
215 (process-put p :notmuch-show-point (point))
216 (message "Getting the GPG key %s asynchronously..." keyid)
217 (continue-process p)))
218 (let ((window (display-buffer buffer t nil)))
219 (with-selected-window window
220 (with-current-buffer buffer
221 (goto-char (point-max))
222 (call-process epg-gpg-program nil t t "--recv-keys" keyid)
224 (call-process epg-gpg-program nil t t "--list-keys" keyid))
226 (notmuch-show-refresh-view)))))
228 (defun notmuch-crypto-insert-encstatus-button (encstatus)
229 (let* ((status (plist-get encstatus :status))
231 (label "Decryption not attempted")
232 (face 'notmuch-crypto-decryption))
234 ((string= status "good")
235 (setq label "Decryption successful"))
236 ((string= status "bad")
237 (setq label "Decryption error"))
239 (setq label (concat "Unknown encryption status"
240 (if status (concat ": " status))))))
242 (concat "[ " label " ]")
243 :type 'notmuch-crypto-status-button-type
251 (provide 'notmuch-crypto)
253 ;;; notmuch-crypto.el ends here