ange-ftp.el 236 KB
Newer Older
Gerd Moellmann's avatar
Gerd Moellmann committed
1 2
;;; ange-ftp.el --- transparent FTP support for GNU Emacs

3
;; Copyright (C) 1989-1996, 1998, 2000-2013 Free Software Foundation, Inc.
Gerd Moellmann's avatar
Gerd Moellmann committed
4 5 6 7 8 9 10

;; Author: Andy Norman (ange@hplb.hpl.hp.com)
;; Maintainer: FSF
;; Keywords: comm

;; This file is part of GNU Emacs.

11
;; GNU Emacs is free software: you can redistribute it and/or modify
Gerd Moellmann's avatar
Gerd Moellmann committed
12
;; it under the terms of the GNU General Public License as published by
13 14
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
Gerd Moellmann's avatar
Gerd Moellmann committed
15 16 17 18 19 20 21

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
22
;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
Gerd Moellmann's avatar
Gerd Moellmann committed
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48

;;; Commentary:

;; This package attempts to make accessing files and directories using FTP
;; from within GNU Emacs as simple and transparent as possible.  A subset of
;; the common file-handling routines are extended to interact with FTP.

;; Usage:
;;
;; Some of the common GNU Emacs file-handling operations have been made
;; FTP-smart.  If one of these routines is given a filename that matches
;; '/user@host:name' then it will spawn an FTP process connecting to machine
;; 'host' as account 'user' and perform its operation on the file 'name'.
;;
;; For example: if find-file is given a filename of:
;;
;;   /ange@anorman:/tmp/notes
;;
;; then ange-ftp spawns an FTP process, connect to the host 'anorman' as
;; user 'ange', get the file '/tmp/notes' and pop up a buffer containing the
;; contents of that file as if it were on the local filesystem.  If ange-ftp
;; needs a password to connect then it reads one in the echo area.

;; Extended filename syntax:
;;
;; The default extended filename syntax is '/user@host:name', where the
Paul Eggert's avatar
Paul Eggert committed
49
;; 'user@' part may be omitted.  This syntax can be customized to a certain
Gerd Moellmann's avatar
Gerd Moellmann committed
50 51
;; extent by changing ange-ftp-name-format.  There are limitations.
;; The `host' part has an optional suffix `#port' which may be used to
Paul Eggert's avatar
Paul Eggert committed
52
;; specify a non-default port number for the connection.
Gerd Moellmann's avatar
Gerd Moellmann committed
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
;;
;; If the user part is omitted then ange-ftp generates a default user
;; instead whose value depends on the variable ange-ftp-default-user.

;; Passwords:
;;
;; A password is required for each host/user pair.  Ange-ftp reads passwords
;; as needed.  You can also specify a password with ange-ftp-set-passwd, or
;; in a *valid* ~/.netrc file.

;; Passwords for user "anonymous":
;;
;; Passwords for the user "anonymous" (or "ftp") are handled
;; specially.  The variable `ange-ftp-generate-anonymous-password'
;; controls what happens: if the value of this variable is a string,
;; then this is used as the password; if non-nil (the default), then
;; the value of `user-mail-address' is used; if nil then the user
;; is prompted for a password as normal.

;; "Dumb" UNIX hosts:
;;
;; The FTP servers on some UNIX machines have problems if the 'ls' command is
;; used.
;;
;; The routine ange-ftp-add-dumb-unix-host can be called to tell ange-ftp to
;; limit itself to the DIR command and not 'ls' for a given UNIX host.  Note
;; that this change will take effect for the current GNU Emacs session only.
;; See below for a discussion of non-UNIX hosts.  If a large number of
;; machines with similar hostnames have this problem then it is easier to set
82
;; the value of ange-ftp-dumb-unix-host-regexp in your init file.  ange-ftp
Gerd Moellmann's avatar
Gerd Moellmann committed
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
;; is unable to automatically recognize dumb unix hosts.

;; File name completion:
;;
;; Full file-name completion is supported on UNIX, VMS, CMS, and MTS hosts.
;; To do filename completion, ange-ftp needs a listing from the remote host.
;; Therefore, for very slow connections, it might not save any time.

;; FTP processes:
;;
;; When ange-ftp starts up an FTP process, it leaves it running for speed
;; purposes.  Some FTP servers will close the connection after a period of
;; time, but ange-ftp should be able to quietly reconnect the next time that
;; the process is needed.
;;
;; Killing the "*ftp user@host*" buffer also kills the ftp process.
;; This should not cause ange-ftp any grief.

;; Binary file transfers:
;;
;; By default ange-ftp transfers files in ASCII mode.  If a file being
;; transferred matches the value of ange-ftp-binary-file-name-regexp then
;; binary mode is used for that transfer.

;; Account passwords:
;;
;; Some FTP servers require an additional password which is sent by the
;; ACCOUNT command.  ange-ftp partially supports this by allowing the user to
;; specify an account password by either calling ange-ftp-set-account, or by
;; specifying an account token in the .netrc file.  If the account password
;; is set by either of these methods then ange-ftp will issue an ACCOUNT
;; command upon starting the FTP process.

;; Preloading:
;;
;; ange-ftp can be preloaded, but must be put in the site-init.el file and
;; not the site-load.el file in order for the documentation strings for the
;; functions being overloaded to be available.

;; Status reports:
;;
;; Most ange-ftp commands that talk to the FTP process output a status
;; message on what they are doing.  In addition, ange-ftp can take advantage
;; of the FTP client's HASH command to display the status of transferring
;; files and listing directories.  See the documentation for the variables
;; ange-ftp-{ascii,binary}-hash-mark-size, ange-ftp-send-hash and
;; ange-ftp-process-verbose for more details.

;; Gateways:
;;
;; Sometimes it is necessary for the FTP process to be run on a different
;; machine than the machine running GNU Emacs.  This can happen when the
;; local machine has restrictions on what hosts it can access.
;;
;; ange-ftp has support for running the ftp process on a different (gateway)
;; machine.  The way it works is as follows:
;;
;; 1) Set the variable 'ange-ftp-gateway-host' to the name of a machine
;;    that doesn't have the access restrictions.
;;
;; 2) Set the variable 'ange-ftp-local-host-regexp' to a regular expression
;;    that matches hosts that can be contacted from running a local ftp
;;    process, but fails to match hosts that can't be accessed locally.  For
;;    example:
;;
;;    "\\.hp\\.com$\\|^[^.]*$"
;;
;;    will match all hosts that are in the .hp.com domain, or don't have an
;;    explicit domain in their name, but will fail to match hosts with
;;    explicit domains or that are specified by their ip address.
;;
;; 3) Using NFS and symlinks, make sure that there is a shared directory with
;;    the *same* name between the local machine and the gateway machine.
;;    This directory is necessary for temporary files created by ange-ftp.
;;
;; 4) Set the variable 'ange-ftp-gateway-tmp-name-template' to the name of
;;    this directory plus an identifying filename prefix.  For example:
;;
;;    "/nfs/hplose/ange/ange-ftp"
;;
;;    where /nfs/hplose/ange is a directory that is shared between the
;;    gateway machine and the local machine.
;;
;; The simplest way of getting a ftp process running on the gateway machine
;; is if you can spawn a remote shell using either 'rsh' or 'remsh'.  If you
;; can't do this for some reason such as security then points 7 onwards will
;; discuss an alternative approach.
;;
;; 5) Set the variable ange-ftp-gateway-program to the name of the remote
;;    shell process such as 'remsh' or 'rsh' if the default isn't correct.
;;
;; 6) Set the variable ange-ftp-gateway-program-interactive to nil if it
;;    isn't already.  This tells ange-ftp that you are using a remote shell
;;    rather than logging in using telnet or rlogin.
;;
;; That should be all you need to allow ange-ftp to spawn a ftp process on
;; the gateway machine.  If you have to use telnet or rlogin to get to the
;; gateway machine then follow the instructions below.
;;
;; 7) Set the variable ange-ftp-gateway-program to the name of the program
;;    that lets you log onto the gateway machine.  This may be something like
;;    telnet or rlogin.
;;
;; 8) Set the variable ange-ftp-gateway-prompt-pattern to a regular
;;    expression that matches the prompt you get when you login to the
;;    gateway machine.  Be very specific here; this regexp must not match
;;    *anything* in your login banner except this prompt.
;;    shell-prompt-pattern is far too general as it appears to match some
;;    login banners from Sun machines.  For example:
;;
;;    "^$*$ *"
;;
;; 9) Set the variable ange-ftp-gateway-program-interactive to 't' to let
;;    ange-ftp know that it has to "hand-hold" the login to the gateway
;;    machine.
;;
;; 10) Set the variable ange-ftp-gateway-setup-term-command to a UNIX command
;;     that will put the pty connected to the gateway machine into a
;;     no-echoing mode, and will strip off carriage-returns from output from
;;     the gateway machine.  For example:
;;
;;     "stty -onlcr -echo"
;;
;;     will work on HP-UX machines, whereas:
;;
;;     "stty -echo nl"
;;
;;     appears to work for some Sun machines.
;;
;; That's all there is to it.

;; Smart gateways:
;;
;; If you have a "smart" ftp program that allows you to issue commands like
;; "USER foo@bar" which do nice proxy things, then look at the variables
;; ange-ftp-smart-gateway and ange-ftp-smart-gateway-port.
;;
;; Otherwise, if there is an alternate ftp program that implements proxy in
;; a transparent way (i.e. w/o specifying the proxy host), that will
;; connect you directly to the desired destination host:
;; Set ange-ftp-gateway-ftp-program-name to that program's name.
;; Set ange-ftp-local-host-regexp to a value as stated earlier on.
;; Leave ange-ftp-gateway-host set to nil.
;; Set ange-ftp-smart-gateway to t.

