#!/usr/bin/perl -w #This perl script was developed by Tom Vile at Baldwin Technology Solutions, it was first #posted on nerd vittles: http://nerdvittles.com/index.php?p=82 # use Asterisk::AGI; use LWP::UserAgent; use DBI; $AGI = new Asterisk::AGI; $Fonetastic = '1' ; $AnyWho = '1' ; $Google = '1' ; $Asteridex = '0' ; my %input = $AGI->ReadParse(); my $callerid = $input{'calleridnum'}; my $calleridfull = $input{'callerid'}; if($callerid eq ''){ $callerid=$input{'callerid'}; } if(substr($callerid,0,1) eq '1'){ $callerid=substr($callerid,1); } if(substr($callerid,0,2) eq '+1'){ $callerid=substr($callerid,2); } $calleridfull =~ s/[\,\"\']+/ /g; $AGI->verbose("CALLERID IS: $calleridfull\n",1); if ($callerid =~ /^(\d{3})(\d{3})(\d{4})$/) { $npa = $1; $nxx = $2; $station = $3; $AGI->verbose("Checking $npa $nxx $station...\n",1); } elsif($callerid=~/\<(\d{3})(\d{3})(\d{4})\>/){ $npa = $1; $nxx = $2; $station = $3; $AGI->verbose("Checking $npa $nxx $station...\n",1); } else { $AGI->verbose("Unable to parse phone number for NPA/NXX/station. Phone number is: $callerid\n",1); exit(0); } #$npa='641'; #$nxx='892'; #$station='8019'; if ($Fonetastic > '0') { $AGI->verbose("Ready for Fonetastic.US lookup... \n",1); if ($name = &fonetastic_lookup ($npa, $nxx)) { $newcallerid = "\"$name <$npa$nxx$station>\""; $AGI->set_callerid($newcallerid); $AGI->verbose("Fonetastic.US match. New CallerIDName = $name\n",1); } else{ $AGI->verbose("Unable to find a Fonetastic.US match.\n",1); } }else{ $AGI->verbose("Fonetastic.US lookup disabled.\n",1); } if ($AnyWho > '0') { $AGI->verbose("Ready for AnyWho lookup... \n",1); if ($name = &anywho_lookup ($npa, $nxx, $station)) { $newcallerid = "\"$name <$npa$nxx$station>\""; $AGI->set_callerid($newcallerid); $AGI->verbose("AnyWho match. New CallerIDName = $name\n",1); } else{ $AGI->verbose("Unable to find an AnyWho lookup.\n",1); } }else{ $AGI->verbose("AnyWho lookup disabled.\n",1); } if ($Google > '0') { $AGI->verbose("Ready for Google lookup... \n",1); if ($name = &google_lookup ($npa, $nxx, $station)) { $newcallerid = "\"$name <$npa$nxx$station>\""; $AGI->set_callerid($newcallerid); $AGI->verbose("Google match. New CallerIDName = $name\n",1); } else{ $AGI->verbose("Unable to find a Google Phonebook match.\n",1); } }else{ $AGI->verbose("Google lookup disabled.\n",1); } if ($Asteridex > '0') { $AGI->verbose("Ready for AsteriDex lookup... \n",1); if ($name = &asteridex_lookup ($npa, $nxx, $station)) { $newcallerid = "\"$name <$npa$nxx$station>\""; $AGI->set_callerid($newcallerid); $AGI->verbose("AsteriDex match. New CallerIDName = $name\n",1); } else{ $AGI->verbose("Unable to find an AsteriDex match.\n",1); } }else{ $AGI->verbose("Asteridex lookup disabled.\n",1); } exit(0); sub fonefinder_lookup { my ($npa, $nxx) = @_; my $ua = LWP::UserAgent->new( timeout => 45); my $URL = 'http://www.fonefinder.net/findome.php?npa=' . $npa . '&nxx=' . $nxx . '&thoublock=&usaquerytype=Search+by+Number&cityname='; $ua->agent('Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)'); my $req = new HTTP::Request GET => $URL; my $res = $ua->request($req); if ($res->is_success()) { if ($res->content =~ /areacode=(.+)H3>/) { my $listing = substr($1,22); #print $listing ; if ($listing =~ />(.+)npamap/) { my $clidcity = substr($1,0,-20); #print $clidcity ; if ($listing =~ /(.+)census/) { my $clidprovider = $1; #print $clidprovider ; if ($clidprovider =~ /(.+)www/) { my $clidprovider = $1; #print $clidprovider ; if ($clidprovider =~ /(.*?)/) { my $clidprovider = $1; #print $clidprovider ; my $clidname = $clidcity . ' ' . $clidprovider ; return $clidname; } } } } } } return ""; } sub anywho_lookup { my ($npa, $nxx, $station) = @_; my $ua = LWP::UserAgent->new( timeout => 45); my $URL = 'http://www.anywho.com/qry/wp_rl'; $URL .= '?npa=' . $npa . '&telephone=' . $nxx . $station; $ua->agent('AsteriskAGIQuery/1'); my $req = new HTTP::Request GET => $URL; my $res = $ua->request($req); if ($res->is_success()) { if ($res->content =~ /(.*)/s) { my $listing = $1; if ($listing =~ /(.*)<\/B>/) { my $clidname = $1; return $clidname; } } } return ""; } sub google_lookup { my ($npa, $nxx, $station) = @_; my $ua = LWP::UserAgent->new( timeout => 45); my $URL = 'http://www.google.com/search?rls=en&q=phonebook:' . $npa . $nxx . $station . '&ie=UTF-8&oe=UTF-8'; $ua->agent('AsteriskAGIQuery/1'); my $req = new HTTP::Request GET => $URL; my $res = $ua->request($req); if ($res->is_success()) { if ($res->content =~ /
<\/font>(.+)/) { my $temp = $1; my $cidname = ""; if ( $temp =~ /(.+)/o ) { $clidname = substr($1, 0, -3); } else { $clidname = substr($temp, 0, -3); } if ($clidname =~ /(.+)connect("dbi:mysql:asteridex","root","passw0rd") or die("Connect failed"); my $sth = $dbh->prepare("select * from user1 where out = '$npa$nxx$station'") or die("Prepare failed."); $sth->execute; if($sth->rows == 0) { return ""; } else { my $resptr = $sth->fetchrow_hashref(); my $clidname = $resptr->{"name"}; return $clidname; } $dbh->disconnect; return ""; } sub npanxx_lookup { my ($npa, $nxx) = @_; my $dbh = DBI->connect("dbi:mysql:phone","root","passw0rd") or die("Connect failed"); my $lookup = $npa . "-" . $nxx ; #print $lookup ; my $sth = $dbh->prepare("select * from npanxx where npanxx = '$lookup'") or die("Prepare failed."); $sth->execute; #print $sth->rows ; if($sth->rows == 0) { return ""; } else { my $resptr = $sth->fetchrow_hashref(); my $ratecenter = $resptr->{"ratecenter"}; my $state = $resptr->{"state"}; my $company = $resptr->{"company"}; $company = substr($company,0,-1) ; $company = substr($company,1,9) ; my $clidname = substr($ratecenter, 0, 3) . "-" . $state . "-" . $company ; return $clidname; } $dbh->disconnect; return ""; } sub fonetastic_lookup { my ($npa, $nxx) = @_; my $ua = LWP::UserAgent->new( timeout => 45); my $URL = 'http://fonetastic.us/npanxx.php?NPANXX=' . $npa . '-' . $nxx ; $ua->agent('Nerd Vittles AsteriDex 1.3'); my $req = new HTTP::Request GET => $URL; my $res = $ua->request($req); if ($res->is_success()) { my $clidname = $res->content ; return $clidname; } else { return ""; } return "" ; }