#!/usr/bin/perl -w

use Net::Whois::Raw qw( whois $OMIT_MSG $CHECK_FAIL $CHECK_EXCEED $TIMEOUT $CACHE_DIR @SRC_IPS );
use Getopt::Std;
use strict;
use vars qw($opt_s $opt_c $opt_S $opt_C $opt_d $opt_e $opt_t $opt_T $opt_a $opt_h);

getopts("scSCetdT:a:h");

if ($opt_h) {
USAGE:
	print <<EOM;
Usage:	$0 [ -s | -S ] [ -c | -C ] [ -d ] [ -T <timeout> ] [ -a <src_ip> ] [ -t ] <domain> [ <server> ]

The -s switch attempts to strip the copyright message or disclaimer.
The -S switch will attempt some exra rules for it.
The -c switch attempts to return an empty answer for failed searches.
The -C switch will attempt some exra rules for it.
The -e switch forces die if connection rate to server have been exceeded.
The -T switch takes a parameter that is used for the timeout for
connection attempts.
The -t switch enables caching.
The -a switch takes a parameter: ip address that should be used as source
address instead of a default one.
The -d switch enables debugging messages.
EOM
	exit; 
}

$OMIT_MSG = $CHECK_FAIL = 0;

$OMIT_MSG = 1 if $opt_s;
$OMIT_MSG = 2 if $opt_S;
$CHECK_FAIL = 1 if $opt_c;
$CHECK_FAIL = 2 if $opt_C;
$CHECK_EXCEED = 1 if $opt_e;
$TIMEOUT = $opt_T;
$CACHE_DIR = undef;
@SRC_IPS = ($opt_a) if $opt_a;

$Net::Whois::Raw::DEBUG = 1 if $opt_d;

if ($opt_t) {
    if ($^O =~ /Win/) {
        $CACHE_DIR = $ENV{'TEMP'} || "C:\\temp";
    } else {
        $CACHE_DIR = $ENV{'TEMP'} || "/tmp";
        my @ent = getpwuid($>);
        if (@ent) {
            foreach ("/tmp/$ent[0]", "$ent[7]/.pwhois") {
                mkdir $_, 0644;
                if (open(O, ">$_/__$$-$$.tmp")) {
                    close(O);
                    unlink "$_/__$$-$$.tmp";
                    $CACHE_DIR = $_;
                    last;
                 }
            }
        }
    }
}

my $dom = $ARGV[0] || goto USAGE;

my $server = $ARGV[1];

eval {
	my $result = whois($dom, $server);
	if ($result) {
		print $result;
	} else {
		print STDERR "Failed.\n";
	}
};
if ($@) {
	my $err = $@;
	$err =~ s/\s+at \S+ line \d+\.$//;
	print "\nWhois information could not be fetched:\n$err\n";
	exit -1;
}	

__END__

=head1 NAME

pwhois   - Perl written whois client

=head1 SYNOPSIS

	pwhois perl.com
	pwhois gnu.org
	pwhois -s police.co.il
	pwhois -c there.is.no.tld.called.foobar
	pwhois yahoo.com whois.networksolutions.com
	pwhois -T 10 funet.fi

etc etc.....

=head1 DESCRIPTION

Just invoke with a domain name, optionally with a whois server name.
The B<-s> switch attempts to strip the copyright message or disclaimer.
The B<-c> switch attempts to return an empty answer for failed searches.
The B<-T> switch takes a parameter that is used for the timeout for
connection attempts.
 
=head1 AUTHORS

Ariel Brosh B<schop@cpan.org>

Current Maintainer: Walery Studennikov B<despair@cpan.org>

=head1 SEE ALSO

L<Net::Whois::Raw>.

