added many new inbound groups options including place-in-line and estimated hold time
added DID call routing features from web interface docs updates and example conf files updates git-svn-id: svn://192.168.202.10@957 3d104415-ff17-0410-8863-d5cf3c621b8a
This commit is contained in:
@@ -0,0 +1,370 @@
|
||||
#!/usr/bin/perl
|
||||
#
|
||||
# agi-DID_route.agi version 2.0.5
|
||||
#
|
||||
# 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:
|
||||
# ;inbound DID catch-all:
|
||||
#exten => _X.,1,AGI(agi-DID_route.agi)
|
||||
#
|
||||
# Copyright (C) 2008 Matt Florell <vicidial@gmail.com> LICENSE: AGPLv2
|
||||
#
|
||||
# changes:
|
||||
# 81007-0324 - First Build
|
||||
#
|
||||
|
||||
$script = 'agi-DID_route.agi';
|
||||
$dtmf_silent_prefix = '7';
|
||||
$at='@';
|
||||
$S='*';
|
||||
|
||||
|
||||
($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";}
|
||||
|
||||
$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 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[0]";
|
||||
if ($DBagi_output =~ /STDERR/) {$AGILOG = '1';}
|
||||
if ($DBagi_output =~ /FILE/) {$AGILOG = '2';}
|
||||
if ($DBagi_output =~ /BOTH/) {$AGILOG = '3';}
|
||||
$rec_count++;
|
||||
}
|
||||
$sthA->finish();
|
||||
|
||||
$|=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;}
|
||||
|
||||
|
||||
### Grab DID values from the database
|
||||
$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 FROM vicidial_inbound_dids where did_pattern = '$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;
|
||||
$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];
|
||||
}
|
||||
$sthA->finish();
|
||||
|
||||
if ($AGILOG) {$did_string = "$SQLdate|$uniqueid|$channel|$extension|$callerid|$calleridname|$did_id|$did_route|"; &did_output;}
|
||||
|
||||
if ($sthArows < 1)
|
||||
{
|
||||
### nothing found, so exit script
|
||||
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 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]";
|
||||
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();
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
### 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();
|
||||
|
||||
if ($protocol =~ /EXTERNAL/)
|
||||
{
|
||||
### 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';
|
||||
}
|
||||
else
|
||||
{
|
||||
#$AGI->exec("Dial", "$protocol/$phone|30");
|
||||
print "EXEC Dial \"$protocol/$phone|30\"\n";
|
||||
checkresult($result);
|
||||
if ($AGILOG) {$agi_string = "Done dialing $protocol/$phone |$result|$pass|$fail|$stmtA|"; &agi_output;}
|
||||
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
### Route call to a voicemail box on the system
|
||||
if ($did_route =~ /VOICEMAIL/)
|
||||
{
|
||||
### 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$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";
|
||||
$exten_context = $ext_context;
|
||||
$did_route = 'EXTEN';
|
||||
}
|
||||
|
||||
|
||||
### 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";
|
||||
checkresult($result);
|
||||
print "SET EXTENSION $XFERexten\n";
|
||||
checkresult($result);
|
||||
print "SET PRIORITY 1\n";
|
||||
checkresult($result);
|
||||
|
||||
exit;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
### Route call to a logged-in VICIDIAL agent
|
||||
if ($did_route =~ /AGENT/)
|
||||
{
|
||||
# THIS FEATURE NOT YET DONE
|
||||
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='';
|
||||
}
|
||||
Reference in New Issue
Block a user