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)))))
77 (defun ftp-find-file (host file &optional user password)
78 "FTP to HOST to get FILE, logging in as USER with password PASSWORD.
79 Interactively, HOST and FILE are specified by reading a string with
80 a colon character separating the host from the filename.
81 USER and PASSWORD are defaulted from the values used when
82 last ftping from HOST (unless password-remembering is disabled).
83 Supply a password of the symbol `t' to override this default
84 (interactively, this is done by giving a prefix arg)"
86 (append (ftp-read-file-name "FTP get host:file: ")
87 (list nil (not (null current-prefix-arg)))))
88 (ftp-find-file-or-directory host file t user password))
90 (defun ftp-list-directory (host file &optional user password)
91 "FTP to HOST to list DIRECTORY, logging in as USER with password PASSWORD.
92 Interactively, HOST and FILE are specified by reading a string with
93 a colon character separating the host from the filename.
94 USER and PASSWORD are defaulted from the values used when
95 last ftping from HOST (unless password-remembering is disabled).
96 Supply a password of the symbol `t' to override this default
97 (interactively, this is done by giving a prefix arg)"
99 (append (ftp-read-file-name "FTP get host:directory: ")
100 (list nil (not (null current-prefix-arg)))))
101 (ftp-find-file-or-directory host file nil user password))
103 (defun ftp-find-file-or-directory (host file filep &optional user password)
104 "FTP to HOST to get FILE. Third arg is t for file, nil for directory.
105 Log in as USER with PASSWORD. If USER is nil or PASSWORD is nil or t,
106 we prompt for the user name and password."
107 (or (and user password (not (eq password t)))
108 (progn (setq user (read-ftp-user-password host user (eq password t))
111 (let ((buffer (get-buffer-create (format "*ftp%s %s:%s*"
112 (if filep "" "-directory")
116 (case-fold-search nil))
120 (setq process (ftp-setup-buffer host file))
121 (if (setq win (ftp-login process host user password))
122 (message "Logged in")
123 (error "Ftp login failed")))
124 (or win (and process (delete-process process)))))
125 (message "Opening %s %s:%s..." (if filep "file" "directory")
127 (if (ftp-command process
128 (format "%s \"%s\" -\nquit\n" (if filep "get" "dir")
130 "\\(150\\|125\\).*\n"
132 (progn (forward-line 1)
133 (let ((buffer-read-only nil))
134 (delete-region (point-min) (point)))
135 (message "Retrieving %s:%s in background. Bye!" host file)
136 (set-process-sentinel process
137 'ftp-asynchronous-input-sentinel)
139 (switch-to-buffer buffer)
140 (let ((buffer-read-only nil))
141 (insert-before-markers "<<<Ftp lost>>>"))
142 (delete-process process)
143 (error "Ftp %s:%s lost" host file)))))
146 (defun ftp-write-file (host file &optional user password)
147 "FTP to HOST to write FILE, logging in as USER with password PASSWORD.
148 Interactively, HOST and FILE are specified by reading a string with colon
149 separating the host from the filename.
150 USER and PASSWORD are defaulted from the values used when
151 last ftping from HOST (unless password-remembering is disabled).
152 Supply a password of the symbol `t' to override this default
153 (interactively, this is done by giving a prefix arg)"
155 (append (ftp-read-file-name "FTP write host:file: ")
156 (list nil (not (null current-prefix-arg)))))
157 (or (and user password (not (eq password t)))
158 (progn (setq user (read-ftp-user-password host user (eq password t))
161 (let ((buffer (get-buffer-create (format "*ftp %s:%s*" host file)))
162 (tmp (make-temp-name "/tmp/emacsftp")))
163 (write-region (point-min) (point-max) tmp)
166 (make-local-variable 'ftp-temp-file-name)
167 (setq ftp-temp-file-name tmp)
168 (let ((process (ftp-setup-buffer host file))
169 (case-fold-search nil))
172 (if (setq win (ftp-login process host user password))
173 (message "Logged in")
174 (error "Ftp login lost"))
175 (or win (delete-process process))))
176 (message "Opening file %s:%s..." host file)
177 (if (ftp-command process
178 (format "send \"%s\" \"%s\"\nquit\n" tmp file)
181 (progn (forward-line 1)
182 (setq foo1 (current-buffer))
183 (let ((buffer-read-only nil))
184 (delete-region (point-min) (point)))
185 (message "Saving %s:%s in background. Bye!" host file)
186 (set-process-sentinel process
187 'ftp-asynchronous-output-sentinel)
189 (switch-to-buffer buffer)
190 (setq foo2 (current-buffer))
191 (let ((buffer-read-only nil))
192 (insert-before-markers "<<<Ftp lost>>>"))
193 (delete-process process)
194 (error "Ftp write %s:%s lost" host file))))))
197 (defun ftp-setup-buffer (host file)
199 (and (get-buffer-process (current-buffer))
200 (progn (discard-input)
201 (if (y-or-n-p (format "Kill process \"%s\" in %s? "
202 (process-name (get-buffer-process
204 (buffer-name (current-buffer))))
205 (while (get-buffer-process (current-buffer))
206 (kill-process (get-buffer-process (current-buffer))))
208 ;(buffer-disable-undo (current-buffer))
209 (setq buffer-read-only nil)
211 (make-local-variable 'ftp-host)
213 (make-local-variable 'ftp-file)
215 (setq foo3 (current-buffer))
216 (setq buffer-read-only t)
217 (start-process "ftp" (current-buffer) "ftp" "-i" "-n" "-g"))
220 (defun ftp-login (process host user password)
221 (message "FTP logging in as %s@%s..." user host)
222 (if (ftp-command process
223 (format "open %s\nuser %s %s\n" host user password)
225 "\\(Connected to \\|220\\|331\\|Remote system type\\|Using.*mode\\|Remember to set\\).*\n")
227 (switch-to-buffer (process-buffer process))
228 (delete-process process)
229 (if (listp ftp-password-alist)
230 (setq ftp-password-alist (delq (assoc host ftp-password-alist)
231 ftp-password-alist)))
234 (defun ftp-command (process command win ignore)
235 (process-send-string process command)
238 (cond ;((not (bolp)))
240 (goto-char (point-max))
242 ((looking-at "^ftp> \\|^\n")
243 (goto-char (match-end 0)))
246 ((not (search-forward "\n" nil t))
247 ;; the way asynchronous process-output works with (point)
248 ;; is really really disgusting.
251 (accept-process-output process)
259 (defun ftp-asynchronous-input-sentinel (process msg)
260 (ftp-sentinel process msg t t))
261 (defun ftp-synchronous-input-sentinel (process msg)
262 (ftp-sentinel process msg nil t))
263 (defun ftp-asynchronous-output-sentinel (process msg)
264 (ftp-sentinel process msg t nil))
265 (defun ftp-synchronous-output-sentinel (process msg)
266 (ftp-sentinel process msg nil nil))
268 (defun ftp-sentinel (process msg asynchronous input)
269 (cond ((null (buffer-name (process-buffer process)))
271 (set-process-buffer process nil))
272 ((and (eq (process-status process) 'exit)
273 (= (process-exit-status process) 0))
275 (set-buffer (process-buffer process))
277 (r (if input "[0-9]+ bytes received in [0-9]+\\.[0-9]+ seconds.*$" "[0-9]+ bytes sent in [0-9]+\\.[0-9]+ seconds.*$")))
278 (let ((buffer-read-only nil))
279 (goto-char (point-max))
280 (search-backward "226 ")
282 (search-backward "226 "))
284 (setq msg (concat (format "ftp %s %s:%s done"
285 (if input "read" "write")
287 (if (re-search-forward r nil t)
288 (concat ": " (buffer-substring
292 (delete-region p (point-max))
294 (set-buffer (get-buffer-create "*ftp log*"))
295 (let ((buffer-read-only nil))
297 ;; Note the preceding let must end here
298 ;; so it doesn't cross the (kill-buffer (current-buffer)).
302 (and (boundp 'ftp-temp-file-name)
304 (delete-file ftp-temp-file-name))
306 ;; Kill the temporary buffer which the ftp process
307 ;; puts its output in.
308 (kill-buffer (current-buffer)))
309 ;; You don't want to look at this.
310 (let ((kludge (generate-new-buffer (format "%s:%s (ftp)"
311 ftp-host ftp-file))))
312 (setq kludge (prog1 (buffer-name kludge) (kill-buffer kludge)))
313 (rename-buffer kludge)
314 ;; ok, you can look again now.
315 (set-buffer-modified-p nil)
316 (ftp-setup-write-file-hooks)))
317 (if (and asynchronous
318 ;(waiting-for-user-input-p)
320 (progn (message "%s" msg)
322 ((memq (process-status process) '(exit signal))
324 (set-buffer (process-buffer process))
325 (setq msg (format "Ftp died (buffer %s): %s"
326 (buffer-name (current-buffer))
328 (let ((buffer-read-only nil))
329 (goto-char (point-max))
330 (insert ?\n ?\n msg))
331 (delete-process proc)
332 (set-buffer (get-buffer-create "*ftp log*"))
333 (let ((buffer-read-only nil))
334 (goto-char (point-max))
336 (if (waiting-for-user-input-p)
337 (error "%s" msg))))))
339 (defun ftp-setup-write-file-hooks ()
340 (let ((hooks write-file-hooks))
341 (make-local-variable 'write-file-hooks)
342 (setq write-file-hooks (append write-file-hooks
343 '(ftp-write-file-hook))))
344 (make-local-variable 'revert-buffer-function)
345 (setq revert-buffer-function 'ftp-revert-buffer)
346 (setq default-directory "/tmp/")
347 (setq buffer-file-name (concat default-directory
349 (buffer-name (current-buffer)))))
350 (setq buffer-read-only nil))
352 (defun ftp-write-file-hook ()
353 (let ((process (ftp-write-file ftp-host ftp-file)))
354 (set-process-sentinel process 'ftp-synchronous-output-sentinel)
355 (message "FTP writing %s:%s..." ftp-host ftp-file)
356 (while (eq (process-status process) 'run)
358 (accept-process-output process)
360 (set-buffer-modified-p nil)
361 (message "FTP writing %s:%s...done" ftp-host ftp-file))
364 (defun ftp-revert-buffer (&rest ignore)
365 (let ((process (ftp-find-file ftp-host ftp-file)))
366 (set-process-sentinel process 'ftp-synchronous-input-sentinel)
367 (message "FTP reverting %s:%s" ftp-host ftp-file)
368 (while (eq (process-status process) 'run)
370 (accept-process-output process)
372 (and (eq (process-status process) 'exit)
373 (= (process-exit-status process) 0)
374 (set-buffer-modified-p nil))
375 (message "Reverted")))