[globals]

TRANSFER_CONTEXT = from-internal


;********************************************************************
; BEGIN - INBOUND CALL HANDLING
;********************************************************************

[from-trunk-telekom]
; MSN 0
exten => 0511xxx,1,ExecIf($[ "${CALLERID(name)}" = "" ] ?Set(CALLERID(name)=${CALLERID(num)}))
         ; MSN valid, check number of simultaneous incoming calls
same  => n,Verbose(1,*** Telekom CALL from ${CALLERID(num)} to ${EXTEN} ***)
same  => n,Set(GROUP()=group-trunk-inbound)
same  => n,Verbose(1,*** Number of incoming calls is ${GROUP_COUNT(group-trunk-inbound)})
same  => n,GotoIf($[${GROUP_COUNT(group-trunk-inbound)} < 2]?true:false)
same  => n(true),Macro(strip-country-code)
same  => n,Goto(ext-group,20,1)
same  => n(false),Verbose(1,*** Number of incoming calls is ${GROUP_COUNT(group-trunk-inbound)} - OVER LIMIT ***)
same  => n,Hangup(17)

; MSN 1
exten => 0511xxx,1,ExecIf($[ "${CALLERID(name)}" = "" ] ?Set(CALLERID(name)=${CALLERID(num)}))
         ; MSN valid, check number of simultaneous incoming calls
same  => n,Verbose(1,*** Telekom CALL from ${CALLERID(num)} to ${EXTEN} ***)
same  => n,Set(GROUP()=group-trunk-inbound-p)
same  => n,Verbose(1,*** Number of incoming calls is ${GROUP_COUNT(group-trunk-inbound-p)})
same  => n,GotoIf($[${GROUP_COUNT(group-trunk-inbound-p)} < 2]?true:false)
same  => n(true),Macro(strip-country-code)
same  => n,Goto(ext-group,10,1)
same  => n(false),Verbose(1,*** Number of incoming calls is ${GROUP_COUNT(group-trunk-inbound-p)} - OVER LIMIT ***)
same  => n,Hangup(17)

; FAX
exten => 0511xxx,1,Verbose(1,*** Telekom FAX from ${CALLERID(num)} to ${EXTEN} ***)
same  =>         n,Goto(from-internal,30,1)

; all others
exten => _X.,1,Verbose(1,*** Telekom INVALID MSN from ${CALLERID(num)} to ${EXTEN} - REJECT ***)
same  =>     n,Hangup(21)

;********************************************************************
; END - INBOUND CALL HANDLING
;********************************************************************



;********************************************************************
; BEGIN - Outbound Call Handling
;********************************************************************
;

[outbound]
exten => _X./_1X,1,Macro(dial,PJSIP/${EXTEN}@trunk-telekom,0511xxx)
exten => _X./_2X,1,Macro(dial,PJSIP/${EXTEN}@trunk-telekom,0511xxx)
exten => _X./30,1,Macro(dial,PJSIP/${EXTEN}@trunk-telekom,0511xxx)
exten => _X.,1,Verbose(1,No outbound trunk match found)
same  =>     n,Hangup()


;********************************************************************
; END - Outbound Call Handling
;********************************************************************



;********************************************************************
; BEGIN - Internal Extensions and Services
;********************************************************************


[unauthenticated]
; This MUST be empty for security reasons!


[default]
include => ext-group
include => ext-local
exten => h,1,Hangup()


[from-internal]
include => ext-group
include => ext-hints
include => ext-local
include => app-echo-test
include => app-speakextennum
include => app-speakingclock
include => app-moh
include => app-vmmain
include => app-pickup
include => outbound
; Hangup
exten => h,1,Macro(hangupcall)
; Time out
exten => t,1,Playback(if-youd-like-to-make-a-call)
exten => t,n,Playback(please-hang-up-and-try-again)
exten => t,n,Congestion()
exten => t,n,Hangup()
; Invalid choice
exten => i,1,Playback(feature-not-avail-line) ; "That's not valid, try again"
exten => i,n,Goto(s,1)


; hints
[ext-hints]
exten => 11,hint,PJSIP/11
exten => 12,hint,SCCP/12
exten => 13,hint,SCCP/13
exten => 14,hint,SCCP/14
exten => 15,hint,SCCP/15
exten => 16,hint,SCCP/16
exten => 21,hint,SCCP/21
exten => 22,hint,SCCP/22
exten => 30,hint,PJSIP/30
exten => 50,hint,PJSIP/50

; special hints
exten => 80,hint,PJSIP/trunk-telekom&PJSIP/anonymous
exten => 81,hint,PJSIP/11&SCCP/12&SCCP/13&SCCP/14&SCCP/15&SCCP/16
exten => 82,hint,SCCP/21&SCCP/22&PJSIP/50


