#!/usr/bin/perl
use warnings;
use strict;
use DBI;
use Asterisk::AGI;
my $AGI = Asterisk::AGI->new;
my $dbh = DBI->connect(
'dbi:mysql:phonestuff',
'phonestuff',
'phonestuff',
{
host => 'db',
port => 3306
}
) or die "Failed to connect to database: $DBI::errstr\n";
open my $fh, '>', '/tmp/aclookup.out';
for my $key ( keys %ENV ) {
print $fh "ENV{$key} => $ENV{$key}\n";
}
for my $arg ( @ARGV ) {
print $fh "ARG: $arg\n";
}
my %input = $AGI->ReadParse;
for my $key ( keys %input ) {
print $fh "INPUT{$key} => $input{$key}\n";
}
if ( $input{dnid} && length $input{dnid} == 10 ) {
print $fh "Found 10-digit number as dnid, parsing for area code\n";
my ( $actest ) = ( $input{dnid} =~ /^(\d{3})/ );
print $fh "Looking for area code '$actest'\n";
my $sth = $dbh->prepare( 'SELECT city,state,country FROM areacodes WHERE ac = ?' );
$sth->execute( $actest );
if ( $sth->rows > 0 ) {
print $fh "Found entry for area code '$actest'\n";
my $row = $sth->fetchrow_hashref;
print "VERBOSE Call is destined for '$row->{city}', '$row->{state}', '$row->{country}'\n";
print $agi->exec("Verbose", "Call is destined for '$row->{city}', '$row->{state}', '$row->{country}'\n");
print $fh "Call is destined for '$row->{city}', '$row->{state}', '$row->{country}'\n";
}
else {
print $fh "Did not find an entry matching '$actest'\n";
}
$sth->finish;
}
else {
print $fh "Not a 10-digit number as dnid, skipping\n";
}
$dbh->disconnect;
exit;