pbx_lua.c 42 KB

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