#
# FRISK F-Prot Daemon
# (should work now - hopefully ...)
#

use IO::Socket;
$|=1; 	# autoflush on

if ($fprotd) {
	do_log(2, "Using $fprotd");
	opendir(DIR, "$TEMPDIR/parts/")
           or die "Can't open directory $TEMPDIR/parts/: $!";
	my @files = grep { -f "$TEMPDIR/parts/$_" } readdir(DIR);               
	closedir(DIR) or die "Can't close directory: $!";
	chomp(@files);

        foreach my $file (@files) {
	    if ($file =~ /^([A-Za-z0-9\._=+-]+)$/) {
		$file = $1;
	    } else {
		die "Unsafe partname $file";
	    }
	    $file = "$TEMPDIR/parts/$file";

	    my $sock;
	    for (my $port = 10200; not defined $sock and $port <= 10204; $port++) {
	         $sock = IO::Socket::INET->new("127.0.0.1:$port");
            }
	    if (!defined $sock) {
		do_log(0, "Virus scanner failure: F-Prot Daemon - can't connect to daemon");
		last;
	    }
	    $sock->print("GET $file?-dumb%20-archive HTTP/1.0\r\n\r\n");
	    chomp($output = join('', $sock->getlines));
	    close($sock);
	    do_log(2, $output);
	    last if ( $output =~ /<summary(.+)>infected|error<\/summary>/i );
	}

	if ( $output =~ /<summary(.+)>infected<\/summary>/i ) {
	    @virusname = ($output =~ /<name>(.+)<\/name>/g);
	    @virusname = (undef) if !@virusname;  # just in case: make list nonnil
	    $scanner_errors = 0;	# no errors, a virus was found
	    do_virus($output);
	} elsif ( $output =~ /<summary(.+)>error<\/summary>/i ) {
	    my @error_reason = ($output =~ /<error>(.+)<\/error>/i);
	    @error_reason = (undef) if !@error_reason;  # just in case: make list nonnil	
	    do_log(0, "Virus scanner failure: F-Prot Daemon - " . shift @error_reason);
	} elsif ( $output =~ /<summary(.+)>clean<\/summary>/i ) {
	    $scanner_errors = 0;        # no errors, no viruses
	}
}
