README 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. Exported Parameters
  16. 3.1. load (string)
  17. 3.2. register (string)
  18. 4. Exported Functions
  19. 4.1. lua_dofile(path)
  20. 4.2. lua_dostring(script)
  21. 4.3. lua_run(function, params)
  22. 4.4. lua_runstring(script)
  23. 5. Example of usage
  24. List of Examples
  25. 1.1. Set load parameter
  26. 1.2. Set register parameter
  27. 1.3. lua_dofile usage
  28. 1.4. lua_dostring usage
  29. 1.5. lua_run usage
  30. 1.6. lua_runstring usage
  31. Chapter 1. Admin Guide
  32. Table of Contents
  33. 1. Overview
  34. 2. Dependencies
  35. 2.1. Kamailio Modules
  36. 2.2. External Libraries or Applications
  37. 3. Exported Parameters
  38. 3.1. load (string)
  39. 3.2. register (string)
  40. 4. Exported Functions
  41. 4.1. lua_dofile(path)
  42. 4.2. lua_dostring(script)
  43. 4.3. lua_run(function, params)
  44. 4.4. lua_runstring(script)
  45. 5. Example of usage
  46. 1. Overview
  47. This module allows executing Lua scripts from config file. It exports a
  48. set of functions to Lua in order to access the current processed SIP
  49. message. These functions are within Lua module 'sr'.
  50. Lua (http://www.lua.org) is a fast and easy to embed scripting
  51. language. Exported API from SIP router to Lua is documented in the
  52. dokuwiki.
  53. The module has two Lua contexts:
  54. * first is used for functions lua_dofile() and lua_dostring().
  55. * second is used for function lua_run() and parameter 'load'.
  56. Therefore lua_run() cannot execute functions from scripts loaded
  57. via lua_dofile() in config. This is kind of caching mode, avoiding
  58. reading file every time, but you must be sure you do not have
  59. someting that is executed by default and requires access to SIP
  60. message.
  61. 2. Dependencies
  62. 2.1. Kamailio Modules
  63. 2.2. External Libraries or Applications
  64. 2.1. Kamailio Modules
  65. The following modules must be loaded before this module:
  66. * none.
  67. 2.2. External Libraries or Applications
  68. The following libraries or applications must be installed before
  69. running Kamailio with this module loaded:
  70. * liblua5.1-dev - Lua devel library.
  71. 3. Exported Parameters
  72. 3.1. load (string)
  73. 3.2. register (string)
  74. 3.1. load (string)
  75. Set the path to the Lua script to be loaded at startup. Then you can
  76. use lua_run(function, params) to execute a function from the script at
  77. runtime.
  78. Default value is “null”.
  79. Example 1.1. Set load parameter
  80. ...
  81. modparam("app_lua", "load", "/usr/local/etc/kamailio/lua/myscript.lua")
  82. ...
  83. 3.2. register (string)
  84. Use this function to register optional SIP Router submodules to Lua.
  85. Available submodules are:
  86. * auth - register functions from auth module under 'sr.auth'.
  87. * auth_db - register functions from auth_db module under
  88. 'sr.auth_db'.
  89. * dispatcher - register functions from dispatcher module under
  90. 'sr.dispatcher'.
  91. * maxfwd - register functions from maxfwd module under 'sr.maxfwd'.
  92. * registrar - register functions from registrar module under
  93. 'sr.registrar'.
  94. * rr - register functions from rr module under 'sr.rr'.
  95. * sqlops - register functions from sqlops module under 'sr.sqlops'.
  96. * sl - register functions from sl module under 'sr.sl'.
  97. * tm - register functions from tm module under 'sr.tm'.
  98. * xhttp - register functions from xhttp module under 'sr.xhttp'.
  99. Note that 'sr', 'sr.hdr' and 'sr.pv' modules are always registered to
  100. Lua.
  101. Default value is “null”.
  102. Example 1.2. Set register parameter
  103. ...
  104. modparam("app_lua", "register", "sl")
  105. ...
  106. 4. Exported Functions
  107. 4.1. lua_dofile(path)
  108. 4.2. lua_dostring(script)
  109. 4.3. lua_run(function, params)
  110. 4.4. lua_runstring(script)
  111. 4.1. lua_dofile(path)
  112. Execute the Lua script stored in 'path'. The parameter can be a string
  113. with pseudo-variables evaluated at runtime.
  114. Example 1.3. lua_dofile usage
  115. ...
  116. lua_dofile("/usr/local/etc/kamailio/lua/myscript.lua");
  117. ...
  118. 4.2. lua_dostring(script)
  119. i Execute the Lua script stored in parameter. The parameter can be a
  120. string with pseudo-variables.
  121. Example 1.4. lua_dostring usage
  122. ...
  123. if(!lua_dostring("sr.log([[err]], [[----------- Hello World from $fU\n]])"))
  124. {
  125. xdbg("SCRIPT: failed to execute lua script!\n");
  126. }
  127. ...
  128. 4.3. lua_run(function, params)
  129. Execute the Lua function 'func' giving params as parameters. There can
  130. be up to 3 string parameters. The function must exist in the script
  131. loaded at startup via parameter 'load'. Parameters can be strings with
  132. pseudo-variables that are evaluated at runtime.
  133. Example 1.5. lua_run usage
  134. ...
  135. if(!lua_run("sr_append_fu_to_reply"))
  136. {
  137. xdbg("SCRIPT: failed to execute lua function!\n");
  138. }
  139. ...
  140. lua_run("lua_funcx", "$rU", "2");
  141. ...
  142. 4.4. lua_runstring(script)
  143. i Execute the Lua script stored in parameter. The parameter can be a
  144. string with pseudo-variables. The script is executed in Lua context
  145. specific to loaded Lua files at startup.
  146. Example 1.6. lua_runstring usage
  147. ...
  148. if(!lua_runstring("sr.log([[err]], [[----------- Hello World from $fU\n]])"))
  149. {
  150. xdbg("SCRIPT: failed to execute lua script!\n");
  151. }
  152. ...
  153. 5. Example of usage
  154. Create your Lua script and stored on file system, say:
  155. '/usr/local/etc/kamailio/lua/myscript.lua'.
  156. ...
  157. function sr_append_fu_to_reply()
  158. sr.hdr.append_to_reply("P-From: " .. sr.pv.get("$fu") .. "\r\n");
  159. end
  160. ...
  161. Load the script via parameter 'load' and execute function via
  162. lua_run(...).
  163. ...
  164. modparam("app_lua", "load", "/usr/local/etc/kamailio/lua/myscript.lua")
  165. ...
  166. route {
  167. ...
  168. if(!lua_run("sr_append_fu_to_reply"))
  169. {
  170. xdbg("SCRIPT: failed to execute lua function!\n");
  171. }
  172. ...
  173. }
  174. ...