ae4fe00ec70ed2a769dbd64a7220b40e08482054
[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 configuration files of `epg-gpg-program'."
42   :group 'epg
43   :type '(choice (const :tag "Default" nil) directory))
44
45 (defcustom epg-locale-coding-system (if (boundp 'locale-coding-system)
46                                         locale-coding-system)
47   "Coding system to use with messages from `epg-gpg-program'."
48   :group 'epg
49   :type 'symbol)
50
51 (defcustom epg-debug nil
52   "If non-nil, debug output goes to the \" *epg-debug*\" buffer.
53 Note that the buffer name starts with a space."
54   :group 'epg
55   :type 'boolean)
56
57 (defconst epg-version-number "0.0.5")
58
59 (defconst epg-gpg-minimum-version "1.4.3")
60
61 ;;;###autoload
62 (defun epg-configuration ()
63   "Return a list of internal configuration parameters of `epg-gpg-program'."
64   (let (config groups type args)
65     (with-temp-buffer
66       (apply #'call-process epg-gpg-program nil (list t nil) nil
67              (append (if epg-gpg-home-directory
68                          (list "--homedir" epg-gpg-home-directory))
69                      '("--with-colons" "--list-config")))
70       (goto-char (point-min))
71       (while (re-search-forward "^cfg:\\([^:]+\\):\\(.*\\)" nil t)
72         (setq type (intern (match-string 1))
73               args (match-string 2))
74         (cond
75          ((eq type 'group)
76           (if (string-match "\\`\\([^:]+\\):" args)
77                   (setq groups
78                         (cons (cons (downcase (match-string 1 args))
79                                     (delete "" (split-string
80                                                 (substring args
81                                                            (match-end 0)))))
82                               groups))
83             (if epg-debug
84                 (message "Invalid group configuration: %S" args))))
85          ((memq type '(pubkey cipher digest compress))
86           (if (string-match "\\`\\([0-9]+\\)\\(;[0-9]+\\)*" args)
87               (setq config
88                     (cons (cons type
89                                 (mapcar #'string-to-number
90                                         (delete "" (split-string args ";"))))
91                           config))
92             (if epg-debug
93                 (message "Invalid %S algorithm configuration: %S"
94                          type args)))))))
95     (if groups
96         (cons (cons 'groups groups) config)
97       config)))
98
99 (defun epg-config--parse-version (string)
100   (let ((index 0)
101         version)
102     (while (eq index (string-match "\\([0-9]+\\)\\.?" string index))
103       (setq version (cons (string-to-number (match-string 1 string))
104                           version)
105             index (match-end 0)))
106     (nreverse version)))
107
108 (defun epg-config--compare-version (v1 v2)
109   (while (and v1 v2 (= (car v1) (car v2)))
110     (setq v1 (cdr v1) v2 (cdr v2)))
111   (- (or (car v1) 0) (or (car v2) 0)))
112
113 ;;;###autoload
114 (defun epg-check-configuration (config &optional minimum-version)
115   "Verify that a sufficient version of GnuPG is installed."
116   (let ((entry (assq 'version config))
117         version)
118     (unless (and entry
119                  (stringp (cdr entry)))
120       (error "Undetermined version: %S" entry))
121     (setq version (epg-config--parse-version (cdr entry))
122           minimum-version (epg-config--parse-version
123                            (or minimum-version
124                                epg-gpg-minimum-version)))
125     (unless (>= (epg-config--compare-version version minimum-version) 0)
126       (error "Unsupported version: %s" (cdr entry)))))
127
128 ;;;###autoload
129 (defun epg-expand-group (config group)
130   "Look at CONFIG and try to expand GROUP."
131   (let ((entry (assq 'groups config)))
132     (if (and entry
133              (setq entry (assoc (downcase group) (cdr entry))))
134         (cdr entry))))
135
136 (provide 'epg-config)
137
138 ;;; epg-config.el ends here