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 Free Software Foundation, Inc.
8 ;;; This file is part of GNU Emacs.
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.
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.
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
))
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")
33 (defgroup url-gateway nil
34 "URL gateway variables"
37 (defcustom url-gateway-local-host-regexp nil
38 "*A regular expression specifying local hostnames/machines."
39 :type
'(choice (const nil
) regexp
)
42 (defcustom url-gateway-prompt-pattern
43 "^[^#$%>;]*[#$%>;] *" ;; "bash\\|\$ *\r?$\\|> *\r?"
44 "*A regular expression matching a shell prompt."
48 (defcustom url-gateway-rlogin-host nil
49 "*What hostname to actually rlog into before doing a telnet."
50 :type
'(choice (const nil
) string
)
53 (defcustom url-gateway-rlogin-user-name nil
54 "*Username to log into the remote machine with when using rlogin."
55 :type
'(choice (const nil
) string
)
58 (defcustom url-gateway-rlogin-parameters
'("telnet" "-8")
59 "*Parameters to `url-open-rlogin'.
60 This list will be used as the parameter list given to rsh."
61 :type
'(repeat string
)
64 (defcustom url-gateway-telnet-host nil
65 "*What hostname to actually login to before doing a telnet."
66 :type
'(choice (const nil
) string
)
69 (defcustom url-gateway-telnet-parameters
'("exec" "telnet" "-8")
70 "*Parameters to `url-open-telnet'.
71 This list will be executed as a command after logging in via telnet."
72 :type
'(repeat string
)
75 (defcustom url-gateway-telnet-login-prompt
"^\r*.?login:"
76 "*Prompt that tells us we should send our username when loggin in w/telnet."
80 (defcustom url-gateway-telnet-password-prompt
"^\r*.?password:"
81 "*Prompt that tells us we should send our password when loggin in w/telnet."
85 (defcustom url-gateway-telnet-user-name nil
86 "User name to log in via telnet with."
87 :type
'(choice (const nil
) string
)
90 (defcustom url-gateway-telnet-password nil
91 "Password to use to log in via telnet with."
92 :type
'(choice (const nil
) string
)
95 (defcustom url-gateway-broken-resolution nil
96 "*Whether to use nslookup to resolve hostnames.
97 This should be used when your version of Emacs cannot correctly use DNS,
98 but your machine can. This usually happens if you are running a statically
99 linked Emacs under SunOS 4.x"
103 (defcustom url-gateway-nslookup-program
"nslookup"
104 "*If non-NIL then a string naming nslookup program."
105 :type
'(choice (const :tag
"None" :value nil
) string
)
108 ;; Stolen from ange-ftp
110 (defun url-gateway-nslookup-host (host)
111 "Attempt to resolve the given HOST using nslookup if possible."
112 (interactive "sHost: ")
113 (if url-gateway-nslookup-program
114 (let ((proc (start-process " *nslookup*" " *nslookup*"
115 url-gateway-nslookup-program host
))
117 (process-kill-without-query proc
)
119 (set-buffer (process-buffer proc
))
120 (while (memq (process-status proc
) '(run open
))
121 (accept-process-output proc
))
122 (goto-char (point-min))
123 (if (re-search-forward "Name:.*\nAddress: *\\(.*\\)$" nil t
)
124 (setq res
(buffer-substring (match-beginning 1)
126 (kill-buffer (current-buffer)))
130 ;; Stolen from red gnus nntp.el
131 (defun url-wait-for-string (regexp proc
)
132 "Wait until string matching REGEXP arrives in process PROC's buffer."
133 (let ((buf (current-buffer)))
134 (goto-char (point-min))
135 (while (not (re-search-forward regexp nil t
))
136 (accept-process-output proc
)
138 (goto-char (point-min)))))
140 ;; Stolen from red gnus nntp.el
141 (defun url-open-rlogin (name buffer host service
)
142 "Open a connection using rsh."
143 (if (not (stringp service
))
144 (setq service
(int-to-string service
)))
145 (let ((proc (if url-gateway-rlogin-user-name
148 url-gateway-rlogin-host
"-l" url-gateway-rlogin-user-name
150 (append url-gateway-rlogin-parameters
151 (list host service
)) " "))
153 name buffer
"rsh" url-gateway-rlogin-host
155 (append url-gateway-rlogin-parameters
159 (url-wait-for-string "^\r*200" proc
)
161 (delete-region (point-min) (point))
164 ;; Stolen from red gnus nntp.el
165 (defun url-open-telnet (name buffer host service
)
166 (if (not (stringp service
))
167 (setq service
(int-to-string service
)))
169 (set-buffer (get-buffer-create buffer
))
171 (let ((proc (start-process name buffer
"telnet" "-8"))
172 (case-fold-search t
))
173 (when (memq (process-status proc
) '(open run
))
174 (process-send-string proc
"set escape \^X\n")
175 (process-send-string proc
(concat
176 "open " url-gateway-telnet-host
"\n"))
177 (url-wait-for-string url-gateway-telnet-login-prompt proc
)
180 (or url-gateway-telnet-user-name
181 (setq url-gateway-telnet-user-name
(read-string "login: ")))
183 (url-wait-for-string url-gateway-telnet-password-prompt proc
)
186 (or url-gateway-telnet-password
187 (setq url-gateway-telnet-password
188 (funcall url-passwd-entry-func
"Password: ")))
191 (url-wait-for-string url-gateway-prompt-pattern proc
)
193 proc
(concat (mapconcat 'identity
194 (append url-gateway-telnet-parameters
195 (list host service
)) " ") "\n"))
196 (url-wait-for-string "^\r*Escape character.*\r*\n+" proc
)
197 (delete-region (point-min) (match-end 0))
198 (process-send-string proc
"\^]\n")
199 (url-wait-for-string "^telnet" proc
)
200 (process-send-string proc
"mode character\n")
201 (accept-process-output proc
1)
203 (goto-char (point-min))
205 (delete-region (point) (point-max)))
209 (defun url-open-stream (name buffer host service
)
210 "Open a stream to HOST, possibly via a gateway.
211 Args per `open-network-stream'.
212 Will not make a connexion if `url-gateway-unplugged' is non-nil."
213 (unless url-gateway-unplugged
214 (let ((gw-method (if (and url-gateway-local-host-regexp
215 (not (eq 'ssl url-gateway-method
))
217 url-gateway-local-host-regexp
221 ;;; ;; This hack is for OS/2 Emacs so that it will not do bogus CRLF
222 ;;; ;; conversions while trying to be 'helpful'
223 ;;; (tcp-binary-process-output-services (if (stringp service)
226 ;;; (int-to-string service))))
228 ;; An attempt to deal with denied connections, and attempt
235 ;; If the user told us to do DNS for them, do it.
236 (if url-gateway-broken-resolution
237 (setq host
(url-gateway-nslookup-host host
)))
239 (condition-case errobj
240 ;; This is a clean way to ensure the new process inherits the
241 ;; right coding systems in both Emacs and XEmacs.
242 (let ((coding-system-for-read 'binary
)
243 (coding-system-for-write 'binary
))
244 (setq conn
(case gw-method
246 (open-ssl-stream name buffer host service
))
248 (open-network-stream name buffer host service
))
250 (socks-open-network-stream name buffer host service
))
252 (url-open-telnet name buffer host service
))
254 (url-open-rlogin name buffer host service
))
256 (error "Bad setting of url-gateway-method: %s"
257 url-gateway-method
)))))
264 ;;; arch-tag: 1c4c0317-6d03-45b8-b3f3-838bd8f9d838