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