Initial Commit
[packages] / xemacs-packages / easypg / 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 (require 'epg-package-info)
27
28 (defgroup epg ()
29   "The EasyPG Library"
30   :group 'emacs)
31
32 (defcustom epg-gpg-program "gpg"
33   "The `gpg' executable."
34   :group 'epg
35   :type 'string)
36
37 (defcustom epg-gpgsm-program "gpgsm"
38   "The `gpgsm' executable."
39   :group 'epg
40   :type 'string)
41
42 (defcustom epg-gpg-home-directory nil
43   "The directory which contains the configuration files of `epg-gpg-program'."
44   :group 'epg
45   :type '(choice (const :tag "Default" nil) directory))
46
47 (defcustom epg-passphrase-coding-system nil
48   "Coding system to use with messages from `epg-gpg-program'."
49   :group 'epg
50   :type 'symbol)
51
52 (defcustom epg-debug nil
53   "If non-nil, debug output goes to the \" *epg-debug*\" buffer.
54 Note that the buffer name starts with a space."
55   :group 'epg
56   :type 'boolean)
57
58 (defconst epg-gpg-minimum-version "1.4.3")
59
60 ;;;###autoload
61 (defun epg-configuration ()
62   "Return a list of internal configuration parameters of `epg-gpg-program'."
63   (let (config groups type args)
64     (with-temp-buffer
65       (apply #'call-process epg-gpg-program nil (list t nil) nil
66              (append (if epg-gpg-home-directory
67                          (list "--homedir" epg-gpg-home-directory))
68                      '("--with-colons" "--list-config")))
69       (goto-char (point-min))
70       (while (re-search-forward "^cfg:\\([^:]+\\):\\(.*\\)" nil t)
71         (setq type (intern (match-string 1))
72               args (match-string 2))
73         (cond
74          ((eq type 'group)
75           (if (string-match "\\`\\([^:]+\\):" args)
76                   (setq groups
77                         (cons (cons (downcase (match-string 1 args))
78                                     (delete "" (split-string
79                                                 (substring args
80                                                            (match-end 0))
81                                                 ";")))
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          (t
96           (setq config (cons (cons type args) config))))))
97     (if groups
98         (cons (cons 'groups groups) config)
99       config)))
100
101 (defun epg-config--parse-version (string)
102   (let ((index 0)
103         version)
104     (while (eq index (string-match "\\([0-9]+\\)\\.?" string index))
105       (setq version (cons (string-to-number (match-string 1 string))
106                           version)
107             index (match-end 0)))
108     (nreverse version)))
109
110 (defun epg-config--compare-version (v1 v2)
111   (while (and v1 v2 (= (car v1) (car v2)))
112     (setq v1 (cdr v1) v2 (cdr v2)))
113   (- (or (car v1) 0) (or (car v2) 0)))
114
115 ;;;###autoload
116 (defun epg-check-configuration (config &optional minimum-version)
117   "Verify that a sufficient version of GnuPG is installed."
118   (let ((entry (assq 'version config))
119         version)
120     (unless (and entry
121                  (stringp (cdr entry)))
122       (error "Undetermined version: %S" entry))
123     (setq version (epg-config--parse-version (cdr entry))
124           minimum-version (epg-config--parse-version
125                            (or minimum-version
126                                epg-gpg-minimum-version)))
127     (unless (>= (epg-config--compare-version version minimum-version) 0)
128       (error "Unsupported version: %s" (cdr entry)))))
129
130 ;;;###autoload
131 (defun epg-expand-group (config group)
132   "Look at CONFIG and try to expand GROUP."
133   (let ((entry (assq 'groups config)))
134     (if (and entry
135              (setq entry (assoc (downcase group) (cdr entry))))
136         (cdr entry))))
137
138 (provide 'epg-config)
139
140 ;;; epg-config.el ends here