]> git.notmuchmail.org Git - notmuch/blob - emacs/notmuch-crypto.el
emacs: Declare function notmuch-show-get-message-id
[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   "Should cryptographic MIME parts be processed?
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   "Retrieve gpg 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
107 by user FROM."
108   (let* ((status (plist-get sigstatus :status))
109          (show-button t)
110          (face 'notmuch-crypto-signature-unknown)
111          (button-action (lambda (button) (message (button-get button 'help-echo))))
112          (keyid (concat "0x" (plist-get sigstatus :keyid)))
113          label help-msg)
114     (cond
115      ((string= status "good")
116       (let ((fingerprint (concat "0x" (plist-get sigstatus :fingerprint)))
117             (userid (plist-get sigstatus :userid)))
118         ;; If userid is present it has full or greater validity.
119         (if userid
120             (setq label (concat "Good signature by: " userid)
121                   face 'notmuch-crypto-signature-good)
122           (setq label (concat "Good signature by key: " fingerprint)
123                 face 'notmuch-crypto-signature-good-key))
124         (setq button-action 'notmuch-crypto-sigstatus-good-callback
125               help-msg (concat "Click to list key ID 0x" fingerprint "."))))
126
127      ((string= status "error")
128       (setq label (concat "Unknown key ID " keyid " or unsupported algorithm")
129             button-action 'notmuch-crypto-sigstatus-error-callback
130             help-msg (concat "Click to retrieve key ID " keyid
131                              " from keyserver.")))
132
133      ((string= status "bad")
134       (setq label (concat "Bad signature (claimed key ID " keyid ")")
135             face 'notmuch-crypto-signature-bad))
136
137      (status
138       (setq label (concat "Unknown signature status: " status)))
139      (t
140       (setq show-button nil)))
141     (when show-button
142       (insert-button
143        (concat "[ " label " ]")
144        :type 'notmuch-crypto-status-button-type
145        'help-echo help-msg
146        'face face
147        'mouse-face face
148        'action button-action
149        :notmuch-sigstatus sigstatus
150        :notmuch-from from)
151       (insert "\n"))))
152
153 (defun notmuch-crypto-sigstatus-good-callback (button)
154   (let* ((id (notmuch-show-get-message-id))
155          (sigstatus (button-get button :notmuch-sigstatus))
156          (fingerprint (concat "0x" (plist-get sigstatus :fingerprint)))
157          (buffer (get-buffer-create "*notmuch-crypto-gpg-out*"))
158          (window (display-buffer buffer)))
159     (with-selected-window window
160       (with-current-buffer buffer
161         (goto-char (point-max))
162         (insert (format "-- Key %s in message %s:\n"
163                         fingerprint id))
164         (call-process notmuch-crypto-gpg-program nil t t "--batch" "--no-tty" "--list-keys" fingerprint))
165       (recenter -1))))
166
167 (declare-function notmuch-show-refresh-view "notmuch-show" (&optional reset-state))
168 (declare-function notmuch-show-get-message-id "notmuch-show" (&optional bare))
169
170 (defun notmuch-crypto--async-key-sentinel (process event)
171   "When the user asks for a GPG key to be retrieved
172 asynchronously, handle completion of that task.
173
174 If the retrieval is successful, the thread where the retrieval
175 was initiated is still displayed and the cursor has not moved,
176 redisplay the thread."
177   (let ((status (process-status process))
178         (exit-status (process-exit-status process))
179         (keyid (process-get process :gpg-key-id)))
180     (when (memq status '(exit signal))
181       (message "Getting the GPG key %s asynchronously...%s."
182                keyid
183                (if (= exit-status 0)
184                    "completed"
185                  "failed"))
186       ;; If the original buffer is still alive and point didn't move
187       ;; (i.e. the user didn't move on or away), refresh the buffer to
188       ;; show the updated signature status.
189       (let ((show-buffer (process-get process :notmuch-show-buffer))
190             (show-point (process-get process :notmuch-show-point)))
191         (when (and (bufferp show-buffer)
192                    (buffer-live-p show-buffer)
193                    (= show-point
194                       (with-current-buffer show-buffer
195                         (point))))
196           (with-current-buffer show-buffer
197             (notmuch-show-refresh-view)))))))
198
199 (defun notmuch-crypto--set-button-label (button label)
200   "Set the text displayed in BUTTON to LABEL."
201   (save-excursion
202     (let ((inhibit-read-only t))
203       ;; This knows rather too much about how we typically format
204       ;; buttons.
205       (goto-char (button-start button))
206       (forward-char 2)
207       (delete-region (point) (- (button-end button) 2))
208       (insert label))))
209
210 (defun notmuch-crypto-sigstatus-error-callback (button)
211   "When signature validation has failed, try to retrieve the
212 corresponding key when the status button is pressed."
213   (let* ((sigstatus (button-get button :notmuch-sigstatus))
214          (keyid (concat "0x" (plist-get sigstatus :keyid)))
215          (buffer (get-buffer-create "*notmuch-crypto-gpg-out*")))
216     (if notmuch-crypto-get-keys-asynchronously
217         (progn
218           (notmuch-crypto--set-button-label
219            button (format "Retrieving key %s asynchronously..." keyid))
220           (with-current-buffer buffer
221             (goto-char (point-max))
222             (insert (format "--- Retrieving key %s:\n" keyid)))
223           (let ((p (make-process :name "notmuch GPG key retrieval"
224                                  :connection-type 'pipe
225                                  :buffer buffer
226                                  :stderr buffer
227                                  :command (list notmuch-crypto-gpg-program "--recv-keys" keyid)
228                                  :sentinel #'notmuch-crypto--async-key-sentinel)))
229             (process-put p :gpg-key-id keyid)
230             (process-put p :notmuch-show-buffer (current-buffer))
231             (process-put p :notmuch-show-point (point))
232             (message "Getting the GPG key %s asynchronously..." keyid)))
233
234       (let ((window (display-buffer buffer)))
235         (with-selected-window window
236           (with-current-buffer buffer
237             (goto-char (point-max))
238             (insert (format "--- Retrieving key %s:\n" keyid))
239             (call-process notmuch-crypto-gpg-program nil t t "--recv-keys" keyid)
240             (insert "\n")
241             (call-process notmuch-crypto-gpg-program nil t t "--list-keys" keyid))
242           (recenter -1))
243         (notmuch-show-refresh-view)))))
244
245 (defun notmuch-crypto-insert-encstatus-button (encstatus)
246   "Insert a button describing the encryption status ENCSTATUS."
247   (insert-button
248    (concat "[ "
249            (let ((status (plist-get encstatus :status)))
250              (cond
251               ((string= status "good")
252                "Decryption successful")
253               ((string= status "bad")
254                "Decryption error")
255               (t
256                (concat "Unknown encryption status"
257                        (if status (concat ": " status))))))
258            " ]")
259    :type 'notmuch-crypto-status-button-type
260    'face 'notmuch-crypto-decryption
261    'mouse-face 'notmuch-crypto-decryption)
262   (insert "\n"))
263
264 ;;
265
266 (provide 'notmuch-crypto)
267
268 ;;; notmuch-crypto.el ends here