#!/usr/bin/perl
#/* ========================================
# * ███╗   ███╗██████╗ ██╗ █████╗ ██╗
# * ████╗ ████║██╔══██╗██║██╔══██╗██║
# * ██╔████╔██║██║  ██║██║███████║██║
# * ██║╚██╔╝██║██║  ██║██║██╔══██║██║
# * ██║ ╚═╝ ██║██████╔╝██║██║  ██║███████╗
# * ╚═╝     ╚═╝╚═════╝ ╚═╝╚═╝  ╚═╝╚══════╝
# * ========================================
# * mDial - Omni-Channel Contact Centre Suite.
# * Initially Written by Martin McCarthy.
# * Contributions welcome.
# * Active: 2020 - 2023.
# *
# * This software is licensed under AGPLv2.
# * You can find more information here;
# * https://www.gnu.org/licenses/agpl-3.0.en.html
# * A copy of the license is also shipped with this build.
# *
# * Important note: this software is provided to you free of charge.
# * If you paid for this software, you were ripped off.
# *
# * This project is a fork of the awesome FOSS project, ViCiDial.
# * ViCiDial is copyrighted by Matt Florell and the ViCiDial Group
# * under the AGPLv2 license.
# *
# * You can find out more about ViCiDial;
# * Web: https://www.vicidial.com/
# * Email: Matt Florell <vicidial@gmail.com>
# * IRC: Libera.Chat - ##vicidial
# *
# * Bug reports, feature requests and patches welcome!
# * ======================================== */
&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       = '_';
$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;
$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();
if ( length( $ARGV[0] ) > 1 ) {
    if ( $ARGV[0] =~ /---/ ) {
        @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];
        $search_phone    = $ARGV_vars[8];
        $search_method   = $ARGV_vars[9];
        $search_error    = $ARGV_vars[10];
        $phone_url       = $ARGV_vars[11];
        $insert_list     = $ARGV_vars[12];
        $override_list   = $ARGV_vars[13];
        $override_code   = $ARGV_vars[14];
        $override_status = $ARGV_vars[15];
        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 ( ( $search_phone =~ /Y/ ) || ( $phone_url =~ /Y/ ) ) {
    $stmtA =
"SELECT nva_call_url,nva_search_method,nva_error_filename,nva_new_list_id,nva_new_phone_code,nva_new_status FROM phones where extension='$accountcode' and 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;
        $nva_call_url       = $aryA[0];
        $nva_search_method  = $aryA[1];
        $nva_error_filename = $aryA[2];
        $nva_new_list_id    = $aryA[3];
        $nva_new_phone_code = $aryA[4];
        $nva_new_status     = $aryA[5];
    }
    $sthA->finish();
}
$temp_phone     = $extension;
$search_lead_id = 'NOTFOUND';
if ( $search_phone =~ /Y/ ) {
    while ( length($temp_phone) > 10 ) { $temp_phone =~ s/^.//gi; }
    $stmtA =
"SELECT lead_id FROM vicidial_list where phone_number='$temp_phone' order by entry_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;
        $search_lead_id = $aryA[0];
        if ($AGILOG) {
            $agi_string =
              "NVA Search, lead found: $search_lead_id|$temp_phone($extension)";
            &agi_output;
        }
    }
    else {
        if ($AGILOG) {
            $agi_string = "NVA Search, lead not found: $temp_phone($extension)";
            &agi_output;
        }
        if ( $search_error =~ /Y/ ) {
            if ($AGILOG) {
                $agi_string = "NVA playing error file then exiting...";
                &agi_output;
            }
            $temp_error_filename = 'invalid';
            if ( length($nva_error_filename) > 0 ) {
                $temp_error_filename = $nva_error_filename;
            }
            $AGI->stream_file('sip-silence');
            $AGI->stream_file('sip-silence');
            $AGI->stream_file('silence');
            $AGI->stream_file("$temp_error_filename");
            sleep(3);
            $AGI->hangup();
            exit;
        }
        else {
            if ( $insert_list =~ /Y/ ) {
                if ( length($override_list) > 1 ) {
                    $nva_new_list_id = $override_list;
                }
                if ( length($override_code) > 0 ) {
                    $nva_new_phone_code = $override_code;
                }
                if ( length($override_status) > 0 ) {
                    $nva_new_status = $override_status;
                }
                $stmtA =
"INSERT INTO vicidial_list (entry_date,status,user,vendor_lead_code,source_id,list_id,called_since_last_reset,phone_code,phone_number,security_phrase,called_count,gmt_offset_now,comments) values('$now_date','$nva_new_status','$accountcode','','PHONE','$nva_new_list_id','Y','$nva_new_phone_code','$temp_phone','$accountcode','1','0','NVA PHONE INSERT');";
                $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: $stmtA ", $dbhA->errstr;
                $sthArows = $sthA->rows;
                if ( $sthArows > 0 ) {
                    @aryA           = $sthA->fetchrow_array;
                    $search_lead_id = $aryA[0];
                }
                $sthA->finish();
                if ($AGILOG) {
                    $agi_string =
"NVA Inserted New Lead: |$affected_rows|$search_lead_id|$nva_new_list_id|$nva_new_phone_code|$nva_new_status|";
                    &agi_output;
                }
            }
        }
    }
    $sthA->finish();
}
if (   ( ( length($lead_id) < 1 ) || ( $lead_id < 1 ) )
    && ( $search_lead_id !~ /NOTFOUND/ ) )
{
    $lead_id = $search_lead_id;
}
if ( $record_call =~ /Y/ ) {
    $filename = "$filedate$US$accountcode$US$extension";
    $filename =~ s/\"|\'//gi;
    $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 ( ( $phone_url =~ /Y/ ) && ( length($nva_call_url) > 8 ) ) {
    $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 do nva URL, curl binary not found";
                    &agi_output;
                }
            }
        }
    }
    if ( ( length($curlbin) > 4 ) && ( $nva_call_url =~ /http/i ) ) {
        if ( ( length($lead_id) > 0 ) && ( $lead_id !~ /NOTFOUND/ ) ) {
            $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;
                $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();
            $stmtA =
"SELECT list_description,campaign_id FROM vicidial_lists where list_id='$list_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;
                $list_description = $aryA[0];
                $campaign_id      = $aryA[1];
            }
            $sthA->finish();
        }
        $nva_call_url =~ s/^VAR|^ //gi;
        $nva_call_url =~ s/--A--phone_number--B--/$temp_phone/gi;
        $nva_call_url =~ s/--A--uniqueid--B--/$unique_id/gi;
        $nva_call_url =~ s/--A--lead_id--B--/$search_lead_id/gi;
        $nva_call_url =~ s/--A--extension--B--/$accountcode/gi;
        $nva_call_url =~ s/--A--channel--B--/$channel/gi;
        $nva_call_url =~ s/--A--server_ip--B--/$VARserver_ip/gi;
        $nva_call_url =~ s/--A--lead_id--B--/$lead_id/gi;
        $nva_call_url =~ s/--A--entry_date--B--/$entry_date/gi;
        $nva_call_url =~ s/--A--modify_date--B--/$modify_date/gi;
        $nva_call_url =~ s/--A--status--B--/$status/gi;
        $nva_call_url =~ s/--A--user--B--/$user/gi;
        $nva_call_url =~ s/--A--vendor_lead_code--B--/$vendor_lead_code/gi;
        $nva_call_url =~ s/--A--source_id--B--/$source_id/gi;
        $nva_call_url =~ s/--A--list_id--B--/$list_id/gi;
        $nva_call_url =~ s/--A--phone_number--B--/$phone_number/gi;
        $nva_call_url =~ s/--A--title--B--/$title/gi;
        $nva_call_url =~ s/--A--first_name--B--/$first_name/gi;
        $nva_call_url =~ s/--A--middle_initial--B--/$middle_initial/gi;
        $nva_call_url =~ s/--A--last_name--B--/$last_name/gi;
        $nva_call_url =~ s/--A--address1--B--/$address1/gi;
        $nva_call_url =~ s/--A--address2--B--/$address2/gi;
        $nva_call_url =~ s/--A--address3--B--/$address3/gi;
        $nva_call_url =~ s/--A--city--B--/$city/gi;
        $nva_call_url =~ s/--A--state--B--/$state/gi;
        $nva_call_url =~ s/--A--province--B--/$province/gi;
        $nva_call_url =~ s/--A--postal_code--B--/$postal_code/gi;
        $nva_call_url =~ s/--A--country_code--B--/$country_code/gi;
        $nva_call_url =~ s/--A--gender--B--/$gender/gi;
        $nva_call_url =~ s/--A--date_of_birth--B--/$date_of_birth/gi;
        $nva_call_url =~ s/--A--alt_phone--B--/$alt_phone/gi;
        $nva_call_url =~ s/--A--email--B--/$email/gi;
        $nva_call_url =~ s/--A--security_phrase--B--/$security_phrase/gi;
        $nva_call_url =~ s/--A--comments--B--/$comments/gi;
        $nva_call_url =~ s/--A--called_count--B--/$called_count/gi;
        $nva_call_url =~
          s/--A--last_local_call_time--B--/$last_local_call_time/gi;
        $nva_call_url =~ s/--A--rank--B--/$rank/gi;
        $nva_call_url =~ s/--A--owner--B--/$owner/gi;
        $nva_call_url =~ s/--A--campaign_id--B--/$campaign_id/gi;
        $nva_call_url =~ s/--A--list_description--B--/$list_description/gi;
        $nva_call_url =~ s/--A--recording_id--B--/$recording_id/gi;
        $nva_call_url =~ s/--A--recording_filename--B--/$filename/gi;
        $nva_call_url =~ s/&/\\&/gi;
        $nva_call_url =~ s/ /+/gi;
        $SQL_log = "$nva_call_url";
        $SQL_log =~ s/;|\||\\//gi;
        $stmtA =
