;
; vmauth prompting for an extension
; usage: vmauth authcontext [exten]
; returns: N+1 = error, N+101 = Success
; sets VMAUTH_EXTEN on success
;

[macro-vmauth]
exten => s,1,Answer()
exten => s,2,SetVar(userExten=${ARG2})
exten => s,3,GotoIf( $[ "${userExten}" = "" ] ? 4 : 9)
exten => s,4,DigitTimeout(4)
exten => s,5,ResponseTimeout(20)
exten => s,6,Read(userExten,sm-021,6) 				; please enter your extension
exten => s,7,GotoIf( $[ "${userExten}" = "" ] ? 4 : 8)		; blank try again
exten => s,8,GotoIf( $[ "${userExten}" = "*" ] ? 10 : 9)	; *  cancel
exten => s,9,Goto(pass,1)					; check password
exten => s,10,Goto(fail,1)

exten => pass,1,DigitTimeout(4)
exten => pass,2,ResponseTimeout(20)
exten => pass,3,Read(userPassword,sm-022,8)			; please enter your password
exten => pass,4,GotoIf( $[ "${userPassword}" = "" ] ? 6 : 5)	; blank try again
exten => pass,5,GotoIf( $[ "${userPassword}" = "*" ] ? 11 : 7)	; * return
exten => pass,6,Goto(s,5)					; blank ask exten
exten => pass,7,AGI(voicemail-auth.agi,${ARG1},${userExten},${userPassword})
exten => pass,8,GotoIf( $[ "${agiResult}" = "1" ] ? 12 : 9 )
exten => pass,9,Playback(sm-023)
exten => pass,10,Wait(1)
exten => pass,11,Goto(s,5)
exten => pass,12,Goto(success,1)

exten => fail,1,SetVar(MACRO_OFFSET=0)
exten => fail,2,Playback(sm-023)				; password is invalid

exten => success,1,SetVar(MACRO_OFFSET=100)
exten => success,2,SetVar(VMAUTH_EXTEN=${userExten})
