#!/afs/athena/contrib/perl/perl

require("chat2.pl");
$|=1;

print("Creating database\n");

while (<>) {

    # Handle article headers and ">>>" quotes.

    next if /^\./;
    next if /^Time:/;
    next if /^From:/;
    next if /^From / .. /^$/;
    next if /^Path: / .. /^$/;
    s/^\W+//;

    # Do each word.

    push(@ary,split(' '));
    while ($#ary > 1) {
        $a = $p;
        $p = $n;
        $w = shift(@ary);
        $n = $num{$w};
        if ($n eq '') {
            push(@word,$w);
            $n = pack('S',$#word);
            $num{$w} = $n;
        }
        $lookup{$a . $p} .= $n;
    }
}

print("Connecting...\n");
&evil();
&listen(3);
print("Going home\n");
&chat'print("home\n");
	&listen(1);
#	print("Fingering for locations\n");
#	&chat'print("finger\n");
#	$fing = &listen(2);
#	@fings = split(/\n/, $fing);
#	for $f (@fings){
#	($name, $loc, $fromwhere) = split(/[ \t]+/, $f);
#	if($loc =~ /#/){$poplocs{$loc}++;}
#	}
#	while(($l, $n)= each %poplocs){
#		if(int($n)>$max){
#			$max = int($n);
#			$maxl = $l;
#		}
#	}
#	print("It appears there are many people at $maxl\n");
#	$maxl = substr($maxl, 1, length($maxl)-2);
#	print("Executing:@tel $maxl\n");
#	&chat'print("@tel $maxl\n");
print(&listen(2));
&chat'print("\"Oh boy oh boy\n");
&chat'print("@doing unless it's something else\n");

while(1){
    $input = '';


    while(($input !~/says/) && ($input !~/pages/)){
	$input.=&listen(1);
	if((length($input)%7)==0){
	    $action = rand($#actions);
	    &chat'print("$actions[$action]\n");
            }
	$oldinpl=length($input);
	}
    @lines = split(/\n/, $input);
    for $line (@lines){
	if($line=~/says/){
	     $text = substr($line, index($line, '"'));
	     chop($text);
	     $user = substr($line, 0, index($line, ' '));
	     $text =~ s/[.?!;] +/\n/g;
             print("$user:");
	     @body = split("\n", $text);

	     foreach $line (@body) {
		 if(substr($text, 1, 8) eq 'godrsays'){
			$rest = substr($text, 10);
			}
            		$outrep = &respond($user, $line);
	    		&chat'print ("\"$user, $outrep\n");
			}

		}
		if($line=~/pages/){
			$text = substr($line, index($line, ':'));
			chop($text);
			$user = substr($line, 0, index($line, ' '));
			$text =~ s/[.?!;] +/\n/g;
			@body = split("\n", $text);
                        print("$user,");
			foreach $line (@body) {
			if(substr($text, 1, 8) eq 'godrsays'){
				$rest = substr($text, 10);
			}
            		$outrep = &respond($user, $line);
	    		&chat'print ("page $user=$outrep\n");
			}
            		$outrep = &respond($user, $line);
	    		&chat'print ("page $user=$outrep\n");
			}

		}

}





        sub evil {
                &connect('fido.econlab.arizona.edu 4201');
                print(&listen(6));
                &chat'print("connect Travesty saywhat\n");
        }
        sub connect {
                local($opts)=@_;
                ($host, $port)=split(/ /,$opts);
                $connected =1;
                (&chat'open_port($host, $port))|| do{
                print("Connection failed\n");
                $connected = 0;}
        }

sub respond {
	local($name, $foo) = @_;
	chop($foo);
    push(@ary,split(' ', $foo));
    while ($#ary > 1) {
        $a = $p;
        $p = $n;
        $w = shift(@ary);
        $n = $num{$w};
        if ($n eq '') {
            push(@word,$w);
            $n = pack('S',$#word);
            $num{$w} = $n;
        }
        $lookup{$a . $p} .= $n;
    }


	$reply ='';
	$col = 0;
	for (;;) {
    $n = $lookup{$a . $p};
    ($foo,$n) = each(lookup) if $n eq '';       # A bootstrap.
    $n = substr($n,int(rand(length($n))) & 0177776,2);
    $a = $p;
    $p = $n;
    ($w) = unpack('S',$n);
    $w = $word[$w];

    # See if word fits on line.

    $col += length($w) + 1;
    if ($col >= 65) {
        $col = 0;
    }
    else {
    }
    $reply .=$w.' ';

    # Paragraph every sentence or so.

    if ($w =~ /(\.|\?)$/) {
        if (rand() < .8) {
            return($reply);
            $col = 80;
		return;
        }
    }
}


}

sub listen {
        local($secs) = @_;
        local($return,$tmp) = "";
        while (length($tmp = &chat'expect($secs, '(.|\n)+', '$&'))) {
                print $tmp if $trace;
                $return .= $tmp;
        }
        $return;
}
