pbx_lua.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2007, Digium, Inc.
  5. *
  6. * Matthew Nicholson <mnicholson@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*!
  19. * \file
  20. *
  21. * \author Matthew Nicholson <mnicholson@digium.com>
  22. * \brief Lua PBX Switch
  23. *
  24. */
  25. /*** MODULEINFO
  26. <depend>lua</depend>
  27. <support_level>extended</support_level>
  28. ***/
  29. #include "asterisk.h"
  30. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  31. #include "asterisk/logger.h"
  32. #include "asterisk/channel.h"
  33. #include "asterisk/pbx.h"
  34. #include "asterisk/module.h"
  35. #include "asterisk/cli.h"
  36. #include "asterisk/utils.h"
  37. #include "asterisk/term.h"
  38. #include "asterisk/paths.h"
  39. #include "asterisk/hashtab.h"
  40. #include <lua.h>
  41. #include <lauxlib.h>
  42. #include <lualib.h>
  43. static char *config = "extensions.lua";
  44. static char *registrar = "pbx_lua";
  45. #ifdef LOW_MEMORY
  46. #define LUA_EXT_DATA_SIZE 256
  47. #else
  48. #define LUA_EXT_DATA_SIZE 8192
  49. #endif
  50. #define LUA_BUF_SIZE 4096
  51. /* This value is used by the lua engine to signal that a Goto or dialplan jump
  52. * was detected. Ensure this value does not conflict with any values dialplan
  53. * applications might return */
  54. #define LUA_GOTO_DETECTED 5
  55. static char *lua_read_extensions_file(lua_State *L, long *size);
  56. static int lua_load_extensions(lua_State *L, struct ast_channel *chan);
  57. static int lua_reload_extensions(lua_State *L);
  58. static void lua_free_extensions(void);
  59. static int lua_sort_extensions(lua_State *L);
  60. static int lua_register_switches(lua_State *L);
  61. static int lua_register_hints(lua_State *L);
  62. static int lua_extension_cmp(lua_State *L);
  63. static int lua_find_extension(lua_State *L, const char *context, const char *exten, int priority, ast_switch_f *func, int push_func);
  64. static int lua_pbx_findapp(lua_State *L);
  65. static int lua_pbx_exec(lua_State *L);
  66. static int lua_get_variable_value(lua_State *L);
  67. static int lua_set_variable_value(lua_State *L);
  68. static int lua_get_variable(lua_State *L);
  69. static int lua_set_variable(lua_State *L);
  70. static int lua_func_read(lua_State *L);
  71. static int lua_autoservice_start(lua_State *L);
  72. static int lua_autoservice_stop(lua_State *L);
  73. static int lua_autoservice_status(lua_State *L);
  74. static int lua_check_hangup(lua_State *L);
  75. static int lua_error_function(lua_State *L);
  76. static void lua_update_registry(lua_State *L, const char *context, const char *exten, int priority);
  77. static void lua_push_variable_table(lua_State *L);
  78. static void lua_create_app_table(lua_State *L);
  79. static void lua_create_channel_table(lua_State *L);
  80. static void lua_create_variable_metatable(lua_State *L);
  81. static void lua_create_application_metatable(lua_State *L);
  82. static void lua_create_autoservice_functions(lua_State *L);
  83. static void lua_create_hangup_function(lua_State *L);
  84. static void lua_concat_args(lua_State *L, int start, int nargs);
  85. static void lua_state_destroy(void *data);
  86. static void lua_datastore_fixup(void *data, struct ast_channel *old_chan, struct ast_channel *new_chan);
  87. static lua_State *lua_get_state(struct ast_channel *chan);
  88. static int exists(struct ast_channel *chan, const char *context, const char *exten, int priority, const char *callerid, const char *data);
  89. static int canmatch(struct ast_channel *chan, const char *context, const char *exten, int priority, const char *callerid, const char *data);
  90. static int matchmore(struct ast_channel *chan, const char *context, const char *exten, int priority, const char *callerid, const char *data);
  91. static int exec(struct ast_channel *chan, const char *context, const char *exten, int priority, const char *callerid, const char *data);
  92. AST_MUTEX_DEFINE_STATIC(config_file_lock);
  93. static char *config_file_data = NULL;
  94. static long config_file_size = 0;
  95. static struct ast_context *local_contexts = NULL;
  96. static struct ast_hashtab *local_table = NULL;
  97. static const struct ast_datastore_info lua_datastore = {
  98. .type = "lua",
  99. .destroy = lua_state_destroy,
  100. .chan_fixup = lua_datastore_fixup,
  101. };
  102. /*!
  103. * \brief The destructor for lua_datastore
  104. */
  105. static void lua_state_destroy(void *data)
  106. {
  107. if (data)
  108. lua_close(data);
  109. }
  110. /*!
  111. * \brief The fixup function for the lua_datastore.
  112. * \param data the datastore data, in this case it will be a lua_State
  113. * \param old_chan the channel we are moving from
  114. * \param new_chan the channel we are moving to
  115. *
  116. * This function updates our internal channel pointer.
  117. */
  118. static void lua_datastore_fixup(void *data, struct ast_channel *old_chan, struct ast_channel *new_chan)
  119. {
  120. lua_State *L = data;
  121. lua_pushlightuserdata(L, new_chan);
  122. lua_setfield(L, LUA_REGISTRYINDEX, "channel");
  123. }
  124. /*!
  125. * \brief [lua_CFunction] Find an app and return it in a lua table (for access from lua, don't
  126. * call directly)
  127. *
  128. * This function would be called in the following example as it would be found
  129. * in extensions.lua.
  130. *
  131. * \code
  132. * app.dial
  133. * \endcode
  134. */
  135. static int lua_pbx_findapp(lua_State *L)
  136. {
  137. const char *app_name = luaL_checkstring(L, 2);
  138. lua_newtable(L);
  139. lua_pushstring(L, "name");
  140. lua_pushstring(L, app_name);
  141. lua_settable(L, -3);
  142. luaL_getmetatable(L, "application");
  143. lua_setmetatable(L, -2);
  144. return 1;
  145. }
  146. /*!
  147. * \brief [lua_CFunction] This function is part of the 'application' metatable
  148. * and is used to execute applications similar to pbx_exec() (for access from
  149. * lua, don't call directly)
  150. *
  151. * \param L the lua_State to use
  152. * \return nothing
  153. *
  154. * This funciton is executed as the '()' operator for apps accessed through the
  155. * 'app' table.
  156. *
  157. * \code
  158. * app.playback('demo-congrats')
  159. * \endcode
  160. */
  161. static int lua_pbx_exec(lua_State *L)
  162. {
  163. int res, nargs = lua_gettop(L);
  164. const char *data = "";
  165. char *app_name, *context, *exten;
  166. char tmp[80], tmp2[80], tmp3[LUA_EXT_DATA_SIZE];
  167. int priority, autoservice;
  168. struct ast_app *app;
  169. struct ast_channel *chan;
  170. lua_getfield(L, 1, "name");
  171. app_name = ast_strdupa(lua_tostring(L, -1));
  172. lua_pop(L, 1);
  173. if (!(app = pbx_findapp(app_name))) {
  174. lua_pushstring(L, "application '");
  175. lua_pushstring(L, app_name);
  176. lua_pushstring(L, "' not found");
  177. lua_concat(L, 3);
  178. return lua_error(L);
  179. }
  180. lua_getfield(L, LUA_REGISTRYINDEX, "channel");
  181. chan = lua_touserdata(L, -1);
  182. lua_pop(L, 1);
  183. context = ast_strdupa(ast_channel_context(chan));
  184. exten = ast_strdupa(ast_channel_exten(chan));
  185. priority = ast_channel_priority(chan);
  186. lua_concat_args(L, 2, nargs);
  187. data = lua_tostring(L, -1);
  188. ast_verb(3, "Executing [%s@%s:%d] %s(\"%s\", \"%s\")\n",
  189. exten, context, priority,
  190. term_color(tmp, app_name, COLOR_BRCYAN, 0, sizeof(tmp)),
  191. term_color(tmp2, ast_channel_name(chan), COLOR_BRMAGENTA, 0, sizeof(tmp2)),
  192. term_color(tmp3, data, COLOR_BRMAGENTA, 0, sizeof(tmp3)));
  193. lua_getfield(L, LUA_REGISTRYINDEX, "autoservice");
  194. autoservice = lua_toboolean(L, -1);
  195. lua_pop(L, 1);
  196. if (autoservice)
  197. ast_autoservice_stop(chan);
  198. res = pbx_exec(chan, app, data);
  199. lua_pop(L, 1); /* pop data */
  200. data = "";
  201. if (autoservice)
  202. ast_autoservice_start(chan);
  203. /* error executing an application, report it */
  204. if (res) {
  205. lua_pushinteger(L, res);
  206. return lua_error(L);
  207. }
  208. if (strcmp(context, ast_channel_context(chan))) {
  209. lua_pushstring(L, context);
  210. lua_pushstring(L, ast_channel_context(chan));
  211. lua_pushliteral(L, "context");
  212. } else if (strcmp(exten, ast_channel_exten(chan))) {
  213. lua_pushstring(L, exten);
  214. lua_pushstring(L, ast_channel_exten(chan));
  215. lua_pushliteral(L, "exten");
  216. } else if (priority != ast_channel_priority(chan)) {
  217. lua_pushinteger(L, priority);
  218. lua_pushinteger(L, ast_channel_priority(chan));
  219. lua_pushliteral(L, "priority");
  220. } else {
  221. /* no goto - restore the original position back
  222. * to lua state, in case this was a recursive dialplan
  223. * call (a dialplan application re-entering dialplan) */
  224. lua_update_registry(L, context, exten, priority);
  225. return 0;
  226. }
  227. /* goto detected - construct error message */
  228. lua_insert(L, -3);
  229. lua_pushliteral(L, " changed from ");
  230. lua_insert(L, -3);
  231. lua_pushliteral(L, " to ");
  232. lua_insert(L, -2);
  233. lua_concat(L, 5);
  234. ast_debug(2, "Goto detected: %s\n", lua_tostring(L, -1));
  235. lua_pop(L, 1);
  236. /* let the lua engine know it needs to return control to the pbx */
  237. lua_pushinteger(L, LUA_GOTO_DETECTED);
  238. lua_error(L);
  239. return 0;
  240. }
  241. /*!
  242. * \brief [lua_CFunction] Used to get the value of a variable or dialplan
  243. * function (for access from lua, don't call directly)
  244. *
  245. * The value of the variable or function is returned. This function is the
  246. * 'get()' function in the following example as would be seen in
  247. * extensions.lua.
  248. *
  249. * \code
  250. * channel.variable:get()
  251. * \endcode
  252. */
  253. static int lua_get_variable_value(lua_State *L)
  254. {
  255. struct ast_channel *chan;
  256. char *value = NULL, *name;
  257. char *workspace = ast_alloca(LUA_BUF_SIZE);
  258. int autoservice;
  259. workspace[0] = '\0';
  260. if (!lua_istable(L, 1)) {
  261. lua_pushstring(L, "User probably used '.' instead of ':' for retrieving a channel variable value");
  262. return lua_error(L);
  263. }
  264. lua_getfield(L, LUA_REGISTRYINDEX, "channel");
  265. chan = lua_touserdata(L, -1);
  266. lua_pop(L, 1);
  267. lua_getfield(L, 1, "name");
  268. name = ast_strdupa(lua_tostring(L, -1));
  269. lua_pop(L, 1);
  270. lua_getfield(L, LUA_REGISTRYINDEX, "autoservice");
  271. autoservice = lua_toboolean(L, -1);
  272. lua_pop(L, 1);
  273. if (autoservice)
  274. ast_autoservice_stop(chan);
  275. /* if this is a dialplan function then use ast_func_read(), otherwise
  276. * use pbx_retrieve_variable() */
  277. if (!ast_strlen_zero(name) && name[strlen(name) - 1] == ')') {
  278. value = ast_func_read(chan, name, workspace, LUA_BUF_SIZE) ? NULL : workspace;
  279. } else {
  280. pbx_retrieve_variable(chan, name, &value, workspace, LUA_BUF_SIZE, ast_channel_varshead(chan));
  281. }
  282. if (autoservice)
  283. ast_autoservice_start(chan);
  284. if (value) {
  285. lua_pushstring(L, value);
  286. } else {
  287. lua_pushnil(L);
  288. }
  289. return 1;
  290. }
  291. /*!
  292. * \brief [lua_CFunction] Used to set the value of a variable or dialplan
  293. * function (for access from lua, don't call directly)
  294. *
  295. * This function is the 'set()' function in the following example as would be
  296. * seen in extensions.lua.
  297. *
  298. * \code
  299. * channel.variable:set("value")
  300. * \endcode
  301. */
  302. static int lua_set_variable_value(lua_State *L)
  303. {
  304. const char *name, *value;
  305. struct ast_channel *chan;
  306. int autoservice;
  307. if (!lua_istable(L, 1)) {
  308. lua_pushstring(L, "User probably used '.' instead of ':' for setting a channel variable");
  309. return lua_error(L);
  310. }
  311. lua_getfield(L, 1, "name");
  312. name = ast_strdupa(lua_tostring(L, -1));
  313. lua_pop(L, 1);
  314. value = luaL_checkstring(L, 2);
  315. lua_getfield(L, LUA_REGISTRYINDEX, "channel");
  316. chan = lua_touserdata(L, -1);
  317. lua_pop(L, 1);
  318. lua_getfield(L, LUA_REGISTRYINDEX, "autoservice");
  319. autoservice = lua_toboolean(L, -1);
  320. lua_pop(L, 1);
  321. if (autoservice)
  322. ast_autoservice_stop(chan);
  323. pbx_builtin_setvar_helper(chan, name, value);
  324. if (autoservice)
  325. ast_autoservice_start(chan);
  326. return 0;
  327. }
  328. /*!
  329. * \brief Update the lua registry with the given context, exten, and priority.
  330. *
  331. * \param L the lua_State to use
  332. * \param context the new context
  333. * \param exten the new exten
  334. * \param priority the new priority
  335. */
  336. static void lua_update_registry(lua_State *L, const char *context, const char *exten, int priority)
  337. {
  338. lua_pushstring(L, context);
  339. lua_setfield(L, LUA_REGISTRYINDEX, "context");
  340. lua_pushstring(L, exten);
  341. lua_setfield(L, LUA_REGISTRYINDEX, "exten");
  342. lua_pushinteger(L, priority);
  343. lua_setfield(L, LUA_REGISTRYINDEX, "priority");
  344. }
  345. /*!
  346. * \brief Push a 'variable' table on the stack for access the channel variable
  347. * with the given name.
  348. *
  349. * The value on the top of the stack is popped and used as the name.
  350. *
  351. * \param L the lua_State to use
  352. * \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, ast_channel_varshead(chan));
  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. #if LUA_VERSION_NUM < 502
  732. lua_pushinteger(L, lua_objlen(L, context_order) + 1);
  733. #else
  734. lua_pushinteger(L, lua_rawlen(L, context_order) + 1);
  735. #endif
  736. lua_pushvalue(L, exten);
  737. lua_settable(L, context_order);
  738. }
  739. lua_settable(L, extensions_order); /* put the context_order table in the extensions_order table */
  740. /* now sort the new table */
  741. /* push the table.sort function */
  742. lua_getglobal(L, "table");
  743. lua_getfield(L, -1, "sort");
  744. lua_remove(L, -2); /* remove the 'table' table */
  745. /* push the context_order table */
  746. lua_pushvalue(L, context_name);
  747. lua_gettable(L, extensions_order);
  748. /* push the comp function */
  749. lua_pushcfunction(L, &lua_extension_cmp);
  750. if (lua_pcall(L, 2, 0, 0)) {
  751. lua_insert(L, -5);
  752. lua_pop(L, 4);
  753. return 1;
  754. }
  755. }
  756. /* remove the extensions table and the extensions_order table */
  757. lua_pop(L, 2);
  758. return 0;
  759. }
  760. /*!
  761. * \brief Register dialplan switches for our pbx_lua contexs.
  762. *
  763. * In the event of an error, an error string will be pushed onto the lua stack.
  764. *
  765. * \retval 0 success
  766. * \retval 1 failure
  767. */
  768. static int lua_register_switches(lua_State *L)
  769. {
  770. int extensions;
  771. struct ast_context *con = NULL;
  772. /* create the hash table for our contexts */
  773. /* XXX do we ever need to destroy this? pbx_config does not */
  774. if (!local_table)
  775. local_table = ast_hashtab_create(17, ast_hashtab_compare_contexts, ast_hashtab_resize_java, ast_hashtab_newsize_java, ast_hashtab_hash_contexts, 0);
  776. /* load the 'extensions' table */
  777. lua_getglobal(L, "extensions");
  778. extensions = lua_gettop(L);
  779. if (lua_isnil(L, -1)) {
  780. lua_pop(L, 1);
  781. lua_pushstring(L, "Unable to find 'extensions' table in extensions.lua\n");
  782. return 1;
  783. }
  784. /* iterate through the extensions table and register a context and
  785. * dialplan switch for each lua context
  786. */
  787. for (lua_pushnil(L); lua_next(L, extensions); lua_pop(L, 1)) {
  788. int context = lua_gettop(L);
  789. int context_name = context - 1;
  790. const char *context_str = lua_tostring(L, context_name);
  791. /* find or create this context */
  792. con = ast_context_find_or_create(&local_contexts, local_table, context_str, registrar);
  793. if (!con) {
  794. /* remove extensions table and context key and value */
  795. lua_pop(L, 3);
  796. lua_pushstring(L, "Failed to find or create context\n");
  797. return 1;
  798. }
  799. /* register the switch */
  800. if (ast_context_add_switch2(con, "Lua", "", 0, registrar)) {
  801. /* remove extensions table and context key and value */
  802. lua_pop(L, 3);
  803. lua_pushstring(L, "Unable to create switch for context\n");
  804. return 1;
  805. }
  806. }
  807. /* remove the extensions table */
  808. lua_pop(L, 1);
  809. return 0;
  810. }
  811. /*!
  812. * \brief Register dialplan hints for our pbx_lua contexs.
  813. *
  814. * In the event of an error, an error string will be pushed onto the lua stack.
  815. *
  816. * \retval 0 success
  817. * \retval 1 failure
  818. */
  819. static int lua_register_hints(lua_State *L)
  820. {
  821. int hints;
  822. struct ast_context *con = NULL;
  823. /* create the hash table for our contexts */
  824. /* XXX do we ever need to destroy this? pbx_config does not */
  825. if (!local_table)
  826. local_table = ast_hashtab_create(17, ast_hashtab_compare_contexts, ast_hashtab_resize_java, ast_hashtab_newsize_java, ast_hashtab_hash_contexts, 0);
  827. /* load the 'hints' table */
  828. lua_getglobal(L, "hints");
  829. hints = lua_gettop(L);
  830. if (lua_isnil(L, -1)) {
  831. /* hints table not found, move along */
  832. lua_pop(L, 1);
  833. return 0;
  834. }
  835. /* iterate through the hints table and register each context and
  836. * the hints that go along with it
  837. */
  838. for (lua_pushnil(L); lua_next(L, hints); lua_pop(L, 1)) {
  839. int context = lua_gettop(L);
  840. int context_name = context - 1;
  841. const char *context_str = lua_tostring(L, context_name);
  842. /* find or create this context */
  843. con = ast_context_find_or_create(&local_contexts, local_table, context_str, registrar);
  844. if (!con) {
  845. /* remove hints table and context key and value */
  846. lua_pop(L, 3);
  847. lua_pushstring(L, "Failed to find or create context\n");
  848. return 1;
  849. }
  850. /* register each hint */
  851. for (lua_pushnil(L); lua_next(L, context); lua_pop(L, 1)) {
  852. const char *hint_value = lua_tostring(L, -1);
  853. const char *hint_name;
  854. /* the hint value is not a string, ignore it */
  855. if (!hint_value) {
  856. continue;
  857. }
  858. /* copy the name then convert it to a string */
  859. lua_pushvalue(L, -2);
  860. if (!(hint_name = lua_tostring(L, -1))) {
  861. /* ignore non-string value */
  862. lua_pop(L, 1);
  863. continue;
  864. }
  865. if (ast_add_extension2(con, 0, hint_name, PRIORITY_HINT, NULL, NULL, hint_value, NULL, NULL, registrar)) {
  866. /* remove hints table, hint name, hint value,
  867. * key copy, context name, and contex table */
  868. lua_pop(L, 6);
  869. lua_pushstring(L, "Error creating hint\n");
  870. return 1;
  871. }
  872. /* pop the name copy */
  873. lua_pop(L, 1);
  874. }
  875. }
  876. /* remove the hints table */
  877. lua_pop(L, 1);
  878. return 0;
  879. }
  880. /*!
  881. * \brief [lua_CFunction] Compare two extensions (for access from lua, don't
  882. * call directly)
  883. *
  884. * This function returns true if the first extension passed should match after
  885. * the second. It behaves like the '<' operator.
  886. */
  887. static int lua_extension_cmp(lua_State *L)
  888. {
  889. const char *a = luaL_checkstring(L, -2);
  890. const char *b = luaL_checkstring(L, -1);
  891. if (ast_extension_cmp(a, b) == -1)
  892. lua_pushboolean(L, 1);
  893. else
  894. lua_pushboolean(L, 0);
  895. return 1;
  896. }
  897. /*!
  898. * \brief Load the extensions.lua file in to a buffer and execute the file
  899. *
  900. * \param L the lua_State to use
  901. * \param size a pointer to store the size of the buffer
  902. *
  903. * \note The caller is expected to free the buffer at some point.
  904. *
  905. * \return a pointer to the buffer
  906. */
  907. static char *lua_read_extensions_file(lua_State *L, long *size)
  908. {
  909. FILE *f;
  910. int error_func;
  911. char *data;
  912. char *path = ast_alloca(strlen(config) + strlen(ast_config_AST_CONFIG_DIR) + 2);
  913. sprintf(path, "%s/%s", ast_config_AST_CONFIG_DIR, config);
  914. if (!(f = fopen(path, "r"))) {
  915. lua_pushstring(L, "cannot open '");
  916. lua_pushstring(L, path);
  917. lua_pushstring(L, "' for reading: ");
  918. lua_pushstring(L, strerror(errno));
  919. lua_concat(L, 4);
  920. return NULL;
  921. }
  922. if (fseek(f, 0l, SEEK_END)) {
  923. fclose(f);
  924. lua_pushliteral(L, "error determining the size of the config file");
  925. return NULL;
  926. }
  927. *size = ftell(f);
  928. if (fseek(f, 0l, SEEK_SET)) {
  929. *size = 0;
  930. fclose(f);
  931. lua_pushliteral(L, "error reading config file");
  932. return NULL;
  933. }
  934. if (!(data = ast_malloc(*size))) {
  935. *size = 0;
  936. fclose(f);
  937. lua_pushstring(L, "not enough memory");
  938. return NULL;
  939. }
  940. if (fread(data, sizeof(char), *size, f) != *size) {
  941. *size = 0;
  942. fclose(f);
  943. lua_pushliteral(L, "problem reading configuration file");
  944. return NULL;
  945. }
  946. fclose(f);
  947. lua_pushcfunction(L, &lua_error_function);
  948. error_func = lua_gettop(L);
  949. if (luaL_loadbuffer(L, data, *size, "extensions.lua")
  950. || lua_pcall(L, 0, LUA_MULTRET, error_func)
  951. || lua_sort_extensions(L)
  952. || lua_register_switches(L)
  953. || lua_register_hints(L)) {
  954. ast_free(data);
  955. data = NULL;
  956. *size = 0;
  957. }
  958. lua_remove(L, error_func);
  959. return data;
  960. }
  961. /*!
  962. * \brief Load the extensions.lua file from the internal buffer
  963. *
  964. * \param L the lua_State to use
  965. * \param chan channel to work on
  966. *
  967. * This function also sets up some constructs used by the extensions.lua file.
  968. * In the event of an error, an error string will be pushed onto the lua stack.
  969. *
  970. * \retval 0 success
  971. * \retval 1 failure
  972. */
  973. static int lua_load_extensions(lua_State *L, struct ast_channel *chan)
  974. {
  975. /* store a pointer to this channel */
  976. lua_pushlightuserdata(L, chan);
  977. lua_setfield(L, LUA_REGISTRYINDEX, "channel");
  978. luaL_openlibs(L);
  979. /* load and sort extensions */
  980. ast_mutex_lock(&config_file_lock);
  981. if (luaL_loadbuffer(L, config_file_data, config_file_size, "extensions.lua")
  982. || lua_pcall(L, 0, LUA_MULTRET, 0)
  983. || lua_sort_extensions(L)) {
  984. ast_mutex_unlock(&config_file_lock);
  985. return 1;
  986. }
  987. ast_mutex_unlock(&config_file_lock);
  988. /* now we setup special tables and functions */
  989. lua_create_app_table(L);
  990. lua_create_channel_table(L);
  991. lua_create_variable_metatable(L);
  992. lua_create_application_metatable(L);
  993. lua_create_autoservice_functions(L);
  994. lua_create_hangup_function(L);
  995. return 0;
  996. }
  997. /*!
  998. * \brief Reload the extensions file and update the internal buffers if it
  999. * loads correctly.
  1000. *
  1001. * \warning This function should not be called on a lua_State returned from
  1002. * lua_get_state().
  1003. *
  1004. * \param L the lua_State to use (must be freshly allocated with
  1005. * luaL_newstate(), don't use lua_get_state())
  1006. */
  1007. static int lua_reload_extensions(lua_State *L)
  1008. {
  1009. long size = 0;
  1010. char *data = NULL;
  1011. luaL_openlibs(L);
  1012. if (!(data = lua_read_extensions_file(L, &size))) {
  1013. return 1;
  1014. }
  1015. ast_mutex_lock(&config_file_lock);
  1016. if (config_file_data)
  1017. ast_free(config_file_data);
  1018. config_file_data = data;
  1019. config_file_size = size;
  1020. /* merge our new contexts */
  1021. ast_merge_contexts_and_delete(&local_contexts, local_table, registrar);
  1022. /* merge_contexts_and_delete will actually, at the correct moment,
  1023. set the global dialplan pointers to your local_contexts and local_table.
  1024. It then will free up the old tables itself. Just be sure not to
  1025. hang onto the pointers. */
  1026. local_table = NULL;
  1027. local_contexts = NULL;
  1028. ast_mutex_unlock(&config_file_lock);
  1029. return 0;
  1030. }
  1031. /*!
  1032. * \brief Free the internal extensions buffer.
  1033. */
  1034. static void lua_free_extensions()
  1035. {
  1036. ast_mutex_lock(&config_file_lock);
  1037. config_file_size = 0;
  1038. ast_free(config_file_data);
  1039. ast_mutex_unlock(&config_file_lock);
  1040. }
  1041. /*!
  1042. * \brief Get the lua_State for this channel
  1043. *
  1044. * If no channel is passed then a new state is allocated. States with no
  1045. * channel assocatied with them should only be used for matching extensions.
  1046. * If the channel does not yet have a lua state associated with it, one will be
  1047. * created.
  1048. *
  1049. * \note If no channel was passed then the caller is expected to free the state
  1050. * using lua_close().
  1051. *
  1052. * \return a lua_State
  1053. */
  1054. static lua_State *lua_get_state(struct ast_channel *chan)
  1055. {
  1056. struct ast_datastore *datastore = NULL;
  1057. lua_State *L;
  1058. if (!chan) {
  1059. L = luaL_newstate();
  1060. if (!L) {
  1061. ast_log(LOG_ERROR, "Error allocating lua_State, no memory\n");
  1062. return NULL;
  1063. }
  1064. if (lua_load_extensions(L, NULL)) {
  1065. const char *error = lua_tostring(L, -1);
  1066. ast_log(LOG_ERROR, "Error loading extensions.lua: %s\n", error);
  1067. lua_close(L);
  1068. return NULL;
  1069. }
  1070. return L;
  1071. } else {
  1072. ast_channel_lock(chan);
  1073. datastore = ast_channel_datastore_find(chan, &lua_datastore, NULL);
  1074. ast_channel_unlock(chan);
  1075. if (!datastore) {
  1076. /* nothing found, allocate a new lua state */
  1077. datastore = ast_datastore_alloc(&lua_datastore, NULL);
  1078. if (!datastore) {
  1079. ast_log(LOG_ERROR, "Error allocation channel datastore for lua_State\n");
  1080. return NULL;
  1081. }
  1082. datastore->data = luaL_newstate();
  1083. if (!datastore->data) {
  1084. ast_datastore_free(datastore);
  1085. ast_log(LOG_ERROR, "Error allocating lua_State, no memory\n");
  1086. return NULL;
  1087. }
  1088. ast_channel_lock(chan);
  1089. ast_channel_datastore_add(chan, datastore);
  1090. ast_channel_unlock(chan);
  1091. L = datastore->data;
  1092. if (lua_load_extensions(L, chan)) {
  1093. const char *error = lua_tostring(L, -1);
  1094. ast_log(LOG_ERROR, "Error loading extensions.lua for %s: %s\n", ast_channel_name(chan), error);
  1095. ast_channel_lock(chan);
  1096. ast_channel_datastore_remove(chan, datastore);
  1097. ast_channel_unlock(chan);
  1098. ast_datastore_free(datastore);
  1099. return NULL;
  1100. }
  1101. }
  1102. return datastore->data;
  1103. }
  1104. }
  1105. static int exists(struct ast_channel *chan, const char *context, const char *exten, int priority, const char *callerid, const char *data)
  1106. {
  1107. int res;
  1108. lua_State *L;
  1109. struct ast_module_user *u = ast_module_user_add(chan);
  1110. if (!u) {
  1111. ast_log(LOG_ERROR, "Error adjusting use count, probably could not allocate memory\n");
  1112. return 0;
  1113. }
  1114. L = lua_get_state(chan);
  1115. if (!L) {
  1116. ast_module_user_remove(u);
  1117. return 0;
  1118. }
  1119. res = lua_find_extension(L, context, exten, priority, &exists, 0);
  1120. if (!chan) lua_close(L);
  1121. ast_module_user_remove(u);
  1122. return res;
  1123. }
  1124. static int canmatch(struct ast_channel *chan, const char *context, const char *exten, int priority, const char *callerid, const char *data)
  1125. {
  1126. int res;
  1127. lua_State *L;
  1128. struct ast_module_user *u = ast_module_user_add(chan);
  1129. if (!u) {
  1130. ast_log(LOG_ERROR, "Error adjusting use count, probably could not allocate memory\n");
  1131. return 0;
  1132. }
  1133. L = lua_get_state(chan);
  1134. if (!L) {
  1135. ast_module_user_remove(u);
  1136. return 0;
  1137. }
  1138. res = lua_find_extension(L, context, exten, priority, &canmatch, 0);
  1139. if (!chan) lua_close(L);
  1140. ast_module_user_remove(u);
  1141. return res;
  1142. }
  1143. static int matchmore(struct ast_channel *chan, const char *context, const char *exten, int priority, const char *callerid, const char *data)
  1144. {
  1145. int res;
  1146. lua_State *L;
  1147. struct ast_module_user *u = ast_module_user_add(chan);
  1148. if (!u) {
  1149. ast_log(LOG_ERROR, "Error adjusting use count, probably could not allocate memory\n");
  1150. return 0;
  1151. }
  1152. L = lua_get_state(chan);
  1153. if (!L) {
  1154. ast_module_user_remove(u);
  1155. return 0;
  1156. }
  1157. res = lua_find_extension(L, context, exten, priority, &matchmore, 0);
  1158. if (!chan) lua_close(L);
  1159. ast_module_user_remove(u);
  1160. return res;
  1161. }
  1162. static int exec(struct ast_channel *chan, const char *context, const char *exten, int priority, const char *callerid, const char *data)
  1163. {
  1164. int res, error_func;
  1165. lua_State *L;
  1166. struct ast_module_user *u = ast_module_user_add(chan);
  1167. if (!u) {
  1168. ast_log(LOG_ERROR, "Error adjusting use count, probably could not allocate memory\n");
  1169. return -1;
  1170. }
  1171. L = lua_get_state(chan);
  1172. if (!L) {
  1173. ast_module_user_remove(u);
  1174. return -1;
  1175. }
  1176. lua_pushcfunction(L, &lua_error_function);
  1177. error_func = lua_gettop(L);
  1178. /* push the extension function onto the stack */
  1179. if (!lua_find_extension(L, context, exten, priority, &exists, 1)) {
  1180. lua_pop(L, 1); /* pop the debug function */
  1181. ast_log(LOG_ERROR, "Could not find extension %s in context %s\n", exten, context);
  1182. if (!chan) lua_close(L);
  1183. ast_module_user_remove(u);
  1184. return -1;
  1185. }
  1186. lua_getfield(L, LUA_REGISTRYINDEX, "autoservice");
  1187. if (lua_toboolean(L, -1)) {
  1188. ast_autoservice_start(chan);
  1189. }
  1190. lua_pop(L, 1);
  1191. lua_update_registry(L, context, exten, priority);
  1192. lua_pushstring(L, context);
  1193. lua_pushstring(L, exten);
  1194. res = lua_pcall(L, 2, 0, error_func);
  1195. if (res) {
  1196. if (res == LUA_ERRRUN) {
  1197. res = -1;
  1198. if (lua_isnumber(L, -1)) {
  1199. res = lua_tointeger(L, -1);
  1200. if (res == LUA_GOTO_DETECTED) {
  1201. res = 0;
  1202. }
  1203. } else if (lua_isstring(L, -1)) {
  1204. const char *error = lua_tostring(L, -1);
  1205. ast_log(LOG_ERROR, "Error executing lua extension: %s\n", error);
  1206. }
  1207. } else if (res == LUA_ERRERR) {
  1208. res = -1;
  1209. ast_log(LOG_ERROR, "Error in the lua error handler (this is probably a bug in pbx_lua)\n");
  1210. } else if (res == LUA_ERRMEM) {
  1211. res = -1;
  1212. ast_log(LOG_ERROR, "Memory allocation error\n");
  1213. }
  1214. lua_pop(L, 1);
  1215. }
  1216. lua_remove(L, error_func);
  1217. lua_getfield(L, LUA_REGISTRYINDEX, "autoservice");
  1218. if (lua_toboolean(L, -1)) {
  1219. ast_autoservice_stop(chan);
  1220. }
  1221. lua_pop(L, 1);
  1222. if (!chan) lua_close(L);
  1223. ast_module_user_remove(u);
  1224. return res;
  1225. }
  1226. /*!
  1227. * \brief Locate an extensions and optionally push the matching function on the
  1228. * stack
  1229. *
  1230. * \param L the lua_State to use
  1231. * \param context the context to look in
  1232. * \param exten the extension to look up
  1233. * \param priority the priority to check, '1' is the only valid priority
  1234. * \param func the calling func, used to adjust matching behavior between,
  1235. * match, canmatch, and matchmore
  1236. * \param push_func whether or not to push the lua function for the given
  1237. * extension onto the stack
  1238. */
  1239. static int lua_find_extension(lua_State *L, const char *context, const char *exten, int priority, ast_switch_f *func, int push_func)
  1240. {
  1241. int context_table, context_order_table, i;
  1242. ast_debug(2, "Looking up %s@%s:%i\n", exten, context, priority);
  1243. if (priority != 1)
  1244. return 0;
  1245. /* load the 'extensions' table */
  1246. lua_getglobal(L, "extensions");
  1247. if (lua_isnil(L, -1)) {
  1248. ast_log(LOG_ERROR, "Unable to find 'extensions' table in extensions.lua\n");
  1249. lua_pop(L, 1);
  1250. return 0;
  1251. }
  1252. /* load the given context */
  1253. lua_getfield(L, -1, context);
  1254. if (lua_isnil(L, -1)) {
  1255. lua_pop(L, 2);
  1256. return 0;
  1257. }
  1258. /* remove the extensions table */
  1259. lua_remove(L, -2);
  1260. context_table = lua_gettop(L);
  1261. /* load the extensions order table for this context */
  1262. lua_getfield(L, LUA_REGISTRYINDEX, "extensions_order");
  1263. lua_getfield(L, -1, context);
  1264. lua_remove(L, -2); /* remove the extensions order table */
  1265. context_order_table = lua_gettop(L);
  1266. /* step through the extensions looking for a match */
  1267. #if LUA_VERSION_NUM < 502
  1268. for (i = 1; i < lua_objlen(L, context_order_table) + 1; i++) {
  1269. #else
  1270. for (i = 1; i < lua_rawlen(L, context_order_table) + 1; i++) {
  1271. #endif
  1272. int e_index_copy, match = 0;
  1273. const char *e;
  1274. lua_pushinteger(L, i);
  1275. lua_gettable(L, context_order_table);
  1276. lua_gettop(L);
  1277. /* copy the key at the top of the stack for use later */
  1278. lua_pushvalue(L, -1);
  1279. e_index_copy = lua_gettop(L);
  1280. if (!(e = lua_tostring(L, e_index_copy))) {
  1281. lua_pop(L, 2);
  1282. continue;
  1283. }
  1284. /* make sure this is not the 'include' extension */
  1285. if (!strcasecmp(e, "include")) {
  1286. lua_pop(L, 2);
  1287. continue;
  1288. }
  1289. if (func == &matchmore)
  1290. match = ast_extension_close(e, exten, E_MATCHMORE);
  1291. else if (func == &canmatch)
  1292. match = ast_extension_close(e, exten, E_CANMATCH);
  1293. else
  1294. match = ast_extension_match(e, exten);
  1295. /* the extension matching functions return 0 on fail, 1 on
  1296. * match, 2 on earlymatch */
  1297. if (!match) {
  1298. /* pop the copy and the extension */
  1299. lua_pop(L, 2);
  1300. continue; /* keep trying */
  1301. }
  1302. if (func == &matchmore && match == 2) {
  1303. /* We match an extension ending in '!'. The decision in
  1304. * this case is final and counts as no match. */
  1305. lua_pop(L, 4);
  1306. return 0;
  1307. }
  1308. /* remove the context table, the context order table, the
  1309. * extension, and the extension copy (or replace the extension
  1310. * with the corresponding function) */
  1311. if (push_func) {
  1312. lua_pop(L, 1); /* pop the copy */
  1313. lua_gettable(L, context_table);
  1314. lua_insert(L, -3);
  1315. lua_pop(L, 2);
  1316. } else {
  1317. lua_pop(L, 4);
  1318. }
  1319. return 1;
  1320. }
  1321. /* load the includes for this context */
  1322. lua_getfield(L, context_table, "include");
  1323. if (lua_isnil(L, -1)) {
  1324. lua_pop(L, 3);
  1325. return 0;
  1326. }
  1327. /* remove the context and the order table*/
  1328. lua_remove(L, context_order_table);
  1329. lua_remove(L, context_table);
  1330. /* Now try any includes we have in this context */
  1331. for (lua_pushnil(L); lua_next(L, -2); lua_pop(L, 1)) {
  1332. const char *c = lua_tostring(L, -1);
  1333. if (!c)
  1334. continue;
  1335. if (lua_find_extension(L, c, exten, priority, func, push_func)) {
  1336. /* remove the value, the key, and the includes table
  1337. * from the stack. Leave the function behind if
  1338. * necessary */
  1339. if (push_func)
  1340. lua_insert(L, -4);
  1341. lua_pop(L, 3);
  1342. return 1;
  1343. }
  1344. }
  1345. /* pop the includes table */
  1346. lua_pop(L, 1);
  1347. return 0;
  1348. }
  1349. static struct ast_switch lua_switch = {
  1350. .name = "Lua",
  1351. .description = "Lua PBX Switch",
  1352. .exists = exists,
  1353. .canmatch = canmatch,
  1354. .exec = exec,
  1355. .matchmore = matchmore,
  1356. };
  1357. static int load_or_reload_lua_stuff(void)
  1358. {
  1359. int res = AST_MODULE_LOAD_SUCCESS;
  1360. lua_State *L = luaL_newstate();
  1361. if (!L) {
  1362. ast_log(LOG_ERROR, "Error allocating lua_State, no memory\n");
  1363. return AST_MODULE_LOAD_DECLINE;
  1364. }
  1365. if (lua_reload_extensions(L)) {
  1366. const char *error = lua_tostring(L, -1);
  1367. ast_log(LOG_ERROR, "Error loading extensions.lua: %s\n", error);
  1368. res = AST_MODULE_LOAD_DECLINE;
  1369. }
  1370. if (!res) {
  1371. ast_log(LOG_NOTICE, "Lua PBX Switch loaded.\n");
  1372. }
  1373. lua_close(L);
  1374. return res;
  1375. }
  1376. static int unload_module(void)
  1377. {
  1378. ast_context_destroy(NULL, registrar);
  1379. ast_unregister_switch(&lua_switch);
  1380. lua_free_extensions();
  1381. ast_log(LOG_NOTICE, "Lua PBX Switch unloaded.\n");
  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. );