#!/usr/bin/perl
use warnings;
use strict;
use Asterisk::AGI;
use DBI;
my $AGI = Asterisk::AGI->new;
my $dbh = DBI->connect('dbi:mysql:asterisk', 'foo', 'bar');
open my $fh, '>', '/tmp/callerid-test.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{channel} =~ /^zap/i ) {
# We are operating on a zap channel
( my $channel ) = ( $input{channel} =~ /^(zap\/[\d]+)/i );
print $fh "Our channel: $channel\n";
my $sth = $dbh->prepare('SELECT e.Extention AS extension, CONCAT(p.FIRST_NAME," ",p.SECOND_NAME) AS employee FROM extentions e LEFT JOIN employee p ON e.Extention = p.EXTENTION WHERE e.Channel = ?');
$sth->execute( $channel );
my $row = $sth->fetchrow_hashref;
$sth->finish;
if ( !$row->{extension} ) {
print $fh "ERROR: Channel '$channel' not defined in database!\n";
}
else {
print $fh "FOUND: Channel '$channel' is extension '$row->{extension}' in database!\n";
}
if ( !$row->{employee} ) {
print $fh "ERROR: Channel '$channel' wont cross-reference to employee in database!\n";
}
else {
print $fh "FOUND: Channel '$channel' is employee '$row->{employee}' in database!\n";
}
if ( $row->{extension} && $row->{employee} ) {
#$AGI->exec('SET','CALLERID','"'. $row->{employee} .'" <'. $row->{extension} .'>');
#$AGI->set_variable('CALLERID',$row->{employee}.' <'.$row->{extension}.'>');
print "SET CALLERID \"$row->{employee}\"<$row->{extension}>";
}
}
elsif ( $input{channel} =~ /^sip/i ) {
# We are operating on a zap channel
( my $channel ) = ( $input{channel} =~ /^(sip\/[\d]+)/i );
print $fh "Our channel: $channel\n";
my $sth = $dbh->prepare('SELECT e.Extention AS extension, CONCAT(p.FIRST_NAME," ",p.SECOND_NAME) AS employee FROM extentions e LEFT JOIN employee p ON e.Extention = p.EXTENTION WHERE e.Channel = ?');
$sth->execute( $channel );
my $row = $sth->fetchrow_hashref;
$sth->finish;
if ( !$row->{extension} ) {
print $fh "ERROR: SIP station '$channel' not defined in database!\n";
}
else {
print $fh "FOUND: SIP station '$channel' is extension '$row->{extension}' in database!\n";
}
if ( !$row->{employee} ) {
print $fh "ERROR: SIP station '$channel' wont cross-reference to employee in database!\n";
}
else {
print $fh "FOUND: SIP station '$channel' is employee '$row->{employee}' in database!\n";
}
if ( $row->{extension} && $row->{employee} ) {
print "SET CALLERID \"$row->{employee}\"<$row->{extension}>";
}
}
close $fh;
exit;