#!/usr/bin/perl

package EPP::whois;

use strict;
use lib('/home/httpd/lib');
use UBILIBET::Debug;
use UBILIBET::Config qw(:path :epp :esnic);
use EPP::Client;
use EPP::Actions::ESNIC;

use UBILIBET::Log;
our $log = new UBILIBET::Log({'filename' => '/var/log/whois.log', 'screen' => 0, 'write' => 1, 'open' => 0});

use Data::Dumper;
################################################################################

my $nom = $ARGV[0] || die("No s'ha especificat cap domini");
my $ext = $ARGV[1] || 'es';
my $domain = {'nom' => $nom, 'extensio' => $ext };

$log->log("Dominio consultado: " .$domain->{'nom'}.".".$domain->{'extensio'});

my $epp = new EPP::Client({ 'url' => EPP_SERVER_URL,
   	                        'lib' => 'ESNIC',
      	                    'debug' => 0,
         	             });
$epp->setAuth('user', 'password');

my $dominiInfo = EPP::Actions::ESNIC::getDomainInfo($epp, $domain);

$log->log("Resultado: " . $dominiInfo->{'name'});

unless($dominiInfo) {
	print("Dominio no registrado en ESNIC\n");
	exit();
}

my $ca = EPP::Actions::ESNIC::getContactInfo($epp, $dominiInfo->{'contact'}->{'admin'});
my $ct = EPP::Actions::ESNIC::getContactInfo($epp, $dominiInfo->{'contact'}->{'tech'});

my $dns_str = join("\n", @{$dominiInfo->{'ns'}});

my $body = <<BODY;
################################################################################
# ESNIC WHOIS
################################################################################

___[ DOMINIO ]__________________________________________________________________
Nombre:           $dominiInfo->{'name'}
Handle:           $dominiInfo->{'handle'}
Fecha creaci�n:   $dominiInfo->{'creat'}
Fecha expiraci�n: $dominiInfo->{'expira'}

___[ TITULAR ]__________________________________________________________________
Nombre:		$dominiInfo->{'registrant'}
Puedes encontrar la informaci�n completa en el Whois oficial de ESNIC:
<a href="https://www.nic.es/esnic/esn/verValidacionWhoisAction?dominio=$nom.$ext" target="_blank">Whois ESNIC</a>

___[ CONTACTO ADMINISTRATIVO ]__________________________________________________
Nombre:		$ca->{'name'} ($ca->{'email'})
Handle:		$dominiInfo->{'contact'}->{'admin'}$dominiInfo->{'contact'}->{'admin'}
Direcci�n:	$ca->{'street'}
         	$ca->{'pc'}, $ca->{'city'} 
         	$ca->{'sp'} ($ca->{'cc'})
Tel�fono:	$ca->{'voice'}
Fax:			$ca->{'fax'}

___[ CONTACTO T�CNICO ]_________________________________________________________
Nombre:		$ct->{'name'} ($ct->{'email'})
Handle:		$dominiInfo->{'contact'}->{'tech'}
Direcci�n:	$ct->{'street'}
         	$ct->{'pc'}, $ct->{'city'} 
         	$ct->{'sp'} ($ct->{'cc'})
Tel�fono:	$ct->{'voice'}
Fax:			$ct->{'fax'}

___[ SERVIDORES DNS ]___________________________________________________________
$dns_str

BODY

print($body);
$log->log("BODY:" . $body);

1;
