app_lua_admin.xml 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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>Parameters</title>
  77. <section id="app_lua.p.load">
  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 id="app_lua.p.register">
  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>alias_db</emphasis> - register functions from
  108. alias_db module under 'sr.alias_db'.
  109. </para>
  110. </listitem>
  111. <listitem>
  112. <para>
  113. <emphasis>auth</emphasis> - register functions from auth module
  114. under 'sr.auth'.
  115. </para>
  116. </listitem>
  117. <listitem>
  118. <para>
  119. <emphasis>auth_db</emphasis> - register functions from auth_db
  120. module under 'sr.auth_db'.
  121. </para>
  122. </listitem>
  123. <listitem>
  124. <para>
  125. <emphasis>dispatcher</emphasis> - register functions from
  126. dispatcher module under 'sr.dispatcher'.
  127. </para>
  128. </listitem>
  129. <listitem>
  130. <para>
  131. <emphasis>maxfwd</emphasis> - register functions from maxfwd
  132. module under 'sr.maxfwd'.
  133. </para>
  134. </listitem>
  135. <listitem>
  136. <para>
  137. <emphasis>msilo</emphasis> - register functions from
  138. msilo module under 'sr.msilo'.
  139. </para>
  140. </listitem>
  141. <listitem>
  142. <para>
  143. <emphasis>presence</emphasis> - register functions from
  144. presence module under 'sr.presence'.
  145. </para>
  146. </listitem>
  147. <listitem>
  148. <para>
  149. <emphasis>presence_xml</emphasis> - register functions from
  150. presence_xml module under 'sr.presence_xml'.
  151. </para>
  152. </listitem>
  153. <listitem>
  154. <para>
  155. <emphasis>pua_usrloc</emphasis> - register functions from
  156. pua_usrloc module under 'sr.pua_usrloc'.
  157. </para>
  158. </listitem>
  159. <listitem>
  160. <para>
  161. <emphasis>registrar</emphasis> - register functions from
  162. registrar module under 'sr.registrar'.
  163. </para>
  164. </listitem>
  165. <listitem>
  166. <para>
  167. <emphasis>rls</emphasis> - register functions from
  168. rls module under 'sr.rls'.
  169. </para>
  170. </listitem>
  171. <listitem>
  172. <para>
  173. <emphasis>rr</emphasis> - register functions from rr module
  174. under 'sr.rr'.
  175. </para>
  176. </listitem>
  177. <listitem>
  178. <para>
  179. <emphasis>sanity</emphasis> - register functions from sanity
  180. module under 'sr.sanity'.
  181. </para>
  182. </listitem>
  183. <listitem>
  184. <para>
  185. <emphasis>sdpops</emphasis> - register functions from
  186. sdpops module under 'sr.sdpops'.
  187. </para>
  188. </listitem>
  189. <listitem>
  190. <para>
  191. <emphasis>siputils</emphasis> - register functions from
  192. siputils module under 'sr.siputils'.
  193. </para>
  194. </listitem>
  195. <listitem>
  196. <para>
  197. <emphasis>sl</emphasis> - register functions from sl module
  198. under 'sr.sl'.
  199. </para>
  200. </listitem>
  201. <listitem>
  202. <para>
  203. <emphasis>sqlops</emphasis> - register functions from sqlops
  204. module under 'sr.sqlops'.
  205. </para>
  206. </listitem>
  207. <listitem>
  208. <para>
  209. <emphasis>textops</emphasis> - register functions from
  210. textops module under 'sr.textops'.
  211. </para>
  212. </listitem>
  213. <listitem>
  214. <para>
  215. <emphasis>tm</emphasis> - register functions from tm module
  216. under 'sr.tm'.
  217. </para>
  218. </listitem>
  219. <listitem>
  220. <para>
  221. <emphasis>xhttp</emphasis> - register functions from xhttp
  222. module under 'sr.xhttp'.
  223. </para>
  224. </listitem>
  225. </itemizedlist>
  226. <para>
  227. Note that 'sr', 'sr.hdr' and 'sr.pv' modules are always registered
  228. to Lua.
  229. </para>
  230. <para>
  231. <emphasis>
  232. Default value is <quote>null</quote>.
  233. </emphasis>
  234. </para>
  235. <example>
  236. <title>Set <varname>register</varname> parameter</title>
  237. <programlisting format="linespecific">
  238. ...
  239. modparam("app_lua", "register", "sl")
  240. ...
  241. </programlisting>
  242. </example>
  243. </section>
  244. <section id="app_lua.p.reload">
  245. <title><varname>reload</varname> (boolean)</title>
  246. <para>
  247. If reload is 1 enables the ability to reload the
  248. scripts using the RPC app_lua.reload command.
  249. </para>
  250. <para>
  251. <emphasis>
  252. Default value is <quote>0 (off)</quote>.
  253. </emphasis>
  254. </para>
  255. <example>
  256. <title>Set <varname>reload</varname> parameter</title>
  257. <programlisting format="linespecific">
  258. ...
  259. modparam("app_lua", "reload", 1)
  260. ...
  261. </programlisting>
  262. </example>
  263. </section>
  264. </section>
  265. <section>
  266. <title>Functions</title>
  267. <section id="app_lua.f.lua_dotfile">
  268. <title>
  269. <function moreinfo="none">lua_dofile(path)</function>
  270. </title>
  271. <para>
  272. Execute the Lua script stored in 'path'. The parameter can be
  273. a string with pseudo-variables evaluated at runtime.
  274. </para>
  275. <example>
  276. <title><function>lua_dofile</function> usage</title>
  277. <programlisting format="linespecific">
  278. ...
  279. lua_dofile("/usr/local/etc/kamailio/lua/myscript.lua");
  280. ...
  281. </programlisting>
  282. </example>
  283. </section>
  284. <section id="app_lua.f.lua_dostring">
  285. <title>
  286. <function moreinfo="none">lua_dostring(script)</function>
  287. </title>
  288. <para>
  289. Execute the Lua script stored in parameter. The parameter can be
  290. a string with pseudo-variables.
  291. </para>
  292. <example>
  293. <title><function>lua_dostring</function> usage</title>
  294. <programlisting format="linespecific">
  295. ...
  296. if(!lua_dostring("sr.log([[err]], [[----------- Hello World from $fU\n]])"))
  297. {
  298. xdbg("SCRIPT: failed to execute lua script!\n");
  299. }
  300. ...
  301. </programlisting>
  302. </example>
  303. </section>
  304. <section id="app_lua.f.lua_run">
  305. <title>
  306. <function moreinfo="none">lua_run(function, params)</function>
  307. </title>
  308. <para>
  309. Execute the Lua function 'func' giving params as parameters. There
  310. can be up to 3 string parameters. The function must exist in the
  311. script loaded at startup via parameter 'load'. Parameters can be
  312. strings with pseudo-variables that are evaluated at runtime.
  313. </para>
  314. <example>
  315. <title><function>lua_run</function> usage</title>
  316. <programlisting format="linespecific">
  317. ...
  318. if(!lua_run("sr_append_fu_to_reply"))
  319. {
  320. xdbg("SCRIPT: failed to execute lua function!\n");
  321. }
  322. ...
  323. lua_run("lua_funcx", "$rU", "2");
  324. ...
  325. </programlisting>
  326. </example>
  327. </section>
  328. <section id="app_lua.f.lua_runstring">
  329. <title>
  330. <function moreinfo="none">lua_runstring(script)</function>
  331. </title>
  332. <para>
  333. Execute the Lua script stored in parameter. The parameter can be
  334. a string with pseudo-variables. The script is executed in Lua context
  335. specific to loaded Lua files at startup.
  336. </para>
  337. <example>
  338. <title><function>lua_runstring</function> usage</title>
  339. <programlisting format="linespecific">
  340. ...
  341. if(!lua_runstring("sr.log([[err]], [[----------- Hello World from $fU\n]])"))
  342. {
  343. xdbg("SCRIPT: failed to execute lua script!\n");
  344. }
  345. ...
  346. </programlisting>
  347. </example>
  348. </section>
  349. </section>
  350. <section>
  351. <title>Exported RPC Commands</title>
  352. <section id="app_lua.r.list">
  353. <title>
  354. <function moreinfo="none">app_lua.list</function>
  355. </title>
  356. <para>
  357. Lists the id and path for every script loaded by
  358. the load parameter.
  359. </para>
  360. <para>
  361. Name: <emphasis>app_lua.list</emphasis>
  362. </para>
  363. <para>Parameters: <emphasis>none</emphasis></para>
  364. <para>
  365. Example:
  366. </para>
  367. <programlisting format="linespecific">
  368. &sercmd; app_lua.lists
  369. </programlisting>
  370. </section>
  371. <section id="app_lua.r.reload">
  372. <title>
  373. <function moreinfo="none">app_lua.reload</function>
  374. </title>
  375. <para>
  376. Marks the need to reload the selected script.
  377. The actual reload is done by every working process when the next
  378. call to lua_run function is executed.
  379. If no parameter is added all the scripts are selected to be reloaded.
  380. </para>
  381. <para>
  382. Name: <emphasis>app_lua.reload</emphasis>
  383. </para>
  384. <para>Parameters: <emphasis>id</emphasis></para>
  385. <para>
  386. Example:
  387. </para>
  388. <programlisting format="linespecific">
  389. &sercmd; app_lua.reload 0
  390. </programlisting>
  391. </section>
  392. </section>
  393. <section>
  394. <title>Example of usage</title>
  395. <para>
  396. Create your Lua script and stored on file system,
  397. say: '/usr/local/etc/kamailio/lua/myscript.lua'.
  398. </para>
  399. <programlisting format="linespecific">
  400. ...
  401. function sr_append_fu_to_reply()
  402. sr.hdr.append_to_reply("P-From: " .. sr.pv.get("$fu") .. "\r\n");
  403. end
  404. ...
  405. </programlisting>
  406. <para>
  407. Load the script via parameter 'load' and execute function
  408. via lua_run(...).
  409. </para>
  410. <programlisting format="linespecific">
  411. ...
  412. modparam("app_lua", "load", "/usr/local/etc/kamailio/lua/myscript.lua")
  413. ...
  414. route {
  415. ...
  416. if(!lua_run("sr_append_fu_to_reply"))
  417. {
  418. xdbg("SCRIPT: failed to execute lua function!\n");
  419. }
  420. ...
  421. }
  422. ...
  423. </programlisting>
  424. </section>
  425. </chapter>