#
# FRISK F-Prot Daemon
# (EXPERIMENTAL CODE!)
#

#  use IO::Socket;  # ('use' already done in amavisd)

if ($fprotd) {
    do_log(2, "Using $fprotd");
    my $sock = IO::Socket::INET->new('127.0.0.1:10200');
    if (defined $sock) {
	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 =~ /^([\w\d\-.]+)$/) {
		$file = $1;
	    } else {
		die "Unsafe partname $file";
	    }
	    $file = "$TEMPDIR/parts/$file";

	    $sock->print("GET $file?-dumb%20-archive HTTP/1.0\n\n");
	    $sock->flush;
	    chomp($output = join('', $sock->getlines));
	    do_log(2, $output);
	    last if ( $output =~ /<summary(.+)>infected<\/summary>/i );
	}

	close($sock) or die "F-Prot Daemon socket close failed: $!";

	# FIXME - this will currently flag a successful scan with no
	# viruses found as scanner error
	# TODO - need an else part for just this case
	if ( $output =~ /<summary(.+)>infected<\/summary>/i ) {
	    @virusname = ($output =~ /<name>(.+)<\/name>/g);
	    $scanner_errors = 0;
	    return 1;  # 'true' indicates virus found and stops further checking
	} 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);
	}

   } else {
	do_log(0, "Virus scanner failure: F-Prot Daemon - can't connect to daemon");
   }
}
