; outgoing.conf:
; Deal with outgoing calls

;[globals]  (all globals are in extensions.conf)
;OUTGOING_CIDNUM=
;OUTGOING_CIDNUM_NOT_TOLLFREE=

; ===========================================================================
;          Outgoing 
;
; Inputs:
;    CALLERIDNUM
;    Database HOS/<CALLERIDNUM>    (Hangup on star)
;    Database PROVIDER/0           (Outgoing provider)
; ===========================================================================

; ========== Change outgoing provider ========== 
[macro-change-provider]
exten = s,1,DBput(PROVIDER/0=${ARG1})
exten = s,2,Playback(beep)
exten = s,3,Goto(${root_context},greet,1)

; ========== outgoing context ========== 
[outgoing]
; Emergency 911 extension
exten = 911,1,SetVar(callto=${EMERGENCY_NUMBER})
exten = 911,2,Macro(set-local_calleridnum)
exten = 911,3,Goto(out-cid,1)

exten = _1XXXXXXXXXX,1,SetVar(callto=${EXTEN})
exten = _1XXXXXXXXXX,2,Macro(set-local_calleridnum)
exten = _1XXXXXXXXXX,3,Macro(get-hos_string,${local_calleridnum})
exten = _1XXXXXXXXXX,4,Goto(out-cid,1)

; For calls from local extensions, change CID
exten = out-cid,1,GotoIf($["${local_calleridnum}" = "77"]?4:2)
exten = out-cid,2,GotoIf($["${local_calleridnum}" = "78"]?4:3)
exten = out-cid,3,GotoIf($["${local_calleridnum}" = "79"]?4:7)
exten = out-cid,4,SetCIDName(Daniel/Janet)
exten = out-cid,5,Macro(custom-SetCIDNum,${OUTGOING_CIDNUM})
exten = out-cid,6,Goto(out-cid-notoll,1)
exten = out-cid,7,SetCIDName(Forwarded)
; don't change CID of forwards
exten = out-cid,8,Goto(out-cid-notoll,1)

; Some exceptions that aren't handled here:
; 1 - if someone calls from a toll-free number, then calls out, it won't go through
; 2 - if *CID is used and both that number and the number called are toll-free,
;     the CID will be silently changed to OUTGOING_CIDNUM_NOT_TOLLFREE

; If we're calling a toll-free number, our CID can't be a toll-free number
; (FCC rule)
exten = out-cid-notoll,1,Macro(is-tollfree,${CALLERIDNUM})
exten = out-cid-notoll,2,GotoIf($["${is_tollfree}" = "true"]?3:9)
exten = out-cid-notoll,3,Macro(is-tollfree,${callto})
exten = out-cid-notoll,4,GotoIf($["${is_tollfree}" = "true"]?5:9)
exten = out-cid-notoll,5,GotoIf($["${is_custom_calleridnum}xxx" = "truexxx"]?6:7)
; Can't make a call to a toll-free number with a toll-free CID!
exten = out-cid-notoll,6,Congestion
exten = out-cid-notoll,7,SetCIDNum(${OUTGOING_CIDNUM_NOT_TOLLFREE})
exten = out-cid-notoll,8,NoOp(CALLERIDNUM forced to ${CALLERIDNUM}: CID&callto were toll-free)
exten = out-cid-notoll,9,Goto(out-trim-number,1)

; Trim the outgoing number if needed
; (currently just passes the number on
;  - Nufone and Teliax like the leading 1)
exten = out-trim-number,1,SetVar(dial_me=${callto})
exten = out-trim-number,2,Goto(out-provider,1)

; Lookup provider and call out using them
exten = out-provider,1,Macro(monitor-begin,${local_calleridnum},${CALLERIDNUM},${dial_me})
exten = out-provider,2,DBget(provider=PROVIDER/0)   ; Get provider string or jump
exten = out-provider,3,GotoIf($["${provider}" = "Teliax"]?4:6)
exten = out-provider,4,Dial(IAX2/danielwebb@teliax/${dial_me},60,g${hos_string})
exten = out-provider,5,Goto(104)
exten = out-provider,6,GotoIf($["${provider}" = "NuFone"]?7:103)
exten = out-provider,7,Dial(IAX2/danielwebb@NuFone/${dial_me},60,g${hos_string})
exten = out-provider,8,Goto(104)

exten = out-provider,103,Congestion              ; Provider not found
exten = out-provider,104,NoOp("Finished call")
exten = out-provider,105,Macro(return_to_top)

