epg-config.el 4.5 KB

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