#!/usr/bin/perl use strict; use Asterisk::AGI; use LWP::UserAgent; use DBI; #### Your Reverse Australia Developers Key: #### http://www.reverseaustralia.com/developer/ my $APIKEY = ""; my $query_online = 1; ## MySQL Database details. my $db_host = "localhost"; my $db_name = "asterisk"; my $db_username = "username"; my $db_password = "password"; ## If you access the web via a proxy, you can define it here #$ in the format "http://my.proxy.com:8080" my $http_proxy = ""; my $AGI = new Asterisk::AGI; my %input = $AGI->ReadParse(); my $number = $input{'callerid'}; ## Open the SQL database and search for the number calling... my $dbh = DBI->connect("dbi:mysql:$db_name;host=$db_host", $db_username, $db_password) or die "Error connecting to database!\n"; my $sth = $dbh->prepare("SELECT name FROM cid WHERE number=?"); $sth->execute($number) or die "SQL Error: $DBI::errstr\n"; my $name = $sth->fetchrow; ## If we still can't find anything, search the online database if enabled. if ( $name eq "" and $name ne "anonymous" ) { if ( $query_online and $APIKEY ne "" ) { my $ua = new LWP::UserAgent; $ua->proxy("http", $http_proxy); $ua->agent("cid-lookup-v4.agi"); my $req = new HTTP::Request GET => 'http://api.reverseaustralia.com/cidlookup.php?format=text&key='.$APIKEY.'&q='.$number.'&extended=0'; my $res = $ua->request($req); if ($res->is_success) { ## We don't need any real parsing on non-extended searches. ## Set the caller ID and add the results to the database as a cache. $name = $res->content; $name =~ s/\t//g; $name =~ s/,//g; $sth = $dbh->prepare("INSERT INTO cid (name,number) VALUES (?,?)"); $sth->execute($name, $number); } else { print("NOOP Error looking up online directory"); } } ## If we got nothing from the database, and nothing from the online directory ## (if enabled) we fall back to this. Add the details to the database so someone ## can come along and add a name later. if ( $name eq "" or $name eq "Not found") { print("NOOP Nothing found. Setting to unknown\n"); $name = "Unknown Caller"; $sth = $dbh->prepare("INSERT INTO cid (name,number) VALUES (?,?)"); $sth->execute($number, $number); } } $AGI->set_callerid("\"$name <$number>\"");