[local]
;check voicemails
exten => *97,1,VoiceMailMain(${CALLERID(num)}@voicemail)
exten => *98,1,VoiceMailMain(@voicemail)

;Direct dial a users voicemail
exten => _*1XX,1,Voicemail(${EXTEN:1}@voicemail,su)
exten => _*1XX,2,Hangup

; Record ivr
exten => 903,1,Wait(2) ; Call 903 to Record new Sound Files
exten => 903,n,Record(custom-sounds/ivr.gsm) ; Press # to stop recording
exten => 903,n,Wait(2)
exten => 903,n,Playback(custom-sounds/ivr) ; Listen to your voice
exten => 903,n,wait(2)
exten => 903,n,Hangup

;Playback ivr
exten => 904,1,Wait(1)
exten => 904,n,Playback(custom-sounds/ivr)
exten => 904,n,Hangup

exten => h,1,Hangup

[outbound]
exten => _NXXNXXXXXX,1,Set(CALLERID(all)="Test" <5551212>)
exten => _NXXNXXXXXX,n,Dial(SIP/${EXTEN}@outbound)

[incoming]
exten => 5551212,1,Verbose(1,Test Line)
        same => n,Dial(SIP/101,18)
        same => n,VoiceMail(101@voicemail,su)
        same => n,Hangup()

exten => s-BUSY,1,Verbose(2, Call from ${CALLERID(num)} to ${CHANNEL} FAILED - extension returned busy)
exten => s-BUSY,n,Answer
exten => s-BUSY,n,Background(custom-sounds/extension-offline)


exten => s-CHANUNAVAIL,1,Verbose(2, Call from ${CALLERID(num)} to ${CHANNEL} FAILED - possibly not registered/chanunavail)
exten => s-CHANUNAVAIL,n,Answer
exten => s-CHANUNAVAIL,n,Background(custom-sounds/extension-offline)
        
[users]
exten => 101,1,Macro(stdexten,101,SIP/0004f2ab6043,20)

exten => s-BUSY,1,Verbose(2, Call from ${CALLERID(num)} to ${CHANNEL} FAILED - extension returned busy)
exten => s-BUSY,n,Answer
exten => s-BUSY,n,Background(custom-sounds/extension-offline)


exten => s-CHANUNAVAIL,1,Verbose(2, Call from ${CALLERID(num)} to ${CHANNEL} FAILED - possibly not registered/chanunavail)
exten => s-CHANUNAVAIL,n,Answer
exten => s-CHANUNAVAIL,n,Background(custom-sounds/extension-offline)

[devices]
include => local
include => outbound
include => users

exten => h,1,Verbose(2, Call hungup ${CALLERID(num)} to ${CHANNEL} FAILED - extension ended call)
exten => h,n,Hangup


[macro-stdexten];
  ;   ${ARG1} - Extension  (we could have used ${MACRO_EXTEN} here as well
  ;   ${ARG2} - Device(s) to ring
  ;   ${ARG3} - Timeout in seconds
  ;   ${ARG4} - Skip voicemail
  ;   ${ARG5} - Mailbox Number
  ;
  exten => s,1,Dial(${ARG2},${ARG3})                                   ; Ring the interface, 20 seconds maximum
  exten => s,2,GotoIf()
  exten => s,3,Goto(s-${DIALSTATUS},1)                            ; Jump based on status (NOANSWER,BUSY,CHANUNAVAIL,CONGESTION,ANSWER)
  exten => s-NOANSWER,1,Voicemail(${ARG1}@voicemail,su)               ; If unavailable, send to voicemail w/ unavail announce
  exten => s-NOANSWER,2,Goto(default,s,1)                 ; If they press #, return to start
  exten => s-BUSY,1,Voicemail(${ARG1}@voicemail,sb)                   ; If busy, send to voicemail w/ busy announce
  exten => s-BUSY,2,Goto(default,s,1)                             ; If they press #, return to start
  exten => _s-.,1,Goto(s-NOANSWER,1)                              ; Treat anything else as no answer
  exten => a,1,VoicemailMain(${ARG1})
