1 ;;; url-gw.el --- Gateway munging for URL loading
3 ;; Copyright (C) 1997, 1998, 2004, 2005 Free Software Foundation, Inc.
5 ;; Author: Bill Perry <wmperry@gnu.org>
6 ;; Keywords: comm, data, processes
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)
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., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
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")
34 (autoload 'open-tls-stream
"tls")
36 (defgroup url-gateway nil
37 "URL gateway variables."
40 (defcustom url-gateway-local-host-regexp nil
41 "*A regular expression specifying local hostnames/machines."
42 :type
'(choice (const nil
) regexp
)
45 (defcustom url-gateway-prompt-pattern
46 "^[^#$%>;]*[#$%>;] *" ;; "bash\\|\$ *\r?$\\|> *\r?"
47 "*A regular expression matching a shell prompt."
51 (defcustom url-gateway-rlogin-host nil
52 "*What hostname to actually rlog into before doing a telnet."
53 :type
'(choice (const nil
) string
)
56 (defcustom url-gateway-rlogin-user-name nil
57 "*Username to log into the remote machine with when using rlogin."
58 :type
'(choice (const nil
) string
)
61 (defcustom url-gateway-rlogin-parameters
'("telnet" "-8")
62 "*Parameters to `url-open-rlogin'.
63 This list will be used as the parameter list given to rsh."
64 :type
'(repeat string
)
67 (defcustom url-gateway-telnet-host nil
68 "*What hostname to actually login to before doing a telnet."
69 :type
'(choice (const nil
) string
)
72 (defcustom url-gateway-telnet-parameters
'("exec" "telnet" "-8")
73 "*Parameters to `url-open-telnet'.
74 This list will be executed as a command after logging in via telnet."
75 :type
'(repeat string
)
78 (defcustom url-gateway-telnet-login-prompt
"^\r*.?login:"
79 "*Prompt that tells us we should send our username when loggin in w/telnet."
83 (defcustom url-gateway-telnet-password-prompt
"^\r*.?password:"
84 "*Prompt that tells us we should send our password when loggin in w/telnet."
88 (defcustom url-gateway-telnet-user-name nil
89 "User name to log in via telnet with."
90 :type
'(choice (const nil
) string
)
93 (defcustom url-gateway-telnet-password nil
94 "Password to use to log in via telnet with."
95 :type
'(choice (const nil
) string
)
98 (defcustom url-gateway-broken-resolution nil
99 "*Whether to use nslookup to resolve hostnames.
100 This should be used when your version of Emacs cannot correctly use DNS,
101 but your machine can. This usually happens if you are running a statically
102 linked Emacs under SunOS 4.x"
106 (defcustom url-gateway-nslookup-program
"nslookup"
107 "*If non-NIL then a string naming nslookup program."
108 :type
'(choice (const :tag
"None" :value nil
) string
)
111 ;; Stolen from ange-ftp
113 (defun url-gateway-nslookup-host (host)
114 "Attempt to resolve the given HOST using nslookup if possible."
115 (interactive "sHost: ")
116 (if url-gateway-nslookup-program
117 (let ((proc (start-process " *nslookup*" " *nslookup*"
118 url-gateway-nslookup-program host
))
120 (set-process-query-on-exit-flag proc nil
)
122 (set-buffer (process-buffer proc
))
123 (while (memq (process-status proc
) '(run open
))
124 (accept-process-output proc
))
125 (goto-char (point-min))
126 (if (re-search-forward "Name:.*\nAddress: *\\(.*\\)$" nil t
)
127 (setq res
(buffer-substring (match-beginning 1)
129 (kill-buffer (current-buffer)))
133 ;; Stolen from red gnus nntp.el
134 (defun url-wait-for-string (regexp proc
)
135 "Wait until string matching REGEXP arrives in process PROC's buffer."
136 (let ((buf (current-buffer)))
137 (goto-char (point-min))
138 (while (not (re-search-forward regexp nil t
))
139 (accept-process-output proc
)
141 (goto-char (point-min)))))
143 ;; Stolen from red gnus nntp.el
144 (defun url-open-rlogin (name buffer host service
)
145 "Open a connection using rsh."
146 (if (not (stringp service
))
147 (setq service
(int-to-string service
)))
148 (let ((proc (if url-gateway-rlogin-user-name
151 url-gateway-rlogin-host
"-l" url-gateway-rlogin-user-name
153 (append url-gateway-rlogin-parameters
154 (list host service
)) " "))
156 name buffer
"rsh" url-gateway-rlogin-host
158 (append url-gateway-rlogin-parameters
162 (url-wait-for-string "^\r*200" proc
)
164 (delete-region (point-min) (point))
167 ;; Stolen from red gnus nntp.el
168 (defun url-open-telnet (name buffer host service
)
169 (if (not (stringp service
))
170 (setq service
(int-to-string service
)))
172 (set-buffer (get-buffer-create buffer
))
174 (let ((proc (start-process name buffer
"telnet" "-8"))
175 (case-fold-search t
))
176 (when (memq (process-status proc
) '(open run
))
177 (process-send-string proc
"set escape \^X\n")
178 (process-send-string proc
(concat
179 "open " url-gateway-telnet-host
"\n"))
180 (url-wait-for-string url-gateway-telnet-login-prompt proc
)
183 (or url-gateway-telnet-user-name
184 (setq url-gateway-telnet-user-name
(read-string "login: ")))
186 (url-wait-for-string url-gateway-telnet-password-prompt proc
)
189 (or url-gateway-telnet-password
190 (setq url-gateway-telnet-password
191 (read-passwd "Password: ")))
194 (url-wait-for-string url-gateway-prompt-pattern proc
)
196 proc
(concat (mapconcat 'identity
197 (append url-gateway-telnet-parameters
198 (list host service
)) " ") "\n"))
199 (url-wait-for-string "^\r*Escape character.*\r*\n+" proc
)
200 (delete-region (point-min) (match-end 0))
201 (process-send-string proc
"\^]\n")
202 (url-wait-for-string "^telnet" proc
)
203 (process-send-string proc
"mode character\n")
204 (accept-process-output proc
1)
206 (goto-char (point-min))
208 (delete-region (point) (point-max)))
212 (defun url-open-stream (name buffer host service
)
213 "Open a stream to HOST, possibly via a gateway.
214 Args per `open-network-stream'.
215 Will not make a connection if `url-gateway-unplugged' is non-nil."
216 (unless url-gateway-unplugged
217 (let ((gw-method (if (and url-gateway-local-host-regexp
218 (not (eq 'tls url-gateway-method
))
219 (not (eq 'ssl url-gateway-method
))
221 url-gateway-local-host-regexp
225 ;;; ;; This hack is for OS/2 Emacs so that it will not do bogus CRLF
226 ;;; ;; conversions while trying to be 'helpful'
227 ;;; (tcp-binary-process-output-services (if (stringp service)
230 ;;; (int-to-string service))))
232 ;; An attempt to deal with denied connections, and attempt
239 ;; If the user told us to do DNS for them, do it.
240 (if url-gateway-broken-resolution
241 (setq host
(url-gateway-nslookup-host host
)))
243 (condition-case errobj
244 ;; This is a clean way to ensure the new process inherits the
245 ;; right coding systems in both Emacs and XEmacs.
246 (let ((coding-system-for-read 'binary
)
247 (coding-system-for-write 'binary
))
248 (setq conn
(case gw-method
250 (open-tls-stream name buffer host service
))
252 (open-ssl-stream name buffer host service
))
254 (open-network-stream name buffer host service
))
256 (socks-open-network-stream name buffer host service
))
258 (url-open-telnet name buffer host service
))
260 (url-open-rlogin name buffer host service
))
262 (error "Bad setting of url-gateway-method: %s"
263 url-gateway-method
)))))
270 ;;; arch-tag: 1c4c0317-6d03-45b8-b3f3-838bd8f9d838
271 ;;; url-gw.el ends here