;; Tips for using ange-ftp:
;;
;; 1. For dired to work on a host which marks symlinks with a trailing @ in
;;    an ls -alF listing, you need to (setq dired-ls-F-marks-symlinks t).
;;    Most UNIX systems do not do this, but ULTRIX does. If you think that
;;    there is a chance you might connect to an ULTRIX machine (such as
;;    prep.ai.mit.edu), then set this variable accordingly.  This will have
;;    the side effect that dired will have problems with symlinks whose names
;;    end in an @.  If you get yourself into this situation then editing
;;    dired's ls-switches to remove "F", will temporarily fix things.
;;
;; 2. If you know that you are connecting to a certain non-UNIX machine
;;    frequently, and ange-ftp seems to be unable to guess its host-type,
;;    then setting the appropriate host-type regexp
;;    (ange-ftp-vms-host-regexp, ange-ftp-mts-host-regexp, or
;;    ange-ftp-cms-host-regexp) accordingly should help. Also, please report
;;    ange-ftp's inability to recognize the host-type as a bug.
;;
;; 3. For slow connections, you might get "listing unreadable" error
;;    messages, or get an empty buffer for a file that you know has something
;;    in it. The solution is to increase the value of ange-ftp-retry-time.
;;    Its default value is 5 which is plenty for reasonable connections.
;;    However, for some transatlantic connections I set this to 20.
;;
;; 4. Beware of compressing files on non-UNIX hosts. Ange-ftp will do it by
;;    copying the file to the local machine, compressing it there, and then
;;    sending it back. Binary file transfers between machines of different
;;    architectures can be a risky business. Test things out first on some
;;    test files. See "Bugs" below. Also, note that ange-ftp copies files by
;;    moving them through the local machine. Again, be careful when doing
;;    this with binary files on non-Unix machines.
;;
;; 5. Beware that dired over ftp will use your setting of dired-no-confirm
;;    (list of dired commands for which confirmation is not asked).  You
;;    might want to reconsider your setting of this variable, because you
;;    might want confirmation for more commands on remote direds than on
;;    local direds. For example, I strongly recommend that you not include
;;    compress and uncompress in this list. If there is enough demand it
;;    might be a good idea to have an alist ange-ftp-dired-no-confirm of
;;    pairs ( TYPE . LIST ), where TYPE is an operating system type and LIST
;;    is a list of commands for which confirmation would be suppressed.  Then
;;    remote dired listings would take their (buffer-local) value of
;;    dired-no-confirm from this alist. Who votes for this?

;; ---------------------------------------------------------------------
;; Non-UNIX support:
;; ---------------------------------------------------------------------

;; VMS support:
;;
278 279 280 281
;; Ange-ftp has full support for VMS hosts.  It should be able to
;; automatically recognize any VMS machine. However, if it fails to do
;; this, you can use the command ange-ftp-add-vms-host.  Also, you can
;; set the variable ange-ftp-vms-host-regexp in your init file.  We
Gerd Moellmann's avatar
Gerd Moellmann committed
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
;; would be grateful if you would report any failures to automatically
;; recognize a VMS host as a bug.
;;
;; Filename Syntax:
;;
;; For ease of *implementation*, the user enters the VMS filename syntax in a
;; UNIX-y way.  For example:
;;  PUB$:[ANONYMOUS.SDSCPUB.NEXT]README.TXT;1
;; would be entered as:
;;  /PUB$$:/ANONYMOUS/SDSCPUB/NEXT/README.TXT;1
;; i.e. to log in as anonymous on ymir.claremont.edu and grab the file:
;;  [.CSV.POLICY]RULES.MEM
;; you would type:
;;  C-x C-f /anonymous@ymir.claremont.edu:CSV/POLICY/RULES.MEM
;;
Juanma Barranquero's avatar
Juanma Barranquero committed
297
;; A valid VMS filename is of the form: FILE.TYPE;##
Gerd Moellmann's avatar
Gerd Moellmann committed
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
;; where FILE can be up to 39 characters
;;       TYPE can be up to 39 characters
;;       ## is a version number (an integer between 1 and 32,767)
;; Valid characters in FILE and TYPE are A-Z 0-9 _ - $
;; $ cannot begin a filename, and - cannot be used as the first or last
;; character.
;;
;; Tips:
;; 1. Although VMS is not case sensitive, EMACS running under UNIX is.
;;    Therefore, to access a VMS file, you must enter the filename with upper
;;    case letters.
;; 2. To access the latest version of file under VMS, you use the filename
;;    without the ";" and version number. You should always edit the latest
;;    version of a file. If you want to edit an earlier version, copy it to a
;;    new file first. This has nothing to do with ange-ftp, but is simply
;;    good VMS operating practice. Therefore, to edit FILE.TXT;3 (say 3 is
;;    latest version), do C-x C-f /ymir.claremont.edu:FILE.TXT. If you
;;    inadvertently do C-x C-f /ymir.claremont.edu:FILE.TXT;3, you will find
;;    that VMS will not allow you to save the file because it will refuse to
;;    overwrite FILE.TXT;3, but instead will want to create FILE.TXT;4, and
;;    attach the buffer to this file. To get out of this situation, M-x
;;    write-file /ymir.claremont.edu:FILE.TXT will attach the buffer to
;;    latest version of the file. For this reason, in dired "f"
;;    (dired-find-file), always loads the file sans version, whereas "v",
;;    (dired-view-file), always loads the explicit version number. The
;;    reasoning being that it reasonable to view old versions of a file, but
;;    not to edit them.
;; 3. EMACS has a feature in which it does environment variable substitution
;;    in filenames. Therefore, to enter a $ in a filename, you must quote it
;;    by typing $$.

;; MTS support:
;;
;; Ange-ftp has full support for hosts running
;; the Michigan terminal system.  It should be able to automatically
;; recognize any MTS machine. However, if it fails to do this, you can use
;; the command ange-ftp-add-mts-host.  As well, you can set the variable
335
;; ange-ftp-mts-host-regexp in your init file. We would be grateful if you
Gerd Moellmann's avatar
Gerd Moellmann committed
336 337 338
;; would report any failures to automatically recognize a MTS host as a bug.
;;
;; Filename syntax:
Sam Steingold's avatar
Sam Steingold committed
339
;;
Gerd Moellmann's avatar
Gerd Moellmann committed
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
;; MTS filenames are entered in a UNIX-y way. For example, if your account
;; was YYYY, the file FILE in the account XXXX: on mtsg.ubc.ca would be
;; entered as
;;   /YYYY@mtsg.ubc.ca:/XXXX:/FILE
;; In other words, MTS accounts are treated as UNIX directories. Of course,
;; to access a file in another account, you must have access permission for
;; it.  If FILE were in your own account, then you could enter it in a
;; relative name fashion as
;;   /YYYY@mtsg.ubc.ca:FILE
;; MTS filenames can be up to 12 characters. Like UNIX, the structure of the
;; filename does not contain a TYPE (i.e. it can have as many "."'s as you
;; like.) MTS filenames are always in upper case, and hence be sure to enter
;; them as such! MTS is not case sensitive, but an EMACS running under UNIX
;; is.

