#!/usr/bin/perl -w
######################################################################
# robot.pl
######################################################################
# Perl Power! - Michael Schilli 1998
######################################################################

use LWP::RobotUA;
use File::Listing;
                    # create robot
$robot = LWP::RobotUA->new('my_fair_robot/1.0', "me\@mysite.com");

                    # scan directory recursively
deep_scan('ftp://remote.host.com/pub');

######################################################################
# Recursive directory scan: deep_scan($url_string);
######################################################################
sub deep_scan {
    my $url_string = shift;

                    # specify directory
    my $request = HTTP::Request->new('GET', $url_string);

                    # carry out network access
    my $response = $robot->request($request);

                    # error check 
    $response->is_success() || die $response->message();

                    # process listing
    for (File::Listing::parse_dir($response->content())) {
        my ($name, $type, $size, $mtime, $mode) = @$_;

                    # files: output URL and size
        print "$url_string/$name ($size)\n" if $type eq "f";

                    # directories: continue scanning
        deep_scan("$url_string/$name") if $type eq "d";
    }
}

