# ftpsearch.pl - gateway to ftpd's site index feature # # $Id: $ # $map{'ftpsearch'} = '&do_ftpsearch($rest,$query)'; do 'sys/socket.ph'; require 'ftp.pl'; package ftpsearch; sub main'do_ftpsearch { local($args,$query) = @_; local($thathost, $thatport); ($thathost, $thatport) = split(/:/,$args,2); if ($thathost =~ /\d+\.\d+\.\d+\.\d+/) { # $thathost is numerical ip address ($name, $aliases, $type, $len, $thataddr) = gethostbyaddr(pack('C4', split(/\./,$thathost)),&main'AF_INET); $bestname = $name; } else { # $thathost seems to be an FQDN ($name, $aliases, $type, $len, $thataddr) = gethostbyname("$thathost"); $bestname=$thathost; } if ($query eq "") { # query is empty, send an empty query sheet print "

Searchable FTP index

\n"; print "
\n"; print "This is a searchable index for the ftp-server at "; print "$bestname

\n"; print "Enter your search keyword "; print "

\n"; return; } # its a real query &main'MIME_header('ok', 'text/html'); print "\n"; print "Enter new search keyword "; print "

\n"; print "

Found FTP items at $thathost

"; $query = substr($query, 2); print "The search keyword was $query

"; # open connection to ftpd at $thathost if (!(do ftp'timed_open($thathost, $thatport, 2, 2))) { print "opening connection to FTP-server $thathost timed out\n"; return; } &ftp'login("anonymous","www@"); $file = $main'plexus{'tmp_dir'} . "/index.$thathost"; # try to create lock file if (!symlink("/", "$file.lock" ) ) { print "Cannot check for new index, lock file exists"; } else { # use output of ftp'dir_open("index.*") as timestamp open(FS,"$file.dir"); $oldcontents=""; while () { $oldcontents .= $_; } close(FS); rename("$file.dir","$file.dir.old"); open(FS,">$file.dir"); do ftp'dir_open("index.*"); $newcontents=""; while () { $newcontents .= $_; print FS; } # get /index, if necessary if ($oldcontents ne $newcontents) { do ftp'get("index",$file); } do ftp'quit; unlink("$file.lock"); } #extract info from index file open(FS,"grep \"$query\" $file|"); print "

";
    $_=();$_=();
    while () {
        chop;chop;
        $line = $_;
        @line = split(/\//,$line);
        while ($line[$#line] !~ /$query/) {
            pop(@line); 
        }
        $line = join("/",@line);
        if (!$lineseen{$line}) { 
            print "";
            print "$line\n";  
            $lineseen{$line} = 1 ; 
        }
    }
    close (FS);
}

1;