* epg-config.el (epg-passphrase-coding-system): New user option.
[elisp/epg.git] / epg-config.el
1 ;;; epg-config.el --- configuration of the EasyPG Library
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 (defgroup epg ()
27   "The EasyPG Library"
28   :group 'emacs)
29
30 (defcustom epg-gpg-program "gpg"
31   "The `gpg' executable."
32   :group 'epg
33   :type 'string)
34
35 (defcustom epg-gpgsm-program "gpgsm"
36   "The `gpgsm' executable."
37   :group 'epg
38   :type 'string)
39
40 (defcustom epg-gpg-home-directory nil
41   "The directory which contains the `gpg' configuration files."
42   :group 'epg
43   :type '(choice (const :tag "Default" nil) directory))
44
45 (defcustom epg-passphrase-coding-system nil
46   "The coding-system used to type passphrases."
47   :group 'epg
48   :type 'symbol)
49
50 (defconst epg-version-number "0.0.4")
51
52 (defconst epg-gpg-minimum-version "1.4.3")
53
54 ;;;###autoload
55 (defun epg-configuration ()
56   "Return a list of internal configuration parameters of `epg-gpg-program'."
57   (let (config type)
58     (with-temp-buffer
59       (apply #'call-process epg-gpg-program nil (list t nil) nil
60              (append (if epg-gpg-home-directory
61                          (list "--homedir" epg-gpg-home-directory))
62                      '("--with-colons" "--list-config")))
63       (goto-char (point-min))
64       (while (re-search-forward "^cfg:\\([^:]+\\):\\(.*\\)" nil t)
65         (setq type (intern (match-string 1))
66               config (cons (cons type
67                                  (if (memq type
68                                            '(pubkey cipher digest compress))
69                                      (mapcar #'string-to-number
70                                              (delete "" (split-string
71                                                          (match-string 2)
72                                                          ";")))
73                                    (match-string 2)))
74                            config))))
75     config))
76
77 (defun epg-config--parse-version (string)
78   (let ((index 0)
79         version)
80     (while (eq index (string-match "\\([0-9]+\\)\\.?" string index))
81       (setq version (cons (string-to-number (match-string 1 string))
82                           version)
83             index (match-end 0)))
84     (nreverse version)))
85
86 (defun epg-config--compare-version (v1 v2)
87   (while (and v1 v2 (= (car v1) (car v2)))
88     (setq v1 (cdr v1) v2 (cdr v2)))
89   (- (or (car v1) 0) (or (car v2) 0)))
90
91 ;;;###autoload
92 (defun epg-check-configuration (config &optional minimum-version)
93   "Verify that a sufficient version of GnuPG is installed."
94   (let ((entry (assq 'version config))
95         version)
96     (unless (and entry
97                  (stringp (cdr entry)))
98       (error "Undetermined version: %S" entry))
99     (setq version (epg-config--parse-version (cdr entry))
100           minimum-version (epg-config--parse-version
101                            (or minimum-version
102                                epg-gpg-minimum-version)))
103     (unless (>= (epg-config--compare-version version minimum-version) 0)
104       (error "Unsupported version: %s" (cdr entry)))))
105
106 (provide 'epg-config)
107
108 ;;; epg-config.el ends here