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