#!/usr/bin/perl # # Use Reverse Number Database on the net to add name to callerid info # Currently only trys to query anywho.com, but other lookups can be added easily # # If your callerid is only passed as a 7 digit number, # you can pass this script an argument with the default area code # # extensions.conf example: # exten => s,1,AGI,calleridnamelookup.agi # exten => s,2,Dial,Zap,1 # # Originally Written by: James Golovich # Revamped by: Roger Gulbranson use Asterisk::AGI; use LWP::UserAgent; use DBI; # connect to mysql $dbh = DBI->connect ("dbi:mysql:asterisk:HOSTNAME", 'USER', 'PWD', {AutoCommit => 1, PrintError => 0, RaiseError => 0}); exit(0) unless ($dbh); # TIMEOUT is maximum number of seconds for http requests # (we don't want to take too long) $TIMEOUT = 2; $VERSION = '0.02'; $AGI = new Asterisk::AGI; my %input = $AGI->ReadParse(); my $callerid = $input{'callerid'}; my $calleridname = $input{'calleridname'}; my $channel = $input{'channel'}; &call_history ($channel, $callerid); #Exit quickly if there is already a name if (length($calleridname)) { buildcache ($callerid, $calleridname); exit(0); } $defaultnpa = $ARGV[0] if (defined($ARGV[0])); if ($callerid =~ /^(\d{3})(\d{3})(\d{4})$/) { $npa = $1; $nxx = $2; $station = $3; } elsif (defined($defaultnpa) && ($callerid =~ /^(\d{3})(\d{4})$/)) { $npa = $defaultnpa; $nxx = $1; $station = $2; } else { exit(0); } if ($name = &lookup ($npa, $nxx, $station)) { $newcallerid = "\"$name <$npa$nxx$station>\""; $AGI->set_callerid($newcallerid); } exit(0); sub lookup { my $name = ''; if ($name = cache_lookup(@_)) { return $name; } elsif ($name = anywho_lookup(@_)) {} &cacheadd (@_, $name); return $name; } sub buildcache { my ($cid, $name) = @_; return unless ($name && $cid); if ($cid =~ m/^(\d\d\d)(\d\d\d)(\d\d\d\d)$/) { my $npa = $1; my $nxx = $2; my $station = $3; $name =~ s/"//g; $name =~ s/^\s+//; $name =~ s/\s+$//; # we don't replace any existing entries my $c = $dbh->prepare ("INSERT INTO asterisk_callerid_name_lookup (callerid,callerid_name) VALUES (?,?)"); $c->execute ($cid, $name); } } sub cache_lookup { my ($npa, $nxx, $station) = @_; my $c = $dbh->prepare ("SELECT callerid_name FROM asterisk_callerid_name_lookup WHERE callerid=?"); $c->execute ($npa . $nxx . $station); my @f; my $retv; while (@f = $c->fetchrow) {$retv = $f[0];} $c->finish; return $retv; } sub anywho_lookup { my ($npa, $nxx, $station) = @_; my $ua = LWP::UserAgent->new( timeout => $TIMEOUT); my $URL = 'http://www.anywho.com/qry/wp_rl'; $URL .= '?npa=' . $npa . '&telephone=' . $nxx . $station; $ua->agent('AsteriskAGIQuery/$VERSION'); 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 call_history { my ($channel, $callerid) = @_; return unless ($channel && $callerid); my $chan; my @chan = split (/\//, $channel); $chan[0] = uc ($chan[0]); if ($chan[0] eq 'ZAP') { $chan[1] =~ s/\-.+//; $chan = join ('/', @chan); } elsif ($chan[0] eq 'IAX2') { $chan = join ('/', $chan[0], $chan[2]); } elsif ($chan[0] eq 'SIP') { $chan[1] =~ s/\-.+//; $chan = join ('/', @chan); } &do ("INSERT INTO asterisk_callerid_history (timestamp,callerid,channel) VALUES (now(),?,?)", $callerid, $chan); } sub do { my $sql = shift; my $c = $dbh->prepare ($sql); $c->execute (@_); }