<?xml version="1.0" encoding="utf-8" ?>
<voicemail xmlns="http://www.innovaphone.com/xsd/voicemail6.xsd" _ver="VM-60137">
    <!-- =======================Answering Machine================================= -->
    <function define="AnsweringMachine">
        <assign out="$sub" value="$usrdir"/>
        <assign out="$greeting" value=""/>
		<if cond="$_initdelay">
            <store-get root="" name="silence.$coder" out-url="$ctrl" />
            <pbx-prompt url="$ctrl" sec="$_initdelay" repeat="true"/>
		</if>
        <call name="MakeUserDirs" />
        <if notcond="$greeting">
            <store-getstat root="$sub" name="personal/personal_greeting.g711a" out-error="$error"/>
            <if notcond="$error">
                <assign out="$greeting" value="personal"/>
            </if>
        </if>
        <if notcond="$greeting">
            <store-getstat root="$sub" name="personal/name_greeting.g711a" out-error="$error"/>
            <if notcond="$error">
                <assign out="$greeting" value="name"/>
            </if>
        </if>
        <if notcond="$greeting">
            <assign out="$greeting" value="box"/>
        </if>

        <event type="dtmf" block="false">
            <pbx-getdtmfdigit out-dtmf="$dtmf" />
            <switch var="$dtmf">
                <case equal="9">
                    <call name="Voicebox"/>
                </case>
            </switch>
        </event>

        <switch var="$greeting">
            <case equal="name">
                <store-get root="$sub" name="personal/name_greeting.g711a" out-url="$ctrl"/>
                <pbx-prompt url="$ctrl"/>
                <store-get root="" name="se_is_not_reachable.$coder" out-url="$ctrl"/>
                <pbx-prompt url="$ctrl"/>
                <store-get root="" name="se_please_leave_a_message.$coder" out-url="$ctrl"/>
                <pbx-prompt url="$ctrl"/>
            </case>
            <case equal="personal">
                <store-get root="$sub" name="personal/personal_greeting.g711a" out-url="$ctrl"/>
                <pbx-prompt url="$ctrl"/>
            </case>
            <case equal="box">
                <store-get root="" name="se_the_subscriber.$coder" out-url="$ctrl"/>
                <pbx-prompt url="$ctrl"/>
                <assign out="$string" value="$leg2"/>
                <call name="PromptString"/>
                <store-get root="" name="se_is_not_reachable.$coder" out-url="$ctrl"/>
                <pbx-prompt url="$ctrl"/>
                <store-get root="" name="se_please_leave_a_message.$coder" out-url="$ctrl"/>
                <pbx-prompt url="$ctrl"/>
            </case>
        </switch>

            <!-- Beep -->
        <store-get root="" name="beep.$coder" out-url="$ctrl" />
        <pbx-prompt url="$ctrl"/>
            <!-- Record -->
        <store-getnew root="$sub" out-url="$vm"/>
        <lib-strcat string="$vm" string2="-" out-string="$vm" />
		<call name="PrepMetaData"/>
		<lib-strcat string="$vm" string2="$sig-meta" out-string="$vm" />
        <lib-strcat string="$vm" string2=".g711a" out-string="$vm" />
        <pbx-mwi/>
        <store-get root="$sub" name="personal/mwi.xml" out-url="$suburl" />
        <call name="Subscript"/>
        <pbx-record url="$vm" sec="50"/>
                <!-- End -->
        <store-get root="" name="se_end_of_menu.$coder" out-url="$ctrl"/>
        <pbx-prompt url="$ctrl" />
        <pbx-disc/>
    </function>

    <!-- =======================Voicebox========================================= -->
    <function define="Voicebox">
        <event type="timer" name="vbguard">
            <store-get root="" name="se_end_of_menu.$coder" out-url="$ctrl"/>
            <pbx-prompt url="$ctrl" />
            <pbx-disc/>
        </event>
        <if notcond="$_vbguard">
            <assign out="$_vbguard" value="300"/>
        </if>
        <timer name="vbguard" sec="$_vbguard"/>
        <assign out="$sub" value="$usrdir"/>
		<if cond="$_initdelay">
            <store-get root="" name="silence.$coder" out-url="$ctrl" />
            <pbx-prompt url="$ctrl" sec="$_initdelay" repeat="true"/>
		</if>		
        <store-get root="" name="se_welcome.$coder" out-url="$ctrl"/>
        <pbx-prompt url="$ctrl"/>
            <!-- Check PIN, except the script was parameterized with $_nopin=true and the caller is the box owner -->
        <assign out="$check" value="true"/>
        <if cond="$_nopin">
            <switch var="$cgpn">
                <case equal="$cdpn">
                    <assign out="$check" value="false"/>
                </case>
            </switch>
        </if>
        <if cond="$check">
            <call name="CheckPin" />
        </if>

        <call name="MakeUserDirs" />
        <assign out="$sub" value="$usrdir"/>
        <assign out="$is-new-messages" value="true"/>

        <event type="dtmf" block="false">
            <timer name="vbguard" sec="$_vbguard"/>
            <pbx-getdtmfdigit out-dtmf="$dtmf" />
            <switch var="$dtmf">
                <case equal="1">
                        <!-- Listen New Messages -->
                    <assign out="$sub" value="$usrdir"/>
                    <assign out="$is-new-messages" value="true"/>
                    <call name="MenuVoiceboxMessages" />
                </case>
                <case equal="2">
                        <!-- Listen Old Messages -->
                    <assign out="$sub" value="$usrdir"/>
                    <lib-strcat string="$sub" string2="/store" out-string="$sub"/>
                    <assign out="$is-new-messages" value="false"/>
                    <store-get-msgcount root="$sub" out-count="$msgcount" />
                    <assign out="$dtmfAhead" value="false"/>
                    <call name="PromptNumOfMessages" />
                    <call name="MenuVoiceboxMessages" />
                </case>
                <case equal="3">
                        <!-- Setting Up Greetings -->
                    <assign out="$sub" value="$usrdir"/>
                    <call name="MenuVoiceboxGreetings" />
                </case>
                <case equal="4">
                        <!-- Change PIN -->
                    <assign out="$sub" value="$usrdir"/>
                    <call name="MenuPin" />
                </case>
                <case equal="*">
                        <!-- Goodbye -->
                    <store-get root="" name="se_end_of_menu.$coder" out-url="$ctrl"/>
                    <pbx-prompt url="$ctrl"/>
                    <pbx-disc/>
                </case>
            </switch>
            <assign out="$dtmfAhead" value="true"/>
        </event>

        <store-get root="$sub" name="/personal/name_greeting.g711a" out-url="$ctrl" />
        <pbx-prompt url="$ctrl"/>

        <store-get-msgcount root="$sub" out-count="$msgcount" />
        <call name="PromptNumOfMessages" />

        <while cond="true">
                <!--  Prompt Menuoptions -->
            <store-get root="" name="se_menu_voicebox.$coder" out-url="$ctrl"/>
            <pbx-prompt url="$ctrl" />
                <!-- Silence -->
            <store-get root="" name="silence.$coder" out-url="$ctrl" />
            <pbx-prompt url="$ctrl" sec="4" repeat="true"/>
        </while>
    </function>

    <!-- =======================Listen Voicebox Messages================================ -->
    <function define="MenuVoiceboxMessages">
        <assign out="$dtmf" value=""/>
        <if cond="$is-new-messages">
            <pbx-mwi value="0"/>
        </if>
        <store-get root="$sub" name="personal/mwi.xml" out-url="$suburl" />
        <call name="Subscript"/>
        <store-getnext root="$sub" out-handle="$handle" out-url="$message"/>

        <while cond="$message">
            <call name="PromptMessage"/>

                <!--Pick next Message-->
            <switch var="$dtmf">
                <case equal="1"> <!--callback-->
                </case>
                <case equal="4"> <!--repeat-->
                </case>
                <case equal="7"> <!--previous-->
                </case>
                <case equal="*">
                    <store-get root="" name="se_cancel.$coder" out-url="$ctrl"/>
                    <pbx-prompt url="$ctrl" barge-in="false"/>
                    <return/>
                </case>
                <default>
                    <store-getnext handle="$handle" out-url="$message"/>
                </default>
            </switch>
        </while>
                <!-- End -->
        <store-get root="" name="se_end_of_messages.$coder" out-url="$ctrl"/>
        <pbx-prompt url="$ctrl"/>
    </function>

    <function define="PromptMessage">
        <event type="dtmf" block="false">
            <timer name="vbguard" sec="$_vbguard"/>
            <event type="dtmf" block="false">
            </event>
            <pbx-getdtmfdigit out-dtmf="$dtmf" />

            <switch var="$dtmf">
                <case equal="1">
                            <!-- Call Back -->
                        <if cond="$callback">
                            <store-get root="" name="se_you_will_be_connected.$coder" out-url="$ctrl"/>
                            <pbx-prompt url="$ctrl" />
                            <timer name="vbguard" sec="0"/>
                            <pbx-fwd e164="$callback" out-cause="$cause"/>
                            <timer name="vbguard" sec="$_vbguard"/>
                            <switch var="$cause">
                                <case equal="17">
                                    <store-get root="" name="se_subscriber_busy.$coder" out-url="$ctrl"/>
                                    <pbx-prompt url="$ctrl" />
                                </case>
                                <default>
                                    <store-get root="" name="se_the_connection_ended.$coder" out-url="$ctrl"/>
                                    <pbx-prompt url="$ctrl" />
                                </default>
                            </switch>
                        </if>
                        <else>
                            <store-get root="" name="se_not_available.$coder" out-url="$ctrl"/>
                            <pbx-prompt url="$ctrl" />
                        </else>
                        <assign out="$is_reprompt_menuoptions" value="true"/>
                </case>
                <case equal="2">
                    <if cond="$is-new-messages">
                        <store-get root="" name="silence.$coder" out-url="$ctrl" />
                        <pbx-prompt url="$ctrl" sec="1" barge-in="false"/>
                            <!-- Save Message -->
                        <store-split url="$message" out-path="$path" out-file="$file"/>
                        <lib-strcat string="$path" string2="store/" out-string="$tmp" />
                        <lib-strcat string="$tmp"  string2="$file"  out-string="$tmp" />
                        <store-ren root="" name="$message" new-name="$tmp"/>
                        <store-get root="" name="se_message_saved.$coder" out-url="$ctrl"/>
                        <pbx-prompt url="$ctrl" />
                    </if>
                </case>
                <case equal="3">
                        <!-- Delete Message -->
                    <store-get root="" name="silence.$coder" out-url="$ctrl" />
                    <pbx-prompt url="$ctrl" sec="1" barge-in="false"/>
                    <store-del root="" url="$message" />
                    <store-get root="" name="se_message_deleted.$coder" out-url="$ctrl"/>
                    <pbx-prompt url="$ctrl" />
                </case>
                <case equal="4">
                        <!-- Repeat Message -->
                    <pbx-prompt url="$message" />
                    <assign out="$is_reprompt_menuoptions" value="true"/>
                </case>
                <case equal="6">
                        <!-- Next Message. Will be either picked in while-loop below -->
                </case>
                <case equal="7">
                        <!-- Previous Message -->
                    <store-getprev handle="$handle" out-url="$message"/>
                </case>
            </switch>
        </event>

        <assign out="$dtmf" value=""/>
        <assign out="$callback" value=""/>
        <store-split url="$message" out-path="" out-file="" out-cgpn="$callback"/>
        <lib-strlen string="$callback" out="$len-callback" />
        <switch var="$len-callback">
            <case greater="1">
            </case>
            <default>
                <assign out="$callback" value=""/>
            </default>
         </switch>

        <if notcond="$is_reprompt_menuoptions">
                    <!-- Announce Next Message -->
            <store-get root="" name="se_next_message.$coder" out-url="$ctrl" />
            <pbx-prompt url="$ctrl" />
            <if cond="$dtmf">
                <switch var="$dtmf">
                    <case not-equal="6">  <!--next(6) is treated here as skip-->
                        <return/>
                    </case>
                 </switch>
            </if>

                    <!-- Announce Next Message -->
            <call name="PromptDate"/>
            <if cond="$dtmf">
                <switch var="$dtmf">
                    <case not-equal="6">  <!--next(6) is treated here as skip-->
                        <return/>
                    </case>
                 </switch>
            </if>

            <assign out="$dtmf" value=""/>
                <!-- Prompt Message -->
            <pbx-prompt url="$message" />
            <if cond="$dtmf">
                <switch var="$dtmf">
                    <default>
                        <return/>
                    </default>
                 </switch>
            </if>
        </if>
        <assign out="$is_reprompt_menuoptions" value=""/>

            <!--  Prompt Menueoptions -->
        <if cond="$is-new-messages">
            <if cond="$callback">
                <store-get root="" name="se_menu_voicebox_messages_123467.$coder" out-url="$ctrl"/>
            </if>
            <else>
                <store-get root="" name="se_menu_voicebox_messages_23467.$coder" out-url="$ctrl"/>
            </else>
        </if>
        <else>
            <if cond="$callback">
                <store-get root="" name="se_menu_voicebox_messages_stored_13467.$coder" out-url="$ctrl"/>
            </if>
            <else>
                <store-get root="" name="se_menu_voicebox_messages_stored_3467.$coder" out-url="$ctrl"/>
            </else>
        </else>
        <pbx-prompt url="$ctrl" />
    </function>

    <!-- =======================Prompt Number Of Messages. Amount in $msgcount====================== -->
    <function define="PromptNumOfMessages">
        <return cond="$dtmfAhead"/>
        <store-get root="" name="se_you_have.$coder" out-url="$ctrl" />
        <assign out="$ctrl1" value=""/>
        <switch var="$msgcount">
            <case equal="0">
                <if notcond="$is-new-messages">
                    <store-get root="" name="se_you_dont_have_stored_messages.$coder" out-url="$ctrl"/>
                </if>
                <else>
                    <store-get root="" name="se_you_dont_have_new_messages.$coder" out-url="$ctrl"/>
                </else>
                <pbx-prompt url="$ctrl"/>
                <return/>
            </case>
            <case equal="1">
                <if notcond="$is-new-messages">
                    <store-get root="" name="se_you_have_one_stored_message.$coder" out-url="$ctrl"/>
                </if>
                <else>
                    <store-get root="" name="se_you_have_one_new_message.$coder" out-url="$ctrl"/>
                </else>
                <pbx-prompt url="$ctrl"/>
                <return/>
            </case>
            <case greater="10">
                <if notcond="$is-new-messages">
                    <store-get root="" name="se_you_have_many_stored_messages.$coder" out-url="$ctrl"/>
                </if>
                <else>
                    <store-get root="" name="se_you_have_many_new_messages.$coder" out-url="$ctrl"/>
                </else>
                <pbx-prompt url="$ctrl"/>
                <return/>
            </case>
            <case greater="0">
                <lib-strcat string="se_" string2="$msgcount" out-string="$fileprefix" />
                <lib-strcat string="$fileprefix" string2=".$coder" out-string="$filename" />
                <store-get root="" name="$filename" out-url="$ctrl1" />
                <store-get root="" name="se_messages.$coder" out-url="$ctrl3" />
            </case>
        </switch>

        <if notcond="$is-new-messages">
            <store-get root="" name="se_stored_messages.$coder" out-url="$ctrl2"/>
        </if>
        <else>
            <store-get root="" name="se_new_messages.$coder" out-url="$ctrl2"/>
        </else>

        <pbx-prompt url="$ctrl"/><return cond="$dtmfAhead"/>    <!-- You have -->
        <pbx-prompt url="$ctrl1"/><return cond="$dtmfAhead"/>   <!-- two -->
        <pbx-prompt url="$ctrl2"/><return cond="$dtmfAhead"/>   <!-- new messages/stored messages -->
    </function>

    <!-- =======================Prompt a Nummer 1..100. Number in $number====================== -->
    <function define="PromptNumber">
        <lib-strcat string="se_" string2="$number" out-string="$fileprefix" />
        <lib-strcat string="$fileprefix" string2=".$coder" out-string="$filename" />
        <store-get root="" name="$filename" out-url="$ctrl" />
        <pbx-prompt url="$ctrl" />
    </function>

    <!-- =======================Prompt Date and Time of File. URL in $message====================== -->
    <function define="PromptDate">
           <if cond="$message">
            <store-getstat root="" name="$message" out-wday="$wday" out-mday="$mday" out-mon="$mon" out-year="year" out-hour="$hour" out-min="$min" out-error="$error" out-size="$size" out-type="$type"/>
            <switch var="$error">
                <case equal="0">
                            <!-- Weekday -->
                        <lib-strcat string="se_weekday_" string2="$wday" out-string="$fileprefix" />
                        <lib-strcat string="$fileprefix" string2=".$coder" out-string="$filename" />
                        <store-get root="" name="$filename" out-url="$ctrl-wday" />

                            <!-- Day Of Month -->
                        <lib-strcat string="se_" string2="$mday" out-string="$fileprefix" />
                        <lib-strcat string="$fileprefix" string2="_ord.$coder" out-string="$filename" />
                        <store-get root="" name="$filename" out-url="$ctrl-mday" />

                            <!-- Month -->
                        <lib-strcat string="se_month_" string2="$mon" out-string="$fileprefix" />
                        <lib-strcat string="$fileprefix" string2=".$coder" out-string="$filename" />
                        <store-get root="" name="$filename" out-url="$ctrl-month" />

                            <!-- Hour -->
                        <lib-strcat string="se_" string2="$hour" out-string="$fileprefix" />
                        <lib-strcat string="$fileprefix" string2=".$coder" out-string="$filename" />
                        <store-get root="" name="$filename" out-url="$ctrl-hour" />


                            <!-- Minute -->
                        <lib-strcat string="se_" string2="$min" out-string="$fileprefix" />
                        <lib-strcat string="$fileprefix" string2=".$coder" out-string="$filename" />
                        <store-get root="" name="$filename" out-url="$ctrl-min" />
                        <assign out="$ctrl-min-0" value=""/>

                            <!-- "den" -->
