# -------------------------------------------------------------------------------------
# flo::plugin::Whois
# -------------------------------------------------------------------------------------
#
#       Author : Jean-Michel Hiver (jhiver@cisedi.com).
#    Copyright : (c) MKDoc Holdings Ltd, 2001
# 
#      Unauthorized modification, use, reuse, distribution or redistribution
#      of this module is stricly forbidden
#
#    Description:
#
#      A plugin that is used to have a user-friendly, dynamic sitemap
#      which can be used to quickly explore the site
#
# -------------------------------------------------------------------------------------
package flo::plugin::Whois;
use Net::Whois::Raw;
use flo::Object;
use lib::sql::Table;
use lib::cgi::CGI;
use flo::Standard;
use lib::Exception;
use flo::HTTP::Header;
use lib::PSP;
use strict;
use vars qw /@ISA @TLD/;
@ISA = qw /flo::Object/;
@TLD = qw /com net org co.uk org.uk info/;


##
# $class->main;
# -------------
#  This method is invoked when it is found that a user is querying for the
#  sitemap plugin.
##
sub main
{
    my $class = shift;
    my $cgi = cgi;
    
    # if this module is not concerned, let's return ASAP
    my $config = $class->config;
    my $http_name = $config->{http_name};
    my $q_http_name = quotemeta ($http_name);
    my $raw_path_info = raw_path_info;
    return unless ( ($raw_path_info =~ /.*\/$q_http_name$/o) );
    
    # otherwise, let's process the request
    $raw_path_info =~ s/$q_http_name$//;
    
    # grabs the current document we're working on. If the document doesn't
    # exist, let's return (which should trigger the flo::plugin::Error module)
    my $document_table = table ('Document');
    my $document = $document_table->get ( Full_Path => $raw_path_info ) or return;
    
    # retrieve the domains to process
    my @domains_param   = $cgi->param ('d');
    my @domains_clean   = ();
    
    # this parameter will be set to 1 if any of the domains passed
    # as a parameter had to be turned into different parameters
    # if so, then we'll need to redirect
    my $screwed = 0;
    while (my $domain = shift @domains_param)
    {
	# if domain is separated by spaces, then let's$
	# split and continue
	if ($domain =~ /\s/)
	{
	    $screwed = 1;
	    push @domains_param, split /\s/, $domain;
	    next;
	}
	
	# if domain is not all lower case, then let's lower
	# case it
	if ($domain ne lc ($domain))
	{
	    $screwed = 1;
	    $domain  = lc ($domain);
	}
	
	# remove potential unwanted dots
	my ($domain, $extension) = $domain =~ /^(.*?)(?:\.(com|net|org|co\.uk|org\.uk|info))?$/;
	if ($domain =~ /\./)
	{
	    $screwed = 1;
	    $domain =~ s/^.*\.//;
	}
	
	# if the extension is not defined, we need
	# to process all TLDs
	unless (defined $extension)
	{
	    $screwed = 1;
	    push @domains_param, "$domain.com";
	    push @domains_param, "$domain.net";
	    push @domains_param, "$domain.org";
	    push @domains_param, "$domain.co.uk";
	    push @domains_param, "$domain.org.uk";
	    push @domains_param, "$domain.info";
	    next;
	}

	push @domains_clean, "$domain.$extension";
    }
    
    if ($screwed)
    {
	my $cgix = $cgi->new;
	$cgix->path_info ($cgi->path_info);
	for ($cgix->param) { $cgix->delete ($_) }
	$cgix->param ( -name=> 'd', -value=> \@domains_clean );
	print $cgix->redirect ($cgix->self_url);
    }
    else
    {
	my @res = ();
	foreach my $domain (@domains_clean)
	{
	    my $info  = whois ($domain);
	    my $available = ($info =~ /no match|not found/i) ? 1 : 0;
	    
	    my @lines = split /\n|\r/, $info;

	    my @info = ();
	    foreach my $line (@lines)
	    {
		chomp ($line);
		chomp ($line);
		$line =~ s/^\s+//;
		if ($line =~ /^[A-Za-z -]+:/) { push @info, $line }
	    }
	    $info = join "\n", @info;
	    
	    push @res, {
		info => $info,
		available => $available,
		domain => $domain,
	    }
	}

	my $hash = {};
	$hash->{Document} = $document;
	$hash->{Results}  = \@res;
	
      flo::HTTP::Header->out
	  ( new flo::Template ( file => template ('whois', $document->{Lang} ),
				hash => $hash ) );
    }
    return 'TERMINATE';
}


1;










