Mysql or perl to match the asterisk dialog pattern
I have an old version of freePBX (over 5000 extensions, hundreds of IVRs) that I have to document to upgrade to a newer version. I have to match which IVRs are using which chests. For this, I need to match the number being dialed to the outgoing route dialing pattern.
The "extensions" column of the template table to match looks like
19328555
_13XXXX
_1933370[0-2]
_2805XX
_28[3-7]XXX
_331XXX
_848XXX
_85XXXXX
_879XXX
For example, I have to find which "extensions" pattern matches the number 8481234, then I can grab a connecting line from another column.
I know there must be a built-in function in Asterisk that works like
$number='8481234';
$pattern='_879XXX';
if (asterisk_pattern_match($number,$pattern)) {
#get trunk column from that row
}
It can be SQL, or Perl, or PHP. I could write this, but I am sure I will reinvent the wheel. Does anyone know or knows where such a function might be? I have googled every path I can think of, but the results are all about using MySQL on the asterisk dial plan and it doesn't make any difference to me.
Thank!
source to share
You can use the following script to find matches, combined with the results dialplan show extension@context
you run in the Asterisk CLI, this will show you the order in which the matches are executed.
#!/usr/bin/env perl
use strict;
use warnings;
my $numbers = [
"8481234", "8581234", "1283123"
];
my $patterns = [
"19328555" , "_13XXXX" , "_1933370[0-2]" ,
"_2805XX" , "_28[3-7]XXX" , "_331XXX" ,
"_848XXX" , "_85XXXXX" , "_879XXX" ,
];
# Lets turn partterns into usable regex, based on the reference:
# https://wiki.asterisk.org/wiki/display/AST/Pattern+Matching
foreach my $r (@$patterns)
{
$r =~ s/_/^/; # Proper regex starts with
$r =~ s/X|x/\\d/g; # Replace X with any digit
$r =~ s/Z|z/[1-9]/g; # Replace Z with 1-9 as per spec
$r =~ s/N|m/[2-9]/g; # Replace N with 2-9 as per spec
my @matches = grep(/$r/i, @$numbers);
print "Matching numbers for: ", $r, " are: ", join(', ', @matches), "\n";
}
source to share
Thanks, everyone. I found the exact program I was looking for in
https://gist.github.com/lgaetz/8695182
It's called match_pattern.php, modified and hosted on git by Lorne Gaet.
Description: Two PHP functions, match_pattern and match_pattern_all, that compare a numeric string with an Asterisk collection pattern (or array of patterns) and return a modified numeric string.
source to share