Files
ViciDialSVN/agc_1-X/trunk/call_park_I.agi
T
root 6ef43b97cf 1.1.10 release
git-svn-id: svn://192.168.202.10@1 3d104415-ff17-0410-8863-d5cf3c621b8a
2006-07-07 14:57:59 +00:00

231 lines
5.8 KiB
Perl

#!/usr/bin/perl
#
# call_park_I.agi
#
# runs when a call is to be parked for pickup of customer service
# This version is for calls send from one server to another over Crossover PRI T
#
# You need to put lines similar to those below in your extensions.conf file:
#
# ;inbound calls:
#exten => _90009.,1,AGI(call_park_I.agi,${EXTEN})
#exten => _90009.,2,Answer ; Answer the line
#exten => _90009.,3,Playback(park) ; Play a welcome message & music
#exten => _90009.,4,AGI(call_park_I.agi,HANGUP-----${EXTEN})
#
#
# Copyright (C) 2006 Matt Florell <vicidial@gmail.com> LICENSE: GPLv2
#
$V = 1; # set to 1 for verbose mode
$M = 1; # set to 1 for 2 line messages mode
### Make sure this file is in a path or put the absolute path to it
require("/home/cron/AST_SERVER_conf.pl"); # local configuration file
($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";
### 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++;
}
### list of command-line array arguments:
@ARGV_vars = split(/-----/, $ARGV[0]);
$status = $ARGV_vars[1];
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 ( (!$callerid) or ($callerid =~ /unknown/) )
{$callerid = $calleridname;}
### allow for internal PRI transfer data string "90009*CL_uk3survy_*8301*10000123*universal***"
if ($extension =~ /^900\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
### set the callerid to the filename of the audio for on-hold
print "SET CALLERID $park_extension\n";
checkresult($result);
print STDERR "callerID changed: $park_extension\n";
}
}
if ($V)
{
if ($V) {print STDERR "AGI Environment Dump:\n";}
foreach $i (sort keys %AGI) {
if ($V) {print STDERR " -- $i = $AGI{$i}\n";}
}
}
$AGI->stream_file('beep');
if ($V) {print STDERR "AGI Environment Dump: |$unique_id|$channel|$extension|$type|$callerid|\n";}
if ( ($extension =~ /h/i) && (length($extension) < 4)) {$stage = 'END';}
else {$stage = 'START';}
### call start stage
if ($status !~ /HANGUP/)
{
if ($V) {print STDERR "\nCALL PARK STARTED\n";}
if ($M) {print STDERR "+++++ CALL PARK START : |$unique_id|$channel|$extension|$type|$callerid-$pin|$now_date\n";}
if ($channel =~ /^SIP/) {$channel =~ s/-.*//gi;}
# if ($channel =~ /^Zap\//) {$channel =~ s/-\d$//gi;}
use Net::MySQL;
if (!$DB_port) {$DB_port='3306';}
my $mysql = Net::MySQL->new(hostname=>"$DB_server", database=>"$DB_database", user=>"$DB_user", password=>"$DB_pass", port => "$DB_port") or die "Couldn't connect to database: $DB_server - $DB_database\n";
$stmtA = "INSERT INTO parked_channels (channel,server_ip,channel_group,extension,parked_by,parked_time) values('$channel','$server_ip','$channel_group','$park_extension','$parked_by','$now_date')";
if ($V) {print STDERR "\n|$stmtA|\n";}
$mysql->query($stmtA) or die "Couldn't execute query:\n";
$stmtA = "INSERT INTO park_log (uniqueid,status,channel,server_ip,parked_time,channel_group) values('$unique_id','PARKED','$channel','$server_ip','$now_date','$channel_group')";
if ($V) {print STDERR "\n|$stmtA|\n";}
$mysql->query($stmtA) or die "Couldn't execute query:\n";
$mysql->close;
}
### call end stage
else
{
if ($V) {print STDERR "\nCALL INBOUND HUNG UP\n";}
use Net::MySQL;
if (!$DB_port) {$DB_port='3306';}
my $dbh = Net::MySQL->new(hostname=>"$DB_server", database=>"$DB_database", user=>"$DB_user", password=>"$DB_pass", port => "$DB_port") or die "Couldn't connect to database: $DB_server - $DB_database\n";
if ($V) {print STDERR "\nCONNECTED: deleting parked_channels record for $unique_id on $server_ip\n";}
$stmtA = "DELETE from parked_channels where channel='$channel' and server_ip='$server_ip'";
$dbh->query($stmtA) or die "Couldn't execute query:\n";
$dbh->close;
if ($M) {print STDERR "+++++ CALL PARK HUNGUP: |$unique_id|\n";}
}
if ($V) {print STDERR "\nAll Done Call Park Exiting:\n";}
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++;
}
}