(rmail-last-rmail-file): Initialize to a file name.
[bpt/emacs.git] / lisp / telnet.el
1 ;;; telnet.el --- run a telnet session from within an Emacs buffer
2
3 ;;; Copyright (C) 1985, 1988, 1992 Free Software Foundation, Inc.
4
5 ;; Author: William F. Schelter
6 ;; Maintainer: FSF
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23
24 ;;; Commentary:
25
26 ;; This mode is intended to be used for telnet or rsh to a remode host;
27 ;; `telnet' and `rsh' are the two entry points. Multiple telnet or rsh
28 ;; sessions are supported.
29 ;;
30 ;; Normally, input is sent to the remote telnet/rsh line-by-line, as you
31 ;; type RET or LFD. C-c C-c sends a C-c to the remote immediately;
32 ;; C-c C-z sends C-z immediately. C-c C-q followed by any character
33 ;; sends that character immediately.
34 ;;
35 ;; All RET characters are filtered out of the output coming back from the
36 ;; remote system. The mode tries to do other useful translations based
37 ;; on what it sees coming back from the other system before the password
38 ;; query. It knows about UNIX, ITS, TOPS-20 and Explorer systems.
39
40 ;;; Code:
41
42 ;; to do fix software types for lispm:
43 ;; to eval current expression. Also to try to send escape keys correctly.
44 ;; essentially we'll want the rubout-handler off.
45
46 ;; filter is simplistic but should be okay for typical shell usage.
47 ;; needs hacking if it is going to deal with asynchronous output in a sane
48 ;; manner
49
50 (require 'comint)
51
52 (defvar telnet-new-line "\r")
53 (defvar telnet-mode-map nil)
54 (defvar telnet-prompt-pattern "^[^#$%>]*[#$%>] *")
55 (defvar telnet-replace-c-g nil)
56 (make-variable-buffer-local
57 (defvar telnet-remote-echoes t
58 "True if the telnet process will echo input."))
59 (make-variable-buffer-local
60 (defvar telnet-interrupt-string "\C-c" "String sent by C-c."))
61
62 (defvar telnet-count 0
63 "Number of output strings read from the telnet process
64 while looking for the initial password.")
65
66 (defvar telnet-initial-count -50
67 "Initial value of `telnet-count'. Should be set to the negative of the
68 number of terminal writes telnet will make setting up the host connection.")
69
70 (defvar telnet-maximum-count 4
71 "Maximum value `telnet-count' can have.
72 After this many passes, we stop looking for initial setup data.
73 Should be set to the number of terminal writes telnet will make
74 rejecting one login and prompting for the again for a username and password.")
75
76 (defun telnet-interrupt-subjob ()
77 (interactive)
78 "Interrupt the program running through telnet on the remote host."
79 (send-string nil telnet-interrupt-string))
80
81 (defun telnet-c-z ()
82 (interactive)
83 (send-string nil "\C-z"))
84
85 (defun send-process-next-char ()
86 (interactive)
87 (send-string nil
88 (char-to-string
89 (let ((inhibit-quit t))
90 (prog1 (read-char)
91 (setq quit-flag nil))))))
92
93 ; initialization on first load.
94 (if telnet-mode-map
95 nil
96 (setq telnet-mode-map (copy-keymap comint-mode-map))
97 (define-key telnet-mode-map "\C-m" 'telnet-send-input)
98 ; (define-key telnet-mode-map "\C-j" 'telnet-send-input)
99 (define-key telnet-mode-map "\C-c\C-q" 'send-process-next-char)
100 (define-key telnet-mode-map "\C-c\C-c" 'telnet-interrupt-subjob)
101 (define-key telnet-mode-map "\C-c\C-z" 'telnet-c-z))
102
103 ;;maybe should have a flag for when have found type
104 (defun telnet-check-software-type-initialize (string)
105 "Tries to put correct initializations in. Needs work."
106 (let ((case-fold-search t))
107 (cond ((string-match "unix" string)
108 (setq telnet-prompt-pattern comint-prompt-regexp)
109 (setq telnet-new-line "\n"))
110 ((string-match "tops-20" string) ;;maybe add telnet-replace-c-g
111 (setq telnet-prompt-pattern "[@>]*"))
112 ((string-match "its" string)
113 (setq telnet-prompt-pattern "^[^*>]*[*>] *"))
114 ((string-match "explorer" string) ;;explorer telnet needs work
115 (setq telnet-replace-c-g ?\n))))
116 (setq comint-prompt-regexp telnet-prompt-pattern))
117
118 (defun telnet-initial-filter (proc string)
119 ;For reading up to and including password; also will get machine type.
120 (cond ((string-match "No such host" string)
121 (kill-buffer (process-buffer proc))
122 (error "No such host."))
123 ((string-match "passw" string)
124 (telnet-filter proc string)
125 (let* ((echo-keystrokes 0)
126 (password (read-password)))
127 (setq telnet-count 0)
128 (send-string proc (concat password telnet-new-line))))
129 (t (telnet-check-software-type-initialize string)
130 (telnet-filter proc string)
131 (cond ((> telnet-count telnet-maximum-count)
132 (set-process-filter proc 'telnet-filter))
133 (t (setq telnet-count (1+ telnet-count)))))))
134
135 ;; Identical to comint-simple-send, except that it sends telnet-new-line
136 ;; instead of "\n".
137 (defun telnet-simple-send (proc string)
138 (comint-send-string proc string)
139 (comint-send-string proc telnet-new-line))
140
141 (defun telnet-filter (proc string)
142 (let ((at-end
143 (and (eq (process-buffer proc) (current-buffer))
144 (= (point) (point-max)))))
145 (save-excursion
146 (set-buffer (process-buffer proc))
147 (goto-char (process-mark proc))
148 (let ((now (point)))
149 ;; Insert STRING, omitting all C-m characters.
150 (let ((index 0) c-m)
151 (while (setq c-m (string-match "\C-m" string index))
152 (insert-before-markers (substring string index c-m))
153 (setq index (1+ c-m)))
154 (insert-before-markers (substring string index)))
155 (and telnet-replace-c-g
156 (subst-char-in-region now (point) ?\C-g telnet-replace-c-g)))
157 ; (if (and (integer-or-marker-p last-input-start)
158 ; (marker-position last-input-start)
159 ; telnet-remote-echoes)
160 ; (delete-region last-input-start last-input-end))
161 )
162 (if at-end
163 (goto-char (point-max)))))
164
165 (defun telnet-send-input ()
166 (interactive)
167 ; (comint-send-input telnet-new-line telnet-remote-echoes)
168 (comint-send-input)
169 (if telnet-remote-echoes
170 (delete-region comint-last-input-start
171 comint-last-input-end)))
172
173 ;;;###autoload
174 (defun telnet (arg)
175 "Open a network login connection to host named HOST (a string).
176 Communication with HOST is recorded in a buffer *HOST-telnet*.
177 Normally input is edited in Emacs and sent a line at a time."
178 (interactive "sOpen telnet connection to host: ")
179 (let ((name (concat arg "-telnet" )))
180 (switch-to-buffer (make-comint name "telnet"))
181 (set-process-filter (get-process name) 'telnet-initial-filter)
182 ;; Don't send the `open' cmd till telnet is ready for it.
183 (accept-process-output (get-process name))
184 (erase-buffer)
185 (send-string name (concat "open " arg "\n"))
186 (telnet-mode)
187 (setq comint-input-sender 'telnet-simple-send)
188 (setq telnet-count telnet-initial-count)))
189
190 (defun telnet-mode ()
191 "This mode is for using telnet (or rsh) from a buffer to another host.
192 It has most of the same commands as comint-mode.
193 There is a variable ``telnet-interrupt-string'' which is the character
194 sent to try to stop execution of a job on the remote host.
195 Data is sent to the remote host when RET is typed.
196
197 \\{telnet-mode-map}
198 "
199 (interactive)
200 (comint-mode)
201 (setq major-mode 'telnet-mode
202 mode-name "Telnet"
203 comint-prompt-regexp telnet-prompt-pattern)
204 (use-local-map telnet-mode-map)
205 (run-hooks 'telnet-mode-hook))
206
207 ;;;###autoload
208 (defun rsh (arg)
209 "Open a network login connection to host named HOST (a string).
210 Communication with HOST is recorded in a buffer *HOST-rsh*.
211 Normally input is edited in Emacs and sent a line at a time."
212 (interactive "sOpen rsh connection to host: ")
213 (require 'shell)
214 (let ((name (concat arg "-rsh" )))
215 (switch-to-buffer (make-comint name "rsh"))
216 (set-process-filter (get-process name) 'telnet-initial-filter)
217 (telnet-mode)
218 (setq telnet-count -16)))
219
220 (defun read-password ()
221 (let ((answ "") tem)
222 (message "Reading password...")
223 (while (prog1 (not (memq (setq tem (read-char)) '(?\C-m ?\n ?\C-g)))
224 (setq quit-flag nil))
225 (setq answ (concat answ (char-to-string tem))))
226 answ))
227
228 (provide 'telnet)
229
230 ;;; telnet.el ends here