[ext-local]

; Retrieve messages from the mailbox box by dialling 41 on ext 12
exten => 41/_1X,1,VoiceMailMain(41@default,g(12))
same  =>        n,Hangup()

; calls between local SCCP extensions
exten => _XX,1,Dial(SCCP/${EXTEN})
same  =>     n,Hangup()

; calls to SIP phones
exten => 11,1,Dial(PJSIP/${EXTEN})
same  =>    n,Hangup()
exten => 30,1,Dial(PJSIP/${EXTEN})
same  =>    n,Hangup()
exten => 50,1,Dial(PJSIP/${EXTEN})
same  =>    n,Hangup()

;********************************************************************
; END - Internal Extensions and Services
;********************************************************************



;********************************************************************
; BEGIN - Ring Groups
;********************************************************************

[ext-group]

exten => 10,1,Dial(PJSIP/11&SCCP/12&SCCP/13&SCCP/16&Local/*41)
same  =>    n,Hangup()

exten => 20,1,Dial(SCCP/21&SCCP/22)
same  =>    n,Hangup()

; Voicemail
exten => *41,1,Wait(20)
same  =>     n,Voicemail(41@default,su)  ; skip instructions, play "unavailable" message
same  =>     n,Hangup()


;********************************************************************
; END - Ring Groups
;********************************************************************



;********************************************************************
; BEGIN - Applications
;********************************************************************

[app-echo-test]
exten => *43,1,Answer
exten => *43,n,Wait(1)
exten => *43,n,Playback(demo-echotest)
exten => *43,n,Echo()
exten => *43,n,Playback(demo-echodone)
exten => *43,n,Hangup


[app-speakextennum]
exten => *65,1,Answer
exten => *65,n,Macro(dumpvars)
exten => *65,n,Wait(1)
exten => *65,n,Playback(your)
exten => *65,n,Playback(extension)
exten => *65,n,Playback(number)
exten => *65,n,Playback(is)
exten => *65,n,SayDigits(${CALLERID(num)})
exten => *65,n,Wait(1)
exten => *65,n,Hangup()


[app-speakingclock]
exten => *60,1,Answer
same  =>     n,Wait(1)
same  =>     n,SayUnixTime(,,)
same  =>     n,Hangup()

[app-moh]
exten => *61,1,Answer
same  =>     n,MusicOnHold()
same  =>     n,Hangup()

[app-vmmain]
exten => *97,1,Answer
exten => *97,n,VoiceMailMain()
exten => *97,n,Hangup()


[app-pickup]
exten => _*8.,1,Verbose(1,*** ${CALLERID(num)} will pickup call from ${EXTEN:2} ***)
same  =>      n,Pickup()


;********************************************************************
; END - Applications
;********************************************************************



;********************************************************************
; BEGIN - Macros
;********************************************************************

[macro-dial]
; ${ARG1} - trunk/number to dial
; ${ARG2} - callerid
exten => s,1,Verbose(1, *** Dialing ${ARG1} from ${CALLERID(all)} ***)
same  =>   n,Set(callerid_all=${CALLERID(all)})
same  =>   n,Set(CALLERID(all)=${ARG2})
same  =>   n,Dial(${ARG1})
same  =>   n,Set(CALLERID(all)=${callerid_all})
same  =>   n,Goto(s-${DIALSTATUS},1)
exten => s-BUSY,1,Busy(10)
same  =>        n,MacroExit
exten => s-CHANUNAVAIL,1,Busy(10)
same  =>               n,MacroExit
exten => _s-.,1,MacroExit


[macro-hangupcall]
exten => s,1,Verbose(1,*** Hanging up, ${callerid_all}, ${CALLERID(all)} ***)
; exten => s,n,Set(CALLERID(all)=${callerid_all})
exten => s,n,Hangup(16)
exten => s,n,MacroExit


[macro-strip-country-code]
; convert +49 to 0
exten => s,1,GotoIf($["${CALLERID(num):0:3}" = "+49"]?:intl)
exten => s,n,Set(CALLERID(name)=0${CALLERID(num):3})
exten => s,n,Set(CALLERID(num)=)
exten => s,n,MacroExit()
; convert +.. to 00
exten => s,n(intl),GotoIf($["${CALLERID(num):0:1}" = "+"]?:end)
exten => s,n,Set(CALLERID(name)=00${CALLERID(num):1})
exten => s,n,Set(CALLERID(num)=)
exten => s,n,MacroExit()
; all the rest
exten => s,n(end),MacroExit()

;********************************************************************
; END - Macros
;********************************************************************
