#!/usr/local/bin/perl

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

$whois_server = "umn.edu";   
$whois_port   = 43;

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

sub whois_main {
    local($_) = @_;
    
    if (/^$/) {
	&Greply("0Search Instructions\thelp\t$whois_server\t$whois_port");
	&Greply("7Query Whois server $whois_server\t$Ggw $whois_server\t$Ghost\t$Gport");
	&Greply(".");
	exit(0);
    }
    
    ($whois_server, $query) = split('\t');
    
    &GopenServer($whois_server,$whois_port);
    &Gsend($query);
    
    while(<GSERVER>) {
	s/\n//;
	s/\r//;
        push(@lines,$_);
    }
    
    #
    # Always return the output of the whois search first
    #
    
    &Greply("0Raw search results\t$query\t$whois_server\t$whois_port");
    
    
    #
    # Test for some common formats
    #
    
    #
    # CSO gateway whois type servers
    #
    
    foreach (@lines) {
	if (/\s+name:\s+(.*)/ ||
	    /\s+person:\s+(.*)/) {
	    $newquery = $1;
	    $newquery =~ s/,//g; #remove commas
	    $newquery =~ s/\s+[A-z]$//; #remove trailing middle initial
	    &Greply("0$1\t$newquery\t$whois_server\t$whois_port");
	}
    }
    
    #    
    # nic.ddn.mil type whois servers
    #
    
    foreach (@lines) {
	if (/(.*)\((.*)\)\t(.*)$/) {
	    &Greply("0$1\t!$2\t$whois_server\t$whois_port");
	}
    }
    
    #
    # format used by stanford.edu
    #    
    if ($lines[$#lines] =~ /^\(returned/) {
	foreach (@lines) {
	    if (/(.*)<(.*)>.*\s+(.*)$/) {
		$text=$1; $handle=$2; $rest = $3;
		$text =~ s/\s+$//;
		$rest =~ s/\s+/ /g;
		&Greply("0$text ($rest)\t$handle\t$whois_server\t$whois_port");
	    }
        }
    }


 
     #
     # Format used at whohost.uwo.ca Try to handle both long form and short
     # forms.  Long is a series of <field-name>: <field-value> pairs
     # Short format: two lines with name on first and department indented
     # on the second.
 
     if ($lines[0] =~ /^There were [0-9]+ matches on your request\./ ||
       $lines[0] =~ /^\s+Full\sName: .*$/) {
       foreach (@lines) {
         if (/:/) {  # must be full form
           if (/^\s+Full\sName: (.*)$/) {      #the name field
               $fullname = $1;
           }
           if (/^\s+Department: (.*)$/) {      # department name
               $department = $1;
           }
           if (/^\s+Index Key: (.*)$/) {       # index number
               $newquery = $1;
               &Greply("0$fullname  [$department]\tkey$newquery\t$whois_server\t$whois_port");
           }
         }
         else { #short form (first line begins with <space> or *
           if (/^[ *]([A-z0-9].*)\[(.*)\]/) {
               $newquery = $2;              #grab index
               $fullname= $1;               # and the name for display
           }
           if (/^  (.*)     /) {     # on the second line
               $department = $1;            #get department
               $department =~ s/ +$//;  #delete trailing spaces
               &Greply("0$fullname  [$department]\tkey$newquery\t$whois_server\t$whois_port");
           }
         }
       }
    }

    
    #
    # format used by X.500 gateways.
    #
    if ($lines[0] =~ m/[0-9]+ matches found/) {
        foreach (@lines) {
	    if (/^\s+[0-9]+\.(.*)\s+   (.*)$/) {
                $newname = $1;
                $newname =~ s/^\s+//;
		$newname =~ s/\s+$//;
		&Greply("0$newname ($2)\t$newname\t$whois_server\twhois_port");
	    }
        }
    }
    
    #
    # Format used by sunysb.edu, software unknown.
    #
    if ($lines[0] =~ /^Connection received/) {
	foreach (@lines) {
	    if (/^(.*)MailName:/) {
		&Greply("0$1\t$1\t$whois_server\twhois_port");
	    }
        }
    }
    
    #
    # format used by horton: username@host  Name  Date
    #
    
    foreach (@lines) {
        if (/([\S]+@[\S]+).*([A-z][A-z][A-z] [A-z][A-z][A-z]\d\d \d\d\d\d)[\s]+$/) {
	    #Not implemented yet.
        }
    }
    
    
    
    foreach (@lines) {
	if (/^\s+name:\s+(.*)/) {
	    &Greply("0$1\t$query\t$whois_server\t$whois_port");
	    &Greply(".");
	    exit(0);
	}
    }

    if ($lines[$#lines] eq 'NO MATCH') {
	&Greply("0No Match Was Found!\thelp\t$gopher_server\t$gopher_port");
    } elsif ($lines[$#lines] =~ /^\(returned/) {
	foreach (@lines) {
	    if (/(.*)<(.*)>.*\s+(.*)$/) {
		$text=$1; $handle=$2; $rest = $3;
		$text =~ s/\s+$//;
		$rest =~ s/\s+/ /g;
		&Greply("0$text ($rest)\t!$handle\t$whois_server\t$whois_port");
	    }
	}
    }
    
    &Greply(".");
    exit(0);
}




1; # for require
