[globals]
RADIUS_Server=IP_Radius_Server(PortaBilling)
RADIUS_Secret=Password
RADIUS_Auth_Port=1812
RAIUS_Acct_Port=1813
Acct_Update_Timeout=60 
NAS_IP_Address=IP_Asterisk_Server
II=0
max_duration=7200

[general]
static=yes
autofallthrough=yes

[forbidden]
exten => _X.,1,NoOp(-- No context for host ${SIPCHANINFO(recvip)} --)
exten => _X.,n,Hangup(21)

[h323-id]
exten => _X.,1,NoOp(-- H323 ID --)
exten => _X.,n,Set(ID=${SIP_HEADER(h323-conf-id)})
exten => _X.,n,ExecIf($[ "${ID}" = "" ],Set,ID=${RAND(300000000,2000000000)}-${RAND(300000000,2000000000)}-${RAND(300000000,2000000000)}-${RAND(300000000,2000000000)})
exten => _X.,n,Set(H323_ID=${ID}) 
exten => _X.,n,Set(H323_ID_HEX=${MATH(${CUT(ID,,1)}+0,hex)} ${MATH(${CUT(ID,,2)}+0,hex)} ${MATH(${CUT(ID,,3)}+0,hex)} ${MATH(${CUT(ID,,4)}+0,hex)})
exten => _X.,n,SIPAddHeader(h323-conf-id: ${H323_ID})
exten => _X.,n,Set(CDR(h323id)=${H323_ID_HEX})
exten => _X.,n,Return()

[auth-ip]
exten => _X.,1,NoOp(-- Inbound Authentication for cusromer IP --)
exten => _X.,n,Gosub(h323-id,${EXTEN},1)
exten => _X.,n,Set(CDR(accountcode)=${SIPCHANINFO(recvip)})
exten => _X.,n,Set(MYCALLID=${SIPCALLID})
exten => _X.,n,Set(CDR(CustomerType)=IpAuth)
exten => _X.,n,agi,agi-rad-auth-2.pl|Routing=SIP&AuthorizeBy=Account&Password=cisco&H323_ID=${H323_ID}&CustomerType=IpAuth
exten => _X.,n,Set(CDR(disconnect-reason)=${h323-return-code})
exten => _X.,n,Goto(rad-auth,${EXTEN},1)
;exten => _X.,n,Goto(routing,${EXTEN},1)

[auth-inbound]
exten => _X.,1,NoOp(-- Inbound Authentication --)
exten => _X.,n,Gosub(h323-id,${EXTEN},1)
exten => _X.,n,Set(CDR(accountcode)=${SIPCHANINFO(recvip)})
exten => _X.,n,Set(MYCALLID=${SIPCALLID})
exten => _X.,n,Set(CDR(CustomerType)=PeerAuth)
exten => _X.,n,agi,agi-rad-auth-2.pl|Routing=SIP&AuthorizeBy=Account&Password=cisco&H323_ID=${H323_ID}&CustomerType=PeerAuth
exten => _X.,n,Set(CDR(disconnect-reason)=${h323-return-code})
exten => _X.,n,Goto(rad-auth,${EXTEN},1)
;exten => _X.,n,Goto(routing,${EXTEN},1)

[sip-auth]
exten => _X.,1,NoOp(-- SIP Authentication --)
exten => _X.,n,Gosub(h323-id,${EXTEN},1)
exten => _X.,n,Set(SIP_Authorization=${SIP_HEADER(Proxy-Authorization)})
exten => _X.,n,Set(CDR(CustomerType)=SipAuth)
exten => _X.,n,agi,agi-rad-auth-2.pl|Routing=SIP&AuthorizeBy=SIP&IfFailed=DoNotHangup&H323_ID=${H323_ID}&CustomerType=SipAuth
exten => _X.,n,Set(CDR(accountcode)=${SIP_Username})
exten => _X.,n,Set(CDR(disconnect-reason)=${h323-return-code})
exten => _X.,n,Goto(rad-auth,${EXTEN},1)

