#! /usr/bin/perl -w

sub getWhois {
   my $domain = shift;
   my @results;
   my $filename = "cache/$domain.whois";
   if ( -e $filename ) {
      print STDERR "Looking up $domain from cache\n";
      open WHOIS,"<$filename" or die "Could not read $filename: $!";
      @results = <WHOIS>;
      close WHOIS;
   } else {
      @results = `whois $domain`;
      print STDERR "Retrieving $domain from whois\n";
      sleep(15);
      open WHOIS,">$filename" or die "Could not write to $filename: $!";
      print WHOIS @results;
      close WHOIS;
   }
   return @results;
}

sub checkDomain {
   my $domain = shift;
   my $results = join( "\n", getWhois( $domain ) );
   my %nameservers;
   while ( $results =~ m/Name Server:\s*(.*)$/gm ) {
      $nameservers{lc $1} = 1;;
   }
   return "$domain\t" . (%nameservers ? join( "\t", sort keys %nameservers ) : 'Possibly bad' );
}

my @results;
while ( my $domain = <> ) {
   chomp $domain;
   next unless $domain;
   push( @results, &checkDomain( $domain ) );
}
print join( "\n", @results ) . "\n";
1;
