#!/local/bin/perl
#          
# Written by: Ove Ruben R Olsen (ruben@uib.no) 
#
################################################################################
#
# Copyright (c) Ove Ruben R Olsen (Gnarfer from hell)
#
# Permission to use, copy, and distribute this software and its
# documentation for any purpose and without fee is hereby granted,
# provided that the above copyright notice appear in all copies and that
# both that copyright notice and this permission notice appear in
# supporting documentation.
# This also apply your own incarnations of any or all of this code.
# If you use portions of this code, you MUST include a apropriate notice
# where you "stole" it from.
#
# THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
# WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
# MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
#
# Email:  Ruben@uib.no 
#
#
# This work is free software, but if you find it usefull I would appreciate
# if you donated aprox 10 USD to your country's cancer research.
#
#
# This work is entierly dedicated to Johanne B Revheim
#
################################################################################
# Alter this lines to suit your site.
#

$ddfil   = "/local/irc/ircstat.idf" ;      # Your domain description file.


# There is no need going beyond this line unless you are very eager to
# debug and destroy this work :-)
################################################################################
################################################################################

$Date    = " Sat Jan 21 19:34:54 GMT 1995.";
$Version = "1.00-001" ;
$ZCAT    = "zcat" ;
$GZIP    = "gzip -cd";

