#!/usr/bin/perl -w
use DBI;
my $bdd = DBI->connect("dbi:Pg:dbname=web;","tfe","monger") or die "error connecting";




Xchat::register('Grabber by tfe','0.1','','');
Xchat::hook_server(PRIVMSG,grabber);

sub grabber($)
{
    $message = shift;
    
    $action =${$message}[1];
    $from =${$message}[0];
    $channel = ${$message}[2];
    $message = join(" ",@{$message}[3..$#$message]);
    if ($from =~ /:([^\!]+)\!.*/) { $nick = $1; }
    $server = Xchat::get_info('network');






    if ($message =~
	/
	    (?:\W|^)
	    (
	     ((?:https?)|(?:ftp))		
	      :\/\/				
	              (
		      (?:
		       [a-z\.\-\_0-9]+ 	
			\.			
		      )?
    	    	        (
			    [a-z\-\_0-9]+?
		    	    \.

		        [a-z]{2,4}		
			)
		      )
		        (?::(\d+))?		
		    
		(
	        (?:\/|\?)
	        [a-z\/\?\(\)\~\;0-9\.\-\_\%\&\=]+
	        )?
	  )
	/ix
  
  )
    {

$url  = $1 ? $1 : "";
$protocol  = $2 ? $2 : "";
$fhost  = $3 ? $3 : "";
$mhost  = $4 ? $4 : "";
$port  = $5 ? $5 : "";


# $1 = url    
# $2 = protocol
# $3 = full host
# $4 = main host
# $5 = port



$sth = $bdd->prepare("SELECT * FROM url WHERE url='$url'");
$sth->execute();
$compteur=0;
while($row = $sth->fetchrow_hashref)
{
    $compteur= 1;
}
if ($compteur ==0)
{
$sth = $bdd->prepare("INSERT INTO url
    (protocol,pseudo,channel,server,host,vhost,url,port)
    VALUES
    ('$protocol','$nick','$channel','$server','$mhost','$fhost','$url','$port')
    ") or die $bdd->errstr;
$sth->execute() or  die $bdd->errstr;
}




}












$_ = $message;










return $_;
}
