pbx_lua.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2007, Digium, Inc.
  5. *
  6. * Matthew Nicholson <mnicholson@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*!
  19. * \file
  20. *
  21. * \author Matthew Nicholson <mnicholson@digium.com>
  22. * \brief Lua PBX Switch
  23. *
  24. */
  25. /*** MODULEINFO
  26. <depend>lua</depend>
  27. <support_level>extended</support_level>
  28. ***/
  29. #include "asterisk.h"
  30. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  31. #include "asterisk/logger.h"
  32. #include "asterisk/channel.h"
  33. #include "asterisk/pbx.h"
  34. #include "asterisk/module.h"
  35. #include "asterisk/cli.h"
  36. #include "asterisk/utils.h"
  37. #include "asterisk/term.h"
  38. #include "asterisk/paths.h"
  39. #include "asterisk/hashtab.h"
  40. #include <lua.h>
  41. #include <lauxlib.h>
  42. #include <lualib.h>
  43. static char *config = "extensions.lua";
  44. static char *registrar = "pbx_lua";
  45. #ifdef LOW_MEMORY
  46. #define LUA_EXT_DATA_SIZE 256
  47. #else
  48. #define LUA_EXT_DATA_SIZE 8192
  49. #endif
  50. #define LUA_BUF_SIZE 4096
  51. /* This value is used by the lua engine to signal that a Goto or dialplan jump
  52. * was detected. Ensure this value does not conflict with any values dialplan
  53. * applications might return */
  54. #define LUA_GOTO_DETECTED 5
  55. static char *lua_read_extensions_file(lua_State *L, long *size);
  56. static int lua_load_extensions(lua_State *L, struct ast_channel *chan);
  57. static int lua_reload_extensions(lua_State *L);
  58. static void lua_free_extensions(void);
  59. static int lua_sort_extensions(lua_State *L);
  60. static int lua_register_switches(lua_State *L);
  61. static int lua_register_hints(lua_State *L);
  62. static int lua_extension_cmp(lua_State *L);
  63. static int lua_find_extension(lua_State *L, const char *context, const char *exten, int priority, ast_switch_f *func, int push_func);
  64. static int lua_pbx_findapp(lua_State *L);
  65. static int lua_pbx_exec(lua_State *L);
  66. static int lua_get_variable_value(lua_State *L);
  67. static int lua_set_variable_value(lua_State *L);
  68. static int lua_get_variable(lua_State *L);
  69. static int lua_set_variable(lua_State *L);
  70. static int lua_func_read(lua_State *L);
  71. static int lua_autoservice_start(lua_State *L);
  72. static int lua_autoservice_stop(lua_State *L);
  73. static int lua_autoservice_status(lua_State *L);
  74. static int lua_check_hangup(lua_State *L);
  75. static int lua_error_function(lua_State *L);
  76. static void lua_update_registry(lua_State *L, const char *context, const char *exten, int priority);
  77. static void lua_push_variable_table(lua_State *L);
  78. static void lua_create_app_table(lua_State *L);
  79. static void lua_create_channel_table(lua_State *L);
  80. static void lua_create_variable_metatable(lua_State *L);
  81. static void lua_create_application_metatable(lua_State *L);
  82. static void lua_create_autoservice_functions(lua_State *L);
  83. static void lua_create_hangup_function(lua_State *L);
  84. static void lua_concat_args(lua_State *L, int start, int nargs);
  85. static void lua_state_destroy(void *data);
  86. static void lua_datastore_fixup(void *data, struct ast_channel *old_chan, struct ast_channel *new_chan);
  87. static lua_State *lua_get_state(struct ast_channel *chan);
  88. static int exists(struct ast_channel *chan, const char *context, const char *exten, int priority, const char *callerid, const char *data);
  89. static int canmatch(struct ast_channel *chan, const char *context, const char *exten, int priority, const char *callerid, const char *data);
  90. static int matchmore(struct ast_channel *chan, const char *context, const char *exten, int priority, const char *callerid, const char *data);
  91. static int exec(struct ast_channel *chan, const char *context, const char *exten, int priority, const char *callerid, const char *data);
  92. AST_MUTEX_DEFINE_STATIC(config_file_lock);
  93. static char *config_file_data = NULL;
  94. static long config_file_size = 0;
  95. static struct ast_context *local_contexts = NULL;
  96. static struct ast_hashtab *local_table = NULL;
  97. static const struct ast_datastore_info lua_datastore = {
  98. .type = "lua",
  99. .destroy = lua_state_destroy,
  100. .chan_fixup = lua_datastore_fixup,
  101. };
  102. /*!
  103. * \brief The destructor for lua_datastore
  104. */
  105. static void lua_state_destroy(void *data)
  106. {
  107. if (data)
  108. lua_close(data);
  109. }
  110. /*!
  111. * \brief The fixup function for the lua_datastore.
  112. * \param data the datastore data, in this case it will be a lua_State
  113. * \param old_chan the channel we are moving from
  114. * \param new_chan the channel we are moving to
  115. *
  116. * This function updates our internal channel pointer.
  117. */
  118. static void lua_datastore_fixup(void *data, struct ast_channel *old_chan, struct ast_channel *new_chan)
  119. {
  120. lua_State *L = data;
  121. lua_pushlightuserdata(L, new_chan);
  122. lua_setfield(L, LUA_REGISTRYINDEX, "channel");
  123. }
  124. /*!
  125. * \brief [lua_CFunction] Find an app and return it in a lua table (for access from lua, don't
  126. * call directly)
  127. *
  128. * This function would be called in the following example as it would be found
  129. * in extensions.lua.
  130. *
  131. * \code
  132. * app.dial
  133. * \endcode
  134. */
  135. static int lua_pbx_findapp(lua_State *L)
  136. {
  137. const char *app_name = luaL_checkstring(L, 2);
  138. lua_newtable(L);
  139. lua_pushstring(L, "name");
  140. lua_pushstring(L, app_name);
  141. lua_settable(L, -3);
  142. luaL_getmetatable(L, "application");
  143. lua_setmetatable(L, -2);
  144. return 1;
  145. }
  146. /*!
  147. * \brief [lua_CFunction] This function is part of the 'application' metatable
  148. * and is used to execute applications similar to pbx_exec() (for access from
  149. * lua, don't call directly)
  150. *
  151. * \param L the lua_State to use
  152. * \return nothing
  153. *
  154. * This funciton is executed as the '()' operator for apps accessed through the
  155. * 'app' table.
  156. *
  157. * \code
  158. * app.playback('demo-congrats')
  159. * \endcode
  160. */
  161. static int lua_pbx_exec(lua_State *L)
  162. {
  163. int res, nargs = lua_gettop(L);
  164. const char *data = "";
  165. char *app_name, *context, *exten;
  166. char tmp[80], tmp2[80], tmp3[LUA_EXT_DATA_SIZE];
  167. int priority, autoservice;
  168. struct ast_app *app;
  169. struct ast_channel *chan;
  170. lua_getfield(L, 1, "name");
  171. app_name = ast_strdupa(lua_tostring(L, -1));
  172. lua_pop(L, 1);
  173. if (!(app = pbx_findapp(app_name))) {
  174. lua_pushstring(L, "application '");
  175. lua_pushstring(L, app_name);
  176. lua_pushstring(L, "' not found");
  177. lua_concat(L, 3);
  178. return lua_error(L);
  179. }
  180. lua_getfield(L, LUA_REGISTRYINDEX, "channel");
  181. chan = lua_touserdata(L, -1);
  182. lua_pop(L, 1);
  183. context = ast_strdupa(ast_channel_context(chan));
  184. exten = ast_strdupa(ast_channel_exten(chan));
  185. priority = ast_channel_priority(chan);
  186. lua_concat_args(L, 2, nargs);
  187. data = lua_tostring(L, -1);
  188. ast_verb(3, "Executing [%s@%s:%d] %s(\"%s\", \"%s\")\n",
  189. exten, context, priority,
  190. term_color(tmp, app_name, COLOR_BRCYAN, 0, sizeof(tmp)),
  191. term_color(tmp2, ast_channel_name(chan), COLOR_BRMAGENTA, 0, sizeof(tmp2)),
  192. term_color(tmp3, data, COLOR_BRMAGENTA, 0, sizeof(tmp3)));
  193. lua_getfield(L, LUA_REGISTRYINDEX, "autoservice");
  194. autoservice = lua_toboolean(L, -1);
  195. lua_pop(L, 1);
  196. if (autoservice)
  197. ast_autoservice_stop(chan);
  198. res = pbx_exec(chan, app, data);
  199. lua_pop(L, 1); /* pop data */
  200. data = "";
  201. if (autoservice)
  202. ast_autoservice_start(chan);
  203. /* error executing an application, report it */
  204. if (res) {
  205. lua_pushinteger(L, res);
  206. return lua_error(L);
  207. }
  208. if (strcmp(context, ast_channel_context(chan))) {
  209. lua_pushstring(L, context);
  210. lua_pushstring(L, ast_channel_context(chan));
  211. lua_pushliteral(L, "context");
  212. } else if (strcmp(exten, ast_channel_exten(chan))) {
  213. lua_pushstring(L, exten);
  214. lua_pushstring(L, ast_channel_exten(chan));
  215. lua_pushliteral(L, "exten");
  216. } else if (priority != ast_channel_priority(chan)) {
  217. lua_pushinteger(L, priority);
  218. lua_pushinteger(L, ast_channel_priority(chan));
  219. lua_pushliteral(L, "priority");
  220. } else {
  221. /* no goto - restore the original position back
  222. * to lua state, in case this was a recursive dialplan
  223. * call (a dialplan application re-entering dialplan) */
  224. lua_update_registry(L, context, exten, priority);
  225. return 0;
  226. }
  227. /* goto detected - construct error message */
  228. lua_insert(L, -3);
  229. lua_pushliteral(L, " changed from ");
  230. lua_insert(L, -3);
  231. lua_pushliteral(L, " to ");
  232. lua_insert(L, -2);
  233. lua_concat(L, 5);
  234. ast_debug(2, "Goto detected: %s\n", lua_tostring(L, -1));
  235. lua_pop(L, 1);
  236. /* let the lua engine know it needs to return control to the pbx */
  237. lua_pushinteger(L, LUA_GOTO_DETECTED);
  238. lua_error(L);
  239. return 0;
  240. }
  241. /*!
  242. * \brief [lua_CFunction] Used to get the value of a variable or dialplan
  243. * function (for access from lua, don't call directly)
  244. *
  245. * The value of the variable or function is returned. This function is the
  246. * 'get()' function in the following example as would be seen in
  247. * extensions.lua.
  248. *
  249. * \code
  250. * channel.variable:get()
  251. * \endcode
  252. */
  253. static int lua_get_variable_value(lua_State *L)
  254. {
  255. struct ast_channel *chan;
  256. char *value = NULL, *name;
  257. char *workspace = ast_alloca(LUA_BUF_SIZE);
  258. int autoservice;
  259. workspace[0] = '\0';
  260. if (!lua_istable(L, 1)) {
  261. lua_pushstring(L, "User probably used '.' instead of ':' for retrieving a channel variable value");
  262. return lua_error(L);
  263. }
  264. lua_getfield(L, LUA_REGISTRYINDEX, "channel");
  265. chan = lua_touserdata(L, -1);
  266. lua_pop(L, 1);
  267. lua_getfield(L, 1, "name");
  268. name = ast_strdupa(lua_tostring(L, -1));
  269. lua_pop(L, 1);
  270. lua_getfield(L, LUA_REGISTRYINDEX, "autoservice");
  271. autoservice = lua_toboolean(L, -1);
  272. lua_pop(L, 1);
  273. if (autoservice)
  274. ast_autoservice_stop(chan);
  275. /* if this is a dialplan function then use ast_func_read(), otherwise
  276. * use pbx_retrieve_variable() */
  277. if (!ast_strlen_zero(name) && name[strlen(name) - 1] == ')') {
  278. value = ast_func_read(chan, name, workspace, LUA_BUF_SIZE) ? NULL : workspace;
  279. } else {
  280. pbx_retrieve_variable(chan, name, &value, workspace, LUA_BUF_SIZE, ast_channel_varshead(chan));
  281. }
  282. if (autoservice)
  283. ast_autoservice_start(chan);
  284. if (value) {
  285. lua_pushstring(L, value);
  286. } else {
  287. lua_pushnil(L);
  288. }
  289. return 1;
  290. }
  291. /*!
  292. * \brief [lua_CFunction] Used to set the value of a variable or dialplan
  293. * function (for access from lua, don't call directly)
  294. *
  295. * This function is the 'set()' function in the following example as would be
  296. * seen in extensions.lua.
  297. *
  298. * \code
  299. * channel.variable:set("value")
  300. * \endcode
  301. */
  302. static int lua_set_variable_value(lua_State *L)
  303. {
  304. const char *name, *value;
  305. struct ast_channel *chan;
  306. int autoservice;
  307. if (!lua_istable(L, 1)) {
  308. lua_pushstring(L, "User probably used '.' instead of ':' for setting a channel variable");
  309. return lua_error(L);
  310. }
  311. lua_getfield(L, 1, "name");
  312. name = ast_strdupa(lua_tostring(L, -1));
  313. lua_pop(L, 1);
  314. value = luaL_checkstring(L, 2);
  315. lua_getfield(L, LUA_REGISTRYINDEX, "channel");
  316. chan = lua_touserdata(L, -1);
  317. lua_pop(L, 1);
  318. lua_getfield(L, LUA_REGISTRYINDEX, "autoservice");
  319. autoservice = lua_toboolean(L, -1);
  320. lua_pop(L, 1);
  321. if (autoservice)
  322. ast_autoservice_stop(chan);
  323. pbx_builtin_setvar_helper(chan, name, value);
  324. if (autoservice)
  325. ast_autoservice_start(chan);
  326. return 0;
  327. }
  328. /*!
  329. * \brief Update the lua registry with the given context, exten, and priority.
  330. *
  331. * \param L the lua_State to use
  332. * \param context the new context
  333. * \param exten the new exten
  334. * \param priority the new priority
  335. */
  336. static void lua_update_registry(lua_State *L, const char *context, const char *exten, int priority)
  337. {
  338. lua_pushstring(L, context);
  339. lua_setfield(L, LUA_REGISTRYINDEX, "context");
  340. lua_pushstring(L, exten);
  341. lua_setfield(L, LUA_REGISTRYINDEX, "exten");
  342. lua_pushinteger(L, priority);
  343. lua_setfield(L, LUA_REGISTRYINDEX, "priority");
  344. }
  345. /*!
  346. * \brief Push a 'variable' table on the stack for access the channel variable
  347. * with the given name.
  348. *
  349. * The value on the top of the stack is popped and used as the name.
  350. *
  351. * \param L the lua_State to use
  352. */
  353. static void lua_push_variable_table(lua_State *L)
  354. {
  355. lua_newtable(L);
  356. luaL_getmetatable(L, "variable");
  357. lua_setmetatable(L, -2);
  358. lua_insert(L, -2); /* move the table after the name */
  359. lua_setfield(L, -2, "name");
  360. lua_pushcfunction(L, &lua_get_variable_value);
  361. lua_setfield(L, -2, "get");
  362. lua_pushcfunction(L, &lua_set_variable_value);
  363. lua_setfield(L, -2, "set");
  364. }
  365. /*!
  366. * \brief Create the global 'app' table for executing applications
  367. *
  368. * \param L the lua_State to use
  369. */
  370. static void lua_create_app_table(lua_State *L)
  371. {
  372. lua_newtable(L);
  373. luaL_newmetatable(L, "app");
  374. lua_pushstring(L, "__index");
  375. lua_pushcfunction(L, &lua_pbx_findapp);
  376. lua_settable(L, -3);
  377. lua_setmetatable(L, -2);
  378. lua_setglobal(L, "app");
  379. }
  380. /*!
  381. * \brief Create the global 'channel' table for accesing channel variables
  382. *
  383. * \param L the lua_State to use
  384. */
  385. static void lua_create_channel_table(lua_State *L)
  386. {
  387. lua_newtable(L);
  388. luaL_newmetatable(L, "channel_data");
  389. lua_pushstring(L, "__index");
  390. lua_pushcfunction(L, &lua_get_variable);
  391. lua_settable(L, -3);
  392. lua_pushstring(L, "__newindex");
  393. lua_pushcfunction(L, &lua_set_variable);
  394. lua_settable(L, -3);
  395. lua_setmetatable(L, -2);
  396. lua_setglobal(L, "channel");
  397. }
  398. /*!
  399. * \brief Create the 'variable' metatable, used to retrieve channel variables
  400. *
  401. * \param L the lua_State to use
  402. */
  403. static void lua_create_variable_metatable(lua_State *L)
  404. {
  405. luaL_newmetatable(L, "variable");
  406. lua_pushstring(L, "__call");
  407. lua_pushcfunction(L, &lua_func_read);
  408. lua_settable(L, -3);
  409. lua_pop(L, 1);
  410. }
  411. /*!
  412. * \brief Create the 'application' metatable, used to execute asterisk
  413. * applications from lua
  414. *
  415. * \param L the lua_State to use
  416. */
  417. static void lua_create_application_metatable(lua_State *L)
  418. {
  419. luaL_newmetatable(L, "application");
  420. lua_pushstring(L, "__call");
  421. lua_pushcfunction(L, &lua_pbx_exec);
  422. lua_settable(L, -3);
  423. lua_pop(L, 1);
  424. }
  425. /*!
  426. * \brief Create the autoservice functions
  427. *
  428. * \param L the lua_State to use
  429. */
  430. static void lua_create_autoservice_functions(lua_State *L)
  431. {
  432. lua_pushcfunction(L, &lua_autoservice_start);
  433. lua_setglobal(L, "autoservice_start");
  434. lua_pushcfunction(L, &lua_autoservice_stop);
  435. lua_setglobal(L, "autoservice_stop");
  436. lua_pushcfunction(L, &lua_autoservice_status);
  437. lua_setglobal(L, "autoservice_status");
  438. lua_pushboolean(L, 1);
  439. lua_setfield(L, LUA_REGISTRYINDEX, "autoservice");
  440. }
  441. /*!
  442. * \brief Create the hangup check function
  443. *
  444. * \param L the lua_State to use
  445. */
  446. static void lua_create_hangup_function(lua_State *L)
  447. {
  448. lua_pushcfunction(L, &lua_check_hangup);
  449. lua_setglobal(L, "check_hangup");
  450. }
  451. /*!
  452. * \brief [lua_CFunction] Return a lua 'variable' object (for access from lua, don't call
  453. * directly)
  454. *
  455. * This function is called to lookup a variable construct a 'variable' object.
  456. * It would be called in the following example as would be seen in
  457. * extensions.lua.
  458. *
  459. * \code
  460. * channel.variable
  461. * \endcode
  462. */
  463. static int lua_get_variable(lua_State *L)
  464. {
  465. struct ast_channel *chan;
  466. const char *name = luaL_checkstring(L, 2);
  467. char *value = NULL;
  468. char *workspace = ast_alloca(LUA_BUF_SIZE);
  469. workspace[0] = '\0';
  470. lua_getfield(L, LUA_REGISTRYINDEX, "channel");
  471. chan = lua_touserdata(L, -1);
  472. lua_pop(L, 1);
  473. lua_pushvalue(L, 2);
  474. lua_push_variable_table(L);
  475. /* if this is not a request for a dialplan funciton attempt to retrieve
  476. * the value of the variable */
  477. if (!ast_strlen_zero(name) && name[strlen(name) - 1] != ')') {
  478. pbx_retrieve_variable(chan, name, &value, workspace, LUA_BUF_SIZE, ast_channel_varshead(chan));
  479. }
  480. if (value) {
  481. lua_pushstring(L, value);
  482. lua_setfield(L, -2, "value");
  483. }
  484. return 1;
  485. }
  486. /*!
  487. * \brief [lua_CFunction] Set the value of a channel variable or dialplan
  488. * function (for access from lua, don't call directly)
  489. *
  490. * This function is called to set a variable or dialplan function. It would be
  491. * called in the following example as would be seen in extensions.lua.
  492. *
  493. * \code
  494. * channel.variable = "value"
  495. * \endcode
  496. */
  497. static int lua_set_variable(lua_State *L)
  498. {
  499. struct ast_channel *chan;
  500. int autoservice;
  501. const char *name = luaL_checkstring(L, 2);
  502. const char *value = luaL_checkstring(L, 3);
  503. lua_getfield(L, LUA_REGISTRYINDEX, "channel");
  504. chan = lua_touserdata(L, -1);
  505. lua_pop(L, 1);
  506. lua_getfield(L, LUA_REGISTRYINDEX, "autoservice");
  507. autoservice = lua_toboolean(L, -1);
  508. lua_pop(L, 1);
  509. if (autoservice)
  510. ast_autoservice_stop(chan);
  511. pbx_builtin_setvar_helper(chan, name, value);
  512. if (autoservice)
  513. ast_autoservice_start(chan);
  514. return 0;
  515. }
  516. /*!
  517. * \brief Concatenate a list of lua function arguments into a comma separated
  518. * string.
  519. * \param L the lua_State to use
  520. * \param start the index of the first argument
  521. * \param nargs the number of args
  522. *
  523. * The resulting string will be left on the top of the stack.
  524. */
  525. static void lua_concat_args(lua_State *L, int start, int nargs) {
  526. int concat = 0;
  527. int i = start + 1;
  528. if (start <= nargs && !lua_isnil(L, start)) {
  529. lua_pushvalue(L, start);
  530. concat += 1;
  531. }
  532. for (; i <= nargs; i++) {
  533. if (lua_isnil(L, i)) {
  534. lua_pushliteral(L, ",");
  535. concat += 1;
  536. } else {
  537. lua_pushliteral(L, ",");
  538. lua_pushvalue(L, i);
  539. concat += 2;
  540. }
  541. }
  542. lua_concat(L, concat);
  543. }
  544. /*!
  545. * \brief [lua_CFunction] Create a 'variable' object for accessing a dialplan
  546. * function (for access from lua, don't call directly)
  547. *
  548. * This function is called to create a 'variable' object to access a dialplan
  549. * function. It would be called in the following example as would be seen in
  550. * extensions.lua.
  551. *
  552. * \code
  553. * channel.func("arg1", "arg2", "arg3")
  554. * \endcode
  555. *
  556. * To actually do anything with the resulting value you must use the 'get()'
  557. * and 'set()' methods (the reason is the resulting value is not a value, but
  558. * an object in the form of a lua table).
  559. */
  560. static int lua_func_read(lua_State *L)
  561. {
  562. int nargs = lua_gettop(L);
  563. /* build a string in the form of "func_name(arg1,arg2,arg3)" */
  564. lua_getfield(L, 1, "name");
  565. lua_pushliteral(L, "(");
  566. lua_concat_args(L, 2, nargs);
  567. lua_pushliteral(L, ")");
  568. lua_concat(L, 4);
  569. lua_push_variable_table(L);
  570. return 1;
  571. }
  572. /*!
  573. * \brief [lua_CFunction] Tell pbx_lua to maintain an autoservice on this
  574. * channel (for access from lua, don't call directly)
  575. *
  576. * \param L the lua_State to use
  577. *
  578. * This function will set a flag that will cause pbx_lua to maintain an
  579. * autoservice on this channel. The autoservice will automatically be stopped
  580. * and restarted before calling applications and functions.
  581. */
  582. static int lua_autoservice_start(lua_State *L)
  583. {
  584. struct ast_channel *chan;
  585. lua_getfield(L, LUA_REGISTRYINDEX, "autoservice");
  586. if (lua_toboolean(L, -1)) {
  587. /* autservice already running */
  588. lua_pop(L, 1);
  589. return 0;
  590. }
  591. lua_pop(L, 1);
  592. lua_getfield(L, LUA_REGISTRYINDEX, "channel");
  593. chan = lua_touserdata(L, -1);
  594. lua_pop(L, 1);
  595. ast_autoservice_start(chan);
  596. lua_pushboolean(L, 1);
  597. lua_setfield(L, LUA_REGISTRYINDEX, "autoservice");
  598. return 0;
  599. }
  600. /*!
  601. * \brief [lua_CFunction] Tell pbx_lua to stop maintaning an autoservice on
  602. * this channel (for access from lua, don't call directly)
  603. *
  604. * \param L the lua_State to use
  605. *
  606. * This function will stop any autoservice running and turn off the autoservice
  607. * flag. If this function returns false, it's probably because no autoservice
  608. * was running to begin with.
  609. */
  610. static int lua_autoservice_stop(lua_State *L)
  611. {
  612. struct ast_channel *chan;
  613. lua_getfield(L, LUA_REGISTRYINDEX, "autoservice");
  614. if (!lua_toboolean(L, -1)) {
  615. /* no autservice running */
  616. lua_pop(L, 1);
  617. return 0;
  618. }
  619. lua_pop(L, 1);
  620. lua_getfield(L, LUA_REGISTRYINDEX, "channel");
  621. chan = lua_touserdata(L, -1);
  622. lua_pop(L, 1);
  623. ast_autoservice_stop(chan);
  624. lua_pushboolean(L, 0);
  625. lua_setfield(L, LUA_REGISTRYINDEX, "autoservice");
  626. return 0;
  627. }
  628. /*!
  629. * \brief [lua_CFunction] Get the status of the autoservice flag (for access
  630. * from lua, don't call directly)
  631. *
  632. * \param L the lua_State to use
  633. *
  634. * \return This function returns the status of the autoservice flag as a
  635. * boolean to its lua caller.
  636. */
  637. static int lua_autoservice_status(lua_State *L)
  638. {
  639. lua_getfield(L, LUA_REGISTRYINDEX, "autoservice");
  640. return 1;
  641. }
  642. /*!
  643. * \brief [lua_CFunction] Check if this channel has been hungup or not (for
  644. * access from lua, don't call directly)
  645. *
  646. * \param L the lua_State to use
  647. *
  648. * \return This function returns true if the channel was hungup
  649. */
  650. static int lua_check_hangup(lua_State *L)
  651. {
  652. struct ast_channel *chan;
  653. lua_getfield(L, LUA_REGISTRYINDEX, "channel");
  654. chan = lua_touserdata(L, -1);
  655. lua_pop(L, 1);
  656. lua_pushboolean(L, ast_check_hangup(chan));
  657. return 1;
  658. }
  659. /*!
  660. * \brief [lua_CFunction] Handle lua errors (for access from lua, don't call
  661. * directly)
  662. *
  663. * \param L the lua_State to use
  664. */
  665. static int lua_error_function(lua_State *L)
  666. {
  667. int message_index;
  668. /* pass number arguments right through back to asterisk*/
  669. if (lua_isnumber(L, -1)) {
  670. return 1;
  671. }
  672. /* if we are here then we have a string error message, let's attach a
  673. * backtrace to it */
  674. message_index = lua_gettop(L);
  675. /* prepare to prepend a new line to the traceback */
  676. lua_pushliteral(L, "\n");
  677. lua_getglobal(L, "debug");
  678. lua_getfield(L, -1, "traceback");
  679. lua_remove(L, -2); /* remove the 'debug' table */
  680. lua_pushvalue(L, message_index);
  681. lua_remove(L, message_index);
  682. lua_pushnumber(L, 2);
  683. lua_call(L, 2, 1);
  684. /* prepend the new line we prepared above */
  685. lua_concat(L, 2);
  686. return 1;
  687. }
  688. /*!
  689. * \brief Store the sort order of each context
  690. * In the event of an error, an error string will be pushed onto the lua stack.
  691. *
  692. * \retval 0 success
  693. * \retval 1 failure
  694. */
  695. static int lua_sort_extensions(lua_State *L)
  696. {
  697. int extensions, extensions_order;
  698. /* create the extensions_order table */
  699. lua_newtable(L);
  700. lua_setfield(L, LUA_REGISTRYINDEX, "extensions_order");
  701. lua_getfield(L, LUA_REGISTRYINDEX, "extensions_order");
  702. extensions_order = lua_gettop(L);
  703. /* sort each context in the extensions table */
  704. /* load the 'extensions' table */
  705. lua_getglobal(L, "extensions");
  706. extensions = lua_gettop(L);
  707. if (lua_isnil(L, -1)) {
  708. lua_pop(L, 1);
  709. lua_pushstring(L, "Unable to find 'extensions' table in extensions.lua\n");
  710. return 1;
  711. }
  712. /* iterate through the extensions table and create a
  713. * matching table (holding the sort order) in the
  714. * extensions_order table for each context that is found
  715. */
  716. for (lua_pushnil(L); lua_next(L, extensions); lua_pop(L, 1)) {
  717. int context = lua_gettop(L);
  718. int context_name = context - 1;
  719. int context_order;
  720. /* copy the context_name to be used as the key for the
  721. * context_order table in the extensions_order table later */
  722. lua_pushvalue(L, context_name);
  723. /* create the context_order table */
  724. lua_newtable(L);
  725. context_order = lua_gettop(L);
  726. /* iterate through this context an popluate the corrisponding
  727. * table in the extensions_order table */
  728. for (lua_pushnil(L); lua_next(L, context); lua_pop(L, 1)) {
  729. int exten = lua_gettop(L) - 1;
  730. #if LUA_VERSION_NUM < 502
  731. lua_pushinteger(L, lua_objlen(L, context_order) + 1);
  732. #else
  733. lua_pushinteger(L, lua_rawlen(L, context_order) + 1);
  734. #endif
  735. lua_pushvalue(L, exten);
  736. lua_settable(L, context_order);
  737. }
  738. lua_settable(L, extensions_order); /* put the context_order table in the extensions_order table */
  739. /* now sort the new table */
  740. /* push the table.sort function */
  741. lua_getglobal(L, "table");
  742. lua_getfield(L, -1, "sort");
  743. lua_remove(L, -2); /* remove the 'table' table */
  744. /* push the context_order table */
  745. lua_pushvalue(L, context_name);
  746. lua_gettable(L, extensions_order);
  747. /* push the comp function */
  748. lua_pushcfunction(L, &lua_extension_cmp);
  749. if (lua_pcall(L, 2, 0, 0)) {
  750. lua_insert(L, -5);
  751. lua_pop(L, 4);
  752. return 1;
  753. }
  754. }
  755. /* remove the extensions table and the extensions_order table */
  756. lua_pop(L, 2);
  757. return 0;
  758. }
  759. /*!
  760. * \brief Register dialplan switches for our pbx_lua contexs.
  761. *
  762. * In the event of an error, an error string will be pushed onto the lua stack.
  763. *
  764. * \retval 0 success
  765. * \retval 1 failure
  766. */
  767. static int lua_register_switches(lua_State *L)
  768. {
  769. int extensions;
  770. struct ast_context *con = NULL;
  771. /* create the hash table for our contexts */
  772. /* XXX do we ever need to destroy this? pbx_config does not */
  773. if (!local_table)
  774. local_table = ast_hashtab_create(17, ast_hashtab_compare_contexts, ast_hashtab_resize_java, ast_hashtab_newsize_java, ast_hashtab_hash_contexts, 0);
  775. /* load the 'extensions' table */
  776. lua_getglobal(L, "extensions");
  777. extensions = lua_gettop(L);
  778. if (lua_isnil(L, -1)) {
  779. lua_pop(L, 1);
  780. lua_pushstring(L, "Unable to find 'extensions' table in extensions.lua\n");
  781. return 1;
  782. }
  783. /* iterate through the extensions table and register a context and
  784. * dialplan switch for each lua context
  785. */
  786. for (lua_pushnil(L); lua_next(L, extensions); lua_pop(L, 1)) {
  787. int context = lua_gettop(L);
  788. int context_name = context - 1;
  789. const char *context_str = lua_tostring(L, context_name);
  790. /* find or create this context */
  791. con = ast_context_find_or_create(&local_contexts, local_table, context_str, registrar);
  792. if (!con) {
  793. /* remove extensions table and context key and value */
  794. lua_pop(L, 3);
  795. lua_pushstring(L, "Failed to find or create context\n");
  796. return 1;
  797. }
  798. /* register the switch */
  799. if (ast_context_add_switch2(con, "Lua", "", 0, registrar)) {
  800. /* remove extensions table and context key and value */
  801. lua_pop(L, 3);
  802. lua_pushstring(L, "Unable to create switch for context\n");
  803. return 1;
  804. }
  805. }
  806. /* remove the extensions table */
  807. lua_pop(L, 1);
  808. return 0;
  809. }
  810. /*!
  811. * \brief Register dialplan hints for our pbx_lua contexs.
  812. *
  813. * In the event of an error, an error string will be pushed onto the lua stack.
  814. *
  815. * \retval 0 success
  816. * \retval 1 failure
  817. */
  818. static int lua_register_hints(lua_State *L)
  819. {
  820. int hints;
  821. struct ast_context *con = NULL;
  822. /* create the hash table for our contexts */
  823. /* XXX do we ever need to destroy this? pbx_config does not */
  824. if (!local_table)
  825. local_table = ast_hashtab_create(17, ast_hashtab_compare_contexts, ast_hashtab_resize_java, ast_hashtab_newsize_java, ast_hashtab_hash_contexts, 0);
  826. /* load the 'hints' table */
  827. lua_getglobal(L, "hints");
  828. hints = lua_gettop(L);
  829. if (lua_isnil(L, -1)) {
  830. /* hints table not found, move along */
  831. lua_pop(L, 1);
  832. return 0;
  833. }
  834. /* iterate through the hints table and register each context and
  835. * the hints that go along with it
  836. */
  837. for (lua_pushnil(L); lua_next(L, hints); lua_pop(L, 1)) {
  838. int context = lua_gettop(L);
  839. int context_name = context - 1;
  840. const char *context_str = lua_tostring(L, context_name);
  841. /* find or create this context */
  842. con = ast_context_find_or_create(&local_contexts, local_table, context_str, registrar);
  843. if (!con) {
  844. /* remove hints table and context key and value */
  845. lua_pop(L, 3);
  846. lua_pushstring(L, "Failed to find or create context\n");
  847. return 1;
  848. }
  849. /* register each hint */
  850. for (lua_pushnil(L); lua_next(L, context); lua_pop(L, 1)) {
  851. const char *hint_value = lua_tostring(L, -1);
  852. const char *hint_name;
  853. /* the hint value is not a string, ignore it */
  854. if (!hint_value) {
  855. continue;
  856. }
  857. /* copy the name then convert it to a string */
  858. lua_pushvalue(L, -2);
  859. if (!(hint_name = lua_tostring(L, -1))) {
  860. /* ignore non-string value */
  861. lua_pop(L, 1);
  862. continue;
  863. }
  864. if (ast_add_extension2(con, 0, hint_name, PRIORITY_HINT, NULL, NULL, hint_value, NULL, NULL, registrar)) {
  865. /* remove hints table, hint name, hint value,
  866. * key copy, context name, and contex table */
  867. lua_pop(L, 6);
  868. lua_pushstring(L, "Error creating hint\n");
  869. return 1;
  870. }
  871. /* pop the name copy */
  872. lua_pop(L, 1);
  873. }
  874. }
  875. /* remove the hints table */
  876. lua_pop(L, 1);
  877. return 0;
  878. }
  879. /*!
  880. * \brief [lua_CFunction] Compare two extensions (for access from lua, don't
  881. * call directly)
  882. *
  883. * This function returns true if the first extension passed should match after
  884. * the second. It behaves like the '<' operator.
  885. */
  886. static int lua_extension_cmp(lua_State *L)
  887. {
  888. const char *a = luaL_checkstring(L, -2);
  889. const char *b = luaL_checkstring(L, -1);
  890. if (ast_extension_cmp(a, b) == -1)
  891. lua_pushboolean(L, 1);
  892. else
  893. lua_pushboolean(L, 0);
  894. return 1;
  895. }
  896. /*!
  897. * \brief Load the extensions.lua file in to a buffer and execute the file
  898. *
  899. * \param L the lua_State to use
  900. * \param size a pointer to store the size of the buffer
  901. *
  902. * \note The caller is expected to free the buffer at some point.
  903. *
  904. * \return a pointer to the buffer
  905. */
  906. static char *lua_read_extensions_file(lua_State *L, long *size)
  907. {
  908. FILE *f;
  909. int error_func;
  910. char *data;
  911. char *path = ast_alloca(strlen(config) + strlen(ast_config_AST_CONFIG_DIR) + 2);
  912. sprintf(path, "%s/%s", ast_config_AST_CONFIG_DIR, config);
  913. if (!(f = fopen(path, "r"))) {
  914. lua_pushstring(L, "cannot open '");
  915. lua_pushstring(L, path);
  916. lua_pushstring(L, "' for reading: ");
  917. lua_pushstring(L, strerror(errno));
  918. lua_concat(L, 4);
  919. return NULL;
  920. }
  921. if (fseek(f, 0l, SEEK_END)) {
  922. fclose(f);
  923. lua_pushliteral(L, "error determining the size of the config file");
  924. return NULL;
  925. }
  926. *size = ftell(f);
  927. if (fseek(f, 0l, SEEK_SET)) {
  928. *size = 0;
  929. fclose(f);
  930. lua_pushliteral(L, "error reading config file");
  931. return NULL;
  932. }
  933. if (!(data = ast_malloc(*size))) {
  934. *size = 0;
  935. fclose(f);
  936. lua_pushstring(L, "not enough memory");
  937. return NULL;
  938. }
  939. if (fread(data, sizeof(char), *size, f) != *size) {
  940. *size = 0;
  941. fclose(f);
  942. lua_pushliteral(L, "problem reading configuration file");
  943. return NULL;
  944. }
  945. fclose(f);
  946. lua_pushcfunction(L, &lua_error_function);
  947. error_func = lua_gettop(L);
  948. if (luaL_loadbuffer(L, data, *size, "extensions.lua")
  949. || lua_pcall(L, 0, LUA_MULTRET, error_func)
  950. || lua_sort_extensions(L)
  951. || lua_register_switches(L)
  952. || lua_register_hints(L)) {
  953. ast_free(data);
  954. data = NULL;
  955. *size = 0;
  956. }
  957. lua_remove(L, error_func);
  958. return data;
  959. }
  960. /*!
  961. * \brief Load the extensions.lua file from the internal buffer
  962. *
  963. * \param L the lua_State to use
  964. * \param chan channel to work on
  965. *
  966. * This function also sets up some constructs used by the extensions.lua file.
  967. * In the event of an error, an error string will be pushed onto the lua stack.
  968. *
  969. * \retval 0 success
  970. * \retval 1 failure
  971. */
  972. static int lua_load_extensions(lua_State *L, struct ast_channel *chan)
  973. {
  974. /* store a pointer to this channel */
  975. lua_pushlightuserdata(L, chan);
  976. lua_setfield(L, LUA_REGISTRYINDEX, "channel");
  977. luaL_openlibs(L);
  978. /* load and sort extensions */
  979. ast_mutex_lock(&config_file_lock);
  980. if (luaL_loadbuffer(L, config_file_data, config_file_size, "extensions.lua")
  981. || lua_pcall(L, 0, LUA_MULTRET, 0)
  982. || lua_sort_extensions(L)) {
  983. ast_mutex_unlock(&config_file_lock);
  984. return 1;
  985. }
  986. ast_mutex_unlock(&config_file_lock);
  987. /* now we setup special tables and functions */
  988. lua_create_app_table(L);
  989. lua_create_channel_table(L);
  990. lua_create_variable_metatable(L);
  991. lua_create_application_metatable(L);
  992. lua_create_autoservice_functions(L);
  993. lua_create_hangup_function(L);
  994. return 0;
  995. }
  996. /*!
  997. * \brief Reload the extensions file and update the internal buffers if it
  998. * loads correctly.
  999. *
  1000. * \warning This function should not be called on a lua_State returned from
  1001. * lua_get_state().
  1002. *
  1003. * \param L the lua_State to use (must be freshly allocated with
  1004. * luaL_newstate(), don't use lua_get_state())
  1005. */
  1006. static int lua_reload_extensions(lua_State *L)
  1007. {
  1008. long size = 0;
  1009. char *data = NULL;
  1010. luaL_openlibs(L);
  1011. if (!(data = lua_read_extensions_file(L, &size))) {
  1012. return 1;
  1013. }
  1014. ast_mutex_lock(&config_file_lock);
  1015. if (config_file_data)
  1016. ast_free(config_file_data);
  1017. config_file_data = data;
  1018. config_file_size = size;
  1019. /* merge our new contexts */
  1020. ast_merge_contexts_and_delete(&local_contexts, local_table, registrar);
  1021. /* merge_contexts_and_delete will actually, at the correct moment,
  1022. set the global dialplan pointers to your local_contexts and local_table.
  1023. It then will free up the old tables itself. Just be sure not to
  1024. hang onto the pointers. */
  1025. local_table = NULL;
  1026. local_contexts = NULL;
  1027. ast_mutex_unlock(&config_file_lock);
  1028. return 0;
  1029. }
  1030. /*!
  1031. * \brief Free the internal extensions buffer.
  1032. */
  1033. static void lua_free_extensions()
  1034. {
  1035. ast_mutex_lock(&config_file_lock);
  1036. config_file_size = 0;
  1037. ast_free(config_file_data);
  1038. ast_mutex_unlock(&config_file_lock);
  1039. }
  1040. /*!
  1041. * \brief Get the lua_State for this channel
  1042. *
  1043. * If no channel is passed then a new state is allocated. States with no
  1044. * channel assocatied with them should only be used for matching extensions.
  1045. * If the channel does not yet have a lua state associated with it, one will be
  1046. * created.
  1047. *
  1048. * \note If no channel was passed then the caller is expected to free the state
  1049. * using lua_close().
  1050. *
  1051. * \return a lua_State
  1052. */
  1053. static lua_State *lua_get_state(struct ast_channel *chan)
  1054. {
  1055. struct ast_datastore *datastore = NULL;
  1056. lua_State *L;
  1057. if (!chan) {
  1058. L = luaL_newstate();
  1059. if (!L) {
  1060. ast_log(LOG_ERROR, "Error allocating lua_State, no memory\n");
  1061. return NULL;
  1062. }
  1063. if (lua_load_extensions(L, NULL)) {
  1064. const char *error = lua_tostring(L, -1);
  1065. ast_log(LOG_ERROR, "Error loading extensions.lua: %s\n", error);
  1066. lua_close(L);
  1067. return NULL;
  1068. }
  1069. return L;
  1070. } else {
  1071. ast_channel_lock(chan);
  1072. datastore = ast_channel_datastore_find(chan, &lua_datastore, NULL);
  1073. ast_channel_unlock(chan);
  1074. if (!datastore) {
  1075. /* nothing found, allocate a new lua state */
  1076. datastore = ast_datastore_alloc(&lua_datastore, NULL);
  1077. if (!datastore) {
  1078. ast_log(LOG_ERROR, "Error allocation channel datastore for lua_State\n");
  1079. return NULL;
  1080. }
  1081. datastore->data = luaL_newstate();
  1082. if (!datastore->data) {
  1083. ast_datastore_free(datastore);
  1084. ast_log(LOG_ERROR, "Error allocating lua_State, no memory\n");
  1085. return NULL;
  1086. }
  1087. ast_channel_lock(chan);
  1088. ast_channel_datastore_add(chan, datastore);
  1089. ast_channel_unlock(chan);
  1090. L = datastore->data;
  1091. if (lua_load_extensions(L, chan)) {
  1092. const char *error = lua_tostring(L, -1);
  1093. ast_log(LOG_ERROR, "Error loading extensions.lua for %s: %s\n", ast_channel_name(chan), error);
  1094. ast_channel_lock(chan);
  1095. ast_channel_datastore_remove(chan, datastore);
  1096. ast_channel_unlock(chan);
  1097. ast_datastore_free(datastore);
  1098. return NULL;
  1099. }
  1100. }
  1101. return datastore->data;
  1102. }
  1103. }
  1104. static int exists(struct ast_channel *chan, const char *context, const char *exten, int priority, const char *callerid, const char *data)
  1105. {
  1106. int res;
  1107. lua_State *L;
  1108. struct ast_module_user *u = ast_module_user_add(chan);
  1109. if (!u) {
  1110. ast_log(LOG_ERROR, "Error adjusting use count, probably could not allocate memory\n");
  1111. return 0;
  1112. }
  1113. L = lua_get_state(chan);
  1114. if (!L) {
  1115. ast_module_user_remove(u);
  1116. return 0;
  1117. }
  1118. res = lua_find_extension(L, context, exten, priority, &exists, 0);
  1119. if (!chan) lua_close(L);
  1120. ast_module_user_remove(u);
  1121. return res;
  1122. }
  1123. static int canmatch(struct ast_channel *chan, const char *context, const char *exten, int priority, const char *callerid, const char *data)
  1124. {
  1125. int res;
  1126. lua_State *L;
  1127. struct ast_module_user *u = ast_module_user_add(chan);
  1128. if (!u) {
  1129. ast_log(LOG_ERROR, "Error adjusting use count, probably could not allocate memory\n");
  1130. return 0;
  1131. }
  1132. L = lua_get_state(chan);
  1133. if (!L) {
  1134. ast_module_user_remove(u);
  1135. return 0;
  1136. }
  1137. res = lua_find_extension(L, context, exten, priority, &canmatch, 0);
  1138. if (!chan) lua_close(L);
  1139. ast_module_user_remove(u);
  1140. return res;
  1141. }
  1142. static int matchmore(struct ast_channel *chan, const char *context, const char *exten, int priority, const char *callerid, const char *data)
  1143. {
  1144. int res;
  1145. lua_State *L;
  1146. struct ast_module_user *u = ast_module_user_add(chan);
  1147. if (!u) {
  1148. ast_log(LOG_ERROR, "Error adjusting use count, probably could not allocate memory\n");
  1149. return 0;
  1150. }
  1151. L = lua_get_state(chan);
  1152. if (!L) {
  1153. ast_module_user_remove(u);
  1154. return 0;
  1155. }
  1156. res = lua_find_extension(L, context, exten, priority, &matchmore, 0);
  1157. if (!chan) lua_close(L);
  1158. ast_module_user_remove(u);
  1159. return res;
  1160. }
  1161. static int exec(struct ast_channel *chan, const char *context, const char *exten, int priority, const char *callerid, const char *data)
  1162. {
  1163. int res, error_func;
  1164. lua_State *L;
  1165. struct ast_module_user *u = ast_module_user_add(chan);
  1166. if (!u) {
  1167. ast_log(LOG_ERROR, "Error adjusting use count, probably could not allocate memory\n");
  1168. return -1;
  1169. }
  1170. L = lua_get_state(chan);
  1171. if (!L) {
  1172. ast_module_user_remove(u);
  1173. return -1;
  1174. }
  1175. lua_pushcfunction(L, &lua_error_function);
  1176. error_func = lua_gettop(L);
  1177. /* push the extension function onto the stack */
  1178. if (!lua_find_extension(L, context, exten, priority, &exists, 1)) {
  1179. lua_pop(L, 1); /* pop the debug function */
  1180. ast_log(LOG_ERROR, "Could not find extension %s in context %s\n", exten, context);
  1181. if (!chan) lua_close(L);
  1182. ast_module_user_remove(u);
  1183. return -1;
  1184. }
  1185. lua_getfield(L, LUA_REGISTRYINDEX, "autoservice");
  1186. if (lua_toboolean(L, -1)) {
  1187. ast_autoservice_start(chan);
  1188. }
  1189. lua_pop(L, 1);
  1190. lua_update_registry(L, context, exten, priority);
  1191. lua_pushstring(L, context);
  1192. lua_pushstring(L, exten);
  1193. res = lua_pcall(L, 2, 0, error_func);
  1194. if (res) {
  1195. if (res == LUA_ERRRUN) {
  1196. res = -1;
  1197. if (lua_isnumber(L, -1)) {
  1198. res = lua_tointeger(L, -1);
  1199. if (res == LUA_GOTO_DETECTED) {
  1200. res = 0;
  1201. }
  1202. } else if (lua_isstring(L, -1)) {
  1203. const char *error = lua_tostring(L, -1);
  1204. ast_log(LOG_ERROR, "Error executing lua extension: %s\n", error);
  1205. }
  1206. } else if (res == LUA_ERRERR) {
  1207. res = -1;
  1208. ast_log(LOG_ERROR, "Error in the lua error handler (this is probably a bug in pbx_lua)\n");
  1209. } else if (res == LUA_ERRMEM) {
  1210. res = -1;
  1211. ast_log(LOG_ERROR, "Memory allocation error\n");
  1212. }
  1213. lua_pop(L, 1);
  1214. }
  1215. lua_remove(L, error_func);
  1216. lua_getfield(L, LUA_REGISTRYINDEX, "autoservice");
  1217. if (lua_toboolean(L, -1)) {
  1218. ast_autoservice_stop(chan);
  1219. }
  1220. lua_pop(L, 1);
  1221. if (!chan) lua_close(L);
  1222. ast_module_user_remove(u);
  1223. return res;
  1224. }
  1225. /*!
  1226. * \brief Locate an extensions and optionally push the matching function on the
  1227. * stack
  1228. *
  1229. * \param L the lua_State to use
  1230. * \param context the context to look in
  1231. * \param exten the extension to look up
  1232. * \param priority the priority to check, '1' is the only valid priority
  1233. * \param func the calling func, used to adjust matching behavior between,
  1234. * match, canmatch, and matchmore
  1235. * \param push_func whether or not to push the lua function for the given
  1236. * extension onto the stack
  1237. */
  1238. static int lua_find_extension(lua_State *L, const char *context, const char *exten, int priority, ast_switch_f *func, int push_func)
  1239. {
  1240. int context_table, context_order_table, i;
  1241. ast_debug(2, "Looking up %s@%s:%i\n", exten, context, priority);
  1242. if (priority != 1)
  1243. return 0;
  1244. /* load the 'extensions' table */
  1245. lua_getglobal(L, "extensions");
  1246. if (lua_isnil(L, -1)) {
  1247. ast_log(LOG_ERROR, "Unable to find 'extensions' table in extensions.lua\n");
  1248. lua_pop(L, 1);
  1249. return 0;
  1250. }
  1251. /* load the given context */
  1252. lua_getfield(L, -1, context);
  1253. if (lua_isnil(L, -1)) {
  1254. lua_pop(L, 2);
  1255. return 0;
  1256. }
  1257. /* remove the extensions table */
  1258. lua_remove(L, -2);
  1259. context_table = lua_gettop(L);
  1260. /* load the extensions order table for this context */
  1261. lua_getfield(L, LUA_REGISTRYINDEX, "extensions_order");
  1262. lua_getfield(L, -1, context);
  1263. lua_remove(L, -2); /* remove the extensions order table */
  1264. context_order_table = lua_gettop(L);
  1265. /* step through the extensions looking for a match */
  1266. #if LUA_VERSION_NUM < 502
  1267. for (i = 1; i < lua_objlen(L, context_order_table) + 1; i++) {
  1268. #else
  1269. for (i = 1; i < lua_rawlen(L, context_order_table) + 1; i++) {
  1270. #endif
  1271. int e_index_copy, match = 0;
  1272. const char *e;
  1273. lua_pushinteger(L, i);
  1274. lua_gettable(L, context_order_table);
  1275. lua_gettop(L);
  1276. /* copy the key at the top of the stack for use later */
  1277. lua_pushvalue(L, -1);
  1278. e_index_copy = lua_gettop(L);
  1279. if (!(e = lua_tostring(L, e_index_copy))) {
  1280. lua_pop(L, 2);
  1281. continue;
  1282. }
  1283. /* make sure this is not the 'include' extension */
  1284. if (!strcasecmp(e, "include")) {
  1285. lua_pop(L, 2);
  1286. continue;
  1287. }
  1288. if (func == &matchmore)
  1289. match = ast_extension_close(e, exten, E_MATCHMORE);
  1290. else if (func == &canmatch)
  1291. match = ast_extension_close(e, exten, E_CANMATCH);
  1292. else
  1293. match = ast_extension_match(e, exten);
  1294. /* the extension matching functions return 0 on fail, 1 on
  1295. * match, 2 on earlymatch */
  1296. if (!match) {
  1297. /* pop the copy and the extension */
  1298. lua_pop(L, 2);
  1299. continue; /* keep trying */
  1300. }
  1301. if (func == &matchmore && match == 2) {
  1302. /* We match an extension ending in '!'. The decision in
  1303. * this case is final and counts as no match. */
  1304. lua_pop(L, 4);
  1305. return 0;
  1306. }
  1307. /* remove the context table, the context order table, the
  1308. * extension, and the extension copy (or replace the extension
  1309. * with the corresponding function) */
  1310. if (push_func) {
  1311. lua_pop(L, 1); /* pop the copy */
  1312. lua_gettable(L, context_table);
  1313. lua_insert(L, -3);
  1314. lua_pop(L, 2);
  1315. } else {
  1316. lua_pop(L, 4);
  1317. }
  1318. return 1;
  1319. }
  1320. /* load the includes for this context */
  1321. lua_getfield(L, context_table, "include");
  1322. if (lua_isnil(L, -1)) {
  1323. lua_pop(L, 3);
  1324. return 0;
  1325. }
  1326. /* remove the context and the order table*/
  1327. lua_remove(L, context_order_table);
  1328. lua_remove(L, context_table);
  1329. /* Now try any includes we have in this context */
  1330. for (lua_pushnil(L); lua_next(L, -2); lua_pop(L, 1)) {
  1331. const char *c = lua_tostring(L, -1);
  1332. if (!c)
  1333. continue;
  1334. if (lua_find_extension(L, c, exten, priority, func, push_func)) {
  1335. /* remove the value, the key, and the includes table
  1336. * from the stack. Leave the function behind if
  1337. * necessary */
  1338. if (push_func)
  1339. lua_insert(L, -4);
  1340. lua_pop(L, 3);
  1341. return 1;
  1342. }
  1343. }
  1344. /* pop the includes table */
  1345. lua_pop(L, 1);
  1346. return 0;
  1347. }
  1348. static struct ast_switch lua_switch = {
  1349. .name = "Lua",
  1350. .description = "Lua PBX Switch",
  1351. .exists = exists,
  1352. .canmatch = canmatch,
  1353. .exec = exec,
  1354. .matchmore = matchmore,
  1355. };
  1356. static int load_or_reload_lua_stuff(void)
  1357. {
  1358. int res = AST_MODULE_LOAD_SUCCESS;
  1359. lua_State *L = luaL_newstate();
  1360. if (!L) {
  1361. ast_log(LOG_ERROR, "Error allocating lua_State, no memory\n");
  1362. return AST_MODULE_LOAD_DECLINE;
  1363. }
  1364. if (lua_reload_extensions(L)) {
  1365. const char *error = lua_tostring(L, -1);
  1366. ast_log(LOG_ERROR, "Error loading extensions.lua: %s\n", error);
  1367. res = AST_MODULE_LOAD_DECLINE;
  1368. }
  1369. if (!res) {
  1370. ast_log(LOG_NOTICE, "Lua PBX Switch loaded.\n");
  1371. }
  1372. lua_close(L);
  1373. return res;
  1374. }
  1375. static int unload_module(void)
  1376. {
  1377. ast_context_destroy(NULL, registrar);
  1378. ast_unregister_switch(&lua_switch);
  1379. lua_free_extensions();
  1380. ast_log(LOG_NOTICE, "Lua PBX Switch unloaded.\n");
  1381. return 0;
  1382. }
  1383. static int reload(void)
  1384. {
  1385. return load_or_reload_lua_stuff();
  1386. }
  1387. static int load_module(void)
  1388. {
  1389. int res;
  1390. if ((res = load_or_reload_lua_stuff()))
  1391. return res;
  1392. if (ast_register_switch(&lua_switch)) {
  1393. ast_log(LOG_ERROR, "Unable to register LUA PBX switch\n");
  1394. return AST_MODULE_LOAD_DECLINE;
  1395. }
  1396. return AST_MODULE_LOAD_SUCCESS;
  1397. }
  1398. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS, "Lua PBX Switch",
  1399. .support_level = AST_MODULE_SUPPORT_EXTENDED,
  1400. .load = load_module,
  1401. .unload = unload_module,
  1402. .reload = reload,
  1403. );