[rad-auth]
exten => _X.,1,NoOp()
exten => _X.,n,GotoIf($[ ${h323-return-code} = 1 ]?invalid_account,${EXTEN},1)
exten => _X.,n,GotoIf($[ ${h323-return-code} = 2 ]?wrong_passwd,${EXTEN},1)
exten => _X.,n,GotoIf($[ ${h323-return-code} = 3 ]?in_use,${EXTEN},1)
exten => _X.,n,GotoIf($[ ${h323-return-code} = 4 ]?zero_balance,${EXTEN},1)
exten => _X.,n,GotoIf($[ ${h323-return-code} = 5 ]?account_expired,${EXTEN},1)
exten => _X.,n,GotoIf($[ ${h323-return-code} = 6 ]?credit_limit,${EXTEN},1)
exten => _X.,n,GotoIf($[ ${h323-return-code} = 7 ]?user_denied,${EXTEN},1)
exten => _X.,n,GotoIf($[ ${h323-return-code} = 8 ]?not_avail,${EXTEN},1)
exten => _X.,n,GotoIf($[ ${h323-return-code} = 9 ]?no-route-exists-to-dest,${EXTEN},1)
exten => _X.,n,GotoIf($[ ${h323-return-code} = 10 ]?retries,${EXTEN},1)
exten => _X.,n,GotoIf($[ ${h323-return-code} = 12 ]?insufficient_balance,${EXTEN},1)
exten => _X.,n,GotoIf($[ ${h323-return-code} = 0 | ${h323-return-code} = 13 ]?welcome-msg,${EXTEN},1)
exten => _X.,n,Goto(cannot-complete-temp-error,${EXTEN},1)

[welcome-msg]
exten => _X.,1,NoOp()
exten => _X.,n,GotoIf($[ ${XTOT} < 1 ]?telephone-number-temp-disconnected,${EXTEN},1)
exten => _X.,n,Set(User_Agent=${SIP_HEADER(User-Agent)})
exten => _X.,n,Set(CDR(User-Agent)=User-Agent:${User_Agent})
exten => _X.,n,GotoIf($[ "${User_Agent:0:7}" = "Linksys" ]?:nomsg)
exten => _X.,n,GotoIf($[ ${h323-return-code} = 13 ]?free-call,${EXTEN},1)
exten => _X.,n,GotoIf($[ ${h323-return-code} = 0 ]?normal-call,${EXTEN},1)
exten => _X.,n(nomsg),Goto(routing,${EXTEN},1)
[free-call]
exten => _X.,1,NoOp()
exten => _X.,n,Playback(free-calls-en,noanswer)
exten => _X.,n,Playback(free-calls-fr,noanswer)
exten => _X.,n,Goto(routing,${EXTEN},1)
[normal-call]
exten => _X.,1,NoOp()
exten => _X.,n,Playback(beep,noanswer)
exten => _X.,n,Goto(routing,${EXTEN},1)

