Template for D-series telephones

full_template.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- Template für Telefone der D-Serie  -->    
<configuration>
	<locales>
        <deviceLanguage>$(ui.language:isolocale)</deviceLanguage>
        <webserverLanguage>$(ui.language:isolocale)</webserverLanguage>
        <deviceLocation>$(pbx.loc.language:iculocale)</deviceLocation>
	</locales>
    <identities>
       <identity>
          <active>true</active>
          <displayname>$(sip.displayname)</displayname> 
          <username>$(sip.username)</username>
          <host>$(sip.server)</host>
          <password>$(sip.userpassword)</password>
          <frameSize>20</frameSize>
          <audiocodecs>
             <audiocodec>G.722</audiocodec>
             <audiocodec>G.711</audiocodec>
             <audiocodec>G.726</audiocodec>
             <audiocodec>G.729</audiocodec>
             <audiocodec>iLBC</audiocodec>
          </audiocodecs>
          <clir>displayAnonymous</clir>
          <localmoh>false</localmoh>
          <secureConnection>$(sip.usesrtp:false,true)</secureConnection>
          <srtp>$(sip.usesrtp:disabled,mandatory)</srtp>
          <checkHostname>true</checkHostname>
          <pickupCode>##06</pickupCode>
		  <certificate>$(pbx.certificate)</certificate>
       </identity>
       <identity>
          <active>false</active>
       </identity>
       <identity>
          <active>false</active>
       </identity>
       <identity>
          <active>false</active>
       </identity>
       <identity>
          <active>false</active>
       </identity>
       <identity>
          <active>false</active>
       </identity>
    </identities>
    <contacts>
       <ldap>
          <active>$(ldap.enable:false,true)</active>
          <baseDN>$(ldap.base)</baseDN>
          <ldapProtocol>ldap</ldapProtocol>
          <ldapVersion>three</ldapVersion>
          <maxHits>20</maxHits>
          <nameAttributes>cn sn givenName company o</nameAttributes>
          <nameFilter>(|(cn=%*)(sn=%*)(givenName=%*)(company=%*)(o=%*))</nameFilter>
          <numberAttributes>mobile telephoneNumber homePhone</numberAttributes>
          <numberFilter>(|(telephoneNumber=*%*)(mobile=*%*)(homePhone=*%*))</numberFilter>
          <orderBy>orderByFirstname</orderBy>
          <port>389</port>
          <reverseNumberLookupIncoming>1</reverseNumberLookupIncoming>
          <reverseNumberLookupOutgoing>1</reverseNumberLookupOutgoing>
          <password>$(ldap.password)</password>
          <server>$(ldap.server)</server>
          <username>$(ldap.username)</username>
       </ldap>
    </contacts>
    <provisioning>
        <active>true</active>
        <dhcp>false</dhcp>
        <redirect>false</redirect>
        <pbx>true</pbx>
        <execution>
            <mondays>true</mondays>
            <tuesdays>true</tuesdays>
            <wednesdays>true</wednesdays>
            <thursdays>true</thursdays>
            <fridays>true</fridays>
            <saturdays>true</saturdays>
            <sundays>true</sundays>
            <time>01:00</time>
        </execution>
  	    <sip>
    	   <active>true</active>
  	    </sip>
        <url>https://$(prov.secureserver):$(prov.port)/$(prov.path)</url>
    </provisioning>
    <timeAndDate>
        <ntpEnable>$(time.ntp_enable:false,true)</ntpEnable>
        <ntpServer>$(time.server)</ntpServer>
        <timezone>Europe/$(time.timezonename.town)</timezone>
    </timeAndDate>
	<keys>
	  <phoneXtensions>
		<key keyNumber="4" label="ID $(sip.username)">
		  <keyConfiguration>
		    <line>
		      <id>1</id>
		      <component/>
		    </line>
		  </keyConfiguration>
		  <templateName>@string/line_title</templateName>
		</key>
	  </phoneXtensions>
		<homeScreen>
		<key keyNumber="1">
		  <keyConfiguration>
		    <function>
		      <invocations>
		        <invocation>
		          <intent>
		            <component>de.auerswald.home/.MenuActivity</component>
		            <action>de.auerswald.intent.ALL_APPS</action>
		          </intent>
		        </invocation>
		      </invocations>
		    </function>
		  </keyConfiguration>
		</key>
		<key keyNumber="2">
		  <keyConfiguration>
		    <function>
		      <invocations>
		        <invocation>
		          <intent>
		            <component>com.android.dialer/.calllog.CallLogActivity</component>
		            <action>android.intent.action.MAIN</action>
		          </intent>
		        </invocation>
		      </invocations>
		    </function>
		  </keyConfiguration>
		</key>
		<key keyNumber="3">
		  <keyConfiguration>
		    <function>
		      <invocations>
		        <invocation>
		          <intent>
		            <component>de.auerswald.contacts/.AddressBookActivityFromSoftkey</component>
		            <action>android.intent.action.MAIN</action>
		          </intent>
		        </invocation>
		      </invocations>
		    </function>
		  </keyConfiguration>
		</key>
		<key keyNumber="4" label="@string/softkey_layer">
		  <keyConfiguration>
		    <function>
		      <invocations>
		        <invocation>
		          <setting path="/keyLayer/phoneXtensions">
		            <values>
		              <value>1</value>
		            </values>
		            <values>
		              <value>2</value>
		            </values>
		            <values>
		              <value>3</value>
		            </values>
		          </setting>
		        </invocation>
		      </invocations>
		    </function>
		  </keyConfiguration>
		</key>
	  </homeScreen>
	</keys> 
	<templates>
        <!-- Activate a predefined config in the PBX
           parameter: identity for PBX identification
           parameter: configuration_id
        -->
		<template prio="10" name="@string/config_activation_title">
			<keyConfiguration>
				<lua>
					<code>
						local valid = false
						local active = false
						local on = "##8*81"
						local userpath = "/identities/identity["..identity.."]/"
						local host = config.get(userpath .. "host")
						local user = config.get(userpath .. "username")
 
						local function updateLed()
							if valid then
								if active then
									key:setLed( "green" )
								else
									key:setLed( "off" )
								end
							else
								key:setLed( "red", true )
							end
						end
						local unescape = function(url)
							return url:gsub("%%(%x%x)", function(x) return string.char(tonumber(x, 16)) end )
						end
						local function check_sub(data, uri, handle)
							local x = xml.eval(data)
							local test_uri_on = "sip:"..on..konfid.."#"..user.."@"..host
							if unescape(uri) == test_uri_on then
								for _,dlg in ipairs(x) do
									state = dlg:find("state") 
									if state and state[1] == "trying" then 
										active = true
									elseif state and state[1] == "terminated" then
										active = false
									end
								end
							end
							updateLed()
						end
						local function check_valid()
							if identity == "" or konfid == "" then
								valid = false
							else
								valid = true
							end
						end
						local function sub()
							sip.subscribe(on..konfid.."#"..user,"0",identity,check_sub)
						end
						function onKeyUp()
							if not active and valid then
								sip.invite(on..konfid.."#"..user,false,identity,true)
							end
						end
						check_valid()
						updateLed()
						sub()
					</code>
					<params>
						<param name="identity"/>
						<param name="konfid"/>
					</params>
				</lua>
			</keyConfiguration>
			<parameters>
                <parameter name="@string/identity">
					<path>//param[@name="identity"]/value</path>
				</parameter>
                <parameter name="@string/configuration_id">
					<path>//param[@name="konfid"]/value</path>
				</parameter>
			</parameters>
		</template>
        <!-- switch the call forwarding within the PBX on or off
             parameter: identity
             parameter: msn: which msn to forward
             parameter: target: call forwarding target
        -->
        <template prio="10" name="@string/call_forwarding_extern_unconditional_title">
		<keyConfiguration>
			<lua>
				<code>
					local valid = false
					local active = false
					local prefix ="##96"
					local on = "*21*"
					local off = "##21#"
					local userpath = "/identities/identity["..identity.."]/"
					local host = config.get(userpath .. "host")
					local user = config.get(userpath .. "username")
 
					local function updateLed()
						if valid then
							if active then
								key:setLed( "green" )
							else
								key:setLed( "off" )
							end
						else
							key:setLed( "red", true )
						end
					end
					local unescape = function(url)
						return url:gsub("%%(%x%x)", function(x) return string.char(tonumber(x, 16)) end )
					end
					local function check_sub(data, uri, handle)
						local x = xml.eval(data)
						local test_uri_on = "sip:"..prefix..msn.."#"..on..target.."#"..user.."@"..host
						if (unescape(uri) == test_uri_on) and x ~= nil then
							for _,dlg in ipairs(x) do
								state = dlg:find("state") 
								if state and state[1] == "trying" then 
									active = true
								elseif state and state[1] == "terminated" then
									active = false
								end
							end
						end
						updateLed()
					end
					local function check_valid()
						if identity =="" or target=="" then
							valid = false
						else
							valid = true
						end
					end
					local function sub()
						sip.subscribe(prefix..msn.."#"..on..target.."#"..user,"0",identity,check_sub)
					end
					function onKeyUp()
						if active and valid then
								sip.invite(prefix..msn..off.."#"..user,false,identity,true)
						elseif valid then
							sip.invite(prefix..msn.."#"..on..target.."#"..user,false,identity,true)
						end
					end
					check_valid()
					updateLed()
					sub()
				</code>
				<params>
					<param name="identity"/>
					<param name="target"/>
					<param name="msn"/>
				</params>
			</lua>
		</keyConfiguration>
		<parameters>
            <parameter name="@string/identity">
				<path>//param[@name="identity"]/value</path>
			</parameter>
            <parameter name="@string/msn">
				<path>//param[@name="msn"]/value</path>
			</parameter>
            <parameter name="@string/target">
				<path>//param[@name="target"]/value</path>
			</parameter>
		</parameters>
		</template>
        <!-- switch the call forwarding for busy within the PBX on or off
             parameter: identity
             parameter: msn: which msn to forward
             parameter: target: call forwarding target
        -->
        <template prio="10" name="@string/call_forwarding_extern_busy_title">                
			<keyConfiguration>
				<lua>
					<code>
						local valid = false
						local active = false
						local prefix ="##96"
						local on = "*67*"
						local off = "##67#"
						local userpath = "/identities/identity["..identity.."]/"
						local host = config.get(userpath .. "host")
						local user = config.get(userpath .. "username")
						local function updateLed()
							if valid then
								if active then
									key:setLed( "green" )
								else
									key:setLed( "off" )
								end
							else
								key:setLed( "red", true )
							end
						end
 
						local unescape = function(url)
							return url:gsub("%%(%x%x)", function(x) return string.char(tonumber(x, 16)) end )
						end
						local function check_sub(data, uri, handle)
							local x = xml.eval(data)
							local test_uri_on = "sip:"..prefix..msn.."#"..on..target.."#"..user.."@"..host
							if unescape(uri) == test_uri_on and x ~= nil then
								for _,dlg in ipairs(x) do
									state = dlg:find("state") 
									if state and state[1] == "trying" then 
										active = true
									elseif state and state[1] == "terminated" then
										active = false
									end
								end
							end
							updateLed()
						end
						local function check_valid()
							if identity =="" or target=="" then
								valid = false
							else
								valid = true
							end
						end
						local function sub()
							sip.subscribe(prefix..msn.."#"..on..target.."#"..user,"0",identity,check_sub)
						end	
						function onKeyUp()
							if active and valid then
									sip.invite(prefix..msn..off.."#"..user,false,identity,true)
							elseif valid then
								sip.invite(prefix..msn.."#"..on..target.."#"..user,false,identity,true)
							end
						end
						check_valid()
						updateLed()
						sub()
					</code>
					<params>
						<param name="identity"/>
						<param name="target"/>
						<param name="msn"/>
					</params>
				</lua>
			</keyConfiguration>
			<parameters>
                <parameter name="@string/identity">
					<path>//param[@name="identity"]/value</path>
				</parameter>
                <parameter name="@string/msn">
					<path>//param[@name="msn"]/value</path>
				</parameter>
                <parameter name="@string/target">
					<path>//param[@name="target"]/value</path>
				</parameter>
			</parameters>
		</template>
        <!-- switch the call forwarding for not responding within the PBX on or off
             parameter: identity
             parameter: msn: which msn to forward
             parameter: target: call forwarding target
        -->
        <template prio="10" name="@string/call_forwarding_extern_no_response_title">
			<keyConfiguration>
				<lua>
					<code>
						local valid = false
						local active = false
						local prefix ="##96"
						local on = "*61*"
						local off = "##61#"
						local userpath = "/identities/identity["..identity.."]/"
						local host = config.get(userpath .. "host")
						local user = config.get(userpath .. "username")
 
						local function updateLed()
							if valid then
								if active then
									key:setLed( "green" )
								else
									key:setLed( "off" )
								end
							else
								key:setLed( "red", true )
							end
						end				
						local unescape = function(url)
							return url:gsub("%%(%x%x)", function(x) return string.char(tonumber(x, 16)) end )
						end
						local function check_sub(data, uri, handle)
							local x = xml.eval(data)
							local test_uri_on = "sip:"..prefix..msn.."#"..on..target.."#"..user.."@"..host
							if unescape(uri) == test_uri_on and x ~= nil then
								for _,dlg in ipairs(x) do
									state = dlg:find("state") 
									if state and state[1] == "trying" then 
										active = true
									elseif state and state[1] == "terminated" then
										active = false
									end
								end
							end
							updateLed()
						end
						local function check_valid()
							if identity =="" or target=="" then
								valid = false
							else
								valid = true
							end
						end
						local function sub()
							sip.subscribe(prefix..msn.."#"..on..target.."#"..user,"0",identity,check_sub)
						end		
						function onKeyUp()
							if active and valid then
									sip.invite(prefix..msn..off.."#"..user,false,identity,true)
							elseif valid then
								sip.invite(prefix..msn.."#"..on..target.."#"..user,false,identity,true)
							end
						end
						check_valid()
						updateLed()
						sub()
					</code>
					<params>
						<param name="identity"/>
						<param name="target"/>
						<param name="msn"/>
					</params>
				</lua>
			</keyConfiguration>
			<parameters>
				<parameter name="@string/identity">
					<path>//param[@name="identity"]/value</path>
				</parameter>
				<parameter name="@string/msn">
					<path>//param[@name="msn"]/value</path>
				</parameter>
				<parameter name="@string/target">
					<path>//param[@name="target"]/value</path>
				</parameter>
			</parameters>
		</template>
        <!-- switch the call forwarding within the PBX on or off
             parameter: identity
             parameter: target: call forwarding target
        -->
        <template prio="10" name="@string/call_forwarding_unconditional_title">
			<keyConfiguration>
				<lua>
					<code>
						local valid = false
						local active = false
						local on = "*21*"
						local off = "#21#"
						local userpath = "/identities/identity["..identity.."]/"
						local host = config.get(userpath .. "host")
						local user = config.get(userpath .. "username")		
						local function updateLed()
							if valid then
								if active then
									key:setLed( "green" )
								else
									key:setLed( "off" )
								end
							else
								key:setLed( "red", true )
							end
						end					
						local unescape = function(url)
							return url:gsub("%%(%x%x)", function(x) return string.char(tonumber(x, 16)) end )
						end
						local function check_sub(data, uri, handle)
							local x = xml.eval(data)
							local test_uri_on = "sip:"..on..target.."#"..user.."@"..host			
							if unescape(uri) == test_uri_on and x ~= nil then
								for _,dlg in ipairs(x) do
									state = dlg:find("state") 
									if state and state[1] == "trying" then 
										active = true
									elseif state and state[1] == "terminated" then
										active = false
									end
								end
							end
							updateLed()
						end
						local function check_valid()
							if identity == "" or target== "" then
								valid = false
							else
								valid = true
							end
						end
						local function sub()
							sip.subscribe(on..target.."#"..user,"0",identity,check_sub)
						end		
						function onKeyUp()
							if active and valid then
									sip.invite(off.."#"..user,false,identity,true)
							elseif valid then
								sip.invite(on..target.."#"..user,false,identity,true)
							end
						end
						check_valid()
						updateLed()
						sub()
					</code>
					<params>
						<param name="identity"/>
						<param name="target"/>
					</params>
				</lua>
			</keyConfiguration>
			<parameters>
				<parameter name="@string/identity">
					<path>//param[@name="identity"]/value</path>
				</parameter>
				<parameter name="@string/target">
					<path>//param[@name="target"]/value</path>
				</parameter>
			</parameters>
		</template>
	    <!-- switch the call forwarding for busy within the PBX on or off
             parameter: identity
             parameter: target: call forwarding target
        -->
        <template prio="10" name="@string/call_forwarding_busy_title">
			<keyConfiguration>
				<lua>
					<code>
						local valid = false
						local active = false
						local on = "*67*"
						local off = "#67#"
						local userpath = "/identities/identity["..identity.."]/"
						local host = config.get(userpath .. "host")
						local user = config.get(userpath .. "username")		
						local function updateLed()
							if valid then
								if active then
									key:setLed( "green" )
								else
									key:setLed( "off" )
								end
							else
								key:setLed( "red", true )
							end
						end					
						local unescape = function(url)
							return url:gsub("%%(%x%x)", function(x) return string.char(tonumber(x, 16)) end )
						end
						local function check_sub(data, uri, handle)
							local x = xml.eval(data)
							local test_uri_on = "sip:"..on..target.."#"..user.."@"..host
							if unescape(uri) == test_uri_on and x ~= nil then
								for _,dlg in ipairs(x) do
									state = dlg:find("state") 
									if state and state[1] == "trying" then 
										active = true
									elseif state and state[1] == "terminated" then
										active = false
									end
								end
							end
							updateLed()
						end
						local function check_valid()
							if identity =="" or target=="" then
								valid = false
							else
								valid = true
							end
						end
						local function sub()
							sip.subscribe(on..target.."#"..user,"0",identity,check_sub)
						end		
						function onKeyUp()
							if active and valid then
									sip.invite(off.."#"..user,false,identity,true)
							elseif valid then
								sip.invite(on..target.."#"..user,false,identity,true)
							end
						end
						check_valid()
						updateLed()
						sub()
					</code>
					<params>
						<param name="identity"/>
						<param name="target"/>
					</params>
				</lua>
			</keyConfiguration>
			<parameters>
				<parameter name="@string/identity">
					<path>//param[@name="identity"]/value</path>
				</parameter>
				<parameter name="@string/target">
					<path>//param[@name="target"]/value</path>
				</parameter>
			</parameters>
		</template>
	    <!-- switch the call forwarding for not responding within the PBX on or off
             parameter: identity
             parameter: target: call forwarding target
        -->
		<template prio="10" name="@string/call_forwarding_no_response_title">
			<keyConfiguration>
				<lua>
					<code>
						local valid = false
						local active = false
						local on = "*61*"
						local off = "#61#"
						local userpath = "/identities/identity["..identity.."]/"
						local host = config.get(userpath .. "host")
						local user = config.get(userpath .. "username")		
						local function updateLed()
							if valid then
								if active then
									key:setLed( "green" )
								else
									key:setLed( "off" )
								end
							else
								key:setLed( "red", true )
							end
						end					
						local unescape = function(url)
							return url:gsub("%%(%x%x)", function(x) return string.char(tonumber(x, 16)) end )
						end					
						local function check_sub(data, uri, handle)
							local x = xml.eval(data)
							local test_uri_on = "sip:"..on..target.."#"..user.."@"..host
							if unescape(uri) == test_uri_on and x ~= nil then
								for _,dlg in ipairs(x) do
									state = dlg:find("state") 
									if state and state[1] == "trying" then 
										active = true
									elseif state and state[1] == "terminated" then
										active = false
									end
								end
							end
							updateLed()
						end
						local function check_valid()
							if identity =="" or target=="" then
								valid = false
							else
								valid = true
							end
						end
						local function sub()
							sip.subscribe(on..target.."#"..user,"0",identity,check_sub)
						end		
						function onKeyUp()
							if active and valid then
									sip.invite(off.."#"..user,false,identity,true)
							elseif valid then
								sip.invite(on..target.."#"..user,false,identity,true)
							end
						end
						check_valid()
						updateLed()
						sub()
					</code>
					<params>
						<param name="identity"/>
						<param name="target"/>
					</params>
				</lua>
			</keyConfiguration>
			<parameters>
				<parameter name="@string/identity">
					<path>//param[@name="identity"]/value</path>
				</parameter>
				<parameter name="@string/target">
					<path>//param[@name="target"]/value</path>
				</parameter>
			</parameters>
		</template>
		<!-- toggles do not disturb in the PBX
        parameter: identity specifying the PBX
		-->
		<template prio="10" name="@string/do_not_disturb_title">
			<keyConfiguration>
				<lua>
					<code>
						local valid = false
						local active = false
						local on = "##8*211"
						local off = "##8*210"
						local userpath = "/identities/identity["..identity.."]/"
						local host = config.get(userpath .. "host")
						local user = config.get(userpath .. "username")	
						local function updateLed()
							if valid then
								if active then
									key:setLed( "red" )
								else
									key:setLed( "off" )
								end
							else
								key:setLed( "red", true )
							end
						end
						local unescape = function(url)
							return url:gsub("%%(%x%x)", function(x) return string.char(tonumber(x, 16)) end )
						end					
						local function check_sub(data, uri, handle)
							local x = xml.eval(data)
							local test_uri_on = "sip:"..on.."#"..user.."@"..host
							if unescape(uri) == test_uri_on and x ~= nil then
								for _,dlg in ipairs(x) do
									state = dlg:find("state") 
									if state and state[1] == "trying" then 
										active = true
									elseif state and state[1] == "terminated" then
										active = false
									end
								end
							end
							updateLed()
						end
						local function check_valid()
							if identity == "" then
								valid = false
							else
								valid = true
							end
						end
						local function sub()
							sip.subscribe(on.."#"..user,"0",identity,check_sub)
						end		
						function onKeyUp()
							if active and valid then
									sip.invite(off.."#"..user,false,identity,true)
							elseif valid then
								sip.invite(on.."#"..user,false,identity,true)
							end
						end
						check_valid()
						updateLed()
						sub()
					</code>
					<params>
						<param name="identity"/>
					</params>
				</lua>
			</keyConfiguration>
			<parameters>
				<parameter name="@string/identity">
					<path>//param[@name="identity"]/value</path>
				</parameter>
			</parameters>
		</template>
		<!-- toggle a group membership
        parameter: identity specifying the PBX
        parameter: group id
         -->
		<template prio="10" name="@string/group_title">
			<keyConfiguration>
				<lua>
					<code>
						local valid = false
						local active = false
						local on = "##8*401"
						local off = "##8*400"
						local userpath = "/identities/identity["..identity.."]/"
						local host = config.get(userpath .. "host")
						local user = config.get(userpath .. "username")		
						local function updateLed()
							if valid then
								if active then
									key:setLed( "green" )
								else
									key:setLed( "off" )
								end
							else
								key:setLed( "red", true )
							end
						end
						local unescape = function(url)
							return url:gsub("%%(%x%x)", function(x) return string.char(tonumber(x, 16)) end )
						end					
						local function check_sub(data, uri, handle)
							local x = xml.eval(data)
							local test_uri_on = "sip:"..on..group.."#"..user.."@"..host
							if unescape(uri) == test_uri_on and x ~= nil then
								for _,dlg in ipairs(x) do
									state = dlg:find("state") 
									if state and state[1] == "trying" then 
										active = true
									elseif state and state[1] == "terminated" then
										active = false
									end
								end
							end
							updateLed()
						end
						local function check_valid()
							if identity == "" or group == "" then
								valid = false
							else
								valid = true
							end
						end
						local function sub()
							sip.subscribe(on..group.."#"..user,"0",identity,check_sub)
						end		
						function onKeyUp()
							if active and valid then
									sip.invite(off..group.."#"..user,false,identity,true)
							elseif valid then
								sip.invite(on..group.."#"..user,false,identity,true)
							end
						end
						check_valid()
						updateLed()
						sub()
					</code>
					<params>
						<param name="identity"/>
						<param name="group"/>
					</params>
				</lua>
			</keyConfiguration>
			<parameters>
				<parameter name="@string/identity">
					<path>//param[@name="identity"]/value</path>
				</parameter>
				<parameter name="@string/group">
					<path>//param[@name="group"]/value</path>
				</parameter>
			</parameters>
		</template>
        </templates>
    </configuration>
