entered into RCS
[emacs.git] / lisp / netunam.el
blobee755312f9227a7f1efc731e5ad416b73e98b6bb
1 ;;; netunam.el --- HP-UX RFA Commands
3 ;; Copyright (C) 1988 Free Software Foundation, Inc.
5 ;; Author: Chris Hanson <cph@zurich.ai.mit.edu>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs 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.
14 ;; GNU Emacs 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.
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
21 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23 ;;; Code:
25 (defconst rfa-node-directory "/net/"
26 "Directory in which RFA network special files are stored.
27 By HP convention, this is \"/net/\".")
29 (defvar rfa-default-node nil
30 "If not nil, this is the name of the default RFA network special file.")
32 (defvar rfa-password-memoize-p t
33 "If non-nil, remember login user's passwords after they have been entered.")
35 (defvar rfa-password-alist '()
36 "An association from node-name strings to password strings.
37 Used if `rfa-password-memoize-p' is non-nil.")
39 (defvar rfa-password-per-node-p t
40 "If nil, login user uses same password on all machines.
41 Has no effect if `rfa-password-memoize-p' is nil.")
43 (defun rfa-set-password (password &optional node user)
44 "Add PASSWORD to the RFA password database.
45 Optional second arg NODE is a string specifying a particular nodename;
46 if supplied and not nil, PASSWORD applies to only that node.
47 Optional third arg USER is a string specifying the (remote) user whose
48 password this is; if not supplied this defaults to (user-login-name)."
49 (if (not user) (setq user (user-login-name)))
50 (let ((node-entry (assoc node rfa-password-alist)))
51 (if node-entry
52 (let ((user-entry (assoc user (cdr node-entry))))
53 (if user-entry
54 (rplacd user-entry password)
55 (rplacd node-entry
56 (nconc (cdr node-entry)
57 (list (cons user password))))))
58 (setq rfa-password-alist
59 (nconc rfa-password-alist
60 (list (list node (cons user password))))))))
62 (defun rfa-open (node &optional user password)
63 "Open a network connection to a server using remote file access.
64 First argument NODE is the network node for the remote machine.
65 Second optional argument USER is the user name to use on that machine.
66 If called interactively, the user name is prompted for.
67 Third optional argument PASSWORD is the password string for that user.
68 If not given, this is filled in from the value of
69 `rfa-password-alist', or prompted for. A prefix argument of - will
70 cause the password to be prompted for even if previously memoized."
71 (interactive
72 (list (read-file-name "rfa-open: " rfa-node-directory rfa-default-node t)
73 (read-string "user-name: " (user-login-name))))
74 (let ((node
75 (and (or rfa-password-per-node-p
76 (not (equal user (user-login-name))))
77 node)))
78 (if (not password)
79 (setq password
80 (let ((password
81 (cdr (assoc user (cdr (assoc node rfa-password-alist))))))
82 (or (and (not current-prefix-arg) password)
83 (rfa-password-read
84 (format "password for user %s%s: "
85 user
86 (if node (format " on node \"%s\"" node) ""))
87 password))))))
88 (let ((result
89 (sysnetunam (expand-file-name node rfa-node-directory)
90 (concat user ":" password))))
91 (if (interactive-p)
92 (if result
93 (message "Opened network connection to %s as %s" node user)
94 (error "Unable to open network connection")))
95 (if (and rfa-password-memoize-p result)
96 (rfa-set-password password node user))
97 result))
99 (defun rfa-close (node)
100 "Close a network connection to a server using remote file access.
101 NODE is the network node for the remote machine."
102 (interactive
103 (list (read-file-name "rfa-close: " rfa-node-directory rfa-default-node t)))
104 (let ((result (sysnetunam (expand-file-name node rfa-node-directory) "")))
105 (cond ((not (interactive-p)) result)
106 ((not result) (error "Unable to close network connection"))
107 (t (message "Closed network connection to %s" node)))))
109 (defun rfa-password-read (prompt default)
110 (let ((rfa-password-accumulator (or default "")))
111 (read-from-minibuffer prompt
112 (and default
113 (let ((copy (concat default))
114 (index 0)
115 (length (length default)))
116 (while (< index length)
117 (aset copy index ?.)
118 (setq index (1+ index)))
119 copy))
120 rfa-password-map)
121 rfa-password-accumulator))
123 (defvar rfa-password-map nil)
124 (if (not rfa-password-map)
125 (let ((char ? ))
126 (setq rfa-password-map (make-keymap))
127 (while (< char 127)
128 (define-key rfa-password-map (char-to-string char)
129 'rfa-password-self-insert)
130 (setq char (1+ char)))
131 (define-key rfa-password-map "\C-g"
132 'abort-recursive-edit)
133 (define-key rfa-password-map "\177"
134 'rfa-password-rubout)
135 (define-key rfa-password-map "\n"
136 'exit-minibuffer)
137 (define-key rfa-password-map "\r"
138 'exit-minibuffer)))
140 (defvar rfa-password-accumulator nil)
142 (defun rfa-password-self-insert ()
143 (interactive)
144 (setq rfa-password-accumulator
145 (concat rfa-password-accumulator
146 (char-to-string last-command-char)))
147 (insert ?.))
149 (defun rfa-password-rubout ()
150 (interactive)
151 (delete-char -1)
152 (setq rfa-password-accumulator
153 (substring rfa-password-accumulator 0 -1)))
155 ;;; netunam.el ends here