[routing]
exten => _X.,1,NoOp()
exten => _X.,n,Set(II=1)
exten => _X.,n(loop),NoOp()
exten => _X.,n,GotoIf($[ ${II} = 1 ]?SingleRoute)
exten => _X.,n,Playback(the-next,noanswer)
exten => _X.,n(SingleRoute),Set(KK=0)
exten => _X.,n,Set(SUM_ROUTE=${XSUM_${II}})
exten => _X.,n(loop1),NoOp()
exten => _X.,n,Set(TARGET_ROUTE=${XROUTE_${II}_${KK}})
exten => _X.,n,Set(TARGET_EXPIRE=${XEXP_${II}_${KK}})
exten => _X.,n,Set(TARGET_CLI=${XCLI_${II}_${KK}})
exten => _X.,n,Set(CALLERID(all)=${TARGET_CLI} <${TARGET_CLI}>)
exten => _X.,n,Set(MYDNID=${CUT(TARGET_ROUTE,@,1)})
exten => _X.,n,Set(VDNID=${CUT(MYDNID,/,2)})
exten => _X.,n,Dial(${IF($[ ${CUT(TARGET_ROUTE,@,2)} = ${NAS_IP_Address}]?${MYDNID}:${TARGET_ROUTE})},${TARGET_EXPIRE})
exten => _X.,n,Set(CDR(Hangupcause)=Hangupcause:${HANGUPCAUSE})
exten => _X.,n,Set(KK=$[${KK}+1])
exten => _X.,n,GotoIf($[ ${HANGUPCAUSE} = 1 | ${HANGUPCAUSE} = 3 | ${HANGUPCAUSE} = 27 | ${HANGUPCAUSE} = 28 | ${HANGUPCAUSE} = 31 | ${HANGUPCAUSE} = 88 | ${HANGUPCAUSE} >= 90 | "${DIALSTATUS}" = "BUSY" | "${DIALSTATUS}" = "NOANSWER" | "${DIALSTATUS}" = "CANCEL" ]?end1)
exten => _X.,n,GotoIf($[${KK} < ${SUM_ROUTE} ]?loop1:end1)
exten => _X.,n(end1),NoOp()
exten => _X.,n,Set(II=$[${II}+1])
exten => _X.,n,GotoIf($[${II} <= ${XTOT} ]?loop:end)
exten => _X.,n(end),NoOp()
exten => _X.,n,Goto(Error-Info,${EXTEN},1)

[Error-Info]
exten => _X.,1,NoOp()
exten => _X.,n,GotoIf($[ ${XTOT} > 1 ]?Ter)
exten => _X.,n,GotoIf($[ ${HANGUPCAUSE} = 28 ]?ISUP28,${EXTEN},1)
exten => _X.,n,GotoIf($[ ${HANGUPCAUSE} = 1 | ${HANGUPCAUSE} = 3 | ${HANGUPCAUSE} = 27 | ${HANGUPCAUSE} = 31 | ${HANGUPCAUSE} = 88 | ${HANGUPCAUSE} = 95 | ${HANGUPCAUSE} = 111 ]?Not-Found,${EXTEN},1)
exten => _X.,n,GotoIf($[ ${HANGUPCAUSE} >= 90 ]?cannot-complete-temp-error,${EXTEN},1)
exten => _X.,n,GotoIf($[ "${DIALSTATUS}" = "CANCEL" ]?cancelled,${EXTEN},1)
exten => _X.,n,GotoIf($[ ${HANGUPCAUSE} = 34 | ${HANGUPCAUSE} = 38 | ${HANGUPCAUSE} = 41 | ${HANGUPCAUSE} = 42 | ${HANGUPCAUSE} = 47 ]?all-outgoing-lines-unavailable,${EXTEN},1)
exten => _X.,n,GotoIf($[ "${DIALSTATUS}" = "CHANUNAVAIL" ]?an-error-has-occured,${EXTEN},1)
exten => _X.,n,GotoIf($[ "${DIALSTATUS}" = "CONGESTION" ]?cannot-complete-otherend-error,${EXTEN},1)
;exten => _X.,n(Ter),SayNumber(${HANGUPCAUSE},c)
;exten => _X.,n,Playback(beeperr,noanswer)
exten => _X.,n(Ter),Hangup(${HANGUPCAUSE})

