Import No Gnus v0.4.
[elisp/gnus.git-] / lisp / password.el
1 ;;; password.el --- Read passwords from user, possibly using a password cache.
2
3 ;; Copyright (C) 1999, 2000, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
4
5 ;; Author: Simon Josefsson <simon@josefsson.org>
6 ;; Created: 2003-12-21
7 ;; Keywords: password cache passphrase key
8
9 ;; This file is part of GNU Emacs.
10
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 2, or (at your option)
14 ;; any later version.
15
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.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
25
26 ;;; Commentary:
27
28 ;; Greatly influenced by pgg.el written by Daiki Ueno, with timer
29 ;; fixes for XEmacs by Katsumi Yamaoka.  In fact, this is mostly just
30 ;; a rip-off.
31 ;;
32 ;; (password-read "Password? " "test")
33 ;; ;; Minibuffer prompt for password.
34 ;;  => "foo"
35 ;;
36 ;; (password-cache-add "test" "foo")
37 ;;  => nil
38
39 ;; Note the previous two can be replaced with:
40 ;; (password-read-and-add "Password? " "test")
41 ;; ;; Minibuffer prompt for password.
42 ;; => "foo"
43 ;; ;; "foo" is now cached with key "test"
44
45
46 ;; (password-read "Password? " "test")
47 ;; ;; No minibuffer prompt
48 ;;  => "foo"
49 ;;
50 ;; (password-read "Password? " "test")
51 ;; ;; No minibuffer prompt
52 ;;  => "foo"
53 ;;
54 ;; ;; Wait `password-cache-expiry' seconds.
55 ;;
56 ;; (password-read "Password? " "test")
57 ;; ;; Minibuffer prompt for password is back.
58 ;;  => "foo"
59
60 ;;; Code:
61
62 (when (featurep 'xemacs)
63   (require 'timer-funcs))
64
65 (eval-when-compile
66   (require 'cl))
67
68 (defcustom password-cache t
69   "Whether to cache passwords."
70   :group 'password
71   :type 'boolean)
72
73 (defcustom password-cache-expiry 16
74   "How many seconds passwords are cached, or nil to disable expiring.
75 Whether passwords are cached at all is controlled by `password-cache'."
76   :group 'password
77   :type '(choice (const :tag "Never" nil)
78                  (integer :tag "Seconds")))
79
80 (defvar password-data (make-vector 7 0))
81
82 (defun password-read-from-cache (key)
83   "Obtain passphrase for KEY from time-limited passphrase cache.
84 Custom variables `password-cache' and `password-cache-expiry'
85 regulate cache behavior."
86   (and password-cache
87        key
88        (symbol-value (intern-soft key password-data))))
89
90 (defun password-read (prompt &optional key)
91   "Read password, for use with KEY, from user, or from cache if wanted.
92 KEY indicate the purpose of the password, so the cache can
93 separate passwords.  The cache is not used if KEY is nil.  It is
94 typically a string.
95 The variable `password-cache' control whether the cache is used."
96   (or (password-read-from-cache key)
97       (read-passwd prompt)))
98
99 (defun password-read-and-add (prompt &optional key)
100   "Read password, for use with KEY, from user, or from cache if wanted.
101 Then store the password in the cache.  Uses `password-read' and
102 `password-cache-add'.
103 Custom variables `password-cache' and `password-cache-expiry'
104 regulate cache behavior."
105   (let ((password (password-read prompt key)))
106     (when (and password key)
107       (password-cache-add key password))
108     password))
109
110 (defun password-cache-remove (key)
111   "Remove password indexed by KEY from password cache.
112 This is typically run be a timer setup from `password-cache-add',
113 but can be invoked at any time to forcefully remove passwords
114 from the cache.  This may be useful when it has been detected
115 that a password is invalid, so that `password-read' query the
116 user again."
117   (let ((password (symbol-value (intern-soft key password-data))))
118     (when password
119       (fillarray password ?_)
120       (unintern key password-data))))
121
122 (defun password-cache-add (key password)
123   "Add password to cache.
124 The password is removed by a timer after `password-cache-expiry'
125 seconds."
126   (when (and password-cache-expiry (null (intern-soft key password-data)))
127     (run-at-time password-cache-expiry nil
128                  #'password-cache-remove
129                  key))
130   (set (intern key password-data) password)
131   nil)
132
133 (defun password-reset ()
134   "Clear the password cache."
135   (interactive)
136   (fillarray password-data 0))
137
138 (provide 'password)
139
140 ;;; arch-tag: ab160494-16c8-4c68-a4a1-73eebf6686e5
141 ;;; password.el ends here