# whois.pl - simple whois client
#
# Copyright (c) 1993, 1994, 1995, 1996, 1997, 1998, 1999 by RIPE NCC
#
# All Rights Reserved
#
# Permission to use, copy, modify, 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, and that the name of the author not be
# used in advertising or publicity pertaining to distribution of the
# software without specific, written prior permission.
#
# THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
# ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS; IN NO EVENT SHALL
# AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
# DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
# AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#
# $Id: whois.pl,v 2.8 1999/10/06 16:41:25 marek Exp $
#
#	$RCSfile: whois.pl,v $
#	$Revision: 2.8 $
#	$Author: marek $
#	$Date: 1999/10/06 16:41:25 $

# This is a very simple whois client, but it knows the new server arguments

require "getopts.pl";

&Getopts('aFg:h:i:LmMp:rRs:St:v:T:');

if ((!@ARGV) && (!$opt_t) && (!$opt_g) && (!$opt_v) && (!(($opt_g) && ($opt_t)))) {
    
    print <<"EOF";

Usage: whois [-acFLmMrSv] [-h hostname] [-s sources] [-T types] [-i attr] keys
       whois -t type 	whois -v type

Where:

-a                         search all databases
-F                         fast raw output
-h hostname                search alternate server
-i attr[[,attr] ... ]      do an inverse lookup for specified attributes
-L                         find all Less specific matches
-m                         find first level more specific matches
-M                         find all More specific matches
-p port                    port to connect to
-r                         turn off recursive lookups
-R                         do not trigger referral mechanism
-s source[[,source] ... ]  search databases with source 'source'
-S                         tell server to leave out 'syntactic sugar'
-t type                    requests template for object of type 'type'
-v 			   requests verbose template for object of type 'type'
-T type[[,type] ... ]      only look for objects of type 'type'

Please note that most of these flags are NOT understood by
non RIPE whois servers
EOF
	exit(0);
}

if (!$opt_h) {
  $opt_h=$RIPEConfig{WHOISHOST};
}

if ($opt_L) {
    print STDERR "whois: warning -L overrides -m\n" if $opt_m;
    print STDERR "whois: warning -L overrides -M\n" if $opt_M;
    $opt_m = 0;
    $opt_M = 0;
}

if ($opt_m) {
    print STDERR "whois: warning -m overrides -M\n" if $opt_M;
    $opt_M = 0;
}

local($query)="";

$query.="-a "            if ($opt_a);
#
#  undocumented option, get specified updates:
#
# -g Source:First-Last       get updates with 'Source' from 
#                            serial 'First' till 'Last' (you may use LAST)        
$query.="-g ".$opt_g." " if ($opt_g);
$query.="-F "            if ($opt_F);
$query.="-i ".$opt_i." " if ($opt_i);
$query.="-L "            if ($opt_L);
$query.="-m "            if ($opt_m);
$query.="-M "            if ($opt_M);
$query.="-r "            if ($opt_r);
$query.="-R "            if ($opt_R);
$query.="-S "            if ($opt_S);
$query.="-s ".$opt_s." " if ($opt_s);
$query.="-t ".$opt_t." " if ($opt_t);
$query.="-v ".$opt_v." " if ($opt_v);
$query.="-T ".$opt_T." " if ($opt_T);


#
# tell the server what version of RIPE whois we are running,
# but only if we are sure that we are talking to an
# RIPE whois server

if (($opt_h=~ /(ripe\.net|ra\.net|apnic\.net|mci\.net|isi\.edu|garr\.it|ans\.net)$/i) ||
    $opt_a || $opt_g || $opt_F || $opt_i || $opt_m || $opt_M || $opt_R ||
    $opt_L || $opt_r || $opt_s || $opt_S || $opt_t || $opt_v || $opt_T) {
    
   $query.="-Vwp2.0 ";

}

$query.=join(" ",@ARGV);


#
# returning sockets doesn't work with perl4 so we use the global one ;-(

local($msg);

if ($msg=&initwhoisqry($opt_h,$opt_p?$opt_p:"", $query)) {
   
   print STDERR $msg;
   exit 1;

}

#
# exit cleanly when asked to ...

sub quitconnection {
   
    close(WHOIS_S);
    print "\nConnection closed.\n";
    
    exit;

}

$SIG{'HUP'}='quitconnection';
$SIG{'INT'}='quitconnection';
$SIG{'KILL'}='quitconnection';
$SIG{'TERM'}='quitconnection';

while (<WHOIS_S>) {
    print;
}

close(WHOIS_S);

# end of program
