mirror of
https://github.com/asterisk/asterisk.git
synced 2025-09-05 12:16:00 +00:00
pbx: Add helper function to execute applications.
Finding an application and executing it if found is a common task throughout Asterisk. This adds a helper function around pbx_exec to do this, to eliminate redundant code and make it easier for modules to substitute variables and execute applications by name. ASTERISK-30061 #close Change-Id: Ifee4d2825df7545fb515d763d393065675140c84
This commit is contained in:
committed by
Friendly Automation
parent
2d500a090f
commit
6720caa29c
@@ -361,7 +361,6 @@ static int disa_exec(struct ast_channel *chan, const char *data)
|
|||||||
|
|
||||||
if (k == 3) {
|
if (k == 3) {
|
||||||
int recheck = 0;
|
int recheck = 0;
|
||||||
struct ast_app *app_reset_cdr;
|
|
||||||
|
|
||||||
if (!ast_exists_extension(chan, args.context, exten, 1,
|
if (!ast_exists_extension(chan, args.context, exten, 1,
|
||||||
S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL))) {
|
S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL))) {
|
||||||
@@ -386,10 +385,7 @@ static int disa_exec(struct ast_channel *chan, const char *data)
|
|||||||
ast_channel_unlock(chan);
|
ast_channel_unlock(chan);
|
||||||
}
|
}
|
||||||
|
|
||||||
app_reset_cdr = pbx_findapp("ResetCDR");
|
if (ast_pbx_exec_application(chan, "ResetCDR", special_noanswer ? "" : "e")) {
|
||||||
if (app_reset_cdr) {
|
|
||||||
pbx_exec(chan, app_reset_cdr, special_noanswer ? "" : "e");
|
|
||||||
} else {
|
|
||||||
ast_log(AST_LOG_NOTICE, "ResetCDR application not found; CDR will not be reset\n");
|
ast_log(AST_LOG_NOTICE, "ResetCDR application not found; CDR will not be reset\n");
|
||||||
}
|
}
|
||||||
ast_explicit_goto(chan, args.context, exten, 1);
|
ast_explicit_goto(chan, args.context, exten, 1);
|
||||||
|
@@ -268,6 +268,23 @@ struct ast_app *pbx_findapp(const char *app);
|
|||||||
*/
|
*/
|
||||||
int pbx_exec(struct ast_channel *c, struct ast_app *app, const char *data);
|
int pbx_exec(struct ast_channel *c, struct ast_app *app, const char *data);
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* \brief Execute an application
|
||||||
|
*
|
||||||
|
* \param c channel to execute on
|
||||||
|
* \param app name of app to execute
|
||||||
|
* \param data the data passed into the app
|
||||||
|
*
|
||||||
|
* This application executes an application by name on a given channel.
|
||||||
|
* It is a wrapper around pbx_exec that will perform variable substitution
|
||||||
|
* and then execute the application if it exists.
|
||||||
|
* If the application is not found, a warning is logged.
|
||||||
|
*
|
||||||
|
* \retval 0 success
|
||||||
|
* \retval -1 failure (including application not found)
|
||||||
|
*/
|
||||||
|
int ast_pbx_exec_application(struct ast_channel *chan, const char *app_name, const char *app_args);
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* \brief Register a new context or find an existing one
|
* \brief Register a new context or find an existing one
|
||||||
*
|
*
|
||||||
|
@@ -1178,23 +1178,7 @@ static int run_app_helper(struct ast_channel *chan, const char *app_name, const
|
|||||||
} else if (!strcasecmp("Macro", app_name)) {
|
} else if (!strcasecmp("Macro", app_name)) {
|
||||||
ast_app_exec_macro(NULL, chan, app_args);
|
ast_app_exec_macro(NULL, chan, app_args);
|
||||||
} else {
|
} else {
|
||||||
struct ast_app *app;
|
res = ast_pbx_exec_application(chan, app_name, app_args);
|
||||||
|
|
||||||
app = pbx_findapp(app_name);
|
|
||||||
if (!app) {
|
|
||||||
ast_log(LOG_WARNING, "Could not find application (%s)\n", app_name);
|
|
||||||
} else {
|
|
||||||
struct ast_str *substituted_args = ast_str_create(16);
|
|
||||||
|
|
||||||
if (substituted_args) {
|
|
||||||
ast_str_substitute_variables(&substituted_args, 0, chan, app_args);
|
|
||||||
res = pbx_exec(chan, app, ast_str_buffer(substituted_args));
|
|
||||||
ast_free(substituted_args);
|
|
||||||
} else {
|
|
||||||
ast_log(LOG_WARNING, "Could not substitute application argument variables for %s\n", app_name);
|
|
||||||
res = pbx_exec(chan, app, app_args);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
10
main/dial.c
10
main/dial.c
@@ -166,14 +166,12 @@ static int predial_disable(void *data)
|
|||||||
static void answer_exec_run(struct ast_dial *dial, struct ast_dial_channel *dial_channel, char *app, char *args)
|
static void answer_exec_run(struct ast_dial *dial, struct ast_dial_channel *dial_channel, char *app, char *args)
|
||||||
{
|
{
|
||||||
struct ast_channel *chan = dial_channel->owner;
|
struct ast_channel *chan = dial_channel->owner;
|
||||||
struct ast_app *ast_app = pbx_findapp(app);
|
|
||||||
|
|
||||||
/* If the application was not found, return immediately */
|
/* Execute the application, if available */
|
||||||
if (!ast_app)
|
if (ast_pbx_exec_application(chan, app, args)) {
|
||||||
|
/* If the application was not found, return immediately */
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
/* All is well... execute the application */
|
|
||||||
pbx_exec(chan, ast_app, args);
|
|
||||||
|
|
||||||
/* If another thread is not taking over hang up the channel */
|
/* If another thread is not taking over hang up the channel */
|
||||||
ast_mutex_lock(&dial->lock);
|
ast_mutex_lock(&dial->lock);
|
||||||
|
@@ -504,12 +504,7 @@ static void bridge_check_monitor(struct ast_channel *chan, struct ast_channel *p
|
|||||||
ast_channel_unlock(peer);
|
ast_channel_unlock(peer);
|
||||||
}
|
}
|
||||||
if (monitor_chan) {
|
if (monitor_chan) {
|
||||||
struct ast_app *monitor_app;
|
ast_pbx_exec_application(monitor_chan, "Monitor", monitor_args);
|
||||||
|
|
||||||
monitor_app = pbx_findapp("Monitor");
|
|
||||||
if (monitor_app) {
|
|
||||||
pbx_exec(monitor_chan, monitor_app, monitor_args);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -498,6 +498,31 @@ int pbx_exec(struct ast_channel *c, /*!< Channel */
|
|||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int ast_pbx_exec_application(struct ast_channel *chan, const char *app_name, const char *app_args)
|
||||||
|
{
|
||||||
|
int res = -1;
|
||||||
|
struct ast_app *app;
|
||||||
|
|
||||||
|
app = pbx_findapp(app_name);
|
||||||
|
if (!app) {
|
||||||
|
ast_log(LOG_WARNING, "Could not find application (%s)\n", app_name);
|
||||||
|
} else {
|
||||||
|
struct ast_str *substituted_args = NULL;
|
||||||
|
|
||||||
|
if (!ast_strlen_zero(app_args) && (substituted_args = ast_str_create(16))) {
|
||||||
|
ast_str_substitute_variables(&substituted_args, 0, chan, app_args);
|
||||||
|
res = pbx_exec(chan, app, ast_str_buffer(substituted_args));
|
||||||
|
ast_free(substituted_args);
|
||||||
|
} else {
|
||||||
|
if (!ast_strlen_zero(app_args)) {
|
||||||
|
ast_log(LOG_WARNING, "Could not substitute application argument variables for %s\n", app_name);
|
||||||
|
}
|
||||||
|
res = pbx_exec(chan, app, app_args);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
static struct ast_cli_entry app_cli[] = {
|
static struct ast_cli_entry app_cli[] = {
|
||||||
AST_CLI_DEFINE(handle_show_applications, "Shows registered dialplan applications"),
|
AST_CLI_DEFINE(handle_show_applications, "Shows registered dialplan applications"),
|
||||||
AST_CLI_DEFINE(handle_show_application, "Describe a specific dialplan application"),
|
AST_CLI_DEFINE(handle_show_application, "Describe a specific dialplan application"),
|
||||||
|
@@ -1000,7 +1000,6 @@ static int pbx_builtin_execiftime(struct ast_channel *chan, const char *data)
|
|||||||
{
|
{
|
||||||
char *s, *appname;
|
char *s, *appname;
|
||||||
struct ast_timing timing;
|
struct ast_timing timing;
|
||||||
struct ast_app *app;
|
|
||||||
static const char * const usage = "ExecIfTime requires an argument:\n <time range>,<days of week>,<days of month>,<months>[,<timezone>]?<appname>[(<appargs>)]";
|
static const char * const usage = "ExecIfTime requires an argument:\n <time range>,<days of week>,<days of month>,<months>[,<timezone>]?<appname>[(<appargs>)]";
|
||||||
|
|
||||||
if (ast_strlen_zero(data)) {
|
if (ast_strlen_zero(data)) {
|
||||||
@@ -1038,13 +1037,7 @@ static int pbx_builtin_execiftime(struct ast_channel *chan, const char *data)
|
|||||||
ast_log(LOG_WARNING, "Failed to find closing parenthesis\n");
|
ast_log(LOG_WARNING, "Failed to find closing parenthesis\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return ast_pbx_exec_application(chan, appname, S_OR(s, ""));
|
||||||
if ((app = pbx_findapp(appname))) {
|
|
||||||
return pbx_exec(chan, app, S_OR(s, ""));
|
|
||||||
} else {
|
|
||||||
ast_log(LOG_WARNING, "Cannot locate application %s\n", appname);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
|
@@ -263,14 +263,8 @@ static struct ast_channel_tech snoop_tech = {
|
|||||||
static void *snoop_stasis_thread(void *obj)
|
static void *snoop_stasis_thread(void *obj)
|
||||||
{
|
{
|
||||||
RAII_VAR(struct stasis_app_snoop *, snoop, obj, ao2_cleanup);
|
RAII_VAR(struct stasis_app_snoop *, snoop, obj, ao2_cleanup);
|
||||||
struct ast_app *stasis = pbx_findapp("Stasis");
|
|
||||||
|
|
||||||
if (!stasis) {
|
ast_pbx_exec_application(snoop->chan, "Stasis", ast_str_buffer(snoop->app));
|
||||||
ast_hangup(snoop->chan);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
pbx_exec(snoop->chan, stasis, ast_str_buffer(snoop->app));
|
|
||||||
|
|
||||||
ast_hangup(snoop->chan);
|
ast_hangup(snoop->chan);
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user