Initial Commit
[packages] / xemacs-packages / w3 / lisp / url-gw.el
1 ;;; url-gw.el --- Gateway munging for URL loading
2
3 ;; Copyright (C) 1997-1998, 2004-2012  Free Software Foundation, Inc.
4
5 ;; Author: Bill Perry <wmperry@gnu.org>
6 ;; Keywords: comm, data, processes
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 3 of the License, or
13 ;; (at your option) 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.  If not, see <http://www.gnu.org/licenses/>.
22
23 ;;; Code:
24
25 (eval-when-compile (require 'cl))
26 (require 'url-vars)
27
28 ;; Fixme: support SSH explicitly or via a url-gateway-rlogin-program?
29
30 (autoload 'socks-open-network-stream "socks")
31
32 (defgroup url-gateway nil
33   "URL gateway variables."
34   :group 'url)
35
36 (defcustom url-gateway-local-host-regexp nil
37   "A regular expression specifying local hostnames/machines."
38   :type '(choice (const nil) regexp)
39   :group 'url-gateway)
40
41 (defcustom url-gateway-prompt-pattern
42   "^[^#$%>;]*[#$%>;] *" ;; "bash\\|\$ *\r?$\\|> *\r?"
43   "A regular expression matching a shell prompt."
44   :type 'regexp
45   :group 'url-gateway)
46
47 (defcustom url-gateway-rlogin-host nil
48   "What hostname to actually rlog into before doing a telnet."
49   :type '(choice (const nil) string)
50   :group 'url-gateway)
51
52 (defcustom url-gateway-rlogin-user-name nil
53   "Username to log into the remote machine with when using rlogin."
54   :type '(choice (const nil) string)
55   :group 'url-gateway)
56
57 (defcustom url-gateway-rlogin-parameters '("telnet" "-8")
58   "Parameters to `url-open-rlogin'.
59 This list will be used as the parameter list given to rsh."
60   :type '(repeat string)
61   :group 'url-gateway)
62
63 (defcustom url-gateway-telnet-host nil
64   "What hostname to actually login to before doing a telnet."
65   :type '(choice (const nil) string)
66   :group 'url-gateway)
67
68 (defcustom url-gateway-telnet-parameters '("exec" "telnet" "-8")
69   "Parameters to `url-open-telnet'.
70 This list will be executed as a command after logging in via telnet."
71   :type '(repeat string)
72   :group 'url-gateway)
73
74 (defcustom url-gateway-telnet-login-prompt "^\r*.?login:"
75   "Prompt that tells us we should send our username when logging in w/telnet."
76   :type 'regexp
77   :group 'url-gateway)
78
79 (defcustom url-gateway-telnet-password-prompt "^\r*.?password:"
80   "Prompt that tells us we should send our password when logging in w/telnet."
81   :type 'regexp
82   :group 'url-gateway)
83
84 (defcustom url-gateway-telnet-user-name nil
85   "User name to log in via telnet with."
86   :type '(choice (const nil) string)
87   :group 'url-gateway)
88
89 (defcustom url-gateway-telnet-password nil
90   "Password to use to log in via telnet with."
91   :type '(choice (const nil) string)
92   :group 'url-gateway)
93
94 (defcustom url-gateway-broken-resolution nil
95   "Whether to use nslookup to resolve hostnames.
96 This should be used when your version of Emacs cannot correctly use DNS,
97 but your machine can.  This usually happens if you are running a statically
98 linked Emacs under SunOS 4.x."
99   :type 'boolean
100   :group 'url-gateway)
101
102 (defcustom url-gateway-nslookup-program "nslookup"
103   "If non-nil then a string naming nslookup program."
104   :type '(choice (const :tag "None" :value nil) string)
105   :group 'url-gateway)
106
107 ;; Stolen from ange-ftp
108 ;;;###autoload
109 (defun url-gateway-nslookup-host (host)
110   "Attempt to resolve the given HOST using nslookup if possible."
111   (interactive "sHost:  ")
112   (if url-gateway-nslookup-program
113       (let ((proc (start-process " *nslookup*" " *nslookup*"
114                                  url-gateway-nslookup-program host))
115             (res host))
116         (if (featurep 'xemacs)
117             (process-kill-without-query proc t)
118           (set-process-query-on-exit-flag proc nil))
119         (with-current-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)
125                                           (match-end 1))))
126           (kill-buffer (current-buffer)))
127         res)
128     host))
129
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)
137       (set-buffer buf)
138       (goto-char (point-min)))))
139
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
146                   (start-process
147                    name buffer "rsh"
148                    url-gateway-rlogin-host "-l" url-gateway-rlogin-user-name
149                    (mapconcat 'identity
150                               (append url-gateway-rlogin-parameters
151                                       (list host service)) " "))
152                 (start-process
153                  name buffer "rsh" url-gateway-rlogin-host
154                  (mapconcat 'identity
155                             (append url-gateway-rlogin-parameters
156                                     (list host service))
157                             " ")))))
158     (set-buffer buffer)
159     (url-wait-for-string "^\r*200" proc)
160     (beginning-of-line)
161     (delete-region (point-min) (point))
162     proc))
163
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)))
168   (with-current-buffer (get-buffer-create buffer)
169     (erase-buffer)
170     (let ((proc (start-process name buffer "telnet" "-8"))
171           (case-fold-search t))
172       (when (memq (process-status proc) '(open run))
173         (process-send-string proc "set escape \^X\n")
174         (process-send-string proc (concat
175                                    "open " url-gateway-telnet-host "\n"))
176         (url-wait-for-string url-gateway-telnet-login-prompt proc)
177         (process-send-string
178          proc (concat
179                (or url-gateway-telnet-user-name
180                    (setq url-gateway-telnet-user-name (read-string "login: ")))
181                "\n"))
182         (url-wait-for-string url-gateway-telnet-password-prompt proc)
183         (process-send-string
184          proc (concat
185                (or url-gateway-telnet-password
186                    (setq url-gateway-telnet-password
187                          (read-passwd "Password: ")))
188                "\n"))
189         (erase-buffer)
190         (url-wait-for-string url-gateway-prompt-pattern proc)
191         (process-send-string
192          proc (concat (mapconcat 'identity
193                                  (append url-gateway-telnet-parameters
194                                          (list host service)) " ") "\n"))
195         (url-wait-for-string "^\r*Escape character.*\r*\n+" proc)
196         (delete-region (point-min) (match-end 0))
197         (process-send-string proc "\^]\n")
198         (url-wait-for-string "^telnet" proc)
199         (process-send-string proc "mode character\n")
200         (accept-process-output proc 1)
201         (sit-for 1)
202         (goto-char (point-min))
203         (forward-line 1)
204         (delete-region (point) (point-max)))
205       proc)))
206
207 ;;;###autoload
208 (defun url-open-stream (name buffer host service)
209   "Open a stream to HOST, possibly via a gateway.
210 Args per `open-network-stream'.
211 Will not make a connection if `url-gateway-unplugged' is non-nil.
212 Might do a non-blocking connection; use `process-status' to check."
213   (unless url-gateway-unplugged
214     (let ((gw-method (if (and url-gateway-local-host-regexp
215                               (not (eq 'tls url-gateway-method))
216                               (not (eq 'ssl url-gateway-method))
217                               (string-match
218                                url-gateway-local-host-regexp
219                                host))
220                          'native
221                        url-gateway-method))
222           ;; An attempt to deal with denied connections, and attempt
223           ;; to reconnect
224           (cur-retries 0)
225           (retry t)
226           (errobj nil)
227           (conn nil))
228
229       ;; If the user told us to do DNS for them, do it.
230       (if url-gateway-broken-resolution
231           (setq host (url-gateway-nslookup-host host)))
232
233       (condition-case errobj
234           ;; This is a clean way to ensure the new process inherits the
235           ;; right coding systems in both Emacs and XEmacs.
236           (let ((coding-system-for-read 'binary)
237                 (coding-system-for-write 'binary))
238             (setq conn (case gw-method
239                          ((tls ssl native)
240                           (if (eq gw-method 'native)
241                               (setq gw-method 'plain))
242                           (apply
243                            #'open-network-stream
244                            name buffer host service
245                            (if (or (not (featurep 'xemacs))
246                                    (function-allows-args 'open-network-stream
247                                                          8))
248                              `(:type gw-method
249                                ;; Use non-blocking socket if we can.
250                                :nowait ,(ignore-errors
251                                           (featurep 'make-network-process
252                                                     '(:nowait t)))))))
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       conn)))
263
264 (provide 'url-gw)
265
266 ;;; url-gw.el ends here