#!/usr/bin/perl # # agi-3way_press_outside.agi version 2.14 # # Runs when a 3way call using a dial override is placed through a special # extension from the agent screen to an outside-agent where that outside-agent # will have to press-1 to accept the phone call. # An example of the special extension: "49907917275551212" where: # - 49907 is the special extension # - 9 is the dial prefix # - 1 is the phone code # - 7275551212 is the outside-agent number to be dialed # # THIS IS THE SECOND REQUIRED AGI SCRIPT OF 2 FOR THIS FEATURE # # 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 stage of the outside-agent phone call: # ; answered - outside-agent has answered the phone call # ; declined - outside-agent has not pressed-1 in time and the call is to hung up # ; accepted - outside-agent has pressed-1 and is to be sent to the agent # #; VICIdial outside-agent transfer after initiated by agent: #[xfer_press_dialplan] #exten => s,1,Answer #exten => s,n,NoOp(${call_3way_id}) #exten => s,n,NoOp(${CALLERID(name)}) #exten => s,n,AGI(agi-3way_press_outside.agi,answered---20) #exten => s,n,Playback(sip-silence) #exten => s,n,Wait(1) #exten => s,n,Background(outside-sales) #exten => s,n,Background(press-1) #exten => s,n,Background(now) #exten => s,n,Wait(10) #exten => s,n,AGI(agi-3way_press_outside.agi,declined) #exten => s,n,Hangup # #exten => 1,1,NoOp(${call_3way_id}) #exten => 1,n,NoOp(${CALLERID(name)}) #exten => 1,n,AGI(agi-3way_press_outside.agi,accepted) #exten => 1,n,Hangup() # #exten => t,1,Playback(buzz) #exten => t,n,Hangup() # #exten => i,1,Playback(buzz) #exten => i,n,Hangup() # # # Copyright (C) 2024 Matt Florell LICENSE: AGPLv2 # # changes: # 231113-2041 - First Build # 231227-1427 - Added multi-phone ability # 240108-1503 - Added call_channel and call_transfer logging # 240114-0946 - Added check of agent_heartbeat # 240116-2109 - Added CLI flag for timeout # 240425-2208 - Added ConfBridge support # $script = 'agi-3way_press_outside.agi'; $build = '240116-2109'; $at='@'; $S='*'; $mel=1; # Mysql Error Log enabled = 1 $mysql_log_count=272; $one_mysql_log=0; $MT[0]=''; $timeout=100; $conf_engine=''; $now_date_epoch = time(); ($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";} $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 = ; 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 (!$TPALOGfile) {$TPALOGfile = "$PATHlogs/tpaout.$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,conf_engine 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; $dbhP=$dbhA; $mysql_count='02001'; $MEL_aff_rows=$sthArows; &mysql_error_logging; $rec_count=0; while ($sthArows > $rec_count) { $AGILOG = '0'; @aryA = $sthA->fetchrow_array; $DBagi_output = $aryA[0]; $local_gmt = $aryA[1]; $conf_engine = $aryA[2]; if ($isdst) {$local_gmt++;} if ($DBagi_output =~ /STDERR/) {$AGILOG = '1';} if ($DBagi_output =~ /FILE/) {$AGILOG = '2';} if ($DBagi_output =~ /BOTH/) {$AGILOG = '3';} $rec_count++; } $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,queuemetrics_pe_phone_append,did_agent_log,alt_log_server_ip,alt_log_dbname,alt_log_login,alt_log_pass,tables_use_alt_log_db,did_ra_extensions_enabled,queuemetrics_socket,queuemetrics_socket_url,enable_did_entry_list_id,enable_drop_lists,inbound_answer_config,agent_search_method,abandon_check_queue,inbound_credits FROM system_settings;"; $sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr; $sthA->execute or die "executing: $stmtA ", $dbhA->errstr; $sthArows=$sthA->rows; $dbhP=$dbhA; $mysql_count='02026'; $MEL_aff_rows=$sthArows; &mysql_error_logging; 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]; $queuemetrics_pe_phone_append = $aryA[7]; $did_agent_log = $aryA[8]; $alt_log_server_ip = $aryA[9]; $alt_log_dbname = $aryA[10]; $alt_log_login = $aryA[11]; $alt_log_pass = $aryA[12]; $tables_use_alt_log_db = $aryA[13]; $did_ra_extensions_enabled = $aryA[14]; $queuemetrics_socket = $aryA[15]; $queuemetrics_socket_url = $aryA[16]; $enable_did_entry_list_id = $aryA[17]; $enable_drop_lists = $aryA[18]; $inbound_answer_config = $aryA[19]; $SSagent_search_method = $aryA[20]; $SSabandon_check_queue = $aryA[21]; $SSinbound_credits = $aryA[22]; } $sthA->finish(); ##### END SYSTEM SETTINGS 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]); $stage = $ARGV_vars[0]; $CLI_timeout = $ARGV_vars[1]; $CLI_timeout =~ s/\D//gi; if (length($CLI_timeout) > 0) { if ($AGILOG) {$agi_string = " Timeout CLI override: $timeout -> $CLI_timeout"; &agi_output;} $timeout = $CLI_timeout; } } $|=1; while() { 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 (/^agi_context\:\s+(.*)$/) {$context = $1;} } $callerid =~ s/\'|\\\\|\\\|\\|\\;|\\\;|\;|;//gi; $calleridname =~ s/\'|\\\\|\\\|\\|\\;|\\\;|\;|;//gi; $extension =~ s/\'|\"|\\\\|\\\|\\|\\;|\\\;|\;|;//gi; 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|^T\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); } if ($calleridname =~ /^DC\d\d\d\d\d\dW\d\d\d\d\d\d\d\d\d\dW/) { $lead_id = substr($calleridname, 9, 10); $lead_id = ($lead_id + 0); } 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|^T\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d/) {$callerid = $calleridname;} if ( ($callerid =~ /\".*\"/) && ( (!$calleridname) or ($calleridname =~ /unknown/) ) ) { $calleridname = $callerid; $calleridname =~ s/\<\d\d\d\d\d\d\d\d\d\d\>//gi; $calleridname =~ s/\"|\" //gi; } if ($callerid !~ /^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|^T\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d/) { $callerid =~ s/\D|\'//gi; $calleridname =~ s/unknown|\'//gi; if ( (!$callerid) or ($callerid =~ /unknown/) ) {$callerid = $calleridname;} } 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|$stage|$build|"; &agi_output;} $CALLvendor_lead_code = $AGI->get_variable('vendor_lead_code'); $CALLlead_id = $AGI->get_variable('lead_id'); if ($AGILOG) {$agi_string = "CALL Variables: |Lead ID: $lead_id($CALLlead_id) VLC: $CALLvendor_lead_code $callerid|"; &agi_output;} if ($AGILOG) {$agi_string = "+++++ 3WAY PRESS OUTSIDE-AGENT STARTED : |$channel_group|$callerid|$now_date"; &agi_output;} $VDADphone=''; $VDADphone_code=''; $VCcallerid = $channel_group; $VD_campaign_id = $channel_group; if (length($VCcallerid)<2) {$VCcallerid = 'ViciDial Inbound';} ### 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,routing_prefix 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; $dbhP=$dbhA; $mysql_count='02004'; $MEL_aff_rows=$sthArows; &mysql_error_logging; 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]; $asterisk_version = $aryA[4]; $DBmax_vicidial_trunks = $aryA[5]; $voicemail_dump_exten_no_inst = $aryA[6]; $routing_prefix = $aryA[7]; 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 ($asterisk_version) {$AST_ver = $asterisk_version;} if ($DBmax_vicidial_trunks) {$max_vicidial_trunks = $DBmax_vicidial_trunks;} $LOCAL_GMT_OFF = $SERVER_GMT; $LOCAL_GMT_OFF_STD = $SERVER_GMT; if ($isdst) {$LOCAL_GMT_OFF++;} if ($DB) {print "SEED TIME $now_date_epoch : $year-$mon-$mday $hour:$min:$sec LOCAL GMT OFFSET NOW: $LOCAL_GMT_OFF\n";} $cbc++; } $sthA->finish(); $AGI->stream_file('sip-silence'); $AGI->stream_file('sip-silence'); ### Grab call details based on vicidial_3way_press_live record $CALLserver_ip=''; $CALLsession_id=''; $CALLuser=''; $CALLlead_id=''; $CALLsession_id_3way=''; $CALLagent_heartbeat=0; $stmtA = "SELECT server_ip,session_id,user,lead_id,session_id_3way,UNIX_TIMESTAMP(agent_heartbeat) FROM vicidial_3way_press_live where call_3way_id='$calleridname' 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; $dbhP=$dbhA; $mysql_count='02009'; $MEL_aff_rows=$sthArows; &mysql_error_logging; if ($sthArows > 0) { @aryA = $sthA->fetchrow_array; $CALLserver_ip = $aryA[0]; $CALLsession_id = $aryA[1]; $CALLuser = $aryA[2]; $CALLlead_id = $aryA[3]; $CALLsession_id_3way = $aryA[4]; $CALLagent_heartbeat = $aryA[5]; } $sthA->finish(); $heartbeat_test = ( (time()) - 9); if ( ($sthArows > 0) && ($CALLagent_heartbeat <= $heartbeat_test) ) { if ($AGILOG) {$agi_string = "3-WAY AGENT ERROR: heartbeat gone, exiting... |($CALLagent_heartbeat < $heartbeat_test)|$calleridname|"; &agi_output;} ### Update the call in vicidial_3way_press_live $stmtA="DELETE FROM vicidial_3way_press_live where call_3way_id='$calleridname';"; if ($AGILOG) {$agi_string = "|$stmtA|"; &agi_output;} $affected_rowsA = $dbhA->do($stmtA); if ($AGILOG) {$agi_string = "-- vicidial_3way_press_live agent gone DELETE: |$affected_rowsA|$stmtA|"; &agi_output;} ### Update the call in vicidial_3way_press_log $stmtA="UPDATE vicidial_3way_press_log SET result=CONCAT(result,'|Agent Gone: $SQLdate') where call_3way_id='$calleridname';"; if ($AGILOG) {$agi_string = "|$stmtA|"; &agi_output;} $affected_rowsA = $dbhA->do($stmtA); if ($AGILOG) {$agi_string = "-- vicidial_3way_press_log agent gone UPDATE: |$affected_rowsA|$stmtA|"; &agi_output;} print "SET CONTEXT $context\n"; $result = ; checkresult($result); print "SET EXTENSION i\n"; $result = ; checkresult($result); print "SET PRIORITY 1\n"; $result = ; checkresult($result); exit; } $stmtA = "SELECT user FROM vicidial_live_agents where lead_id='$lead_id' and server_ip='$CALLserver_ip' order by live_agent_id desc limit 1;"; $sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr; $sthA->execute or die "executing: $stmtA ", $dbhA->errstr; $sthArows=$sthA->rows; $dbhP=$dbhA; $mysql_count='02009'; $MEL_aff_rows=$sthArows; &mysql_error_logging; if ($sthArows > 0) { @aryA = $sthA->fetchrow_array; $CALLuser = $aryA[0]; } $sthA->finish(); if ( (length($CALLuser) < 1) || (length($CALLserver_ip) < 5) || (length($CALLsession_id) < 1) ) { if ($AGILOG) {$agi_string = "3-WAY AGENT ERROR: no user/server/session found, exiting... |$CALLuser|$CALLserver_ip|$CALLsession_id|$calleridname|"; &agi_output;} print "SET CONTEXT $context\n"; $result = ; checkresult($result); print "SET EXTENSION i\n"; $result = ; checkresult($result); print "SET PRIORITY 1\n"; $result = ; checkresult($result); exit; } if ($AGILOG) {$agi_string = "3-WAY DEBUG: user/server/session found: |$CALLuser|$CALLserver_ip|$CALLsession_id|$calleridname|"; &agi_output;} if ($stage =~ /answered/i) { ### Update the call in vicidial_3way_press_live $stmtA="UPDATE vicidial_3way_press_live SET status='ANSWERED',call_channel='$channel' where call_3way_id='$calleridname' and status!='DEFEATED';"; if ($AGILOG) {$agi_string = "|$stmtA|"; &agi_output;} $affected_rowsA = $dbhA->do($stmtA); if ($AGILOG) {$agi_string = "-- vicidial_3way_press_live answered UPDATE: |$affected_rowsA|$stmtA|"; &agi_output;} ### Update the call in vicidial_3way_press_log $stmtA="UPDATE vicidial_3way_press_log SET result=CONCAT(result,'|Answered: $SQLdate'),call_channel='$channel' where call_3way_id='$calleridname';"; if ($AGILOG) {$agi_string = "|$stmtA|"; &agi_output;} $affected_rowsA = $dbhA->do($stmtA); if ($AGILOG) {$agi_string = "-- vicidial_3way_press_log answered UPDATE: |$affected_rowsA|$stmtA|"; &agi_output;} } if ($stage =~ /accepted/i) { $accepted_ct=0; $stmtA = "LOCK TABLES vicidial_3way_press_live WRITE;"; my $LOCKaffected_rows = $dbhA->do($stmtA); $dbhP=$dbhA; $mysql_count='02057'; $MEL_aff_rows=$LOCKaffected_rows; &mysql_error_logging; if (length($qp_groupWAIT_aco)<2) {$qp_groupWAIT_aco="''";} ### Get list of users that should take higher priority inbound calls first $stmtA = "SELECT count(*) from vicidial_3way_press_live where lead_id='$CALLlead_id' and status IN('ACCEPTED','RESERVED','TRANSFER') and call_3way_id!='$calleridname';"; $sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr; $sthA->execute or die "executing: $stmtA ", $dbhA->errstr; $sthArows=$sthA->rows; $dbhP=$dbhA; $mysql_count='02XXX'; $MEL_aff_rows=$sthArows; &mysql_error_logging; if ($sthArows > 0) { @aryA = $sthA->fetchrow_array; $accepted_ct = $aryA[0]; } if ($accepted_ct < 1) { ### Update the call in vicidial_3way_press_live $stmtA = "UPDATE vicidial_3way_press_live SET status='ACCEPTED',call_channel='$channel' where call_3way_id='$calleridname';"; my $UPDATEaffected_rows = $dbhA->do($stmtA); $dbhP=$dbhA; $mysql_count='02XXX'; $MEL_aff_rows=$UPDATEaffected_rows; &mysql_error_logging; if ($AGILOG) {$agi_string = "-- vicidial_3way_press_live accepted UPDATE: |$UPDATEaffected_rows|$stmtA|"; &agi_output;} ### Update all other calls for this lead in vicidial_3way_press_live to DEFEATED status $stmtA = "UPDATE vicidial_3way_press_live SET status='DEFEATED' where lead_id='$CALLlead_id' and call_3way_id!='$calleridname';"; my $UPDATEaffected_rows = $dbhA->do($stmtA); $dbhP=$dbhA; $mysql_count='02XXX'; $MEL_aff_rows=$UPDATEaffected_rows; &mysql_error_logging; if ($AGILOG) {$agi_string = "-- vicidial_3way_press_live defeated UPDATE: |$UPDATEaffected_rows|$stmtA|"; &agi_output;} $stmtA = "UNLOCK TABLES;"; my $UNLOCKaffected_rows = $dbhA->do($stmtA); $dbhP=$dbhA; $mysql_count='02XXX'; $MEL_aff_rows=$UNLOCKaffected_rows; &mysql_error_logging; ### Update the call in vicidial_3way_press_log $stmtA="UPDATE vicidial_3way_press_log SET result=CONCAT(result,'|Accepted: $SQLdate'),call_channel='$channel' where call_3way_id='$calleridname';"; if ($AGILOG) {$agi_string = "|$stmtA|"; &agi_output;} $affected_rowsA = $dbhA->do($stmtA); if ($AGILOG) {$agi_string = "-- vicidial_3way_press_log accepted UPDATE: |$affected_rowsA|$stmtA|"; &agi_output;} # loop until session_id_3way is populated $CALLstatus=''; $CALLconf_exten=''; $wait_loop=0; while ($wait_loop < $timeout) { usleep(1*990*1000); if ($wait_loop =~ /0$|5$/) { $AGI->stream_file('hold_tone'); } $stmtA = "SELECT status,session_id_3way,UNIX_TIMESTAMP(agent_heartbeat) FROM vicidial_3way_press_live where call_3way_id='$calleridname' 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; $dbhP=$dbhA; $mysql_count='02XXX'; $MEL_aff_rows=$sthArows; &mysql_error_logging; if ($sthArows > 0) { @aryA = $sthA->fetchrow_array; $CALLstatus = $aryA[0]; $CALLconf_exten = $aryA[1]; $CALLagent_heartbeat = $aryA[2]; } $sthA->finish(); $heartbeat_test = ( (time()) - 9); if ($CALLagent_heartbeat <= $heartbeat_test) { if ($AGILOG) {$agi_string = "3-WAY AGENT ERROR: heartbeat gone 2, exiting... |($CALLagent_heartbeat < $heartbeat_test)|$calleridname|"; &agi_output;} ### Update the call in vicidial_3way_press_live $stmtA="DELETE FROM vicidial_3way_press_live where call_3way_id='$calleridname';"; if ($AGILOG) {$agi_string = "|$stmtA|"; &agi_output;} $affected_rowsA = $dbhA->do($stmtA); if ($AGILOG) {$agi_string = "-- vicidial_3way_press_live agent gone 2 DELETE: |$affected_rowsA|$stmtA|"; &agi_output;} ### Update the call in vicidial_3way_press_log $stmtA="UPDATE vicidial_3way_press_log SET result=CONCAT(result,'|Agent Gone: $SQLdate') where call_3way_id='$calleridname';"; if ($AGILOG) {$agi_string = "|$stmtA|"; &agi_output;} $affected_rowsA = $dbhA->do($stmtA); if ($AGILOG) {$agi_string = "-- vicidial_3way_press_log agent gone 2 UPDATE: |$affected_rowsA|$stmtA|"; &agi_output;} print "SET CONTEXT $context\n"; $result = ; checkresult($result); print "SET EXTENSION i\n"; $result = ; checkresult($result); print "SET PRIORITY 1\n"; $result = ; checkresult($result); exit; } if ( ($CALLstatus =~ /RESERVED/) && (length($CALLconf_exten) > 0) ) { ### Update the call in vicidial_3way_press_live $stmtA="UPDATE vicidial_3way_press_live SET status='TRANSFER' where call_3way_id='$calleridname';"; if ($AGILOG) {$agi_string = "|$stmtA|"; &agi_output;} $affected_rowsA = $dbhA->do($stmtA); if ($AGILOG) {$agi_string = "-- vicidial_3way_press_live transfer UPDATE: |$affected_rowsA|$stmtA|"; &agi_output;} ### Update the call in vicidial_3way_press_log $stmtA="UPDATE vicidial_3way_press_log SET result=CONCAT(result,'|Transfer: ',NOW()),call_transfer='Y' where call_3way_id='$calleridname';"; if ($AGILOG) {$agi_string = "|$stmtA|"; &agi_output;} $affected_rowsA = $dbhA->do($stmtA); if ($AGILOG) {$agi_string = "-- vicidial_3way_press_log transfer UPDATE: |$affected_rowsA|$stmtA|"; &agi_output;} ### Update vicidial_conferences to leave_3way=1 # $Ttimeout='Xtimeout9'; # $stmtA="UPDATE vicidial_conferences set leave_3way='1', leave_3way_datetime=NOW(), extension='3WAY_$CALLuser$Ttimeout' where server_ip='$CALLserver_ip' and conf_exten='$CALLconf_exten';"; $stmtA="UPDATE vicidial_conferences set leave_3way='1', leave_3way_datetime=NOW(), extension='3WAY_$CALLuser' where server_ip='$CALLserver_ip' and conf_exten='$CALLconf_exten';"; if ($AGILOG) {$agi_string = "|$stmtA|"; &agi_output;} $affected_rowsD = $dbhA->do($stmtA); if ($AGILOG) {$agi_string = "-- vicidial_conferences UPDATE: |$affected_rowsD|$stmtA|"; &agi_output;} ### Delete the call in vicidial_3way_press_live $stmtA="DELETE from vicidial_3way_press_live where call_3way_id='$calleridname';"; if ($AGILOG) {$agi_string = "|$stmtA|"; &agi_output;} $affected_rowsE = $dbhA->do($stmtA); if ($AGILOG) {$agi_string = "-- vicidial_3way_press_live transfer DELETE: |$affected_rowsE|$stmtA|"; &agi_output;} if ($conf_engine eq "CONFBRIDGE") {$XFERstr = "8$CALLconf_exten";} else {$XFERstr = "7$CALLconf_exten";} if ($AGILOG) {$agi_string = "3-WAY AGENT TRANSFER: |$CALLuser|$CALLserver_ip|$CALLconf_exten|$calleridname|"; &agi_output;} print "SET CONTEXT $ext_context\n"; $result = ; checkresult($result); print "SET EXTENSION $XFERstr\n"; $result = ; checkresult($result); print "SET PRIORITY 1\n"; $result = ; checkresult($result); $wait_loop=1000; exit; } $wait_loop++; } } else { # already accepted $stmtA = "UNLOCK TABLES;"; my $UNLOCKaffected_rows = $dbhA->do($stmtA); $dbhP=$dbhA; $mysql_count='02XXX'; $MEL_aff_rows=$UNLOCKaffected_rows; &mysql_error_logging; ### Update the call in vicidial_3way_press_live $stmtA="UPDATE vicidial_3way_press_live SET status='TOOSLOW' where call_3way_id='$calleridname';"; if ($AGILOG) {$agi_string = "|$stmtA|"; &agi_output;} $affected_rowsA = $dbhA->do($stmtA); if ($AGILOG) {$agi_string = "-- vicidial_3way_press_live too slow UPDATE: |$affected_rowsA|$stmtA|"; &agi_output;} ### Update the call in vicidial_3way_press_log $stmtA="UPDATE vicidial_3way_press_log SET result=CONCAT(result,'|Too Slow: $SQLdate') where call_3way_id='$calleridname';"; if ($AGILOG) {$agi_string = "|$stmtA|"; &agi_output;} $affected_rowsA = $dbhA->do($stmtA); if ($AGILOG) {$agi_string = "-- vicidial_3way_press_log too slow UPDATE: |$affected_rowsA|$stmtA|"; &agi_output;} } } if ($stage =~ /declined/i) { ### Update the call in vicidial_3way_press_live $stmtA="UPDATE vicidial_3way_press_live SET status='DECLINED' where call_3way_id='$calleridname' and status!='DEFEATED';"; if ($AGILOG) {$agi_string = "|$stmtA|"; &agi_output;} $affected_rowsA = $dbhA->do($stmtA); if ($AGILOG) {$agi_string = "-- vicidial_3way_press_live declined UPDATE: |$affected_rowsA|$stmtA|"; &agi_output;} ### Update the call in vicidial_3way_press_log $stmtA="UPDATE vicidial_3way_press_log SET result=CONCAT(result,'|Declined: $SQLdate') where call_3way_id='$calleridname';"; if ($AGILOG) {$agi_string = "|$stmtA|"; &agi_output;} $affected_rowsA = $dbhA->do($stmtA); if ($AGILOG) {$agi_string = "-- vicidial_3way_press_log declined UPDATE: |$affected_rowsA|$stmtA|"; &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 leading_zero($) { $_ = $_[0]; s/^(\d)$/0$1/; s/^(\d\d)$/0$1/; return $_; } # End of the leading_zero() routine. sub agi_output { ($Lsec,$Lmin,$Lhour,$Lmday,$Lmon,$Lyear,$Lwday,$Lyday,$Lisdst) = localtime(time); if ($Lhour < 10) {$Lhour = "0$Lhour";} if ($Lmin < 10) {$Lmin = "0$Lmin";} if ($Lsec < 10) {$Lsec = "0$Lsec";} $LOGtime = "$Lhour:$Lmin:$Lsec"; if ($AGILOG >=2) { ### open the log file for writing ### open(Lout, ">>$AGILOGfile") || die "Can't open $AGILOGfile: $!\n"; print Lout "$now_date|$LOGtime|$script|$agi_string\n"; close(Lout); } ### send to STDERR writing ### if ( ($AGILOG == '1') || ($AGILOG == '3') ) {print STDERR "$now_date|$LOGtime|$script|$agi_string\n";} $agi_string=''; } sub mysql_error_logging { ($Lsec,$Lmin,$Lhour,$Lmday,$Lmon,$Lyear,$Lwday,$Lyday,$Lisdst) = localtime(time); if ($Lhour < 10) {$Lhour = "0$Lhour";} if ($Lmin < 10) {$Lmin = "0$Lmin";} if ($Lsec < 10) {$Lsec = "0$Lsec";} $LOGtime = "$Lhour:$Lmin:$Lsec"; $errno=''; $error=''; if ( ($mel > 0) || ($one_mysql_log > 0) ) { $errno = $dbhP->err(); if ( ($errno > 0) || ($mel > 1) || ($one_mysql_log > 0) ) { $error = $dbhP->errstr(); ### open the log file for writing ### open(Eout, ">>$ERRLOGfile") || die "Can't open $ERRLOGfile: $!\n"; print Eout "$now_date|$LOGtime|$script|$mysql_count|$MEL_aff_rows|$errno|$error|$stmtA|$callerid|$insert_lead_id|\n"; close(Eout); } } $one_mysql_log=0; } # 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 ); }