#/usr/local/bin/perl
#
# Program   : waitfor.pl
# Version   : 1.3
# Author    : Mark mark@otto.bf.rmit.oz.au mark@phoenix.pub.uu.oz.au
# Written   : 15th March 1992
# Copyright : Your free to copy this for your personal, non-commercial use.
#             If I see it in BSD4.5 or SysV5 then I want some money :)
#             Oh yeah, dont claim you wrote it either.
#             Contains code written by L.Wall and R.Schwartz.
#
# Script to check if a user is on a system. If they are it writes to your tty
# and continues to do so until killed or they log off. Normal use is to run
# this script in the background from your .login and when it calls you then
# kill -9 %1 to remove this program and talk(1) to the user.
#
# 16th March 1992 :
#
# Added direct connection via tcp sockets. The old `finger | head | tail` etc
# stood out a mile on `ps` listings. Now it's a stand alone process once 
# running. Simply opens a connection to port 79 of the remote machine, sends
# the desired user's id and filters through the output until their information
# is printed and then parses out the "Last on"/"On since" and then acts 
# according to that. Any excess data is merely ignored.
#
# 17th March 1992 :
#
# Checks to see if ppid is 1, if so then it dies off. For some reason on this
# machine perl gets claimed by init when your session dies (stupid terminal
# server) and you end up with 15 pagers running on a bad day. Not much point
# to running this when your not on anyway.
# 
# Uses most of the demo client from the Camel book. Scrapped the forking bits :)
# I didnt write that bit of this program ok :). Larry and Randall did.
# Btw Larry/Randall, check the text for getppid in Functions. s/your/it's/
#
# Improvements :
#
# Put in command line options. Doesnt interest me but you might want it.
# Add in multiple fingering of people at a list of sites.
# Either add a '-k' option to kill off itself and any others or knock up a
#    quick command that kills all of these processes.
#

chop($my_tty = `tty`);
chop($my_site = `hostname`);
$port = 79;		     # finger/tcp port
$sleep_time = 15;            # time to sleep between each finger
$them = 'cjm';              # user to finger
$their_name = "Chris";        # The name you want to see when your paged
$their_site = 'dunno.edu';
$sockaddr   = 'S n a4 x8';
$AF_INET = 2;
$SOCK_STREAM = 1;

($name, $aliases, $proto) = getprotobyname('tcp');
($name, $aliases, $port) = getservbyname($port, 'tcp') unless $port =~ /^\d+$/;;
($name, $aliases, $type, $len, $thisaddr) = gethostbyname($my_site);
($name, $aliases, $type, $len, $thataddr) = gethostbyname($their_site);
$this = pack($sockaddr, $AF_INET, 0, $thisaddr);
$that = pack($sockaddr, $AF_INET, $port, $thataddr);

while (1) {
    $my_ppid = getppid(); $their_on = 0;
    if ($my_ppid == 1) { die "horribly"; } # Dont run if init is my parent
    die $! unless socket(S, $AF_INET, $SOCK_STREAM, $proto); # Make socket fd
    die $! unless bind(S, $this);    # Give the socket an address
    die $! unless connect(S, $that); # Call up the remote host
    select (S); $| = 1; select (STDOUT);# Set the socket to be command buffered
    print S "$them\n";               # Tell the remote daemon the user you want
    while (<S>) {                    # While the remote daemon is sending info
        if (/Login name: $them/) {   # Read until the user you want appears
            do { $trash = <S>; } until $trash =~ /Directory/;
            chop($their_finger = <S>); # Read the line we want
            if ($their_finger =~ /On since/i) { $their_on = 1; }
            @trash = <S>;             # Black hole the rest of the info.
        }
    }
    if ($their_on) {                 # Page me lots :)
        open(MYTTY, ">$my_tty");
        print(MYTTY "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
        print(MYTTY "$their_name is on $their_site\n");
        print(MYTTY "$their_finger\n");
        print(MYTTY "\n\n\n\n\n\n\n");
        close(MYTTY);
    }
    sleep $sleep_time;
}