1 ;;; ftp.el --- file input and output over Internet using FTP
3 ;; Copyright (C) 1987 Free Software Foundation, Inc.
4 ;; Author mly@prep.ai.mit.edu.
6 ;; This file is part of GNU Emacs.
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)
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.
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.
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
)
27 ;; you can turn this off by doing
28 ;; (setq ftp-password-alist 'compulsory-urinalysis)
29 (defvar ftp-password-alist
() "Security sucks")
31 (defun read-ftp-user-password (host user new
)
34 (listp ftp-password-alist
)
35 (setq tem
(cdr (assoc host ftp-password-alist
)))
37 (string= user
(car tem
))))
41 (setq tem
(or (and (listp ftp-password-alist
)
42 (car (cdr (assoc host ftp-password-alist
))))
44 (setq user
(read-string (format
45 "User-name for %s (default \"%s\"): "
47 (if (equal user
"") (setq user tem
))))
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
))
54 (or (cdr (cdr (assoc host ftp-password-alist
)))
55 (let ((l ftp-password-alist
))
58 (if (string= (car (cdr (car l
))) user
)
59 (throw 'foo
(cdr (cdr (car l
))))
64 (if (and (listp ftp-password-alist
)
65 (not (string= (cdr tem
) "")))
66 (setq ftp-password-alist
(cons (cons host tem
)
70 (defun ftp-read-file-name (prompt)
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)))))
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)"
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
))
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)"
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
))
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
))
114 (let ((buffer (get-buffer-create (format "*ftp%s %s:%s*"
115 (if filep
"" "-directory")
119 (case-fold-search nil
))
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")
130 (if (ftp-command process
131 (format "%s \"%s\" -\nquit\n" (if filep
"get" "dir")
133 "\\(150\\|125\\).*\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
)
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
)))))
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
155 last ftping from HOST (unless `password-remembering' is disabled).
156 Supply a password of the symbol `t' to override this default
157 (interactively, this is done by giving a prefix arg)"
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
))
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
)
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
))
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
)
183 "\\(150\\|125\\).*\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
)
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
))))))
201 (defun ftp-setup-buffer (host file
)
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
208 (buffer-name (current-buffer))))
209 (while (get-buffer-process (current-buffer))
210 (kill-process (get-buffer-process (current-buffer))))
212 ;(buffer-disable-undo (current-buffer))
213 (setq buffer-read-only nil
)
215 (make-local-variable 'ftp-host
)
217 (make-local-variable 'ftp-file
)
219 (setq foo3
(current-buffer))
220 (setq buffer-read-only t
)
221 (start-process "ftp" (current-buffer) "ftp" "-i" "-n" "-g"))
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
)
229 "\\(Connected to \\|220\\|331\\|Remote system type\\|Using.*mode\\|Remember to set\\).*\n")
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
)))
238 (defun ftp-command (process command win ignore
)
239 (process-send-string process command
)
242 (cond ;((not (bolp)))
243 ((looking-at "^[0-9]+-")
244 (while (not (re-search-forward "^[0-9]+ " nil t
))
246 (accept-process-output process
)))
249 (goto-char (point-max))
251 ((looking-at "^ftp> \\|^\n")
252 (goto-char (match-end 0)))
254 ;; Ignore status messages whose codes indicate no problem.
256 ((looking-at "^[^0-9]")
257 ;; Ignore any lines that don't have status codes.
259 ((not (search-forward "\n" nil t
))
260 ;; the way asynchronous process-output works with (point)
261 ;; is really really disgusting.
264 (accept-process-output process
)
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
))
281 (defun ftp-sentinel (process msg asynchronous input
)
282 (cond ((null (buffer-name (process-buffer process
)))
284 (set-process-buffer process nil
))
285 ((and (eq (process-status process
) 'exit
)
286 (= (process-exit-status process
) 0))
288 (set-buffer (process-buffer process
))
290 (r (if input
"[0-9]+ bytes received in [0-9]+\\.[0-9]+ seconds.*$" "[0-9]+ bytes sent in [0-9]+\\.[0-9]+ seconds.*$")))
291 (goto-char (point-max))
292 (search-backward "226 ")
294 (search-backward "226 "))
296 (setq msg
(concat (format "ftp %s %s:%s done"
297 (if input
"read" "write")
299 (if (re-search-forward r nil t
)
300 (concat ": " (buffer-substring
304 (delete-region p
(point-max))
306 (set-buffer (get-buffer-create "*ftp log*"))
307 (let ((buffer-read-only nil
))
309 ;; Note the preceding let must end here
310 ;; so it doesn't cross the (kill-buffer (current-buffer)).
314 (and (boundp 'ftp-temp-file-name
)
316 (delete-file ftp-temp-file-name
))
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)
332 (progn (message "%s" msg
)
334 ((memq (process-status process
) '(exit signal
))
336 (set-buffer (process-buffer process
))
337 (setq msg
(format "Ftp died (buffer %s): %s"
338 (buffer-name (current-buffer))
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))
348 (if (waiting-for-user-input-p)
349 (error "%s" msg
))))))
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
361 (buffer-name (current-buffer)))))
362 (setq buffer-read-only nil
))
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
)
370 (accept-process-output process
)
372 (set-buffer-modified-p nil
)
373 (message "FTP writing %s:%s...done" ftp-host ftp-file
))
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
)
382 (accept-process-output process
)
384 (and (eq (process-status process
) 'exit
)
385 (= (process-exit-status process
) 0)
386 (set-buffer-modified-p nil
))
387 (message "Reverted")))