Fixed.
[elisp/epg.git] / epa-file.el
1 ;;; epa-file.el --- the EasyPG Assistant hooks for transparent file encryption
2 ;; Copyright (C) 2006 Daiki Ueno
3
4 ;; Author: Daiki Ueno <ueno@unixuser.org>
5 ;; Keywords: PGP, GnuPG
6
7 ;; This file is part of EasyPG.
8
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)
12 ;; any later version.
13
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.
18
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.
23
24 ;;; Code:
25
26 (require 'epa)
27
28 (defgroup epa-file nil
29   "The EasyPG Assistant hooks for transparent file encryption"
30   :group 'epa)
31
32 (defcustom epa-file-name-regexp "\\.gpg\\'"
33   "Regexp which matches filenames to be encrypted with GnuPG."
34   :type 'regexp
35   :group 'epa-file)
36   
37 (defvar epa-file nil)
38 (defvar epa-passphrase nil)
39
40 (defun epa-file-passphrase-callback-function (key-id buffer)
41   (save-excursion
42     (set-buffer buffer)
43     (if (eq key-id 'SYM)
44         (or epa-passphrase
45             (let ((passphrase (epg-passphrase-callback-function
46                                key-id buffer)))
47               (setq epa-passphrase (copy-sequence passphrase))
48               passphrase))
49       (epg-passphrase-callback-function key-id buffer))))
50
51 (defvar last-coding-system-used)
52 (defun epa-find-file ()
53   (when (string-match epa-file-name-regexp (buffer-file-name))
54     (if (file-exists-p (expand-file-name (buffer-file-name)))
55         (let ((context (epg-make-context))
56               passphrase)
57           (if (fboundp 'set-buffer-multibyte)
58               (set-buffer-multibyte t))
59           (goto-char (point-min))
60           (epg-context-set-passphrase-callback
61            context
62            (cons #'epa-file-passphrase-callback-function
63                  (current-buffer)))
64           (make-local-variable 'epa-passphrase)
65           (insert (epg-decrypt-file context
66                                     (expand-file-name (buffer-file-name))
67                                     nil))
68           (setq passphrase epa-passphrase)
69           (delete-region (point) (point-max))
70           (decode-coding-region (point-min) (point-max) 'undecided)
71           (if (boundp 'last-coding-system-used)
72               (set-buffer-file-coding-system last-coding-system-used)
73             (set-buffer-file-coding-system default-buffer-file-coding-system))
74           (set-auto-mode)
75           (hack-local-variables)
76           (auto-save-mode nil)
77           (set-buffer-modified-p nil)
78           (setq buffer-undo-list nil)
79           (make-local-variable 'epa-passphrase)
80           (setq epa-passphrase passphrase)))
81     (make-local-variable 'epa-passphrase)
82     (make-local-variable 'epa-file)
83     (setq epa-file (buffer-file-name))))
84
85 (defun epa-write-file ()
86   (if epa-file
87       (let* ((coding-system (if (boundp 'last-coding-system-used)
88                                 (condition-case nil
89                                     (write-region (point-min) (point-max) "/")
90                                   (error last-coding-system-used))
91                               buffer-file-coding-system))
92              (context (epg-make-context))
93              (coding-system-for-write 'binary))
94         (epg-context-set-passphrase-callback
95          context
96          (cons #'epa-file-passphrase-callback-function
97                (current-buffer)))
98         (write-region
99          (epg-encrypt-string
100           context
101           (encode-coding-string (buffer-string) coding-system)
102           (mapcar (lambda (key)
103                     (epg-sub-key-id (car (epg-key-sub-key-list key))))
104                   (unless epa-passphrase
105                     (epa-select-keys
106                      "Select recipents for encryption.
107 If no one is selected, symmetric encryption will be performed.  "))))
108          nil (expand-file-name (buffer-file-name)))
109         (if (boundp 'last-coding-system-used)
110             (setq last-coding-system-used coding-system))
111         (set-visited-file-modtime)
112         (set-buffer-modified-p nil)
113         t)))
114
115 (provide 'epa-file)
116
117 ;;; epa-file.el ends here