home *** CD-ROM | disk | FTP | other *** search
/ CLIX - Fazer Clix Custa Nix / CLIX-CD.cdr / mac / lib / Net / FTP.pm < prev    next >
Text File  |  1997-11-18  |  34KB  |  1,544 lines

  1. # Net::FTP.pm
  2. #
  3. # Copyright (c) 1995 Graham Barr <gbarr@ti.com>. All rights reserved.
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the same terms as Perl itself.
  6. #
  7. # Documentation (at end) improved 1996 by Nathan Torkington <gnat@frii.com>.
  8.  
  9. package Net::FTP;
  10.  
  11. require 5.001;
  12.  
  13. use strict;
  14. use vars qw(@ISA $VERSION);
  15. use Carp;
  16.  
  17. use Socket 1.3;
  18. use IO::Socket;
  19. use Time::Local;
  20. use Net::Cmd;
  21. use Net::Config;
  22.  
  23. $VERSION = do { my @r=(q$Revision: 1.1 $=~/\d+/g); sprintf "%d."."%02d"x$#r,@r};
  24. @ISA     = qw(Exporter Net::Cmd IO::Socket::INET);
  25.  
  26. sub new
  27. {
  28.  my $pkg  = shift;
  29.  my $peer = shift;
  30.  my %arg  = @_; 
  31.  
  32.  my $host = $peer;
  33.  my $fire = undef;
  34.  
  35.  unless(defined inet_aton($peer)) # GMB: Should I use Net::Ping here ??
  36.   {
  37.    $fire = $ENV{FTP_FIREWALL}
  38.     || $arg{Firewall}
  39.     || $NetConfig{ftp_firewall}
  40.     || undef;
  41.  
  42.    if(defined $fire)
  43.     {
  44.      $peer = $fire;
  45.      delete $arg{Port};
  46.     }
  47.   }
  48.  
  49.  my $ftp = $pkg->SUPER::new(PeerAddr => $peer, 
  50.                 PeerPort => $arg{Port} || 'ftp(21)',
  51.                 Proto    => 'tcp',
  52.                 Timeout  => defined $arg{Timeout}
  53.                         ? $arg{Timeout}
  54.                         : 120
  55.                ) or return undef;
  56.  
  57.  ${*$ftp}{'net_ftp_host'}     = $host;        # Remote hostname
  58.  ${*$ftp}{'net_ftp_type'}     = 'A';        # ASCII/binary/etc mode
  59.  
  60.  ${*$ftp}{'net_ftp_firewall'} = $fire
  61.     if(defined $fire);
  62.  
  63.  ${*$ftp}{'net_ftp_passive'} = int
  64.     exists $arg{Passive}
  65.         ? $arg{Passive}
  66.         : exists $ENV{FTP_PASSIVE}
  67.         ? $ENV{FTP_PASSIVE}
  68.         : defined $fire
  69.             ? $NetConfig{ftp_ext_passive}
  70.             : $NetConfig{ftp_int_passive};    # Whew! :-)
  71.  
  72.  $ftp->autoflush(1);
  73.  
  74.  $ftp->debug(exists $arg{Debug} ? $arg{Debug} : undef);
  75.  
  76.  unless ($ftp->response() == CMD_OK)
  77.   {
  78.    $ftp->SUPER::close();
  79.    undef $ftp;
  80.   }
  81.  
  82.  $ftp;
  83. }
  84.  
  85. ##
  86. ## User interface methods
  87. ##
  88.  
  89. sub quit
  90. {
  91.  my $ftp = shift;
  92.  
  93.  $ftp->_QUIT
  94.     && $ftp->close;
  95. }
  96.  
  97. sub close
  98. {
  99.  my $ftp = shift;
  100.  
  101.  ref($ftp) 
  102.     && defined fileno($ftp)
  103.     && $ftp->SUPER::close;
  104. }
  105.  
  106. sub DESTROY { shift->close }
  107.  
  108. sub ascii  { shift->type('A',@_); }
  109. sub binary { shift->type('I',@_); }
  110.  
  111. sub ebcdic
  112. {
  113.  carp "TYPE E is unsupported, shall default to I";
  114.  shift->type('E',@_);
  115. }
  116.  
  117. sub byte
  118. {
  119.  carp "TYPE L is unsupported, shall default to I";
  120.  shift->type('L',@_);
  121. }
  122.  
  123. # Allow the user to send a command directly, BE CAREFUL !!
  124.  
  125. sub quot
  126.  my $ftp = shift;
  127.  my $cmd = shift;
  128.  
  129.  $ftp->command( uc $cmd, @_);
  130.  $ftp->response();
  131. }
  132.  
  133. sub mdtm
  134. {
  135.  my $ftp  = shift;
  136.  my $file = shift;
  137.  
  138.  $ftp->_MDTM($file) && $ftp->message =~ /(\d{4})(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)/
  139.     ? timegm($6,$5,$4,$3,$2-1,$1 - 1900)
  140.     : undef;
  141. }
  142.  
  143. sub size
  144. {
  145.  my $ftp  = shift;
  146.  my $file = shift;
  147.  
  148.  $ftp->_SIZE($file)
  149.     ? ($ftp->message =~ /(\d+)/)[0]
  150.     : undef;
  151. }
  152.  
  153. sub login
  154. {
  155.  my($ftp,$user,$pass,$acct) = @_;
  156.  my($ok,$ruser);
  157.  
  158.  unless (defined $user)
  159.   {
  160.    require Net::Netrc;
  161.  
  162.    my $rc = Net::Netrc->lookup(${*$ftp}{'net_ftp_host'});
  163.  
  164.    ($user,$pass,$acct) = $rc->lpa()
  165.     if ($rc);
  166.   }
  167.  
  168.  $user ||= "anonymous";
  169.  $ruser = $user;
  170.  
  171.  if(defined ${*$ftp}{'net_ftp_firewall'})
  172.   {
  173.    $user .= "@" . ${*$ftp}{'net_ftp_host'};
  174.   }
  175.  
  176.  $ok = $ftp->_USER($user);
  177.  
  178.  # Some dumb firewall's don't prefix the connection messages
  179.  $ok = $ftp->response()
  180.     if($ok == CMD_OK && $ftp->code == 220 && $user =~ /\@/);
  181.  
  182.  if ($ok == CMD_MORE)
  183.   {
  184.    unless(defined $pass)
  185.     {
  186.      require Net::Netrc;
  187.  
  188.      my $rc = Net::Netrc->lookup(${*$ftp}{'net_ftp_host'}, $ruser);
  189.  
  190.      ($ruser,$pass,$acct) = $rc->lpa()
  191.     if ($rc);
  192.  
  193.      $pass = "-" . (getpwuid($>))[0] . "@" 
  194.         if (!defined $pass && $ruser =~ /^anonymous/o);
  195.     }
  196.  
  197.    $ok = $ftp->_PASS($pass || "");
  198.   }
  199.  
  200.  $ok = $ftp->_ACCT($acct || "")
  201.     if ($ok == CMD_MORE);
  202.  
  203.  $ftp->authorize()
  204.     if($ok == CMD_OK && defined ${*$ftp}{'net_ftp_firewall'});
  205.  
  206.  $ok == CMD_OK;
  207. }
  208.  
  209. sub authorize
  210. {
  211.  @_ >= 1 || @_ <= 3 or croak 'usage: $ftp->authorize( [AUTH [, RESP]])';
  212.  
  213.  my($ftp,$auth,$resp) = @_;
  214.  
  215.  unless(defined $resp)
  216.   {
  217.    require Net::Netrc;
  218.  
  219.    $auth ||= (getpwuid($>))[0];
  220.  
  221.    my $rc = Net::Netrc->lookup(${*$ftp}{'net_ftp_firewall'}, $auth)
  222.         || Net::Netrc->lookup(${*$ftp}{'net_ftp_firewall'});
  223.  
  224.    ($auth,$resp) = $rc->lpa()
  225.      if($rc);
  226.   }
  227.  
  228.  my $ok = $ftp->_AUTH($auth || "");
  229.  
  230.  $ok = $ftp->_RESP($resp || "")
  231.     if ($ok == CMD_MORE);
  232.  
  233.  $ok == CMD_OK;
  234. }
  235.  
  236. sub rename
  237. {
  238.  @_ == 3 or croak 'usage: $ftp->rename(FROM, TO)';
  239.  
  240.  my($ftp,$from,$to) = @_;
  241.  
  242.  $ftp->_RNFR($from)
  243.     && $ftp->_RNTO($to);
  244. }
  245.  
  246. sub type
  247. {
  248.  my $ftp = shift;
  249.  my $type = shift;
  250.  my $oldval = ${*$ftp}{'net_ftp_type'};
  251.  
  252.  return $oldval
  253.     unless (defined $type);
  254.  
  255.  return undef
  256.     unless ($ftp->_TYPE($type,@_));
  257.  
  258.  ${*$ftp}{'net_ftp_type'} = join(" ",$type,@_);
  259.  
  260.  $oldval;
  261. }
  262.  
  263. my($TELNET_IAC,$TELNET_IP,$TELNET_DM) = (255,244,242);
  264.  
  265. sub abort
  266. {
  267.  my $ftp = shift;
  268.  
  269.  send($ftp,pack("CC",$TELNET_IAC,$TELNET_IP),0);
  270.  send($ftp,pack("C", $TELNET_IAC),MSG_OOB);
  271.  send($ftp,pack("C", $TELNET_DM),0);
  272.  
  273.  $ftp->command("ABOR");
  274.  
  275.  defined ${*$ftp}{'net_ftp_dataconn'}
  276.     ? ${*$ftp}{'net_ftp_dataconn'}->close()
  277.     : $ftp->response();
  278.  
  279.  $ftp->response()
  280.     if $ftp->status == CMD_REJECT;
  281.  
  282.  $ftp->status == CMD_OK;
  283. }
  284.  
  285. sub get
  286. {
  287.  my($ftp,$remote,$local,$where) = @_;
  288.  
  289.  my($loc,$len,$buf,$resp,$localfd,$data);
  290.  local *FD;
  291.  
  292.  $localfd = ref($local) ? fileno($local)
  293.             : undef;
  294.  
  295.  ($local = $remote) =~ s#^.*/##
  296.     unless(defined $local);
  297.  
  298.  ${*$ftp}{'net_ftp_rest'} = $where
  299.     if ($where);
  300.  
  301.  delete ${*$ftp}{'net_ftp_port'};
  302.  delete ${*$ftp}{'net_ftp_pasv'};
  303.  
  304.  $data = $ftp->retr($remote) or
  305.     return undef;
  306.  
  307.  if(defined $localfd)
  308.   {
  309.    $loc = $local;
  310.   }
  311.  else
  312.   {
  313.    $loc = \*FD;
  314.  
  315.    unless(($where) ? open($loc,">>$local") : open($loc,">$local"))
  316.     {
  317.      carp "Cannot open Local file $local: $!\n";
  318.      $data->abort;
  319.      return undef;
  320.     }
  321.   }
  322.  
  323.  if(($ftp->type =~ /^I/) && !binmode($loc))
  324.   {
  325.    carp "Cannot binmode Local file $local: $!\n";
  326.    $data->abort;
  327.    return undef;
  328.   }
  329.  
  330.  $buf = '';
  331.  
  332.  do
  333.   {
  334.    $len = $data->read($buf,1024);
  335.   }
  336.  while($len > 0 && syswrite($loc,$buf,$len) == $len);
  337.  
  338.  close($loc)
  339.     unless defined $localfd;
  340.  
  341.  $data->close(); # implied $ftp->response
  342.  
  343.  return $local;
  344. }
  345.  
  346. sub cwd
  347. {
  348.  @_ == 2 || @_ == 3 or croak 'usage: $ftp->cwd( [ DIR ] )';
  349.  
  350.  my($ftp,$dir) = @_;
  351.  
  352.  $dir ||= "/";
  353.  
  354.  $dir eq ".."
  355.     ? $ftp->_CDUP()
  356.     : $ftp->_CWD($dir);
  357. }
  358.  
  359. sub cdup
  360. {
  361.  @_ == 1 or croak 'usage: $ftp->cdup()';
  362.  $_[0]->_CDUP;
  363. }
  364.  
  365. sub pwd
  366. {
  367.  @_ == 1 || croak 'usage: $ftp->pwd()';
  368.  my $ftp = shift;
  369.  
  370.  $ftp->_PWD();
  371.  $ftp->_extract_path;
  372. }
  373.  
  374. sub rmdir
  375. {
  376.  @_ == 2 || croak 'usage: $ftp->rmdir( DIR )';
  377.  
  378.  $_[0]->_RMD($_[1]);
  379. }
  380.  
  381. sub mkdir
  382. {
  383.  @_ == 2 || @_ == 3 or croak 'usage: $ftp->mkdir( DIR [, RECURSE ] )';
  384.  
  385.  my($ftp,$dir,$recurse) = @_;
  386.  
  387.  $ftp->_MKD($dir) || $recurse or
  388.     return undef;
  389.  
  390.  my $path = $dir;
  391.  
  392.  unless($ftp->ok)
  393.   {
  394.    my @path = split(m#(?=/+)#, $dir);
  395.  
  396.    $path = "";
  397.  
  398.    while(@path)
  399.     {
  400.      $path .= shift @path;
  401.  
  402.      $ftp->_MKD($path);
  403.      $path = $ftp->_extract_path($path);
  404.  
  405.      # 521 means directory already exists
  406.      last
  407.         unless $ftp->ok || $ftp->code == 521 || $ftp->code == 550;
  408.     }
  409.   }
  410.  
  411.  $ftp->_extract_path($path);
  412. }
  413.  
  414. sub delete
  415. {
  416.  @_ == 2 || croak 'usage: $ftp->delete( FILENAME )';
  417.  
  418.  $_[0]->_DELE($_[1]);
  419. }
  420.  
  421. sub put        { shift->_store_cmd("stor",@_) }
  422. sub put_unique { shift->_store_cmd("stou",@_) }
  423. sub append     { shift->_store_cmd("appe",@_) }
  424.  
  425. sub nlst { shift->_data_cmd("NLST",@_) }
  426. sub list { shift->_data_cmd("LIST",@_) }
  427. sub retr { shift->_data_cmd("RETR",@_) }
  428. sub stor { shift->_data_cmd("STOR",@_) }
  429. sub stou { shift->_data_cmd("STOU",@_) }
  430. sub appe { shift->_data_cmd("APPE",@_) }
  431.  
  432. sub _store_cmd 
  433. {
  434.  my($ftp,$cmd,$local,$remote) = @_;
  435.  my($loc,$sock,$len,$buf,$localfd);
  436.  local *FD;
  437.  use File::Basename;
  438.  
  439.  $localfd = ref($local) ? fileno($local)
  440.             : undef;
  441.  
  442.  unless(defined $remote)
  443.   {
  444.    croak 'Must specify remote filename with stream input'
  445.     if defined $localfd;
  446.  
  447. #  ($remote = $local) =~ s%.*/%%;
  448.    $remote = basename($local);
  449.   }
  450.  
  451.  if(defined $localfd)
  452.   {
  453.    $loc = $local;
  454.   }
  455.  else
  456.   {
  457.    $loc = \*FD;
  458.  
  459.    unless(open($loc,"<$local"))
  460.     {
  461.      carp "Cannot open Local file $local: $!\n";
  462.      return undef;
  463.     }
  464.   }
  465.  
  466.  if(($ftp->type =~ /^I/) && !binmode($loc))
  467.   {
  468.    carp "Cannot binmode Local file $local: $!\n";
  469.    return undef;
  470.   }
  471.  
  472.  delete ${*$ftp}{'net_ftp_port'};
  473.  delete ${*$ftp}{'net_ftp_pasv'};
  474.  
  475.  $sock = $ftp->_data_cmd($cmd, $remote) or 
  476.     return undef;
  477.  
  478.  while(1)
  479.   {
  480.    last unless $len = sysread($loc,$buf="",1024);
  481.  
  482.    unless($sock->write($buf,$len) == $len)
  483.     {
  484.      $sock->abort;
  485.      close($loc)
  486.     unless defined $localfd;
  487.      return undef;
  488.     }
  489.   }
  490.  
  491.  $sock->close();
  492.  
  493.  close($loc)
  494.     unless defined $localfd;
  495.  
  496.  ($remote) = $ftp->message =~ /unique file name:\s*(\S*)\s*\)/
  497.     if ('STOU' eq uc $cmd);
  498.  
  499.  return $remote;
  500. }
  501.  
  502. sub port
  503. {
  504.  @_ == 1 || @_ == 2 or croak 'usage: $ftp->port([PORT])';
  505.  
  506.  my($ftp,$port) = @_;
  507.  my $ok;
  508.  
  509.  delete ${*$ftp}{'net_ftp_intern_port'};
  510.  
  511.  unless(defined $port)
  512.   {
  513.    # create a Listen socket at same address as the command socket
  514.  
  515.    ${*$ftp}{'net_ftp_listen'} ||= IO::Socket::INET->new(Listen    => 5,
  516.                                         Proto     => 'tcp',
  517.                                         LocalAddr => $ftp->sockhost, 
  518.                                        );
  519.   
  520.    my $listen = ${*$ftp}{'net_ftp_listen'};
  521.  
  522.    my($myport, @myaddr) = ($listen->sockport, split(/\./,$listen->sockhost));
  523.  
  524.    $port = join(',', @myaddr, $myport >> 8, $myport & 0xff);
  525.  
  526.    ${*$ftp}{'net_ftp_intern_port'} = 1;
  527.   }
  528.  
  529.  $ok = $ftp->_PORT($port);
  530.  
  531.  ${*$ftp}{'net_ftp_port'} = $port;
  532.  
  533.  $ok;
  534. }
  535.  
  536. sub ls  { shift->_list_cmd("NLST",@_); }
  537. sub dir { shift->_list_cmd("LIST",@_); }
  538.  
  539. sub pasv
  540. {
  541.  @_ == 1 or croak 'usage: $ftp->pasv()';
  542.  
  543.  my $ftp = shift;
  544.  
  545.  delete ${*$ftp}{'net_ftp_intern_port'};
  546.  
  547.  $ftp->_PASV && $ftp->message =~ /(\d+(,\d+)+)/
  548.     ? ${*$ftp}{'net_ftp_pasv'} = $1
  549.     : undef;    
  550. }
  551.  
  552. sub unique_name
  553. {
  554.  my $ftp = shift;
  555.  ${*$ftp}{'net_ftp_unique'} || undef;
  556. }
  557.  
  558. ##
  559. ## Depreciated methods
  560. ##
  561.  
  562. sub lsl
  563. {
  564.  carp "Use of Net::FTP::lsl depreciated, use 'dir'"
  565.     if $^W;
  566.  goto &dir;
  567. }
  568.  
  569. sub authorise
  570. {
  571.  carp "Use of Net::FTP::authorise depreciated, use 'authorize'"
  572.     if $^W;
  573.  goto &authorize;
  574. }
  575.  
  576.  
  577. ##
  578. ## Private methods
  579. ##
  580.  
  581. sub _extract_path
  582. {
  583.  my($ftp, $path) = @_;
  584.  
  585.  # This tries to work both with and without the quote doubling
  586.  # convention (RFC 959 requires it, but the first 3 servers I checked
  587.  # didn't implement it).  It will fail on a server which uses a quote in
  588.  # the message which isn't a part of or surrounding the path.
  589.  $ftp->ok &&
  590.     $ftp->message =~ /(?:^|\s)\"(.*)\"(?:$|\s)/ &&
  591.     ($path = $1) =~ s/\"\"/\"/g;
  592.  
  593.  $path;
  594. }
  595.  
  596. ##
  597. ## Communication methods
  598. ##
  599.  
  600. sub _dataconn
  601. {
  602.  my $ftp = shift;
  603.  my $data = undef;
  604.  my $pkg = "Net::FTP::" . $ftp->type;
  605.  
  606.  $pkg =~ s/ /_/g;
  607.  
  608.  delete ${*$ftp}{'net_ftp_dataconn'};
  609.  
  610.  if(defined ${*$ftp}{'net_ftp_pasv'})
  611.   {
  612.    my @port = split(/,/,${*$ftp}{'net_ftp_pasv'});
  613.  
  614.    $data = $pkg->new(PeerAddr => join(".",@port[0..3]),
  615.                      PeerPort => $port[4] * 256 + $port[5],
  616.                      Proto    => 'tcp'
  617.                     );
  618.   }
  619.  elsif(defined ${*$ftp}{'net_ftp_listen'})
  620.   {
  621.    $data = ${*$ftp}{'net_ftp_listen'}->accept($pkg);
  622.    close(delete ${*$ftp}{'net_ftp_listen'});
  623.   }
  624.  
  625.  if($data)
  626.   {
  627.    ${*$data} = "";
  628.    $data->timeout($ftp->timeout);
  629.    ${*$ftp}{'net_ftp_dataconn'} = $data;
  630.    ${*$data}{'net_ftp_cmd'} = $ftp;
  631.   }
  632.  
  633.  $data;
  634. }
  635.  
  636. sub _list_cmd
  637. {
  638.  my $ftp = shift;
  639.  my $cmd = uc shift;
  640.  
  641.  delete ${*$ftp}{'net_ftp_port'};
  642.  delete ${*$ftp}{'net_ftp_pasv'};
  643.  
  644.  my $data = $ftp->_data_cmd($cmd,@_);
  645.  
  646.  return undef
  647.     unless(defined $data);
  648.  
  649.  bless $data, "Net::FTP::A"; # Force ASCII mode
  650.  
  651.  my $databuf = '';
  652.  my $buf = '';
  653.  
  654.  while($data->read($databuf,1024))
  655.   {
  656.    $buf .= $databuf;
  657.   }
  658.  
  659.  my $list = [ split(/\n/,$buf) ];
  660.  
  661.  $data->close();
  662.  
  663.  wantarray ? @{$list}
  664.            : $list;
  665. }
  666.  
  667. sub _data_cmd
  668. {
  669.  my $ftp = shift;
  670.  my $cmd = uc shift;
  671.  my $ok = 1;
  672.  my $where = delete ${*$ftp}{'net_ftp_rest'} || 0;
  673.  
  674.  if(${*$ftp}{'net_ftp_passive'} &&
  675.      !defined ${*$ftp}{'net_ftp_pasv'} &&
  676.      !defined ${*$ftp}{'net_ftp_port'})
  677.   {
  678.    my $data = undef;
  679.  
  680.    $ok = defined $ftp->pasv;
  681.    $ok = $ftp->_REST($where)
  682.     if $ok && $where;
  683.  
  684.    if($ok)
  685.     {
  686.      $ftp->command($cmd,@_);
  687.      $data = $ftp->_dataconn();
  688.      $ok = CMD_INFO == $ftp->response();
  689.       return $data
  690.         if $ok;
  691.       $data->_close
  692.         if $data;
  693.     }
  694.     return undef;
  695.   }
  696.  
  697.  $ok = $ftp->port
  698.     unless (defined ${*$ftp}{'net_ftp_port'} ||
  699.             defined ${*$ftp}{'net_ftp_pasv'});
  700.  
  701.  $ok = $ftp->_REST($where)
  702.     if $ok && $where;
  703.  
  704.  return undef
  705.     unless $ok;
  706.  
  707.  $ftp->command($cmd,@_);
  708.  
  709.  return 1
  710.     if(defined ${*$ftp}{'net_ftp_pasv'});
  711.  
  712.  $ok = CMD_INFO == $ftp->response();
  713.  
  714.  return $ok 
  715.     unless exists ${*$ftp}{'net_ftp_intern_port'};
  716.  
  717.  return $ftp->_dataconn()
  718.     if $ok;
  719.  
  720.  close(delete ${*$ftp}{'net_ftp_listen'});
  721.  
  722.  return undef;
  723. }
  724.  
  725. ##
  726. ## Over-ride methods (Net::Cmd)
  727. ##
  728.  
  729. sub debug_text { $_[2] =~ /^(pass|resp)/i ? "$1 ....\n" : $_[2]; }
  730.  
  731. sub command
  732. {
  733.  my $ftp = shift;
  734.  
  735.  delete ${*$ftp}{'net_ftp_port'};
  736.  $ftp->SUPER::command(@_);
  737. }
  738.  
  739. sub response
  740. {
  741.  my $ftp = shift;
  742.  my $code = $ftp->SUPER::response();
  743.  
  744.  delete ${*$ftp}{'net_ftp_pasv'}
  745.     if ($code != CMD_MORE && $code != CMD_INFO);
  746.  
  747.  $code;
  748. }
  749.  
  750. sub parse_response
  751. {
  752.  return ($1, $2 eq "-")
  753.     if $_[1] =~ s/^(\d\d\d)(.?)//o;
  754.  
  755.  my $ftp = shift;
  756.  
  757.  # Darn MS FTP server is a load of CRAP !!!!
  758.  return ()
  759.     unless ${*$ftp}{'net_cmd_code'};
  760.  
  761.  (${*$ftp}{'net_cmd_code'},1);
  762. }
  763.  
  764. ##
  765. ## Allow 2 servers to talk directly
  766. ##
  767.  
  768. sub pasv_xfer
  769. {
  770.  my($sftp,$sfile,$dftp,$dfile) = @_;
  771.  
  772.  ($dfile = $sfile) =~ s#.*/##
  773.     unless(defined $dfile);
  774.  
  775.  my $port = $sftp->pasv or
  776.     return undef;
  777.  
  778.  unless($dftp->port($port) && $sftp->retr($sfile) && $dftp->stou($dfile))
  779.   {
  780.    $sftp->abort;
  781.    $dftp->abort;
  782.    return undef;
  783.   }
  784.  
  785.  $dftp->pasv_wait($sftp);
  786. }
  787.  
  788. sub pasv_wait
  789. {
  790.  @_ == 2 or croak 'usage: $ftp->pasv_wait(NON_PASV_FTP)';
  791.  
  792.  my($ftp, $non_pasv) = @_;
  793.  my($file,$rin,$rout);
  794.  
  795.  vec($rin,fileno($ftp),1) = 1;
  796.  select($rout=$rin, undef, undef, undef);
  797.  
  798.  $ftp->response();
  799.  $non_pasv->response();
  800.  
  801.  return undef
  802.     unless $ftp->ok() && $non_pasv->ok();
  803.  
  804.  return $1
  805.     if $ftp->message =~ /unique file name:\s*(\S*)\s*\)/;
  806.  
  807.  return $1
  808.     if $non_pasv->message =~ /unique file name:\s*(\S*)\s*\)/;
  809.  
  810.  return 1;
  811. }
  812.  
  813. sub cmd { shift->command(@_)->responce() }
  814.  
  815. ########################################
  816. #
  817. # RFC959 commands
  818. #
  819.  
  820. sub _ABOR { shift->command("ABOR")->response()     == CMD_OK }
  821. sub _CDUP { shift->command("CDUP")->response()     == CMD_OK }
  822. sub _NOOP { shift->command("NOOP")->response()     == CMD_OK }
  823. sub _PASV { shift->command("PASV")->response()     == CMD_OK }
  824. sub _QUIT { shift->command("QUIT")->response()     == CMD_OK }
  825. sub _DELE { shift->command("DELE",@_)->response() == CMD_OK }
  826. sub _CWD  { shift->command("CWD", @_)->response() == CMD_OK }
  827. sub _PORT { shift->command("PORT",@_)->response() == CMD_OK }
  828. sub _RMD  { shift->command("RMD", @_)->response() == CMD_OK }
  829. sub _MKD  { shift->command("MKD", @_)->response() == CMD_OK }
  830. sub _PWD  { shift->command("PWD", @_)->response() == CMD_OK }
  831. sub _TYPE { shift->command("TYPE",@_)->response() == CMD_OK }
  832. sub _RNTO { shift->command("RNTO",@_)->response() == CMD_OK }
  833. sub _ACCT { shift->command("ACCT",@_)->response() == CMD_OK }
  834. sub _RESP { shift->command("RESP",@_)->response() == CMD_OK }
  835. sub _MDTM { shift->command("MDTM",@_)->response() == CMD_OK }
  836. sub _SIZE { shift->command("SIZE",@_)->response() == CMD_OK }
  837. sub _APPE { shift->command("APPE",@_)->response() == CMD_INFO }
  838. sub _LIST { shift->command("LIST",@_)->response() == CMD_INFO }
  839. sub _NLST { shift->command("NLST",@_)->response() == CMD_INFO }
  840. sub _RETR { shift->command("RETR",@_)->response() == CMD_INFO }
  841. sub _STOR { shift->command("STOR",@_)->response() == CMD_INFO }
  842. sub _STOU { shift->command("STOU",@_)->response() == CMD_INFO }
  843. sub _RNFR { shift->command("RNFR",@_)->response() == CMD_MORE }
  844. sub _REST { shift->command("REST",@_)->response() == CMD_MORE }
  845. sub _USER { shift->command("user",@_)->response() } # A certain brain dead firewall :-)
  846. sub _PASS { shift->command("PASS",@_)->response() }
  847. sub _AUTH { shift->command("AUTH",@_)->response() }
  848.  
  849. sub _ALLO { shift->unsupported(@_) }
  850. sub _SMNT { shift->unsupported(@_) }
  851. sub _HELP { shift->unsupported(@_) }
  852. sub _MODE { shift->unsupported(@_) }
  853. sub _SITE { shift->unsupported(@_) }
  854. sub _SYST { shift->unsupported(@_) }
  855. sub _STAT { shift->unsupported(@_) }
  856. sub _STRU { shift->unsupported(@_) }
  857. sub _REIN { shift->unsupported(@_) }
  858.  
  859. ##
  860. ## Generic data connection package
  861. ##
  862.  
  863. package Net::FTP::dataconn;
  864.  
  865. use Carp;
  866. use vars qw(@ISA $timeout);
  867. use Net::Cmd;
  868.  
  869. @ISA = qw(IO::Socket::INET);
  870.  
  871. sub abort
  872. {
  873.  my $data = shift;
  874.  my $ftp  = ${*$data}{'net_ftp_cmd'};
  875.  
  876.  $ftp->abort; # this will close me
  877. }
  878.  
  879. sub _close
  880. {
  881.  my $data = shift;
  882.  my $ftp  = ${*$data}{'net_ftp_cmd'};
  883.  
  884.  $data->SUPER::close();
  885.  
  886.  delete ${*$ftp}{'net_ftp_dataconn'}
  887.     if exists ${*$ftp}{'net_ftp_dataconn'} &&
  888.         $data == ${*$ftp}{'net_ftp_dataconn'};
  889. }
  890.  
  891. sub close
  892. {
  893.  my $data = shift;
  894.  my $ftp  = ${*$data}{'net_ftp_cmd'};
  895.  
  896.  $data->_close;
  897.  
  898.  $ftp->response() == CMD_OK &&
  899.     $ftp->message =~ /unique file name:\s*(\S*)\s*\)/ &&
  900.     (${*$ftp}{'net_ftp_unique'} = $1);
  901.  
  902.  $ftp->status == CMD_OK;
  903. }
  904.  
  905. sub _select
  906. {
  907.  my    $data     = shift;
  908.  local *timeout = \$_[0]; shift;
  909.  my    $rw     = shift;
  910.  
  911.  my($rin,$win);
  912.  
  913.  return 1 unless $timeout;
  914.  
  915.  $rin = '';
  916.  vec($rin,fileno($data),1) = 1;
  917.  
  918.  $win = $rw ? undef : $rin;
  919.  $rin = undef unless $rw;
  920.  
  921.  my $nfound = select($rin, $win, undef, $timeout);
  922.  
  923.  croak "select: $!"
  924.     if $nfound < 0;
  925.  
  926.  return $nfound;
  927. }
  928.  
  929. sub can_read
  930. {
  931.  my    $data    = shift;
  932.  local *timeout = \$_[0];
  933.  
  934.  $data->_select($timeout,1);
  935. }
  936.  
  937. sub can_write
  938. {
  939.  my    $data    = shift;
  940.  local *timeout = \$_[0];
  941.  
  942.  $data->_select($timeout,0);
  943. }
  944.  
  945. sub cmd
  946. {
  947.  my $ftp = shift;
  948.  
  949.  ${*$ftp}{'net_ftp_cmd'};
  950. }
  951.  
  952.  
  953. @Net::FTP::L::ISA = qw(Net::FTP::I);
  954. @Net::FTP::E::ISA = qw(Net::FTP::I);
  955.  
  956. ##
  957. ## Package to read/write on ASCII data connections
  958. ##
  959.  
  960. package Net::FTP::A;
  961.  
  962. use vars qw(@ISA $buf);
  963. use Carp;
  964.  
  965. @ISA = qw(Net::FTP::dataconn);
  966.  
  967. sub read
  968. {
  969.  my    $data     = shift;
  970.  local *buf     = \$_[0]; shift;
  971.  my    $size     = shift || croak 'read($buf,$size,[$offset])';
  972.  my    $offset     = shift || 0;
  973.  my    $timeout = $data->timeout;
  974.  
  975.  croak "Bad offset"
  976.     if($offset < 0);
  977.  
  978.  $offset = length $buf
  979.     if($offset > length $buf);
  980.  
  981.  ${*$data} ||= "";
  982.  my $l = 0;
  983.  
  984.  READ:
  985.   {
  986.    $data->can_read($timeout) or
  987.     croak "Timeout";
  988.  
  989.    my $n = sysread($data, ${*$data}, $size, length ${*$data});
  990.  
  991.    return $n
  992.     unless($n >= 0);
  993.  
  994.    ${*$data} =~ s/(\015)?(?!\012)\Z//so;
  995.    my $lf = $1 || "";
  996.  
  997.    ${*$data} =~ s/\015\012/\n/sgo;
  998.  
  999.    substr($buf,$offset) = ${*$data};
  1000.  
  1001.    $l += length(${*$data});
  1002.    $offset += length(${*$data});
  1003.  
  1004.    ${*$data} = $lf;
  1005.    
  1006.    redo READ
  1007.      if($l == 0 && $n > 0);
  1008.  
  1009.    if($n == 0 && $l == 0)
  1010.     {
  1011.      substr($buf,$offset) = ${*$data};
  1012.      ${*$data} = "";
  1013.     }
  1014.   }
  1015.  
  1016.  return $l;
  1017. }
  1018.  
  1019. sub write
  1020. {
  1021.  my    $data     = shift;
  1022.  local *buf     = \$_[0]; shift;
  1023.  my    $size     = shift || croak 'write($buf,$size,[$timeout])';
  1024.  my    $timeout = @_ ? shift : $data->timeout;
  1025.  
  1026.  $data->can_write($timeout) or
  1027.     croak "Timeout";
  1028.  
  1029.  # What is previous pkt ended in \015 or not ??
  1030.  
  1031.  my $tmp;
  1032. #($tmp = $buf) =~ s/(?!\015)\012/\015\012/sg;
  1033.  ($tmp = $buf) =~ s/\n/\015\012/sg;
  1034.  
  1035.  my $len = $size + length($tmp) - length($buf);
  1036.  my $wrote = syswrite($data, $tmp, $len);
  1037.  
  1038.  if($wrote >= 0)
  1039.   {
  1040.    $wrote = $wrote == $len ? $size
  1041.                : $len - $wrote
  1042.   }
  1043.  
  1044.  return $wrote;
  1045. }
  1046.  
  1047. ##
  1048. ## Package to read/write on BINARY data connections
  1049. ##
  1050.  
  1051. package Net::FTP::I;
  1052.  
  1053. use vars qw(@ISA $buf);
  1054. use Carp;
  1055.  
  1056. @ISA = qw(Net::FTP::dataconn);
  1057.  
  1058. sub read
  1059. {
  1060.  my    $data     = shift;
  1061.  local *buf     = \$_[0]; shift;
  1062.  my    $size    = shift || croak 'read($buf,$size,[$timeout])';
  1063.  my    $timeout = @_ ? shift : $data->timeout;
  1064.  
  1065.  $data->can_read($timeout) or
  1066.     croak "Timeout";
  1067.  
  1068.  my $n = sysread($data, $buf, $size);
  1069.  
  1070.  $n;
  1071. }
  1072.  
  1073. sub write
  1074. {
  1075.  my    $data    = shift;
  1076.  local *buf     = \$_[0]; shift;
  1077.  my    $size    = shift || croak 'write($buf,$size,[$timeout])';
  1078.  my    $timeout = @_ ? shift : $data->timeout;
  1079.  
  1080.  $data->can_write($timeout) or
  1081.     croak "Timeout";
  1082.  
  1083.  syswrite($data, $buf, $size);
  1084. }
  1085.  
  1086.  
  1087. 1;
  1088.  
  1089. __END__
  1090.  
  1091. =head1 NAME
  1092.  
  1093. Net::FTP - FTP Client class
  1094.  
  1095. =head1 SYNOPSIS
  1096.  
  1097.     use Net::FTP;
  1098.     
  1099.     $ftp = Net::FTP->new("some.host.name");
  1100.     $ftp->login("anonymous","me@here.there");
  1101.     $ftp->cwd("/pub");
  1102.     $ftp->get("that.file");
  1103.     $ftp->quit;
  1104.  
  1105. =head1 DESCRIPTION
  1106.  
  1107. C<Net::FTP> is a class implementing a simple FTP client in Perl as
  1108. described in RFC959.  It provides wrappers for a subset of the RFC959
  1109. commands.
  1110.  
  1111. =head1 OVERVIEW
  1112.  
  1113. FTP stands for File Transfer Protocol.  It is a way of transferring
  1114. files between networked machines.  The protocol defines a client
  1115. (whose commands are provided by this module) and a server (not
  1116. implemented in this module).  Communication is always initiated by the
  1117. client, and the server responds with a message and a status code (and
  1118. sometimes with data).
  1119.  
  1120. The FTP protocol allows files to be sent to or fetched from the
  1121. server.  Each transfer involves a B<local file> (on the client) and a
  1122. B<remote file> (on the server).  In this module, the same file name
  1123. will be used for both local and remote if only one is specified.  This
  1124. means that transferring remote file C</path/to/file> will try to put
  1125. that file in C</path/to/file> locally, unless you specify a local file
  1126. name.
  1127.  
  1128. The protocol also defines several standard B<translations> which the
  1129. file can undergo during transfer.  These are ASCII, EBCDIC, binary,
  1130. and byte.  ASCII is the default type, and indicates that the sender of
  1131. files will translate the ends of lines to a standard representation
  1132. which the receiver will then translate back into their local
  1133. representation.  EBCDIC indicates the file being transferred is in
  1134. EBCDIC format.  Binary (also known as image) format sends the data as
  1135. a contiguous bit stream.  Byte format transfers the data as bytes, the
  1136. values of which remain the same regardless of differences in byte size
  1137. between the two machines (in theory - in practice you should only use
  1138. this if you really know what you're doing).
  1139.  
  1140. =head1 CONSTRUCTOR
  1141.  
  1142. =over 4
  1143.  
  1144. =item new (HOST [,OPTIONS])
  1145.  
  1146. This is the constructor for a new Net::FTP object. C<HOST> is the
  1147. name of the remote host to which a FTP connection is required.
  1148.  
  1149. C<OPTIONS> are passed in a hash like fashion, using key and value pairs.
  1150. Possible options are:
  1151.  
  1152. B<Firewall> - The name of a machine which acts as a FTP firewall. This can be
  1153. overridden by an environment variable C<FTP_FIREWALL>. If specified, and the
  1154. given host cannot be directly connected to, then the
  1155. connection is made to the firewall machine and the string C<@hostname> is
  1156. appended to the login identifier. This kind of setup is also refered to
  1157. as a ftp proxy.
  1158.  
  1159. B<Port> - The port number to connect to on the remote machine for the
  1160. FTP connection
  1161.  
  1162. B<Timeout> - Set a timeout value (defaults to 120)
  1163.  
  1164. B<Debug> - debug level (see the debug method in L<Net::Cmd>)
  1165.  
  1166. B<Passive> - If set to I<true> then all data transfers will be done using 
  1167. passive mode. This is required for some I<dumb> servers, and some
  1168. firewall configurations.  This can also be set by the environment
  1169. variable C<FTP_PASSIVE>.
  1170.  
  1171. =back
  1172.  
  1173. =head1 METHODS
  1174.  
  1175. Unless otherwise stated all methods return either a I<true> or I<false>
  1176. value, with I<true> meaning that the operation was a success. When a method
  1177. states that it returns a value, failure will be returned as I<undef> or an
  1178. empty list.
  1179.  
  1180. =over 4
  1181.  
  1182. =item login ([LOGIN [,PASSWORD [, ACCOUNT] ] ])
  1183.  
  1184. Log into the remote FTP server with the given login information. If
  1185. no arguments are given then the C<Net::FTP> uses the C<Net::Netrc>
  1186. package to lookup the login information for the connected host.
  1187. If no information is found then a login of I<anonymous> is used.
  1188. If no password is given and the login is I<anonymous> then the users
  1189. Email address will be used for a password.
  1190.  
  1191. If the connection is via a firewall then the C<authorize> method will
  1192. be called with no arguments.
  1193.  
  1194. =item authorize ( [AUTH [, RESP]])
  1195.  
  1196. This is a protocol used by some firewall ftp proxies. It is used
  1197. to authorise the user to send data out.  If both arguments are not specified
  1198. then C<authorize> uses C<Net::Netrc> to do a lookup.
  1199.  
  1200. =item type (TYPE [, ARGS])
  1201.  
  1202. This method will send the TYPE command to the remote FTP server
  1203. to change the type of data transfer. The return value is the previous
  1204. value.
  1205.  
  1206. =item ascii ([ARGS]) binary([ARGS]) ebcdic([ARGS]) byte([ARGS])
  1207.  
  1208. Synonyms for C<type> with the first arguments set correctly
  1209.  
  1210. B<NOTE> ebcdic and byte are not fully supported.
  1211.  
  1212. =item rename ( OLDNAME, NEWNAME )
  1213.  
  1214. Rename a file on the remote FTP server from C<OLDNAME> to C<NEWNAME>. This
  1215. is done by sending the RNFR and RNTO commands.
  1216.  
  1217. =item delete ( FILENAME )
  1218.  
  1219. Send a request to the server to delete C<FILENAME>.
  1220.  
  1221. =item cwd ( [ DIR ] )
  1222.  
  1223. Attempt to change directory to the directory given in C<$dir>.  If
  1224. C<$dir> is C<"..">, the FTP C<CDUP> command is used to attempt to
  1225. move up one directory. If no directory is given then an attempt is made
  1226. to change the directory to the root directory.
  1227.  
  1228. =item cdup ()
  1229.  
  1230. Change directory to the parent of the current directory.
  1231.  
  1232. =item pwd ()
  1233.  
  1234. Returns the full pathname of the current directory.
  1235.  
  1236. =item rmdir ( DIR )
  1237.  
  1238. Remove the directory with the name C<DIR>.
  1239.  
  1240. =item mkdir ( DIR [, RECURSE ])
  1241.  
  1242. Create a new directory with the name C<DIR>. If C<RECURSE> is I<true> then
  1243. C<mkdir> will attempt to create all the directories in the given path.
  1244.  
  1245. Returns the full pathname to the new directory.
  1246.  
  1247. =item ls ( [ DIR ] )
  1248.  
  1249. Get a directory listing of C<DIR>, or the current directory.
  1250.  
  1251. Returns a reference to a list of lines returned from the server.
  1252.  
  1253. =item dir ( [ DIR ] )
  1254.  
  1255. Get a directory listing of C<DIR>, or the current directory in long format.
  1256.  
  1257. Returns a reference to a list of lines returned from the server.
  1258.  
  1259. =item get ( REMOTE_FILE [, LOCAL_FILE ] )
  1260.  
  1261. Get C<REMOTE_FILE> from the server and store locally. C<LOCAL_FILE> may be
  1262. a filename or a filehandle. If not specified the the file will be stored in
  1263. the current directory with the same leafname as the remote file.
  1264.  
  1265. If C<WHERE> is specified, continue transfer of the remote file
  1266. from this point.
  1267.  
  1268. Returns C<LOCAL_FILE>, or the generated local file name if C<LOCAL_FILE>
  1269. is not given.
  1270.  
  1271. =item put ( LOCAL_FILE [, REMOTE_FILE ] )
  1272.  
  1273. Put a file on the remote server. C<LOCAL_FILE> may be a name or a filehandle.
  1274. If C<LOCAL_FILE> is a filehandle then C<REMOTE_FILE> must be specified. If
  1275. C<REMOTE_FILE> is not specified then the file will be stored in the current
  1276. directory with the same leafname as C<LOCAL_FILE>.
  1277.  
  1278. Returns C<REMOTE_FILE>, or the generated remote filename if C<REMOTE_FILE>
  1279. is not given.
  1280.  
  1281. =item put_unique ( LOCAL_FILE [, REMOTE_FILE ] )
  1282.  
  1283. Same as put but uses the C<STOU> command.
  1284.  
  1285. Returns the name of the file on the server.
  1286.  
  1287. =item append ( LOCAL_FILE [, REMOTE_FILE ] )
  1288.  
  1289. Same as put but appends to the file on the remote server.
  1290.  
  1291. Returns C<REMOTE_FILE>, or the generated remote filename if C<REMOTE_FILE>
  1292. is not given.
  1293.  
  1294. =item unique_name ()
  1295.  
  1296. Returns the name of the last file stored on the server using the
  1297. C<STOU> command.
  1298.  
  1299. =item mdtm ( FILE )
  1300.  
  1301. Returns the I<modification time> of the given file
  1302.  
  1303. =item size ( FILE )
  1304.  
  1305. Returns the size in bytes for the given file.
  1306.  
  1307. =back
  1308.  
  1309. The following methods can return different results depending on
  1310. how they are called. If the user explicitly calls either
  1311. of the C<pasv> or C<port> methods then these methods will
  1312. return a I<true> or I<false> value. If the user does not
  1313. call either of these methods then the result will be a
  1314. reference to a C<Net::FTP::dataconn> based object.
  1315.  
  1316. =over 4
  1317.  
  1318. =item nlst ( [ DIR ] )
  1319.  
  1320. Send a C<NLST> command to the server, with an optional parameter.
  1321.  
  1322. =item list ( [ DIR ] )
  1323.  
  1324. Same as C<nlst> but using the C<LIST> command
  1325.  
  1326. =item retr ( FILE )
  1327.  
  1328. Begin the retrieval of a file called C<FILE> from the remote server.
  1329.  
  1330. =item stor ( FILE )
  1331.  
  1332. Tell the server that you wish to store a file. C<FILE> is the
  1333. name of the new file that should be created.
  1334.  
  1335. =item stou ( FILE )
  1336.  
  1337. Same as C<stor> but using the C<STOU> command. The name of the unique
  1338. file which was created on the server will be available via the C<unique_name>
  1339. method after the data connection has been closed.
  1340.  
  1341. =item appe ( FILE )
  1342.  
  1343. Tell the server that we want to append some data to the end of a file
  1344. called C<FILE>. If this file does not exist then create it.
  1345.  
  1346. =back
  1347.  
  1348. If for some reason you want to have complete control over the data connection,
  1349. this includes generating it and calling the C<response> method when required,
  1350. then the user can use these methods to do so.
  1351.  
  1352. However calling these methods only affects the use of the methods above that
  1353. can return a data connection. They have no effect on methods C<get>, C<put>,
  1354. C<put_unique> and those that do not require data connections.
  1355.  
  1356. =over 4
  1357.  
  1358. =item port ( [ PORT ] )
  1359.  
  1360. Send a C<PORT> command to the server. If C<PORT> is specified then it is sent
  1361. to the server. If not the a listen socket is created and the correct information
  1362. sent to the server.
  1363.  
  1364. =item pasv ()
  1365.  
  1366. Tell the server to go into passive mode. Returns the text that represents the
  1367. port on which the server is listening, this text is in a suitable form to
  1368. sent to another ftp server using the C<port> method.
  1369.  
  1370. =back
  1371.  
  1372. The following methods can be used to transfer files between two remote
  1373. servers, providing that these two servers can connect directly to each other.
  1374.  
  1375. =over 4
  1376.  
  1377. =item pasv_xfer ( SRC_FILE, DEST_SERVER [, DEST_FILE ] )
  1378.  
  1379. This method will do a file transfer between two remote ftp servers. If
  1380. C<DEST_FILE> is omitted then the leaf name of C<SRC_FILE> will be used.
  1381.  
  1382. =item pasv_wait ( NON_PASV_SERVER )
  1383.  
  1384. This method can be used to wait for a transfer to complete between a passive
  1385. server and a non-passive server. The method should be called on the passive
  1386. server with the C<Net::FTP> object for the non-passive server passed as an
  1387. argument.
  1388.  
  1389. =item abort ()
  1390.  
  1391. Abort the current data transfer.
  1392.  
  1393. =item quit ()
  1394.  
  1395. Send the QUIT command to the remote FTP server and close the socket connection.
  1396.  
  1397. =back
  1398.  
  1399. =head2 Methods for the adventurous
  1400.  
  1401. C<Net::FTP> inherits from C<Net::Cmd> so methods defined in C<Net::Cmd> may
  1402. be used to send commands to the remote FTP server.
  1403.  
  1404. =over 4
  1405.  
  1406. =item quot (CMD [,ARGS])
  1407.  
  1408. Send a command, that Net::FTP does not directly support, to the remote
  1409. server and wait for a response.
  1410.  
  1411. Returns most significant digit of the response code.
  1412.  
  1413. B<WARNING> This call should only be used on commands that do not require
  1414. data connections. Misuse of this method can hang the connection.
  1415.  
  1416. =back
  1417.  
  1418. =head1 THE dataconn CLASS
  1419.  
  1420. Some of the methods defined in C<Net::FTP> return an object which will
  1421. be derived from this class.The dataconn class itself is derived from
  1422. the C<IO::Socket::INET> class, so any normal IO operations can be performed.
  1423. However the following methods are defined in the dataconn class and IO should
  1424. be performed using these.
  1425.  
  1426. =over 4
  1427.  
  1428. =item read ( BUFFER, SIZE [, TIMEOUT ] )
  1429.  
  1430. Read C<SIZE> bytes of data from the server and place it into C<BUFFER>, also
  1431. performing any <CRLF> translation necessary. C<TIMEOUT> is optional, if not
  1432. given the the timeout value from the command connection will be used.
  1433.  
  1434. Returns the number of bytes read before any <CRLF> translation.
  1435.  
  1436. =item write ( BUFFER, SIZE [, TIMEOUT ] )
  1437.  
  1438. Write C<SIZE> bytes of data from C<BUFFER> to the server, also
  1439. performing any <CRLF> translation necessary. C<TIMEOUT> is optional, if not
  1440. given the the timeout value from the command connection will be used.
  1441.  
  1442. Returns the number of bytes written before any <CRLF> translation.
  1443.  
  1444. =item abort ()
  1445.  
  1446. Abort the current data transfer.
  1447.  
  1448. =item close ()
  1449.  
  1450. Close the data connection and get a response from the FTP server. Returns
  1451. I<true> if the connection was closed successfully and the first digit of
  1452. the response from the server was a '2'.
  1453.  
  1454. =back
  1455.  
  1456. =head1 UNIMPLEMENTED
  1457.  
  1458. The following RFC959 commands have not been implemented:
  1459.  
  1460. =over 4
  1461.  
  1462. =item B<ALLO>
  1463.  
  1464. Allocates storage for the file to be transferred.
  1465.  
  1466. =item B<SMNT>
  1467.  
  1468. Mount a different file system structure without changing login or
  1469. accounting information.
  1470.  
  1471. =item B<HELP>
  1472.  
  1473. Ask the server for "helpful information" (that's what the RFC says) on
  1474. the commands it accepts.
  1475.  
  1476. =item B<MODE>
  1477.  
  1478. Specifies transfer mode (stream, block or compressed) for file to be
  1479. transferred.
  1480.  
  1481. =item B<SITE>
  1482.  
  1483. Request remote server site parameters.
  1484.  
  1485. =item B<SYST>
  1486.  
  1487. Request remote server system identification.
  1488.  
  1489. =item B<STAT>
  1490.  
  1491. Request remote server status.
  1492.  
  1493. =item B<STRU>
  1494.  
  1495. Specifies file structure for file to be transferred.
  1496.  
  1497. =item B<REIN>
  1498.  
  1499. Reinitialize the connection, flushing all I/O and account information.
  1500.  
  1501. =back
  1502.  
  1503. =head1 REPORTING BUGS
  1504.  
  1505. When reporting bugs/problems please include as much information as possible.
  1506. It may be difficult for me to reproduce the problem as almost every setup
  1507. is different.
  1508.  
  1509. A small script which yields the problem will probably be of help. It would
  1510. also be useful if this script was run with the extra options C<Debug => 1>
  1511. passed to the constructor, and the output sent with the bug report. If you
  1512. cannot include a small script then please include a Debug trace from a
  1513. run of your program which does yield the problem.
  1514.  
  1515. =head1 AUTHOR
  1516.  
  1517. Graham Barr <gbarr@ti.com>
  1518.  
  1519. =head1 SEE ALSO
  1520.  
  1521. L<Net::Netrc>
  1522. L<Net::Cmd>
  1523.  
  1524. ftp(1), ftpd(8), RFC 959
  1525. http://www.cis.ohio-state.edu/htbin/rfc/rfc959.html
  1526.  
  1527. =head1 CREDITS
  1528.  
  1529. Henry Gabryjelski <henryg@WPI.EDU> - for the suggestion of creating directories
  1530. recursively.
  1531.  
  1532. Nathan Torkington <gnat@frii.com> - for some input on the documentation.
  1533.  
  1534. Roderick Schertler <roderick@gate.net> - for various inputs
  1535.  
  1536. =head1 COPYRIGHT
  1537.  
  1538. Copyright (c) 1995-1997 Graham Barr. All rights reserved.
  1539. This program is free software; you can redistribute it and/or modify it
  1540. under the same terms as Perl itself.
  1541.  
  1542. =cut
  1543.