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