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