Article 9830 of comp.lang.perl:
Xref: feenix.metronet.com comp.lang.perl:9830
Path: feenix.metronet.com!news.ecn.bgu.edu!usenet.ins.cwru.edu!eff!news.umbc.edu!haven.umd.edu!cs.umd.edu!mojo.eng.umd.edu!rensin
From: [email protected] (David Rensin)
Newsgroups: comp.lang.perl
Subject: Archie/ftp Perl interface
Date: 16 Jan 1994 23:45:41 GMT
Organization: Project GLUE, University of Maryland, College Park
Lines: 282
Distribution: world
Message-ID: <[email protected]>
NNTP-Posting-Host: avalon.eng.umd.edu



The following is an archie/ftp interface I wrote in Perl.. It's still pretty
bare, but I use it a lot... Enjoy..

               -dave :-)


ps: The docs are in the source...


-----------------BEGIN----------------------

#! /usr/local/bin/perl

##########################################################
#                                                        #
#     artp - by Dave Rensin                              #
#                                                        #
#   Syntax: artp <text> <max hits for archie> [-l] [-u]  #
#   eg: artp SLIP 50 -l -u                               #
#                                                        #
# Options:                                               #
#        -l   - Will log all transfered files to the     #
#               file .artplog in your home root.         #
#                                                        #
#        -u   - Will cause artp to use .artplog instead  #
#               of querying an archie server. This is    #
#               for those of us who go to the same place #
#               often.                                   #
#                                                        #
# Commands:                                              #
#   <number>  - will either recurse into a dir           #
#               or get a file in BIN mode.               #
#                                                        #
#   <..>      - will go up one directory.                #
#                                                        #
#   <blank line> - will exit artp                        #
#                                                        #
# Hints:                                                 #
#    This script assumes 1) that you have                #
#    archie compiled and running and 2) you              #
#    have a PAGER environment variable set to            #
#    whatever you want your pager to be.                 #
#    (ie. more, less, etc...)                            #
#                                                        #
#               - good luck,                             #
#               - Dave Rensin :)                         #
#               - [email protected]                     #
#                                                        #
##########################################################




require "open2.pl";
$| = 1;

