Files
ViciDialSVN/agc_2-X/trunk/agi/agi-NVA_recording.agi
T
mattf 1bd24b2302 Added optional GPG audio recording encryption.
Added option to use RECID as a recording filename variable.

git-svn-id: svn://192.168.202.10@2378 3d104415-ff17-0410-8863-d5cf3c621b8a
2015-09-16 02:20:11 +00:00

399 lines
12 KiB
Perl

#!/usr/bin/perl
#
# agi-NVA_recording.agi version 2.12
#
# 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!!!
# ; 7. play the recording ID of this call before recording starts
# ; 8. include the recording ID in the filename
#
# ;custom dialplan entry example: (similar to the defaultlog Call Menu)
#exten => _X.,1,AGI(agi-NVA_recording.agi,BOTH------Y---N---Y---N---N---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---N---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)
#
# ; example of using as Call Menu prompt to record with playing of recording id
#agi-NVA_recording.agi,BOTH------Y---N---Y---N---Y---Y
#
# Copyright (C) 2015 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
# 150915-1824 - Added recording ID play and filename options
#
&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];
$play_recid = $ARGV_vars[6];
$file_recid = $ARGV_vars[7];
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 ###
### 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);
$stmtB = "SELECT LAST_INSERT_ID() LIMIT 1;";
$sthA = $dbhA->prepare($stmtB) or die "preparing: ",$dbhA->errstr;
$sthA->execute or die "executing: $stmtB ", $dbhA->errstr;
$sthArows=$sthA->rows;
if ($sthArows > 0)
{
@aryA = $sthA->fetchrow_array;
$recording_id = $aryA[0];
}
if ($file_recid =~ /Y/)
{
$filename .= "$US$recording_id";
$stmtA = "UPDATE recording_log SET filename='$filename',location='$filename' where recording_id='$recording_id';";
$affected_rows = $dbhA->do($stmtA);
}
if ($play_recid =~ /Y/)
{
$AGI->stream_file('id_number');
$AGI->say_digits("$recording_id");
}
%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");
}
}
### 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 );
}