#!/usr/bin/perl

#  whois2 - Recursive whois client.
#  Copyright (C) 1999 Tom Rothamel
#  
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#  
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#  
#  You should have received a copy of the GNU General Public License
#  along with this program; see the file COPYING.  If not, write to
#  the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
#  Boston, MA 02111-1307, USA.


use IO::Socket;

sub whoisit {
	my $server = shift;
	my $domain = shift;
	my $whois;
	
	print "[$server]\n";

	my $scon = new IO::Socket::INET(PeerAddr => $server,
				  PeerPort => 'whois',
				  Proto => 'tcp');
	unless ($scon) {
		print "Couldn't connect to whois server. Sorry.\n";
		return;
	}
	
	print $scon $domain, "\r\n";

	while (<$scon>) {
		print;
		if (/Whois Server: (.*)$/) {
			$whois = $1;
		}
	}

	return $whois;
}

# End common code.

if ($ARGV[0] eq '-h' || $ARGV[0] eq '--help' || ! $ARGV[0]) {
	print <<EOT;
Usage: whois2 domain[\@server] [domain[\@server]] ...
EOT
	exit;
}

for $_ (@ARGV) {
	my ($domain, $server) = split '@';
	$server ||= 'whois.crsnic.net';

	while ($server = whoisit($server, $domain)) {}
}

=head1 NAME

whois2 - recursively queries whois servers.

=head1 SYNOPSIS

  whois2 domain[@server] [domain[@server]] ...

=head1 DESCRIPTION

The whois2 command recursively queries whois servers. This makes it simple
to determine the complete information about domain ownership in today's
shared registry system. For each domain listed on the command line, 
whois2 will query the server given. (If no server is given for that
domain, whois2 will query whois.crsnic.net.) Whois2 will print out the
response. If the response contains a 'Whois Server:' field, whois2 will
then proceed to contact that server and repeat the request.

=head1 AUTHOR

Tom Rothamel <tom-whois2@onegeek.org> for
Computerworks of Northport (http://www.computerworks.net/), who later
gave permission for its global release.

=head1 WEB SITE

The whois2 web site is located at 

	http://onegeek.org/~tom/software/whois2/

=head1 SEE ALSO

L<whois(1)>

=cut
