Add arch taglines
[gnus] / lisp / password.el
1 ;;; password.el --- Read passwords from user, possibly using a password cache.
2
3 ;; Copyright (C) 1999, 2000, 2003, 2004 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., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, 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 ;; (password-read "Password? " "test")
40 ;; ;; No minibuffer prompt
41 ;;  => "foo"
42 ;;
43 ;; (password-read "Password? " "test")
44 ;; ;; No minibuffer prompt
45 ;;  => "foo"
46 ;;
47 ;; ;; Wait `password-cache-expiry' seconds.
48 ;;
49 ;; (password-read "Password? " "test")
50 ;; ;; Minibuffer prompt for password is back.
51 ;;  => "foo"
52
53 ;;; Code:
54
55 (when (featurep 'xemacs)
56   (require 'run-at-time))
57
58 (eval-when-compile
59   (require 'cl))
60
61 (defcustom password-cache t
62   "Whether to cache passwords."
63   :group 'password
64   :type 'boolean)
65
66 (defcustom password-cache-expiry 16
67   "How many seconds passwords are cached, or nil to disable expiring.
68 Whether passwords are cached at all is controlled by `password-cache'."
69   :group 'password
70   :type '(choice (const :tag "Never" nil)
71                  (integer :tag "Seconds")))
72
73 (defvar password-data (make-vector 7 0))
74
75 (defun password-read (prompt &optional key)
76   "Read password, for use with KEY, from user, or from cache if wanted.
77 KEY indicate the purpose of the password, so the cache can
78 separate passwords.  The cache is not used if KEY is nil.  It is
79 typically a string.
80 The variable `password-cache' control whether the cache is used."
81   (or (and password-cache
82            key
83            (symbol-value (intern-soft key password-data)))
84       (read-passwd prompt)))
85
86 (defun password-cache-remove (key)
87   "Remove password indexed by KEY from password cache.
88 This is typically run be a timer setup from `password-cache-add',
89 but can be invoked at any time to forcefully remove passwords
90 from the cache.  This may be useful when it has been detected
91 that a password is invalid, so that `password-read' query the
92 user again."
93   (let ((password (symbol-value (intern-soft key password-data))))
94     (when password
95       (fillarray password ?_)
96       (unintern key password-data))))
97
98 (defun password-cache-add (key password)
99   "Add password to cache.
100 The password is removed by a timer after `password-cache-expiry'
101 seconds."
102   (set (intern key password-data) password)
103   (when password-cache-expiry
104     (run-at-time password-cache-expiry nil
105                  #'password-cache-remove
106                  key))
107   nil)
108
109 (provide 'password)
110
111 ;;; arch-tag: ab160494-16c8-4c68-a4a1-73eebf6686e5
112 ;;; password.el ends here