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

($user, $remotehost) = split ('@', $ARGV[0]);

require "sys/socket.ph";

$proto = (getprotobyname ("TCP"))[2];

chop ($localhost = `hostname`);

$localaddr = ((gethostbyname ($localhost))[4])[0];

if ($remotehost =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/) {  # ip address given
    $remoteaddr = pack ('C4', ($1, $2, $3, $4));
}
else {	# hostname given		      
    ($remoteaddr = ((gethostbyname ($remotehost))[4])[0]) || 
	die "No such host '$hostname'\n";
}

$sockaddr = 'S n a4 x8';

$port = 79;

$local = pack ($sockaddr, &AF_INET (), 0, $localaddr);
$remote = pack ($sockaddr, &AF_INET (), $port, $remoteaddr); 

print "local ip address is ", join ('.', unpack ('C4', $localaddr)), "\n";
print "remote ip address is ", join ('.', unpack ('C4', $remoteaddr)), "\n";

$me = (getpwuid($<))[0] || $ENV{'USER'};

while (1) {
    socket (S, &AF_INET (), &SOCK_STREAM (), $proto) || die $!;
    bind (S, $local) || die $!;
    connect (S, $remote) || die $!;
    
    select(S); $| = 1; select(STDOUT);


    print S "$user\r\n";
    $on = "";
    while (<S>) {	
	$on = $_ if /On since/;
    }
    shutdown (S, 2) || die $!;
    close (S);
    if ($on) {
	chop $on;
	system "zwrite -n -q -d -s 'FINGER MONITOR for $user' $me -m '$on'";
	exit 0;
    }
    sleep (60 * 15);
}
