[asterisk-users] Asterisk dialplan best practices syntax

Steve Edwards asterisk.org at sedwards.com
Fri Jun 26 10:11:16 CDT 2015


On Fri, 26 Jun 2015, Ludovic Gasc wrote:

> 1. What's the "official" notation of each line: "=>" or "=" ? In the 
> wiki of Asterisk, I see very often "=>", however, what's the reason for 
> both syntaxes authorized ? Historical ?

I'm not 'official,' but I have a strong preference for just '=.' Using 
'=>' seems clunky, ugly, and unnecessary.

> 2. To write info in logs/console, you have two commands: NoOp and 
> Verbose. Verbose seems to be better, because you can define a log level. 
> Are you agree or another command fits better for logs ?

This is kind of a pet peeve of mine. Why use the misguided 'side effect' 
of an application when there is a specific, 'more featured,' application 
for that purpose? A 'noop' is a contraction of 'no operation' meaning it 
should do nothing but act as a placeholder.

Two other areas of 'best practices' I'm a strong believer in are: 
alphabetize wherever possible, and use white space to improve readability.

For example, here's a 'sanitized' sip.conf snippet from a popular 
provider's web site:

[xxx-inbound]
type=friend
dtmfmode=auto
host=xxx.yyy.zzz
context=inbound

username=xxx
secret=yyy

allow=all
insecure=port,invite
canreinvite=no

[xxx-outbound]
type=friend
dtmfmode=auto
host=xxx.yyy.zzz
username=xxx
fromuser=xxx
secret=xxx
trustrpid=yes
sendrpid=yes
allow=all
canreinvite=no

Pretty ugly and difficult to read. With a little whitespace and 
alphabetizing we get:

[xxx-inbound]
         allow                           = all
         canreinvite                     = no
         context                         = inbound
         dtmfmode                        = auto
         host                            = xxx.yyy.zzz
         insecure                        = port,invite
         secret                          = yyy
         type                            = friend
         username                        = xxx

[xxx-outbound]
         allow                           = all
         canreinvite                     = no
         dtmfmode                        = auto
         fromuser                        = xxx
         host                            = xxx.yyy.zzz
         secret                          = xxx
         sendrpid                        = yes
         trustrpid                       = yes
         type                            = friend
         username                        = xxx

Now, the major sections are easy to 'visually delineated.' Finding the 
'secret' is much easier now. Comparing a 'working' extension with a 
'broken' extension will be much easier as well.

I use the same formatting in the dialplan. This snippet is from 
extensions.conf.sample:

[outbound-freenum2]
exten => _X!,1,Verbose(2,Performing ISN lookup for ${EXTEN})
same => n,Set(SUFFIX=${CUT(EXTEN,*,2-)})
same => n,GotoIf($["${FILTER(0-9,${SUFFIX})}" != "${SUFFIX}"]?fn-CONGESTION,1)
same => n,Set(TIMEOUT(absolute)=10800)
same => n,Set(isnresult=${ENUMLOOKUP(${EXTEN},sip,,1,freenum.org)})
same => n,GotoIf($["${isnresult}" != ""]?from)
same => n,Set(DIALSTATUS=CONGESTION)
same => n,Goto(fn-CONGESTION,1)
same => n(from),Set(__SIPFROMUSER=${CALLERID(num)})
same => n,GotoIf($["${GLOBAL(FREENUMDOMAIN)}" = ""]?dial)
same => n,Set(__SIPFROMDOMAIN=${GLOBAL(FREENUMDOMAIN)})
same => n(dial),Dial(SIP/${isnresult},40)
same => n,Goto(fn-${DIALSTATUS},1)

exten => fn-BUSY,1,Busy()

exten => _f[n]-.,1,NoOp(ISN: ${DIALSTATUS})
same => n,Congestion()

With a little whitespace, this becomes much more readable:

[outbound-freenum2]
         exten = _X!,1,                  Verbose(2,Performing ISN lookup for ${EXTEN})
         same = n,                       Set(SUFFIX=${CUT(EXTEN,*,2-)})
         same = n,                       GotoIf($["${FILTER(0-9,${SUFFIX})}" != "${SUFFIX}"]?fn-CONGESTION,1)
         same = n,                       Set(TIMEOUT(absolute)=10800)
         same = n,                       Set(isnresult=${ENUMLOOKUP(${EXTEN},sip,,1,freenum.org)})
         same = n,                       GotoIf($["${isnresult}" != ""]?from)
         same = n,                       Set(DIALSTATUS=CONGESTION)
         same = n,                       Goto(fn-CONGESTION,1)
         same = n(from),                 Set(__SIPFROMUSER=${CALLERID(num)})
         same = n,                       GotoIf($["${GLOBAL(FREENUMDOMAIN)}" = ""]?dial)
         same = n,                       Set(__SIPFROMDOMAIN=${GLOBAL(FREENUMDOMAIN)})
         same = n(dial),                 Dial(SIP/${isnresult},40)
         same = n,                       Goto(fn-${DIALSTATUS},1)

         exten = fn-BUSY,1,              Busy()

         exten = _f[n]-.,1,              NoOp(ISN: ${DIALSTATUS})
         same = n,                       Congestion()

Compare the effort to find where the 'dial' is in the 'before' and 
'after.'

-- 
Thanks in advance,
-------------------------------------------------------------------------
Steve Edwards       sedwards at sedwards.com      Voice: +1-760-468-3867 PST
Newline                                              Fax: +1-760-731-3000



More information about the asterisk-users mailing list