Daniel-Constantin Mierla f3cc40a1e3 app_lua: updated the docs 15 years ago
..
doc f3cc40a1e3 app_lua: updated the docs 15 years ago
Makefile 2489d57d6a app_lua: fix finding lua libs 15 years ago
README f3cc40a1e3 app_lua: updated the docs 15 years ago
app_lua_api.c b457031210 app_lua: helper function to return error code 15 years ago
app_lua_api.h b457031210 app_lua: helper function to return error code 15 years ago
app_lua_exp.c 2bcf113321 app_lua: include xhttp api headers 15 years ago
app_lua_exp.h 482db59aa2 app_lua: added sl module to Lua 16 years ago
app_lua_mod.c d098c129dc app_lua: do basic Lua probing before fork 16 years ago
app_lua_sr.c 0026a3b553 app_lua: added several core functions to sr package 15 years ago
app_lua_sr.h 482db59aa2 app_lua: added sl module to Lua 16 years ago

README

app_lua Module

Daniel-Constantin Mierla

asipto.com

Edited by

Daniel-Constantin Mierla



Copyright © 2010 Daniel-Constantin Mierla (asipto.com)
__________________________________________________________________

Table of Contents

1. Admin Guide

1. Overview
2. Dependencies

2.1. Kamailio Modules
2.2. External Libraries or Applications

3. Exported Parameters

3.1. load (string)
3.2. register (string)

4. Exported Functions

4.1. lua_dofile(path)
4.2. lua_dostring(script)
4.3. lua_run(function, params)
4.4. lua_runstring(script)

5. Example of usage

List of Examples

1.1. Set load parameter
1.2. Set register parameter
1.3. lua_dofile usage
1.4. lua_dostring usage
1.5. lua_run usage
1.6. lua_runstring usage

Chapter 1. Admin Guide

Table of Contents

1. Overview
2. Dependencies

2.1. Kamailio Modules
2.2. External Libraries or Applications

3. Exported Parameters

3.1. load (string)
3.2. register (string)

4. Exported Functions

4.1. lua_dofile(path)
4.2. lua_dostring(script)
4.3. lua_run(function, params)
4.4. lua_runstring(script)

5. Example of usage

1. Overview

This module allows executing Lua scripts from config file. It exports a
set of functions to Lua in order to access the current processed SIP
message. These functions are within Lua module 'sr'.

Lua (http://www.lua.org) is a fast and easy to embed scripting
language. Exported API from SIP router to Lua is documented in the
dokuwiki.

The module has two Lua contexts:
* first is used for functions lua_dofile() and lua_dostring().
* second is used for function lua_run() and parameter 'load'.
Therefore lua_run() cannot execute functions from scripts loaded
via lua_dofile() in config. This is kind of caching mode, avoiding
reading file every time, but you must be sure you do not have
someting that is executed by default and requires access to SIP
message.

2. Dependencies

2.1. Kamailio Modules
2.2. External Libraries or Applications

2.1. Kamailio Modules

The following modules must be loaded before this module:
* none.

2.2. External Libraries or Applications

The following libraries or applications must be installed before
running Kamailio with this module loaded:
* liblua5.1-dev - Lua devel library.

3. Exported Parameters

3.1. load (string)
3.2. register (string)

3.1. load (string)

Set the path to the Lua script to be loaded at startup. Then you can
use lua_run(function, params) to execute a function from the script at
runtime.

Default value is “null”.

Example 1.1. Set load parameter
...
modparam("app_lua", "load", "/usr/local/etc/kamailio/lua/myscript.lua")
...

3.2. register (string)

Use this function to register optional SIP Router submodules to Lua.
Available submodules are:
* auth - register functions from auth module under 'sr.auth'.
* auth_db - register functions from auth_db module under
'sr.auth_db'.
* dispatcher - register functions from dispatcher module under
'sr.dispatcher'.
* maxfwd - register functions from maxfwd module under 'sr.maxfwd'.
* registrar - register functions from registrar module under
'sr.registrar'.
* rr - register functions from rr module under 'sr.rr'.
* sqlops - register functions from sqlops module under 'sr.sqlops'.
* sl - register functions from sl module under 'sr.sl'.
* tm - register functions from tm module under 'sr.tm'.
* xhttp - register functions from xhttp module under 'sr.xhttp'.

Note that 'sr', 'sr.hdr' and 'sr.pv' modules are always registered to
Lua.

Default value is “null”.

Example 1.2. Set register parameter
...
modparam("app_lua", "register", "sl")
...

4. Exported Functions

4.1. lua_dofile(path)
4.2. lua_dostring(script)
4.3. lua_run(function, params)
4.4. lua_runstring(script)

4.1. lua_dofile(path)

Execute the Lua script stored in 'path'. The parameter can be a string
with pseudo-variables evaluated at runtime.

Example 1.3. lua_dofile usage
...
lua_dofile("/usr/local/etc/kamailio/lua/myscript.lua");
...

4.2. lua_dostring(script)

i Execute the Lua script stored in parameter. The parameter can be a
string with pseudo-variables.

Example 1.4. lua_dostring usage
...
if(!lua_dostring("sr.log([[err]], [[----------- Hello World from $fU\n]])"))
{
xdbg("SCRIPT: failed to execute lua script!\n");
}
...

4.3. lua_run(function, params)

Execute the Lua function 'func' giving params as parameters. There can
be up to 3 string parameters. The function must exist in the script
loaded at startup via parameter 'load'. Parameters can be strings with
pseudo-variables that are evaluated at runtime.

Example 1.5. lua_run usage
...
if(!lua_run("sr_append_fu_to_reply"))
{
xdbg("SCRIPT: failed to execute lua function!\n");
}
...
lua_run("lua_funcx", "$rU", "2");
...

4.4. lua_runstring(script)

i Execute the Lua script stored in parameter. The parameter can be a
string with pseudo-variables. The script is executed in Lua context
specific to loaded Lua files at startup.

Example 1.6. lua_runstring usage
...
if(!lua_runstring("sr.log([[err]], [[----------- Hello World from $fU\n]])"))
{
xdbg("SCRIPT: failed to execute lua script!\n");
}
...

5. Example of usage

Create your Lua script and stored on file system, say:
'/usr/local/etc/kamailio/lua/myscript.lua'.
...
function sr_append_fu_to_reply()
sr.hdr.append_to_reply("P-From: " .. sr.pv.get("$fu") .. "\r\n");
end
...

Load the script via parameter 'load' and execute function via
lua_run(...).
...
modparam("app_lua", "load", "/usr/local/etc/kamailio/lua/myscript.lua")
...
route {
...
if(!lua_run("sr_append_fu_to_reply"))
{
xdbg("SCRIPT: failed to execute lua function!\n");
}
...
}
...