pbx_lua.c 39 KB

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