Mercurial > emacs
annotate lisp/=ftp.el @ 797:ecc182062509
entered into RCS
author | Jim Blandy <jimb@redhat.com> |
---|---|
date | Thu, 16 Jul 1992 18:51:53 +0000 |
parents | 6fb68a1460a6 |
children | 4f28bd14272c |
rev | line source |
---|---|
660
08eb386dd0f3
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
633
diff
changeset
|
1 ;;; ftp.el --- file input and output over Internet using FTP |
08eb386dd0f3
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
633
diff
changeset
|
2 |
793
6fb68a1460a6
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
660
diff
changeset
|
3 ;; Author: Richard Mlynarik <mly@prep.ai.mit.edu> |
6fb68a1460a6
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
660
diff
changeset
|
4 ;; Last-Modified: 05 May 1992 |
6fb68a1460a6
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
660
diff
changeset
|
5 |
64 | 6 ;; Copyright (C) 1987 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 1, 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 | |
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | |
23 | |
24 ;; Prevent changes in major modes from altering these variables. | |
25 (put 'ftp-temp-file-name 'permanent-local t) | |
26 (put 'ftp-file 'permanent-local t) | |
27 (put 'ftp-host 'permanent-local t) | |
28 | |
29 ;; you can turn this off by doing | |
30 ;; (setq ftp-password-alist 'compulsory-urinalysis) | |
31 (defvar ftp-password-alist () "Security sucks") | |
32 | |
33 (defun read-ftp-user-password (host user new) | |
34 (let (tem) | |
35 (if (and (not new) | |
36 (listp ftp-password-alist) | |
37 (setq tem (cdr (assoc host ftp-password-alist))) | |
38 (or (null user) | |
39 (string= user (car tem)))) | |
40 tem | |
41 (or user | |
42 (progn | |
43 (setq tem (or (and (listp ftp-password-alist) | |
44 (car (cdr (assoc host ftp-password-alist)))) | |
45 (user-login-name))) | |
46 (setq user (read-string (format | |
47 "User-name for %s (default \"%s\"): " | |
48 host tem))) | |
49 (if (equal user "") (setq user tem)))) | |
50 (setq tem (cons user | |
51 ;; If you want to use some non-echoing string-reader, | |
52 ;; feel free to write it yourself. I don't care enough. | |
53 (read-string (format "Password for %s@%s: " user host) | |
54 (if (not (listp ftp-password-alist)) | |
55 "" | |
56 (or (cdr (cdr (assoc host ftp-password-alist))) | |
57 (let ((l ftp-password-alist)) | |
58 (catch 'foo | |
59 (while l | |
60 (if (string= (car (cdr (car l))) user) | |
61 (throw 'foo (cdr (cdr (car l)))) | |
62 (setq l (cdr l)))) | |
63 nil)) | |
64 ""))))) | |
65 (message "") | |
66 (if (and (listp ftp-password-alist) | |
67 (not (string= (cdr tem) ""))) | |
68 (setq ftp-password-alist (cons (cons host tem) | |
69 ftp-password-alist))) | |
70 tem))) | |
71 | |
72 (defun ftp-read-file-name (prompt) | |
73 (let ((s "")) | |
74 (while (not (string-match "\\`[ \t]*\\([^ \t:]+\\)[ \t]*:\\(.+\\)\\'" s)) | |
75 (setq s (read-string prompt s))) | |
76 (list (substring s (match-beginning 1) (match-end 1)) | |
77 (substring s (match-beginning 2) (match-end 2))))) | |
78 | |
79 | |
318 | 80 ;;;###autoload |
64 | 81 (defun ftp-find-file (host file &optional user password) |
82 "FTP to HOST to get FILE, logging in as USER with password PASSWORD. | |
83 Interactively, HOST and FILE are specified by reading a string with | |
84 a colon character separating the host from the filename. | |
85 USER and PASSWORD are defaulted from the values used when | |
86 last ftping from HOST (unless password-remembering is disabled). | |
87 Supply a password of the symbol `t' to override this default | |
88 (interactively, this is done by giving a prefix arg)" | |
89 (interactive | |
90 (append (ftp-read-file-name "FTP get host:file: ") | |
91 (list nil (not (null current-prefix-arg))))) | |
92 (ftp-find-file-or-directory host file t user password)) | |
93 | |
318 | 94 ;;;###autoload |
64 | 95 (defun ftp-list-directory (host file &optional user password) |
96 "FTP to HOST to list DIRECTORY, logging in as USER with password PASSWORD. | |
97 Interactively, HOST and FILE are specified by reading a string with | |
98 a colon character separating the host from the filename. | |
99 USER and PASSWORD are defaulted from the values used when | |
100 last ftping from HOST (unless password-remembering is disabled). | |
101 Supply a password of the symbol `t' to override this default | |
102 (interactively, this is done by giving a prefix arg)" | |
103 (interactive | |
104 (append (ftp-read-file-name "FTP get host:directory: ") | |
105 (list nil (not (null current-prefix-arg))))) | |
106 (ftp-find-file-or-directory host file nil user password)) | |
107 | |
108 (defun ftp-find-file-or-directory (host file filep &optional user password) | |
109 "FTP to HOST to get FILE. Third arg is t for file, nil for directory. | |
110 Log in as USER with PASSWORD. If USER is nil or PASSWORD is nil or t, | |
111 we prompt for the user name and password." | |
112 (or (and user password (not (eq password t))) | |
113 (progn (setq user (read-ftp-user-password host user (eq password t)) | |
114 password (cdr user) | |
115 user (car user)))) | |
116 (let ((buffer (get-buffer-create (format "*ftp%s %s:%s*" | |
117 (if filep "" "-directory") | |
118 host file)))) | |
119 (set-buffer buffer) | |
120 (let ((process nil) | |
121 (case-fold-search nil)) | |
122 (let ((win nil)) | |
123 (unwind-protect | |
124 (progn | |
125 (setq process (ftp-setup-buffer host file)) | |
126 (if (setq win (ftp-login process host user password)) | |
127 (message "Logged in") | |
128 (error "Ftp login failed"))) | |
129 (or win (and process (delete-process process))))) | |
130 (message "Opening %s %s:%s..." (if filep "file" "directory") | |
131 host file) | |
132 (if (ftp-command process | |
133 (format "%s \"%s\" -\nquit\n" (if filep "get" "dir") | |
134 file) | |
135 "\\(150\\|125\\).*\n" | |
136 "200.*\n") | |
137 (progn (forward-line 1) | |
138 (let ((buffer-read-only nil)) | |
139 (delete-region (point-min) (point))) | |
140 (message "Retrieving %s:%s in background. Bye!" host file) | |
141 (set-process-sentinel process | |
142 'ftp-asynchronous-input-sentinel) | |
143 process) | |
144 (switch-to-buffer buffer) | |
145 (let ((buffer-read-only nil)) | |
146 (insert-before-markers "<<<Ftp lost>>>")) | |
147 (delete-process process) | |
148 (error "Ftp %s:%s lost" host file))))) | |
149 | |
150 | |
318 | 151 ;;;###autoload |
64 | 152 (defun ftp-write-file (host file &optional user password) |
153 "FTP to HOST to write FILE, logging in as USER with password PASSWORD. | |
154 Interactively, HOST and FILE are specified by reading a string with colon | |
155 separating the host from the filename. | |
156 USER and PASSWORD are defaulted from the values used when | |
318 | 157 last ftping from HOST (unless `password-remembering' is disabled). |
64 | 158 Supply a password of the symbol `t' to override this default |
159 (interactively, this is done by giving a prefix arg)" | |
160 (interactive | |
161 (append (ftp-read-file-name "FTP write host:file: ") | |
162 (list nil (not (null current-prefix-arg))))) | |
163 (or (and user password (not (eq password t))) | |
164 (progn (setq user (read-ftp-user-password host user (eq password t)) | |
165 password (cdr user) | |
166 user (car user)))) | |
167 (let ((buffer (get-buffer-create (format "*ftp %s:%s*" host file))) | |
168 (tmp (make-temp-name "/tmp/emacsftp"))) | |
169 (write-region (point-min) (point-max) tmp) | |
170 (save-excursion | |
171 (set-buffer buffer) | |
172 (make-local-variable 'ftp-temp-file-name) | |
173 (setq ftp-temp-file-name tmp) | |
174 (let ((process (ftp-setup-buffer host file)) | |
175 (case-fold-search nil)) | |
176 (let ((win nil)) | |
177 (unwind-protect | |
178 (if (setq win (ftp-login process host user password)) | |
179 (message "Logged in") | |
180 (error "Ftp login lost")) | |
181 (or win (delete-process process)))) | |
182 (message "Opening file %s:%s..." host file) | |
183 (if (ftp-command process | |
184 (format "send \"%s\" \"%s\"\nquit\n" tmp file) | |
318 | 185 "\\(150\\|125\\).*\n" |
64 | 186 "200.*\n") |
187 (progn (forward-line 1) | |
188 (setq foo1 (current-buffer)) | |
189 (let ((buffer-read-only nil)) | |
190 (delete-region (point-min) (point))) | |
191 (message "Saving %s:%s in background. Bye!" host file) | |
192 (set-process-sentinel process | |
193 'ftp-asynchronous-output-sentinel) | |
194 process) | |
195 (switch-to-buffer buffer) | |
196 (setq foo2 (current-buffer)) | |
197 (let ((buffer-read-only nil)) | |
198 (insert-before-markers "<<<Ftp lost>>>")) | |
199 (delete-process process) | |
200 (error "Ftp write %s:%s lost" host file)))))) | |
201 | |
202 | |
203 (defun ftp-setup-buffer (host file) | |
204 (fundamental-mode) | |
205 (and (get-buffer-process (current-buffer)) | |
206 (progn (discard-input) | |
207 (if (y-or-n-p (format "Kill process \"%s\" in %s? " | |
208 (process-name (get-buffer-process | |
209 (current-buffer))) | |
210 (buffer-name (current-buffer)))) | |
211 (while (get-buffer-process (current-buffer)) | |
212 (kill-process (get-buffer-process (current-buffer)))) | |
213 (error "Foo")))) | |
214 ;(buffer-disable-undo (current-buffer)) | |
215 (setq buffer-read-only nil) | |
216 (erase-buffer) | |
217 (make-local-variable 'ftp-host) | |
218 (setq ftp-host host) | |
219 (make-local-variable 'ftp-file) | |
220 (setq ftp-file file) | |
221 (setq foo3 (current-buffer)) | |
222 (setq buffer-read-only t) | |
223 (start-process "ftp" (current-buffer) "ftp" "-i" "-n" "-g")) | |
224 | |
225 | |
226 (defun ftp-login (process host user password) | |
227 (message "FTP logging in as %s@%s..." user host) | |
228 (if (ftp-command process | |
229 (format "open %s\nuser %s %s\n" host user password) | |
230 "230.*\n" | |
231 "\\(Connected to \\|220\\|331\\|Remote system type\\|Using.*mode\\|Remember to set\\).*\n") | |
232 t | |
233 (switch-to-buffer (process-buffer process)) | |
234 (delete-process process) | |
235 (if (listp ftp-password-alist) | |
236 (setq ftp-password-alist (delq (assoc host ftp-password-alist) | |
237 ftp-password-alist))) | |
238 nil)) | |
239 | |
240 (defun ftp-command (process command win ignore) | |
241 (process-send-string process command) | |
242 (let ((p 1)) | |
243 (while (numberp p) | |
244 (cond ;((not (bolp))) | |
318 | 245 ((looking-at "^[0-9]+-") |
246 (while (not (re-search-forward "^[0-9]+ " nil t)) | |
247 (save-excursion | |
248 (accept-process-output process))) | |
249 (beginning-of-line)) | |
64 | 250 ((looking-at win) |
251 (goto-char (point-max)) | |
252 (setq p t)) | |
253 ((looking-at "^ftp> \\|^\n") | |
254 (goto-char (match-end 0))) | |
255 ((looking-at ignore) | |
318 | 256 ;; Ignore status messages whose codes indicate no problem. |
64 | 257 (forward-line 1)) |
633
379b94c9f29e
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
318
diff
changeset
|
258 ((looking-at "^[^0-9]") |
379b94c9f29e
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
318
diff
changeset
|
259 ;; Ignore any lines that don't have status codes. |
379b94c9f29e
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
318
diff
changeset
|
260 (forward-line 1)) |
64 | 261 ((not (search-forward "\n" nil t)) |
81
ef6cee0af549
*** empty log message ***
Robert J. Chassell <bob@rattlesnake.com>
parents:
64
diff
changeset
|
262 ;; the way asynchronous process-output works with (point) |
64 | 263 ;; is really really disgusting. |
264 (setq p (point)) | |
265 (condition-case () | |
266 (accept-process-output process) | |
267 (error nil)) | |
268 (goto-char p)) | |
269 (t | |
270 (setq p nil)))) | |
271 p)) | |
272 | |
273 | |
274 (defun ftp-asynchronous-input-sentinel (process msg) | |
275 (ftp-sentinel process msg t t)) | |
276 (defun ftp-synchronous-input-sentinel (process msg) | |
277 (ftp-sentinel process msg nil t)) | |
278 (defun ftp-asynchronous-output-sentinel (process msg) | |
279 (ftp-sentinel process msg t nil)) | |
280 (defun ftp-synchronous-output-sentinel (process msg) | |
281 (ftp-sentinel process msg nil nil)) | |
282 | |
283 (defun ftp-sentinel (process msg asynchronous input) | |
284 (cond ((null (buffer-name (process-buffer process))) | |
285 ;; deleted buffer | |
286 (set-process-buffer process nil)) | |
287 ((and (eq (process-status process) 'exit) | |
288 (= (process-exit-status process) 0)) | |
289 (save-excursion | |
290 (set-buffer (process-buffer process)) | |
291 (let (msg | |
292 (r (if input "[0-9]+ bytes received in [0-9]+\\.[0-9]+ seconds.*$" "[0-9]+ bytes sent in [0-9]+\\.[0-9]+ seconds.*$"))) | |
318 | 293 (goto-char (point-max)) |
294 (search-backward "226 ") | |
295 (if (looking-at r) | |
296 (search-backward "226 ")) | |
297 (let ((p (point))) | |
298 (setq msg (concat (format "ftp %s %s:%s done" | |
299 (if input "read" "write") | |
300 ftp-host ftp-file) | |
301 (if (re-search-forward r nil t) | |
302 (concat ": " (buffer-substring | |
303 (match-beginning 0) | |
304 (match-end 0))) | |
305 ""))) | |
306 (delete-region p (point-max)) | |
307 (save-excursion | |
308 (set-buffer (get-buffer-create "*ftp log*")) | |
309 (let ((buffer-read-only nil)) | |
310 (insert msg ?\n)))) | |
64 | 311 ;; Note the preceding let must end here |
312 ;; so it doesn't cross the (kill-buffer (current-buffer)). | |
313 (if (not input) | |
314 (progn | |
315 (condition-case () | |
316 (and (boundp 'ftp-temp-file-name) | |
317 ftp-temp-file-name | |
318 (delete-file ftp-temp-file-name)) | |
319 (error nil)) | |
320 ;; Kill the temporary buffer which the ftp process | |
321 ;; puts its output in. | |
322 (kill-buffer (current-buffer))) | |
323 ;; You don't want to look at this. | |
324 (let ((kludge (generate-new-buffer (format "%s:%s (ftp)" | |
325 ftp-host ftp-file)))) | |
326 (setq kludge (prog1 (buffer-name kludge) (kill-buffer kludge))) | |
327 (rename-buffer kludge) | |
328 ;; ok, you can look again now. | |
329 (set-buffer-modified-p nil) | |
330 (ftp-setup-write-file-hooks))) | |
331 (if (and asynchronous | |
332 ;(waiting-for-user-input-p) | |
333 ) | |
334 (progn (message "%s" msg) | |
335 (sleep-for 2)))))) | |
336 ((memq (process-status process) '(exit signal)) | |
337 (save-excursion | |
338 (set-buffer (process-buffer process)) | |
339 (setq msg (format "Ftp died (buffer %s): %s" | |
340 (buffer-name (current-buffer)) | |
341 msg)) | |
342 (let ((buffer-read-only nil)) | |
343 (goto-char (point-max)) | |
344 (insert ?\n ?\n msg)) | |
345 (delete-process proc) | |
346 (set-buffer (get-buffer-create "*ftp log*")) | |
347 (let ((buffer-read-only nil)) | |
348 (goto-char (point-max)) | |
349 (insert msg)) | |
350 (if (waiting-for-user-input-p) | |
351 (error "%s" msg)))))) | |
352 | |
353 (defun ftp-setup-write-file-hooks () | |
354 (let ((hooks write-file-hooks)) | |
355 (make-local-variable 'write-file-hooks) | |
356 (setq write-file-hooks (append write-file-hooks | |
357 '(ftp-write-file-hook)))) | |
358 (make-local-variable 'revert-buffer-function) | |
359 (setq revert-buffer-function 'ftp-revert-buffer) | |
360 (setq default-directory "/tmp/") | |
361 (setq buffer-file-name (concat default-directory | |
362 (make-temp-name | |
363 (buffer-name (current-buffer))))) | |
364 (setq buffer-read-only nil)) | |
365 | |
366 (defun ftp-write-file-hook () | |
367 (let ((process (ftp-write-file ftp-host ftp-file))) | |
368 (set-process-sentinel process 'ftp-synchronous-output-sentinel) | |
369 (message "FTP writing %s:%s..." ftp-host ftp-file) | |
370 (while (eq (process-status process) 'run) | |
371 (condition-case () | |
372 (accept-process-output process) | |
373 (error nil))) | |
374 (set-buffer-modified-p nil) | |
375 (message "FTP writing %s:%s...done" ftp-host ftp-file)) | |
376 t) | |
377 | |
378 (defun ftp-revert-buffer (&rest ignore) | |
379 (let ((process (ftp-find-file ftp-host ftp-file))) | |
380 (set-process-sentinel process 'ftp-synchronous-input-sentinel) | |
381 (message "FTP reverting %s:%s" ftp-host ftp-file) | |
382 (while (eq (process-status process) 'run) | |
383 (condition-case () | |
384 (accept-process-output process) | |
385 (error nil))) | |
386 (and (eq (process-status process) 'exit) | |
387 (= (process-exit-status process) 0) | |
388 (set-buffer-modified-p nil)) | |
389 (message "Reverted"))) | |
660
08eb386dd0f3
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
633
diff
changeset
|
390 |
08eb386dd0f3
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
633
diff
changeset
|
391 ;;; ftp.el ends here |