#!/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!
# * ======================================== */
$signalonly   = 0;
$cli_vars_set = 0;
if ( length( $ARGV[0] ) > 1 ) {
    $i = 0;
    while ( $#ARGV >= $i ) {
        $args = "$args $ARGV[$i]";
        $i++;
    }
    @ARGV_vars  = split( /---/, $ARGV[0] );
    $signalonly = $ARGV_vars[0];
    $cli_vars   = $ARGV_vars[1];
}
if ( $signalonly =~ /signalonly/ ) { $signalonly   = 1; }
if ( length($cli_vars) > 0 )       { $cli_vars_set = 1; }
$| = 1;
while (<STDIN>) {
    chomp;
    last unless length($_);
    if (/^agi_(\w+)\:\s+(.*)$/)        { $AGI{$1}       = $2; }
    if (/^agi_callerid\:\s+(.*)$/)     { $caller_id     = $1; }
    if (/^agi_calleridname\:\s+(.*)$/) { $caller_idname = $1; }
}
print STDERR "AGI Environment Dump:\n";
foreach $i ( sort keys %AGI ) {
    print STDERR " -- $i = $AGI{$i}\n";
}
use Time::HiRes ( 'gettimeofday', 'usleep', 'sleep' )
  ;    # necessary to have perl sleep command of less than one second
use Asterisk::AGI;
my $agi      = new Asterisk::AGI;
my $clid     = $agi->get_variable('CALLERID');
my $clidname = $agi->get_variable('CALLERIDNAME');
print STDERR
"X.  |$clid|$caller_id|$clidname|     |$signalonly|$cli_vars_set|$cli_vars|\n";
print "STREAM FILE silence \"\"\n";
usleep( 1 * 10 * 1000 );
$caller_id = "$caller_id$caller_idname";
$caller_id =~ s/unknown//g;
if ( $cli_vars_set > 0 ) { $caller_id = $cli_vars; }
@CALLERID_DIGITS = split( //, $caller_id );

foreach (@CALLERID_DIGITS) {
    if ( $CALLERID_DIGITS[$g] =~ /\d/ ) {
        if ( $signalonly > 0 ) {
            print "EXEC SendDTMF $CALLERID_DIGITS[$g] \"\"\n";
        }
        else { print "STREAM FILE $CALLERID_DIGITS[$g] \"\"\n"; }
        print STDERR "DIGIT: $CALLERID_DIGITS[$g]\n";
    }
    else {
        $dtmf_played = 0;
        if ( $CALLERID_DIGITS[$g] =~ /\#/ ) {
            if   ( $signalonly > 0 ) { print "EXEC SendDTMF \# \"\"\n"; }
            else                     { print "STREAM FILE hash \"\"\n"; }
            $dtmf_played++;
        }
        if ( $CALLERID_DIGITS[$g] =~ /\*/ ) {
            if   ( $signalonly > 0 ) { print "EXEC SendDTMF \* \"\"\n"; }
            else                     { print "STREAM FILE star \"\"\n"; }
            $dtmf_played++;
        }
        if ( $CALLERID_DIGITS[$g] =~ /\,/ ) {
            sleep(1);
            $dtmf_played++;
        }
        if ( $CALLERID_DIGITS[$g] =~ /[A-Za-z]/ ) {
            $translated_digit = '';
            if ( $CALLERID_DIGITS[$g] =~ /[A-Ca-c]/ ) { $translated_digit = 2; }
            if ( $CALLERID_DIGITS[$g] =~ /[D-Fd-f]/ ) { $translated_digit = 3; }
            if ( $CALLERID_DIGITS[$g] =~ /[G-Ig-i]/ ) { $translated_digit = 4; }
            if ( $CALLERID_DIGITS[$g] =~ /[J-Lj-l]/ ) { $translated_digit = 5; }
            if ( $CALLERID_DIGITS[$g] =~ /[M-Om-o]/ ) { $translated_digit = 6; }
            if ( $CALLERID_DIGITS[$g] =~ /[P-Sp-s]/ ) { $translated_digit = 7; }
            if ( $CALLERID_DIGITS[$g] =~ /[T-Vt-v]/ ) { $translated_digit = 8; }
            if ( $CALLERID_DIGITS[$g] =~ /[W-Zw-z]/ ) { $translated_digit = 9; }

            if ( $signalonly > 0 ) {
                print "EXEC SendDTMF $translated_digit \"\"\n";
            }
            else { print "STREAM FILE $translated_digit \"\"\n"; }
            print STDERR "DIGIT: $translated_digit\n";
            $dtmf_played++;
        }
        print STDERR "NON-DIGIT: $CALLERID_DIGITS[$g]\n";
    }
    $g++;
    usleep( 1 * 150 * 1000 );
    print "STREAM FILE silence \"\"\n";
    usleep( 1 * 100 * 1000 );
}
print "STREAM FILE silence \"\"\n";
$result = <STDIN>;
checkresult($result);
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++;
    }
}
