* epg.el (epg-status-GET_BOOL): New function.
[elisp/epg.git] / epg-file.el
index 08d1bcb..cd6f6a0 100644 (file)
@@ -1,5 +1,42 @@
+;;; epg-file.el --- transparent file encryption utility
+;; Copyright (C) 1999, 2000, 2002, 2003, 2004,
+;;   2005, 2006 Free Software Foundation, Inc.
+;; Copyright (C) 2006 Daiki Ueno
+
+;; Author: Daiki Ueno <ueno@unixuser.org>
+;;     Naoto Morishima <naoto@morishima.net>
+;; Keywords: PGP, GnuPG
+
+;; This file is part of EasyPG.
+
+;; This program is free software; you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation; either version 2, or (at your option)
+;; any later version.
+
+;; This program is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.         See the
+;; GNU General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with GNU Emacs; see the file COPYING.  If not, write to the
+;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+;; Boston, MA 02110-1301, USA.
+
+;;; Commentary:
+
+;; epg-file.el is based on hedgehog.el by Naoto Morishima.
+;; http://www.morishima.net/~naoto/software/hedgehog/index.php.ja
+
+;;; Code:
+
 (require 'epg)
 
+(defgroup epg-file ()
+  "Transparent file encryption utility of EasyPG."
+  :group 'epg)
+
 (defcustom epg-file-name-regexp "\\.gpg\\'"
   "Regexp that matches filenames that are assumed to be encrypted
 with GnuPG."
@@ -10,16 +47,18 @@ with GnuPG."
   (let ((epg-file-operation (get operation 'epg-file)))
     (if epg-file-operation
         (apply epg-file-operation args)
-      (epg-file-invoke-default-handler operation args))))
+      (epg-file-run-real-handler operation args))))
 
-(defun epg-file-invoke-default-handler (operation args)
+(defun epg-file-run-real-handler (operation args)
   (let ((inhibit-file-name-handlers
-         (cons 'epg-file-handler
-               (and (eq inhibit-file-name-operation operation)
-                    inhibit-file-name-handlers)))
-        (inhibit-file-name-operation operation))
+        (cons 'epg-file-handler
+              (and (eq inhibit-file-name-operation operation)
+                   inhibit-file-name-handlers)))
+       (inhibit-file-name-operation operation))
     (apply operation args)))
 
+(defvar buffer-file-type)
+(defvar last-coding-system-used)
 (defun epg-file-write-region (start end filename &optional append visit
                                    lockname mustbenew)
   (let* ((visit-file (if (stringp visit)
@@ -27,10 +66,11 @@ with GnuPG."
                        (expand-file-name filename)))
          ;; XXX: Obtain the value returned by choose_write_coding_system
          (coding-system (condition-case nil
-                            (epg-file-invoke-default-handler
-                            #'write-region
-                            (list start end "/"))
-                          (file-error last-coding-system-used)))
+                           (epg-file-run-real-handler
+                            'write-region (list start end "/"))
+                          (file-error (if (boundp 'last-coding-system-used)
+                                         last-coding-system-used
+                                       buffer-file-coding-system))))
          ;; start and end are normally buffer positions
          ;; specifying the part of the buffer to write.
          ;; If start is nil, that means to use the entire buffer contents.
@@ -45,7 +85,8 @@ with GnuPG."
                                          (buffer-substring start end)))
                                        coding-system)))
     (with-temp-buffer
-      (set-buffer-multibyte nil)
+      (if (fboundp 'set-buffer-multibyte)
+         (set-buffer-multibyte nil))
       ;; Optional fourth argument append if non-nil means
       ;;   append to existing file contents (if any).  If it is an integer,
       ;;   seek to that offset in the file before writing.
@@ -63,12 +104,20 @@ with GnuPG."
       (let ((coding-system-for-write 'binary)
             (coding-system-for-read 'binary)
            (context (epg-make-context))
+           recipients
+           string
            cipher)
-       (when (setq cipher (epg-encrypt-string context (buffer-string) nil))
-         (if (memq system-type '(ms-dos windows-nt))
+       (while (not (equal (setq string
+                                (read-string "To (end with an empty line): "))
+                          ""))
+         (setq recipients (cons string recipients)))
+       (when (setq cipher (epg-encrypt-string context (buffer-string)
+                                              recipients))
+         (if (and (memq system-type '(ms-dos windows-nt))
+                  (boundp 'buffer-file-type))
              (setq buffer-file-type t))
-         (epg-file-invoke-default-handler
-          #'write-region
+         (epg-file-run-real-handler
+          'write-region
           (list cipher nil filename nil 'not-visit lockname mustbenew)))))
     ;; Optional fifth argument visit, if t or a string, means
     ;;   set the last-save-file-modtime of buffer to this file's modtime
@@ -81,33 +130,33 @@ with GnuPG."
     (when (or (eq visit t) (stringp visit))
       (setq buffer-file-name filename)
       (set-visited-file-modtime))
-    (when (stringp visit)
-      (setq buffer-file-name visit))
+    (if (stringp visit)
+       (setq buffer-file-name visit))
     (when (or (eq visit t) (eq visit nil) (stringp visit))
       (message "Wrote %s" visit-file))
-    (setq last-coding-system-used coding-system)
+    (if (boundp 'last-coding-system-used)
+       (setq last-coding-system-used coding-system))
     nil))
 
 (defun epg-file-insert-file-contents (filename &optional visit beg end replace)
   (barf-if-buffer-read-only)
-
-  ;; If visit is non-nil, beg and end must be nil.
-  (if (and visit (or beg end))
-      (error "Attempt to visit less than an entire file"))
-
-  (let ((expanded-filename (expand-file-name filename))
+  (setq filename (expand-file-name filename))
+  (let ((filename (expand-file-name filename))
        (length 0))
-    (if (file-exists-p expanded-filename)
-       (let* ((local-copy (epg-file-invoke-default-handler
-                           'file-local-copy
-                           (list expanded-filename)))
-              (local-file (or local-copy expanded-filename))
-              (coding-system-for-read 'binary)
-              (context (epg-make-context))
-              string)
+    (if (file-exists-p filename)
+       (let ((local-file
+              (let ((inhibit-file-name-operation
+                     (when (eq inhibit-file-name-operation
+                               'insert-file-contents)
+                       'file-local-copy)))
+                (file-local-copy filename)))
+             (coding-system-for-read 'binary)
+             (context (epg-make-context))
+             string)
          (unwind-protect
              (progn
-               (setq string (epg-decrypt-file context local-file)
+               (setq string (epg-decrypt-file context (or local-file
+                                                          filename))
                      length (length string))
                (if replace
                    (goto-char (point-min)))
@@ -118,22 +167,22 @@ with GnuPG."
                      (insert (decode-coding-string string 'undecided)))
                    (if replace
                        (delete-region (point) (point-max))))))
-           (when (and local-copy (file-exists-p local-copy))
-             (delete-file local-copy)))))
+           (when (and local-file (file-exists-p local-file))
+             (delete-file local-file)))))
     ;; If second argument visit is non-nil, the buffer's visited filename
     ;; and last save file modtime are set, and it is marked unmodified.
     (when visit
       (unlock-buffer)
-      (setq buffer-file-name expanded-filename)
+      (setq buffer-file-name filename)
       (set-visited-file-modtime))
 
     ;; If visiting and the file does not exist, visiting is completed
     ;; before the error is signaled.
-    (if (and visit (not (file-exists-p expanded-filename)))
+    (if (and visit (not (file-exists-p filename)))
        (signal 'file-error (list "Opening input file" filename)))
 
     ;; Returns list of absolute file name and number of characters inserted.
-    (list expanded-filename length)))
+    (list filename length)))
 
 (put 'write-region 'epg-file 'epg-file-write-region)
 (put 'insert-file-contents 'epg-file 'epg-file-insert-file-contents)
@@ -147,3 +196,7 @@ with GnuPG."
   (setq auto-mode-alist
        (cons (list epg-file-name-regexp nil 'strip-suffix)
              auto-mode-alist)))
+
+(provide 'epg-file)
+
+;;; epg-file.el ends here