#!/usr/bin/perl
#
# iwhois.pl
# Copyright (c) 1996-1999 SurfUtah.Com
# written by Rus Berrett
#
# $Id: iwhois.pl,v 1.3 1999/12/03 23:21:44 rus Exp $
#
# check domain name availability (any top level domain)

##############################################################################
# initialization and configuration

# where does everything live
$whois = "/usr/bin/whois";

# check for existence of necessary files
unless (-e "$whois") {
  print "$0: Cannot find \"$whois\".\n";
  exit(1);
}

##############################################################################
# process the data

$domain = $ARGV[0];
$domain =~ s/[^a-zA-Z0-9-\.]//g;

$hostname = tld_hostname($domain);

$output = 0;
open(PIPE, "$whois -h $hostname $domain |");
while(<PIPE>) {
  print $_;
  $output = 1;
}
close(PIPE);

if ($output == 0) {
  print "$0: timeout connecting to host, $hostname\n";
}

##############################################################################
sub tld_hostname
{
  local($mydomain) = @_;
  local(@parts, $tld);

  @parts = split(/\./, $mydomain);
  $tld = $parts[$#parts]; 

  if (($tld eq "arpa") || # old style arpanet
      ($tld eq "com") ||  # us commercial
      ($tld eq "edu") ||  # us educational
      ($tld eq "gov") ||  # us government
      ($tld eq "net") ||  # network
      ($tld eq "org")) {  # non-profit
    $hostname = "whois.internic.net";
  }

  elsif (($tld eq "int") || # international
         ($tld eq "us")) {  # united states
    $hostname = "whois.isi.edu";
  }

  elsif ($tld eq "mil") {   # us military
    $hostname = "nic.ddn.mil";
  }

  elsif ($tld eq "pr") {   # puerto rico
    $hostname = "whois.uprr.pr";
  }

  #
  #   south america
  #

  elsif ($tld eq "mx") {   # mexico
    $hostname = "whois.nic.mx";
  }

  #
  #   europe
  #

  elsif (($tld eq "gb") ||  # great britian (uk)
         ($tld eq "uk")) {  # united kingdom
    $hostname = "whois.nic.uk";
  }

  elsif (($tld eq "at") || # austria
         ($tld eq "be") || # belgium
         ($tld eq "bg") || # bulgaria
         ($tld eq "cz") || # czech republic
         ($tld eq "de") || # germany
         ($tld eq "dk") || # denmark
         ($tld eq "fi") || # finland
         ($tld eq "fr") || # france
         ($tld eq "gr") || # greece
         ($tld eq "gl") || # greenland
         ($tld eq "hr") || # croatia (local name: hrvatska)
         ($tld eq "hu") || # hungary
         ($tld eq "ie") || # ireland
         ($tld eq "il") || # israel
         ($tld eq "is") || # iceland
         ($tld eq "it") || # italy
         ($tld eq "lt") || # lithuania
         ($tld eq "lu") || # luxembourg
         ($tld eq "lv") || # latvia
         ($tld eq "no") || # norway
         ($tld eq "pt") || # portugal
         ($tld eq "sm") || # san marino
         ($tld eq "sk") || # slovak republic
         ($tld eq "tr")) { # turkey
    $hostname = "whois.ripe.net";
  }

  elsif ($tld eq "nl") {   # netherlands
    $hostname = "domain-registry.nl";
  }

  elsif (($tld eq "ru") ||  # russian federation
         ($tld eq "su")) {  # soviet union
    $hostname = "whois.ripn.net";
  }

  elsif ($tld eq "se") {   # sweden
    $hostname = "whois.nic-se.se";
  }

  elsif (($tld eq "ch") ||  # switzerland
         ($tld eq "li")) {  # liechtenstein
    $hostname = "whois.nic.ch";
  }

  elsif ($tld eq "yu") {   # yugoslavia
    $hostname = "whois.nic.yu";
  }

  #
  #   oceania
  #

  elsif ($tld eq "au") {   # australia
    $hostname = "whois.aunic.net";
  }

  elsif ($tld eq "nz") {   # new zealand
    $hostname = "whois.waikato.ac.nz";
  }

  #
  #   middle east
  #

  #
  #   asia
  #

  elsif ($tld eq "cn") {   # china
    $hostname = "whois.cnnic.net.cn";
  }

  elsif ($tld eq "hk") {   # hong kong
    $hostname = "whois.hknic.net.hk";
  }

  elsif ($tld eq "in") {   # india
    $hostname = "whois.ncst.ernet.in";
  }

  elsif ($tld eq "jp") {   # japan
    $hostname = "whois.nic.ad.jp";
  }
  elsif ($tld eq "kz") {   # kazachstan
    $hostname = "whois.domain.kz";
  }

  elsif ($tld eq "kr") {   # korea, republic of
    $hostname = "whois.krnic.net";
  }

  elsif ($tld eq "sg") {   # singapore
    $hostname = "whois.nic.net.sg";
  }

  elsif ($tld eq "th") {   # thailand
    $hostname = "whois.thnic.net";
  }

  elsif ($tld eq "tw") {   # taiwan
    $hostname = "whois.twnic.net";
  }

  #
  #   africa and miscellaneous
  #

  elsif (($tld eq "af") ||  # afghanistan
         ($tld eq "as") ||  # american samoa
         ($tld eq "bt") ||  # bhutan
         ($tld eq "pw") ||  # palau
         ($tld eq "tm")) {  # turkmenistan
    $hostname = "whois.netnames.net";
  }
  elsif ($tld eq "lk") {   # sri lanka
    $hostname = "whois.nic.lk";
  }

  elsif ($tld eq "gf") {   # guyana (french)
    $hostname = "whois.nplus.gf";
  }

  elsif ($tld eq "ng") {   # nigeria
    $hostname = "whois.cnuce.cnr.it";
  }

  elsif ($tld eq "za") {   # south africa
    $hostname = "whois.ac.za";
  }

  #
  #   tld's with no known whois server
  #

  elsif (($tld eq "ad") || # andorra
         ($tld eq "ae") || # united arab emirates
         ($tld eq "ag") || # antigua and barbuda
         ($tld eq "ai") || # anguilla
         ($tld eq "al") || # albania
         ($tld eq "am") || # armenia
         ($tld eq "an") || # netherland antilles
         ($tld eq "ao") || # angola
         ($tld eq "aq") || # antarctica
         ($tld eq "ar") || # argentina
         ($tld eq "aw") || # aruba
         ($tld eq "az") || # azerbaidjan
         ($tld eq "ba") || # bosnia-herzegovina
         ($tld eq "bb") || # barbados
         ($tld eq "bd") || # banglades
         ($tld eq "bf") || # burkina faso
         ($tld eq "bh") || # bahrain
         ($tld eq "bi") || # burundi
         ($tld eq "bj") || # benin
         ($tld eq "bm") || # bermuda
         ($tld eq "bn") || # brunei darussalam
         ($tld eq "bo") || # bolivia
         ($tld eq "br") || # brazil
         ($tld eq "bs") || # bahamas
         ($tld eq "bv") || # bouvet island
         ($tld eq "bw") || # botswana
         ($tld eq "by") || # belarus
         ($tld eq "bz") || # belize
         ($tld eq "ca") || # canada
         ($tld eq "cc") || # cocos (keeling) islands
         ($tld eq "cf") || # central african republic
         ($tld eq "cg") || # congo
         ($tld eq "ci") || # ivory coast
         ($tld eq "ck") || # cook islands
         ($tld eq "cl") || # chile
         ($tld eq "cm") || # cameroon
         ($tld eq "co") || # colombia
         ($tld eq "cr") || # costa rica
         ($tld eq "cu") || # cuba
         ($tld eq "cv") || # cape verde
         ($tld eq "cx") || # christmas island
         ($tld eq "cy") || # cyprus
         ($tld eq "cz") || # czech republic
         ($tld eq "dj") || # djibouti
         ($tld eq "dk") || # denmark
         ($tld eq "dm") || # dominica
         ($tld eq "do") || # dominican republic
         ($tld eq "dz") || # algeria
         ($tld eq "ec") || # ecuador
         ($tld eq "ee") || # estonia
         ($tld eq "eg") || # egypt
         ($tld eq "eh") || # western sahara
         ($tld eq "es") || # spain
         ($tld eq "et") || # ethiopia
         ($tld eq "fj") || # fiji
         ($tld eq "fk") || # falkland islands (malvinas)
         ($tld eq "fm") || # micronesia
         ($tld eq "fo") || # faroe islands
         ($tld eq "fx") || # france (european territory)
         ($tld eq "ga") || # gabon
         ($tld eq "gd") || # grenada
         ($tld eq "ge") || # georgia
         ($tld eq "gh") || # ghana
         ($tld eq "gi") || # gibraltar
         ($tld eq "gp") || # guadeloupe (french)
         ($tld eq "gq") || # equatorial guinea
         ($tld eq "gm") || # gambia
         ($tld eq "gn") || # guinea
         ($tld eq "gt") || # guatemala
         ($tld eq "gu") || # guam (us)
         ($tld eq "gw") || # guinea bissau
         ($tld eq "gy") || # guyana
         ($tld eq "hm") || # heard and mcdonald islands
         ($tld eq "hn") || # honduras
         ($tld eq "ht") || # haiti
         ($tld eq "id") || # indonesia
         ($tld eq "io") || # british indian ocean territory
         ($tld eq "iq") || # iraq
         ($tld eq "ir") || # iran
         ($tld eq "jm") || # jamaica
         ($tld eq "jo") || # jordan
         ($tld eq "ke") || # kenya
         ($tld eq "kg") || # kirgistan
         ($tld eq "kh") || # cambodia
         ($tld eq "ki") || # kiribati
         ($tld eq "km") || # comoros
         ($tld eq "kn") || # saint kitts nevis anguilla
         ($tld eq "kp") || # north korea
         ($tld eq "kw") || # kuwait
         ($tld eq "ky") || # cayman islands
         ($tld eq "la") || # laos
         ($tld eq "lb") || # lebanon
         ($tld eq "lc") || # saint lucia
         ($tld eq "lr") || # liberia
         ($tld eq "ls") || # lesotho
         ($tld eq "ly") || # libya
         ($tld eq "ma") || # morocco
         ($tld eq "mc") || # monaco
         ($tld eq "md") || # moldavia
         ($tld eq "mg") || # madagascar
         ($tld eq "mh") || # marshall islands
         ($tld eq "ml") || # mali
         ($tld eq "mm") || # myanmar
         ($tld eq "mn") || # mongolia
         ($tld eq "mo") || # macau
         ($tld eq "mp") || # northern mariana islands
         ($tld eq "mq") || # martinique (french)
         ($tld eq "mr") || # mauritania
         ($tld eq "ms") || # montserrat
         ($tld eq "mt") || # malta
         ($tld eq "mu") || # mauritius
         ($tld eq "mv") || # maldives
         ($tld eq "mw") || # malawi
         ($tld eq "my") || # malaysia
         ($tld eq "mz") || # mozambique
         ($tld eq "na") || # namibia
         ($tld eq "nc") || # new caledonia (french)
         ($tld eq "ne") || # niger
         ($tld eq "nf") || # norfolk island
         ($tld eq "ni") || # nicaragua
         ($tld eq "np") || # nepal
         ($tld eq "nr") || # nauru
         ($tld eq "nt") || # neutral zone
         ($tld eq "nu") || # niue
         ($tld eq "om") || # oman
         ($tld eq "pa") || # panama
         ($tld eq "pe") || # peru
         ($tld eq "pf") || # polynesia (french)
         ($tld eq "pg") || # papua new
         ($tld eq "ph") || # philippines
         ($tld eq "pk") || # pakistan
         ($tld eq "pl") || # poland
         ($tld eq "pm") || # saint pierre and miquelon
         ($tld eq "pn") || # pitcairn
         ($tld eq "py") || # paraguay
         ($tld eq "qa") || # qatar
         ($tld eq "re") || # reunion (french)
         ($tld eq "ro") || # romania
         ($tld eq "rw") || # rwanda
         ($tld eq "sa") || # saudi arabia
         ($tld eq "sb") || # solomon islands
         ($tld eq "sc") || # seychelles
         ($tld eq "sd") || # sudan
         ($tld eq "sh") || # saint helena
         ($tld eq "si") || # slovenia
         ($tld eq "sj") || # svalbard and jan mayen islands
         ($tld eq "sl") || # sierra leone
         ($tld eq "sn") || # senegal
         ($tld eq "so") || # somalia
         ($tld eq "sr") || # suriname
         ($tld eq "st") || # saint tome and principe
         ($tld eq "sv") || # el salvador
         ($tld eq "sy") || # syria
         ($tld eq "sz") || # swaziland
         ($tld eq "tc") || # turks and caicos islands
         ($tld eq "td") || # chad
         ($tld eq "tf") || # french southern territory
         ($tld eq "tg") || # togo
         ($tld eq "tj") || # tadjikistan
         ($tld eq "tk") || # tokelau
         ($tld eq "tn") || # tunisia
         ($tld eq "to") || # tonga
         ($tld eq "tp") || # east timor
         ($tld eq "tt") || # trinidad and tobago
         ($tld eq "tv") || # tuvalu
         ($tld eq "tz") || # tanzania
         ($tld eq "ua") || # ukraine
         ($tld eq "ug") || # uganda
         ($tld eq "um") || # us minor outlying islands
         ($tld eq "uy") || # uruguay
         ($tld eq "uz") || # uzbekistan
         ($tld eq "va") || # vatican city state
         ($tld eq "vc") || # saint vincent and grenadines
         ($tld eq "ve") || # venezuela
         ($tld eq "vg") || # virgin islands (british)
         ($tld eq "vi") || # virgin islands (us)
         ($tld eq "vn") || # vietnam
         ($tld eq "vu") || # vanuatu
         ($tld eq "wf") || # wallis and futuna islands
         ($tld eq "ws") || # samoa
         ($tld eq "ye") || # yemen
         ($tld eq "zm") || # zambia
         ($tld eq "zr") || # zaire
         ($tld eq "zw")) { # zimbabwe 
    $hostname = "";
  }

  else {
    print "$0: unknown top level domain, $tld\n";
    exit(1);
  }

  if ($hostname eq "") {
    print "$0: no server known for top level domain, $tld\n";
    exit(1);
  }

  return($hostname);
}

##############################################################################
# eof

