# &adminguide;

## Overview

This module allows executing Lua scripts from config file, implementing the KEMI framework and exporting Lua module 'KSR'.

To read more about KEMI exports and available KSR submodules, see:

- https://kamailio.org/docs/tutorials/devel/kamailio-kemi-framework/

Note: to have the old Lua module 'sr' available, load the 'app_lua_sr' &kamailio; module.

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 something that is executed by default and requires access to SIP message. This is also the context used with KEMI framework.

## 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. Versions 5.2, 5.3 and 5.4 also work.

This module can be compiled against LuaJIT compiler (instead of standard Lua). Then this library is needed:

- libluajit-5.1-dev - LuaJIT devel library.

To enable that, LUAJIT variable has to be set.

#### Build against LuaJIT libraries

E.g: $ LUAJIT="yes" make modules modules=modules/app_lua
(Warning: LuaJIT version is 5.1, so scripts prepared for higher Lua versions may not work with LuaJIT)

## Parameters

### `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. The script can be in Lua-source or bytecode.

Default value is null.

#### Set `load` parameter

    ...
    modparam("app_lua", "load", "/usr/local/etc/kamailio/lua/myscript.lua")
    ...

### `reload` (boolean)

If reload is 1 enables the ability to reload the scripts using the RPC app_lua.reload command. To disable reload feature, set this parameter to 0.

Default value is 1 (on).

#### Set `reload` parameter

    ...
    modparam("app_lua", "reload", 0)
    ...

### `log_mode` (int)

Control what is printed in log messages. If bit 1 is set, then the module prints debug messages for each KEMI export.

Default value is 0.

#### Set `log_mode` parameter

    ...
    modparam("app_lua", "log_mode", 1)
    ...

## Functions

### `lua_dofile(path)`

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

#### `lua_dofile` usage

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

### `lua_dostring(script)`

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

#### `lua_dostring` usage

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

### `lua_run(func [, params])`

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

#### `lua_run` usage

    ...
    if(!lua_run("sr_append_fu_to_reply")) {
        xdbg("SCRIPT: failed to execute lua function!\n");
    }
    ...
    lua_run("lua_func0");
    lua_run("lua_func1", "$rU");
    lua_run("lua_func2", "$rU", "2");
    lua_run("lua_func3", "$rU", "2", "$si");
    ...

### `lua_runstring(script)`

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.

#### `lua_runstring` usage

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

## RPC Commands

### `app_lua.list`

Lists the id and path for every script loaded by the load parameter.

Name: app_lua.list

Parameters: none

Example:

    ...
        &kamctl; rpc app_lua.list
    ...

### `app_lua.reload`

Marks the need to reload the selected script. The actual reload is done by every working process when the next call to lua_run function is executed. If no parameter is added all the scripts are selected to be reloaded.

Name: app_lua.reload

Parameters: id

Example:

    ...
        &kamctl; rpc app_lua.reload 0
    ...

### `app_lua.api_list`

Lists the exported KEMI functions that can be used inside Lua scripts.

Name: app_lua.api_list

Parameters: none

Example:

    ...
        &kamctl; rpc app_lua.api_list
    ...

## 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()
    	KSR.hdr.append_to_reply("P-From: " .. KSR.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");
        }
        ...
    }
    ...