Pureish LUA Dial Plan

Hi folks.

I’ve not been able to find much in the way of pure LUA dial plans out there, so I thought I’d post mine - partially to show off (never used LUA before) but more importantly for opinions of things to do differently.

Before I show you the dial plan, I will explain why I seperate the destinations out before handing over to nodephone - well, I intend to have more then one carrier (pennytel for example)

I’m also aware that I could have done it all in AEL or even standard extensions, but I want to add much more to it in coming weeks.

configuration = {
        area = 7;
        country = 61;
}

function linenotinuse()
        app.wait(1)
        app.answer()
        app.playback("tt-monkeys")
        app.hangup()
end

function time()
        app.datetime()
        app.wait(1)
        -- TODO: replace with while loop
        time()
end

function call_emergency(ctx, ext)
        app.dial("SIP/nodephone/" .. ext, 600, 'TWK')
end

function call_localrate(ctx, ext)
        app.dial("SIP/nodephone/" .. ext, 600, 'TWK')
end

function call_freecall(ctx, ext)
        app.dial("SIP/nodephone/" .. ext, 600, 'TWK')
end

function call_local(ctx, ext)
        app.dial("SIP/nodephone/0" .. configuration.area .. ext, 600, 'TWK')
end

function call_std(ctx, ext)
        app.dial("SIP/nodephone/" .. ext, 600, 'TWK')
end

function call_mobile(ctx, ext)
        app.dial("SIP/nodephone/" .. ext, 600, 'TWK')
end

function call_international(ctx, ext)
        app.dial("SIP/nodephone/" .. ext, 600, 'TWK')
end

function call_lan(ctx, ext)
        app.chanisavail("SIP/" .. ext, "s")
        status = channel["AVAILSTATUS"]:get()
        app.noop("Status: " .. status)
        if status == "4" or status == "5" or status == "20" then
                app.playback("pbx-invalid")
                return
        end
        app.dial("SIP/" .. ext)
end

function nodephoneincoming()
        app.chanisavail("SIP/9100&SIP/9101", "s")
        status = channel["AVAILSTATUS"]:get()
        if string.find(status, '2', 1, true) == nil and string.find(status, '3', 1, true) == nil and string.find(status, '6', 1, true) == nil then
                app.dial("SIP/9100&SIP/9101", 60, 'twk')
                dialstatus = channel["DIALSTATUS"]:get()
        else
                dialstatus = "BUSY"
        end
        if not dialstatus == "ANSWER" then
                app.noop(dialstatus)
                app.goto("takemessage", "s", 1)
        end
end

function takemessage()
        if count == nil then count = 0 elseif count < 2 then count = count + 1 else hangup() end
        if count == 0 then
                app.answer()
                app.wait(2)
                if dialstatus == "BUSY" then
                        message = "the-party-you-are-calling&is-curntly-busy"
                elseif dialstatus == "CHANUNAVAIL" then
                        message = "cannot-complete-network-error"
                else
                        message = "away-naughty-boy"
                end
                message = message .. "&"
        else
                message = ""
        end

        app.background(message .. "T-to-leave-msg&press-star", "", "", "takemessage")

        if not(channel["EXTEN"]:get() == "*") then app.waitexten() end
end

function hangup()
        app.playback("thank-you-for-calling")
        app.playback("goodbye")
        app.hangup()
end

function voicemail()
        app.voicemail("9100@default")
end

function voicemailmain()
        app.nocdr()
        app.voicemailmain()
end

extensions = {
        -- Context used for message taking
        takemessage = {
                s = takemessage;
                t = takemessage;
                i = takemessage;
                ["*"] = voicemail;
        };

        -- Context used to potentially incomming providers
        incoming = {
                nodephone = nodephoneincoming;
                pennytel = linenotinuse;
        };

        -- Generally unused context - it's here for voicemail really
        default = {
                s = makethefun;
        };

        -- All dialplan for the internal lines
        internal = {
                ["_970X"] = function()
                        app.goto("parkedcalls", exten, "1")
                end;
                ["101"] = voicemailmain;
                ["102"] = time;
                ["000"] = call_emergency;
                ["_91XX"] = call_lan;
                ["_13ZXXX"] = call_localrate;
                ["_1300XXXXXX"] = call_localrate;
                ["_1800XXXXXX"] = call_freecall;
                ["_NXXXXXXX"] = call_local;
                ["_0[2378]XXXXXXXX"] = call_std;
                ["_04XXXXXXXX"] = call_mobile;
                ["_0011."] = call_international;
        };
}