Replacement ENUM Lookup Macro for FreePBX

Asterisk enum lookups really really suck in the current stable versions, and Rob tried to work round this but he didn’t really have a workable fix, I spent a bit of time working on this problem yesterday and below is the results of the labour, you should be able to just drop it in and off you go, it even filters duplicate results etc.

; This has been violently beaten upon by Rob Thomas, [email protected]
; to 1 Be compliant with all the depreciated bits in asterisk 1.2 and
; above, and 2 to give a good shot at attempting to be compliant with
; RFC3761 by honouring the order in which records are returned.
exten => s,1,GotoIf($["${ARG3}" = ""]PASSWD?NOPASSWD); arg3 is pattern password 
exten => s,n(PASSWD),Authenticate(${ARG3}) ­
exten => s,n(NOPASSWD),Macro(user-callerid,SKIPTTL) 
exten => s,n,Macro(record-enable,${CALLERID(number)},OUT)
exten => s,n,Macro(outbound-callerid,${ARG1})
exten => s,n,Set(GROUP()=OUT_${ARG1})
exten => s,n,GotoIf($[ ${GROUP_COUNT()} > ${OUTMAXCHANS_${ARG1}} ]?nochans)
exten => s,n,Set(DIAL_NUMBER=${ARG2})
exten => s,n,Set(DIAL_TRUNK=${ARG1})
exten => s,n,DeadAGI(fixlocalprefix) ; this sets DIAL_NUMBER to the proper dial string for this trunk
exten => s,n,Set(COUNT=0)
exten => s,n,Set( ; enum networks to check
exten => s,n,GotoIf($["${DIAL_NUMBER01}" = "+"]?begin) ; Skip next line if it already is prefixed by a plus
exten => s,n,Set(DIAL_NUMBER=+${DIAL_NUMBER}) ;Add a plus to the start, becasue ENUMLOOKUP needs it.

; start of main network loop
exten => s,n(begin),NoOp(E164NETWORKS is ${E164NETWORKS})
exten => s,n,GotoIf($["${E164NETWORKS12}"=""]?loop2)
exten => s,n,Set(ENUMNET=${CUT(E164NETWORKS,-,1)})
exten => s,n,Set(E164NETWORKS=${CUT(E164NETWORKS,-,2-)})

exten => s,n,NoOp(E164NETWORKS is now ${E164NETWORKS})
exten => s,n,NoOp(ENUMNET is ${ENUMNET})

exten => s,n,Set(ENUMCOUNT=${ENUMLOOKUP(${DIAL_NUMBER},all,c,${ENUMNET})})
exten => s,n,Set(ENUMPTR=0)
exten => s,n,Set(LOOKUPBUG=0)
exten => s,n,Set(DECCOUNT=0)

exten => s,n,GotoIf($["${ENUMCOUNT}"=""]?begin)

; start of main lookup loop
exten => s,n(startloop),NoOp(Is ${ENUMPTR} >= ${ENUMCOUNT} goto begin)
exten => s,n,GotoIf($[${ENUMPTR} >= ${ENUMCOUNT}]?begin)

; Now, let's start through them.
exten => s,n,NoOp(Doing ENUMLOOKUP(${DIAL_NUMBER},all,${ENUMPTR},${ENUMNET}))
exten => s,n,Set(ENUM=${ENUMLOOKUP(${DIAL_NUMBER},all,${ENUMPTR},${ENUMNET})})
exten => s,n,GotoIf($[${DECCOUNT} >= 1]?dec)
exten => s,n,Set(DECCOUNT=1)
exten => s,n,Set(ENUMPTR=$[${ENUMPTR}+1])
exten => s,n,Goto(skip)
exten => s,n(dec),Set(DECCOUNT=0)

; Deal with reponse
exten => s,n(skip),GotoIf($["${ENUM03}" = "sip" ]?sipuri)
exten => s,n,GotoIf($["${ENUM04}" = "iax2" ]?iaxuri)
; It doesn't matter if you don't have h323 enabled, as when it tries to dial, it cares
; about dialstatus and retries if there are any enum results left.
exten => s,n,GotoIf($["${ENUM04}" = "h323" ]?h323uri)

; OK. If we're here, we've still got some enum entries to go through. Back to 
; the start with you!
exten => s,n,Goto(startloop)

; If the prefix is 'sip'...
exten => s,n(sipuri),Set(DIALSTR=SIP/${ENUM4})
exten => s,n,Goto(dodial)

; If it's IAX2...
exten => s,n(iaxuri),Set(DIALSTR=IAX2/${ENUM5})
exten => s,n,Goto(dodial)

; Or even if it's H323.
exten => s,n(h323uri),Set(DIALSTR=H323/${ENUM5})

exten => s,n(dodial),Set(tmp=0)
exten => s,n(tmploop),GotoIf($[${tmp} >= ${COUNT}]?add)
exten => s,n,GotoIf($["${DIALSTRING${tmp}}"="${DIALSTR}"]?startloop)
exten => s,n,Set(tmp=$[${tmp}+1])
exten => s,n,Goto(tmploop)
exten => s,n(add),Set(DIALSTRING${COUNT}=${DIALSTR})
exten => s,n,Set(COUNT=$[${COUNT}+1])
exten => s,n,Goto(startloop)

exten => s,n(loop2),Set(counter=0)
exten => s,n(startloop2),NoOp(If ${counter} >= ${COUNT} goto failedtotally)
exten => s,n,GotoIf($[${counter} >= ${COUNT}]?failedtotally)
exten => s,n,NoOp(Checking ${counter} which is set to ${DIALSTRING${counter}})
exten => s,n,Set(DIALSTR=${DIALSTRING${counter}})
exten => s,n,Set(counter=$[${counter}+1])
exten => s,n,Dial(${DIALSTR})
exten => s,n,NoOp(Dial exited in macro-enum-dialout with ${DIALSTATUS})

; Now, if we're still here, that means the Dial failed for some reason. 
; If it's CONGESTION or CHANUNAVAIL we probably want to try again on a
; different channel. However, if it's the last one, we don't have any
; left, and I didn't keep any previous dialstatuses, so hopefully 
; someone looking throught the logs would have seen the NoOp's
exten => s,n,GotoIf($[${counter} >= ${COUNT}]?dialfailed)
exten => s,n,GotoIf($["${DIALSTATUS}"="CHANUNAVAIL"|"${DIALSTATUS}"="CONGESTI­ON"]?startloop2)

; If we're here, then it's BUSY or NOANSWER or something and well, deal with it.
exten => s,n(dialfailed),Goto(s-${DIALSTATUS},1)

exten => s,n(failedtotally),NoOp(EnumLookupfailed -- no more networks to try)
exten => s,n,Goto(end)

exten => s,n(nochans),NoOp(max channels used up)

exten => s,n(end),NoOp(Exiting macro-dialout-enum)

exten => s-BUSY,1,NoOp(Trunk is reporting BUSY)
exten => s-BUSY,2,Busy(20)

exten => _s-.,1,NoOp(Dial failed due to ${DIALSTATUS})

Were these changes ever put into free pbx?
(providing that this is the same evilbunny from sipbroker and 614 website)

If so which version?

Thank you