app_lua_admin.xml 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. <?xml version="1.0" encoding='ISO-8859-1'?>
  2. <!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.4//EN"
  3. "http://www.oasis-open.org/docbook/xml/4.4/docbookx.dtd" [
  4. <!-- Include general documentation entities -->
  5. <!ENTITY % docentities SYSTEM "../../../docbook/entities.xml">
  6. %docentities;
  7. ]>
  8. <!-- Module User's Guide -->
  9. <chapter>
  10. <title>&adminguide;</title>
  11. <section>
  12. <title>Overview</title>
  13. <para>
  14. This module allows executing Lua scripts from config file. It exports
  15. a set of functions to Lua in order to access the current processed
  16. SIP message. These functions are within Lua module 'sr'.
  17. </para>
  18. <para>
  19. Lua (http://www.lua.org) is a fast and easy to embed scripting
  20. language. Exported API from SIP router to Lua is documented in the
  21. dokuwiki.
  22. </para>
  23. <para>
  24. The module has two Lua contexts:
  25. <itemizedlist>
  26. <listitem>
  27. <para>
  28. <emphasis>first</emphasis> is used for functions lua_dofile()
  29. and lua_dostring().
  30. </para>
  31. </listitem>
  32. <listitem>
  33. <para>
  34. <emphasis>second</emphasis> is used for function lua_run()
  35. and parameter 'load'. Therefore lua_run() cannot execute functions
  36. from scripts loaded via lua_dofile() in config. This is kind of
  37. caching mode, avoiding reading file every time, but you must be sure
  38. you do not have someting that is executed by default and requires
  39. access to SIP message.
  40. </para>
  41. </listitem>
  42. </itemizedlist>
  43. </para>
  44. </section>
  45. <section>
  46. <title>Dependencies</title>
  47. <section>
  48. <title>&kamailio; Modules</title>
  49. <para>
  50. The following modules must be loaded before this module:
  51. <itemizedlist>
  52. <listitem>
  53. <para>
  54. <emphasis>none</emphasis>.
  55. </para>
  56. </listitem>
  57. </itemizedlist>
  58. </para>
  59. </section>
  60. <section>
  61. <title>External Libraries or Applications</title>
  62. <para>
  63. The following libraries or applications must be installed before running
  64. &kamailio; with this module loaded:
  65. <itemizedlist>
  66. <listitem>
  67. <para>
  68. <emphasis>liblua5.1-dev</emphasis> - Lua devel library.
  69. </para>
  70. </listitem>
  71. </itemizedlist>
  72. </para>
  73. </section>
  74. </section>
  75. <section>
  76. <title>Exported Parameters</title>
  77. <section>
  78. <title><varname>load</varname> (string)</title>
  79. <para>
  80. Set the path to the Lua script to be loaded at startup. Then you
  81. can use lua_run(function, params) to execute a function from the
  82. script at runtime.
  83. </para>
  84. <para>
  85. <emphasis>
  86. Default value is <quote>null</quote>.
  87. </emphasis>
  88. </para>
  89. <example>
  90. <title>Set <varname>load</varname> parameter</title>
  91. <programlisting format="linespecific">
  92. ...
  93. modparam("app_lua", "load", "/usr/local/etc/kamailio/lua/myscript.lua")
  94. ...
  95. </programlisting>
  96. </example>
  97. </section>
  98. <section>
  99. <title><varname>register</varname> (string)</title>
  100. <para>
  101. Use this function to register optional SIP Router submodules
  102. to Lua. Available submodules are:
  103. </para>
  104. <itemizedlist>
  105. <listitem>
  106. <para>
  107. <emphasis>auth</emphasis> - register functions from auth module
  108. under 'sr.auth'.
  109. </para>
  110. </listitem>
  111. <listitem>
  112. <para>
  113. <emphasis>auth_db</emphasis> - register functions from auth_db
  114. module under 'sr.auth_db'.
  115. </para>
  116. </listitem>
  117. <listitem>
  118. <para>
  119. <emphasis>dispatcher</emphasis> - register functions from
  120. dispatcher module under 'sr.dispatcher'.
  121. </para>
  122. </listitem>
  123. <listitem>
  124. <para>
  125. <emphasis>maxfwd</emphasis> - register functions from maxfwd
  126. module under 'sr.maxfwd'.
  127. </para>
  128. </listitem>
  129. <listitem>
  130. <para>
  131. <emphasis>registrar</emphasis> - register functions from
  132. registrar module under 'sr.registrar'.
  133. </para>
  134. </listitem>
  135. <listitem>
  136. <para>
  137. <emphasis>rr</emphasis> - register functions from rr module
  138. under 'sr.rr'.
  139. </para>
  140. </listitem>
  141. <listitem>
  142. <para>
  143. <emphasis>sqlops</emphasis> - register functions from sqlops
  144. module under 'sr.sqlops'.
  145. </para>
  146. </listitem>
  147. <listitem>
  148. <para>
  149. <emphasis>sl</emphasis> - register functions from sl module
  150. under 'sr.sl'.
  151. </para>
  152. </listitem>
  153. <listitem>
  154. <para>
  155. <emphasis>tm</emphasis> - register functions from tm module
  156. under 'sr.tm'.
  157. </para>
  158. </listitem>
  159. <listitem>
  160. <para>
  161. <emphasis>xhttp</emphasis> - register functions from xhttp
  162. module under 'sr.xhttp'.
  163. </para>
  164. </listitem>
  165. </itemizedlist>
  166. <para>
  167. Note that 'sr', 'sr.hdr' and 'sr.pv' modules are always registered
  168. to Lua.
  169. </para>
  170. <para>
  171. <emphasis>
  172. Default value is <quote>null</quote>.
  173. </emphasis>
  174. </para>
  175. <example>
  176. <title>Set <varname>register</varname> parameter</title>
  177. <programlisting format="linespecific">
  178. ...
  179. modparam("app_lua", "register", "sl")
  180. ...
  181. </programlisting>
  182. </example>
  183. </section>
  184. </section>
  185. <section>
  186. <title>Exported Functions</title>
  187. <section>
  188. <title>
  189. <function moreinfo="none">lua_dofile(path)</function>
  190. </title>
  191. <para>
  192. Execute the Lua script stored in 'path'. The parameter can be
  193. a string with pseudo-variables evaluated at runtime.
  194. </para>
  195. <example>
  196. <title><function>lua_dofile</function> usage</title>
  197. <programlisting format="linespecific">
  198. ...
  199. lua_dofile("/usr/local/etc/kamailio/lua/myscript.lua");
  200. ...
  201. </programlisting>
  202. </example>
  203. </section>
  204. <section>
  205. <title>
  206. <function moreinfo="none">lua_dostring(script)</function>
  207. </title>
  208. <para>i
  209. Execute the Lua script stored in parameter. The parameter can be
  210. a string with pseudo-variables.
  211. </para>
  212. <example>
  213. <title><function>lua_dostring</function> usage</title>
  214. <programlisting format="linespecific">
  215. ...
  216. if(!lua_dostring("sr.log([[err]], [[----------- Hello World from $fU\n]])"))
  217. {
  218. xdbg("SCRIPT: failed to execute lua script!\n");
  219. }
  220. ...
  221. </programlisting>
  222. </example>
  223. </section>
  224. <section>
  225. <title>
  226. <function moreinfo="none">lua_run(function, params)</function>
  227. </title>
  228. <para>
  229. Execute the Lua function 'func' giving params as parameters. There
  230. can be up to 3 string parameters. The function must exist in the
  231. script loaded at startup via parameter 'load'. Parameters can be
  232. strings with pseudo-variables that are evaluated at runtime.
  233. </para>
  234. <example>
  235. <title><function>lua_run</function> usage</title>
  236. <programlisting format="linespecific">
  237. ...
  238. if(!lua_run("sr_append_fu_to_reply"))
  239. {
  240. xdbg("SCRIPT: failed to execute lua function!\n");
  241. }
  242. ...
  243. lua_run("lua_funcx", "$rU", "2");
  244. ...
  245. </programlisting>
  246. </example>
  247. </section>
  248. <section>
  249. <title>
  250. <function moreinfo="none">lua_runstring(script)</function>
  251. </title>
  252. <para>i
  253. Execute the Lua script stored in parameter. The parameter can be
  254. a string with pseudo-variables. The script is executed in Lua context
  255. specific to loaded Lua files at startup.
  256. </para>
  257. <example>
  258. <title><function>lua_runstring</function> usage</title>
  259. <programlisting format="linespecific">
  260. ...
  261. if(!lua_runstring("sr.log([[err]], [[----------- Hello World from $fU\n]])"))
  262. {
  263. xdbg("SCRIPT: failed to execute lua script!\n");
  264. }
  265. ...
  266. </programlisting>
  267. </example>
  268. </section>
  269. </section>
  270. <section>
  271. <title>Example of usage</title>
  272. <para>
  273. Create your Lua script and stored on file system,
  274. say: '/usr/local/etc/kamailio/lua/myscript.lua'.
  275. </para>
  276. <programlisting format="linespecific">
  277. ...
  278. function sr_append_fu_to_reply()
  279. sr.hdr.append_to_reply("P-From: " .. sr.pv.get("$fu") .. "\r\n");
  280. end
  281. ...
  282. </programlisting>
  283. <para>
  284. Load the script via parameter 'load' and execute function
  285. via lua_run(...).
  286. </para>
  287. <programlisting format="linespecific">
  288. ...
  289. modparam("app_lua", "load", "/usr/local/etc/kamailio/lua/myscript.lua")
  290. ...
  291. route {
  292. ...
  293. if(!lua_run("sr_append_fu_to_reply"))
  294. {
  295. xdbg("SCRIPT: failed to execute lua function!\n");
  296. }
  297. ...
  298. }
  299. ...
  300. </programlisting>
  301. </section>
  302. </chapter>