Created stable release branch of 2.11

git-svn-id: svn://192.168.202.10@2292 3d104415-ff17-0410-8863-d5cf3c621b8a
This commit is contained in:
mattf
2015-03-02 14:35:39 +00:00
parent 261ea57108
commit fffcb49a80
1769 changed files with 653200 additions and 0 deletions
@@ -0,0 +1,411 @@
#!/usr/bin/perl
#
# VDL_CID_lookup.agi version 2.6
#
# runs after a call goes through AMD in extensions.conf to send the call on to
# it's proper destination
#
# You need to put lines similar to those below in your extensions.conf file:
#
# ; VICIDIAL_auto_dialer transfer script AMD with Load Balanced:
# exten => 8369,1,Ringing
# exten => 8369,2,Wait(1)
# exten => 8369,3,Answer
# exten => 8369,4,AGI(VDL_CID_lookup.agi)
# exten => 8369,5,AGI(agi-VDAD_ALL_inbound.agi,CID-----LB-----IN_TEST-----7275551212-----Closer-----park----------999-----1)
# exten => 8369,6,Hangup
# exten => 8369,7,AGI(call_logCID.agi,${EXTEN}-----${CALLERID}-----${CALLERIDNUM}-----${CALLERIDNAME})
# exten => 8369,8,AGI(agi-VDADinbound_NI_DNC_CIDlookup.agi)
# exten => 8369,9,Hangup
#
#
# Copyright (C) 2013 Matt Florell <vicidial@gmail.com> LICENSE: GPLv2
#
# changes:
# 80930-0226 - first build
# 81015-1702 - added filter stats
# 130108-1836 - Changes for Asterisk 1.8 compatibility
#
$filter_stats = 1;
$script = 'VDL_CID_lookup.agi';
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year = ($year + 1900);
$mon++;
if ($mon < 10) {$mon = "0$mon";}
if ($mday < 10) {$mday = "0$mday";}
if ($hour < 10) {$hour = "0$hour";}
if ($min < 10) {$min = "0$min";}
if ($sec < 10) {$sec = "0$sec";}
$now_date_epoch = time();
$now_date = "$year-$mon-$mday $hour:$min:$sec";
$SQLdate = "$year-$mon-$mday $hour:$min:$sec";
# default path to astguiclient configuration file:
$PATHconf = '/etc/astguiclient.conf';
open(conf, "$PATHconf") || die "can't open $PATHconf: $!\n";
@conf = <conf>;
close(conf);
$i=0;
foreach(@conf)
{
$line = $conf[$i];
$line =~ s/ |>|\n|\r|\t|\#.*|;.*//gi;
if ( ($line =~ /^PATHhome/) && ($CLIhome < 1) )
{$PATHhome = $line; $PATHhome =~ s/.*=//gi;}
if ( ($line =~ /^PATHlogs/) && ($CLIlogs < 1) )
{$PATHlogs = $line; $PATHlogs =~ s/.*=//gi;}
if ( ($line =~ /^PATHagi/) && ($CLIagi < 1) )
{$PATHagi = $line; $PATHagi =~ s/.*=//gi;}
if ( ($line =~ /^PATHweb/) && ($CLIweb < 1) )
{$PATHweb = $line; $PATHweb =~ s/.*=//gi;}
if ( ($line =~ /^PATHsounds/) && ($CLIsounds < 1) )
{$PATHsounds = $line; $PATHsounds =~ s/.*=//gi;}
if ( ($line =~ /^PATHmonitor/) && ($CLImonitor < 1) )
{$PATHmonitor = $line; $PATHmonitor =~ s/.*=//gi;}
if ( ($line =~ /^VARserver_ip/) && ($CLIserver_ip < 1) )
{$VARserver_ip = $line; $VARserver_ip =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_server/) && ($CLIDB_server < 1) )
{$VARDB_server = $line; $VARDB_server =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_database/) && ($CLIDB_database < 1) )
{$VARDB_database = $line; $VARDB_database =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_user/) && ($CLIDB_user < 1) )
{$VARDB_user = $line; $VARDB_user =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_pass/) && ($CLIDB_pass < 1) )
{$VARDB_pass = $line; $VARDB_pass =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_port/) && ($CLIDB_port < 1) )
{$VARDB_port = $line; $VARDB_port =~ s/.*=//gi;}
$i++;
}
if (!$VARDB_port) {$VARDB_port='3306';}
if (!$AGILOGfile) {$AGILOGfile = "$PATHlogs/agiout.$year-$mon-$mday";}
use DBI;
use Asterisk::AGI;
$AGI = new Asterisk::AGI;
$dbhA = DBI->connect("DBI:mysql:$VARDB_database:$VARDB_server:$VARDB_port", "$VARDB_user", "$VARDB_pass")
or die "Couldn't connect to database: " . DBI->errstr;
### Grab Server values from the database
$stmtA = "SELECT agi_output FROM servers where server_ip = '$VARserver_ip';";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
$rec_count=0;
while ($sthArows > $rec_count)
{
$AGILOG = '0';
@aryA = $sthA->fetchrow_array;
$DBagi_output = "$aryA[0]";
if ($DBagi_output =~ /STDERR/) {$AGILOG = '1';}
if ($DBagi_output =~ /FILE/) {$AGILOG = '2';}
if ($DBagi_output =~ /BOTH/) {$AGILOG = '3';}
$rec_count++;
}
$sthA->finish();
### begin parsing run-time options ###
if (length($ARGV[0])>1)
{
if ($AGILOG) {$agi_string = "Perl Environment Dump:"; &agi_output;}
$i=0;
while ($#ARGV >= $i)
{
$args = "$args $ARGV[$i]";
if ($AGILOG) {$agi_string = "$i|$ARGV[$i]"; &agi_output;}
$i++;
}
if ($args =~ /--help/i)
{
print "allowed run time options:\n [-q] = quiet\n [-t] = test\n [-debug] = verbose debug messages\n\n";
}
else
{
if ($args =~ /-V/i)
{
$V=1;
}
if ($args =~ /-debug/i)
{
$DG=1;
}
if ($args =~ /-dbAVS/i)
{
$DGA=1;
}
if ($args =~ /-q/i)
{
$q=1;
$Q=1;
}
if ($args =~ /-t/i)
{
$TEST=1;
$T=1;
}
}
}
$|=1;
while(<STDIN>)
{
chomp;
last unless length($_);
if ($AGILOG)
{
if (/^agi_(\w+)\:\s+(.*)$/)
{
$AGI{$1} = $2;
}
}
if (/^agi_uniqueid\:\s+(.*)$/) {$unique_id = $1; $uniqueid = $unique_id;}
if (/^agi_priority\:\s+(.*)$/) {$priority = $1;}
if (/^agi_channel\:\s+(.*)$/) {$channel = $1;}
if (/^agi_extension\:\s+(.*)$/) {$extension = $1;}
if (/^agi_type\:\s+(.*)$/) {$type = $1;}
if (/^agi_callerid\:\s+(.*)$/) {$callerid = $1; $calleridnum = $callerid;}
if (/^agi_calleridname\:\s+(.*)$/) {$calleridname = $1;}
}
if ( (length($callerid)>20) && ($callerid =~ /\"\S\S\S\S\S\S\S\S\S\S\S\S\S\S\S\S\S\S/) )
{
$callerid =~ s/^\"//gi;
$callerid =~ s/\".*$//gi;
}
if ( (
(length($calleridname)>5) && ( (!$callerid) or ($callerid =~ /unknown|private|00000000/i) or ($callerid =~ /5551212/) )
) or ( (length($calleridname)>17) && ($calleridname =~ /\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d/) ) )
{
$callerid = $calleridname;
}
if ($AGILOG) {$agi_string = "AGI Environment Dump:"; &agi_output;}
foreach $i (sort keys %AGI)
{
if ($AGILOG) {$agi_string = " -- $i = $AGI{$i}"; &agi_output;}
}
if ($AGILOG) {$agi_string = "AGI Variables: |$unique_id|$channel|$extension|$type|$callerid|BUS: $business_hours|"; &agi_output;}
$callerid =~ s/\"//gi;
if ($filter_stats > 0)
{
### find out if phone number exists in the vicidial_list table
$stmtA = "SELECT count(*) FROM vicidial_list where phone_number = '$callerid';";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
if ($sthArows > 0)
{
@aryA = $sthA->fetchrow_array;
$VDL_count = "$aryA[0]";
}
$sthA->finish();
if ($VDL_count < 1)
{
$priority = ($priority + 3);
$stmtA = "INSERT INTO live_inbound_log (uniqueid,channel,server_ip,caller_id,extension,start_time,comment_a,comment_b) values('$uniqueid','$channel','$VARserver_ip','$callerid','$extension','$SQLdate','INBOUND_IVR_FILTER','NOT_FOUND')";
if ($AGILOG) {$agi_string = "|$stmtA|"; &agi_output;}
$affected_rows = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "-- cid_lookup finish: : |$affected_rows|$callerid|$priority|NOT_FOUND|"; &agi_output;}
print "SET PRIORITY $priority\n";
$result = <STDIN>;
checkresult($result);
$dbhA->disconnect();
exit;
}
else ################# LEAD IN SYSTEM, FILTER FURTHER #########################
{
$stmtA = "SELECT count(*) FROM vicidial_list where phone_number = '$callerid' and status IN('A1','A2','A3','A4','SALE');";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
if ($sthArows > 0)
{
@aryA = $sthA->fetchrow_array;
$sales_count = "$aryA[0]";
}
$sthA->finish();
if ($sales_count > 0)
{
$priority = ($priority + 3);
$stmtA = "INSERT INTO live_inbound_log (uniqueid,channel,server_ip,caller_id,extension,start_time,comment_a,comment_b,comment_c) values('$uniqueid','$channel','$VARserver_ip','$callerid','$extension','$SQLdate','INBOUND_IVR_FILTER','EXISTING','SALE')";
if ($AGILOG) {$agi_string = "|$stmtA|"; &agi_output;}
$affected_rows = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "-- cid_lookup finish: : |$affected_rows|$callerid|$priority|EXISTING|SALE|"; &agi_output;}
print "SET PRIORITY $priority\n";
$result = <STDIN>;
checkresult($result);
$dbhA->disconnect();
exit;
}
$stmtA = "SELECT count(*) FROM vicidial_list where phone_number = '$callerid' and status IN('DNC');";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
if ($sthArows > 0)
{
@aryA = $sthA->fetchrow_array;
$dnc_count = "$aryA[0]";
}
$sthA->finish();
if ($dnc_count > 0)
{
$priority = ($priority + 3);
$stmtA = "INSERT INTO live_inbound_log (uniqueid,channel,server_ip,caller_id,extension,start_time,comment_a,comment_b,comment_c) values('$uniqueid','$channel','$VARserver_ip','$callerid','$extension','$SQLdate','INBOUND_IVR_FILTER','EXISTING','DNC')";
if ($AGILOG) {$agi_string = "|$stmtA|"; &agi_output;}
$affected_rows = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "-- cid_lookup finish: : |$affected_rows|$callerid|$priority|EXISTING|DNC|"; &agi_output;}
print "SET PRIORITY $priority\n";
$result = <STDIN>;
checkresult($result);
$dbhA->disconnect();
exit;
}
$stmtA = "SELECT count(*) FROM vicidial_list where phone_number = '$callerid' and list_id NOT IN('999999');";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
if ($sthArows > 0)
{
@aryA = $sthA->fetchrow_array;
$archive_count = "$aryA[0]";
}
$sthA->finish();
if ($archive_count < 1)
{
$priority = ($priority + 3);
$stmtA = "INSERT INTO live_inbound_log (uniqueid,channel,server_ip,caller_id,extension,start_time,comment_a,comment_b,comment_c) values('$uniqueid','$channel','$VARserver_ip','$callerid','$extension','$SQLdate','INBOUND_IVR_FILTER','EXISTING','ARCHIVE')";
if ($AGILOG) {$agi_string = "|$stmtA|"; &agi_output;}
$affected_rows = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "-- cid_lookup finish: : |$affected_rows|$callerid|$priority|EXISTING|ARCHIVE|"; &agi_output;}
print "SET PRIORITY $priority\n";
$result = <STDIN>;
checkresult($result);
$dbhA->disconnect();
exit;
}
$stmtA = "INSERT INTO live_inbound_log (uniqueid,channel,server_ip,caller_id,extension,start_time,comment_a,comment_b,comment_c) values('$uniqueid','$channel','$VARserver_ip','$callerid','$extension','$SQLdate','INBOUND_IVR_FILTER','CLEAN','TO_AGENT')";
if ($AGILOG) {$agi_string = "|$stmtA|"; &agi_output;}
$affected_rows = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "-- cid_lookup finish: : |$affected_rows|$callerid|$priority|EXISTING|CLEAN|"; &agi_output;}
exit;
}
}
else
{
### find out if phone number exists in the vicidial_list table
$stmtA = "SELECT count(*) FROM vicidial_list where phone_number = '$callerid' and list_id NOT IN('999999');";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
if ($sthArows > 0)
{
@aryA = $sthA->fetchrow_array;
$VDL_count = "$aryA[0]";
}
$sthA->finish();
if ($VDL_count < 1)
{
$priority = ($priority + 3);
if ($AGILOG) {$agi_string = "-- cid_lookup finish: |$priority|$callerid"; &agi_output;}
print "SET PRIORITY $priority\n";
$result = <STDIN>;
checkresult($result);
}
}
$dbhA->disconnect();
exit;
sub checkresult {
my ($res) = @_;
my $retval;
$tests++;
chomp $res;
if ($res =~ /^200/) {
$res =~ /result=(-?\d+)/;
if (!length($1)) {
# print STDERR "FAIL ($res)\n";
$fail++;
} else {
# print STDERR "PASS ($1)\n";
$pass++;
}
} else {
# print STDERR "FAIL (unexpected result '$res')\n";
$fail++;
}
}
sub agi_output
{
if ($AGILOG >=2)
{
### open the log file for writing ###
open(Lout, ">>$AGILOGfile")
|| die "Can't open $AGILOGfile: $!\n";
print Lout "$now_date|$script|$agi_string\n";
close(Lout);
}
### send to STDERR writing ###
if ( ($AGILOG == '1') || ($AGILOG == '3') )
{print STDERR "$now_date|$script|$agi_string\n";}
$agi_string='';
}
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,394 @@
#!/usr/bin/perl
#
# VD_auto_post_VERIFY.agi version 2.0.5 *DBI-version*
#
# NOTE: This script is only used with auto-message campaigns with dynamic data
#
# runs after a call is answered
#
# You need to put lines similar to those below in your extensions.conf file:
#
# ; VICIDIAL_auto_dialer transfer script AMD with Load Balanced:
#exten => 8320,1,WaitForSilence(2000,2) ; wait for 2 seconds of silence
#exten => 8320,2,AGI(VD_auto_post_VERIFY.agi,${EXTEN})
#exten => 8320,3,Hangup
#
#
# Copyright (C) 2008 Matt Florell <vicidial@gmail.com> LICENSE: AGPLv2
#
# changes:
# 60915-1616 - first build
# 100903-0041 - Changed lead_id max length to 10 digits
#
$script = 'VD_auto_post_VERIFY.agi';
### files to play for message of not interested or DNC and your request has been processed
$goodbye_audio_file = 'US_reminder_goodbye';
$mc_one_file = '85100019';
$mc_two_file = '85100021';
$visa_one_file = '85100018';
$visa_two_file = '85100020';
#$A = 1; # set to 1 for AMD output messages mode
$AMD_LOG = 0; # set to 1 for logfile
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year = ($year + 1900);
$mon++;
if ($mon < 10) {$mon = "0$mon";}
if ($mday < 10) {$mday = "0$mday";}
if ($hour < 10) {$hour = "0$hour";}
if ($min < 10) {$min = "0$min";}
if ($sec < 10) {$sec = "0$sec";}
$now_date_epoch = time();
$now_date = "$year-$mon-$mday $hour:$min:$sec";
$start_time=$now_date;
$CIDdate = "$mon$mday$hour$min$sec";
$tsSQLdate = "$year$mon$mday$hour$min$sec";
$SQLdate = "$year-$mon-$mday $hour:$min:$sec";
$SQLdateBEGIN = $SQLdate;
# default path to astguiclient configuration file:
$PATHconf = '/etc/astguiclient.conf';
open(conf, "$PATHconf") || die "can't open $PATHconf: $!\n";
@conf = <conf>;
close(conf);
$i=0;
foreach(@conf)
{
$line = $conf[$i];
$line =~ s/ |>|\n|\r|\t|\#.*|;.*//gi;
if ( ($line =~ /^PATHhome/) && ($CLIhome < 1) )
{$PATHhome = $line; $PATHhome =~ s/.*=//gi;}
if ( ($line =~ /^PATHlogs/) && ($CLIlogs < 1) )
{$PATHlogs = $line; $PATHlogs =~ s/.*=//gi;}
if ( ($line =~ /^PATHagi/) && ($CLIagi < 1) )
{$PATHagi = $line; $PATHagi =~ s/.*=//gi;}
if ( ($line =~ /^PATHweb/) && ($CLIweb < 1) )
{$PATHweb = $line; $PATHweb =~ s/.*=//gi;}
if ( ($line =~ /^PATHsounds/) && ($CLIsounds < 1) )
{$PATHsounds = $line; $PATHsounds =~ s/.*=//gi;}
if ( ($line =~ /^PATHmonitor/) && ($CLImonitor < 1) )
{$PATHmonitor = $line; $PATHmonitor =~ s/.*=//gi;}
if ( ($line =~ /^VARserver_ip/) && ($CLIserver_ip < 1) )
{$VARserver_ip = $line; $VARserver_ip =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_server/) && ($CLIDB_server < 1) )
{$VARDB_server = $line; $VARDB_server =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_database/) && ($CLIDB_database < 1) )
{$VARDB_database = $line; $VARDB_database =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_user/) && ($CLIDB_user < 1) )
{$VARDB_user = $line; $VARDB_user =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_pass/) && ($CLIDB_pass < 1) )
{$VARDB_pass = $line; $VARDB_pass =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_port/) && ($CLIDB_port < 1) )
{$VARDB_port = $line; $VARDB_port =~ s/.*=//gi;}
$i++;
}
if (!$VARDB_port) {$VARDB_port='3306';}
if (!$AGILOGfile) {$AGILOGfile = "$PATHlogs/agiout.$year-$mon-$mday";}
use DBI;
use Asterisk::AGI;
$AGI = new Asterisk::AGI;
$dbhA = DBI->connect("DBI:mysql:$VARDB_database:$VARDB_server:$VARDB_port", "$VARDB_user", "$VARDB_pass")
or die "Couldn't connect to database: " . DBI->errstr;
### Grab Server values from the database
$stmtA = "SELECT agi_output FROM servers where server_ip = '$VARserver_ip';";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
$rec_count=0;
while ($sthArows > $rec_count)
{
$AGILOG = '0';
@aryA = $sthA->fetchrow_array;
$DBagi_output = "$aryA[0]";
if ($DBagi_output =~ /STDERR/) {$AGILOG = '1';}
if ($DBagi_output =~ /FILE/) {$AGILOG = '2';}
if ($DBagi_output =~ /BOTH/) {$AGILOG = '3';}
$rec_count++;
}
$sthA->finish();
### begin parsing run-time options ###
if (length($ARGV[0])>1)
{
if ($AGILOG) {$agi_string = "Perl Environment Dump:"; &agi_output;}
$i=0;
while ($#ARGV >= $i)
{
$args = "$args $ARGV[$i]";
if ($AGILOG) {$agi_string = "$i|$ARGV[$i]"; &agi_output;}
$i++;
}
if ($args =~ /--help/i)
{
print "allowed run time options:\n [-q] = quiet\n [-t] = test\n [-debug] = verbose debug messages\n\n";
}
else
{
if ($args =~ /-V/i)
{
$V=1;
}
if ($args =~ /-debug/i)
{
$DG=1;
}
if ($args =~ /-dbAVS/i)
{
$DGA=1;
}
if ($args =~ /-q/i)
{
$q=1;
$Q=1;
}
if ($args =~ /-t/i)
{
$TEST=1;
$T=1;
}
}
}
$|=1;
while(<STDIN>)
{
chomp;
last unless length($_);
if ($AGILOG)
{
if (/^agi_(\w+)\:\s+(.*)$/)
{
$AGI{$1} = $2;
}
}
if (/^agi_uniqueid\:\s+(.*)$/) {$unique_id = $1; $uniqueid = $unique_id;}
if (/^agi_priority\:\s+(.*)$/) {$priority = $1;}
if (/^agi_channel\:\s+(.*)$/) {$channel = $1;}
if (/^agi_extension\:\s+(.*)$/) {$extension = $1;}
if (/^agi_type\:\s+(.*)$/) {$type = $1;}
if (/^agi_callerid\:\s+(.*)$/) {$callerid = $1;}
if (/^agi_calleridname\:\s+(.*)$/) {$calleridname = $1;}
}
if ( (length($callerid)>20) && ($callerid =~ /\"\S\S\S\S\S\S\S\S\S\S\S\S\S\S\S\S\S\S/) )
{
$callerid =~ s/^\"//gi;
$callerid =~ s/\".*$//gi;
# ### set the callerid to the ACQS value(calleridname)
# print "SET CALLERID $callerid\n";
# checkresult($result);
# print STDERR "callerID changed: $callerid\n";
}
if ( (
(length($calleridname)>5) && ( (!$callerid) or ($callerid =~ /unknown|private|00000000/i) or ($callerid =~ /5551212/) )
) or ( (length($calleridname)>17) && ($calleridname =~ /\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d/) ) )
{
$callerid = $calleridname;
# ### set the callerid to the ACQS value(calleridname)
# print "SET CALLERID $callerid\n";
# checkresult($result);
# print STDERR "callerID changed: $callerid\n";
}
if ($AGILOG) {$agi_string = "AGI Environment Dump:"; &agi_output;}
foreach $i (sort keys %AGI)
{
if ($AGILOG) {$agi_string = " -- $i = $AGI{$i}"; &agi_output;}
}
if ($AGILOG) {$agi_string = "AGI Variables: |$unique_id|$channel|$extension|$type|$callerid|$calleridname|$priority|"; &agi_output;}
$VDADcampaign='';
$VDADphone='';
$VDADphone_code='';
$callerid =~ s/ .*//gi;
$callerid =~ s/\"//gi;
$CIDlead_id = $callerid;
$CIDlead_id = substr($CIDlead_id, 10, 10);
$CIDlead_id = ($CIDlead_id + 0);
$VD_lead_id = $CIDlead_id;
if ($AGILOG) {$agi_string = "+++++ VD amd START: |$CIDlead_id|$now_date|$AST_ver|"; &agi_output;}
if ( ($channel =~ /Local/i) && ($AST_ver !~ /^1\.0\.8|^1\.0\.9/) )
{
if ($AGILOG) {$agi_string = "+++++ VDAD START LOCAL CHANNEL: EXITING- $priority"; &agi_output;}
if ($priority > 2) {sleep(1);}
exit;
}
# Thank you for your recent purchase of ACME Sprockets, we have
# confirmed your enrollment in the
# [Verified by Visa| MasterCard SecureCode]
# program. Please go to www.iconfirm.net and enter your name and
# verification code
# [verification code]
# within the next (x amount of hours) to
# complete
# [Verified by Visa|MasterCard SecureCode]
# authentication. Your
# participation is optional and does not effect the processing of your
# purchase. Thank you."
$AGI->stream_file('beep');
$stmtA = "UPDATE vicidial_log set status='PU' where lead_id='$VD_lead_id' order by call_date desc limit 1;";
if ($AGILOG) {$agi_string = "|$stmtA|"; &agi_output;}
$affected_rows = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "-- VDAD vicidial_log PU update: |$affected_rows|$uniqueid|"; &agi_output;}
$stmtA = "UPDATE vicidial_list set status='PU' where lead_id='$VD_lead_id';";
if ($AGILOG) {$agi_string = "|$stmtA|"; &agi_output;}
$affected_rows = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "-- VDAD vicidial_list PU update: |$affected_rows|$uniqueid|"; &agi_output;}
### Check to see if the number is in the system ###
$stmtA= "SELECT security_phrase,comments from vicidial_list where lead_id='$VD_lead_id';";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
if ($sthArows > 0)
{
@aryA = $sthA->fetchrow_array;
$security_phrase = "$aryA[0]";
$comments = "$aryA[1]";
$sthA->finish();
}
else
{
$AGI->stream_file("$goodbye_audio_file");
}
### first read of the message
if ($comments =~ /Master/i)
{
$message_one = $mc_one_file;
$message_two = $mc_two_file;
}
else
{
$message_one = $visa_one_file;
$message_two = $visa_two_file;
}
if ($AGILOG) {$agi_string = "Verify message playing: |$VD_lead_id|$security_phrase|$comments|$message_one|$message_two|"; &agi_output;}
$AGI->stream_file("$message_one");
sleep(1);
$AGI->stream_file("letters/v");
$AGI->say_digits("$security_phrase");
sleep(1);
$AGI->stream_file("$message_two");
$stmtA = "UPDATE vicidial_log set status='AL' where lead_id='$VD_lead_id' order by call_date desc limit 1;";
if ($AGILOG) {$agi_string = "|$stmtA|"; &agi_output;}
$affected_rows = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "-- VDAD vicidial_log AM update: |$affected_rows|$uniqueid|"; &agi_output;}
$stmtA = "UPDATE vicidial_list set status='AL' where lead_id='$VD_lead_id';";
if ($AGILOG) {$agi_string = "|$stmtA|"; &agi_output;}
$affected_rows = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "-- VDAD vicidial_list AM update: |$affected_rows|$uniqueid|"; &agi_output;}
$dbhA->disconnect();
### second read of the message
$AGI->stream_file("$message_one");
sleep(1);
$AGI->stream_file("letters/v");
$AGI->say_digits("$security_phrase");
sleep(1);
$AGI->stream_file("$message_two");
$AGI->stream_file("$goodbye_audio_file");
exit;
sub checkresult {
my ($res) = @_;
my $retval;
$tests++;
chomp $res;
if ($res =~ /^200/) {
$res =~ /result=(-?\d+)/;
if (!length($1)) {
# print STDERR "FAIL ($res)\n";
$fail++;
} else {
# print STDERR "PASS ($1)\n";
$pass++;
}
} else {
# print STDERR "FAIL (unexpected result '$res')\n";
$fail++;
}
}
sub agi_output
{
if ($AGILOG >=2)
{
### open the log file for writing ###
open(Lout, ">>$AGILOGfile")
|| die "Can't open $AGILOGfile: $!\n";
print Lout "$now_date|$script|$agi_string\n";
close(Lout);
}
### send to STDERR writing ###
if ( ($AGILOG == '1') || ($AGILOG == '3') )
{print STDERR "$now_date|$script|$agi_string\n";}
$agi_string='';
}
@@ -0,0 +1,710 @@
#!/usr/bin/perl
#
# agent_monitor.agi version 2.8
#
# This script is designed to allow managers to listen to agent sessions by
# dialing a phone exten number or user ID number. For this to work, the user ID
# or phone extension must be only digits. If you are using the phone extension,
# then all phone extensions on the system must be unique.
#
# user is prompted for a user ID or phone login then press #
# then the user is forwarded to monitor the active vicidial agent session
#
# ; settings for this script:
# ; 1. USER, PHONE, PLOGIN or REDIRECT, lookup by user id, phone extension or phone login (default is USER)
# ; 2. Y or N, log in api log (default is N)
# ; 3. BARGE or LISTEN, (default is LISTEN)
# ; 4. Prepopulated ID, for use in context by itself
# ; 5. Campaign limits, double-dash-delimited, i.e. "TEST--TEST2", (default is disabled)
# ; 6. Y or N, play user IDs to listen to (default is N)
#
# can be added as an AGI to a Call Menu, or just a direct extension transfer:
#
# ; call menu AGI with options:
# agent_monitor.agi,USER---N---LISTEN------TEST--TEST2---N
#
# ; or as extension transfer to "up_monitor" context and "s" extension
#
# Copyright (C) 2013 Matt Florell <vicidial@gmail.com> LICENSE: AGPLv2
#
# CHANGELOG
# 130716-1315 - First build based upon agi-phone_monitor.agi
# 130925-1823 - Added variable filter to prevent DID SQL injection attack
#
&get_time_now;
$start_epoch = $now_date_epoch;
$script = 'agent_monitor.agi';
$now_date_epoch = time();
$now_date = "$year-$mon-$mday $hour:$min:$sec";
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year = ($year + 1900);
$mon++;
if ($mon < 10) {$mon = "0$mon";}
if ($mday < 10) {$mday = "0$mday";}
if ($hour < 10) {$hour = "0$hour";}
if ($min < 10) {$min = "0$min";}
if ($sec < 10) {$sec = "0$sec";}
$SQLdate = "$year-$mon-$mday $hour:$min:$sec";
$CIDdate = "$mon$mday$hour$min$sec";
while (length($CIDdate) > 9) {$CIDdate =~ s/^.//gi;} # 0902235959 changed to 902235959
$US='_';
# default path to astguiclient configuration file:
$PATHconf = '/etc/astguiclient.conf';
open(conf, "$PATHconf") || die "can't open $PATHconf: $!\n";
@conf = <conf>;
close(conf);
$i=0;
foreach(@conf)
{
$line = $conf[$i];
$line =~ s/ |>|\n|\r|\t|\#.*|;.*//gi;
if ( ($line =~ /^PATHlogs/) && ($CLIlogs < 1) )
{$PATHlogs = $line; $PATHlogs =~ s/.*=//gi;}
if ( ($line =~ /^PATHsounds/) && ($CLIsounds < 1) )
{$PATHsounds = $line; $PATHsounds =~ s/.*=//gi;}
if ( ($line =~ /^PATHmonitor/) && ($CLImonitor < 1) )
{$PATHmonitor = $line; $PATHmonitor =~ s/.*=//gi;}
if ( ($line =~ /^VARserver_ip/) && ($CLIserver_ip < 1) )
{$VARserver_ip = $line; $VARserver_ip =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_server/) && ($CLIDB_server < 1) )
{$VARDB_server = $line; $VARDB_server =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_database/) && ($CLIDB_database < 1) )
{$VARDB_database = $line; $VARDB_database =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_user/) && ($CLIDB_user < 1) )
{$VARDB_user = $line; $VARDB_user =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_pass/) && ($CLIDB_pass < 1) )
{$VARDB_pass = $line; $VARDB_pass =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_port/) && ($CLIDB_port < 1) )
{$VARDB_port = $line; $VARDB_port =~ s/.*=//gi;}
$i++;
}
if (!$VARDB_port) {$VARDB_port='3306';}
if (!$AGILOGfile) {$AGILOGfile = "$PATHlogs/agent_monitor.$year-$mon-$mday";}
use Time::HiRes ('gettimeofday','usleep','sleep'); # necessary to have perl sleep command of less than one second
($START_s_hires, $START_usec) = gettimeofday();
$START_usec = sprintf("%06s", $START_usec);
use DBI;
use Asterisk::AGI;
$AGI = new Asterisk::AGI;
$dbhA = DBI->connect("DBI:mysql:$VARDB_database:$VARDB_server:$VARDB_port", "$VARDB_user", "$VARDB_pass")
or die "Couldn't connect to database: " . DBI->errstr;
### BEGIN parsing run-time options ###
if (length($ARGV[0])>1)
{
if ($AGILOG) {$agi_string = "Perl Environment Dump:"; &agi_output;}
$i=0;
while ($#ARGV >= $i)
{
$args = "$args $ARGV[$i]";
if ($AGILOG) {$agi_string = "$i|$ARGV[$i]"; &agi_output;}
$i++;
}
### list of command-line array arguments:
@ARGV_vars = split(/---/, $ARGV[0]);
$user_phone = $ARGV_vars[0];
$api_log = $ARGV_vars[1];
$barge_listen = $ARGV_vars[2];
$prepop_id = $ARGV_vars[3];
$campaigns_limit = $ARGV_vars[4];
$users_list = $ARGV_vars[5];
}
if ( (length($user_phone) < 1) || ($user_phone !~ /USER|PHONE|PLOGIN|REDIRECT/) )
{$user_phone = 'USER';}
if ( (length($api_log) < 1) || ($api_log !~ /Y|N/) )
{$api_log = 'N';}
if ( (length($barge_listen) < 1) || ($barge_listen !~ /BARGE|LISTEN/) )
{$barge_listen = 'LISTEN';}
if ( (length($users_list) < 1) || ($users_list !~ /Y|N/) )
{$users_list = 'N';}
$campaigns_limitSQL='';
if (length($campaigns_limit) > 1)
{
$campaigns_limitSQL = $campaigns_limit;
$campaigns_limitSQL =~ s/--/','/gi;
$campaigns_limitSQL = "and campaign_id IN('$campaigns_limitSQL')";
$campaigns_limitSQL =~ s/,''//gi;
}
### END parsing run-time options ###
$|=1;
while(<STDIN>)
{
chomp;
last unless length($_);
if ($V)
{
if (/^agi_(\w+)\:\s+(.*)$/)
{
$AGI{$1} = $2;
}
}
if (/^agi_context\:\s+(.*)$/) {$context = $1;}
if (/^agi_uniqueid\:\s+(.*)$/) {$unique_id = $1; $uniqueid = $1;}
if (/^agi_extension\:\s+(.*)$/) {$extension = $1;}
if (/^agi_channel\:\s+(.*)$/) {$channel = $1;}
if (/^agi_callerid\:\s+(.*)$/) {$callerid = $1;}
if (/^agi_calleridname\:\s+(.*)$/) {$calleridname = $1;}
}
$callerid =~ s/\'|\\\\|\\\|\\|\\;|\\\;|\;|;//gi;
$calleridname =~ s/\'|\\\\|\\\|\\|\\;|\\\;|\;|;//gi;
$extension =~ s/\'|\"|\\\\|\\\|\\|\\;|\\\;|\;|;//gi;
if ( ($callerid =~ /\".*\"/) && ( (!$calleridname) or ($calleridname =~ /unknown/) ) )
{
$calleridname = $callerid;
$calleridname =~ s/\<\d\d\d\d\d\d\d\d\d\d\>//gi;
$calleridname =~ s/\"|\" //gi;
}
$orig_callerid = $callerid;
$callerid =~ s/\D//gi;
$calleridname =~ s/unknown//gi;
if ( (!$callerid) or ($callerid =~ /unknown/) )
{$callerid = $calleridname;}
$phone_number=$callerid;
### Grab Server values from the database
$stmtA = "SELECT ext_context,agi_output FROM servers where server_ip = '$VARserver_ip';";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
$rec_count=0;
while ($sthArows > $rec_count)
{
@aryA = $sthA->fetchrow_array;
$DBext_context = $aryA[0];
$DBagi_output = $aryA[1];
if ($DBext_context) {$ext_context = $DBext_context;}
if ($DBagi_output =~ /STDERR/) {$AGILOG = '1';}
if ($DBagi_output =~ /FILE/) {$AGILOG = '2';}
if ($DBagi_output =~ /BOTH/) {$AGILOG = '3';}
$rec_count++;
}
$sthA->finish();
if ($calleridname =~ /^AGMON/)
{
$user_REDIRECT=0;
if ($user_phone =~ /REDIRECT/)
{$user_REDIRECT++;}
$stmtA = "SELECT channel,server_ip,user_phone,api_log,barge_listen,prepop_id,campaigns_limit,users_list,context FROM vicidial_monitor_calls where callerid='$calleridname';";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
if ($sthArows > 0)
{
@aryA = $sthA->fetchrow_array;
$channel = $aryA[0];
$server_ip = $aryA[1];
$user_phone = $aryA[2];
$api_log = $aryA[3];
$barge_listen = $aryA[4];
$prepop_id = $aryA[5];
$campaigns_limit = $aryA[6];
$users_list = $aryA[7];
$context = $aryA[8];
}
$sthA->finish();
if ($AGILOG) {$agi_string = "EXISTING MONITOR SESSION: |$calleridname|$channel|$server_ip|$user_phone|$api_log|$barge_listen|$prepop_id|$campaigns_limit|$users_list|$context|"; &agi_output;}
$campaigns_limitSQL='';
if (length($campaigns_limit) > 1)
{
$campaigns_limitSQL = $campaigns_limit;
$campaigns_limitSQL =~ s/--/','/gi;
$campaigns_limitSQL = "and campaign_id IN('$campaigns_limitSQL')";
$campaigns_limitSQL =~ s/,''//gi;
}
if ($user_REDIRECT > 0)
{
$MHGqueryCID = "AGMHG$CIDdate$START_usec";
### insert a NEW record to the vicidial_manager table to be processed
$stmtA = "INSERT INTO vicidial_manager values('','','$SQLdate','NEW','N','$server_ip','','Redirect','$MHGqueryCID','Channel: $channel','Context: $context','Exten: s','Priority: 1','CallerID: $MHGqueryCID','','','','','')";
$affected_rows = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "-- AGENT MONITOR CALL RESET: |$affected_rows|$calleridname|$channel|$server_ip|$context|$MHGqueryCID|"; &agi_output;}
sleep(5);
exit;
}
}
else
{
### Insert agent monitor record entry to keep track of the call
$MONqueryCID = "AGMON$CIDdate$START_usec";
$calleridname = $MONqueryCID;
$AGI->set_callerid($MONqueryCID);
print STDERR "Setting CID \"$MONqueryCID\"\n";
$stmtA="INSERT INTO vicidial_monitor_calls set server_ip='$VARserver_ip',callerid='$MONqueryCID',channel='$channel',context='$context',uniqueid='$uniqueid',monitor_time=NOW(),user_phone='$user_phone',api_log='$api_log',barge_listen='$barge_listen',prepop_id='$prepop_id',campaigns_limit='$campaigns_limit',users_list='$users_list';";
$affected_rows = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "MONITOR CALLS INSERT $affected_rows|$stmtA|"; &agi_output;}
}
$AGI->stream_file('sip-silence');
$AGI->stream_file('sip-silence');
if ($users_list =~ /Y/i)
{
### find sox binary to do the combination of audio files for agent listing
$soxbin = '';
if ( -e ('/usr/bin/sox')) {$soxbin = '/usr/bin/sox';}
else
{
if ( -e ('/usr/local/bin/sox')) {$soxbin = '/usr/local/bin/sox';}
else
{
print "Can't find sox binary! Exiting...\n";
$AGI->stream_file('an-error-has-occured');
exit;
}
}
}
$entry_chances=0;
$VALID_login=0;
$ACTIVE_login=0;
$VDADremDIALstr='';
while ( ($entry_chances < 3) && ($VALID_login < 1) )
{
$entry_chances++;
if (length($prepop_id)<2)
{
&user_phone_gather;
}
else
{$PIN_number = $prepop_id;}
###### Check userID and password against vicidial_users table
$stmtA = "SELECT count(*) FROM vicidial_users where user='$PIN_number' and active='Y';";
if ($user_phone =~ /PHONE/)
{$stmtA = "SELECT count(*) FROM phones where extension='$PIN_number' and active='Y';";}
if ($user_phone =~ /PLOGIN/)
{$stmtA = "SELECT count(*) FROM phones where login='$PIN_number' and active='Y';";}
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
if ($sthArows > 0)
{
@aryA = $sthA->fetchrow_array;
$VALID_login = $aryA[0];
}
$sthA->finish();
if ($VALID_login > 0)
{
$AGI->stream_file('pin-number-accepted');
}
else
{
if ($api_log =~ /Y/)
{
$stmtA="INSERT INTO vicidial_api_log set user='$calleridname',agent_user='$PIN_number',function='phone_monitor',value='',result='ERROR',result_reason='No user found',source='monitor',data='$calleridname|$callerid',api_date='$now_date',api_script='$script',run_time='0';";
$affected_rows = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "API LOG INSERT $affected_rows|$stmtA|"; &agi_output;}
}
$AGI->stream_file('pin-invalid');
}
}
if ($VALID_login < 1)
{
if ($AGILOG) {$agi_string = "PHONE MONITOR END: Invalid Login |$VALID_login|$PIN_number|"; &agi_output;}
if ($use_call_menu =~ /Y/)
{
if ($AGILOG) {$agi_string = "exiting the agent dial-in app, transferring call to B @ $context"; &agi_output;}
print "SET CONTEXT $context\n";
$result = <STDIN>;
checkresult($result);
print "SET EXTENSION B\n";
$result = <STDIN>;
checkresult($result);
print "SET PRIORITY 1\n";
$result = <STDIN>;
checkresult($result);
}
else
{
$AGI->stream_file('vm-goodbye');
$AGI->hangup();
$dbhA->disconnect();
}
exit;
}
else
{
$secX = time();
$PDtarget = ($secX - 30);
($Psec,$Pmin,$Phour,$Pmday,$Pmon,$Pyear,$Pwday,$Pyday,$Pisdst) = localtime($PDtarget);
$Pyear = ($Pyear + 1900);
$Pmon++;
if ($Pmon < 10) {$Pmon = "0$Pmon";}
if ($Pmday < 10) {$Pmday = "0$Pmday";}
if ($Phour < 10) {$Phour = "0$Phour";}
if ($Pmin < 10) {$Pmin = "0$Pmin";}
if ($Psec < 10) {$Psec = "0$Psec";}
$PDtsSQLdate = "$Pyear$Pmon$Pmday$Phour$Pmin$Psec";
###### Check if user is logged in, vicidial_live_agents table
$stmtA = "SELECT count(*) FROM vicidial_live_agents where user='$PIN_number' and extension NOT LIKE \"R/%\" and last_update_time > \"$PDtsSQLdate\" $campaigns_limitSQL;";
if ($user_phone =~ /PHONE/)
{$stmtA = "SELECT count(*) FROM vicidial_live_agents where extension LIKE \"%/$PIN_number\" and extension NOT LIKE \"R/%\" and last_update_time > \"$PDtsSQLdate\" $campaigns_limitSQL;";}
if ($user_phone =~ /PLOGIN/)
{
$PLOGIN_extension='XXXXXXXXXXXXXXXXXX';
$PLOGIN_server_ip='1.1.1.1';
$stmtA = "SELECT extension,server_ip FROM phones where login='$PIN_number' and active='Y';";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
if ($sthArows > 0)
{
@aryA = $sthA->fetchrow_array;
$PLOGIN_extension = $aryA[0];
$PLOGIN_server_ip = $aryA[1];
}
$sthA->finish();
if ($AGILOG) {$agi_string = "PLOGIN DEBUG: |$PLOGIN_extension|$PLOGIN_server_ip|$stmtA|"; &agi_output;}
$stmtA = "SELECT count(*) FROM vicidial_live_agents where extension LIKE \"%/$PLOGIN_extension\" and server_ip='$PLOGIN_server_ip' and extension NOT LIKE \"R/%\" and last_update_time > \"$PDtsSQLdate\" $campaigns_limitSQL;";
}
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
if ($sthArows > 0)
{
@aryA = $sthA->fetchrow_array;
$ACTIVE_login = "$aryA[0]";
}
$sthA->finish();
if ($ACTIVE_login < 1)
{
if ($AGILOG) {$agi_string = "PHONE MONIOR END: Inactive Login |$VALID_login|$PIN_number|$PASS_word| |$ACTIVE_login|$PDtsSQLdate|$stmtA|"; &agi_output;}
if ($api_log =~ /Y/)
{
$stmtA="INSERT INTO vicidial_api_log set user='$calleridname',agent_user='$PIN_number',function='phone_monitor',value='',result='ERROR',result_reason='Inactive Login',source='monitor',data='$calleridname|$callerid',api_date='$now_date',api_script='$script',run_time='0';";
$affected_rows = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "API LOG INSERT $affected_rows|$stmtA|"; &agi_output;}
}
$AGI->stream_file('login-fail');
$AGI->stream_file('vm-goodbye');
$AGI->hangup();
$dbhA->disconnect();
exit;
}
else
{
$stmtA = "SELECT conf_exten,extension,server_ip,campaign_id from vicidial_live_agents WHERE user='$PIN_number' and extension NOT LIKE \"R/%\" and last_update_time > \"$PDtsSQLdate\" $campaigns_limitSQL limit 1;";
if ($user_phone =~ /PHONE/)
{$stmtA = "SELECT conf_exten,extension,server_ip,campaign_id FROM vicidial_live_agents where extension LIKE \"%/$PIN_number\" and extension NOT LIKE \"R/%\" and last_update_time > \"$PDtsSQLdate\" $campaigns_limitSQL limit 1;";}
if ($user_phone =~ /PLOGIN/)
{
$PLOGIN_extension='XXXXXXXXXXXXXXXXXX';
$PLOGIN_server_ip='1.1.1.1';
$stmtA = "SELECT extension,server_ip FROM phones where login='$PIN_number' and active='Y';";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
if ($sthArows > 0)
{
@aryA = $sthA->fetchrow_array;
$PLOGIN_extension = $aryA[0];
$PLOGIN_server_ip = $aryA[1];
}
$sthA->finish();
$stmtA = "SELECT conf_exten,extension,server_ip,campaign_id FROM vicidial_live_agents where extension LIKE \"%/$PLOGIN_extension\" and server_ip='$PLOGIN_server_ip' and extension NOT LIKE \"R/%\" and last_update_time > \"$PDtsSQLdate\" $campaigns_limitSQL limit 1;";
}
if ($AGILOG) {$agi_string = "|$stmtA|"; &agi_output;}
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
$rec_countCUSTDATA=0;
if ($sthArows > 0)
{
@aryA = $sthA->fetchrow_array;
$VDADconf_exten = $aryA[0];
$VDADextension = $aryA[1];
$VDADserver_ip = $aryA[2];
$VDADcampaign_id = $aryA[3];
}
$sthA->finish();
### format the remote server dialstring to get the call to the overflow agent meetme room
$S='*';
if( $VDADserver_ip =~ m/(\S+)\.(\S+)\.(\S+)\.(\S+)/ )
{
$a = leading_zero($1);
$b = leading_zero($2);
$c = leading_zero($3);
$d = leading_zero($4);
# use double-star prefix to go through native bridge IAX path
$VDADremDIALstr = "$S$S$a$S$b$S$c$S$d$S";
}
$barge_listen_prefix = '58';
if($barge_listen =~ /BARGE/)
{$barge_listen_prefix = '59';}
$VDADremDIALstr .= "$barge_listen_prefix$VDADconf_exten";
if ($api_log =~ /Y/)
{
$stmtA="INSERT INTO vicidial_api_log set user='$orig_callerid',agent_user='$PIN_number',function='phone_monitor',value='',result='SUCCESS',result_reason='',source='monitor',data='$VDADconf_exten|$VDADextension|$VDADserver_ip|$calleridname|$callerid',api_date='$now_date',api_script='$script',run_time='0';";
$affected_rows = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "API LOG INSERT $affected_rows|$stmtA|"; &agi_output;}
}
$stmtA="INSERT INTO vicidial_monitor_log set server_ip='$VARserver_ip',callerid='$calleridname',channel='$channel',context='$context',uniqueid='$uniqueid',monitor_time=NOW(),user='$PIN_number',campaign_id='$VDADcampaign_id';";
$affected_rows = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "MONITOR LOG INSERT $affected_rows|$stmtA|"; &agi_output;}
if ($AGILOG) {$agi_string = "Agent $PIN_number sent to $VDADremDIALstr"; &agi_output;}
print "SET CONTEXT $ext_context\n";
$result = <STDIN>;
checkresult($result);
print "SET EXTENSION $VDADremDIALstr\n";
$result = <STDIN>;
checkresult($result);
print "SET PRIORITY 1\n";
$result = <STDIN>;
checkresult($result);
}
}
$dbhA->disconnect();
exit;
################################################################################
##### SUBROUTINES ##############################################################
################################################################################
##### BEGIN collect the user and password ######################################
sub user_phone_gather
{
################################################################################
# please enter the pin number followed by the pound key
$gsm = '.gsm';
$gather_prompt = 'ld_welcome_pin_number';
if ($users_list =~ /Y/i)
{
### generate user list for live agents available for monitoring
$sox_cmd = "$soxbin --combine concatenate /var/lib/asterisk/sounds/ld_welcome_pin_number.gsm /var/lib/asterisk/sounds/silence.wav";
$secX = time();
$PDtarget = ($secX - 30);
($Psec,$Pmin,$Phour,$Pmday,$Pmon,$Pyear,$Pwday,$Pyday,$Pisdst) = localtime($PDtarget);
$Pyear = ($Pyear + 1900);
$Pmon++;
if ($Pmon < 10) {$Pmon = "0$Pmon";}
if ($Pmday < 10) {$Pmday = "0$Pmday";}
if ($Phour < 10) {$Phour = "0$Phour";}
if ($Pmin < 10) {$Pmin = "0$Pmin";}
if ($Psec < 10) {$Psec = "0$Psec";}
$PDtsSQLdate = "$Pyear$Pmon$Pmday$Phour$Pmin$Psec";
$stmtA = "SELECT user FROM vicidial_live_agents where last_update_time > \"$PDtsSQLdate\" and extension NOT LIKE \"R/%\" $campaigns_limitSQL order by last_update_time desc limit 100;";
if ($user_phone =~ /PHONE/)
{$stmtA = "SELECT extension FROM vicidial_live_agents where extension NOT LIKE \"R/%\" and last_update_time > \"$PDtsSQLdate\" $campaigns_limitSQL limit 100;";}
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
if ($AGILOG) {$agi_string = "Available Agents Found: |$sthArows|$stmtA|"; &agi_output;}
$i=0;
while ($sthArows > $i)
{
@aryA = $sthA->fetchrow_array;
$sox_cmd .= " /var/lib/asterisk/sounds/silence.wav /var/lib/asterisk/sounds/user.gsm";
$sox_user = $aryA[0];
$sox_user =~ s/IAX2\/|IAX\/|SIP\///gi;
$sox_user =~ s/\D//gi;
foreach $user_char (split //, $sox_user)
{
$sox_cmd .= " /var/lib/asterisk/sounds/digits/$user_char$gsm";
}
$i++;
}
$sthA->finish();
if ($sthArows < 1)
{
$sox_cmd .= " /var/lib/asterisk/sounds/silence.wav /var/lib/asterisk/sounds/user.gsm /var/lib/asterisk/sounds/unavailable.gsm";
}
$sox_cmd .= " /var/lib/asterisk/sounds/mtemp/$calleridname$gsm";
if ($AGILOG) {$agi_string = "SoX CLI: |$i|$sox_cmd|"; &agi_output;}
@sox_cmd_output = `$sox_cmd`;
sleep(2);
if ( -e ("/var/lib/asterisk/sounds/mtemp/$calleridname$gsm"))
{$gather_prompt = "./mtemp/$calleridname";}
else
{
print "ERROR! Dynamic user list not created! Exiting...\n";
$AGI->stream_file('an-error-has-occured');
exit;
}
}
$interrupt_digit='';
$interrupt_digit = $AGI->stream_file("$gather_prompt",'0123456789');
print STDERR "interrupt_digit |$interrupt_digit|\n";
$digits_being_entered=1;
$PIN_number='';
if ($interrupt_digit > 0)
{
# if ($interrupt_digit == 35) {$interrupt_digit='#';}
# if ($interrupt_digit == 42) {$interrupt_digit='*';}
if ($interrupt_digit == 48) {$interrupt_digit=0;}
if ($interrupt_digit == 49) {$interrupt_digit=1;}
if ($interrupt_digit == 50) {$interrupt_digit=2;}
if ($interrupt_digit == 51) {$interrupt_digit=3;}
if ($interrupt_digit == 52) {$interrupt_digit=4;}
if ($interrupt_digit == 53) {$interrupt_digit=5;}
if ($interrupt_digit == 54) {$interrupt_digit=6;}
if ($interrupt_digit == 55) {$interrupt_digit=7;}
if ($interrupt_digit == 56) {$interrupt_digit=8;}
if ($interrupt_digit == 57) {$interrupt_digit=9;}
$PIN_number=$interrupt_digit;
}
$digit_loop_counter=0;
while ( ($digits_being_entered > 0) && ($digit_loop_counter < 20) )
{
$digit = chr($AGI->wait_for_digit('10000')); # wait 10 seconds for input or until the pound key is pressed
if ($digit =~ /\d/)
{
$PIN_number = "$PIN_number$digit";
print STDERR "digit |$digit| PIN_number |$PIN_number|\n";
# $AGI->say_digits("$digit");
undef $digit;
}
else
{
$digits_being_entered=0;
}
$digit_loop_counter++;
}
$PIN_number =~ s/\D//gi;
if ($PIN_number) {print STDERR "digit |$digit| PIN_number |$PIN_number|\n";}
}
##### END collect the user and password ###############################################
sub get_time_now #get the current date and time and epoch for logging call lengths and datetimes
{
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year = ($year + 1900);
$mon++;
if ($mon < 10) {$mon = "0$mon";}
if ($mday < 10) {$mday = "0$mday";}
if ($hour < 10) {$hour = "0$hour";}
if ($min < 10) {$min = "0$min";}
if ($sec < 10) {$sec = "0$sec";}
$now_date_epoch = time();
$now_date = "$year-$mon-$mday $hour:$min:$sec";
$filedate = "$year$mon$mday$hour$min$sec";
}
sub checkresult
{
my ($res) = @_;
my $retval;
$tests++;
chomp $res;
if ($res =~ /^200/) {
$res =~ /result=(-?\d+)/;
if (!length($1))
{
print STDERR "FAIL ($res)\n";
$fail++;
}
else
{
print STDERR "PASS ($1)\n";
$pass++;
}
}
else
{
print STDERR "FAIL (unexpected result '$res')\n";
$fail++;
}
}
sub agi_output
{
if ($AGILOG >=2)
{
### open the log file for writing ###
open(Lout, ">>$AGILOGfile")
|| die "Can't open $AGILOGfile: $!\n";
print Lout "$now_date|$script|$agi_string\n";
close(Lout);
}
### send to STDERR writing ###
if ( ($AGILOG == '1') || ($AGILOG == '3') )
{print STDERR "$now_date|$script|$agi_string\n";}
$agi_string='';
}
sub leading_zero($)
{
$_ = $_[0];
s/^(\d)$/0$1/;
s/^(\d\d)$/0$1/;
return $_;
} # End of the leading_zero() routine.
@@ -0,0 +1,597 @@
#!/usr/bin/perl
#
# agi-AGENT_dial_in.agi version 2.6
#
# This script is designed to allow agents the ability to dial in to an extension
# to establish the phone connection to their VICIDIAL session. The user login
# and password must be only digits and nothing but digits to use this utility.
#
# user is prompted for their user ID then press #
# user is prompted for their passcode then press #
# then the user is forwarded to the active vicidial agent session
#
# ; settings for this script:
# ; 1. allow user_id only login (disabled by default)
# ; Y - allow user ID only with no password
# ; CONTACTS - lookup the user ID as the 'office_num' field in the contact_information table
# ; N or (blank) - require password (default)
# ; 2. forward to A or B within Call Menu instead of logging into an agent session
# ; Y - forward to A option on valid user ID or B option on invalid user ID
# ; N or (blank) - go to agent session (default)
#
# can be added as an AGI to a Call Menu, or as below:
# ;agent dial-in:
#exten => 2345,1,Answer ; Answer the line
#exten => 2345,2,AGI(agi-AGENT_dial_in.agi)
#exten => 2345,3,Hangup
#
# ; call menu with options:
# agi-AGENT_dial_in.agi,Y---Y
#
# Copyright (C) 2013 Matt Florell <vicidial@gmail.com> LICENSE: AGPLv2
#
# CHANGELOG
# 81022-2115 - First build
# 120511-1423 - Added new options
# 130108-1809 - Changes for Asterisk 1.8 compatibility
#
&get_time_now;
$start_epoch = $now_date_epoch;
$script = 'agi-AGENT_dial_in.agi';
$now_date_epoch = time();
$now_date = "$year-$mon-$mday $hour:$min:$sec";
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year = ($year + 1900);
$mon++;
if ($mon < 10) {$mon = "0$mon";}
if ($mday < 10) {$mday = "0$mday";}
if ($hour < 10) {$hour = "0$hour";}
if ($min < 10) {$min = "0$min";}
if ($sec < 10) {$sec = "0$sec";}
$US='_';
# default path to astguiclient configuration file:
$PATHconf = '/etc/astguiclient.conf';
open(conf, "$PATHconf") || die "can't open $PATHconf: $!\n";
@conf = <conf>;
close(conf);
$i=0;
foreach(@conf)
{
$line = $conf[$i];
$line =~ s/ |>|\n|\r|\t|\#.*|;.*//gi;
if ( ($line =~ /^PATHlogs/) && ($CLIlogs < 1) )
{$PATHlogs = $line; $PATHlogs =~ s/.*=//gi;}
if ( ($line =~ /^PATHsounds/) && ($CLIsounds < 1) )
{$PATHsounds = $line; $PATHsounds =~ s/.*=//gi;}
if ( ($line =~ /^PATHmonitor/) && ($CLImonitor < 1) )
{$PATHmonitor = $line; $PATHmonitor =~ s/.*=//gi;}
if ( ($line =~ /^VARserver_ip/) && ($CLIserver_ip < 1) )
{$VARserver_ip = $line; $VARserver_ip =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_server/) && ($CLIDB_server < 1) )
{$VARDB_server = $line; $VARDB_server =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_database/) && ($CLIDB_database < 1) )
{$VARDB_database = $line; $VARDB_database =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_user/) && ($CLIDB_user < 1) )
{$VARDB_user = $line; $VARDB_user =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_pass/) && ($CLIDB_pass < 1) )
{$VARDB_pass = $line; $VARDB_pass =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_port/) && ($CLIDB_port < 1) )
{$VARDB_port = $line; $VARDB_port =~ s/.*=//gi;}
$i++;
}
if (!$VARDB_port) {$VARDB_port='3306';}
if (!$AGILOGfile) {$AGILOGfile = "$PATHlogs/ivrout.$year-$mon-$mday";}
use DBI;
use Asterisk::AGI;
$AGI = new Asterisk::AGI;
$dbhA = DBI->connect("DBI:mysql:$VARDB_database:$VARDB_server:$VARDB_port", "$VARDB_user", "$VARDB_pass")
or die "Couldn't connect to database: " . DBI->errstr;
### BEGIN parsing run-time options ###
if (length($ARGV[0])>1)
{
if ($AGILOG) {$agi_string = "Perl Environment Dump:"; &agi_output;}
$i=0;
while ($#ARGV >= $i)
{
$args = "$args $ARGV[$i]";
if ($AGILOG) {$agi_string = "$i|$ARGV[$i]"; &agi_output;}
$i++;
}
### list of command-line array arguments:
@ARGV_vars = split(/---/, $ARGV[0]);
$user_id_only = $ARGV_vars[0];
$use_call_menu = $ARGV_vars[1];
}
if ( (length($user_id_only) < 1) || ($user_id_only !~ /Y|N/) )
{$user_id_only = 'N';}
if ( (length($use_call_menu) < 1) || ($use_call_menu !~ /Y|N/) )
{$use_call_menu = 'N';}
### END parsing run-time options ###
$|=1;
while(<STDIN>)
{
chomp;
last unless length($_);
if ($V)
{
if (/^agi_(\w+)\:\s+(.*)$/)
{
$AGI{$1} = $2;
}
}
if (/^agi_context\:\s+(.*)$/) {$context = $1;}
if (/^agi_uniqueid\:\s+(.*)$/) {$unique_id = $1;}
if (/^agi_extension\:\s+(.*)$/) {$extension = $1;}
if (/^agi_channel\:\s+(.*)$/) {$channel = $1;}
if (/^agi_callerid\:\s+(.*)$/) {$callerid = $1;}
if (/^agi_calleridname\:\s+(.*)$/) {$calleridname = $1;}
}
if ( ($callerid =~ /\".*\"/) && ( (!$calleridname) or ($calleridname =~ /unknown/) ) )
{
$calleridname = $callerid;
$calleridname =~ s/\<\d\d\d\d\d\d\d\d\d\d\>//gi;
$calleridname =~ s/\"|\" //gi;
}
$callerid =~ s/\D//gi;
$calleridname =~ s/unknown//gi;
if ( (!$callerid) or ($callerid =~ /unknown/) )
{$callerid = $calleridname;}
$phone_number=$callerid;
### Grab Server values from the database
$stmtA = "SELECT ext_context,agi_output FROM servers where server_ip = '$VARserver_ip';";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
$rec_count=0;
while ($sthArows > $rec_count)
{
@aryA = $sthA->fetchrow_array;
$DBext_context = "$aryA[0]";
$DBagi_output = "$aryA[1]";
if ($DBext_context) {$ext_context = $DBext_context;}
if ($DBagi_output =~ /STDERR/) {$AGILOG = '1';}
if ($DBagi_output =~ /FILE/) {$AGILOG = '2';}
if ($DBagi_output =~ /BOTH/) {$AGILOG = '3';}
$rec_count++;
}
$sthA->finish();
$AGI->stream_file('sip-silence');
$AGI->stream_file('sip-silence');
$entry_chances=0;
$VALID_login=0;
$ACTIVE_login=0;
$VDADremDIALstr='';
while ( ($entry_chances < 3) && ($VALID_login < 1) )
{
$entry_chances++;
&user_password_gather;
###### Check userID and password against vicidial_users table
if ($user_id_only =~ /CONTACT/)
{
#############################################
##### START SYSTEM SETTINGS LOOKUP #####
$stmtA = "SELECT alt_log_server_ip,alt_log_dbname,alt_log_login,alt_log_pass,tables_use_alt_log_db FROM system_settings;";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
if ($sthArows > 0)
{
@aryA = $sthA->fetchrow_array;
$alt_log_server_ip = $aryA[0];
$alt_log_dbname = $aryA[1];
$alt_log_login = $aryA[2];
$alt_log_pass = $aryA[3];
$tables_use_alt_log_db = $aryA[4];
}
$sthA->finish();
##### END SYSTEM SETTINGS LOOKUP #####
###########################################
if ( ($tables_use_alt_log_db =~ /contact_information/) && (length($alt_log_server_ip)>4) && (length($alt_log_dbname)>0) )
{
$linkALT = DBI->connect("DBI:mysql:$alt_log_dbname:$alt_log_server_ip:$VARDB_port", "$alt_log_login", "$alt_log_pass")
or die "Couldn't connect to database: " . DBI->errstr;
}
else
{$linkALT = $dbhA;}
$stmtA = "SELECT count(*) FROM contact_information where office_num='$PIN_number';";
$sthA = $linkALT->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $linkALT->errstr;
$sthArows=$sthA->rows;
if ($sthArows > 0)
{
@aryA = $sthA->fetchrow_array;
$VALID_login = "$aryA[0]";
}
$sthA->finish();
}
else
{
$stmtA = "SELECT count(*) FROM vicidial_users where user='$PIN_number' and pass='$PASS_word' and active='Y';";
if ($user_id_only =~ /Y/)
{$stmtA = "SELECT count(*) FROM vicidial_users where user='$PIN_number' and active='Y';";}
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
if ($sthArows > 0)
{
@aryA = $sthA->fetchrow_array;
$VALID_login = "$aryA[0]";
}
$sthA->finish();
}
if ($VALID_login > 0)
{
$AGI->exec("EXEC Set(_agent_user_id=$PIN_number)");
$AGI->stream_file('pin-number-accepted');
}
else
{
$AGI->stream_file('pin-invalid');
}
}
if ($VALID_login < 1)
{
if ($AGILOG) {$agi_string = "DIAL-IN END: Invalid Login |$VALID_login|$PIN_number|$PASS_word|"; &agi_output;}
if ($use_call_menu =~ /Y/)
{
if ($AGILOG) {$agi_string = "exiting the agent dial-in app, transferring call to B @ $context"; &agi_output;}
print "SET CONTEXT $context\n";
$result = <STDIN>;
checkresult($result);
print "SET EXTENSION B\n";
$result = <STDIN>;
checkresult($result);
print "SET PRIORITY 1\n";
$result = <STDIN>;
checkresult($result);
}
else
{
$AGI->stream_file('vm-goodbye');
$AGI->hangup();
$dbhA->disconnect();
}
exit;
}
else
{
$secX = time();
$PDtarget = ($secX - 30);
($Psec,$Pmin,$Phour,$Pmday,$Pmon,$Pyear,$Pwday,$Pyday,$Pisdst) = localtime($PDtarget);
$Pyear = ($Pyear + 1900);
$Pmon++;
if ($Pmon < 10) {$Pmon = "0$Pmon";}
if ($Pmday < 10) {$Pmday = "0$Pmday";}
if ($Phour < 10) {$Phour = "0$Phour";}
if ($Pmin < 10) {$Pmin = "0$Pmin";}
if ($Psec < 10) {$Psec = "0$Psec";}
$PDtsSQLdate = "$Pyear$Pmon$Pmday$Phour$Pmin$Psec";
if ($use_call_menu =~ /Y/)
{
if ($AGILOG) {$agi_string = "exiting the agent dial-in app, transferring call to A @ $context"; &agi_output;}
print "SET CONTEXT $context\n";
$result = <STDIN>;
checkresult($result);
print "SET EXTENSION A\n";
$result = <STDIN>;
checkresult($result);
print "SET PRIORITY 1\n";
$result = <STDIN>;
checkresult($result);
exit;
}
else
{
###### Check if user is logged in, vicidial_live_agents table
$stmtA = "SELECT count(*) FROM vicidial_live_agents where user='$PIN_number' and last_update_time > \"$PDtsSQLdate\";";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
if ($sthArows > 0)
{
@aryA = $sthA->fetchrow_array;
$ACTIVE_login = "$aryA[0]";
}
$sthA->finish();
if ($ACTIVE_login < 1)
{
if ($AGILOG) {$agi_string = "DIAL-IN END: Inactive Login |$VALID_login|$PIN_number|$PASS_word| |$ACTIVE_login|$PDtsSQLdate|"; &agi_output;}
$AGI->stream_file('login-fail');
$AGI->stream_file('you-must-be-logged-in');
$AGI->stream_file('vm-goodbye');
$AGI->hangup();
$dbhA->disconnect();
exit;
}
else
{
$stmtA = "SELECT conf_exten,extension,server_ip from vicidial_live_agents WHERE user='$PIN_number' limit 1;";
if ($AGILOG) {$agi_string = "|$stmtA|"; &agi_output;}
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
$rec_countCUSTDATA=0;
if ($sthArows > 0)
{
@aryA = $sthA->fetchrow_array;
$VDADconf_exten = "$aryA[0]";
$VDADextension = "$aryA[1]";
$VDADserver_ip = "$aryA[2]";
}
$sthA->finish();
### format the remote server dialstring to get the call to the overflow agent meetme room
$S='*';
if( $VDADserver_ip =~ m/(\S+)\.(\S+)\.(\S+)\.(\S+)/ )
{
$a = leading_zero($1);
$b = leading_zero($2);
$c = leading_zero($3);
$d = leading_zero($4);
$VDADremDIALstr = "$a$S$b$S$c$S$d$S";
}
$VDADremDIALstr .= "$VDADconf_exten";
if ($AGILOG) {$agi_string = "Agent $PIN_number sent to $VDADremDIALstr"; &agi_output;}
print "SET CONTEXT $ext_context\n";
$result = <STDIN>;
checkresult($result);
print "SET EXTENSION $VDADremDIALstr\n";
$result = <STDIN>;
checkresult($result);
print "SET PRIORITY 1\n";
$result = <STDIN>;
checkresult($result);
}
}
}
$dbhA->disconnect();
exit;
################################################################################
##### SUBROUTINES ##############################################################
################################################################################
##### BEGIN collect the user and password ######################################
sub user_password_gather
{
################################################################################
# please enter the pin number followed by the pound key
$interrupt_digit='';
$interrupt_digit = $AGI->stream_file('ld_welcome_pin_number','0123456789');
print STDERR "interrupt_digit |$interrupt_digit|\n";
$digits_being_entered=1;
$PIN_number='';
if ($interrupt_digit > 0)
{
# if ($interrupt_digit == 35) {$interrupt_digit='#';}
# if ($interrupt_digit == 42) {$interrupt_digit='*';}
if ($interrupt_digit == 48) {$interrupt_digit=0;}
if ($interrupt_digit == 49) {$interrupt_digit=1;}
if ($interrupt_digit == 50) {$interrupt_digit=2;}
if ($interrupt_digit == 51) {$interrupt_digit=3;}
if ($interrupt_digit == 52) {$interrupt_digit=4;}
if ($interrupt_digit == 53) {$interrupt_digit=5;}
if ($interrupt_digit == 54) {$interrupt_digit=6;}
if ($interrupt_digit == 55) {$interrupt_digit=7;}
if ($interrupt_digit == 56) {$interrupt_digit=8;}
if ($interrupt_digit == 57) {$interrupt_digit=9;}
$PIN_number=$interrupt_digit;
}
$digit_loop_counter=0;
while ( ($digits_being_entered > 0) && ($digit_loop_counter < 20) )
{
$digit = chr($AGI->wait_for_digit('10000')); # wait 10 seconds for input or until the pound key is pressed
if ($digit =~ /\d/)
{
$PIN_number = "$PIN_number$digit";
print STDERR "digit |$digit| PIN_number |$PIN_number|\n";
# $AGI->say_digits("$digit");
undef $digit;
}
else
{
$digits_being_entered=0;
}
$digit_loop_counter++;
}
$PIN_number =~ s/\D//gi;
if ($PIN_number) {print STDERR "digit |$digit| PIN_number |$PIN_number|\n";}
if ($user_id_only !~ /Y|CONTACT/)
{
################################################################################
# please enter your password followed by the pound key
$interrupt_digit='';
$interrupt_digit = $AGI->stream_file('enter-password','0123456789');
print STDERR "interrupt_digit |$interrupt_digit|\n";
$digits_being_entered=1;
$PASS_word='';
if ($interrupt_digit > 0)
{
# if ($interrupt_digit == 35) {$interrupt_digit='#';}
# if ($interrupt_digit == 42) {$interrupt_digit='*';}
if ($interrupt_digit == 48) {$interrupt_digit=0;}
if ($interrupt_digit == 49) {$interrupt_digit=1;}
if ($interrupt_digit == 50) {$interrupt_digit=2;}
if ($interrupt_digit == 51) {$interrupt_digit=3;}
if ($interrupt_digit == 52) {$interrupt_digit=4;}
if ($interrupt_digit == 53) {$interrupt_digit=5;}
if ($interrupt_digit == 54) {$interrupt_digit=6;}
if ($interrupt_digit == 55) {$interrupt_digit=7;}
if ($interrupt_digit == 56) {$interrupt_digit=8;}
if ($interrupt_digit == 57) {$interrupt_digit=9;}
$PASS_word=$interrupt_digit;
}
$digit_loop_counter=0;
while ( ($digits_being_entered > 0) && ($digit_loop_counter < 20) )
{
$digit = chr($AGI->wait_for_digit('10000')); # wait 10 seconds for input or until the pound key is pressed
if ($digit =~ /\d/)
{
$PASS_word = "$PASS_word$digit";
print STDERR "digit |$digit| PASS_word |$PASS_word|\n";
# $AGI->say_digits("$digit");
undef $digit;
}
else
{
$digits_being_entered=0;
}
$digit_loop_counter++;
}
$PASS_word =~ s/\D//gi;
if ($PASS_word) {print STDERR "digit |$digit| PASS_word |$PASS_word| PIN_number |$PIN_number|\n";}
}
}
##### END collect the user and password ###############################################
##### SUBROUTINES #####
sub get_time_now #get the current date and time and epoch for logging call lengths and datetimes
{
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year = ($year + 1900);
$mon++;
if ($mon < 10) {$mon = "0$mon";}
if ($mday < 10) {$mday = "0$mday";}
if ($hour < 10) {$hour = "0$hour";}
if ($min < 10) {$min = "0$min";}
if ($sec < 10) {$sec = "0$sec";}
$now_date_epoch = time();
$now_date = "$year-$mon-$mday $hour:$min:$sec";
$filedate = "$year$mon$mday$hour$min$sec";
}
sub checkresult
{
my ($res) = @_;
my $retval;
$tests++;
chomp $res;
if ($res =~ /^200/) {
$res =~ /result=(-?\d+)/;
if (!length($1))
{
print STDERR "FAIL ($res)\n";
$fail++;
}
else
{
print STDERR "PASS ($1)\n";
$pass++;
}
}
else
{
print STDERR "FAIL (unexpected result '$res')\n";
$fail++;
}
}
sub agi_output
{
if ($AGILOG >=2)
{
### open the log file for writing ###
open(Lout, ">>$AGILOGfile")
|| die "Can't open $AGILOGfile: $!\n";
print Lout "$now_date|$script|$agi_string\n";
close(Lout);
}
### send to STDERR writing ###
if ( ($AGILOG == '1') || ($AGILOG == '3') )
{print STDERR "$now_date|$script|$agi_string\n";}
$agi_string='';
}
sub leading_zero($)
{
$_ = $_[0];
s/^(\d)$/0$1/;
s/^(\d\d)$/0$1/;
return $_;
} # End of the leading_zero() routine.
@@ -0,0 +1,478 @@
#!/usr/bin/perl
#
# agi-AGENT_route.agi version 2.6
#
# for use with a Call Menu in the custom dialplan section or as an AGI route.
#
# ; settings for this script:
# ; 1. DID pattern to use for In-group initiation settings ('default' is default)
# ; 2. the agentdirect in-group to send the calls to ('AGENTDIRECT' is default)
# ; 3. transfer only to ACTIVE in the system agent or vicidial LOGGED_IN agent
# ; ('ACTIVE' is default)
# ; 4. prompt for user ID, if this is filled in with a filename it will ask,
# ; otherwise it will assume $extension is the user ID
# ; 5. number of digits required for user ID validation, X will allow anything,
# ; this will only be enforced if "prompt for user ID" prompt is populated
# ; 6. audio filename for invalid selection, agent not available ('invalid' is default)
# ; 7. audio filename for invalid user id re-enter prompt, agent not available
# ; 8. number of retry attempts to enter a valid user ID
# ; this will only be enforced if "prompt for user ID" prompt is populated
# ; 9. in-group to send the call to if there is no valid agent, if none is
# ; defined, then the call will be directed to 's' exten
# ; 10. audio filename before sending call to no-agent route
# ; 11. audio filename before sending call to no-agent route if agent active but
# ; not logged-in to vicidial
#
#; example in CallMenu AGI route with all options
# "agi-AGENT_route.agi,default---AGENTDIRECT---LOGGED_IN---if-u-know-ext-dial---X---invalid---please-try-again---3---TEST_IN3---pbxtransfer---outside-transfer"
#; example with 4-digit agent IDs
# exten => _XXXX,1,AGI(agi-AGENT_route.agi,default---AGENTDIRECT---ACTIVE)
#
# Copyright (C) 2013 Matt Florell <vicidial@gmail.com> LICENSE: AGPLv2
#
# changes:
# 100321-0108 - First Build
# 100704-2128 - Added several options
# 130108-1811 - Changes for Asterisk 1.8 compatibility
#
$script = 'agi-AGENT_route.agi';
$S='*';
$US='_';
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year = ($year + 1900);
$mon++;
if ($mon < 10) {$mon = "0$mon";}
if ($mday < 10) {$mday = "0$mday";}
if ($hour < 10) {$hour = "0$hour";}
if ($min < 10) {$min = "0$min";}
if ($sec < 10) {$sec = "0$sec";}
$hm = "$hour$min";
$hm = ($hm + 0);
$now_date_epoch = time();
$now_date = "$year-$mon-$mday $hour:$min:$sec";
$CLInow_date = "$year-$mon-$mday\\ $hour:$min:$sec";
$start_time=$now_date;
$CIDdate = "$mon$mday$hour$min$sec";
$tsSQLdate = "$year$mon$mday$hour$min$sec";
$SQLdate = "$year-$mon-$mday $hour:$min:$sec";
$SQLdateBEGIN = $SQLdate;
# default path to astguiclient configuration file:
$PATHconf = '/etc/astguiclient.conf';
open(conf, "$PATHconf") || die "can't open $PATHconf: $!\n";
@conf = <conf>;
close(conf);
$i=0;
foreach(@conf)
{
$line = $conf[$i];
$line =~ s/ |>|\n|\r|\t|\#.*|;.*//gi;
if ( ($line =~ /^PATHhome/) && ($CLIhome < 1) )
{$PATHhome = $line; $PATHhome =~ s/.*=//gi;}
if ( ($line =~ /^PATHlogs/) && ($CLIlogs < 1) )
{$PATHlogs = $line; $PATHlogs =~ s/.*=//gi;}
if ( ($line =~ /^PATHagi/) && ($CLIagi < 1) )
{$PATHagi = $line; $PATHagi =~ s/.*=//gi;}
if ( ($line =~ /^PATHweb/) && ($CLIweb < 1) )
{$PATHweb = $line; $PATHweb =~ s/.*=//gi;}
if ( ($line =~ /^PATHsounds/) && ($CLIsounds < 1) )
{$PATHsounds = $line; $PATHsounds =~ s/.*=//gi;}
if ( ($line =~ /^PATHmonitor/) && ($CLImonitor < 1) )
{$PATHmonitor = $line; $PATHmonitor =~ s/.*=//gi;}
if ( ($line =~ /^VARserver_ip/) && ($CLIserver_ip < 1) )
{$VARserver_ip = $line; $VARserver_ip =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_server/) && ($CLIDB_server < 1) )
{$VARDB_server = $line; $VARDB_server =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_database/) && ($CLIDB_database < 1) )
{$VARDB_database = $line; $VARDB_database =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_user/) && ($CLIDB_user < 1) )
{$VARDB_user = $line; $VARDB_user =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_pass/) && ($CLIDB_pass < 1) )
{$VARDB_pass = $line; $VARDB_pass =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_port/) && ($CLIDB_port < 1) )
{$VARDB_port = $line; $VARDB_port =~ s/.*=//gi;}
$i++;
}
if (!$VARDB_port) {$VARDB_port='3306';}
if (!$AGILOGfile) {$AGILOGfile = "$PATHlogs/agiout.$year-$mon-$mday";}
use DBI;
use Time::HiRes ('gettimeofday','usleep','sleep'); # necessary to have perl sleep command of less than one second
use Asterisk::AGI;
$AGI = new Asterisk::AGI;
$dbhA = DBI->connect("DBI:mysql:$VARDB_database:$VARDB_server:$VARDB_port", "$VARDB_user", "$VARDB_pass")
or die "Couldn't connect to database: " . DBI->errstr;
### Grab Server values from the database
$stmtA = "SELECT agi_output,local_gmt FROM servers where server_ip = '$VARserver_ip';";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
if ($sthArows > 0)
{
$AGILOG = '0';
@aryA = $sthA->fetchrow_array;
$DBagi_output = $aryA[0];
$local_gmt = $aryA[1];
if ($DBagi_output =~ /STDERR/) {$AGILOG = '1';}
if ($DBagi_output =~ /FILE/) {$AGILOG = '2';}
if ($DBagi_output =~ /BOTH/) {$AGILOG = '3';}
}
$sthA->finish();
### begin parsing run-time options ###
if (length($ARGV[0])>1)
{
if ($AGILOG) {$agi_string = "Perl Environment Dump:"; &agi_output;}
$i=0;
while ($#ARGV >= $i)
{
$args = "$args $ARGV[$i]";
if ($AGILOG) {$agi_string = "$i|$ARGV[$i]"; &agi_output;}
$i++;
}
### list of command-line array arguments:
@ARGV_vars = split(/---/, $ARGV[0]);
$did_settings = $ARGV_vars[0];
$in_group = $ARGV_vars[1];
$agent_active_filter = $ARGV_vars[2];
$user_id_prompt = $ARGV_vars[3];
$minimum_user_digits = $ARGV_vars[4];
$invalid_prompt = $ARGV_vars[5];
$invalid_reenter_prompt = $ARGV_vars[6];
$retry_attempts = $ARGV_vars[7];
$invalid_ingroup = $ARGV_vars[8];
$transfer_prompt = $ARGV_vars[9];
$transfer_logout_prompt = $ARGV_vars[10];
}
if (length($did_settings) < 1)
{$did_settings = 'default';}
if (length($in_group) < 1)
{$in_group = 'AGENTDIRECT';}
if (length($agent_active_filter) < 1)
{$agent_active_filter = 'ACTIVE';}
if (length($invalid_prompt) < 1)
{$invalid_prompt = 'invalid';}
if (length($retry_attempts) < 1)
{$retry_attempts = '3';}
if (length($minimum_user_digits) < 1)
{$minimum_user_digits = 'X';}
$pound_finish = 0;
if ($minimum_user_digits =~ /X/)
{
$minimum_user_digits = '20';
$pound_finish = 1;
}
$|=1;
while(<STDIN>)
{
chomp;
last unless length($_);
if ($AGILOG)
{
if (/^agi_(\w+)\:\s+(.*)$/)
{
$AGI{$1} = $2;
}
}
if (/^agi_context\:\s+(.*)$/) {$context = $1;}
if (/^agi_priority\:\s+(.*)$/) {$priority = $1;}
if (/^agi_uniqueid\:\s+(.*)$/) {$unique_id = $1; $uniqueid = $unique_id;}
if (/^agi_channel\:\s+(.*)$/) {$channel = $1;}
if (/^agi_extension\:\s+(.*)$/) {$extension = $1;}
if (/^agi_type\:\s+(.*)$/) {$type = $1;}
if (/^agi_callerid\:\s+(.*)$/) {$callerid = $1;}
if (/^agi_calleridname\:\s+(.*)$/) {$calleridname = $1;}
}
if ( ($callerid =~ /\".*\"/) && ( (!$calleridname) or ($calleridname =~ /unknown/) ) )
{
$calleridname = $callerid;
$calleridname =~ s/\<\d\d\d\d\d\d\d\d\d\d\>//gi;
$calleridname =~ s/\"|\" //gi;
}
$callerid =~ s/\D|\'//gi;
$calleridname =~ s/unknown|\'//gi;
if ( (!$callerid) or ($callerid =~ /unknown/) )
{$callerid = $calleridname;}
### Grab DID values from the database
$DIDs_in_system=0;
$stmtA = "SELECT did_id FROM vicidial_inbound_dids where did_pattern = '$did_settings' and did_active='Y';";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
if ($sthArows > 0)
{
@aryA = $sthA->fetchrow_array;
$did_id = $aryA[0];
$DIDs_in_system++;
}
$sthA->finish();
if ($sthArows < 1)
{
### Grab DID values from the database for a default DID pattern if there is one
$stmtA = "SELECT did_id FROM vicidial_inbound_dids where did_pattern = 'default' and did_active='Y';";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$DIDs_in_system=$sthA->rows;
if ($DIDs_in_system > 0)
{
@aryA = $sthA->fetchrow_array;
$did_id = $aryA[0];
}
$sthA->finish();
}
### Grab Server values from the database
$cbc=0;
$stmtA = "SELECT ext_context FROM servers where server_ip = '$VARserver_ip';";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
while ($sthArows > $cbc)
{
@aryA = $sthA->fetchrow_array;
$DBext_context = $aryA[0];
if ($DBext_context) {$ext_context = $DBext_context;}
$cbc++;
}
$sthA->finish();
$active_user=0;
$logged_in_user=0;
if (length($user_id_prompt) > 0)
{
$attempts=0;
while ( ($retry_attempts > $attempts) && ($active_user < 1) )
{
$attempts++;
$AGI->stream_file('sip-silence');
$AGI->stream_file('sip-silence');
$AGI->stream_file('sip-silence');
&user_id_gather;
&find_if_active_user;
if (length($invalid_reenter_prompt) > 0)
{
$user_id_prompt = $invalid_reenter_prompt;
}
}
}
else
{
&find_if_active_user;
}
if ( ($agent_active_filter =~ /LOGGED_IN/) && ($logged_in_user < 1) && ($active_user > 0) )
{
$transfer_prompt = $transfer_logout_prompt;
$active_user=0;
}
### Route call to a logged-in VICIDIAL agent
if ($active_user > 0)
{
$did_extension = "99909$S$did_id$S$in_group$S$extension$S";
}
else
{
$AGI->stream_file('sip-silence');
$AGI->stream_file('sip-silence');
$AGI->stream_file('sip-silence');
$AGI->stream_file("$invalid_prompt");
if (length($invalid_ingroup) > 1)
{
$did_extension = "99909$S$did_id$S$invalid_ingroup$S$S";
}
else
{
$did_extension = "s";
$ext_context = $context;
}
}
if (length($transfer_prompt) > 1)
{
$AGI->stream_file('sip-silence');
$AGI->stream_file('sip-silence');
$AGI->stream_file('sip-silence');
$AGI->stream_file("$transfer_prompt");
}
if ($AGILOG) {$agi_string = "exiting the AGENT app, transferring call to $did_extension @ $ext_context"; &agi_output;}
print "SET CONTEXT $ext_context\n";
$result = <STDIN>;
checkresult($result);
print "SET EXTENSION $did_extension\n";
$result = <STDIN>;
checkresult($result);
print "SET PRIORITY 1\n";
$result = <STDIN>;
checkresult($result);
exit;
######### SUBROUTINES #################
sub find_if_active_user
{
### find out if user exists and is active in system
$stmtA = "SELECT count(*) FROM vicidial_users where user='$extension' and active='Y';";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
if ($sthArows > 0)
{
@aryA = $sthA->fetchrow_array;
$active_user = $aryA[0];
}
$sthA->finish();
if ($agent_active_filter =~ /LOGGED_IN/)
{
$stmtA = "SELECT count(*) FROM vicidial_live_agents where user='$extension';";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
if ($sthArows > 0)
{
@aryA = $sthA->fetchrow_array;
$logged_in_user = $aryA[0];
}
$sthA->finish();
}
}
##### BEGIN collect the user id number ######################################
sub user_id_gather
{
################################################################################
# please enter the agent id followed by the pound key
$interrupt_digit='';
$interrupt_digit = $AGI->stream_file("$user_id_prompt",'0123456789');
print STDERR "interrupt_digit |$interrupt_digit|\n";
$digits_being_entered=1;
$extension='';
if ($interrupt_digit > 0)
{
# if ($interrupt_digit == 35) {$interrupt_digit='#';}
# if ($interrupt_digit == 42) {$interrupt_digit='*';}
if ($interrupt_digit == 48) {$interrupt_digit=0;}
if ($interrupt_digit == 49) {$interrupt_digit=1;}
if ($interrupt_digit == 50) {$interrupt_digit=2;}
if ($interrupt_digit == 51) {$interrupt_digit=3;}
if ($interrupt_digit == 52) {$interrupt_digit=4;}
if ($interrupt_digit == 53) {$interrupt_digit=5;}
if ($interrupt_digit == 54) {$interrupt_digit=6;}
if ($interrupt_digit == 55) {$interrupt_digit=7;}
if ($interrupt_digit == 56) {$interrupt_digit=8;}
if ($interrupt_digit == 57) {$interrupt_digit=9;}
$extension=$interrupt_digit;
}
$digit_loop_counter=0;
while ( ($digits_being_entered > 0) && ($digit_loop_counter < $minimum_user_digits) )
{
$digit = chr($AGI->wait_for_digit('10000')); # wait 10 seconds for input, until the pound key is pressed or X digits
if ($digit =~ /\d/)
{
$extension = "$extension$digit";
print STDERR "digit |$digit| extension |$extension|\n";
# $AGI->say_digits("$digit");
undef $digit;
}
else
{
if ($pound_finish > 0)
{$digits_being_entered=0;}
}
$digit_loop_counter++;
}
$extension =~ s/\D//gi;
if ($extension > 0) {print STDERR "digit collection done|$digit| extension |$extension|\n";}
}
##### END collect the user id number ###############################################
sub checkresult
{
$pass=0; $fail=0;
my ($res) = @_;
my $retval;
$tests++;
chomp $res;
if ($res =~ /^200/)
{
$res =~ /result=(-?\d+)/;
if (!length($1))
{
# print STDERR "FAIL ($res)\n";
$fail++;
}
else
{
# print STDERR "PASS ($1)\n";
$pass++;
}
}
else
{
# print STDERR "FAIL (unexpected result '$res')\n";
$fail++;
}
}
sub agi_output
{
if ($AGILOG >=2)
{
### open the log file for writing ###
open(Lout, ">>$AGILOGfile")
|| die "Can't open $AGILOGfile: $!\n";
print Lout "$now_date|$script|$agi_string\n";
close(Lout);
}
### send to STDERR writing ###
if ( ($AGILOG == '1') || ($AGILOG == '3') )
{print STDERR "$now_date|$script|$agi_string\n";}
$agi_string='';
}
@@ -0,0 +1,494 @@
#!/usr/bin/perl
#
# agi-CALLCARD_dial_in.agi version 2.6
#
# This script is designed to allow customers the ability to dial in to a DID and
# enter in a PIN number that would look up their account and send on to a
# specified in-group if pin is valid
#
# user is prompted for their PIN number only (no pound '#' required)
# then the user is forwarded to the active vicidial agent session
#
# can be added as an AGI to a Call Menu, or as below:
# Call Menu Entry: "agi-CALLCARD_dial_in.agi,en---3---callcard_pin_enter---callcard_pin_invalid---default"
# ;agent dial-in:
#exten => 2346,1,Answer ; Answer the line
#exten => 2346,2,AGI(agi-CALLCARD_dial_in.agi,en---3---callcard_pin_enter---callcard_pin_invalid---default)
#exten => 2346,3,Hangup
#
# ; Below are the parameters needed for the script to be run properly
# ; 1. language
# ; - en - English <default>
# ; - es - Spanish
# ; - fr - French, not fully supported
# ; 2. maximum attempts before hangup:
# ; - 3 <default>
# ; 3. audio file enter pin number prompt:
# ; - callcard_pin_enter <default>
# ; 4. audio file invalid pin number prompt:
# ; - callcard_pin_invalid <default>
# ; 5. DID pattern to use for in-group forwarding settings:
# ; - default <default>
# ; - USE-ORIGINATING-DID - this option will look up the DID the customer dialed in on and use those settings
#
# other audio files used:
# - pin-number-accepted
# - vm-youhave
# - callcard_minutes_left
#
# Copyright (C) 2013 Matt Florell <vicidial@gmail.com> LICENSE: AGPLv2
#
# CHANGELOG
# 100311-1135 - First build
# 130108-1812 - Changes for Asterisk 1.8 compatibility
#
&get_time_now;
$start_epoch = $now_date_epoch;
$script = 'agi-CALLCARD_dial_in.agi';
$now_date_epoch = time();
$now_date = "$year-$mon-$mday $hour:$min:$sec";
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year = ($year + 1900);
$mon++;
if ($mon < 10) {$mon = "0$mon";}
if ($mday < 10) {$mday = "0$mday";}
if ($hour < 10) {$hour = "0$hour";}
if ($min < 10) {$min = "0$min";}
if ($sec < 10) {$sec = "0$sec";}
$US='_';
# default path to astguiclient configuration file:
$PATHconf = '/etc/astguiclient.conf';
open(conf, "$PATHconf") || die "can't open $PATHconf: $!\n";
@conf = <conf>;
close(conf);
$i=0;
foreach(@conf)
{
$line = $conf[$i];
$line =~ s/ |>|\n|\r|\t|\#.*|;.*//gi;
if ( ($line =~ /^PATHlogs/) && ($CLIlogs < 1) )
{$PATHlogs = $line; $PATHlogs =~ s/.*=//gi;}
if ( ($line =~ /^PATHsounds/) && ($CLIsounds < 1) )
{$PATHsounds = $line; $PATHsounds =~ s/.*=//gi;}
if ( ($line =~ /^PATHmonitor/) && ($CLImonitor < 1) )
{$PATHmonitor = $line; $PATHmonitor =~ s/.*=//gi;}
if ( ($line =~ /^VARserver_ip/) && ($CLIserver_ip < 1) )
{$VARserver_ip = $line; $VARserver_ip =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_server/) && ($CLIDB_server < 1) )
{$VARDB_server = $line; $VARDB_server =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_database/) && ($CLIDB_database < 1) )
{$VARDB_database = $line; $VARDB_database =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_user/) && ($CLIDB_user < 1) )
{$VARDB_user = $line; $VARDB_user =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_pass/) && ($CLIDB_pass < 1) )
{$VARDB_pass = $line; $VARDB_pass =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_port/) && ($CLIDB_port < 1) )
{$VARDB_port = $line; $VARDB_port =~ s/.*=//gi;}
$i++;
}
if (!$VARDB_port) {$VARDB_port='3306';}
if (!$AGILOGfile) {$AGILOGfile = "$PATHlogs/ivrout.$year-$mon-$mday";}
use DBI;
use Asterisk::AGI;
$AGI = new Asterisk::AGI;
$dbhA = DBI->connect("DBI:mysql:$VARDB_database:$VARDB_server:$VARDB_port", "$VARDB_user", "$VARDB_pass")
or die "Couldn't connect to database: " . DBI->errstr;
# defaults:
$language = 'en';
$maximum_attempts = '3';
$pin_enter_audio = 'callcard_pin_enter';
$pin_invalid_audio = 'callcard_pin_invalid';
$did_ingroup_settings = 'default';
### begin parsing run-time options ###
if (length($ARGV[0])>1)
{
if ($AGILOG) {$agi_string = "Perl Environment Dump:"; &agi_output;}
$i=0;
while ($#ARGV >= $i)
{
$args = "$args $ARGV[$i]";
if ($AGILOG) {$agi_string = "$i|$ARGV[$i]"; &agi_output;}
$i++;
}
### list of command-line array arguments:
@ARGV_vars = split(/---/, $ARGV[0]);
if (length($ARGV_vars[0]) > 0) {$language = $ARGV_vars[0];}
if (length($ARGV_vars[1]) > 0) {$maximum_attempts = $ARGV_vars[1];}
if (length($ARGV_vars[2]) > 0) {$pin_enter_audio = $ARGV_vars[2];}
if (length($ARGV_vars[3]) > 0) {$pin_invalid_audio = $ARGV_vars[3];}
if (length($ARGV_vars[4]) > 0) {$did_ingroup_settings = $ARGV_vars[4];}
}
if ($language =~ /es/)
{
$audio_suffix='_es';
$AGI->exec("EXEC Set(CHANNEL(language)=$language)");
# $AGI->exec("SetLanguage($language)");
$AGI->stream_file('sip-silence');
$AGI->stream_file('sip-silence');
}
if ($language =~ /fr/)
{
$audio_suffix='_fr';
$AGI->exec("EXEC Set(CHANNEL(language)=$language)");
# $AGI->exec("SetLanguage($language)");
$AGI->stream_file('sip-silence');
$AGI->stream_file('sip-silence');
}
if ($language !~ /es|fr/) {$audio_suffix='';}
$|=1;
while(<STDIN>)
{
chomp;
last unless length($_);
if ($V)
{
if (/^agi_(\w+)\:\s+(.*)$/)
{
$AGI{$1} = $2;
}
}
if (/^agi_uniqueid\:\s+(.*)$/) {$unique_id = $1; $uniqueid=$unique_id}
if (/^agi_extension\:\s+(.*)$/) {$extension = $1;}
if (/^agi_channel\:\s+(.*)$/) {$channel = $1;}
if (/^agi_callerid\:\s+(.*)$/) {$callerid = $1;}
if (/^agi_calleridname\:\s+(.*)$/) {$calleridname = $1;}
}
if ( ($callerid =~ /\".*\"/) && ( (!$calleridname) or ($calleridname =~ /unknown/) ) )
{
$calleridname = $callerid;
$calleridname =~ s/\<\d\d\d\d\d\d\d\d\d\d\>//gi;
$calleridname =~ s/\"|\" //gi;
}
$callerid =~ s/\D|\'//gi;
$calleridname =~ s/unknown|\'//gi;
if ( (!$callerid) or ($callerid =~ /unknown/) )
{$callerid = $calleridname;}
$phone_number=$callerid;
### Grab Server values from the database
$stmtA = "SELECT ext_context,agi_output FROM servers where server_ip = '$VARserver_ip';";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
$rec_count=0;
while ($sthArows > $rec_count)
{
@aryA = $sthA->fetchrow_array;
$DBext_context = "$aryA[0]";
$DBagi_output = "$aryA[1]";
if ($DBext_context) {$ext_context = $DBext_context;}
if ($DBagi_output =~ /STDERR/) {$AGILOG = '1';}
if ($DBagi_output =~ /FILE/) {$AGILOG = '2';}
if ($DBagi_output =~ /BOTH/) {$AGILOG = '3';}
$rec_count++;
}
$sthA->finish();
$AGI->stream_file('sip-silence');
$AGI->stream_file('sip-silence');
$AGI->stream_file('sip-silence');
$entry_chances=0;
$VALID_login=0;
$ACTIVE_login=0;
$VDADremDIALstr='';
while ( ($entry_chances < $maximum_attempts) && ($VALID_login < 1) )
{
$entry_chances++;
&user_password_gather;
###### Check userID and password against vicidial_users table
$stmtA = "SELECT count(*) FROM callcard_accounts where pin='$PIN_number' and status IN('ACTIVE','USED') and balance_minutes > 0;";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
if ($sthArows > 0)
{
@aryA = $sthA->fetchrow_array;
$VALID_login = $aryA[0];
}
$sthA->finish();
if ($AGILOG) {$agi_string = "validate PIN: |$VALID_login|$stmtA|"; &agi_output;}
if ($VALID_login > 0)
{
$AGI->stream_file("pin-number-accepted$audio_suffix");
}
else
{
$AGI->stream_file("$pin_invalid_audio$audio_suffix");
}
}
if ($VALID_login < 1)
{
if ($AGILOG) {$agi_string = "DIAL-IN END: Invalid Pin |$VALID_login|$PIN_number|"; &agi_output;}
$AGI->stream_file("vm-goodbye$audio_suffix");
$AGI->hangup();
$dbhA->disconnect();
exit;
}
else
{
$secX = time();
$PDtarget = ($secX - 30);
($Psec,$Pmin,$Phour,$Pmday,$Pmon,$Pyear,$Pwday,$Pyday,$Pisdst) = localtime($PDtarget);
$Pyear = ($Pyear + 1900);
$Pmon++;
if ($Pmon < 10) {$Pmon = "0$Pmon";}
if ($Pmday < 10) {$Pmday = "0$Pmday";}
if ($Phour < 10) {$Phour = "0$Phour";}
if ($Pmin < 10) {$Pmin = "0$Pmin";}
if ($Psec < 10) {$Psec = "0$Psec";}
$PDtsSQLdate = "$Pyear$Pmon$Pmday$Phour$Pmin$Psec";
###### Check for the current minute balance to play back to the customer in the callcard_accounts table
$stmtA = "SELECT card_id,balance_minutes,inbound_group_id FROM callcard_accounts where pin='$PIN_number';";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
if ($sthArows > 0)
{
@aryA = $sthA->fetchrow_array;
$card_id = $aryA[0];
$balance_minutes = $aryA[1];
$inbound_group_id = $aryA[2];
}
$sthA->finish();
###### Check for the current minute balance to play back to the customer in the callcard_accounts table
$stmtA = "SELECT vdl.did_id,did_pattern FROM vicidial_did_log vdl, vicidial_inbound_dids vid where uniqueid='$uniqueid' and vdl.did_id=vid.did_id order by call_date desc limit 1;";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
if ($sthArows > 0)
{
@aryA = $sthA->fetchrow_array;
$original_did_pattern = $aryA[1];
}
$sthA->finish();
if ($did_ingroup_settings =~ /USE-ORIGINATING-DID/)
{$did_id_search = $original_did_pattern;}
else
{$did_id_search = $did_ingroup_settings;}
$did_id=0;
###### find the in-group settings to use when forwarding this call
$stmtA = "SELECT did_id FROM vicidial_inbound_dids where did_pattern='$did_id_search';";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
if ($sthArows > 0)
{
@aryA = $sthA->fetchrow_array;
$did_id = $aryA[0];
}
$sthA->finish();
##### play customer's balance to them before transferring
$AGI->stream_file("vm-youhave$audio_suffix");
$AGI->say_number("$balance_minutes");
$AGI->stream_file("callcard_minutes_left$audio_suffix");
##### update callcard_accounts_detail record for this account
$stmtA = "UPDATE callcard_accounts_detail set used_time='$now_date' where card_id='$card_id';";
$affected_rows = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "callcard_accounts_detail update: |$affected_rows|$now_date|$card_id|"; &agi_output;}
##### insert callcard_log record for this call
$stmtA = "INSERT INTO callcard_log set uniqueid='$uniqueid',card_id='$card_id',call_time='$now_date',phone_number='$phone_number',inbound_did='$original_did_pattern',balance_minutes_start='$balance_minutes';";
$affected_rows = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "callcard_log INSERT: |$affected_rows|$stmtA|"; &agi_output;}
### format the remote server dialstring to get the call to the overflow agent meetme room
$forward_extension = "99909*$did_id*$inbound_group_id";
if ($AGILOG) {$agi_string = "Customer $PIN_number sent to $forward_extension"; &agi_output;}
print "SET CONTEXT $ext_context\n";
$result = <STDIN>;
checkresult($result);
print "SET EXTENSION $forward_extension\n";
$result = <STDIN>;
checkresult($result);
print "SET PRIORITY 1\n";
$result = <STDIN>;
checkresult($result);
}
$dbhA->disconnect();
exit;
################################################################################
##### SUBROUTINES ##############################################################
################################################################################
##### BEGIN collect the pin number ######################################
sub user_password_gather
{
################################################################################
# please enter the pin number followed by the pound key
$interrupt_digit='';
$interrupt_digit = $AGI->stream_file("$pin_enter_audio$audio_suffix",'0123456789');
print STDERR "interrupt_digit |$interrupt_digit|\n";
$digits_being_entered=1;
$PIN_number='';
if ($interrupt_digit > 0)
{
# if ($interrupt_digit == 35) {$interrupt_digit='#';}
# if ($interrupt_digit == 42) {$interrupt_digit='*';}
if ($interrupt_digit == 48) {$interrupt_digit=0;}
if ($interrupt_digit == 49) {$interrupt_digit=1;}
if ($interrupt_digit == 50) {$interrupt_digit=2;}
if ($interrupt_digit == 51) {$interrupt_digit=3;}
if ($interrupt_digit == 52) {$interrupt_digit=4;}
if ($interrupt_digit == 53) {$interrupt_digit=5;}
if ($interrupt_digit == 54) {$interrupt_digit=6;}
if ($interrupt_digit == 55) {$interrupt_digit=7;}
if ($interrupt_digit == 56) {$interrupt_digit=8;}
if ($interrupt_digit == 57) {$interrupt_digit=9;}
$PIN_number=$interrupt_digit;
}
$digit_loop_counter=0;
while ( ($digits_being_entered > 0) && ($digit_loop_counter < 8) )
{
$digit = chr($AGI->wait_for_digit('10000')); # wait 10 seconds for input, until the pound key is pressed or 8 digits
if ($digit =~ /\d/)
{
$PIN_number = "$PIN_number$digit";
print STDERR "digit |$digit| PIN_number |$PIN_number|\n";
# $AGI->say_digits("$digit");
undef $digit;
}
else
{
$digits_being_entered=0;
}
$digit_loop_counter++;
}
$PIN_number =~ s/\D//gi;
if ($PIN_number) {print STDERR "digit collection done|$digit| PIN_number |$PIN_number|\n";}
}
##### END collect the pin number ###############################################
sub get_time_now #get the current date and time and epoch for logging call lengths and datetimes
{
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year = ($year + 1900);
$mon++;
if ($mon < 10) {$mon = "0$mon";}
if ($mday < 10) {$mday = "0$mday";}
if ($hour < 10) {$hour = "0$hour";}
if ($min < 10) {$min = "0$min";}
if ($sec < 10) {$sec = "0$sec";}
$now_date_epoch = time();
$now_date = "$year-$mon-$mday $hour:$min:$sec";
$filedate = "$year$mon$mday$hour$min$sec";
}
sub checkresult
{
my ($res) = @_;
my $retval;
$tests++;
chomp $res;
if ($res =~ /^200/) {
$res =~ /result=(-?\d+)/;
if (!length($1))
{
print STDERR "FAIL ($res)\n";
$fail++;
}
else
{
print STDERR "PASS ($1)\n";
$pass++;
}
}
else
{
print STDERR "FAIL (unexpected result '$res')\n";
$fail++;
}
}
sub agi_output
{
if ($AGILOG >=2)
{
### open the log file for writing ###
open(Lout, ">>$AGILOGfile")
|| die "Can't open $AGILOGfile: $!\n";
print Lout "$now_date|$script|$agi_string\n";
close(Lout);
}
### send to STDERR writing ###
if ( ($AGILOG == '1') || ($AGILOG == '3') )
{print STDERR "$now_date|$script|$agi_string\n";}
$agi_string='';
}
sub leading_zero($)
{
$_ = $_[0];
s/^(\d)$/0$1/;
s/^(\d\d)$/0$1/;
return $_;
} # End of the leading_zero() routine.
@@ -0,0 +1,261 @@
#!/usr/bin/perl
#
# agi-CANADA_PRI_CIDname.agi version 0.1
#
# Runs after call goes through IAX loop to send out custom CallerIDname on
# Canadian PRIs that allow dynamic setting of CIDname with call initiation
#
# You need to have lines similar to those below in your extensions.conf file:
#
# ;outbound VICIDIAL calls:
#exten => _91NXXNXXXXXX,1,AGI(call_log.agi,${EXTEN})
#exten => _91NXXNXXXXXX,2,AGI(agi-CANADA_PRI_CIDname.agi,${EXTEN})
#exten => _91NXXNXXXXXX,3,Dial(${TRUNKX}/${EXTEN:1},55,tTo)
#exten => _91NXXNXXXXXX,4,Hangup
#
# Copyright (C) 2009 Matt Florell <vicidial@gmail.com> LICENSE: AGPLv2
#
# changes:
# 90305-2136 - First build
#
$script = 'agi-CANADA_PRI_CIDname.agi';
$AGILOG=0;
$FD=0; # force debug for testing
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year = ($year + 1900);
$mon++;
if ($mon < 10) {$mon = "0$mon";}
if ($mday < 10) {$mday = "0$mday";}
if ($hour < 10) {$hour = "0$hour";}
if ($min < 10) {$min = "0$min";}
if ($sec < 10) {$sec = "0$sec";}
$now_date_epoch = time();
$now_date = "$year-$mon-$mday $hour:$min:$sec";
use Asterisk::AGI;
$AGI = new Asterisk::AGI;
### begin parsing run-time options ###
if (length($ARGV[0])>1)
{
if ($AGILOG) {$agi_string = "Perl Environment Dump:"; &agi_output;}
$i=0;
while ($#ARGV >= $i)
{
$args = "$args $ARGV[$i]";
if ($AGILOG) {$agi_string = "$i|$ARGV[$i]"; &agi_output;}
$i++;
}
}
# default path to astguiclient configuration file:
$PATHconf = '/etc/astguiclient.conf';
open(conf, "$PATHconf") || die "can't open $PATHconf: $!\n";
@conf = <conf>;
close(conf);
$i=0;
foreach(@conf)
{
$line = $conf[$i];
$line =~ s/ |>|\n|\r|\t|\#.*|;.*//gi;
if ( ($line =~ /^PATHhome/) && ($CLIhome < 1) )
{$PATHhome = $line; $PATHhome =~ s/.*=//gi;}
if ( ($line =~ /^PATHlogs/) && ($CLIlogs < 1) )
{$PATHlogs = $line; $PATHlogs =~ s/.*=//gi;}
if ( ($line =~ /^PATHagi/) && ($CLIagi < 1) )
{$PATHagi = $line; $PATHagi =~ s/.*=//gi;}
if ( ($line =~ /^PATHweb/) && ($CLIweb < 1) )
{$PATHweb = $line; $PATHweb =~ s/.*=//gi;}
if ( ($line =~ /^PATHsounds/) && ($CLIsounds < 1) )
{$PATHsounds = $line; $PATHsounds =~ s/.*=//gi;}
if ( ($line =~ /^PATHmonitor/) && ($CLImonitor < 1) )
{$PATHmonitor = $line; $PATHmonitor =~ s/.*=//gi;}
if ( ($line =~ /^VARserver_ip/) && ($CLIserver_ip < 1) )
{$VARserver_ip = $line; $VARserver_ip =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_server/) && ($CLIDB_server < 1) )
{$VARDB_server = $line; $VARDB_server =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_database/) && ($CLIDB_database < 1) )
{$VARDB_database = $line; $VARDB_database =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_user/) && ($CLIDB_user < 1) )
{$VARDB_user = $line; $VARDB_user =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_pass/) && ($CLIDB_pass < 1) )
{$VARDB_pass = $line; $VARDB_pass =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_port/) && ($CLIDB_port < 1) )
{$VARDB_port = $line; $VARDB_port =~ s/.*=//gi;}
$i++;
}
if (!$VARDB_port) {$VARDB_port='3306';}
if (!$AGILOGfile) {$AGILOGfile = "$PATHlogs/agiout.$year-$mon-$mday";}
if (!$DIDLOGfile) {$DIDLOGfile = "$PATHlogs/didin.$year-$mon-$mday";}
$|=1;
while(<STDIN>)
{
chomp;
last unless length($_);
if ($AGILOG)
{
if (/^agi_(\w+)\:\s+(.*)$/)
{
$AGI{$1} = $2;
}
}
if (/^agi_extension\:\s+(.*)$/) {$extension = $1;}
if (/^agi_callerid\:\s+(.*)$/) {$callerid = $1;}
if (/^agi_calleridname\:\s+(.*)$/) {$calleridname = $1;}
}
if ( ($callerid =~ /\".*\"/) && ( (!$calleridname) or ($calleridname =~ /unknown/) ) )
{
$calleridname = $callerid;
$calleridname =~ s/\<\d\d\d\d\d\d\d\d\d\d\>//gi;
$calleridname =~ s/\"|\" //gi;
}
$callerid =~ s/\D//gi;
$calleridname =~ s/unknown//gi;
if ( (!$callerid) or ($callerid =~ /unknown/) )
{$callerid = $calleridname;}
if (length($callerid)>0) {$phone_number = $callerid;}
else {$phone_number = '';}
if (length($calleridname)>0) {$VLcomments = $calleridname;}
else {$VLcomments = '';}
foreach $i (sort keys %AGI)
{
if ($AGILOG) {$agi_string = " -- $i = $AGI{$i}"; &agi_output;}
}
if ( (length($callerid)>20) && ($callerid =~ /\"\S\S\S\S\S\S\S\S\S\S\S\S\S\S\S\S\S\S/) )
{
$calleridname =~ s/^\"//gi;
$calleridname =~ s/\".*$//gi;
}
use DBI;
use Time::HiRes ('gettimeofday','usleep','sleep'); # necessary to have perl sleep command of less than one second
$dbhA = DBI->connect("DBI:mysql:$VARDB_database:$VARDB_server:$VARDB_port", "$VARDB_user", "$VARDB_pass")
or die "Couldn't connect to database: " . DBI->errstr;
### Grab Server values from the database
$stmtA = "SELECT agi_output,local_gmt FROM servers where server_ip = '$VARserver_ip';";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
$rec_count=0;
while ($sthArows > $rec_count)
{
$AGILOG = '0';
@aryA = $sthA->fetchrow_array;
$DBagi_output = "$aryA[0]";
$local_gmt = "$aryA[1]";
if ($DBagi_output =~ /STDERR/) {$AGILOG = '1';}
if ($DBagi_output =~ /FILE/) {$AGILOG = '2';}
if ($DBagi_output =~ /BOTH/) {$AGILOG = '3';}
$rec_count++;
}
$sthA->finish();
### Grab Server values from the database
$stmtA = "SELECT cmd_line_g,cmd_line_h,cmd_line_i,cmd_line_j FROM vicidial_manager where callerid = '$calleridname' limit 1;";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
if ($sthArows > 0)
{
@aryA = $sthA->fetchrow_array;
$sthA->finish();
$account_check = "|$aryA[0]|$aryA[1]|$aryA[2]|$aryA[3]";
if ($account_check =~ /usegroupalias=1/)
{
@data = split(/Account: /,$account_check);
$data[1] =~ s/\|.*//gi;
$account = $data[1];
### Grab callerIDname and number from the database
$stmtA = "SELECT caller_id_name,caller_id_number FROM groups_alias where group_alias_id = '$account' limit 1;";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
if ($sthArows > 0)
{
@aryA = $sthA->fetchrow_array;
$sthA->finish();
$caller_id_name = "$aryA[0]";
$caller_id_number = "$aryA[1]";
### set the callerid to the ACQS value(calleridname)
## use these two lines for Asterisk 1.2 tree
$newcallerid = "\"$caller_id_name <$caller_id_number>\"";
$AGI->set_callerid($newcallerid);
## use these two lines for Asterisk 1.0 tree
# print "SET CALLERID \"$calleridname\" <0000000000>\n";
# print "SET CALLERIDNAME \"$calleridname\"\n";
print STDERR "Setting CID \"$newcallerid\"\n";
checkresult($result);
if ($AGILOG) {$agi_string = "callerID changed: newcallerid; &agi_output";}
if ($FD > 0) {print STDERR "callerID changed: newcallerid\n";}
if ($AGILOG) {$agi_string = "AGI Variables: |$extension|$areacode|$callerid|$calleridname|newcallerid|"; &agi_output;}
}
}
}
exit;
sub checkresult {
my ($res) = @_;
my $retval;
$tests++;
chomp $res;
if ($res =~ /^200/) {
$res =~ /result=(-?\d+)/;
if (!length($1)) {
# print STDERR "FAIL ($res)\n";
$fail++;
} else {
# print STDERR "PASS ($1)\n";
$pass++;
}
} else {
# print STDERR "FAIL (unexpected result '$res')\n";
$fail++;
}
}
sub agi_output
{
if ($AGILOG >=2)
{
### open the log file for writing ###
open(Lout, ">>$AGILOGfile")
|| die "Can't open $AGILOGfile: $!\n";
print Lout "$now_date|$script|$agi_string\n";
close(Lout);
}
### send to STDERR writing ###
if ( ($AGILOG == '1') || ($AGILOG == '3') )
{print STDERR "$now_date|$script|$agi_string\n";}
$agi_string='';
}
@@ -0,0 +1,943 @@
#!/usr/bin/perl
#
# agi-DID_route.agi version 2.10
#
# runs when a call comes into an inbound context on a trunk. This script will
# send the calls to various places depending on the settings for each DID.
#
# You need to put a line similar to this below in your extensions.conf file in
# the context for where incoming calls go from your trunks (for example, the
# trunkinbound context which is present in the default dialplan):
#
#[trunkinbound]
# ;inbound DID catch-all:
#exten => _X.,1,AGI(agi-DID_route.agi)
#
# Copyright (C) 2015 Matt Florell <vicidial@gmail.com> LICENSE: AGPLv2
#
# changes:
# 81007-0324 - First Build
# 90115-0645 - Activated AGENT call routing
# 90117-0756 - Added vicidial_did_log logging to database
# 90214-0217 - Added 'default' did option, to send all non-matching calls to, enforced did_active setting
# 90507-1145 - Added CALLMENU option
# 90825-2252 - Changed PHONE to route using dialplan number
# 91122-0743 - Added QueueMetrics logging as DID INFO entries
# 100116-0631 - Logging bug fixes
# 100123-1423 - Added record call DID option
# 100805-0943 - Added Filter Phone Group alternate routing feature and CID number cleanup
# 110822-1210 - Added did_agent_log optional logging
# 120314-1112 - Fixed small filter bug
# 120430-2214 - Converted call to Monitor app to be asterisk 1.8 compatible
# 130108-1810 - Changes for Asterisk 1.8 compatibility
# 130925-1819 - Added variable filter to prevent DID SQL injection attack
# 130926-1549 - Added new queue_log IVR entries for QM
# 140126-1138 - Added VMAIL_NO_INST options
# 140404-0738 - Added DNC filter options and URL filter DID redirect option
# 140622-0240 - Added no-agent in-group redirect and pre-filter phone group redirect features
# 140811-2151 - Added ability to filter by DNC areacodes if filter_inbound_number set to a DNC option
# 141110-1417 - Fixed issue with multiple filtering options enabled
# 150112-1426 - Added no-agent last_update_time check to match in-group queries
#
$script = 'agi-DID_route.agi';
$dtmf_silent_prefix = '7';
$at='@';
$S='*';
$US='_';
$route_type='DID';
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year = ($year + 1900);
$mon++;
if ($mon < 10) {$mon = "0$mon";}
if ($mday < 10) {$mday = "0$mday";}
if ($hour < 10) {$hour = "0$hour";}
if ($min < 10) {$min = "0$min";}
if ($sec < 10) {$sec = "0$sec";}
$hm = "$hour$min";
$hm = ($hm + 0);
$now_date_epoch = time();
$now_date = "$year-$mon-$mday $hour:$min:$sec";
$CLInow_date = "$year-$mon-$mday\\ $hour:$min:$sec";
$start_time=$now_date;
$CIDdate = "$mon$mday$hour$min$sec";
$tsSQLdate = "$year$mon$mday$hour$min$sec";
$SQLdate = "$year-$mon-$mday $hour:$min:$sec";
$SQLdateBEGIN = $SQLdate;
# default path to astguiclient configuration file:
$PATHconf = '/etc/astguiclient.conf';
open(conf, "$PATHconf") || die "can't open $PATHconf: $!\n";
@conf = <conf>;
close(conf);
$i=0;
foreach(@conf)
{
$line = $conf[$i];
$line =~ s/ |>|\n|\r|\t|\#.*|;.*//gi;
if ( ($line =~ /^PATHhome/) && ($CLIhome < 1) )
{$PATHhome = $line; $PATHhome =~ s/.*=//gi;}
if ( ($line =~ /^PATHlogs/) && ($CLIlogs < 1) )
{$PATHlogs = $line; $PATHlogs =~ s/.*=//gi;}
if ( ($line =~ /^PATHagi/) && ($CLIagi < 1) )
{$PATHagi = $line; $PATHagi =~ s/.*=//gi;}
if ( ($line =~ /^PATHweb/) && ($CLIweb < 1) )
{$PATHweb = $line; $PATHweb =~ s/.*=//gi;}
if ( ($line =~ /^PATHsounds/) && ($CLIsounds < 1) )
{$PATHsounds = $line; $PATHsounds =~ s/.*=//gi;}
if ( ($line =~ /^PATHmonitor/) && ($CLImonitor < 1) )
{$PATHmonitor = $line; $PATHmonitor =~ s/.*=//gi;}
if ( ($line =~ /^VARserver_ip/) && ($CLIserver_ip < 1) )
{$VARserver_ip = $line; $VARserver_ip =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_server/) && ($CLIDB_server < 1) )
{$VARDB_server = $line; $VARDB_server =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_database/) && ($CLIDB_database < 1) )
{$VARDB_database = $line; $VARDB_database =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_user/) && ($CLIDB_user < 1) )
{$VARDB_user = $line; $VARDB_user =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_pass/) && ($CLIDB_pass < 1) )
{$VARDB_pass = $line; $VARDB_pass =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_port/) && ($CLIDB_port < 1) )
{$VARDB_port = $line; $VARDB_port =~ s/.*=//gi;}
$i++;
}
if (!$VARDB_port) {$VARDB_port='3306';}
if (!$AGILOGfile) {$AGILOGfile = "$PATHlogs/agiout.$year-$mon-$mday";}
if (!$DIDLOGfile) {$DIDLOGfile = "$PATHlogs/didin.$year-$mon-$mday";}
use DBI;
use Time::HiRes ('gettimeofday','usleep','sleep'); # necessary to have perl sleep command of less than one second
use Asterisk::AGI;
$AGI = new Asterisk::AGI;
$dbhA = DBI->connect("DBI:mysql:$VARDB_database:$VARDB_server:$VARDB_port", "$VARDB_user", "$VARDB_pass")
or die "Couldn't connect to database: " . DBI->errstr;
### Grab Server values from the database
$stmtA = "SELECT agi_output,local_gmt,asterisk_version FROM servers where server_ip = '$VARserver_ip';";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
if ($sthArows > 0)
{
$AGILOG = '0';
@aryA = $sthA->fetchrow_array;
$DBagi_output = $aryA[0];
$local_gmt = $aryA[1];
$asterisk_version = $aryA[2];
if ($DBagi_output =~ /STDERR/) {$AGILOG = '1';}
if ($DBagi_output =~ /FILE/) {$AGILOG = '2';}
if ($DBagi_output =~ /BOTH/) {$AGILOG = '3';}
}
$sthA->finish();
#############################################
##### START SYSTEM SETTINGS LOOKUP #####
$stmtA = "SELECT enable_queuemetrics_logging,queuemetrics_server_ip,queuemetrics_dbname,queuemetrics_login,queuemetrics_pass,queuemetrics_log_id,queuemetrics_eq_prepend,did_agent_log,alt_log_server_ip,alt_log_dbname,alt_log_login,alt_log_pass,tables_use_alt_log_db FROM system_settings;";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
if ($sthArows > 0)
{
@aryA = $sthA->fetchrow_array;
$enable_queuemetrics_logging = $aryA[0];
$queuemetrics_server_ip = $aryA[1];
$queuemetrics_dbname = $aryA[2];
$queuemetrics_login= $aryA[3];
$queuemetrics_pass = $aryA[4];
$queuemetrics_log_id = $aryA[5];
$queuemetrics_eq_prepend = $aryA[6];
$did_agent_log = $aryA[7];
$alt_log_server_ip = $aryA[8];
$alt_log_dbname = $aryA[9];
$alt_log_login = $aryA[10];
$alt_log_pass = $aryA[11];
$tables_use_alt_log_db = $aryA[12];
}
$sthA->finish();
##### END SYSTEM SETTINGS LOOKUP #####
###########################################
$|=1;
while(<STDIN>)
{
chomp;
last unless length($_);
if ($AGILOG)
{
if (/^agi_(\w+)\:\s+(.*)$/)
{
$AGI{$1} = $2;
}
}
if (/^agi_uniqueid\:\s+(.*)$/) {$unique_id = $1; $uniqueid = $unique_id;}
if (/^agi_channel\:\s+(.*)$/) {$channel = $1;}
if (/^agi_extension\:\s+(.*)$/) {$extension = $1;}
if (/^agi_type\:\s+(.*)$/) {$type = $1;}
if (/^agi_callerid\:\s+(.*)$/) {$callerid = $1;}
if (/^agi_calleridname\:\s+(.*)$/) {$calleridname = $1;}
}
if ( ($callerid =~ /\".*\"/) && ( (!$calleridname) or ($calleridname =~ /unknown/) ) )
{
$calleridname = $callerid;
$calleridname =~ s/\<\d\d\d\d\d\d\d\d\d\d\>//gi;
$calleridname =~ s/\"|\" //gi;
}
$callerid =~ s/\D|\'//gi;
$calleridname =~ s/unknown|\'//gi;
if ( (!$callerid) or ($callerid =~ /unknown/) )
{$callerid = $calleridname;}
$callerid =~ s/\'|\"|\\\\|\\\|\\|\\;|\\\;|\;|;//gi;
$calleridname =~ s/\'|\"|\\\\|\\\|\\|\\;|\\\;|\;|;//gi;
$extension =~ s/\'|\"|\\\\|\\\|\\|\\;|\\\;|\;|;//gi;
### Grab DID values from the database
$DIDs_in_system=0;
$stmtA = "SELECT did_id,did_pattern,did_description,did_active,did_route,extension,exten_context,voicemail_ext,phone,server_ip,user,user_unavailable_action,user_route_settings_ingroup,menu_id,record_call,filter_inbound_number,filter_phone_group_id,filter_url,filter_action,filter_extension,filter_exten_context,filter_voicemail_ext,filter_phone,filter_server_ip,filter_user,filter_user_unavailable_action,filter_user_route_settings_ingroup,filter_group_id,filter_call_handle_method,filter_agent_search_method,filter_list_id,filter_campaign_id,filter_phone_code,filter_menu_id,filter_clean_cid_number,group_id,filter_dnc_campaign,filter_url_did_redirect,no_agent_ingroup_redirect,no_agent_ingroup_id,no_agent_ingroup_extension,pre_filter_phone_group_id,pre_filter_extension FROM vicidial_inbound_dids where did_pattern = '$extension' and did_active='Y';";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
if ($sthArows > 0)
{
@aryA = $sthA->fetchrow_array;
$did_id = $aryA[0];
$did_pattern = $aryA[1];
$did_description = $aryA[2];
$did_active = $aryA[3];
$did_route = $aryA[4];
$did_extension = $aryA[5];
$exten_context = $aryA[6];
$voicemail_ext = $aryA[7];
$phone = $aryA[8];
$did_server_ip = $aryA[9];
$user = $aryA[10];
$user_unavailable_action = $aryA[11];
$user_route_settings_ingroup = $aryA[12];
$menu_id = $aryA[13];
$record_call = $aryA[14];
$filter_inbound_number = $aryA[15];
$filter_phone_group_id = $aryA[16];
$filter_url = $aryA[17];
$filter_action = $aryA[18];
$filter_extension = $aryA[19];
$filter_exten_context = $aryA[20];
$filter_voicemail_ext = $aryA[21];
$filter_phone = $aryA[22];
$filter_server_ip = $aryA[23];
$filter_user = $aryA[24];
$filter_user_unavailable_action = $aryA[25];
$filter_user_route_settings_ingroup = $aryA[26];
$filter_group_id = $aryA[27];
$filter_call_handle_method = $aryA[28];
$filter_agent_search_method = $aryA[29];
$filter_list_id = $aryA[30];
$filter_campaign_id = $aryA[31];
$filter_phone_code = $aryA[32];
$filter_menu_id = $aryA[33];
$filter_clean_cid_number = $aryA[34];
$group_id = $aryA[35];
$filter_dnc_campaign = $aryA[36];
$filter_url_did_redirect = $aryA[37];
$no_agent_ingroup_redirect = $aryA[38];
$no_agent_ingroup_id = $aryA[39];
$no_agent_ingroup_extension = $aryA[40];
$pre_filter_phone_group_id = $aryA[41];
$pre_filter_extension = $aryA[42];
$DIDs_in_system++;
}
$sthA->finish();
if ($sthArows < 1)
{
### Grab DID values from the database for a default DID pattern if there is one
$stmtA = "SELECT did_id,did_pattern,did_description,did_active,did_route,extension,exten_context,voicemail_ext,phone,server_ip,user,user_unavailable_action,user_route_settings_ingroup,menu_id,record_call,filter_inbound_number,filter_phone_group_id,filter_url,filter_action,filter_extension,filter_exten_context,filter_voicemail_ext,filter_phone,filter_server_ip,filter_user,filter_user_unavailable_action,filter_user_route_settings_ingroup,filter_group_id,filter_call_handle_method,filter_agent_search_method,filter_list_id,filter_campaign_id,filter_phone_code,filter_menu_id,filter_clean_cid_number,group_id,filter_dnc_campaign,filter_url_did_redirect,no_agent_ingroup_redirect,no_agent_ingroup_id,no_agent_ingroup_extension,pre_filter_phone_group_id,pre_filter_extension FROM vicidial_inbound_dids where did_pattern = 'default' and did_active='Y';";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$DIDs_in_system=$sthA->rows;
if ($DIDs_in_system > 0)
{
@aryA = $sthA->fetchrow_array;
$did_id = $aryA[0];
$did_pattern = $aryA[1];
$did_description = $aryA[2];
$did_active = $aryA[3];
$did_route = $aryA[4];
$did_extension = $aryA[5];
$exten_context = $aryA[6];
$voicemail_ext = $aryA[7];
$phone = $aryA[8];
$did_server_ip = $aryA[9];
$user = $aryA[10];
$user_unavailable_action = $aryA[11];
$user_route_settings_ingroup = $aryA[12];
$menu_id = $aryA[13];
$record_call = $aryA[14];
$filter_inbound_number = $aryA[15];
$filter_phone_group_id = $aryA[16];
$filter_url = $aryA[17];
$filter_action = $aryA[18];
$filter_extension = $aryA[19];
$filter_exten_context = $aryA[20];
$filter_voicemail_ext = $aryA[21];
$filter_phone = $aryA[22];
$filter_server_ip = $aryA[23];
$filter_user = $aryA[24];
$filter_user_unavailable_action = $aryA[25];
$filter_user_route_settings_ingroup = $aryA[26];
$filter_group_id = $aryA[27];
$filter_call_handle_method = $aryA[28];
$filter_agent_search_method = $aryA[29];
$filter_list_id = $aryA[30];
$filter_campaign_id = $aryA[31];
$filter_phone_code = $aryA[32];
$filter_menu_id = $aryA[33];
$filter_clean_cid_number = $aryA[34];
$group_id = $aryA[35];
$filter_dnc_campaign = $aryA[36];
$filter_url_did_redirect = $aryA[37];
$no_agent_ingroup_redirect = $aryA[38];
$no_agent_ingroup_id = $aryA[39];
$no_agent_ingroup_extension = $aryA[40];
$pre_filter_phone_group_id = $aryA[41];
$pre_filter_extension = $aryA[42];
}
$sthA->finish();
}
### Log the call to vicidial_did_log
$stmtA = "INSERT INTO vicidial_did_log SET uniqueid='$uniqueid',channel='$channel',server_ip='$VARserver_ip',caller_id_number='$callerid',caller_id_name='$calleridname',extension='$extension',call_date='$SQLdate',did_id='$did_id',did_route='$did_route';";
if ($AGILOG) {$agi_string = "|$stmtA|"; &agi_output;}
$affected_rowsL = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "-- DID LOG : |$affected_rowsL|$stmtA|"; &agi_output;}
### Log the call to call_log
$stmtA = "INSERT INTO call_log SET uniqueid='$unique_id', channel='$channel', channel_group='DID_INBOUND', server_ip='$VARserver_ip', type='$type', extension='$extension', number_dialed='$extension', caller_code='$callerid', start_time='$SQLdate', start_epoch='$now_date_epoch';";
$affected_rowsC = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "-- CALL LOG : |$affected_rowsC|$stmtA|"; &agi_output;}
if ($AGILOG) {$did_string = "$SQLdate|$uniqueid|$channel|$extension|$callerid|$calleridname|$did_id|$did_route|"; &did_output;}
if ($did_agent_log =~ /Y/)
{
if ($did_route !~ /IN_GROUP/)
{$group_id='';}
$stmtA = "INSERT INTO vicidial_did_agent_log SET uniqueid='$uniqueid',server_ip='$VARserver_ip',caller_id_number='$callerid',caller_id_name='$calleridname',extension='$extension',call_date='$SQLdate',did_id='$did_id',did_route='$did_route',user='VDCL',group_id='$group_id',did_description='$did_description';";
if ( ($tables_use_alt_log_db =~ /did_agent_log/i) && (length($alt_log_server_ip)>4) && (length($alt_log_dbname)>0) )
{
$dbhD = DBI->connect("DBI:mysql:$alt_log_dbname:$alt_log_server_ip:3306", "$alt_log_login", "$alt_log_pass")
or die "Couldn't connect to database: " . DBI->errstr;
if ($AGILOG) {$agi_string = "CONNECTED TO ALT-LOG DATABASE: $alt_log_server_ip|$alt_log_dbname|$uniqueid"; &agi_output;}
$affected_rowsAL = $dbhD->do($stmtA);
$dbhD->disconnect();
}
else
{
$affected_rowsAL = $dbhA->do($stmtA);
}
if ($AGILOG) {$agi_string = "-- DID AGENT LOG : |$affected_rowsAL|$stmtA|"; &agi_output;}
}
if ($DIDs_in_system < 1)
{
### if nothing found, exit script
if ($AGILOG) {$did_string = "NO default DID DEFINED! EXITING... $SQLdate|$uniqueid|$channel|$extension|$callerid|$calleridname|"; &did_output;}
exit;
}
### if set to record this call, start recording ###
if ($record_call =~ /Y/)
{
$insert_recording_id='';
$filename = "$tsSQLdate$US$extension$US$callerid";
### code to record call goes here ###
%ast_ver_str = parse_asterisk_version($asterisk_version);
if (( $ast_ver_str{major} = 1 ) && ($ast_ver_str{minor} < 6))
{
$AGI->exec("Monitor wav|/var/spool/asterisk/monitor/MIX/$filename");
}
else
{
$AGI->exec("Monitor","wav,/var/spool/asterisk/monitor/MIX/$filename");
}
### insert record into recording_log table ###
$stmtA = "INSERT INTO recording_log (channel,server_ip,extension,start_time,start_epoch,length_in_sec,filename,lead_id,user,location,vicidial_id) values('$channel','$VARserver_ip','$callerid','$SQLdate','$now_date_epoch','0','$filename','0','$extension','$filename','$unique_id');";
$affected_rowsR = $dbhA->do($stmtA);
if ($affected_rowsR > 0)
{
$stmtB = "select LAST_INSERT_ID() LIMIT 1;";
$sthA = $dbhA->prepare($stmtB) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
if ($sthArows > 0)
{
@aryA = $sthA->fetchrow_array;
$insert_recording_id = $aryA[0];
}
$sthA->finish();
}
if ($AGILOG) {$did_string = "-- RECORDING LOG : |$affected_rowsR|$insert_recording_id|$stmtA|"; &did_output;}
}
### Grab Server values from the database
$cbc=0;
$stmtA = "SELECT voicemail_dump_exten,ext_context,answer_transfer_agent,local_gmt,asterisk_version,max_vicidial_trunks,voicemail_dump_exten_no_inst FROM servers where server_ip = '$VARserver_ip';";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
while ($sthArows > $cbc)
{
@aryA = $sthA->fetchrow_array;
$DBvoicemail_dump_exten = $aryA[0];
$DBext_context = $aryA[1];
$DBanswer_transfer_agent = $aryA[2];
$DBSERVER_GMT = $aryA[3];
$DBasterisk_version = $aryA[4];
$DBmax_vicidial_trunks = $aryA[5];
$voicemail_dump_exten_no_inst = $aryA[6];
if ($DBvoicemail_dump_exten) {$voicemail_dump_exten = $DBvoicemail_dump_exten;}
if ($DBext_context) {$ext_context = $DBext_context;}
if ($DBanswer_transfer_agent) {$answer_transfer_agent = $DBanswer_transfer_agent;}
if ($DBSERVER_GMT) {$SERVER_GMT = $DBSERVER_GMT;}
if ($DBasterisk_version) {$AST_ver = $DBasterisk_version;}
if ($DBmax_vicidial_trunks) {$max_vicidial_trunks = $DBmax_vicidial_trunks;}
$cbc++;
}
$sthA->finish();
### BEGIN clean caller id number ###
if (length($filter_clean_cid_number) > 1)
{
$original_callerid = $callerid;
$filter_number_altered=0;
if ($filter_clean_cid_number =~ /^R\d/)
{
@filter_rule = split(/R/,$filter_clean_cid_number);
$filter_rule[1] =~ s/ .*//gi;
while (length($callerid) > $filter_rule[1])
{$callerid =~ s/^.//gi;}
if (length($callerid) != length($original_callerid))
{$filter_number_altered++;}
}
if ($filter_clean_cid_number =~ /^L\d/)
{
@filter_rule = split(/L/,$filter_clean_cid_number);
$filter_rule[1] =~ s/ .*//gi;
$filter_rule_reg = $filter_rule[1];
$callerid =~ s/^$filter_rule_reg//gi;
if (length($callerid) != length($original_callerid))
{$filter_number_altered++;}
}
if ($filter_number_altered > 0)
{
$newcallerid = "\"$calleridname <$callerid>\"";
$AGI->set_callerid($newcallerid);
if ($AGILOG) {$agi_string = "filter clean callerid, setting new CID: |$callerid|$calleridname|$newcallerid|$filter_clean_cid_number| Old: |$original_callerid|"; &agi_output;}
}
}
### END clean caller id number ###
##### BEGIN pre-filtering process #####
$pre_filter_match_found=0;
$filter_match_found=0;
if ( (length($pre_filter_phone_group_id)>0) && (length($pre_filter_extension)>0) )
{
if ($AGILOG) {$agi_string = "pre-filtering active: |$extension|$pre_filter_phone_group_id|$pre_filter_extension|$filter_clean_cid_number|$callerid"; &agi_output;}
$stmtA = "SELECT count(*) FROM vicidial_filter_phone_numbers where filter_phone_group_id='$pre_filter_phone_group_id' and phone_number='$callerid';";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
if ($sthArows > 0)
{
@aryA = $sthA->fetchrow_array;
$pre_filter_match_found = $aryA[0];
}
$sthA->finish();
if ($pre_filter_match_found > 0)
{
$did_route='DID';
$did_extension = $pre_filter_extension;
$filter_match_found = $pre_filter_extension;
$exten_context = 'trunkinbound';
$route_type = 'PREFILTER';
if ($AGILOG) {$agi_string = "PRE-Filter match found: |$pre_filter_match_found|$pre_filter_phone_group_id|$pre_filter_extension|$did_route|"; &agi_output;}
### Update the call in vicidial_did_log
$stmtA = "UPDATE vicidial_did_log SET did_route='PF_$did_route' where uniqueid='$uniqueid' and channel='$channel' and server_ip='$VARserver_ip' and caller_id_number='$callerid' and call_date='$SQLdate';";
if ($AGILOG) {$agi_string = "|$stmtA|"; &agi_output;}
$affected_rowsL = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "-- DID UPDATE LOG : |$affected_rowsL|$stmtA|"; &agi_output;}
}
}
##### END pre-filtering process #####
##### BEGIN filtering process #####
if ( ($filter_inbound_number =~ /GROUP|URL|DNC_INTERNAL|DNC_CAMPAIGN/) && ($pre_filter_match_found < 1) )
{
if ($AGILOG) {$agi_string = "filtering active: |$extension|$filter_inbound_number|$filter_phone_group_id|$filter_url|$filter_action|$filter_clean_cid_number|"; &agi_output;}
if ($filter_inbound_number =~ /GROUP/)
{
$stmtA = "SELECT count(*) FROM vicidial_filter_phone_numbers where filter_phone_group_id='$filter_phone_group_id' and phone_number='$callerid';";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
if ($sthArows > 0)
{
@aryA = $sthA->fetchrow_array;
$filter_match_found = $aryA[0];
}
$sthA->finish();
if ($AGILOG) {$agi_string = "GROUP: |$filter_match_found|$stmtA|"; &agi_output;}
}
elsif ($filter_inbound_number =~ /DNC_INTERNAL/)
{
$alt_areacode = substr($callerid, 0, 3);
$alt_areacode .= "XXXXXXX";
$stmtA = "SELECT count(*) FROM vicidial_dnc where phone_number IN('$callerid','$alt_areacode');";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
if ($sthArows > 0)
{
@aryA = $sthA->fetchrow_array;
$filter_match_found = $aryA[0];
}
$sthA->finish();
if ($AGILOG) {$agi_string = "DNC_INTERNAL: |$filter_match_found|$stmtA|"; &agi_output;}
}
elsif ($filter_inbound_number =~ /DNC_CAMPAIGN/)
{
$alt_areacode = substr($callerid, 0, 3);
$alt_areacode .= "XXXXXXX";
$stmtA = "SELECT count(*) FROM vicidial_campaign_dnc where campaign_id='$filter_dnc_campaign' and phone_number IN('$callerid','$alt_areacode');";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
if ($sthArows > 0)
{
@aryA = $sthA->fetchrow_array;
$filter_match_found = $aryA[0];
}
$sthA->finish();
if ($AGILOG) {$agi_string = "DNC_CAMPAIGN: |$filter_match_found|$stmtA|"; &agi_output;}
}
else
{
### first, find curl binary
$curlbin = '';
if ( -e ('/bin/curl')) {$curlbin = '/bin/curl';}
else
{
if ( -e ('/usr/bin/curl')) {$curlbin = '/usr/bin/curl';}
else
{
if ( -e ('/usr/local/bin/curl')) {$curlbin = '/usr/local/bin/curl';}
else
{
if ($AGILOG) {$agi_string = "ERROR: cannot filter by URL, curl binary not found"; &agi_output;}
}
}
}
if ( (length($curlbin) > 4) && ($filter_url =~ /http/i) && (length($filter_url) > 8) )
{
$filter_url =~ s/^VAR|^ //gi;
$filter_url =~ s/--A--phone_number--B--/$callerid/gi;
$filter_url =~ s/--A--did_pattern--B--/$did_pattern/gi;
$filter_url =~ s/&/\\&/gi;
$filter_url =~ s/ /+/gi;
@curl_output = `$curlbin $filter_url`;
$k=0;
$full_output='';
foreach (@curl_output)
{
$full_output .= $curl_output[$k];
$k++;
}
$full_output =~ s/\D//gi;
if ($full_output > 0)
{
$filter_match_found = $full_output;
if ($filter_url_did_redirect =~ /Y/)
{
$did_route='DID';
if ($AGILOG) {$agi_string = "Filter URL DID Redirect found: |$filter_match_found|$filter_url_did_redirect|$did_route|"; &agi_output;}
}
}
if ($AGILOG) {$agi_string = "URL filter search response: |$filter_match_found|$full_output|$filter_url|"; &agi_output;}
}
}
if ($filter_match_found > 0)
{
if ($did_route !~ /DID/)
{$did_route = $filter_action;}
$did_extension = $filter_extension;
$exten_context = $filter_exten_context;
$voicemail_ext = $filter_voicemail_ext;
$phone = $filter_phone;
$did_server_ip = $filter_server_ip;
$user = $filter_user;
$user_unavailable_action = $filter_user_unavailable_action;
$user_route_settings_ingroup = $filter_user_route_settings_ingroup;
$menu_id = $filter_menu_id;
$route_type = 'FILTER';
if ($AGILOG) {$agi_string = "Filter match found: |$filter_match_found|$filter_inbound_number|$filter_action|$did_route|"; &agi_output;}
### Update the call in vicidial_did_log
$stmtA = "UPDATE vicidial_did_log SET did_route='F_$did_route' where uniqueid='$uniqueid' and channel='$channel' and server_ip='$VARserver_ip' and caller_id_number='$callerid' and call_date='$SQLdate';";
if ($AGILOG) {$agi_string = "|$stmtA|"; &agi_output;}
$affected_rowsL = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "-- DID UPDATE LOG : |$affected_rowsL|$stmtA|"; &agi_output;}
}
}
##### END filtering process #####
##### BEGIN no-agent redirect process #####
if ( ($no_agent_ingroup_redirect =~ /Y|NO_PAUSED|READY_ONLY/) && ($pre_filter_match_found < 1) && ($filter_match_found < 1) )
{
$no_agent_redirect_bypass=1;
if ($AGILOG) {$agi_string = "filtering active: |$extension|$no_agent_ingroup_redirect|$no_agent_ingroup_id|$no_agent_ingroup_extension|$filter_clean_cid_number|"; &agi_output;}
$queue_date_epoch = time();
$BDtarget = ($queue_date_epoch - 5);
($Bsec,$Bmin,$Bhour,$Bmday,$Bmon,$Byear,$Bwday,$Byday,$Bisdst) = localtime($BDtarget);
$Byear = ($Byear + 1900);
$Bmon++;
if ($Bmon < 10) {$Bmon = "0$Bmon";}
if ($Bmday < 10) {$Bmday = "0$Bmday";}
if ($Bhour < 10) {$Bhour = "0$Bhour";}
if ($Bmin < 10) {$Bmin = "0$Bmin";}
if ($Bsec < 10) {$Bsec = "0$Bsec";}
$BDtsSQLdate = "$Byear$Bmon$Bmday$Bhour$Bmin$Bsec";
$NPsql='';
if ($no_agent_ingroup_redirect =~ /NO_PAUSED/)
{$NPsql = "and status NOT IN('PAUSED')";}
if ($no_agent_ingroup_redirect =~ /READY_ONLY/)
{$NPsql = "and status IN('READY','CLOSER')";}
$stmtA = "SELECT count(*) FROM vicidial_live_agents where closer_campaigns LIKE \"% $no_agent_ingroup_id %\" and last_update_time > '$BDtsSQLdate' $NPsql;";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
if ($sthArows > 0)
{
@aryA = $sthA->fetchrow_array;
$no_agent_redirect_bypass = $aryA[0];
}
$sthA->finish();
if ($no_agent_redirect_bypass < 1)
{
$did_route = 'EXTEN';
$did_extension = $no_agent_ingroup_extension;
$exten_context = 'default';
$route_type = 'NOAGENT';
if ($AGILOG) {$agi_string = "No-Agent Redirect: |$no_agent_redirect_bypass|$no_agent_ingroup_redirect|$did_extension|$no_agent_ingroup_id|$stmtA|"; &agi_output;}
### Update the call in vicidial_did_log
$stmtA = "UPDATE vicidial_did_log SET did_route='NA_$did_route' where uniqueid='$uniqueid' and channel='$channel' and server_ip='$VARserver_ip' and caller_id_number='$callerid' and call_date='$SQLdate';";
if ($AGILOG) {$agi_string = "|$stmtA|"; &agi_output;}
$affected_rowsL = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "-- DID UPDATE LOG : |$affected_rowsL|$stmtA|"; &agi_output;}
}
else
{
if ($AGILOG) {$agi_string = "No-Agent Debug: |$no_agent_redirect_bypass|$no_agent_ingroup_redirect|$did_extension|$no_agent_ingroup_id|$stmtA|"; &agi_output;}
}
}
##### END no-agent redirect process #####
### Route call to a VICIDIAL-defined phone
if ($did_route =~ /PHONE/)
{
$stmtA = "SELECT dialplan_number,voicemail_id,protocol,ext_context FROM phones where server_ip='$did_server_ip' and extension='$phone';";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
if ($sthArows > 0)
{
@aryA = $sthA->fetchrow_array;
$dialplan_number = $aryA[0];
$voicemail_id = $aryA[1];
$protocol = $aryA[2];
$ext_context = $aryA[3];
}
$sthA->finish();
### format the remote server dialstring to get the call to the overflow agent meetme room
if( $did_server_ip =~ m/(\S+)\.(\S+)\.(\S+)\.(\S+)/ )
{
$a = leading_zero($1);
$b = leading_zero($2);
$c = leading_zero($3);
$d = leading_zero($4);
$ServerString = "$a$S$b$S$c$S$d$S";
}
$did_extension = "$ServerString$dialplan_number";
$did_route = 'EXTEN';
}
### Route call to a voicemail box on the system
if ($did_route =~ /VOICEMAIL|VMAIL_NO_INST/)
{
### format the remote server dialstring to get the call to the overflow agent meetme room
if( $did_server_ip =~ m/(\S+)\.(\S+)\.(\S+)\.(\S+)/ )
{
$a = leading_zero($1);
$b = leading_zero($2);
$c = leading_zero($3);
$d = leading_zero($4);
$ServerString = "$a$S$b$S$c$S$d$S";
}
if ($did_route =~ /VMAIL_NO_INST/)
{$voicemail_dump_exten = $voicemail_dump_exten_no_inst;}
$did_extension = "$ServerString$voicemail_dump_exten$voicemail_ext";
$did_route = 'EXTEN';
}
### Route call to a VICIDIAL Inbound Group
if ($did_route =~ /IN_GROUP/)
{
$did_extension = "99909$S$did_id$S$S$S$route_type";
$exten_context = $ext_context;
$did_route = 'EXTEN';
}
### Route call to a logged-in VICIDIAL agent
if ($did_route =~ /AGENT/)
{
$did_extension = "99909$S$did_id$S$S$S$route_type";
$exten_context = $ext_context;
$did_route = 'EXTEN';
}
### QueueMetrics logging if enabled
if ( ($did_route =~ /EXTEN|CALLMENU/) && ($enable_queuemetrics_logging > 0) )
{
$dbhB = DBI->connect("DBI:mysql:$queuemetrics_dbname:$queuemetrics_server_ip:3306", "$queuemetrics_login", "$queuemetrics_pass")
or die "Couldn't connect to database: " . DBI->errstr;
if ($DBX) {print "CONNECTED TO QueueMetrics DATABASE: $queuemetrics_server_ip|$queuemetrics_dbname\n";}
$stmtB = "INSERT INTO queue_log SET partition='P01',time_id='$now_date_epoch',call_id='$uniqueid',queue='NONE',agent='NONE',verb='INFO',data1='DID',data2='$extension',serverid='$queuemetrics_log_id';";
$Baffected_rows = $dbhB->do($stmtB);
$dbhB->disconnect();
}
### Route call to an extension on the system
if ($did_route =~ /EXTEN/)
{
$XFERexten = "$did_extension";
if ($AGILOG) {$agi_string = "exiting the DID app, transferring call to $XFERexten @ $exten_context"; &agi_output;}
print "SET CONTEXT $exten_context\n";
$result = <STDIN>;
checkresult($result);
print "SET EXTENSION $XFERexten\n";
$result = <STDIN>;
checkresult($result);
print "SET PRIORITY 1\n";
$result = <STDIN>;
checkresult($result);
exit;
}
### Route call to a DID in the trunkinbound context on the system
if ($did_route =~ /DID/)
{
if ($AGILOG) {$agi_string = "exiting the DID app, transferring call to $filter_match_found @ trunkinbound"; &agi_output;}
print "SET CONTEXT trunkinbound\n";
$result = <STDIN>;
checkresult($result);
print "SET EXTENSION $filter_match_found\n";
$result = <STDIN>;
checkresult($result);
print "SET PRIORITY 1\n";
$result = <STDIN>;
checkresult($result);
exit;
}
### Route call to a call menu on the system
if ($did_route =~ /CALLMENU/)
{
if ($enable_queuemetrics_logging > 0)
{
$dbhB = DBI->connect("DBI:mysql:$queuemetrics_dbname:$queuemetrics_server_ip:3306", "$queuemetrics_login", "$queuemetrics_pass")
or die "Couldn't connect to database: " . DBI->errstr;
if ($DBX) {print "CONNECTED TO QM DATABASE: $queuemetrics_server_ip|$queuemetrics_dbname\n";}
$stmtB = "INSERT INTO queue_log SET partition='P01',time_id='$now_date_epoch',call_id='$uniqueid',queue='NONE',agent='NONE',verb='INFO',data1='IVRSTART',data2='$callerid',data3='$extension',serverid='$queuemetrics_log_id';";
$Baffected_rows = $dbhB->do($stmtB);
$dbhB->disconnect();
}
$XFERcontext = "$menu_id";
if ($AGILOG) {$agi_string = "exiting the DID app, transferring call to CALLMENU $XFERcontext"; &agi_output;}
print "SET CONTEXT $XFERcontext\n";
$result = <STDIN>;
checkresult($result);
print "SET EXTENSION s\n";
$result = <STDIN>;
checkresult($result);
print "SET PRIORITY 1\n";
$result = <STDIN>;
checkresult($result);
exit;
}
######### SUBROUTINES #################
sub checkresult
{
$pass=0; $fail=0;
my ($res) = @_;
my $retval;
$tests++;
chomp $res;
if ($res =~ /^200/)
{
$res =~ /result=(-?\d+)/;
if (!length($1))
{
# print STDERR "FAIL ($res)\n";
$fail++;
}
else
{
# print STDERR "PASS ($1)\n";
$pass++;
}
}
else
{
# print STDERR "FAIL (unexpected result '$res')\n";
$fail++;
}
}
sub leading_zero($)
{
$_ = $_[0];
s/^(\d)$/0$1/;
s/^(\d\d)$/0$1/;
return $_;
} # End of the leading_zero() routine.
sub agi_output
{
if ($AGILOG >=2)
{
### open the log file for writing ###
open(Lout, ">>$AGILOGfile")
|| die "Can't open $AGILOGfile: $!\n";
print Lout "$now_date|$script|$agi_string\n";
close(Lout);
}
### send to STDERR writing ###
if ( ($AGILOG == '1') || ($AGILOG == '3') )
{print STDERR "$now_date|$script|$agi_string\n";}
$agi_string='';
}
sub did_output
{
if ($AGILOG >=2)
{
### open the log file for writing ###
open(Dout, ">>$DIDLOGfile")
|| die "Can't open $DIDLOGfile: $!\n";
print Dout "$did_string\n";
close(Dout);
}
$did_string='';
}
# subroutine to parse the asterisk version
# and return a hash with the various part
sub parse_asterisk_version
{
# grab the arguments
my $ast_ver_str = $_[0];
# get everything after the - and put it in $ast_ver_postfix
my @hyphen_parts = split( /-/ , $ast_ver_str );
my $ast_ver_postfix = $hyphen_parts[1];
# now split everything before the - up by the .
my @dot_parts = split( /\./ , $hyphen_parts[0] );
my %ast_ver_hash;
if ( $dot_parts[0] <= 1 )
{
%ast_ver_hash = (
"major" => $dot_parts[0],
"minor" => $dot_parts[1],
"build" => $dot_parts[2],
"revision" => $dot_parts[3],
"postfix" => $ast_ver_postfix
);
}
# digium dropped the 1 from asterisk 10 but we still need it
if ( $dot_parts[0] > 1 )
{
%ast_ver_hash = (
"major" => 1,
"minor" => $dot_parts[0],
"build" => $dot_parts[1],
"revision" => $dot_parts[2],
"postfix" => $ast_ver_postfix
);
}
return ( %ast_ver_hash );
}
@@ -0,0 +1,816 @@
#!/usr/bin/perl
#
# agi-IVR_recording_verification.agi version 2.6
#
# This script is designed to function as a complete automated 3rd party
# verification IVR recording system. This script will work by optionally asking
# for a company ID and/or a customer phone number, then it will play through the
# listed prompts asking for a single digit press (i.e. 1 for a yes or a 3 for a
# no, hash/pound/star/zero/0 are not accepted only 1-9 is valid for these steps).
# All data for these calls will be logged to the vicidial_ivr table as well as
# optionally to a log file in the standard astguiclient log file directory
# and/or STDERR output on the asterisk process. This script uses the
# /etc/astguiclient.conf file for database connection settings, logging data and
# for logfile path information only.
#
# With a modified VDAD agi transfer script, this script could also be used to do
# automated outbound surveys as well. Feature under development.
#
# ; 1. the DNIS or inbound number called
# ; 2. play the recording ID (Y|N) with prompt leading up to it
# ; 3. record this call(Y|N)
# ; 4. ask for a company code(Y|N) with the number of digits in that company code and the prompt
# ; 5. ask for a user code(Y|N) with the number of digits in that user code and the prompt
# ; 6. ask for the customer phone number(Y|N) with the number of digits in that phone number and the prompt (Y-10-85100001)
# ; 7. speak the current date (Y|N) with prompt announcing the date
# ; 8. a dash-delimited list of prompts that expect responses(85100002-85100003)
# ; 9. a "your recording ID is..." prompt (85100004)
# ; 10. logging/output (FILE|STDERR|BOTH|NONE)
#
# ;inbound IVR call:
#exten => 1234,1,Ringing ; call ringing
#exten => 1234,2,Wait(1) ; Wait 1 second for CID delivery from PRI
#exten => 1234,3,Answer ; Answer the line
#exten => 1234,4,AGI(agi-IVR_recording_verification.agi,7274506620---Y-85100004---Y---Y-1-85100016---N---N---Y-85100007---85100009-600--85100012-30--85100013---85100004---BOTH)
#exten => 1234,5,Hangup
#
# ;inbound IVR call from a VICIDIAL transfer:
#exten => _83002*.,1,Ringing ; call ringing
#exten => _83002*.,2,Wait(1) ; Wait 1 second for CID delivery
#exten => _83002*.,3,Answer ; Answer the line
#exten => _83002*.,4,AGI(agi-IVR_recording_verification.agi,7275551234---N---Y---N---N---Y-10-85100001---N---85100002-85100003---85100004---BOTH)
#exten => _83002*.,5,Hangup
#
#
# Copyright (C) 2013 Matt Florell <vicidial@gmail.com> LICENSE: AGPLv2
#
# CHANGELOG
# 70912-1105 - First build, non-functional
# 70916-2331 - functional alpha version, reads all prompts and logs data, no recording yet
# 70921-1110 - full beta version with recording working
# 71028-1122 - added say datetime as an option
# 71112-0238 - added prompt delays and date announcement message
# 71120-1337 - added playing of recording ID at beginning of session
# 120430-2214 - Converted call to Monitor app to be asterisk 1.8 compatible
# 130108-1816 - Changes for Asterisk 1.8 compatibility
#
&get_time_now;
$start_epoch = $now_date_epoch;
$script = 'agi-IVR_recording_verification.agi';
$now_date_epoch = time();
$now_date = "$year-$mon-$mday $hour:$min:$sec";
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year = ($year + 1900);
$mon++;
if ($mon < 10) {$mon = "0$mon";}
if ($mday < 10) {$mday = "0$mday";}
if ($hour < 10) {$hour = "0$hour";}
if ($min < 10) {$min = "0$min";}
if ($sec < 10) {$sec = "0$sec";}
$US='_';
# default path to astguiclient configuration file:
$PATHconf = '/etc/astguiclient.conf';
open(conf, "$PATHconf") || die "can't open $PATHconf: $!\n";
@conf = <conf>;
close(conf);
$i=0;
foreach(@conf)
{
$line = $conf[$i];
$line =~ s/ |>|\n|\r|\t|\#.*|;.*//gi;
if ( ($line =~ /^PATHlogs/) && ($CLIlogs < 1) )
{$PATHlogs = $line; $PATHlogs =~ s/.*=//gi;}
if ( ($line =~ /^PATHsounds/) && ($CLIsounds < 1) )
{$PATHsounds = $line; $PATHsounds =~ s/.*=//gi;}
if ( ($line =~ /^PATHmonitor/) && ($CLImonitor < 1) )
{$PATHmonitor = $line; $PATHmonitor =~ s/.*=//gi;}
if ( ($line =~ /^VARserver_ip/) && ($CLIserver_ip < 1) )
{$VARserver_ip = $line; $VARserver_ip =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_server/) && ($CLIDB_server < 1) )
{$VARDB_server = $line; $VARDB_server =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_database/) && ($CLIDB_database < 1) )
{$VARDB_database = $line; $VARDB_database =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_user/) && ($CLIDB_user < 1) )
{$VARDB_user = $line; $VARDB_user =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_pass/) && ($CLIDB_pass < 1) )
{$VARDB_pass = $line; $VARDB_pass =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_port/) && ($CLIDB_port < 1) )
{$VARDB_port = $line; $VARDB_port =~ s/.*=//gi;}
$i++;
}
if (!$VARDB_port) {$VARDB_port='3306';}
if (!$AGILOGfile) {$AGILOGfile = "$PATHlogs/ivrout.$year-$mon-$mday";}
use DBI;
use Asterisk::AGI;
$AGI = new Asterisk::AGI;
$dbhA = DBI->connect("DBI:mysql:$VARDB_database:$VARDB_server:$VARDB_port", "$VARDB_user", "$VARDB_pass")
or die "Couldn't connect to database: " . DBI->errstr;
### Grab Server values from the database
$stmtA = "SELECT agi_output,ext_context,asterisk_version FROM servers where server_ip = '$VARserver_ip';";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
$rec_count=0;
while ($sthArows > $rec_count)
{
$AGILOG = '0';
@aryA = $sthA->fetchrow_array;
$DBagi_output = $aryA[0];
$ext_context = $aryA[1];
$asterisk_version = $aryA[2];
if ($DBagi_output =~ /STDERR/) {$AGILOG = '1';}
if ($DBagi_output =~ /FILE/) {$AGILOG = '2';}
if ($DBagi_output =~ /BOTH/) {$AGILOG = '3';}
$rec_count++;
}
$sthA->finish();
### begin parsing run-time options ###
if (length($ARGV[0])>1)
{
if ($ARGV[0] =~ /---/)
{
### list of command-line array arguments:
@ARGV_vars = split(/---/, $ARGV[0]);
$inbound_number = $ARGV_vars[0];
$play_ID = $ARGV_vars[1];
$record_call = $ARGV_vars[2];
$company_ID_ask = $ARGV_vars[3];
$user_ID_ask = $ARGV_vars[4];
$cust_phone_ask = $ARGV_vars[5];
$say_date = $ARGV_vars[6];
$prompts = $ARGV_vars[7];
$last_prompt = $ARGV_vars[8];
$AGI_output = $ARGV_vars[9];
@prompts_list = split(/-/, $prompts);
$prompts_count = $#prompts_list;
if ($play_ID =~ /Y/)
{
@play_IDary = split(/-/, $play_ID);
$play_ID_prompt = $play_IDary[1];
}
if ($company_ID_ask =~ /Y/)
{
@company_ID = split(/-/, $company_ID_ask);
$company_ID_length = $company_ID[1];
$company_ID_prompt = $company_ID[2];
}
if ($user_ID_ask =~ /Y/)
{
@user_ID = split(/-/, $user_ID_ask);
$user_ID_length = $user_ID[1];
$user_ID_prompt = $user_ID[2];
}
if ($cust_phone_ask =~ /Y/)
{
@cust_phone_ID = split(/-/, $cust_phone_ask);
$cust_phone_length = $cust_phone_ID[1];
$cust_phone_prompt = $cust_phone_ID[2];
}
if ($say_date =~ /Y/)
{
@say_date_ID = split(/-/, $say_date);
$say_date_prompt = $say_date_ID[1];
}
if ($AGI_output =~ /STDERR/) {$AGILOG = '1';}
if ($AGI_output =~ /FILE/) {$AGILOG = '2';}
if ($AGI_output =~ /BOTH/) {$AGILOG = '3';}
}
}
$|=1;
while(<STDIN>)
{
chomp;
last unless length($_);
if ($V)
{
if (/^agi_(\w+)\:\s+(.*)$/)
{
$AGI{$1} = $2;
}
}
if (/^agi_uniqueid\:\s+(.*)$/) {$unique_id = $1;}
if (/^agi_extension\:\s+(.*)$/) {$extension = $1;}
if (/^agi_channel\:\s+(.*)$/) {$channel = $1;}
if (/^agi_callerid\:\s+(.*)$/) {$callerid = $1;}
if (/^agi_calleridname\:\s+(.*)$/) {$calleridname = $1;}
}
if ( ($callerid =~ /\".*\"/) && ( (!$calleridname) or ($calleridname =~ /unknown/) ) )
{
$calleridname = $callerid;
$calleridname =~ s/\<\d\d\d\d\d\d\d\d\d\d\>//gi;
$calleridname =~ s/\"|\" //gi;
}
$callerid =~ s/\D//gi;
$calleridname =~ s/unknown//gi;
if ( (!$callerid) or ($callerid =~ /unknown/) )
{$callerid = $calleridname;}
$phone_number=$callerid;
### allow for VICIDIAL transfer data string "83002*3125551212*10000123*TESTCAMP*ABC*1234*"
if ($extension =~ /^832\d\d\*|^833\d\d\*/)
{
@EXT_vars = split(/\*/, $extension);
$referring_extension = $EXT_vars[0]; # initial extension sent
$phone_number = $EXT_vars[1]; # phone number
$lead_id = $EXT_vars[2]; # lead_id in vicidial_list
$campaign_id = $EXT_vars[3]; # campaign id
$product_code = $EXT_vars[4]; # product code
$user = $EXT_vars[5]; # vicidial_user that sent the call here
}
### table structure for data ###
# CREATE TABLE vicidial_ivr (
# ivr_id INT(9) UNSIGNED AUTO_INCREMENT PRIMARY KEY NOT NULL,
# entry_time DATETIME,
# length_in_sec SMALLINT(5) UNSIGNED default '0',
# inbound_number VARCHAR(12),
# recording_id INT(9) UNSIGNED,
# recording_filename VARCHAR(50),
# company_id VARCHAR(12),
# phone_number VARCHAR(12),
# lead_id INT(9) UNSIGNED,
# campaign_id VARCHAR(20),
# product_code VARCHAR(20),
# user VARCHAR(20),
# prompt_audio_1 VARCHAR(20),
# prompt_response_1 TINYINT(1) UNSIGNED default '0',
# prompt_audio_2 VARCHAR(20),
# prompt_response_2 TINYINT(1) UNSIGNED default '0',
# prompt_audio_3 VARCHAR(20),
# prompt_response_3 TINYINT(1) UNSIGNED default '0',
# prompt_audio_4 VARCHAR(20),
# prompt_response_4 TINYINT(1) UNSIGNED default '0',
# prompt_audio_5 VARCHAR(20),
# prompt_response_5 TINYINT(1) UNSIGNED default '0',
# prompt_audio_6 VARCHAR(20),
# prompt_response_6 TINYINT(1) UNSIGNED default '0',
# prompt_audio_7 VARCHAR(20),
# prompt_response_7 TINYINT(1) UNSIGNED default '0',
# prompt_audio_8 VARCHAR(20),
# prompt_response_8 TINYINT(1) UNSIGNED default '0',
# prompt_audio_9 VARCHAR(20),
# prompt_response_9 TINYINT(1) UNSIGNED default '0',
# prompt_audio_10 VARCHAR(20),
# prompt_response_10 TINYINT(1) UNSIGNED default '0',
# prompt_audio_11 VARCHAR(20),
# prompt_response_11 TINYINT(1) UNSIGNED default '0',
# prompt_audio_12 VARCHAR(20),
# prompt_response_12 TINYINT(1) UNSIGNED default '0',
# prompt_audio_13 VARCHAR(20),
# prompt_response_13 TINYINT(1) UNSIGNED default '0',
# prompt_audio_14 VARCHAR(20),
# prompt_response_14 TINYINT(1) UNSIGNED default '0',
# prompt_audio_15 VARCHAR(20),
# prompt_response_15 TINYINT(1) UNSIGNED default '0',
# prompt_audio_16 VARCHAR(20),
# prompt_response_16 TINYINT(1) UNSIGNED default '0',
# prompt_audio_17 VARCHAR(20),
# prompt_response_17 TINYINT(1) UNSIGNED default '0',
# prompt_audio_18 VARCHAR(20),
# prompt_response_18 TINYINT(1) UNSIGNED default '0',
# prompt_audio_19 VARCHAR(20),
# prompt_response_19 TINYINT(1) UNSIGNED default '0',
# prompt_audio_20 VARCHAR(20),
# prompt_response_20 TINYINT(1) UNSIGNED default '0',
# index (phone_number),
# index (entry_time)
# );
### insert record into vicidial_ivr table ###
$stmtA = "INSERT INTO vicidial_ivr (entry_time,length_in_sec,inbound_number,phone_number,lead_id,campaign_id,product_code,user) values('$now_date','0','$inbound_number','$phone_number','$lead_id','$campaign_id','$product_code','$user');";
$affected_rows = $dbhA->do($stmtA);
$cbc=0;
$stmtA = "select LAST_INSERT_ID() LIMIT 1;";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
while ($sthArows > $cbc)
{
@aryA = $sthA->fetchrow_array;
$ivr_id = "$aryA[0]";
$cbc++;
}
$sthA->finish();
if ($AGILOG) {$agi_string = "IVR START- $ivr_id $inbound_number|$record_call|$company_ID_ask|$cust_phone_ask|$prompts|$last_prompt|$AGI_output"; &agi_output;}
$AGI->stream_file('beep');
### if set to play ID at beginning of session ###
if ($play_ID =~ /Y/)
{
$AGI->stream_file("$play_ID_prompt");
$AGI->say_digits("$ivr_id");
sleep(1);
$AGI->stream_file('beep');
$AGI->say_digits("$ivr_id");
sleep(1);
}
### if set to ask for company ID, run that subroutine ###
if ($company_ID_ask =~ /Y/)
{
&company_ID_gather_response;
$company_ID = $totalDTMF;
&get_time_now;
$length_in_sec = ($now_date_epoch - $start_epoch);
$stmtA = "UPDATE vicidial_ivr SET company_id='$company_ID', length_in_sec='$length_in_sec' where ivr_id='$ivr_id';";
if ($AGILOG) {$agi_string = "IVR- $ivr_id company: $company_ID length: $length_in_sec"; &agi_output;}
$affected_rows = $dbhA->do($stmtA);
}
### if set to ask for user ID, run that subroutine ###
if ($user_ID_ask =~ /Y/)
{
&user_ID_gather_response;
$user_ID = $totalDTMF;
&get_time_now;
$length_in_sec = ($now_date_epoch - $start_epoch);
$stmtA = "UPDATE vicidial_ivr SET user='$user_ID', length_in_sec='$length_in_sec' where ivr_id='$ivr_id';";
if ($AGILOG) {$agi_string = "IVR- $ivr_id user: $user_ID length: $length_in_sec"; &agi_output;}
$affected_rows = $dbhA->do($stmtA);
}
### if set to ask for customer phone number, run that subroutine ###
if ($cust_phone_ask =~ /Y/)
{
&cust_phone_gather_response;
$phone_number = $totalDTMF;
&get_time_now;
$length_in_sec = ($now_date_epoch - $start_epoch);
$stmtA = "UPDATE vicidial_ivr SET phone_number='$phone_number', length_in_sec='$length_in_sec' where ivr_id='$ivr_id';";
if ($AGILOG) {$agi_string = "IVR- $ivr_id phone: $phone_number length: $length_in_sec"; &agi_output;}
$affected_rows = $dbhA->do($stmtA);
}
### if set to record this session, start recording ###
if ($record_call =~ /Y/)
{
#$filename = "$inbound_number$US$phone_number$US$ivr_id";
$filename = "$ivr_id";
### code to record call goes here ###
%ast_ver_str = parse_asterisk_version($asterisk_version);
if (( $ast_ver_str{major} = 1 ) && ($ast_ver_str{minor} < 6))
{
$AGI->exec("Monitor wav|/var/spool/asterisk/monitor/MIX/$filename|m");
}
else
{
$AGI->exec("Monitor","wav,/var/spool/asterisk/monitor/MIX/$filename,m");
}
### insert record into recording_log table ###
$stmtA = "INSERT INTO recording_log (channel,server_ip,extension,start_time,start_epoch,length_in_sec,filename,lead_id,user,location) values('$channel','$server_ip','$inbound_number','$now_date','start_epoch','0','$filename','$lead_id','$user','$ivr_id');";
$affected_rows = $dbhA->do($stmtA);
$cbc=0;
$stmtA = "select LAST_INSERT_ID() LIMIT 1;";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
while ($sthArows > $cbc)
{
@aryA = $sthA->fetchrow_array;
$recording_id = "$aryA[0]";
$cbc++;
}
$sthA->finish();
$stmtA = "UPDATE vicidial_ivr SET recording_id='$recording_id', recording_filename='$filename' where ivr_id='$ivr_id';";
if ($AGILOG) {$agi_string = "RECORDING- $recording_id $ivr_id START filename: $filename"; &agi_output;}
$affected_rows = $dbhA->do($stmtA);
}
### if set, say the current date ###
if ($say_date =~ /Y/)
{
if ($AGILOG) {$agi_string = "SAYING DATETIME $now_date_epoch"; &agi_output;}
if (length($say_date_prompt)>0) {$AGI->stream_file("$say_date_prompt");}
sleep(1);
$AGI->exec("SayUnixtime");
sleep(1);
}
##### Go through the propmts one at a time until done #####
@prompts_list = split(/--/, $prompts);
$prompts_count = $#prompts_list;
$i=0;
$j=1;
while($prompts_count >= $i)
{
$prompt_ary = $prompts_list[$i];
@prompts_specs = split(/-/, $prompt_ary);
$prompt = $prompts_specs[0];
$wait_sec = $prompts_specs[1];
if ($wait_sec < 1) {$wait_sec=10;}
$response = '';
$prompt_name = "prompt_audio_$j";
$response_name = "prompt_response_$j";
sleep(1);
&prompt_gather_response;
$response = $totalDTMF;
&get_time_now;
$length_in_sec = ($now_date_epoch - $start_epoch);
$stmtA = "UPDATE vicidial_ivr SET $prompt_name='$prompt', $response_name='$response', length_in_sec='$length_in_sec' where ivr_id='$ivr_id';";
if ($AGILOG) {$agi_string = "|$stmtA|"; &agi_output;}
$affected_rows = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "IVR- $ivr_id $i prompt: $prompt - $wait_sec response: $response length: $length_in_sec"; &agi_output;}
$i++;
$j++;
}
##### Play the last prompt and say the IVR number #####
$AGI->stream_file("$last_prompt");
$AGI->say_digits("$ivr_id");
&get_time_now;
$length_in_sec = ($now_date_epoch - $start_epoch);
$stmtA = "UPDATE vicidial_ivr SET length_in_sec='$length_in_sec' where ivr_id='$ivr_id';";
if ($AGILOG) {$agi_string = "|$stmtA|"; &agi_output;}
$affected_rows = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "IVR- $ivr_id END prompt: $last_prompt length: $length_in_sec"; &agi_output;}
if ($record_call =~ /Y/)
{
$stmtA = "UPDATE recording_log SET length_in_sec='$length_in_sec' where recording_id='$recording_id';";
if ($AGILOG) {$agi_string = "|$stmtA|"; &agi_output;}
$affected_rows = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "RECORDING- $recording_id length: $length_in_sec"; &agi_output;}
}
sleep(1);
$AGI->stream_file('beep');
$AGI->say_digits("$ivr_id");
sleep(1);
$AGI->stream_file('vm-goodbye');
if ($AGILOG) {$agi_string = "IVR- ID: $ivr_id DONE Exiting"; &agi_output;}
exit;
################################################################################
##### SUBROUTINES ##############################################################
################################################################################
##### BEGIN collect the company ID #############################################
sub company_ID_gather_response
{
$digit='';
$interrupt_digit='';
$interrupt_digit = $AGI->stream_file("$company_ID_prompt",'1234567890');
print STDERR "interrupt_digit |$interrupt_digit|\n";
$digits_being_entered=1;
$digit_loop_counter=0;
$totalDTMF='';
if ($interrupt_digit > 1)
{
# if ($interrupt_digit == 35) {$interrupt_digit='#';}
# if ($interrupt_digit == 42) {$interrupt_digit='*';}
if ($interrupt_digit == 48) {$interrupt_digit=0;}
if ($interrupt_digit == 49) {$interrupt_digit=1;}
if ($interrupt_digit == 50) {$interrupt_digit=2;}
if ($interrupt_digit == 51) {$interrupt_digit=3;}
if ($interrupt_digit == 52) {$interrupt_digit=4;}
if ($interrupt_digit == 53) {$interrupt_digit=5;}
if ($interrupt_digit == 54) {$interrupt_digit=6;}
if ($interrupt_digit == 55) {$interrupt_digit=7;}
if ($interrupt_digit == 56) {$interrupt_digit=8;}
if ($interrupt_digit == 57) {$interrupt_digit=9;}
$totalDTMF=$interrupt_digit;
$digit_loop_counter++;
}
while ($digit_loop_counter < $company_ID_length)
{
$digit = chr($AGI->wait_for_digit('100000')); # wait 100 seconds for input
if ($digit =~ /\d/)
{
$totalDTMF = "$totalDTMF$digit";
print STDERR "digit |$digit| TotalDTMF |$totalDTMF|\n";
# $AGI->say_digits("$digit");
undef $digit;
}
else
{
$digit_loop_counter=$company_ID_length;
}
$digit_loop_counter++;
}
}
##### END collect the company ID ###############################################
##### BEGIN collect the user ID ################################################
sub user_ID_gather_response
{
$digit='';
$interrupt_digit='';
$interrupt_digit = $AGI->stream_file("$user_ID_prompt",'1234567890');
print STDERR "interrupt_digit |$interrupt_digit|\n";
$digits_being_entered=1;
$digit_loop_counter=0;
$totalDTMF='';
if ($interrupt_digit > 1)
{
# if ($interrupt_digit == 35) {$interrupt_digit='#';}
# if ($interrupt_digit == 42) {$interrupt_digit='*';}
if ($interrupt_digit == 48) {$interrupt_digit=0;}
if ($interrupt_digit == 49) {$interrupt_digit=1;}
if ($interrupt_digit == 50) {$interrupt_digit=2;}
if ($interrupt_digit == 51) {$interrupt_digit=3;}
if ($interrupt_digit == 52) {$interrupt_digit=4;}
if ($interrupt_digit == 53) {$interrupt_digit=5;}
if ($interrupt_digit == 54) {$interrupt_digit=6;}
if ($interrupt_digit == 55) {$interrupt_digit=7;}
if ($interrupt_digit == 56) {$interrupt_digit=8;}
if ($interrupt_digit == 57) {$interrupt_digit=9;}
$totalDTMF=$interrupt_digit;
$digit_loop_counter++;
}
while ($digit_loop_counter < $user_ID_length)
{
$digit = chr($AGI->wait_for_digit('100000')); # wait 100 seconds for input
if ($digit =~ /\d/)
{
$totalDTMF = "$totalDTMF$digit";
print STDERR "digit |$digit| TotalDTMF |$totalDTMF|\n";
# $AGI->say_digits("$digit");
undef $digit;
}
else
{
$digit_loop_counter=$user_ID_length;
}
$digit_loop_counter++;
}
}
##### END collect the user ID ##################################################
##### BEGIN collect the customer phone number ##################################
sub cust_phone_gather_response
{
$digit='';
$interrupt_digit='';
$interrupt_digit = $AGI->stream_file("$cust_phone_prompt",'1234567890');
print STDERR "interrupt_digit |$interrupt_digit|\n";
$digits_being_entered=1;
$digit_loop_counter=0;
$totalDTMF='';
if ($interrupt_digit > 1)
{
if ($interrupt_digit == 48) {$interrupt_digit=0;}
if ($interrupt_digit == 49) {$interrupt_digit=1;}
if ($interrupt_digit == 50) {$interrupt_digit=2;}
if ($interrupt_digit == 51) {$interrupt_digit=3;}
if ($interrupt_digit == 52) {$interrupt_digit=4;}
if ($interrupt_digit == 53) {$interrupt_digit=5;}
if ($interrupt_digit == 54) {$interrupt_digit=6;}
if ($interrupt_digit == 55) {$interrupt_digit=7;}
if ($interrupt_digit == 56) {$interrupt_digit=8;}
if ($interrupt_digit == 57) {$interrupt_digit=9;}
$totalDTMF=$interrupt_digit;
$digit_loop_counter++;
}
while ($digit_loop_counter < $cust_phone_length)
{
$digit = chr($AGI->wait_for_digit('100000')); # wait 100 seconds for input
if ($digit =~ /\d/)
{
$totalDTMF = "$totalDTMF$digit";
print STDERR "digit |$digit| TotalDTMF |$totalDTMF|\n";
# $AGI->say_digits("$digit");
undef $digit;
}
else
{
$digit_loop_counter=$cust_phone_length;
}
$digit_loop_counter++;
}
}
##### END collect the customer phone number ####################################
##### BEGIN collect the response to a prompt ###################################
sub prompt_gather_response
{
$digit='';
$interrupt_digit='';
$interrupt_digit = $AGI->stream_file("$prompt",'123456789');
print STDERR "interrupt_digit |$interrupt_digit|\n";
$digits_being_entered=1;
$digit_loop_counter=0;
$totalDTMF='';
if ($interrupt_digit > 1)
{
# if ($interrupt_digit == 48) {$interrupt_digit=0;}
if ($interrupt_digit == 49) {$interrupt_digit=1;}
if ($interrupt_digit == 50) {$interrupt_digit=2;}
if ($interrupt_digit == 51) {$interrupt_digit=3;}
if ($interrupt_digit == 52) {$interrupt_digit=4;}
if ($interrupt_digit == 53) {$interrupt_digit=5;}
if ($interrupt_digit == 54) {$interrupt_digit=6;}
if ($interrupt_digit == 55) {$interrupt_digit=7;}
if ($interrupt_digit == 56) {$interrupt_digit=8;}
if ($interrupt_digit == 57) {$interrupt_digit=9;}
$totalDTMF=$interrupt_digit;
$digit_loop_counter++;
}
while ($digit_loop_counter < 1)
{
$wait_ms = ($wait_sec * 1000);
$digit = chr($AGI->wait_for_digit("$wait_ms")); # wait number of milli-seconds for input
if ($digit =~ /\d/)
{
$totalDTMF = "$totalDTMF$digit";
print STDERR "digit |$digit| TotalDTMF |$totalDTMF|\n";
# $AGI->say_digits("$digit");
undef $digit;
}
else
{
$digit_loop_counter=-1;
}
$digit_loop_counter++;
}
}
##### END collect the response to a prompt #####################################
sub get_time_now #get the current date and time and epoch for logging call lengths and datetimes
{
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year = ($year + 1900);
$mon++;
if ($mon < 10) {$mon = "0$mon";}
if ($mday < 10) {$mday = "0$mday";}
if ($hour < 10) {$hour = "0$hour";}
if ($min < 10) {$min = "0$min";}
if ($sec < 10) {$sec = "0$sec";}
$now_date_epoch = time();
$now_date = "$year-$mon-$mday $hour:$min:$sec";
$filedate = "$year$mon$mday$hour$min$sec";
}
sub checkresult
{
my ($res) = @_;
my $retval;
$tests++;
chomp $res;
if ($res =~ /^200/)
{
$res =~ /result=(-?\d+)/;
if (!length($1))
{
print STDERR "FAIL ($res)\n";
$fail++;
}
else
{
print STDERR "PASS ($1)\n";
$pass++;
}
}
else
{
print STDERR "FAIL (unexpected result '$res')\n";
$fail++;
}
}
sub agi_output
{
if ($AGILOG >=2)
{
### open the log file for writing ###
open(Lout, ">>$AGILOGfile")
|| die "Can't open $AGILOGfile: $!\n";
print Lout "$now_date|$script|$agi_string\n";
close(Lout);
}
### send to STDERR writing ###
if ( ($AGILOG == '1') || ($AGILOG == '3') )
{print STDERR "$now_date|$script|$agi_string\n";}
$agi_string='';
}
# subroutine to parse the asterisk version
# and return a hash with the various part
sub parse_asterisk_version
{
# grab the arguments
my $ast_ver_str = $_[0];
# get everything after the - and put it in $ast_ver_postfix
my @hyphen_parts = split( /-/ , $ast_ver_str );
my $ast_ver_postfix = $hyphen_parts[1];
# now split everything before the - up by the .
my @dot_parts = split( /\./ , $hyphen_parts[0] );
my %ast_ver_hash;
if ( $dot_parts[0] <= 1 )
{
%ast_ver_hash = (
"major" => $dot_parts[0],
"minor" => $dot_parts[1],
"build" => $dot_parts[2],
"revision" => $dot_parts[3],
"postfix" => $ast_ver_postfix
);
}
# digium dropped the 1 from asterisk 10 but we still need it
if ( $dot_parts[0] > 1 )
{
%ast_ver_hash = (
"major" => 1,
"minor" => $dot_parts[0],
"build" => $dot_parts[1],
"revision" => $dot_parts[2],
"postfix" => $ast_ver_postfix
);
}
return ( %ast_ver_hash );
}
@@ -0,0 +1,184 @@
#!/usr/bin/perl
#
# agi-LCR-Route.agi version 2.2.0
#
# This script looks up the NPANXX in the lcr table and sets
# the LCRTRUNK dialplan variable to the carrier string
# with the cheapest rate. If no carrier is found for this NPANXX
# the variable is unchanged so you should set it with the default
# previously.
#
# [globals]
# LCRTRUNK=SIP/provider1 ; default LCR trunk
#
# [default]
# ;outbound VICIDIAL calls:
# exten => _91NXXNXXXXXX,1,AGI(agi://127.0.0.1:4577/call_log)
# exten => _91NXXNXXXXXX,n,AGI(agi-LCR-Route.agi,${EXTEN:2:6})
# exten => _91NXXNXXXXXX,n,Dial(${LCRTRUNK}/${EXTEN:2},,To)
# exten => _91NXXNXXXXXX,n,Hangup
#
# Here is the SQL to create the lcr table:
#
# create table if not exists lcr (
# npanxx mediumint not null,
# rate varchar(10) not null,
# carrier_string varchar(20) not null
# );
# alter table lcr add unique index (npanxx, rate, carrier_string);
#
# A typical npanxx from this table should look like:
#
# mysql> select npanxx,rate,carrier_string from lcr where npanxx='734223';
# +--------+--------+----------------+
# | npanxx | rate | carrier_string |
# +--------+--------+----------------+
# | 734223 | 0.0064 | SIP/provider1 |
# | 734223 | 0.0076 | SIP/provider2 |
# +--------+--------+----------------+
#
# Unfortunately these rate sheets are not in a standard format.
# You will have to write a custom loading script to populate this table.
# An exampe loading script is provided in the extras directory of the
# astguiclient source code called lcr_loader_example.pl
#
#
# Copyright (C) 2009 Michael Cargile <mikec@vicidial.com> LICENSE: AGPLv2
#
# changes:
# 90717-1554 - First Build
# 91129-2209 - formatting fixes
#
sub agi_output
{
if ($AGILOG >=2)
{
### open the log file for writing ###
open(Lout, ">>$AGILOGfile")
|| die "Can't open $AGILOGfile: $!\n";
print Lout "$now_date|$script|$agi_string\n";
close(Lout);
}
### send to STDERR writing ###
if ( ($AGILOG == '1') || ($AGILOG == '3') )
{print STDERR "$now_date|$script|$agi_string\n";}
$agi_string='';
}
$script = 'agi-LCR-Route.agi';
$AGILOG = '0';
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year = ($year + 1900);
$mon++;
if ($mon < 10) {$mon = "0$mon";}
if ($mday < 10) {$mday = "0$mday";}
if ($hour < 10) {$hour = "0$hour";}
if ($min < 10) {$min = "0$min";}
if ($sec < 10) {$sec = "0$sec";}
# default path to astguiclient configuration file:
$PATHconf = '/etc/astguiclient.conf';
open(conf, "$PATHconf") || die "can't open $PATHconf: $!\n";
@conf = <conf>;
close(conf);
$i=0;
foreach(@conf)
{
$line = $conf[$i];
$line =~ s/ |>|\n|\r|\t|\#.*|;.*//gi;
if ( ($line =~ /^PATHhome/) && ($CLIhome < 1) )
{$PATHhome = $line; $PATHhome =~ s/.*=//gi;}
if ( ($line =~ /^PATHlogs/) && ($CLIlogs < 1) )
{$PATHlogs = $line; $PATHlogs =~ s/.*=//gi;}
if ( ($line =~ /^PATHagi/) && ($CLIagi < 1) )
{$PATHagi = $line; $PATHagi =~ s/.*=//gi;}
if ( ($line =~ /^PATHweb/) && ($CLIweb < 1) )
{$PATHweb = $line; $PATHweb =~ s/.*=//gi;}
if ( ($line =~ /^PATHsounds/) && ($CLIsounds < 1) )
{$PATHsounds = $line; $PATHsounds =~ s/.*=//gi;}
if ( ($line =~ /^PATHmonitor/) && ($CLImonitor < 1) )
{$PATHmonitor = $line; $PATHmonitor =~ s/.*=//gi;}
if ( ($line =~ /^VARserver_ip/) && ($CLIserver_ip < 1) )
{$VARserver_ip = $line; $VARserver_ip =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_server/) && ($CLIDB_server < 1) )
{$VARDB_server = $line; $VARDB_server =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_database/) && ($CLIDB_database < 1) )
{$VARDB_database = $line; $VARDB_database =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_user/) && ($CLIDB_user < 1) )
{$VARDB_user = $line; $VARDB_user =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_pass/) && ($CLIDB_pass < 1) )
{$VARDB_pass = $line; $VARDB_pass =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_port/) && ($CLIDB_port < 1) )
{$VARDB_port = $line; $VARDB_port =~ s/.*=//gi;}
$i++;
}
if (!$VARDB_port) {$VARDB_port='3306';}
if (!$AGILOGfile) {$AGILOGfile = "$PATHlogs/agiout.$year-$mon-$mday";}
use DBI;
use Asterisk::AGI;
$AGI = new Asterisk::AGI;
$dbhA = DBI->connect("DBI:mysql:$VARDB_database:$VARDB_server:$VARDB_port", "$VARDB_user", "$VARDB_pass")
or die "Couldn't connect to database: " . DBI->errstr;
### Grab Server values from the database
$stmtA = "SELECT agi_output FROM servers where server_ip = '$VARserver_ip';";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
if ($sthArows > 0)
{
@aryA = $sthA->fetchrow_array;
$DBagi_output = "$aryA[0]";
if ($DBagi_output =~ /STDERR/) {$AGILOG = '1';}
if ($DBagi_output =~ /FILE/) {$AGILOG = '2';}
if ($DBagi_output =~ /BOTH/) {$AGILOG = '3';}
}
$sthA->finish();
### begin parsing run-time options ###
if (length($ARGV[0])>1)
{
if ($AGILOG) {$agi_string = "Perl Environment Dump:"; &agi_output;}
$i=0;
while ($#ARGV >= $i)
{
$args = "$args $ARGV[$i]";
if ($AGILOG) {$agi_string = "$i|$ARGV[$i]"; &agi_output;}
$i++;
}
### list of command-line array arguments:
@ARGV_vars = split(/-----/, $ARGV[0]);
$npanxx = $ARGV_vars[0];
if ($AGILOG) {$agi_string = "Searching for NPANXX = $npanxx"; &agi_output;}
}
# get the cheapest carrier
$stmtA = "SELECT carrier_string, rate FROM lcr WHERE npanxx='$npanxx' ORDER BY rate ASC";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
if ( $sthArows > 0 )
{
$rec_count=0;
@aryA = $sthA->fetchrow_array;
$carrier_string = "$aryA[0]";
$sthA->finish();
if ($AGILOG) {$agi_string = "Setting LCRTRUNK to $carrier_string"; &agi_output;}
$AGI->set_variable( "LCRTRUNK",$carrier_string );
}
else
{
if ($AGILOG) {$agi_string = "No carrier found for NPANXX = $npanxx. Leaving the LCRTRUNK variable as the default."; &agi_output;}
}
@@ -0,0 +1,367 @@
#!/usr/bin/perl
#
# agi-NVA_recording.agi version 2.10
#
# This script is designed to give recording ability to agents not the using
# ViciDial agent screen
#
# ; 1. logging output (NONE|STDERR|FILE|BOTH)
# ; 2. the ViciDial user ID, if empty it defaults to accountcode(usually phone extension) or vicidial_live_agents user who launched the call
# ; 3. log this call in user_call_log (Y|N) default N
# ; 4. log this call in call_log (Y|N) default N, ONLY NEEDED FOR INBOUND AND INTERSYSTEM CALLS!!!
# ; 5. audio record this call (Y|N) default N
# ; 6. double-log this call in call_log (Y|N) default N, ONLY NEEDED FOR INBOUND CALLMENU FORWARDED CALLS!!!
#
# ;custom dialplan entry example: (similar to the defaultlog Call Menu)
#exten => _X.,1,AGI(agi-NVA_recording.agi,BOTH------Y---N---Y---N)
#exten => _X.,n,Goto(default,${EXTEN},1)
#exten => _X.,n,Hangup
#
# ;inbound to agent example:
#exten => 5678,1,AGI(agi-NVA_recording.agi,BOTH------Y---Y---Y---N)
#exten => 5678,n,Goto(vicidial-auto,5678,1)
#exten => 5678,n,Hangup
#
# ; example of dial prefix of 94 in System Settings Custom Dialplan to default log
#exten => _941NXXNXXXXXX,1,Goto(defaultlog,9${EXTEN:2},1)
#
# Copyright (C) 2013 Matt Florell <vicidial@gmail.com> LICENSE: AGPLv2
#
# CHANGELOG
# 91105-1402 - First build
# 100123-1434 - Added double-log option for CallMenu forwarded calls
# 120430-2214 - Converted call to Monitor app to be asterisk 1.8 compatible
# 130108-1811 - Changes for Asterisk 1.8 compatibility
# 130326-1510 - Added lead_id and user logging from vicidial values
# 141124-2309 - Fixed Fhour variable bug
#
&get_time_now;
$script = 'agi-NVA_recording.agi';
$now_date_epoch = time();
$start_epoch = $now_date_epoch;
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year = ($year + 1900);
$mon++;
if ($mon < 10) {$mon = "0$mon";}
if ($mday < 10) {$mday = "0$mday";}
if ($hour < 10) {$hour = "0$hour";}
if ($min < 10) {$min = "0$min";}
if ($sec < 10) {$sec = "0$sec";}
$now_date = "$year-$mon-$mday $hour:$min:$sec";
$filedate = "$year$mon$mday$hour$min$sec";
$US='_';
# default path to astguiclient configuration file:
$PATHconf = '/etc/astguiclient.conf';
open(conf, "$PATHconf") || die "can't open $PATHconf: $!\n";
@conf = <conf>;
close(conf);
$i=0;
foreach(@conf)
{
$line = $conf[$i];
$line =~ s/ |>|\n|\r|\t|\#.*|;.*//gi;
if ( ($line =~ /^PATHlogs/) && ($CLIlogs < 1) )
{$PATHlogs = $line; $PATHlogs =~ s/.*=//gi;}
if ( ($line =~ /^PATHsounds/) && ($CLIsounds < 1) )
{$PATHsounds = $line; $PATHsounds =~ s/.*=//gi;}
if ( ($line =~ /^PATHmonitor/) && ($CLImonitor < 1) )
{$PATHmonitor = $line; $PATHmonitor =~ s/.*=//gi;}
if ( ($line =~ /^VARserver_ip/) && ($CLIserver_ip < 1) )
{$VARserver_ip = $line; $VARserver_ip =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_server/) && ($CLIDB_server < 1) )
{$VARDB_server = $line; $VARDB_server =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_database/) && ($CLIDB_database < 1) )
{$VARDB_database = $line; $VARDB_database =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_user/) && ($CLIDB_user < 1) )
{$VARDB_user = $line; $VARDB_user =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_pass/) && ($CLIDB_pass < 1) )
{$VARDB_pass = $line; $VARDB_pass =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_port/) && ($CLIDB_port < 1) )
{$VARDB_port = $line; $VARDB_port =~ s/.*=//gi;}
$i++;
}
if (!$VARDB_port) {$VARDB_port='3306';}
if (!$AGILOGfile) {$AGILOGfile = "$PATHlogs/nvaout.$year-$mon-$mday";}
use DBI;
use Asterisk::AGI;
$AGI = new Asterisk::AGI;
$dbhA = DBI->connect("DBI:mysql:$VARDB_database:$VARDB_server:$VARDB_port", "$VARDB_user", "$VARDB_pass")
or die "Couldn't connect to database: " . DBI->errstr;
### Grab Server values from the database
$stmtA = "SELECT agi_output,ext_context,asterisk_version FROM servers where server_ip = '$VARserver_ip';";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
$rec_count=0;
while ($sthArows > $rec_count)
{
$AGILOG = '0';
@aryA = $sthA->fetchrow_array;
$DBagi_output = $aryA[0];
$ext_context = $aryA[1];
$asterisk_version = $aryA[2];
if ($DBagi_output =~ /STDERR/) {$AGILOG = '1';}
if ($DBagi_output =~ /FILE/) {$AGILOG = '2';}
if ($DBagi_output =~ /BOTH/) {$AGILOG = '3';}
$rec_count++;
}
$sthA->finish();
### begin parsing run-time options ###
if (length($ARGV[0])>1)
{
if ($ARGV[0] =~ /---/)
{
### list of command-line array arguments:
@ARGV_vars = split(/---/, $ARGV[0]);
$AGI_output = $ARGV_vars[0];
if (length($ARGV_vars[1]) > 0)
{$user = $ARGV_vars[1];}
$log_user_call = $ARGV_vars[2];
$log_call = $ARGV_vars[3];
$record_call = $ARGV_vars[4];
$double_log = $ARGV_vars[5];
if ($AGI_output =~ /STDERR/) {$AGILOG = '1';}
if ($AGI_output =~ /FILE/) {$AGILOG = '2';}
if ($AGI_output =~ /BOTH/) {$AGILOG = '3';}
if ($AGILOG) {$agi_string = "Perl Environment Dump:"; &agi_output;}
if ($AGILOG) {$agi_string = "$AGI_output|$user|$log_call|$record_call|$uniqueid_log|$double_log"; &agi_output;}
}
}
$|=1;
while(<STDIN>)
{
chomp;
last unless length($_);
if ($AGILOG)
{
if (/^agi_(\w+)\:\s+(.*)$/)
{
$AGI{$1} = $2;
$agi_string = "$1|$2"; &agi_output;
}
}
if (/^agi_uniqueid\:\s+(.*)$/) {$unique_id = $1;}
if (/^agi_extension\:\s+(.*)$/) {$extension = $1;}
if (/^agi_accountcode\:\s+(.*)$/) {$accountcode = $1;}
if (/^agi_type\:\s+(.*)$/) {$type = $1;}
if (/^agi_channel\:\s+(.*)$/) {$channel = $1;}
if (/^agi_callerid\:\s+(.*)$/) {$callerid = $1;}
if (/^agi_calleridname\:\s+(.*)$/) {$calleridname = $1;}
}
if ( ($callerid =~ /\".*\"/) && ( (!$calleridname) or ($calleridname =~ /unknown/) ) )
{
$calleridname = $callerid;
$calleridname =~ s/\<\d\d\d\d\d\d\d\d\d\d\>//gi;
$calleridname =~ s/\"|\" //gi;
}
$callerid =~ s/\D|\'//gi;
$calleridname =~ s/unknown|\'//gi;
if ( (!$callerid) or ($callerid =~ /unknown/) )
{$callerid = $calleridname;}
if ($calleridname =~ /^V\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d|^Y\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d|^M\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d|^D\S\d\d\d\d\d\dW\d\d\d\d\d\d\d\d\d\dW/)
{
$vla_string='';
$call_type='';
$lead_id='0';
if ($calleridname =~ /^V/)
{
$lead_id = substr($calleridname, 10, 10);
$call_type = 'auto-dial';
}
if ($calleridname =~ /^Y/)
{
$lead_id = substr($calleridname, 10, 10);
$call_type = 'inbound';
}
if ($calleridname =~ /^M/)
{
$lead_id = substr($calleridname, 10, 10);
$call_type = 'manual-dial';
}
if ($calleridname =~ /^D/)
{
$lead_id = substr($calleridname, 9, 10);
$call_type = '3-way';
}
if (length($call_type) > 0)
{
$lead_id = ($lead_id + 0);
$stmtA = "SELECT user,campaign_id FROM vicidial_live_agents where lead_id='$lead_id' order by last_update_time desc limit 1;";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
if ($sthArows > 0)
{
@aryA = $sthA->fetchrow_array;
$VLAuser = $aryA[0];
$VLAcampaign = $aryA[1];
if (length($user) < 1)
{$user = $VLAuser;}
$vla_string = "Live agent record found: |$VLAuser|$user|$VLAcampaign|";
}
if ($AGILOG) {$agi_string = "Vicidial $call_type call: |$calleridname|$lead_id| $vla_string"; &agi_output;}
}
else
{
if ($AGILOG) {$agi_string = "Not a Vicidial call: $calleridname"; &agi_output;}
}
}
else
{
if ($AGILOG) {$agi_string = "Not a Vicidial call: $calleridname"; &agi_output;}
}
if (length($user) > 0)
{$accountcode = $user;}
### if set to record this call, start recording ###
if ($record_call =~ /Y/)
{
$filename = "$filedate$US$accountcode$US$extension";
$filename =~ s/\"|\'//gi;
### code to record call goes here ###
%ast_ver_str = parse_asterisk_version($asterisk_version);
if (( $ast_ver_str{major} = 1 ) && ($ast_ver_str{minor} < 6))
{
$AGI->exec("Monitor wav|/var/spool/asterisk/monitor/MIX/$filename");
}
else
{
$AGI->exec("Monitor","wav,/var/spool/asterisk/monitor/MIX/$filename");
}
### insert record into recording_log table ###
$stmtA = "INSERT INTO recording_log (channel,server_ip,extension,start_time,start_epoch,length_in_sec,filename,lead_id,user,location,vicidial_id) values('$channel','$VARserver_ip','$extension','$now_date','$start_epoch','0','$filename','$lead_id','$accountcode','$filename','$unique_id');";
$affected_rows = $dbhA->do($stmtA);
}
### if set to user log this call, insert user_call_log entry ###
if ($log_user_call =~ /Y/)
{
### insert record into user_call_log table ###
$stmtA = "INSERT INTO user_call_log SET user='$accountcode', call_date='$now_date', call_type='PHONE_DIAL', server_ip='$VARserver_ip', phone_number='$extension', number_dialed='$extension', callerid='$callerid', group_alias_id='$unique_id', lead_id='$lead_id';";
$affected_rows = $dbhA->do($stmtA);
}
### if set to log this call, insert call_log entry ###
if ($log_call =~ /Y/)
{
### insert record into call_log table ###
$stmtA = "INSERT INTO call_log SET uniqueid='$unique_id', channel='$channel', channel_group='PHONE_DIAL', server_ip='$VARserver_ip', type='$type', extension='$accountcode', number_dialed='$extension', caller_code='$callerid', start_time='$now_date', start_epoch='$start_epoch';";
$affected_rows = $dbhA->do($stmtA);
}
### if set to double-log this call, insert call_log entry with uniqueid appended with a 99 ###
if ($double_log =~ /Y/)
{
### insert record into call_log table ###
$stmtA = "INSERT INTO call_log SET uniqueid='" . $unique_id . "99', channel='$channel', channel_group='DOUBLE_LOG', server_ip='$VARserver_ip', type='$type', extension='$accountcode', number_dialed='$extension', caller_code='$callerid', start_time='$now_date', start_epoch='$start_epoch';";
$affected_rows = $dbhA->do($stmtA);
}
exit;
sub get_time_now #get the current date and time and epoch for logging call lengths and datetimes
{
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year = ($year + 1900);
$mon++;
if ($mon < 10) {$mon = "0$mon";}
if ($mday < 10) {$mday = "0$mday";}
if ($hour < 10) {$hour = "0$hour";}
if ($min < 10) {$min = "0$min";}
if ($sec < 10) {$sec = "0$sec";}
$now_date_epoch = time();
$now_date = "$year-$mon-$mday $hour:$min:$sec";
$filedate = "$year$mon$mday$hour$min$sec";
}
sub agi_output
{
if ($AGILOG >=2)
{
### open the log file for writing ###
open(Lout, ">>$AGILOGfile")
|| die "Can't open $AGILOGfile: $!\n";
print Lout "$now_date|$script|$agi_string\n";
close(Lout);
}
### send to STDERR writing ###
if ( ($AGILOG == '1') || ($AGILOG == '3') )
{print STDERR "$now_date|$script|$agi_string\n";}
$agi_string='';
}
# subroutine to parse the asterisk version
# and return a hash with the various part
sub parse_asterisk_version
{
# grab the arguments
my $ast_ver_str = $_[0];
# get everything after the - and put it in $ast_ver_postfix
my @hyphen_parts = split( /-/ , $ast_ver_str );
my $ast_ver_postfix = $hyphen_parts[1];
# now split everything before the - up by the .
my @dot_parts = split( /\./ , $hyphen_parts[0] );
my %ast_ver_hash;
if ( $dot_parts[0] <= 1 )
{
%ast_ver_hash = (
"major" => $dot_parts[0],
"minor" => $dot_parts[1],
"build" => $dot_parts[2],
"revision" => $dot_parts[3],
"postfix" => $ast_ver_postfix
);
}
# digium dropped the 1 from asterisk 10 but we still need it
if ( $dot_parts[0] > 1 )
{
%ast_ver_hash = (
"major" => 1,
"minor" => $dot_parts[0],
"build" => $dot_parts[1],
"revision" => $dot_parts[2],
"postfix" => $ast_ver_postfix
);
}
return ( %ast_ver_hash );
}
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,735 @@
#!/usr/bin/perl
#
# agi-VDAD_RINGALL.agi version 2.8
#
# NOTE: this script is used for on-hook agents to be connected to customer calls
# when they answer their ringing phones
#
# In the default dialplan context, this is set to the following:
# ; these are used for the ring_all function in VICIDIAL
# exten => _8331*.,1,Playback(sip-silence)
# exten => _8331*.,n,AGI(agi-VDAD_RINGALL.agi,${EXTEN})
# exten => _8331*.,n,AGI(agi-VDAD_RINGALL.agi,${EXTEN})
# exten => _8331*.,n,Hangup
#
# Copyright (C) 2014 Matt Florell <vicidial@gmail.com> LICENSE: AGPLv2
#
# changes:
# 110303-2325 - First build
# 110324-2328 - Added recording of remote agent calls, per in-group and campaign recording settings
# 110325-1411 - Added user recording override settings checking for remote agent recording
# 121114-1935 - Added INGROUP recording option
# 130108-1808 - Changes for Asterisk 1.8 compatibility
# 140312-2112 - Added CALLID as recording filename variable
# 140421-1448 - Added code to halt ringing of other channels in ring_all NAC method
#
$script = 'agi-VDAD_RINGALL.agi';
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year = ($year + 1900);
$mon++;
if ($mon < 10) {$mon = "0$mon";}
if ($mday < 10) {$mday = "0$mday";}
if ($hour < 10) {$hour = "0$hour";}
if ($min < 10) {$min = "0$min";}
if ($sec < 10) {$sec = "0$sec";}
$now_date_epoch = time();
$now_date = "$year-$mon-$mday $hour:$min:$sec";
$now_num = "$year$mon$mday$hour$min$sec";
$CIDdate = "$mon$mday$hour$min$sec";
$S='*';
# default path to astguiclient configuration file:
$PATHconf = '/etc/astguiclient.conf';
open(conf, "$PATHconf") || die "can't open $PATHconf: $!\n";
@conf = <conf>;
close(conf);
$i=0;
foreach(@conf)
{
$line = $conf[$i];
$line =~ s/ |>|\n|\r|\t|\#.*|;.*//gi;
if ( ($line =~ /^PATHhome/) && ($CLIhome < 1) )
{$PATHhome = $line; $PATHhome =~ s/.*=//gi;}
if ( ($line =~ /^PATHlogs/) && ($CLIlogs < 1) )
{$PATHlogs = $line; $PATHlogs =~ s/.*=//gi;}
if ( ($line =~ /^PATHagi/) && ($CLIagi < 1) )
{$PATHagi = $line; $PATHagi =~ s/.*=//gi;}
if ( ($line =~ /^PATHweb/) && ($CLIweb < 1) )
{$PATHweb = $line; $PATHweb =~ s/.*=//gi;}
if ( ($line =~ /^PATHsounds/) && ($CLIsounds < 1) )
{$PATHsounds = $line; $PATHsounds =~ s/.*=//gi;}
if ( ($line =~ /^PATHmonitor/) && ($CLImonitor < 1) )
{$PATHmonitor = $line; $PATHmonitor =~ s/.*=//gi;}
if ( ($line =~ /^VARserver_ip/) && ($CLIserver_ip < 1) )
{$VARserver_ip = $line; $VARserver_ip =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_server/) && ($CLIDB_server < 1) )
{$VARDB_server = $line; $VARDB_server =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_database/) && ($CLIDB_database < 1) )
{$VARDB_database = $line; $VARDB_database =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_user/) && ($CLIDB_user < 1) )
{$VARDB_user = $line; $VARDB_user =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_pass/) && ($CLIDB_pass < 1) )
{$VARDB_pass = $line; $VARDB_pass =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_port/) && ($CLIDB_port < 1) )
{$VARDB_port = $line; $VARDB_port =~ s/.*=//gi;}
$i++;
}
if (!$VARDB_port) {$VARDB_port='3306';}
if (!$AGILOGfile) {$AGILOGfile = "$PATHlogs/agiout.$year-$mon-$mday";}
use DBI;
use Asterisk::AGI;
$AGI = new Asterisk::AGI;
$dbhA = DBI->connect("DBI:mysql:$VARDB_database:$VARDB_server:$VARDB_port", "$VARDB_user", "$VARDB_pass")
or die "Couldn't connect to database: " . DBI->errstr;
### Grab Server values from the database
$stmtA = "SELECT agi_output,ext_context,asterisk_version FROM servers where server_ip = '$VARserver_ip';";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
if ($sthArows > 0)
{
$AGILOG = '0';
@aryA = $sthA->fetchrow_array;
$DBagi_output = $aryA[0];
$ext_context = $aryA[1];
$asterisk_version = $aryA[2];
if ($DBagi_output =~ /STDERR/) {$AGILOG = '1';}
if ($DBagi_output =~ /FILE/) {$AGILOG = '2';}
if ($DBagi_output =~ /BOTH/) {$AGILOG = '3';}
}
$sthA->finish();
### begin parsing run-time options ###
$|=1;
while(<STDIN>)
{
chomp;
last unless length($_);
if ($AGILOG)
{
if (/^agi_(\w+)\:\s+(.*)$/)
{
$AGI{$1} = $2;
}
}
if (/^agi_uniqueid\:\s+(.*)$/) {$unique_id = $1;}
if (/^agi_channel\:\s+(.*)$/) {$channel = $1;}
if (/^agi_extension\:\s+(.*)$/) {$extension = $1;}
if (/^agi_type\:\s+(.*)$/) {$type = $1;}
if (/^agi_callerid\:\s+(.*)$/) {$callerid = $1;}
if (/^agi_calleridname\:\s+(.*)$/) {$calleridname = $1;}
if ( $calleridname =~ /\"/) {$calleridname =~ s/\"//gi;}
if ( (!$callerid) or ($callerid =~ /unknown/) or ( ($calleridname =~ /\d\d\d\d\d\d\d\d\d/) && (length($calleridname) > 16) ) )
{$callerid = $calleridname;}
if ( ($callerid =~ / /) && (length($callerid) > 20) )
{$callerid =~ s/ .*//gi;}
$calleridname = $callerid;
if ($extension =~ /^8331\*\d/)
{
@EXTEN_vars = split(/\*/, $extension);
$auto_call_id = $EXTEN_vars[1];
$call_callerid = $EXTEN_vars[2];
$dial_user = $EXTEN_vars[3];
$phone_exten = $EXTEN_vars[4];
$channel_root = $channel;
$channel_root =~ s/\,.*//gi;
}
}
if ($AGILOG) {$agi_string = "AGI Environment Dump:"; &agi_output;}
foreach $i (sort keys %AGI)
{
if ($AGILOG) {$agi_string = " -- $i = $AGI{$i}"; &agi_output;}
}
if ($AGILOG) {$agi_string = "AGI Variables: |$unique_id|$channel|$extension|$type|$callerid|$auto_call_id|"; &agi_output;}
if ($AGILOG) {$agi_string = "RING-AGENT AGI STARTED"; &agi_output;}
### Hangup if Local/ channel, since these are usually invalid or voicemail
if ($channel =~ /^Local/)
{
$AGI->stream_file('sip-silence');
$AGI->stream_file('sip-silence');
$AGI->stream_file('sip-silence');
if ($AGILOG) {$agi_string = "RING-ALL AGI Local Channel, exiting..."; &agi_output;}
exit;
}
### Check that the call exists, if not then buzz and hangup ###
$stmtA = "SELECT campaign_id,call_type,extension,phone_number,lead_id,callerid FROM vicidial_auto_calls where auto_call_id='$auto_call_id';";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$VACcount=$sthA->rows;
if ($VACcount > 0)
{
@aryA = $sthA->fetchrow_array;
$VAC_campaign_id = $aryA[0];
$VAC_call_type = $aryA[1];
$VAC_extension = $aryA[2];
$VAC_phone_number = $aryA[3];
$VAC_lead_id = $aryA[4];
$VAC_call_id = $aryA[5];
if ($AGILOG) {$agi_string = "-- VAC Call Found: |$callerid|$VAC_campaign_id|$VAC_call_type|$VAC_extension|$VAC_phone_number|$VAC_lead_id|$auto_call_id|"; &agi_output;}
$sthA->finish();
}
else
{
if ($AGILOG) {$agi_string = "-- VAC Call Not Found, exiting: |$callerid|$auto_call_id|"; &agi_output;}
$AGI->stream_file('buzz');
$AGI->hangup();
exit;
}
### Check that the user exists in vicidial_live_agents, if not then buzz and hangup ###
$stmtA = "SELECT conf_exten,extension,server_ip,ra_user,campaign_id from vicidial_live_agents WHERE user='$dial_user' and status IN('READY','CLOSER');";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$VLAcount=$sthA->rows;
if ($VLAcount > 0)
{
@aryA = $sthA->fetchrow_array;
$VLAconf_exten = $aryA[0];
$VLAextension = $aryA[1];
$VLAserver_ip = $aryA[2];
$VLAra_user = $aryA[3];
$VLAcampaign_id = $aryA[4];
if ($AGILOG) {$agi_string = "-- VLA Agent Found: |$callerid|$VLAconf_exten|$VLAextension|$VLAserver_ip|$VLAra_user|$VLAcampaign_id|$auto_call_id|"; &agi_output;}
$sthA->finish();
}
else
{
if ($AGILOG) {$agi_string = "-- VLA Agent Not Found, exiting: |$callerid|$auto_call_id|$dial_user|"; &agi_output;}
$AGI->stream_file('buzz');
$AGI->hangup();
exit;
}
$stmtA = "SELECT user,extension,conf_exten,server_ip FROM vicidial_live_agents where ring_callerid='$call_callerid';";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
$orAGENTct=0;
$RINGct=0;
$user_phone='';
while ($sthArows > $RINGct)
{
@aryA = $sthA->fetchrow_array;
$ringVLA_user[$RINGct] = $aryA[0];
$ringVLA_exten[$RINGct] = $aryA[1];
$ringVLA_conf_exten[$RINGct] = $aryA[2];
$ringVLA_server_ip[$RINGct] = $aryA[3];
$ring_user = $aryA[0];
if ($AGILOG) {$agi_string = "-- Ringing agents for this call: |$call_callerid|$RINGct|$ringVLA_user[$RINGct]|$ringVLA_exten[$RINGct]|$ringVLA_conf_exten[$RINGct]|$ringVLA_server_ip[$RINGct]|"; &agi_output;}
if ($dial_user =~ /^$ring_user$/)
{$user_phone=$ringVLA_exten[$RINGct];}
else
{
$orVLA_user[$orAGENTct] = $aryA[0];
$orVLA_exten[$orAGENTct] = $aryA[1];
$orVLA_conf_exten[$orAGENTct] = $aryA[2];
$orVLA_server_ip[$orAGENTct] = $aryA[3];
if ($orVLA_exten[$orAGENTct] =~ /^R\//)
{$orVLA_exten[$orAGENTct] = "Local/" . $orVLA_conf_exten[$orAGENTct] . "\@default";}
$orAGENTct++;
}
$RINGct++;
}
$sthA->finish();
if ($AGILOG) {$agi_string = "-- Ringing stats: |$call_callerid|$RINGct|$orAGENTct|$user_phone|"; &agi_output;}
### Check that the call has not been grabbed already, if it has then buzz and hangup ###
$stmtA = "SELECT count(*) FROM vicidial_auto_calls where auto_call_id='$auto_call_id' and agent_grab='';";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$NAGcount=$sthA->rows;
if ($NAGcount > 0)
{
@aryA = $sthA->fetchrow_array;
$not_grabbed_count = $aryA[0];
$sthA->finish();
if ($not_grabbed_count > 0)
{
if ($VLAextension =~ /^R\//)
{
### BEGIN If remote agent, then reserve a vicidial_conferences session on their server to send the calls to ###
$stmtA="SELECT count(*) FROM vicidial_conferences where server_ip='$VLAserver_ip' and ((extension='') or (extension is null)) and conf_exten != '$VLAconf_exten';";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$AVCcount=$sthA->rows;
if ($AVCcount > 0)
{
@aryA = $sthA->fetchrow_array;
$available_sessions = $aryA[0];
$sthA->finish();
if ($available_sessions > 0)
{
$stmtA="UPDATE vicidial_conferences set extension='3WAY_$dial_user', leave_3way='1', leave_3way_datetime='$now_date' where server_ip='$VLAserver_ip' and ((extension='') or (extension is null)) and conf_exten != '$VLAconf_exten' limit 1;";
$AVCaffected_rows = $dbhA->do($stmtA);
if ($AVCaffected_rows > 0)
{
$stmtA="SELECT conf_exten from vicidial_conferences where server_ip='$VLAserver_ip' and extension='3WAY_$dial_user' and leave_3way_datetime='$now_date' and conf_exten != '$VLAconf_exten';";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$SVCcount=$sthA->rows;
if ($SVCcount > 0)
{
@aryA = $sthA->fetchrow_array;
$VLAconf_exten = $aryA[0];
$sthA->finish();
if ($AGILOG) {$agi_string = "-- AVC Session Reserved: |$callerid|$auto_call_id|$dial_user|3WAY_$dial_user|$now_date|$VLAconf_exten|"; &agi_output;}
### get the recording settings for the campaign that this remote agent is logged into
if (length($VLAcampaign_id) > 0)
{
$stmtA = "SELECT campaign_recording,campaign_rec_filename FROM vicidial_campaigns where campaign_id='$VLAcampaign_id';";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$VCRcount=$sthA->rows;
if ($VCRcount > 0)
{
@aryA = $sthA->fetchrow_array;
$campaign_recording = $aryA[0];
$campaign_rec_filename = $aryA[1];
}
$sthA->finish();
}
### look for the recording settings for the start user of this remote agent
if (length($VLAra_user) > 0)
{
$stmtA = "SELECT vicidial_recording_override,vicidial_recording FROM vicidial_users where user='$VLAra_user';";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArowsVUrec=$sthA->rows;
if ($sthArowsVUrec > 0)
{
@aryA = $sthA->fetchrow_array;
if ($aryA[0] !~ /DISABLED/)
{$campaign_recording = $aryA[0];}
if ($aryA[1] < 1)
{$campaign_recording = 'NONE';}
}
$sthA->finish();
}
### if recording is enabled then start recording on this call before sending to a remote agent
if ($campaign_recording =~ /ALLCALLS|ALLFORCE/)
{
$stmtA = "SELECT vendor_lead_code FROM vicidial_list where lead_id='$VAC_lead_id';";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
if ($sthArows > 0)
{
@aryA = $sthA->fetchrow_array;
$vendor_lead_code = $aryA[0];
}
$sthA->finish();
# get date/time
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year = ($year + 1900);
$Tyear = ($year - 2000);
$mon++;
if ($mon < 10) {$mon = "0$mon";}
if ($mday < 10) {$mday = "0$mday";}
if ($hour < 10) {$hour = "0$hour";}
if ($min < 10) {$min = "0$min";}
if ($sec < 10) {$sec = "0$sec";}
$now_date_epoch = time();
$now_date = "$year-$mon-$mday $hour:$min:$sec";
$recdate = "$year$mon$mday-$hour$min$sec";
$tinydate = "$Tyear$mon$mday$hour$min$sec";
$campaign_rec_filename =~ s/CAMPAIGN/$VLAcampaign_id/gi;
$campaign_rec_filename =~ s/INGROUP/$VAC_campaign_id/gi;
$campaign_rec_filename =~ s/CUSTPHONE/$VAC_phone_number/gi;
$campaign_rec_filename =~ s/FULLDATE/$recdate/gi;
$campaign_rec_filename =~ s/TINYDATE/$tinydate/gi;
$campaign_rec_filename =~ s/EPOCH/$now_date_epoch/gi;
$campaign_rec_filename =~ s/AGENT/$dial_user/gi;
$campaign_rec_filename =~ s/VENDORLEADCODE/$vendor_lead_code/gi;
$campaign_rec_filename =~ s/LEADID/$VAC_lead_id/gi;
$campaign_rec_filename =~ s/CALLID/$VAC_call_id/gi;
$campaign_rec_filename =~ s/\"|\'//gi;
%ast_ver_str = parse_asterisk_version($asterisk_version);
if (( $ast_ver_str{major} = 1 ) && ($ast_ver_str{minor} < 6))
{
$AGI->exec("Monitor wav|/var/spool/asterisk/monitor/MIX/$campaign_rec_filename");
}
else
{
$AGI->exec("Monitor","wav,/var/spool/asterisk/monitor/MIX/$campaign_rec_filename");
}
### insert record into recording_log table ###
$stmtA = "INSERT INTO recording_log (channel,server_ip,extension,start_time,start_epoch,length_in_sec,filename,lead_id,user,location,vicidial_id) values('$channel','$VARserver_ip','$VAC_phone_number','$now_date','$now_date_epoch','0','$campaign_rec_filename','$VAC_lead_id','$dial_user','$campaign_rec_filename','$uniqueid');";
$RLRAaffected_rows = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "-- REMOTE RECORDING STARTED : |$RLRAaffected_rows|$campaign_rec_filename|$stmtA|"; &agi_output;}
}
}
else
{
if ($AGILOG) {$agi_string = "-- AVC No Available Sessions(SELECT3), exiting: |$VLAserver_ip|$auto_call_id|$dial_user|$VLAextension$now_num|"; &agi_output;}
$AGI->stream_file('buzz');
$AGI->hangup();
exit;
}
}
else
{
if ($AGILOG) {$agi_string = "-- AVC No Available Sessions(UPDATE), exiting: |$VLAserver_ip|$auto_call_id|$dial_user|"; &agi_output;}
$AGI->stream_file('buzz');
$AGI->hangup();
exit;
}
}
else
{
if ($AGILOG) {$agi_string = "-- AVC No Available Sessions(SELECT2), exiting: |$VLAserver_ip|$auto_call_id|$dial_user|"; &agi_output;}
$AGI->stream_file('buzz');
$AGI->hangup();
exit;
}
}
else
{
if ($AGILOG) {$agi_string = "-- AVC No Available Sessions(SELECT), exiting: |$VLAserver_ip|$auto_call_id|$dial_user|"; &agi_output;}
$AGI->stream_file('buzz');
$AGI->hangup();
exit;
}
### END If remote agent, then reserve a vicidial_conferences session on their server to send the calls to ###
}
### Try to grab the call for this agent, if already grabbed then buzz and hangup ###
$stmtA = "UPDATE vicidial_auto_calls SET agent_grab='$dial_user',agent_grab_extension='$VLAconf_exten' where auto_call_id='$auto_call_id' and agent_grab='';";
$AGaffected_rows = $dbhA->do($stmtA);
if ($AGaffected_rows > 0)
{
if ($AGILOG) {$agi_string = "-- VAC Call Grabbed: |$callerid|$auto_call_id|$dial_user|$AGaffected_rows|"; &agi_output;}
### Check if this was a RING_ALL method ###
$VIG_next_agent_call='';
$stmtA = "SELECT next_agent_call FROM vicidial_inbound_groups where group_id='$VAC_campaign_id';";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$VIGcount=$sthA->rows;
if ($VIGcount > 0)
{
@aryA = $sthA->fetchrow_array;
$VIG_next_agent_call = $aryA[0];
if ($AGILOG) {$agi_string = "-- Check for in-group next agent call setting: |$callerid|$VAC_campaign_id|$VIG_next_agent_call|"; &agi_output;}
$sthA->finish();
}
if ( ($VIG_next_agent_call =~ /ring_all/i) && ($orAGENTct > 0) )
{
### gather list of ringing channels
$stmtA = "SELECT channel,server_ip FROM live_sip_channels where extension='ring';";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
$rec_count=0;
while ($sthArows > $rec_count)
{
@aryA = $sthA->fetchrow_array;
$LSC_channel[$rec_count] = $aryA[0];
$LSC_server_ip[$rec_count] = $aryA[1];
$LSC_channelREG[$rec_count] = $aryA[0];
$LSC_channelREG[$rec_count] =~ s/\*/STARSYM/gi;
$LSC_channelREG[$rec_count] =~ s/\@/ATSYM/gi;
if ($AGILOG) {$agi_string = "-- Other ringing channels: |$rec_count|$LSC_channel[$rec_count]|$LSC_server_ip[$rec_count]|"; &agi_output;}
$rec_count++;
}
$sthA->finish();
### go through other-agent ringing agent records looking for matches to hang up
if ($rec_count > 0)
{
$orct=0;
foreach(@orVLA_exten)
{
$exten_match='';
if ($orVLA_exten[$orct] =~ /^Local\//)
{
$orVLA_exten[$orct] =~ s/^Local\/|\@.*//gi;
$exten_match = "STARSYM" . $orVLA_exten[$orct] . "ATSYM";
}
if ($orVLA_exten[$orct] =~ /^SIP/i)
{
$orVLA_exten[$orct] =~ s/^SIP\///gi;
$stmtA = "SELECT dialplan_number FROM phones where extension='$orVLA_exten[$orct]' and server_ip='$orVLA_server_ip[$orct]' and protocol='SIP';";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$Pcount=$sthA->rows;
if ($Pcount > 0)
{
@aryA = $sthA->fetchrow_array;
$Pdialplan_number = $aryA[0];
$exten_match = "STARSYM" . $Pdialplan_number . "ATSYM";
}
$sthA->finish();
}
if ($orVLA_exten[$orct] =~ /^IAX/i)
{
$orVLA_exten[$orct] =~ s/^IAX2\/|^IAX\///gi;
$stmtA = "SELECT dialplan_number FROM phones where extension='$orVLA_exten[$orct]' and server_ip='$orVLA_server_ip[$orct]' and protocol='IAX2';";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$Pcount=$sthA->rows;
if ($Pcount > 0)
{
@aryA = $sthA->fetchrow_array;
$Pdialplan_number = $aryA[0];
$exten_match = "STARSYM" . $Pdialplan_number . "ATSYM";
}
$sthA->finish();
}
if ($orVLA_exten[$orct] =~ /^Zap|^DAHDI/i)
{
$orVLA_exten[$orct] =~ s/^Zap\/|^DAHDI\///gi;
$stmtA = "SELECT dialplan_number FROM phones where extension='$orVLA_exten[$orct]' and server_ip='$orVLA_server_ip[$orct]' and protocol='Zap';";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$Pcount=$sthA->rows;
if ($Pcount > 0)
{
@aryA = $sthA->fetchrow_array;
$Pdialplan_number = $aryA[0];
$exten_match = "STARSYM" . $Pdialplan_number . "ATSYM";
}
$sthA->finish();
}
# if ($AGILOG) {$agi_string = "-- Check ORC: |$callerid|$orct|$exten_match|$orVLA_exten[$orct]|$orVLA_exten[$orct]|"; &agi_output;}
if (length($exten_match) > 2)
{
$lscct=0;
foreach(@LSC_channel)
{
# if ($AGILOG) {$agi_string = "-- Check LSC: |$callerid|$orct|$lscct|$exten_match|$LSC_channel[$lscct]|$LSC_channelREG[$lscct]|$orVLA_exten[$orct]|$orVLA_exten[$orct]|"; &agi_output;}
if ($LSC_channelREG[$lscct] =~ /$exten_match/)
{
if ($AGILOG) {$agi_string = "-- Chan to hangup: |$callerid|$exten_match|$LSC_channel[$lscct]|$LSC_server_ip[$lscct]|"; &agi_output;}
### send hangup command for channel
$VHqueryCID = "RGHANGUP$CIDdate$lscct";
$stmtA = "INSERT INTO vicidial_manager values('','','$now_date','NEW','N','$VARserver_ip','','Hangup','$VHqueryCID','Channel: $LSC_channel[$lscct]','','','','','','','','','')";
$AGaffected_rows = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "-- Other Ringing Hangup Sent: |$callerid|$AGaffected_rows|$LSC_channel[$lscct]|$VHqueryCID|"; &agi_output;}
}
$lscct++;
}
}
$orct++;
}
}
}
### send agent to their session ###
if($VLAserver_ip =~ m/(\S+)\.(\S+)\.(\S+)\.(\S+)/ )
{
$a = leading_zero($1);
$b = leading_zero($2);
$c = leading_zero($3);
$d = leading_zero($4);
$VLA_DIALstr = "$a$S$b$S$c$S$d$S";
}
$VLA_DIALstr .= "7$VLAconf_exten";
$AGI->stream_file('sip-silence');
$AGI->stream_file('sip-silence');
$AGI->stream_file('sip-silence');
$AGI->stream_file('sip-silence');
if ($AGILOG) {$agi_string = "-- Ring-Agent Sending Call: |$callerid|$auto_call_id|$dial_user|$VLA_DIALstr|"; &agi_output;}
print "SET CONTEXT $ext_context\n";
$result = <STDIN>;
checkresult($result);
print "SET EXTENSION $VLA_DIALstr\n";
$result = <STDIN>;
checkresult($result);
print "SET PRIORITY 1\n";
$result = <STDIN>;
checkresult($result);
$dbhA->disconnect();
exit;
}
else
{
if ($AGILOG) {$agi_string = "-- VAC Call Already Grabbed(UPDATE2), exiting: |$callerid|$auto_call_id|$dial_user|$AGaffected_rows|"; &agi_output;}
$AGI->stream_file('buzz');
$AGI->hangup();
exit;
}
}
else
{
if ($AGILOG) {$agi_string = "-- VAC Call Already Grabbed(UPDATE), exiting: |$callerid|$auto_call_id|$dial_user|$AGaffected_rows|"; &agi_output;}
$AGI->stream_file('buzz');
$AGI->hangup();
exit;
}
}
else
{
if ($AGILOG) {$agi_string = "-- VAC Call Already Grabbed(SELECT), exiting: |$callerid|$auto_call_id|$dial_user|$AGaffected_rows|"; &agi_output;}
$AGI->stream_file('buzz');
$AGI->hangup();
exit;
}
if ($AGILOG) {$agi_string = "-- sleeping: |$callerid|$auto_call_id|"; &agi_output;}
sleep(10);
exit;
$dbhA->disconnect();
$AGI->stream_file('sip-silence');
if ($AGILOG) {$agi_string = "-- Exiting Ring-Agent AGI: |$callerid|"; &agi_output;}
exit;
sub agi_output
{
if ($AGILOG >=2)
{
### open the log file for writing ###
open(Lout, ">>$AGILOGfile")
|| die "Can't open $AGILOGfile: $!\n";
print Lout "$now_date|$script|$agi_string\n";
close(Lout);
}
### send to STDERR writing ###
if ( ($AGILOG == '1') || ($AGILOG == '3') )
{print STDERR "$now_date|$script|$agi_string\n";}
$agi_string='';
}
sub checkresult
{
my ($res) = @_;
my $retval;
$tests++;
chomp $res;
if ($res =~ /^200/)
{
$res =~ /result=(-?\d+)/;
if (!length($1))
{
# print STDERR "FAIL ($res)\n";
$fail++;
}
else
{
# print STDERR "PASS ($1)\n";
$pass++;
}
}
else
{
# print STDERR "FAIL (unexpected result '$res')\n";
$fail++;
}
}
sub leading_zero($)
{
$_ = $_[0];
s/^(\d)$/0$1/;
s/^(\d\d)$/0$1/;
return $_;
} # End of the leading_zero() routine.
# subroutine to parse the asterisk version
# and return a hash with the various part
sub parse_asterisk_version
{
# grab the arguments
my $ast_ver_str = $_[0];
# get everything after the - and put it in $ast_ver_postfix
my @hyphen_parts = split( /-/ , $ast_ver_str );
my $ast_ver_postfix = $hyphen_parts[1];
# now split everything before the - up by the .
my @dot_parts = split( /\./ , $hyphen_parts[0] );
my %ast_ver_hash;
if ( $dot_parts[0] <= 1 )
{
%ast_ver_hash = (
"major" => $dot_parts[0],
"minor" => $dot_parts[1],
"build" => $dot_parts[2],
"revision" => $dot_parts[3],
"postfix" => $ast_ver_postfix
);
}
# digium dropped the 1 from asterisk 10 but we still need it
if ( $dot_parts[0] > 1 )
{
%ast_ver_hash = (
"major" => 1,
"minor" => $dot_parts[0],
"build" => $dot_parts[1],
"revision" => $dot_parts[2],
"postfix" => $ast_ver_postfix
);
}
return ( %ast_ver_hash );
}
@@ -0,0 +1,813 @@
#!/usr/bin/perl
#
# agi-VDAD_inbound_calltime_check.agi version 2.8
#
# Runs when a call comes in from an inbound call. This script will check the
# settings of the defined calltime scheme in VICIDIAL and either send the call
# to the after-hours method for the defined IN-GROUP or exit to let the call
# continue. This script is also used by Call Menus.
#
# You need to put lines similar to those below in your extensions.conf file:
# ; Below are the parameters needed for the script to be run properly
# ; 1. the in-group to take settings from
# ; 2. whether to log the call into the vicidial_auto_calls table:
# ; - YES - insert the call as status IVR
# ; - NO - do not insert call into vac table
# ; 3. note to put in the comment_b field when record inserted into the
# ; live_inbound_log table, no spaces in this field, use underscore '_'
# ; default if not populated is NONE
# ; 4. call time scheme override
# ; 5. call route after hours override
# ; 6. call route value after hours override
# ; 7. call route context after hours override
# ; 8. vicidial_list query qualification (YES/NO, default is NO)
# ; * only works with lead_id-populated calleridname phone calls.
# ; This option allows you to specify a SQL fragment used to do a count(*)
# ; against the vicidial_list table for the lead and other field values
# ; This feature will look for the qualify_sql in the cacll menu record
# ;
# ;inbound calls check calltime:
#exten => 1234,1,Answer ; Answer the line
#exten => 1234,2,AGI(agi-VDAD_inbound_calltime_check.agi,INBOUND-----YES-----START-----24hours-----EXTENSION-----101-----default-----NO)
#exten => 1234,3,Hangup
#
# Copyright (C) 2014 Matt Florell <vicidial@gmail.com> LICENSE: AGPLv2
#
# changes:
# 80525-0144 - First Build
# 81015-0305 - Added drop logging
# 81015-2009 - Added more logging and NOTE option
# 81017-0238 - Fixed duplication bug for vac in multi-entry dialplans
# 90504-1555 - Added call time scheme override
# 90621-1130 - Fixed issue when no call time is specified
# 91122-2353 - Added QueueMetrics logging as IVR INFO entries
# 101219-2110 - Added call time overflow
# 110525-1347 - Added compatibility for outbound IVR logging
# 111229-1716 - Changed call time overflow to look at previous day stop time
# 120517-1257 - Added CALLMENU timeout option
# 120706-1659 - Added Qualify SQL option
# 120719-1411 - Fixed issue with inbound IVR calls not showing up in real-time screen
# 120730-0727 - Fixed issue with non-populating phone number
# 121124-1414 - Added call times holidays function
# 130108-1807 - Changes for Asterisk 1.8 compatibility
# 130925-1826 - Added variable filter to prevent DID SQL injection attack
# 140126-1141 - Added VMAIL_NO_INST options
#
$script = 'agi-VDAD_inbound_calltime_check.agi';
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year = ($year + 1900);
$mon++;
if ($mon < 10) {$mon = "0$mon";}
if ($mday < 10) {$mday = "0$mday";}
if ($hour < 10) {$hour = "0$hour";}
if ($min < 10) {$min = "0$min";}
if ($sec < 10) {$sec = "0$sec";}
$hm = "$hour$min";
$hm = ($hm + 0);
$now_date_epoch = time();
$now_date = "$year-$mon-$mday $hour:$min:$sec";
$CLInow_date = "$year-$mon-$mday\\ $hour:$min:$sec";
$YMD = "$year-$mon-$mday";
$start_time=$now_date;
$CIDdate = "$mon$mday$hour$min$sec";
$tsSQLdate = "$year$mon$mday$hour$min$sec";
$SQLdate = "$year-$mon-$mday $hour:$min:$sec";
$SQLdateBEGIN = $SQLdate;
$BDtarget = ($now_date_epoch + 600);
($Bsec,$Bmin,$Bhour,$Bmday,$Bmon,$Byear,$Bwday,$Byday,$Bisdst) = localtime($BDtarget);
$Byear = ($Byear + 1900);
$Bmon++;
if ($Bmon < 10) {$Bmon = "0$Bmon";}
if ($Bmday < 10) {$Bmday = "0$Bmday";}
if ($Bhour < 10) {$Bhour = "0$Bhour";}
if ($Bmin < 10) {$Bmin = "0$Bmin";}
if ($Bsec < 10) {$Bsec = "0$Bsec";}
$last_update_time = "$Byear$Bmon$Bmday$Bhour$Bmin$Bsec";
# default path to astguiclient configuration file:
$PATHconf = '/etc/astguiclient.conf';
open(conf, "$PATHconf") || die "can't open $PATHconf: $!\n";
@conf = <conf>;
close(conf);
$i=0;
foreach(@conf)
{
$line = $conf[$i];
$line =~ s/ |>|\n|\r|\t|\#.*|;.*//gi;
if ( ($line =~ /^PATHhome/) && ($CLIhome < 1) )
{$PATHhome = $line; $PATHhome =~ s/.*=//gi;}
if ( ($line =~ /^PATHlogs/) && ($CLIlogs < 1) )
{$PATHlogs = $line; $PATHlogs =~ s/.*=//gi;}
if ( ($line =~ /^PATHagi/) && ($CLIagi < 1) )
{$PATHagi = $line; $PATHagi =~ s/.*=//gi;}
if ( ($line =~ /^PATHweb/) && ($CLIweb < 1) )
{$PATHweb = $line; $PATHweb =~ s/.*=//gi;}
if ( ($line =~ /^PATHsounds/) && ($CLIsounds < 1) )
{$PATHsounds = $line; $PATHsounds =~ s/.*=//gi;}
if ( ($line =~ /^PATHmonitor/) && ($CLImonitor < 1) )
{$PATHmonitor = $line; $PATHmonitor =~ s/.*=//gi;}
if ( ($line =~ /^VARserver_ip/) && ($CLIserver_ip < 1) )
{$VARserver_ip = $line; $VARserver_ip =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_server/) && ($CLIDB_server < 1) )
{$VARDB_server = $line; $VARDB_server =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_database/) && ($CLIDB_database < 1) )
{$VARDB_database = $line; $VARDB_database =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_user/) && ($CLIDB_user < 1) )
{$VARDB_user = $line; $VARDB_user =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_pass/) && ($CLIDB_pass < 1) )
{$VARDB_pass = $line; $VARDB_pass =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_port/) && ($CLIDB_port < 1) )
{$VARDB_port = $line; $VARDB_port =~ s/.*=//gi;}
$i++;
}
if (!$VARDB_port) {$VARDB_port='3306';}
if (!$AGILOGfile) {$AGILOGfile = "$PATHlogs/agiout.$year-$mon-$mday";}
if (!$PRSLOGfile) {$PRSLOGfile = "$PATHlogs/prsout.$year-$mon-$mday";}
if (!$PRSTESTfile) {$PRSTESTfile = "$PATHlogs/prstest.$year-$mon-$mday";}
use DBI;
use Time::HiRes ('gettimeofday','usleep','sleep'); # necessary to have perl sleep command of less than one second
use Asterisk::AGI;
$AGI = new Asterisk::AGI;
$dbhA = DBI->connect("DBI:mysql:$VARDB_database:$VARDB_server:$VARDB_port", "$VARDB_user", "$VARDB_pass")
or die "Couldn't connect to database: " . DBI->errstr;
### Grab Server values from the database
$stmtA = "SELECT agi_output FROM servers where server_ip = '$VARserver_ip';";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
if ($sthArows > 0)
{
$AGILOG = '0';
@aryA = $sthA->fetchrow_array;
$DBagi_output = $aryA[0];
if ($DBagi_output =~ /STDERR/) {$AGILOG = '1';}
if ($DBagi_output =~ /FILE/) {$AGILOG = '2';}
if ($DBagi_output =~ /BOTH/) {$AGILOG = '3';}
}
$sthA->finish();
#############################################
##### START QUEUEMETRICS LOGGING LOOKUP #####
$stmtA = "SELECT enable_queuemetrics_logging,queuemetrics_server_ip,queuemetrics_dbname,queuemetrics_login,queuemetrics_pass,queuemetrics_log_id,queuemetrics_eq_prepend,call_menu_qualify_enabled FROM system_settings;";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
if ($sthArows > 0)
{
@aryA = $sthA->fetchrow_array;
$enable_queuemetrics_logging = $aryA[0];
$queuemetrics_server_ip = $aryA[1];
$queuemetrics_dbname = $aryA[2];
$queuemetrics_login= $aryA[3];
$queuemetrics_pass = $aryA[4];
$queuemetrics_log_id = $aryA[5];
$queuemetrics_eq_prepend = $aryA[6];
$call_menu_qualify_enabled = $aryA[7];
}
$sthA->finish();
##### END QUEUEMETRICS LOGGING LOOKUP #####
###########################################
### begin parsing run-time options ###
if (length($ARGV[0])>1)
{
if ($AGILOG) {$agi_string = "Perl Environment Dump:"; &agi_output;}
$i=0;
while ($#ARGV >= $i)
{
$args = "$args $ARGV[$i]";
if ($AGILOG) {$agi_string = "$i|$ARGV[$i]"; &agi_output;}
$i++;
}
### list of command-line array arguments:
@ARGV_vars = split(/-----/, $ARGV[0]);
$channel_group = $ARGV_vars[0];
$log_to_vac = $ARGV_vars[1];
$note = $ARGV_vars[2];
if (length($note)<1) {$note = 'NONE';}
$call_time_override = $ARGV_vars[3];
$route_override = $ARGV_vars[4];
$route_value_override = $ARGV_vars[5];
$route_context_override = $ARGV_vars[6];
$query_qualification = $ARGV_vars[7];
}
$|=1;
while(<STDIN>)
{
chomp;
last unless length($_);
if ($AGILOG)
{
if (/^agi_(\w+)\:\s+(.*)$/)
{
$AGI{$1} = $2;
}
}
if (/^agi_context\:\s+(.*)$/) {$context = $1;}
if (/^agi_priority\:\s+(.*)$/) {$priority = $1;}
if (/^agi_uniqueid\:\s+(.*)$/) {$unique_id = $1; $uniqueid = $unique_id;}
if (/^agi_channel\:\s+(.*)$/) {$channel = $1;}
if (/^agi_extension\:\s+(.*)$/) {$extension = $1;}
if (/^agi_type\:\s+(.*)$/) {$type = $1;}
if (/^agi_callerid\:\s+(.*)$/) {$callerid = $1;}
if (/^agi_calleridname\:\s+(.*)$/) {$calleridname = $1;}
}
$callerid =~ s/\'|\\\\|\\\|\\|\\;|\\\;|\;|;//gi;
$calleridname =~ s/\'|\\\\|\\\|\\|\\;|\\\;|\;|;//gi;
$extension =~ s/\'|\"|\\\\|\\\|\\|\\;|\\\;|\;|;//gi;
$outboundIVR=0;
$ingroupIVR=0;
$lead_id='';
if ($calleridname =~ /^V\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d|^Y\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d/)
{
$stmtA = "SELECT campaign_id,lead_id FROM vicidial_auto_calls where callerid='$calleridname';";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
if ($sthArows > 0)
{
@aryA = $sthA->fetchrow_array;
$campaign_id = $aryA[0];
$lead_id = $aryA[1];
}
else
{
$lead_id = substr($calleridname, 10, 10);
$lead_id = ($lead_id + 0);
$callerid =~ s/\D|\'//gi;
if (length($callerid)>0) {$phone_number = $callerid;}
else {$phone_number = '';}
$phone_code='1';
}
$sthA->finish();
if ($calleridname =~ /^V\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d/)
{$outboundIVR=1;}
if ($calleridname =~ /^Y\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d/)
{$ingroupIVR=1;}
if (length($phone_number) < 6)
{
$stmtA = "SELECT phone_number FROM vicidial_list where lead_id='$lead_id';";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
if ($sthArows > 0)
{
@aryA = $sthA->fetchrow_array;
$phone_number = $aryA[0];
}
$sthA->finish();
}
$callerid = $calleridname;
}
else
{
if ( ($callerid =~ /\".*\"/) && ( (!$calleridname) or ($calleridname =~ /unknown/) ) )
{
$calleridname = $callerid;
$calleridname =~ s/\<\d\d\d\d\d\d\d\d\d\d\>//gi;
$calleridname =~ s/\"|\" //gi;
}
$callerid =~ s/\D|\'//gi;
$calleridname =~ s/unknown|\'//gi;
if ( (!$callerid) or ($callerid =~ /unknown/) )
{$callerid = $calleridname;}
if (length($callerid)>0) {$phone_number = $callerid;}
else {$phone_number = '';}
if (length($calleridname)>0) {$VLcomments = $calleridname;}
else {$VLcomments = '';}
}
if (length($callerid)<8) {$callerid = $parked_by;}
if (length($pin)>0) {$callerid = $pin;}
foreach $i (sort keys %AGI)
{
if ($AGILOG) {$agi_string = " -- $i = $AGI{$i}"; &agi_output;}
}
if ($AGILOG) {$agi_string = "AGI Variables: |$unique_id|$channel|$extension|$type|$callerid|"; &agi_output;}
if (length($pin) < 1) {$pin=$inbound_number;}
$fronter = $pin;
if ($AGILOG) {$agi_string = "+++++ INBOUND CALL VDCL STARTED : |$channel_group|$callerid-$pin|$now_date"; &agi_output;}
$VDADphone='';
$VDADphone_code='';
if ($channel =~ /Local/i)
{
if ($AGILOG) {$agi_string = "+++++ VDAD START LOCAL CHANNEL: EXITING- $priority"; &agi_output;}
exit;
}
### Grab Server values from the database
$cbc=0;
$stmtA = "SELECT voicemail_dump_exten,ext_context,answer_transfer_agent,local_gmt,asterisk_version,max_vicidial_trunks,voicemail_dump_exten_no_inst FROM servers where server_ip = '$VARserver_ip';";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
while ($sthArows > $cbc)
{
@aryA = $sthA->fetchrow_array;
$DBvoicemail_dump_exten = $aryA[0];
$DBext_context = $aryA[1];
$DBanswer_transfer_agent = $aryA[2];
$DBSERVER_GMT = $aryA[3];
$DBasterisk_version = $aryA[4];
$DBmax_vicidial_trunks = $aryA[5];
$voicemail_dump_exten_no_inst = $aryA[6];
if ($DBvoicemail_dump_exten) {$voicemail_dump_exten = $DBvoicemail_dump_exten;}
if ($DBext_context) {$ext_context = $DBext_context;}
if ($DBanswer_transfer_agent) {$answer_transfer_agent = $DBanswer_transfer_agent;}
if ($DBSERVER_GMT) {$SERVER_GMT = $DBSERVER_GMT;}
if ($DBasterisk_version) {$AST_ver = $DBasterisk_version;}
if ($DBmax_vicidial_trunks) {$max_vicidial_trunks = $DBmax_vicidial_trunks;}
$cbc++;
}
$sthA->finish();
if (length($channel_group) > 1)
{
### Grab inbound groups values from the database
$cbc=0;
$stmtA = "SELECT call_time_id,after_hours_action,after_hours_message_filename,after_hours_exten,after_hours_voicemail,welcome_message_filename,moh_context,onhold_prompt_filename,prompt_interval,agent_alert_exten,agent_alert_delay,drop_call_seconds,drop_action,drop_exten,next_agent_call,voicemail_ext,queue_priority,drop_inbound_group,afterhours_xfer_group FROM vicidial_inbound_groups where group_id = '$channel_group';";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
while ($sthArows > $cbc)
{
@aryA = $sthA->fetchrow_array;
$call_time_id = $aryA[0];
$after_hours_action = $aryA[1];
$after_hours_message_filename = $aryA[2];
$after_hours_exten = $aryA[3];
$after_hours_voicemail = $aryA[4];
$welcome_message_filename = $aryA[5];
$moh_context = $aryA[6];
$onhold_prompt_filename = $aryA[7];
$prompt_interval = $aryA[8];
$agent_alert_exten = $aryA[9];
$agent_alert_delay = $aryA[10];
$drop_call_seconds = $aryA[11];
$DROP_TIME = $drop_call_seconds;
$drop_action = $aryA[12];
$drop_exten = $aryA[13];
$CAMP_callorder = $aryA[14];
$VDADvoicemail_ext = $aryA[15];
$VDADvoicemail_ext =~ s/\D//gi;
$queue_priority = $aryA[16];
$drop_inbound_group = $aryA[17];
$afterhours_xfer_group = $aryA[18];
$cbc++;
}
$sthA->finish();
}
if (length($call_time_override) > 1)
{
$call_time_id = $call_time_override;
}
if (length($route_override) > 1)
{
$after_hours_action = $route_override;
}
if (length($route_value_override) > 1)
{
$after_hours_message_filename = $route_value_override;
$after_hours_exten = $route_value_override;
$after_hours_voicemail = $route_value_override;
}
if (length($route_context_override) > 1)
{
$ext_context = $route_context_override;
}
if ($wday==0)
{
$daySQL=',ct_sunday_start,ct_sunday_stop';
$yestSQL=',ct_saturday_start,ct_saturday_stop';
}
if ($wday==1)
{
$daySQL=',ct_monday_start,ct_monday_stop';
$yestSQL=',ct_sunday_start,ct_sunday_stop';
}
if ($wday==2)
{
$daySQL=',ct_tuesday_start,ct_tuesday_stop';
$yestSQL=',ct_monday_start,ct_monday_stop';
}
if ($wday==3)
{
$daySQL=',ct_wednesday_start,ct_wednesday_stop';
$yestSQL=',ct_tuesday_start,ct_tuesday_stop';
}
if ($wday==4)
{
$daySQL=',ct_thursday_start,ct_thursday_stop';
$yestSQL=',ct_wednesday_start,ct_wednesday_stop';
}
if ($wday==5)
{
$daySQL=',ct_friday_start,ct_friday_stop';
$yestSQL=',ct_thursday_start,ct_thursday_stop';
}
if ($wday==6)
{
$daySQL=',ct_saturday_start,ct_saturday_stop';
$yestSQL=',ct_friday_start,ct_friday_stop';
}
$ct_default_start = '0';
$ct_default_stop = '9999';
### Grab call_times values from the database
$stmtA = "SELECT ct_default_start,ct_default_stop $daySQL $yestSQL,ct_holidays FROM vicidial_call_times where call_time_id = '$call_time_id';";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
if ($sthArows > 0)
{
@aryA = $sthA->fetchrow_array;
$ct_default_start = $aryA[0];
$ct_default_stop = $aryA[1];
$ct_day_start = $aryA[2];
$ct_day_stop = $aryA[3];
$ct_yest_start = $aryA[4];
$ct_yest_stop = $aryA[5];
$ct_holidays = $aryA[6];
$sthA->finish();
if ( ($ct_yest_start < 1) && ($ct_yest_stop < 1) )
{
$ct_yest_start = $ct_default_start;
$ct_yest_stop = $ct_default_stop;
}
if ( ($ct_day_start > 0) || ($ct_day_stop > 0) )
{
$ct_default_start = $ct_day_start;
$ct_default_stop = $ct_day_stop;
}
}
if (length($ct_holidays) > 2)
{
$stmtA = "SELECT count(*) from vicidial_call_time_holidays where holiday_date='$YMD' and holiday_status='ACTIVE';";
if ($DBX) {print " |$stmtA|\n";}
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
@aryA = $sthA->fetchrow_array;
$holiday_today_count = $aryA[0];
if ($AGILOG) {$agi_string = "Holiday check: |$holiday_today_count|$stmtA|$ct_holidays|"; &agi_output;}
$ct_hrs=0;
$b=0;
if ($holiday_today_count > 0)
{
@holiday_rules = split(/\|/,$ct_holidays);
$ct_hrs = ($#holiday_rules - 0);
while($ct_hrs >= $b)
{
if (length($holiday_rules[$b])>1)
{
$stmtA = "SELECT holiday_id,holiday_date,holiday_name,ct_default_start,ct_default_stop,default_afterhours_filename_override from vicidial_call_time_holidays where holiday_id='$holiday_rules[$b]' and holiday_status='ACTIVE' and holiday_date='$YMD';";
if ($DBX) {print " |$stmtA|\n";}
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
if ($sthArows > 0)
{
@aryA = $sthA->fetchrow_array;
$holiday_id = $aryA[0];
$holiday_date = $aryA[1];
$holiday_name = $aryA[2];
$ct_default_start = $aryA[3];
$ct_default_stop = $aryA[4];
$default_afterhours_filename_override = $aryA[5];
if ($AGILOG) {$agi_string = "Active Holiday found! |$holiday_id|$holiday_date|$holiday_name|$ct_default_start|$ct_default_stop|$default_afterhours_filename_override|"; &agi_output;}
}
$sthA->finish();
}
$b++;
}
}
}
$AGI->stream_file('sip-silence');
$AGI->stream_file('sip-silence');
##### BEGIN AFTER HOURS CHECK #####
$ct_stop_overflow=0; # allow for overflow time, past midnight
if ($ct_yest_stop > 2400)
{$ct_stop_overflow = ($ct_yest_stop - 2400);}
if ( ( ($hm < $ct_default_start) || ($hm > $ct_default_stop) ) && ($hm >= $ct_stop_overflow) )
{
$VHqueryCID = "VA$CIDdate$hour$min$sec$sec";
if ($outboundIVR > 0)
{
$stmtA = "DELETE FROM vicidial_auto_calls where callerid='$callerid' and campaign_id='$campaign_id' and server_ip='$VARserver_ip' order by call_time desc limit 1;";
$affected_rows = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "-- AH VDAC vac record deleted: |$affected_rows|$campaign_id|$callerid|$call_time_id|"; &agi_output;}
$stmtA = "INSERT INTO vicidial_outbound_ivr_log (uniqueid,caller_code,event_date,campaign_id,lead_id,menu_id,menu_action) values('$uniqueid','$callerid','$SQLdate','$campaign_id','$lead_id','$context','AFTERHOURSDROP')";
if ($AGILOG) {$agi_string = "|$stmtA|"; &agi_output;}
$affected_rowsL = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "-- VOIL insert: |$affected_rowsL|$uniqueid|$callerid|$campaign_id|$lead_id|$context|"; &agi_output;}
}
else
{
$stmtA = "DELETE FROM vicidial_auto_calls where callerid='$callerid' and uniqueid='$uniqueid' and server_ip='$VARserver_ip' order by call_time desc limit 1;";
$affected_rows = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "-- AH VDAC vac record deleted: |$affected_rows|$uniqueid|$callerid|$call_time_id|"; &agi_output;}
$stmtA = "INSERT INTO live_inbound_log (uniqueid,channel,server_ip,caller_id,extension,phone_ext,start_time,comment_a,comment_b,comment_c,comment_d,comment_e) values('$uniqueid','$channel','$VARserver_ip','$callerid','$extension','$phone_number','$SQLdate','$channel_group','$note','AFTERHOURSDROP','$context','$priority')";
if ($AGILOG) {$agi_string = "|$stmtA|"; &agi_output;}
$affected_rowsL = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "-- AH LIL insert: |$affected_rowsL|$uniqueid|$callerid|$extension|"; &agi_output;}
}
### QueueMetrics logging if enabled
if ($enable_queuemetrics_logging > 0)
{
$dbhB = DBI->connect("DBI:mysql:$queuemetrics_dbname:$queuemetrics_server_ip:3306", "$queuemetrics_login", "$queuemetrics_pass")
or die "Couldn't connect to database: " . DBI->errstr;
if ($DBX) {print "CONNECTED TO QueueMetrics DATABASE: $queuemetrics_server_ip|$queuemetrics_dbname\n";}
$stmtB = "INSERT INTO queue_log SET partition='P01',time_id='$now_date_epoch',call_id='$uniqueid',queue='NONE',agent='NONE',verb='INFO',data1='IVR',data2='$context',serverid='$queuemetrics_log_id';";
if ($AGILOG) {$agi_string = "|$stmtB|"; &agi_output;}
$Baffected_rows = $dbhB->do($stmtB);
$dbhB->disconnect();
}
if ($after_hours_action =~ /EXTENSION|VOICEMAIL|IN_GROUP|CALLMENU/)
{
if ($after_hours_action =~ /CALLMENU/)
{
$DROPexten = 's';
$ext_context = "$after_hours_exten";
}
if ($after_hours_action =~ /EXTENSION/)
{
$DROPexten = "$after_hours_exten";
}
if ($after_hours_action =~ /VOICEMAIL|VMAIL_NO_INST/)
{
if ($drop_action =~ /VMAIL_NO_INST/)
{$voicemail_dump_exten = $voicemail_dump_exten_no_inst;}
$DROPexten = "$voicemail_dump_exten$after_hours_voicemail";
}
if ($after_hours_action =~ /IN_GROUP/)
{ # 90009*CL_uk3survy_*8301*10000123*universal*7275551212*1234*"
$S='*';
$DROPexten = "90009*$afterhours_xfer_group$S$S$insert_lead_id$S$S$phone_number$S$fronter$S";
}
if (length($DROPexten)>0)
{ ### if DROP extension is defined then send the dropped call there instead of hangup
$AGI->stream_file('sip-silence'); # stop music-on-hold process
$AGI->stream_file('sip-silence'); # stop music-on-hold process
sleep(1);
if ($AGILOG) {$agi_string = "exiting the VDAD app after hours, transferring call to $DROPexten |$hm|$ct_default_start|$ct_default_stop|$ct_stop_overflow|"; &agi_output;}
print "SET CONTEXT $ext_context\n";
$result = <STDIN>;
checkresult($result);
print "SET EXTENSION $DROPexten\n";
$result = <STDIN>;
checkresult($result);
print "SET PRIORITY 1\n";
$result = <STDIN>;
checkresult($result);
}
}
if ($after_hours_action =~ /MESSAGE|HANGUP/)
{
$AGI->stream_file('sip-silence');
$AGI->stream_file('sip-silence');
$AGI->stream_file("$after_hours_message_filename");
sleep(1);
### insert a NEW record to the vicidial_manager table to hangup the channel
$stmtA = "INSERT INTO vicidial_manager values('','','$SQLdate','NEW','N','$VARserver_ip','','Hangup','$VHqueryCID','Channel: $channel','','','','','','','','','')";
$affected_rows = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "-- VDCL call_hungup after hours: |$VHqueryCID|$VDADconf_exten|$channel|insert to vicidial_manager"; &agi_output;}
}
$dbhA->disconnect();
exit;
}
##### END AFTER HOURS CHECK #####
##### BEGIN QUALIFY SQL CHECK #####
if ( ($call_menu_qualify_enabled > 0) && ($query_qualification =~ /YES/) && ( ($outboundIVR > 0) || ($ingroupIVR > 0) ) )
{
$qualify_count=0;
$stmtA = "SELECT qualify_sql FROM vicidial_call_menu where menu_id='$context';";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
if ($sthArows > 0)
{
@aryA = $sthA->fetchrow_array;
$qualify_sql = $aryA[0];
}
$sthA->finish();
if (length($qualify_sql) > 5)
{
$lead_id = substr($calleridname, 10, 10);
$lead_id = ($lead_id + 0);
$stmtA = "SELECT count(*) FROM vicidial_list where lead_id='$lead_id' and $qualify_sql;";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
if ($sthArows > 0)
{
@aryA = $sthA->fetchrow_array;
$qualify_count = $aryA[0];
}
$sthA->finish();
if ($AGILOG) {$agi_string = "Exiting the TimeCheck App Qualify SQL |$qualify_count|$lead_id|$context|$calleridname|$stmtA|"; &agi_output;}
if ($qualify_count < 1)
{
$AGI->stream_file('sip-silence'); # stop music-on-hold process
$AGI->stream_file('sip-silence'); # stop music-on-hold process
# sleep for one tenth of a second
usleep(1*100*1000);
if ($AGILOG) {$agi_string = " TimeCheck App Qualify SQL FAIL, transferring call to D |$qualify_count|$lead_id|$context|$calleridname|"; &agi_output;}
print "SET CONTEXT $context\n";
$result = <STDIN>;
checkresult($result);
print "SET EXTENSION D\n";
$result = <STDIN>;
checkresult($result);
print "SET PRIORITY 1\n";
$result = <STDIN>;
checkresult($result);
exit;
}
}
}
##### END QUALIFY SQL CHECK #####
### insert an IVR record to the vicidial_auto_calls table
if ($outboundIVR > 0)
{
$stmtA = "INSERT INTO vicidial_outbound_ivr_log (uniqueid,caller_code,event_date,campaign_id,lead_id,menu_id,menu_action) values('$uniqueid','$callerid','$SQLdate','$campaign_id','$lead_id','$context','')";
if ($AGILOG) {$agi_string = "|$stmtA|"; &agi_output;}
$affected_rowsL = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "-- VOIL insert: |$affected_rowsL|$uniqueid|$callerid|$campaign_id|$lead_id|$context|"; &agi_output;}
$stmtA = "UPDATE vicidial_auto_calls SET last_update_time='$last_update_time',status='IVR' where callerid='$callerid';";
if ($AGILOG) {$agi_string = "|$stmtA|"; &agi_output;}
$affected_rowsV = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "-- VAC update: |$affected_rowsV|$uniqueid|$callerid|$last_update_time|"; &agi_output;}
}
else
{
if ($log_to_vac =~ /YES/)
{
if ($ingroupIVR > 0)
{
$stmtA = "UPDATE vicidial_auto_calls SET last_update_time='$last_update_time',status='IVR' where callerid='$callerid';";
if ($AGILOG) {$agi_string = "|$stmtA|"; &agi_output;}
$affected_rowsV = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "-- VAC update: |$affected_rowsV|$uniqueid|$callerid|$last_update_time|"; &agi_output;}
if ($affected_rowsV < 1)
{
$stmtA = "INSERT INTO vicidial_auto_calls (server_ip,campaign_id,lead_id,status,uniqueid,callerid,channel,phone_code,phone_number,call_time,call_type,stage,queue_priority,last_update_time) values('$VARserver_ip','$channel_group','$lead_id','IVR','$uniqueid','$callerid','$channel','$phone_code','$phone_number','$SQLdate','IN','IVR-0','0','$last_update_time')";
if ($AGILOG) {$agi_string = "|$stmtA|"; &agi_output;}
$affected_rowsV = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "-- VAC insert: |$affected_rowsV|$uniqueid|$callerid|$extension|$lead_id|"; &agi_output;}
}
}
else
{
$stmtA = "DELETE FROM vicidial_auto_calls where callerid='$callerid' and uniqueid='$uniqueid' and server_ip='$VARserver_ip';";
$affected_rows = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "-- VDAC vac record deleted: |$affected_rows| $uniqueid|$callerid|"; &agi_output;}
$stmtA = "INSERT INTO vicidial_auto_calls (server_ip,campaign_id,lead_id,status,uniqueid,callerid,channel,phone_code,phone_number,call_time,call_type,stage,queue_priority,last_update_time) values('$VARserver_ip','$channel_group','0','IVR','$uniqueid','$callerid','$channel','$phone_code','$phone_number','$SQLdate','IN','IVR-0','$queue_priority','$last_update_time')";
if ($AGILOG) {$agi_string = "|$stmtA|"; &agi_output;}
$affected_rowsV = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "-- VAC insert: |$affected_rowsV|$uniqueid|$callerid|$extension|"; &agi_output;}
}
}
$stmtA = "INSERT INTO live_inbound_log (uniqueid,channel,server_ip,caller_id,extension,phone_ext,start_time,comment_a,comment_b,comment_d,comment_e) values('$uniqueid','$channel','$VARserver_ip','$callerid','$extension','$phone_number','$SQLdate','$channel_group','$note','$context','$priority')";
if ($AGILOG) {$agi_string = "|$stmtA|"; &agi_output;}
$affected_rowsL = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "-- LIL insert: |$affected_rowsL|$uniqueid|$callerid|$extension|"; &agi_output;}
}
### QueueMetrics logging if enabled
if ($enable_queuemetrics_logging > 0)
{
$dbhB = DBI->connect("DBI:mysql:$queuemetrics_dbname:$queuemetrics_server_ip:3306", "$queuemetrics_login", "$queuemetrics_pass")
or die "Couldn't connect to database: " . DBI->errstr;
if ($DBX) {print "CONNECTED TO QueueMetrics DATABASE: $queuemetrics_server_ip|$queuemetrics_dbname\n";}
if ($outboundIVR > 0)
{$uniqueid = $caller_id;}
$stmtB = "INSERT INTO queue_log SET partition='P01',time_id='$now_date_epoch',call_id='$uniqueid',queue='NONE',agent='NONE',verb='INFO',data1='IVR',data2='$context',serverid='$queuemetrics_log_id';";
if ($AGILOG) {$agi_string = "|$stmtB|"; &agi_output;}
$Baffected_rows = $dbhB->do($stmtB);
$dbhB->disconnect();
}
$sthA->finish();
$dbhA->disconnect();
exit;
sub checkresult
{
my ($res) = @_;
my $retval;
$tests++;
chomp $res;
if ($res =~ /^200/)
{
$res =~ /result=(-?\d+)/;
if (!length($1))
{
# print STDERR "FAIL ($res)\n";
$fail++;
}
else
{
# print STDERR "PASS ($1)\n";
$pass++;
}
}
else
{
# print STDERR "FAIL (unexpected result '$res')\n";
$fail++;
}
}
sub agi_output
{
if ($AGILOG >=2)
{
### open the log file for writing ###
open(Lout, ">>$AGILOGfile")
|| die "Can't open $AGILOGfile: $!\n";
print Lout "$now_date|$script|$agi_string\n";
close(Lout);
}
### send to STDERR writing ###
if ( ($AGILOG == '1') || ($AGILOG == '3') )
{print STDERR "$now_date|$script|$agi_string\n";}
$agi_string='';
}
@@ -0,0 +1,240 @@
#!/usr/bin/perl
#
# agi-VDAD_pin_IVR.agi version 2.0.5 *DBI-version*
#
# At the end of a call, the agent transfers customer to this script
#
# This script plays a beep then plays the first portion of the message
# then logs the portion as listened to in the vicidial_list_pins table
# then plays second portion and hangs up.
#
# IN THE CAMPAIGN OR IN-GROUP NUMBER PRESET FIELD:
# AXFER83209
#
# ON THE SERVER ORIGINATING THE CALL:
#;outbound IVR play VICIDIAL calls:
#exten => _983209.,1,AGI(call_log.agi,${EXTEN})
#exten => _983209.,2,Dial(${TRUNKY}/${EXTEN:1},60)
#exten => _983209.,3,Hangup
#
# ON THE SERVER THAT HAS THIS SCRIPT:
#;inbound IVR play VICIDIAL calls:
#exten => _83209.,1,Answer
#exten => _83209.,2,AGI(agi-VDAD_pin_IVR.agi,${EXTEN})
#exten => _83209.,3,Hangup
#
# Copyright (C) 2008 Matt Florell <vicidial@gmail.com> LICENSE: AGPLv2
#
# changes:
# 60802-1304 - first build
# 60817-1113 - added output options check from database
#
$script = 'agi-VDAD_pin_IVR.agi';
$first_audio_file = '85100003';
$second_audio_file = '85100004';
$goodbye_audio_file = 'US_reminder_goodbye';
#$first_audio_file = 'US_reminder_message';
#$second_audio_file = 'US_reminder_options';
#$goodbye_audio_file = 'US_reminder_goodbye';
### set defaults for testing
$referring_extension = '83209';
$phone_number = '9999999999';
$lead_id = '999999999';
$campaign_id = 'TESTCAMP';
$product_code = 'ABC';
$user = '1234';
$pin = '0';
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year = ($year + 1900);
$mon++;
if ($mon < 10) {$mon = "0$mon";}
if ($mday < 10) {$mday = "0$mday";}
if ($hour < 10) {$hour = "0$hour";}
if ($min < 10) {$min = "0$min";}
if ($sec < 10) {$sec = "0$sec";}
$SQLdate = "$year-$mon-$mday $hour:$min:$sec";
# default path to astguiclient configuration file:
$PATHconf = '/etc/astguiclient.conf';
open(conf, "$PATHconf") || die "can't open $PATHconf: $!\n";
@conf = <conf>;
close(conf);
$i=0;
foreach(@conf)
{
$line = $conf[$i];
$line =~ s/ |>|\n|\r|\t|\#.*|;.*//gi;
if ( ($line =~ /^PATHhome/) && ($CLIhome < 1) )
{$PATHhome = $line; $PATHhome =~ s/.*=//gi;}
if ( ($line =~ /^PATHlogs/) && ($CLIlogs < 1) )
{$PATHlogs = $line; $PATHlogs =~ s/.*=//gi;}
if ( ($line =~ /^PATHagi/) && ($CLIagi < 1) )
{$PATHagi = $line; $PATHagi =~ s/.*=//gi;}
if ( ($line =~ /^PATHweb/) && ($CLIweb < 1) )
{$PATHweb = $line; $PATHweb =~ s/.*=//gi;}
if ( ($line =~ /^PATHsounds/) && ($CLIsounds < 1) )
{$PATHsounds = $line; $PATHsounds =~ s/.*=//gi;}
if ( ($line =~ /^PATHmonitor/) && ($CLImonitor < 1) )
{$PATHmonitor = $line; $PATHmonitor =~ s/.*=//gi;}
if ( ($line =~ /^VARserver_ip/) && ($CLIserver_ip < 1) )
{$VARserver_ip = $line; $VARserver_ip =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_server/) && ($CLIDB_server < 1) )
{$VARDB_server = $line; $VARDB_server =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_database/) && ($CLIDB_database < 1) )
{$VARDB_database = $line; $VARDB_database =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_user/) && ($CLIDB_user < 1) )
{$VARDB_user = $line; $VARDB_user =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_pass/) && ($CLIDB_pass < 1) )
{$VARDB_pass = $line; $VARDB_pass =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_port/) && ($CLIDB_port < 1) )
{$VARDB_port = $line; $VARDB_port =~ s/.*=//gi;}
$i++;
}
if (!$VARDB_port) {$VARDB_port='3306';}
if (!$AGILOGfile) {$AGILOGfile = "$PATHlogs/agiout.$year-$mon-$mday";}
use DBI;
use Asterisk::AGI;
$AGI = new Asterisk::AGI;
$dbhA = DBI->connect("DBI:mysql:$VARDB_database:$VARDB_server:$VARDB_port", "$VARDB_user", "$VARDB_pass")
or die "Couldn't connect to database: " . DBI->errstr;
### Grab Server values from the database
$stmtA = "SELECT agi_output FROM servers where server_ip = '$VARserver_ip';";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
$rec_count=0;
while ($sthArows > $rec_count)
{
$AGILOG = '0';
@aryA = $sthA->fetchrow_array;
$DBagi_output = "$aryA[0]";
if ($DBagi_output =~ /STDERR/) {$AGILOG = '1';}
if ($DBagi_output =~ /FILE/) {$AGILOG = '2';}
if ($DBagi_output =~ /BOTH/) {$AGILOG = '3';}
$rec_count++;
}
$sthA->finish();
### begin parsing run-time options ###
if (length($ARGV[0])>1)
{
if ($AGILOG) {$agi_string = "Perl Environment Dump:"; &agi_output;}
$i=0;
while ($#ARGV >= $i)
{
$args = "$args $ARGV[$i]";
if ($AGILOG) {$agi_string = "$i|$ARGV[$i]"; &agi_output;}
$i++;
}
if ($args =~ /--help/i)
{
print "allowed run time options:\n [-q] = quiet\n [-t] = test\n [-debug] = verbose debug messages\n\n";
}
else
{
if ($args =~ /-V/i)
{
$V=1;
}
if ($args =~ /-debug/i)
{
$DG=1;
}
if ($args =~ /-q/i)
{
$q=1;
$Q=1;
}
if ($args =~ /-t/i)
{
$TEST=1;
$T=1;
}
}
}
$|=1;
while(<STDIN>) {
chomp;
last unless length($_);
if ($AGILOG)
{
if (/^agi_(\w+)\:\s+(.*)$/)
{
$AGI{$1} = $2;
}
}
if (/^agi_extension\:\s+(.*)$/) {$extension = $1;}
### allow for internal transfer data string "83009*3125551212*10000123*TESTCAMP*ABC*1234*"
if ($extension =~ /^8320\d\*/)
{
@EXT_vars = split(/\*/, $extension);
$referring_extension = $EXT_vars[0]; # initial extension sent
$phone_number = $EXT_vars[1]; # phone number
$lead_id = $EXT_vars[2]; # lead_id in vicidial_list
$campaign_id = $EXT_vars[3]; # campaign id
$product_code = $EXT_vars[4]; # product code
$user = $EXT_vars[5]; # vicidial_user that sent the call here
}
}
if ($AGILOG) {$agi_string = "INBOUND IVR PLAY CALL VDCL STARTED"; &agi_output;}
$AGI->stream_file('beep');
sleep(1);
$AGI->stream_file("$first_audio_file");
### insert a record into the vicidial_list_pins table
$stmtA = "INSERT INTO vicidial_list_pins (entry_time,phone_number,lead_id,campaign_id,product_code,user,digits) values('$SQLdate','$phone_number','$lead_id','$campaign_id','$product_code','$user','IVR');";
$affected_rows = $dbhA->do($stmtA);
$insert_pin_id = $dbhA->{'mysql_insertid'};
if ($AGILOG) {$agi_string = "|$stmtA|$insert_pin_id|$affected_rows|"; &agi_output;}
$dbhA->disconnect();
$AGI->stream_file("$second_audio_file");
sleep(1);
$AGI->stream_file("$goodbye_audio_file");
exit;
sub agi_output
{
if ($AGILOG >=2)
{
### open the log file for writing ###
open(Lout, ">>$AGILOGfile")
|| die "Can't open $AGILOGfile: $!\n";
print Lout "$now_date|$script|$agi_string\n";
close(Lout);
}
### send to STDERR writing ###
if ( ($AGILOG == '1') || ($AGILOG == '3') )
{print STDERR "$now_date|$script|$agi_string\n";}
$agi_string='';
}
@@ -0,0 +1,673 @@
#!/usr/bin/perl
#
# agi-VDADautoREMINDER.agi version 2.6
#
# runs when a call comes in from the VICIDIAL auto_dialer. This script will
# send the calls out to specified voicemail boxes in if called person presses key.
# The voicemail box is defined per-lead in the vicidial_list.security_phrase field
# This campaign does not send calls to live agents.
#
# Call statuses from this script:
# N- Call was picked up, didn't finish initial message
# MP- Initial message was played
# VM- call was sent to voicemail
# CALLBK- customer selected to be called back
#
# It is recommended that you run this campaign at a dial level of 1.0
# It is recommended that you use remote_agents for activation of this script
#
# You need to put lines similar to those below in your extensions.conf file:
#
# ;VICIDIAL_auto_dialer transfer script:
# exten => 8372,1,AGI(call_log.agi,${EXTEN})
# exten => 8372,2,AGI(agi-VDADautoREMINDER.agi,${EXTEN})
#
# Copyright (C) 2013 Matt Florell <vicidial@gmail.com> LICENSE: GPLv2
#
# CHANGES
# 60313-1626 - First build
# 60810-1729 - Changed to DBI
# - changed to use /etc/astguiclient.conf for configs
# 60818-1144 - added output options check from database
# 130108-1814 - Changes for Asterisk 1.8 compatibility
#
$script = 'agi-VDADautoREMINDER.agi';
##### AUDIO FILES IN THIS PROGRAM #####
$message_audio_file = 'US_reminder_message';
$options_audio_file = 'US_reminder_options';
# $voicemail_xfer_file = 'US_reminder_voicemail'; not used
$callback_audio_file = 'US_reminder_callback';
$goodbye_audio_file = 'US_reminder_goodbye';
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year = ($year + 1900);
$mon++;
if ($mon < 10) {$mon = "0$mon";}
if ($mday < 10) {$mday = "0$mday";}
if ($hour < 10) {$hour = "0$hour";}
if ($min < 10) {$min = "0$min";}
if ($sec < 10) {$sec = "0$sec";}
$now_date_epoch = time();
$now_date = "$year-$mon-$mday $hour:$min:$sec";
$start_time=$now_date;
$CIDdate = "$mon$mday$hour$min$sec";
$tsSQLdate = "$year$mon$mday$hour$min$sec"; #20060310100402
$SQLdate = "$year-$mon-$mday $hour:$min:$sec";
$SQLdateBEGIN = $SQLdate;
$BDtarget = ($secX - 5);
($Bsec,$Bmin,$Bhour,$Bmday,$Bmon,$Byear,$Bwday,$Byday,$Bisdst) = localtime($BDtarget);
$Byear = ($Byear + 1900);
$Bmon++;
if ($Bmon < 10) {$Bmon = "0$Bmon";}
if ($Bmday < 10) {$Bmday = "0$Bmday";}
if ($Bhour < 10) {$Bhour = "0$Bhour";}
if ($Bmin < 10) {$Bmin = "0$Bmin";}
if ($Bsec < 10) {$Bsec = "0$Bsec";}
$BDtsSQLdate = "$Byear$Bmon$Bmday$Bhour$Bmin$Bsec";
# default path to astguiclient configuration file:
$PATHconf = '/etc/astguiclient.conf';
open(conf, "$PATHconf") || die "can't open $PATHconf: $!\n";
@conf = <conf>;
close(conf);
$i=0;
foreach(@conf)
{
$line = $conf[$i];
$line =~ s/ |>|\n|\r|\t|\#.*|;.*//gi;
if ( ($line =~ /^PATHhome/) && ($CLIhome < 1) )
{$PATHhome = $line; $PATHhome =~ s/.*=//gi;}
if ( ($line =~ /^PATHlogs/) && ($CLIlogs < 1) )
{$PATHlogs = $line; $PATHlogs =~ s/.*=//gi;}
if ( ($line =~ /^PATHagi/) && ($CLIagi < 1) )
{$PATHagi = $line; $PATHagi =~ s/.*=//gi;}
if ( ($line =~ /^PATHweb/) && ($CLIweb < 1) )
{$PATHweb = $line; $PATHweb =~ s/.*=//gi;}
if ( ($line =~ /^PATHsounds/) && ($CLIsounds < 1) )
{$PATHsounds = $line; $PATHsounds =~ s/.*=//gi;}
if ( ($line =~ /^PATHmonitor/) && ($CLImonitor < 1) )
{$PATHmonitor = $line; $PATHmonitor =~ s/.*=//gi;}
if ( ($line =~ /^VARserver_ip/) && ($CLIserver_ip < 1) )
{$VARserver_ip = $line; $VARserver_ip =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_server/) && ($CLIDB_server < 1) )
{$VARDB_server = $line; $VARDB_server =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_database/) && ($CLIDB_database < 1) )
{$VARDB_database = $line; $VARDB_database =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_user/) && ($CLIDB_user < 1) )
{$VARDB_user = $line; $VARDB_user =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_pass/) && ($CLIDB_pass < 1) )
{$VARDB_pass = $line; $VARDB_pass =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_port/) && ($CLIDB_port < 1) )
{$VARDB_port = $line; $VARDB_port =~ s/.*=//gi;}
$i++;
}
if (!$VARDB_port) {$VARDB_port='3306';}
if (!$AGILOGfile) {$AGILOGfile = "$PATHlogs/agiout.$year-$mon-$mday";}
use DBI;
use Time::HiRes ('gettimeofday','usleep','sleep'); # necessary to have perl sleep command of less than one second
use Asterisk::AGI;
$AGI = new Asterisk::AGI;
$dbhA = DBI->connect("DBI:mysql:$VARDB_database:$VARDB_server:$VARDB_port", "$VARDB_user", "$VARDB_pass")
or die "Couldn't connect to database: " . DBI->errstr;
### Grab Server values from the database
$stmtA = "SELECT agi_output FROM servers where server_ip = '$VARserver_ip';";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
$rec_count=0;
while ($sthArows > $rec_count)
{
$AGILOG = '0';
@aryA = $sthA->fetchrow_array;
$DBagi_output = "$aryA[0]";
if ($DBagi_output =~ /STDERR/) {$AGILOG = '1';}
if ($DBagi_output =~ /FILE/) {$AGILOG = '2';}
if ($DBagi_output =~ /BOTH/) {$AGILOG = '3';}
$rec_count++;
}
$sthA->finish();
### begin parsing run-time options ###
if (length($ARGV[0])>1)
{
if ($AGILOG) {$agi_string = "Perl Environment Dump:"; &agi_output;}
$i=0;
while ($#ARGV >= $i)
{
$args = "$args $ARGV[$i]";
if ($AGILOG) {$agi_string = "$i|$ARGV[$i]"; &agi_output;}
$i++;
}
if ($args =~ /--help/i)
{
print "allowed run time options:\n [-q] = quiet\n [-t] = test\n [-debug] = verbose debug messages\n\n";
}
else
{
if ($args =~ /-V/i)
{
$V=1;
}
if ($args =~ /-debug/i)
{
$DG=1;
}
if ($args =~ /-dbAVS/i)
{
$DGA=1;
}
if ($args =~ /-q/i)
{
$q=1;
$Q=1;
}
if ($args =~ /-t/i)
{
$TEST=1;
$T=1;
}
}
}
$|=1;
while(<STDIN>) {
chomp;
last unless length($_);
if ($AGILOG)
{
if (/^agi_(\w+)\:\s+(.*)$/)
{
$AGI{$1} = $2;
}
}
if (/^agi_uniqueid\:\s+(.*)$/) {$unique_id = $1; $uniqueid = $unique_id;}
if (/^agi_priority\:\s+(.*)$/) {$priority = $1;}
if (/^agi_channel\:\s+(.*)$/) {$channel = $1;}
if (/^agi_extension\:\s+(.*)$/) {$extension = $1;}
if (/^agi_type\:\s+(.*)$/) {$type = $1;}
if (/^agi_callerid\:\s+(.*)$/) {$callerid = $1;}
if (/^agi_calleridname\:\s+(.*)$/) {$calleridname = $1;}
}
if ( (length($callerid)>20) && ($callerid =~ /\"\S\S\S\S\S\S\S\S\S\S\S\S\S\S\S\S\S\S/) )
{
$callerid =~ s/^\"//gi;
$callerid =~ s/\".*$//gi;
### set the callerid to the ACQS value(calleridname)
print "SET CALLERID $callerid\n";
$result = <STDIN>;
checkresult($result);
if ($AGILOG) {$agi_string = "callerID changed: $callerid"; &agi_output;}
}
if ( (
(length($calleridname)>5) && ( (!$callerid) or ($callerid =~ /unknown|private|00000000/i) or ($callerid =~ /5551212/) )
) or ( (length($calleridname)>17) && ($calleridname =~ /\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d/) ) )
{
$callerid = $calleridname;
### set the callerid to the ACQS value(calleridname)
print "SET CALLERID $callerid\n";
$result = <STDIN>;
checkresult($result);
if ($AGILOG) {$agi_string = "callerID changed: $callerid"; &agi_output;}
}
if ($AGILOG) {$agi_string = "AGI Environment Dump:"; &agi_output;}
foreach $i (sort keys %AGI)
{
if ($AGILOG) {$agi_string = " -- $i = $AGI{$i}"; &agi_output;}
}
if ($AGILOG) {$agi_string = "AGI Variables: |$unique_id|$channel|$extension|$type|$callerid|"; &agi_output;}
$VDADcampaign='';
$VDADphone='';
$VDADphone_code='';
$callerid =~ s/\"//gi;
$CIDlead_id = $callerid;
$CIDlead_id = substr($CIDlead_id, 11, 9);
$CIDlead_id = ($CIDlead_id + 0);
if ($AGILOG) {$agi_string = "+++++ VDAD START : |$CIDlead_id|$now_date|"; &agi_output;}
if ( ($channel =~ /Local/i) && ($AST_ver !~ /^1\.0\.8|^1\.0\.9/) )
{
if ($AGILOG) {$agi_string = "+++++ VDAD START LOCAL CHANNEL: EXITING- $priority"; &agi_output;}
if ($priority > 2) {sleep(1);}
exit;
}
sleep(1);
$AGI->stream_file('beep');
### Grab Server values from the database
$stmtA = "SELECT voicemail_dump_exten,ext_context,answer_transfer_agent,local_gmt,asterisk_version,max_vicidial_trunks FROM servers where server_ip = '$VARserver_ip';";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
$rec_count=0;
while ($sthArows > $rec_count)
{
@aryA = $sthA->fetchrow_array;
$DBvoicemail_dump_exten = "$aryA[0]";
$DBext_context = "$aryA[1]";
$DBanswer_transfer_agent = "$aryA[2]";
$DBSERVER_GMT = "$aryA[3]";
$DBasterisk_version = "$aryA[4]";
$DBmax_vicidial_trunks = "$aryA[5]";
if ($DBvoicemail_dump_exten) {$voicemail_dump_exten = $DBvoicemail_dump_exten;}
if ($DBext_context) {$ext_context = $DBext_context;}
if ($DBanswer_transfer_agent) {$answer_transfer_agent = $DBanswer_transfer_agent;}
if ($DBSERVER_GMT) {$SERVER_GMT = $DBSERVER_GMT;}
if ($DBasterisk_version) {$AST_ver = $DBasterisk_version;}
if ($DBmax_vicidial_trunks) {$max_vicidial_trunks = $DBmax_vicidial_trunks;}
$rec_count++;
}
$sthA->finish();
### Grab call parameters from vicidial_auto_calls table
$stmtA = "UPDATE vicidial_auto_calls set uniqueid='$unique_id', channel='$channel',status='LIVE',stage='LIVE-0' where callerid='$callerid' order by call_time desc limit 1;";
$affected_rows = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "-- VDAD : |$affected_rows|update of vac table: $callerid\n|$stmtA|"; &agi_output;}
if ($affected_rows > 0)
{
$stmtA = "SELECT campaign_id,phone_number,phone_code,lead_id,call_time FROM vicidial_auto_calls where callerid='$callerid' order by call_time desc limit 1;";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
$rec_count=0;
while ($sthArows > $rec_count)
{
@aryA = $sthA->fetchrow_array;
$VDADcampaign = "$aryA[0]";
$VDADphone = "$aryA[1]";
$VDADphone_code = "$aryA[2]";
$VDADlead_id = "$aryA[3]";
$VDADcall_time = "$aryA[4]";
$rec_count++;
}
$sthA->finish();
$stmtA = "INSERT INTO vicidial_log (uniqueid,lead_id,campaign_id,call_date,start_epoch,status,phone_code,phone_number,user,processed) values('$uniqueid','$CIDlead_id','$VDADcampaign','$SQLdate','$now_date_epoch','N','$VDADphone_code','$VDADphone','VDAD','N')";
if ($AGILOG) {$agi_string = "|$stmtA|"; &agi_output;}
$affected_rows = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "-- VDAD : |$VDADlead_id|$CIDlead_id|insert to vicidial_log: $uniqueid"; &agi_output;}
}
######### REMINDER CHOICE FROM RECORDINGS ########
&reminder_choice;
if ($pin < 1)
{
&reminder_choice;
if ($pin < 1)
{
$AGI->stream_file("$goodbye_audio_file");
$stmtA = "DELETE FROM vicidial_auto_calls where callerid='$callerid' order by call_time desc limit 1;";
$affected_rows = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "-- VDAD vac record deleted: |$affected_rows|$VDADcampaign|"; &agi_output;}
$end_date_epoch = time();
$length_in_sec = ($end_date_epoch - $now_date_epoch);
$stmtA = "UPDATE vicidial_log set end_epoch='$end_date_epoch', length_in_sec='$length_in_sec' where uniqueid='$uniqueid' and lead_id='$CIDlead_id';";
$affected_rows = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "-- VDAD vac record deleted: |$affected_rows|$VDADcampaign|$end_date_epoch|$now_date_epoch|$length_in_sec|"; &agi_output;}
$VHqueryCID = "VHNGUP$tsSQLdate";
### insert a NEW record to the vicidial_manager table to be processed
$stmtA = "INSERT INTO vicidial_manager values('','','$SQLdate','NEW','N','$VARserver_ip','','Hangup','$VHqueryCID','Channel: $channel','','','','','','','','','')";
$affected_rows = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "-- VDAD call_hungup timout: |$VHqueryCID|$VDADconf_exten|$channel|insert to vicidial_manager"; &agi_output;}
sleep(1);
exit;
}
}
if ($AGILOG) {$agi_string = "+++++ VDAD STATUS : pressed $pin|$unique_id|$channel|$extension|$type|$callerid|$CIDlead_id|$now_date"; &agi_output;}
if ($pin !~ /^1|^2/) {exit;}
if (!$DB_port) {$DB_port='3306';}
if ($pin =~ /^1|^2/)
{
if ($pin == "1") ### send call to voicemail
{
# message can be left on agent vmail box
# # Thank you for your response, we will now transfer you to your agent's voicemail box.
# $AGI->stream_file("$voicemail_xfer_file");
$stmtA = "UPDATE vicidial_auto_calls set status='XFER', stage='XFER-$drop_timer' where callerid='$callerid';";
$affected_rows = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "-- VDAD XFER : |$affected_rows|update of vac table: $callerid\n|$stmtA|"; &agi_output;}
$stmtA = "UPDATE vicidial_list set status='VM' where lead_id = '$CIDlead_id';";
if ($AGILOG) {$agi_string = "|$stmtA|"; &agi_output;}
$affected_rows = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "-- VDAD vicidial_list update: |$affected_rows|$CIDlead_id|XFER"; &agi_output;}
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year = ($year + 1900);
$mon++;
if ($mon < 10) {$mon = "0$mon";}
if ($mday < 10) {$mday = "0$mday";}
if ($hour < 10) {$hour = "0$hour";}
if ($min < 10) {$min = "0$min";}
if ($sec < 10) {$sec = "0$sec";}
$end_date_epoch = time();
$length_in_sec = ($end_date_epoch - $now_date_epoch);
$now_date = "$year-$mon-$mday $hour:$min:$sec";
if ($AGILOG) {$agi_string = "XXXXXXXXXX VDAD transferred: start|stop $start_time|$now_date"; &agi_output;}
$stmtA = "UPDATE vicidial_log set status='VM', end_epoch='$end_date_epoch', length_in_sec='$length_in_sec' where uniqueid='$uniqueid' and lead_id='$CIDlead_id';";
$affected_rows = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "-- VDAD vac record updated: |$affected_rows|$VDADcampaign|"; &agi_output;}
$VMAIL_box=0;
### Grab Server values from the database
$stmtA = "SELECT security_phrase FROM vicidial_list where lead_id = '$CIDlead_id';";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
$rec_count=0;
while ($sthArows > $rec_count)
{
@aryA = $sthA->fetchrow_array;
$VMAIL_box = "$aryA[0]";
$rec_count++;
}
$sthA->finish();
### SEND CALL TO VOICEMAIL BOX ###
if ($AGILOG) {$agi_string = "exiting the VDAD app, transferring call to $voicemail_dump_exten$VMAIL_box"; &agi_output;}
print "SET CONTEXT $ext_context\n";
$result = <STDIN>;
checkresult($result);
print "SET EXTENSION $voicemail_dump_exten$VMAIL_box\n";
$result = <STDIN>;
checkresult($result);
print "SET PRIORITY 1\n";
$result = <STDIN>;
checkresult($result);
}
if ($pin == "2") ### mark call as a CALLBK
{
# Thank you for your response, your agent will be contacting you shortly.
$stmtA = "DELETE from vicidial_auto_calls where callerid='$callerid';";
$affected_rows = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "-- VDAD DONE : |$affected_rows|delete of vac table: $callerid\n|$stmtA|"; &agi_output;}
$stmtA = "UPDATE vicidial_list set status='CALLBK' where lead_id = '$CIDlead_id';";
if ($AGILOG) {$agi_string = "|$stmtA|"; &agi_output;}
$affected_rows = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "-- VDAD vicidial_list update: |$affected_rows|$CIDlead_id|XFER"; &agi_output;}
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year = ($year + 1900);
$mon++;
if ($mon < 10) {$mon = "0$mon";}
if ($mday < 10) {$mday = "0$mday";}
if ($hour < 10) {$hour = "0$hour";}
if ($min < 10) {$min = "0$min";}
if ($sec < 10) {$sec = "0$sec";}
$end_date_epoch = time();
$length_in_sec = ($end_date_epoch - $now_date_epoch);
$now_date = "$year-$mon-$mday $hour:$min:$sec";
if ($AGILOG) {$agi_string = "XXXXXXXXXX VDAD transferred: start|stop $start_time|$now_date"; &agi_output;}
$stmtA = "UPDATE vicidial_log set status='CALLBK', end_epoch='$end_date_epoch', length_in_sec='$length_in_sec' where uniqueid='$uniqueid' and lead_id='$CIDlead_id';";
$affected_rows = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "-- VDAD vac record updated: |$affected_rows|$VDADcampaign|"; &agi_output;}
$AGI->stream_file("$callback_audio_file");
$VHqueryCID = "VHNGUP$tsSQLdate";
### insert a NEW record to the vicidial_manager table to be processed
$stmtA = "INSERT INTO vicidial_manager values('','','$SQLdate','NEW','N','$VARserver_ip','','Hangup','$VHqueryCID','Channel: $channel','','','','','','','','','')";
$affected_rows = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "-- VDAD call_hungup timout: |$VHqueryCID|$VDADconf_exten|$channel|insert to vicidial_manager"; &agi_output;}
sleep(1);
exit;
}
} ### end if pressed 1,2
else ### user pressed digit other than 1 or 2
{
exit;
}
$dbhA->close;
exit;
sub reminder_choice
{
$digit='';
undef $digit;
$interrupt_digit='';
undef $interrupt_digit;
# Hello, this is a courtesy call from ACME Industries reminding you that the deadline for the submission of orders for the June 5th batch delivery is tomorrow, March 10th:
# second audio file
# If you would like to leave a message for your agent please press 1.
# If you would like to have your agent call you back please press 2.
# third audio file
# Thank you, Goodbye
######### PLAY THE MESSAGE FILE THEN WAIT 2 SECONDS FOR DIGITS ############
$interrupt_digit = $AGI->stream_file("$message_audio_file",'12');
if ($AGILOG) {$agi_string = "interrupt_digit |$interrupt_digit|"; &agi_output;}
$digits_being_entered=1;
$digit_loop_counter=0;
$totalDTMF='';
if ($interrupt_digit > 1)
{
if ($interrupt_digit == 48) {$interrupt_digit=0;}
if ($interrupt_digit == 49) {$interrupt_digit=1;}
if ($interrupt_digit == 50) {$interrupt_digit=2;}
if ($interrupt_digit == 51) {$interrupt_digit=3;}
if ($interrupt_digit == 52) {$interrupt_digit=4;}
if ($interrupt_digit == 53) {$interrupt_digit=5;}
if ($interrupt_digit == 54) {$interrupt_digit=6;}
if ($interrupt_digit == 55) {$interrupt_digit=7;}
if ($interrupt_digit == 56) {$interrupt_digit=8;}
if ($interrupt_digit == 57) {$interrupt_digit=9;}
$totalDTMF=$interrupt_digit;
$digit_loop_counter++;
}
$channel_status = $AGI->channel_status("$channel");
if ($AGILOG) {$agi_string = "SSSSSSSSSS Channel Status: $channel_status"; &agi_output;}
if ($channel_status == 6)
{
$stmtA = "UPDATE vicidial_log set status='MP' where uniqueid = '$uniqueid' and lead_id='$CIDlead_id';";
if ($AGILOG) {$agi_string = "|$stmtA|"; &agi_output;}
$affected_rows = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "-- VDAD vicidial_log update: N|$affected_rows|$uniqueid"; &agi_output;}
$stmtA = "UPDATE vicidial_list set status='MP' where lead_id = '$CIDlead_id';";
if ($AGILOG) {$agi_string = "|$stmtA|"; &agi_output;}
$affected_rows = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "-- VDAD vicidial_list update: N|$affected_rows|$CIDlead_id"; &agi_output;}
}
while ($digit_loop_counter < 1)
{
$digit = chr($AGI->wait_for_digit('2000')); # wait 2 seconds for input or until digit pressed
if ($digit =~ /\d/)
{
$totalDTMF = "$totalDTMF$digit";
if ($AGILOG) {$agi_string = "digit |$digit| TotalDTMF |$totalDTMF|"; &agi_output;}
# $AGI->say_digits("$digit");
undef $digit;
}
else
{
$digit_loop_counter=1;
}
$digit_loop_counter++;
}
$totalDTMF =~ s/\D//gi;
$pin = $totalDTMF;
if ($totalDTMF)
{
if ($AGILOG) {$agi_string = "digit |$digit| TotalDTMF |$totalDTMF|"; &agi_output;}
return;
}
######### PLAY THE OPTIONS FILE THEN WAIT 5 SECONDS FOR DIGITS ############
$interrupt_digit = $AGI->stream_file("$options_audio_file",'12');
if ($AGILOG) {$agi_string = "interrupt_digit |$interrupt_digit|"; &agi_output;}
$digits_being_entered=1;
$digit_loop_counter=0;
$totalDTMF='';
if ($interrupt_digit > 1)
{
if ($interrupt_digit == 48) {$interrupt_digit=0;}
if ($interrupt_digit == 49) {$interrupt_digit=1;}
if ($interrupt_digit == 50) {$interrupt_digit=2;}
if ($interrupt_digit == 51) {$interrupt_digit=3;}
if ($interrupt_digit == 52) {$interrupt_digit=4;}
if ($interrupt_digit == 53) {$interrupt_digit=5;}
if ($interrupt_digit == 54) {$interrupt_digit=6;}
if ($interrupt_digit == 55) {$interrupt_digit=7;}
if ($interrupt_digit == 56) {$interrupt_digit=8;}
if ($interrupt_digit == 57) {$interrupt_digit=9;}
$totalDTMF=$interrupt_digit;
$digit_loop_counter++;
}
while ($digit_loop_counter < 1)
{
$digit = chr($AGI->wait_for_digit('10000')); # wait 10 seconds for input or until digit pressed
if ($digit =~ /\d/)
{
$totalDTMF = "$totalDTMF$digit";
if ($AGILOG) {$agi_string = "digit |$digit| TotalDTMF |$totalDTMF|"; &agi_output;}
# $AGI->say_digits("$digit");
undef $digit;
}
else
{
$digit_loop_counter=1;
}
$digit_loop_counter++;
}
$totalDTMF =~ s/\D//gi;
$pin = $totalDTMF;
if ($totalDTMF)
{
if ($AGILOG) {$agi_string = "digit |$digit| TotalDTMF |$totalDTMF|"; &agi_output;}
return;
}
}
sub checkresult {
my ($res) = @_;
my $retval;
$tests++;
chomp $res;
if ($res =~ /^200/) {
$res =~ /result=(-?\d+)/;
if (!length($1)) {
# print STDERR "FAIL ($res)\n";
$fail++;
} else {
# print STDERR "PASS ($1)\n";
$pass++;
}
} else {
# print STDERR "FAIL (unexpected result '$res')\n";
$fail++;
}
}
sub agi_output
{
if ($AGILOG >=2)
{
### open the log file for writing ###
open(Lout, ">>$AGILOGfile")
|| die "Can't open $AGILOGfile: $!\n";
print Lout "$now_date|$script|$agi_string\n";
close(Lout);
}
### send to STDERR writing ###
if ( ($AGILOG == '1') || ($AGILOG == '3') )
{print STDERR "$now_date|$script|$agi_string\n";}
$agi_string='';
}
@@ -0,0 +1,738 @@
#!/usr/bin/perl
#
# agi-VDADautoREMINDERxfer.agi version 2.6
#
# runs when a call comes in from the VICIDIAL auto_dialer. This script will
# send the calls out to specified voicemail boxes or specified extensions if
# called person presses specified key(1,2,3).
# The voicemail box is defined per-lead in the vicidial_list.security_phrase field
# The exten xfer number is defined per-lead in the vicidial_list.comments field
#
# Call statuses from this script:
# N- Call was picked up, didn't finish initial message
# MP- Initial message was played
# VM- call was sent to voicemail
# XFER- call was sent to specified extension
# CALLBK- customer selected to be called back
#
# It is recommended that you run this campaign at a dial level of 1.0
# It is recommended that you use remote_agents for activation of this script
#
# You need to put lines similar to those below in your extensions.conf file:
#
# ;VICIDIAL_auto_dialer transfer script:
# exten => 8372,1,AGI(call_log.agi,${EXTEN})
# exten => 8372,2,AGI(agi-VDADautoREMINDERxfer.agi,${EXTEN})
#
# Copyright (C) 2013 Matt Florell <vicidial@gmail.com> LICENSE: GPLv2
#
# CHANGES
# 60511-1214 - First build
# 60810-1729 - Changed to DBI
# - changed to use /etc/astguiclient.conf for configs
# 60818-1144 - added output options check from database
# 130108-1812 - Changes for Asterisk 1.8 compatibility
#
$script = 'agi-VDADautoREMINDERxfer.agi';
##### AUDIO FILES IN THIS PROGRAM #####
$message_audio_file = 'US_reminder_message';
$options_audio_file = 'US_reminder_options';
# $voicemail_xfer_file = 'US_reminder_voicemail'; not used
$callback_audio_file = 'US_reminder_callback';
$goodbye_audio_file = 'US_reminder_goodbye';
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year = ($year + 1900);
$mon++;
if ($mon < 10) {$mon = "0$mon";}
if ($mday < 10) {$mday = "0$mday";}
if ($hour < 10) {$hour = "0$hour";}
if ($min < 10) {$min = "0$min";}
if ($sec < 10) {$sec = "0$sec";}
$now_date_epoch = time();
$now_date = "$year-$mon-$mday $hour:$min:$sec";
$start_time=$now_date;
$CIDdate = "$mon$mday$hour$min$sec";
$tsSQLdate = "$year$mon$mday$hour$min$sec"; #20060310100402
$SQLdate = "$year-$mon-$mday $hour:$min:$sec";
$SQLdateBEGIN = $SQLdate;
$BDtarget = ($secX - 5);
($Bsec,$Bmin,$Bhour,$Bmday,$Bmon,$Byear,$Bwday,$Byday,$Bisdst) = localtime($BDtarget);
$Byear = ($Byear + 1900);
$Bmon++;
if ($Bmon < 10) {$Bmon = "0$Bmon";}
if ($Bmday < 10) {$Bmday = "0$Bmday";}
if ($Bhour < 10) {$Bhour = "0$Bhour";}
if ($Bmin < 10) {$Bmin = "0$Bmin";}
if ($Bsec < 10) {$Bsec = "0$Bsec";}
$BDtsSQLdate = "$Byear$Bmon$Bmday$Bhour$Bmin$Bsec";
# default path to astguiclient configuration file:
$PATHconf = '/etc/astguiclient.conf';
open(conf, "$PATHconf") || die "can't open $PATHconf: $!\n";
@conf = <conf>;
close(conf);
$i=0;
foreach(@conf)
{
$line = $conf[$i];
$line =~ s/ |>|\n|\r|\t|\#.*|;.*//gi;
if ( ($line =~ /^PATHhome/) && ($CLIhome < 1) )
{$PATHhome = $line; $PATHhome =~ s/.*=//gi;}
if ( ($line =~ /^PATHlogs/) && ($CLIlogs < 1) )
{$PATHlogs = $line; $PATHlogs =~ s/.*=//gi;}
if ( ($line =~ /^PATHagi/) && ($CLIagi < 1) )
{$PATHagi = $line; $PATHagi =~ s/.*=//gi;}
if ( ($line =~ /^PATHweb/) && ($CLIweb < 1) )
{$PATHweb = $line; $PATHweb =~ s/.*=//gi;}
if ( ($line =~ /^PATHsounds/) && ($CLIsounds < 1) )
{$PATHsounds = $line; $PATHsounds =~ s/.*=//gi;}
if ( ($line =~ /^PATHmonitor/) && ($CLImonitor < 1) )
{$PATHmonitor = $line; $PATHmonitor =~ s/.*=//gi;}
if ( ($line =~ /^VARserver_ip/) && ($CLIserver_ip < 1) )
{$VARserver_ip = $line; $VARserver_ip =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_server/) && ($CLIDB_server < 1) )
{$VARDB_server = $line; $VARDB_server =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_database/) && ($CLIDB_database < 1) )
{$VARDB_database = $line; $VARDB_database =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_user/) && ($CLIDB_user < 1) )
{$VARDB_user = $line; $VARDB_user =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_pass/) && ($CLIDB_pass < 1) )
{$VARDB_pass = $line; $VARDB_pass =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_port/) && ($CLIDB_port < 1) )
{$VARDB_port = $line; $VARDB_port =~ s/.*=//gi;}
$i++;
}
if (!$VARDB_port) {$VARDB_port='3306';}
if (!$AGILOGfile) {$AGILOGfile = "$PATHlogs/agiout.$year-$mon-$mday";}
use DBI;
use Time::HiRes ('gettimeofday','usleep','sleep'); # necessary to have perl sleep command of less than one second
use Asterisk::AGI;
$AGI = new Asterisk::AGI;
$dbhA = DBI->connect("DBI:mysql:$VARDB_database:$VARDB_server:$VARDB_port", "$VARDB_user", "$VARDB_pass")
or die "Couldn't connect to database: " . DBI->errstr;
### Grab Server values from the database
$stmtA = "SELECT agi_output FROM servers where server_ip = '$VARserver_ip';";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
$rec_count=0;
while ($sthArows > $rec_count)
{
$AGILOG = '0';
@aryA = $sthA->fetchrow_array;
$DBagi_output = "$aryA[0]";
if ($DBagi_output =~ /STDERR/) {$AGILOG = '1';}
if ($DBagi_output =~ /FILE/) {$AGILOG = '2';}
if ($DBagi_output =~ /BOTH/) {$AGILOG = '3';}
$rec_count++;
}
$sthA->finish();
### begin parsing run-time options ###
if (length($ARGV[0])>1)
{
if ($AGILOG) {$agi_string = "Perl Environment Dump:"; &agi_output;}
$i=0;
while ($#ARGV >= $i)
{
$args = "$args $ARGV[$i]";
if ($AGILOG) {$agi_string = "$i|$ARGV[$i]"; &agi_output;}
$i++;
}
if ($args =~ /--help/i)
{
print "allowed run time options:\n [-q] = quiet\n [-t] = test\n [-debug] = verbose debug messages\n\n";
}
else
{
if ($args =~ /-V/i)
{
$V=1;
}
if ($args =~ /-debug/i)
{
$DG=1;
}
if ($args =~ /-dbAVS/i)
{
$DGA=1;
}
if ($args =~ /-q/i)
{
$q=1;
$Q=1;
}
if ($args =~ /-t/i)
{
$TEST=1;
$T=1;
}
}
}
$|=1;
while(<STDIN>) {
chomp;
last unless length($_);
if ($AGILOG)
{
if (/^agi_(\w+)\:\s+(.*)$/)
{
$AGI{$1} = $2;
}
}
if (/^agi_uniqueid\:\s+(.*)$/) {$unique_id = $1; $uniqueid = $unique_id;}
if (/^agi_priority\:\s+(.*)$/) {$priority = $1;}
if (/^agi_channel\:\s+(.*)$/) {$channel = $1;}
if (/^agi_extension\:\s+(.*)$/) {$extension = $1;}
if (/^agi_type\:\s+(.*)$/) {$type = $1;}
if (/^agi_callerid\:\s+(.*)$/) {$callerid = $1;}
if (/^agi_calleridname\:\s+(.*)$/) {$calleridname = $1;}
}
if ( (length($callerid)>20) && ($callerid =~ /\"\S\S\S\S\S\S\S\S\S\S\S\S\S\S\S\S\S\S/) )
{
$callerid =~ s/^\"//gi;
$callerid =~ s/\".*$//gi;
### set the callerid to the ACQS value(calleridname)
print "SET CALLERID $callerid\n";
$result = <STDIN>;
checkresult($result);
if ($AGILOG) {$agi_string = "callerID changed: $callerid"; &agi_output;}
}
if ( (
(length($calleridname)>5) && ( (!$callerid) or ($callerid =~ /unknown|private|00000000/i) or ($callerid =~ /5551212/) )
) or ( (length($calleridname)>17) && ($calleridname =~ /\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d/) ) )
{
$callerid = $calleridname;
### set the callerid to the ACQS value(calleridname)
print "SET CALLERID $callerid\n";
$result = <STDIN>;
checkresult($result);
if ($AGILOG) {$agi_string = "callerID changed: $callerid"; &agi_output;}
}
if ($AGILOG) {$agi_string = "AGI Environment Dump:"; &agi_output;}
foreach $i (sort keys %AGI)
{
if ($AGILOG) {$agi_string = " -- $i = $AGI{$i}"; &agi_output;}
}
if ($AGILOG) {$agi_string = "AGI Variables: |$unique_id|$channel|$extension|$type|$callerid|"; &agi_output;}
$VDADcampaign='';
$VDADphone='';
$VDADphone_code='';
$callerid =~ s/\"//gi;
$CIDlead_id = $callerid;
$CIDlead_id = substr($CIDlead_id, 11, 9);
$CIDlead_id = ($CIDlead_id + 0);
if ($AGILOG) {$agi_string = "+++++ VDAD START : |$CIDlead_id|$now_date|"; &agi_output;}
if ( ($channel =~ /Local/i) && ($AST_ver !~ /^1\.0\.8|^1\.0\.9/) )
{
if ($AGILOG) {$agi_string = "+++++ VDAD START LOCAL CHANNEL: EXITING- $priority"; &agi_output;}
if ($priority > 2) {sleep(1);}
exit;
}
sleep(1);
$AGI->stream_file('beep');
### Grab Server values from the database
$stmtA = "SELECT voicemail_dump_exten,ext_context,answer_transfer_agent,local_gmt,asterisk_version,max_vicidial_trunks FROM servers where server_ip = '$VARserver_ip';";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
$rec_count=0;
while ($sthArows > $rec_count)
{
@aryA = $sthA->fetchrow_array;
$DBvoicemail_dump_exten = "$aryA[0]";
$DBext_context = "$aryA[1]";
$DBanswer_transfer_agent = "$aryA[2]";
$DBSERVER_GMT = "$aryA[3]";
$DBasterisk_version = "$aryA[4]";
$DBmax_vicidial_trunks = "$aryA[5]";
if ($DBvoicemail_dump_exten) {$voicemail_dump_exten = $DBvoicemail_dump_exten;}
if ($DBext_context) {$ext_context = $DBext_context;}
if ($DBanswer_transfer_agent) {$answer_transfer_agent = $DBanswer_transfer_agent;}
if ($DBSERVER_GMT) {$SERVER_GMT = $DBSERVER_GMT;}
if ($DBasterisk_version) {$AST_ver = $DBasterisk_version;}
if ($DBmax_vicidial_trunks) {$max_vicidial_trunks = $DBmax_vicidial_trunks;}
$rec_count++;
}
$sthA->finish();
### Grab call parameters from vicidial_auto_calls table
$stmtA = "UPDATE vicidial_auto_calls set uniqueid='$unique_id', channel='$channel',status='LIVE',stage='LIVE-0' where callerid='$callerid' order by call_time desc limit 1;";
$affected_rows = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "-- VDAD : |$affected_rows|update of vac table: $callerid\n|$stmtA|"; &agi_output;}
if ($affected_rows > 0)
{
$stmtA = "SELECT campaign_id,phone_number,phone_code,lead_id,call_time FROM vicidial_auto_calls where callerid='$callerid' order by call_time desc limit 1;";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
$rec_count=0;
while ($sthArows > $rec_count)
{
@aryA = $sthA->fetchrow_array;
$VDADcampaign = "$aryA[0]";
$VDADphone = "$aryA[1]";
$VDADphone_code = "$aryA[2]";
$VDADlead_id = "$aryA[3]";
$VDADcall_time = "$aryA[4]";
$rec_count++;
}
$sthA->finish();
$stmtA = "INSERT INTO vicidial_log (uniqueid,lead_id,campaign_id,call_date,start_epoch,status,phone_code,phone_number,user,processed) values('$uniqueid','$CIDlead_id','$VDADcampaign','$SQLdate','$now_date_epoch','N','$VDADphone_code','$VDADphone','VDAD','N')";
if ($AGILOG) {$agi_string = "|$stmtA|"; &agi_output;}
$affected_rows = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "-- VDAD : |$VDADlead_id|$CIDlead_id|insert to vicidial_log: $uniqueid"; &agi_output;}
}
######### REMINDER CHOICE FROM RECORDINGS ########
&reminder_choice;
if ($pin < 1)
{
&reminder_choice;
if ($pin < 1)
{
$AGI->stream_file("$goodbye_audio_file");
$stmtA = "DELETE FROM vicidial_auto_calls where callerid='$callerid' order by call_time desc limit 1;";
$affected_rows = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "-- VDAD vac record deleted: |$affected_rows|$VDADcampaign|"; &agi_output;}
$end_date_epoch = time();
$length_in_sec = ($end_date_epoch - $now_date_epoch);
$stmtA = "UPDATE vicidial_log set end_epoch='$end_date_epoch', length_in_sec='$length_in_sec' where uniqueid='$uniqueid' and lead_id='$CIDlead_id';";
$affected_rows = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "-- VDAD vac record deleted: |$affected_rows|$VDADcampaign|$end_date_epoch|$now_date_epoch|$length_in_sec|"; &agi_output;}
$VHqueryCID = "VHNGUP$tsSQLdate";
### insert a NEW record to the vicidial_manager table to be processed
$stmtA = "INSERT INTO vicidial_manager values('','','$SQLdate','NEW','N','$VARserver_ip','','Hangup','$VHqueryCID','Channel: $channel','','','','','','','','','')";
$affected_rows = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "-- VDAD call_hungup timout: |$VHqueryCID|$VDADconf_exten|$channel|insert to vicidial_manager"; &agi_output;}
sleep(1);
exit;
}
}
if ($AGILOG) {$agi_string = "+++++ VDAD STATUS : pressed $pin|$unique_id|$channel|$extension|$type|$callerid|$CIDlead_id|$now_date"; &agi_output;}
if ($pin !~ /^1|^2|^3/) {exit;}
if (!$DB_port) {$DB_port='3306';}
if ($pin =~ /^1|^2|^3/)
{
if ($pin == "1") ### send call to voicemail
{
# message can be left on agent vmail box
# # Thank you for your response, we will now transfer you to your agent's voicemail box.
# $AGI->stream_file("$voicemail_xfer_file");
$stmtA = "UPDATE vicidial_auto_calls set status='XFER', stage='XFER-$drop_timer' where callerid='$callerid';";
$affected_rows = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "-- VDAD XFER : |$affected_rows|update of vac table: $callerid\n|$stmtA|"; &agi_output;}
$stmtA = "UPDATE vicidial_list set status='VM' where lead_id = '$CIDlead_id';";
if ($AGILOG) {$agi_string = "|$stmtA|"; &agi_output;}
$affected_rows = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "-- VDAD vicidial_list update: |$affected_rows|$CIDlead_id|XFER"; &agi_output;}
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year = ($year + 1900);
$mon++;
if ($mon < 10) {$mon = "0$mon";}
if ($mday < 10) {$mday = "0$mday";}
if ($hour < 10) {$hour = "0$hour";}
if ($min < 10) {$min = "0$min";}
if ($sec < 10) {$sec = "0$sec";}
$end_date_epoch = time();
$length_in_sec = ($end_date_epoch - $now_date_epoch);
$now_date = "$year-$mon-$mday $hour:$min:$sec";
if ($AGILOG) {$agi_string = "XXXXXXXXXX VDAD transferred: start|stop $start_time|$now_date"; &agi_output;}
$stmtA = "UPDATE vicidial_log set status='VM', end_epoch='$end_date_epoch', length_in_sec='$length_in_sec' where uniqueid='$uniqueid' and lead_id='$CIDlead_id';";
$affected_rows = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "-- VDAD vac record updated: |$affected_rows|$VDADcampaign|"; &agi_output;}
$VMAIL_box=0;
### Grab Server values from the database
$stmtA = "SELECT security_phrase FROM vicidial_list where lead_id = '$CIDlead_id';";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
$rec_count=0;
while ($sthArows > $rec_count)
{
@aryA = $sthA->fetchrow_array;
$VMAIL_box = "$aryA[0]";
$rec_count++;
}
$sthA->finish();
### SEND CALL TO VOICEMAIL BOX ###
if ($AGILOG) {$agi_string = "exiting the VDAD app, transferring call to $voicemail_dump_exten$VMAIL_box"; &agi_output;}
print "SET CONTEXT $ext_context\n";
$result = <STDIN>;
checkresult($result);
print "SET EXTENSION $voicemail_dump_exten$VMAIL_box\n";
$result = <STDIN>;
checkresult($result);
print "SET PRIORITY 1\n";
$result = <STDIN>;
checkresult($result);
}
if ($pin == "2") ### mark call as a CALLBK
{
# Thank you for your response, your agent will be contacting you shortly.
$stmtA = "DELETE from vicidial_auto_calls where callerid='$callerid';";
$affected_rows = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "-- VDAD DONE : |$affected_rows|delete of vac table: $callerid\n|$stmtA|"; &agi_output;}
$stmtA = "UPDATE vicidial_list set status='CALLBK' where lead_id = '$CIDlead_id';";
if ($AGILOG) {$agi_string = "|$stmtA|"; &agi_output;}
$affected_rows = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "-- VDAD vicidial_list update: |$affected_rows|$CIDlead_id|XFER"; &agi_output;}
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year = ($year + 1900);
$mon++;
if ($mon < 10) {$mon = "0$mon";}
if ($mday < 10) {$mday = "0$mday";}
if ($hour < 10) {$hour = "0$hour";}
if ($min < 10) {$min = "0$min";}
if ($sec < 10) {$sec = "0$sec";}
$end_date_epoch = time();
$length_in_sec = ($end_date_epoch - $now_date_epoch);
$now_date = "$year-$mon-$mday $hour:$min:$sec";
if ($AGILOG) {$agi_string = "XXXXXXXXXX VDAD transferred: start|stop $start_time|$now_date"; &agi_output;}
$stmtA = "UPDATE vicidial_log set status='CALLBK', end_epoch='$end_date_epoch', length_in_sec='$length_in_sec' where uniqueid='$uniqueid' and lead_id='$CIDlead_id';";
$affected_rows = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "-- VDAD vac record updated: |$affected_rows|$VDADcampaign|"; &agi_output;}
$AGI->stream_file("$callback_audio_file");
$VHqueryCID = "VHNGUP$tsSQLdate";
### insert a NEW record to the vicidial_manager table to be processed
$stmtA = "INSERT INTO vicidial_manager values('','','$SQLdate','NEW','N','$VARserver_ip','','Hangup','$VHqueryCID','Channel: $channel','','','','','','','','','')";
$affected_rows = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "-- VDAD call_hungup timout: |$VHqueryCID|$VDADconf_exten|$channel|insert to vicidial_manager"; &agi_output;}
sleep(1);
exit;
}
if ($pin == "3") ### send call to extension
{
# call can be sent to extension
# # Thank you for your response, we will now transfer you to your agent's voicemail box.
# $AGI->stream_file("$voicemail_xfer_file");
$stmtA = "UPDATE vicidial_auto_calls set status='XFER' where callerid='$callerid';";
$affected_rows = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "-- VDAD XFER : |$affected_rows|update of vac table: $callerid\n|$stmtA|"; &agi_output;}
$stmtA = "UPDATE vicidial_list set status='XFER' where lead_id = '$CIDlead_id';";
if ($AGILOG) {$agi_string = "|$stmtA|"; &agi_output;}
$affected_rows = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "-- VDAD vicidial_list update: |$affected_rows|$CIDlead_id|XFER"; &agi_output;}
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year = ($year + 1900);
$mon++;
if ($mon < 10) {$mon = "0$mon";}
if ($mday < 10) {$mday = "0$mday";}
if ($hour < 10) {$hour = "0$hour";}
if ($min < 10) {$min = "0$min";}
if ($sec < 10) {$sec = "0$sec";}
$end_date_epoch = time();
$length_in_sec = ($end_date_epoch - $now_date_epoch);
$now_date = "$year-$mon-$mday $hour:$min:$sec";
if ($AGILOG) {$agi_string = "XXXXXXXXXX VDAD transferred: start|stop $start_time|$now_date"; &agi_output;}
$stmtA = "UPDATE vicidial_log set status='XFER', end_epoch='$end_date_epoch', length_in_sec='$length_in_sec' where uniqueid='$uniqueid' and lead_id='$CIDlead_id';";
$affected_rows = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "-- VDAD vac record updated: |$affected_rows|$VDADcampaign|"; &agi_output;}
$VMAIL_box=0;
### Grab Server values from the database
$stmtA = "SELECT comments FROM vicidial_list where lead_id = '$CIDlead_id';";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
$rec_count=0;
while ($sthArows > $rec_count)
{
@aryA = $sthA->fetchrow_array;
$XFER_exten = "$aryA[0]";
$rec_count++;
}
$sthA->finish();
### SEND CALL TO VOICEMAIL BOX ###
if ($AGILOG) {$agi_string = "exiting the VDAD app, transferring call to $XFER_exten"; &agi_output;}
print "SET CONTEXT $ext_context\n"
$result = <STDIN>;
checkresult($result);
print "SET EXTENSION $XFER_exten\n";
$result = <STDIN>;
checkresult($result);
print "SET PRIORITY 1\n";
$result = <STDIN>;
checkresult($result);
}
} ### end if pressed 1,2,3
else ### user pressed digit other than 1 or 2 or 3
{
exit;
}
$dbhA->close;
exit;
sub reminder_choice
{
$digit='';
undef $digit;
$interrupt_digit='';
undef $interrupt_digit;
# Hello, this is a courtesy call from ACME Industries reminding you that the deadline for the submission of orders for the June 5th batch delivery is tomorrow, March 10th:
# second audio file
# If you would like to leave a message for your agent please press 1.
# If you would like to have your agent call you back please press 2.
# third audio file
# Thank you, Goodbye
######### PLAY THE MESSAGE FILE THEN WAIT 2 SECONDS FOR DIGITS ############
$interrupt_digit = $AGI->stream_file("$message_audio_file",'123');
if ($AGILOG) {$agi_string = "interrupt_digit |$interrupt_digit|"; &agi_output;}
$digits_being_entered=1;
$digit_loop_counter=0;
$totalDTMF='';
if ($interrupt_digit > 1)
{
if ($interrupt_digit == 48) {$interrupt_digit=0;}
if ($interrupt_digit == 49) {$interrupt_digit=1;}
if ($interrupt_digit == 50) {$interrupt_digit=2;}
if ($interrupt_digit == 51) {$interrupt_digit=3;}
if ($interrupt_digit == 52) {$interrupt_digit=4;}
if ($interrupt_digit == 53) {$interrupt_digit=5;}
if ($interrupt_digit == 54) {$interrupt_digit=6;}
if ($interrupt_digit == 55) {$interrupt_digit=7;}
if ($interrupt_digit == 56) {$interrupt_digit=8;}
if ($interrupt_digit == 57) {$interrupt_digit=9;}
$totalDTMF=$interrupt_digit;
$digit_loop_counter++;
}
$channel_status = $AGI->channel_status("$channel");
if ($AGILOG) {$agi_string = "SSSSSSSSSS Channel Status: $channel_status"; &agi_output;}
if ($channel_status == 6)
{
$stmtA = "UPDATE vicidial_log set status='MP' where uniqueid = '$uniqueid' and lead_id='$CIDlead_id';";
if ($AGILOG) {$agi_string = "|$stmtA|"; &agi_output;}
$affected_rows = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "-- VDAD vicidial_log update: N|$affected_rows|$uniqueid"; &agi_output;}
$stmtA = "UPDATE vicidial_list set status='MP' where lead_id = '$CIDlead_id';";
if ($AGILOG) {$agi_string = "|$stmtA|"; &agi_output;}
$affected_rows = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "-- VDAD vicidial_list update: N|$affected_rows|$CIDlead_id"; &agi_output;}
}
while ($digit_loop_counter < 1)
{
$digit = chr($AGI->wait_for_digit('2000')); # wait 2 seconds for input or until digit pressed
if ($digit =~ /\d/)
{
$totalDTMF = "$totalDTMF$digit";
if ($AGILOG) {$agi_string = "digit |$digit| TotalDTMF |$totalDTMF|"; &agi_output;}
# $AGI->say_digits("$digit");
undef $digit;
}
else
{
$digit_loop_counter=1;
}
$digit_loop_counter++;
}
$totalDTMF =~ s/\D//gi;
$pin = $totalDTMF;
if ($totalDTMF)
{
if ($AGILOG) {$agi_string = "digit |$digit| TotalDTMF |$totalDTMF|"; &agi_output;}
return;
}
######### PLAY THE OPTIONS FILE THEN WAIT 5 SECONDS FOR DIGITS ############
$interrupt_digit = $AGI->stream_file("$options_audio_file",'123');
if ($AGILOG) {$agi_string = "interrupt_digit |$interrupt_digit|"; &agi_output;}
$digits_being_entered=1;
$digit_loop_counter=0;
$totalDTMF='';
if ($interrupt_digit > 1)
{
if ($interrupt_digit == 48) {$interrupt_digit=0;}
if ($interrupt_digit == 49) {$interrupt_digit=1;}
if ($interrupt_digit == 50) {$interrupt_digit=2;}
if ($interrupt_digit == 51) {$interrupt_digit=3;}
if ($interrupt_digit == 52) {$interrupt_digit=4;}
if ($interrupt_digit == 53) {$interrupt_digit=5;}
if ($interrupt_digit == 54) {$interrupt_digit=6;}
if ($interrupt_digit == 55) {$interrupt_digit=7;}
if ($interrupt_digit == 56) {$interrupt_digit=8;}
if ($interrupt_digit == 57) {$interrupt_digit=9;}
$totalDTMF=$interrupt_digit;
$digit_loop_counter++;
}
while ($digit_loop_counter < 1)
{
$digit = chr($AGI->wait_for_digit('10000')); # wait 10 seconds for input or until digit pressed
if ($digit =~ /\d/)
{
$totalDTMF = "$totalDTMF$digit";
if ($AGILOG) {$agi_string = "digit |$digit| TotalDTMF |$totalDTMF|"; &agi_output;}
# $AGI->say_digits("$digit");
undef $digit;
}
else
{
$digit_loop_counter=1;
}
$digit_loop_counter++;
}
$totalDTMF =~ s/\D//gi;
$pin = $totalDTMF;
if ($totalDTMF)
{
if ($AGILOG) {$agi_string = "digit |$digit| TotalDTMF |$totalDTMF|"; &agi_output;}
return;
}
}
sub checkresult {
my ($res) = @_;
my $retval;
$tests++;
chomp $res;
if ($res =~ /^200/) {
$res =~ /result=(-?\d+)/;
if (!length($1)) {
# print STDERR "FAIL ($res)\n";
$fail++;
} else {
# print STDERR "PASS ($1)\n";
$pass++;
}
} else {
# print STDERR "FAIL (unexpected result '$res')\n";
$fail++;
}
}
sub agi_output
{
if ($AGILOG >=2)
{
### open the log file for writing ###
open(Lout, ">>$AGILOGfile")
|| die "Can't open $AGILOGfile: $!\n";
print Lout "$now_date|$script|$agi_string\n";
close(Lout);
}
### send to STDERR writing ###
if ( ($AGILOG == '1') || ($AGILOG == '3') )
{print STDERR "$now_date|$script|$agi_string\n";}
$agi_string='';
}
@@ -0,0 +1,265 @@
#!/usr/bin/perl
#
# agi-VDADfixCXFER.agi version 2.6
#
# runs when a Consultative transfer is released by the vicidial fronter as the
# call arrives on another channel so that the vicidial_live_agents record is
# updated to be accurate.
#
# You need to put lines similar to those below in your extensions.conf file:
#
# ;VICIDIAL Consultative Xfer fix script:
# exten => _8600XXX*.,1,AGI(agi-VDADfixCXFER.agi)
# exten => _010*010*010*015*8600XXX*.,1,AGI(agi-VDADfixCXFER.agi)
#
# Copyright (C) 2013 Matt Florell <vicidial@gmail.com> LICENSE: AGPLv2
#
# CHANGELOG:
# 60809-1749 - First build
# 60817-1122 - Added output options check from database
# 70212-1252 - Fixed issue with Local/ channel transfers on same server
# 130108-1812 - Changes for Asterisk 1.8 compatibility
#
$script = 'agi-VDADfixCXFER.agi';
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year = ($year + 1900);
$mon++;
if ($mon < 10) {$mon = "0$mon";}
if ($mday < 10) {$mday = "0$mday";}
if ($hour < 10) {$hour = "0$hour";}
if ($min < 10) {$min = "0$min";}
if ($sec < 10) {$sec = "0$sec";}
$SQLdate = "$year-$mon-$mday $hour:$min:$sec";
# default path to astguiclient configuration file:
$PATHconf = '/etc/astguiclient.conf';
open(conf, "$PATHconf") || die "can't open $PATHconf: $!\n";
@conf = <conf>;
close(conf);
$i=0;
foreach(@conf)
{
$line = $conf[$i];
$line =~ s/ |>|\n|\r|\t|\#.*|;.*//gi;
if ( ($line =~ /^PATHhome/) && ($CLIhome < 1) )
{$PATHhome = $line; $PATHhome =~ s/.*=//gi;}
if ( ($line =~ /^PATHlogs/) && ($CLIlogs < 1) )
{$PATHlogs = $line; $PATHlogs =~ s/.*=//gi;}
if ( ($line =~ /^PATHagi/) && ($CLIagi < 1) )
{$PATHagi = $line; $PATHagi =~ s/.*=//gi;}
if ( ($line =~ /^PATHweb/) && ($CLIweb < 1) )
{$PATHweb = $line; $PATHweb =~ s/.*=//gi;}
if ( ($line =~ /^PATHsounds/) && ($CLIsounds < 1) )
{$PATHsounds = $line; $PATHsounds =~ s/.*=//gi;}
if ( ($line =~ /^PATHmonitor/) && ($CLImonitor < 1) )
{$PATHmonitor = $line; $PATHmonitor =~ s/.*=//gi;}
if ( ($line =~ /^VARserver_ip/) && ($CLIserver_ip < 1) )
{$VARserver_ip = $line; $VARserver_ip =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_server/) && ($CLIDB_server < 1) )
{$VARDB_server = $line; $VARDB_server =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_database/) && ($CLIDB_database < 1) )
{$VARDB_database = $line; $VARDB_database =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_user/) && ($CLIDB_user < 1) )
{$VARDB_user = $line; $VARDB_user =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_pass/) && ($CLIDB_pass < 1) )
{$VARDB_pass = $line; $VARDB_pass =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_port/) && ($CLIDB_port < 1) )
{$VARDB_port = $line; $VARDB_port =~ s/.*=//gi;}
$i++;
}
if (!$VARDB_port) {$VARDB_port='3306';}
if (!$AGILOGfile) {$AGILOGfile = "$PATHlogs/agiout.$year-$mon-$mday";}
use DBI;
use Asterisk::AGI;
$AGI = new Asterisk::AGI;
$dbhA = DBI->connect("DBI:mysql:$VARDB_database:$VARDB_server:$VARDB_port", "$VARDB_user", "$VARDB_pass")
or die "Couldn't connect to database: " . DBI->errstr;
### Grab Server values from the database
$stmtA = "SELECT agi_output FROM servers where server_ip = '$VARserver_ip';";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
$rec_count=0;
while ($sthArows > $rec_count)
{
$AGILOG = '0';
@aryA = $sthA->fetchrow_array;
$DBagi_output = "$aryA[0]";
if ($DBagi_output =~ /STDERR/) {$AGILOG = '1';}
if ($DBagi_output =~ /FILE/) {$AGILOG = '2';}
if ($DBagi_output =~ /BOTH/) {$AGILOG = '3';}
$rec_count++;
}
$sthA->finish();
### begin parsing run-time options ###
if (length($ARGV[0])>1)
{
if ($AGILOG) {$agi_string = "Perl Environment Dump:"; &agi_output;}
$i=0;
while ($#ARGV >= $i)
{
$args = "$args $ARGV[$i]";
if ($AGILOG) {$agi_string = "$i|$ARGV[$i]"; &agi_output;}
$i++;
}
### list of command-line array arguments:
@ARGV_vars = split(/-----/, $ARGV[0]);
if ($args =~ /--help/i)
{
print "allowed run time options:\n [-q] = quiet\n [-t] = test\n [-debug] = verbose debug messages\n\n";
}
else
{
if ($args =~ /-V/i)
{
$V=1;
}
if ($args =~ /-debug/i)
{
$DG=1;
}
if ($args =~ /-dbAVS/i)
{
$DGA=1;
}
if ($args =~ /-q/i)
{
$q=1;
$Q=1;
}
if ($args =~ /-t/i)
{
$TEST=1;
$T=1;
}
}
}
$|=1;
while(<STDIN>)
{
chomp;
last unless length($_);
if ($AGILOG)
{
if (/^agi_(\w+)\:\s+(.*)$/)
{
$AGI{$1} = $2;
}
}
if (/^agi_uniqueid\:\s+(.*)$/) {$unique_id = $1; $uniqueid = $unique_id;}
if (/^agi_channel\:\s+(.*)$/) {$channel = $1;}
if (/^agi_extension\:\s+(.*)$/) {$extension = $1;}
}
# 010*010*011*011*
$extension =~ s/^\d\d\d\*\d\d\d\*\d\d\d\*\d\d\d\*//gi;
@EXTEN = split(/\*/, $extension);
$conf_exten = $EXTEN[0];
$lead_id = $EXTEN[1];
$user = $EXTEN[2];
$phone_code = $EXTEN[3];
$phone_number = $EXTEN[4];
$campaign_id = $EXTEN[5];
if ($AGILOG) {$agi_string = "AGI Environment Dump:"; &agi_output;}
foreach $i (sort keys %AGI)
{
if ($AGILOG) {$agi_string = " -- $i = $AGI{$i}"; &agi_output;}
}
if ($AGILOG) {$agi_string = "AGI Variables: |$unique_id|$channel|$extension|$type|$callerid|$calleridname|$priority|"; &agi_output;}
if ($AGILOG) {$agi_string = "CXFERfix : $lead_id $user $channel $uniqueid"; &agi_output;}
$CIDlead = sprintf("%010s", $lead_id);
$CIDlead = "C123412345$CIDlead";
print "SET CALLERID $CIDlead\n";
$result = <STDIN>;
checkresult($result);
if ($AGILOG) {$agi_string = "callerID changed: $CIDlead"; &agi_output;}
########## FIND AND UPDATE vicidial_live_agents ##########
$stmtA = "UPDATE vicidial_live_agents SET channel='$channel',call_server_ip='$VARserver_ip',uniqueid='$uniqueid' where user='$user' and lead_id='$lead_id';";
if ($AGILOG) {$agi_string = "|$stmtA|"; &agi_output;}
$affected_rows = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "-- VDAD vicidial_live_agents update: |$affected_rows|$lead_id|"; &agi_output;}
########## INSERT NEW vicidial_auto_calls record ##########
$stmtA = "INSERT INTO vicidial_auto_calls SET channel='$channel',server_ip='$VARserver_ip',uniqueid='$uniqueid',campaign_id='$campaign_id',status='CLOSER',lead_id='$lead_id',phone_code='$phone_code',phone_number='$phone_number',call_time='$SQLdate',call_type='IN',callerid='$CIDlead';";
if ($AGILOG) {$agi_string = "|$stmtA|"; &agi_output;}
$affected_rows = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "-- VDAD vicidial_auto_calls insert: |$affected_rows|$lead_id|"; &agi_output;}
$dbhA->disconnect();
if ($AGILOG) {$agi_string = "exiting the CXFERfix app, transferring call to $conf_exten"; &agi_output;}
#print "SET CONTEXT $ext_context\n";
# checkresult($result);
print "SET EXTENSION $conf_exten\n";
$result = <STDIN>;
checkresult($result);
print "SET PRIORITY 1\n";
$result = <STDIN>;
checkresult($result);
exit;
sub checkresult {
my ($res) = @_;
my $retval;
$tests++;
chomp $res;
if ($res =~ /^200/) {
$res =~ /result=(-?\d+)/;
if (!length($1)) {
# print STDERR "FAIL ($res)\n";
$fail++;
} else {
# print STDERR "PASS ($1)\n";
$pass++;
}
} else {
# print STDERR "FAIL (unexpected result '$res')\n";
$fail++;
}
}
sub agi_output
{
if ($AGILOG >=2)
{
### open the log file for writing ###
open(Lout, ">>$AGILOGfile")
|| die "Can't open $AGILOGfile: $!\n";
print Lout "$now_date|$script|$agi_string\n";
close(Lout);
}
### send to STDERR writing ###
if ( ($AGILOG == '1') || ($AGILOG == '3') )
{print STDERR "$now_date|$script|$agi_string\n";}
$agi_string='';
}
@@ -0,0 +1,445 @@
#!/usr/bin/perl
#
# agi-VDADinbound_NI_DNC_CIDlookup.agi version 2.0.5 *DBI-version*
#
# runs when a call comes in from an inbound call. This script will
# play a message and take input to automatically change customer lead to NI or DNC.
# *** This version detects inbound callerID over PRI ***
#
# Flag Options:
# 1- default to insert into the vicidial_dnc table for DNC selections (Default=YES)
# 2- campaign to take dnc settings from(overrides option 1)
#
# ;inbound call from outbound CID callbacks from VICIDIAL calls:
#exten => 1234,1,Ringing ; call ringing
#exten => 1234,2,Wait(1) ; Wait 1 second for CID delivery from PRI
#exten => 1234,3,Answer ; Answer the line
#exten => 1234,4,AGI(agi-VDADinbound_NI_DNC_CIDlookup.agi,YES-----TESTCAMP)
#exten => 1234,5,Hangup
#
# Copyright (C) 2008 Matt Florell <vicidial@gmail.com> LICENSE: AGPLv2
#
# changes:
# 60914-1051 - First draft
# 80909-0614 - Added optional field for campaign so that vicidial_campaign_dnc could be used
#
$script = 'agi-VDADinbound_NI_DNC_CIDlookup.agi';
### number of seconds to wait until you drop a waiting call
$DROP_TIME = 360;
### files to play for message of not interested or DNC and your request has been processed
$prompt_file = 'not_interested_or_dnc';
$processed_file = 'request_has_been_processed';
$goodbye_audio_file = 'US_reminder_goodbye';
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year = ($year + 1900);
$mon++;
if ($mon < 10) {$mon = "0$mon";}
if ($mday < 10) {$mday = "0$mday";}
if ($hour < 10) {$hour = "0$hour";}
if ($min < 10) {$min = "0$min";}
if ($sec < 10) {$sec = "0$sec";}
$now_date_epoch = time();
$now_date = "$year-$mon-$mday $hour:$min:$sec";
$start_time=$now_date;
$CIDdate = "$mon$mday$hour$min$sec";
$tsSQLdate = "$year$mon$mday$hour$min$sec";
$SQLdate = "$year-$mon-$mday $hour:$min:$sec";
$SQLdateBEGIN = $SQLdate;
# default path to astguiclient configuration file:
$PATHconf = '/etc/astguiclient.conf';
open(conf, "$PATHconf") || die "can't open $PATHconf: $!\n";
@conf = <conf>;
close(conf);
$i=0;
foreach(@conf)
{
$line = $conf[$i];
$line =~ s/ |>|\n|\r|\t|\#.*|;.*//gi;
if ( ($line =~ /^PATHhome/) && ($CLIhome < 1) )
{$PATHhome = $line; $PATHhome =~ s/.*=//gi;}
if ( ($line =~ /^PATHlogs/) && ($CLIlogs < 1) )
{$PATHlogs = $line; $PATHlogs =~ s/.*=//gi;}
if ( ($line =~ /^PATHagi/) && ($CLIagi < 1) )
{$PATHagi = $line; $PATHagi =~ s/.*=//gi;}
if ( ($line =~ /^PATHweb/) && ($CLIweb < 1) )
{$PATHweb = $line; $PATHweb =~ s/.*=//gi;}
if ( ($line =~ /^PATHsounds/) && ($CLIsounds < 1) )
{$PATHsounds = $line; $PATHsounds =~ s/.*=//gi;}
if ( ($line =~ /^PATHmonitor/) && ($CLImonitor < 1) )
{$PATHmonitor = $line; $PATHmonitor =~ s/.*=//gi;}
if ( ($line =~ /^VARserver_ip/) && ($CLIserver_ip < 1) )
{$VARserver_ip = $line; $VARserver_ip =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_server/) && ($CLIDB_server < 1) )
{$VARDB_server = $line; $VARDB_server =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_database/) && ($CLIDB_database < 1) )
{$VARDB_database = $line; $VARDB_database =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_user/) && ($CLIDB_user < 1) )
{$VARDB_user = $line; $VARDB_user =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_pass/) && ($CLIDB_pass < 1) )
{$VARDB_pass = $line; $VARDB_pass =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_port/) && ($CLIDB_port < 1) )
{$VARDB_port = $line; $VARDB_port =~ s/.*=//gi;}
$i++;
}
if (!$VARDB_port) {$VARDB_port='3306';}
if (!$AGILOGfile) {$AGILOGfile = "$PATHlogs/agiout.$year-$mon-$mday";}
use DBI;
use Time::HiRes ('gettimeofday','usleep','sleep'); # necessary to have perl sleep command of less than one second
use Asterisk::AGI;
$AGI = new Asterisk::AGI;
$dbhA = DBI->connect("DBI:mysql:$VARDB_database:$VARDB_server:$VARDB_port", "$VARDB_user", "$VARDB_pass")
or die "Couldn't connect to database: " . DBI->errstr;
### Grab Server values from the database
$stmtA = "SELECT agi_output FROM servers where server_ip = '$VARserver_ip';";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
$rec_count=0;
while ($sthArows > $rec_count)
{
$AGILOG = '0';
@aryA = $sthA->fetchrow_array;
$DBagi_output = "$aryA[0]";
if ($DBagi_output =~ /STDERR/) {$AGILOG = '1';}
if ($DBagi_output =~ /FILE/) {$AGILOG = '2';}
if ($DBagi_output =~ /BOTH/) {$AGILOG = '3';}
$rec_count++;
}
$sthA->finish();
### begin parsing run-time options ###
if (length($ARGV[0])>1)
{
if ($AGILOG) {$agi_string = "Perl Environment Dump:"; &agi_output;}
$i=0;
while ($#ARGV >= $i)
{
$args = "$args $ARGV[$i]";
if ($AGILOG) {$agi_string = "$i|$ARGV[$i]"; &agi_output;}
$i++;
}
### list of command-line array arguments:
@ARGV_vars = split(/-----/, $ARGV[0]);
$dnc_default = $ARGV_vars[0];
$campaign_id = $ARGV_vars[1];
}
if (length($dnc_default) < 2)
{$dnc_default = 'YES';}
$|=1;
while(<STDIN>) {
chomp;
last unless length($_);
if ($AGILOG)
{
if (/^agi_(\w+)\:\s+(.*)$/)
{
$AGI{$1} = $2;
}
}
if (/^agi_uniqueid\:\s+(.*)$/) {$unique_id = $1; $uniqueid = $unique_id;}
if (/^agi_channel\:\s+(.*)$/) {$channel = $1;}
if (/^agi_extension\:\s+(.*)$/) {$extension = $1;}
if (/^agi_type\:\s+(.*)$/) {$type = $1;}
if (/^agi_callerid\:\s+(.*)$/) {$callerid = $1;}
if (/^agi_calleridname\:\s+(.*)$/) {$calleridname = $1;}
if ( ($callerid =~ /\".*\"/) && ( (!$calleridname) or ($calleridname =~ /unknown/) ) )
{
$calleridname = $callerid;
$calleridname =~ s/\<\d\d\d\d\d\d\d\d\d\d\>//gi;
$calleridname =~ s/\"|\" //gi;
}
$callerid =~ s/\D//gi;
$calleridname =~ s/unknown//gi;
if ( (!$callerid) or ($callerid =~ /unknown/) )
{$callerid = $calleridname;}
if (length($callerid)>0) {$phone_number = $callerid;}
else {$phone_number = '';}
if (length($calleridname)>0) {$VLcomments = $calleridname;}
else {$VLcomments = '';}
# if ($channel =~ /^SIP/) {$channel =~ s/-.*//gi;}
# if ($channel =~ /^Zap\//) {$channel =~ s/-\d$//gi;}
if (length($callerid)<10) {$callerid = $parked_by;}
if (length($pin)>0) {$callerid = $pin;}
### allow for external callerid to be collected
# if ($extension =~ /\*\d\d\d\d\d\d\d\d\d\d\*/)
# {
# @EXT_vars = split(/\*/, $extension);
#
# $referring_extension = $EXT_vars[0]; # initial extension sent
# $channel_group = $EXT_vars[1]; # name of the parked group
# $inbound_number = $EXT_vars[2]; # extension to send call to after parsing
# $parked_by = $EXT_vars[3]; # leadID
# $park_extension = $EXT_vars[4]; # filename of the on-hold music file
# $comment_d = $EXT_vars[5]; # N/A
# $comment_e = $EXT_vars[6]; # N/A
#
# $PADlead_id = sprintf("%09s", $parked_by); while (length($PADlead_id) > 9) {chop($PADlead_id);}
# # JmmddhhmmssLLLLLLLLL
# $JqueryCID = "J$CIDdate$PADlead_id";
# $callerid = $JqueryCID;
# ### set the callerid to the filename of the audio for on-hold
# print "SET CALLERID $JqueryCID\n";
# checkresult($result);
# print STDERR "callerID changed: $JqueryCID\n";
# }
}
foreach $i (sort keys %AGI)
{
if ($AGILOG) {$agi_string = " -- $i = $AGI{$i}"; &agi_output;}
}
if ($AGILOG) {$agi_string = "AGI Variables: |$unique_id|$channel|$extension|$type|$callerid|"; &agi_output;}
$AGI->stream_file('beep');
&enter_pin_number;
if ( (length($pin)<1) || ($pin =~ /9/) )
{&enter_pin_number;}
if ( (length($pin)<1) || ($pin =~ /9/) )
{&enter_pin_number;}
if ( (length($pin)<1) || ($pin =~ /9/) )
{&enter_pin_number;}
if ( (length($pin)<1) || ($pin =~ /9/) )
{&enter_pin_number;}
if ($pin =~ /2/) # set the phone_number to NI
{
### Check to see if the number is in the system ###
$stmtA= "SELECT count(*) from vicidial_list where phone_number='$phone_number';";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
if ($sthArows > 0)
{
@aryA = $sthA->fetchrow_array;
$lead_in_system = "$aryA[0]";
$sthA->finish();
### update all records for the phone_number in vicidial_list to NI (Not Interested)
$stmtA = "UPDATE vicidial_list SET status='NI' where phone_number='$phone_number';";
$affected_rows = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "$affected_rows|$stmtA|"; &agi_output;}
}
$AGI->stream_file("$processed_file");
}
if ($pin =~ /3/) # set the phone_number to DNC
{
### Check to see if the number is in the system ###
$stmtA= "SELECT count(*) from vicidial_list where phone_number='$phone_number';";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
if ($sthArows > 0)
{
@aryA = $sthA->fetchrow_array;
$lead_in_system = "$aryA[0]";
$sthA->finish();
### update all records for the phone_number in vicidial_list to NI (Not Interested)
$stmtA = "UPDATE vicidial_list SET status='DNC' where phone_number='$phone_number';";
$affected_rows = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "$affected_rows|$stmtA|"; &agi_output;}
}
if (length($campaign_id)>1)
{
### Grab DNC settings from campaign
$stmtA = "SELECT use_internal_dnc,use_campaign_dnc FROM vicidial_campaigns where campaign_id = '$campaign_id';";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
@aryA = $sthA->fetchrow_array;
$use_internal_dnc = "$aryA[0]";
$use_campaign_dnc = "$aryA[1]";
$sthA->finish();
if ($use_internal_dnc =~ /Y/)
{
### add the number to the vicidial_dnc table
$stmtA = "INSERT INTO vicidial_dnc SET phone_number='$phone_number';";
$affected_rows = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "$affected_rows|$stmtA|"; &agi_output;}
}
if ($use_campaign_dnc =~ /Y/)
{
### add the number to the vicidial_campaign_dnc table
$stmtA = "INSERT INTO vicidial_campaign_dnc SET phone_number='$phone_number',campaign_id='$campaign_id';";
$affected_rows = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "$affected_rows|$stmtA|"; &agi_output;}
}
}
else
{
if ($dnc_default =~ /YES/)
{
### add the number to the vicidial_dnc table
$stmtA = "INSERT INTO vicidial_dnc SET phone_number='$phone_number';";
$affected_rows = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "$affected_rows|$stmtA|"; &agi_output;}
}
}
$AGI->stream_file("$processed_file");
}
$dbhA->disconnect();
$AGI->stream_file("$goodbye_audio_file");
exit;
sub enter_pin_number
{
# if you are not interested in this program press 2
# if you would not like to be contacted by us again press 3
# if you would like us to try to get in contact with you hangup
$interrupt_digit='';
undef $interrupt_digit;
$interrupt_digit = $AGI->stream_file("$prompt_file",'239');
if ($AGILOG) {$agi_string = "interrupt_digit |$interrupt_digit|"; &agi_output;}
$digits_being_entered=1;
$totalDTMF='';
if ($interrupt_digit > 1)
{
if ($interrupt_digit == 48) {$interrupt_digit=0;}
if ($interrupt_digit == 49) {$interrupt_digit=1;}
if ($interrupt_digit == 50) {$interrupt_digit=2;}
if ($interrupt_digit == 51) {$interrupt_digit=3;}
if ($interrupt_digit == 52) {$interrupt_digit=4;}
if ($interrupt_digit == 53) {$interrupt_digit=5;}
if ($interrupt_digit == 54) {$interrupt_digit=6;}
if ($interrupt_digit == 55) {$interrupt_digit=7;}
if ($interrupt_digit == 56) {$interrupt_digit=8;}
if ($interrupt_digit == 57) {$interrupt_digit=9;}
$totalDTMF=$interrupt_digit;
}
#$digit_loop_counter=0;
#while ( ($digits_being_entered) && ($digit_loop_counter < 20) )
# {
# $digit = chr($AGI->wait_for_digit('30000')); # wait 30 seconds for input or until the pound key is pressed
# if ($digit =~ /\d/)
# {
# $totalDTMF = "$totalDTMF$digit";
# print STDERR "digit |$digit| TotalDTMF |$totalDTMF|\n";
# $AGI->say_digits("$digit");
# undef $digit;
# }
# else
# {
# $digits_being_entered=0;
# }
#
# $digit_loop_counter++;
# }
#while ( ($digit_loop_counter < 4) )
# {
# $digit = chr($AGI->wait_for_digit('10000')); # wait 10 seconds for input or until the pound key is pressed
# if ($digit =~ /\d/)
# {
# $totalDTMF = "$totalDTMF$digit";
# if ($AGILOG) {$agi_string = "digit |$digit| TotalDTMF |$totalDTMF|"; &agi_output;}
# $AGI->say_digits("$digit");
# undef $digit;
# }
# else
# {
# $digit_loop_counter=4;
# }
#
# $digit_loop_counter++;
# }
$totalDTMF =~ s/\D//gi;
$pin = $totalDTMF;
if ($totalDTMF) {if ($AGILOG) {$agi_string = "digit |$digit| TotalDTMF |$totalDTMF|"; &agi_output;}}
}
sub checkresult {
my ($res) = @_;
my $retval;
$tests++;
chomp $res;
if ($res =~ /^200/) {
$res =~ /result=(-?\d+)/;
if (!length($1)) {
# print STDERR "FAIL ($res)\n";
$fail++;
} else {
# print STDERR "PASS ($1)\n";
$pass++;
}
} else {
# print STDERR "FAIL (unexpected result '$res')\n";
$fail++;
}
}
sub agi_output
{
if ($AGILOG >=2)
{
### open the log file for writing ###
open(Lout, ">>$AGILOGfile")
|| die "Can't open $AGILOGfile: $!\n";
print Lout "$now_date|$script|$agi_string\n";
close(Lout);
}
### send to STDERR writing ###
if ( ($AGILOG == '1') || ($AGILOG == '3') )
{print STDERR "$now_date|$script|$agi_string\n";}
$agi_string='';
}
@@ -0,0 +1,293 @@
#!/usr/bin/perl
#
# agi-VDADlisten_DTMF.agi version 2.0.5 *DBI-version*
#
# To be initiated by an agent so during a call. Agent will click a PIN NUMBER
# link on a web form webpage that will Originate a new call to this script
#
# This script plays a beep and waits for entry of 4 digits then inserts record
#
# ***** THIS SCRIPT ONLY WORKS WITH A CROSSOVER T1 CABLE FROM ONE T1 PORT *****
# ***** TO ANOTHER T1 PORT WHETHER ON THE SAME SERVER OR DIFFERENT ONE *****
#
# IN THE CAMPAIGN OR IN-GROUP NUMBER PRESET FIELD:
# AXFER83009
#
# ON THE SERVER ORIGINATING THE CALL:
#;outbound DTMF VICIDIAL calls:
#exten => _983009.,1,AGI(call_log.agi,${EXTEN})
#exten => _983009.,2,Dial(${TRUNKY}/${EXTEN:1},60)
#exten => _983009.,3,Hangup
#
# ON THE SERVER THAT HAS THIS SCRIPT:
#;inbound DTMF VICIDIAL calls:
#exten => _83009.,1,AGI(agi-VDADlisten_DTMF.agi,${EXTEN})
#exten => _83009.,2,Hangup
#
# Copyright (C) 2008 Matt Florell <vicidial@gmail.com> LICENSE: AGPLv2
#
# changes:
# 60206-1434 first build
# 60207-1205 - added database records entries/modifications
# 60207-1507 - added ability to send AMs to campaign-defined message to leave
# 60731-1536 - changed to use DBI-DBD::mysql
# - changed to use /etc/astguiclient.conf for configs
# 60817-1113 - added output options check from database
# 70806-1619 - added HeavyDTMF check and filter
#
$script = 'agi-VDADlisten_DTMF.agi';
$heavyDTMFcheck = 1; # set to 1 to check for heavy DTMF double-tones
### set defaults for testing
$referring_extension = '83009';
$phone_number = '9999999999';
$lead_id = '999999999';
$campaign_id = 'TESTCAMP';
$product_code = 'ABC';
$user = '1234';
$pin = '0';
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year = ($year + 1900);
$mon++;
if ($mon < 10) {$mon = "0$mon";}
if ($mday < 10) {$mday = "0$mday";}
if ($hour < 10) {$hour = "0$hour";}
if ($min < 10) {$min = "0$min";}
if ($sec < 10) {$sec = "0$sec";}
$SQLdate = "$year-$mon-$mday $hour:$min:$sec";
# default path to astguiclient configuration file:
$PATHconf = '/etc/astguiclient.conf';
open(conf, "$PATHconf") || die "can't open $PATHconf: $!\n";
@conf = <conf>;
close(conf);
$i=0;
foreach(@conf)
{
$line = $conf[$i];
$line =~ s/ |>|\n|\r|\t|\#.*|;.*//gi;
if ( ($line =~ /^PATHhome/) && ($CLIhome < 1) )
{$PATHhome = $line; $PATHhome =~ s/.*=//gi;}
if ( ($line =~ /^PATHlogs/) && ($CLIlogs < 1) )
{$PATHlogs = $line; $PATHlogs =~ s/.*=//gi;}
if ( ($line =~ /^PATHagi/) && ($CLIagi < 1) )
{$PATHagi = $line; $PATHagi =~ s/.*=//gi;}
if ( ($line =~ /^PATHweb/) && ($CLIweb < 1) )
{$PATHweb = $line; $PATHweb =~ s/.*=//gi;}
if ( ($line =~ /^PATHsounds/) && ($CLIsounds < 1) )
{$PATHsounds = $line; $PATHsounds =~ s/.*=//gi;}
if ( ($line =~ /^PATHmonitor/) && ($CLImonitor < 1) )
{$PATHmonitor = $line; $PATHmonitor =~ s/.*=//gi;}
if ( ($line =~ /^VARserver_ip/) && ($CLIserver_ip < 1) )
{$VARserver_ip = $line; $VARserver_ip =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_server/) && ($CLIDB_server < 1) )
{$VARDB_server = $line; $VARDB_server =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_database/) && ($CLIDB_database < 1) )
{$VARDB_database = $line; $VARDB_database =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_user/) && ($CLIDB_user < 1) )
{$VARDB_user = $line; $VARDB_user =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_pass/) && ($CLIDB_pass < 1) )
{$VARDB_pass = $line; $VARDB_pass =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_port/) && ($CLIDB_port < 1) )
{$VARDB_port = $line; $VARDB_port =~ s/.*=//gi;}
$i++;
}
if (!$VARDB_port) {$VARDB_port='3306';}
if (!$AGILOGfile) {$AGILOGfile = "$PATHlogs/agiout.$year-$mon-$mday";}
use Time::HiRes qw(gettimeofday);
use DBI;
use Asterisk::AGI;
$AGI = new Asterisk::AGI;
$dbhA = DBI->connect("DBI:mysql:$VARDB_database:$VARDB_server:$VARDB_port", "$VARDB_user", "$VARDB_pass")
or die "Couldn't connect to database: " . DBI->errstr;
### Grab Server values from the database
$stmtA = "SELECT agi_output FROM servers where server_ip = '$VARserver_ip';";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
$rec_count=0;
while ($sthArows > $rec_count)
{
$AGILOG = '0';
@aryA = $sthA->fetchrow_array;
$DBagi_output = "$aryA[0]";
if ($DBagi_output =~ /STDERR/) {$AGILOG = '1';}
if ($DBagi_output =~ /FILE/) {$AGILOG = '2';}
if ($DBagi_output =~ /BOTH/) {$AGILOG = '3';}
$rec_count++;
}
$sthA->finish();
### begin parsing run-time options ###
if (length($ARGV[0])>1)
{
if ($AGILOG) {$agi_string = "Perl Environment Dump:"; &agi_output;}
$i=0;
while ($#ARGV >= $i)
{
$args = "$args $ARGV[$i]";
if ($AGILOG) {$agi_string = "$i|$ARGV[$i]"; &agi_output;}
$i++;
}
if ($args =~ /--help/i)
{
print "allowed run time options:\n [-q] = quiet\n [-t] = test\n [-debug] = verbose debug messages\n\n";
}
else
{
if ($args =~ /-V/i)
{
$V=1;
}
if ($args =~ /-debug/i)
{
$DG=1;
}
if ($args =~ /-q/i)
{
$q=1;
$Q=1;
}
if ($args =~ /-t/i)
{
$TEST=1;
$T=1;
}
}
}
$|=1;
while(<STDIN>) {
chomp;
last unless length($_);
if ($AGILOG)
{
if (/^agi_(\w+)\:\s+(.*)$/)
{
$AGI{$1} = $2;
}
}
if (/^agi_extension\:\s+(.*)$/) {$extension = $1;}
### allow for internal transfer data string "83009*3125551212*10000123*TESTCAMP*ABC*1234*"
if ($extension =~ /^8300\d\*/)
{
@EXT_vars = split(/\*/, $extension);
$referring_extension = $EXT_vars[0]; # initial extension sent
$phone_number = $EXT_vars[1]; # phone number
$lead_id = $EXT_vars[2]; # lead_id in vicidial_list
$campaign_id = $EXT_vars[3]; # campaign id
$product_code = $EXT_vars[4]; # product code
$user = $EXT_vars[5]; # vicidial_user that sent the call here
}
}
if ($AGILOG) {$agi_string = "INBOUND DTMF CALL VDCL STARTED"; &agi_output;}
&enter_pin_number;
if (length($pin) < 4) {$AGI->stream_file('buzz');}
else {$AGI->stream_file('ding');}
### insert a record into the vicidial_list_pins table
$stmtA = "INSERT INTO vicidial_list_pins (entry_time,phone_number,lead_id,campaign_id,product_code,user,digits) values('$SQLdate','$phone_number','$lead_id','$campaign_id','$product_code','$user','$pin');";
$affected_rows = $dbhA->do($stmtA);
$insert_pin_id = $dbhA->{'mysql_insertid'};
if ($AGILOG) {$agi_string = "|$stmtA|$insert_pin_id|$affected_rows|"; &agi_output;}
$dbhA->disconnect();
exit;
sub enter_pin_number
{
$lastdigit='';
$lastdigittime='';
$AGI->stream_file('beep');
while ( ($digit_loop_counter < 4) )
{
$digit = chr($AGI->wait_for_digit('30000')); # wait 30 seconds for input or until the pound key is pressed
if ($digit =~ /\d/)
{
# get seconds and microseconds since the epoch
($s, $usec) = gettimeofday();
$Susec = sprintf("%06s", $usec);
$utime = "$s$Susec";
if (length($lastdigittime)<1) {$lastdigittime = $utime; $lastdigit = 99; $difftime = 0;}
else
{
$difftime = ($utime - $lastdigittime);
}
print STDERR "$s$Susec - $digit - $lastdigittime - $difftime\n";
if ( ($difftime < 200000) && ($digit == $lastdigit) && ($heavyDTMFcheck>0) )
{
print STDERR "HEAVY DTMF DETECTED AND IGNORED: $digit - $lastdigit - $difftime\n";
$digit_loop_counter = ($digit_loop_counter - 1);
}
else
{
$totalDTMF = "$totalDTMF$digit";
if ($AGILOG) {$agi_string = "digit |$digit| TotalDTMF |$totalDTMF|"; &agi_output;}
# $AGI->say_digits("$digit");
}
$lastdigittime = $utime; $lastdigit = $digit;
undef $digit;
}
else
{
$digit_loop_counter=4;
}
$digit_loop_counter++;
}
$totalDTMF =~ s/\D//gi;
$pin = $totalDTMF;
if ($totalDTMF)
{
if ($AGILOG) {$agi_string = "digit |$digit| TotalDTMF |$totalDTMF|"; &agi_output;}
}
# $AGI->say_digits("$pin");
}
sub agi_output
{
if ($AGILOG >=2)
{
### open the log file for writing ###
open(Lout, ">>$AGILOGfile")
|| die "Can't open $AGILOGfile: $!\n";
print Lout "$now_date|$script|$agi_string\n";
close(Lout);
}
### send to STDERR writing ###
if ( ($AGILOG == '1') || ($AGILOG == '3') )
{print STDERR "$now_date|$script|$agi_string\n";}
$agi_string='';
}
@@ -0,0 +1,175 @@
#!/usr/bin/perl
#
# agi-VDADselective_CID.agi version 0.1 *NO DB Connection*
#
# Runs before a call is placed outbound to filter outgoing callerIDnumber
#
# You need to put lines similar to those below in your extensions.conf file:
#
# ;outbound VICIDIAL calls:
#exten => _91NXXNXXXXXX,1,AGI(call_log.agi,${EXTEN})
#exten => _91NXXNXXXXXX,2,AGI(agi-VDADselective_CID.agi,${EXTEN})
#exten => _91NXXNXXXXXX,3,Dial(${TRUNKX}/${EXTEN:1},55,tTo)
#exten => _91NXXNXXXXXX,4,Hangup
#
# Copyright (C) 2008 Matt Florell <vicidial@gmail.com> LICENSE: AGPLv2
#
# changes:
# 60918-1042 - First build
# 71012-1239 - changed method for getting areacode
#
$script = 'agi-VDADselective_CID.agi';
$AGILOG=0;
$FD=0; # force debug for testing
# CLEAR AREA CODE LISTINGS:
# Florida: 239|305|321|352|386|407|561|689|727|754|772|786|813|836|850|861|863|904|941|954
$clear_area_codes = '239|305|321|352|386|407|561|689|727|754|772|786|813|836|850|861|863|904|941|954';
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year = ($year + 1900);
$mon++;
if ($mon < 10) {$mon = "0$mon";}
if ($mday < 10) {$mday = "0$mday";}
if ($hour < 10) {$hour = "0$hour";}
if ($min < 10) {$min = "0$min";}
if ($sec < 10) {$sec = "0$sec";}
$now_date_epoch = time();
$now_date = "$year-$mon-$mday $hour:$min:$sec";
use Asterisk::AGI;
$AGI = new Asterisk::AGI;
### begin parsing run-time options ###
if (length($ARGV[0])>1)
{
if ($AGILOG) {$agi_string = "Perl Environment Dump:"; &agi_output;}
$i=0;
while ($#ARGV >= $i)
{
$args = "$args $ARGV[$i]";
if ($AGILOG) {$agi_string = "$i|$ARGV[$i]"; &agi_output;}
$i++;
}
}
$|=1;
while(<STDIN>)
{
chomp;
last unless length($_);
if ($AGILOG)
{
if (/^agi_(\w+)\:\s+(.*)$/)
{
$AGI{$1} = $2;
}
}
if (/^agi_extension\:\s+(.*)$/) {$extension = $1;}
if (/^agi_callerid\:\s+(.*)$/) {$callerid = $1;}
if (/^agi_calleridname\:\s+(.*)$/) {$calleridname = $1;}
if ( ($callerid =~ /\".*\"/) && ( (!$calleridname) or ($calleridname =~ /unknown/) ) )
{
$calleridname = $callerid;
$calleridname =~ s/\<\d\d\d\d\d\d\d\d\d\d\>//gi;
$calleridname =~ s/\"|\" //gi;
}
$callerid =~ s/\D//gi;
$calleridname =~ s/unknown//gi;
if ( (!$callerid) or ($callerid =~ /unknown/) )
{$callerid = $calleridname;}
if (length($callerid)>0) {$phone_number = $callerid;}
else {$phone_number = '';}
if (length($calleridname)>0) {$VLcomments = $calleridname;}
else {$VLcomments = '';}
}
$areacode = substr($extension, -10, 3);
foreach $i (sort keys %AGI)
{
if ($AGILOG) {$agi_string = " -- $i = $AGI{$i}"; &agi_output;}
}
if ($areacode !~ /$clear_area_codes/)
{
if ( (length($callerid)>20) && ($callerid =~ /\"\S\S\S\S\S\S\S\S\S\S\S\S\S\S\S\S\S\S/) )
{
$calleridname =~ s/^\"//gi;
$calleridname =~ s/\".*$//gi;
}
### set the callerid to the ACQS value(calleridname)
## use these two lines for Asterisk 1.2 tree
$newcallerid = "\"$calleridname <0000000000>\"";
$AGI->set_callerid($newcallerid);
## use these two lines for Asterisk 1.0 tree
# print "SET CALLERID \"$calleridname\" <0000000000>\n";
# print "SET CALLERIDNAME \"$calleridname\"\n";
print STDERR "Setting CID \"$newcallerid\"\n";
checkresult($result);
if ($AGILOG) {$agi_string = "callerID changed: \"$calleridname\" \<0000000000\>"; &agi_output;}
if ($FD > 0) {print STDERR "callerID changed: \"$calleridname\" \<0000000000\> |$areacode|\n";}
}
else
{
if ($FD > 0) {print STDERR "callerID NOT changed: \"$calleridname\" <$callerid> |$areacode|\n";}
}
if ($AGILOG) {$agi_string = "AGI Variables: |$extension|$areacode|$callerid|$calleridname|"; &agi_output;}
exit;
sub checkresult {
my ($res) = @_;
my $retval;
$tests++;
chomp $res;
if ($res =~ /^200/) {
$res =~ /result=(-?\d+)/;
if (!length($1)) {
# print STDERR "FAIL ($res)\n";
$fail++;
} else {
# print STDERR "PASS ($1)\n";
$pass++;
}
} else {
# print STDERR "FAIL (unexpected result '$res')\n";
$fail++;
}
}
sub agi_output
{
if ($AGILOG >=2)
{
### open the log file for writing ###
open(Lout, ">>$AGILOGfile")
|| die "Can't open $AGILOGfile: $!\n";
print Lout "$now_date|$script|$agi_string\n";
close(Lout);
}
### send to STDERR writing ###
if ( ($AGILOG == '1') || ($AGILOG == '3') )
{print STDERR "$now_date|$script|$agi_string\n";}
$agi_string='';
}
@@ -0,0 +1,231 @@
#!/usr/bin/perl
#
# agi-VDADselective_CID_hangup.agi version 2.6
#
# Will hangup a call if it is not within the specified area codes
#
# You need to put lines similar to those below in your extensions.conf file:
#
# ;outbound VICIDIAL calls:
#exten => 8352,1,AGI(agi-VDADselective_CID_hangup.agi,${EXTEN})
#exten => 8352,2,Playback(85100001)
#exten => 8352,3,Hangup
#
# Copyright (C) 2013 Matt Florell <vicidial@gmail.com> LICENSE: AGPLv2
#
# changes:
# 60918-1151 - First build
# 100903-0041 - Changed lead_id max length to 10 digits
# 130108-1813 - Changes for Asterisk 1.8 compatibility
#
$script = 'agi-VDADselective_CID_hangup.agi';
$AGILOG=1;
# CLEAR AREA CODE LISTINGS:
# Florida: 239|305|321|352|386|407|561|689|727|754|772|786|813|836|850|861|863|904|941|954
$clear_area_codes = '239|305|321|352|386|407|561|689|727|754|772|786|813|836|850|861|863|904|941|954';
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year = ($year + 1900);
$mon++;
if ($mon < 10) {$mon = "0$mon";}
if ($mday < 10) {$mday = "0$mday";}
if ($hour < 10) {$hour = "0$hour";}
if ($min < 10) {$min = "0$min";}
if ($sec < 10) {$sec = "0$sec";}
$now_date_epoch = time();
$now_date = "$year-$mon-$mday $hour:$min:$sec";
# default path to astguiclient configuration file:
$PATHconf = '/etc/astguiclient.conf';
open(conf, "$PATHconf") || die "can't open $PATHconf: $!\n";
@conf = <conf>;
close(conf);
$i=0;
foreach(@conf)
{
$line = $conf[$i];
$line =~ s/ |>|\n|\r|\t|\#.*|;.*//gi;
if ( ($line =~ /^PATHhome/) && ($CLIhome < 1) )
{$PATHhome = $line; $PATHhome =~ s/.*=//gi;}
if ( ($line =~ /^PATHlogs/) && ($CLIlogs < 1) )
{$PATHlogs = $line; $PATHlogs =~ s/.*=//gi;}
if ( ($line =~ /^PATHagi/) && ($CLIagi < 1) )
{$PATHagi = $line; $PATHagi =~ s/.*=//gi;}
if ( ($line =~ /^PATHweb/) && ($CLIweb < 1) )
{$PATHweb = $line; $PATHweb =~ s/.*=//gi;}
if ( ($line =~ /^PATHsounds/) && ($CLIsounds < 1) )
{$PATHsounds = $line; $PATHsounds =~ s/.*=//gi;}
if ( ($line =~ /^PATHmonitor/) && ($CLImonitor < 1) )
{$PATHmonitor = $line; $PATHmonitor =~ s/.*=//gi;}
if ( ($line =~ /^VARserver_ip/) && ($CLIserver_ip < 1) )
{$VARserver_ip = $line; $VARserver_ip =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_server/) && ($CLIDB_server < 1) )
{$VARDB_server = $line; $VARDB_server =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_database/) && ($CLIDB_database < 1) )
{$VARDB_database = $line; $VARDB_database =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_user/) && ($CLIDB_user < 1) )
{$VARDB_user = $line; $VARDB_user =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_pass/) && ($CLIDB_pass < 1) )
{$VARDB_pass = $line; $VARDB_pass =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_port/) && ($CLIDB_port < 1) )
{$VARDB_port = $line; $VARDB_port =~ s/.*=//gi;}
$i++;
}
if (!$VARDB_port) {$VARDB_port='3306';}
if (!$AGILOGfile) {$AGILOGfile = "$PATHlogs/agiout.$year-$mon-$mday";}
use DBI;
use Asterisk::AGI;
$AGI = new Asterisk::AGI;
$dbhA = DBI->connect("DBI:mysql:$VARDB_database:$VARDB_server:$VARDB_port", "$VARDB_user", "$VARDB_pass")
or die "Couldn't connect to database: " . DBI->errstr;
### begin parsing run-time options ###
if (length($ARGV[0])>1)
{
if ($AGILOG) {$agi_string = "Perl Environment Dump:"; &agi_output;}
$i=0;
while ($#ARGV >= $i)
{
$args = "$args $ARGV[$i]";
if ($AGILOG) {$agi_string = "$i|$ARGV[$i]"; &agi_output;}
$i++;
}
}
$|=1;
while(<STDIN>)
{
chomp;
last unless length($_);
if ($AGILOG)
{
if (/^agi_(\w+)\:\s+(.*)$/)
{
$AGI{$1} = $2;
}
}
if (/^agi_channel\:\s+(.*)$/) {$channel = $1;}
if (/^agi_extension\:\s+(.*)$/) {$extension = $1;}
if (/^agi_callerid\:\s+(.*)$/) {$callerid = $1;}
if (/^agi_calleridname\:\s+(.*)$/) {$calleridname = $1;}
if ( ($callerid =~ /\".*\"/) && ( (!$calleridname) or ($calleridname =~ /unknown/) ) )
{
$calleridname = $callerid;
$calleridname =~ s/\<\d\d\d\d\d\d\d\d\d\d\>//gi;
$calleridname =~ s/\"|\" //gi;
}
$callerid =~ s/\D//gi;
$calleridname =~ s/unknown//gi;
if ( (!$callerid) or ($callerid =~ /unknown/) )
{$callerid = $calleridname;}
if (length($callerid)>0) {$phone_number = $callerid;}
else {$phone_number = '';}
}
$CIDlead_id = $calleridname;
$CIDlead_id = substr($CIDlead_id, 10, 10);
$CIDlead_id = ($CIDlead_id + 0);
$stmtA = "SELECT phone_number FROM vicidial_list where lead_id = '$CIDlead_id' limit 1;";
if ($AGILOG) {$agi_string = "|$stmtA|"; &agi_output;}
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
$rec_count=0;
while ($sthArows > $rec_count)
{
@aryA = $sthA->fetchrow_array;
$phone_number = "$aryA[0]";
$rec_count++;
}
$sthA->finish();
$areacode = $phone_number;
if (length($areacode) > 10)
{
$areacode =~ s/^91|^991|^981|^971|^961|^951//gi;
$areacode =~ s/^1//gi;
}
$areacode =~ s/\d\d\d\d\d\d\d$//gi;
foreach $i (sort keys %AGI)
{
if ($AGILOG) {$agi_string = " -- $i = $AGI{$i}"; &agi_output;}
}
if ($areacode !~ /$clear_area_codes/)
{
print "SET HANGUP $channel\n";
$result = <STDIN>;
checkresult($result);
if ($AGILOG) {$agi_string = "channel Hungup: $channel"; &agi_output;}
print "SET EXTENSION $extension\n";
$result = <STDIN>;
checkresult($result);
print "SET PRIORITY 3\n";
$result = <STDIN>;
checkresult($result);
}
if ($AGILOG) {$agi_string = "AGI Variables: |$extension|$areacode|$phone_number|$callerid|$calleridname|$channel|"; &agi_output;}
exit;
sub checkresult {
my ($res) = @_;
my $retval;
$tests++;
chomp $res;
if ($res =~ /^200/) {
$res =~ /result=(-?\d+)/;
if (!length($1)) {
# print STDERR "FAIL ($res)\n";
$fail++;
} else {
# print STDERR "PASS ($1)\n";
$pass++;
}
} else {
# print STDERR "FAIL (unexpected result '$res')\n";
$fail++;
}
}
sub agi_output
{
if ($AGILOG >=2)
{
### open the log file for writing ###
open(Lout, ">>$AGILOGfile")
|| die "Can't open $AGILOGfile: $!\n";
print Lout "$now_date|$script|$agi_string\n";
close(Lout);
}
### send to STDERR writing ###
if ( ($AGILOG == '1') || ($AGILOG == '3') )
{print STDERR "$now_date|$script|$agi_string\n";}
$agi_string='';
}
+159
View File
@@ -0,0 +1,159 @@
#!/usr/bin/perl
#
# agi-dtmf.agi - script for playing DTMF tones or signals received as the callerid field or in the command CLI
#
# DTMF sound files must be in /var/lib/asterisk/sounds for this to work
#
# Accepted values are: 1 2 3 4 5 6 7 8 9 0 # * ,
# comma means pause for 1 second (use 'S' for pause in CLI mode)
#
# exten => 8500998,1,Answer
# exten => 8500998,2,Playback(silence)
# exten => 8500998,3,AGI(agi-dtmf.agi)
#; exten => 8500998,3,AGI(agi-dtmf.agi,signalonly) ; optional signal only DTMF
# exten => 8500998,4,Hangup
#
# Can also be used in a Call Menu: agi-dtmf.agi,signalonly---12345 # optional signal only DTMF with CLI-defined DTMF values
#
# Copyright (C) 2014 Matt Florell <vicidial@gmail.com> LICENSE: AGPLv2
#
# CHANGES
# 90312-1534 - Added signalonly option
# 140611-1330 - Added CLI dtmf variable option
#
$signalonly=0;
$cli_vars_set=0;
### begin parsing run-time options ###
if (length($ARGV[0])>1)
{
$i=0;
while ($#ARGV >= $i)
{
$args = "$args $ARGV[$i]";
$i++;
}
### list of command-line array arguments:
@ARGV_vars = split(/---/, $ARGV[0]);
$signalonly = $ARGV_vars[0];
$cli_vars = $ARGV_vars[1];
}
if ($signalonly =~ /signalonly/)
{$signalonly=1;}
if (length($cli_vars) > 0)
{$cli_vars_set=1;}
$|=1;
while(<STDIN>)
{
chomp;
last unless length($_);
if (/^agi_(\w+)\:\s+(.*)$/)
{$AGI{$1} = $2;}
if (/^agi_callerid\:\s+(.*)$/) {$caller_id = $1;}
if (/^agi_calleridname\:\s+(.*)$/) {$caller_idname = $1;}
}
print STDERR "AGI Environment Dump:\n";
foreach $i (sort keys %AGI) {
print STDERR " -- $i = $AGI{$i}\n";
}
use Time::HiRes ('gettimeofday','usleep','sleep'); # necessary to have perl sleep command of less than one second
use Asterisk::AGI;
my $agi = new Asterisk::AGI;
my $clid = $agi->get_variable('CALLERID');
my $clidname = $agi->get_variable('CALLERIDNAME');
print STDERR "X. |$clid|$caller_id|$clidname| |$signalonly|$cli_vars_set|$cli_vars|\n";
### sleep for 5 tenths of a second
usleep(1*500*1000);
print "STREAM FILE silence \"\"\n";
$caller_id = "$caller_id$caller_idname";
if ($cli_vars_set > 0)
{$caller_id = $cli_vars;}
@CALLERID_DIGITS = split(//, $caller_id);
foreach (@CALLERID_DIGITS)
{
if ($CALLERID_DIGITS[$g] =~ /\d/)
{
if ($signalonly > 0)
{print "EXEC SendDTMF $CALLERID_DIGITS[$g] \"\"\n";}
else
{print "STREAM FILE $CALLERID_DIGITS[$g] \"\"\n";}
print STDERR "DIGIT: $CALLERID_DIGITS[$g]\n";
}
else
{
if ($CALLERID_DIGITS[$g] =~ /\#/)
{
if ($signalonly > 0)
{print "EXEC SendDTMF \# \"\"\n";}
else
{print "STREAM FILE hash \"\"\n";}
}
if ($CALLERID_DIGITS[$g] =~ /\*/)
{
if ($signalonly > 0)
{print "EXEC SendDTMF \* \"\"\n";}
else
{print "STREAM FILE star \"\"\n";}
}
if ($CALLERID_DIGITS[$g] =~ /\,|S/)
{sleep(1);}
print STDERR "NON-DIGIT: $CALLERID_DIGITS[$g]\n";
}
$g++;
### sleep for 20 hundredths of a second
usleep(1*150*1000);
print "STREAM FILE silence \"\"\n";
### sleep for 10 hundredths of a second
usleep(1*100*1000);
}
print "STREAM FILE silence \"\"\n";
$result = <STDIN>;
checkresult($result);
exit;
sub checkresult
{
my ($res) = @_;
my $retval;
$tests++;
chomp $res;
if ($res =~ /^200/)
{
$res =~ /result=(-?\d+)/;
if (!length($1))
{
print STDERR "FAIL ($res)\n";
$fail++;
}
else
{
print STDERR "PASS ($1)\n";
$pass++;
}
}
else
{
print STDERR "FAIL (unexpected result '$res')\n";
$fail++;
}
}
@@ -0,0 +1,544 @@
#!/usr/bin/perl
#
# agi-phone_monitor.agi version 2.8
#
# This script is designed to allow managers to listen to agent sessions by
# dialing a phone exten number or user ID number. For this to work, the user ID
# or phone extension must be only digits. If you are using the phone extension,
# then all phone extensions on the system must be unique.
#
# user is prompted for a user ID or phone login then press #
# then the user is forwarded to monitor the active vicidial agent session
#
# ; settings for this script:
# ; 1. USER, PHONE or PLOGIN, lookup by user id, phone extension or phone login (default is USER)
# ; 2. Y or N, log in api log (default is N)
# ; 3. BARGE or LISTEN, (default is LISTEN)
# ; 4. Prepopulated ID, for use in context by itself
#
# can be added as an AGI to a Call Menu, or just a direct extension transfer:
#
# ; call menu AGI with options:
# agi-phone_monitor.agi,USER---N---LISTEN---
#
# ; or as extension transfer to "up_monitor_exit" context and "s" extension
#
# Copyright (C) 2013 Matt Florell <vicidial@gmail.com> LICENSE: AGPLv2
#
# CHANGELOG
# 130401-0724 - First build
# 130520-1013 - Added PLOGIN lookup option and no-prompt extension feature
# 130710-1658 - Small change to allow non-hangup between agent calls
# 130925-1824 - Added variable filter to prevent DID SQL injection attack
#
&get_time_now;
$start_epoch = $now_date_epoch;
$script = 'agi-phone_monitor.agi';
$now_date_epoch = time();
$now_date = "$year-$mon-$mday $hour:$min:$sec";
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year = ($year + 1900);
$mon++;
if ($mon < 10) {$mon = "0$mon";}
if ($mday < 10) {$mday = "0$mday";}
if ($hour < 10) {$hour = "0$hour";}
if ($min < 10) {$min = "0$min";}
if ($sec < 10) {$sec = "0$sec";}
$US='_';
# default path to astguiclient configuration file:
$PATHconf = '/etc/astguiclient.conf';
open(conf, "$PATHconf") || die "can't open $PATHconf: $!\n";
@conf = <conf>;
close(conf);
$i=0;
foreach(@conf)
{
$line = $conf[$i];
$line =~ s/ |>|\n|\r|\t|\#.*|;.*//gi;
if ( ($line =~ /^PATHlogs/) && ($CLIlogs < 1) )
{$PATHlogs = $line; $PATHlogs =~ s/.*=//gi;}
if ( ($line =~ /^PATHsounds/) && ($CLIsounds < 1) )
{$PATHsounds = $line; $PATHsounds =~ s/.*=//gi;}
if ( ($line =~ /^PATHmonitor/) && ($CLImonitor < 1) )
{$PATHmonitor = $line; $PATHmonitor =~ s/.*=//gi;}
if ( ($line =~ /^VARserver_ip/) && ($CLIserver_ip < 1) )
{$VARserver_ip = $line; $VARserver_ip =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_server/) && ($CLIDB_server < 1) )
{$VARDB_server = $line; $VARDB_server =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_database/) && ($CLIDB_database < 1) )
{$VARDB_database = $line; $VARDB_database =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_user/) && ($CLIDB_user < 1) )
{$VARDB_user = $line; $VARDB_user =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_pass/) && ($CLIDB_pass < 1) )
{$VARDB_pass = $line; $VARDB_pass =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_port/) && ($CLIDB_port < 1) )
{$VARDB_port = $line; $VARDB_port =~ s/.*=//gi;}
$i++;
}
if (!$VARDB_port) {$VARDB_port='3306';}
if (!$AGILOGfile) {$AGILOGfile = "$PATHlogs/phone_monitor.$year-$mon-$mday";}
use DBI;
use Asterisk::AGI;
$AGI = new Asterisk::AGI;
$dbhA = DBI->connect("DBI:mysql:$VARDB_database:$VARDB_server:$VARDB_port", "$VARDB_user", "$VARDB_pass")
or die "Couldn't connect to database: " . DBI->errstr;
### BEGIN parsing run-time options ###
if (length($ARGV[0])>1)
{
if ($AGILOG) {$agi_string = "Perl Environment Dump:"; &agi_output;}
$i=0;
while ($#ARGV >= $i)
{
$args = "$args $ARGV[$i]";
if ($AGILOG) {$agi_string = "$i|$ARGV[$i]"; &agi_output;}
$i++;
}
### list of command-line array arguments:
@ARGV_vars = split(/---/, $ARGV[0]);
$user_phone = $ARGV_vars[0];
$api_log = $ARGV_vars[1];
$barge_listen = $ARGV_vars[2];
$prepop_id = $ARGV_vars[3];
}
if ( (length($user_phone) < 1) || ($user_phone !~ /USER|PHONE|PLOGIN/) )
{$user_phone = 'USER';}
if ( (length($api_log) < 1) || ($api_log !~ /Y|N/) )
{$api_log = 'N';}
if ( (length($barge_listen) < 1) || ($barge_listen !~ /BARGE|LISTEN/) )
{$barge_listen = 'LISTEN';}
### END parsing run-time options ###
$|=1;
while(<STDIN>)
{
chomp;
last unless length($_);
if ($V)
{
if (/^agi_(\w+)\:\s+(.*)$/)
{
$AGI{$1} = $2;
}
}
if (/^agi_context\:\s+(.*)$/) {$context = $1;}
if (/^agi_uniqueid\:\s+(.*)$/) {$unique_id = $1;}
if (/^agi_extension\:\s+(.*)$/) {$extension = $1;}
if (/^agi_channel\:\s+(.*)$/) {$channel = $1;}
if (/^agi_callerid\:\s+(.*)$/) {$callerid = $1;}
if (/^agi_calleridname\:\s+(.*)$/) {$calleridname = $1;}
}
$callerid =~ s/\'|\\\\|\\\|\\|\\;|\\\;|\;|;//gi;
$calleridname =~ s/\'|\\\\|\\\|\\|\\;|\\\;|\;|;//gi;
$extension =~ s/\'|\"|\\\\|\\\|\\|\\;|\\\;|\;|;//gi;
if ( ($callerid =~ /\".*\"/) && ( (!$calleridname) or ($calleridname =~ /unknown/) ) )
{
$calleridname = $callerid;
$calleridname =~ s/\<\d\d\d\d\d\d\d\d\d\d\>//gi;
$calleridname =~ s/\"|\" //gi;
}
$callerid =~ s/\D//gi;
$calleridname =~ s/unknown//gi;
if ( (!$callerid) or ($callerid =~ /unknown/) )
{$callerid = $calleridname;}
$phone_number=$callerid;
### Grab Server values from the database
$stmtA = "SELECT ext_context,agi_output FROM servers where server_ip = '$VARserver_ip';";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
$rec_count=0;
while ($sthArows > $rec_count)
{
@aryA = $sthA->fetchrow_array;
$DBext_context = $aryA[0];
$DBagi_output = $aryA[1];
if ($DBext_context) {$ext_context = $DBext_context;}
if ($DBagi_output =~ /STDERR/) {$AGILOG = '1';}
if ($DBagi_output =~ /FILE/) {$AGILOG = '2';}
if ($DBagi_output =~ /BOTH/) {$AGILOG = '3';}
$rec_count++;
}
$sthA->finish();
$AGI->stream_file('sip-silence');
$AGI->stream_file('sip-silence');
$entry_chances=0;
$VALID_login=0;
$ACTIVE_login=0;
$VDADremDIALstr='';
while ( ($entry_chances < 3) && ($VALID_login < 1) )
{
$entry_chances++;
if (length($prepop_id)<2)
{
&user_phone_gather;
}
else
{$PIN_number = $prepop_id;}
###### Check userID and password against vicidial_users table
$stmtA = "SELECT count(*) FROM vicidial_users where user='$PIN_number' and active='Y';";
if ($user_phone =~ /PHONE/)
{$stmtA = "SELECT count(*) FROM phones where extension='$PIN_number' and active='Y';";}
if ($user_phone =~ /PLOGIN/)
{$stmtA = "SELECT count(*) FROM phones where login='$PIN_number' and active='Y';";}
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
if ($sthArows > 0)
{
@aryA = $sthA->fetchrow_array;
$VALID_login = $aryA[0];
}
$sthA->finish();
if ($VALID_login > 0)
{
$AGI->stream_file('pin-number-accepted');
}
else
{
if ($api_log =~ /Y/)
{
$stmtA="INSERT INTO vicidial_api_log set user='$calleridname',agent_user='$PIN_number',function='phone_monitor',value='',result='ERROR',result_reason='No user found',source='monitor',data='$calleridname|$callerid',api_date='$now_date',api_script='$script',run_time='0';";
$affected_rows = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "API LOG INSERT $affected_rows|$stmtA|"; &agi_output;}
}
$AGI->stream_file('pin-invalid');
}
}
if ($VALID_login < 1)
{
if ($AGILOG) {$agi_string = "PHONE MONITOR END: Invalid Login |$VALID_login|$PIN_number|"; &agi_output;}
if ($use_call_menu =~ /Y/)
{
if ($AGILOG) {$agi_string = "exiting the agent dial-in app, transferring call to B @ $context"; &agi_output;}
print "SET CONTEXT $context\n";
$result = <STDIN>;
checkresult($result);
print "SET EXTENSION B\n";
$result = <STDIN>;
checkresult($result);
print "SET PRIORITY 1\n";
$result = <STDIN>;
checkresult($result);
}
else
{
$AGI->stream_file('vm-goodbye');
$AGI->hangup();
$dbhA->disconnect();
}
exit;
}
else
{
$secX = time();
$PDtarget = ($secX - 30);
($Psec,$Pmin,$Phour,$Pmday,$Pmon,$Pyear,$Pwday,$Pyday,$Pisdst) = localtime($PDtarget);
$Pyear = ($Pyear + 1900);
$Pmon++;
if ($Pmon < 10) {$Pmon = "0$Pmon";}
if ($Pmday < 10) {$Pmday = "0$Pmday";}
if ($Phour < 10) {$Phour = "0$Phour";}
if ($Pmin < 10) {$Pmin = "0$Pmin";}
if ($Psec < 10) {$Psec = "0$Psec";}
$PDtsSQLdate = "$Pyear$Pmon$Pmday$Phour$Pmin$Psec";
###### Check if user is logged in, vicidial_live_agents table
$stmtA = "SELECT count(*) FROM vicidial_live_agents where user='$PIN_number' and last_update_time > \"$PDtsSQLdate\";";
if ($user_phone =~ /PHONE/)
{$stmtA = "SELECT count(*) FROM vicidial_live_agents where extension LIKE \"%/$PIN_number\" and last_update_time > \"$PDtsSQLdate\";";}
if ($user_phone =~ /PLOGIN/)
{
$PLOGIN_extension='XXXXXXXXXXXXXXXXXX';
$PLOGIN_server_ip='1.1.1.1';
$stmtA = "SELECT extension,server_ip FROM phones where login='$PIN_number' and active='Y';";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
if ($sthArows > 0)
{
@aryA = $sthA->fetchrow_array;
$PLOGIN_extension = $aryA[0];
$PLOGIN_server_ip = $aryA[1];
}
$sthA->finish();
if ($AGILOG) {$agi_string = "PLOGIN DEBUG: |$PLOGIN_extension|$PLOGIN_server_ip|$stmtA|"; &agi_output;}
$stmtA = "SELECT count(*) FROM vicidial_live_agents where extension LIKE \"%/$PLOGIN_extension\" and server_ip='$PLOGIN_server_ip' and last_update_time > \"$PDtsSQLdate\";";
}
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
if ($sthArows > 0)
{
@aryA = $sthA->fetchrow_array;
$ACTIVE_login = "$aryA[0]";
}
$sthA->finish();
if ($ACTIVE_login < 1)
{
if ($AGILOG) {$agi_string = "PHONE MONIOR END: Inactive Login |$VALID_login|$PIN_number|$PASS_word| |$ACTIVE_login|$PDtsSQLdate|$stmtA|"; &agi_output;}
if ($api_log =~ /Y/)
{
$stmtA="INSERT INTO vicidial_api_log set user='$calleridname',agent_user='$PIN_number',function='phone_monitor',value='',result='ERROR',result_reason='Inactive Login',source='monitor',data='$calleridname|$callerid',api_date='$now_date',api_script='$script',run_time='0';";
$affected_rows = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "API LOG INSERT $affected_rows|$stmtA|"; &agi_output;}
}
$AGI->stream_file('login-fail');
$AGI->stream_file('vm-goodbye');
$AGI->hangup();
$dbhA->disconnect();
exit;
}
else
{
$stmtA = "SELECT conf_exten,extension,server_ip from vicidial_live_agents WHERE user='$PIN_number' and last_update_time > \"$PDtsSQLdate\" limit 1;";
if ($user_phone =~ /PHONE/)
{$stmtA = "SELECT conf_exten,extension,server_ip FROM vicidial_live_agents where extension LIKE \"%/$PIN_number\" and last_update_time > \"$PDtsSQLdate\" limit 1;";}
if ($user_phone =~ /PLOGIN/)
{
$PLOGIN_extension='XXXXXXXXXXXXXXXXXX';
$PLOGIN_server_ip='1.1.1.1';
$stmtA = "SELECT extension,server_ip FROM phones where login='$PIN_number' and active='Y';";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
if ($sthArows > 0)
{
@aryA = $sthA->fetchrow_array;
$PLOGIN_extension = $aryA[0];
$PLOGIN_server_ip = $aryA[1];
}
$sthA->finish();
$stmtA = "SELECT conf_exten,extension,server_ip FROM vicidial_live_agents where extension LIKE \"%/$PLOGIN_extension\" and server_ip='$PLOGIN_server_ip' and last_update_time > \"$PDtsSQLdate\" limit 1;";
}
if ($AGILOG) {$agi_string = "|$stmtA|"; &agi_output;}
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
$rec_countCUSTDATA=0;
if ($sthArows > 0)
{
@aryA = $sthA->fetchrow_array;
$VDADconf_exten = $aryA[0];
$VDADextension = $aryA[1];
$VDADserver_ip = $aryA[2];
}
$sthA->finish();
### format the remote server dialstring to get the call to the overflow agent meetme room
$S='*';
if( $VDADserver_ip =~ m/(\S+)\.(\S+)\.(\S+)\.(\S+)/ )
{
$a = leading_zero($1);
$b = leading_zero($2);
$c = leading_zero($3);
$d = leading_zero($4);
# use double-star prefix to go through native bridge IAX path
$VDADremDIALstr = "$S$S$a$S$b$S$c$S$d$S";
}
$barge_listen_prefix = '58';
if($barge_listen =~ /BARGE/)
{$barge_listen_prefix = '59';}
$VDADremDIALstr .= "$barge_listen_prefix$VDADconf_exten";
if ($api_log =~ /Y/)
{
$stmtA="INSERT INTO vicidial_api_log set user='$calleridname',agent_user='$PIN_number',function='phone_monitor',value='',result='SUCCESS',result_reason='',source='monitor',data='$VDADconf_exten|$VDADextension|$VDADserver_ip|$calleridname|$callerid',api_date='$now_date',api_script='$script',run_time='0';";
$affected_rows = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "API LOG INSERT $affected_rows|$stmtA|"; &agi_output;}
}
if ($AGILOG) {$agi_string = "Agent $PIN_number sent to $VDADremDIALstr"; &agi_output;}
print "SET CONTEXT $ext_context\n";
$result = <STDIN>;
checkresult($result);
print "SET EXTENSION $VDADremDIALstr\n";
$result = <STDIN>;
checkresult($result);
print "SET PRIORITY 1\n";
$result = <STDIN>;
checkresult($result);
}
}
$dbhA->disconnect();
exit;
################################################################################
##### SUBROUTINES ##############################################################
################################################################################
##### BEGIN collect the user and password ######################################
sub user_phone_gather
{
################################################################################
# please enter the pin number followed by the pound key
$interrupt_digit='';
$interrupt_digit = $AGI->stream_file('ld_welcome_pin_number','0123456789');
print STDERR "interrupt_digit |$interrupt_digit|\n";
$digits_being_entered=1;
$PIN_number='';
if ($interrupt_digit > 0)
{
# if ($interrupt_digit == 35) {$interrupt_digit='#';}
# if ($interrupt_digit == 42) {$interrupt_digit='*';}
if ($interrupt_digit == 48) {$interrupt_digit=0;}
if ($interrupt_digit == 49) {$interrupt_digit=1;}
if ($interrupt_digit == 50) {$interrupt_digit=2;}
if ($interrupt_digit == 51) {$interrupt_digit=3;}
if ($interrupt_digit == 52) {$interrupt_digit=4;}
if ($interrupt_digit == 53) {$interrupt_digit=5;}
if ($interrupt_digit == 54) {$interrupt_digit=6;}
if ($interrupt_digit == 55) {$interrupt_digit=7;}
if ($interrupt_digit == 56) {$interrupt_digit=8;}
if ($interrupt_digit == 57) {$interrupt_digit=9;}
$PIN_number=$interrupt_digit;
}
$digit_loop_counter=0;
while ( ($digits_being_entered > 0) && ($digit_loop_counter < 20) )
{
$digit = chr($AGI->wait_for_digit('10000')); # wait 10 seconds for input or until the pound key is pressed
if ($digit =~ /\d/)
{
$PIN_number = "$PIN_number$digit";
print STDERR "digit |$digit| PIN_number |$PIN_number|\n";
# $AGI->say_digits("$digit");
undef $digit;
}
else
{
$digits_being_entered=0;
}
$digit_loop_counter++;
}
$PIN_number =~ s/\D//gi;
if ($PIN_number) {print STDERR "digit |$digit| PIN_number |$PIN_number|\n";}
}
##### END collect the user and password ###############################################
sub get_time_now #get the current date and time and epoch for logging call lengths and datetimes
{
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year = ($year + 1900);
$mon++;
if ($mon < 10) {$mon = "0$mon";}
if ($mday < 10) {$mday = "0$mday";}
if ($hour < 10) {$hour = "0$hour";}
if ($min < 10) {$min = "0$min";}
if ($sec < 10) {$sec = "0$sec";}
$now_date_epoch = time();
$now_date = "$year-$mon-$mday $hour:$min:$sec";
$filedate = "$year$mon$mday$hour$min$sec";
}
sub checkresult
{
my ($res) = @_;
my $retval;
$tests++;
chomp $res;
if ($res =~ /^200/) {
$res =~ /result=(-?\d+)/;
if (!length($1))
{
print STDERR "FAIL ($res)\n";
$fail++;
}
else
{
print STDERR "PASS ($1)\n";
$pass++;
}
}
else
{
print STDERR "FAIL (unexpected result '$res')\n";
$fail++;
}
}
sub agi_output
{
if ($AGILOG >=2)
{
### open the log file for writing ###
open(Lout, ">>$AGILOGfile")
|| die "Can't open $AGILOGfile: $!\n";
print Lout "$now_date|$script|$agi_string\n";
close(Lout);
}
### send to STDERR writing ###
if ( ($AGILOG == '1') || ($AGILOG == '3') )
{print STDERR "$now_date|$script|$agi_string\n";}
$agi_string='';
}
sub leading_zero($)
{
$_ = $_[0];
s/^(\d)$/0$1/;
s/^(\d\d)$/0$1/;
return $_;
} # End of the leading_zero() routine.
@@ -0,0 +1,419 @@
#!/usr/bin/perl
#
# agi-record_prompts.agi version 2.6
#
# for recording prompts to GSM file over the phone
# Saves recordings with 8-digit filenames to be played when exten is dialed
#
# ; prompts for recording AGI script, ID is 4321
# ; first variable is format (gsm/wav)
# ; second variable is timeout in milliseconds (default is 360000 [6 minutes])
# exten => 8167,1,Answer
# exten => 8167,2,AGI(agi-record_prompts.agi,wav-----360000)
# exten => 8167,3,Hangup
# exten => 8168,1,Answer
# exten => 8168,2,AGI(agi-record_prompts.agi,gsm-----360000)
# exten => 8168,3,Hangup
#
# ; playback of recorded prompts
# exten => _851XXXXX,1,Answer
# exten => _851XXXXX,2,Playback(${EXTEN})
# exten => _851XXXXX,3,Hangup
#
# GSM Sound Files used:
# - ld_welcome_pin_number
# - ld_invalid_pin_number
# - vm-rec-generic
# - auth-thankyou
# - vm-review
# - vm-msgsaved
# - vm-goodbye
#
# Copyright (C) 2013 Matt Florell <vicidial@gmail.com> LICENSE: AGPLv2
#
# CHANGELOG
# 71226-1303 - Added option to change format GSM/WAV and timeout through flags
# 90519-1019 - Changed formatting to conform to other scripts
# 130108-1815 - Changes for Asterisk 1.8 compatibility
#
$US='_';
use Asterisk::AGI;
$AGI = new Asterisk::AGI;
### begin parsing run-time options ###
if (length($ARGV[0])>1)
{
$i=0;
while ($#ARGV >= $i)
{
$args = "$args $ARGV[$i]";
$i++;
}
### list of command-line array arguments:
@ARGV_vars = split(/-----/, $ARGV[0]);
$format = $ARGV_vars[0];
$timeout = $ARGV_vars[1];
}
$|=1;
while(<STDIN>)
{
chomp;
last unless length($_);
if ($V)
{
if (/^agi_(\w+)\:\s+(.*)$/)
{
$AGI{$1} = $2;
}
}
if (/^agi_uniqueid\:\s+(.*)$/) {$unique_id = $1;}
}
if ($format =~ /WAV/i) {$format = 'wav';}
else {$format = 'gsm';}
if ($timeout < 1) {$timeout = 360000;} # default is 6 minutes
$unique_id_out = $unique_id;
@NEW_ID = split(/\./, $unique_id_out);
$NEW_ID_DIF = '.0';
$unique_id_out = "$NEW_ID[0]$NEW_ID_DIF$NEW_ID[1]";
$pin='';
$stop_loop=0;
$quit_program=0;
$loop_counter=0;
$REC_id = 85100000;
$REC_next = 0;
if (-e "/prompt_count.txt")
{
open(test, "/prompt_count.txt") || die "can't open /prompt_count.txt: $!\n";
@test = <test>;
close(test);
$REC_next = ($test[0] + 1);
$REC_id = ($REC_id + $REC_next);
open(test, ">/prompt_count.txt") || die "can't open /prompt_count.txt: $!\n";
print test "$REC_next";
close(test);
}
else
{
open(test, ">/prompt_count.txt") || die "can't open /prompt_count.txt: $!\n";
print test "1";
close(test);
$REC_id = ($REC_id + 1);
}
$session_recording = "$REC_id";
print STDERR "Recording ID: $REC_id\n";
&welcome_1;
print STDERR "DONE Exiting...\n";
exit;
##### SUBROUTINES ########################################################
##### steps ########################################################
sub welcome_1
{
##### Play welcome message and capture PIN ########################################################
while ( (length($pin) ne 4) && (!$stop_loop) )
{
&enter_pin_number;
&log_transfer;
$loop_counter++;
if ($loop_counter > 3) {$stop_loop++; $quit_program++;}
print STDERR "\nPIN|$pin|\n";
}
##### Check PIN account in database ########################################################
$rec_count=0;
&lookup_account;
if (!$rec_count)
{
# please enter the pin number followed by the pound key
$AGI->stream_file('ld_invalid_pin_number');
$quit_program++;
}
##### quit program if error ########################################################
if ($quit_program)
{
print STDERR "\nexiting the ping app\n";
print "SET CONTEXT demo\n";
$result = <STDIN>;
checkresult($result);
print "SET EXTENSION 8158\n";
$result = <STDIN>;
checkresult($result);
print "SET PRIORITY 3\n";
$result = <STDIN>;
checkresult($result);
exit;
}
&record_prompts;
}
sub record_prompts
{
$AGI->stream_file('beep');
print STDERR "\nrecord_prompts AGI welcome\n";
$AGI->stream_file('vm-rec-generic');
$AGI->stream_file('beep');
$digit='';
$interrupt_digit='';
$interrupt_digit = $AGI->record_file("$session_recording", "$format", '123456789*#', "$timeout", 1);
print STDERR "interrupt_digit |$interrupt_digit|\n";
$digits_being_entered=1;
$digit_loop_counter=0;
$totalDTMF='';
if ($interrupt_digit > 1)
{
if ($interrupt_digit == 35) {$interrupt_digit='#';}
if ($interrupt_digit == 42) {$interrupt_digit='*';}
if ($interrupt_digit == 48) {$interrupt_digit=0;}
if ($interrupt_digit == 49) {$interrupt_digit=1;}
if ($interrupt_digit == 50) {$interrupt_digit=2;}
if ($interrupt_digit == 51) {$interrupt_digit=3;}
if ($interrupt_digit == 52) {$interrupt_digit=4;}
if ($interrupt_digit == 53) {$interrupt_digit=5;}
if ($interrupt_digit == 54) {$interrupt_digit=6;}
if ($interrupt_digit == 55) {$interrupt_digit=7;}
if ($interrupt_digit == 56) {$interrupt_digit=8;}
if ($interrupt_digit == 57) {$interrupt_digit=9;}
$totalDTMF=$interrupt_digit;
$digit_loop_counter++;
}
$AGI->stream_file('beep');
if (length($totalDTMF) > 0) {print STDERR "digit |$digit| TotalDTMF |$totalDTMF|\n";}
&verify_recording;
}
sub verify_recording
{
$digit='';
$interrupt_digit='';
$interrupt_digit = $AGI->stream_file('vm-review','123456789');
print STDERR "interrupt_digit |$interrupt_digit|\n";
$digits_being_entered=1;
$digit_loop_counter=0;
$totalDTMF='';
if ($interrupt_digit > 1)
{
if ($interrupt_digit == 35) {$interrupt_digit='#';}
if ($interrupt_digit == 42) {$interrupt_digit='*';}
if ($interrupt_digit == 48) {$interrupt_digit=0;}
if ($interrupt_digit == 49) {$interrupt_digit=1;}
if ($interrupt_digit == 50) {$interrupt_digit=2;}
if ($interrupt_digit == 51) {$interrupt_digit=3;}
if ($interrupt_digit == 52) {$interrupt_digit=4;}
if ($interrupt_digit == 53) {$interrupt_digit=5;}
if ($interrupt_digit == 54) {$interrupt_digit=6;}
if ($interrupt_digit == 55) {$interrupt_digit=7;}
if ($interrupt_digit == 56) {$interrupt_digit=8;}
if ($interrupt_digit == 57) {$interrupt_digit=9;}
$totalDTMF=$interrupt_digit;
$digit_loop_counter++;
}
while ($digit_loop_counter < 1)
{
$digit = chr($AGI->wait_for_digit('2000000')); # wait 0.2 seconds for input or until the pound key is pressed
if ($digit =~ /\d/)
{
$totalDTMF = "$totalDTMF$digit";
print STDERR "digit |$digit| TotalDTMF |$totalDTMF|\n";
# $AGI->say_digits("$digit");
undef $digit;
}
else
{
$digit_loop_counter=1;
}
$digit_loop_counter++;
}
$totalDTMF =~ s/\D//gi;
$pin = $totalDTMF;
if (length($pin)< 1)
{
&verify_recording;
}
else
{
if ($pin == '1')
{
$AGI->stream_file('auth-thankyou');
$AGI->say_digits("$REC_id");
$AGI->stream_file('beep');
$AGI->say_digits("$REC_id");
$AGI->stream_file('vm-goodbye');
exit;
}
if ($pin == '2')
{
$AGI->stream_file("$session_recording");
&verify_recording;
}
if ($pin == '3')
{
&record_prompts;
}
}
}
##### SUBROUTINES PROCESSES ########################################################
sub log_transfer
{
# nothing
}
sub lookup_account
{
if ($pin eq '4321')
{
$rec_count++;
}
}
sub get_time_now #get the current date and time and epoch for logging call lengths and datetimes
{
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year = ($year + 1900);
$mon++;
if ($mon < 10) {$mon = "0$mon";}
if ($mday < 10) {$mday = "0$mday";}
if ($hour < 10) {$hour = "0$hour";}
if ($min < 10) {$min = "0$min";}
if ($sec < 10) {$sec = "0$sec";}
$now_date_epoch = time();
$now_date = "$year-$mon-$mday $hour:$min:$sec";
}
sub enter_pin_number
{
# please enter the pin number followed by the pound key
$interrupt_digit='';
$interrupt_digit = $AGI->stream_file('ld_welcome_pin_number','123456789');
print STDERR "interrupt_digit |$interrupt_digit|\n";
$digits_being_entered=1;
$totalDTMF='';
if ($interrupt_digit > 0)
{
if ($interrupt_digit == 48) {$interrupt_digit=0;}
if ($interrupt_digit == 49) {$interrupt_digit=1;}
if ($interrupt_digit == 50) {$interrupt_digit=2;}
if ($interrupt_digit == 51) {$interrupt_digit=3;}
if ($interrupt_digit == 52) {$interrupt_digit=4;}
if ($interrupt_digit == 53) {$interrupt_digit=5;}
if ($interrupt_digit == 54) {$interrupt_digit=6;}
if ($interrupt_digit == 55) {$interrupt_digit=7;}
if ($interrupt_digit == 56) {$interrupt_digit=8;}
if ($interrupt_digit == 57) {$interrupt_digit=9;}
$totalDTMF=$interrupt_digit;
}
$digit_loop_counter=0;
while ( ($digits_being_entered) && ($digit_loop_counter < 20) )
{
$digit = chr($AGI->wait_for_digit('90000')); # wait 90 seconds for input or until the pound key is pressed
if ($digit =~ /\d/)
{
$totalDTMF = "$totalDTMF$digit";
print STDERR "digit |$digit| TotalDTMF |$totalDTMF|\n";
# $AGI->say_digits("$digit");
undef $digit;
}
else
{
$digits_being_entered=0;
}
$digit_loop_counter++;
}
$totalDTMF =~ s/\D//gi;
$pin = $totalDTMF;
if ($totalDTMF) {print STDERR "digit |$digit| TotalDTMF |$totalDTMF|\n";}
}
sub checkresult
{
my ($res) = @_;
my $retval;
$tests++;
chomp $res;
if ($res =~ /^200/)
{
$res =~ /result=(-?\d+)/;
if (!length($1))
{
print STDERR "FAIL ($res)\n";
$fail++;
}
else
{
print STDERR "PASS ($1)\n";
$pass++;
}
}
else
{
print STDERR "FAIL (unexpected result '$res')\n";
$fail++;
}
}
@@ -0,0 +1,400 @@
#!/usr/bin/perl
#
# agi-set_variables.agi version 2.10
#
# runs after a call_log before call is placed out the trunk and sets all
# vicidial_list fields as variables for the call
#
# You need to put lines similar to those below in your extensions.conf file:
#
# ; VICIDIAL_auto_dialer transfer script AMD with Load Balanced:
# exten => _91NXXNXXXXXX,1,AGI(agi://127.0.0.1:4577/call_log)
# exten => _91NXXNXXXXXX,n,AGI(agi-set_variables.agi,)
# exten => _91NXXNXXXXXX,n,Dial(${TESTSIPTRUNK}/${EXTEN:2},,tTor)
# exten => _91NXXNXXXXXX,n,Hangup
#
# Copyright (C) 2014 Matt Florell <vicidial@gmail.com> LICENSE: AGPLv2
#
# changes:
# 101118-1409 - First build
# 101125-2115 - Added parsing for 3-way calls
# 110404-1604 - Added dial_timeout and cpd_amd_action variables
# 141211-1643 - Added cpd_unknown_action
#
$script = 'agi-set_variables.agi';
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year = ($year + 1900);
$mon++;
if ($mon < 10) {$mon = "0$mon";}
if ($mday < 10) {$mday = "0$mday";}
if ($hour < 10) {$hour = "0$hour";}
if ($min < 10) {$min = "0$min";}
if ($sec < 10) {$sec = "0$sec";}
$now_date_epoch = time();
$now_date = "$year-$mon-$mday $hour:$min:$sec";
# default path to astguiclient configuration file:
$PATHconf = '/etc/astguiclient.conf';
open(conf, "$PATHconf") || die "can't open $PATHconf: $!\n";
@conf = <conf>;
close(conf);
$i=0;
foreach(@conf)
{
$line = $conf[$i];
$line =~ s/ |>|\n|\r|\t|\#.*|;.*//gi;
if ( ($line =~ /^PATHhome/) && ($CLIhome < 1) )
{$PATHhome = $line; $PATHhome =~ s/.*=//gi;}
if ( ($line =~ /^PATHlogs/) && ($CLIlogs < 1) )
{$PATHlogs = $line; $PATHlogs =~ s/.*=//gi;}
if ( ($line =~ /^PATHagi/) && ($CLIagi < 1) )
{$PATHagi = $line; $PATHagi =~ s/.*=//gi;}
if ( ($line =~ /^PATHweb/) && ($CLIweb < 1) )
{$PATHweb = $line; $PATHweb =~ s/.*=//gi;}
if ( ($line =~ /^PATHsounds/) && ($CLIsounds < 1) )
{$PATHsounds = $line; $PATHsounds =~ s/.*=//gi;}
if ( ($line =~ /^PATHmonitor/) && ($CLImonitor < 1) )
{$PATHmonitor = $line; $PATHmonitor =~ s/.*=//gi;}
if ( ($line =~ /^VARserver_ip/) && ($CLIserver_ip < 1) )
{$VARserver_ip = $line; $VARserver_ip =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_server/) && ($CLIDB_server < 1) )
{$VARDB_server = $line; $VARDB_server =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_database/) && ($CLIDB_database < 1) )
{$VARDB_database = $line; $VARDB_database =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_user/) && ($CLIDB_user < 1) )
{$VARDB_user = $line; $VARDB_user =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_pass/) && ($CLIDB_pass < 1) )
{$VARDB_pass = $line; $VARDB_pass =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_port/) && ($CLIDB_port < 1) )
{$VARDB_port = $line; $VARDB_port =~ s/.*=//gi;}
$i++;
}
if (!$VARDB_port) {$VARDB_port='3306';}
if (!$AGILOGfile) {$AGILOGfile = "$PATHlogs/agiout.$year-$mon-$mday";}
use DBI;
use Asterisk::AGI;
$AGI = new Asterisk::AGI;
$dbhA = DBI->connect("DBI:mysql:$VARDB_database:$VARDB_server:$VARDB_port", "$VARDB_user", "$VARDB_pass")
or die "Couldn't connect to database: " . DBI->errstr;
### Grab Server values from the database
$stmtA = "SELECT agi_output FROM servers where server_ip = '$VARserver_ip';";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
if ($sthArows > 0)
{
$AGILOG = '0';
@aryA = $sthA->fetchrow_array;
$DBagi_output = $aryA[0];
if ($DBagi_output =~ /STDERR/) {$AGILOG = '1';}
if ($DBagi_output =~ /FILE/) {$AGILOG = '2';}
if ($DBagi_output =~ /BOTH/) {$AGILOG = '3';}
}
$sthA->finish();
### begin parsing run-time options ###
if (length($ARGV[0])>1)
{
if ($AGILOG)
{$agi_string = "Perl Environment Dump:"; &agi_output;}
$i=0;
while ($#ARGV >= $i)
{
$args = "$args $ARGV[$i]";
if ($AGILOG) {$agi_string = "$i|$ARGV[$i]"; &agi_output;}
$i++;
}
### list of command-line array arguments:
@ARGV_vars = split(/-----/, $ARGV[0]);
$CLI_exten = $ARGV_vars[0];
$force_playback = $ARGV_vars[1];
}
$|=1;
while(<STDIN>)
{
chomp;
last unless length($_);
if ($AGILOG)
{
if (/^agi_(\w+)\:\s+(.*)$/)
{
$AGI{$1} = $2;
}
}
if (/^agi_uniqueid\:\s+(.*)$/) {$unique_id = $1; $uniqueid = $unique_id;}
if (/^agi_priority\:\s+(.*)$/) {$priority = $1;}
if (/^agi_channel\:\s+(.*)$/) {$channel = $1;}
if (/^agi_extension\:\s+(.*)$/) {$extension = $1;}
if (/^agi_type\:\s+(.*)$/) {$type = $1;}
if (/^agi_callerid\:\s+(.*)$/) {$callerid = $1; $calleridnum = $callerid;}
if (/^agi_calleridname\:\s+(.*)$/) {$calleridname = $1;}
}
if ( (length($callerid)>20) && ($callerid =~ /\"\S\S\S\S\S\S\S\S\S\S\S\S\S\S\S\S\S\S/) )
{
$callerid =~ s/^\"//gi;
$callerid =~ s/\".*$//gi;
# ### set the callerid to the ACQS value(calleridname)
# print "SET CALLERID $callerid\n";
# checkresult($result);
# print STDERR "callerID changed: $callerid\n";
}
if ( (
(length($calleridname)>5) && ( (!$callerid) or ($callerid =~ /unknown|private|00000000/i) or ($callerid =~ /5551212/) )
) or ( (length($calleridname)>17) && ($calleridname =~ /\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d/) ) or ( (length($calleridname)>17) && ($calleridname =~ /\d\d\d\d\d\dW\d\d\d\d\d\d\d\d\d\dW$/) ) )
{
$callerid = $calleridname;
# ### set the callerid to the ACQS value(calleridname)
# print "SET CALLERID $callerid\n";
# checkresult($result);
# print STDERR "callerID changed: $callerid\n";
}
if ($AGILOG) {$agi_string = "AGI Environment Dump:"; &agi_output;}
foreach $i (sort keys %AGI)
{
if ($AGILOG) {$agi_string = " -- $i = $AGI{$i}"; &agi_output;}
}
if ($AGILOG) {$agi_string = "AGI Variables: |$unique_id|$channel|$extension|$type|$callerid|"; &agi_output;}
$VDADcampaign='';
$VDADphone='';
$VDADphone_code='';
$auto_dial_timeout='60';
$cpd_amd_action='DISABLED';
$cpd_unknown_action='DISABLED';
$callerid =~ s/\"//gi;
$callerid =~ s/ .*//gi;
$CIDlead_id = $callerid;
if ($CIDlead_id =~ /W\d\d\d\d\d\d\d\d\d\dW$/)
{
@CIDlead_id_array = split(/W/, $CIDlead_id);
$CIDlead_id = $CIDlead_id_array[1];
}
else
{
$CIDlead_id = substr($CIDlead_id, 10, 10);
}
$CIDlead_id = ($CIDlead_id + 0);
if ( ($CLIlead_id > 0) && ($CIDlead_id < 1) ) {$CIDlead_id = $CLIlead_id;}
$VD_lead_id = $CIDlead_id;
if ($AGILOG) {$agi_string = "+++++ VARIABLE SET START : |$CIDlead_id|$now_date|$AST_ver|$priority|$calleridname|"; &agi_output;}
########## FIND vicidial_auto_calls record ##########
$stmtA = "SELECT campaign_id,phone_number FROM vicidial_auto_calls where callerid='$callerid' order by auto_call_id desc limit 1;";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
if ($AGILOG) {$agi_string = "$sthArows|$stmtA|"; &agi_output;}
if ($sthArows > 0)
{
@aryA = $sthA->fetchrow_array;
$campaign_id = $aryA[0];
$dialed_number = $aryA[1];
$sthA->finish();
}
$stmtA = "SELECT dial_timeout,cpd_amd_action,cpd_unknown_action FROM vicidial_campaigns where campaign_id='$campaign_id' limit 1;";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
if ($AGILOG) {$agi_string = "$sthArows|$stmtA|"; &agi_output;}
if ($sthArows > 0)
{
@aryA = $sthA->fetchrow_array;
$auto_dial_timeout = $aryA[0];
# only set if auto-dial call
if ($callerid =~ /^V/)
{
$cpd_amd_action = $aryA[1];
$cpd_unknown_action = $aryA[2];
}
$sthA->finish();
}
$stmtA = "SELECT lead_id,entry_date,modify_date,status,user,vendor_lead_code,source_id,list_id,phone_number,title,first_name,middle_initial,last_name,address1,address2,address3,city,state,province,postal_code,country_code,gender,date_of_birth,alt_phone,email,security_phrase,comments,called_count,last_local_call_time,rank,owner FROM vicidial_list where lead_id='$VD_lead_id';";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
if ($sthArows > 0)
{
@aryA = $sthA->fetchrow_array;
$lead_id = $aryA[0];
$entry_date = $aryA[1];
$modify_date = $aryA[2];
$status = $aryA[3];
$user = $aryA[4];
$vendor_lead_code = $aryA[5];
$source_id = $aryA[6];
$list_id = $aryA[7];
$phone_number = $aryA[8];
$title = $aryA[9];
$first_name = $aryA[10];
$middle_initial = $aryA[11];
$last_name = $aryA[12];
$address1 = $aryA[13];
$address2 = $aryA[14];
$address3 = $aryA[15];
$city = $aryA[16];
$state = $aryA[17];
$province = $aryA[18];
$postal_code = $aryA[19];
$country_code = $aryA[20];
$gender = $aryA[21];
$date_of_birth = $aryA[22];
$alt_phone = $aryA[23];
$email = $aryA[24];
$security_phrase = $aryA[25];
$comments = $aryA[26];
$called_count = $aryA[27];
$last_local_call_time = $aryA[28];
$rank = $aryA[29];
$owner = $aryA[30];
}
$sthA->finish();
$lead_id =~ s/ /+/gi;
$entry_date =~ s/ /+/gi;
$modify_date =~ s/ /+/gi;
$status =~ s/ /+/gi;
$user =~ s/ /+/gi;
$vendor_lead_code =~ s/ /+/gi;
$source_id =~ s/ /+/gi;
$list_id =~ s/ /+/gi;
$phone_number =~ s/ /+/gi;
$title =~ s/ /+/gi;
$first_name =~ s/ /+/gi;
$middle_initial =~ s/ /+/gi;
$last_name =~ s/ /+/gi;
$address1 =~ s/ /+/gi;
$address2 =~ s/ /+/gi;
$address3 =~ s/ /+/gi;
$city =~ s/ /+/gi;
$state =~ s/ /+/gi;
$province =~ s/ /+/gi;
$postal_code =~ s/ /+/gi;
$country_code =~ s/ /+/gi;
$gender =~ s/ /+/gi;
$date_of_birth =~ s/ /+/gi;
$alt_phone =~ s/ /+/gi;
$email =~ s/ /+/gi;
$security_phrase =~ s/ /+/gi;
$comments =~ s/ /+/gi;
$called_count =~ s/ /+/gi;
$last_local_call_time =~ s/ /+/gi;
$rank =~ s/ /+/gi;
$owner =~ s/ /+/gi;
$campaign_id =~ s/ /+/gi;
$dialed_number =~ s/ /+/gi;
$AGI->exec("EXEC Set(_lead_id=$lead_id)");
$AGI->exec("EXEC Set(_entry_date=$entry_date)");
$AGI->exec("EXEC Set(_modify_date=$modify_date)");
$AGI->exec("EXEC Set(_status=$status)");
$AGI->exec("EXEC Set(_user=$user)");
$AGI->exec("EXEC Set(_vendor_lead_code=$vendor_lead_code)");
$AGI->exec("EXEC Set(_source_id=$source_id)");
$AGI->exec("EXEC Set(_list_id=$list_id)");
$AGI->exec("EXEC Set(_phone_number=$phone_number)");
$AGI->exec("EXEC Set(_title=$title)");
$AGI->exec("EXEC Set(_first_name=$first_name)");
$AGI->exec("EXEC Set(_middle_initial=$middle_initial)");
$AGI->exec("EXEC Set(_last_name=$last_name)");
$AGI->exec("EXEC Set(_address1=$address1)");
$AGI->exec("EXEC Set(_address2=$address2)");
$AGI->exec("EXEC Set(_address3=$address3)");
$AGI->exec("EXEC Set(_city=$city)");
$AGI->exec("EXEC Set(_state=$state)");
$AGI->exec("EXEC Set(_province=$province)");
$AGI->exec("EXEC Set(_postal_code=$postal_code)");
$AGI->exec("EXEC Set(_country_code=$country_code)");
$AGI->exec("EXEC Set(_gender=$gender)");
$AGI->exec("EXEC Set(_date_of_birth=$date_of_birth)");
$AGI->exec("EXEC Set(_alt_phone=$alt_phone)");
$AGI->exec("EXEC Set(_email=$email)");
$AGI->exec("EXEC Set(_security_phrase=$security_phrase)");
$AGI->exec("EXEC Set(_comments=$comments)");
$AGI->exec("EXEC Set(_called_count=$called_count)");
$AGI->exec("EXEC Set(_last_local_call_time=$last_local_call_time)");
$AGI->exec("EXEC Set(_rank=$rank)");
$AGI->exec("EXEC Set(_owner=$owner)");
$AGI->exec("EXEC Set(_campaign_id=$campaign_id)");
$AGI->exec("EXEC Set(_dialed_number=$dialed_number)");
$AGI->exec("EXEC Set(_auto_dial_timeout=$auto_dial_timeout)");
$AGI->exec("EXEC Set(_cpd_amd_action=$cpd_amd_action)");
$AGI->exec("EXEC Set(_cpd_unknown_action=$cpd_unknown_action)");
$dbhA->disconnect();
if ($AGILOG) {$agi_string = "+++++ VARIABLE SET DONE EXITING : |$CIDlead_id|$now_date|$calleridname|$phone_number|"; &agi_output;}
exit;
sub checkresult
{
my ($res) = @_;
my $retval;
$tests++;
chomp $res;
if ($res =~ /^200/)
{
$res =~ /result=(-?\d+)/;
if (!length($1))
{
# print STDERR "FAIL ($res)\n";
$fail++;
}
else
{
# print STDERR "PASS ($1)\n";
$pass++;
}
}
else
{
# print STDERR "FAIL (unexpected result '$res')\n";
$fail++;
}
}
sub agi_output
{
if ($AGILOG >=2)
{
### open the log file for writing ###
open(Lout, ">>$AGILOGfile")
|| die "Can't open $AGILOGfile: $!\n";
print Lout "$now_date|$script|$agi_string\n";
close(Lout);
}
### send to STDERR writing ###
if ( ($AGILOG == '1') || ($AGILOG == '3') )
{print STDERR "$now_date|$script|$agi_string\n";}
$agi_string='';
}
@@ -0,0 +1,294 @@
#!/usr/bin/perl
#
# call_inbound.agi version 2.6
#
# runs at the beginning and ending of every inbound call received
#
# You need to put lines similar to those below in your extensions.conf file:
#
# parameters for call_inbound.agi (7 fields separated by five dashes "-----"):
# 1. the extension of the phone to ring as defined in the asterisk.phones table
# 2. the phone number that was called, for the live_inbound/_log entry
# 3. a text description of the number that was called in
# 4-7. optional fields, they are also passed as fields in the GUI to web browser
#
# ;inbound calls:
#exten => _*NXXNXXXXXX*3429,3,AGI(call_inbound.agi,spa2000-----8005553429-----Inbound 800-----x-----y-----z-----w)
#
#
#
# Copyright (C) 2013 Matt Florell <vicidial@gmail.com> LICENSE: AGPLv2
#
# changes:
# 60731-1435 - changed to use DBI-DBD::mysql
# - changed to use /etc/astguiclient.conf for configs
# 60817-1102 - added output options check from database
# 130108-1838 - Changes for Asterisk 1.8 compatibility
#
$script = 'call_inbound.agi';
$US='_';
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year = ($year + 1900);
$Y = substr($year, 0, 1);
$mon++;
if ($mon < 10) {$mon = "0$mon";}
if ($mday < 10) {$mday = "0$mday";}
if ($hour < 10) {$hour = "0$hour";}
if ($min < 10) {$min = "0$min";}
if ($sec < 10) {$sec = "0$sec";}
$now_date_epoch = time();
$now_date = "$year-$mon-$mday $hour:$min:$sec";
$filedate = "$US$Y$mon$mday$hour$min$sec$US";
# default path to astguiclient configuration file:
$PATHconf = '/etc/astguiclient.conf';
open(conf, "$PATHconf") || die "can't open $PATHconf: $!\n";
@conf = <conf>;
close(conf);
$i=0;
foreach(@conf)
{
$line = $conf[$i];
$line =~ s/ |>|\n|\r|\t|\#.*|;.*//gi;
if ( ($line =~ /^PATHhome/) && ($CLIhome < 1) )
{$PATHhome = $line; $PATHhome =~ s/.*=//gi;}
if ( ($line =~ /^PATHlogs/) && ($CLIlogs < 1) )
{$PATHlogs = $line; $PATHlogs =~ s/.*=//gi;}
if ( ($line =~ /^PATHagi/) && ($CLIagi < 1) )
{$PATHagi = $line; $PATHagi =~ s/.*=//gi;}
if ( ($line =~ /^PATHweb/) && ($CLIweb < 1) )
{$PATHweb = $line; $PATHweb =~ s/.*=//gi;}
if ( ($line =~ /^PATHsounds/) && ($CLIsounds < 1) )
{$PATHsounds = $line; $PATHsounds =~ s/.*=//gi;}
if ( ($line =~ /^PATHmonitor/) && ($CLImonitor < 1) )
{$PATHmonitor = $line; $PATHmonitor =~ s/.*=//gi;}
if ( ($line =~ /^VARserver_ip/) && ($CLIserver_ip < 1) )
{$VARserver_ip = $line; $VARserver_ip =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_server/) && ($CLIDB_server < 1) )
{$VARDB_server = $line; $VARDB_server =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_database/) && ($CLIDB_database < 1) )
{$VARDB_database = $line; $VARDB_database =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_user/) && ($CLIDB_user < 1) )
{$VARDB_user = $line; $VARDB_user =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_pass/) && ($CLIDB_pass < 1) )
{$VARDB_pass = $line; $VARDB_pass =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_port/) && ($CLIDB_port < 1) )
{$VARDB_port = $line; $VARDB_port =~ s/.*=//gi;}
$i++;
}
if (!$VARDB_port) {$VARDB_port='3306';}
if (!$AGILOGfile) {$AGILOGfile = "$PATHlogs/agiout.$year-$mon-$mday";}
use DBI;
$dbhA = DBI->connect("DBI:mysql:$VARDB_database:$VARDB_server:$VARDB_port", "$VARDB_user", "$VARDB_pass")
or die "Couldn't connect to database: " . DBI->errstr;
### Grab Server values from the database
$stmtA = "SELECT agi_output FROM servers where server_ip = '$VARserver_ip';";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
$rec_count=0;
while ($sthArows > $rec_count)
{
$AGILOG = '0';
@aryA = $sthA->fetchrow_array;
$DBagi_output = "$aryA[0]";
if ($DBagi_output =~ /STDERR/) {$AGILOG = '1';}
if ($DBagi_output =~ /FILE/) {$AGILOG = '2';}
if ($DBagi_output =~ /BOTH/) {$AGILOG = '3';}
$rec_count++;
}
$sthA->finish();
### begin parsing run-time options ###
if (length($ARGV[0])>1)
{
if ($AGILOG) {$agi_string = "Perl Environment Dump:"; &agi_output;}
$i=0;
while ($#ARGV >= $i)
{
$args = "$args $ARGV[$i]";
if ($AGILOG) {$agi_string = "$i|$ARGV[$i]"; &agi_output;}
$i++;
}
### list of command-line array arguments:
@ARGV_vars = split(/-----/, $ARGV[0]);
$phone_ext = $ARGV_vars[0];
$inbound_number = $ARGV_vars[1];
$comment_a = $ARGV_vars[2];
$comment_b = $ARGV_vars[3];
$comment_c = $ARGV_vars[4];
$comment_d = $ARGV_vars[5];
$comment_e = $ARGV_vars[6];
if ($args =~ /--help/i)
{
print "allowed run time options:\n [-q] = quiet\n [-t] = test\n [-debug] = verbose debug messages\n\n";
}
else
{
if ($args =~ /-V/i)
{
$V=1;
}
if ($args =~ /-debug/i)
{
$DG=1;
}
if ($args =~ /-dbAVS/i)
{
$DGA=1;
}
if ($args =~ /-q/i)
{
$q=1;
$Q=1;
}
if ($args =~ /-t/i)
{
$TEST=1;
$T=1;
}
}
}
$|=1;
while(<STDIN>) {
chomp;
last unless length($_);
if ($AGILOG)
{
if (/^agi_(\w+)\:\s+(.*)$/)
{
$AGI{$1} = $2;
}
}
if (/^agi_uniqueid\:\s+(.*)$/) {$unique_id = $1;}
if (/^agi_channel\:\s+(.*)$/) {$channel = $1;}
if (/^agi_extension\:\s+(.*)$/) {$extension = $1;}
if (/^agi_type\:\s+(.*)$/) {$type = $1;}
if (/^agi_callerid\:\s+(.*)$/) {$callerid = $1;}
if (/^agi_calleridname\:\s+(.*)$/) {$calleridname = $1;}
if ( $calleridname =~ /\"/) {$calleridname =~ s/\"//gi;}
if ( (length($calleridname)>5) && ( (!$callerid) or ($callerid =~ /unknown|private|00000000/i) or ($callerid =~ /5551212/) ) )
{$callerid = $calleridname;}
### allow for ANI being sent with the DNIS "*3125551212*9999*"
if ($extension =~ /^\*\d\d\d\d\d\d\d\d\d\d\*/)
{
$callerid = $extension;
$callerid =~ s/\*\d\d\d\d\*$//gi;
$callerid =~ s/\*\d\d\d\d$//gi;
$callerid =~ s/^\*//gi;
$extension =~ s/^\*\d\d\d\d\d\d\d\d\d\d\*//gi;
$extension =~ s/\*$//gi;
### set the callerid if sent with DNIS
print "SET CALLERID $callerid\n";
$result = <STDIN>;
checkresult($result);
}
}
if ($AGILOG) {$agi_string = "AGI Environment Dump:"; &agi_output;}
foreach $i (sort keys %AGI)
{
if ($AGILOG) {$agi_string = " -- $i = $AGI{$i}"; &agi_output;}
}
if ($AGILOG) {$agi_string = "AGI Variables: |$unique_id|$channel|$extension|$type|$callerid|"; &agi_output;}
$callerid = "\"$calleridname\" <$callerid>";
if ($extension =~ /h/i) {$stage = 'END';}
else {$stage = 'START';}
### call start stage
if ($stage =~ /START/)
{
if ($AGILOG) {$agi_string = "+++++ CALL INBOUND START : |$now_date|"; &agi_output;}
# if ($channel =~ /^SIP/) {$channel =~ s/-.*//gi;}
# if ($channel =~ /^Zap\//) {$channel =~ s/-\d$//gi;}
$stmtA = "INSERT INTO live_inbound (uniqueid,channel,server_ip,caller_id,extension,phone_ext,start_time,inbound_number,comment_a,comment_b,comment_c,comment_d,comment_e) values('$unique_id','$channel','$VARserver_ip','$callerid','$extension','$phone_ext','$now_date','$inbound_number','$comment_a','$comment_b','$comment_c','$comment_d','$comment_e')";
if ($AGILOG) {$agi_string = "|$stmtA|"; &agi_output;}
$affected_rows = $dbhA->do($stmtA);
$stmtA = "INSERT INTO live_inbound_log (uniqueid,channel,server_ip,caller_id,extension,phone_ext,start_time,inbound_number,comment_a,comment_b,comment_c,comment_d,comment_e) values('$unique_id','$channel','$VARserver_ip','$callerid','$extension','$phone_ext','$now_date','$inbound_number','$comment_a','$comment_b','$comment_c','$comment_d','$comment_e')";
if ($AGILOG) {$agi_string = "|$stmtA|"; &agi_output;}
$affected_rows = $dbhA->do($stmtA);
$dbhA->disconnect();
}
### call end stage
else
{
if ($AGILOG) {$agi_string = "CALL INBOUND HUNG UP; deleting live_inbound record for $unique_id"; &agi_output;}
$stmtA = "DELETE from live_inbound where uniqueid='$unique_id' and server_ip='$VARserver_ip'";
$affected_rows = $dbhA->do($stmtA);
$dbhA->disconnect();
if ($AGILOG) {$agi_string = "+++++ CALL INBOUND HUNGUP: |$unique_id|$affected_rows|"; &agi_output;}
}
exit;
sub checkresult {
my ($res) = @_;
my $retval;
$tests++;
chomp $res;
if ($res =~ /^200/) {
$res =~ /result=(-?\d+)/;
if (!length($1)) {
print STDERR "FAIL ($res)\n";
$fail++;
} else {
print STDERR "PASS ($1)\n";
$pass++;
}
} else {
print STDERR "FAIL (unexpected result '$res')\n";
$fail++;
}
}
sub agi_output
{
if ($AGILOG >=2)
{
### open the log file for writing ###
open(Lout, ">>$AGILOGfile")
|| die "Can't open $AGILOGfile: $!\n";
print Lout "$now_date|$script|$agi_string\n";
close(Lout);
}
### send to STDERR writing ###
if ( ($AGILOG == '1') || ($AGILOG == '3') )
{print STDERR "$now_date|$script|$agi_string\n";}
$agi_string='';
}
@@ -0,0 +1,339 @@
#!/usr/bin/perl
#
# cepstral_generate.pl version 2.2.0
#
# Text-To-Speech files will be cached for later use.
#
# Call this script like this:
# /var/lib/asterisk/agi-bin/cepstral_generate.pl --debug --voice=Allison-8kHz --dialog=Hello\ There
#
# Copyright (C) 2009 Matt Florell, Mike Cargile <vicidial@gmail.com> LICENSE: AGPLv2
#
# CHANGES:
# 90406-1058 - First Build
# 90410-1458 - Fixed generate bug, added conf path variables
#
# Cepstral paths
$PATHvoices = '/tmp/swift_voices';
$PATHswift = '/opt/swift/bin/swift';
# default path to astguiclient configuration file:
$PATHconf = '/etc/astguiclient.conf';
open(conf, "$PATHconf") || die "can't open $PATHconf: $!\n";
@conf = <conf>;
close(conf);
$i=0;
foreach(@conf)
{
$line = $conf[$i];
$line =~ s/ |>|\n|\r|\t|\#.*|;.*//gi;
if ( ($line =~ /^PATHagi/) && ($CLIagi < 1) )
{$PATHagi = $line; $PATHagi =~ s/.*=//gi;}
if ( ($line =~ /^PATHsounds/) && ($CLIsounds < 1) )
{$PATHsounds = $line; $PATHsounds =~ s/.*=//gi;}
$i++;
}
### begin parsing run-time options ###
if (length($ARGV[0])>1)
{
$i=0;
while ($#ARGV >= $i)
{
$args = "$args $ARGV[$i]";
$i++;
}
if ($args =~ /--help/i)
{
print "allowed run time options:\n";
print " [-t] = test\n";
print " [-debug] = verbose debug messages\n";
print " [-debugX] = Extra-verbose debug messages\n";
print " [-voice=Allison-8kHz] = Cepstral voice to use(Allison-8kHz is default)\n\n";
print " [-dialog=Hello] = Message to generate\n\n";
print " (This must be the LAST option)\n\n";
}
else
{
if ($args =~ /-debug/i)
{
$DB=1; # Debug flag
}
if ($args =~ /--debugX/i)
{
$DBX=1;
print "\n----- SUPER-DUPER DEBUGGING -----\n\n";
}
if ($args =~ /-t/i)
{
$TEST=1;
$T=1;
}
if ($args =~ /-voice=/i)
{
@data_in = split(/-voice=/,$args);
$voice = $data_in[1];
$voice =~ s/ .*//gi;
if ($DBX > 0) {print "\n----- VOICE: $voice -----\n\n";}
}
else
{$voice = 'Allison-8kHz';}
if ($args =~ /-dialog=/i)
{
@data_in = split(/-dialog=/,$args);
$dialog = $data_in[1];
$dialog =~ s/\n|\r|\l|\t//gi;
if ($DBX > 0) {print "\n----- DIALOG: $dialog -----\n\n";}
}
else
{$dialog = '';}
}
}
else
{
# print "no command line options set\n";
}
### end parsing run-time options ###
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year = ($year + 1900);
$mon++;
if ($hour < 10) {$hour = "0$hour";}
if ($min < 10) {$min = "0$min";}
if ($sec < 10) {$sec = "0$sec";}
if ($mon < 10) {$mon = "0$mon";}
if ($mday < 10) {$mday = "0$mday";}
$SQL_date = "$year-$mon-$mday $hour:$min:$sec";
use Digest::MD5 qw(md5_hex);
use Asterisk::AGI;
$AGI = new Asterisk::AGI;
#my @voice = &get_cep_voice(2);
if (length($dialog) > 1)
{
$enc = md5_hex("$dialog-$voice"); # the hash
$enc_ftl = substr($enc, 0, 2); # first letter of hash
$enc_file = "tts/" . $enc_ftl . "/tts-" . $enc . ".wav";
if ($DB > 0)
{print "$SQL_date - Creating $dialog - $voice file: $enc_file\n";}
# &say_cepstral("$dialog","$voice");
&gen_cepstral("$dialog","$voice");
}
exit;
# EXAMPLES
#&say_cepstral("Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal. Now we are engaged in a great civil war, testing whether that nation, or any nation so conceived and so dedicated, can long endure. We are met on a great battle-field of that war. We have come to dedicate a portion of that field, as a final resting place for those who here gave their lives that that nation might live. It is altogether fitting and proper that we should do this. But, in a larger sense, we can not dedicate -- we can not consecrate -- we can not hallow -- this ground. The brave men, living and dead, who struggled here, have consecrated it, far above our poor power to add or detract. The world will little note, nor long remember what we say here, but it can never forget what they did here. It is for us the living, rather, to be dedicated here to the unfinished work which they who fought here have thus far so nobly advanced. It is rather for us to be here dedicated to the great task remaining before us -- that from these honored dead we take increased devotion to that cause for which they gave the last full measure of devotion -- that we here highly resolve that these dead shall not have died in vain -- that this nation, under God, shall have a new birth of freedom -- and that government of the people, by the people, for the people, shall not perish from the earth.",$voice[0]);
#&say_cepstral("Welcome to the VEECHEE DIAL open source call center suite",$voice[0]);
# function to get a voice that is currently installed
# takes and integer
# the integer needs to be greater than 0
# returns an array with the following info in this order
# Voice
# Version
# Lic?
# Gender
# Age
# Language
# Sample Rate
sub get_cep_voice {
my $number = $_[0];
if ($number <= 0) {
return 0; # failure
}
my $cepstral_app = "$PATHswift";
my $command = $cepstral_app.' --voices | tail +8 | tr -d " " > '.$PATHvoices;
system( $command ); # Ask swift for the voices
my @voice;
my @voice_lines;
my $voice_count;
open(VFILE,"$PATHvoices"); # read in the file with the voices
@voice_lines = <VFILE>;
close(VFILE);
#unlink("/tmp/swift_vioces"); # delete the file with the voices
# find the correct voice
foreach my $voice_line(@voice_lines) {
chomp($voice_line);
$voice_count++;
if ($voice_count == $number) {
@voice = split(/\|/,$voice_line);
}
}
# return it
return @voice;
}
# Function to return the number of voices that are installed
# Returns an int
sub get_cep_num_voices {
my $cepstral_app = "$PATHswift";
my $command = $cepstral_app.' --voices | tail +8 | tr -d " " > '.$PATHvoices;
system( $command ); # Ask swift for the voices
my @voice_lines;
my $number = 0;
# read in the file with the voices
open(VFILE,"$PATHvoices");
@voice_lines = <VFILE>;
close(VFILE);
#unlink("/tmp/swift_vioces"); # delete it
foreach (@voice_lines) {
$number++;
}
return $number;
}
# Function to pregenerate TTS file
# First option is the text
# Second option is the voice
sub gen_cepstral {
my $text = $_[0]; # the text to play
my $voice = $_[1]; # voice to use
# hash of the text
my $hash = md5_hex("$text-$voice"); # the hash
my $hash_ftl = substr($hash, 0, 2); # first two letters of hash
# Directories that hold everything
my $astsounddir = $PATHsounds; # asterisk sound directory
my $ttssounddir = $astsounddir."/tts";
my $astsubdir = "tts/".$hash_ftl; # sub directory that holds the file
my $sounddir = $astsounddir . "/" . $astsubdir; # full path to the directory
# Wave File to play
my $wavefile = "tts-".$hash; # wave file without the .wav at the end
my $wavepath = $sounddir."/".$wavefile.".wav"; # full path to the wavefile
my $astwavpath = $astsubdir."/".$wavefile; # asterisk path to .wav file without .wav
# text file of words to say
my $textfile = "tts-text-".$hash.".txt"; # file to hold the words to say
my $textpath = $sounddir."/".$textfile; # full path to the text file
# generate the tts
if (!(&real_gen_cepstral($text, $voice, $ttssounddir, $sounddir, $wavepath, $textpath))) {
#TODO print an error message somewhere
return 0; # failure
}
return 1; # success
}
# Function to play TTS file
# Will generate TTS file if it is not pregenerated
# First option is the text
# Second option is the voice
sub say_cepstral {
my $text = $_[0]; # the text to play
my $voice = $_[1]; # voice to use
my %input = $AGI->ReadParse();
# hash of the text
my $hash = md5_hex("$text-$voice"); # the hash
my $hash_ftl = substr($hash, 0, 2); # first letter of hash
# Directories that hold everything
my $astsounddir = $PATHsounds; # asterisk sound directory
my $ttssounddir = $astsounddir."/tts";
my $astsubdir = "tts/".$hash_ftl; # sub directory that holds the file
my $sounddir = $astsounddir."/".$astsubdir; # full path to the directory
# Wave File to play
my $wavefile = "tts-".$hash; # wave file without the .wav at the end
my $wavepath = $sounddir."/".$wavefile.".wav"; # full path to the wavefile
my $astwavpath = $astsubdir."/".$wavefile; # asterisk path to .wav file without .wav
# text file of words to say
my $textfile = "tts-text-".$hash.".txt"; # file to hold the words to say
my $textpath = $sounddir."/".$textfile; # full path to the text file
# generate the tts
if (!(&real_gen_cepstral($text, $voice, $ttssounddir, $sounddir, $wavepath, $textpath))) {
#TODO print an error message somewhere
return 0; # failure
}
# wait up to a second
# for the sound file to appear
# and the textfile to be deleted
# This is for concurrency issues
my $i = 0;
while ( ($i < 100) && !(-f $wavepath) && (-f $textpath) ) {
usleep(10000);
$i++;
}
$AGI->stream_file($astwavpath); # play the tts
return 1; # success
}
# This is a helper function do not call it directly
sub real_gen_cepstral {
my $text = $_[0];
my $voice = $_[1];
my $ttssounddir = $_[2];
my $sounddir = $_[3];
my $wavepath = $_[4];
my $textpath = $_[5];
# Cepstral commnad and options
my $cepstral_app = "$PATHswift"; # the executable
my $cepstral_opt = "-p speech/rate=140"; # the command line options
# Make sure the tts directory is there
if (!(-d $ttssounddir)) {
if (!(mkdir($ttssounddir))) {
#TODO print an error message somewhere
return 0; # failure - cannot make the tts directory
}
}
# Make sure the sub directory is there
if (!(-d $sounddir)) {
if (!(mkdir($sounddir))) {
#TODO print an error message somewhere
return 0; # failure - cannot make the sub directory
}
}
# check if both the wav file and the text file do not exists
if (!( -f $wavepath ) && !( -f $textpath )) {
# write to the text file whats to be said
open(fileOUT, ">$textpath");
print fileOUT "$text";
close(fileOUT);
# build the command to call cepstral and give it the text file to generate
my $command=$cepstral_app." ".$cepstral_opt." -n ".$voice." -o ".$wavepath." -f ".$textpath." > /dev/null";
# execute the command
system($command);
# delete the text file
unlink($textpath);
}
return 1; # success
}
+349
View File
@@ -0,0 +1,349 @@
#!/usr/bin/perl
#
# cm.agi version 2.8
#
# Used by Call Menus to log digit presses if enabled in the settings. You should
# not need to manually use this script, it is used by the dialplan builder.
#
# You need to put lines similar to those below in your extensions.conf file:
# ; Below are the parameters needed for the script to be run properly
# ; 1. the in-group to take settings from
# ; 2. digit pressed
# ; 3. field(in vicidial_list) to update with DTMF pressed (only if lead_id populated)
# ;
# ;inbound calls check calltime:
#exten => 1,1,AGI(cm.agi,INBOUND-----1-----province)
#exten => 1,2,...
#
# ;to allow for multi-digit entries, put similar entry in the Custom Dialplan Entry:
#exten => _XXXXX,1,AGI(cm.agi,INBOUND-----${EXTEN}-----postal_code)
#exten => _XXXXX,n,Goto(next_callmenu_here,s,1)
#
# Copyright (C) 2013 Matt Florell <vicidial@gmail.com> LICENSE: AGPLv2
#
# changes:
# 110525-2100 - First Build
# 111003-2150 - Added field to update
# 120730-0729 - Changed to allow inbound logging when lead is defined
# 130925-1822 - Added variable filter to prevent DID SQL injection attack
# 130926-1547 - Added new queue_log IVR entries for QM
#
$script = 'cm.agi';
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year = ($year + 1900);
$mon++;
if ($mon < 10) {$mon = "0$mon";}
if ($mday < 10) {$mday = "0$mday";}
if ($hour < 10) {$hour = "0$hour";}
if ($min < 10) {$min = "0$min";}
if ($sec < 10) {$sec = "0$sec";}
$now_date_epoch = time();
$now_date = "$year-$mon-$mday $hour:$min:$sec";
$vicidial_list_fields = '|vendor_lead_code|source_id|phone_code|title|first_name|middle_initial|last_name|address1|address2|address3|city|state|province|postal_code|country_code|alt_phone|email|security_phrase|comments|rank|owner|status|user|q01|q02|q03|q04|q05|q06|q07|q08|q09|q10|q11|q12|q13|q14|q15|q16|q17|q18|q19|q20|q21|q22|q23|q24|q25|q26|q27|q28|q29|q30|q31|q32|q33|q34|q35|q36|q37|q38|q39|q40|q41|q42|q43|q44|q45|q46|q47|q48|q49|q50|q51|q52|q53|q54|q55|q56|q57|q58|q59|q60|q61|q62|q63|q64|q65|q66|q67|q68|q69|q70|q71|q72|q73|q74|q75|q76|q77|q78|q79|q80|q81|q82|q83|q84|q85|q86|q87|q88|q89|q90|q91|q92|q93|q94|q95|q96|q97|q98|q99|';
# default path to astguiclient configuration file:
$PATHconf = '/etc/astguiclient.conf';
open(conf, "$PATHconf") || die "can't open $PATHconf: $!\n";
@conf = <conf>;
close(conf);
$i=0;
foreach(@conf)
{
$line = $conf[$i];
$line =~ s/ |>|\n|\r|\t|\#.*|;.*//gi;
if ( ($line =~ /^PATHhome/) && ($CLIhome < 1) )
{$PATHhome = $line; $PATHhome =~ s/.*=//gi;}
if ( ($line =~ /^PATHlogs/) && ($CLIlogs < 1) )
{$PATHlogs = $line; $PATHlogs =~ s/.*=//gi;}
if ( ($line =~ /^PATHagi/) && ($CLIagi < 1) )
{$PATHagi = $line; $PATHagi =~ s/.*=//gi;}
if ( ($line =~ /^PATHweb/) && ($CLIweb < 1) )
{$PATHweb = $line; $PATHweb =~ s/.*=//gi;}
if ( ($line =~ /^PATHsounds/) && ($CLIsounds < 1) )
{$PATHsounds = $line; $PATHsounds =~ s/.*=//gi;}
if ( ($line =~ /^PATHmonitor/) && ($CLImonitor < 1) )
{$PATHmonitor = $line; $PATHmonitor =~ s/.*=//gi;}
if ( ($line =~ /^VARserver_ip/) && ($CLIserver_ip < 1) )
{$VARserver_ip = $line; $VARserver_ip =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_server/) && ($CLIDB_server < 1) )
{$VARDB_server = $line; $VARDB_server =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_database/) && ($CLIDB_database < 1) )
{$VARDB_database = $line; $VARDB_database =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_user/) && ($CLIDB_user < 1) )
{$VARDB_user = $line; $VARDB_user =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_pass/) && ($CLIDB_pass < 1) )
{$VARDB_pass = $line; $VARDB_pass =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_port/) && ($CLIDB_port < 1) )
{$VARDB_port = $line; $VARDB_port =~ s/.*=//gi;}
$i++;
}
if (!$VARDB_port) {$VARDB_port='3306';}
if (!$AGILOGfile) {$AGILOGfile = "$PATHlogs/agiout.$year-$mon-$mday";}
use DBI;
use Time::HiRes ('gettimeofday','usleep','sleep'); # necessary to have perl sleep command of less than one second
use Asterisk::AGI;
$AGI = new Asterisk::AGI;
$dbhA = DBI->connect("DBI:mysql:$VARDB_database:$VARDB_server:$VARDB_port", "$VARDB_user", "$VARDB_pass")
or die "Couldn't connect to database: " . DBI->errstr;
### Grab Server values from the database
$stmtA = "SELECT agi_output FROM servers where server_ip = '$VARserver_ip';";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
if ($sthArows > 0)
{
$AGILOG = '0';
@aryA = $sthA->fetchrow_array;
$DBagi_output = "$aryA[0]";
if ($DBagi_output =~ /STDERR/) {$AGILOG = '1';}
if ($DBagi_output =~ /FILE/) {$AGILOG = '2';}
if ($DBagi_output =~ /BOTH/) {$AGILOG = '3';}
}
$sthA->finish();
#############################################
##### START QUEUEMETRICS LOGGING LOOKUP #####
$stmtA = "SELECT enable_queuemetrics_logging,queuemetrics_server_ip,queuemetrics_dbname,queuemetrics_login,queuemetrics_pass,queuemetrics_log_id,queuemetrics_eq_prepend FROM system_settings;";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
if ($sthArows > 0)
{
@aryA = $sthA->fetchrow_array;
$enable_queuemetrics_logging = $aryA[0];
$queuemetrics_server_ip = $aryA[1];
$queuemetrics_dbname = $aryA[2];
$queuemetrics_login= $aryA[3];
$queuemetrics_pass = $aryA[4];
$queuemetrics_log_id = $aryA[5];
$queuemetrics_eq_prepend = $aryA[6];
}
$sthA->finish();
##### END QUEUEMETRICS LOGGING LOOKUP #####
###########################################
### begin parsing run-time options ###
if (length($ARGV[0])>1)
{
if ($AGILOG) {$agi_string = "Perl Environment Dump:"; &agi_output;}
$i=0;
while ($#ARGV >= $i)
{
$args = "$args $ARGV[$i]";
if ($AGILOG) {$agi_string = "$i|$ARGV[$i]"; &agi_output;}
$i++;
}
### list of command-line array arguments:
@ARGV_vars = split(/-----/, $ARGV[0]);
$channel_group = $ARGV_vars[0];
$dtmf = $ARGV_vars[1];
$field = $ARGV_vars[2];
}
$|=1;
while(<STDIN>)
{
chomp;
last unless length($_);
if ($AGILOG)
{
if (/^agi_(\w+)\:\s+(.*)$/)
{
$AGI{$1} = $2;
}
}
if (/^agi_context\:\s+(.*)$/) {$context = $1;}
if (/^agi_priority\:\s+(.*)$/) {$priority = $1;}
if (/^agi_uniqueid\:\s+(.*)$/) {$unique_id = $1; $uniqueid = $unique_id;}
if (/^agi_channel\:\s+(.*)$/) {$channel = $1;}
if (/^agi_extension\:\s+(.*)$/) {$extension = $1;}
if (/^agi_type\:\s+(.*)$/) {$type = $1;}
if (/^agi_callerid\:\s+(.*)$/) {$callerid = $1;}
if (/^agi_calleridname\:\s+(.*)$/) {$calleridname = $1;}
}
$callerid =~ s/\'|\\\\|\\\|\\|\\;|\\\;|\;|;//gi;
$calleridname =~ s/\'|\\\\|\\\|\\|\\;|\\\;|\;|;//gi;
$extension =~ s/\'|\"|\\\\|\\\|\\|\\;|\\\;|\;|;//gi;
$outboundIVR=0;
$fieldUPDATE=0;
if ($calleridname =~ /^V\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d/)
{
$phone_number = $callerid;
$callerid = $calleridname;
$outboundIVR=1;
$stmtA = "SELECT campaign_id,lead_id FROM vicidial_auto_calls where callerid='$callerid';";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
if ($sthArows > 0)
{
@aryA = $sthA->fetchrow_array;
$campaign_id = $aryA[0];
$lead_id = $aryA[1];
}
$sthA->finish();
}
elsif ($calleridname =~ /^Y\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d/)
{
$lead_id = substr($calleridname, 10, 10);
$lead_id = ($lead_id + 0);
$phone_number = $callerid;
$callerid = $calleridname;
if ( (length($field)>2) && ($vicidial_list_fields =~ /\|$field\|/) )
{
$fieldUPDATE=1;
}
}
else
{
if ( ($callerid =~ /\".*\"/) && ( (!$calleridname) or ($calleridname =~ /unknown/) ) )
{
$calleridname = $callerid;
$calleridname =~ s/\<\d\d\d\d\d\d\d\d\d\d\>//gi;
$calleridname =~ s/\"|\" //gi;
}
$callerid =~ s/\D|\'//gi;
$calleridname =~ s/unknown|\'//gi;
if ( (!$callerid) or ($callerid =~ /unknown/) )
{$callerid = $calleridname;}
if (length($callerid)>0) {$phone_number = $callerid;}
else {$phone_number = '';}
if (length($calleridname)>0) {$VLcomments = $calleridname;}
else {$VLcomments = '';}
}
if (length($callerid)<8) {$callerid = $parked_by;}
if (length($pin)>0) {$callerid = $pin;}
foreach $i (sort keys %AGI)
{
if ($AGILOG) {$agi_string = " -- $i = $AGI{$i}"; &agi_output;}
}
if ($AGILOG) {$agi_string = "AGI Variables: |$unique_id|$channel|$extension|$type|$callerid|"; &agi_output;}
if (length($pin) < 1) {$pin=$inbound_number;}
$fronter = $pin;
if ($AGILOG) {$agi_string = "+++++ DTMF LOG STARTED : |$channel_group|$callerid-$pin|$now_date"; &agi_output;}
$VDADphone='';
$VDADphone_code='';
if ($channel =~ /Local/i)
{
if ($AGILOG) {$agi_string = "+++++ VDAD START LOCAL CHANNEL: EXITING- $priority"; &agi_output;}
exit;
}
### insert an IVR record to the vicidial_auto_calls table
if ($outboundIVR > 0)
{
$stmtA = "INSERT INTO vicidial_outbound_ivr_log (uniqueid,caller_code,event_date,campaign_id,lead_id,menu_id,menu_action) values('$uniqueid','$callerid','$now_date','$campaign_id','$lead_id','$context>$dtmf','$dtmf');";
if ($AGILOG) {$agi_string = "|$stmtA|"; &agi_output;}
$affected_rowsL = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "-- VOIL insert: |$affected_rowsL|$uniqueid|$callerid|$campaign_id|$lead_id|$context|"; &agi_output;}
if ( (length($field)>2) && ($vicidial_list_fields =~ /\|$field\|/) )
{
if ( ($field =~ /^q/i) && ($dtmf !~ /[0-9]/) )
{
if ($AGILOG) {$agi_string = "-- VL non-update: |$field|$dtmf|$lead_id|$context|"; &agi_output;}
}
else
{
$stmtA = "UPDATE vicidial_list SET $field='$dtmf' where lead_id='$lead_id';";
if ($AGILOG) {$agi_string = "|$stmtA|"; &agi_output;}
$affected_rowsL = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "-- VL update: |$affected_rowsL|$field|$dtmf|$lead_id|$context|"; &agi_output;}
}
}
}
else
{
if ($fieldUPDATE > 0)
{
if ( ($field =~ /^q/i) && ($dtmf !~ /[0-9]/) )
{
if ($AGILOG) {$agi_string = "-- VL non-update: |$field|$dtmf|$lead_id|$context|"; &agi_output;}
}
else
{
$stmtA = "UPDATE vicidial_list SET $field='$dtmf' where lead_id='$lead_id';";
if ($AGILOG) {$agi_string = "|$stmtA|"; &agi_output;}
$affected_rowsL = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "-- VL update: |$affected_rowsL|$field|$dtmf|$lead_id|$context|"; &agi_output;}
}
}
$stmtA = "INSERT INTO live_inbound_log (uniqueid,channel,server_ip,caller_id,extension,phone_ext,start_time,comment_a,comment_b,comment_d,comment_e) values('$uniqueid','$channel','$VARserver_ip','$callerid','$extension','$phone_number','$now_date','$channel_group','$dtmf','$context>$dtmf','$priority');";
if ($AGILOG) {$agi_string = "|$stmtA|"; &agi_output;}
$affected_rowsL = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "-- LIL insert: |$affected_rowsL|$uniqueid|$callerid|$extension|"; &agi_output;}
}
### QueueMetrics logging if enabled
if ($enable_queuemetrics_logging > 0)
{
$dbhB = DBI->connect("DBI:mysql:$queuemetrics_dbname:$queuemetrics_server_ip:3306", "$queuemetrics_login", "$queuemetrics_pass")
or die "Couldn't connect to database: " . DBI->errstr;
if ($DBX) {print "CONNECTED TO QueueMetrics DATABASE: $queuemetrics_server_ip|$queuemetrics_dbname\n";}
if ($outboundIVR > 0)
{$uniqueid = $caller_id;}
$stmtB = "INSERT INTO queue_log SET partition='P01',time_id='$now_date_epoch',call_id='$uniqueid',queue='NONE',agent='NONE',verb='INFO',data1='IVRAPPEND',data2='$dtmf',data3='$context',serverid='$queuemetrics_log_id';";
if ($AGILOG) {$agi_string = "|$stmtB|"; &agi_output;}
$Baffected_rows = $dbhB->do($stmtB);
$dbhB->disconnect();
}
$sthA->finish();
$dbhA->disconnect();
exit;
sub agi_output
{
if ($AGILOG >=2)
{
### open the log file for writing ###
open(Lout, ">>$AGILOGfile")
|| die "Can't open $AGILOGfile: $!\n";
print Lout "$now_date|$script|$agi_string\n";
close(Lout);
}
### send to STDERR writing ###
if ( ($AGILOG == '1') || ($AGILOG == '3') )
{print STDERR "$now_date|$script|$agi_string\n";}
$agi_string='';
}
+149
View File
@@ -0,0 +1,149 @@
#!/usr/bin/perl
#
# cm_date.agi version 2.6
#
# Can be used in a call menu prompt to play a date string as defined by the settings below
#
# ; Below are the parameters needed for the script to be run properly
# ; the "SAY DATETIME" parameters for what to say separated by three dashes
# ; 1. offset from current epoch in seconds, positive or negative
# ; 1 hour is 3600
# ; 1 day is 86400
# ; 1 week is 604800
# ; 30 days is 2592000
# ; 1 year is 31536000
# ; 2. format of the date to say, i.e. "ABdY":
# ; A: Day of the week
# ; B: Month (Full Text)
# ; m: Month (Numeric)
# ; d: Day of the month
# ; Y: Year
# ; I: Hour (12-hour format)
# ; H: Hour (24-hour format)
# ; M: Minutes
# ; P: AM/PM
# ; Q: Shorthand for Today, Yesterday or ABdY
# ; R: Shorthand for HM
# ; S: Seconds
# ; T: Timezone
#
# ;inbound calls check calltime:
# ; (this example is for yesterday and says "Monday March 25th 2013")
# cm_date.agi,-86400---ABdY
#
# Copyright (C) 2013 Matt Florell <vicidial@gmail.com> LICENSE: AGPLv2
#
# changes:
# 130326-1718 - First Build
#
$script = 'cm_date.agi';
$now_date_epoch = time();
$now_date = time();
$AGILOG = '1'; # 1=STDERR output
$exten='';
use Asterisk::AGI;
$AGI = new Asterisk::AGI;
$AGI->stream_file('sip-silence');
$AGI->stream_file('sip-silence');
$AGI->stream_file('sip-silence');
### begin parsing run-time options ###
if (length($ARGV[0])>1)
{
if ($AGILOG) {$agi_string = "Perl Environment Dump:"; &agi_output;}
$i=0;
while ($#ARGV >= $i)
{
$args = "$args $ARGV[$i]";
if ($AGILOG) {$agi_string = "$i|$ARGV[$i]"; &agi_output;}
$i++;
}
### list of command-line array arguments:
@ARGV_vars = split(/---/, $ARGV[0]);
$epoch_offset = $ARGV_vars[0];
$datetime_format = $ARGV_vars[1];
}
$|=1;
while(<STDIN>)
{
chomp;
last unless length($_);
if ($AGILOG)
{
if (/^agi_(\w+)\:\s+(.*)$/)
{
$AGI{$1} = $2;
}
}
if (/^agi_context\:\s+(.*)$/) {$context = $1;}
}
$new_epoch = ($now_date_epoch + $epoch_offset);
if ($AGILOG) {$agi_string = "Date Epoch: |$now_date_epoch|$epoch_offset|$new_epoch| Format: |$datetime_format|"; &agi_output;}
# SAY DATETIME <time> <escape digits> [format] [timezone]
print "SAY DATETIME $new_epoch 0 \"$datetime_format\"\n"; checkresult($result);
exit;
sub checkresult
{
$pass=0;
$fail=0;
my ($res) = @_;
my $retval;
$tests++;
chomp $res;
if ($res =~ /^200/)
{
$res =~ /result=(-?\d+)/;
if (!length($1))
{
# print STDERR "FAIL ($res|$1)\n";
$fail++;
}
else
{
# print STDERR "PASS ($res|$1)\n";
$pass++;
}
}
else
{
# print STDERR "FAIL (unexpected result '$res')\n";
$fail++;
}
}
sub agi_output
{
if ($AGILOG >=2)
{
### open the log file for writing ###
open(Lout, ">>$AGILOGfile")
|| die "Can't open $AGILOGfile: $!\n";
print Lout "$now_date|$script|$agi_string\n";
close(Lout);
}
### send to STDERR writing ###
if ( ($AGILOG == '1') || ($AGILOG == '3') )
{print STDERR "$now_date|$script|$agi_string\n";}
$agi_string='';
}
+362
View File
@@ -0,0 +1,362 @@
#!/usr/bin/perl
#
# cm_dnc.agi version 2.8
#
# Designed to work with Cell Menus to allow setting of a number as DNC or NI
# This script is meant as a replacecment for the agi-VDADinbound_NI_DNC_CIDlookup.agi
# script. It is designed to work easily in the Call Menu framework, it plays no
# audio itself, and it has more options.
#
# Flag Options:
# 1- run as DNC status or NI status (Default=NI)
# 2- default to insert into the vicidial_dnc table for DNC selections (Default=YES)
# 3- campaign to take dnc settings from(overrides option 2)
# 4- call menu option to send the call to after executing (Default=B)
# 5- if option 3 is used, search for lead in campaign lists or whole system(CAMP or SYS) (Default=SYS)
#
# example of what to put in the AGI entry for a Call Menu AGI route:
# cm_dnc.agi,DNC---YES---TESTCAMP---B---CAMP
#
# Copyright (C) 2013 Matt Florell <vicidial@gmail.com> LICENSE: AGPLv2
#
# changes:
# 121109-1623 - First draft
# 130108-1817 - Changes for Asterisk 1.8 compatibility
# 130925-1825 - Added variable filter to prevent DID SQL injection attack
#
$script = 'cm_dnc.agi';
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year = ($year + 1900);
$mon++;
if ($mon < 10) {$mon = "0$mon";}
if ($mday < 10) {$mday = "0$mday";}
if ($hour < 10) {$hour = "0$hour";}
if ($min < 10) {$min = "0$min";}
if ($sec < 10) {$sec = "0$sec";}
$now_date_epoch = time();
$now_date = "$year-$mon-$mday $hour:$min:$sec";
$start_time=$now_date;
$CIDdate = "$mon$mday$hour$min$sec";
$tsSQLdate = "$year$mon$mday$hour$min$sec";
$SQLdate = "$year-$mon-$mday $hour:$min:$sec";
$SQLdateBEGIN = $SQLdate;
# default path to astguiclient configuration file:
$PATHconf = '/etc/astguiclient.conf';
open(conf, "$PATHconf") || die "can't open $PATHconf: $!\n";
@conf = <conf>;
close(conf);
$i=0;
foreach(@conf)
{
$line = $conf[$i];
$line =~ s/ |>|\n|\r|\t|\#.*|;.*//gi;
if ( ($line =~ /^PATHhome/) && ($CLIhome < 1) )
{$PATHhome = $line; $PATHhome =~ s/.*=//gi;}
if ( ($line =~ /^PATHlogs/) && ($CLIlogs < 1) )
{$PATHlogs = $line; $PATHlogs =~ s/.*=//gi;}
if ( ($line =~ /^PATHagi/) && ($CLIagi < 1) )
{$PATHagi = $line; $PATHagi =~ s/.*=//gi;}
if ( ($line =~ /^PATHweb/) && ($CLIweb < 1) )
{$PATHweb = $line; $PATHweb =~ s/.*=//gi;}
if ( ($line =~ /^PATHsounds/) && ($CLIsounds < 1) )
{$PATHsounds = $line; $PATHsounds =~ s/.*=//gi;}
if ( ($line =~ /^PATHmonitor/) && ($CLImonitor < 1) )
{$PATHmonitor = $line; $PATHmonitor =~ s/.*=//gi;}
if ( ($line =~ /^VARserver_ip/) && ($CLIserver_ip < 1) )
{$VARserver_ip = $line; $VARserver_ip =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_server/) && ($CLIDB_server < 1) )
{$VARDB_server = $line; $VARDB_server =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_database/) && ($CLIDB_database < 1) )
{$VARDB_database = $line; $VARDB_database =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_user/) && ($CLIDB_user < 1) )
{$VARDB_user = $line; $VARDB_user =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_pass/) && ($CLIDB_pass < 1) )
{$VARDB_pass = $line; $VARDB_pass =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_port/) && ($CLIDB_port < 1) )
{$VARDB_port = $line; $VARDB_port =~ s/.*=//gi;}
$i++;
}
if (!$VARDB_port) {$VARDB_port='3306';}
if (!$AGILOGfile) {$AGILOGfile = "$PATHlogs/agiout.$year-$mon-$mday";}
use DBI;
use Asterisk::AGI;
$AGI = new Asterisk::AGI;
$dbhA = DBI->connect("DBI:mysql:$VARDB_database:$VARDB_server:$VARDB_port", "$VARDB_user", "$VARDB_pass")
or die "Couldn't connect to database: " . DBI->errstr;
### Grab Server values from the database
$stmtA = "SELECT agi_output FROM servers where server_ip = '$VARserver_ip';";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
$rec_count=0;
while ($sthArows > $rec_count)
{
$AGILOG = '0';
@aryA = $sthA->fetchrow_array;
$DBagi_output = $aryA[0];
if ($DBagi_output =~ /STDERR/) {$AGILOG = '1';}
if ($DBagi_output =~ /FILE/) {$AGILOG = '2';}
if ($DBagi_output =~ /BOTH/) {$AGILOG = '3';}
$rec_count++;
}
$sthA->finish();
### begin parsing run-time options ###
if (length($ARGV[0])>1)
{
if ($AGILOG) {$agi_string = "Perl Environment Dump:"; &agi_output;}
$i=0;
while ($#ARGV >= $i)
{
$args = "$args $ARGV[$i]";
if ($AGILOG) {$agi_string = "$i|$ARGV[$i]"; &agi_output;}
$i++;
}
### list of command-line array arguments:
@ARGV_vars = split(/---/, $ARGV[0]);
$new_status = $ARGV_vars[0];
$dnc_default = $ARGV_vars[1];
$campaign_id = $ARGV_vars[2];
$next_step = $ARGV_vars[3];
$camp_sys = $ARGV_vars[4];
}
if (length($new_status) < 1)
{$new_status = 'NI';}
if (length($dnc_default) < 2)
{$dnc_default = 'YES';}
if (length($next_step) < 1)
{$next_step = 'B';}
if (length($camp_sys) < 2)
{$camp_sys = 'SYS';}
$|=1;
while(<STDIN>)
{
chomp;
last unless length($_);
if ($AGILOG)
{
if (/^agi_(\w+)\:\s+(.*)$/)
{
$AGI{$1} = $2;
}
}
if (/^agi_context\:\s+(.*)$/) {$context = $1;}
if (/^agi_uniqueid\:\s+(.*)$/) {$unique_id = $1; $uniqueid = $unique_id;}
if (/^agi_channel\:\s+(.*)$/) {$channel = $1;}
if (/^agi_extension\:\s+(.*)$/) {$extension = $1;}
if (/^agi_type\:\s+(.*)$/) {$type = $1;}
if (/^agi_callerid\:\s+(.*)$/) {$callerid = $1;}
if (/^agi_calleridname\:\s+(.*)$/) {$calleridname = $1;}
$callerid =~ s/\'|\\\\|\\\|\\|\\;|\\\;|\;|;//gi;
$calleridname =~ s/\'|\\\\|\\\|\\|\\;|\\\;|\;|;//gi;
$extension =~ s/\'|\"|\\\\|\\\|\\|\\;|\\\;|\;|;//gi;
if ( ($callerid =~ /\".*\"/) && ( (!$calleridname) or ($calleridname =~ /unknown/) ) )
{
$calleridname = $callerid;
$calleridname =~ s/\<\d\d\d\d\d\d\d\d\d\d\>//gi;
$calleridname =~ s/\"|\" //gi;
}
$callerid =~ s/\D//gi;
$calleridname =~ s/unknown//gi;
if ( (!$callerid) or ($callerid =~ /unknown/) )
{$callerid = $calleridname;}
if (length($callerid)>0) {$phone_number = $callerid;}
else {$phone_number = '';}
if (length($calleridname)>0) {$VLcomments = $calleridname;}
else {$VLcomments = '';}
if (length($callerid)<10) {$callerid = $parked_by;}
if (length($pin)>0) {$callerid = $pin;}
}
foreach $i (sort keys %AGI)
{
if ($AGILOG) {$agi_string = " -- $i = $AGI{$i}"; &agi_output;}
}
if ($AGILOG) {$agi_string = "AGI Variables: |$unique_id|$channel|$extension|$type|$callerid|"; &agi_output;}
# if campaign is used and selected for search, find the lists in the campaign
$listSQL='';
if ( (length($campaign_id) > 0) && ($camp_sys =~ /CAMP/) )
{
$cbc=0;
$SlistsSQL='';
$stmtA = "SELECT list_id FROM vicidial_lists where campaign_id='$campaign_id' limit 1000;";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
if ($AGILOG) {$agi_string = "$sthArows|$stmtA|"; &agi_output;}
while ($sthArows > $cbc)
{
@aryA = $sthA->fetchrow_array;
$SlistsSQL .= "'$aryA[0]',";
$cbc++;
}
$sthA->finish();
chop($SlistsSQL);
if (length($SlistsSQL)<3)
{$SlistsSQL = "''";}
if (length($SlistsSQL)>1)
{$listSQL = "and list_id IN($SlistsSQL)";}
}
### Gather the lead IDs of the leads with this phone number ###
$stmtA= "SELECT lead_id from vicidial_list where phone_number='$phone_number' $listSQL;";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
if ($AGILOG) {$agi_string = "$sthArows|$stmtA|"; &agi_output;}
$g=0;
$SleadsSQL='';
while ($sthArows > $g)
{
@aryA = $sthA->fetchrow_array;
$SleadsSQL .= "'$aryA[0]',";
$g++;
}
$sthA->finish();
chop($SleadsSQL);
### process for NI path
if ($new_status =~ /NI/) # set the phone_number to NI
{
if ($g > 0)
{
### update all records for the phone_number in vicidial_list to NI (Not Interested)
$stmtA = "UPDATE vicidial_list SET status='NI' where lead_id IN($SleadsSQL);";
$affected_rows = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "$affected_rows|$stmtA|"; &agi_output;}
}
}
### process for DNC path
if ($new_status =~ /DNC/) # set the phone_number to DNC
{
if ($g > 0)
{
### update all records for the phone_number in vicidial_list to NI (Not Interested)
$stmtA = "UPDATE vicidial_list SET status='DNC' where lead_id IN($SleadsSQL);";
$affected_rows = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "$affected_rows|$stmtA|"; &agi_output;}
}
if (length($campaign_id)>1)
{
### Grab DNC settings from campaign
$stmtA = "SELECT use_internal_dnc,use_campaign_dnc FROM vicidial_campaigns where campaign_id = '$campaign_id';";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
@aryA = $sthA->fetchrow_array;
$use_internal_dnc = $aryA[0];
$use_campaign_dnc = $aryA[1];
$sthA->finish();
if ( ($use_internal_dnc =~ /Y|AREACODE/) && ($dnc_default =~ /YES/) )
{
### add the number to the vicidial_dnc table
$stmtA = "INSERT IGNORE INTO vicidial_dnc SET phone_number='$phone_number';";
$affected_rows = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "$affected_rows|$stmtA|"; &agi_output;}
}
if ($use_campaign_dnc =~ /Y|AREACODE/)
{
### add the number to the vicidial_campaign_dnc table
$stmtA = "INSERT IGNORE INTO vicidial_campaign_dnc SET phone_number='$phone_number',campaign_id='$campaign_id';";
$affected_rows = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "$affected_rows|$stmtA|"; &agi_output;}
}
}
else
{
if ($dnc_default =~ /YES/)
{
### add the number to the vicidial_dnc table
$stmtA = "INSERT INTO vicidial_dnc SET phone_number='$phone_number';";
$affected_rows = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "$affected_rows|$stmtA|"; &agi_output;}
}
}
}
$dbhA->disconnect();
if ($AGILOG) {$agi_string = "exiting the cm_dnc app, transferring call to $next_step:$context |$phone_number|"; &agi_output;}
print "SET CONTEXT $context\n";
$result = <STDIN>;
checkresult($result);
print "SET EXTENSION $next_step\n";
$result = <STDIN>;
checkresult($result);
print "SET PRIORITY 1\n";
$result = <STDIN>;
checkresult($result);
exit;
sub agi_output
{
if ($AGILOG >=2)
{
### open the log file for writing ###
open(Lout, ">>$AGILOGfile")
|| die "Can't open $AGILOGfile: $!\n";
print Lout "$now_date|$script|$agi_string\n";
close(Lout);
}
### send to STDERR writing ###
if ( ($AGILOG == '1') || ($AGILOG == '3') )
{print STDERR "$now_date|$script|$agi_string\n";}
$agi_string='';
}
sub checkresult
{
my ($res) = @_;
my $retval;
$tests++;
chomp $res;
if ($res =~ /^200/)
{
$res =~ /result=(-?\d+)/;
if (!length($1))
{
# print STDERR "FAIL ($res)\n";
$fail++;
}
else
{
# print STDERR "PASS ($1)\n";
$pass++;
}
}
else
{
# print STDERR "FAIL (unexpected result '$res')\n";
$fail++;
}
}
+647
View File
@@ -0,0 +1,647 @@
#!/usr/bin/perl
#
# cm_lookup.agi version 2.6
#
# for use with a Call Menu in the custom dialplan section.
#
# ; settings for this script:
# ; 1. The vicidial_list field to do a lookup on ('vendor_lead_code' is default)
# ; 2. Type of lookup to do: (default is SYSTEM)
# ; SYSTEM will search the entire system
# ; CAMPAIGNLISTS will search inside all of the active lists within the campaign
# ; CAMPLISTSALL will search inside all of the active and inactive lists within the campaign
# ; LIST will search only within the specified List ID
# ; 3. If CAMPAIGN or LIST is used above, this is the campaign_id or list_id to be used
# ; 4. TTS confirmation of name (1 for yes, 2 for no) put in TTS prompt ID
# ; 5. Update the vicidial_list record phone number with the callerID of the caller:
# ; Y - update the record with the phone number
# ; N - Do nothing (default)
# ; 6. Update lead's address3 field to the uniqueid of the call
# ; Y - update the record with the uniqueid
# ; N - Do nothing (default)
# ; 7. Update the lead's record with the call's agent_user_id variable
# ; Y - update the lead record's 'user' field with the agent_user_id variable
# ; COMMENTS - update the lead record's 'comments' field with the agent_user_id variable
# ; N - Do nothing (default)
# ; 8. Update the lead's record with the call's title variable
# ; Y - update the lead record's 'title' field with the call's title variable
# ; N - Do nothing (default)
#
# Found entries will send the call to the 'A' option in the source call menu
# Not-Found entries will send the call to the 'B' option in the source call menu
#
#; example with 8-digit vendor lead codes
# exten => _XXXXXXXX,1,AGI(cm_lookup.agi,vendor_lead_code---CAMPLISTSALL---TESTCAMP---lastname_oneyestwono---N---N---Y---N)
#
#; example with 8-digit vendor lead codes
# exten => _XXXXXXXX,1,AGI(cm_lookup.agi,vendor_lead_code---CAMPLISTSALL---TESTCAMP---lastname_oneyestwono---N---N---Y---N)
#
# Copyright (C) 2013 Matt Florell <vicidial@gmail.com> LICENSE: AGPLv2
#
# changes:
# 111004-1450 - First Build
# 111212-1507 - Added update of vicidial_list phone_number with caller callerid number option
# 120511-1508 - Added option for 'address3' uniqueid update and 'user' agent_user_id field update if AGENT-dial-in AGI is used beforoe this AGI
# 120604-1311 - Added title call variable option
# 120824-1221 - Added filtering for non-digits in extension, and example of variable-digit account number
# 130108-1817 - Changes for Asterisk 1.8 compatibility
#
$script = 'cm_lookup.agi';
$S='*';
$US='_';
$vicidial_list_fields = '|lead_id|vendor_lead_code|source_id|list_id|gmt_offset_now|called_since_last_reset|phone_code|phone_number|title|first_name|middle_initial|last_name|address1|address2|address3|city|state|province|postal_code|country_code|gender|date_of_birth|alt_phone|email|security_phrase|comments|called_count|last_local_call_time|rank|owner|status|entry_date|entry_list_id|modify_date|user|q01|q02|q03|q04|q05|q06|q07|q08|q09|q10|q11|q12|q13|q14|q15|q16|q17|q18|q19|q20|q21|q22|q23|q24|q25|q26|q27|q28|q29|q30|q31|q32|q33|q34|q35|q36|q37|q38|q39|q40|q41|q42|q43|q44|q45|q46|q47|q48|q49|q50|q51|q52|q53|q54|q55|q56|q57|q58|q59|q60|q61|q62|q63|q64|q65|q66|q67|q68|q69|q70|q71|q72|q73|q74|q75|q76|q77|q78|q79|q80|q81|q82|q83|q84|q85|q86|q87|q88|q89|q90|q91|q92|q93|q94|q95|q96|q97|q98|q99|';
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year = ($year + 1900);
$mon++;
if ($mon < 10) {$mon = "0$mon";}
if ($mday < 10) {$mday = "0$mday";}
if ($hour < 10) {$hour = "0$hour";}
if ($min < 10) {$min = "0$min";}
if ($sec < 10) {$sec = "0$sec";}
$now_date_epoch = time();
$now_date = "$year-$mon-$mday $hour:$min:$sec";
$CLInow_date = "$year-$mon-$mday\\ $hour:$min:$sec";
$start_time=$now_date;
$CIDdate = "$mon$mday$hour$min$sec";
$tsSQLdate = "$year$mon$mday$hour$min$sec";
$SQLdate = "$year-$mon-$mday $hour:$min:$sec";
$SQLdateBEGIN = $SQLdate;
while (length($CIDdate) > 9) {$CIDdate =~ s/^.//gi;} # 0902235959 changed to 902235959
# default path to astguiclient configuration file:
$PATHconf = '/etc/astguiclient.conf';
open(conf, "$PATHconf") || die "can't open $PATHconf: $!\n";
@conf = <conf>;
close(conf);
$i=0;
foreach(@conf)
{
$line = $conf[$i];
$line =~ s/ |>|\n|\r|\t|\#.*|;.*//gi;
if ( ($line =~ /^PATHhome/) && ($CLIhome < 1) )
{$PATHhome = $line; $PATHhome =~ s/.*=//gi;}
if ( ($line =~ /^PATHlogs/) && ($CLIlogs < 1) )
{$PATHlogs = $line; $PATHlogs =~ s/.*=//gi;}
if ( ($line =~ /^PATHagi/) && ($CLIagi < 1) )
{$PATHagi = $line; $PATHagi =~ s/.*=//gi;}
if ( ($line =~ /^PATHweb/) && ($CLIweb < 1) )
{$PATHweb = $line; $PATHweb =~ s/.*=//gi;}
if ( ($line =~ /^PATHsounds/) && ($CLIsounds < 1) )
{$PATHsounds = $line; $PATHsounds =~ s/.*=//gi;}
if ( ($line =~ /^PATHmonitor/) && ($CLImonitor < 1) )
{$PATHmonitor = $line; $PATHmonitor =~ s/.*=//gi;}
if ( ($line =~ /^VARserver_ip/) && ($CLIserver_ip < 1) )
{$VARserver_ip = $line; $VARserver_ip =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_server/) && ($CLIDB_server < 1) )
{$VARDB_server = $line; $VARDB_server =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_database/) && ($CLIDB_database < 1) )
{$VARDB_database = $line; $VARDB_database =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_user/) && ($CLIDB_user < 1) )
{$VARDB_user = $line; $VARDB_user =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_pass/) && ($CLIDB_pass < 1) )
{$VARDB_pass = $line; $VARDB_pass =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_port/) && ($CLIDB_port < 1) )
{$VARDB_port = $line; $VARDB_port =~ s/.*=//gi;}
$i++;
}
if (!$VARDB_port) {$VARDB_port='3306';}
if (!$AGILOGfile) {$AGILOGfile = "$PATHlogs/agiout.$year-$mon-$mday";}
if (!$CEPLOGfile) {$CEPLOGfile = "$PATHlogs/Cepstral.$year-$mon-$mday";}
use DBI;
use Time::HiRes ('gettimeofday','usleep','sleep'); # necessary to have perl sleep command of less than one second
use Asterisk::AGI;
$AGI = new Asterisk::AGI;
$dbhA = DBI->connect("DBI:mysql:$VARDB_database:$VARDB_server:$VARDB_port", "$VARDB_user", "$VARDB_pass")
or die "Couldn't connect to database: " . DBI->errstr;
### Grab Server values from the database
$stmtA = "SELECT agi_output,local_gmt FROM servers where server_ip = '$VARserver_ip';";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
if ($sthArows > 0)
{
$AGILOG = '0';
@aryA = $sthA->fetchrow_array;
$DBagi_output = $aryA[0];
$local_gmt = $aryA[1];
if ($DBagi_output =~ /STDERR/) {$AGILOG = '1';}
if ($DBagi_output =~ /FILE/) {$AGILOG = '2';}
if ($DBagi_output =~ /BOTH/) {$AGILOG = '3';}
}
$sthA->finish();
### BEGIN parsing run-time options ###
if (length($ARGV[0])>1)
{
if ($AGILOG) {$agi_string = "Perl Environment Dump:"; &agi_output;}
$i=0;
while ($#ARGV >= $i)
{
$args = "$args $ARGV[$i]";
if ($AGILOG) {$agi_string = "$i|$ARGV[$i]"; &agi_output;}
$i++;
}
### list of command-line array arguments:
@ARGV_vars = split(/---/, $ARGV[0]);
$field = $ARGV_vars[0];
$search_method = $ARGV_vars[1];
$search_id = $ARGV_vars[2];
$tts_prompt = $ARGV_vars[3];
$phone_update = $ARGV_vars[4];
$uniqueid_address3 = $ARGV_vars[5];
$agent_id_update = $ARGV_vars[6];
$title_update = $ARGV_vars[7];
}
if ( (length($field) < 2) || ($vicidial_list_fields !~ /\|$field\|/) )
{$field = 'vendor_lead_code';}
if (length($search_method) < 1)
{$search_method = 'SYSTEM';}
if (length($search_id) < 1)
{$search_method = 'SYSTEM';}
### END parsing run-time options ###
$|=1;
while(<STDIN>)
{
chomp;
last unless length($_);
if ($AGILOG)
{
if (/^agi_(\w+)\:\s+(.*)$/)
{
$AGI{$1} = $2;
}
}
if (/^agi_context\:\s+(.*)$/) {$context = $1;}
if (/^agi_priority\:\s+(.*)$/) {$priority = $1;}
if (/^agi_uniqueid\:\s+(.*)$/) {$unique_id = $1; $uniqueid = $unique_id;}
if (/^agi_channel\:\s+(.*)$/) {$channel = $1;}
if (/^agi_extension\:\s+(.*)$/) {$extension = $1;}
if (/^agi_type\:\s+(.*)$/) {$type = $1;}
if (/^agi_callerid\:\s+(.*)$/) {$callerid = $1;}
if (/^agi_calleridname\:\s+(.*)$/) {$calleridname = $1;}
}
if ( ($callerid =~ /\".*\"/) && ( (!$calleridname) or ($calleridname =~ /unknown/) ) )
{
$calleridname = $callerid;
$calleridname =~ s/\<\d\d\d\d\d\d\d\d\d\d\>//gi;
$calleridname =~ s/\"|\" //gi;
}
$callerid =~ s/\D|\'//gi;
$calleridname =~ s/unknown|\'//gi;
if ( (!$callerid) or ($callerid =~ /unknown/) )
{$callerid = $calleridname;}
# remove non-digits from extension
$extension =~ s/\D//gi;
$agent_user_id = $AGI->get_variable('agent_user_id');
$call_var_title = $AGI->get_variable('title');
if ( ($AGILOG) && (length($agent_user_id)>0) ) {$agi_string = "user id AGI variables: |$agent_user_id|$call_var_title|"; &agi_output;}
$listSQL = '';
if ($search_method =~ /LIST$/)
{$listSQL = "and list_id='$search_id'";}
if ($search_method =~ /CAMPAIGNLISTS$|CAMPLISTSALL$/)
{
$activeSQL='';
if ($search_method =~ /CAMPAIGNLISTS$/)
{$activeSQL = "where active='Y'";}
### Grab all active lists associated with campaign_id
$cbc=0;
$SlistsSQL='';
$stmtA = "SELECT list_id FROM vicidial_lists where campaign_id='$search_id' $activeSQL limit 1000;";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
while ($sthArows > $cbc)
{
@aryA = $sthA->fetchrow_array;
$SlistsSQL .= "'$aryA[0]',";
$cbc++;
}
$sthA->finish();
chop($SlistsSQL);
if (length($SlistsSQL)>3)
{$listSQL = "and list_id IN($SlistsSQL)";}
}
if ($AGILOG) {$agi_string = "searching for account by $field|$search_method|$search_id|$extension"; &agi_output;}
$found_extension='B';
$found_priority='1';
$stmtA= "SELECT lead_id,phone_number,first_name,last_name from vicidial_list where $field='$extension' $listSQL order by last_local_call_time desc limit 1;";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
if ($sthArows > 0)
{
@aryA = $sthA->fetchrow_array;
$lead_id = $aryA[0];
$phone_number = $aryA[1];
$first_name = $aryA[2];
$last_name = $aryA[3];
$sthA->finish();
$found_extension='A';
$found_priority='1';
$confirmation=1;
### BEGIN if TTS prompt is defined, generate it and wait for "1 for yes or 2 for no" response
### if no response or no match then continue on as if nothing played
if (length($tts_prompt)>1)
{
if ($AGILOG) {$agi_string = "-- TTS : |$tts_prompt|"; &agi_output;}
$stmtA = "SELECT lead_id,entry_date,modify_date,status,user,vendor_lead_code,source_id,list_id,phone_number,title,first_name,middle_initial,last_name,address1,address2,address3,city,state,province,postal_code,country_code,gender,date_of_birth,alt_phone,email,security_phrase,comments,called_count,last_local_call_time,rank,owner FROM vicidial_list where lead_id='$lead_id';";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
if ($sthArows > 0)
{
@aryA = $sthA->fetchrow_array;
$TTS_lead_id = $aryA[0];
$TTS_entry_date = $aryA[1];
$TTS_modify_date = $aryA[2];
$TTS_status = $aryA[3];
$TTS_user = $aryA[4];
$TTS_vendor_lead_code = $aryA[5];
$TTS_source_id = $aryA[6];
$TTS_list_id = $aryA[7];
$TTS_phone_number = $aryA[8];
$TTS_title = $aryA[9];
$TTS_first_name = $aryA[10];
$TTS_middle_initial = $aryA[11];
$TTS_last_name = $aryA[12];
$TTS_address1 = $aryA[13];
$TTS_address2 = $aryA[14];
$TTS_address3 = $aryA[15];
$TTS_city = $aryA[16];
$TTS_state = $aryA[17];
$TTS_province = $aryA[18];
$TTS_postal_code = $aryA[19];
$TTS_country_code = $aryA[20];
$TTS_gender = $aryA[21];
$TTS_date_of_birth = $aryA[22];
$TTS_alt_phone = $aryA[23];
$TTS_email = $aryA[24];
$TTS_security_phrase = $aryA[25];
$TTS_comments = $aryA[26];
$TTS_called_count = $aryA[27];
$TTS_last_local_call_time = $aryA[28];
$TTS_rank = $aryA[29];
$TTS_owner = $aryA[30];
}
$sthA->finish();
$stmtA = "SELECT tts_text,tts_voice FROM vicidial_tts_prompts where tts_id='$tts_prompt';";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
if ($sthArows > 0)
{
@aryA = $sthA->fetchrow_array;
$TTS_voice = $aryA[1];
$TTS_text = $aryA[0];
### BEGIN replace variables with record values ###
$TTS_text =~ s/--A--lead_id--B--/$TTS_lead_id/gi;
$TTS_text =~ s/--A--entry_date--B--/$TTS_entry_date/gi;
$TTS_text =~ s/--A--modify_date--B--/$TTS_modify_date/gi;
$TTS_text =~ s/--A--status--B--/$TTS_status/gi;
$TTS_text =~ s/--A--user--B--/$TTS_user/gi;
$TTS_text =~ s/--A--vendor_lead_code--B--/$TTS_vendor_lead_code/gi;
$TTS_text =~ s/--A--source_id--B--/$TTS_source_id/gi;
$TTS_text =~ s/--A--list_id--B--/$TTS_list_id/gi;
$TTS_text =~ s/--A--phone_number--B--/$TTS_phone_number/gi;
$TTS_text =~ s/--A--title--B--/$TTS_title/gi;
$TTS_text =~ s/--A--first_name--B--/$TTS_first_name/gi;
$TTS_text =~ s/--A--middle_initial--B--/$TTS_middle_initial/gi;
$TTS_text =~ s/--A--last_name--B--/$TTS_last_name/gi;
$TTS_text =~ s/--A--address1--B--/$TTS_address1/gi;
$TTS_text =~ s/--A--address2--B--/$TTS_address2/gi;
$TTS_text =~ s/--A--address3--B--/$TTS_address3/gi;
$TTS_text =~ s/--A--city--B--/$TTS_city/gi;
$TTS_text =~ s/--A--state--B--/$TTS_state/gi;
$TTS_text =~ s/--A--province--B--/$TTS_province/gi;
$TTS_text =~ s/--A--postal_code--B--/$TTS_postal_code/gi;
$TTS_text =~ s/--A--country_code--B--/$TTS_country_code/gi;
$TTS_text =~ s/--A--gender--B--/$TTS_gender/gi;
$TTS_text =~ s/--A--date_of_birth--B--/$TTS_date_of_birth/gi;
$TTS_text =~ s/--A--alt_phone--B--/$TTS_alt_phone/gi;
$TTS_text =~ s/--A--email--B--/$TTS_email/gi;
$TTS_text =~ s/--A--security_phrase--B--/$TTS_security_phrase/gi;
$TTS_text =~ s/--A--comments--B--/$TTS_comments/gi;
$TTS_text =~ s/--A--called_count--B--/$TTS_called_count/gi;
$TTS_text =~ s/--A--last_local_call_time--B--/$TTS_last_local_call_time/gi;
$TTS_text =~ s/--A--rank--B--/$TTS_rank/gi;
$TTS_text =~ s/--A--owner--B--/$TTS_owner/gi;
### END replace variables with record values ###
### BEGIN check for --C-- and --D-- tags for static file replacement
if ( ($TTS_text =~ /--C--/) || ($TTS_text =~ /--D--/) )
{
$Cdash='--C--';
$Ddash='--D--';
@static_check = split(/--C--/,$TTS_text);
$sc=0;
$sr=0;
foreach(@static_check)
{
if ($sc > 0)
{
$static_check[$sc] =~ s/--D-.*//gi;
$static_check_lc = lc($static_check[$sc]);
print STDERR " Checking For Static Replacement: $static_check[$sc]|$static_check_lc$wav\n";
$static_found=0;
if (-e "/var/lib/asterisk/sounds/tts_static/$static_check_lc$wav")
{
$CDstatic = "<audio src='/var/lib/asterisk/sounds/tts_static/$static_check_lc$wav' /> ";
$static_found++;
}
else
{
if (-e "/var/lib/asterisk/sounds/$static_check_lc$wav")
{
$CDstatic = "<audio src='/var/lib/asterisk/sounds/$static_check_lc$wav' /> ";
$static_found++;
}
else
{
$firsttwo = substr($static_check_lc, 0, 2);
if (-e "/var/lib/asterisk/sounds/tts_static/$firsttwo/$static_check_lc$wav")
{
$CDstatic = "<audio src='/var/lib/asterisk/sounds/tts_static/$firsttwo/$static_check_lc$wav' /> ";
$static_found++;
}
}
}
if ($static_found > 0)
{
$CDmatch = "$Cdash$static_check[$sc]$Ddash";
$TTS_text =~ s/$CDmatch/$CDstatic/gi;
print STDERR " Static Replacement Found: $CDmatch|$CDstatic\n";
$sr++;
}
}
$sc++;
}
$TTS_text =~ s/--C--|--D--//gi;
print STDERR "\nStatic Replacements Found: $sc|$sr\n";
}
### END check for --C-- and --D-- tags for static file replacement
### filter input for CLI-safe characters
$TTS_text =~ s/[^,\.\<\>\'\/\=\_\-\: 0-9a-zA-Z]//gi;
$TTS_textRAW = $TTS_text;
$TTS_text =~ s/ /\\ /gi;
$TTS_text =~ s/\./\\./gi;
$TTS_text =~ s/\=/\\=/gi;
$TTS_text =~ s/\</\\</gi;
$TTS_text =~ s/\>/\\>/gi;
$TTS_text =~ s/\//\\\//gi;
$TTS_text =~ s/\n|\r/ /gi;
$TTS_text =~ s/\'/\\'/gi;
# $voice = 'Allison-8kHz'; # override the voice to Allison-8k
print STDERR "\nGenerating: $TTS_text - $TTS_voice\n";
### send command to generate cepstral TTS audio file
`$PATHagi/cepstral_generate.pl --debug --voice=$TTS_voice --dialog=$TTS_text >> $CEPLOGfile `;
use Digest::MD5 qw(md5_hex);
$enc = md5_hex("$TTS_textRAW-$TTS_voice"); # the hash
$enc_ftl = substr($enc, 0, 2); # first letter of hash
$TTS_filename_path = $PATHsounds . "/tts/" . $enc_ftl . "/tts-" . $enc;
print STDERR "\nfilename: $TTS_filename_path\n";
$confirm_entry_prompt = $TTS_filename_path;
$confirmation=0;
$confirmation_chances=0;
$ms_delay='6000';
while ( ($confirmation_chances < 2) && ($confirmation < 1) )
{
$confirmation_chances++;
&confirm_entry;
if ($ANSWER_confirm > 1)
{
$found_extension='B';
$found_priority='1';
$confirmation_chances=99;
if ($AGILOG) {$agi_string = "account not confirmed: $ANSWER_confirm|$lead_id"; &agi_output;}
}
if ( ($ANSWER_confirm > 0) && ($ANSWER_confirm < 2) )
{
$confirmation=1;
}
}
}
$sthA->finish();
}
### END if TTS prompt is defined, generate it and wait for "1 for yes or 2 for no" response
# YmddhhmmssLLLLLLLLLL CID format
$PADlead_id = sprintf("%010s", $lead_id); while (length($PADlead_id) > 10) {chop($PADlead_id);}
$YqueryCID = "Y$CIDdate$PADlead_id";
$Ynewcallerid = "\"$YqueryCID <$callerid>\"";
if ($AGILOG) {$agi_string = "account found: $lead_id|$phone_number|$first_name|$last_name|$Ynewcallerid"; &agi_output;}
$AGI->set_callerid($Ynewcallerid);
if ($confirmation > 0)
{
$phone_update_SQL='';
$user_update_SQL='';
$uniqueid_address3_SQL='';
$title_update_SQL='';
if ($phone_update =~ /Y/i)
{
if ( (length($callerid) > 6) && ($callerid !~ /unknown|private/i) )
{$phone_update_SQL = ",phone_number='$callerid'";}
else
{
if ($AGILOG) {$agi_string = "-- CMLOOKUP vl record failed: Invalid CIDnumber - $callerid|$phone_number"; &agi_output;}
}
}
if ($uniqueid_address3 =~ /Y/)
{$uniqueid_address3_SQL = ",address3='$uniqueid'";}
if ( (length($call_var_title) > 0) && ($title_update =~ /Y/i) )
{$title_update_SQL = ",title='$call_var_title'";}
if (length($agent_user_id) > 1)
{
if ($agent_id_update =~ /COMMENT/i)
{$user_update_SQL = ",comments='$agent_user_id'";}
if ($agent_id_update =~ /Y/i)
{$user_update_SQL = ",user='$agent_user_id'";}
}
$stmtA = "UPDATE vicidial_list SET modify_date=NOW() $phone_update_SQL $user_update_SQL $uniqueid_address3_SQL $title_update_SQL where lead_id='$lead_id';";
$affected_rows = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "-- CMLOOKUP vl record updated: |$affected_rows| $lead_id|$callerid|$phone_number|$first_name|$last_name|$agent_user_id|$call_var_title|$uniqueid|"; &agi_output;}
}
}
else
{
$sthA->finish();
$INVCOUNT = $AGI->get_variable(INVCOUNT);
$INVCOUNT = ($INVCOUNT + 1);
$INVCOUNT_set = $AGI->set_variable('INVCOUNT', "$INVCOUNT");
if ($AGILOG) {$agi_string = "account not found: $INVCOUNT"; &agi_output;}
}
$AGI->stream_file('sip-silence');
$AGI->stream_file('sip-silence');
$AGI->stream_file('sip-silence');
if ($AGILOG) {$agi_string = "exiting the cm_lookup.agi app, transferring call to $found_extension @ $context + $found_priority"; &agi_output;}
print "SET CONTEXT $context\n";
$result = <STDIN>;
checkresult($result);
print "SET EXTENSION $found_extension\n";
$result = <STDIN>;
checkresult($result);
print "SET PRIORITY $found_priority\n";
$result = <STDIN>;
checkresult($result);
exit;
######### SUBROUTINES #################
sub checkresult
{
$pass=0; $fail=0;
my ($res) = @_;
my $retval;
$tests++;
chomp $res;
if ($res =~ /^200/)
{
$res =~ /result=(-?\d+)/;
if (!length($1))
{
# print STDERR "FAIL ($res)\n";
$fail++;
}
else
{
# print STDERR "PASS ($1)\n";
$pass++;
}
}
else
{
# print STDERR "FAIL (unexpected result '$res')\n";
$fail++;
}
}
sub agi_output
{
if ($AGILOG >=2)
{
### open the log file for writing ###
open(Lout, ">>$AGILOGfile")
|| die "Can't open $AGILOGfile: $!\n";
print Lout "$now_date|$script|$agi_string\n";
close(Lout);
}
### send to STDERR writing ###
if ( ($AGILOG == '1') || ($AGILOG == '3') )
{print STDERR "$now_date|$script|$agi_string\n";}
$agi_string='';
}
##### BEGIN confirm entry, 1 or 2 ######################################
sub confirm_entry
{
$interrupt_digit='';
$audio_file = "$confirm_entry_prompt";
if ($language =~ /es/)
{
$file_extension_gsm='.gsm';
$file_extension_wav='.wav';
$audio_suffix='_es';
if ( (-e ("$PATHsounds/$audio_file$audio_suffix$file_extension_gsm")) || (-e ("$PATHsounds/$audio_file$audio_suffix$file_extension_wav")) )
{$donothing=1;}
else
{$audio_suffix='';}
}
else
{$audio_suffix='';}
$interrupt_digit = $AGI->stream_file("$audio_file$audio_suffix",'12');
print STDERR "interrupt_digit |$interrupt_digit|\n";
$digits_being_entered=1;
$ANSWER_confirm='';
if ($interrupt_digit > 0)
{
# if ($interrupt_digit == 35) {$interrupt_digit='#';}
# if ($interrupt_digit == 42) {$interrupt_digit='*';}
if ($interrupt_digit == 48) {$interrupt_digit=0;}
if ($interrupt_digit == 49) {$interrupt_digit=1;}
if ($interrupt_digit == 50) {$interrupt_digit=2;}
if ($interrupt_digit == 51) {$interrupt_digit=3;}
if ($interrupt_digit == 52) {$interrupt_digit=4;}
if ($interrupt_digit == 53) {$interrupt_digit=5;}
if ($interrupt_digit == 54) {$interrupt_digit=6;}
if ($interrupt_digit == 55) {$interrupt_digit=7;}
if ($interrupt_digit == 56) {$interrupt_digit=8;}
if ($interrupt_digit == 57) {$interrupt_digit=9;}
$ANSWER_confirm=$interrupt_digit;
}
if (length($ANSWER_confirm) < 1)
{
$digit_loop_counter=0;
while ( ($digits_being_entered > 0) && ($digit_loop_counter < 1) )
{
$digit = chr($AGI->wait_for_digit("$ms_delay")); # wait 10 seconds for input or until the pound key is pressed
if ($digit =~ /\d/)
{
$ANSWER_confirm = "$ANSWER_confirm$digit";
print STDERR "digit |$digit| ANSWER_confirm |$ANSWER_confirm|\n";
# $AGI->say_digits("$digit");
undef $digit;
}
else
{
$digits_being_entered=0;
}
$digit_loop_counter++;
}
}
$ANSWER_confirm =~ s/\D//gi;
if ($ANSWER_confirm) {print STDERR "digit |$digit| ANSWER_confirm |$ANSWER_confirm|\n";}
}
##### END confirm entry ######################################
@@ -0,0 +1,390 @@
#!/usr/bin/perl
#
# cm_speak_var.agi version 2.10
#
# runs after a call has lead_id assigned to it, speaks letters/numbers
#
# You need to put lines similar to those below in your Custom Dialplan:
#
# exten => 8412,1,Answer
# exten => 8412,n,AGI(cm_speak_var.agi,say_digits---security_phrase)
# exten => 8412,n,Hangup
#
# CLI FLAG OPTIONS:
# 1. function: (say_digits, say_number, say_alpha, say_datetime, say_phonetic, stream_file)
# 2. field: (any vicidial_list field)
#
# Copyright (C) 2014 Matt Florell <vicidial@gmail.com> LICENSE: AGPLv2
#
# changes:
# 140619-2005 - first build
#
$script = 'cm_speak_var.agi';
$CM_LOG = 1; # set to 1 for logfile
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year = ($year + 1900);
$mon++;
if ($mon < 10) {$mon = "0$mon";}
if ($mday < 10) {$mday = "0$mday";}
if ($hour < 10) {$hour = "0$hour";}
if ($min < 10) {$min = "0$min";}
if ($sec < 10) {$sec = "0$sec";}
$now_date_epoch = time();
$now_date = "$year-$mon-$mday $hour:$min:$sec";
# default path to astguiclient configuration file:
$PATHconf = '/etc/astguiclient.conf';
open(conf, "$PATHconf") || die "can't open $PATHconf: $!\n";
@conf = <conf>;
close(conf);
$i=0;
foreach(@conf)
{
$line = $conf[$i];
$line =~ s/ |>|\n|\r|\t|\#.*|;.*//gi;
if ( ($line =~ /^PATHhome/) && ($CLIhome < 1) )
{$PATHhome = $line; $PATHhome =~ s/.*=//gi;}
if ( ($line =~ /^PATHlogs/) && ($CLIlogs < 1) )
{$PATHlogs = $line; $PATHlogs =~ s/.*=//gi;}
if ( ($line =~ /^PATHagi/) && ($CLIagi < 1) )
{$PATHagi = $line; $PATHagi =~ s/.*=//gi;}
if ( ($line =~ /^PATHweb/) && ($CLIweb < 1) )
{$PATHweb = $line; $PATHweb =~ s/.*=//gi;}
if ( ($line =~ /^PATHsounds/) && ($CLIsounds < 1) )
{$PATHsounds = $line; $PATHsounds =~ s/.*=//gi;}
if ( ($line =~ /^PATHmonitor/) && ($CLImonitor < 1) )
{$PATHmonitor = $line; $PATHmonitor =~ s/.*=//gi;}
if ( ($line =~ /^VARserver_ip/) && ($CLIserver_ip < 1) )
{$VARserver_ip = $line; $VARserver_ip =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_server/) && ($CLIDB_server < 1) )
{$VARDB_server = $line; $VARDB_server =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_database/) && ($CLIDB_database < 1) )
{$VARDB_database = $line; $VARDB_database =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_user/) && ($CLIDB_user < 1) )
{$VARDB_user = $line; $VARDB_user =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_pass/) && ($CLIDB_pass < 1) )
{$VARDB_pass = $line; $VARDB_pass =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_port/) && ($CLIDB_port < 1) )
{$VARDB_port = $line; $VARDB_port =~ s/.*=//gi;}
$i++;
}
if (!$VARDB_port) {$VARDB_port='3306';}
if (!$CMLOGfile) {$CMLOGfile = "$PATHlogs/cmout.$year-$mon-$mday";}
if (!$AGILOGfile) {$AGILOGfile = "$PATHlogs/agiout.$year-$mon-$mday";}
use DBI;
use Asterisk::AGI;
$AGI = new Asterisk::AGI;
$dbhA = DBI->connect("DBI:mysql:$VARDB_database:$VARDB_server:$VARDB_port", "$VARDB_user", "$VARDB_pass")
or die "Couldn't connect to database: " . DBI->errstr;
### Grab Server values from the database
$stmtA = "SELECT agi_output,asterisk_version,ext_context FROM servers where server_ip = '$VARserver_ip';";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
if ($sthArows > 0)
{
$AGILOG = '0';
@aryA = $sthA->fetchrow_array;
$DBagi_output = $aryA[0];
$asterisk_version = $aryA[1];
$DBext_context = $aryA[2];
if ($DBext_context) {$ext_context = $DBext_context;}
if ($DBagi_output =~ /STDERR/) {$AGILOG = '1';}
if ($DBagi_output =~ /FILE/) {$AGILOG = '2';}
if ($DBagi_output =~ /BOTH/) {$AGILOG = '3';}
}
$sthA->finish();
### begin parsing run-time options ###
if (length($ARGV[0])>1)
{
if ($AGILOG)
{$agi_string = "Perl Environment Dump:"; &agi_output;}
$i=0;
while ($#ARGV >= $i)
{
$args = "$args $ARGV[$i]";
if ($AGILOG) {$agi_string = "$i|$ARGV[$i]"; &agi_output;}
$i++;
}
### list of command-line array arguments:
@ARGV_vars = split(/---/, $ARGV[0]);
$CLI_function = $ARGV_vars[0];
$CLI_field = $ARGV_vars[1];
}
$|=1;
while(<STDIN>)
{
chomp;
last unless length($_);
if ($AGILOG)
{
if (/^agi_(\w+)\:\s+(.*)$/)
{
$AGI{$1} = $2;
}
}
if (/^agi_uniqueid\:\s+(.*)$/) {$unique_id = $1; $uniqueid = $unique_id;}
if (/^agi_priority\:\s+(.*)$/) {$priority = $1;}
if (/^agi_channel\:\s+(.*)$/) {$channel = $1;}
if (/^agi_extension\:\s+(.*)$/) {$extension = $1;}
if (/^agi_type\:\s+(.*)$/) {$type = $1;}
if (/^agi_callerid\:\s+(.*)$/) {$callerid = $1; $calleridnum = $callerid;}
if (/^agi_calleridname\:\s+(.*)$/) {$calleridname = $1;}
}
if ( (length($callerid)>20) && ($callerid =~ /\"\S\S\S\S\S\S\S\S\S\S\S\S\S\S\S\S\S\S/) )
{
$callerid =~ s/^\"//gi;
$callerid =~ s/\".*$//gi;
}
if ( (
(length($calleridname)>5) && ( (!$callerid) or ($callerid =~ /unknown|private|00000000/i) or ($callerid =~ /5551212/) )
) or ( (length($calleridname)>17) && ($calleridname =~ /\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d/) ) )
{
$callerid = $calleridname;
}
if ($AGILOG) {$agi_string = "AGI Environment Dump:"; &agi_output;}
foreach $i (sort keys %AGI)
{
if ($AGILOG) {$agi_string = " -- $i = $AGI{$i}"; &agi_output;}
}
if ($AGILOG) {$agi_string = "AGI Variables: |$unique_id|$channel|$extension|$type|$callerid|"; &agi_output;}
$callerid =~ s/\"//gi;
$callerid =~ s/ .*//gi;
$CIDlead_id = $callerid;
if ( ($calleridname =~ /^DC/) && ($calleridname =~ /\d\d\d\d\d\d\d\d\d\dW$/) ) #DC234728W0001010141W
{$CIDlead_id = substr($calleridname, 9, 10);}
else
{$CIDlead_id = substr($CIDlead_id, 10, 10);}
$CIDlead_id = ($CIDlead_id + 0);
if ( ($CLIlead_id > 0) && ($CIDlead_id < 1) ) {$CIDlead_id = $CLIlead_id;}
$VD_lead_id = $CIDlead_id;
if ($AGILOG) {$agi_string = "+++++ cm_speak_var START : |$CLI_function|$CLI_field|$CIDlead_id|$now_date|$AST_ver|$priority|$calleridname|"; &agi_output;}
if ($CM_LOG)
{
open(Lout, ">>$CMLOGfile")
|| die "Can't open $CMLOGfile: $!\n";
print Lout "$now_date|$VD_lead_id| |$CLI_function|$CLI_field|$calleridnum|$callerid|\n";
close(Lout);
}
$AGI->stream_file('sip-silence');
$AGI->stream_file('sip-silence');
$stmtA = "SELECT lead_id,entry_date,modify_date,status,user,vendor_lead_code,source_id,list_id,phone_number,title,first_name,middle_initial,last_name,address1,address2,address3,city,state,province,postal_code,country_code,gender,date_of_birth,alt_phone,email,security_phrase,comments,called_count,last_local_call_time,rank,owner FROM vicidial_list where lead_id='$VD_lead_id';";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
if ($sthArows > 0)
{
@aryA = $sthA->fetchrow_array;
$CM_lead_id = $aryA[0];
$CM_entry_date = $aryA[1];
$CM_modify_date = $aryA[2];
$CM_status = $aryA[3];
$CM_user = $aryA[4];
$CM_vendor_lead_code = $aryA[5];
$CM_source_id = $aryA[6];
$CM_list_id = $aryA[7];
$CM_phone_number = $aryA[8];
$CM_title = $aryA[9];
$CM_first_name = $aryA[10];
$CM_middle_initial = $aryA[11];
$CM_last_name = $aryA[12];
$CM_address1 = $aryA[13];
$CM_address2 = $aryA[14];
$CM_address3 = $aryA[15];
$CM_city = $aryA[16];
$CM_state = $aryA[17];
$CM_province = $aryA[18];
$CM_postal_code = $aryA[19];
$CM_country_code = $aryA[20];
$CM_gender = $aryA[21];
$CM_date_of_birth = $aryA[22];
$CM_alt_phone = $aryA[23];
$CM_email = $aryA[24];
$CM_security_phrase = $aryA[25];
$CM_comments = $aryA[26];
$CM_called_count = $aryA[27];
$CM_last_local_call_time = $aryA[28];
$CM_rank = $aryA[29];
$CM_owner = $aryA[30];
}
$sthA->finish();
if ($CLI_field =~ /lead_id/) {$field_value = $CM_lead_id;}
if ($CLI_field =~ /entry_date/) {$field_value = $CM_entry_date;}
if ($CLI_field =~ /modify_date/) {$field_value = $CM_modify_date;}
if ($CLI_field =~ /status/) {$field_value = $CM_status;}
if ($CLI_field =~ /user/) {$field_value = $CM_user;}
if ($CLI_field =~ /vendor_lead_code/) {$field_value = $CM_vendor_lead_code;}
if ($CLI_field =~ /source_id/) {$field_value = $CM_source_id;}
if ($CLI_field =~ /list_id/) {$field_value = $CM_list_id;}
if ($CLI_field =~ /phone_number/) {$field_value = $CM_phone_number;}
if ($CLI_field =~ /title/) {$field_value = $CM_title;}
if ($CLI_field =~ /first_name/) {$field_value = $CM_first_name;}
if ($CLI_field =~ /middle_initial/) {$field_value = $CM_middle_initial;}
if ($CLI_field =~ /last_name/) {$field_value = $CM_last_name;}
if ($CLI_field =~ /address1/) {$field_value = $CM_address1;}
if ($CLI_field =~ /address2/) {$field_value = $CM_address2;}
if ($CLI_field =~ /address3/) {$field_value = $CM_address3;}
if ($CLI_field =~ /city/) {$field_value = $CM_city;}
if ($CLI_field =~ /state/) {$field_value = $CM_state;}
if ($CLI_field =~ /province/) {$field_value = $CM_province;}
if ($CLI_field =~ /postal_code/) {$field_value = $CM_postal_code;}
if ($CLI_field =~ /country_code/) {$field_value = $CM_country_code;}
if ($CLI_field =~ /gender/) {$field_value = $CM_gender;}
if ($CLI_field =~ /date_of_birth/) {$field_value = $CM_date_of_birth;}
if ($CLI_field =~ /alt_phone/) {$field_value = $CM_alt_phone;}
if ($CLI_field =~ /email/) {$field_value = $CM_email;}
if ($CLI_field =~ /security_phrase/) {$field_value = $CM_security_phrase;}
if ($CLI_field =~ /comments/) {$field_value = $CM_comments;}
if ($CLI_field =~ /called_count/) {$field_value = $CM_called_count;}
if ($CLI_field =~ /last_local_call_time/) {$field_value = $CM_last_local_call_time;}
if ($CLI_field =~ /rank/) {$field_value = $CM_rank;}
if ($CLI_field =~ /owner/) {$field_value = $CM_owner;}
if ($CLI_function =~ /say_digits/)
{
$AGI->say_digits("$field_value");
}
if ($CLI_function =~ /say_number/)
{
$AGI->say_number("$field_value");
}
if ($CLI_function =~ /say_alpha/)
{
$AGI->say_alpha("$field_value");
}
if ($CLI_function =~ /say_datetime/)
{
$AGI->say_datetime("$field_value");
}
if ($CLI_function =~ /say_phonetic/)
{
$AGI->say_phonetic("$field_value");
}
if ($CLI_function =~ /stream_file/)
{
$AGI->stream_file("$field_value");
}
$dbhA->disconnect();
#$AGI->hangup($channel);
exit;
sub checkresult
{
my ($res) = @_;
my $retval;
$tests++;
chomp $res;
if ($res =~ /^200/)
{
$res =~ /result=(-?\d+)/;
if (!length($1))
{
# print STDERR "FAIL ($res)\n";
$fail++;
}
else
{
# print STDERR "PASS ($1)\n";
$pass++;
}
}
else
{
# print STDERR "FAIL (unexpected result '$res')\n";
$fail++;
}
}
sub agi_output
{
if ($AGILOG >=2)
{
### open the log file for writing ###
open(Lout, ">>$AGILOGfile")
|| die "Can't open $AGILOGfile: $!\n";
print Lout "$now_date|$script|$agi_string\n";
close(Lout);
}
### send to STDERR writing ###
if ( ($AGILOG == '1') || ($AGILOG == '3') )
{print STDERR "$now_date|$script|$agi_string\n";}
$agi_string='';
}
# subroutine to parse the asterisk version
# and return a hash with the various part
sub parse_asterisk_version
{
# grab the arguments
my $ast_ver_str = $_[0];
# get everything after the - and put it in $ast_ver_postfix
my @hyphen_parts = split( /-/ , $ast_ver_str );
my $ast_ver_postfix = $hyphen_parts[1];
# now split everything before the - up by the .
my @dot_parts = split( /\./ , $hyphen_parts[0] );
my %ast_ver_hash;
if ( $dot_parts[0] <= 1 )
{
%ast_ver_hash = (
"major" => $dot_parts[0],
"minor" => $dot_parts[1],
"build" => $dot_parts[2],
"revision" => $dot_parts[3],
"postfix" => $ast_ver_postfix
);
}
# digium dropped the 1 from asterisk 10 but we still need it
if ( $dot_parts[0] > 1 )
{
%ast_ver_hash = (
"major" => 1,
"minor" => $dot_parts[0],
"build" => $dot_parts[1],
"revision" => $dot_parts[2],
"postfix" => $ast_ver_postfix
);
}
return ( %ast_ver_hash );
}
@@ -0,0 +1,352 @@
#!/usr/bin/perl
#
# cm_status_dial.agi version 2.6
#
# Used as a Call Menu AGI option route to alter status of a lead before dialing.
# The lead is only updated if the call is tagged with a lead_id before this script.
#
# You need to put lines similar to those below in your extensions.conf file:
# ; Below are the parameters needed for the script to be run properly
# ; 1. the status to change the lead to
# ; 2. the phone number or extension to dial
# ; 3. the context to dial in
# ;
# ;inbound calls check calltime:
#cm_status_dial.agi,XFER-----917275551212-----default
#
# Copyright (C) 2013 Matt Florell <vicidial@gmail.com> LICENSE: AGPLv2
#
# changes:
# 120909-2301 - First Build
# 120924-0637 - Added log updating
# 130108-1820 - Changes for Asterisk 1.8 compatibility
#
$script = 'cm_status_dial.agi';
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year = ($year + 1900);
$mon++;
if ($mon < 10) {$mon = "0$mon";}
if ($mday < 10) {$mday = "0$mday";}
if ($hour < 10) {$hour = "0$hour";}
if ($min < 10) {$min = "0$min";}
if ($sec < 10) {$sec = "0$sec";}
$now_date_epoch = time();
$now_date = "$year-$mon-$mday $hour:$min:$sec";
# default path to astguiclient configuration file:
$PATHconf = '/etc/astguiclient.conf';
open(conf, "$PATHconf") || die "can't open $PATHconf: $!\n";
@conf = <conf>;
close(conf);
$i=0;
foreach(@conf)
{
$line = $conf[$i];
$line =~ s/ |>|\n|\r|\t|\#.*|;.*//gi;
if ( ($line =~ /^PATHhome/) && ($CLIhome < 1) )
{$PATHhome = $line; $PATHhome =~ s/.*=//gi;}
if ( ($line =~ /^PATHlogs/) && ($CLIlogs < 1) )
{$PATHlogs = $line; $PATHlogs =~ s/.*=//gi;}
if ( ($line =~ /^PATHagi/) && ($CLIagi < 1) )
{$PATHagi = $line; $PATHagi =~ s/.*=//gi;}
if ( ($line =~ /^PATHweb/) && ($CLIweb < 1) )
{$PATHweb = $line; $PATHweb =~ s/.*=//gi;}
if ( ($line =~ /^PATHsounds/) && ($CLIsounds < 1) )
{$PATHsounds = $line; $PATHsounds =~ s/.*=//gi;}
if ( ($line =~ /^PATHmonitor/) && ($CLImonitor < 1) )
{$PATHmonitor = $line; $PATHmonitor =~ s/.*=//gi;}
if ( ($line =~ /^VARserver_ip/) && ($CLIserver_ip < 1) )
{$VARserver_ip = $line; $VARserver_ip =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_server/) && ($CLIDB_server < 1) )
{$VARDB_server = $line; $VARDB_server =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_database/) && ($CLIDB_database < 1) )
{$VARDB_database = $line; $VARDB_database =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_user/) && ($CLIDB_user < 1) )
{$VARDB_user = $line; $VARDB_user =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_pass/) && ($CLIDB_pass < 1) )
{$VARDB_pass = $line; $VARDB_pass =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_port/) && ($CLIDB_port < 1) )
{$VARDB_port = $line; $VARDB_port =~ s/.*=//gi;}
$i++;
}
if (!$VARDB_port) {$VARDB_port='3306';}
if (!$AGILOGfile) {$AGILOGfile = "$PATHlogs/agiout.$year-$mon-$mday";}
use DBI;
use Time::HiRes ('gettimeofday','usleep','sleep'); # necessary to have perl sleep command of less than one second
use Asterisk::AGI;
$AGI = new Asterisk::AGI;
$dbhA = DBI->connect("DBI:mysql:$VARDB_database:$VARDB_server:$VARDB_port", "$VARDB_user", "$VARDB_pass")
or die "Couldn't connect to database: " . DBI->errstr;
### Grab Server values from the database
$stmtA = "SELECT agi_output FROM servers where server_ip = '$VARserver_ip';";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
if ($sthArows > 0)
{
$AGILOG = '0';
@aryA = $sthA->fetchrow_array;
$DBagi_output = "$aryA[0]";
if ($DBagi_output =~ /STDERR/) {$AGILOG = '1';}
if ($DBagi_output =~ /FILE/) {$AGILOG = '2';}
if ($DBagi_output =~ /BOTH/) {$AGILOG = '3';}
}
$sthA->finish();
#############################################
##### START QUEUEMETRICS LOGGING LOOKUP #####
$stmtA = "SELECT enable_queuemetrics_logging,queuemetrics_server_ip,queuemetrics_dbname,queuemetrics_login,queuemetrics_pass,queuemetrics_log_id,queuemetrics_eq_prepend FROM system_settings;";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
if ($sthArows > 0)
{
@aryA = $sthA->fetchrow_array;
$enable_queuemetrics_logging = $aryA[0];
$queuemetrics_server_ip = $aryA[1];
$queuemetrics_dbname = $aryA[2];
$queuemetrics_login= $aryA[3];
$queuemetrics_pass = $aryA[4];
$queuemetrics_log_id = $aryA[5];
$queuemetrics_eq_prepend = $aryA[6];
}
$sthA->finish();
##### END QUEUEMETRICS LOGGING LOOKUP #####
###########################################
### begin parsing run-time options ###
if (length($ARGV[0])>1)
{
if ($AGILOG) {$agi_string = "Perl Environment Dump:"; &agi_output;}
$i=0;
while ($#ARGV >= $i)
{
$args = "$args $ARGV[$i]";
if ($AGILOG) {$agi_string = "$i|$ARGV[$i]"; &agi_output;}
$i++;
}
### list of command-line array arguments:
@ARGV_vars = split(/-----/, $ARGV[0]);
$new_status = $ARGV_vars[0];
$dial_extension = $ARGV_vars[1];
$dial_context = $ARGV_vars[2];
}
$|=1;
while(<STDIN>)
{
chomp;
last unless length($_);
if ($AGILOG)
{
if (/^agi_(\w+)\:\s+(.*)$/)
{
$AGI{$1} = $2;
}
}
if (/^agi_context\:\s+(.*)$/) {$context = $1;}
if (/^agi_priority\:\s+(.*)$/) {$priority = $1;}
if (/^agi_uniqueid\:\s+(.*)$/) {$unique_id = $1; $uniqueid = $unique_id;}
if (/^agi_channel\:\s+(.*)$/) {$channel = $1;}
if (/^agi_extension\:\s+(.*)$/) {$extension = $1;}
if (/^agi_type\:\s+(.*)$/) {$type = $1;}
if (/^agi_callerid\:\s+(.*)$/) {$callerid = $1;}
if (/^agi_calleridname\:\s+(.*)$/) {$calleridname = $1;}
}
$outboundIVR=0;
$fieldUPDATE=0;
if ($calleridname =~ /^V\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d/)
{
$phone_number = $callerid;
$callerid = $calleridname;
$outboundIVR=1;
$stmtA = "SELECT campaign_id,lead_id FROM vicidial_auto_calls where callerid='$callerid';";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
if ($sthArows > 0)
{
@aryA = $sthA->fetchrow_array;
$campaign_id = $aryA[0];
$lead_id = $aryA[1];
}
$sthA->finish();
}
elsif ($calleridname =~ /^Y\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d/)
{
$lead_id = substr($calleridname, 10, 10);
$lead_id = ($lead_id + 0);
$phone_number = $callerid;
$callerid = $calleridname;
$fieldUPDATE=1;
}
else
{
if ( ($callerid =~ /\".*\"/) && ( (!$calleridname) or ($calleridname =~ /unknown/) ) )
{
$calleridname = $callerid;
$calleridname =~ s/\<\d\d\d\d\d\d\d\d\d\d\>//gi;
$calleridname =~ s/\"|\" //gi;
}
$callerid =~ s/\D|\'//gi;
$calleridname =~ s/unknown|\'//gi;
if ( (!$callerid) or ($callerid =~ /unknown/) )
{$callerid = $calleridname;}
if (length($callerid)>0) {$phone_number = $callerid;}
else {$phone_number = '';}
if (length($calleridname)>0) {$VLcomments = $calleridname;}
else {$VLcomments = '';}
}
if (length($callerid)<8) {$callerid = $parked_by;}
if (length($pin)>0) {$callerid = $pin;}
foreach $i (sort keys %AGI)
{
if ($AGILOG) {$agi_string = " -- $i = $AGI{$i}"; &agi_output;}
}
if ($AGILOG) {$agi_string = "AGI Variables: |$unique_id|$channel|$extension|$type|$callerid|"; &agi_output;}
if (length($pin) < 1) {$pin=$inbound_number;}
$fronter = $pin;
if ($AGILOG) {$agi_string = "+++++ STATUS DIAL LOG STARTED : |$channel_group|$callerid-$pin|$now_date"; &agi_output;}
$VDADphone='';
$VDADphone_code='';
if ($channel =~ /Local/i)
{
if ($AGILOG) {$agi_string = "+++++ STATUS DIAL START LOCAL CHANNEL: EXITING- $priority"; &agi_output;}
exit;
}
### insert an IVR record to the vicidial_auto_calls table
if ($outboundIVR > 0)
{
$stmtA = "UPDATE vicidial_list SET status='$new_status' where lead_id='$lead_id';";
if ($AGILOG) {$agi_string = "|$stmtA|"; &agi_output;}
$affected_rowsL = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "-- VL update: |$affected_rowsL|$new_status|$dtmf|$lead_id|$context|"; &agi_output;}
$stmtA = "UPDATE vicidial_log SET status='$new_status' where lead_id='$lead_id' order by call_date desc limit 1;";
if ($AGILOG) {$agi_string = "|$stmtA|"; &agi_output;}
$affected_rowsL = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "-- VLOG update: |$affected_rowsL|$new_status|$dtmf|$lead_id|$context|"; &agi_output;}
}
else
{
if ($fieldUPDATE > 0)
{
$stmtA = "UPDATE vicidial_list SET status='$new_status' where lead_id='$lead_id';";
if ($AGILOG) {$agi_string = "|$stmtA|"; &agi_output;}
$affected_rowsL = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "-- VL update: |$affected_rowsL|$new_status|$dtmf|$lead_id|$context|"; &agi_output;}
$stmtA = "UPDATE vicidial_closer_log SET status='$new_status' where lead_id='$lead_id' order by call_date desc limit 1;";
if ($AGILOG) {$agi_string = "|$stmtA|"; &agi_output;}
$affected_rowsL = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "-- VCLOG update: |$affected_rowsL|$new_status|$dtmf|$lead_id|$context|"; &agi_output;}
}
}
### QueueMetrics logging if enabled
if ($enable_queuemetrics_logging > 0)
{
$dbhB = DBI->connect("DBI:mysql:$queuemetrics_dbname:$queuemetrics_server_ip:3306", "$queuemetrics_login", "$queuemetrics_pass")
or die "Couldn't connect to database: " . DBI->errstr;
$stmtB = "UPDATE queue_log SET data1='$new_status' where call_id='$calleridname' and verb='CALLSTATUS' order by time_id desc limit 1;";
if ($AGILOG) {$agi_string = "|$stmtB|"; &agi_output;}
$Baffected_rows = $dbhB->do($stmtB);
$dbhB->disconnect();
if ($DBX) {print "QueueMetrics UPDATE: $queuemetrics_server_ip|$queuemetrics_dbname|$calleridname|$new_status\n";}
}
$sthA->finish();
$dbhA->disconnect();
if ($AGILOG) {$agi_string = " Status Dial, transferring call |$dial_extension|$dial_context|$lead_id|$calleridname|"; &agi_output;}
print "SET CONTEXT $dial_context\n";
$result = <STDIN>;
checkresult($result);
print "SET EXTENSION $dial_extension\n";
$result = <STDIN>;
checkresult($result);
print "SET PRIORITY 1\n";
$result = <STDIN>;
checkresult($result);
exit;
sub agi_output
{
if ($AGILOG >=2)
{
### open the log file for writing ###
open(Lout, ">>$AGILOGfile")
|| die "Can't open $AGILOGfile: $!\n";
print Lout "$now_date|$script|$agi_string\n";
close(Lout);
}
### send to STDERR writing ###
if ( ($AGILOG == '1') || ($AGILOG == '3') )
{print STDERR "$now_date|$script|$agi_string\n";}
$agi_string='';
}
sub checkresult
{
my ($res) = @_;
my $retval;
$tests++;
chomp $res;
if ($res =~ /^200/)
{
$res =~ /result=(-?\d+)/;
if (!length($1))
{
# print STDERR "FAIL ($res)\n";
$fail++;
}
else
{
# print STDERR "PASS ($1)\n";
$pass++;
}
}
else
{
# print STDERR "FAIL (unexpected result '$res')\n";
$fail++;
}
}
@@ -0,0 +1,123 @@
#!/usr/bin/perl
#
# debug_speak.agi version 2.0.5
#
# runs during any extension and speaks the extension sent to it while
# printing the extension as well as lots of info about the call to STDERR
#
# You need to put lines similar to those below in your extensions.conf file:
#
# ;inbound errors with speaking of extension:
#exten => _XXXX,1,AGI(debug_speak.agi,${EXTEN})
#exten => _XXXX,3,Hangup
#
# Copyright (C) 2008 Matt Florell <vicidial@gmail.com> LICENSE: AGPLv2
#
$V = 1; # set to 1 for verbose mode
$CLIOUT = 1; # set to 1 for printing 1 line to STDERR
$SPEAK = 1; # set to 1 for saying invalid digits
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year = ($year + 1900);
$mon++;
if ($mon < 10) {$mon = "0$mon";}
if ($mday < 10) {$mday = "0$mday";}
if ($hour < 10) {$hour = "0$hour";}
if ($min < 10) {$min = "0$min";}
if ($sec < 10) {$sec = "0$sec";}
$now_date_epoch = time();
$now_date = "$year-$mon-$mday $hour:$min:$sec";
### begin parsing run-time options ###
if (length($ARGV[0])>1)
{
if ($CLIOUT) {print STDERR "Perl Environment Dump:\n";}
$i=0;
while ($#ARGV >= $i)
{
$args = "$args $ARGV[$i]";
if ($CLIOUT) {print STDERR "$i|$ARGV[$i]|\n";}
$i++;
}
if ($args =~ /--help/i)
{
print "allowed run time options:\n [-q] = quiet\n [-t] = test\n [-debug] = verbose debug messages\n\n";
}
else
{
if ($args =~ /-V/i)
{
$V=1;
}
if ($args =~ /-debug/i)
{
$DG=1;
}
if ($args =~ /-dbAVS/i)
{
$DGA=1;
}
if ($args =~ /-q/i)
{
$q=1;
$Q=1;
}
if ($args =~ /-t/i)
{
$TEST=1;
$T=1;
}
}
}
else
{
if ($V) {print "no command line options set\n";}
}
use Asterisk::AGI;
$AGI = new Asterisk::AGI;
$|=1;
while(<STDIN>) {
chomp;
last unless length($_);
if ($V)
{
if (/^agi_(\w+)\:\s+(.*)$/)
{
$AGI{$1} = $2;
}
}
if (/^agi_uniqueid\:\s+(.*)$/) {$unique_id = $1;}
if (/^agi_channel\:\s+(.*)$/) {$channel = $1;}
if (/^agi_extension\:\s+(.*)$/) {$extension = $1;}
if (/^agi_type\:\s+(.*)$/) {$type = $1;}
if (/^agi_callerid\:\s+(.*)$/) {$callerid = $1;}
if (/^agi_calleridname\:\s+(.*)$/) {$calleridname = $1;}
}
if ($V)
{
if ($V) {print STDERR "AGI Environment Dump:\n";}
foreach $i (sort keys %AGI) {
if ($V) {print STDERR " -- $i = $AGI{$i}\n";}
}
}
if ($CLIOUT) {print STDERR "****** DEBUG: |$unique_id|$channel|$extension|$type|$callerid|$calleridname|\n";}
if ($SPEAK)
{
$AGI->stream_file('beep');
sleep(1);
$AGI->say_digits("$extension");
}
exit;
@@ -0,0 +1,139 @@
#!/usr/bin/perl
#
# invalid_speak.agi version 2.0.5
#
# runs during an invalid extension and speaks the extension sent to it while printing to STDERR
#
# You need to put lines similar to those below in your extensions.conf file:
#
# ;inbound errors with speaking of extension:
#exten => _XXXX,1,AGI(invalid_speak.agi,${EXTEN})
#exten => _XXXX,2,Playback(invalid)
#exten => _XXXX,3,Hangup
#
#exten => _*XXXXXXXXXX*XXXX,1,AGI(invalid_speak.agi,${EXTEN})
#exten => _*XXXXXXXXXX*XXXX,2,Playback(invalid)
#exten => _*XXXXXXXXXX*XXXX,3,Hangup
#
#exten => _*XXXXXXXXXX*XXXX*,1,AGI(invalid_speak.agi,${EXTEN})
#exten => _*XXXXXXXXXX*XXXX*,2,Playback(invalid)
#exten => _*XXXXXXXXXX*XXXX*,3,Hangup
#
#exten => _**XXXX,1,AGI(invalid_speak.agi,${EXTEN})
#exten => _**XXXX,2,Playback(invalid)
#exten => _**XXXX,3,Hangup
#
#exten => _**XXXX*,1,AGI(invalid_speak.agi,${EXTEN})
#exten => _**XXXX*,2,Playback(invalid)
#exten => _**XXXX*,3,Hangup
#
#
# Copyright (C) 2008 Matt Florell <vicidial@gmail.com> LICENSE: AGPLv2
#
$V = 0; # set to 1 for verbose mode
$CLIOUT = 1; # set to 1 for printing 1 line to STDERR
$SPEAK = 1; # set to 1 for saying invalid digits
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year = ($year + 1900);
$mon++;
if ($mon < 10) {$mon = "0$mon";}
if ($mday < 10) {$mday = "0$mday";}
if ($hour < 10) {$hour = "0$hour";}
if ($min < 10) {$min = "0$min";}
if ($sec < 10) {$sec = "0$sec";}
$now_date_epoch = time();
$now_date = "$year-$mon-$mday $hour:$min:$sec";
### begin parsing run-time options ###
if (length($ARGV[0])>1)
{
if ($V) {print STDERR "Perl Environment Dump:\n";}
$i=0;
while ($#ARGV >= $i)
{
$args = "$args $ARGV[$i]";
if ($V) {print STDERR "$i|$ARGV[$i]|\n";}
$i++;
}
if ($args =~ /--help/i)
{
print "allowed run time options:\n [-q] = quiet\n [-t] = test\n [-debug] = verbose debug messages\n\n";
}
else
{
if ($args =~ /-V/i)
{
$V=1;
}
if ($args =~ /-debug/i)
{
$DG=1;
}
if ($args =~ /-dbAVS/i)
{
$DGA=1;
}
if ($args =~ /-q/i)
{
$q=1;
$Q=1;
}
if ($args =~ /-t/i)
{
$TEST=1;
$T=1;
}
}
}
else
{
if ($V) {print "no command line options set\n";}
}
use Asterisk::AGI;
$AGI = new Asterisk::AGI;
$|=1;
while(<STDIN>) {
chomp;
last unless length($_);
if ($V)
{
if (/^agi_(\w+)\:\s+(.*)$/)
{
$AGI{$1} = $2;
}
}
if (/^agi_uniqueid\:\s+(.*)$/) {$unique_id = $1;}
if (/^agi_channel\:\s+(.*)$/) {$channel = $1;}
if (/^agi_extension\:\s+(.*)$/) {$extension = $1;}
if (/^agi_type\:\s+(.*)$/) {$type = $1;}
if (/^agi_callerid\:\s+(.*)$/) {$callerid = $1;}
}
if ($V)
{
if ($V) {print STDERR "AGI Environment Dump:\n";}
foreach $i (sort keys %AGI) {
if ($V) {print STDERR " -- $i = $AGI{$i}\n";}
}
}
if ($CLIOUT) {print STDERR "****** INVALID EXTENSION: |$unique_id|$channel|$extension|$type|$callerid|\n";}
if ($SPEAK)
{
$AGI->stream_file('beep');
sleep(1);
$AGI->say_digits("$extension");
}
exit;
+363
View File
@@ -0,0 +1,363 @@
#!/usr/bin/perl
#
# park_CID.agi version 2.6
#
# runs before the park extension to populate the parked_channels entry with the channel's callerid
#
# You need to put lines similar to those below in your extensions.conf file:
#
# ;outbound dialing:
# exten => 8301,1,Answer
# exten => 8301,2,AGI(park_CID.agi)
# exten => 8301,3,Playback(park)
# exten => 8301,4,Hangup
#
# Copyright (C) 2013 Matt Florell <vicidial@gmail.com> LICENSE: AGPLv2
#
# changes:
# 60731-1423 - changed to use DBI-DBD::mysql
# - changed to use /etc/astguiclient.conf for configs
# 60815-1256 - added output options check from database
# 90909-0922 - Fixed calleridname issue
# 101004-1012 - Changed park to Music On Hold, added option for park on AGI
# 130108-1817 - Changes for Asterisk 1.8 compatibility
#
$script = 'park_CID.agi';
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year = ($year + 1900);
$mon++;
if ($mon < 10) {$mon = "0$mon";}
if ($mday < 10) {$mday = "0$mday";}
if ($hour < 10) {$hour = "0$hour";}
if ($min < 10) {$min = "0$min";}
if ($sec < 10) {$sec = "0$sec";}
$now_date_epoch = time();
$now_date = "$year-$mon-$mday $hour:$min:$sec";
# default path to astguiclient configuration file:
$PATHconf = '/etc/astguiclient.conf';
open(conf, "$PATHconf") || die "can't open $PATHconf: $!\n";
@conf = <conf>;
close(conf);
$i=0;
foreach(@conf)
{
$line = $conf[$i];
$line =~ s/ |>|\n|\r|\t|\#.*|;.*//gi;
if ( ($line =~ /^PATHhome/) && ($CLIhome < 1) )
{$PATHhome = $line; $PATHhome =~ s/.*=//gi;}
if ( ($line =~ /^PATHlogs/) && ($CLIlogs < 1) )
{$PATHlogs = $line; $PATHlogs =~ s/.*=//gi;}
if ( ($line =~ /^PATHagi/) && ($CLIagi < 1) )
{$PATHagi = $line; $PATHagi =~ s/.*=//gi;}
if ( ($line =~ /^PATHweb/) && ($CLIweb < 1) )
{$PATHweb = $line; $PATHweb =~ s/.*=//gi;}
if ( ($line =~ /^PATHsounds/) && ($CLIsounds < 1) )
{$PATHsounds = $line; $PATHsounds =~ s/.*=//gi;}
if ( ($line =~ /^PATHmonitor/) && ($CLImonitor < 1) )
{$PATHmonitor = $line; $PATHmonitor =~ s/.*=//gi;}
if ( ($line =~ /^VARserver_ip/) && ($CLIserver_ip < 1) )
{$VARserver_ip = $line; $VARserver_ip =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_server/) && ($CLIDB_server < 1) )
{$VARDB_server = $line; $VARDB_server =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_database/) && ($CLIDB_database < 1) )
{$VARDB_database = $line; $VARDB_database =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_user/) && ($CLIDB_user < 1) )
{$VARDB_user = $line; $VARDB_user =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_pass/) && ($CLIDB_pass < 1) )
{$VARDB_pass = $line; $VARDB_pass =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_port/) && ($CLIDB_port < 1) )
{$VARDB_port = $line; $VARDB_port =~ s/.*=//gi;}
$i++;
}
if (!$VARDB_port) {$VARDB_port='3306';}
if (!$AGILOGfile) {$AGILOGfile = "$PATHlogs/agiout.$year-$mon-$mday";}
use DBI;
use Asterisk::AGI;
$AGI = new Asterisk::AGI;
$dbhA = DBI->connect("DBI:mysql:$VARDB_database:$VARDB_server:$VARDB_port", "$VARDB_user", "$VARDB_pass")
or die "Couldn't connect to database: " . DBI->errstr;
### Grab Server values from the database
$stmtA = "SELECT agi_output FROM servers where server_ip = '$VARserver_ip';";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
$rec_count=0;
while ($sthArows > $rec_count)
{
$AGILOG = '0';
@aryA = $sthA->fetchrow_array;
$DBagi_output = $aryA[0];
if ($DBagi_output =~ /STDERR/) {$AGILOG = '1';}
if ($DBagi_output =~ /FILE/) {$AGILOG = '2';}
if ($DBagi_output =~ /BOTH/) {$AGILOG = '3';}
$rec_count++;
}
$sthA->finish();
### begin parsing run-time options ###
if (length($ARGV[0])>1)
{
if ($AGILOG) {$agi_string = "Perl Environment Dump:"; &agi_output;}
$i=0;
while ($#ARGV >= $i)
{
$args = "$args $ARGV[$i]";
if ($AGILOG) {$agi_string = "$i|$ARGV[$i]"; &agi_output;}
$i++;
}
if ($args =~ />| |\"/i)
{
@CID = split(/-----/, $args);
$fullCID=1;
$callerid = $CID[2];
$calleridname = $CID[3];
}
if ($args =~ /--help/i)
{
print "allowed run time options:\n [-q] = quiet\n [-t] = test\n [-debug] = verbose debug messages\n\n";
}
else
{
if ($args =~ /-V/i)
{
$V=1;
}
if ($args =~ /-debug/i)
{
$DG=1;
}
if ($args =~ /-dbAVS/i)
{
$DGA=1;
}
if ($args =~ /-q/i)
{
$q=1;
$Q=1;
}
if ($args =~ /-t/i)
{
$TEST=1;
$T=1;
}
}
}
$|=1;
while(<STDIN>)
{
chomp;
last unless length($_);
if ($AGILOG)
{
if (/^agi_(\w+)\:\s+(.*)$/)
{
$AGI{$1} = $2;
}
}
if (/^agi_uniqueid\:\s+(.*)$/) {$unique_id = $1;}
if (/^agi_channel\:\s+(.*)$/) {$channel = $1;}
if (/^agi_extension\:\s+(.*)$/) {$extension = $1;}
if (/^agi_type\:\s+(.*)$/) {$type = $1;}
if (!$fullCID) # if no fullCID sent
{
if (/^agi_callerid\:\s+(.*)$/) {$callerid = $1;}
if (/^agi_calleridname\:\s+(.*)$/) {$calleridname = $1;}
if ( $calleridname =~ /\"/) {$calleridname =~ s/\"//gi;}
if ( (!$callerid) or ($callerid =~ /unknown/) or ( ($calleridname =~ /\d\d\d\d\d\d\d\d\d/) && (length($calleridname) > 16) ) )
{$callerid = $calleridname;}
if ( ($callerid =~ / /) && (length($callerid) > 20) )
{$callerid =~ s/ .*//gi;}
### allow for ANI being sent with the DNIS "*3125551212*9999*"
if ($extension =~ /^\*\d\d\d\d\d\d\d\d\d\d\*/)
{
$callerid = $extension;
$callerid =~ s/\*\d\d\d\d\*$//gi;
$callerid =~ s/^\*//gi;
$extension =~ s/^\*\d\d\d\d\d\d\d\d\d\d\*//gi;
$extension =~ s/\*$//gi;
}
$calleridname = $callerid;
}
}
if ($AGILOG) {$agi_string = "AGI Environment Dump:"; &agi_output;}
foreach $i (sort keys %AGI)
{
if ($AGILOG) {$agi_string = " -- $i = $AGI{$i}"; &agi_output;}
}
if ($AGILOG) {$agi_string = "AGI Variables: |$unique_id|$channel|$extension|$type|$callerid|"; &agi_output;}
if ($AGILOG) {$agi_string = "CALL BEING PARKED"; &agi_output;}
$stmtA = "UPDATE parked_channels set channel_group='$callerid' where server_ip='$VARserver_ip' and channel='$channel';";
if ($AGILOG) {$agi_string = "|$stmtA|"; &agi_output;}
$affected_rows = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "-- Park record updated: |$affected_rows| |$callerid|$channel|$VARserver_ip"; &agi_output;}
$VACcount=0;
$VAC_campaign_id='';
$VAC_call_type='';
$VAC_extension='';
$VLAcount=0;
$VLA_user='';
$VLA_campaign_id='';
$VCcount=0;
$park_context='default';
$MOHfiles=0;
$stmtA = "SELECT campaign_id,call_type,extension FROM vicidial_auto_calls where callerid='$callerid';";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$VACcount=$sthA->rows;
if ($VACcount > 0)
{
@aryA = $sthA->fetchrow_array;
$VAC_campaign_id = $aryA[0];
$VAC_call_type = $aryA[1];
$VAC_extension = $aryA[2];
}
$sthA->finish();
if ($VACcount > 0)
{
$stmtA = "SELECT user,campaign_id FROM vicidial_live_agents where callerid='$callerid';";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$VLAcount=$sthA->rows;
if ($VLAcount > 0)
{
@aryA = $sthA->fetchrow_array;
$VLA_user = $aryA[0];
$VLA_campaign_id = $aryA[1];
}
$sthA->finish();
if ($VLAcount > 0)
{
$stmtA = "SELECT park_file_name,ivr_park_call_agi FROM vicidial_campaigns where campaign_id='$VLA_campaign_id';";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$VCcount=$sthA->rows;
if ($VCcount > 0)
{
@aryA = $sthA->fetchrow_array;
$park_context = $aryA[0];
$VC_ivr_park_call_agi = $aryA[1];
}
$sthA->finish();
$stmtA = "SELECT count(*) FROM vicidial_music_on_hold_files where moh_id='$park_context';";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$MOHcount=$sthA->rows;
if ($MOHcount > 0)
{
@aryA = $sthA->fetchrow_array;
$MOHfiles = $aryA[0];
}
$sthA->finish();
if ($MOHfiles< 1)
{
if ($AGILOG) {$agi_string = "-- Context empty, switching to default: |$park_context|$MOHcount|$MOHfiles| |$callerid|"; &agi_output;}
$park_context = 'default';
}
### call was parked to an IVR, launch the AGI script
if ( ($VCcount > 0) && ($VAC_extension =~ /PARK_IVR/) && (length($VC_ivr_park_call_agi)>2) )
{
if ($AGILOG) {$agi_string = "-- Starting AGI: |$VC_ivr_park_call_agi| |$callerid|"; &agi_output;}
print "EXEC AGI $VC_ivr_park_call_agi\n";
checkresult($result);
if ($AGILOG) {$agi_string = "-- AGI Done: |$callerid|"; &agi_output;}
}
}
}
$dbhA->disconnect();
$AGI->stream_file('sip-silence');
$AGI->stream_file('sip-silence');
$AGI->stream_file('sip-silence');
$AGI->stream_file('sip-silence');
if ($AGILOG) {$agi_string = "-- Starting MoH: |$park_context| |$callerid|"; &agi_output;}
print "SET MUSIC ON $park_context\n";
$result = <STDIN>;
checkresult($result);
sleep(360);
if ($AGILOG) {$agi_string = "-- MoH Done: |$callerid|"; &agi_output;}
exit;
sub agi_output
{
if ($AGILOG >=2)
{
### open the log file for writing ###
open(Lout, ">>$AGILOGfile")
|| die "Can't open $AGILOGfile: $!\n";
print Lout "$now_date|$script|$agi_string\n";
close(Lout);
}
### send to STDERR writing ###
if ( ($AGILOG == '1') || ($AGILOG == '3') )
{print STDERR "$now_date|$script|$agi_string\n";}
$agi_string='';
}
sub checkresult
{
my ($res) = @_;
my $retval;
$tests++;
chomp $res;
if ($res =~ /^200/)
{
$res =~ /result=(-?\d+)/;
if (!length($1))
{
# print STDERR "FAIL ($res)\n";
$fail++;
}
else
{
# print STDERR "PASS ($1)\n";
$pass++;
}
}
else
{
# print STDERR "FAIL (unexpected result '$res')\n";
$fail++;
}
}
@@ -0,0 +1,611 @@
#!/usr/bin/perl
#
# park_call_IVR_example.agi version 2.4
#
# NOTE: this script is an example of how you can use a custom AGI script with
# the new Park Call IVR feature in campaigns. You will most likely need to
# significantly alter this script to suit your specific needs
#
# Defined in the Park Call AGI IVR field of a campaign, this script will ask
# customer for their ID and then populate it in the vicidial_list table and then
# send the call back to the agent. If the customer enters nothing X number of
# times then the call can be kicked out to a separate in-group
#
# Here is a list of the variables that you can set:
# 1. Language - default: en (English), other options are es-Spanish, fr-French
# 2. First prompt - default: please_enter_id_number
# 3. You-Entered prompt - default: you-entered
# 4. Confirmation prompt - default: confirm_id_number
# 5. Thank you prompt - default: auth-thankyou
# 6. Invalid prompt - default: buzz
# 7. Attempts before kick-out - default: 2
# 8. Kick-out prompt - default: outside-transfer
# 9. Kick-out In-Group
#
# Example Park Call AGI IVR field:
# park_call_IVR_example.agi|en---please_enter_id_number---you-entered---confirm_id_number---auth-thankyou---buzz---2---outside-transfer---TEST_IN
#
# Copyright (C) 2010 Matt Florell <vicidial@gmail.com> LICENSE: AGPLv2
#
# changes:
# 101004-1132 - First build
#
$script = 'park_call_IVR_example.agi';
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year = ($year + 1900);
$mon++;
if ($mon < 10) {$mon = "0$mon";}
if ($mday < 10) {$mday = "0$mday";}
if ($hour < 10) {$hour = "0$hour";}
if ($min < 10) {$min = "0$min";}
if ($sec < 10) {$sec = "0$sec";}
$now_date_epoch = time();
$now_date = "$year-$mon-$mday $hour:$min:$sec";
$S='*';
# default path to astguiclient configuration file:
$PATHconf = '/etc/astguiclient.conf';
open(conf, "$PATHconf") || die "can't open $PATHconf: $!\n";
@conf = <conf>;
close(conf);
$i=0;
foreach(@conf)
{
$line = $conf[$i];
$line =~ s/ |>|\n|\r|\t|\#.*|;.*//gi;
if ( ($line =~ /^PATHhome/) && ($CLIhome < 1) )
{$PATHhome = $line; $PATHhome =~ s/.*=//gi;}
if ( ($line =~ /^PATHlogs/) && ($CLIlogs < 1) )
{$PATHlogs = $line; $PATHlogs =~ s/.*=//gi;}
if ( ($line =~ /^PATHagi/) && ($CLIagi < 1) )
{$PATHagi = $line; $PATHagi =~ s/.*=//gi;}
if ( ($line =~ /^PATHweb/) && ($CLIweb < 1) )
{$PATHweb = $line; $PATHweb =~ s/.*=//gi;}
if ( ($line =~ /^PATHsounds/) && ($CLIsounds < 1) )
{$PATHsounds = $line; $PATHsounds =~ s/.*=//gi;}
if ( ($line =~ /^PATHmonitor/) && ($CLImonitor < 1) )
{$PATHmonitor = $line; $PATHmonitor =~ s/.*=//gi;}
if ( ($line =~ /^VARserver_ip/) && ($CLIserver_ip < 1) )
{$VARserver_ip = $line; $VARserver_ip =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_server/) && ($CLIDB_server < 1) )
{$VARDB_server = $line; $VARDB_server =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_database/) && ($CLIDB_database < 1) )
{$VARDB_database = $line; $VARDB_database =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_user/) && ($CLIDB_user < 1) )
{$VARDB_user = $line; $VARDB_user =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_pass/) && ($CLIDB_pass < 1) )
{$VARDB_pass = $line; $VARDB_pass =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_port/) && ($CLIDB_port < 1) )
{$VARDB_port = $line; $VARDB_port =~ s/.*=//gi;}
$i++;
}
if (!$VARDB_port) {$VARDB_port='3306';}
if (!$AGILOGfile) {$AGILOGfile = "$PATHlogs/agiout.$year-$mon-$mday";}
use DBI;
use Asterisk::AGI;
$AGI = new Asterisk::AGI;
$dbhA = DBI->connect("DBI:mysql:$VARDB_database:$VARDB_server:$VARDB_port", "$VARDB_user", "$VARDB_pass")
or die "Couldn't connect to database: " . DBI->errstr;
### Grab Server values from the database
$stmtA = "SELECT agi_output,ext_context FROM servers where server_ip = '$VARserver_ip';";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
$rec_count=0;
while ($sthArows > $rec_count)
{
$AGILOG = '0';
@aryA = $sthA->fetchrow_array;
$DBagi_output = $aryA[0];
$ext_context = $aryA[1];
if ($DBagi_output =~ /STDERR/) {$AGILOG = '1';}
if ($DBagi_output =~ /FILE/) {$AGILOG = '2';}
if ($DBagi_output =~ /BOTH/) {$AGILOG = '3';}
$rec_count++;
}
$sthA->finish();
### begin parsing run-time options ###
if (length($ARGV[0])>1)
{
if ($AGILOG) {$agi_string = "Perl Environment Dump:"; &agi_output;}
$i=0;
while ($#ARGV >= $i)
{
$args = "$args $ARGV[$i]";
if ($AGILOG) {$agi_string = "$i|$ARGV[$i]"; &agi_output;}
$i++;
}
### list of command-line array arguments:
@ARGV_vars = split(/---/, $ARGV[0]);
$language = $ARGV_vars[0];
$first_prompt = $ARGV_vars[1];
$you_entered_prompt = $ARGV_vars[2];
$confirmation_prompt = $ARGV_vars[3];
$thank_you_prompt = $ARGV_vars[4];
$invalid_prompt = $ARGV_vars[5];
$kickout_attempts = $ARGV_vars[6];
$kickout_prompt = $ARGV_vars[7];
$kickout_ingroup = $ARGV_vars[8];
}
if (length($language)<1) {$language='en';}
if (length($first_prompt)<1) {$first_prompt='please_enter_id_number';}
if (length($you_entered_prompt)<1) {$you_entered_prompt='you-entered';}
if (length($confirmation_prompt)<1) {$confirmation_prompt='confirm_id_number';}
if (length($thank_you_prompt)<1) {$thank_you_prompt='auth-thankyou';}
if (length($invalid_prompt)<1) {$invalid_prompt='buzz';}
if (length($kickout_attempts)<1) {$kickout_attempts='2';}
if (length($kickout_prompt)<1) {$kickout_prompt='outside-transfer';}
$|=1;
while(<STDIN>)
{
chomp;
last unless length($_);
if ($AGILOG)
{
if (/^agi_(\w+)\:\s+(.*)$/)
{
$AGI{$1} = $2;
}
}
if (/^agi_uniqueid\:\s+(.*)$/) {$unique_id = $1;}
if (/^agi_channel\:\s+(.*)$/) {$channel = $1;}
if (/^agi_extension\:\s+(.*)$/) {$extension = $1;}
if (/^agi_type\:\s+(.*)$/) {$type = $1;}
if (!$fullCID) # if no fullCID sent
{
if (/^agi_callerid\:\s+(.*)$/) {$callerid = $1;}
if (/^agi_calleridname\:\s+(.*)$/) {$calleridname = $1;}
if ( $calleridname =~ /\"/) {$calleridname =~ s/\"//gi;}
if ( (!$callerid) or ($callerid =~ /unknown/) or ( ($calleridname =~ /\d\d\d\d\d\d\d\d\d/) && (length($calleridname) > 16) ) )
{$callerid = $calleridname;}
if ( ($callerid =~ / /) && (length($callerid) > 20) )
{$callerid =~ s/ .*//gi;}
### allow for ANI being sent with the DNIS "*3125551212*9999*"
if ($extension =~ /^\*\d\d\d\d\d\d\d\d\d\d\*/)
{
$callerid = $extension;
$callerid =~ s/\*\d\d\d\d\*$//gi;
$callerid =~ s/^\*//gi;
$extension =~ s/^\*\d\d\d\d\d\d\d\d\d\d\*//gi;
$extension =~ s/\*$//gi;
}
$calleridname = $callerid;
}
}
if ($AGILOG) {$agi_string = "AGI Environment Dump:"; &agi_output;}
foreach $i (sort keys %AGI)
{
if ($AGILOG) {$agi_string = " -- $i = $AGI{$i}"; &agi_output;}
}
if ($AGILOG) {$agi_string = "AGI Variables: |$unique_id|$channel|$extension|$type|$callerid|"; &agi_output;}
if ($AGILOG) {$agi_string = "PARKED CALL IVR STARTED"; &agi_output;}
$stmtA = "UPDATE parked_channels set channel_group='$callerid' where server_ip='$VARserver_ip' and channel='$channel';";
if ($AGILOG) {$agi_string = "|$stmtA|"; &agi_output;}
$affected_rows = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "-- Park record updated: |$affected_rows| |$callerid|$channel|$VARserver_ip"; &agi_output;}
$VACcount=0;
$VAC_campaign_id='';
$VAC_call_type='';
$VAC_extension='';
$VLAcount=0;
$VLA_user='';
$VLA_campaign_id='';
$VCcount=0;
$MOHfiles=0;
$stmtA = "SELECT campaign_id,call_type,extension,phone_number,lead_id FROM vicidial_auto_calls where callerid='$callerid';";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$VACcount=$sthA->rows;
if ($VACcount > 0)
{
@aryA = $sthA->fetchrow_array;
$VAC_campaign_id = $aryA[0];
$VAC_call_type = $aryA[1];
$VAC_extension = $aryA[2];
$VAC_phone_number = $aryA[3];
$VAC_lead_id = $aryA[4];
if ($AGILOG) {$agi_string = "-- VAC Call Found: |$callerid|$VAC_campaign_id|$VAC_call_type|$VAC_extension|$VAC_phone_number|$VAC_lead_id|"; &agi_output;}
}
$sthA->finish();
if ($VACcount > 0)
{
$stmtA = "SELECT user,campaign_id,server_ip,conf_exten FROM vicidial_live_agents where callerid='$callerid';";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$VLAcount=$sthA->rows;
if ($VLAcount > 0)
{
@aryA = $sthA->fetchrow_array;
$VLA_user = $aryA[0];
$VLA_campaign_id = $aryA[1];
$VLA_server_ip = $aryA[2];
$VLA_conf_exten = $aryA[3];
}
$sthA->finish();
if ($VLAcount > 0)
{
if ($AGILOG) {$agi_string = "-- Starting Gather Data: |$VLA_user|$VLA_campaign_id|$kickout_attempts| |$callerid|"; &agi_output;}
### BEGIN prompt for customer ID number ($customer_id_number) ###
$customer_id_number='';
$variable='';
$confirmation=0;
$confirmation_chances=0;
$collect_digits='20';
$ms_delay='8000';
$variable_entry_prompt = $first_prompt;
$confirm_entry_prompt = $you_entered_prompt;
while ( ($confirmation_chances < $kickout_attempts) && ($confirmation < 1) )
{
$entry_chances=0;
while ( ($entry_chances < $kickout_attempts) && (length($variable) < 1) )
{
$entry_chances++;
&customer_variable_gather;
$customer_id_number = $variable;
if ( (length($customer_id_number) < 1) || (length($customer_id_number) > 20) )
{
if ($AGILOG) {$agi_string = "-- CUSTOMER ID GATHER FAIL : |$customer_id_number|"; &agi_output;}
$audio_file = $invalid_prompt; &play_audio;
$variable='';
}
}
$confirmation_chances++;
if ( (length($customer_id_number) > 0) || (length($customer_id_number) <= 20) )
{
$audio_file = $confirm_entry_prompt; &play_audio;
$AGI->say_digits("$variable");
&confirm_entry;
if ($ANSWER_confirm > 1)
{$variable='';}
if ( ($ANSWER_confirm > 0) && ($ANSWER_confirm < 2) )
{$confirmation=1;}
}
else
{
if ($entry_chances >= $kickout_attempts)
{$confirmation_chances=$kickout_attempts;}
}
}
### customer ID confirmed, update in vicidial_list and tag agent for update
if ($confirmation > 0)
{
$stmtA = "UPDATE vicidial_list set vendor_lead_code='$customer_id_number' where lead_id='$VAC_lead_id';";
$affected_rows = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "-- Lead record updated: |$affected_rows| |$callerid|$stmtA|"; &agi_output;}
$stmtA = "UPDATE vicidial_live_agents set external_update_fields='1',external_update_fields_data='vendor_lead_code' where user='$VLA_user';";
$affected_rows = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "-- Agent data update triggered: |$affected_rows| |$callerid|$stmtA|"; &agi_output;}
}
### no valid customer ID received, send customer to kickout in-group if defined
if ( ($confirmation < 1) && (length($kickout_ingroup)>0) )
{
$audio_file = $kickout_prompt; &play_audio;
sleep(1);
$stmtA = "UPDATE vicidial_live_agents set external_transferconf='LOCAL_CLOSER---$kickout_ingroup' where user='$VLA_user';";
$affected_rows = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "-- AGI Done, kick-out, sending call to kickout ingroup: |$affected_rows| |$callerid|$kickout_ingroup|$stmtA"; &agi_output;}
}
### END prompt for customer ID number ($customer_id_number) ###
else
{
### send API command to have agent grab call back
$AGI->stream_file('sip-silence');
$AGI->stream_file('sip-silence');
$AGI->stream_file('sip-silence');
$AGI->stream_file('sip-silence');
$stmtA = "UPDATE vicidial_live_agents set external_park='GRAB_IVR_CUSTOMER' where user='$VLA_user';";
$affected_rows = $dbhA->do($stmtA);
if ($AGILOG) {$agi_string = "-- AGI Done, sending call back to agent: |$affected_rows| |$callerid|$stmtA|$VLA_user|"; &agi_output;}
}
}
}
else
{
if ($AGILOG) {$agi_string = "-- Call Not Found in vicidial_auto_calls: |$callerid|"; &agi_output;}
}
$dbhA->disconnect();
$AGI->stream_file('sip-silence');
if ($AGILOG) {$agi_string = "-- Exiting Park Call IVR: |$callerid|"; &agi_output;}
exit;
sub agi_output
{
if ($AGILOG >=2)
{
### open the log file for writing ###
open(Lout, ">>$AGILOGfile")
|| die "Can't open $AGILOGfile: $!\n";
print Lout "$now_date|$script|$agi_string\n";
close(Lout);
}
### send to STDERR writing ###
if ( ($AGILOG == '1') || ($AGILOG == '3') )
{print STDERR "$now_date|$script|$agi_string\n";}
$agi_string='';
}
sub checkresult
{
my ($res) = @_;
my $retval;
$tests++;
chomp $res;
if ($res =~ /^200/)
{
$res =~ /result=(-?\d+)/;
if (!length($1))
{
# print STDERR "FAIL ($res)\n";
$fail++;
}
else
{
# print STDERR "PASS ($1)\n";
$pass++;
}
}
else
{
# print STDERR "FAIL (unexpected result '$res')\n";
$fail++;
}
}
sub leading_zero($)
{
$_ = $_[0];
s/^(\d)$/0$1/;
s/^(\d\d)$/0$1/;
return $_;
} # End of the leading_zero() routine.
### play audio, check for existing other language if defined ###
sub play_audio
{
if ($language =~ /es/)
{
$file_extension_gsm='.gsm';
$file_extension_wav='.wav';
$audio_suffix='_es';
if ( (-e ("$PATHsounds/$audio_file$audio_suffix$file_extension_gsm")) || (-e ("$PATHsounds/$audio_file$audio_suffix$file_extension_wav")) )
{$donothing=1;}
else
{$audio_suffix='';}
}
elsif ($language =~ /fr/)
{
$file_extension_gsm='.gsm';
$file_extension_wav='.wav';
$audio_suffix='_fr';
if ( (-e ("$PATHsounds/$audio_file$audio_suffix$file_extension_gsm")) || (-e ("$PATHsounds/$audio_file$audio_suffix$file_extension_wav")) )
{$donothing=1;}
else
{$audio_suffix='';}
}
else
{$audio_suffix='';}
$AGI->stream_file("$audio_file$audio_suffix");
}
##### BEGIN collect the customer variable ###############################################
sub customer_variable_gather
{
$interrupt_digit='';
$audio_file = "$variable_entry_prompt";
if ($language =~ /es/)
{
$file_extension_gsm='.gsm';
$file_extension_wav='.wav';
$audio_suffix='_es';
if ( (-e ("$PATHsounds/$audio_file$audio_suffix$file_extension_gsm")) || (-e ("$PATHsounds/$audio_file$audio_suffix$file_extension_wav")) )
{$donothing=1;}
else
{$audio_suffix='';}
}
elsif ($language =~ /fr/)
{
$file_extension_gsm='.gsm';
$file_extension_wav='.wav';
$audio_suffix='_fr';
if ( (-e ("$PATHsounds/$audio_file$audio_suffix$file_extension_gsm")) || (-e ("$PATHsounds/$audio_file$audio_suffix$file_extension_wav")) )
{$donothing=1;}
else
{$audio_suffix='';}
}
else
{$audio_suffix='';}
$interrupt_digit = $AGI->stream_file("$audio_file$audio_suffix",'0123456789');
print STDERR "interrupt_digit |$interrupt_digit|\n";
$digits_being_entered=1;
$PASS_word='';
if ($interrupt_digit > 0)
{
# if ($interrupt_digit == 35) {$interrupt_digit='#';}
# if ($interrupt_digit == 42) {$interrupt_digit='*';}
if ($interrupt_digit == 48) {$interrupt_digit=0;}
if ($interrupt_digit == 49) {$interrupt_digit=1;}
if ($interrupt_digit == 50) {$interrupt_digit=2;}
if ($interrupt_digit == 51) {$interrupt_digit=3;}
if ($interrupt_digit == 52) {$interrupt_digit=4;}
if ($interrupt_digit == 53) {$interrupt_digit=5;}
if ($interrupt_digit == 54) {$interrupt_digit=6;}
if ($interrupt_digit == 55) {$interrupt_digit=7;}
if ($interrupt_digit == 56) {$interrupt_digit=8;}
if ($interrupt_digit == 57) {$interrupt_digit=9;}
$PASS_word=$interrupt_digit;
}
$digit_loop_counter=0;
while ($digits_being_entered > 0)
{
$digit = chr($AGI->wait_for_digit('8000')); # wait 8 seconds for input or until the pound key is pressed
if ($digit =~ /\d/)
{
$PASS_word = "$PASS_word$digit";
print STDERR "digit |$digit| PASS_word |$PASS_word|\n";
# $AGI->say_digits("$digit");
undef $digit;
}
else
{
$digits_being_entered=0;
}
if ( (length($PASS_word)) >= $collect_digits)
{
$digits_being_entered=0;
}
$digit_loop_counter++;
}
$PASS_word =~ s/\D//gi;
$variable = $PASS_word;
if ($PASS_word) {print STDERR "digit |$digit| PASS_word |$PASS_word| variable |$variable|\n";}
$collect_digits='99';
}
##### END collect the customer variable ###############################################
##### BEGIN confirm entry ######################################
sub confirm_entry
{
$interrupt_digit='';
$audio_file = "$confirmation_prompt";
if ($language =~ /es/)
{
$file_extension_gsm='.gsm';
$file_extension_wav='.wav';
$audio_suffix='_es';
if ( (-e ("$PATHsounds/$audio_file$audio_suffix$file_extension_gsm")) || (-e ("$PATHsounds/$audio_file$audio_suffix$file_extension_wav")) )
{$donothing=1;}
else
{$audio_suffix='';}
}
elsif ($language =~ /fr/)
{
$file_extension_gsm='.gsm';
$file_extension_wav='.wav';
$audio_suffix='_fr';
if ( (-e ("$PATHsounds/$audio_file$audio_suffix$file_extension_gsm")) || (-e ("$PATHsounds/$audio_file$audio_suffix$file_extension_wav")) )
{$donothing=1;}
else
{$audio_suffix='';}
}
else
{$audio_suffix='';}
$interrupt_digit = $AGI->stream_file("$audio_file$audio_suffix",'12');
print STDERR "interrupt_digit |$interrupt_digit|\n";
$digits_being_entered=1;
$ANSWER_confirm='';
if ($interrupt_digit > 0)
{
# if ($interrupt_digit == 35) {$interrupt_digit='#';}
# if ($interrupt_digit == 42) {$interrupt_digit='*';}
if ($interrupt_digit == 48) {$interrupt_digit=0;}
if ($interrupt_digit == 49) {$interrupt_digit=1;}
if ($interrupt_digit == 50) {$interrupt_digit=2;}
if ($interrupt_digit == 51) {$interrupt_digit=3;}
if ($interrupt_digit == 52) {$interrupt_digit=4;}
if ($interrupt_digit == 53) {$interrupt_digit=5;}
if ($interrupt_digit == 54) {$interrupt_digit=6;}
if ($interrupt_digit == 55) {$interrupt_digit=7;}
if ($interrupt_digit == 56) {$interrupt_digit=8;}
if ($interrupt_digit == 57) {$interrupt_digit=9;}
$ANSWER_confirm=$interrupt_digit;
}
if (length($ANSWER_confirm) < 1)
{
$digit_loop_counter=0;
while ( ($digits_being_entered > 0) && ($digit_loop_counter < 1) )
{
$digit = chr($AGI->wait_for_digit("$ms_delay")); # wait 10 seconds for input or until the pound key is pressed
if ($digit =~ /\d/)
{
$ANSWER_confirm = "$ANSWER_confirm$digit";
print STDERR "digit |$digit| ANSWER_confirm |$ANSWER_confirm|\n";
# $AGI->say_digits("$digit");
undef $digit;
}
else
{
$digits_being_entered=0;
}
$digit_loop_counter++;
}
}
$ANSWER_confirm =~ s/\D//gi;
if ($ANSWER_confirm) {print STDERR "digit |$digit| ANSWER_confirm |$ANSWER_confirm|\n";}
}
##### END confirm entry ######################################
@@ -0,0 +1,283 @@
#!/usr/bin/perl
#
# timer_rebeep.agi
#
# This script is designed to be launched from the in-group/campaign timer
# setting to set another trigger event for the next X seconds. Often used for
# calls that need to have a periodic beep signifying that the call is being
# recorded
#
# Just add the following to the System Settings Custom Dialplan Entry
# and set rebuild conf=Y
# exten => 8399,1,Answer
# exten => 8399,n,Wait(1)
# exten => 8399,n,Playback(beep)
# exten => 8399,n,AGI(timer_rebeep.agi,20---D1_DIAL_QUIET)
# exten => 8399,n,Hangup
#
# ; Below are the parameters needed for the script to be run properly
# ; 1. seconds delay before next trigger goes
# ; 2. timer action to launch
#
# Then set the in-groups/campaigns that you want to have this enabled
# "Timer Action" = D1_DIAL_QUIET
# "Timer Action Seconds" = 20
# "Transfer Conf Number 1" = 8399
#
# Copyright (C) 2014 Matt Florell <vicidial@gmail.com> LICENSE: AGPLv2
#
# CHANGELOG
# 111227-1418 - First build
# 141124-2309 - Fixed Fhour variable bug
#
$US='_';
&get_time_now;
$start_epoch = $now_date_epoch;
$script = 'timer_rebeep.agi';
$now_date_epoch = time();
$now_date = "$year-$mon-$mday $hour:$min:$sec";
$now_day = "$year-$mon-$mday";
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year = ($year + 1900);
$mon++;
if ($mon < 10) {$mon = "0$mon";}
if ($mday < 10) {$mday = "0$mday";}
if ($hour < 10) {$hour = "0$hour";}
if ($min < 10) {$min = "0$min";}
if ($sec < 10) {$sec = "0$sec";}
$US='_';
# default path to astguiclient configuration file:
$PATHconf = '/etc/astguiclient.conf';
open(conf, "$PATHconf") || die "can't open $PATHconf: $!\n";
@conf = <conf>;
close(conf);
$i=0;
foreach(@conf)
{
$line = $conf[$i];
$line =~ s/ |>|\n|\r|\t|\#.*|;.*//gi;
if ( ($line =~ /^PATHlogs/) && ($CLIlogs < 1) )
{$PATHlogs = $line; $PATHlogs =~ s/.*=//gi;}
if ( ($line =~ /^PATHagi/) && ($CLIagi < 1) )
{$PATHagi = $line; $PATHagi =~ s/.*=//gi;}
if ( ($line =~ /^PATHsounds/) && ($CLIsounds < 1) )
{$PATHsounds = $line; $PATHsounds =~ s/.*=//gi;}
if ( ($line =~ /^PATHmonitor/) && ($CLImonitor < 1) )
{$PATHmonitor = $line; $PATHmonitor =~ s/.*=//gi;}
if ( ($line =~ /^VARserver_ip/) && ($CLIserver_ip < 1) )
{$VARserver_ip = $line; $VARserver_ip =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_server/) && ($CLIDB_server < 1) )
{$VARDB_server = $line; $VARDB_server =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_database/) && ($CLIDB_database < 1) )
{$VARDB_database = $line; $VARDB_database =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_user/) && ($CLIDB_user < 1) )
{$VARDB_user = $line; $VARDB_user =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_pass/) && ($CLIDB_pass < 1) )
{$VARDB_pass = $line; $VARDB_pass =~ s/.*=//gi;}
if ( ($line =~ /^VARDB_port/) && ($CLIDB_port < 1) )
{$VARDB_port = $line; $VARDB_port =~ s/.*=//gi;}
$i++;
}
if (!$VARDB_port) {$VARDB_port='3306';}
if (!$AGILOGfile) {$AGILOGfile = "$PATHlogs/rebeep.$year-$mon-$mday";}
use DBI;
use Asterisk::AGI;
$AGI = new Asterisk::AGI;
$dbhA = DBI->connect("DBI:mysql:$VARDB_database:$VARDB_server:$VARDB_port", "$VARDB_user", "$VARDB_pass")
or die "Couldn't connect to database: " . DBI->errstr;
# defaults:
$AGILOG = 2;
$seconds_delay = '20';
$timer_action = 'D1_DIAL_QUIET';
### begin parsing run-time options ###
if (length($ARGV[0])>1)
{
if ($AGILOG) {$agi_string = "Perl Environment Dump:"; &agi_output;}
$i=0;
while ($#ARGV >= $i)
{
$args = "$args $ARGV[$i]";
if ($AGILOG) {$agi_string = "$i|$ARGV[$i]"; &agi_output;}
$i++;
}
### list of command-line array arguments:
@ARGV_vars = split(/---/, $ARGV[0]);
if (length($ARGV_vars[0]) > 0) {$seconds_delay = $ARGV_vars[0];}
if (length($ARGV_vars[1]) > 0) {$timer_action = $ARGV_vars[1];}
}
$|=1;
while(<STDIN>)
{
chomp;
last unless length($_);
if ($V)
{
if (/^agi_(\w+)\:\s+(.*)$/)
{
$AGI{$1} = $2;
}
}
if (/^agi_context\:\s+(.*)$/) {$context = $1;}
if (/^agi_priority\:\s+(.*)$/) {$priority = $1;}
if (/^agi_uniqueid\:\s+(.*)$/) {$unique_id = $1; $uniqueid = $1;}
if (/^agi_channel\:\s+(.*)$/) {$channel = $1;}
if (/^agi_callerid\:\s+(.*)$/) {$callerid = $1;}
if (/^agi_calleridname\:\s+(.*)$/) {$calleridname = $1;}
if (/^agi_extension\:\s+(.*)$/) {$extension = $1;}
}
if ( ($callerid =~ /\".*\"/) && ( (!$calleridname) or ($calleridname =~ /unknown/) ) )
{
$calleridname = $callerid;
$calleridname =~ s/\<\d\d\d\d\d\d\d\d\d\d\>//gi;
$calleridname =~ s/\"|\" //gi;
}
$callerid =~ s/\D//gi;
$calleridname =~ s/unknown//gi;
if ( (!$callerid) or ($callerid =~ /unknown/) )
{$callerid = $calleridname;}
$phone_number=$callerid;
### Grab vicidial_manager record
$man_id='';
$stmtA = "SELECT man_id,entry_date,cmd_line_d,server_ip FROM vicidial_manager where callerid='$calleridname' order by man_id desc limit 1;";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
if ($sthArows > 0)
{
@aryA = $sthA->fetchrow_array;
$man_id = $aryA[0];
$entry_date = $aryA[1];
$EXTsession = $aryA[2];
$EXTsession =~ s/Exten: //gi;
while (length($EXTsession) > 7) {$EXTsession =~ s/^.//gi;}
$EXTserver_ip = $aryA[3];
}
$sthA->finish();
$AGI->stream_file('sip-silence');
$AGI->stream_file('sip-silence');
$AGI->stream_file('sip-silence');
if (length($man_id) > 0)
{
$stmtA = "SELECT user,UNIX_TIMESTAMP(last_call_time) FROM vicidial_live_agents where server_ip='$EXTserver_ip' and conf_exten='$EXTsession' order by last_update_time desc limit 1;";
$sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtA ", $dbhA->errstr;
$sthArows=$sthA->rows;
if ($sthArows > 0)
{
@aryA = $sthA->fetchrow_array;
$user = $aryA[0];
$last_call_time = $aryA[1];
$call_length = ($now_date_epoch - $last_call_time);
$next_length = ($call_length + $seconds_delay - 4);
$sthA->finish();
### update the agent live record for the next timer trigger
$stmtA="UPDATE vicidial_live_agents set external_timer_action='$timer_action',external_timer_action_message='',external_timer_action_seconds='$next_length' where user='$user' and conf_exten='$EXTsession' and server_ip='$EXTserver_ip';";
$affected_rows = $dbhA->do($stmtA);
print STDERR "|$affected_rows|$man_id|$entry_date|$timer_action|$next_length|$EXTserver_ip|$EXTsession|$stmtA|\n";
}
else
{
$sthA->finish();
print STDERR "|ERROR|$man_id|$entry_date|$timer_action|$seconds_delay|$EXTserver_ip|$EXTsession|\n";
}
}
else
{
print STDERR "|ERROR|$man_id|$entry_date|$timer_action|$seconds_delay|$EXTserver_ip|$EXTsession|\n";
}
$dbhA->disconnect();
exit;
#######################
##### Subroutines #####
sub get_time_now #get the current date and time and epoch for logging call lengths and datetimes
{
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year = ($year + 1900);
$mon++;
if ($mon < 10) {$mon = "0$mon";}
if ($mday < 10) {$mday = "0$mday";}
if ($hour < 10) {$Fhour = "0$hour";}
if ($min < 10) {$min = "0$min";}
if ($sec < 10) {$sec = "0$sec";}
$now_date_epoch = time();
$now_date = "$year-$mon-$mday $hour:$min:$sec";
$filedate = "$year$mon$mday$hour$min$sec";
}
sub checkresult {
my ($res) = @_;
my $retval;
$tests++;
chomp $res;
if ($res =~ /^200/) {
$res =~ /result=(-?\d+)/;
if (!length($1)) {
print STDERR "FAIL ($res)\n";
$fail++;
} else {
print STDERR "PASS ($1)\n";
$pass++;
}
} else {
print STDERR "FAIL (unexpected result '$res')\n";
$fail++;
}
}
sub agi_output
{
if ($AGILOG >=2)
{
### open the log file for writing ###
open(Lout, ">>$AGILOGfile")
|| die "Can't open $AGILOGfile: $!\n";
print Lout "$now_date|$script|$agi_string\n";
close(Lout);
}
### send to STDERR writing ###
if ( ($AGILOG == '1') || ($AGILOG == '3') )
{print STDERR "$now_date|$script|$agi_string\n";}
$agi_string='';
}
sub leading_zero($)
{
$_ = $_[0];
s/^(\d)$/0$1/;
s/^(\d\d)$/0$1/;
return $_;
} # End of the leading_zero() routine.