Newer
Older
* Asterisk -- An open source telephony toolkit.
* Copyright (c) 2004 - 2005, Tilghman Lesher. All rights reserved.
Tilghman Lesher
committed
* Portions copyright (c) 2006, Philipp Dunkel.
Tilghman Lesher
committed
* Tilghman Lesher <app_exec__v002@the-tilghman.com>
*
* This code is released by the author with no restrictions on usage.
*
* See http://www.asterisk.org for more information about
* the Asterisk project. Please do not directly contact
* any of the maintainers of this project for assistance;
* the project provides a web site, mailing lists and IRC
* channels for your use.
*
*/
* \brief Exec application
Tilghman Lesher
committed
* \author Tilghman Lesher <app_exec__v002@the-tilghman.com>
* \author Philipp Dunkel <philipp.dunkel@ebox.at>
Kevin P. Fleming
committed
#include "asterisk.h"
ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
Kevin P. Fleming
committed
#include "asterisk/file.h"
#include "asterisk/channel.h"
#include "asterisk/pbx.h"
#include "asterisk/module.h"
Tilghman Lesher
committed
#include "asterisk/app.h"
/* Maximum length of any variable */
#define MAXRESULT 1024
Tilghman Lesher
committed
/*! Note
*
* The key difference between these two apps is exit status. In a
* nutshell, Exec tries to be transparent as possible, behaving
* in exactly the same way as if the application it calls was
* directly invoked from the dialplan.
*
* TryExec, on the other hand, provides a way to execute applications
* and catch any possible fatal error without actually fatally
* affecting the dialplan.
*/
Tilghman Lesher
committed
static char *app_exec = "Exec";
static char *exec_synopsis = "Executes dialplan application";
" Exec(appname(arguments)):\n"
"Allows an arbitrary application to be invoked even when not\n"
Tilghman Lesher
committed
"hardcoded into the dialplan. If the underlying application\n"
"terminates the dialplan, or if the application cannot be found,\n"
"Exec will terminate the dialplan.\n"
" To invoke external applications, see the application System.\n"
" If you would like to catch any error instead, see TryExec.\n";
static char *app_tryexec = "TryExec";
static char *tryexec_synopsis = "Executes dialplan application, always returning";
static char *tryexec_descrip =
" TryExec(appname(arguments)):\n"
"Allows an arbitrary application to be invoked even when not\n"
"hardcoded into the dialplan. To invoke external applications\n"
Tilghman Lesher
committed
"see the application System. Always returns to the dialplan.\n"
"The channel variable TRYSTATUS will be set to one of:\n"
Tilghman Lesher
committed
" SUCCESS if the application returned zero\n"
" FAILED if the application returned non-zero\n"
Russell Bryant
committed
" NOAPP if the application was not found or was not specified\n";
static char *app_execif = "ExecIf";
static char *execif_synopsis = "Executes dialplan application, conditionally";
static char *execif_descrip =
Jason Parker
committed
" ExecIF (<expr>?<appiftrue>(<args>)[:<appiffalse>(<args>)])\n"
"If <expr> is true, execute and return the result of <appiftrue>(<args>).\n"
"If <expr> is true, but <appiftrue> is not found, then the application\n"
"will return a non-zero value.\n";
static int exec_exec(struct ast_channel *chan, void *data)
{
int res = 0;
Steve Murphy
committed
char *s, *appname, *endargs, args[MAXRESULT];
if (ast_strlen_zero(data))
return 0;
s = ast_strdupa(data);
Steve Murphy
committed
args[0] = 0;
appname = strsep(&s, "(");
if (s) {
endargs = strrchr(s, ')');
if (endargs)
*endargs = '\0';
pbx_substitute_variables_helper(chan, s, args, MAXRESULT - 1);
}
if (appname) {
app = pbx_findapp(appname);
if (app) {
res = pbx_exec(chan, app, args);
} else {
ast_log(LOG_WARNING, "Could not find application (%s)\n", appname);
res = -1;
Russell Bryant
committed
}
Tilghman Lesher
committed
static int tryexec_exec(struct ast_channel *chan, void *data)
{
int res = 0;
Steve Murphy
committed
char *s, *appname, *endargs, args[MAXRESULT];
Tilghman Lesher
committed
struct ast_app *app;
if (ast_strlen_zero(data))
return 0;
s = ast_strdupa(data);
Steve Murphy
committed
args[0] = 0;
appname = strsep(&s, "(");
if (s) {
endargs = strrchr(s, ')');
if (endargs)
*endargs = '\0';
pbx_substitute_variables_helper(chan, s, args, MAXRESULT - 1);
}
if (appname) {
app = pbx_findapp(appname);
if (app) {
res = pbx_exec(chan, app, args);
pbx_builtin_setvar_helper(chan, "TRYSTATUS", res ? "FAILED" : "SUCCESS");
} else {
ast_log(LOG_WARNING, "Could not find application (%s)\n", appname);
pbx_builtin_setvar_helper(chan, "TRYSTATUS", "NOAPP");
Tilghman Lesher
committed
}
}
return 0;
}
static int execif_exec(struct ast_channel *chan, void *data)
{
int res = 0;
Tilghman Lesher
committed
char *truedata = NULL, *falsedata = NULL, *end;
struct ast_app *app = NULL;
Tilghman Lesher
committed
AST_DECLARE_APP_ARGS(expr,
AST_APP_ARG(expr);
AST_APP_ARG(remainder);
);
AST_DECLARE_APP_ARGS(apps,
Tilghman Lesher
committed
);
char *parse = ast_strdupa(data);
AST_NONSTANDARD_APP_ARGS(expr, parse, '?');
if (ast_strlen_zero(expr.remainder)) {
Jason Parker
committed
ast_log(LOG_ERROR, "Usage: ExecIf(<expr>?<appiftrue>(<args>)[:<appiffalse>(<args)])\n");
return -1;
}
Tilghman Lesher
committed
AST_NONSTANDARD_APP_ARGS(apps, expr.remainder, ':');
if (apps.t && (truedata = strchr(apps.t, '('))) {
Tilghman Lesher
committed
*truedata++ = '\0';
if ((end = strrchr(truedata, ')')))
*end = '\0';
}
if (apps.f && (falsedata = strchr(apps.f, '('))) {
Tilghman Lesher
committed
*falsedata++ = '\0';
if ((end = strrchr(falsedata, ')')))
*end = '\0';
}
Tilghman Lesher
committed
if (pbx_checkcondition(expr.expr)) {
if (!ast_strlen_zero(apps.t) && (app = pbx_findapp(apps.t))) {
Tilghman Lesher
committed
res = pbx_exec(chan, app, S_OR(truedata, ""));
} else {
ast_log(LOG_WARNING, "Could not find application! (%s)\n", apps.t);
Tilghman Lesher
committed
res = -1;
Jason Parker
committed
} else if (!ast_strlen_zero(apps.f)) {
if ((app = pbx_findapp(apps.f))) {
Tilghman Lesher
committed
res = pbx_exec(chan, app, S_OR(falsedata, ""));
} else {
ast_log(LOG_WARNING, "Could not find application! (%s)\n", apps.f);
Tilghman Lesher
committed
res = -1;
}
return res;
}
static int unload_module(void)
Russell Bryant
committed
int res;
res = ast_unregister_application(app_exec);
Tilghman Lesher
committed
res |= ast_unregister_application(app_tryexec);
res |= ast_unregister_application(app_execif);
Russell Bryant
committed
return res;
static int load_module(void)
Tilghman Lesher
committed
int res = ast_register_application(app_exec, exec_exec, exec_synopsis, exec_descrip);
res |= ast_register_application(app_tryexec, tryexec_exec, tryexec_synopsis, tryexec_descrip);
res |= ast_register_application(app_execif, execif_exec, execif_synopsis, execif_descrip);
Tilghman Lesher
committed
return res;
AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Executes dialplan applications");