[ISUP28]
exten => _X.,1,NoOp()
exten => _X.,1,wait(1)
exten => _X.,n,Playback(beeperr,noanswer)
exten => _X.,n,Playback(incomplete-num-en,noanswer)
exten => _X.,n,Playback(beeperr,noanswer)
exten => _X.,n,Playback(incomplete-num-fr,noanswer)
exten => _X.,n,Playback(beeperr,noanswer)
exten => _X.,n,Playback(incomplete-num-en,noanswer)
exten => _X.,n,Playback(beeperr,noanswer)
exten => _X.,n,Playback(incomplete-num-fr,noanswer)
exten => _X.,n,Playback(beeperr,noanswer)
exten => _X.,n,Playback(incomplete-num-en,noanswer)
exten => _X.,n,Playback(beeperr,noanswer)
exten => _X.,n,Playback(incomplete-num-fr,noanswer)
exten => _X.,n,Playback(beeperr,noanswer)
exten => _X.,n,Hangup(${HANGUPCAUSE})

[user_denied]
exten => _X.,1,NoOp()
exten => _X.,n,wait(1)
exten => _X.,n,Playback(beeperr,noanswer)
exten => _X.,n,Playback(user_denied,noanswer)
exten => _X.,n,Playback(beeperr,noanswer)
exten => _X.,n,Hangup(${HANGUPCAUSE})

[cannot-complete-otherend-error]
exten => _X.,1,NoOp()
exten => _X.,n,wait(1)
exten => _X.,n,Playback(beeperr,noanswer)
exten => _X.,n,Playback(cannot-complete-otherend-error,noanswer)
exten => _X.,n,Playback(beeperr,noanswer)
exten => _X.,n,Playback(cannot-complete-otherend-error,noanswer)
exten => _X.,n,Playback(beeperr,noanswer)
exten => _X.,n,Playback(cannot-complete-otherend-error,noanswer)
exten => _X.,n,Playback(beeperr,noanswer)
exten => _X.,n,Hangup(${HANGUPCAUSE})

[cancelled]
exten => _X.,1,NoOp()
exten => _X.,n,wait(1)
exten => _X.,n,Playback(beeperr,noanswer)
exten => _X.,n,Playback(cancelled,noanswer)
exten => _X.,n,Playback(beeperr,noanswer)
exten => _X.,n,Playback(cancelled,noanswer)
exten => _X.,n,Playback(beeperr,noanswer)
exten => _X.,n,Playback(cancelled,noanswer)
exten => _X.,n,Playback(beeperr,noanswer)
exten => _X.,n,Hangup(${HANGUPCAUSE})

[not_avail]
exten => _X.,1,NoOp()
exten => _X.,n,wait(1)
exten => _X.,n,Playback(beeperr,noanswer)
exten => _X.,n,Playback(not_avail,noanswer)
exten => _X.,n,Playback(beeperr,noanswer)
exten => _X.,n,Hangup(${HANGUPCAUSE})

[invalid_account]
exten => _X.,1,NoOp()
exten => _X.,n,wait(1)
exten => _X.,n,Playback(beeperr,noanswer)
exten => _X.,n,Playback(invalid_account,noanswer)
exten => _X.,n,Playback(beeperr,noanswer)
exten => _X.,n,Hangup(${HANGUPCAUSE})

[wrong_passwd]
exten => _X.,1,NoOp()
exten => _X.,n,wait(1)
exten => _X.,n,Playback(beeperr,noanswer)
exten => _X.,n,Playback(wrong_passwd,noanswer)
exten => _X.,n,Playback(beeperr,noanswer)
exten => _X.,n,Hangup(${HANGUPCAUSE})

[account_expired]
exten => _X.,1,NoOp()
exten => _X.,n,wait(1)
exten => _X.,n,Playback(beeperr,noanswer)
exten => _X.,n,Playback(account_expired,noanswer)
exten => _X.,n,Playback(beeperr,noanswer)
exten => _X.,n,Hangup(${HANGUPCAUSE})

[credit_limit]
exten => _X.,1,NoOp()
exten => _X.,n,wait(1)
exten => _X.,n,Playback(beeperr,noanswer)
exten => _X.,n,Playback(credit_limit,noanswer)
exten => _X.,n,Playback(beeperr,noanswer)
exten => _X.,n,Hangup(${HANGUPCAUSE})

