]> git.notmuchmail.org Git - notmuch/blob - emacs/notmuch-crypto.el
emacs: Drop content-free "Unknown signature status" button
[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 (defface notmuch-crypto-part-header
47   '((((class color)
48       (background dark))
49      (:foreground "LightBlue1"))
50     (((class color)
51       (background light))
52      (:foreground "blue")))
53   "Face used for crypto parts headers."
54   :group 'notmuch-crypto
55   :group 'notmuch-faces)
56
57 (defface notmuch-crypto-signature-good
58   '((t (:background "green" :foreground "black")))
59   "Face used for good signatures."
60   :group 'notmuch-crypto
61   :group 'notmuch-faces)
62
63 (defface notmuch-crypto-signature-good-key
64   '((t (:background "orange" :foreground "black")))
65   "Face used for good signatures."
66   :group 'notmuch-crypto
67   :group 'notmuch-faces)
68
69 (defface notmuch-crypto-signature-bad
70   '((t (:background "red" :foreground "black")))
71   "Face used for bad signatures."
72   :group 'notmuch-crypto
73   :group 'notmuch-faces)
74
75 (defface notmuch-crypto-signature-unknown
76   '((t (:background "red" :foreground "black")))
77   "Face used for signatures of unknown status."
78   :group 'notmuch-crypto
79   :group 'notmuch-faces)
80
81 (defface notmuch-crypto-decryption
82   '((t (:background "purple" :foreground "black")))
83   "Face used for encryption/decryption status messages."
84   :group 'notmuch-crypto
85   :group 'notmuch-faces)
86
87 (define-button-type 'notmuch-crypto-status-button-type
88   'action (lambda (button) (message (button-get button 'help-echo)))
89   'follow-link t
90   'help-echo "Set notmuch-crypto-process-mime to process cryptographic mime parts."
91   :supertype 'notmuch-button-type)
92
93 (defun notmuch-crypto-insert-sigstatus-button (sigstatus from)
94   (let* ((status (plist-get sigstatus :status))
95          (help-msg nil)
96          (show-button t)
97          (label nil)
98          (face 'notmuch-crypto-signature-unknown)
99          (button-action (lambda (button) (message (button-get button 'help-echo)))))
100     (cond
101      ((string= status "good")
102       (let ((fingerprint (concat "0x" (plist-get sigstatus :fingerprint))))
103         ;; if userid present, userid has full or greater validity
104         (if (plist-member sigstatus :userid)
105             (let ((userid (plist-get sigstatus :userid)))
106               (setq label (concat "Good signature by: " userid))
107               (setq face 'notmuch-crypto-signature-good))
108           (progn
109             (setq label (concat "Good signature by key: " fingerprint))
110             (setq face 'notmuch-crypto-signature-good-key)))
111         (setq button-action 'notmuch-crypto-sigstatus-good-callback)
112         (setq help-msg (concat "Click to list key ID 0x" fingerprint "."))))
113      ((string= status "error")
114       (let ((keyid (concat "0x" (plist-get sigstatus :keyid))))
115         (setq label (concat "Unknown key ID " keyid " or unsupported algorithm"))
116         (setq button-action 'notmuch-crypto-sigstatus-error-callback)
117         (setq help-msg (concat "Click to retrieve key ID " keyid " from keyserver and redisplay."))))
118      ((string= status "bad")
119       (let ((keyid (concat "0x" (plist-get sigstatus :keyid))))
120         (setq label (concat "Bad signature (claimed key ID " keyid ")"))
121         (setq face 'notmuch-crypto-signature-bad)))
122      (status
123       (setq label (concat "Unknown signature status: " status)))
124      (t
125       (setq show-button nil)))
126     (when show-button
127       (insert-button
128        (concat "[ " label " ]")
129        :type 'notmuch-crypto-status-button-type
130        'help-echo help-msg
131        'face face
132        'mouse-face face
133        'action button-action
134        :notmuch-sigstatus sigstatus
135        :notmuch-from from)
136       (insert "\n"))))
137
138 (declare-function notmuch-show-refresh-view "notmuch-show" (&optional reset-state))
139
140 (defun notmuch-crypto-sigstatus-good-callback (button)
141   (let* ((sigstatus (button-get button :notmuch-sigstatus))
142          (fingerprint (concat "0x" (plist-get sigstatus :fingerprint)))
143          (buffer (get-buffer-create "*notmuch-crypto-gpg-out*"))
144          (window (display-buffer buffer t nil)))
145     (with-selected-window window
146       (with-current-buffer buffer
147         (goto-char (point-max))
148         (call-process epg-gpg-program nil t t "--batch" "--no-tty" "--list-keys" fingerprint))
149       (recenter -1))))
150
151 (defun notmuch-crypto-sigstatus-error-callback (button)
152   (let* ((sigstatus (button-get button :notmuch-sigstatus))
153          (keyid (concat "0x" (plist-get sigstatus :keyid)))
154          (buffer (get-buffer-create "*notmuch-crypto-gpg-out*"))
155          (window (display-buffer buffer t nil)))
156     (with-selected-window window
157       (with-current-buffer buffer
158         (goto-char (point-max))
159         (call-process epg-gpg-program nil t t "--batch" "--no-tty" "--recv-keys" keyid)
160         (insert "\n")
161         (call-process epg-gpg-program nil t t "--batch" "--no-tty" "--list-keys" keyid))
162       (recenter -1))
163     (notmuch-show-refresh-view)))
164
165 (defun notmuch-crypto-insert-encstatus-button (encstatus)
166   (let* ((status (plist-get encstatus :status))
167          (help-msg nil)
168          (label "Decryption not attempted")
169          (face 'notmuch-crypto-decryption))
170     (cond
171      ((string= status "good")
172       (setq label "Decryption successful"))
173      ((string= status "bad")
174       (setq label "Decryption error"))
175      (t
176       (setq label (concat "Unknown encryption status"
177                           (if status (concat ": " status))))))
178     (insert-button
179      (concat "[ " label " ]")
180      :type 'notmuch-crypto-status-button-type
181      'help-echo help-msg
182      'face face
183      'mouse-face face)
184     (insert "\n")))
185
186 ;;
187
188 (provide 'notmuch-crypto)
189
190 ;;; notmuch-crypto.el ends here