($rest,$0) = split(/\//,$0,2) while ($0 =~ /\//o);
die "\nSyntax: $0 <text to search for> <max hits for archie> [-l] [-u]\n\n" if ($#ARGV < 1);
$SIG{'PIPE'} = 'IGNORE';

require "ftplib.pl" || die "NO FTBLIB!";

$log_out = "";
$use_log_for_input = "";
$log_out = grep(/-l(\s|$)/o,@ARGV);
$use_log_for_input = grep(/-u(\s|$)/o,@ARGV);


if ($use_log_for_input) {

       open (log_file,"$ENV{'HOME'}/.artplog") || die "\nCan't find file .artplog in your home directory.\n\n";

       while (<log_file>) {
               push(@archie_lines_temp,$_) if ($#archie_lines_temp+1 < $ARGV[1] && /$ARGV[0]/io);
       }

}else{
       print "\n\tUnfortunately, there is no reliable way to predict the\n";
       print "amount of time this is going to take. It depends on net traffic,\n";
       print "the load of the archie server, the number of max hits, and other\n";
       print "various things. Your patience is appreciated.\n\n";
       print "\t\tThe Managment\n";

       $ctr =0;

       @archie_servers = ("archie.rutgers.edu","archie.sura.net","archie.funet.fi");
       print "\nChecking the loads of some archie servers...\n\n";
       print "#   Server:                    Load:\n";
       print "--  ---------                  -----\n";
       print "\n";

       $ctr =1;


       foreach $archie_server (@archie_servers) {
               $load_line = `rup $archie_server`;

               if ($load_line !~ /RPC: Program not registered/og){
                       $load_line =~ /load average: \d+.\d+, (\d+.\d+)/o;
                       $load = $1;
                       print "                               $load\r";
                       print "$ctr) $archie_server\n";
                       $ctr++;
               }
       }


       print "\nEnter Number of Desired Server: ";
       $server_no = <STDIN>;
       $server_no =~ s/\n//og;
       die "Bye..Bye...\n\n" if ($server_no !~ /^\d+$/o || $server_no > $#archie_servers+1);
       $server_no--;
       $archie_server = $archie_servers[$server_no];
       print "\nUsing archie server $archie_server...\n";
       print "Searching for \"$ARGV[0]\" with max hits $ARGV[1]...\n";

       @archie_lines_temp = `archie -V -l -m $ARGV[1] -s $ARGV[0] -h $archie_server`;
}

foreach $line_temp (@archie_lines_temp) {
       $year = substr($line_temp,0,4);
       $month = substr($line_temp,4,2);
       $day = substr($line_temp,6,2);
       $line = substr($line_temp,23);
       ($server,$file) = split(/ /,$line);

       if (!$server ) {
               $server = $file;
               $file = "";
       }

       chop $file;
       $archie_lines[$ctr] = $server.",".$file.",".$month."/".$day."/".$year;
       $ctr++;
}

@archie_lines = sort @archie_lines;
$ctr =0;
open (out,"| $ENV{'PAGER'}") || die "shit.";

print out "     Server:                       Modified:   File Path:\n\n";

foreach $line (@archie_lines) {
       $ctr++;
       ($server,$file,$date) = split(/,/,$line);
       $ctr_line = $ctr.")";
       write out;
}

close (out);


print "\nEnter line #: ";
$line_no = <STDIN>;
die "Bye..Bye...\n\n" if ($line_no eq "\n");
chop $line_no;
$line_no--;

($server,$file,$date) = split(/,/,$archie_lines[$line_no]);

print "\nConnecting to server $server...\n";
&ftp'open($server);
print "Connected to server $server...\n";

while ($file =~ /\//o) {
       ($rest,$file) = split(/\//,$file,2);
       $path.="$rest\/";
}
$file = "/";

while ($line_no ne "\n") {

       while ($file =~ /\//o || $goto eq "..") {

               $path .= "$file";
               $path =~ s/\/\//\//;
               print "\nRetrieving directory for $path...\n";
               &ftp'cwd($path);
               @tempx_lines = &ftp'dir($path);
               undef(@temp_lines);

               foreach $line (@tempx_lines) {
                       push(@temp_lines,$line) if ($line !~ /total \d+/o);
               }

               $ctr=0;

               foreach $temp_line (@temp_lines) {
                       $temp_lines[$ctr].="/" if ($temp_line =~ /^dr/o);
                       $ctr++
               }

               $ctr = 1;
               $toast = 0;

               open (out,"| $ENV{'PAGER'}") || die "shit.";

               foreach $temp_line (@temp_lines) {

                       print out "$ctr) $temp_line\n";
                       $ctr++;
               }

               close (out);

               print "\nEnter line #: ";
               $line_no = <STDIN>;
               die "Bye..Bye...\n\n" if ($line_no eq "\n");
               $goto = "";
               $goto = ".." if ($line_no eq "..\n");
               chop $line_no;
               $line_no--;

               $file = $temp_lines[$line_no];

               if ($file =~ / -> /o) {
                       ($file,$junk) = split(/ -> /,$file,2);
                       $file.="/";
               }

               ($junk,$file) = split(/ /,$file,2) while ($file =~ / /o);

               if ($goto eq "..") {
                       $path =~ s/\/[^\/]+\/$//o;
                       $file = "/";
               }
       }

       print "\nRecursing into $path...\n";
       &ftp'cwd($path);
       print "And getting file $file...\n...";
       &ftp'type("I");
       &ftp'get ($file);
       print "Done.\n";

       if ($log_out) {

               ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
               $this_year = 1900+$year;
               $this_month = $mon+1;

               open (log_file,"$ENV{'HOME'}/.artplog");
               @log_lines = <log_file>;
               close (log_file);
               open (log_file,">$ENV{'HOME'}/.artplog");
               print log_file @log_lines;
               $mday="0".$mday if (length($mday) == 1);
               print log_file $this_year.$this_month.$mday."000000Z    000 ".$server." $path"."$file\n";
       close (log_file);
       }

       $file = "/";

}
&ftp'close;



################## Report Format ##########################

format out=
@<<< @<<<<<<<<<<<<<<<<<<<<<<<<<<<< @<<<<<<<<<  ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<...
$ctr_line,$server,$date,$file