;Version("3.1.2.1")
;InformationalVersion("3.1.2.0")

[nixxis-voicemail-v2]

exten => s,1,NoOp(Getting a voicemail IVR call)
 same => n,Set(ACCOUNTCODE=${SIP_HEADER(x-cr-acc)})
 same => n,Set(CONTACT=${SIP_HEADER(x-cr-con)})
 same => n,Set(IVRCONFIG=${SIP_HEADER(x-cr-ivrcfg)})
 same => n,Set(IVRDIR=${SIP_HEADER(x-cr-ivrdir)})
 same => n,Set(LAN=${SIP_HEADER(x-cr-lan)})
 same => n,Set(VAR=${SIP_HEADER(x-cr-var)})

 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),ExecIf($["${CONTACT}" = ""]?Congestion)
 same => n,Set(CHANNEL(language)=${LAN})
 same => n,Set(Config=${FILE(${NixxisSoundPath}/${ACCOUNTCODE}/Configs/${IVRCONFIG})})
 same => n,GoSub(ParseSettings(${Config}))

 same => n,Answer
 same => n,Wait(2)
 same => n,Set(TIMEOUT(absolute)=14400)
 same => n,Set(PLAYDIR=${NixxisSoundPath}/${ACCOUNTCODE})
 same => n,GoTo(nixxis-voicemail-v2-menu,s,1)

 same => n(ParseSettings),NoOp(Parsing settings)
 same => n,While($["${SET(AnswerPart=${SHIFT(ARG1,\n)})}" != ""])
 same => n,Set(PartName=${CUT(AnswerPart,=,1)})
 same => n,Set(PartValue=${CUT(AnswerPart,=,2)}) 
  same => n,Set(${PartName}=${SHIFT(PartValue,.)})
 same => n,EndWhile
 same => n,Return

 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-con"]?Set(CONTACT=${PartValue}))
 same => n,ExecIf($["${PartName}" = "x-cr-ivrcfg"]?Set(IVRCONFIG=${PartValue}))
 same => n,ExecIf($["${PartName}" = "x-cr-ivrdir"]?Set(IVRDIR=${PartValue}))
 same => n,ExecIf($["${PartName}" = "x-cr-lan"]?Set(LAN=${PartValue}))
 same => n,ExecIf($["${PartName}" = "x-cr-var"]?Set(VAR=${PartValue}))
 same => n,EndWhile
 same => n,Return


[nixxis-voicemail-v2-menu]
exten => s,1,NoOp(prompt: ${MSGANNOUNCE${LAN}})

 same => n,ExecIf($["${MSGANNOUNCE${LAN}}" != "" & "${LAN}" != ""]?Playback(${PLAYDIR}/${MSGANNOUNCE${LAN}}))
 same => n,ExecIf($["${MSGANNOUNCE}" != ""]?Playback(${PLAYDIR}/${MSGANNOUNCE}))

 same => n,Set(IVRCHOICES=CRIVRVoicemail:${CONTACT}\,CRIVRpositive:1\)

 same => n,Playback(beep)

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

 same => n(SetIvrInfoHttp),Set(CURLOPT(httptimeout)=3)
 same => n,Set(RequestUri=${NixxisAppServerUri}/sip?action=setIvrInfo&sipid=${SIPCALLID}&value=${IVRCHOICES})
 same => n,Set(RawAnswer=${CURL(${RequestUri})})
 same => n,Goto(mon)

 same => n(setIvrInfoByText),SendText(${IVRCHOICES})

 same => n(mon),MixMonitor(${NixxisRecordingPath}/${ACCOUNTCODE}/${CONTACT}.wav)
 same => n,Wait(600)
 same => n,Goto(nixxis-voicemail-v2-end,s,1)



[nixxis-voicemail-v2-end]

exten => s,1,NoOp(Done with IVR sending END)

 same => n,Set(i=1)
 same => n,While($[${i} < 3])

 same => n,SendText(${IVRCHOICES}\,CRIVRdrop\,CRIVRend)
 same => n,Set(i=$[${i} + 1])
 same => n,Wait(5)
 same => n,EndWhile
 same => n,HangUp()
