; forward.conf:
; Interactive application to handle call forwarding.

; Forward modes:
;   off       : call extension normally
;   on        : call forward number after timeout trying to call extension
;   immediate : call forward number without trying extension

[macro-forward_set_number]
; Set the forwarding number for an extension
; ARG1 <- caller id number
; ARG2 <- forward to number
exten = s,1,DBput(FORWARD/${ARG1}=${ARG2})
exten = s,2,DBput(FORWARD/${ARG1}-mode=on)
exten = s,3,DBput(FORWARD/${ARG1}-timeout=20)
exten = s,4,Playback(beep)
exten = s,5,Goto(${root_context},greet,1)

[macro-forward_cancel]
; Cancel call forward for this extension
; ARG1 <- caller id number
exten = s,1,DBdel(FORWARD/${ARG1})
exten = s,2,DBdel(FORWARD/${ARG1}-mode)
exten = s,3,DBdel(FORWARD/${ARG1}-timeout)
exten = s,4,Playback(beep)
exten = s,5,Goto(${root_context},greet,1)

[forward_mode_menu]
; Set the forwarding mode for an extension
; Set ${F_EXT} to the extension to set before entering this context.
;
exten = s,1,NoOp(F_EXT = ${F_EXT})
exten = s,2,NoOp(F_CALLER = ${F_CALLER})
exten = s,3,Goto(s-report_number,1)

exten = s-report_number,1,DBget(forward_number=FORWARD/${F_EXT})
;   forward_number_is: "The forward number is currently set to"
exten = s-report_number,2,Background(user_sounds/forward/forward_number_is)
exten = s-report_number,3,sayalpha(${forward_number})
exten = s-report_number,4,Goto(s-report_mode,1)
exten = s-report_number,102,Goto(s-not_set,1)

;   forward_is_set_to: "Forward mode is currently set to"
exten = s-report_mode,1,Background(user_sounds/forward/forward_mode_is)
exten = s-report_mode,2,DBget(forward_mode=FORWARD/${F_EXT}-mode)
exten = s-report_mode,3,GotoIf($["${forward_mode}" = "off"]?6:4)
exten = s-report_mode,4,GotoIf($["${forward_mode}" = "on"]?8:5)
exten = s-report_mode,5,GotoIf($["${forward_mode}" = "immediate"]?10:103)
exten = s-report_mode,6,Background(user_sounds/forward/forward_mode_off)
exten = s-report_mode,7,Goto(s-menu,1)
exten = s-report_mode,8,Background(user_sounds/forward/forward_mode_on)
exten = s-report_mode,9,Goto(s-menu,1)
exten = s-report_mode,10,Background(user_sounds/forward/forward_mode_immediate)
exten = s-report_mode,11,Goto(s-menu,1)
;   forward_not_set: "Forward mode is not set"
exten = s-report_mode,102,Goto(s-not_set,1)
exten = s-report_mode,103,Congestion

exten = s-not_set,1,Playback(user_sounds/forward/forward_not_set)
exten = s-not_set,2,Macro(return_to_top)

;   forward_set_menu: "Select forward mode: 1 for off, 2 for on, 3 for immediate"
exten = s-menu,1,Background(user_sounds/forward/forward_set_menu)
exten = s-menu,2,WaitExten(6)

exten = 1,1,DBget(forward_number=FORWARD/${F_EXT}) ; Make sure there is a forward number
exten = 1,2,DBput(FORWARD/${F_EXT}-mode=off)
exten = 1,3,Playback(beep)
exten = 1,4,Macro(return_to_top)
exten = 1,102,Goto(s-not_set,1)

exten = 2,1,DBget(forward_number=FORWARD/${F_EXT}) ; Make sure there is a forward number
exten = 2,2,DBput(FORWARD/${F_EXT}-mode=on)
exten = 2,3,Playback(beep)
exten = 2,4,Macro(return_to_top)
exten = 2,102,Goto(s-not_set,1)

exten = 3,1,DBget(forward_number=FORWARD/${F_EXT}) ; Make sure there is a forward number
exten = 3,2,DBput(FORWARD/${F_EXT}-mode=immediate)
exten = 3,3,Playback(beep)
exten = 3,4,Macro(return_to_top)
exten = 3,102,Goto(s-not_set,1)

