#!/usr/bin/perl

use Net::SenderBase;
use Getopt::Std;

## Command to options
getopts('cgrf:i:l:m:d:', \%options);

sub HELP_MESSAGE {
    print <<EOF;

 Usage: -f : List of hated country codes
        -m : Your Countrys code
        -i : remote ipaddress (can NOT be used with -d)
        -l : log to this file
	-r : Exit code 3 (aka reject for xmail)
	-g : log good countries too
	-d : File to get to get remote domain from (can NOT be used with -i)
        -b : From address
	-c : Use Caching (requires Cache::FileCache module)

  Exmaple (reject only hated Countries):
  ./test.pl -f "JP:TW" -i "555.555.555.555"

   
  Exmaple (reject every one but my country):
  ./test.pl -m "US" -i "555.555.555.555"

EOF
}

sub VERSION_MESSAGE {
    print "$0 Version: 1.0 \n";
}

## Log info
sub LogIt {
 my $txt  = shift;
 my $file = shift;
 if ($file ne "") {
  open(LOG, ">>$file") or warn "Failed to open $LogFile. Message was $txt\n";
   print LOG localtime() . " " . $txt . "\n";
  close(LOG);
 }
}


my $MyCountry        = $options{'m'} if defined $options{'m'};
my $HatedCountrys    = $options{'f'} if defined $options{'f'};
my $RemoteIP         = $options{'i'} if defined $options{'i'};
my $LogFile          = $options{'l'} if defined $options{'l'};
my $File             = $options{'d'} if defined $options{'d'};
my $From             = $options{'b'} if defined $options{'b'};
my $Exit             = 0;
my $RemoteCountry    = "";
my $FromCache        = "No";

if (defined $options{'c'}) {
 use Cache::FileCache;
 my $cache_options_ref = { namespace => 'CountryCode', default_expires_in  => '1 day', auto_purge_interval => '1 hour'};
 my $cache;
}

# Covert all to upper
$MyCountry     =~ tr/a-z/A-Z/; 
$HatedCountrys =~ tr/a-z/A-Z/; 
@HatedCountries = split(/:/, $HatedCountrys);

if (defined $options{'c'}) {
 $cache = Cache::FileCache->new($cache_options_ref);
}

## Display help if -h is given
if (defined $options{'h'}) {
 HELP_MESSAGE();
 exit(0);
}

if ($MyCountry eq "" && $HatedCountrys eq "") {
 print "You forgot to define your country (-m \"US\") OR define a list of hated countried (-f \"JP:BR:TW\")\n";
 LogIt("Error : You forgot to define your country (-m \"US\") OR define a list of hated countried (-f \"JP:BR:TW\")",$LogFile);
 HELP_MESSAGE();
 exit(0);
}

if (defined $options{'d'} && defined $options{'i'}) {
 print "You can only use -d OR -i but not both.\n;";
 LogIt("Error : You can only use -d OR -i but not both.",$LogFile);
 HELP_MESSAGE();
 exit(0);
}

if (defined $options{'d'}) {
 open(FileData, $File);
 my $line = <FileData>;
 chomp($line);
 close FileData;
 my @InfoData = split(/;/, $line);
 my $ClientDomain = @InfoData[0];
 $RemoteIP = $ClientDomain; 
}


if (defined $options{'c'}) {
 $RemoteCountry = $cache->get( $RemoteIP );
}

if ($RemoteCountry eq "") {
 my $query = Net::SenderBase::Query->new(Transport => 'dns', Address => $RemoteIP, Host => 'test.senderbase.org',); 
 my $results = $query->results();
 $RemoteCountry = $results->ip_country;
} else {
 $FromCache = "Yes";
}

if ($RemoteCountry eq "") {
 LogIt("Error : Error getting info about : $RemoteIP",$LogFile);
 exit($Exit);
}

if (defined $options{'c'} && $FromCache ne "Yes") {
 $cache->set( $RemoteIP, $RemoteCountry);
}

if ($RemoteCountry ne $MyCountry || grep (/$RemoteCountry/, @HatedCountries)) {
 $Exit = 3;
}

if (defined $options{'r'} && $Exit == 3) {
 $Txt = "Blocking";
} elsif (!defined $options{'r'} && $Exit == 3) {
 $Txt = "Would Have Blocked";
 $Exit = 0;
} elsif ($LogFile) {
 $Txt = "Allowing";
}

LogIt("$Txt : $RemoteIP : $RemoteCountry : $FromCache",$LogFile);

exit($Exit);
