#!/usr/local/bin/perl

sub dokill {
    kill 9,$child if $child;
}

sub Opengopher {

    local($them,$port) = @_;	
    $them = 'localhost' unless $them;

    $AF_INET = 2;
    $SOCK_STREAM = 1;

    $SIG{'INT'} = 'dokill';

    $sockaddr = 'S n a4 x8';

    chop($hostname = `hostname`);

    ($name,$aliases,$proto) = getprotobyname('tcp');
    ($name,$aliases,$port) = getservbyname($port,'tcp')
	unless $port =~ /^\d+$/;;
    ($name,$aliases,$type,$len,$thisaddr) = gethostbyname($hostname);
    ($name,$aliases,$type,$len,$thataddr) = gethostbyname($them);

    $this = pack($sockaddr, $AF_INET, 0, $thisaddr);
    $that = pack($sockaddr, $AF_INET, $port, $thataddr);

    # Make the socket filehandle.
    socket(S, $AF_INET, $SOCK_STREAM, $proto) || die $!;

    # Give the socket an address.
    bind(S, $this) || die $!;

    # Call up the server.
    connect(S,$that) || die $!;

    # Set socket to be command buffered.
    select(S); $| = 1; select(STDOUT);

}


sub ParseMail {
    
    # First parse out the header lines.

    while (<>) {
	chop;

	if (/^$/) {
	    # end of headers
	    last;
	}

	if (/^Reply-To: (.*)/) {
	    print "Mailaddr is $1\n";
        }

	if (/^From: "(.*)" <(.*)>$/) {
	    print "Realname is $1, mailaddr is $2\n";
	}

	elsif (/^From: (.*) <(.*)>$/) {
	    print "Realname is $1, mailaddr is $2\n";
	    $mailaddr = $2;
	}

	elsif (/^From: (.*) \((.*)\)$/) {
	    print "Realname is $1, mailaddr is $2\n";
	}

	elsif (/^From: (.*)$/) {
	    print "Realname is $1, mailaddr is $2\n";
	}

    }

    # Start collecting search words

    while (<>) {
	chop;
	
	if (/^--$/) {		# Usually denotes .signature
	    last;
	}

	@linewords = split;
	
	foreach $i (@linewords) {
	    $i =~ s/[\.\!\?\:\,\-\~\/\\]*$//;

	    if (length($i) >50) { # Probably some long ------ type of thing
		next;
	    }

	    if (length($i) <3) {
		next;
            }

	    $indexwords{$i} ++;
	    
	}
    }

    @names = keys(indexwords);
    
    foreach $i (@names) {
	print $i . " $indexwords{$i}\n";
    }
#
# Open up the socket
#

    &Opengopher("ashpool.micro.umn.edu", "158");

    foreach $i (@names) {
	print S "$i or ";
    }


print S "\r\n";

while (<S>) {
	chop;
	chop;
	print;
	print "\n";
}



while ($arg = shift(@ARGV)) {
    if ($arg eq "-l") {
	$LongFlag = 1;
    }
}
}

&ParseMail;
