4

I have an old version of freePBX (over 5000 extensions, hundreds of IVRs) that I must document for migration to newer version. I must map what IVRs use which trunks. To do this, I must match the number being dialed to the dial pattern of the outbound route.

The 'extensions' column of the table with patterns I must match looks like

19328555
_13XXXX
_1933370[0-2]
_2805XX
_28[3-7]XXX
_331XXX
_848XXX
_85XXXXX
_879XXX

For example I must find which 'extensions' pattern matches the number 8481234 then I can grab the trunk from another column.

I know there must be a function embedded in Asterisk that works like

$number='8481234';
$pattern='_879XXX';
    if (asterisk_pattern_match($number,$pattern)) {
       #get trunk column from that row
    }

It could be SQL, or Perl or PHP. I could write it, but I'm sure I would be reinventing the wheel. Does anyone have or know where a function like this may be? I have googled every way I can think of, but all the results are about using MySQL within the asterisk dial plan, and that is of no value to me.

Thanks!

jerryrig
  • 125
  • 11
  • In PHP: [fnmatch()](http://www.php.net/manual/en/function.fnmatch.php); [sscanf](http://www.php.net/manual/en/function.sscanf.php); or a [regular expression](http://www.php.net/manual/en/function.preg-match.php).... perhaps one of those can be used to match what you need – Mark Baker Aug 06 '15 at 17:53
  • Thanks, Mark. That will be helpful if I have to write the function myself, but the point of the question is to find an existing solution so I don't have to. The pattern matching in the asterisk dialplan can get rather complex. – jerryrig Aug 06 '15 at 18:29
  • AFAIK, there isn't any documented publicly available asterisk function to do what you want, but it's been a few years since I've been in the group that works with our asterisk servers, so I can't say for sure. You'll need to write your own parser or look through the source code to see if you can call their backend function(s) that do that matching when routing calls. – Ron Bergin Aug 06 '15 at 21:01

2 Answers2

3

Thanks, everyone. I found the exact program I was looking for at

https://gist.github.com/lgaetz/8695182

It is called match_pattern.php, modified and posted on git by Lorne Gaetz.

Description: Two PHP functions, match_pattern and match_pattern_all that compare a numeric string against an Asterisk dial pattern (or array of patterns) and return a modified numeric string.

jerryrig
  • 125
  • 11
2

You can use the following script to find matches, in combination with the results from dialplan show extension@context which you run on Asterisk CLI, this will show you the order in which the matches will execute.

#!/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";
}
harvey
  • 2,945
  • 9
  • 10