1 ;;; riece-epg.el --- Encrypt/decrypt messages add-on
2 ;; Copyright (C) 2006 Daiki Ueno
4 ;; Author: Daiki Ueno <ueno@unixuser.org>
5 ;; Keywords: IRC, riece
7 ;; This file is part of Riece.
9 ;; This program is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; This program is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 ;; Boston, MA 02110-1301, USA.
26 (require 'riece-message)
27 (require 'riece-identity)
29 (autoload 'epg-make-context "epg")
30 (autoload 'epg-decrypt-string "epg")
31 (autoload 'epg-encrypt-string "epg")
32 (autoload 'epg-passphrase-callback-function "epg")
33 (autoload 'epg-context-set-passphrase-callback "epg")
36 (autoload 'riece-command-send-message "riece-commands"))
38 (defgroup riece-epg nil
39 "Encrypt/decrypt messages."
42 (defconst riece-epg-description
43 "Encrypt/decrypt messages.")
45 (defvar riece-epg-passphrase-alist nil)
47 (defun riece-epg-passphrase-callback-function (context key-id identity)
49 (let ((entry (riece-identity-assoc identity riece-epg-passphrase-alist))
51 (or (copy-sequence (cdr entry))
54 (setq entry (list identity)
55 riece-epg-passphrase-alist (cons entry
56 riece-epg-passphrase-alist)))
57 (setq passphrase (epg-passphrase-callback-function context
59 (setcdr entry (copy-sequence passphrase))
61 (epg-passphrase-callback-function context key-id nil)))
63 (defun riece-epg-passphrase-callback-function-for-decrypt (context key-id
66 (let ((entry (riece-identity-assoc identity riece-epg-passphrase-alist))
69 (copy-sequence (cdr entry))
70 (epg-cancel context)))
71 (epg-passphrase-callback-function context key-id nil)))
73 (defun riece-epg-funcall-clear-passphrase (identity function &rest args)
77 (let ((entry (riece-identity-assoc identity riece-epg-passphrase-alist)))
79 (setq riece-epg-passphrase-alist
80 (delq entry riece-epg-passphrase-alist))))
81 (signal (car error) (cdr error)))))
83 (defun riece-command-enter-encrypted-message ()
84 "Encrypt the current line send send it to the current channel."
86 (let ((context (epg-make-context))
87 (string (buffer-substring
88 (riece-line-beginning-position)
89 (riece-line-end-position)))
91 (riece-with-server-buffer (riece-identity-server riece-current-channel)
92 (setq string (riece-encode-coding-string-for-identity
94 riece-current-channel)))
95 (epg-context-set-passphrase-callback
97 (cons #'riece-epg-passphrase-callback-function
98 riece-current-channel))
99 (setq string (riece-epg-funcall-clear-passphrase riece-current-channel
102 (riece-command-send-message
103 (concat "[encrypted:" (base64-encode-string string t) "]")
105 (let ((next-line-add-newlines t))
108 (defun riece-command-set-passphrase (identity passphrase)
109 "Set PASSPHRASE associated with IDENTITY."
112 (riece-completing-read-identity
114 riece-current-channels nil t nil nil
115 (riece-format-identity riece-current-channel))))
117 (read-passwd (format "Passphrase for %s: "
118 (riece-format-identity identity))))))
119 (let ((entry (riece-identity-assoc identity riece-epg-passphrase-alist)))
120 (if (equal passphrase "")
122 (setq riece-epg-passphrase-alist
123 (delq entry riece-epg-passphrase-alist)))
125 (setcdr entry passphrase)
126 (setq riece-epg-passphrase-alist
127 (cons (cons identity passphrase)
128 riece-epg-passphrase-alist))))))
130 (defun riece-epg-decrypt-string-for-identity (context cipher target)
132 (or (riece-coding-system-for-identity target)
133 riece-default-coding-system)))
134 (riece-with-server-buffer (riece-identity-server target)
135 (decode-coding-string
136 (riece-epg-funcall-clear-passphrase
140 (base64-decode-string cipher))
141 (if (consp coding-system)
145 (defun riece-epg-message-filter (message)
146 (if (get 'riece-epg 'riece-addon-enabled)
147 (when (string-match "\\`\\[encrypted:\\(.*\\)]"
148 (riece-message-text message))
149 (let ((context (epg-make-context))
150 (string (match-string 1 (riece-message-text message)))
152 (epg-context-set-passphrase-callback
154 (cons #'riece-epg-passphrase-callback-function-for-decrypt
155 riece-current-channel))
156 (condition-case error
158 (riece-message-set-text
162 (riece-epg-decrypt-string-for-identity
163 context string (riece-message-target message))
166 (riece-put-text-property-nonsticky
167 0 (length (riece-message-text message))
168 'riece-epg-encryption-target (riece-message-target message)
169 (riece-message-text message))
170 (message "%s" (cdr error)))))))
173 (defun riece-epg-add-encrypted-button (start end)
174 (if (and (get 'riece-button 'riece-addon-enabled)
175 (get 'riece-epg 'riece-addon-enabled))
176 (riece-scan-property-region
177 'riece-epg-encryption-target
180 (let ((inhibit-read-only t)
182 (widget-convert-button
184 :help-echo "Click to decrypt"
185 :notify #'riece-epg-encrypted-button-notify
186 (get-text-property start 'riece-epg-encryption-target)))))))
188 (defun riece-epg-encrypted-button-notify (widget &rest ignore)
189 (let* ((from (marker-position (widget-get widget :from)))
190 (to (marker-position (widget-get widget :to)))
191 (target (widget-get widget :value))
192 (cipher (buffer-substring from to))
193 (inhibit-read-only t)
196 (when (string-match "\\`\\[encrypted:\\(.*\\)]" cipher)
197 (setq plain (riece-epg-decrypt-string-for-identity
198 (epg-make-context) (match-string 1 cipher) target))
199 (widget-delete widget)
200 (delete-region from to)
203 (insert "[decrypted:" plain "]")))))
205 (defun riece-epg-requires ()
206 (if (memq 'riece-button riece-addons)
209 (defun riece-epg-insinuate ()
210 (add-hook 'riece-message-filter-functions 'riece-epg-message-filter)
211 (add-hook 'riece-after-insert-functions 'riece-epg-add-encrypted-button))
213 (defun riece-epg-uninstall ()
214 (remove-hook 'riece-message-filter-functions 'riece-epg-message-filter)
215 (remove-hook 'riece-after-insert-functions 'riece-epg-add-encrypted-button))
217 (defvar riece-command-mode-map)
218 (defun riece-epg-enable ()
219 (define-key riece-command-mode-map
220 "\C-ce" 'riece-command-enter-encrypted-message)
221 (define-key riece-command-mode-map
222 "\C-c\C-ec" 'riece-command-set-passphrase))
224 (defun riece-epg-disable ()
225 (define-key riece-command-mode-map
227 (define-key riece-command-mode-map
232 ;;; riece-epg.el ends here