[zero_balance]
exten => _X.,1,NoOp()
exten => _X.,n,wait(3)
exten => _X.,n,Playback(beeperr,noanswer)
exten => _X.,n,Playback(zero_balance,noanswer)
exten => _X.,n,Playback(beeperr,noanswer)
exten => _X.,n,Hangup(${HANGUPCAUSE})

[in_use]
exten => _X.,1,NoOp()
exten => _X.,n,wait(1)
exten => _X.,n,Playback(beeperr,noanswer)
exten => _X.,n,Playback(simul-call-limit-reached,noanswer)
exten => _X.,n,Playback(beeperr,noanswer)
exten => _X.,n,Playback(simul-call-limit-reached,noanswer)
exten => _X.,n,Playback(beeperr,noanswer)
exten => _X.,n,Playback(in_use,noanswer)
exten => _X.,n,Playback(beeperr,noanswer)
exten => _X.,n,Playback(simul-call-limit-reached,noanswer)
exten => _X.,n,Playback(beeperr,noanswer)
exten => _X.,n,Hangup(${HANGUPCAUSE})

[insufficient_balance]
exten => _X.,1,NoOp()
exten => _X.,n,wait(1)
exten => _X.,n,Playback(beeperr,noanswer)
exten => _X.,n,Playback(insufficient_balance,noanswer)
exten => _X.,n,Playback(beeperr,noanswer)
exten => _X.,n,Playback(insufficient_balance,noanswer)
exten => _X.,n,Playback(beeperr,noanswer)
exten => _X.,n,Playback(insufficient_balance,noanswer)
exten => _X.,n,Playback(beeperr,noanswer)
exten => _X.,n,Hangup(${HANGUPCAUSE})

[retries]
exten => _X.,1,NoOp()
exten => _X.,n,wait(1)
exten => _X.,n,Playback(beeperr,noanswer)
exten => _X.,n,Playback(retries,noanswer)
exten => _X.,n,Playback(beeperr,noanswer)
exten => _X.,n,Playback(retries,noanswer)
exten => _X.,n,Playback(beeperr,noanswer)
exten => _X.,n,Playback(retries,noanswer)
exten => _X.,n,Playback(beeperr,noanswer)
exten => _X.,n,Hangup(${HANGUPCAUSE})

[cannot-complete-temp-error]
exten => _X.,1,NoOp()
exten => _X.,n,wait(1)
exten => _X.,n,Playback(beeperr,noanswer)
exten => _X.,n,Playback(cannot-complete-temp-error,noanswer)
exten => _X.,n,Playback(beeperr,noanswer)
exten => _X.,n,Playback(cannot-complete-temp-error,noanswer)
exten => _X.,n,Playback(beeperr,noanswer)
exten => _X.,n,Playback(cannot-complete-temp-error,noanswer)
exten => _X.,n,Playback(beeperr,noanswer)
exten => _X.,n,Hangup(${HANGUPCAUSE})

[no-route-exists-to-dest]
exten => _X.,1,NoOp()
exten => _X.,n,wait(1)
exten => _X.,n,Playback(beeperr,noanswer)
exten => _X.,n,Playback(no-route-exists-to-dest,noanswer)
exten => _X.,n,Playback(beeperr,noanswer)
exten => _X.,n,Playback(no-route-exists-to-dest,noanswer)
exten => _X.,n,Playback(beeperr,noanswer)
exten => _X.,n,Playback(no-route-exists-to-dest,noanswer)
exten => _X.,n,Playback(beeperr,noanswer)
exten => _X.,n,Playback(cld_blocked,noanswer)
exten => _X.,n,Playback(beeperr,noanswer)
exten => _X.,n,Hangup(${HANGUPCAUSE})

