#!/usr/bin/perl

# StatBot v1.1

# mcl@irc.pl
# nexus@irc.pl
# axs@wanadoo.be

use Socket;

# Where to store output file
$MRTG_PATH = "/usr/local/mrtg/irc";

# Server to query for national statistics
$TLDlink="ircd.belnet.be";
$hostmask="*.be";

# Server to query for global statistics
$uplink="ircnet.wanadoo.be";

# List of servers to query for local statistics
$server{"ircnet.wanadoo.be"}=0;
$server{"ratbert.skybel.net"}=0;
$server{"irc.krameria.net"}=0;
$server{"irc.vub.ac.be"}=0;
$server{"irc.belnet.be"}=0;

# IRC server the bot will connect to
$ircserv = "localhost";
$ircport = "6667";

# Bot parameters
$botnick = "statbot11";
$botuser = "monitor";
$botname = "http://irc.wanadoo.be/stats";

while (true){
  ($g, $g, $proto) = getprotobyname("tcp");
  ($g, $g, $g, $g, $rawserver) = gethostbyname($ircserv);
  $serveraddr = pack("Sna4x8", 2, $ircport, $rawserver);
  socket(SOCKET, AF_INET, SOCK_STREAM, $proto) || die "No socket: $!";
  if (connect(SOCKET, $serveraddr)){
    select(SOCKET); $| = 1; # flush immediatelly SOCKET data
    select(STDOUT); $| = 1; 
 
    print SOCKET "USER $botuser 2 3 :$botname\n";
    print SOCKET "NICK $botnick\n";
 
    while (<SOCKET>){
      @i = split(" ",$_);
      if ($i[1] eq "433") {print SOCKET "NICK s".time()."\n";}
      elsif ($i[1] eq "437") {print SOCKET "NICK s".time()."\n";}
      elsif ($i[1] eq "251" && $i[0] eq ":$TLDlink") {$tld=$i[5]}
      elsif ($i[1] eq "251" && $i[0] eq ":$TLDlink") {$world=$i[5]}
      elsif ($i[1] eq "252" && $i[0] eq ":$TLDlink") {$globalop=$i[3]}
      elsif ($i[1] eq "254" && $i[0] eq ":$TLDlink") {$globalch=$i[3]}
      elsif ($i[1] eq "376") {&inforeq}
      elsif ($i[1] eq "402") {$server{$i[3]}=0}
#      elsif ($i[1] eq "PRIVMSG" && $i[3] eq ":save") {&infosave}
#      elsif ($i[1] eq "PRIVMSG" && $i[3] eq ":uplink") {$uplink="$i[4]"}
#      elsif ($i[1] eq "PRIVMSG" && $i[3] eq ":tldlink") {$TLDlink="$i[4]"}
      elsif ($i[1] eq "255" && $i[0] ne ":$uplink") {$i[0]=~s/\://;$server{$i[0]}=$i[5];$globals=$i[10]}
      elsif ($i[0] eq "PING") {
        &infosave;
        print SOCKET "PONG $i[1]\n";
        &inforeq;
      }
  print "$_";
    }
  }
  close(SOCKET);
  sleep(60); 
}

sub infosave {
  foreach $s (keys %server){
    open(OUTPUT,">$MRTG_PATH/$s");
    print OUTPUT "$server{$s}\n$server{$s}\n$time\n";
    close(OUTPUT); 
  }
  open(OUTPUT,">$MRTG_PATH/irc");
  print OUTPUT "$world\n$world\n$time\n";
  close(OUTPUT);
  open(OUTPUT,">$MRTG_PATH/irc.tld");
  print OUTPUT "$tld\n$tld\n$time\n";
  close(OUTPUT);
  open(OUTPUT,"$MRTG_PATH/global.channel");
  print OUTPUT "$globalch\n$globalch\n$time\n";
  close(OUTPUT);
  open(OUTPUT,"$MRTG_PATH/global.server");
  print OUTPUT "$globals\n$globals\n$time\n";
  close(OUTPUT);
  open(OUTPUT,"$MRTG_PATH/global.ircop");
  print OUTPUT "$globalop\n$globalop\n$time\n";
  close(OUTPUT);
}
 
sub inforeq {
  foreach $s (keys %server){print SOCKET "lusers * $s\n";}
  print SOCKET "lusers $hostmask $tldlink\n";
  print SOCKET "lusers * $uplink\n";
  $world=0;
  $tld=0;
  $time=time();
}
