home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / perl / scripts-convex / ftp-chat2 / ftpr < prev    next >
Encoding:
Text File  |  1993-07-14  |  4.9 KB  |  228 lines

  1. #!/usr/bin/perl
  2.  
  3. ## ftpr, last update 91/08/16
  4. ## usage: ftpr [-a] [-d] [-t timeout] [-n] hostname topdir yes-regex except-regex
  5. ## topdir may be whitespace-separated list of topdirs
  6. ## yes-regex defaults to . (meaning everything)
  7. ## except-regex defaults to ' ' (meaning no exceptions)
  8.  
  9. push(@INC, '/local/merlyn/lib/perl');
  10.  
  11. require 'chat2.pl';
  12.  
  13. $| = 1; # not much output, but we like to see it as it happens
  14. $timeout = 60;
  15. $dasha = "";
  16. $nflag = 0;
  17. $host = "localhost";
  18. $topdir = ".";
  19. $yesregex = ".";
  20. $noregex = " ";
  21. $user = "anonymous";
  22. $pass = 'merlyn@iwarp.intel.com';
  23.  
  24. {
  25.     last unless $ARGV[0] =~ /^-/;
  26.     $_ = shift;
  27.     $trace++, redo if /^-d/; # debug mode
  28.     $timeout = $1, redo if /^-t(\d+)/;
  29.     $timeout = shift, redo if /^-t/;
  30.     $dasha = "-a", redo if /^-a/;
  31.     $nflag++, redo if /^-n/;
  32.     die "bad flag: $_";
  33. }
  34.  
  35. $host = shift if @ARGV;
  36. $topdir = shift if @ARGV;
  37. $yesregex = shift if @ARGV;
  38. $noregex = shift if @ARGV;
  39.  
  40. die "extra args: @ARGV" if @ARGV;
  41.  
  42. ($Control = &chat'open_port($host,21)) || die "open control: $!";
  43. die "expected 2dd for initial banner, got $_"
  44.     unless ($_ = &clisten($timeout)) =~ /^2\d\d/;
  45. &ctalk("user $user\n");
  46. $_ = &clisten($timeout);
  47. unless (/^2\d\d/) { # might be logged in already:
  48.     die "expected 3dd for password query, got $_"
  49.         unless /^3\d\d/;
  50.     &ctalk("pass $pass\n");
  51.     die "expected 2dd for logged in, got $_"
  52.         unless ($_ = &clisten($timeout)) =~ /^2\d\d/;
  53. }
  54. ## all set up for a conversation
  55.  
  56. @list = split(/\s+/,$topdir);
  57. while ($dir = shift list) {
  58.     next if $seen{$dir}++;
  59.     print "listing $dir\n";
  60.     for (&list($dir)) {
  61.         (warn "can't parse $_ in $dir"), next
  62.             unless ($tag,$file) = /^(.).*\s(\S+)\s*$/;
  63.         push(@list, "$dir/$file") if
  64.             ($tag eq 'd') && ($file !~ /^\.\.?$/);
  65.         if (    ($tag eq '-') &&
  66.             ("$dir/$file" =~ /$yesregex/o) &&
  67.             ("$dir/$file" !~ /$noregex/o) &&
  68.             (! -e "$dir/$file")
  69.         ) {
  70.             print "fetching $dir/$file...\n";
  71.             &get("$dir/$file","$dir/$file") unless $nflag;
  72.         }
  73.     }
  74. }
  75.  
  76. ## shutdown
  77. &ctalk("quit\n");
  78. &clisten(5); # for trace
  79. &chat'close($Control);
  80. exit(0);
  81.  
  82. sub ctalk {
  83.     local($text) = @_;
  84.     print "{$text}" if $trace;
  85.     &chat'print($Control,$text);
  86. }
  87.  
  88. sub clisten {
  89.     local($secs) = @_;
  90.     local($return,$tmp);
  91.     while (1) {
  92.         $tmp = &chat'expect($Control, $secs, '(.*)\r?\n', '"$1\n"');
  93.         print $tmp if $trace;
  94.         $return .= $tmp;
  95.         return $return if !length($tmp) || $tmp =~ /^\d\d\d /;
  96.     }
  97. }
  98.  
  99. sub dopen {
  100.     local($_);
  101.  
  102.     local(@ret) = &chat'open_listen();
  103.     &ctalk("port " .
  104.         join(",", @ret[0,1,2,3], int($ret[4]/256), $ret[4]%256) .
  105.         "\n");
  106.     die "expected 2dd for data open, got $_"
  107.         unless ($_ = &clisten($timeout)) =~ /^2\d\d/;
  108.     $Data = $ret[5];
  109. }
  110.  
  111. <<'END_NOT_USED';
  112. sub dtalk {
  113.     local($text) = @_;
  114.     print "{D:$text}" if $trace;
  115.     &chat'print($Data,$text);
  116. }
  117. END_NOT_USED
  118.  
  119. sub dlisten {
  120.     local($secs,$forcereturn) = @_;
  121.     local($return,$tmp);
  122.     while (1) {
  123.         $tmp = &chat'expect($Data, $secs,
  124.             '(.|\n)+', '$&',
  125.             TIMEOUT, '""',
  126.             EOF, 'undef');
  127.         if (defined $tmp) {
  128.             print "[D:$tmp]" if $trace > 1;
  129.             $return .= $tmp;
  130.             return $return unless (!$forcereturn) && (length $tmp);
  131.                 # if timeout, return what you have
  132.         } else { # eof
  133.             return $return;
  134.                 # maybe undef
  135.         }
  136.     }
  137. }
  138.  
  139. sub dclose {
  140.     &chat'close($Data);
  141. }
  142.  
  143. <<'END_NOT_USED';
  144. sub nlst {
  145.     local($dir) = @_;
  146.     local(@files);
  147.     local($_,$tmp);
  148.  
  149.     &dopen();
  150.     &ctalk("nlst $dasha $dir/.\n");
  151.     die "expected 1dd for nlst, got $_"
  152.         unless ($_ = &clisten($timeout)) =~ /^1\d\d/;
  153.     $_ = "";
  154.     while (1) {
  155.         $tmp = &dlisten($timeout);
  156.         last unless defined $tmp;
  157.         $_ .= $tmp;
  158.     }
  159.     @files = sort grep(!/^\.\.?$/, split(/\r?\n/))
  160.         unless /^ls: /;
  161.     die "expected 2dd for nlst complete, got $_"
  162.         unless ($_ = &clisten($timeout)) =~ /^2\d\d/;
  163.     &dclose();
  164.     @files;
  165. }
  166. END_NOT_USED
  167.  
  168. sub list {
  169.     local($dir) = @_;
  170.     local(@files);
  171.     local($_,$tmp);
  172.  
  173.     &dopen();
  174.     &ctalk("list $dasha $dir/.\n");
  175.     die "expected 1dd for list, got $_"
  176.         unless ($_ = &clisten($timeout)) =~ /^(.*\n)*1/;
  177.     $_ = "";
  178.     while (1) {
  179.         $tmp = &dlisten($timeout);
  180.         last unless defined $tmp;
  181.         $_ .= $tmp;
  182.     }
  183.     @files = grep(/^\S[rwx\-]{8}/, split(/\r?\n/));
  184.     die "expected 2dd for list complete, got $_"
  185.         unless ($_ = &clisten($timeout)) =~ /^2\d\d/;
  186.     &dclose();
  187.     @files;
  188. }
  189.  
  190. sub get {
  191.     local($from, $to) = @_;
  192.     local($todir,*OUT);
  193.  
  194.     ($todir = "./$to") =~ s#(.*)/.*#$1#;
  195.     system "mkdir -p $todir" unless -d $todir;
  196.     (warn "cannot create $to.TMP: $!"), return
  197.         unless open(OUT, ">$to.TMP");
  198.     select((select(OUT),$|=1)[0]);
  199.     &ctalk("type i\n");
  200.     die "expected 2dd for type i ok, got $_"
  201.         unless ($_ = &clisten($timeout)) =~ /^2\d\d/;
  202.     &dopen();
  203.     &ctalk("retr $from\n");
  204.     unless (($_ = &clisten($timeout)) =~ /^1\d\d/) {
  205.         warn "expected 1dd for retr, got $_";
  206.         close(OUT);
  207.         unlink("$to.TMP");
  208.         &dclose();
  209.         return;
  210.     }
  211.     {
  212.         $_ = &dlisten($timeout,1);
  213.         last unless defined $_;
  214.         print OUT;
  215.         redo;
  216.     }
  217.     close(OUT);
  218.     unless (($_ = &clisten($timeout)) =~ /^2\d\d/) {
  219.         warn "expected 2dd for retr complete, got $_";
  220.         close(OUT);
  221.         unlink("$to.TMP");
  222.         &dclose();
  223.         return;
  224.     }
  225.     &dclose();
  226.     rename("$to.TMP","$to") || warn "cannot rename $to.TMP to $to: $!";
  227. }
  228.