#!/usr/bin/perl
use strict;
$| = 1;
my $addrbook = '/etc/asterisk/addrbook.dat';
open( DEBUG, "> /tmp/InboundCallerID.txt" );
select DEBUG; $| = 1; select STDOUT;
# Setup some variables
my %AGI;
my( $err, $status, $data );
#print DEBUG " ==== start\n";
while(<STDIN>) {
chomp;
if ( !length ) {
last;
}
last unless length($_);
if (/^agi_(\w+)\:\s+(.*)$/) {
$AGI{$1} = $2;
#
print DEBUG "$AGI{$1} = $2\n";
}
}
my $inbound_Number = $AGI{'callerid'};
print DEBUG "Original Number: $inbound_Number\n";
my $original_Number = $inbound_Number;
if (length($inbound_Number) >= 5) {
# if the number if long enough remove the first few chars..
#this helps with number ID where some numbers might be internationlized and other not.
# eg +1-408-555-1234 or 408-555-1234
$inbound_Number = substr($inbound_Number,3);
}
print DEBUG "Before Number: $inbound_Number\n";
my $name = `grep $inbound_Number $addrbook |awk -F: '{print \$1}'`;
chomp($name);
print DEBUG "Name: $name...$original_Number..\n";
if (length($name) > 1 ) { ( $err, $status, $data ) = cmd( "set CALLERID \"$name <$original_Number>\"" );}
exit 0;
sub cmd {
my( $cmd ) = @_;
my $result;
my $status;
my $extended;
print $cmd, "\n";
$result = <STDIN>;
chomp( $result );
if ( $result =~ s/^200\s+result=(\d+)\s*(\((.*)\))?$// ) {
( $status, $extended ) = ( $1, $3 );
( undef, $status, $extended );
}
else {
( "Unexpected result '$result'" );
}
}