#!/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.
#

$logfile = "/local/irc/userlog" ;		# Your userlogfil.
$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    = " Sun Jan 29 20:29:07 GMT 1995";
$Version = "1.00-003";
$ZCAT    = "zcat" ;
$GZIP    = "gzip -cd";

while ($i = shift) {
  $ddfil = shift, next if ($i =~ /^-d$/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: newdoms [options] [logfile] ... [logfile]
 Options:
   -d <IDFile>    Uses IDFile as an alternative Ircstat Description file.
   -l <logfile>   Uses logfile instead of default logfile.
   -i             Reads from STDIN.

 Logfiles:
   Logfiles 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 NEWDOMAINS version $Version - $Date.\n",
        "Written by Ove Ruben R Olsen - Copyright (C) 1995.\n\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 @logfil as logfile(s).\n" ;
  foreach (@logfil) { &LOG ($_) ; }
}


print "# New domains found:\n\n";
delete $funnet{""};
foreach (keys %funnet) {  
  if ($_ =~ /^\d+.\d+/) { print "dt ^$_\t\t$_\t\tfar\n"; next ; }
  print "dt $_\$\t\t$_\t\tfar\n"; 
}
print "\n\n";
foreach (keys %funnet) { 
  print "di $_\t- No information avail.\n"; 
}

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


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]: " unless $stille ;

  while (<FILEN>) {
    print STDERR "." if ( $. % 250  ==  0 )  ;
    ($dom) = ($_ =~ /(\w+.\w+)\s+\[\w+\]$/);
    if ($dom =~ /^\d+\.\d+$/) {
      # print "INN  $dom";
      ($dom) = ($_ =~ /@(\d+\.\d+\.\d+\.\d+)\s+\[\w+]$/) ;
      # print " D $dom\n";
      $f = 0 ;
      foreach $k (keys %def) {
	# print "$dom -> $k\n";
	if ($dom =~ /$k/)  {
	  $f = 1 ;
	  # print "skip $k\n";
          ($dom) = ($dom =~ /^(\d+\.\d+\.\d+)/) ;
	  $def{"^$dom"} = 1;
          last ;
	}
        next if ($f == 1) ;
	# print "ADD ^$dom\n";
      }
    }
    next if ($domains{$dom});
    next if ($funnet{$dom});
    $funnet{$dom} = 1 ;
  }
  print STDERR "\n";

}


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

sub IDF {
  if ($_ =~  /^dt/) {
    ($ju,$ju,$dom) = split (/\s+/);
    next if ($dom eq "");
    $domains{$dom} = 1 ;
    $def{$ju} = 1 if ($ju =~ /\d+/);
  }
}
################################################################################
