]> git.notmuchmail.org Git - notmuch/blob - emacs/notmuch-crypto.el
0ab85f4bde8da9d962392e963a901508fff63efa
[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   (let* ((status (plist-get sigstatus :status))
100          (help-msg nil)
101          (show-button t)
102          (label nil)
103          (face 'notmuch-crypto-signature-unknown)
104          (button-action (lambda (button) (message (button-get button 'help-echo)))))
105     (cond
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))
113           (progn
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)))
127      (status
128       (setq label (concat "Unknown signature status: " status)))
129      (t
130       (setq show-button nil)))
131     (when show-button
132       (insert-button
133        (concat "[ " label " ]")
134        :type 'notmuch-crypto-status-button-type
135        'help-echo help-msg
136        'face face
137        'mouse-face face
138        'action button-action
139        :notmuch-sigstatus sigstatus
140        :notmuch-from from)
141       (insert "\n"))))
142
143 (declare-function notmuch-show-refresh-view "notmuch-show" (&optional reset-state))
144
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))
154       (recenter -1))))
155
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.
159
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."
168                keyid
169                (if (= exit-status 0)
170                    "completed"
171                  "failed"))
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)
179                    (= show-point
180                       (with-current-buffer show-buffer
181                         (point))))
182           (with-current-buffer show-buffer
183             (notmuch-show-refresh-view)))))))
184
185 (defun notmuch-crypto--set-button-label (button label)
186   "Set the text displayed in BUTTON to LABEL."
187   (save-excursion
188     (let ((inhibit-read-only t))
189       ;; This knows rather too much about how we typically format
190       ;; buttons.
191       (goto-char (button-start button))
192       (forward-char 2)
193       (delete-region (point) (- (button-end button) 2))
194       (insert label))))
195
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
201         (progn
202           (notmuch-crypto--set-button-label
203            button (format "Retrieving key %s asynchronously..." keyid))
204           (let ((p (make-process :name "notmuch GPG key retrieval"
205                                  :buffer buffer
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,
211                                  ;; etc. on it.
212                                  :stop t)))
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)
223             (insert "\n")
224             (call-process epg-gpg-program nil t t "--list-keys" keyid))
225           (recenter -1))
226         (notmuch-show-refresh-view)))))
227
228 (defun notmuch-crypto-insert-encstatus-button (encstatus)
229   (let* ((status (plist-get encstatus :status))
230          (help-msg nil)
231          (label "Decryption not attempted")
232          (face 'notmuch-crypto-decryption))
233     (cond
234      ((string= status "good")
235       (setq label "Decryption successful"))
236      ((string= status "bad")
237       (setq label "Decryption error"))
238      (t
239       (setq label (concat "Unknown encryption status"
240                           (if status (concat ": " status))))))
241     (insert-button
242      (concat "[ " label " ]")
243      :type 'notmuch-crypto-status-button-type
244      'help-echo help-msg
245      'face face
246      'mouse-face face)
247     (insert "\n")))
248
249 ;;
250
251 (provide 'notmuch-crypto)
252
253 ;;; notmuch-crypto.el ends here