| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317 |
- <?xml version="1.0" encoding='ISO-8859-1'?>
- <!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.4//EN"
- "http://www.oasis-open.org/docbook/xml/4.4/docbookx.dtd" [
- <!-- Include general documentation entities -->
- <!ENTITY % docentities SYSTEM "../../../docbook/entities.xml">
- %docentities;
- ]>
- <!-- Module User's Guide -->
- <chapter>
-
- <title>&adminguide;</title>
-
- <section>
- <title>Overview</title>
- <para>
- 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'.
- </para>
- <para>
- 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.
- </para>
- <para>
- The module has two Lua contexts:
- <itemizedlist>
- <listitem>
- <para>
- <emphasis>first</emphasis> is used for functions lua_dofile()
- and lua_dostring().
- </para>
- </listitem>
- <listitem>
- <para>
- <emphasis>second</emphasis> 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.
- </para>
- </listitem>
- </itemizedlist>
- </para>
- </section>
- <section>
- <title>Dependencies</title>
- <section>
- <title>&kamailio; Modules</title>
- <para>
- The following modules must be loaded before this module:
- <itemizedlist>
- <listitem>
- <para>
- <emphasis>none</emphasis>.
- </para>
- </listitem>
- </itemizedlist>
- </para>
- </section>
- <section>
- <title>External Libraries or Applications</title>
- <para>
- The following libraries or applications must be installed before running
- &kamailio; with this module loaded:
- <itemizedlist>
- <listitem>
- <para>
- <emphasis>liblua5.1-dev</emphasis> - Lua devel library.
- </para>
- </listitem>
- </itemizedlist>
- </para>
- </section>
- </section>
- <section>
- <title>Exported Parameters</title>
- <section>
- <title><varname>load</varname> (string)</title>
- <para>
- 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.
- </para>
- <para>
- <emphasis>
- Default value is <quote>null</quote>.
- </emphasis>
- </para>
- <example>
- <title>Set <varname>load</varname> parameter</title>
- <programlisting format="linespecific">
- ...
- modparam("app_lua", "load", "/usr/local/etc/kamailio/lua/myscript.lua")
- ...
- </programlisting>
- </example>
- </section>
- <section>
- <title><varname>register</varname> (string)</title>
- <para>
- Use this function to register optional SIP Router submodules
- to Lua. Available submodules are:
- </para>
- <itemizedlist>
- <listitem>
- <para>
- <emphasis>auth</emphasis> - register functions from auth module
- under 'sr.auth'.
- </para>
- </listitem>
- <listitem>
- <para>
- <emphasis>auth_db</emphasis> - register functions from auth_db
- module under 'sr.auth_db'.
- </para>
- </listitem>
- <listitem>
- <para>
- <emphasis>dispatcher</emphasis> - register functions from
- dispatcher module under 'sr.dispatcher'.
- </para>
- </listitem>
- <listitem>
- <para>
- <emphasis>maxfwd</emphasis> - register functions from maxfwd
- module under 'sr.maxfwd'.
- </para>
- </listitem>
- <listitem>
- <para>
- <emphasis>registrar</emphasis> - register functions from
- registrar module under 'sr.registrar'.
- </para>
- </listitem>
- <listitem>
- <para>
- <emphasis>rr</emphasis> - register functions from rr module
- under 'sr.rr'.
- </para>
- </listitem>
- <listitem>
- <para>
- <emphasis>sqlops</emphasis> - register functions from sqlops
- module under 'sr.sqlops'.
- </para>
- </listitem>
- <listitem>
- <para>
- <emphasis>sl</emphasis> - register functions from sl module
- under 'sr.sl'.
- </para>
- </listitem>
- <listitem>
- <para>
- <emphasis>tm</emphasis> - register functions from tm module
- under 'sr.tm'.
- </para>
- </listitem>
- <listitem>
- <para>
- <emphasis>xhttp</emphasis> - register functions from xhttp
- module under 'sr.xhttp'.
- </para>
- </listitem>
- </itemizedlist>
- <para>
- Note that 'sr', 'sr.hdr' and 'sr.pv' modules are always registered
- to Lua.
- </para>
- <para>
- <emphasis>
- Default value is <quote>null</quote>.
- </emphasis>
- </para>
- <example>
- <title>Set <varname>register</varname> parameter</title>
- <programlisting format="linespecific">
- ...
- modparam("app_lua", "register", "sl")
- ...
- </programlisting>
- </example>
- </section>
- </section>
-
- <section>
- <title>Exported Functions</title>
- <section>
- <title>
- <function moreinfo="none">lua_dofile(path)</function>
- </title>
- <para>
- Execute the Lua script stored in 'path'. The parameter can be
- a string with pseudo-variables evaluated at runtime.
- </para>
- <example>
- <title><function>lua_dofile</function> usage</title>
- <programlisting format="linespecific">
- ...
- lua_dofile("/usr/local/etc/kamailio/lua/myscript.lua");
- ...
- </programlisting>
- </example>
- </section>
-
- <section>
- <title>
- <function moreinfo="none">lua_dostring(script)</function>
- </title>
- <para>i
- Execute the Lua script stored in parameter. The parameter can be
- a string with pseudo-variables.
- </para>
- <example>
- <title><function>lua_dostring</function> usage</title>
- <programlisting format="linespecific">
- ...
- if(!lua_dostring("sr.log([[err]], [[----------- Hello World from $fU\n]])"))
- {
- xdbg("SCRIPT: failed to execute lua script!\n");
- }
- ...
- </programlisting>
- </example>
- </section>
- <section>
- <title>
- <function moreinfo="none">lua_run(function, params)</function>
- </title>
- <para>
- 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.
- </para>
- <example>
- <title><function>lua_run</function> usage</title>
- <programlisting format="linespecific">
- ...
- if(!lua_run("sr_append_fu_to_reply"))
- {
- xdbg("SCRIPT: failed to execute lua function!\n");
- }
- ...
- lua_run("lua_funcx", "$rU", "2");
- ...
- </programlisting>
- </example>
- </section>
- <section>
- <title>
- <function moreinfo="none">lua_runstring(script)</function>
- </title>
- <para>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.
- </para>
- <example>
- <title><function>lua_runstring</function> usage</title>
- <programlisting format="linespecific">
- ...
- if(!lua_runstring("sr.log([[err]], [[----------- Hello World from $fU\n]])"))
- {
- xdbg("SCRIPT: failed to execute lua script!\n");
- }
- ...
- </programlisting>
- </example>
- </section>
-
- </section>
-
- <section>
- <title>Example of usage</title>
- <para>
- Create your Lua script and stored on file system,
- say: '/usr/local/etc/kamailio/lua/myscript.lua'.
- </para>
- <programlisting format="linespecific">
- ...
- function sr_append_fu_to_reply()
- sr.hdr.append_to_reply("P-From: " .. sr.pv.get("$fu") .. "\r\n");
- end
- ...
- </programlisting>
- <para>
- Load the script via parameter 'load' and execute function
- via lua_run(...).
- </para>
- <programlisting format="linespecific">
- ...
- 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");
- }
- ...
- }
- ...
- </programlisting>
- </section>
- </chapter>
|