; * etc/NEWS: Use double spaces to end a sentence.
[emacs.git] / lisp / epg-config.el
blobc41d97dbfacbd1d8c9ee98de17060be1f59e373b
1 ;;; epg-config.el --- configuration of the EasyPG Library
3 ;; Copyright (C) 2006-2016 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 (if (executable-find "gpg2")
43 "gpg2"
44 "gpg")
45 "The `gpg' executable."
46 :version "25.1"
47 :group 'epg
48 :type 'string)
50 (defcustom epg-gpgsm-program "gpgsm"
51 "The `gpgsm' executable."
52 :group 'epg
53 :type 'string)
55 (defcustom epg-gpgconf-program "gpgconf"
56 "The `gpgconf' executable."
57 :version "25.1"
58 :group 'epg
59 :type 'string)
61 (defcustom epg-gpg-home-directory nil
62 "The directory which contains the configuration files of `epg-gpg-program'."
63 :group 'epg
64 :type '(choice (const :tag "Default" nil) directory))
66 (defcustom epg-passphrase-coding-system nil
67 "Coding system to use with messages from `epg-gpg-program'."
68 :group 'epg
69 :type 'symbol)
71 (defcustom epg-debug nil
72 "If non-nil, debug output goes to the \" *epg-debug*\" buffer.
73 Note that the buffer name starts with a space."
74 :group 'epg
75 :type 'boolean)
77 (defconst epg-gpg-minimum-version "1.4.3")
79 ;;;###autoload
80 (defun epg-configuration ()
81 "Return a list of internal configuration parameters of `epg-gpg-program'."
82 (let (config groups type args)
83 (with-temp-buffer
84 (apply #'call-process epg-gpg-program nil (list t nil) nil
85 (append (if epg-gpg-home-directory
86 (list "--homedir" epg-gpg-home-directory))
87 '("--with-colons" "--list-config")))
88 (goto-char (point-min))
89 (while (re-search-forward "^cfg:\\([^:]+\\):\\(.*\\)" nil t)
90 (setq type (intern (match-string 1))
91 args (match-string 2))
92 (cond
93 ((eq type 'group)
94 (if (string-match "\\`\\([^:]+\\):" args)
95 (setq groups
96 (cons (cons (downcase (match-string 1 args))
97 (delete "" (split-string
98 (substring args
99 (match-end 0))
100 ";")))
101 groups))
102 (if epg-debug
103 (message "Invalid group configuration: %S" args))))
104 ((memq type '(pubkey cipher digest compress))
105 (if (string-match "\\`\\([0-9]+\\)\\(;[0-9]+\\)*" args)
106 (setq config
107 (cons (cons type
108 (mapcar #'string-to-number
109 (delete "" (split-string args ";"))))
110 config))
111 (if epg-debug
112 (message "Invalid %S algorithm configuration: %S"
113 type args))))
115 (setq config (cons (cons type args) config))))))
116 (if groups
117 (cons (cons 'groups groups) config)
118 config)))
120 (defun epg-config--parse-version (string)
121 (let ((index 0)
122 version)
123 (while (eq index (string-match "\\([0-9]+\\)\\.?" string index))
124 (setq version (cons (string-to-number (match-string 1 string))
125 version)
126 index (match-end 0)))
127 (nreverse version)))
129 (defun epg-config--compare-version (v1 v2)
130 (while (and v1 v2 (= (car v1) (car v2)))
131 (setq v1 (cdr v1) v2 (cdr v2)))
132 (- (or (car v1) 0) (or (car v2) 0)))
134 ;;;###autoload
135 (defun epg-check-configuration (config &optional minimum-version)
136 "Verify that a sufficient version of GnuPG is installed."
137 (let ((entry (assq 'version config))
138 version)
139 (unless (and entry
140 (stringp (cdr entry)))
141 (error "Undetermined version: %S" entry))
142 (setq version (epg-config--parse-version (cdr entry))
143 minimum-version (epg-config--parse-version
144 (or minimum-version
145 epg-gpg-minimum-version)))
146 (unless (>= (epg-config--compare-version version minimum-version) 0)
147 (error "Unsupported version: %s" (cdr entry)))))
149 ;;;###autoload
150 (defun epg-expand-group (config group)
151 "Look at CONFIG and try to expand GROUP."
152 (let ((entry (assq 'groups config)))
153 (if (and entry
154 (setq entry (assoc (downcase group) (cdr entry))))
155 (cdr entry))))
157 (provide 'epg-config)
159 ;;; epg-config.el ends here