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