Asterisk - The Open Source Telephony Project
18.5.0
|
Lua PBX Switch. More...
#include "asterisk.h"
#include "asterisk/logger.h"
#include "asterisk/channel.h"
#include "asterisk/pbx.h"
#include "asterisk/module.h"
#include "asterisk/cli.h"
#include "asterisk/utils.h"
#include "asterisk/term.h"
#include "asterisk/paths.h"
#include "asterisk/hashtab.h"
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
Go to the source code of this file.
Macros | |
#define | LUA_BUF_SIZE 4096 |
#define | LUA_EXT_DATA_SIZE 8192 |
#define | LUA_GOTO_DETECTED 5 |
Functions | |
static void | __reg_module (void) |
static void | __unreg_module (void) |
struct ast_module * | AST_MODULE_SELF_SYM (void) |
static int | canmatch (struct ast_channel *chan, const char *context, const char *exten, int priority, const char *callerid, const char *data) |
static int | exec (struct ast_channel *chan, const char *context, const char *exten, int priority, const char *callerid, const char *data) |
static int | exists (struct ast_channel *chan, const char *context, const char *exten, int priority, const char *callerid, const char *data) |
static int | load_module (void) |
static int | load_or_reload_lua_stuff (void) |
static int | lua_autoservice_start (lua_State *L) |
[lua_CFunction] Tell pbx_lua to maintain an autoservice on this channel (for access from lua, don't call directly) More... | |
static int | lua_autoservice_status (lua_State *L) |
[lua_CFunction] Get the status of the autoservice flag (for access from lua, don't call directly) More... | |
static int | lua_autoservice_stop (lua_State *L) |
[lua_CFunction] Tell pbx_lua to stop maintaning an autoservice on this channel (for access from lua, don't call directly) More... | |
static int | lua_check_hangup (lua_State *L) |
[lua_CFunction] Check if this channel has been hungup or not (for access from lua, don't call directly) More... | |
static void | lua_concat_args (lua_State *L, int start, int nargs) |
Concatenate a list of lua function arguments into a comma separated string. More... | |
static void | lua_create_app_table (lua_State *L) |
Create the global 'app' table for executing applications. More... | |
static void | lua_create_application_metatable (lua_State *L) |
Create the 'application' metatable, used to execute asterisk applications from lua. More... | |
static void | lua_create_autoservice_functions (lua_State *L) |
Create the autoservice functions. More... | |
static void | lua_create_channel_table (lua_State *L) |
Create the global 'channel' table for accesing channel variables. More... | |
static void | lua_create_hangup_function (lua_State *L) |
Create the hangup check function. More... | |
static void | lua_create_variable_metatable (lua_State *L) |
Create the 'variable' metatable, used to retrieve channel variables. More... | |
static void | lua_datastore_fixup (void *data, struct ast_channel *old_chan, struct ast_channel *new_chan) |
The fixup function for the lua_datastore. More... | |
static int | lua_error_function (lua_State *L) |
[lua_CFunction] Handle lua errors (for access from lua, don't call directly) More... | |
static int | lua_extension_cmp (lua_State *L) |
[lua_CFunction] Compare two extensions (for access from lua, don't call directly) More... | |
static int | lua_find_extension (lua_State *L, const char *context, const char *exten, int priority, ast_switch_f *func, int push_func) |
Locate an extensions and optionally push the matching function on the stack. More... | |
static void | lua_free_extensions () |
Free the internal extensions buffer. More... | |
static int | lua_func_read (lua_State *L) |
[lua_CFunction] Create a 'variable' object for accessing a dialplan function (for access from lua, don't call directly) More... | |
static lua_State * | lua_get_state (struct ast_channel *chan) |
Get the lua_State for this channel. More... | |
static int | lua_get_variable (lua_State *L) |
[lua_CFunction] Return a lua 'variable' object (for access from lua, don't call directly) More... | |
static int | lua_get_variable_value (lua_State *L) |
[lua_CFunction] Used to get the value of a variable or dialplan function (for access from lua, don't call directly) More... | |
static int | lua_load_extensions (lua_State *L, struct ast_channel *chan) |
Load the extensions.lua file from the internal buffer. More... | |
static int | lua_pbx_exec (lua_State *L) |
[lua_CFunction] This function is part of the 'application' metatable and is used to execute applications similar to pbx_exec() (for access from lua, don't call directly) More... | |
static int | lua_pbx_findapp (lua_State *L) |
[lua_CFunction] Find an app and return it in a lua table (for access from lua, don't call directly) More... | |
static void | lua_push_variable_table (lua_State *L) |
Push a 'variable' table on the stack for access the channel variable with the given name. More... | |
static char * | lua_read_extensions_file (lua_State *L, long *size, int *file_not_openable) |
Load the extensions.lua file in to a buffer and execute the file. More... | |
static int | lua_register_hints (lua_State *L) |
Register dialplan hints for our pbx_lua contexs. More... | |
static int | lua_register_switches (lua_State *L) |
Register dialplan switches for our pbx_lua contexs. More... | |
static int | lua_reload_extensions (lua_State *L) |
Reload the extensions file and update the internal buffers if it loads correctly. More... | |
static int | lua_set_variable (lua_State *L) |
[lua_CFunction] Set the value of a channel variable or dialplan function (for access from lua, don't call directly) More... | |
static int | lua_set_variable_value (lua_State *L) |
[lua_CFunction] Used to set the value of a variable or dialplan function (for access from lua, don't call directly) More... | |
static int | lua_sort_extensions (lua_State *L) |
Store the sort order of each context. More... | |
static void | lua_state_destroy (void *data) |
The destructor for lua_datastore. More... | |
static void | lua_update_registry (lua_State *L, const char *context, const char *exten, int priority) |
Update the lua registry with the given context, exten, and priority. More... | |
static int | matchmore (struct ast_channel *chan, const char *context, const char *exten, int priority, const char *callerid, const char *data) |
static int | reload (void) |
static int | unload_module (void) |
Variables | |
static struct ast_module_info | __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_GLOBAL_SYMBOLS , .description = "Lua PBX Switch" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = "30ef0c93b36035ec78c9cfd712d36d9b" , .support_level = AST_MODULE_SUPPORT_EXTENDED, .load = load_module, .unload = unload_module, .reload = reload, } |
static const struct ast_module_info * | ast_module_info = &__mod_info |
static char * | config = "extensions.lua" |
static char * | config_file_data = NULL |
static ast_mutex_t | config_file_lock = { PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP , NULL, {1, 0} } |
static long | config_file_size = 0 |
static struct ast_context * | local_contexts = NULL |
static struct ast_hashtab * | local_table = NULL |
static const struct ast_datastore_info | lua_datastore |
static struct ast_switch | lua_switch |
static char * | registrar = "pbx_lua" |
Lua PBX Switch.
Definition in file pbx_lua.c.
#define LUA_BUF_SIZE 4096 |
Definition at line 56 of file pbx_lua.c.
Referenced by lua_get_variable(), and lua_get_variable_value().
#define LUA_EXT_DATA_SIZE 8192 |
Definition at line 54 of file pbx_lua.c.
Referenced by lua_pbx_exec().
#define LUA_GOTO_DETECTED 5 |
Definition at line 61 of file pbx_lua.c.
Referenced by exec(), and lua_pbx_exec().
struct ast_module* AST_MODULE_SELF_SYM | ( | void | ) |
|
static |
Definition at line 1358 of file pbx_lua.c.
References ast_log, ast_module_user_add, ast_module_user_remove, LOG_ERROR, lua_find_extension(), and lua_get_state().
Referenced by lua_find_extension().
|
static |
Definition at line 1405 of file pbx_lua.c.
References ast_autoservice_start(), ast_autoservice_stop(), ast_log, ast_module_user_add, ast_module_user_remove, error(), exists(), LOG_ERROR, lua_error_function(), lua_find_extension(), lua_get_state(), LUA_GOTO_DETECTED, and lua_update_registry().
|
static |
Definition at line 1335 of file pbx_lua.c.
References ast_log, ast_module_user_add, ast_module_user_remove, LOG_ERROR, lua_find_extension(), and lua_get_state().
Referenced by exec().
|
static |
Definition at line 1680 of file pbx_lua.c.
References ast_log, AST_MODFLAG_GLOBAL_SYMBOLS, AST_MODULE_INFO(), AST_MODULE_LOAD_FAILURE, AST_MODULE_LOAD_SUCCESS, AST_MODULE_SUPPORT_EXTENDED, ast_register_switch(), ASTERISK_GPL_KEY, load_or_reload_lua_stuff(), LOG_ERROR, reload(), and unload_module().
|
static |
Definition at line 1640 of file pbx_lua.c.
References ast_log, AST_MODULE_LOAD_DECLINE, AST_MODULE_LOAD_FAILURE, AST_MODULE_LOAD_SUCCESS, error(), loaded, LOG_ERROR, and lua_reload_extensions().
Referenced by load_module(), and reload().
|
static |
[lua_CFunction] Tell pbx_lua to maintain an autoservice on this channel (for access from lua, don't call directly)
L | the lua_State to use |
This function will set a flag that will cause pbx_lua to maintain an autoservice on this channel. The autoservice will automatically be stopped and restarted before calling applications and functions.
Definition at line 695 of file pbx_lua.c.
References ast_autoservice_start().
Referenced by lua_create_autoservice_functions().
|
static |
[lua_CFunction] Get the status of the autoservice flag (for access from lua, don't call directly)
L | the lua_State to use |
Definition at line 760 of file pbx_lua.c.
Referenced by lua_create_autoservice_functions().
|
static |
[lua_CFunction] Tell pbx_lua to stop maintaning an autoservice on this channel (for access from lua, don't call directly)
L | the lua_State to use |
This function will stop any autoservice running and turn off the autoservice flag. If this function returns false, it's probably because no autoservice was running to begin with.
Definition at line 728 of file pbx_lua.c.
References ast_autoservice_stop().
Referenced by lua_create_autoservice_functions().
|
static |
[lua_CFunction] Check if this channel has been hungup or not (for access from lua, don't call directly)
L | the lua_State to use |
Definition at line 774 of file pbx_lua.c.
References ast_check_hangup().
Referenced by lua_create_hangup_function().
|
static |
Concatenate a list of lua function arguments into a comma separated string.
L | the lua_State to use |
start | the index of the first argument |
nargs | the number of args |
The resulting string will be left on the top of the stack.
Definition at line 631 of file pbx_lua.c.
Referenced by lua_func_read(), and lua_pbx_exec().
|
static |
Create the global 'app' table for executing applications.
L | the lua_State to use |
Definition at line 445 of file pbx_lua.c.
References lua_pbx_findapp().
Referenced by lua_load_extensions().
|
static |
Create the 'application' metatable, used to execute asterisk applications from lua.
L | the lua_State to use |
Definition at line 502 of file pbx_lua.c.
References lua_pbx_exec().
Referenced by lua_load_extensions().
|
static |
Create the autoservice functions.
L | the lua_State to use |
Definition at line 518 of file pbx_lua.c.
References lua_autoservice_start(), lua_autoservice_status(), and lua_autoservice_stop().
Referenced by lua_load_extensions().
|
static |
Create the global 'channel' table for accesing channel variables.
L | the lua_State to use |
Definition at line 463 of file pbx_lua.c.
References lua_get_variable(), and lua_set_variable().
Referenced by lua_load_extensions().
|
static |
Create the hangup check function.
L | the lua_State to use |
Definition at line 538 of file pbx_lua.c.
References lua_check_hangup().
Referenced by lua_load_extensions().
|
static |
Create the 'variable' metatable, used to retrieve channel variables.
L | the lua_State to use |
Definition at line 485 of file pbx_lua.c.
References lua_func_read().
Referenced by lua_load_extensions().
|
static |
|
static |
[lua_CFunction] Handle lua errors (for access from lua, don't call directly)
L | the lua_State to use |
Definition at line 791 of file pbx_lua.c.
Referenced by exec(), and lua_read_extensions_file().
|
static |
[lua_CFunction] Compare two extensions (for access from lua, don't call directly)
This function returns true if the first extension passed should match after the second. It behaves like the '<' operator.
Definition at line 1067 of file pbx_lua.c.
References a, ast_extension_cmp(), and b.
Referenced by lua_sort_extensions().
|
static |
Locate an extensions and optionally push the matching function on the stack.
L | the lua_State to use |
context | the context to look in |
exten | the extension to look up |
priority | the priority to check, '1' is the only valid priority |
func | the calling func, used to adjust matching behavior between, match, canmatch, and matchmore |
push_func | whether or not to push the lua function for the given extension onto the stack |
Definition at line 1493 of file pbx_lua.c.
References ast_debug, ast_extension_close(), ast_extension_match(), ast_log, c, canmatch(), E_CANMATCH, E_MATCHMORE, LOG_ERROR, match(), and matchmore().
Referenced by canmatch(), exec(), exists(), and matchmore().
|
static |
Free the internal extensions buffer.
Definition at line 1252 of file pbx_lua.c.
References ast_free, ast_mutex_lock, ast_mutex_unlock, config_file_data, config_file_lock, and config_file_size.
Referenced by unload_module().
|
static |
[lua_CFunction] Create a 'variable' object for accessing a dialplan function (for access from lua, don't call directly)
This function is called to create a 'variable' object to access a dialplan function. It would be called in the following example as would be seen in extensions.lua.
To actually do anything with the resulting value you must use the 'get()' and 'set()' methods (the reason is the resulting value is not a value, but an object in the form of a lua table).
Definition at line 670 of file pbx_lua.c.
References lua_concat_args(), and lua_push_variable_table().
Referenced by lua_create_variable_metatable().
|
static |
Get the lua_State for this channel.
If no channel is passed then a new state is allocated. States with no channel assocatied with them should only be used for matching extensions. If the channel does not yet have a lua state associated with it, one will be created.
Definition at line 1273 of file pbx_lua.c.
References ast_channel_datastore_add(), ast_channel_datastore_find(), ast_channel_datastore_remove(), ast_channel_lock, ast_channel_name(), ast_channel_unlock, ast_datastore_alloc, ast_datastore_free(), ast_log, ast_datastore::data, error(), LOG_ERROR, lua_load_extensions(), and NULL.
Referenced by canmatch(), exec(), exists(), and matchmore().
|
static |
[lua_CFunction] Return a lua 'variable' object (for access from lua, don't call directly)
This function is called to lookup a variable construct a 'variable' object. It would be called in the following example as would be seen in extensions.lua.
Definition at line 556 of file pbx_lua.c.
References ast_alloca, ast_channel_varshead(), ast_strlen_zero, LUA_BUF_SIZE, lua_push_variable_table(), name, NULL, pbx_retrieve_variable(), and value.
Referenced by lua_create_channel_table().
|
static |
[lua_CFunction] Used to get the value of a variable or dialplan function (for access from lua, don't call directly)
The value of the variable or function is returned. This function is the 'get()' function in the following example as would be seen in extensions.lua.
Definition at line 300 of file pbx_lua.c.
References ast_alloca, ast_autoservice_start(), ast_autoservice_stop(), ast_channel_varshead(), ast_func_read(), ast_strdupa, ast_strlen_zero, LUA_BUF_SIZE, name, NULL, pbx_retrieve_variable(), and value.
Referenced by lua_push_variable_table().
|
static |
Load the extensions.lua file from the internal buffer.
L | the lua_State to use |
chan | channel to work on |
This function also sets up some constructs used by the extensions.lua file. In the event of an error, an error string will be pushed onto the lua stack.
0 | success |
1 | failure |
Definition at line 1170 of file pbx_lua.c.
References ast_mutex_lock, ast_mutex_unlock, config_file_data, config_file_lock, config_file_size, lua_create_app_table(), lua_create_application_metatable(), lua_create_autoservice_functions(), lua_create_channel_table(), lua_create_hangup_function(), lua_create_variable_metatable(), and lua_sort_extensions().
Referenced by lua_get_state().
|
static |
[lua_CFunction] This function is part of the 'application' metatable and is used to execute applications similar to pbx_exec() (for access from lua, don't call directly)
L | the lua_State to use |
This funciton is executed as the '()' operator for apps accessed through the 'app' table.
Definition at line 186 of file pbx_lua.c.
References app, app_name(), ast_autoservice_start(), ast_autoservice_stop(), ast_channel_context(), ast_channel_exten(), ast_channel_name(), ast_channel_priority(), ast_debug, ast_strdupa, ast_verb, COLOR_BRCYAN, COLOR_BRMAGENTA, context, exten, lua_concat_args(), LUA_EXT_DATA_SIZE, LUA_GOTO_DETECTED, lua_update_registry(), pbx_exec(), pbx_findapp(), priority, term_color(), and tmp().
Referenced by lua_create_application_metatable().
|
static |
[lua_CFunction] Find an app and return it in a lua table (for access from lua, don't call directly)
This function would be called in the following example as it would be found in extensions.lua.
Definition at line 155 of file pbx_lua.c.
References app_name().
Referenced by lua_create_app_table().
|
static |
Push a 'variable' table on the stack for access the channel variable with the given name.
The value on the top of the stack is popped and used as the name.
L | the lua_State to use |
Definition at line 424 of file pbx_lua.c.
References lua_get_variable_value(), and lua_set_variable_value().
Referenced by lua_func_read(), and lua_get_variable().
|
static |
Load the extensions.lua file in to a buffer and execute the file.
L | the lua_State to use |
size | a pointer to store the size of the buffer |
file_not_openable | a pointer to store if config file could be opened |
Definition at line 1091 of file pbx_lua.c.
References ast_alloca, ast_config_AST_CONFIG_DIR, ast_free, ast_malloc, config, errno, lua_error_function(), lua_register_hints(), lua_register_switches(), lua_sort_extensions(), and NULL.
Referenced by lua_reload_extensions().
|
static |
Register dialplan hints for our pbx_lua contexs.
In the event of an error, an error string will be pushed onto the lua stack.
0 | success |
1 | failure |
Definition at line 987 of file pbx_lua.c.
References ast_add_extension2(), ast_context_find_or_create(), ast_hashtab_compare_contexts(), ast_hashtab_create, ast_hashtab_hash_contexts(), ast_hashtab_newsize_java(), ast_hashtab_resize_java(), context, context_name, hints, NULL, PRIORITY_HINT, and registrar.
Referenced by lua_read_extensions_file().
|
static |
Register dialplan switches for our pbx_lua contexs.
In the event of an error, an error string will be pushed onto the lua stack.
0 | success |
1 | failure |
Definition at line 929 of file pbx_lua.c.
References ast_context_add_switch2(), ast_context_find_or_create(), ast_hashtab_compare_contexts(), ast_hashtab_create, ast_hashtab_hash_contexts(), ast_hashtab_newsize_java(), ast_hashtab_resize_java(), context, context_name, NULL, and registrar.
Referenced by lua_read_extensions_file().
|
static |
Reload the extensions file and update the internal buffers if it loads correctly.
L | the lua_State to use (must be freshly allocated with luaL_newstate(), don't use lua_get_state()) |
Definition at line 1213 of file pbx_lua.c.
References ast_free, ast_merge_contexts_and_delete(), ast_mutex_lock, ast_mutex_unlock, config_file_data, config_file_lock, config_file_size, lua_read_extensions_file(), NULL, and registrar.
Referenced by load_or_reload_lua_stuff().
|
static |
[lua_CFunction] Set the value of a channel variable or dialplan function (for access from lua, don't call directly)
This function is called to set a variable or dialplan function. It would be called in the following example as would be seen in extensions.lua.
Definition at line 596 of file pbx_lua.c.
References ast_autoservice_start(), ast_autoservice_stop(), name, pbx_builtin_setvar_helper(), and value.
Referenced by lua_create_channel_table().
|
static |
[lua_CFunction] Used to set the value of a variable or dialplan function (for access from lua, don't call directly)
This function is the 'set()' function in the following example as would be seen in extensions.lua.
Definition at line 360 of file pbx_lua.c.
References ast_autoservice_start(), ast_autoservice_stop(), ast_strdupa, name, pbx_builtin_setvar_helper(), and value.
Referenced by lua_push_variable_table().
|
static |
Store the sort order of each context.
In the event of an error, an error string will be pushed onto the lua stack.
0 | success |
1 | failure |
Definition at line 844 of file pbx_lua.c.
References context, context_name, exten, and lua_extension_cmp().
Referenced by lua_load_extensions(), and lua_read_extensions_file().
|
static |
|
static |
Update the lua registry with the given context, exten, and priority.
L | the lua_State to use |
context | the new context |
exten | the new exten |
priority | the new priority |
Definition at line 404 of file pbx_lua.c.
Referenced by exec(), and lua_pbx_exec().
|
static |
Definition at line 1381 of file pbx_lua.c.
References ast_log, ast_module_user_add, ast_module_user_remove, LOG_ERROR, lua_find_extension(), and lua_get_state().
Referenced by complete_dpreply(), and lua_find_extension().
|
static |
Definition at line 1675 of file pbx_lua.c.
References load_or_reload_lua_stuff().
Referenced by load_module().
|
static |
Definition at line 1667 of file pbx_lua.c.
References ast_context_destroy(), ast_unregister_switch(), lua_free_extensions(), NULL, and registrar.
Referenced by load_module().
|
static |
|
static |
|
static |
Definition at line 48 of file pbx_lua.c.
Referenced by lua_read_extensions_file().
|
static |
Definition at line 107 of file pbx_lua.c.
Referenced by lua_free_extensions(), lua_load_extensions(), and lua_reload_extensions().
|
static |
Definition at line 106 of file pbx_lua.c.
Referenced by lua_free_extensions(), lua_load_extensions(), and lua_reload_extensions().
|
static |
Definition at line 108 of file pbx_lua.c.
Referenced by lua_free_extensions(), lua_load_extensions(), and lua_reload_extensions().
|
static |
|
static |
|
static |
|
static |
|
static |
Definition at line 49 of file pbx_lua.c.
Referenced by lua_register_hints(), lua_register_switches(), lua_reload_extensions(), and unload_module().