From 92f58df5134f9ba64bf2246fff9ce6195cbdf5c8 Mon Sep 17 00:00:00 2001 From: mattf Date: Mon, 11 May 2026 19:18:19 +0000 Subject: [PATCH] Added CID_change.agi script for CID-group CID manual dial direct from phones git-svn-id: svn://192.168.202.10@3990 3d104415-ff17-0410-8863-d5cf3c621b8a --- agc_2-X/trunk/agi/CID_change.agi | 340 +++++++++++++++++++++++++++++++ 1 file changed, 340 insertions(+) create mode 100644 agc_2-X/trunk/agi/CID_change.agi diff --git a/agc_2-X/trunk/agi/CID_change.agi b/agc_2-X/trunk/agi/CID_change.agi new file mode 100644 index 00000000..579771b0 --- /dev/null +++ b/agc_2-X/trunk/agi/CID_change.agi @@ -0,0 +1,340 @@ +#!/usr/bin/perl +# +# CID_change.agi +# +# Changes the CID number on calls that pass through it to the cid_group_id +# defined in the flag below OR a specific CID number in that place. +# If a CID Group is used, script will try to find CIDs within phone number +# dialed areacode. If none are found, it will grab a random active CID in the +# CID group to use. +# +# Flag Options: (separated by three dashes '---') +# 1- Fixed CID number, or a valid CID Group ID +# 2- number of digits to strip before the phone number +# 3- number of digits in the area-code +# 4- should always be ${EXTEN} +# +# You need to have lines similar to those below in your Custom Dialplan: +# +#; change CID to a CID within the 'testCIDgroup' CID Group: +#exten => _91NXXNXXXXXX,1,AGI(CID_change.agi,testCIDgroup---2---3---${EXTEN}) +#exten => _91NXXNXXXXXX,n,Goto(default,${EXTEN},1) +# +#; change CID to a fixed CID: +#exten => _91NXXNXXXXXX,1,AGI(CID_change.agi,9998887112---------${EXTEN}) +#exten => _91NXXNXXXXXX,n,Goto(default,${EXTEN},1) +# +# The easiest way to set this up for a phone is to create a new Call Menu and +# then put the above Custom Dialplan into it, then set the Phone's +# 'phone context' to the call menu ID that you just created. +# +# Copyright (C) 2026 Matt Florell LICENSE: AGPLv2 +# +# changes: +# 260509-1847 - First build +# + +$script = 'CID_change.agi'; + +$AGILOG=1; +$FD=0; # force debug for testing +$new_cid=''; +$temp_new_cid=''; +$cid_group_id=''; +$cid_count=0; + +($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"; + +use Asterisk::AGI; +$AGI = new Asterisk::AGI; + +### 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]); + + $new_cid = $ARGV_vars[0]; + $strip_digits = $ARGV_vars[1]; + $ac_digits = $ARGV_vars[2]; + $dialed_exten = $ARGV_vars[3]; + $phone_ac = substr("$dialed_exten", $strip_digits, $ac_digits); + if ($AGILOG) {$agi_string = "Exten dialed: $dialed_exten AC: $phone_ac"; &agi_output;} + $cid_group_id = $new_cid; + } + + + +# 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/changecid.$year-$mon-$mday";} + + +$|=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_accountcode\:\s+(.*)$/) {$accountcode = $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 (length($callerid)>0) {$phone_number = $callerid;} + else {$phone_number = '';} +if (length($calleridname)>0) {$VLcomments = $calleridname;} + else {$VLcomments = '';} + +foreach $i (sort keys %AGI) + { + if ($AGILOG) {$agi_string = " -- $i = $AGI{$i}"; &agi_output;} + } + +if ( (length($callerid)>20) && ($callerid =~ /\"\S\S\S\S\S\S\S\S\S\S\S\S\S\S\S\S\S\S/) ) + { + $calleridname =~ s/^\"//gi; + $calleridname =~ s/\".*$//gi; + } + +use DBI; +use Time::HiRes ('gettimeofday','usleep','sleep'); # necessary to have perl sleep command of less than one second + +$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 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]; + $local_gmt = $aryA[1]; + if ($DBagi_output =~ /STDERR/) {$AGILOG = '1';} + if ($DBagi_output =~ /FILE/) {$AGILOG = '2';} + if ($DBagi_output =~ /BOTH/) {$AGILOG = '3';} + $rec_count++; + } +$sthA->finish(); + + +$phone_extension = $channel; +$phone_extension =~ s/^SIP\/|^IAX\/|^IAX2\///gi; +$phone_extension =~ s/-.*//gi; +if ($AGILOG) {$agi_string = "Phone Account: ($phone_extension)"; &agi_output;} + + +# look for CID Group, if one exists look for CID in group, otherwise use defined CID as new CID +if (length($new_cid) > 0) + { + $stmtA= "SELECT cid_group_notes from vicidial_cid_groups where cid_group_id='$cid_group_id';"; + $sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr; + $sthA->execute or die "executing: $stmtA ", $dbhA->errstr; + $sthArows=$sthA->rows; + if ($AGILOG) {$agi_string = "$sthArows|$stmtA|"; &agi_output;} + if ($sthArows > 0) + { + @aryA = $sthA->fetchrow_array; + $cid_group_notes = $aryA[0]; + } + $sthA->finish(); + + if ($sthArows > 0) + { + $stmtA= "SELECT count(*) from vicidial_campaign_cid_areacodes where campaign_id='$cid_group_id' and active='Y';"; + $sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr; + $sthA->execute or die "executing: $stmtA ", $dbhA->errstr; + $sthArowsC=$sthA->rows; + if ($AGILOG) {$agi_string = "$sthArowsC|$stmtA|"; &agi_output;} + if ($sthArowsC > 0) + { + @aryA = $sthA->fetchrow_array; + $cid_count = $aryA[0]; + } + $sthA->finish(); + + if ($cid_count > 0) + { + $stmtA= "SELECT outbound_cid from vicidial_campaign_cid_areacodes where campaign_id='$cid_group_id' and active='Y' and areacode='$phone_ac' order by RAND() limit 1;"; + $sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr; + $sthA->execute or die "executing: $stmtA ", $dbhA->errstr; + $sthArowsD=$sthA->rows; + if ($AGILOG) {$agi_string = "$sthArowsD|$stmtA|"; &agi_output;} + if ($sthArowsD > 0) + { + @aryA = $sthA->fetchrow_array; + $temp_new_cid = $aryA[0]; + } + $sthA->finish(); + + if (length($temp_new_cid) < 1) + { + if ($AGILOG) {$agi_string = "CID Group ID no AC CIDs found, grabbing random CID in group: $cid_group_id|$calleridname"; &agi_output;} + + $stmtA= "SELECT outbound_cid from vicidial_campaign_cid_areacodes where campaign_id='$cid_group_id' and active='Y' order by RAND() limit 1;"; + $sthA = $dbhA->prepare($stmtA) or die "preparing: ",$dbhA->errstr; + $sthA->execute or die "executing: $stmtA ", $dbhA->errstr; + $sthArowsD=$sthA->rows; + if ($AGILOG) {$agi_string = "$sthArowsD|$stmtA|"; &agi_output;} + if ($sthArowsD > 0) + { + @aryA = $sthA->fetchrow_array; + $temp_new_cid = $aryA[0]; + } + $sthA->finish(); + } + else + { + if ($AGILOG) {$agi_string = "CID Group ID - AC CID found in AC: $cid_group_id|$calleridname"; &agi_output;} + } + + if ($AGILOG) {$agi_string = "New CID Group CID to use: $temp_new_cid|$cid_group_id|$calleridname"; &agi_output;} + $new_cid = $temp_new_cid; + } + else + { + if ($AGILOG) {$agi_string = "CID Group ID empty: $cid_group_id|$calleridname"; &agi_output;} + } + } + else + { + if ($AGILOG) {$agi_string = "CID Group ID not found: $cid_group_id|$calleridname"; &agi_output;} + } + + ### set the callerid to the new value + $newcallerid = "\"$calleridname <$new_cid>\""; + $AGI->set_callerid($newcallerid); + + print STDERR "Setting New CID \"$newcallerid\"\n"; + checkresult($result); + if ($AGILOG) {$agi_string = "callerID changed: $newcallerid"; &agi_output;} + } +else + { + if ($AGILOG) {$agi_string = "No new CID defined: $new_cid|$calleridname"; &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 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=''; +}