function_templates.xml
  <?xml version="1.0" encoding="utf-8"?>
  <templates>
  <!-- Activate a predefined config in the PBX
    parameter: identity for PBX identification
    parameter: configuration_id
        -->
    <template prio="10" name="@string/config_activation_title">
			<keyConfiguration>
				<lua>
					<code>
						local valid = false
						local active = false
						local on = "##8*81"
						local userpath = "/identities/identity["..identity.."]/"
						local host = config.get(userpath .. "host")
						local user = config.get(userpath .. "username")
 
						local function updateLed()
							if valid then
								if active then
									key:setLed( "green" )
								else
									key:setLed( "off" )
								end
							else
								key:setLed( "red", true )
							end
						end
						local unescape = function(url)
							return url:gsub("%%(%x%x)", function(x) return string.char(tonumber(x, 16)) end )
						end
						local function check_sub(data, uri, handle)
							local x = xml.eval(data)
							local test_uri_on = "sip:"..on..konfid.."#"..user.."@"..host
							if unescape(uri) == test_uri_on then
								for _,dlg in ipairs(x) do
									state = dlg:find("state") 
									if state and state[1] == "trying" then 
										active = true
									elseif state and state[1] == "terminated" then
										active = false
									end
								end
							end
							updateLed()
						end
						local function check_valid()
							if identity == "" or konfid == "" then
								valid = false
							else
								valid = true
							end
						end
 
						local function sub()
							sip.subscribe(on..konfid.."#"..user,"0",identity,check_sub)
						end
						function onKeyUp()
							if not active and valid then
								sip.invite(on..konfid.."#"..user,false,identity,true)
							end
						end
						check_valid()
						updateLed()
						sub()
					</code>
					<params>
						<param name="identity"/>
						<param name="konfid"/>
					</params>
				</lua>
			</keyConfiguration>
			<parameters>
                <parameter name="@string/identity">
					<path>//param[@name="identity"]/value</path>
				</parameter>
                <parameter name="@string/configuration_id">
					<path>//param[@name="konfid"]/value</path>
				</parameter>
			</parameters>
		</template>
        <!-- switch the call forwarding within the PBX on or off
             parameter: identity
             parameter: msn: which msn to forward
             parameter: target: call forwarding target
        -->
        <template prio="10" name="@string/call_forwarding_extern_unconditional_title">
		<keyConfiguration>
			<lua>
				<code>
					local valid = false
					local active = false
					local prefix ="##96"
					local on = "*21*"
					local off = "##21#"
					local userpath = "/identities/identity["..identity.."]/"
					local host = config.get(userpath .. "host")
					local user = config.get(userpath .. "username")
 
					local function updateLed()
						if valid then
							if active then
								key:setLed( "green" )
							else
								key:setLed( "off" )
							end
						else
							key:setLed( "red", true )
						end
					end
					local unescape = function(url)
						return url:gsub("%%(%x%x)", function(x) return string.char(tonumber(x, 16)) end )
					end
					local function check_sub(data, uri, handle)
						local x = xml.eval(data)
						local test_uri_on = "sip:"..prefix..msn.."#"..on..target.."#"..user.."@"..host
						if (unescape(uri) == test_uri_on) and x ~= nil then
							for _,dlg in ipairs(x) do
								state = dlg:find("state") 
								if state and state[1] == "trying" then 
									active = true
								elseif state and state[1] == "terminated" then
									active = false
								end
							end
						end
						updateLed()
					end
					local function check_valid()
						if identity =="" or target=="" then
							valid = false
						else
							valid = true
						end
					end
					local function sub()
						sip.subscribe(prefix..msn.."#"..on..target.."#"..user,"0",identity,check_sub)
					end
					function onKeyUp()
						if active and valid then
								sip.invite(prefix..msn..off.."#"..user,false,identity,true)
						elseif valid then
							sip.invite(prefix..msn.."#"..on..target.."#"..user,false,identity,true)
						end
					end
					check_valid()
					updateLed()
					sub()
				</code>
				<params>
					<param name="identity"/>
					<param name="target"/>
					<param name="msn"/>
				</params>
			</lua>
		</keyConfiguration>
		<parameters>
            <parameter name="@string/identity">
				<path>//param[@name="identity"]/value</path>
			</parameter>
            <parameter name="@string/msn">
				<path>//param[@name="msn"]/value</path>
			</parameter>
            <parameter name="@string/target">
				<path>//param[@name="target"]/value</path>
			</parameter>
		</parameters>
		</template>
        <!-- switch the call forwarding for busy within the PBX on or off
             parameter: identity
             parameter: msn: which msn to forward
             parameter: target: call forwarding target
        -->
        <template prio="10" name="@string/call_forwarding_extern_busy_title">                
			<keyConfiguration>
				<lua>
					<code>
						local valid = false
						local active = false
						local prefix ="##96"
						local on = "*67*"
						local off = "##67#"
						local userpath = "/identities/identity["..identity.."]/"
						local host = config.get(userpath .. "host")
						local user = config.get(userpath .. "username")
						local function updateLed()
							if valid then
								if active then
									key:setLed( "green" )
								else
									key:setLed( "off" )
								end
							else
								key:setLed( "red", true )
							end
						end
 
						local unescape = function(url)
							return url:gsub("%%(%x%x)", function(x) return string.char(tonumber(x, 16)) end )
						end
						local function check_sub(data, uri, handle)
							local x = xml.eval(data)
							local test_uri_on = "sip:"..prefix..msn.."#"..on..target.."#"..user.."@"..host
							if unescape(uri) == test_uri_on and x ~= nil then
								for _,dlg in ipairs(x) do
									state = dlg:find("state") 
									if state and state[1] == "trying" then 
										active = true
									elseif state and state[1] == "terminated" then
										active = false
									end
								end
							end
							updateLed()
						end
						local function check_valid()
							if identity =="" or target=="" then
								valid = false
							else
								valid = true
							end
						end
						local function sub()
							sip.subscribe(prefix..msn.."#"..on..target.."#"..user,"0",identity,check_sub)
						end	
						function onKeyUp()
							if active and valid then
									sip.invite(prefix..msn..off.."#"..user,false,identity,true)
							elseif valid then
								sip.invite(prefix..msn.."#"..on..target.."#"..user,false,identity,true)
							end
						end
						check_valid()
						updateLed()
						sub()
					</code>
					<params>
						<param name="identity"/>
						<param name="target"/>
						<param name="msn"/>
					</params>
				</lua>
			</keyConfiguration>
			<parameters>
                <parameter name="@string/identity">
					<path>//param[@name="identity"]/value</path>
				</parameter>
                <parameter name="@string/msn">
					<path>//param[@name="msn"]/value</path>
				</parameter>
                <parameter name="@string/target">
					<path>//param[@name="target"]/value</path>
				</parameter>
			</parameters>
		</template>
        <!-- switch the call forwarding for not responding within the PBX on or off
             parameter: identity
             parameter: msn: which msn to forward
             parameter: target: call forwarding target
        -->
        <template prio="10" name="@string/call_forwarding_extern_no_response_title">
			<keyConfiguration>
				<lua>
					<code>
						local valid = false
						local active = false
						local prefix ="##96"
						local on = "*61*"
						local off = "##61#"
						local userpath = "/identities/identity["..identity.."]/"
						local host = config.get(userpath .. "host")
						local user = config.get(userpath .. "username")
 
						local function updateLed()
							if valid then
								if active then
									key:setLed( "green" )
								else
									key:setLed( "off" )
								end
							else
								key:setLed( "red", true )
							end
						end				
						local unescape = function(url)
							return url:gsub("%%(%x%x)", function(x) return string.char(tonumber(x, 16)) end )
						end
						local function check_sub(data, uri, handle)
							local x = xml.eval(data)
							local test_uri_on = "sip:"..prefix..msn.."#"..on..target.."#"..user.."@"..host
							if unescape(uri) == test_uri_on and x ~= nil then
								for _,dlg in ipairs(x) do
									state = dlg:find("state") 
									if state and state[1] == "trying" then 
										active = true
									elseif state and state[1] == "terminated" then
										active = false
									end
								end
							end
							updateLed()
						end
						local function check_valid()
							if identity =="" or target=="" then
								valid = false
							else
								valid = true
							end
						end
						local function sub()
							sip.subscribe(prefix..msn.."#"..on..target.."#"..user,"0",identity,check_sub)
						end		
						function onKeyUp()
							if active and valid then
									sip.invite(prefix..msn..off.."#"..user,false,identity,true)
							elseif valid then
								sip.invite(prefix..msn.."#"..on..target.."#"..user,false,identity,true)
							end
						end
						check_valid()
						updateLed()
						sub()
					</code>
					<params>
						<param name="identity"/>
						<param name="target"/>
						<param name="msn"/>
					</params>
				</lua>
			</keyConfiguration>
			<parameters>
				<parameter name="@string/identity">
					<path>//param[@name="identity"]/value</path>
				</parameter>
				<parameter name="@string/msn">
					<path>//param[@name="msn"]/value</path>
				</parameter>
				<parameter name="@string/target">
					<path>//param[@name="target"]/value</path>
				</parameter>
			</parameters>
		</template>
        <!-- switch the call forwarding within the PBX on or off
             parameter: identity
             parameter: target: call forwarding target
        -->
        <template prio="10" name="@string/call_forwarding_unconditional_title">
			<keyConfiguration>
				<lua>
					<code>
						local valid = false
						local active = false
						local on = "*21*"
						local off = "#21#"
						local userpath = "/identities/identity["..identity.."]/"
						local host = config.get(userpath .. "host")
						local user = config.get(userpath .. "username")		
						local function updateLed()
							if valid then
								if active then
									key:setLed( "green" )
								else
									key:setLed( "off" )
								end
							else
								key:setLed( "red", true )
							end
						end					
						local unescape = function(url)
							return url:gsub("%%(%x%x)", function(x) return string.char(tonumber(x, 16)) end )
						end
						local function check_sub(data, uri, handle)
							local x = xml.eval(data)
							local test_uri_on = "sip:"..on..target.."#"..user.."@"..host			
							if unescape(uri) == test_uri_on and x ~= nil then
								for _,dlg in ipairs(x) do
									state = dlg:find("state") 
									if state and state[1] == "trying" then 
										active = true
									elseif state and state[1] == "terminated" then
										active = false
									end
								end
							end
							updateLed()
						end
						local function check_valid()
							if identity == "" or target== "" then
								valid = false
							else
								valid = true
							end
						end
						local function sub()
							sip.subscribe(on..target.."#"..user,"0",identity,check_sub)
						end		
						function onKeyUp()
							if active and valid then
									sip.invite(off.."#"..user,false,identity,true)
							elseif valid then
								sip.invite(on..target.."#"..user,false,identity,true)
							end
						end
						check_valid()
						updateLed()
						sub()
					</code>
					<params>
						<param name="identity"/>
						<param name="target"/>
					</params>
				</lua>
			</keyConfiguration>
			<parameters>
				<parameter name="@string/identity">
					<path>//param[@name="identity"]/value</path>
				</parameter>
				<parameter name="@string/target">
					<path>//param[@name="target"]/value</path>
				</parameter>
			</parameters>
		</template>
	    <!-- switch the call forwarding for busy within the PBX on or off
             parameter: identity
             parameter: target: call forwarding target
        -->
        <template prio="10" name="@string/call_forwarding_busy_title">
			<keyConfiguration>
				<lua>
					<code>
						local valid = false
						local active = false
						local on = "*67*"
						local off = "#67#"
						local userpath = "/identities/identity["..identity.."]/"
						local host = config.get(userpath .. "host")
						local user = config.get(userpath .. "username")		
						local function updateLed()
							if valid then
								if active then
									key:setLed( "green" )
								else
									key:setLed( "off" )
								end
							else
								key:setLed( "red", true )
							end
						end					
						local unescape = function(url)
							return url:gsub("%%(%x%x)", function(x) return string.char(tonumber(x, 16)) end )
						end
						local function check_sub(data, uri, handle)
							local x = xml.eval(data)
							local test_uri_on = "sip:"..on..target.."#"..user.."@"..host
							if unescape(uri) == test_uri_on and x ~= nil then
								for _,dlg in ipairs(x) do
									state = dlg:find("state") 
									if state and state[1] == "trying" then 
										active = true
									elseif state and state[1] == "terminated" then
										active = false
									end
								end
							end
							updateLed()
						end
						local function check_valid()
							if identity =="" or target=="" then
								valid = false
							else
								valid = true
							end
						end
						local function sub()
							sip.subscribe(on..target.."#"..user,"0",identity,check_sub)
						end		
						function onKeyUp()
							if active and valid then
									sip.invite(off.."#"..user,false,identity,true)
							elseif valid then
								sip.invite(on..target.."#"..user,false,identity,true)
							end
						end
						check_valid()
						updateLed()
						sub()
					</code>
					<params>
						<param name="identity"/>
						<param name="target"/>
					</params>
				</lua>
			</keyConfiguration>
			<parameters>
				<parameter name="@string/identity">
					<path>//param[@name="identity"]/value</path>
				</parameter>
				<parameter name="@string/target">
					<path>//param[@name="target"]/value</path>
				</parameter>
			</parameters>
		</template>
	    <!-- switch the call forwarding for not responding within the PBX on or off
             parameter: identity
             parameter: target: call forwarding target
        -->
		<template prio="10" name="@string/call_forwarding_no_response_title">
			<keyConfiguration>
				<lua>
					<code>
						local valid = false
						local active = false
						local on = "*61*"
						local off = "#61#"
						local userpath = "/identities/identity["..identity.."]/"
						local host = config.get(userpath .. "host")
						local user = config.get(userpath .. "username")		
						local function updateLed()
							if valid then
								if active then
									key:setLed( "green" )
								else
									key:setLed( "off" )
								end
							else
								key:setLed( "red", true )
							end
						end					
						local unescape = function(url)
							return url:gsub("%%(%x%x)", function(x) return string.char(tonumber(x, 16)) end )
						end					
						local function check_sub(data, uri, handle)
							local x = xml.eval(data)
							local test_uri_on = "sip:"..on..target.."#"..user.."@"..host
							if unescape(uri) == test_uri_on and x ~= nil then
								for _,dlg in ipairs(x) do
									state = dlg:find("state") 
									if state and state[1] == "trying" then 
										active = true
									elseif state and state[1] == "terminated" then
										active = false
									end
								end
							end
							updateLed()
						end
						local function check_valid()
							if identity =="" or target=="" then
								valid = false
							else
								valid = true
							end
						end
						local function sub()
							sip.subscribe(on..target.."#"..user,"0",identity,check_sub)
						end		
						function onKeyUp()
							if active and valid then
									sip.invite(off.."#"..user,false,identity,true)
							elseif valid then
								sip.invite(on..target.."#"..user,false,identity,true)
							end
						end
						check_valid()
						updateLed()
						sub()
					</code>
					<params>
						<param name="identity"/>
						<param name="target"/>
					</params>
				</lua>
			</keyConfiguration>
			<parameters>
				<parameter name="@string/identity">
					<path>//param[@name="identity"]/value</path>
				</parameter>
				<parameter name="@string/target">
					<path>//param[@name="target"]/value</path>
				</parameter>
			</parameters>
		</template>
		<!-- toggles do not disturb in the PBX
        parameter: identity specifying the PBX
		-->
		<template prio="10" name="@string/do_not_disturb_title">
			<keyConfiguration>
				<lua>
					<code>
						local valid = false
						local active = false
						local on = "##8*211"
						local off = "##8*210"
						local userpath = "/identities/identity["..identity.."]/"
						local host = config.get(userpath .. "host")
						local user = config.get(userpath .. "username")	
						local function updateLed()
							if valid then
								if active then
									key:setLed( "red" )
								else
									key:setLed( "off" )
								end
							else
								key:setLed( "red", true )
							end
						end
						local unescape = function(url)
							return url:gsub("%%(%x%x)", function(x) return string.char(tonumber(x, 16)) end )
						end					
						local function check_sub(data, uri, handle)
							local x = xml.eval(data)
							local test_uri_on = "sip:"..on.."#"..user.."@"..host
							if unescape(uri) == test_uri_on and x ~= nil then
								for _,dlg in ipairs(x) do
									state = dlg:find("state") 
									if state and state[1] == "trying" then 
										active = true
									elseif state and state[1] == "terminated" then
										active = false
									end
								end
							end
							updateLed()
						end
						local function check_valid()
							if identity == "" then
								valid = false
							else
								valid = true
							end
						end
						local function sub()
							sip.subscribe(on.."#"..user,"0",identity,check_sub)
						end		
						function onKeyUp()
							if active and valid then
									sip.invite(off.."#"..user,false,identity,true)
							elseif valid then
								sip.invite(on.."#"..user,false,identity,true)
							end
						end
						check_valid()
						updateLed()
						sub()
					</code>
					<params>
						<param name="identity"/>
					</params>
				</lua>
			</keyConfiguration>
			<parameters>
				<parameter name="@string/identity">
					<path>//param[@name="identity"]/value</path>
				</parameter>
			</parameters>
		</template>
		<!-- toggle a group membership
        parameter: identity specifying the PBX
        parameter: group id
         -->
		<template prio="10" name="@string/group_title">
			<keyConfiguration>
				<lua>
					<code>
						local valid = false
						local active = false
						local on = "##8*401"
						local off = "##8*400"
						local userpath = "/identities/identity["..identity.."]/"
						local host = config.get(userpath .. "host")
						local user = config.get(userpath .. "username")		
						local function updateLed()
							if valid then
								if active then
									key:setLed( "green" )
								else
									key:setLed( "off" )
								end
							else
								key:setLed( "red", true )
							end
						end
						local unescape = function(url)
							return url:gsub("%%(%x%x)", function(x) return string.char(tonumber(x, 16)) end )
						end					
						local function check_sub(data, uri, handle)
							local x = xml.eval(data)
							local test_uri_on = "sip:"..on..group.."#"..user.."@"..host
							if unescape(uri) == test_uri_on and x ~= nil then
								for _,dlg in ipairs(x) do
									state = dlg:find("state") 
									if state and state[1] == "trying" then 
										active = true
									elseif state and state[1] == "terminated" then
										active = false
									end
								end
							end
							updateLed()
						end
						local function check_valid()
							if identity == "" or group == "" then
								valid = false
							else
								valid = true
							end
						end
						local function sub()
							sip.subscribe(on..group.."#"..user,"0",identity,check_sub)
						end		
						function onKeyUp()
							if active and valid then
									sip.invite(off..group.."#"..user,false,identity,true)
							elseif valid then
								sip.invite(on..group.."#"..user,false,identity,true)
							end
						end
						check_valid()
						updateLed()
						sub()
					</code>
					<params>
						<param name="identity"/>
						<param name="group"/>
					</params>
				</lua>
			</keyConfiguration>
			<parameters>
				<parameter name="@string/identity">
					<path>//param[@name="identity"]/value</path>
				</parameter>
				<parameter name="@string/group">
					<path>//param[@name="group"]/value</path>
				</parameter>
			</parameters>
		</template>
	</templates>