;Version("3.3.1255.136")
;InformationalVersion("3.3.1255.0")

[nixxis-monitor-auth]

exten => s,1,Answer()
 same  => n,Set(CHANNEL(language)=${SIP_HEADER(x-cr-actlan)})

 same => n,GotoIf($["${SIP_HEADER(x-cr-iex)}" = "http"]?ivrInfoHttp:ivrInfoDone)

 same => n(ivrInfoHttp),Set(NixxisAppServerUri=${SIP_HEADER(x-cr-hsr)})
 same => n,Set(CURLOPT(httptimeout)=3)
 same => n,Set(RequestUri=${NixxisAppServerUri}/sip?action=getIvrInfo&sipid=${SIPCALLID}&version=${CUT(NixxisFileVersion,\",2)})
 same => n,Set(RawAnswer=${CURL(${RequestUri})})
 same => n,GoSub(ParseIvrInfo(${RawAnswer}))

 same => n(ivrInfoDone),Wait(2)
 same  => n(enteruser),Read(AUTHUSER,agent-user)
 same  => n,Read(AUTHPASS,agent-pass)
 same  => n,Set(RequestUri=http://${SIP_HEADER(x-cr-hsr)}/admin?action=authenticate&username=${AUTHUSER}&pass=${AUTHPASS})
 same  => n,Set(AUTHRESULT=${CURL(${RequestUri})})
 same  => n,NoOp( Auth result : ${AUTHRESULT} - ${LEN(${AUTHRESULT})} )
 same  => n,GotoIf($[${LEN(${AUTHRESULT})} > 1]?loginok)
 same  => n,Playback(login-fail)
 same  => n,Goto(enteruser)
 same  => n(loginok),NoOp( Login OK)
 same  => n,SendText(CRIVRagentid:${AUTHRESULT})
 same  => n,Set(IVRCHOICES=CRIVRagentid:${AUTHRESULT})
 same  => n,GoTo(nixxis-ivr-v2-end,s,1)

 same => n(ParseIvrInfo),NoOp(Parsing IvrInfo)
 same => n,While($["${SET(AnswerPart=${SHIFT(ARG1,&)})}" != ""])
 same => n,Set(PartName=${CUT(AnswerPart,=,1)})
 same => n,Set(PartValue=${CUT(AnswerPart,=,2)})
 same => n,ExecIf($["${PartName}" = "x-cr-acc"]?Set(ACCOUNTCODE=${PartValue}))
 same => n,ExecIf($["${PartName}" = "x-cr-ani"]?Set(ANI=${PartValue}))
 same => n,ExecIf($["${PartName}" = "x-cr-act"]?Set(ACTIVITY=${PartValue}))
 same => n,ExecIf($["${PartName}" = "x-cr-lan"]?Set(CHANNEL(language)=${PartValue}))
 same => n,ExecIf($["${PartName}" = "x-cr-actlan"]?Set(CHANNEL(language)=${PartValue}))
 same => n,EndWhile
 same => n,Return
