%docentities; ]> &adminguide;
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.
Dependencies
&kamailio; Modules The following modules must be loaded before this module: none.
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.
Exported Parameters
<varname>load</varname> (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. Set <varname>load</varname> parameter ... modparam("lua", "load", "/usr/local/etc/kamailio/lua/myscript.lua") ...
<varname>register</varname> (string) Use this function to register optional SIP Router submodules to Lua. Available submodules are: sl - register functions from sl module under 'sr.sl'. Note that 'sr', 'sr.hdr' and 'sr.pv' modules are always registered to Lua. Default value is null. Set <varname>register</varname> parameter ... modparam("lua", "register", "sr") ...
Exported Functions
<function moreinfo="none">lua_dofile(path)</function> Execute the Lua script stored in 'path'. The parameter can be a string with pseudo-variables evaluated at runtime. <function>lua_dofile</function> usage ... lua_dofile("/usr/local/etc/kamailio/lua/myscript.lua"); ...
<function moreinfo="none">lua_dostring(script)</function> i Execute the Lua script stored in parameter. The parameter can be a string with pseudo-variables. <function>lua_dostring</function> usage ... if(!lua_dostring("sr.log([[err]], [[----------- Hello World from $fU\n]])")) { xdbg("SCRIPT: failed to execute lua script!\n"); } ...
<function moreinfo="none">lua_run(function, params)</function> 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. <function>lua_run</function> usage ... if(!lua_run("sr_append_fu_to_reply")) { xdbg("SCRIPT: failed to execute lua function!\n"); } ... lua_run("lua_funcx", "$rU", "2"); ...
<function moreinfo="none">lua_runstring(script)</function> 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. <function>lua_runstring</function> usage ... if(!lua_runstring("sr.log([[err]], [[----------- Hello World from $fU\n]])")) { xdbg("SCRIPT: failed to execute lua script!\n"); } ...
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"); } ... } ...