]> git.notmuchmail.org Git - notmuch/blob - emacs/notmuch-crypto.el
Merge tag '0.23.5'
[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 'notmuch-lib)
25
26 (defcustom notmuch-crypto-process-mime nil
27   "Should cryptographic MIME parts be processed?
28
29 If this variable is non-nil signatures in multipart/signed
30 messages will be verified and multipart/encrypted parts will be
31 decrypted.  The result of the crypto operation will be displayed
32 in a specially colored header button at the top of the processed
33 part.  Signed parts will have variously colored headers depending
34 on the success or failure of the verification process and on the
35 validity of user ID of the signer.
36
37 The effect of setting this variable can be seen temporarily by
38 providing a prefix when viewing a signed or encrypted message, or
39 by providing a prefix when reloading the message in notmuch-show
40 mode."
41   :type 'boolean
42   :group 'notmuch-crypto)
43
44 (defface notmuch-crypto-part-header
45   '((((class color)
46       (background dark))
47      (:foreground "LightBlue1"))
48     (((class color)
49       (background light))
50      (:foreground "blue")))
51   "Face used for crypto parts headers."
52   :group 'notmuch-crypto
53   :group 'notmuch-faces)
54
55 (defface notmuch-crypto-signature-good
56   '((t (:background "green" :foreground "black")))
57   "Face used for good signatures."
58   :group 'notmuch-crypto
59   :group 'notmuch-faces)
60
61 (defface notmuch-crypto-signature-good-key
62   '((t (:background "orange" :foreground "black")))
63   "Face used for good signatures."
64   :group 'notmuch-crypto
65   :group 'notmuch-faces)
66
67 (defface notmuch-crypto-signature-bad
68   '((t (:background "red" :foreground "black")))
69   "Face used for bad signatures."
70   :group 'notmuch-crypto
71   :group 'notmuch-faces)
72
73 (defface notmuch-crypto-signature-unknown
74   '((t (:background "red" :foreground "black")))
75   "Face used for signatures of unknown status."
76   :group 'notmuch-crypto
77   :group 'notmuch-faces)
78
79 (defface notmuch-crypto-decryption
80   '((t (:background "purple" :foreground "black")))
81   "Face used for encryption/decryption status messages."
82   :group 'notmuch-crypto
83   :group 'notmuch-faces)
84
85 (define-button-type 'notmuch-crypto-status-button-type
86   'action (lambda (button) (message (button-get button 'help-echo)))
87   'follow-link t
88   'help-echo "Set notmuch-crypto-process-mime to process cryptographic mime parts."
89   :supertype 'notmuch-button-type)
90
91 (defun notmuch-crypto-insert-sigstatus-button (sigstatus from)
92   (let* ((status (plist-get sigstatus :status))
93          (help-msg nil)
94          (label "Signature not processed")
95          (face 'notmuch-crypto-signature-unknown)
96          (button-action (lambda (button) (message (button-get button 'help-echo)))))
97     (cond
98      ((string= status "good")
99       (let ((fingerprint (concat "0x" (plist-get sigstatus :fingerprint))))
100         ;; if userid present, userid has full or greater validity
101         (if (plist-member sigstatus :userid)
102             (let ((userid (plist-get sigstatus :userid)))
103               (setq label (concat "Good signature by: " userid))
104               (setq face 'notmuch-crypto-signature-good))
105           (progn
106             (setq label (concat "Good signature by key: " fingerprint))
107             (setq face 'notmuch-crypto-signature-good-key)))
108         (setq button-action 'notmuch-crypto-sigstatus-good-callback)
109         (setq help-msg (concat "Click to list key ID 0x" fingerprint "."))))
110      ((string= status "error")
111       (let ((keyid (concat "0x" (plist-get sigstatus :keyid))))
112         (setq label (concat "Unknown key ID " keyid " or unsupported algorithm"))
113         (setq button-action 'notmuch-crypto-sigstatus-error-callback)
114         (setq help-msg (concat "Click to retrieve key ID " keyid " from keyserver and redisplay."))))
115      ((string= status "bad")
116       (let ((keyid (concat "0x" (plist-get sigstatus :keyid))))
117         (setq label (concat "Bad signature (claimed key ID " keyid ")"))
118         (setq face 'notmuch-crypto-signature-bad)))
119      (t
120       (setq label (concat "Unknown signature status"
121                           (if status (concat ": " status))))))
122     (insert-button
123      (concat "[ " label " ]")
124      :type 'notmuch-crypto-status-button-type
125      'help-echo help-msg
126      'face face
127      'mouse-face face
128      'action button-action
129      :notmuch-sigstatus sigstatus
130      :notmuch-from from)
131     (insert "\n")))
132
133 (declare-function notmuch-show-refresh-view "notmuch-show" (&optional reset-state))
134
135 (defun notmuch-crypto-sigstatus-good-callback (button)
136   (let* ((sigstatus (button-get button :notmuch-sigstatus))
137          (fingerprint (concat "0x" (plist-get sigstatus :fingerprint)))
138          (buffer (get-buffer-create "*notmuch-crypto-gpg-out*"))
139          (window (display-buffer buffer t nil)))
140     (with-selected-window window
141       (with-current-buffer buffer
142         (goto-char (point-max))
143         (call-process "gpg" nil t t "--list-keys" fingerprint))
144       (recenter -1))))
145
146 (defun notmuch-crypto-sigstatus-error-callback (button)
147   (let* ((sigstatus (button-get button :notmuch-sigstatus))
148          (keyid (concat "0x" (plist-get sigstatus :keyid)))
149          (buffer (get-buffer-create "*notmuch-crypto-gpg-out*"))
150          (window (display-buffer buffer t nil)))
151     (with-selected-window window
152       (with-current-buffer buffer
153         (goto-char (point-max))
154         (call-process "gpg" nil t t "--recv-keys" keyid)
155         (insert "\n")
156         (call-process "gpg" nil t t "--list-keys" keyid))
157       (recenter -1))
158     (notmuch-show-refresh-view)))
159
160 (defun notmuch-crypto-insert-encstatus-button (encstatus)
161   (let* ((status (plist-get encstatus :status))
162          (help-msg nil)
163          (label "Decryption not attempted")
164          (face 'notmuch-crypto-decryption))
165     (cond
166      ((string= status "good")
167       (setq label "Decryption successful"))
168      ((string= status "bad")
169       (setq label "Decryption error"))
170      (t
171       (setq label (concat "Unknown encryption status"
172                           (if status (concat ": " status))))))
173     (insert-button
174      (concat "[ " label " ]")
175      :type 'notmuch-crypto-status-button-type
176      'help-echo help-msg
177      'face face
178      'mouse-face face)
179     (insert "\n")))
180
181 ;;
182
183 (provide 'notmuch-crypto)
184
185 ;;; notmuch-crypto.el ends here