[Not-Found]
exten => _X.,1,NoOp()
exten => _X.,n,Set(NotFoundContext=Not-Found-${IF($[ ${CUT(TARGET_ROUTE,@,2)} = ${NAS_IP_Address}]?1:2)})
exten => _X.,n,Goto(${NotFoundContext},${EXTEN},1)
[Not-Found-1]
exten => _X.,1,NoOp()
exten => _X.,n,wait(1) 
exten => _X.,n,Playback(beeperr,noanswer)
exten => _X.,n,Playback(not-found-en,noanswer)
exten => _X.,n,Playback(beeperr,noanswer)
exten => _X.,n,Playback(not-found-fr,noanswer)
exten => _X.,n,Playback(beeperr,noanswer)
exten => _X.,n,Playback(not-found-en,noanswer)
exten => _X.,n,Playback(beeperr,noanswer)
exten => _X.,n,Playback(not-found-fr,noanswer)
exten => _X.,n,Playback(beeperr,noanswer)
exten => _X.,n,Playback(not-found-en,noanswer)
exten => _X.,n,Playback(beeperr,noanswer)
exten => _X.,n,Playback(not-found-fr,noanswer)
exten => _X.,n,Playback(beeperr,noanswer)
exten => _X.,n,Hangup(${HANGUPCAUSE})
[Not-Found-2]
exten => _X.,1,NoOp()
exten => _X.,n,wait(1) 
exten => _X.,n,Playback(beeperr,noanswer)
exten => _X.,n,Playback(you-dialed-wrong-number,noanswer)
exten => _X.,n,Playback(beeperr,noanswer)
exten => _X.,n,Playback(you-dialed-wrong-number,noanswer)
exten => _X.,n,Playback(beeperr,noanswer)
exten => _X.,n,Playback(you-dialed-wrong-number,noanswer)
exten => _X.,n,Playback(beeperr,noanswer)
exten => _X.,n,Hangup(${HANGUPCAUSE})

[telephone-number-temp-disconnected]
exten => _X.,1,NoOp()
exten => _X.,n,wait(1)
exten => _X.,n,Playback(beeperr,noanswer)
exten => _X.,n,Playback(the-number-u-dialed,noanswer)
exten => _X.,n,Playback(temp-disconnected,noanswer)
exten => _X.,n,Playback(beeperr,noanswer)
exten => _X.,n,Playback(the-number-u-dialed,noanswer)
exten => _X.,n,Playback(temp-disconnected,noanswer)
exten => _X.,n,Playback(beeperr,noanswer)
exten => _X.,n,Playback(the-number-u-dialed,noanswer)
exten => _X.,n,Playback(temp-disconnected,noanswer)
exten => _X.,n,Playback(beeperr,noanswer)
exten => _X.,n,Hangup(${HANGUPCAUSE})

[all-outgoing-lines-unavailable]
exten => _X.,1,NoOp()
exten => _X.,n,wait(1)
exten => _X.,n,Playback(beeperr,noanswer)
exten => _X.,n,Playback(all-outgoing-lines-unavailable,noanswer)
exten => _X.,n,Playback(beeperr,noanswer)
exten => _X.,n,Playback(all-outgoing-lines-unavailable,noanswer)
exten => _X.,n,Playback(beeperr,noanswer)
exten => _X.,n,Playback(all-outgoing-lines-unavailable,noanswer)
exten => _X.,n,Playback(beeperr,noanswer)
exten => _X.,n,Hangup(${HANGUPCAUSE})

[an-error-has-occured]
exten => _X.,1,NoOp()
exten => _X.,n,wait(1)
exten => _X.,n,Playback(beeperr,noanswer)
exten => _X.,n,Playback(an-error-has-occured,noanswer)
exten => _X.,n,Playback(beeperr,noanswer)
exten => _X.,n,Playback(an-error-has-occured,noanswer)
exten => _X.,n,Playback(beeperr,noanswer)
exten => _X.,n,Playback(an-error-has-occured,noanswer)
exten => _X.,n,Playback(beeperr,noanswer)
exten => _X.,n,Hangup(${HANGUPCAUSE})

