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