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