Skip to main content

Stop creating a loop with *72

Posted by douglas.ross on Wed, 08/01/2012

Hi,

We had a problem with clients forwarding extensions to each other using *72. And as I couldn't find anything to stop a loop that was created, I wrote this simple script. Its a combination of the *72 function and the *96 where you can get number you're forwarded to.

When a user uses *72 to forward his number to an extension it gives a warning when this will create a loop and doesn't save the forwarding.

This is only my 2nd script I wrote for asterisk, so please only constructive criticism. And please share any improvements.

Name:
forwardingloop
used by
feature codes
Short Description
Detects if forwarding to an extension will create a loop back to the forwarded extension. Enter a 3 letter word. CFA - Call forwarding always; CFB - Call forwarding when busy or CFNA - Call forwarding on no answer
arg1
CFA/CFB/CFNA (text)
Media files (/var/lib/asterisk/sounds/custom)
ForwardingLoop


;;;;Get forwared extension(source) and number to forward to (target)

exten => s,1,Answer
exten => s,n,Set(KEY=${ARG1}N)
exten => s,n,GotoIf("${ARG1}" != "CFNA"?start)
exten => s,n,Set(KEY=CFNAN1)
exten => s,n(start),Wait(1)

;;ask for and record source extension
exten => s,n,Playback(tl/please-enter-your)
exten => s,n,Playback(tl/then-press-pound)
exten => s,n,Read(source)
exten => s,n,Wait(1)

;;ask for and record target number
exten => s,n,Playback(tl/enter-target)
exten => s,n,Playback(tl/then-press-pound)
exten => s,n,Read(target)
exten => s,n,Wait(1)

;;;check for call forwarding

;; override MYID
exten => s,n(usext),Set(MYID=${tenant}${TL_DASH}${target})

;;Check call forwarding always

exten => s,n,Set(FORWARDED=${DB(TL/${MYID}/CFA)})
exten => s,n,GotoIf($["${FORWARDED}" != "1"]?CFB) ;;;;CFA not set goto CFB
exten => s,n,Set(FORWARD_NUM=${DB(TL/${MYID}/CFAN)})
exten => s,n,Set(IS_FORWARDED=1)
exten => s,n,Goto(CHK_LOOP)

;;Check for callforwarding when busy

exten => s,n(CFB),Set(FORWARDED=${DB(TL/${MYID}/CFB)})
exten => s,n,GotoIf($["${FORWARDED}" != "1"]?CFNA) ;;;CFB not set goto CFNA
exten => s,n,Set(FORWARD_NUM=${DB(TL/${MYID}/CFBN)})
exten => s,n,Set(IS_FORWARDED=1)
exten => s,n,Goto(CHK_LOOP)

;;Check for callforwarding when not available

exten => s,n(CFNA),Set(FORWARDED=${DB(TL/${MYID}/CFNA)})
exten => s,n,GotoIf($["${FORWARDED}" != "1"]?CHECK_FORWARDING) ;;; call forwarding on no answer goto check forwarding
exten => s,n,Set(FORWARD_NUM=${DB(TL/${MYID}/CFNAN1)})
exten => s,n,Set(IS_FORWARDED=1)

exten => s,n(CHECK_FORWARDING),GotoIf($["${IS_FORWARDED}" = "1"]?CHK_LOOP)
exten => s,n,Goto(STORE)

;;target has call forwarding

exten => s,n(CHK_LOOP),GotoIf($["${FORWARD_NUM}" != "${source}"]?STORE)
exten => s,n,Playback(custom/ForwardingLoop)
exten => s,n,Goto(END)

exten => s,n(STORE),Set(DB(TL/${tenant}${TL_DASH}${source}/${ARG1})=1)
exten => s,n,Set(DB(TL/${tenant}${TL_DASH}${source}/${KEY})=${target})
exten => s,n,Playback(tl/call-fwd-${ARG1})
exten => s,n,Playback(tl/for)
exten => s,n,Playback(tl/extension)
exten => s,n,SayDigits(${source})
exten => s,n,Playback(tl/is-set-to)
exten => s,n,SayDigits(${target})
exten => s,n,Wait(1)

exten => s,n(END),Hangup


Submitted by eeman on Thu, 08/02/2012 Permalink

i find sometimes people do this crap for 'fun'. If they cant do it with 2 extensions they'll just add a 3rd into the mix to still make a loop. You cant legislate crazy, and you cant write enough code to protect against stupid :-)

Submitted by douglas.ross on Thu, 08/02/2012 Permalink

At least with this we can stop the people who does it by accident. I thought about the 3rd extension, all you can do is edit the code to do the check a 2nd time for the 2nd extensions, but then it becomes too much. At least we know this works, the 1st day after adding this we had a customer complaining he cant set the forward it plays a message about loops.