<?xml version="1.0" encoding="utf-8" ?>
<voicemail xmlns="http://www.innovaphone.com/xsd/voicemail6.xsd">
    <function define="StripMailto">
        <index value="$to" pos="1" size="7" out="$scheme"/>
        <if cond="$scheme">
            <switch var="$scheme">
                <case equal="mailto:">
                    <index value="$to" pos="8" size="0" out="$to"/> <!--skip "mailto" -->
                    <return/>
                </case>
            </switch>
        </if>
    </function>

    <function define="CheckTo">
        <assign out="$i" value="1"/>
        <assign out="$c" value=""/>
        <return notcond="$to"/>
        <index value="$to" pos="$i" size="1" out="$c"/>
        <lib-strlen string="$c" out="$len-c" />
        <while cond="$len-c">
            <switch var="$c">
                <case equal="@">
                    <return/>          <!-- '@' found, smells like an email address -->
                </case>
            </switch>

            <add value="$i" value2="1" out="$i"/>
            <assign out="$c" value=""/>
            <index value="$to" pos="$i" size="1" out="$c"/>
            <lib-strlen string="$c" out="$len-c" />
        </while>
        <assign out="$to" value=""/>   <!-- doesn't seem to be an email address -->
    </function>
	
	<function define="QueryEmail">
		<!-- V10 -->
		<pbx-query-obj name="$cn_obj" type="email" out="$to"/>
		<call name="CheckTo"/>
	</function>

	<function define="LegacyQueryEmail">
		<!-- Prior V10 -->
		<pbx-query-obj name="$cn_obj" type="url" out="$to"/>
		<call name="CheckTo"/>
		<if cond="$to">
			<call name="StripMailto"/>
		</if>
		<else>
			<store-cookie root="$sub" name="personal/mail.txt" out="$to"/>
		</else>		
	</function>
	
    <function define="Main">
		<if cond="$app">
			<dbg string="App Mode:switch $email=" string2="$email"/>
			<if notcond="$email">
			    <!-- variable $email as on/off switch in app mode -->
				<return/>
			</if>
		</if>
		
        <store-getstat root="" name="$vm" out-size="$size" out-error="$error"/>
        <switch var="$error">
            <case equal="0">
                <call name="QueryEmail"/>
                <if notcond="$to">
                    <call name="LegacyQueryEmail"/>
                </if>
                <if cond="$to">
                    <assign out="$from" value="$to"/>    <!--from email = to address by default-->
                    <assign out="$subject" value="You have received a new voicemail from: "/>

					<assign out="$server" value=""/>
					<assign out="$user" value=""/>
					<assign out="$password" value=""/>
					
					<!-- read static config from PBX/Config/Authentication/Email Verification -->
					<if cond="$_pbx_auth_email_server">
						<assign out="$server" value="$_pbx_auth_email_server"/>  <!--smtp server-->
					</if>
					<if cond="$_pbx_auth_email_usr">
						<assign out="$user" value="$_pbx_auth_email_usr"/>       <!--auth info-->
					</if>
					<if cond="$_pbx_auth_email_pwd">
						<assign out="$password" value="$_pbx_auth_email_pwd"/>   <!--auth info-->
					</if>					
					<if cond="$_pbx_auth_email_addr">
						<assign out="$from" value="$_pbx_auth_email_addr"/>   <!--optional, from email-->
					</if>
					
					<if cond="$vm">					
						<assign out="$file" value="$vm"/>       <!-- $vm holds file name -->
					</if>

                        <!--Now send the email-->
					<if cond="$_pbx_version_soap">
						<lib-enc type="url" string="$file" out-string="$file"/>
						<lib-enc type="url" string="$user" out-string="$user"/>
						<lib-enc type="url" string="$password" out-string="$password"/>
					</if>
					<assign out="$exec-err" value="0"/>
                    <exec url="mailto:$to?from=$from&amp;subject=$subject&amp;subject=$cgpn&amp;srv=$server&amp;usr=$user&amp;pwd=$password&amp;file=$file" out-error="$exec-err"/>
					<if cond="$exec-err">
						<!-- 2nd try -->
						<exec url="mailto:$to?from=$from&amp;subject=$subject&amp;subject=$cgpn&amp;srv=$server&amp;usr=$user&amp;pwd=$password&amp;file=$file"/>
					</if>
                </if>
            </case>
        </switch>
    </function>
</voicemail>