"INSERT INTO vicidial_url_log SET uniqueid='$unique_id',url_date=NOW(),url_type='nva_phone',url='$SQL_log',url_response='';";
        $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: $stmtA ", $dbhA->errstr;
        $sthArows = $sthA->rows;

        if ( $sthArows > 0 ) {
            @aryA   = $sthA->fetchrow_array;
            $url_id = $aryA[0];
        }
        $sthA->finish();
        my $secW = time();
        @curl_output = `$curlbin $nva_call_url`;
        my $secY         = time();
        my $response_sec = ( $secY - $secW );
        $k           = 0;
        $full_output = '';

        foreach (@curl_output) {
            $full_output_raw .= $curl_output[$k];
            $k++;
        }
        $full_output = $full_output_raw;
        $full_output     =~ s/\D//gi;
        $full_output_raw =~ s/;|\||\\//gi;
        $stmtA =
"UPDATE vicidial_url_log SET url_response='$full_output_raw|$full_output',response_sec='$response_sec' where url_log_id='$url_id';";
        $affected_rows = $dbhA->do($stmtA);
        if ($AGILOG) {
            $agi_string =
"phone NVA URL response: |$search_lead_id|$full_output|$nva_call_url|$affected_rows|";
            &agi_output;
        }
    }
}
if ( $log_user_call =~ /Y/ ) {
    $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 ( $log_call =~ /Y/ ) {
    $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 ( $double_log =~ /Y/ ) {
    $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( Lout, ">>$AGILOGfile" )
          || die "Can't open $AGILOGfile: $!\n";
        print Lout "$now_date|$script|$agi_string\n";
        close(Lout);
    }
    if ( ( $AGILOG == '1' ) || ( $AGILOG == '3' ) ) {
        print STDERR "$now_date|$script|$agi_string\n";
    }
    $agi_string = '';
}

sub parse_asterisk_version {
    my $ast_ver_str     = $_[0];
    my @hyphen_parts    = split( /-/, $ast_ver_str );
    my $ast_ver_postfix = $hyphen_parts[1];
    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
        );
    }
    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);
}