;; CMS support:
Sam Steingold's avatar
Sam Steingold committed
356
;;
Gerd Moellmann's avatar
Gerd Moellmann committed
357 358 359 360
;; Ange-ftp has full support for hosts running
;; CMS.  It should be able to automatically recognize any CMS machine.
;; However, if it fails to do this, you can use the command
;; ange-ftp-add-cms-host.  As well, you can set the variable
361
;; ange-ftp-cms-host-regexp in your init file. We would be grateful if you
Gerd Moellmann's avatar
Gerd Moellmann committed
362
;; would report any failures to automatically recognize a CMS host as a bug.
Sam Steingold's avatar
Sam Steingold committed
363
;;
Gerd Moellmann's avatar
Gerd Moellmann committed
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
;; Filename syntax:
;;
;; CMS filenames are entered in a UNIX-y way. In otherwords, minidisks are
;; treated as UNIX directories. For example to access the file READ.ME in
;; minidisk *.311 on cuvmb.cc.columbia.edu, you would enter
;;   /anonymous@cuvmb.cc.columbia.edu:/*.311/READ.ME
;; If *.301 is the default minidisk for this account, you could access
;; FOO.BAR on this minidisk as
;;   /anonymous@cuvmb.cc.columbia.edu:FOO.BAR
;; CMS filenames are of the form FILE.TYPE, where both FILE and TYPE can be
;; up to 8 characters. Again, beware that CMS filenames are always upper
;; case, and hence must be entered as such.
;;
;; Tips:
;; 1. CMS machines, with the exception of anonymous accounts, nearly always
;;    need an account password. To have ange-ftp send an account password,
;;    you can either include it in your .netrc file, or use
;;    ange-ftp-set-account.
;; 2. Ange-ftp cannot send "write passwords" for a minidisk. Hopefully, we
;;    can fix this.
;;
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
;; BS2000 support:
;;
;; Ange-ftp has full support for BS2000 hosts.  It should be able to
;; automatically recognize any BS2000 machine. However, if it fails to
;; do this, you can use the command ange-ftp-add-bs2000-host.  As well,
;; you can set the variable ange-ftp-bs2000-host-regexp in your .emacs
;; file. We would be grateful if you would report any failures to auto-
;; matically recognize a BS2000 host as a bug.
;;
;; If you want to access the POSIX subsystem on BS2000 you MUST use
;; command ange-ftp-add-bs2000-posix-host for that particular
;; hostname.  ange-ftp can't decide if you want to access the native
;; filesystem or the POSIX filesystem, so it accesses the native
;; filesystem by default.  And if you have an ASCII filesystem in
;; your BS2000 POSIX subsystem you must use
;; ange-ftp-binary-file-name-regexp to access its files.
;;
;; Filename Syntax:
;;
;; For ease of *implementation*, the user enters the BS2000 filename
;; syntax in a UNIX-y way.  For example:
;;  :PUB:$PUBLIC.ANONYMOUS.SDSCPUB.NEXT.README.TXT
;; would be entered as:
;;  /:PUB:/$$PUBLIC/ANONYMOUS.SDSCPUB.NEXT.README.TXT
409
;; You don't have to type pubset and account, if they have default values,
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
;; i.e. to log in as anonymous on bs2000.anywhere.com and grab the file
;; IMPORTANT.TEXT.ON.BS2000 on the default pubset X on userid PUBLIC
;; (there are only 8 characters in a valid username), you could type:
;;  C-x C-f /public@bs2000.anywhere.com:/IMPORTANT.TEXT.ON.BS2000
;; or
;;  C-x C-f /anonym@bs2000.anywhere.com:/:X:/$$PUBLIC/IMPORTANT.TEXT.ON.BS2000
;;
;; If X is not your default pubset, you could add it as 'subdirectory' (BS2000
;; has a flat architecture) with the command
;; (setq ange-ftp-bs2000-additional-pubsets '(":X:"))
;; and then you could type:
;;  C-x C-f /anonym@bs2000.anywhere.com:/:X:/IMPORTANT.TEXT.ON.BS2000
;;
;; Valid characters in an BS2000 filename are A-Z 0-9 $ # @ . -
;; If the first character in a filename is # or @, this is replaced with
;; ange-ftp-bs2000-special-prefix because names starting with # or @
;; are reserved for temporary files.
;; This is especially important for auto-save files.
428
;; Valid file generations are ending with ([+|-|*]0-9...) .
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
;; File generations are not supported yet!
;; A filename must at least contain one character (A-Z) and cannot be longer
;; than 41 characters.
;;
;; Tips:
;; 1. Although BS2000 is not case sensitive, EMACS running under UNIX is.
;;    Therefore, to access a BS2000 file, you must enter the filename with
;;    upper case letters.
;; 2. EMACS has a feature in which it does environment variable substitution
;;    in filenames. Therefore, to enter a $ in a filename, you must quote it
;;    by typing $$.
;; 3. BS2000 machines, with the exception of anonymous accounts, nearly
;;    always need an account password. To have ange-ftp send an account
;;    password, you can either include it in your .netrc file, or use
;;    ange-ftp-set-account.
;;
Gerd Moellmann's avatar
Gerd Moellmann committed
445 446 447
;; ------------------------------------------------------------------
;; Bugs:
;; ------------------------------------------------------------------
Sam Steingold's avatar
Sam Steingold committed
448
;;
Gerd Moellmann's avatar
Gerd Moellmann committed
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
;; 1. Umask problems:
;;    Be warned that files created by using ange-ftp will take account of the
;;    umask of the ftp daemon process rather than the umask of the creating
;;    user.  This is particularly important when logging in as the root user.
;;    The way that I tighten up the ftp daemon's umask under HP-UX is to make
;;    sure that the umask is changed to 027 before I spawn /etc/inetd.  I
;;    suspect that there is something similar on other systems.
;;
;; 2. Some combinations of FTP clients and servers break and get out of sync
;;    when asked to list a non-existent directory.  Some of the ai.mit.edu
;;    machines cause this problem for some FTP clients. Using
;;    ange-ftp-kill-ftp-process can restart the ftp process, which
;;    should get things back in sync.
;;
;; 3. Ange-ftp does not check to make sure that when creating a new file,
;;    you provide a valid filename for the remote operating system.
;;    If you do not, then the remote FTP server will most likely
;;    translate your filename in some way. This may cause ange-ftp to
;;    get confused about what exactly is the name of the file. The
;;    most common causes of this are using lower case filenames on systems
;;    which support only upper case, and using filenames which are too
;;    long.
;;
;; 4. Null (blank) passwords confuse both ange-ftp and some FTP daemons.
;;
;; 5. Ange-ftp likes to use pty's to talk to its FTP processes.  If GNU Emacs
;;    for some reason creates a FTP process that only talks via pipes then
;;    ange-ftp won't be getting the information it requires at the time that
;;    it wants it since pipes flush at different times to pty's.  One
;;    disgusting way around this problem is to talk to the FTP process via
;;    rlogin which does the 'right' things with pty's.
;;
;; 6. For CMS support, we send too many cd's. Since cd's are cheap, I haven't
;;    worried about this too much. Eventually, we should have some caching
;;    of the current minidisk.
Sam Steingold's avatar
Sam Steingold committed
484
;;
Gerd Moellmann's avatar
Gerd Moellmann committed
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
;; 7. Some CMS machines do not assign a default minidisk when you ftp them as
;;    anonymous. It is then necessary to guess a valid minidisk name, and cd
;;    to it. This is (understandably) beyond ange-ftp.
;;
;; 8. Remote to remote copying of files on non-Unix machines can be risky.
;;    Depending on the variable ange-ftp-binary-file-name-regexp, ange-ftp
;;    will use binary mode for the copy. Between systems of different
;;    architecture, this still may not be enough to guarantee the integrity
;;    of binary files. Binary file transfers from VMS machines are
;;    particularly problematical. Should ange-ftp-binary-file-name-regexp be
;;    an alist of OS type, regexp pairs?
;;
;; 9. The code to do compression of files over ftp is not as careful as it
;;    should be. It deletes the old remote version of the file, before
;;    actually checking if the local to remote transfer of the compressed
;;    file succeeds. Of course to delete the original version of the file
;;    after transferring the compressed version back is also dangerous,
;;    because some OS's have severe restrictions on the length of filenames,
;;    and when the compressed version is copied back the "-Z" or ".Z" may be
;;    truncated. Then, ange-ftp would delete the only remaining version of
;;    the file.  Maybe ange-ftp should make backups when it compresses files
;;    (of course, the backup "~" could also be truncated off, sigh...).
;;    Suggestions?
;;
;; 10. If a dir listing is attempted for an empty directory on (at least
;;     some) VMS hosts, an ftp error is given. This is really an ftp bug, and
;;     I don't know how to get ange-ftp work to around it.
;;
;; 11. Bombs on filenames that start with a space. Deals well with filenames
;;     containing spaces, but beware that the remote ftpd may not like them
;;     much.
;;
;; 12. The dired support for non-Unix-like systems does not currently work.
;;     It needs to be reimplemented by modifying the parse-...-listing
;;	functions to convert the directory listing to ls -l format.
Sam Steingold's avatar
Sam Steingold committed
520
;;
Gerd Moellmann's avatar
Gerd Moellmann committed
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545
;; 13. The famous @ bug. As mentioned above in TIPS, ULTRIX marks symlinks
;;     with a trailing @ in a ls -alF listing. In order to account for this
;;     ange-ftp looks to chop trailing @'s off of symlink names when it is
;;     parsing a listing with the F switch. This will cause ange-ftp to
;;     incorrectly get the name of a symlink on a non-ULTRIX host if its name
;;     ends in an @. ange-ftp will correct itself if you take F out of the
;;     dired ls switches (C-u s will allow you to edit the switches). The
;;     dired buffer will be automatically reverted, which will allow ange-ftp
;;     to fix its files hashtable.  A cookie to anyone who can think of a
;;     fast, sure-fire way to recognize ULTRIX over ftp.

;; If you find any bugs or problems with this package, PLEASE either e-mail
;; the above author, or send a message to the ange-ftp-lovers mailing list
;; below.  Ideas and constructive comments are especially welcome.

;; ange-ftp-lovers:
;;
;; ange-ftp has its own mailing list modestly called ange-ftp-lovers.  All
;; users of ange-ftp are welcome to subscribe (see below) and to discuss
;; aspects of ange-ftp.  New versions of ange-ftp are posted periodically to
;; the mailing list.

;; To [un]subscribe to ange-ftp-lovers, or to report mailer problems with the
;; list, please mail one of the following addresses:
;;
546
;;     ange-ftp-lovers-request@hplb.hpl.hp.com
Gerd Moellmann's avatar
Gerd Moellmann committed
547 548 549 550 551
;;
;; Please don't forget the -request part.
;;
;; For mail to be posted directly to ange-ftp-lovers, send to one of the
;; following addresses:
Sam Steingold's avatar
Sam Steingold committed
552
;;
553
;;     ange-ftp-lovers@hplb.hpl.hp.com
Gerd Moellmann's avatar
Gerd Moellmann committed
554 555 556 557 558 559 560 561 562 563 564 565 566 567 568
;;
;; Alternatively, there is a mailing list that only gets announcements of new
;; ange-ftp releases.  This is called ange-ftp-lovers-announce, and can be
;; subscribed to by e-mailing to the -request address as above.  Please make
;; it clear in the request which mailing list you wish to join.

;; -----------------------------------------------------------
;; Technical information on this package:
;; -----------------------------------------------------------

;; ange-ftp works by putting a handler on file-name-handler-alist
;; which is called by many primitives, and a few non-primitives,
;; whenever they see a file name of the appropriate sort.

;; Checklist for adding non-UNIX support for TYPE
Sam Steingold's avatar
Sam Steingold committed
569
;;
Gerd Moellmann's avatar
Gerd Moellmann committed
570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624
;; The following functions may need TYPE versions:
;; (not all functions will be needed for every OS)
;;
;; ange-ftp-fix-name-for-TYPE
;; ange-ftp-fix-dir-name-for-TYPE
;; ange-ftp-TYPE-host
;; ange-ftp-TYPE-add-host
;; ange-ftp-parse-TYPE-listing
;; ange-ftp-TYPE-delete-file-entry
;; ange-ftp-TYPE-add-file-entry
;; ange-ftp-TYPE-file-name-as-directory
;; ange-ftp-TYPE-make-compressed-filename
;; ange-ftp-TYPE-file-name-sans-versions
;;
;; Variables:
;;
;; ange-ftp-TYPE-host-regexp
;; May need to add TYPE to ange-ftp-dumb-host-types
;;
;; Check the following functions for OS dependent coding:
;;
;; ange-ftp-host-type
;; ange-ftp-guess-host-type
;; ange-ftp-allow-child-lookup

;; Host type conventions:
;;
;; The function ange-ftp-host-type and the variable ange-ftp-dired-host-type
;; (mostly) follow the following conventions for remote host types.  At
;; least, I think that future code should try to follow these conventions,
;; and the current code should eventually be made compliant.
;;
;; nil = local host type, whatever that is (probably unix).
;;       Think nil as in "not a remote host". This value is used by
;;       ange-ftp-dired-host-type for local buffers.
;;
;; t = a remote host of unknown type. Think t as in true, it's remote.
;;     Currently, `unix' is used as the default remote host type.
;;     Maybe we should use t.
;;
;; TYPE = a remote host of TYPE type.
;;
;; TYPE:LIST = a remote host of TYPE type, using a specialized ftp listing
;;             program called list. This is currently only used for Unix
;;             dl (descriptive listings), when ange-ftp-dired-host-type
;;             is set to `unix:dl'.

;; Bug report codes:
;;
;; Because of their naive faith in this code, there are certain situations
;; which the writers of this program believe could never happen. However,
;; being realists they have put calls to `error' in the program at these
;; points. These errors provide a code, which is an integer, greater than 1.
;; To aid debugging.  the error codes, and the functions in which they reside
;; are listed below.
Sam Steingold's avatar
Sam Steingold committed
625
;;
Gerd Moellmann's avatar
Gerd Moellmann committed
626 627 628 629 630 631
;; 1: See ange-ftp-ls
;;

;; -----------------------------------------------------------
;; Hall of fame:
;; -----------------------------------------------------------
Sam Steingold's avatar
Sam Steingold committed
632
;;
Gerd Moellmann's avatar
Gerd Moellmann committed
633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673
;; Thanks to Roland McGrath for improving the filename syntax handling,
;; for suggesting many enhancements and for numerous cleanups to the code.
;;
;; Thanks to Jamie Zawinski for bugfixes and for ideas such as gateways.
;;
;; Thanks to Ken Laprade for improved .netrc parsing, password reading, and
;; dired / shell auto-loading.
;;
;; Thanks to Sebastian Kremer for dired support and for many ideas and
;; bugfixes.
;;
;; Thanks to Joe Wells for bugfixes, the original non-UNIX system support,
;; VOS support, and hostname completion.
;;
;; Thanks to Nakagawa Takayuki for many good ideas, filename-completion, help
;; with file-name expansion, efficiency worries, stylistic concerns and many
;; bugfixes.
;;
;; Thanks to Sandy Rutherford who re-wrote most of ange-ftp to support VMS,
;; MTS, CMS and UNIX-dls.  Sandy also added dired-support for non-UNIX OS and
;; auto-recognition of the host type.
;;
;; Thanks to Dave Smith who wrote the info file for ange-ftp.
;;
;; Finally, thanks to Keith Waclena, Mark D. Baushke, Terence Kelleher, Ping
;; Zhou, Edward Vielmetti, Jack Repenning, Mike Balenger, Todd Kaufmann,
;; Kjetil Svarstad, Tom Wurgler, Linus Tolke, Niko Makila, Carl Edman, Bill
;; Trost, Dave Brennan, Dan Jacobson, Andy Scott, Steve Anderson, Sanjay
;; Mathur, the folks on the ange-ftp-lovers mailing list and many others
;; whose names I've forgotten who have helped to debug and fix problems with
;; ange-ftp.el.

;;; Code:

(require 'comint)

;;;; ------------------------------------------------------------
;;;; User customization variables.
;;;; ------------------------------------------------------------

(defgroup ange-ftp nil
674
  "Accessing remote files and directories using FTP."
Gerd Moellmann's avatar
Gerd Moellmann committed
675
  :group 'files
676
  :group 'comm
Gerd Moellmann's avatar
Gerd Moellmann committed
677 678 679
  :prefix "ange-ftp-")

(defcustom ange-ftp-name-format
680
  '("\\`/\\(\\([^/:]*\\)@\\)?\\([^@/:]*[^@/:.]\\):\\(.*\\)" . (3 2 4))
681
  "Format of a fully expanded remote file name.
Gerd Moellmann's avatar
Gerd Moellmann committed
682 683 684 685 686 687

This is a list of the form \(REGEXP HOST USER NAME\),
where REGEXP is a regular expression matching
the full remote name, and HOST, USER, and NAME are the numbers of
parenthesized expressions in REGEXP for the components (in that order)."
  :group 'ange-ftp
688
  :type '(list (regexp  :tag "Name regexp")
Gerd Moellmann's avatar
Gerd Moellmann committed
689 690 691 692 693 694 695 696 697
	       (integer :tag "Host group")
	       (integer :tag "User group")
	       (integer :tag "Name group")))

;; ange-ftp-multi-skip-msgs should only match ###-, where ### is one of
;; the number codes corresponding to ange-ftp-good-msgs or ange-ftp-fatal-msgs.
;; Otherwise, ange-ftp will go into multi-skip mode, and never come out.

(defvar ange-ftp-multi-msgs
698
  "^150-\\|^220-\\|^230-\\|^226\\|^25.-\\|^221-\\|^200-\\|^331-\\|^4[25]1-\\|^530-"
699
  "Regular expression matching the start of a multiline FTP reply.")
Gerd Moellmann's avatar
Gerd Moellmann committed
700 701

(defvar ange-ftp-good-msgs
702
  "^220 \\|^230 \\|^226 \\|^25. \\|^221 \\|^200 \\|^[Hh]ash mark\\|^Remote directory:"
703
  "Regular expression matching FTP \"success\" messages.")
Gerd Moellmann's avatar
Gerd Moellmann committed
704 705 706 707 708 709 710

;; CMS and the odd VMS machine say 200 Port rather than 200 PORT.
;; Also CMS machines use a multiline 550- reply to say that you
;; don't have write permission. ange-ftp gets into multi-line skip
;; mode and hangs. Have it ignore 550- instead. It will then barf
;; when it gets the 550 line, as it should.

711 712 713 714 715
;; RFC2228 "FTP Security Extensions" defines extensions to the FTP
;; protocol which involve the client requesting particular
;; authentication methods (typically) at connection establishment. Non
;; security-aware FTP servers should respond to this with a 500 code,
;; which we ignore.
Gerd Moellmann's avatar
Gerd Moellmann committed
716 717 718 719 720
(defcustom ange-ftp-skip-msgs
  (concat "^200 \\(PORT\\|Port\\) \\|^331 \\|^150 \\|^350 \\|^[0-9]+ bytes \\|"
	  "^Connected \\|^$\\|^Remote system\\|^Using\\|^ \\|Password:\\|"
	  "^Data connection \\|"
	  "^local:\\|^Trying\\|^125 \\|^550-\\|^221 .*oodbye\\|"
721
          "^500 .*AUTH\\|^KERBEROS\\|"
722
          "^500 This security scheme is not implemented\\|"
723
          "^504 Unknown security mechanism\\|"
Paul Eggert's avatar
Paul Eggert committed
724
	  "^530 Please login with USER and PASS\\|" ; non kerberized vsFTPd
725
	  "^534 Kerberos Authentication not enabled\\|"
726
	  "^22[789] .*[Pp]assive\\|^200 EPRT\\|^500 .*EPRT")
727
  "Regular expression matching FTP messages that can be ignored."
Gerd Moellmann's avatar
Gerd Moellmann committed
728 729 730 731 732 733
  :group 'ange-ftp
  :type 'regexp)

(defcustom ange-ftp-fatal-msgs
  (concat "^ftp: \\|^Not connected\\|^530 \\|^4[25]1 \\|rcmd: \\|"
	  "^No control connection\\|unknown host\\|^lost connection")
734
  "Regular expression matching FTP messages that indicate serious errors.
Gerd Moellmann's avatar
Gerd Moellmann committed
735

736
These mean that the FTP process should be (or already has been) killed."
Gerd Moellmann's avatar
Gerd Moellmann committed
737 738 739
  :group 'ange-ftp
  :type 'regexp)

740 741
(defcustom ange-ftp-potential-error-msgs
  ;; On Mac OS X we sometimes get things like:
742
  ;;
743 744 745 746 747 748
  ;;     ftp> open ftp.nluug.nl
  ;;     Trying 2001:610:1:80aa:192:87:102:36...
  ;;     ftp: connect to address 2001:610:1:80aa:192:87:102:36: No route to host
  ;;     Trying 192.87.102.36...
  ;;     Connected to ftp.nluug.nl.
  "^ftp: connect to address .*: No route to host"
749
  "Regular expression matching FTP messages that can indicate serious errors.
750 751 752 753 754
These mean that something went wrong, but they may be followed by more
messages indicating that the error was somehow corrected."
  :group 'ange-ftp
  :type 'regexp)

Gerd Moellmann's avatar
Gerd Moellmann committed
755 756
(defcustom ange-ftp-gateway-fatal-msgs
  "No route to host\\|Connection closed\\|No such host\\|Login incorrect"
757
  "Regular expression matching login failure messages from rlogin/telnet."
Gerd Moellmann's avatar
Gerd Moellmann committed
758 759 760 761 762
  :group 'ange-ftp
  :type 'regexp)

(defcustom ange-ftp-xfer-size-msgs
  "^150 .* connection for .* (\\([0-9]+\\) bytes)"
763
  "Regular expression used to determine the number of bytes in a FTP transfer."
Gerd Moellmann's avatar
Gerd Moellmann committed
764 765 766
  :group 'ange-ftp
  :type 'regexp)

Sam Steingold's avatar
Sam Steingold committed
767
(defcustom ange-ftp-tmp-name-template
Gerd Moellmann's avatar
Gerd Moellmann committed
768
  (expand-file-name "ange-ftp" temporary-file-directory)
769
  "Template used to create temporary files."
Gerd Moellmann's avatar
Gerd Moellmann committed
770 771 772 773
  :group 'ange-ftp
  :type 'directory)

(defcustom ange-ftp-gateway-tmp-name-template "/tmp/ange-ftp"
774
  "Template used to create temporary files when FTP-ing through a gateway.
Gerd Moellmann's avatar
Gerd Moellmann committed
775 776 777 778 779 780 781 782 783

Files starting with this prefix need to be accessible from BOTH the local
machine and the gateway machine, and need to have the SAME name on both
machines, that is, /tmp is probably NOT what you want, since that is rarely
cross-mounted."
  :group 'ange-ftp
  :type 'directory)

(defcustom ange-ftp-netrc-filename "~/.netrc"
784
  "File in .netrc format to search for passwords."
Gerd Moellmann's avatar
Gerd Moellmann committed
785 786 787 788
  :group 'ange-ftp
  :type 'file)

(defcustom ange-ftp-disable-netrc-security-check (eq system-type 'windows-nt)
789
  "If non-nil avoid checking permissions on the .netrc file."
Gerd Moellmann's avatar
Gerd Moellmann committed
790 791 792 793
  :group 'ange-ftp
  :type 'boolean)

(defcustom ange-ftp-default-user nil
794
  "User name to use when none is specified in a file name.
Gerd Moellmann's avatar
Gerd Moellmann committed
795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819

If non-nil but not a string, you are prompted for the name.
If nil, the value of `ange-ftp-netrc-default-user' is used.
If that is nil too, then your login name is used.

Once a connection to a given host has been initiated, the user name
and password information for that host are cached and re-used by
ange-ftp.  Use \\[ange-ftp-set-user] to change the cached values,
since setting `ange-ftp-default-user' directly does not affect
the cached information."
  :group 'ange-ftp
  :type '(choice (const :tag "Default" nil)
		 string
		 (other :tag "Prompt" t)))

(defcustom ange-ftp-netrc-default-user nil
  "Alternate default user name to use when none is specified.

This variable is set from the `default' command in your `.netrc' file,
if there is one."
  :group 'ange-ftp
  :type '(choice (const :tag "Default" nil)
		 string))

(defcustom ange-ftp-default-password nil
820
  "Password to use when the user name equals `ange-ftp-default-user'."
Gerd Moellmann's avatar
Gerd Moellmann committed
821 822 823 824 825
  :group 'ange-ftp
  :type '(choice (const :tag "Default" nil)
		 string))

(defcustom ange-ftp-default-account nil
826
  "Account to use when the user name equals `ange-ftp-default-user'."
Gerd Moellmann's avatar
Gerd Moellmann committed
827 828 829 830 831
  :group 'ange-ftp
  :type '(choice (const :tag "Default" nil)
		 string))

(defcustom ange-ftp-netrc-default-password nil
832
  "Password to use when the user name equals `ange-ftp-netrc-default-user'."
Gerd Moellmann's avatar
Gerd Moellmann committed
833 834 835 836 837
  :group 'ange-ftp
  :type '(choice (const :tag "Default" nil)
		 string))

(defcustom ange-ftp-netrc-default-account nil
838
  "Account to use when the user name equals `ange-ftp-netrc-default-user'."
Gerd Moellmann's avatar
Gerd Moellmann committed
839 840 841 842 843
  :group 'ange-ftp
  :type '(choice (const :tag "Default" nil)
		 string))

(defcustom ange-ftp-generate-anonymous-password t
844
  "If t, use value of `user-mail-address' as password for anonymous FTP.
Gerd Moellmann's avatar
Gerd Moellmann committed
845 846 847 848 849 850 851 852 853

If a string, then use that string as the password.
If nil, prompt the user for a password."
  :group 'ange-ftp
  :type '(choice (const :tag "Prompt" nil)
		 string
		 (other :tag "User address" t)))

(defcustom ange-ftp-dumb-unix-host-regexp nil
854
  "If non-nil, regexp matching hosts on which `dir' command lists directory."
Gerd Moellmann's avatar
Gerd Moellmann committed
855 856 857 858
  :group 'ange-ftp
  :type '(choice (const :tag "Default" nil)
		 string))

859
(defcustom ange-ftp-binary-file-name-regexp ""
860
  "If a file matches this regexp then it is transferred in binary mode."
Gerd Moellmann's avatar
Gerd Moellmann committed
861
  :group 'ange-ftp
862 863
  :type 'regexp
  :version "24.1")
Gerd Moellmann's avatar
Gerd Moellmann committed
864 865

(defcustom ange-ftp-gateway-host nil
866
  "Name of host to use as gateway machine when local FTP isn't possible."
Gerd Moellmann's avatar
Gerd Moellmann committed
867 868 869 870 871
  :group 'ange-ftp
  :type '(choice (const :tag "Default" nil)
		 string))

(defcustom ange-ftp-local-host-regexp ".*"
872
  "Regexp selecting hosts which can be reached directly with FTP.
Gerd Moellmann's avatar
Gerd Moellmann committed
873

874 875
For other hosts the FTP process is started on `ange-ftp-gateway-host'
instead, and/or reached via `ange-ftp-gateway-ftp-program-name'."
Gerd Moellmann's avatar
Gerd Moellmann committed
876 877 878 879
  :group 'ange-ftp
  :type 'regexp)

(defcustom ange-ftp-gateway-program-interactive nil
880
  "If non-nil then the gateway program should give a shell prompt.
Gerd Moellmann's avatar
Gerd Moellmann committed
881 882 883 884 885 886

Both telnet and rlogin do something like this."
  :group 'ange-ftp
  :type 'boolean)

(defcustom ange-ftp-gateway-program remote-shell-program
887
  "Name of program to spawn a shell on the gateway machine.
Gerd Moellmann's avatar
Gerd Moellmann committed
888

889 890
Valid candidates are rsh (remsh on some systems), telnet and rlogin.
See also the gateway variable above."
Gerd Moellmann's avatar
Gerd Moellmann committed
891 892 893 894 895 896 897
  :group 'ange-ftp
  :type '(choice (const "rsh")
		 (const "telnet")
		 (const "rlogin")
		 string))

(defcustom ange-ftp-gateway-prompt-pattern "^[^#$%>;\n]*[#$%>;] *"
898
  "Regexp matching prompt after complete login sequence on gateway machine.
Gerd Moellmann's avatar
Gerd Moellmann committed
899 900 901 902 903 904 905 906 907 908 909 910

A match for this means the shell is now awaiting input.  Make this regexp as
strict as possible; it shouldn't match *anything* at all except the user's
initial prompt.  The above string will fail under most SUN-3's since it
matches the login banner."
  :group 'ange-ftp
  :type 'regexp)

(defvar ange-ftp-gateway-setup-term-command
  (if (eq system-type 'hpux)
      "stty -onlcr -echo\n"
    "stty -echo nl\n")
911
  "Set up terminal after logging in to the gateway machine.
Gerd Moellmann's avatar
Gerd Moellmann committed
912 913 914 915
This command should stop the terminal from echoing each command, and
arrange to strip out trailing ^M characters.")

(defcustom ange-ftp-smart-gateway nil
916
  "Non-nil says the FTP gateway (proxy) or gateway FTP program is smart.
Gerd Moellmann's avatar
Gerd Moellmann committed
917 918

Don't bother telnetting, etc., already connected to desired host transparently,
919
or just issue a user@host command in case `ange-ftp-gateway-host' is non-nil.
920
See also `ange-ftp-smart-gateway-port'."
Gerd Moellmann's avatar
Gerd Moellmann committed
921 922 923 924
  :group 'ange-ftp
  :type 'boolean)

(defcustom ange-ftp-smart-gateway-port "21"
925
  "Port on gateway machine to use when smart gateway is in operation."
Gerd Moellmann's avatar
Gerd Moellmann committed
926 927 928 929
  :group 'ange-ftp
  :type 'string)

(defcustom ange-ftp-send-hash t
930
  "If non-nil, send the HASH command to the FTP client."
Gerd Moellmann's avatar
Gerd Moellmann committed
931 932 933 934
  :group 'ange-ftp
  :type 'boolean)

(defcustom ange-ftp-binary-hash-mark-size nil
935
  "Default size, in bytes, between hash-marks when transferring a binary file.
Gerd Moellmann's avatar
Gerd Moellmann committed
936 937 938 939 940 941 942 943
If nil, this variable will be locally overridden if the FTP client outputs a
suitable response to the HASH command.  If non-nil, this value takes
precedence over the local value."
  :group 'ange-ftp
  :type '(choice (const :tag "Overridden" nil)
		 integer))

(defcustom ange-ftp-ascii-hash-mark-size 1024
944
  "Default size, in bytes, between hash-marks when transferring an ASCII file.
Gerd Moellmann's avatar
Gerd Moellmann committed
945 946 947 948 949 950
This variable is buffer-local and will be locally overridden if the FTP client
outputs a suitable response to the HASH command."
  :group 'ange-ftp
  :type 'integer)

(defcustom ange-ftp-process-verbose t
951
  "If non-nil then be chatty about interaction with the FTP process."
Gerd Moellmann's avatar
Gerd Moellmann committed
952 953 954 955
  :group 'ange-ftp
  :type 'boolean)

(defcustom ange-ftp-ftp-program-name "ftp"
956
  "Name of FTP program to run."
Gerd Moellmann's avatar
Gerd Moellmann committed
957 958 959 960
  :group 'ange-ftp
  :type 'string)

(defcustom ange-ftp-gateway-ftp-program-name "ftp"
961
  "Name of FTP program to run when accessing non-local hosts.
Gerd Moellmann's avatar
Gerd Moellmann committed
962 963 964 965 966 967

Some AT&T folks claim to use something called `pftp' here."
  :group 'ange-ftp
  :type 'string)

(defcustom ange-ftp-ftp-program-args '("-i" "-n" "-g" "-v")
968
  "A list of arguments passed to the FTP program when started."
Gerd Moellmann's avatar
Gerd Moellmann committed
969 970 971 972
  :group 'ange-ftp
  :type '(repeat string))

(defcustom ange-ftp-nslookup-program nil
973
  "If non-nil, this is a string naming the nslookup program."
Gerd Moellmann's avatar
Gerd Moellmann committed
974 975 976 977 978
  :group 'ange-ftp
  :type '(choice (const :tag "None" nil)
		 string))

(defcustom ange-ftp-make-backup-files ()
979
  "Non-nil means make backup files for \"magic\" remote files."
Gerd Moellmann's avatar
Gerd Moellmann committed
980 981 982 983
  :group 'ange-ftp
  :type 'boolean)

(defcustom ange-ftp-retry-time 5
984
  "Number of seconds to wait before retry if file or listing doesn't arrive.
Gerd Moellmann's avatar
Gerd Moellmann committed
985 986 987 988 989 990 991 992 993 994 995 996 997
This might need to be increased for very slow connections."
  :group 'ange-ftp
  :type 'integer)

(defcustom ange-ftp-auto-save 0
  "If 1, allow ange-ftp files to be auto-saved.
If 0, inhibit auto-saving of ange-ftp files.
Don't use any other value."
  :group 'ange-ftp
  :type '(choice (const :tag "Suppress" 0)
		 (const :tag "Allow" 1)))

(defcustom ange-ftp-try-passive-mode nil
998
  "If t, try to use passive mode in FTP, if the client program supports it."
Gerd Moellmann's avatar
Gerd Moellmann committed
999 1000
  :group 'ange-ftp
  :type 'boolean
Pavel Janík's avatar
Pavel Janík committed
1001
  :version "21.1")
Gerd Moellmann's avatar
Gerd Moellmann committed
1002

1003 1004 1005 1006 1007 1008 1009
(defcustom ange-ftp-passive-host-alist nil
  "Alist of FTP servers that need \"passive\" mode.
Each element is of the form (HOSTNAME . SETTING).
HOSTNAME is a regular expression to match the FTP server host name(s).
SETTING is \"on\" to turn passive mode on, \"off\" to turn it off,
or nil meaning don't change it."
  :group 'ange-ftp
1010 1011 1012
  :type '(repeat (cons regexp (choice (const :tag "On" "on")
				      (const :tag "Off" "off")
				      (const :tag "Don't change" nil))))
1013
  :version "22.1")
Gerd Moellmann's avatar
Gerd Moellmann committed
1014 1015 1016 1017 1018 1019 1020 1021

;;;; ------------------------------------------------------------
;;;; Hash table support.
;;;; ------------------------------------------------------------

(require 'backquote)

(defun ange-ftp-hash-entry-exists-p (key tbl)
1022
  "Return whether there is an association for KEY in table TBL."
David Kastrup's avatar
David Kastrup committed
1023
  (and tbl (not (eq (gethash key tbl 'unknown) 'unknown))))
Gerd Moellmann's avatar
Gerd Moellmann committed
1024 1025

(defun ange-ftp-hash-table-keys (tbl)
1026
  "Return a sorted list of all the active keys in table TBL, as strings."
Stefan Monnier's avatar
Stefan Monnier committed
1027 1028 1029 1030
  ;; (let ((keys nil))
  ;;   (maphash (lambda (k v) (push k keys)) tbl)
  ;;   (sort keys 'string-lessp))
  (sort (all-completions "" tbl) 'string-lessp))
Gerd Moellmann's avatar
Gerd Moellmann committed
1031 1032 1033 1034 1035 1036

;;;; ------------------------------------------------------------
;;;; Internal variables.
;;;; ------------------------------------------------------------

(defvar ange-ftp-data-buffer-name " *ftp data*"
1037
  "Buffer name to hold directory listing data received from FTP process.")
Gerd Moellmann's avatar
Gerd Moellmann committed
1038 1039 1040 1041

(defvar ange-ftp-netrc-modtime nil
  "Last modified time of the netrc file from file-attributes.")

Stefan Monnier's avatar
Stefan Monnier committed
1042
(defvar ange-ftp-user-hashtable (make-hash-table :test 'equal)
Gerd Moellmann's avatar
Gerd Moellmann committed
1043 1044
  "Hash table holding associations between HOST, USER pairs.")

Stefan Monnier's avatar
Stefan Monnier committed
1045
(defvar ange-ftp-passwd-hashtable (make-hash-table :test 'equal)
Gerd Moellmann's avatar
Gerd Moellmann committed
1046 1047 1048
  "Mapping between a HOST, USER pair and a PASSWORD for them.
All HOST values should be in lower case.")

Stefan Monnier's avatar
Stefan Monnier committed
1049
(defvar ange-ftp-account-hashtable (make-hash-table :test 'equal)
1050
  "Mapping between a HOST, USER pair and an ACCOUNT password for them.")
Gerd Moellmann's avatar
Gerd Moellmann committed
1051

Stefan Monnier's avatar
Stefan Monnier committed
1052
(defvar ange-ftp-files-hashtable (make-hash-table :test 'equal :size 97)
Gerd Moellmann's avatar
Gerd Moellmann committed
1053 1054
  "Hash table for storing directories and their respective files.")

Stefan Monnier's avatar
Stefan Monnier committed
1055
(defvar ange-ftp-inodes-hashtable (make-hash-table :test 'equal :size 97)
Gerd Moellmann's avatar
Gerd Moellmann committed
1056 1057 1058 1059 1060 1061
  "Hash table for storing file names and their \"inode numbers\".")

(defvar ange-ftp-next-inode-number 1
  "Next \"inode number\" value.  We give each file name a unique number.")

(defvar ange-ftp-ls-cache-lsargs nil
1062
  "Last set of args used by `ange-ftp-ls'.")
Gerd Moellmann's avatar
Gerd Moellmann committed
1063 1064

(defvar ange-ftp-ls-cache-file nil
1065
  "Last file passed to `ange-ftp-ls'.")
Gerd Moellmann's avatar
Gerd Moellmann committed
1066 1067

(defvar ange-ftp-ls-cache-res nil
1068
  "Last result returned from `ange-ftp-ls'.")
Gerd Moellmann's avatar
Gerd Moellmann committed
1069

Stefan Monnier's avatar
Stefan Monnier committed
1070
(defconst ange-ftp-expand-dir-hashtable (make-hash-table :test 'equal))
Gerd Moellmann's avatar
Gerd Moellmann committed
1071 1072 1073 1074 1075 1076 1077 1078 1079

(defconst ange-ftp-expand-dir-regexp "^5.0 \\([^: ]+\\):")

;; These are local variables in each FTP process buffer.
(defvar ange-ftp-hash-mark-unit nil)
(defvar ange-ftp-hash-mark-count nil)
(defvar ange-ftp-xfer-size nil)
(defvar ange-ftp-process-string nil)
(defvar ange-ftp-process-result-line nil)
1080
(defvar ange-ftp-pending-error-line nil)
Gerd Moellmann's avatar
Gerd Moellmann committed
1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098
(defvar ange-ftp-process-busy nil)
(defvar ange-ftp-process-result nil)
(defvar ange-ftp-process-multi-skip nil)
(defvar ange-ftp-process-msg nil)
(defvar ange-ftp-process-continue nil)
(defvar ange-ftp-last-percent nil)

;; These variables are bound by one function and examined by another.
;; Leave them void globally for error checking.
(defvar ange-ftp-this-file)
(defvar ange-ftp-this-dir)
(defvar ange-ftp-this-user)
(defvar ange-ftp-this-host)
(defvar ange-ftp-this-msg)
(defvar ange-ftp-completion-ignored-pattern)
(defvar ange-ftp-trample-marker)

;; New error symbols.
1099
(define-error 'ftp-error nil 'file-error) ;"FTP error"
Gerd Moellmann's avatar
Gerd Moellmann committed
1100 1101 1102 1103 1104 1105 1106 1107

;;; ------------------------------------------------------------
;;; Enhanced message support.
;;; ------------------------------------------------------------

(defun ange-ftp-message (fmt &rest args)
  "Display message in echo area, but indicate if truncated.
Args are as in `message': a format string, plus arguments to be formatted."
Stefan Monnier's avatar
Stefan Monnier committed
1108
  (let ((msg (apply 'format fmt args))
Gerd Moellmann's avatar
Gerd Moellmann committed
1109 1110 1111 1112 1113 1114 1115 1116 1117
	(max (window-width (minibuffer-window))))
    (if noninteractive
	msg
      (if (>= (length msg) max)
	  ;; Take just the last MAX - 3 chars of the string.
	  (setq msg (concat "> " (substring msg (- 3 max)))))
      (message "%s" msg))))

(defun ange-ftp-abbreviate-filename (file &optional new)
Sam Steingold's avatar
Sam Steingold committed
1118
  "Abbreviate the file name FILE relative to the `default-directory'.
Gerd Moellmann's avatar
Gerd Moellmann committed
1119 1120 1121 1122
If the optional parameter NEW is given and the non-directory parts match,
only return the directory part of FILE."
  (save-match-data
    (if (and default-directory
1123
	     (string-match (concat "\\`"
Gerd Moellmann's avatar
Gerd Moellmann committed
1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139
				   (regexp-quote default-directory)
				   ".") file))
	(setq file (substring file (1- (match-end 0)))))
    (if (and new
	     (string-equal (file-name-nondirectory file)
			   (file-name-nondirectory new)))
	(setq file (file-name-directory file)))
    (or file "./")))

;;;; ------------------------------------------------------------
;;;; User / Host mapping support.
;;;; ------------------------------------------------------------

(defun ange-ftp-set-user (host user)
  "For a given HOST, set or change the default USER."
  (interactive "sHost: \nsUser: ")
Stefan Monnier's avatar
Stefan Monnier committed
1140
  (puthash host user ange-ftp-user-hashtable))
Gerd Moellmann's avatar
Gerd Moellmann committed
1141 1142

(defun ange-ftp-get-user (host)
1143
  "Given a HOST, return the default user."
Gerd Moellmann's avatar
Gerd Moellmann committed
1144
  (ange-ftp-parse-netrc)
Stefan Monnier's avatar
Stefan Monnier committed
1145
  (let ((user (gethash host ange-ftp-user-hashtable)))
Gerd Moellmann's avatar
Gerd Moellmann committed
1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167
    (or user
	(prog1
	    (setq user
		  (cond ((stringp ange-ftp-default-user)
			 ;; We have a default name.  Use it.
			 ange-ftp-default-user)
			(ange-ftp-default-user
			 ;; Ask the user.
			 (let ((enable-recursive-minibuffers t))
			   (read-string (format "User for %s: " host)
					(user-login-name))))
			(ange-ftp-netrc-default-user)
			;; Default to the user's login name.
			(t
			 (user-login-name))))
	  (ange-ftp-set-user host user)))))

;;;; ------------------------------------------------------------
;;;; Password support.
;;;; ------------------------------------------------------------

(defmacro ange-ftp-generate-passwd-key (host user)
1168
  `(and (stringp ,host) (stringp ,user) (concat (downcase ,host) "/" ,user)))
Gerd Moellmann's avatar
Gerd Moellmann committed
1169 1170

(defmacro ange-ftp-lookup-passwd (host user)
Stefan Monnier's avatar
Stefan Monnier committed
1171 1172
  `(gethash (ange-ftp-generate-passwd-key ,host ,user)
	    ange-ftp-passwd-hashtable))
Gerd Moellmann's avatar
Gerd Moellmann committed
1173

1174
(defun ange-ftp-set-passwd (host user password)
Gerd Moellmann's avatar
Gerd Moellmann committed
1175 1176 1177 1178
  "For a given HOST and USER, set or change the associated PASSWORD."
  (interactive (list (read-string "Host: ")
		     (read-string "User: ")
		     (read-passwd "Password: ")))
Stefan Monnier's avatar
Stefan Monnier committed
1179
  (puthash (ange-ftp-generate-passwd-key host user)
1180
	   password ange-ftp-passwd-hashtable))
Gerd Moellmann's avatar
Gerd Moellmann committed
1181 1182 1183 1184 1185

(defun ange-ftp-get-host-with-passwd (user)
  "Given a USER, return a host we know the password for."
  (ange-ftp-parse-netrc)
  (catch 'found-one
Stefan Monnier's avatar
Stefan Monnier committed
1186 1187 1188
    (maphash
     (lambda (host val)
       (if (ange-ftp-lookup-passwd host user) (throw 'found-one host)))
Gerd Moellmann's avatar
Gerd Moellmann committed
1189 1190
     ange-ftp-user-hashtable)
    (save-match-data
Stefan Monnier's avatar
Stefan Monnier committed
1191 1192
      (maphash
       (lambda (key value)
1193
	 (if (string-match "\\`[^/]*\\(/\\).*\\'" key)
Stefan Monnier's avatar
Stefan Monnier committed
1194 1195 1196 1197
	     (let ((host (substring key 0 (match-beginning 1))))
	       (if (and (string-equal user (substring key (match-end 1)))
			value)
		   (throw 'found-one host)))))
Gerd Moellmann's avatar
Gerd Moellmann committed
1198 1199 1200 1201 1202
       ange-ftp-passwd-hashtable))
    nil))

(defun ange-ftp-get-passwd (host user)
  "Return the password for specified HOST and USER, asking user if necessary."
1203
  ;; If `non-essential' is non-nil, don't ask for a password.  It will
Paul Eggert's avatar
Paul Eggert committed
1204
  ;; be caught in Tramp.
1205 1206 1207
  (when non-essential
    (throw 'non-essential 'non-essential))

Gerd Moellmann's avatar
Gerd Moellmann committed
1208 1209 1210 1211 1212
  (ange-ftp-parse-netrc)

  ;; look up password in the hash table first; user might have overridden the
  ;; defaults.
  (cond ((ange-ftp-lookup-passwd host user))
Sam Steingold's avatar
Sam Steingold committed
1213

Gerd Moellmann's avatar
Gerd Moellmann committed
1214 1215 1216 1217 1218
	;; See if default user and password set.
	((and (stringp ange-ftp-default-user)
	      ange-ftp-default-password
	      (string-equal user ange-ftp-default-user))
	 ange-ftp-default-password)
Sam Steingold's avatar
Sam Steingold committed
1219

Gerd Moellmann's avatar
Gerd Moellmann committed
1220 1221 1222 1223 1224
	;; See if default user and password set from .netrc file.
	((and (stringp ange-ftp-netrc-default-user)
	      ange-ftp-netrc-default-password
	      (string-equal user ange-ftp-netrc-default-user))
	 ange-ftp-netrc-default-password)
Sam Steingold's avatar
Sam Steingold committed
1225

Gerd Moellmann's avatar
Gerd Moellmann committed
1226 1227 1228 1229 1230 1231 1232 1233
	;; anonymous ftp password is handled specially since there is an
	;; unwritten rule about how that is used on the Internet.
	((and (or (string-equal user "anonymous")
		  (string-equal user "ftp"))
	      ange-ftp-generate-anonymous-password)
	 (if (stringp ange-ftp-generate-anonymous-password)
	     ange-ftp-generate-anonymous-password
	   user-mail-address))
Sam Steingold's avatar
Sam Steingold committed
1234

Gerd Moellmann's avatar
Gerd Moellmann committed
1235 1236 1237
	;; see if same user has logged in to other hosts; if so then prompt
	;; with the password that was used there.
	(t
1238 1239
	 (let* ((enable-recursive-minibuffers t)
		(other (ange-ftp-get-host-with-passwd user))
Gerd Moellmann's avatar
Gerd Moellmann committed
1240
		(passwd (if other
Sam Steingold's avatar
Sam Steingold committed
1241

Gerd Moellmann's avatar
Gerd Moellmann committed
1242 1243 1244 1245 1246 1247 1248
			    ;; found another machine with the same user.
			    ;; Try that account.
			    (read-passwd
			     (format "passwd for %s@%s (default same as %s@%s): "
				     user host user other)
			     nil
			     (ange-ftp-lookup-passwd other user))
Sam Steingold's avatar
Sam Steingold committed
1249

Gerd Moellmann's avatar
Gerd Moellmann committed
1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263
			  ;; I give up.  Ask the user for the password.
			  (read-passwd
			   (format "Password for %s@%s: " user host)))))
	   (ange-ftp-set-passwd host user passwd)
	   passwd))))

;;;; ------------------------------------------------------------
;;;; Account support
;;;; ------------------------------------------------------------

;; Account passwords must be either specified in the .netrc file, or set
;; manually by calling ange-ftp-set-account.  For the moment, ange-ftp doesn't
;; check to see whether the FTP process is actually prompting for an account
;; password.
Sam Steingold's avatar
Sam Steingold committed
1264

Gerd Moellmann's avatar
Gerd Moellmann committed
1265 1266 1267 1268 1269
(defun ange-ftp-set-account (host user account)
  "For a given HOST and USER, set or change the associated ACCOUNT password."
  (interactive (list (read-string "Host: ")
		     (read-string "User: ")
		     (read-passwd "Account password: ")))
Stefan Monnier's avatar
Stefan Monnier committed
1270 1271
  (puthash (ange-ftp-generate-passwd-key host user)
	   account ange-ftp-account-hashtable))
Gerd Moellmann's avatar
Gerd Moellmann committed
1272 1273 1274 1275

(defun ange-ftp-get-account (host user)
  "Given a HOST and USER, return the FTP account."
  (ange-ftp-parse-netrc)
Stefan Monnier's avatar
Stefan Monnier committed
1276 1277
  (or (gethash (ange-ftp-generate-passwd-key host user)
	       ange-ftp-account-hashtable)
Gerd Moellmann's avatar
Gerd Moellmann committed
1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295
      (and (stringp ange-ftp-default-user)
	   (string-equal user ange-ftp-default-user)
	   ange-ftp-default-account)
      (and (stringp ange-ftp-netrc-default-user)
	   (string-equal user ange-ftp-netrc-default-user)
	   ange-ftp-netrc-default-account)))

;;;; ------------------------------------------------------------
;;;; ~/.netrc support
;;;; ------------------------------------------------------------

(defun ange-ftp-chase-symlinks (file)
  "Return the filename that FILE references, following all symbolic links."
  (let (temp)
    (while (setq temp (ange-ftp-real-file-symlink-p file))
      (setq file
	    (if (file-name-absolute-p temp)
		temp
1296 1297
	      ;; Wouldn't `expand-file-name' be better than `concat' ?
	      ;; It would fail when `a/b/..' != `a', tho.  --Stef
Gerd Moellmann's avatar
Gerd Moellmann committed
1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384
	      (concat (file-name-directory file) temp)))))
  file)

;; Move along current line looking for the value of the TOKEN.
;; Valid separators between TOKEN and its value are commas and
;; whitespace.  Second arg LIMIT is a limit for the search.

(defun ange-ftp-parse-netrc-token (token limit)
  (if (search-forward token limit t)
      (let (beg)
	(skip-chars-forward ", \t\r\n" limit)
	(if (eq (following-char) ?\")	;quoted token value
	    (progn (forward-char 1)
		   (setq beg (point))
		   (skip-chars-forward "^\"" limit)
		   (forward-char 1)
		   (buffer-substring beg (1- (point))))
	  (setq beg (point))
	  (skip-chars-forward "^, \t\r\n" limit)
	  (buffer-substring beg (point))))))

;; Extract the values for the tokens `machine', `login',
;; `password' and `account' in the current buffer.  If successful,
;; record the information found.

(defun ange-ftp-parse-netrc-group ()
  (let ((start (point))
	(end (save-excursion
	       (if (looking-at "machine\\>")
		   ;; Skip `machine' and the machine name that follows.
		   (progn
		     (skip-chars-forward "^ \t\r\n")
		     (skip-chars-forward " \t\r\n")
		     (skip-chars-forward "^ \t\r\n"))
		 ;; Skip `default'.
		 (skip-chars-forward "^ \t\r\n"))
	       ;; Find start of the next `machine' or `default'
	       ;; or the end of the buffer.
	       (if (re-search-forward "machine\\>\\|default\\>" nil t)
		   (match-beginning 0)
		 (point-max))))
	machine login password account)
    (setq machine  (ange-ftp-parse-netrc-token "machine"  end)
	  login    (ange-ftp-parse-netrc-token "login"    end)
	  password (ange-ftp-parse-netrc-token "password" end)
	  account  (ange-ftp-parse-netrc-token "account"  end))
    (if (and machine login)
	;; found a `machine` token.
	(progn
	  (ange-ftp-set-user machine login)
	  (ange-ftp-set-passwd machine login password)
	  (and account
	       (ange-ftp-set-account machine login account)))
      (goto-char start)
      (if (search-forward "default" end t)
	  ;; found a `default' token
	  (progn
	    (setq login    (ange-ftp-parse-netrc-token "login"    end)
		  password (ange-ftp-parse-netrc-token "password" end)
		  account  (ange-ftp-parse-netrc-token "account"  end))
	    (and login
		 (setq ange-ftp-netrc-default-user login))
	    (and password
		 (setq ange-ftp-netrc-default-password password))
	    (and account
		 (setq ange-ftp-netrc-default-account account)))))
    (goto-char end)))

;; Read in ~/.netrc, if one exists.  If ~/.netrc file exists and has
;; the correct permissions then extract the \`machine\', \`login\',
;; \`password\' and \`account\' information from within.

(defun ange-ftp-parse-netrc ()
  ;; We set this before actually doing it to avoid the possibility
  ;; of an infinite loop if ange-ftp-netrc-filename is an FTP file.
  (interactive)
  (let (file attr)
    (let ((default-directory "/"))
      (setq file (ange-ftp-chase-symlinks
		  (ange-ftp-real-expand-file-name ange-ftp-netrc-filename)))
      (setq attr (ange-ftp-real-file-attributes file)))
    (if (and attr			; file exists.
	     (not (equal (nth 5 attr) ange-ftp-netrc-modtime)))	; file changed
	(save-match-data
	  (if (or ange-ftp-disable-netrc-security-check
		  (and (eq (nth 2 attr) (user-uid)) ; Same uids.
		       (string-match ".r..------" (nth 8 attr))))
Stefan Monnier's avatar
Stefan Monnier committed
1385
	      (with-current-buffer
Gerd Moellmann's avatar
Gerd Moellmann committed
1386 1387 1388 1389
		;; we are cheating a bit here.  I'm trying to do the equivalent
		;; of find-file on the .netrc file, but then nuke it afterwards.
		;; with the bit of logic below we should be able to have
		;; encrypted .netrc files.
Stefan Monnier's avatar
Stefan Monnier committed
1390
                  (generate-new-buffer "*ftp-.netrc*")
Gerd Moellmann's avatar
Gerd Moellmann committed
1391 1392 1393 1394
		(ange-ftp-real-insert-file-contents file)
		(setq buffer-file-name file)
		(setq default-directory (file-name-directory file))
		(normal-mode t)
1395
		(run-hooks 'find-file-hook)
Gerd Moellmann's avatar
Gerd Moellmann committed
1396 1397
		(setq buffer-file-name nil)
		(goto-char (point-min))
1398 1399 1400
		(while (search-forward-regexp "^[ \t]*#.*$" nil t)
		  (replace-match ""))
		(goto-char (point-min))
Gerd Moellmann's avatar
Gerd Moellmann committed
1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416
		(skip-chars-forward " \t\r\n")
		(while (not (eobp))
		  (ange-ftp-parse-netrc-group))
		(kill-buffer (current-buffer)))
	    (ange-ftp-message "%s either not owned by you or badly protected."
			      ange-ftp-netrc-filename)
	    (sit-for 1))
	  (setq ange-ftp-netrc-modtime (nth 5 attr))))))

;; Return a list of prefixes of the form 'user@host:' to be used when
;; completion is done in the root directory.

(defun ange-ftp-generate-root-prefixes ()
  (ange-ftp-parse-netrc)
  (save-match-data
    (let (res)
Stefan Monnier's avatar
Stefan Monnier committed
1417 1418
      (maphash
       (lambda (key value)
1419
	 (if (string-match "\\`[^/]*\\(/\\).*\\'" key)
Stefan Monnier's avatar
Stefan Monnier committed
1420 1421 1422
	     (let ((host (substring key 0 (match-beginning 1)))
		   (user (substring key (match-end 1))))
	       (push (concat user "@" host ":") res))))
Gerd Moellmann's avatar
Gerd Moellmann committed
1423
       ange-ftp-passwd-hashtable)
Stefan Monnier's avatar
Stefan Monnier committed
1424 1425
      (maphash
       (lambda (host user) (push (concat host ":") res))
Gerd Moellmann's avatar
Gerd Moellmann committed
1426 1427 1428 1429 1430 1431 1432 1433
       ange-ftp-user-hashtable)
      (or res (list nil)))))

;;;; ------------------------------------------------------------
;;;; Remote file name syntax support.
;;;; ------------------------------------------------------------

(defmacro ange-ftp-ftp-name-component (n ns name)
1434
  "Extract the Nth FTP file name component from NS."
Sam Steingold's avatar
Sam Steingold committed
1435
  `(let ((elt (nth ,n ,ns)))
1436
     (match-string elt ,name)))
Gerd Moellmann's avatar
Gerd Moellmann committed
1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488

(defvar ange-ftp-ftp-name-arg "")
(defvar ange-ftp-ftp-name-res nil)

;; Parse NAME according to `ange-ftp-name-format' (which see).
;; Returns a list (HOST USER NAME), or nil if NAME does not match the format.
(defun ange-ftp-ftp-name (name)
  (if (string-equal name ange-ftp-ftp-name-arg)
      ange-ftp-ftp-name-res
    (setq ange-ftp-ftp-name-arg name
	  ange-ftp-ftp-name-res
	  (save-match-data
	    (if (posix-string-match (car ange-ftp-name-format) name)
		(let* ((ns (cdr ange-ftp-name-format))
		       (host (ange-ftp-ftp-name-component 0 ns name))
		       (user (ange-ftp-ftp-name-component 1 ns name))
		       (name (ange-ftp-ftp-name-component 2 ns name)))
		  (if (zerop (length user))
		      (setq user (ange-ftp-get-user host)))
		  (list host user name))
	      nil)))))

;; Take a FULLNAME that matches according to ange-ftp-name-format and
;; replace the name component with NAME.
(defun ange-ftp-replace-name-component (fullname name)
  (save-match-data
    (if (posix-string-match (car ange-ftp-name-format) fullname)
	(let* ((ns (cdr ange-ftp-name-format))
	       (elt (nth 2 ns)))
	  (concat (substring fullname 0 (match-beginning elt))
		  name
		  (substring fullname (match-end elt)))))))

;;;; ------------------------------------------------------------
;;;; Miscellaneous utils.
;;;; ------------------------------------------------------------

;; (setq ange-ftp-tmp-keymap (make-sparse-keymap))
;; (define-key ange-ftp-tmp-keymap "\C-m" 'exit-minibuffer)

(defun ange-ftp-repaint-minibuffer ()
  "Clear any existing minibuffer message; let the minibuffer contents show."
  (message nil))

;; Return the name of the buffer that collects output from the ftp process
;; connected to the given HOST and USER pair.
(defun ange-ftp-ftp-process-buffer (host user)
  (concat "*ftp " user "@" host "*"))

;; Display the last chunk of output from the ftp process for the given HOST
;; USER pair, and signal an error including MSG in the text.
(defun ange-ftp-error (host user msg)
1489 1490 1491 1492 1493 1494 1495 1496 1497
  (save-excursion  ;; Prevent pop-to-buffer from changing current buffer.
    (let ((cur (selected-window))
	  (pop-up-windows t))
      (pop-to-buffer
       (get-buffer-create
	(ange-ftp-ftp-process-buffer host user)))
      (goto-char (point-max))
      (select-window cur))
    (signal 'ftp-error (list (format "FTP Error: %s" msg)))))
Gerd Moellmann's avatar
Gerd Moellmann committed
1498 1499 1500 1501 1502 1503 1504 1505 1506

(defun ange-ftp-set-buffer-mode ()
  "Set correct modes for the current buffer if visiting a remote file."
  (if (and (stringp buffer-file-name)
	   (ange-ftp-ftp-name buffer-file-name))
      (auto-save-mode ange-ftp-auto-save)))

(defun ange-ftp-kill-ftp-process (&optional buffer)
  "Kill the FTP process associated with BUFFER (the current buffer, if nil).
1507 1508
If the BUFFER's visited filename or `default-directory' is an FTP filename
then kill the related FTP process."
Gerd Moellmann's avatar
Gerd Moellmann committed
1509 1510 1511 1512 1513
  (interactive "bKill FTP process associated with buffer: ")
  (if (null buffer)
      (setq buffer (current-buffer))
    (setq buffer (get-buffer buffer)))
  (let ((file (or (buffer-file-name buffer)
Stefan Monnier's avatar
Stefan Monnier committed
1514
		  (with-current-buffer buffer default-directory))))
Gerd Moellmann's avatar
Gerd Moellmann committed
1515 1516 1517 1518 1519 1520 1521 1522
    (if file
	(let ((parsed (ange-ftp-ftp-name (expand-file-name file))))
	  (if parsed
	      (let ((host (nth 0 parsed))
		    (user (nth 1 parsed)))
		(kill-buffer (get-buffer (ange-ftp-ftp-process-buffer host user)))))))))

(defun ange-ftp-quote-string (string)
1523
  "Quote any characters in STRING that may confuse the FTP process."
1524 1525 1526 1527
  ;; This is said to be wrong; ftp is said to need quoting only for ",
  ;; and that by doubling it.  But experiment says UNIX-style kind of
  ;; quoting is correct when talking to ftp on GNU/Linux systems, and
  ;; W32-style kind of quoting on, yes, W32 systems.
1528 1529 1530
  (if (stringp string)
      (shell-quote-argument string)
    ""))