* lisp/emacs-lisp/package.el (package--with-work-buffer-async):
[emacs.git] / lisp / epg-config.el
blobdb2951306d0536ca6e4a9a6d45fcd43edf271b8a
1 ;;; epg-config.el --- configuration of the EasyPG Library
3 ;; Copyright (C) 2006-2015 Free Software Foundation, Inc.
5 ;; Author: Daiki Ueno <ueno@unixuser.org>
6 ;; Keywords: PGP, GnuPG
7 ;; Package: epg
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Code:
26 (defconst epg-package-name "epg"
27 "Name of this package.")
29 (defconst epg-version-number "1.0.0"
30 "Version number of this package.")
32 (defconst epg-bug-report-address "ueno@unixuser.org"
33 "Report bugs to this address.")
35 (defgroup epg ()
36 "Interface to the GNU Privacy Guard (GnuPG)."
37 :tag "EasyPG"
38 :version "23.1"
39 :group 'data
40 :group 'external)
42 (defcustom epg-gpg-program (cond ((executable-find "gpg") "gpg")
43 ((executable-find "gpg2") "gpg2")
44 (t "gpg"))
45 "The `gpg' executable."
46 :group 'epg
47 :type 'string)
49 (defcustom epg-gpgsm-program "gpgsm"
50 "The `gpgsm' executable."
51 :group 'epg
52 :type 'string)
54 (defcustom epg-gpgconf-program "gpgconf"
55 "The `gpgconf' executable."
56 :group 'epg
57 :type 'string)
59 (defcustom epg-gpg-home-directory nil
60 "The directory which contains the configuration files of `epg-gpg-program'."
61 :group 'epg
62 :type '(choice (const :tag "Default" nil) directory))
64 (defcustom epg-passphrase-coding-system nil
65 "Coding system to use with messages from `epg-gpg-program'."
66 :group 'epg
67 :type 'symbol)
69 (defcustom epg-debug nil
70 "If non-nil, debug output goes to the \" *epg-debug*\" buffer.
71 Note that the buffer name starts with a space."
72 :group 'epg
73 :type 'boolean)
75 (defconst epg-gpg-minimum-version "1.4.3")
77 ;;;###autoload
78 (defun epg-configuration ()
79 "Return a list of internal configuration parameters of `epg-gpg-program'."
80 (let (config groups type args)
81 (with-temp-buffer
82 (apply #'call-process epg-gpg-program nil (list t nil) nil
83 (append (if epg-gpg-home-directory
84 (list "--homedir" epg-gpg-home-directory))
85 '("--with-colons" "--list-config")))
86 (goto-char (point-min))
87 (while (re-search-forward "^cfg:\\([^:]+\\):\\(.*\\)" nil t)
88 (setq type (intern (match-string 1))
89 args (match-string 2))
90 (cond
91 ((eq type 'group)
92 (if (string-match "\\`\\([^:]+\\):" args)
93 (setq groups
94 (cons (cons (downcase (match-string 1 args))
95 (delete "" (split-string
96 (substring args
97 (match-end 0))
98 ";")))
99 groups))
100 (if epg-debug
101 (message "Invalid group configuration: %S" args))))
102 ((memq type '(pubkey cipher digest compress))
103 (if (string-match "\\`\\([0-9]+\\)\\(;[0-9]+\\)*" args)
104 (setq config
105 (cons (cons type
106 (mapcar #'string-to-number
107 (delete "" (split-string args ";"))))
108 config))
109 (if epg-debug
110 (message "Invalid %S algorithm configuration: %S"
111 type args))))
113 (setq config (cons (cons type args) config))))))
114 (if groups
115 (cons (cons 'groups groups) config)
116 config)))
118 (defun epg-config--parse-version (string)
119 (let ((index 0)
120 version)
121 (while (eq index (string-match "\\([0-9]+\\)\\.?" string index))
122 (setq version (cons (string-to-number (match-string 1 string))
123 version)
124 index (match-end 0)))
125 (nreverse version)))
127 (defun epg-config--compare-version (v1 v2)
128 (while (and v1 v2 (= (car v1) (car v2)))
129 (setq v1 (cdr v1) v2 (cdr v2)))
130 (- (or (car v1) 0) (or (car v2) 0)))
132 ;;;###autoload
133 (defun epg-check-configuration (config &optional minimum-version)
134 "Verify that a sufficient version of GnuPG is installed."
135 (let ((entry (assq 'version config))
136 version)
137 (unless (and entry
138 (stringp (cdr entry)))
139 (error "Undetermined version: %S" entry))
140 (setq version (epg-config--parse-version (cdr entry))
141 minimum-version (epg-config--parse-version
142 (or minimum-version
143 epg-gpg-minimum-version)))
144 (unless (>= (epg-config--compare-version version minimum-version) 0)
145 (error "Unsupported version: %s" (cdr entry)))))
147 ;;;###autoload
148 (defun epg-expand-group (config group)
149 "Look at CONFIG and try to expand GROUP."
150 (let ((entry (assq 'groups config)))
151 (if (and entry
152 (setq entry (assoc (downcase group) (cdr entry))))
153 (cdr entry))))
155 (provide 'epg-config)
157 ;;; epg-config.el ends here