1 ;;; url-gw.el --- Gateway munging for URL loading
2 ;; Author: Bill Perry <wmperry@gnu.org>
3 ;; Created: $Date: 2002/04/22 09:26:46 $
5 ;; Keywords: comm, data, processes
7 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
8 ;;; Copyright (c) 1997, 1998 Free Software Foundation, Inc.
10 ;;; This file is part of GNU Emacs.
12 ;;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;;; it under the terms of the GNU General Public License as published by
14 ;;; the Free Software Foundation; either version 2, or (at your option)
15 ;;; any later version.
17 ;;; GNU Emacs is distributed in the hope that it will be useful,
18 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;;; GNU General Public License for more details.
22 ;;; You should have received a copy of the GNU General Public License
23 ;;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;;; Boston, MA 02111-1307, USA.
26 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
27 (eval-when-compile (require 'cl
))
30 ;; Fixme: support SSH explicitly or via a url-gateway-rlogin-program?
32 (autoload 'socks-open-network-stream
"socks")
33 (autoload 'open-ssl-stream
"ssl")
35 (defgroup url-gateway nil
36 "URL gateway variables"
39 (defcustom url-gateway-local-host-regexp nil
40 "*A regular expression specifying local hostnames/machines."
41 :type
'(choice (const nil
) regexp
)
44 (defcustom url-gateway-prompt-pattern
45 "^[^#$%>;]*[#$%>;] *" ;; "bash\\|\$ *\r?$\\|> *\r?"
46 "*A regular expression matching a shell prompt."
50 (defcustom url-gateway-rlogin-host nil
51 "*What hostname to actually rlog into before doing a telnet."
52 :type
'(choice (const nil
) string
)
55 (defcustom url-gateway-rlogin-user-name nil
56 "*Username to log into the remote machine with when using rlogin."
57 :type
'(choice (const nil
) string
)
60 (defcustom url-gateway-rlogin-parameters
'("telnet" "-8")
61 "*Parameters to `url-open-rlogin'.
62 This list will be used as the parameter list given to rsh."
63 :type
'(repeat string
)
66 (defcustom url-gateway-telnet-host nil
67 "*What hostname to actually login to before doing a telnet."
68 :type
'(choice (const nil
) string
)
71 (defcustom url-gateway-telnet-parameters
'("exec" "telnet" "-8")
72 "*Parameters to `url-open-telnet'.
73 This list will be executed as a command after logging in via telnet."
74 :type
'(repeat string
)
77 (defcustom url-gateway-telnet-login-prompt
"^\r*.?login:"
78 "*Prompt that tells us we should send our username when loggin in w/telnet."
82 (defcustom url-gateway-telnet-password-prompt
"^\r*.?password:"
83 "*Prompt that tells us we should send our password when loggin in w/telnet."
87 (defcustom url-gateway-telnet-user-name nil
88 "User name to log in via telnet with."
89 :type
'(choice (const nil
) string
)
92 (defcustom url-gateway-telnet-password nil
93 "Password to use to log in via telnet with."
94 :type
'(choice (const nil
) string
)
97 (defcustom url-gateway-broken-resolution nil
98 "*Whether to use nslookup to resolve hostnames.
99 This should be used when your version of Emacs cannot correctly use DNS,
100 but your machine can. This usually happens if you are running a statically
101 linked Emacs under SunOS 4.x"
105 (defcustom url-gateway-nslookup-program
"nslookup"
106 "*If non-NIL then a string naming nslookup program."
107 :type
'(choice (const :tag
"None" :value nil
) string
)
110 ;; Stolen from ange-ftp
112 (defun url-gateway-nslookup-host (host)
113 "Attempt to resolve the given HOST using nslookup if possible."
114 (interactive "sHost: ")
115 (if url-gateway-nslookup-program
116 (let ((proc (start-process " *nslookup*" " *nslookup*"
117 url-gateway-nslookup-program host
))
119 (process-kill-without-query proc
)
121 (set-buffer (process-buffer proc
))
122 (while (memq (process-status proc
) '(run open
))
123 (accept-process-output proc
))
124 (goto-char (point-min))
125 (if (re-search-forward "Name:.*\nAddress: *\\(.*\\)$" nil t
)
126 (setq res
(buffer-substring (match-beginning 1)
128 (kill-buffer (current-buffer)))
132 ;; Stolen from red gnus nntp.el
133 (defun url-wait-for-string (regexp proc
)
134 "Wait until string matching REGEXP arrives in process PROC's buffer."
135 (let ((buf (current-buffer)))
136 (goto-char (point-min))
137 (while (not (re-search-forward regexp nil t
))
138 (accept-process-output proc
)
140 (goto-char (point-min)))))
142 ;; Stolen from red gnus nntp.el
143 (defun url-open-rlogin (name buffer host service
)
144 "Open a connection using rsh."
145 (if (not (stringp service
))
146 (setq service
(int-to-string service
)))
147 (let ((proc (if url-gateway-rlogin-user-name
150 url-gateway-rlogin-host
"-l" url-gateway-rlogin-user-name
152 (append url-gateway-rlogin-parameters
153 (list host service
)) " "))
155 name buffer
"rsh" url-gateway-rlogin-host
157 (append url-gateway-rlogin-parameters
161 (url-wait-for-string "^\r*200" proc
)
163 (delete-region (point-min) (point))
166 ;; Stolen from red gnus nntp.el
167 (defun url-open-telnet (name buffer host service
)
168 (if (not (stringp service
))
169 (setq service
(int-to-string service
)))
171 (set-buffer (get-buffer-create buffer
))
173 (let ((proc (start-process name buffer
"telnet" "-8"))
174 (case-fold-search t
))
175 (when (memq (process-status proc
) '(open run
))
176 (process-send-string proc
"set escape \^X\n")
177 (process-send-string proc
(concat
178 "open " url-gateway-telnet-host
"\n"))
179 (url-wait-for-string url-gateway-telnet-login-prompt proc
)
182 (or url-gateway-telnet-user-name
183 (setq url-gateway-telnet-user-name
(read-string "login: ")))
185 (url-wait-for-string url-gateway-telnet-password-prompt proc
)
188 (or url-gateway-telnet-password
189 (setq url-gateway-telnet-password
190 (funcall url-passwd-entry-func
"Password: ")))
193 (url-wait-for-string url-gateway-prompt-pattern proc
)
195 proc
(concat (mapconcat 'identity
196 (append url-gateway-telnet-parameters
197 (list host service
)) " ") "\n"))
198 (url-wait-for-string "^\r*Escape character.*\r*\n+" proc
)
199 (delete-region (point-min) (match-end 0))
200 (process-send-string proc
"\^]\n")
201 (url-wait-for-string "^telnet" proc
)
202 (process-send-string proc
"mode character\n")
203 (accept-process-output proc
1)
205 (goto-char (point-min))
207 (delete-region (point) (point-max)))
211 (defun url-open-stream (name buffer host service
)
212 "Open a stream to HOST, possibly via a gateway.
213 Args per `open-network-stream'.
214 Will not make a connexion if `url-gateway-unplugged' is non-nil."
215 (unless url-gateway-unplugged
216 (let ((gw-method (if (and url-gateway-local-host-regexp
217 (not (eq 'ssl url-gateway-method
))
219 url-gateway-local-host-regexp
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)
228 ;;; (int-to-string service))))
230 ;; An attempt to deal with denied connections, and attempt
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
248 (open-ssl-stream name buffer host service
))
250 (open-network-stream name buffer host service
))
252 (socks-open-network-stream name buffer host service
))
254 (url-open-telnet name buffer host service
))
256 (url-open-rlogin name buffer host service
))
258 (error "Bad setting of url-gateway-method: %s"
259 url-gateway-method
)))))