Asterisk tips renumber extens
Created by: gray,Last modification on Mon 03 of Jan, 2005 [22:28 UTC]
Adding a new rule in the middle of extensions.conf can be painful if you need to renumber the priorites of things below it.
I wrote a little perl script to renumbers the priorities.
It looks at rules in the Extension Patterns in sets. a set consists a number of lines of the form
extern => extension,priority,Command(parameters)
Anything other then a comment line or another exten line will end the set . If the extension changes that also ends the set.
The script leaves the priority of the first rule in the set alone, and increments the priority for each of the remaining rules in the set.
so:
exten => 202,1,Dial(${PHONE001},30,rt)
exten => 202,2,Voicemail(u202)
exten => 202,3,Hangup
exten => 202,102,Voicemail(u202)
exten => 202,103,Hangup
becomes:
exten => 202,1,Dial(${PHONE001},30,rt)
exten => 202,2,Voicemail(u202)
exten => 202,3,Hangup
exten => 202,4,Voicemail(u202)
exten => 202,5,Hangup
So here's the script:
I wrote a little perl script to renumbers the priorities.
It looks at rules in the Extension Patterns in sets. a set consists a number of lines of the form
extern => extension,priority,Command(parameters)
Anything other then a comment line or another exten line will end the set . If the extension changes that also ends the set.
The script leaves the priority of the first rule in the set alone, and increments the priority for each of the remaining rules in the set.
so:
exten => 202,1,Dial(${PHONE001},30,rt)
exten => 202,2,Voicemail(u202)
exten => 202,3,Hangup
exten => 202,102,Voicemail(u202)
exten => 202,103,Hangup
becomes:
exten => 202,1,Dial(${PHONE001},30,rt)
exten => 202,2,Voicemail(u202)
exten => 202,3,Hangup
exten => 202,4,Voicemail(u202)
exten => 202,5,Hangup
So here's the script:
#! /usr/bin/perl
my $state = 1; # 1 for not in a run; 2 for in a run
my $next;
while ( <> )
{
# if this line is an exten rule
if ( /^(\s*exten\s+=>)(\s+\w+,)(\d+)(,.*)$/ )
{
# if we are in a set of rules
# just give this rule the next number
if ( $state == 2 && $prefix eq $2 )
{
print "$1$2$next$4\n";
$next++;
}
# If we aren't in a set or
# we were in a set but the
# prefix changed (if we are in a set and the
# prefix matches, we'd hit the rule above)
# start a new set
elsif ( $state == 1 || $state == 2 )
{
# we are in a new state
$prefix = $2;
$next = $3;
$next++;
print;
$state = 2;
}
}
elsif ( /^\s*;/ )
{
# just a comment don't change state
print;
}
else
{
# if we see anything else, assume the end of the set
# was reached, and put us back into the "not in
# a run" state.
print;
$state = 1;
}
}
Comments
333Two things missing
1. If there are any BRANCHES (Goto, GotoIf, etc.) then the target of the branch needs to be renumbered to its new number as well.
2. If there are implicit branches (goto ext+101, ext+51, etc.) then those should be renumbered at the same offset, not as a sequence of 1.
So, for example,
example
exten => 100,1,NoOp,This is an example
exten => 100,3,Dial(SIP/mysipphone) ;;;;; NOTE 100,4,DBGet(voicemail=vmail/mysipphone)
exten => 100,105,Goto(20)
exten => 100,5,Goto(vmail,${voicemail},1)
exten => 100,20,Playback(recorded/no-such-voicemail)
exten => 100,21,Hangup
Would become:
exten => 100,1,NoOp,This is an example
exten => 100,2,Dial(SIP/mysipphone) ;;; RENUMBERED
exten => 100,3,DBGet(voicemail=vmail/mysipphone)
exten => 100,104,Goto(5) ;;;; Branch target renumbered
exten => 100,4,Goto(vmail,${voicemail},1)
exten => 100,5,Playback(recorded/no-such-voicemail)
exten => 100,6,Hangup
Ehud