Skip to content

Default LuaLibraries#

luaLibraries.xml
<?xml version="1.0" encoding="utf-8"?>
<luaLibraries version="1.20.8">
  <luaLibrary name="callforward">
    <code><![CDATA[
local valid = false
local active = config.get( pathPrefix .. "active" )

local function updateLed()
    if not valid then
        key:setLed( "orange" )
    elseif active == "true" then
        key:setLed( "green" )
    else
        key:setLed( "off" )
    end
end

local function checkValid()
    local value = config.get( pathPrefix .. "target" )
    valid = value and value:len() > 0
end

local function checkActive()
    active = config.get( pathPrefix .. "active" )
end

local function configListener( path )
    checkValid()
    checkActive()
    updateLed()
end

function onKeyUp()
    --[] check for validity []--
    if not valid then
        return
    end

    local value = config.get( pathPrefix .. "active" )
    if value == "false" then
        config.set( pathPrefix .. "active", "true" )
    elseif value == "true" then
        config.set( pathPrefix .. "active", "false" )
    end
end

config.register( pathPrefix, configListener )
checkValid()
updateLed()]]></code>
  </luaLibrary>
  <luaLibrary name="subscription_key">
    <code><![CDATA[
-- returns a function that needs to be called to start this subscription_key, it takes the following parameters in this order:
local identity = "" -- index of the identity to be used
local activeColor = "green" -- which color to constantly light LED in when subscription reports 'active'
local fctName = "myKeyFct" -- name of this function to use in logs, e.g.: DND or CallForwardWhenBusy
local isValid = function() -- reports if key has all it's params set to valid values. Set to nil to keep this default
    if identity == "" or identity == nil then
        return false
    end
    return true
end
local createUri = function(onNotOff, user) -- returns the sip-uri (or at least the user-name-part) to turn the feature
    return ""                              -- either on or off through silent call. user contains the username of the
end                                        -- associated identity
---------------------------------------------------------------------------------------------------------------
local userpath = "/identities/identity[1]/" -- will be overwritten in start()
local subscriptionIsTrying = false
local subscriptionIsWorking = false
local activatedOnServer = false
local inLimbo = false -- true when silent call to change setting is send until new notify is received
local currentLimboTimerId = 42 -- we can only start timers, never stop -> use ID to discard obsoleted timers when they fire
local lastWasOnCmd = false -- whether last silent-call made was to activate the feature or not (aka de-activate)
local user = ""
local domain = ""
local ongoingSubscription = nil

local function updateLed()
    if subscriptionIsWorking then
        if activatedOnServer then
            key:setLed( activeColor )
        else
            key:setLed( "off" )
        end
    elseif subscriptionIsTrying then
        key:setLed("orange", true) -- trying
    else
        key:setLed("orange", false) -- failed, nothing can be done atm (phone will auto-retry)
    end
end

local function handleNotify(subscr, data, headers)
    subscriptionIsWorking = true
    subscriptionIsTrying = false
    local x = xml.eval(data)
    if x ~= nil then
        debug.log("received subscr data from "..subscr:getUri() , "i")
        inLimbo = false
        local set_activatedOnServer = false
        for _,dlg in ipairs(x) do
            state = dlg:find("state")
            if state and state[1] == "trying" then
                set_activatedOnServer = true
                break
            end
        end
        activatedOnServer = set_activatedOnServer
    else
        debug.log("received empty notify from "..subscr:getUri() , "e")
    end
    updateLed()
end

local function unsubscribe()
    subscriptionIsWorking = false
    subscriptionIsTrying = false
    if (ongoingSubscription) then
        ongoingSubscription:unsubscribe()
        ongoingSubscription = nil
    end
end

local function subscriptionTerminated()
    subscriptionIsWorking = false
    subscriptionIsTrying = false
    updateLed()
end

local function onSubscrIsTrying()
    subscriptionIsWorking = false
    subscriptionIsTrying = true
    updateLed()
end

local function subscribe()
    unsubscribe()
    ongoingSubscription = sip.subscribe{uri=createUri(true, user), onNotify=handleNotify, line=identity,
                                        onTerminated=subscriptionTerminated, onTrying=onSubscrIsTrying}
end

local function identityChanged(path)
    new_user = config.get(userpath .. "username")
    new_domain = sip.identities.getDomain(identity);
    if new_user ~= user or new_domain ~= domain then
        unsubscribe()
        user = new_user
        domain = new_domain
        subscribe()
        updateLed()
    end
end

local function setupRegChange()
    config.register(userpath, identityChanged)
    identityChanged()
end

local function newLimboTimer(myId)
    timer = function ()
        if inLimbo and myId == currentLimboTimerId then
            debug.log("no news from server after we've issued a change command, trying to force update through re-subscribing", "w")
            unsubscribe()
            subscribe()
            updateLed()
        end
    end
    return timer
end

local function sendCmd (turnItOnNotOff)
    lastWasOnCmd = turnItOnNotOff
    local url =  createUri(turnItOnNotOff, user)
    if url == "" or url == nil then
        debug.log("there is no way to "..(turnItOnNotOff and "activate " or "deactivate ")..fctName, "e")
    else
        debug.log("calling server to "..(turnItOnNotOff and "activate " or "deactivate ")..fctName, "d")
        if not inLimbo then
            inLimbo = true
            currentLimboTimerId = currentLimboTimerId + 1
            time.callbackIn(newLimboTimer(currentLimboTimerId), 14)
        end
        sip.invite{uri=url, line=identity, hidden=true}
        key:setLed(activeColor, true)
    end
end

function onKeyUp()
    debug.log(fctName.."-key pressed", "d")
    if not isValid() then
        debug.log("missing parameter(s), "..fctName.."-key won't work ever", "e")
    elseif not ongoingSubscription then
        debug.log("subscription had failed before, attempting restart", "i")
        subscribe()
        updateLed()
    elseif not subscriptionIsWorking then
        debug.log("subscription is being tried: stopping and restarting", "i")
        unsubscribe()
        subscribe()
        updateLed()
    else
        if inLimbo then
            debug.log("no news from server yet, trying the opposite command instead", "i")
            sendCmd(not lastWasOnCmd);
        else
            sendCmd(not activatedOnServer);
        end
    end
end

local startFkt = function(id, aColor, name, validFkt, cUri)
    identity = id
    userpath = "/identities/identity["..id.."]/"
    activeColor = aColor
    fctName = name
    createUri = cUri
    if validFkt then
      isValid = validFkt
    end

    if isValid() then
        setupRegChange()
    else
        debug.log("missing parameters, "..fctName.."-key won't work", "e")
    end
    updateLed()
end
return startFkt]]></code>
  </luaLibrary>
</luaLibraries>