
; ========== Strip a leading 1 in a number ========== 
; ARG1 <- number to strip
; Sets stripped_number variable to the number without a leading 1
[macro-strip-leading-one]
exten = s,1,GotoIf($["${ARG1:0:1}" = "1"]?2:4)
exten = s,2,SetVar(stripped_number=${ARG1:1})
exten = s,3,Goto(5)
exten = s,4,SetVar(stripped_number=${ARG1})
exten = s,5,NoOp("Returning stripped_number ${stripped_number}")

; ========== Is a number a toll-free number?  ========== 
; ARG1 <- number to test (with or without leading 1)
; Sets is_tollfree variable to true or false
; Can handle 1+ 10-digit numbers or 10-digit numbers
[macro-is-tollfree]
exten = s,1,NoOp("Testing if ${ARG1} is toll-free")
exten = s,2,Macro(strip-leading-one,${ARG1})
exten = s,3,GotoIf($["${stripped_number:0:3}" = "800"]?7:4)
exten = s,4,GotoIf($["${stripped_number:0:3}" = "888"]?7:5)
exten = s,5,GotoIf($["${stripped_number:0:3}" = "877"]?7:6)
exten = s,6,GotoIf($["${stripped_number:0:3}" = "866"]?7:9)
exten = s,7,SetVar(is_tollfree=true)
exten = s,8,Goto(s,10)
exten = s,9,SetVar(is_tollfree=false)
exten = s,10,NoOp("Returning ${is_tollfree}")

; ========== Set local_calleridnum ========== 
; No arguments
; Sets local_calleridnum variable if it hasn't been set.
; This macro is only called by custom-SetCIDNum as long
; as the custom-SetCIDNum macro is always used instead of SetCIDNum.
[macro-set-local_calleridnum]
exten = s,1,GotoIf($["${local_calleridnum}xxx" = "xxx"]?2:4)
exten = s,2,SetVar(local_calleridnum=${CALLERIDNUM})
exten = s,3,NoOp("Setting local_calleridnum")
exten = s,4,NoOp("local_calleridnum is ${local_calleridnum}")

; ========== Set CALLERIDNUM (used internally to replace SetCIDNum) ========== 
; ARG1 <- new number for CALLERIDNUM
; Sets CALLERIDNUM, *unless* is_custom_calleridnum is true, in
; which case it is left alone.  is_custom_calleridnum should only
; be set by macro-custom-CALLERIDNUM.
[macro-custom-SetCIDNum]
exten = s,1,Macro(set-local_calleridnum)
exten = s,2,GotoIf($["${is_custom_calleridnum}xxx" = "truexxx"]?4:3)
exten = s,3,SetCIDNum(${ARG1})
exten = s,4,NoOp("CALLERIDNUM is ${CALLERIDNUM}")

; ========== Set CALLERIDNUM to a user-customized number ========== 
[macro-custom-CALLERIDNUM]
; ARG1 <- <extra digits> + new CALLERIDNUM
; ARG2 <- first digit offset of CALLERIDNUM in ARG1 (0 is first digit in ARG1)
;  (the leading digits are stripped so that they can be used with a user
;  code like *CID<number>).
exten = s,1,Macro(custom-SetCIDNum,${ARG1:${ARG2}})
exten = s,2,SetVar(is_custom_calleridnum=true)