die "Farnear needs a argument.\n" if ($#ARGV == -1);

while ($i = shift) {
  $ddfil = shift, next if ("-d" eq $i) ;
  push (@logfil,$i), next if ($i !~ /^\-/) ;
  push (@logfil,shift(ARGV)), next if ("-l" eq $i)  ;
  $STDIN  = 1  if ($i eq "-i");

  if ("-h" eq $i) {
    print (STDERR <<SLUTT);

Usage: farnear [options] [logfile]
 Options:
   -d <IDFile>    Uses IDFile as an alternative Ircstat Description file.
   -l <logfile>   Uses logfile.
   -i             Reads from STDIN.

 Logfile:
   Logfile is for the lazy ones who do not want to use the -l option.

SLUTT
  exit ;
  }
  
}

push (@logfil,$logfile) unless @logfil;

print STDERR "\nThis is FARNEAR version $Version - $Date.\n",
        "Written by Ove Ruben R Olsen - Copyright (C) 1995.\n\n" ; 

print STDERR "Using IDF file $ddfil.\n";
open (FIL, $ddfil) || warn "\nCannot open IDF file $ddfil.\n";
while (<FIL>) { &IDF() ; } close (FIL);


if ($STDIN) {
  print STDERR "Using STDIN as logfile(s).\n" ;
  &LOG ();
} else {
  print STDERR "Using @logfile as logfile(s).\n" ;
  foreach (@logfil) { &LOG ($_) ; }
}

print "The FAR-NEAR report for IRC-SERVER $server

TOTAL INFORMATION
^^^^^^^^^^^^^^^^^

                   Hosts  Logins     Users           Login time    Percent\n";
printf "Faraway hosts    %6s %8s %9s    %10s %10s\n", 
      $farc, $farlogins, $farusers , &skriv($fartime), 
      &Precent($fartime,$fartime+$neartime);
printf "Total figures    %6s %8s %9s    %10s %10s\n",
    $farc+$nearc,$farlogins+$nearlogins, $farusers+$nearusers , 
    &skriv($fartime+$neartime), "100.00";
printf "Near hosts       %6s %8s %9s    %10s %10s\n",
    $nearc, $nearlogins, $nearusers , &skriv($neartime), 
    &Precent($neartime,$fartime+$neartime);

print "
FAR HOSTS
^^^^^^^^^
Hosts                Logins     Users           Login time  Percent
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
";
foreach (@FAR) {
   $teller ++ ;
   ($host,$login,$users,$time) = unpack ("A40 A40 A40 A40",$_);
   printf "%-17s   %6s %9s %20s %8s\n", 
   $host,$login,$users,&skriv($time),&Precent($time,$fartime);
   last if ($time < 86400);
}

print "\n", $farc - $teller ," far hosts skipped.\n";

print " 

NEAR HOSTS
^^^^^^^^^^
Hosts                Logins     Users           Login time  Percent
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
";
$teller = 0 ;
foreach (@NEAR) {
   $teller ++ ;
   ($host,$login,$users,$time) = unpack ("A40 A40 A40 A40",$_);
   printf "%-17s   %6s %9s %20s %8s\n", 
   $host,$login,$users,&skriv($time),&Precent($time,$fartime);
   last if ($time < 86400);
}
print "\n", $nearc - $teller ," near host skipped.\n";

################################################################################
sub Precent {
  return "0.00" if (@_[0] == 0) ;
  $a = (@_[0] * 100) / @_[1] ;
  if ($a > 0.01) {
    ($a,$j) = split (/\./,$a) ;
    join (".",$a, unpack ("a2",$j)) ;
  } else {
    "0.00" ;
  }
}
################################################################################
sub skriv {
    local ($a,$b,$c,$d) ;
    $a = @_[0] % 60;
    $b = (@_[0] % 3600) / 60;
    $c = (@_[0] % (24*3600)) / 3600;
    $d = @_[0] / (24 * 3600);
    sprintf   ("%3d ds + %02d:%02d:%02d", $d, $c, $b, $a);
}



sub LOG {
  $ofil = @_[0];
  if ($STDIN == 0) {
    if (! (-e $ofil && -R $ofil ))  {
       print STDERR "Cannot open userlog file: $ofil\n";
       return  ;
    }
    $ofil = "$ZCAT $ofil |" if ($ofil =~ /.Z$/);
    $ofil = "$GZIP $ofil |" if ($ofil =~ /.gz$|.z$/);
    open(FILEN, $ofil) ; 
  } else {
    open (FILEN, "-");
  }
  print STDERR "Processing @_[0]: \n";

  $flag = 0 ;
  while (<FILEN>) {
    if ($_ =~ /IRC SERVER (.+) \d+/) { $server = $1; next ; }
    $flag = 1 if (/^Num  D/) ;
    if ($flag == 1 ) {
      # print "FLAG $_";
      next if (/^\^|^Num  D|^$/) ;
      last if (/^\w+/) ;
      ($junk,$dom,$users,$logins,$time) = unpack("a4 A19 A6 A7 a19",$_);
      ($dom,$junk) = split(/\s+/,$dom,2);
      ($dg,$ti,$mi,$se) = ($time =~ (/(\d+) ds \+ (\d+):(\d+):(\d+)/));
      $sek = $dg * 86400 + $ti * 3600 + $mi * 60 + $se;
      if (! defined($domains{$dom}) ) {
	print STDERR "  $dom not found in the IDF file. Far assumed.\n";
	$fartime += $sek; $farusers += $users ; $farlogins += $logins ;
	push (@FAR,pack("A40 A40 A40 A40",$dom,$logins,$users,$sek)); 
	$farc ++ ;
      }
      if ($domains{$dom} =~ /far/i) {
	$fartime += $sek; $farusers += $users ; $farlogins += $logins ;
	push (@FAR,pack("A40 A40 A40 A40",$dom,$logins,$users,$sek)); 
	$farc ++ ;
      } 
      if ($domains{$dom} =~ /near/i) {
	$neartime += $sek; $nearusers += $users ; $nearlogins += $logins ;
	push (@NEAR,pack("A40 A40 A40 A40",$dom,$logins,$users,$sek)); 
	$nearc ++ ;
      }
    }
  }

}


################################################################################

sub IDF {
  if ($_ =~  /^dt/) {
    ($ju,$ju,$dom,$fn) = split (/\s+/);
    next if ($dom eq "");
    $fn = "far" if ($fn eq "");
    # print "DOM $dom $fn\n";
    $domains{$dom} = $fn ;
  }
}
################################################################################
