pbx_lua.c 42 KB

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