<!--
                        <store-get root="" name="se_den.$coder" out-url="$ctrl-den" />
-->

                            <!-- "klokken" -->
<!--
                        <store-get root="" name="se_klokken.$coder" out-url="$ctrl-klokken" />
-->
                        <switch var="$min">
                            <case less="10">
                               <store-get root="" name="se_0.$coder" out-url="$ctrl-min-0" />
                            </case>
                        </switch>

                        <if notcond="$dtmf">
                            <pbx-prompt url="$ctrl-wday"/>
                        </if>
<!--
                        <if notcond="$dtmf">
                            <pbx-prompt url="$ctrl-den"/>
                        </if>
-->
                        <if notcond="$dtmf">
                            <pbx-prompt url="$ctrl-mday"/>
                        </if>
                        <if notcond="$dtmf">
                            <pbx-prompt url="$ctrl-month"/>
                        </if>
<!--
                        <if notcond="$dtmf">
                            <pbx-prompt url="$ctrl-klokken"/>
                        </if>
-->
                        <if notcond="$dtmf">
                            <pbx-prompt url="$ctrl-hour"/>
                        </if>
                        <if notcond="$dtmf">
                            <if cond="$ctrl-min-0">
                                <pbx-prompt url="$ctrl-min-0"/>
                            </if>
                            <pbx-prompt url="$ctrl-min"/>
                        </if>
                </case>
            </switch>
           </if>
    </function>

    <!-- =======================Personal Announcement Administration====================== -->
    <function define="SavePersonalGreeting">
            <!-- Neue Ansage als aktuelle speichern -->
        <store-del root="$sub" name="personal/personal_greeting.g711a"/>
        <store-ren root="$sub" name="personal/temp_personal_greeting.g711a" new-name="personal/personal_greeting.g711a"/>
        <store-get root="" name="se_message_saved.$coder" out-url="$ctrl" />
        <pbx-prompt url="$ctrl" barge-in="false"/>
    </function>

    <function define="SaveNameGreeting">
            <!-- Neue Ansage als aktuelle speichern -->
        <store-del root="$sub" name="personal/name_greeting.g711a"/>
        <store-ren root="$sub" name="personal/temp_name_greeting.g711a" new-name="personal/name_greeting.g711a"/>
        <store-get root="" name="se_message_saved.$coder" out-url="$ctrl" />
        <pbx-prompt url="$ctrl" barge-in="false"/>
    </function>

    <function define="MenuVoiceboxGreetings">
        <assign out="abort" value="false"/>
        <event type="dtmf" block="false">
            <timer name="vbguard" sec="$_vbguard"/>
            <event type="dtmf" block="false">
            </event>
            <pbx-getdtmfdigit out-dtmf="$dtmf" />
            <switch var="$dtmf">
                <case equal="1">
                    <assign out="$is_name" value="true"/>
                    <call name="MenuVoiceboxGreeting" />
                </case>
                <case equal="2">
                    <assign out="$is_name" value="false"/>
                    <call name="MenuVoiceboxGreeting" />
                </case>
                <case equal="*">
                    <assign out="abort" value="true"/>
                    <store-get root="" name="se_cancel.$coder" out-url="$ctrl"/>
                    <pbx-prompt url="$ctrl" barge-in="false"/>
                </case>
                <case equal="#">
                        <!--  Hilfe -->
                    <store-get root="" name="se_menu_voicebox_greetings.$coder" out-url="$ctrl"/>
                    <pbx-prompt url="$ctrl" />
                </case>
            </switch>
        </event>
        <while notcond="$abort">
                <!--  Prompt Menueoptions -->
            <store-get root="" name="se_menu_voicebox_greetings.$coder" out-url="$ctrl"/>
            <pbx-prompt url="$ctrl" />
            <if notcond="$abort">
                    <!-- Silence. 4 Seconds -->
                <store-get root="" name="silence.$coder" out-url="$ctrl" />
                <pbx-prompt url="$ctrl" sec="4" repeat="true"/>
            </if>
        </while>
                <!-- End -->
        <store-get root="" name="se_end_of_menu.$coder" out-url="$ctrl"/>
        <pbx-prompt url="$ctrl"/>
    </function>

    <!-- ============MenuVoiceboxGreeting differentiation name/personal in $is_name ====================== -->
    <function define="MenuVoiceboxGreeting">
        <assign out="abort" value="false"/>
        <if cond="$is_name">
            <store-get root="" name="se_menu_voicebox_greetings_name.$coder" out-url="$menu"/>
        </if>
        <else>
            <store-get root="" name="se_menu_voicebox_greetings_personal.$coder" out-url="$menu"/>
        </else>
        <event type="dtmf" block="false">
            <timer name="vbguard" sec="$_vbguard"/>
            <event type="dtmf" block="false">
            </event>
            <pbx-getdtmfdigit out-dtmf="$dtmf" />
            <switch var="$dtmf">
                <case equal="0">
                </case>
                <case equal="1">
                        <!-- Zum Sprechen auffordern -->
                    <store-get root="" name="se_please_speak_after_the_signal.$coder" out-url="$ctrl" />
                    <pbx-prompt url="$ctrl" barge-in="false"/>
                    <store-get root="" name="se_press_key_to_end.$coder" out-url="$ctrl" />
                    <pbx-prompt url="$ctrl" barge-in="false"/>
                        <!-- Silence 1s -->
                    <store-get root="" name="silence.$coder" out-url="$ctrl" />
                    <pbx-prompt url="$ctrl" sec="1" barge-in="false"/>
                        <!-- Beep -->
                    <store-get root="" name="beep.$coder" out-url="$ctrl" />
                    <pbx-prompt url="$ctrl" />
                        <!-- Neue Ansage temporaer aufnehmen -->
                    <if cond="$is_name">
                        <store-get root="$sub" name="personal/temp_name_greeting.g711a" out-url="$file"/>
                    </if>
                    <else>
                        <store-get root="$sub" name="personal/temp_personal_greeting.g711a" out-url="$file"/>
                    </else>
                    <pbx-record url="$file"/>
                        <!-- Rehearsal -->
                    <store-get root="" name="se_your_input_is.$coder" out-url="$ctrl" />
                    <pbx-prompt url="$ctrl" barge-in="false"/>
                    <pbx-prompt url="$file"/>
                        <!-- Save new Announcement -->
                    <if cond="$is_name">
                        <assign out="$savefunc" value="SaveNameGreeting"/>
                    </if>
                    <else>
                        <assign out="$savefunc" value="SavePersonalGreeting"/>
                    </else>
                    <call name="SaveOrCancel"/>
                </case>
                <case equal="2">
                        <!-- Aktuell aktive Ansage abspielen -->
                    <if cond="$is_name">
                        <store-get root="$sub" name="personal/name_greeting.g711a" out-url="$file"/>
                    </if>
                    <else>
                        <store-get root="$sub" name="personal/personal_greeting.g711a" out-url="$file"/>
                    </else>
                    <pbx-prompt url="$file"/>
                </case>
                <case equal="3">
                        <!-- Aktuell aktive loeschen -->
                    <if cond="$is_name">
                        <store-del root="$sub" name="personal/name_greeting.g711a"/>
                        <store-get root="" name="se_message_deleted.$coder" out-url="$ctrl"/>
                        <pbx-prompt url="$ctrl" />
                    </if>
                    <else>
                        <store-del root="$sub" name="personal/personal_greeting.g711a"/>
                        <store-get root="" name="se_message_deleted.$coder" out-url="$ctrl"/>
                        <pbx-prompt url="$ctrl" />
                    </else>
                </case>
                <case equal="*">
                    <assign out="abort" value="true"/>
                    <store-get root="" name="se_cancel.$coder" out-url="$ctrl"/>
                    <pbx-prompt url="$ctrl" barge-in="false"/>
                </case>
                <case equal="#">
                        <!--  Hilfe -->
                    <pbx-prompt url="$menu" />
                </case>
            </switch>
        </event>
        <while notcond="$abort">
                <!--  Prompt Menueoptions -->
            <pbx-prompt url="$menu" />
            <if notcond="$abort">
                    <!-- Silence. 4 Seconds -->
                <store-get root="" name="silence.$coder" out-url="$ctrl" />
                <pbx-prompt url="$ctrl" sec="4" repeat="true"/>
            </if>
        </while>
                <!-- End -->
        <store-get root="" name="se_end_of_menu.$coder" out-url="$ctrl"/>
        <pbx-prompt url="$ctrl"/>
    </function>

    <!-- =======================PromptString, Input in $string========================================= -->

    <function define="PromptString">
        <assign out="$i" value="1"/>
        <assign out="$c" value=""/>
        <index value="$string" pos="$i" size="1" out="$c"/>
        <lib-strlen string="$c" out="$len-c" />
        <while cond="$len-c">
            <switch var="$c">
                <case equal="*">
                    <assign out="$filename" value="se_star.$coder"/>
                </case>
                <case equal="#">
                    <assign out="$filename" value="se_hash.$coder"/>
                </case>
                <default>
                    <lib-strcat string="se_" string2="$c" out-string="$ctrl"/>
                    <lib-strcat string="$ctrl" string2=".$coder" out-string="$filename"/>
                </default>
            </switch>
            <store-get root="" name="$filename" out-url="$ctrl" />
            <pbx-prompt url="$ctrl" barge-in="false"/>
            <add value="$i" value2="1" out="$i"/>
            <assign out="$c" value=""/>
            <index value="$string" pos="$i" size="1" out="$c"/>
            <lib-strlen string="$c" out="$len-c" />
        </while>
    </function>

    <!-- =======================SaveOrCancel, $savefunc contains save function to be called================= -->
    <function define="SavePin">
        <store-cookie root="$sub" name="personal/pin.txt" value="$pin"/>
        <wait sec="1"/> <!-- grant server a second -->
        <assign out="$test" value=""/>
        <store-cookie root="$sub" name="personal/pin.txt" out="$test"/>
        <switch var="$test">
            <case equal="$pin">
                <store-get root="" name="se_new_pin_is_saved.$coder" out-url="$ctrl" />
            </case>
            <default>
                <store-get root="" name="se_an_error_occurred.$coder" out-url="$ctrl" />
            </default>
        </switch>
        <pbx-prompt url="$ctrl" barge-in="false"/>
    </function>

    <function define="SaveOrCancel">
        <assign out="abortSave" value="false"/>
        <event type="dtmf" block="false">
            <timer name="vbguard" sec="$_vbguard"/>
            <pbx-getdtmfdigit out-dtmf="$dtmf" />
            <switch var="$dtmf">
                <case equal="2">
                    <call name="$savefunc"/>
                    <assign out="abortSave" value="true"/>
                </case>
                <case equal="*">
                    <assign out="abortSave" value="true"/>
                    <store-get root="" name="se_cancel.$coder" out-url="$ctrl"/>
                    <pbx-prompt url="$ctrl" barge-in="false"/>
                </case>
            </switch>
        </event>
        <while notcond="$abortSave">
                <!--  Prompt Menueoptions -->
            <store-get root="" name="se_save_2_cancel_star.$coder" out-url="$ctrl"/>
            <pbx-prompt url="$ctrl" />
            <if notcond="$abortSave">
                    <!-- Silence. 4 Seconds -->
                <store-get root="" name="silence.$coder" out-url="$ctrl" />
                <pbx-prompt url="$ctrl" sec="4" repeat="true"/>
            </if>
        </while>
    </function>
    <!-- =======================EnterPin========================================= -->
    <function define="EnterPin">
        <assign out="$pin-ok" value="false"/>
        <assign out="$test-pin" value="$_defaultpin"/>

        <event type="dtmf" block="false">
            <timer name="vbguard" sec="$_vbguard"/>
            <assign out="$pin-timeout" value="false"/>
            <pbx-getdtmfdigit out-dtmf="$dtmf" />
            <lib-strcat string="$pin" string2="$dtmf" out-string="$pin" />
            <lib-strlen string="$pin" out="$len-pin" />
            <switch var="$len-pin">
                <case equal="$len-defaultpin">
                    <assign out="$pin-ok" value="true"/>
                </case>
            </switch>
        </event>

        <store-get root="" name="se_please_enter_the_new_pin.$coder" out-url="$ctrl"/>
        <pbx-prompt url="$ctrl"/>
        <while notcond="$pin-ok">
                    <!-- Silence. 4 Seconds -->
                <store-get root="" name="silence.$coder" out-url="$ctrl" />
                <assign out="$pin-timeout" value="true"/>
                <pbx-prompt url="$ctrl" sec="4" repeat="true"/>
                <if cond="$pin-ok"><return/></if>
                <if cond="$pin-timeout">
                        <!-- Reprompt -->
                    <store-get root="" name="se_input.$coder" out-url="$ctrl"/>
                    <pbx-prompt url="$ctrl"/>
                </if>
        </while>
    </function>
    <!-- =======================MenuPin========================================= -->
    <function define="MenuPin">
        <assign out="abort" value="false"/>
        <event type="dtmf" block="false">
            <timer name="vbguard" sec="$_vbguard"/>
            <pbx-getdtmfdigit out-dtmf="$dtmf" />
            <switch var="$dtmf">
                <case equal="1">
                    <assign out="$pin" value=""/>
                    <call name="EnterPin"/>
                    <assign out="$string" value="$pin"/>
                    <store-get root="" name="se_your_input_is.$coder" out-url="$ctrl" />
                    <pbx-prompt url="$ctrl" barge-in="false"/>
                    <call name="PromptString"/>
                    <assign out="$savefunc" value="SavePin"/>
                    <call name="SaveOrCancel"/>
                </case>
                <case equal="2">
                    <assign out="$string" value=""/>
                    <store-cookie root="$sub" name="personal/pin.txt" out="$string"/>
                    <if cond="$string">
                        <store-get root="" name="se_the_current_pin_is.$coder" out-url="$ctrl" />
                        <pbx-prompt url="$ctrl" barge-in="false"/>
                        <call name="PromptString"/>
                    </if>
                    <else>
                        <store-get root="" name="se_not_available.$coder" out-url="$ctrl" />
                        <pbx-prompt url="$ctrl" barge-in="false"/>
                    </else>
                </case>
                <case equal="*">
                    <assign out="abort" value="true"/>
                    <store-get root="" name="se_cancel.$coder" out-url="$ctrl"/>
                    <pbx-prompt url="$ctrl" barge-in="false"/>
                </case>
            </switch>
        </event>
        <while notcond="$abort">
                <!--  Prompt Menueoptions -->
            <store-get root="" name="se_menu_pin.$coder" out-url="$ctrl"/>
            <pbx-prompt url="$ctrl" />
            <if notcond="$abort">
                    <!-- Silence. 4 Seconds -->
                <store-get root="" name="silence.$coder" out-url="$ctrl" />
                <pbx-prompt url="$ctrl" sec="4" repeat="true"/>
            </if>
        </while>
                <!-- End -->
        <store-get root="" name="se_end_of_menu.$coder" out-url="$ctrl"/>
        <pbx-prompt url="$ctrl"/>
    </function>
    <!-- =======================CheckPin========================================= -->
    <function define="CheckPin">
        <assign out="$pin" value=""/>
        <assign out="$pin-ok" value="false"/>

        <store-cookie root="$sub" name="personal/pin.txt" out="$test-pin"/>
        <if notcond="$test-pin">
            <assign out="$test-pin" value="$_defaultpin"/>   <!-- default -->
        </if>
        <lib-strlen string="$test-pin" out="$len-test-pin" />

        <event type="dtmf" block="false">
            <timer name="vbguard" sec="$_vbguard"/>
            <assign out="$pin-timeout" value="false"/>
            <pbx-getdtmfdigit out-dtmf="$dtmf" />
            <lib-strcat string="$pin" string2="$dtmf" out-string="$pin" />
            <lib-strlen string="$pin" out="$len-pin" />
            <switch var="$len-pin">
                <case equal="$len-test-pin">
                    <switch var="$pin">
                        <case equal="$test-pin">
                            <!-- ======Success, mark OK===== -->
                            <assign out="$pin-ok" value="true"/>
                            <return/>
                        </case>
                    </switch>
                    <!-- ======Failed, disconnect===== -->
                    <pbx-disc/>
                </case>
            </switch>
        </event>

        <store-get root="" name="se_please_enter_your_pin.$coder" out-url="$ctrl"/>
        <pbx-prompt url="$ctrl"/>
        <while notcond="$pin-ok">
                    <!-- Silence. 4 Seconds -->
                <store-get root="" name="silence.$coder" out-url="$ctrl" />
                <assign out="$pin-timeout" value="true"/>
                <pbx-prompt url="$ctrl" sec="4" repeat="true"/>
                <if cond="$pin-timeout">
                        <!-- Reprompt -->
                    <store-get root="" name="se_input.$coder" out-url="$ctrl"/>
                    <pbx-prompt url="$ctrl"/>
                </if>
        </while>
    </function>

    <!-- =======================TweakCn===================================== -->
    <function define="TweakUsrDir">
		<if notcond="$usrdir_orig">
			<assign value="$usrdir" out="$usrdir_orig"/>
		</if>
        <if cond="$_pbxmwidir">
            <lib-strcat string="$_pbxmwidir" string2="/" out-string="$tmp" />
            <lib-strcat string="$tmp" string2="$usrdir" out-string="$usrdir" />
        </if>
    </function>
    <!-- =======================SetUsrDir===================================== -->
    <function define="SetUsrDir">
		<assign value="$cn" out="$usrdir"/>
		<if cond="$app">
			<pbx-query-obj name="$cn_obj" type="h323" out="$tmpusrdir"/>
			<if cond="$tmpusrdir">
				<assign value="$tmpusrdir" out="$usrdir"/>
			</if>
			<else>
				<dbg string="Error: No SIP-info administrated for user " string2="$cn_obj"/>
                <store-get root="" name="se_an_error_occurred.$coder" out-url="$ctrl" />
                <pbx-prompt url="$ctrl" barge-in="false"/>
                <pbx-disc/>				
			</else>
		</if>
    </function>
    <!-- =======================SetUsrInfo===================================== -->
    <function define="SetUsrInfo">
			<assign value="$cn" out="$cn_obj"/>
			<call name="SetUsrDir" />
            <call name="TweakUsrDir" />
    </function>	
    <!-- =======================MakeUserDirs===================================== -->
    <function define="MakeUserDirs">
        <store-getstat root="$usrdir" name="/" out-error="$error"/>
        <if cond="$error">

			<if notcond="$leg2">
				<!-- Control mode -->
				<switch var="$cdpn">
					<case not-equal="$cgpn">
						<!-- Only the owner can create a new box -->
                    <store-get root="" name="se_the_box_does_not_exist.$coder" out-url="$ctrl" />
                    <pbx-prompt url="$ctrl" barge-in="false"/>
                    <pbx-disc/>
                </case>
            </switch>
			</if>

            <if cond="$_pbxmwidir">
                <store-mkdir root="$_pbxmwidir" name="/"/>
            </if>
            <store-mkdir root="$usrdir" name="/"/>
            <store-mkdir root="$usrdir" name="personal"/>
            <store-mkdir root="$usrdir" name="store"/>
            <!-- =====Retest==== -->
            <store-getstat root="$usrdir" name="/" out-error="$error"/>
            <if cond="$error">
                <store-get root="" name="se_an_error_occurred.$coder" out-url="$ctrl" />
                <pbx-prompt url="$ctrl" barge-in="false"/>
                <pbx-disc/>
            </if>
        </if>
    </function>
    <!-- =======================PrepMetaData===================================== -->	
	<function define="PrepMetaData">
		<assign out="$sig-meta" value=""/>
			<!-- Some meta data go into file name -->		
		<if cond="$app">
				<!-- As query string -->
			<if cond="$cgpn">
				<lib-enc type="fnqs" string="$cgpn" out-string="$cgpn-enc"/>
				<lib-strcat string="$sig-meta" string2="cgpn=" out-string="$sig-meta" />
				<lib-strcat string="$sig-meta" string2="$cgpn-enc" out-string="$sig-meta" />
			</if>
			<if cond="$src-sip">
				<lib-enc type="fnqs" string="$src-sip" out-string="$src-sip-enc"/>
				<lib-strcat string="$sig-meta" string2="&amp;sip=" out-string="$sig-meta" />
				<lib-strcat string="$sig-meta" string2="$src-sip-enc" out-string="$sig-meta" />
			</if>
			<if cond="$src-dn">
				<lib-enc type="fnqs" string="$src-dn" out-string="$src-dn-enc"/>
				<lib-strcat string="$sig-meta" string2="&amp;dn=" out-string="$sig-meta" />
				<lib-strcat string="$sig-meta" string2="$src-dn-enc" out-string="$sig-meta" />
			</if>
		</if>
		<else>
				<!-- Legacy, cpgn only -->
			<lib-strcat string="$sig-meta" string2="$cgpn" out-string="$sig-meta" />
		</else>	
	</function>
    <!-- =======================Subscript, $suburl contains script URL=========== -->
    <function define="Subscript">
        <store-getstat root="" name="$suburl" out-error="$error"/>
        <switch var="$error">
            <case equal="0">
                <exec url="$suburl"/>
            </case>
        </switch>
    </function>
    <!-- =======================Main Routine==================================== -->
    <function define="Main">
        <if notcond="$_defaultpin">
            <assign out="$_defaultpin" value="8765"/>
        </if>
		<lib-strlen string="$_defaultpin" out="$len-defaultpin" />

        <license/>
        <event type="call-end">
            <if cond="$vm">
                <store-get root="" name="email.xml" out-url="$suburl" />
                <call name="Subscript"/>
            </if>
        </event>
        <pbx-getcallinfo out-cgpn="$cgpn" out-cdpn="$cdpn" out-leg2="$leg2" out-sip="$src-sip" out-dn="$src-dn"/>
        <pbx-finduser-e164 e164="$leg2" out-cn="$cn" />
        <if cond="$cn">
            <call name="SetUsrInfo" />
            <call name="AnsweringMachine" />
        </if>
        <while notcond="$cn">
            <pbx-finduser-e164 e164="$cdpn" out-cn="$cn" />
            <if cond="$cn">
				<call name="SetUsrInfo" />
                <call name="Voicebox" />
            </if>
            <event type="call-info" block="true">
                <pbx-getcallinfo out-cdpn="$cdpn" />
            </event>
        </while>
    </function>

</voicemail>
