#!/usr/bin/perl
# Kai Keinanen, kmk@cc.tut.fi
# Please obtain the README file before using this. Thanks
# This is public version 1.0, Nov-08-1993.

$user = 'kmk';	# replace this with the IRC user as seen by 'ps axu'
do 'sys/socket.ph' || die "sys/socket.ph: $!";

$port = 6667;
$out = "PING haloo\nQUIT\n";

$saddr = pack("CCCC", 130, 230, 102, 10);
$sin = pack("S n a4 x8", &AF_INET, $port, $saddr);
socket(SOCKET, &AF_INET, &SOCK_STREAM, &PF_UNSPEC) || die "socket: $!";
$SIG{'ALRM'} = 'noans';
alarm(5);
connect(SOCKET, $sin) || die "connect: $!";
syswrite (SOCKET, $out, length($out));
sysread(SOCKET, $_, 80);

sub noans {
    print "Alarm has expired.\n";
    open(PIDS, '/bin/ps axu | grep ircd |') || die "ps: $!";
    while (<PIDS>) {
      next if (/grep/);
      /$user\s*(\d*).*/;
      print "Killed pid $1...\n";
      kill (-9, $_);
      print "Sleeping 30 seconds...\n";
      sleep(30);
      exec '/mnt/net/irc/etc/ircd';
    }
}
