#!/usr/local/bin/perl

#----------------------------------------------------------------------
# variables you should change:

$dblookupcmd   = "/home/mudhoney/netfind/nf/dblookup";
$netfindcmd    = "/home/mudhoney/netfind/nf/netfind";

#----------------------------------------------------------------------

# gateway from gopher to netfind, requires netfind 3.46 or above


sub netfind_main {
    
    local($_) = @_;
    
    if (/^$/) {
	&Greply("7Search Netfind for E-mail addresses\t$Ggw dblookup\t$Ghost\t$Gport");
	&Greply("0How to Use the Gopher to Netfind Gateway\t$Ggw help\t$Ghost\t$Gport");
	&Greply(".");
	exit;
    }
    
    # Filter out naughtyness that might subvert security
    s/\"\`\$//g;

    ($cmd, $query, $gplus) = split('\t');

    if ($gplus eq '!') {
	print ".\r\n";
	exit;
    }

    if ($cmd =~ /^help/) {
	&Greply("This Gopher gateway performs Netfind searches.  It uses the");
	&Greply("go4gw software.  To use this gateway supply keywords for a");
	&Greply("Netfind search.  A list of matching domains will be returned as");
	&Greply("documents.  Choose from one of the domains to retrieve the results");
        &Greply("from a Netfind search for that domain.  Due to the strict query");
	&Greply("format for Netfind, keywords must be in the format: name key key...");
	&Greply("");
	&Greply("For example, to find information about Darren Hardy at the University");
	&Greply("of Colorado in Boulder, use the keywords 'hardy boulder colorado' or");
	&Greply("'hardy boulder colorado computer science'.");
	&Greply(".");
	exit;
    }
	

    if ($cmd =~ /^dblookup/) {

	&Gabort("0Mangled Search request, need name *and* domain")
	    if (length($query) <1);

	$query =~ s/\./ /;
	$query =~ s/([^A-Za-z0-9 ])/\\$1/g;
	$query =~ /([^ ]*) (.*)$/;
	$name = $1;
	$domain = $2;

	# Need to test for empty values here..
	if ($name eq "" || $domain eq "") {
	    &Gabort("0Mangled Search request, need name *and* domain");
	}

	$dbcmd = "$dblookupcmd \"$domain\"";
	open(Dbfd, "$dbcmd |");
	while (<Dbfd>) {
	    chop;
	    $title = $_;
	    /^([^ ]+) .*/;
	    $domain = $1;
	    &Greply("0$title\t$Ggw netfind $name $domain\t$Ghost\t$Gport");
	}
	close(Dbfd);
    } 

    if ($cmd =~ /^netfind/) {

	$query =~ s/([^A-Za-z0-9. ])/\\$1/g;
	($netfind, $name, $domain) = split(' ');
	$brokendomain = $domain;
	$brokendomain =~ s/\./ /g;

	$nfcmd = "$netfindcmd -L $domain $name $brokendomain </dev/null2>1";
	open(nffd, "$nfcmd|");
	while (<nffd>) {
	    chop;
	    &Greply($_);
	}
	print "\r\n";
	close(nffd);
    }

    &Greply(".\r\n");
    exit;
    
}

1; # for reqiure
