1 ;; File input and output over Internet using FTP
2 ;; Copyright (C) 1987 Free Software Foundation, Inc.
3 ;; Author mly@prep.ai.mit.edu.
5 ;; This file is part of GNU Emacs.
7 ;; GNU Emacs is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation; either version 1, or (at your option)
12 ;; GNU Emacs is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs; see the file COPYING. If not, write to
19 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
21 ;; Prevent changes in major modes from altering these variables.
22 (put 'ftp-temp-file-name
'permanent-local t
)
23 (put 'ftp-file
'permanent-local t
)
24 (put 'ftp-host
'permanent-local t
)
26 ;; you can turn this off by doing
27 ;; (setq ftp-password-alist 'compulsory-urinalysis)
28 (defvar ftp-password-alist
() "Security sucks")
30 (defun read-ftp-user-password (host user new
)
33 (listp ftp-password-alist
)
34 (setq tem
(cdr (assoc host ftp-password-alist
)))
36 (string= user
(car tem
))))
40 (setq tem
(or (and (listp ftp-password-alist
)
41 (car (cdr (assoc host ftp-password-alist
))))
43 (setq user
(read-string (format
44 "User-name for %s (default \"%s\"): "
46 (if (equal user
"") (setq user tem
))))
48 ;; If you want to use some non-echoing string-reader,
49 ;; feel free to write it yourself. I don't care enough.
50 (read-string (format "Password for %s@%s: " user host
)
51 (if (not (listp ftp-password-alist
))
53 (or (cdr (cdr (assoc host ftp-password-alist
)))
54 (let ((l ftp-password-alist
))
57 (if (string= (car (cdr (car l
))) user
)
58 (throw 'foo
(cdr (cdr (car l
))))
63 (if (and (listp ftp-password-alist
)
64 (not (string= (cdr tem
) "")))
65 (setq ftp-password-alist
(cons (cons host tem
)
69 (defun ftp-read-file-name (prompt)
71 (while (not (string-match "\\`[ \t]*\\([^ \t:]+\\)[ \t]*:\\(.+\\)\\'" s
))
72 (setq s
(read-string prompt s
)))
73 (list (substring s
(match-beginning 1) (match-end 1))
74 (substring s
(match-beginning 2) (match-end 2)))))
78 (defun ftp-find-file (host file
&optional user password
)
79 "FTP to HOST to get FILE, logging in as USER with password PASSWORD.
80 Interactively, HOST and FILE are specified by reading a string with
81 a colon character separating the host from the filename.
82 USER and PASSWORD are defaulted from the values used when
83 last ftping from HOST (unless password-remembering is disabled).
84 Supply a password of the symbol `t' to override this default
85 (interactively, this is done by giving a prefix arg)"
87 (append (ftp-read-file-name "FTP get host:file: ")
88 (list nil
(not (null current-prefix-arg
)))))
89 (ftp-find-file-or-directory host file t user password
))
92 (defun ftp-list-directory (host file
&optional user password
)
93 "FTP to HOST to list DIRECTORY, logging in as USER with password PASSWORD.
94 Interactively, HOST and FILE are specified by reading a string with
95 a colon character separating the host from the filename.
96 USER and PASSWORD are defaulted from the values used when
97 last ftping from HOST (unless password-remembering is disabled).
98 Supply a password of the symbol `t' to override this default
99 (interactively, this is done by giving a prefix arg)"
101 (append (ftp-read-file-name "FTP get host:directory: ")
102 (list nil
(not (null current-prefix-arg
)))))
103 (ftp-find-file-or-directory host file nil user password
))
105 (defun ftp-find-file-or-directory (host file filep
&optional user password
)
106 "FTP to HOST to get FILE. Third arg is t for file, nil for directory.
107 Log in as USER with PASSWORD. If USER is nil or PASSWORD is nil or t,
108 we prompt for the user name and password."
109 (or (and user password
(not (eq password t
)))
110 (progn (setq user
(read-ftp-user-password host user
(eq password t
))
113 (let ((buffer (get-buffer-create (format "*ftp%s %s:%s*"
114 (if filep
"" "-directory")
118 (case-fold-search nil
))
122 (setq process
(ftp-setup-buffer host file
))
123 (if (setq win
(ftp-login process host user password
))
124 (message "Logged in")
125 (error "Ftp login failed")))
126 (or win
(and process
(delete-process process
)))))
127 (message "Opening %s %s:%s..." (if filep
"file" "directory")
129 (if (ftp-command process
130 (format "%s \"%s\" -\nquit\n" (if filep
"get" "dir")
132 "\\(150\\|125\\).*\n"
134 (progn (forward-line 1)
135 (let ((buffer-read-only nil
))
136 (delete-region (point-min) (point)))
137 (message "Retrieving %s:%s in background. Bye!" host file
)
138 (set-process-sentinel process
139 'ftp-asynchronous-input-sentinel
)
141 (switch-to-buffer buffer
)
142 (let ((buffer-read-only nil
))
143 (insert-before-markers "<<<Ftp lost>>>"))
144 (delete-process process
)
145 (error "Ftp %s:%s lost" host file
)))))
149 (defun ftp-write-file (host file
&optional user password
)
150 "FTP to HOST to write FILE, logging in as USER with password PASSWORD.
151 Interactively, HOST and FILE are specified by reading a string with colon
152 separating the host from the filename.
153 USER and PASSWORD are defaulted from the values used when
154 last ftping from HOST (unless `password-remembering' is disabled).
155 Supply a password of the symbol `t' to override this default
156 (interactively, this is done by giving a prefix arg)"
158 (append (ftp-read-file-name "FTP write host:file: ")
159 (list nil
(not (null current-prefix-arg
)))))
160 (or (and user password
(not (eq password t
)))
161 (progn (setq user
(read-ftp-user-password host user
(eq password t
))
164 (let ((buffer (get-buffer-create (format "*ftp %s:%s*" host file
)))
165 (tmp (make-temp-name "/tmp/emacsftp")))
166 (write-region (point-min) (point-max) tmp
)
169 (make-local-variable 'ftp-temp-file-name
)
170 (setq ftp-temp-file-name tmp
)
171 (let ((process (ftp-setup-buffer host file
))
172 (case-fold-search nil
))
175 (if (setq win
(ftp-login process host user password
))
176 (message "Logged in")
177 (error "Ftp login lost"))
178 (or win
(delete-process process
))))
179 (message "Opening file %s:%s..." host file
)
180 (if (ftp-command process
181 (format "send \"%s\" \"%s\"\nquit\n" tmp file
)
182 "\\(150\\|125\\).*\n"
184 (progn (forward-line 1)
185 (setq foo1
(current-buffer))
186 (let ((buffer-read-only nil
))
187 (delete-region (point-min) (point)))
188 (message "Saving %s:%s in background. Bye!" host file
)
189 (set-process-sentinel process
190 'ftp-asynchronous-output-sentinel
)
192 (switch-to-buffer buffer
)
193 (setq foo2
(current-buffer))
194 (let ((buffer-read-only nil
))
195 (insert-before-markers "<<<Ftp lost>>>"))
196 (delete-process process
)
197 (error "Ftp write %s:%s lost" host file
))))))
200 (defun ftp-setup-buffer (host file
)
202 (and (get-buffer-process (current-buffer))
203 (progn (discard-input)
204 (if (y-or-n-p (format "Kill process \"%s\" in %s? "
205 (process-name (get-buffer-process
207 (buffer-name (current-buffer))))
208 (while (get-buffer-process (current-buffer))
209 (kill-process (get-buffer-process (current-buffer))))
211 ;(buffer-disable-undo (current-buffer))
212 (setq buffer-read-only nil
)
214 (make-local-variable 'ftp-host
)
216 (make-local-variable 'ftp-file
)
218 (setq foo3
(current-buffer))
219 (setq buffer-read-only t
)
220 (start-process "ftp" (current-buffer) "ftp" "-i" "-n" "-g"))
223 (defun ftp-login (process host user password
)
224 (message "FTP logging in as %s@%s..." user host
)
225 (if (ftp-command process
226 (format "open %s\nuser %s %s\n" host user password
)
228 "\\(Connected to \\|220\\|331\\|Remote system type\\|Using.*mode\\|Remember to set\\).*\n")
230 (switch-to-buffer (process-buffer process
))
231 (delete-process process
)
232 (if (listp ftp-password-alist
)
233 (setq ftp-password-alist
(delq (assoc host ftp-password-alist
)
234 ftp-password-alist
)))
237 (defun ftp-command (process command win ignore
)
238 (process-send-string process command
)
241 (cond ;((not (bolp)))
242 ((looking-at "^[0-9]+-")
243 (while (not (re-search-forward "^[0-9]+ " nil t
))
245 (accept-process-output process
)))
248 (goto-char (point-max))
250 ((looking-at "^ftp> \\|^\n")
251 (goto-char (match-end 0)))
253 ;; Ignore status messages whose codes indicate no problem.
255 ((not (search-forward "\n" nil t
))
256 ;; the way asynchronous process-output works with (point)
257 ;; is really really disgusting.
260 (accept-process-output process
)
263 ((looking-at "^[a-z]")
264 ;; Ignore any lines that don't have error codes.
271 (defun ftp-asynchronous-input-sentinel (process msg
)
272 (ftp-sentinel process msg t t
))
273 (defun ftp-synchronous-input-sentinel (process msg
)
274 (ftp-sentinel process msg nil t
))
275 (defun ftp-asynchronous-output-sentinel (process msg
)
276 (ftp-sentinel process msg t nil
))
277 (defun ftp-synchronous-output-sentinel (process msg
)
278 (ftp-sentinel process msg nil nil
))
280 (defun ftp-sentinel (process msg asynchronous input
)
281 (cond ((null (buffer-name (process-buffer process
)))
283 (set-process-buffer process nil
))
284 ((and (eq (process-status process
) 'exit
)
285 (= (process-exit-status process
) 0))
287 (set-buffer (process-buffer process
))
289 (r (if input
"[0-9]+ bytes received in [0-9]+\\.[0-9]+ seconds.*$" "[0-9]+ bytes sent in [0-9]+\\.[0-9]+ seconds.*$")))
290 (goto-char (point-max))
291 (search-backward "226 ")
293 (search-backward "226 "))
295 (setq msg
(concat (format "ftp %s %s:%s done"
296 (if input
"read" "write")
298 (if (re-search-forward r nil t
)
299 (concat ": " (buffer-substring
303 (delete-region p
(point-max))
305 (set-buffer (get-buffer-create "*ftp log*"))
306 (let ((buffer-read-only nil
))
308 ;; Note the preceding let must end here
309 ;; so it doesn't cross the (kill-buffer (current-buffer)).
313 (and (boundp 'ftp-temp-file-name
)
315 (delete-file ftp-temp-file-name
))
317 ;; Kill the temporary buffer which the ftp process
318 ;; puts its output in.
319 (kill-buffer (current-buffer)))
320 ;; You don't want to look at this.
321 (let ((kludge (generate-new-buffer (format "%s:%s (ftp)"
322 ftp-host ftp-file
))))
323 (setq kludge
(prog1 (buffer-name kludge
) (kill-buffer kludge
)))
324 (rename-buffer kludge
)
325 ;; ok, you can look again now.
326 (set-buffer-modified-p nil
)
327 (ftp-setup-write-file-hooks)))
328 (if (and asynchronous
329 ;(waiting-for-user-input-p)
331 (progn (message "%s" msg
)
333 ((memq (process-status process
) '(exit signal
))
335 (set-buffer (process-buffer process
))
336 (setq msg
(format "Ftp died (buffer %s): %s"
337 (buffer-name (current-buffer))
339 (let ((buffer-read-only nil
))
340 (goto-char (point-max))
341 (insert ?
\n ?
\n msg
))
342 (delete-process proc
)
343 (set-buffer (get-buffer-create "*ftp log*"))
344 (let ((buffer-read-only nil
))
345 (goto-char (point-max))
347 (if (waiting-for-user-input-p)
348 (error "%s" msg
))))))
350 (defun ftp-setup-write-file-hooks ()
351 (let ((hooks write-file-hooks
))
352 (make-local-variable 'write-file-hooks
)
353 (setq write-file-hooks
(append write-file-hooks
354 '(ftp-write-file-hook))))
355 (make-local-variable 'revert-buffer-function
)
356 (setq revert-buffer-function
'ftp-revert-buffer
)
357 (setq default-directory
"/tmp/")
358 (setq buffer-file-name
(concat default-directory
360 (buffer-name (current-buffer)))))
361 (setq buffer-read-only nil
))
363 (defun ftp-write-file-hook ()
364 (let ((process (ftp-write-file ftp-host ftp-file
)))
365 (set-process-sentinel process
'ftp-synchronous-output-sentinel
)
366 (message "FTP writing %s:%s..." ftp-host ftp-file
)
367 (while (eq (process-status process
) 'run
)
369 (accept-process-output process
)
371 (set-buffer-modified-p nil
)
372 (message "FTP writing %s:%s...done" ftp-host ftp-file
))
375 (defun ftp-revert-buffer (&rest ignore
)
376 (let ((process (ftp-find-file ftp-host ftp-file
)))
377 (set-process-sentinel process
'ftp-synchronous-input-sentinel
)
378 (message "FTP reverting %s:%s" ftp-host ftp-file
)
379 (while (eq (process-status process
) 'run
)
381 (accept-process-output process
)
383 (and (eq (process-status process
) 'exit
)
384 (= (process-exit-status process
) 0)
385 (set-buffer-modified-p nil
))
386 (message "Reverted")))