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