| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
#include "asterisk.h" |
| 33 |
|
| 34 |
#include "asterisk/logger.h" |
| 35 |
#include "asterisk/channel.h" |
| 36 |
#include "asterisk/pbx.h" |
| 37 |
#include "asterisk/module.h" |
| 38 |
#include "asterisk/cli.h" |
| 39 |
#include "asterisk/utils.h" |
| 40 |
#include "asterisk/term.h" |
| 41 |
#include "asterisk/paths.h" |
| 42 |
#include "asterisk/hashtab.h" |
| 43 |
|
| 44 |
#include <lua.h> |
| 45 |
#include <lauxlib.h> |
| 46 |
#include <lualib.h> |
| 47 |
|
| 48 |
static const char *config = "extensions.lua"; |
| 49 |
static const char *registrar = "pbx_lua"; |
| 50 |
|
| 51 |
#ifdef LOW_MEMORY |
| 52 |
#define LUA_EXT_DATA_SIZE 256 |
| 53 |
#else |
| 54 |
#define LUA_EXT_DATA_SIZE 8192 |
| 55 |
#endif |
| 56 |
#define LUA_BUF_SIZE 4096 |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
#define LUA_GOTO_DETECTED 5 |
| 62 |
|
| 63 |
static char *lua_read_extensions_file(lua_State *L, size_t *size, int *file_not_openable); |
| 64 |
static int lua_load_extensions(lua_State *L, struct ast_channel *chan); |
| 65 |
static int lua_reload_extensions(lua_State *L); |
| 66 |
static void lua_free_extensions(void); |
| 67 |
static int lua_sort_extensions(lua_State *L); |
| 68 |
static int lua_register_switches(lua_State *L); |
| 69 |
static int lua_register_hints(lua_State *L); |
| 70 |
static int lua_extension_cmp(lua_State *L); |
| 71 |
static int lua_find_extension(lua_State *L, const char *context, const char *exten, int priority, ast_switch_f *func, int push_func); |
| 72 |
static int lua_pbx_findapp(lua_State *L); |
| 73 |
static int lua_pbx_exec(lua_State *L); |
| 74 |
|
| 75 |
static int lua_get_variable_value(lua_State *L); |
| 76 |
static int lua_set_variable_value(lua_State *L); |
| 77 |
static int lua_get_variable(lua_State *L); |
| 78 |
static int lua_set_variable(lua_State *L); |
| 79 |
static int lua_func_read(lua_State *L); |
| 80 |
|
| 81 |
static int lua_autoservice_start(lua_State *L); |
| 82 |
static int lua_autoservice_stop(lua_State *L); |
| 83 |
static int lua_autoservice_status(lua_State *L); |
| 84 |
static int lua_check_hangup(lua_State *L); |
| 85 |
static int lua_error_function(lua_State *L); |
| 86 |
|
| 87 |
static void lua_update_registry(lua_State *L, const char *context, const char *exten, int priority); |
| 88 |
static void lua_push_variable_table(lua_State *L); |
| 89 |
static void lua_create_app_table(lua_State *L); |
| 90 |
static void lua_create_channel_table(lua_State *L); |
| 91 |
static void lua_create_variable_metatable(lua_State *L); |
| 92 |
static void lua_create_application_metatable(lua_State *L); |
| 93 |
static void lua_create_autoservice_functions(lua_State *L); |
| 94 |
static void lua_create_hangup_function(lua_State *L); |
| 95 |
static void lua_concat_args(lua_State *L, int start, int nargs); |
| 96 |
|
| 97 |
static void lua_state_destroy(void *data); |
| 98 |
static void lua_datastore_fixup(void *data, struct ast_channel *old_chan, struct ast_channel *new_chan); |
| 99 |
static lua_State *lua_get_state(struct ast_channel *chan); |
| 100 |
|
| 101 |
static int exists(struct ast_channel *chan, const char *context, const char *exten, int priority, const char *callerid, const char *data); |
| 102 |
static int canmatch(struct ast_channel *chan, const char *context, const char *exten, int priority, const char *callerid, const char *data); |
| 103 |
static int matchmore(struct ast_channel *chan, const char *context, const char *exten, int priority, const char *callerid, const char *data); |
| 104 |
static int exec(struct ast_channel *chan, const char *context, const char *exten, int priority, const char *callerid, const char *data); |
| 105 |
|
| 106 |
AST_MUTEX_DEFINE_STATIC(config_file_lock); |
| 107 |
static char *config_file_data = NULL; |
| 108 |
static size_t config_file_size = 0; |
| 109 |
|
| 110 |
static struct ast_context *local_contexts = NULL; |
| 111 |
static struct ast_hashtab *local_table = NULL; |
| 112 |
|
| 113 |
static const struct ast_datastore_info lua_datastore = { |
| 114 |
.type = "lua", |
| 115 |
.destroy = lua_state_destroy, |
| 116 |
.chan_fixup = lua_datastore_fixup, |
| 117 |
}; |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
static void lua_state_destroy(void *data) |
| 124 |
{ |
| 125 |
if (data) |
| 126 |
lua_close(data); |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
static void lua_datastore_fixup(void *data, struct ast_channel *old_chan, struct ast_channel *new_chan) |
| 138 |
{ |
| 139 |
lua_State *L = data; |
| 140 |
lua_pushlightuserdata(L, new_chan); |
| 141 |
lua_setfield(L, LUA_REGISTRYINDEX, "channel"); |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
static int lua_pbx_findapp(lua_State *L) |
| 156 |
{ |
| 157 |
const char *app_name = luaL_checkstring(L, 2); |
| 158 |
|
| 159 |
lua_newtable(L); |
| 160 |
|
| 161 |
lua_pushstring(L, "name"); |
| 162 |
lua_pushstring(L, app_name); |
| 163 |
lua_settable(L, -3); |
| 164 |
|
| 165 |
luaL_getmetatable(L, "application"); |
| 166 |
lua_setmetatable(L, -2); |
| 167 |
|
| 168 |
return 1; |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
static int lua_pbx_exec(lua_State *L) |
| 188 |
{ |
| 189 |
int res, nargs = lua_gettop(L); |
| 190 |
const char *data = ""; |
| 191 |
char *app_name, *context, *exten; |
| 192 |
char tmp[80], tmp2[80], tmp3[LUA_EXT_DATA_SIZE]; |
| 193 |
int priority, autoservice; |
| 194 |
struct ast_app *app; |
| 195 |
struct ast_channel *chan; |
| 196 |
|
| 197 |
lua_getfield(L, 1, "name"); |
| 198 |
app_name = ast_strdupa(lua_tostring(L, -1)); |
| 199 |
lua_pop(L, 1); |
| 200 |
|
| 201 |
if (!(app = pbx_findapp(app_name))) { |
| 202 |
lua_pushstring(L, "application '"); |
| 203 |
lua_pushstring(L, app_name); |
| 204 |
lua_pushstring(L, "' not found"); |
| 205 |
lua_concat(L, 3); |
| 206 |
return lua_error(L); |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
lua_getfield(L, LUA_REGISTRYINDEX, "channel"); |
| 211 |
chan = lua_touserdata(L, -1); |
| 212 |
lua_pop(L, 1); |
| 213 |
|
| 214 |
context = ast_strdupa(ast_channel_context(chan)); |
| 215 |
exten = ast_strdupa(ast_channel_exten(chan)); |
| 216 |
priority = ast_channel_priority(chan); |
| 217 |
|
| 218 |
lua_concat_args(L, 2, nargs); |
| 219 |
data = lua_tostring(L, -1); |
| 220 |
|
| 221 |
ast_verb(3, "Executing [%s@%s:%d] %s(\"%s\", \"%s\")\n", |
| 222 |
exten, context, priority, |
| 223 |
term_color(tmp, app_name, COLOR_BRCYAN, 0, sizeof(tmp)), |
| 224 |
term_color(tmp2, ast_channel_name(chan), COLOR_BRMAGENTA, 0, sizeof(tmp2)), |
| 225 |
term_color(tmp3, data ? data : "", COLOR_BRMAGENTA, 0, sizeof(tmp3))); |
| 226 |
|
| 227 |
lua_getfield(L, LUA_REGISTRYINDEX, "autoservice"); |
| 228 |
autoservice = lua_toboolean(L, -1); |
| 229 |
lua_pop(L, 1); |
| 230 |
|
| 231 |
if (autoservice) |
| 232 |
ast_autoservice_stop(chan); |
| 233 |
|
| 234 |
res = pbx_exec(chan, app, data); |
| 235 |
|
| 236 |
lua_pop(L, 1); |
| 237 |
data = ""; |
| 238 |
|
| 239 |
if (autoservice) |
| 240 |
ast_autoservice_start(chan); |
| 241 |
|
| 242 |
|
| 243 |
if (res) { |
| 244 |
lua_pushinteger(L, res); |
| 245 |
return lua_error(L); |
| 246 |
} |
| 247 |
|
| 248 |
if (strcmp(context, ast_channel_context(chan))) { |
| 249 |
lua_pushstring(L, context); |
| 250 |
lua_pushstring(L, ast_channel_context(chan)); |
| 251 |
lua_pushliteral(L, "context"); |
| 252 |
} else if (strcmp(exten, ast_channel_exten(chan))) { |
| 253 |
lua_pushstring(L, exten); |
| 254 |
lua_pushstring(L, ast_channel_exten(chan)); |
| 255 |
lua_pushliteral(L, "exten"); |
| 256 |
} else if (priority != ast_channel_priority(chan)) { |
| 257 |
lua_pushinteger(L, priority); |
| 258 |
lua_pushinteger(L, ast_channel_priority(chan)); |
| 259 |
lua_pushliteral(L, "priority"); |
| 260 |
} else { |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
lua_update_registry(L, context, exten, priority); |
| 265 |
return 0; |
| 266 |
} |
| 267 |
|
| 268 |
|
| 269 |
lua_insert(L, -3); |
| 270 |
|
| 271 |
lua_pushliteral(L, " changed from "); |
| 272 |
lua_insert(L, -3); |
| 273 |
|
| 274 |
lua_pushliteral(L, " to "); |
| 275 |
lua_insert(L, -2); |
| 276 |
|
| 277 |
lua_concat(L, 5); |
| 278 |
|
| 279 |
ast_debug(2, "Goto detected: %s\n", lua_tostring(L, -1)); |
| 280 |
lua_pop(L, 1); |
| 281 |
|
| 282 |
|
| 283 |
lua_pushinteger(L, LUA_GOTO_DETECTED); |
| 284 |
lua_error(L); |
| 285 |
|
| 286 |
return 0; |
| 287 |
} |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
static int lua_get_variable_value(lua_State *L) |
| 304 |
{ |
| 305 |
struct ast_channel *chan; |
| 306 |
char *value = NULL, *name; |
| 307 |
char *workspace = ast_alloca(LUA_BUF_SIZE); |
| 308 |
int autoservice; |
| 309 |
|
| 310 |
workspace[0] = '\0'; |
| 311 |
|
| 312 |
if (!lua_istable(L, 1)) { |
| 313 |
lua_pushstring(L, "User probably used '.' instead of ':' for retrieving a channel variable value"); |
| 314 |
return lua_error(L); |
| 315 |
} |
| 316 |
|
| 317 |
lua_getfield(L, LUA_REGISTRYINDEX, "channel"); |
| 318 |
chan = lua_touserdata(L, -1); |
| 319 |
lua_pop(L, 1); |
| 320 |
|
| 321 |
lua_getfield(L, 1, "name"); |
| 322 |
name = ast_strdupa(lua_tostring(L, -1)); |
| 323 |
lua_pop(L, 1); |
| 324 |
|
| 325 |
lua_getfield(L, LUA_REGISTRYINDEX, "autoservice"); |
| 326 |
autoservice = lua_toboolean(L, -1); |
| 327 |
lua_pop(L, 1); |
| 328 |
|
| 329 |
if (autoservice) |
| 330 |
ast_autoservice_stop(chan); |
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
if (!ast_strlen_zero(name) && name[strlen(name) - 1] == ')') { |
| 335 |
value = ast_func_read(chan, name, workspace, LUA_BUF_SIZE) ? NULL : workspace; |
| 336 |
} else { |
| 337 |
pbx_retrieve_variable(chan, name, &value, workspace, LUA_BUF_SIZE, ast_channel_varshead(chan)); |
| 338 |
} |
| 339 |
|
| 340 |
if (autoservice) |
| 341 |
ast_autoservice_start(chan); |
| 342 |
|
| 343 |
if (value) { |
| 344 |
lua_pushstring(L, value); |
| 345 |
} else { |
| 346 |
lua_pushnil(L); |
| 347 |
} |
| 348 |
|
| 349 |
return 1; |
| 350 |
} |
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
static int lua_set_variable_value(lua_State *L) |
| 366 |
{ |
| 367 |
const char *name, *value; |
| 368 |
struct ast_channel *chan; |
| 369 |
int autoservice; |
| 370 |
|
| 371 |
if (!lua_istable(L, 1)) { |
| 372 |
lua_pushstring(L, "User probably used '.' instead of ':' for setting a channel variable"); |
| 373 |
return lua_error(L); |
| 374 |
} |
| 375 |
|
| 376 |
lua_getfield(L, 1, "name"); |
| 377 |
name = ast_strdupa(lua_tostring(L, -1)); |
| 378 |
lua_pop(L, 1); |
| 379 |
|
| 380 |
value = luaL_checkstring(L, 2); |
| 381 |
|
| 382 |
lua_getfield(L, LUA_REGISTRYINDEX, "channel"); |
| 383 |
chan = lua_touserdata(L, -1); |
| 384 |
lua_pop(L, 1); |
| 385 |
|
| 386 |
lua_getfield(L, LUA_REGISTRYINDEX, "autoservice"); |
| 387 |
autoservice = lua_toboolean(L, -1); |
| 388 |
lua_pop(L, 1); |
| 389 |
|
| 390 |
if (autoservice) |
| 391 |
ast_autoservice_stop(chan); |
| 392 |
|
| 393 |
pbx_builtin_setvar_helper(chan, name, value); |
| 394 |
|
| 395 |
if (autoservice) |
| 396 |
ast_autoservice_start(chan); |
| 397 |
|
| 398 |
return 0; |
| 399 |
} |
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
static void lua_update_registry(lua_State *L, const char *context, const char *exten, int priority) |
| 410 |
{ |
| 411 |
lua_pushstring(L, context); |
| 412 |
lua_setfield(L, LUA_REGISTRYINDEX, "context"); |
| 413 |
|
| 414 |
lua_pushstring(L, exten); |
| 415 |
lua_setfield(L, LUA_REGISTRYINDEX, "exten"); |
| 416 |
|
| 417 |
lua_pushinteger(L, priority); |
| 418 |
lua_setfield(L, LUA_REGISTRYINDEX, "priority"); |
| 419 |
} |
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
static void lua_push_variable_table(lua_State *L) |
| 430 |
{ |
| 431 |
lua_newtable(L); |
| 432 |
luaL_getmetatable(L, "variable"); |
| 433 |
lua_setmetatable(L, -2); |
| 434 |
|
| 435 |
lua_insert(L, -2); |
| 436 |
lua_setfield(L, -2, "name"); |
| 437 |
|
| 438 |
lua_pushcfunction(L, &lua_get_variable_value); |
| 439 |
lua_setfield(L, -2, "get"); |
| 440 |
|
| 441 |
lua_pushcfunction(L, &lua_set_variable_value); |
| 442 |
lua_setfield(L, -2, "set"); |
| 443 |
} |
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
static void lua_create_app_table(lua_State *L) |
| 451 |
{ |
| 452 |
lua_newtable(L); |
| 453 |
luaL_newmetatable(L, "app"); |
| 454 |
|
| 455 |
lua_pushstring(L, "__index"); |
| 456 |
lua_pushcfunction(L, &lua_pbx_findapp); |
| 457 |
lua_settable(L, -3); |
| 458 |
|
| 459 |
lua_setmetatable(L, -2); |
| 460 |
lua_setglobal(L, "app"); |
| 461 |
} |
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
static void lua_create_channel_table(lua_State *L) |
| 469 |
{ |
| 470 |
lua_newtable(L); |
| 471 |
luaL_newmetatable(L, "channel_data"); |
| 472 |
|
| 473 |
lua_pushstring(L, "__index"); |
| 474 |
lua_pushcfunction(L, &lua_get_variable); |
| 475 |
lua_settable(L, -3); |
| 476 |
|
| 477 |
lua_pushstring(L, "__newindex"); |
| 478 |
lua_pushcfunction(L, &lua_set_variable); |
| 479 |
lua_settable(L, -3); |
| 480 |
|
| 481 |
lua_setmetatable(L, -2); |
| 482 |
lua_setglobal(L, "channel"); |
| 483 |
} |
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
static void lua_create_variable_metatable(lua_State *L) |
| 491 |
{ |
| 492 |
luaL_newmetatable(L, "variable"); |
| 493 |
|
| 494 |
lua_pushstring(L, "__call"); |
| 495 |
lua_pushcfunction(L, &lua_func_read); |
| 496 |
lua_settable(L, -3); |
| 497 |
|
| 498 |
lua_pop(L, 1); |
| 499 |
} |
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
static void lua_create_application_metatable(lua_State *L) |
| 508 |
{ |
| 509 |
luaL_newmetatable(L, "application"); |
| 510 |
|
| 511 |
lua_pushstring(L, "__call"); |
| 512 |
lua_pushcfunction(L, &lua_pbx_exec); |
| 513 |
lua_settable(L, -3); |
| 514 |
|
| 515 |
lua_pop(L, 1); |
| 516 |
} |
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
static void lua_create_autoservice_functions(lua_State *L) |
| 524 |
{ |
| 525 |
lua_pushcfunction(L, &lua_autoservice_start); |
| 526 |
lua_setglobal(L, "autoservice_start"); |
| 527 |
|
| 528 |
lua_pushcfunction(L, &lua_autoservice_stop); |
| 529 |
lua_setglobal(L, "autoservice_stop"); |
| 530 |
|
| 531 |
lua_pushcfunction(L, &lua_autoservice_status); |
| 532 |
lua_setglobal(L, "autoservice_status"); |
| 533 |
|
| 534 |
lua_pushboolean(L, 1); |
| 535 |
lua_setfield(L, LUA_REGISTRYINDEX, "autoservice"); |
| 536 |
} |
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
static void lua_create_hangup_function(lua_State *L) |
| 544 |
{ |
| 545 |
lua_pushcfunction(L, &lua_check_hangup); |
| 546 |
lua_setglobal(L, "check_hangup"); |
| 547 |
} |
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
static int lua_get_variable(lua_State *L) |
| 562 |
{ |
| 563 |
struct ast_channel *chan; |
| 564 |
const char *name = luaL_checkstring(L, 2); |
| 565 |
char *value = NULL; |
| 566 |
char *workspace = ast_alloca(LUA_BUF_SIZE); |
| 567 |
workspace[0] = '\0'; |
| 568 |
|
| 569 |
lua_getfield(L, LUA_REGISTRYINDEX, "channel"); |
| 570 |
chan = lua_touserdata(L, -1); |
| 571 |
lua_pop(L, 1); |
| 572 |
|
| 573 |
lua_pushvalue(L, 2); |
| 574 |
lua_push_variable_table(L); |
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
if (!ast_strlen_zero(name) && name[strlen(name) - 1] != ')') { |
| 579 |
pbx_retrieve_variable(chan, name, &value, workspace, LUA_BUF_SIZE, ast_channel_varshead(chan)); |
| 580 |
} |
| 581 |
|
| 582 |
if (value) { |
| 583 |
lua_pushstring(L, value); |
| 584 |
lua_setfield(L, -2, "value"); |
| 585 |
} |
| 586 |
|
| 587 |
return 1; |
| 588 |
} |
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
static int lua_set_variable(lua_State *L) |
| 602 |
{ |
| 603 |
struct ast_channel *chan; |
| 604 |
int autoservice; |
| 605 |
const char *name = luaL_checkstring(L, 2); |
| 606 |
const char *value = luaL_checkstring(L, 3); |
| 607 |
|
| 608 |
lua_getfield(L, LUA_REGISTRYINDEX, "channel"); |
| 609 |
chan = lua_touserdata(L, -1); |
| 610 |
lua_pop(L, 1); |
| 611 |
|
| 612 |
lua_getfield(L, LUA_REGISTRYINDEX, "autoservice"); |
| 613 |
autoservice = lua_toboolean(L, -1); |
| 614 |
lua_pop(L, 1); |
| 615 |
|
| 616 |
if (autoservice) |
| 617 |
ast_autoservice_stop(chan); |
| 618 |
|
| 619 |
pbx_builtin_setvar_helper(chan, name, value); |
| 620 |
|
| 621 |
if (autoservice) |
| 622 |
ast_autoservice_start(chan); |
| 623 |
|
| 624 |
return 0; |
| 625 |
} |
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
static void lua_concat_args(lua_State *L, int start, int nargs) { |
| 637 |
int concat = 0; |
| 638 |
int i = start + 1; |
| 639 |
|
| 640 |
if (start <= nargs && !lua_isnil(L, start)) { |
| 641 |
lua_pushvalue(L, start); |
| 642 |
concat += 1; |
| 643 |
} |
| 644 |
|
| 645 |
for (; i <= nargs; i++) { |
| 646 |
if (lua_isnil(L, i)) { |
| 647 |
lua_pushliteral(L, ","); |
| 648 |
concat += 1; |
| 649 |
} else { |
| 650 |
lua_pushliteral(L, ","); |
| 651 |
lua_pushvalue(L, i); |
| 652 |
concat += 2; |
| 653 |
} |
| 654 |
} |
| 655 |
|
| 656 |
lua_concat(L, concat); |
| 657 |
} |
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
static int lua_func_read(lua_State *L) |
| 676 |
{ |
| 677 |
int nargs = lua_gettop(L); |
| 678 |
|
| 679 |
|
| 680 |
lua_getfield(L, 1, "name"); |
| 681 |
lua_pushliteral(L, "("); |
| 682 |
lua_concat_args(L, 2, nargs); |
| 683 |
lua_pushliteral(L, ")"); |
| 684 |
lua_concat(L, 4); |
| 685 |
|
| 686 |
lua_push_variable_table(L); |
| 687 |
return 1; |
| 688 |
} |
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
static int lua_autoservice_start(lua_State *L) |
| 701 |
{ |
| 702 |
struct ast_channel *chan; |
| 703 |
|
| 704 |
lua_getfield(L, LUA_REGISTRYINDEX, "autoservice"); |
| 705 |
if (lua_toboolean(L, -1)) { |
| 706 |
|
| 707 |
lua_pop(L, 1); |
| 708 |
return 0; |
| 709 |
} |
| 710 |
lua_pop(L, 1); |
| 711 |
|
| 712 |
lua_getfield(L, LUA_REGISTRYINDEX, "channel"); |
| 713 |
chan = lua_touserdata(L, -1); |
| 714 |
lua_pop(L, 1); |
| 715 |
|
| 716 |
ast_autoservice_start(chan); |
| 717 |
|
| 718 |
lua_pushboolean(L, 1); |
| 719 |
lua_setfield(L, LUA_REGISTRYINDEX, "autoservice"); |
| 720 |
return 0; |
| 721 |
} |
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
static int lua_autoservice_stop(lua_State *L) |
| 734 |
{ |
| 735 |
struct ast_channel *chan; |
| 736 |
|
| 737 |
lua_getfield(L, LUA_REGISTRYINDEX, "autoservice"); |
| 738 |
if (!lua_toboolean(L, -1)) { |
| 739 |
|
| 740 |
lua_pop(L, 1); |
| 741 |
return 0; |
| 742 |
} |
| 743 |
lua_pop(L, 1); |
| 744 |
|
| 745 |
lua_getfield(L, LUA_REGISTRYINDEX, "channel"); |
| 746 |
chan = lua_touserdata(L, -1); |
| 747 |
lua_pop(L, 1); |
| 748 |
|
| 749 |
ast_autoservice_stop(chan); |
| 750 |
|
| 751 |
lua_pushboolean(L, 0); |
| 752 |
lua_setfield(L, LUA_REGISTRYINDEX, "autoservice"); |
| 753 |
return 0; |
| 754 |
} |
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
static int lua_autoservice_status(lua_State *L) |
| 766 |
{ |
| 767 |
lua_getfield(L, LUA_REGISTRYINDEX, "autoservice"); |
| 768 |
return 1; |
| 769 |
} |
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
static int lua_check_hangup(lua_State *L) |
| 780 |
{ |
| 781 |
struct ast_channel *chan; |
| 782 |
lua_getfield(L, LUA_REGISTRYINDEX, "channel"); |
| 783 |
chan = lua_touserdata(L, -1); |
| 784 |
lua_pop(L, 1); |
| 785 |
|
| 786 |
lua_pushboolean(L, ast_check_hangup(chan)); |
| 787 |
return 1; |
| 788 |
} |
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
static int lua_error_function(lua_State *L) |
| 797 |
{ |
| 798 |
int message_index; |
| 799 |
|
| 800 |
|
| 801 |
if (lua_isnumber(L, -1)) { |
| 802 |
return 1; |
| 803 |
} |
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
message_index = lua_gettop(L); |
| 808 |
|
| 809 |
|
| 810 |
lua_pushliteral(L, "\n"); |
| 811 |
|
| 812 |
lua_getglobal(L, "debug"); |
| 813 |
if (!lua_istable(L, -1)) { |
| 814 |
|
| 815 |
lua_pop(L, 2); |
| 816 |
|
| 817 |
return 1; |
| 818 |
} |
| 819 |
lua_getfield(L, -1, "traceback"); |
| 820 |
if (!lua_isfunction(L, -1)) { |
| 821 |
|
| 822 |
lua_pop(L, 3); |
| 823 |
|
| 824 |
return 1; |
| 825 |
} |
| 826 |
lua_remove(L, -2); |
| 827 |
|
| 828 |
lua_pushvalue(L, message_index); |
| 829 |
lua_remove(L, message_index); |
| 830 |
|
| 831 |
lua_pushnumber(L, 2); |
| 832 |
|
| 833 |
lua_call(L, 2, 1); |
| 834 |
|
| 835 |
|
| 836 |
lua_concat(L, 2); |
| 837 |
|
| 838 |
return 1; |
| 839 |
} |
| 840 |
|
| 841 |
|
| 842 |
|
| 843 |
|
| 844 |
|
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
static int lua_sort_extensions(lua_State *L) |
| 850 |
{ |
| 851 |
int extensions, extensions_order; |
| 852 |
|
| 853 |
|
| 854 |
lua_newtable(L); |
| 855 |
lua_setfield(L, LUA_REGISTRYINDEX, "extensions_order"); |
| 856 |
lua_getfield(L, LUA_REGISTRYINDEX, "extensions_order"); |
| 857 |
extensions_order = lua_gettop(L); |
| 858 |
|
| 859 |
|
| 860 |
|
| 861 |
lua_getglobal(L, "extensions"); |
| 862 |
extensions = lua_gettop(L); |
| 863 |
if (lua_isnil(L, -1)) { |
| 864 |
lua_pop(L, 1); |
| 865 |
lua_pushstring(L, "Unable to find 'extensions' table in extensions.lua\n"); |
| 866 |
return 1; |
| 867 |
} |
| 868 |
|
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
|
| 873 |
for (lua_pushnil(L); lua_next(L, extensions); lua_pop(L, 1)) { |
| 874 |
int context = lua_gettop(L); |
| 875 |
int context_name = context - 1; |
| 876 |
int context_order; |
| 877 |
|
| 878 |
|
| 879 |
|
| 880 |
lua_pushvalue(L, context_name); |
| 881 |
|
| 882 |
|
| 883 |
lua_newtable(L); |
| 884 |
context_order = lua_gettop(L); |
| 885 |
|
| 886 |
|
| 887 |
|
| 888 |
for (lua_pushnil(L); lua_next(L, context); lua_pop(L, 1)) { |
| 889 |
int exten = lua_gettop(L) - 1; |
| 890 |
#if LUA_VERSION_NUM < 502 |
| 891 |
lua_pushinteger(L, lua_objlen(L, context_order) + 1); |
| 892 |
#else |
| 893 |
lua_pushinteger(L, lua_rawlen(L, context_order) + 1); |
| 894 |
#endif |
| 895 |
lua_pushvalue(L, exten); |
| 896 |
lua_settable(L, context_order); |
| 897 |
} |
| 898 |
lua_settable(L, extensions_order); |
| 899 |
|
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
lua_getglobal(L, "table"); |
| 904 |
lua_getfield(L, -1, "sort"); |
| 905 |
lua_remove(L, -2); |
| 906 |
|
| 907 |
|
| 908 |
lua_pushvalue(L, context_name); |
| 909 |
lua_gettable(L, extensions_order); |
| 910 |
|
| 911 |
|
| 912 |
lua_pushcfunction(L, &lua_extension_cmp); |
| 913 |
|
| 914 |
if (lua_pcall(L, 2, 0, 0)) { |
| 915 |
lua_insert(L, -5); |
| 916 |
lua_pop(L, 4); |
| 917 |
return 1; |
| 918 |
} |
| 919 |
} |
| 920 |
|
| 921 |
|
| 922 |
lua_pop(L, 2); |
| 923 |
return 0; |
| 924 |
} |
| 925 |
|
| 926 |
|
| 927 |
|
| 928 |
|
| 929 |
|
| 930 |
|
| 931 |
|
| 932 |
|
| 933 |
|
| 934 |
static int lua_register_switches(lua_State *L) |
| 935 |
{ |
| 936 |
int extensions; |
| 937 |
struct ast_context *con = NULL; |
| 938 |
|
| 939 |
|
| 940 |
|
| 941 |
if (!local_table) |
| 942 |
local_table = ast_hashtab_create(17, ast_hashtab_compare_contexts, ast_hashtab_resize_java, ast_hashtab_newsize_java, ast_hashtab_hash_contexts, 0); |
| 943 |
|
| 944 |
|
| 945 |
lua_getglobal(L, "extensions"); |
| 946 |
extensions = lua_gettop(L); |
| 947 |
if (lua_isnil(L, -1)) { |
| 948 |
lua_pop(L, 1); |
| 949 |
lua_pushstring(L, "Unable to find 'extensions' table in extensions.lua\n"); |
| 950 |
return 1; |
| 951 |
} |
| 952 |
|
| 953 |
|
| 954 |
|
| 955 |
|
| 956 |
for (lua_pushnil(L); lua_next(L, extensions); lua_pop(L, 1)) { |
| 957 |
int context = lua_gettop(L); |
| 958 |
int context_name = context - 1; |
| 959 |
const char *context_str = lua_tostring(L, context_name); |
| 960 |
|
| 961 |
|
| 962 |
con = ast_context_find_or_create(&local_contexts, local_table, context_str, registrar); |
| 963 |
if (!con) { |
| 964 |
|
| 965 |
lua_pop(L, 3); |
| 966 |
lua_pushstring(L, "Failed to find or create context\n"); |
| 967 |
return 1; |
| 968 |
} |
| 969 |
|
| 970 |
|
| 971 |
if (ast_context_add_switch2(con, "Lua", "", 0, registrar)) { |
| 972 |
|
| 973 |
lua_pop(L, 3); |
| 974 |
lua_pushstring(L, "Unable to create switch for context\n"); |
| 975 |
return 1; |
| 976 |
} |
| 977 |
} |
| 978 |
|
| 979 |
|
| 980 |
lua_pop(L, 1); |
| 981 |
return 0; |
| 982 |
} |
| 983 |
|
| 984 |
|
| 985 |
|
| 986 |
|
| 987 |
|
| 988 |
|
| 989 |
|
| 990 |
|
| 991 |
|
| 992 |
static int lua_register_hints(lua_State *L) |
| 993 |
{ |
| 994 |
int hints; |
| 995 |
struct ast_context *con = NULL; |
| 996 |
|
| 997 |
|
| 998 |
|
| 999 |
if (!local_table) |
| 1000 |
local_table = ast_hashtab_create(17, ast_hashtab_compare_contexts, ast_hashtab_resize_java, ast_hashtab_newsize_java, ast_hashtab_hash_contexts, 0); |
| 1001 |
|
| 1002 |
|
| 1003 |
lua_getglobal(L, "hints"); |
| 1004 |
hints = lua_gettop(L); |
| 1005 |
if (lua_isnil(L, -1)) { |
| 1006 |
|
| 1007 |
lua_pop(L, 1); |
| 1008 |
return 0; |
| 1009 |
} |
| 1010 |
|
| 1011 |
|
| 1012 |
|
| 1013 |
|
| 1014 |
for (lua_pushnil(L); lua_next(L, hints); lua_pop(L, 1)) { |
| 1015 |
int context = lua_gettop(L); |
| 1016 |
int context_name = context - 1; |
| 1017 |
const char *context_str = lua_tostring(L, context_name); |
| 1018 |
|
| 1019 |
|
| 1020 |
con = ast_context_find_or_create(&local_contexts, local_table, context_str, registrar); |
| 1021 |
if (!con) { |
| 1022 |
|
| 1023 |
lua_pop(L, 3); |
| 1024 |
lua_pushstring(L, "Failed to find or create context\n"); |
| 1025 |
return 1; |
| 1026 |
} |
| 1027 |
|
| 1028 |
|
| 1029 |
for (lua_pushnil(L); lua_next(L, context); lua_pop(L, 1)) { |
| 1030 |
const char *hint_value = lua_tostring(L, -1); |
| 1031 |
const char *hint_name; |
| 1032 |
|
| 1033 |
|
| 1034 |
if (!hint_value) { |
| 1035 |
continue; |
| 1036 |
} |
| 1037 |
|
| 1038 |
|
| 1039 |
lua_pushvalue(L, -2); |
| 1040 |
if (!(hint_name = lua_tostring(L, -1))) { |
| 1041 |
|
| 1042 |
lua_pop(L, 1); |
| 1043 |
continue; |
| 1044 |
} |
| 1045 |
|
| 1046 |
if (ast_add_extension2(con, 0, hint_name, PRIORITY_HINT, NULL, NULL, hint_value, NULL, NULL, registrar, NULL, 0)) { |
| 1047 |
|
| 1048 |
|
| 1049 |
lua_pop(L, 6); |
| 1050 |
lua_pushstring(L, "Error creating hint\n"); |
| 1051 |
return 1; |
| 1052 |
} |
| 1053 |
|
| 1054 |
|
| 1055 |
lua_pop(L, 1); |
| 1056 |
} |
| 1057 |
} |
| 1058 |
|
| 1059 |
|
| 1060 |
lua_pop(L, 1); |
| 1061 |
|
| 1062 |
return 0; |
| 1063 |
} |
| 1064 |
|
| 1065 |
|
| 1066 |
|
| 1067 |
|
| 1068 |
|
| 1069 |
|
| 1070 |
|
| 1071 |
|
| 1072 |
static int lua_extension_cmp(lua_State *L) |
| 1073 |
{ |
| 1074 |
const char *a = luaL_checkstring(L, -2); |
| 1075 |
const char *b = luaL_checkstring(L, -1); |
| 1076 |
|
| 1077 |
if (ast_extension_cmp(a, b) == -1) |
| 1078 |
lua_pushboolean(L, 1); |
| 1079 |
else |
| 1080 |
lua_pushboolean(L, 0); |
| 1081 |
|
| 1082 |
return 1; |
| 1083 |
} |
| 1084 |
|
| 1085 |
|
| 1086 |
|
| 1087 |
|
| 1088 |
|
| 1089 |
|
| 1090 |
|
| 1091 |
|
| 1092 |
|
| 1093 |
|
| 1094 |
|
| 1095 |
|
| 1096 |
static char *lua_read_extensions_file(lua_State *L, size_t *size, int *file_not_openable) |
| 1097 |
{ |
| 1098 |
FILE *f; |
| 1099 |
int error_func; |
| 1100 |
char *data; |
| 1101 |
char *path = ast_alloca(strlen(config) + strlen(ast_config_AST_CONFIG_DIR) + 2); |
| 1102 |
sprintf(path, "%s/%s", ast_config_AST_CONFIG_DIR, config); |
| 1103 |
|
| 1104 |
if (!(f = fopen(path, "r"))) { |
| 1105 |
lua_pushstring(L, "cannot open '"); |
| 1106 |
lua_pushstring(L, path); |
| 1107 |
lua_pushstring(L, "' for reading: "); |
| 1108 |
lua_pushstring(L, strerror(errno)); |
| 1109 |
lua_concat(L, 4); |
| 1110 |
|
| 1111 |
*file_not_openable = 1; |
| 1112 |
|
| 1113 |
return NULL; |
| 1114 |
} |
| 1115 |
|
| 1116 |
if (fseek(f, 0l, SEEK_END)) { |
| 1117 |
fclose(f); |
| 1118 |
lua_pushliteral(L, "error determining the size of the config file"); |
| 1119 |
return NULL; |
| 1120 |
} |
| 1121 |
|
| 1122 |
*size = ftell(f); |
| 1123 |
|
| 1124 |
if (fseek(f, 0l, SEEK_SET)) { |
| 1125 |
*size = 0; |
| 1126 |
fclose(f); |
| 1127 |
lua_pushliteral(L, "error reading config file"); |
| 1128 |
return NULL; |
| 1129 |
} |
| 1130 |
|
| 1131 |
if (!(data = ast_malloc(*size))) { |
| 1132 |
*size = 0; |
| 1133 |
fclose(f); |
| 1134 |
lua_pushstring(L, "not enough memory"); |
| 1135 |
return NULL; |
| 1136 |
} |
| 1137 |
|
| 1138 |
if (fread(data, sizeof(char), *size, f) != *size) { |
| 1139 |
*size = 0; |
| 1140 |
fclose(f); |
| 1141 |
lua_pushliteral(L, "problem reading configuration file"); |
| 1142 |
return NULL; |
| 1143 |
} |
| 1144 |
fclose(f); |
| 1145 |
|
| 1146 |
lua_pushcfunction(L, &lua_error_function); |
| 1147 |
error_func = lua_gettop(L); |
| 1148 |
|
| 1149 |
if (luaL_loadbuffer(L, data, *size, "extensions.lua") |
| 1150 |
|| lua_pcall(L, 0, LUA_MULTRET, error_func) |
| 1151 |
|| lua_sort_extensions(L) |
| 1152 |
|| lua_register_switches(L) |
| 1153 |
|| lua_register_hints(L)) { |
| 1154 |
ast_free(data); |
| 1155 |
data = NULL; |
| 1156 |
*size = 0; |
| 1157 |
} |
| 1158 |
|
| 1159 |
lua_remove(L, error_func); |
| 1160 |
return data; |
| 1161 |
} |
| 1162 |
|
| 1163 |
|
| 1164 |
|
| 1165 |
|
| 1166 |
|
| 1167 |
|
| 1168 |
|
| 1169 |
|
| 1170 |
|
| 1171 |
|
| 1172 |
|
| 1173 |
|
| 1174 |
|
| 1175 |
static int lua_load_extensions(lua_State *L, struct ast_channel *chan) |
| 1176 |
{ |
| 1177 |
|
| 1178 |
|
| 1179 |
lua_pushlightuserdata(L, chan); |
| 1180 |
lua_setfield(L, LUA_REGISTRYINDEX, "channel"); |
| 1181 |
|
| 1182 |
luaL_openlibs(L); |
| 1183 |
|
| 1184 |
|
| 1185 |
ast_mutex_lock(&config_file_lock); |
| 1186 |
if (luaL_loadbuffer(L, config_file_data, config_file_size, "extensions.lua") |
| 1187 |
|| lua_pcall(L, 0, LUA_MULTRET, 0) |
| 1188 |
|| lua_sort_extensions(L)) { |
| 1189 |
ast_mutex_unlock(&config_file_lock); |
| 1190 |
return 1; |
| 1191 |
} |
| 1192 |
ast_mutex_unlock(&config_file_lock); |
| 1193 |
|
| 1194 |
|
| 1195 |
|
| 1196 |
lua_create_app_table(L); |
| 1197 |
lua_create_channel_table(L); |
| 1198 |
|
| 1199 |
lua_create_variable_metatable(L); |
| 1200 |
lua_create_application_metatable(L); |
| 1201 |
|
| 1202 |
lua_create_autoservice_functions(L); |
| 1203 |
lua_create_hangup_function(L); |
| 1204 |
|
| 1205 |
return 0; |
| 1206 |
} |
| 1207 |
|
| 1208 |
|
| 1209 |
|
| 1210 |
|
| 1211 |
|
| 1212 |
|
| 1213 |
|
| 1214 |
|
| 1215 |
|
| 1216 |
|
| 1217 |
|
| 1218 |
static int lua_reload_extensions(lua_State *L) |
| 1219 |
{ |
| 1220 |
size_t size = 0; |
| 1221 |
char *data = NULL; |
| 1222 |
int file_not_openable = 0; |
| 1223 |
|
| 1224 |
luaL_openlibs(L); |
| 1225 |
|
| 1226 |
if (!(data = lua_read_extensions_file(L, &size, &file_not_openable))) { |
| 1227 |
if (file_not_openable) { |
| 1228 |
return -1; |
| 1229 |
} |
| 1230 |
return 1; |
| 1231 |
} |
| 1232 |
|
| 1233 |
ast_mutex_lock(&config_file_lock); |
| 1234 |
|
| 1235 |
if (config_file_data) |
| 1236 |
ast_free(config_file_data); |
| 1237 |
|
| 1238 |
config_file_data = data; |
| 1239 |
config_file_size = size; |
| 1240 |
|
| 1241 |
|
| 1242 |
ast_merge_contexts_and_delete(&local_contexts, local_table, registrar); |
| 1243 |
|
| 1244 |
|
| 1245 |
|
| 1246 |
|
| 1247 |
local_table = NULL; |
| 1248 |
local_contexts = NULL; |
| 1249 |
|
| 1250 |
ast_mutex_unlock(&config_file_lock); |
| 1251 |
return 0; |
| 1252 |
} |
| 1253 |
|
| 1254 |
|
| 1255 |
|
| 1256 |
|
| 1257 |
static void lua_free_extensions() |
| 1258 |
{ |
| 1259 |
ast_mutex_lock(&config_file_lock); |
| 1260 |
config_file_size = 0; |
| 1261 |
ast_free(config_file_data); |
| 1262 |
ast_mutex_unlock(&config_file_lock); |
| 1263 |
} |
| 1264 |
|
| 1265 |
|
| 1266 |
|
| 1267 |
|
| 1268 |
|
| 1269 |
|
| 1270 |
|
| 1271 |
|
| 1272 |
|
| 1273 |
|
| 1274 |
|
| 1275 |
|
| 1276 |
|
| 1277 |
|
| 1278 |
static lua_State *lua_get_state(struct ast_channel *chan) |
| 1279 |
{ |
| 1280 |
struct ast_datastore *datastore = NULL; |
| 1281 |
lua_State *L; |
| 1282 |
|
| 1283 |
if (!chan) { |
| 1284 |
L = luaL_newstate(); |
| 1285 |
if (!L) { |
| 1286 |
ast_log(LOG_ERROR, "Error allocating lua_State, no memory\n"); |
| 1287 |
return NULL; |
| 1288 |
} |
| 1289 |
|
| 1290 |
if (lua_load_extensions(L, NULL)) { |
| 1291 |
const char *error = lua_tostring(L, -1); |
| 1292 |
ast_log(LOG_ERROR, "Error loading extensions.lua: %s\n", error); |
| 1293 |
lua_close(L); |
| 1294 |
return NULL; |
| 1295 |
} |
| 1296 |
return L; |
| 1297 |
} else { |
| 1298 |
ast_channel_lock(chan); |
| 1299 |
datastore = ast_channel_datastore_find(chan, &lua_datastore, NULL); |
| 1300 |
ast_channel_unlock(chan); |
| 1301 |
|
| 1302 |
if (!datastore) { |
| 1303 |
|
| 1304 |
datastore = ast_datastore_alloc(&lua_datastore, NULL); |
| 1305 |
if (!datastore) { |
| 1306 |
ast_log(LOG_ERROR, "Error allocation channel datastore for lua_State\n"); |
| 1307 |
return NULL; |
| 1308 |
} |
| 1309 |
|
| 1310 |
datastore->data = luaL_newstate(); |
| 1311 |
if (!datastore->data) { |
| 1312 |
ast_datastore_free(datastore); |
| 1313 |
ast_log(LOG_ERROR, "Error allocating lua_State, no memory\n"); |
| 1314 |
return NULL; |
| 1315 |
} |
| 1316 |
|
| 1317 |
ast_channel_lock(chan); |
| 1318 |
ast_channel_datastore_add(chan, datastore); |
| 1319 |
ast_channel_unlock(chan); |
| 1320 |
|
| 1321 |
L = datastore->data; |
| 1322 |
|
| 1323 |
if (lua_load_extensions(L, chan)) { |
| 1324 |
const char *error = lua_tostring(L, -1); |
| 1325 |
ast_log(LOG_ERROR, "Error loading extensions.lua for %s: %s\n", ast_channel_name(chan), error); |
| 1326 |
|
| 1327 |
ast_channel_lock(chan); |
| 1328 |
ast_channel_datastore_remove(chan, datastore); |
| 1329 |
ast_channel_unlock(chan); |
| 1330 |
|
| 1331 |
ast_datastore_free(datastore); |
| 1332 |
return NULL; |
| 1333 |
} |
| 1334 |
} |
| 1335 |
|
| 1336 |
return datastore->data; |
| 1337 |
} |
| 1338 |
} |
| 1339 |
|
| 1340 |
static int exists(struct ast_channel *chan, const char *context, const char *exten, int priority, const char *callerid, const char *data) |
| 1341 |
{ |
| 1342 |
int res; |
| 1343 |
lua_State *L; |
| 1344 |
struct ast_module_user *u = ast_module_user_add(chan); |
| 1345 |
if (!u) { |
| 1346 |
ast_log(LOG_ERROR, "Error adjusting use count, probably could not allocate memory\n"); |
| 1347 |
return 0; |
| 1348 |
} |
| 1349 |
|
| 1350 |
L = lua_get_state(chan); |
| 1351 |
if (!L) { |
| 1352 |
ast_module_user_remove(u); |
| 1353 |
return 0; |
| 1354 |
} |
| 1355 |
|
| 1356 |
res = lua_find_extension(L, context, exten, priority, &exists, 0); |
| 1357 |
|
| 1358 |
if (!chan) lua_close(L); |
| 1359 |
ast_module_user_remove(u); |
| 1360 |
return res; |
| 1361 |
} |
| 1362 |
|
| 1363 |
static int canmatch(struct ast_channel *chan, const char *context, const char *exten, int priority, const char *callerid, const char *data) |
| 1364 |
{ |
| 1365 |
int res; |
| 1366 |
lua_State *L; |
| 1367 |
struct ast_module_user *u = ast_module_user_add(chan); |
| 1368 |
if (!u) { |
| 1369 |
ast_log(LOG_ERROR, "Error adjusting use count, probably could not allocate memory\n"); |
| 1370 |
return 0; |
| 1371 |
} |
| 1372 |
|
| 1373 |
L = lua_get_state(chan); |
| 1374 |
if (!L) { |
| 1375 |
ast_module_user_remove(u); |
| 1376 |
return 0; |
| 1377 |
} |
| 1378 |
|
| 1379 |
res = lua_find_extension(L, context, exten, priority, &canmatch, 0); |
| 1380 |
|
| 1381 |
if (!chan) lua_close(L); |
| 1382 |
ast_module_user_remove(u); |
| 1383 |
return res; |
| 1384 |
} |
| 1385 |
|
| 1386 |
static int matchmore(struct ast_channel *chan, const char *context, const char *exten, int priority, const char *callerid, const char *data) |
| 1387 |
{ |
| 1388 |
int res; |
| 1389 |
lua_State *L; |
| 1390 |
struct ast_module_user *u = ast_module_user_add(chan); |
| 1391 |
if (!u) { |
| 1392 |
ast_log(LOG_ERROR, "Error adjusting use count, probably could not allocate memory\n"); |
| 1393 |
return 0; |
| 1394 |
} |
| 1395 |
|
| 1396 |
L = lua_get_state(chan); |
| 1397 |
if (!L) { |
| 1398 |
ast_module_user_remove(u); |
| 1399 |
return 0; |
| 1400 |
} |
| 1401 |
|
| 1402 |
res = lua_find_extension(L, context, exten, priority, &matchmore, 0); |
| 1403 |
|
| 1404 |
if (!chan) lua_close(L); |
| 1405 |
ast_module_user_remove(u); |
| 1406 |
return res; |
| 1407 |
} |
| 1408 |
|
| 1409 |
|
| 1410 |
static int exec(struct ast_channel *chan, const char *context, const char *exten, int priority, const char *callerid, const char *data) |
| 1411 |
{ |
| 1412 |
int res, error_func; |
| 1413 |
lua_State *L; |
| 1414 |
struct ast_module_user *u = ast_module_user_add(chan); |
| 1415 |
if (!u) { |
| 1416 |
ast_log(LOG_ERROR, "Error adjusting use count, probably could not allocate memory\n"); |
| 1417 |
return -1; |
| 1418 |
} |
| 1419 |
|
| 1420 |
L = lua_get_state(chan); |
| 1421 |
if (!L) { |
| 1422 |
ast_module_user_remove(u); |
| 1423 |
return -1; |
| 1424 |
} |
| 1425 |
|
| 1426 |
lua_pushcfunction(L, &lua_error_function); |
| 1427 |
error_func = lua_gettop(L); |
| 1428 |
|
| 1429 |
|
| 1430 |
if (!lua_find_extension(L, context, exten, priority, &exists, 1)) { |
| 1431 |
lua_pop(L, 1); |
| 1432 |
ast_log(LOG_ERROR, "Could not find extension %s in context %s\n", exten, context); |
| 1433 |
if (!chan) lua_close(L); |
| 1434 |
ast_module_user_remove(u); |
| 1435 |
return -1; |
| 1436 |
} |
| 1437 |
|
| 1438 |
lua_getfield(L, LUA_REGISTRYINDEX, "autoservice"); |
| 1439 |
if (lua_toboolean(L, -1)) { |
| 1440 |
ast_autoservice_start(chan); |
| 1441 |
} |
| 1442 |
lua_pop(L, 1); |
| 1443 |
|
| 1444 |
lua_update_registry(L, context, exten, priority); |
| 1445 |
|
| 1446 |
lua_pushstring(L, context); |
| 1447 |
lua_pushstring(L, exten); |
| 1448 |
|
| 1449 |
res = lua_pcall(L, 2, 0, error_func); |
| 1450 |
if (res) { |
| 1451 |
if (res == LUA_ERRRUN) { |
| 1452 |
res = -1; |
| 1453 |
if (lua_isnumber(L, -1)) { |
| 1454 |
res = lua_tointeger(L, -1); |
| 1455 |
|
| 1456 |
if (res == LUA_GOTO_DETECTED) { |
| 1457 |
res = 0; |
| 1458 |
} |
| 1459 |
} else if (lua_isstring(L, -1)) { |
| 1460 |
const char *error = lua_tostring(L, -1); |
| 1461 |
ast_log(LOG_ERROR, "Error executing lua extension: %s\n", error); |
| 1462 |
} |
| 1463 |
} else if (res == LUA_ERRERR) { |
| 1464 |
res = -1; |
| 1465 |
ast_log(LOG_ERROR, "Error in the lua error handler (this is probably a bug in pbx_lua)\n"); |
| 1466 |
} else if (res == LUA_ERRMEM) { |
| 1467 |
res = -1; |
| 1468 |
ast_log(LOG_ERROR, "Memory allocation error\n"); |
| 1469 |
} |
| 1470 |
lua_pop(L, 1); |
| 1471 |
} |
| 1472 |
lua_remove(L, error_func); |
| 1473 |
|
| 1474 |
lua_getfield(L, LUA_REGISTRYINDEX, "autoservice"); |
| 1475 |
if (lua_toboolean(L, -1)) { |
| 1476 |
ast_autoservice_stop(chan); |
| 1477 |
} |
| 1478 |
lua_pop(L, 1); |
| 1479 |
|
| 1480 |
if (!chan) lua_close(L); |
| 1481 |
ast_module_user_remove(u); |
| 1482 |
return res; |
| 1483 |
} |
| 1484 |
|
| 1485 |
|
| 1486 |
|
| 1487 |
|
| 1488 |
|
| 1489 |
|
| 1490 |
|
| 1491 |
|
| 1492 |
|
| 1493 |
|
| 1494 |
|
| 1495 |
|
| 1496 |
|
| 1497 |
|
| 1498 |
static int lua_find_extension(lua_State *L, const char *context, const char *exten, int priority, ast_switch_f *func, int push_func) |
| 1499 |
{ |
| 1500 |
int context_table, context_order_table; |
| 1501 |
size_t i; |
| 1502 |
|
| 1503 |
ast_debug(2, "Looking up %s@%s:%i\n", exten, context, priority); |
| 1504 |
if (priority != 1) |
| 1505 |
return 0; |
| 1506 |
|
| 1507 |
|
| 1508 |
lua_getglobal(L, "extensions"); |
| 1509 |
if (lua_isnil(L, -1)) { |
| 1510 |
ast_log(LOG_ERROR, "Unable to find 'extensions' table in extensions.lua\n"); |
| 1511 |
lua_pop(L, 1); |
| 1512 |
return 0; |
| 1513 |
} |
| 1514 |
|
| 1515 |
|
| 1516 |
lua_getfield(L, -1, context); |
| 1517 |
if (lua_isnil(L, -1)) { |
| 1518 |
lua_pop(L, 2); |
| 1519 |
return 0; |
| 1520 |
} |
| 1521 |
|
| 1522 |
|
| 1523 |
lua_remove(L, -2); |
| 1524 |
|
| 1525 |
context_table = lua_gettop(L); |
| 1526 |
|
| 1527 |
|
| 1528 |
lua_getfield(L, LUA_REGISTRYINDEX, "extensions_order"); |
| 1529 |
lua_getfield(L, -1, context); |
| 1530 |
|
| 1531 |
lua_remove(L, -2); |
| 1532 |
|
| 1533 |
context_order_table = lua_gettop(L); |
| 1534 |
|
| 1535 |
|
| 1536 |
#if LUA_VERSION_NUM < 502 |
| 1537 |
for (i = 1; i < lua_objlen(L, context_order_table) + 1; i++) { |
| 1538 |
#else |
| 1539 |
for (i = 1; i < lua_rawlen(L, context_order_table) + 1; i++) { |
| 1540 |
#endif |
| 1541 |
int e_index_copy, match = 0; |
| 1542 |
const char *e; |
| 1543 |
|
| 1544 |
lua_pushinteger(L, i); |
| 1545 |
lua_gettable(L, context_order_table); |
| 1546 |
lua_gettop(L); |
| 1547 |
|
| 1548 |
|
| 1549 |
lua_pushvalue(L, -1); |
| 1550 |
e_index_copy = lua_gettop(L); |
| 1551 |
|
| 1552 |
if (!(e = lua_tostring(L, e_index_copy))) { |
| 1553 |
lua_pop(L, 2); |
| 1554 |
continue; |
| 1555 |
} |
| 1556 |
|
| 1557 |
|
| 1558 |
if (!strcasecmp(e, "include")) { |
| 1559 |
lua_pop(L, 2); |
| 1560 |
continue; |
| 1561 |
} |
| 1562 |
|
| 1563 |
if (func == &matchmore) |
| 1564 |
match = ast_extension_close(e, exten, E_MATCHMORE); |
| 1565 |
else if (func == &canmatch) |
| 1566 |
match = ast_extension_close(e, exten, E_CANMATCH); |
| 1567 |
else |
| 1568 |
match = ast_extension_match(e, exten); |
| 1569 |
|
| 1570 |
|
| 1571 |
|
| 1572 |
|
| 1573 |
if (!match) { |
| 1574 |
|
| 1575 |
lua_pop(L, 2); |
| 1576 |
continue; |
| 1577 |
} |
| 1578 |
|
| 1579 |
if (func == &matchmore && match == 2) { |
| 1580 |
|
| 1581 |
|
| 1582 |
lua_pop(L, 4); |
| 1583 |
return 0; |
| 1584 |
} |
| 1585 |
|
| 1586 |
|
| 1587 |
|
| 1588 |
|
| 1589 |
if (push_func) { |
| 1590 |
lua_pop(L, 1); |
| 1591 |
lua_gettable(L, context_table); |
| 1592 |
lua_insert(L, -3); |
| 1593 |
lua_pop(L, 2); |
| 1594 |
} else { |
| 1595 |
lua_pop(L, 4); |
| 1596 |
} |
| 1597 |
|
| 1598 |
return 1; |
| 1599 |
} |
| 1600 |
|
| 1601 |
|
| 1602 |
lua_getfield(L, context_table, "include"); |
| 1603 |
if (lua_isnil(L, -1)) { |
| 1604 |
lua_pop(L, 3); |
| 1605 |
return 0; |
| 1606 |
} |
| 1607 |
|
| 1608 |
|
| 1609 |
lua_remove(L, context_order_table); |
| 1610 |
lua_remove(L, context_table); |
| 1611 |
|
| 1612 |
|
| 1613 |
for (lua_pushnil(L); lua_next(L, -2); lua_pop(L, 1)) { |
| 1614 |
const char *c = lua_tostring(L, -1); |
| 1615 |
if (!c) |
| 1616 |
continue; |
| 1617 |
|
| 1618 |
if (lua_find_extension(L, c, exten, priority, func, push_func)) { |
| 1619 |
|
| 1620 |
|
| 1621 |
|
| 1622 |
|
| 1623 |
if (push_func) |
| 1624 |
lua_insert(L, -4); |
| 1625 |
|
| 1626 |
lua_pop(L, 3); |
| 1627 |
return 1; |
| 1628 |
} |
| 1629 |
} |
| 1630 |
|
| 1631 |
|
| 1632 |
lua_pop(L, 1); |
| 1633 |
return 0; |
| 1634 |
} |
| 1635 |
|
| 1636 |
static struct ast_switch lua_switch = { |
| 1637 |
.name = "Lua", |
| 1638 |
.description = "Lua PBX Switch", |
| 1639 |
.exists = exists, |
| 1640 |
.canmatch = canmatch, |
| 1641 |
.exec = exec, |
| 1642 |
.matchmore = matchmore, |
| 1643 |
}; |
| 1644 |
|
| 1645 |
|
| 1646 |
static int load_or_reload_lua_stuff(void) |
| 1647 |
{ |
| 1648 |
int res = AST_MODULE_LOAD_SUCCESS; |
| 1649 |
int loaded = 0; |
| 1650 |
|
| 1651 |
lua_State *L = luaL_newstate(); |
| 1652 |
if (!L) { |
| 1653 |
ast_log(LOG_ERROR, "Error allocating lua_State, no memory\n"); |
| 1654 |
return AST_MODULE_LOAD_FAILURE; |
| 1655 |
} |
| 1656 |
|
| 1657 |
loaded = lua_reload_extensions(L); |
| 1658 |
if (loaded) { |
| 1659 |
const char *error = lua_tostring(L, -1); |
| 1660 |
ast_log(LOG_ERROR, "Error loading extensions.lua: %s\n", error); |
| 1661 |
|
| 1662 |
if (loaded < 0) { |
| 1663 |
res = AST_MODULE_LOAD_DECLINE; |
| 1664 |
} else { |
| 1665 |
res = AST_MODULE_LOAD_FAILURE; |
| 1666 |
} |
| 1667 |
} |
| 1668 |
|
| 1669 |
lua_close(L); |
| 1670 |
return res; |
| 1671 |
} |
| 1672 |
|
| 1673 |
static int unload_module(void) |
| 1674 |
{ |
| 1675 |
ast_context_destroy(NULL, registrar); |
| 1676 |
ast_unregister_switch(&lua_switch); |
| 1677 |
lua_free_extensions(); |
| 1678 |
return 0; |
| 1679 |
} |
| 1680 |
|
| 1681 |
static int reload(void) |
| 1682 |
{ |
| 1683 |
return load_or_reload_lua_stuff(); |
| 1684 |
} |
| 1685 |
|
| 1686 |
static int load_module(void) |
| 1687 |
{ |
| 1688 |
int res; |
| 1689 |
|
| 1690 |
if ((res = load_or_reload_lua_stuff())) |
| 1691 |
return res; |
| 1692 |
|
| 1693 |
if (ast_register_switch(&lua_switch)) { |
| 1694 |
ast_log(LOG_ERROR, "Unable to register Lua PBX switch\n"); |
| 1695 |
return AST_MODULE_LOAD_FAILURE; |
| 1696 |
} |
| 1697 |
|
| 1698 |
return AST_MODULE_LOAD_SUCCESS; |
| 1699 |
} |
| 1700 |
|
| 1701 |
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS, "Lua PBX Switch", |
| 1702 |
.support_level = AST_MODULE_SUPPORT_EXTENDED, |
| 1703 |
.load = load_module, |
| 1704 |
.unload = unload_module, |
| 1705 |
.reload = reload, |
| 1706 |
); |