README 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. app_lua Module
  2. Daniel-Constantin Mierla
  3. asipto.com
  4. Edited by
  5. Daniel-Constantin Mierla
  6. <[email protected]>
  7. Copyright © 2010 Daniel-Constantin Mierla (asipto.com)
  8. __________________________________________________________________
  9. Table of Contents
  10. 1. Admin Guide
  11. 1. Overview
  12. 2. Dependencies
  13. 2.1. Kamailio Modules
  14. 2.2. External Libraries or Applications
  15. 3. Parameters
  16. 3.1. load (string)
  17. 3.2. register (string)
  18. 3.3. reload (boolean)
  19. 4. Functions
  20. 4.1. lua_dofile(path)
  21. 4.2. lua_dostring(script)
  22. 4.3. lua_run(function, params)
  23. 4.4. lua_runstring(script)
  24. 5. Exported RPC Commands
  25. 5.1. app_lua.list
  26. 5.2. app_lua.reload
  27. 6. Example of usage
  28. List of Examples
  29. 1.1. Set load parameter
  30. 1.2. Set register parameter
  31. 1.3. Set reload parameter
  32. 1.4. lua_dofile usage
  33. 1.5. lua_dostring usage
  34. 1.6. lua_run usage
  35. 1.7. lua_runstring usage
  36. Chapter 1. Admin Guide
  37. Table of Contents
  38. 1. Overview
  39. 2. Dependencies
  40. 2.1. Kamailio Modules
  41. 2.2. External Libraries or Applications
  42. 3. Parameters
  43. 3.1. load (string)
  44. 3.2. register (string)
  45. 3.3. reload (boolean)
  46. 4. Functions
  47. 4.1. lua_dofile(path)
  48. 4.2. lua_dostring(script)
  49. 4.3. lua_run(function, params)
  50. 4.4. lua_runstring(script)
  51. 5. Exported RPC Commands
  52. 5.1. app_lua.list
  53. 5.2. app_lua.reload
  54. 6. Example of usage
  55. 1. Overview
  56. This module allows executing Lua scripts from config file. It exports a
  57. set of functions to Lua in order to access the current processed SIP
  58. message. These functions are within Lua module 'sr'.
  59. Lua (http://www.lua.org) is a fast and easy to embed scripting
  60. language. Exported API from SIP router to Lua is documented in the
  61. dokuwiki.
  62. The module has two Lua contexts:
  63. * first is used for functions lua_dofile() and lua_dostring().
  64. * second is used for function lua_run() and parameter 'load'.
  65. Therefore lua_run() cannot execute functions from scripts loaded
  66. via lua_dofile() in config. This is kind of caching mode, avoiding
  67. reading file every time, but you must be sure you do not have
  68. someting that is executed by default and requires access to SIP
  69. message.
  70. 2. Dependencies
  71. 2.1. Kamailio Modules
  72. 2.2. External Libraries or Applications
  73. 2.1. Kamailio Modules
  74. The following modules must be loaded before this module:
  75. * none.
  76. 2.2. External Libraries or Applications
  77. The following libraries or applications must be installed before
  78. running Kamailio with this module loaded:
  79. * liblua5.1-dev - Lua devel library.
  80. 3. Parameters
  81. 3.1. load (string)
  82. 3.2. register (string)
  83. 3.3. reload (boolean)
  84. 3.1. load (string)
  85. Set the path to the Lua script to be loaded at startup. Then you can
  86. use lua_run(function, params) to execute a function from the script at
  87. runtime.
  88. Default value is "null".
  89. Example 1.1. Set load parameter
  90. ...
  91. modparam("app_lua", "load", "/usr/local/etc/kamailio/lua/myscript.lua")
  92. ...
  93. 3.2. register (string)
  94. Use this function to register optional SIP Router submodules to Lua.
  95. Available submodules are:
  96. * alias_db - register functions from alias_db module under
  97. 'sr.alias_db'.
  98. * auth - register functions from auth module under 'sr.auth'.
  99. * auth_db - register functions from auth_db module under
  100. 'sr.auth_db'.
  101. * dispatcher - register functions from dispatcher module under
  102. 'sr.dispatcher'.
  103. * maxfwd - register functions from maxfwd module under 'sr.maxfwd'.
  104. * msilo - register functions from msilo module under 'sr.msilo'.
  105. * presence - register functions from presence module under
  106. 'sr.presence'.
  107. * presence_xml - register functions from presence_xml module under
  108. 'sr.presence_xml'.
  109. * pua_usrloc - register functions from pua_usrloc module under
  110. 'sr.pua_usrloc'.
  111. * registrar - register functions from registrar module under
  112. 'sr.registrar'.
  113. * rls - register functions from rls module under 'sr.rls'.
  114. * rr - register functions from rr module under 'sr.rr'.
  115. * sanity - register functions from sanity module under 'sr.sanity'.
  116. * sdpops - register functions from sdpops module under 'sr.sdpops'.
  117. * siputils - register functions from siputils module under
  118. 'sr.siputils'.
  119. * sl - register functions from sl module under 'sr.sl'.
  120. * sqlops - register functions from sqlops module under 'sr.sqlops'.
  121. * textops - register functions from textops module under
  122. 'sr.textops'.
  123. * tm - register functions from tm module under 'sr.tm'.
  124. * xhttp - register functions from xhttp module under 'sr.xhttp'.
  125. Note that 'sr', 'sr.hdr' and 'sr.pv' modules are always registered to
  126. Lua.
  127. Default value is "null".
  128. Example 1.2. Set register parameter
  129. ...
  130. modparam("app_lua", "register", "sl")
  131. ...
  132. 3.3. reload (boolean)
  133. If reload is 1 enables the ability to reload the scripts using the RPC
  134. app_lua.reload command.
  135. Default value is "0 (off)".
  136. Example 1.3. Set reload parameter
  137. ...
  138. modparam("app_lua", "reload", 1)
  139. ...
  140. 4. Functions
  141. 4.1. lua_dofile(path)
  142. 4.2. lua_dostring(script)
  143. 4.3. lua_run(function, params)
  144. 4.4. lua_runstring(script)
  145. 4.1. lua_dofile(path)
  146. Execute the Lua script stored in 'path'. The parameter can be a string
  147. with pseudo-variables evaluated at runtime.
  148. Example 1.4. lua_dofile usage
  149. ...
  150. lua_dofile("/usr/local/etc/kamailio/lua/myscript.lua");
  151. ...
  152. 4.2. lua_dostring(script)
  153. Execute the Lua script stored in parameter. The parameter can be a
  154. string with pseudo-variables.
  155. Example 1.5. lua_dostring usage
  156. ...
  157. if(!lua_dostring("sr.log([[err]], [[----------- Hello World from $fU\n]])"))
  158. {
  159. xdbg("SCRIPT: failed to execute lua script!\n");
  160. }
  161. ...
  162. 4.3. lua_run(function, params)
  163. Execute the Lua function 'func' giving params as parameters. There can
  164. be up to 3 string parameters. The function must exist in the script
  165. loaded at startup via parameter 'load'. Parameters can be strings with
  166. pseudo-variables that are evaluated at runtime.
  167. Example 1.6. lua_run usage
  168. ...
  169. if(!lua_run("sr_append_fu_to_reply"))
  170. {
  171. xdbg("SCRIPT: failed to execute lua function!\n");
  172. }
  173. ...
  174. lua_run("lua_funcx", "$rU", "2");
  175. ...
  176. 4.4. lua_runstring(script)
  177. Execute the Lua script stored in parameter. The parameter can be a
  178. string with pseudo-variables. The script is executed in Lua context
  179. specific to loaded Lua files at startup.
  180. Example 1.7. lua_runstring usage
  181. ...
  182. if(!lua_runstring("sr.log([[err]], [[----------- Hello World from $fU\n]])"))
  183. {
  184. xdbg("SCRIPT: failed to execute lua script!\n");
  185. }
  186. ...
  187. 5. Exported RPC Commands
  188. 5.1. app_lua.list
  189. 5.2. app_lua.reload
  190. 5.1. app_lua.list
  191. Lists the id and path for every script loaded by the load parameter.
  192. Name: app_lua.list
  193. Parameters: none
  194. Example:
  195. kamcmd app_lua.lists
  196. 5.2. app_lua.reload
  197. Marks the need to reload the selected script. The actual reload is done
  198. by every working process when the next call to lua_run function is
  199. executed. If no parameter is added all the scripts are selected to be
  200. reloaded.
  201. Name: app_lua.reload
  202. Parameters: id
  203. Example:
  204. kamcmd app_lua.reload 0
  205. 6. Example of usage
  206. Create your Lua script and stored on file system, say:
  207. '/usr/local/etc/kamailio/lua/myscript.lua'.
  208. ...
  209. function sr_append_fu_to_reply()
  210. sr.hdr.append_to_reply("P-From: " .. sr.pv.get("$fu") .. "\r\n");
  211. end
  212. ...
  213. Load the script via parameter 'load' and execute function via
  214. lua_run(...).
  215. ...
  216. modparam("app_lua", "load", "/usr/local/etc/kamailio/lua/myscript.lua")
  217. ...
  218. route {
  219. ...
  220. if(!lua_run("sr_append_fu_to_reply"))
  221. {
  222. xdbg("SCRIPT: failed to execute lua function!\n");
  223. }
  224. ...
  225. }
  226. ...