[Freeswitch-users] originate to loopback/answer and park a lua script from another lua script

Karsten Horsmann khorsmann at gmail.com
Wed Feb 20 15:03:19 MSK 2013


Hello Michael,

i create this loop.lua to update the sip username spelling (register
is not casesensitiv vs. fs is casesensitiv).

Sure i could start the loop.lua with mod_lua as start script.

With the loopback hack i want to simulate an "job control" and maybe
do some nagios check to be sure
that the loop.lua is alive (simple grep output of "show channels").

Is there a other way to check that the loop.lua is alive from outside?

I play around with an start.lua to originate the loop.lua at startup.
But my start.lua didnt work.
Also an freeswitch.api call from start.lua didnt do the trick. Any hints?

my "start.lua":
-- new_session =
freeswitch.Session("loopback/set:job=1234\,answer\,park/default/inline
lua:loop.lua inline", session)
new_session = freeswitch.Session("loopback/set:job=1234", session)
new_session:answer();
new_session:execute("park/default/inline lua:loop.lua inline")

Got this FS-Log output http://pastebin.freeswitch.org/20595


Kind Regards
Karsten

2013/2/15 Michael Collins <msc at freeswitch.org>:
> Can you share with us a small Lua code sample demonstrating what you are
> trying to do? That might make it easier for us to help.
>
> -MC
>
> On Fri, Feb 15, 2013 at 3:26 AM, Karsten Horsmann <khorsmann at gmail.com>
> wrote:
>>
>> Hello!
>>
>> as described in Anthonys mail
>>
>> http://lists.freeswitch.org/pipermail/freeswitch-users/2013-January/091769.html
>> i want also to originate my loop.lua to an loopback channel.
>>
>> No problem from fs_cli. I want to originate the loop.lua from my
>> mod_lua start lua script.
>>
>> How can i do this? The freeswitch.Session() didnt work as aspected.
>>
>>
>> --
>> Kind Regards
>> *Karsten Horsmann*
>
>
> --
> Michael S Collins
> Twitter: @mercutioviz
> http://www.FreeSWITCH.org
> http://www.ClueCon.com
> http://www.OSTAG.org



-- 
Mit freundlichen Grüßen
*Karsten Horsmann*
-------------- next part --------------
package.path = package.path .. ";/usr/share/lua/5.1/?.lua"
package.cpath = package.cpath .. ";/usr/lib64/lua/5.1/?.so"

-- mysql Config
require ('DBI')
mydb,myuser,mypass,myhost = "yourdb,youruser,yourpass,yourhost"

-- Logging
LOGLEVEL = "info"

-- Progname
PROGNAME = "loop.lua"

-- functions 
function logger(message)
    freeswitch.consoleLog(LOGLEVEL,"["..PROGNAME.."] "..message.."\n")
end

function mysqlabfrage(u)
    if u == nil then
        return
    end

    local dbh = assert(DBI.Connect('MySQL', mydb, myuser, mypass, myhost))
    local sth = assert(dbh:prepare('SELECT username, domain, password, fs_username FROM subscriber WHERE username=? LIMIT 1'))
    sth:execute(u)

    -- print all rows, the rows will be indexed by field names
    row = sth:fetch ({}, "a")

    if not row then
        return
    end

    username = row.username
    fs_username = row.fs_username
    domain = row.domain
    password = row.password

    dbh:close()
    sth:close()

    return username,fs_username,domain,password
end

function mysqlupdate(u)
    if u == nil then
        logger("Execute Update failed - no username given")
        return
    end

    local dbh = assert(DBI.Connect('MySQL', mydb, myuser, mypass, myhost))
    dbh:autocommit(true)
    local wrt = assert(dbh:prepare("UPDATE subscriber SET fs_username=? WHERE username=? LIMIT 1"))
    logger("Execute Update fs_username to [" .. u .. "] !")
    success,err = wrt:execute(u,u)

    if success == false then
       logger("Execute Update ERROR")
    end

    dbh:close()
    wrt:close()
end



-- Send Stop Event on arg[1] == stop
if argv[1] then
    i=1
    while argv[i] do
        if argv[i] == "stop" then
            --Send Stop message
            local event = freeswitch.Event("custom", "lua::stop_event")
            event:addHeader("Action", "stop")
            event:fire()
            logger("Sent stop message to lua script")
            return
        end
        i=i+1
    end
    return
end

-- main loop
logger("Starting script and loop forever")
con = freeswitch.EventConsumer("CUSTOM");

run = true
while run do
    for e in (function() return con:pop(1,1000) end) do
        -- logger("event\n" .. e:serialize("xml"))

        element = e:getHeader("Event-Subclass") or ""

        if (element == "lua::stop_event") then
            action = e:getHeader("Action") or ""
            if (action == "stop") then
                logger("got stop message, Exiting")
                run = false
                break
            end
        end 


        if (element == "sofia::register") then
            registerstatus = e:getHeader("status")
            if (registerstatus:find("Registered",1,true)) then
                req_username = e:getHeader("username")
                if req_username then
                    logger("user registerd " .. req_username)
                    mysqlabfrage(req_username)
                    if req_username ~= fs_username then
                       mysqlupdate(req_username)
                    end
                end
            end
        end

    end
end


Join us at ClueCon 2011 Aug 9-11, 2011
More information about the FreeSWITCH-users mailing list