]> git.notmuchmail.org Git - notmuch/blob - emacs/notmuch-crypto.el
457c821c3d1ff9635b27f25d7f118c8120501445
[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 (defface notmuch-crypto-part-header
52   '((((class color)
53       (background dark))
54      (:foreground "LightBlue1"))
55     (((class color)
56       (background light))
57      (:foreground "blue")))
58   "Face used for crypto parts headers."
59   :group 'notmuch-crypto
60   :group 'notmuch-faces)
61
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)
67
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)
73
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)
79
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)
85
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)
91
92 (define-button-type 'notmuch-crypto-status-button-type
93   'action (lambda (button) (message (button-get button 'help-echo)))
94   'follow-link t
95   'help-echo "Set notmuch-crypto-process-mime to process cryptographic mime parts."
96   :supertype 'notmuch-button-type)
97
98 (defun notmuch-crypto-insert-sigstatus-button (sigstatus from)
99   "Insert a button describing the signature status SIGSTATUS sent
100 by user FROM."
101   (let* ((status (plist-get sigstatus :status))
102          (show-button t)
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)))
106          label help-msg)
107     (cond
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.
112         (if userid
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 "."))))
119
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.")))
125
126      ((string= status "bad")
127       (setq label (concat "Bad signature (claimed key ID " keyid ")")
128             face 'notmuch-crypto-signature-bad))
129
130      (status
131       (setq label (concat "Unknown signature status: " status)))
132      (t
133       (setq show-button nil)))
134     (when show-button
135       (insert-button
136        (concat "[ " label " ]")
137        :type 'notmuch-crypto-status-button-type
138        'help-echo help-msg
139        'face face
140        'mouse-face face
141        'action button-action
142        :notmuch-sigstatus sigstatus
143        :notmuch-from from)
144       (insert "\n"))))
145
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))
155       (recenter -1))))
156
157 (declare-function notmuch-show-refresh-view "notmuch-show" (&optional reset-state))
158
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.
162
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."
171                keyid
172                (if (= exit-status 0)
173                    "completed"
174                  "failed"))
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)
182                    (= show-point
183                       (with-current-buffer show-buffer
184                         (point))))
185           (with-current-buffer show-buffer
186             (notmuch-show-refresh-view)))))))
187
188 (defun notmuch-crypto--set-button-label (button label)
189   "Set the text displayed in BUTTON to LABEL."
190   (save-excursion
191     (let ((inhibit-read-only t))
192       ;; This knows rather too much about how we typically format
193       ;; buttons.
194       (goto-char (button-start button))
195       (forward-char 2)
196       (delete-region (point) (- (button-end button) 2))
197       (insert label))))
198
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
206         (progn
207           (notmuch-crypto--set-button-label
208            button (format "Retrieving key %s asynchronously..." keyid))
209           (let ((p (make-process :name "notmuch GPG key retrieval"
210                                  :buffer buffer
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,
216                                  ;; etc. on it.
217                                  :stop t)))
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)
228             (insert "\n")
229             (call-process epg-gpg-program nil t t "--list-keys" keyid))
230           (recenter -1))
231         (notmuch-show-refresh-view)))))
232
233 (defun notmuch-crypto-insert-encstatus-button (encstatus)
234   "Insert a button describing the encryption status ENCSTATUS."
235   (insert-button
236    (concat "[ "
237            (let ((status (plist-get encstatus :status)))
238              (cond
239               ((string= status "good")
240                "Decryption successful")
241               ((string= status "bad")
242                "Decryption error")
243               (t
244                (concat "Unknown encryption status"
245                        (if status (concat ": " status))))))
246            " ]")
247    :type 'notmuch-crypto-status-button-type
248    'face 'notmuch-crypto-decryption
249    'mouse-face 'notmuch-crypto-decryption)
250   (insert "\n"))
251
252 ;;
253
254 (provide 'notmuch-crypto)
255
256 ;;; notmuch-crypto.el ends here