; ===========================================================================
;          Standard Extension Macro
; ===========================================================================
[macro-stdexten];
;
; Standard extension macro:
;   ${ARG1} - Extension
;   ${ARG2} - Device(s) to ring
;   ${ARG3} - Timeout for dialing extension

exten = s,1,NoOp(${ARG1},${ARG2},${ARG3})
exten = s,2,SetVar(lexten=${ARG1})
exten = s,3,SetVar(ringdev=${ARG2})   
exten = s,4,SetVar(timeout=${ARG3})
exten = s,5,Goto(s-GF,1)

; ----- Get Forwarding ------
exten = s-GF,1,DBget(forward_number=FORWARD/${lexten})   ; Get FORWARD key, or goto 102
exten = s-GF,2,DBget(forward_mode=FORWARD/${lexten}-mode)
exten = s-GF,3,DBget(forward_timeout=FORWARD/${lexten}-timeout)
exten = s-GF,4,NoOp(forward_number=${forward_number},forward_mode=${forward_mode},forward_timeout=${forward_timeout})
exten = s-GF,5,Goto(s-DND,1)

exten = s-GF,102,Goto(s-DND,1)                 ; No FORWARD key (or fallthrough)
exten = s-GF,103,Congestion
exten = s-GF,104,Congestion

; ----- Do Not Disturb ------
exten = s-DND,1,GotoIf($["${dnd_override}" = "YES"]?104:2)
exten = s-DND,2,DBget(dnd=DND/${lexten})  ; Get DND key, or jump to 103
exten = s-DND,3,SetVar(dnd_exten=${lexten})
exten = s-DND,4,SetVar(dnd_context=${MACRO_CONTEXT})
exten = s-DND,5,Background(user_sounds/${lexten}-dnd)    ; Play DND message
exten = s-DND,6,WaitExten(4)
; Any extension in the current context can be pressed here.  If the 
; dnd_override_extension is pressed, this macro will be re-called
; with dnd_override set to YES.
exten = s-DND,7,Goto(s-NOANSWER,1)
exten = s-DND,103,Goto(s-IF,1)            ; Normal call, DND not set
exten = s-DND,104,Goto(s-IF,1)            ; DND override

; ----- Immediate Forwarding ------
exten = s-IF,1,GotoIf($["${forward_mode}" = "immediate"]?2:6)
exten = s-IF,2,NoOp(Immediate forwarding)
exten = s-IF,3,Macro(monitor-begin,${lexten},${CALLERIDNUM},${lexten})
exten = s-IF,4,Dial(Local/${forward_number}@internal&${ringdev},${forward_timeout},n)
exten = s-IF,5,Goto(s-${DIALSTATUS},1)         ; Jump based on status (NOANSWER,BUSY,
                                                ;        CHANUNAVAIL,CONGESTION,ANSWER)
exten = s-IF,6,Goto(s-NORMAL,1)

; ----- Normal dial ------
exten = s-NORMAL,1,Macro(monitor-begin,${lexten},${CALLERIDNUM},${lexten})
exten = s-NORMAL,2,Dial(${ringdev},${timeout},n)
exten = s-NORMAL,3,Goto(s-NF,1)

; ----- Forward if needed ------
exten = s-NF,1,GotoIf($["${forward_mode}" = "on"]?2:6)
exten = s-NF,2,NoOp(Forwarding after normal dial failed)
exten = s-NF,3,Playback(user_sounds/forward/forwarding)
exten = s-NF,4,Dial(Local/${forward_number}@internal,${forward_timeout},n)
exten = s-NF,5,Goto(s-${DIALSTATUS},1)         ; Jump based on status (NOANSWER,BUSY,
                                                ;        CHANUNAVAIL,CONGESTION,ANSWER)
exten = s-NF,6,Goto(s-NOANSWER,1)

; ----- Dialstatus targets ------
exten = s-NOANSWER,1,Voicemail(${lexten})	    ; send to voicemail w/ unavail announce
exten = s-NOANSWER,2,Hangup

exten = s-BUSY,1,Voicemail(${lexten})		    ; send to voicemail w/ busy announce
exten = s-BUSY,2,Hangup

exten = _s-.,1,Goto(s-NOANSWER,1)			    ; Treat anything else as no answer

exten = a,1,VoicemailMain(${lexten})			    ; if *, send the user into VoicemailMain

