typo.
[emacs.git] / lisp / url / url-gw.el
blob608827d7ceecd284a7e22fd8b8daf25bdfbea4d0
1 ;;; url-gw.el --- Gateway munging for URL loading
2 ;; Author: Bill Perry <wmperry@gnu.org>
3 ;; Keywords: comm, data, processes
5 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
6 ;;; Copyright (c) 1997, 1998, 2004 Free Software Foundation, Inc.
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 the
22 ;;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;;; Boston, MA 02111-1307, USA.
24 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
25 (eval-when-compile (require 'cl))
26 (require 'url-vars)
28 ;; Fixme: support SSH explicitly or via a url-gateway-rlogin-program?
30 (autoload 'socks-open-network-stream "socks")
31 (autoload 'open-ssl-stream "ssl")
32 (autoload 'open-tls-stream "tls")
34 (defgroup url-gateway nil
35 "URL gateway variables"
36 :group 'url)
38 (defcustom url-gateway-local-host-regexp nil
39 "*A regular expression specifying local hostnames/machines."
40 :type '(choice (const nil) regexp)
41 :group 'url-gateway)
43 (defcustom url-gateway-prompt-pattern
44 "^[^#$%>;]*[#$%>;] *" ;; "bash\\|\$ *\r?$\\|> *\r?"
45 "*A regular expression matching a shell prompt."
46 :type 'regexp
47 :group 'url-gateway)
49 (defcustom url-gateway-rlogin-host nil
50 "*What hostname to actually rlog into before doing a telnet."
51 :type '(choice (const nil) string)
52 :group 'url-gateway)
54 (defcustom url-gateway-rlogin-user-name nil
55 "*Username to log into the remote machine with when using rlogin."
56 :type '(choice (const nil) string)
57 :group 'url-gateway)
59 (defcustom url-gateway-rlogin-parameters '("telnet" "-8")
60 "*Parameters to `url-open-rlogin'.
61 This list will be used as the parameter list given to rsh."
62 :type '(repeat string)
63 :group 'url-gateway)
65 (defcustom url-gateway-telnet-host nil
66 "*What hostname to actually login to before doing a telnet."
67 :type '(choice (const nil) string)
68 :group 'url-gateway)
70 (defcustom url-gateway-telnet-parameters '("exec" "telnet" "-8")
71 "*Parameters to `url-open-telnet'.
72 This list will be executed as a command after logging in via telnet."
73 :type '(repeat string)
74 :group 'url-gateway)
76 (defcustom url-gateway-telnet-login-prompt "^\r*.?login:"
77 "*Prompt that tells us we should send our username when loggin in w/telnet."
78 :type 'regexp
79 :group 'url-gateway)
81 (defcustom url-gateway-telnet-password-prompt "^\r*.?password:"
82 "*Prompt that tells us we should send our password when loggin in w/telnet."
83 :type 'regexp
84 :group 'url-gateway)
86 (defcustom url-gateway-telnet-user-name nil
87 "User name to log in via telnet with."
88 :type '(choice (const nil) string)
89 :group 'url-gateway)
91 (defcustom url-gateway-telnet-password nil
92 "Password to use to log in via telnet with."
93 :type '(choice (const nil) string)
94 :group 'url-gateway)
96 (defcustom url-gateway-broken-resolution nil
97 "*Whether to use nslookup to resolve hostnames.
98 This should be used when your version of Emacs cannot correctly use DNS,
99 but your machine can. This usually happens if you are running a statically
100 linked Emacs under SunOS 4.x"
101 :type 'boolean
102 :group 'url-gateway)
104 (defcustom url-gateway-nslookup-program "nslookup"
105 "*If non-NIL then a string naming nslookup program."
106 :type '(choice (const :tag "None" :value nil) string)
107 :group 'url-gateway)
109 ;; Stolen from ange-ftp
110 ;;;###autoload
111 (defun url-gateway-nslookup-host (host)
112 "Attempt to resolve the given HOST using nslookup if possible."
113 (interactive "sHost: ")
114 (if url-gateway-nslookup-program
115 (let ((proc (start-process " *nslookup*" " *nslookup*"
116 url-gateway-nslookup-program host))
117 (res host))
118 (process-kill-without-query proc)
119 (save-excursion
120 (set-buffer (process-buffer proc))
121 (while (memq (process-status proc) '(run open))
122 (accept-process-output proc))
123 (goto-char (point-min))
124 (if (re-search-forward "Name:.*\nAddress: *\\(.*\\)$" nil t)
125 (setq res (buffer-substring (match-beginning 1)
126 (match-end 1))))
127 (kill-buffer (current-buffer)))
128 res)
129 host))
131 ;; Stolen from red gnus nntp.el
132 (defun url-wait-for-string (regexp proc)
133 "Wait until string matching REGEXP arrives in process PROC's buffer."
134 (let ((buf (current-buffer)))
135 (goto-char (point-min))
136 (while (not (re-search-forward regexp nil t))
137 (accept-process-output proc)
138 (set-buffer buf)
139 (goto-char (point-min)))))
141 ;; Stolen from red gnus nntp.el
142 (defun url-open-rlogin (name buffer host service)
143 "Open a connection using rsh."
144 (if (not (stringp service))
145 (setq service (int-to-string service)))
146 (let ((proc (if url-gateway-rlogin-user-name
147 (start-process
148 name buffer "rsh"
149 url-gateway-rlogin-host "-l" url-gateway-rlogin-user-name
150 (mapconcat 'identity
151 (append url-gateway-rlogin-parameters
152 (list host service)) " "))
153 (start-process
154 name buffer "rsh" url-gateway-rlogin-host
155 (mapconcat 'identity
156 (append url-gateway-rlogin-parameters
157 (list host service))
158 " ")))))
159 (set-buffer buffer)
160 (url-wait-for-string "^\r*200" proc)
161 (beginning-of-line)
162 (delete-region (point-min) (point))
163 proc))
165 ;; Stolen from red gnus nntp.el
166 (defun url-open-telnet (name buffer host service)
167 (if (not (stringp service))
168 (setq service (int-to-string service)))
169 (save-excursion
170 (set-buffer (get-buffer-create buffer))
171 (erase-buffer)
172 (let ((proc (start-process name buffer "telnet" "-8"))
173 (case-fold-search t))
174 (when (memq (process-status proc) '(open run))
175 (process-send-string proc "set escape \^X\n")
176 (process-send-string proc (concat
177 "open " url-gateway-telnet-host "\n"))
178 (url-wait-for-string url-gateway-telnet-login-prompt proc)
179 (process-send-string
180 proc (concat
181 (or url-gateway-telnet-user-name
182 (setq url-gateway-telnet-user-name (read-string "login: ")))
183 "\n"))
184 (url-wait-for-string url-gateway-telnet-password-prompt proc)
185 (process-send-string
186 proc (concat
187 (or url-gateway-telnet-password
188 (setq url-gateway-telnet-password
189 (funcall url-passwd-entry-func "Password: ")))
190 "\n"))
191 (erase-buffer)
192 (url-wait-for-string url-gateway-prompt-pattern proc)
193 (process-send-string
194 proc (concat (mapconcat 'identity
195 (append url-gateway-telnet-parameters
196 (list host service)) " ") "\n"))
197 (url-wait-for-string "^\r*Escape character.*\r*\n+" proc)
198 (delete-region (point-min) (match-end 0))
199 (process-send-string proc "\^]\n")
200 (url-wait-for-string "^telnet" proc)
201 (process-send-string proc "mode character\n")
202 (accept-process-output proc 1)
203 (sit-for 1)
204 (goto-char (point-min))
205 (forward-line 1)
206 (delete-region (point) (point-max)))
207 proc)))
209 ;;;###autoload
210 (defun url-open-stream (name buffer host service)
211 "Open a stream to HOST, possibly via a gateway.
212 Args per `open-network-stream'.
213 Will not make a connexion if `url-gateway-unplugged' is non-nil."
214 (unless url-gateway-unplugged
215 (let ((gw-method (if (and url-gateway-local-host-regexp
216 (not (eq 'tls url-gateway-method))
217 (not (eq 'ssl url-gateway-method))
218 (string-match
219 url-gateway-local-host-regexp
220 host))
221 'native
222 url-gateway-method))
223 ;;; ;; This hack is for OS/2 Emacs so that it will not do bogus CRLF
224 ;;; ;; conversions while trying to be 'helpful'
225 ;;; (tcp-binary-process-output-services (if (stringp service)
226 ;;; (list service)
227 ;;; (list service
228 ;;; (int-to-string service))))
230 ;; An attempt to deal with denied connections, and attempt
231 ;; to reconnect
232 (cur-retries 0)
233 (retry t)
234 (errobj nil)
235 (conn nil))
237 ;; If the user told us to do DNS for them, do it.
238 (if url-gateway-broken-resolution
239 (setq host (url-gateway-nslookup-host host)))
241 (condition-case errobj
242 ;; This is a clean way to ensure the new process inherits the
243 ;; right coding systems in both Emacs and XEmacs.
244 (let ((coding-system-for-read 'binary)
245 (coding-system-for-write 'binary))
246 (setq conn (case gw-method
247 (tls
248 (open-tls-stream name buffer host service))
249 (ssl
250 (open-ssl-stream name buffer host service))
251 ((native)
252 (open-network-stream name buffer host service))
253 (socks
254 (socks-open-network-stream name buffer host service))
255 (telnet
256 (url-open-telnet name buffer host service))
257 (rlogin
258 (url-open-rlogin name buffer host service))
259 (otherwise
260 (error "Bad setting of url-gateway-method: %s"
261 url-gateway-method)))))
262 (error
263 (setq conn nil)))
264 conn)))
266 (provide 'url-gw)
268 ;;; arch-tag: 1c4c0317-6d03-45b8-b3f3-838bd8f9d838