/* * Asterisk -- An open source telephony toolkit. * * Copyright (c) 2004 - 2005, Tilghman Lesher. All rights reserved. * Portions copyright (c) 2006, Philipp Dunkel. * * Tilghman Lesher * * 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. * */ /*! \file * * \brief Exec application * * \author Tilghman Lesher * \author Philipp Dunkel * * \ingroup applications */ #include "asterisk.h" ASTERISK_FILE_VERSION(__FILE__, "$Revision$") #include "asterisk/file.h" #include "asterisk/channel.h" #include "asterisk/pbx.h" #include "asterisk/module.h" #include "asterisk/app.h" /*** DOCUMENTATION Executes dialplan application. Application name and arguments of the dialplan application to execute. Allows an arbitrary application to be invoked even when not hard coded into the dialplan. If the underlying application terminates the dialplan, or if the application cannot be found, Exec will terminate the dialplan. To invoke external applications, see the application System. If you would like to catch any error instead, see TryExec. Executes dialplan application, always returning. Allows an arbitrary application to be invoked even when not hard coded into the dialplan. To invoke external applications see the application System. Always returns to the dialplan. The channel variable TRYSTATUS will be set to one of: If the application returned zero. If the application returned non-zero. If the application was not found or was not specified. Executes dialplan application, conditionally. If expr is true, execute and return the result of appiftrue(args). If expr is true, but appiftrue is not found, then the application will return a non-zero value. ***/ /* Maximum length of any variable */ #define MAXRESULT 1024 /*! 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. */ static char *app_exec = "Exec"; static char *app_tryexec = "TryExec"; static char *app_execif = "ExecIf"; static int exec_exec(struct ast_channel *chan, void *data) { int res = 0; char *s, *appname, *endargs, args[MAXRESULT]; struct ast_app *app; if (ast_strlen_zero(data)) return 0; s = ast_strdupa(data); 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; } } return res; } static int tryexec_exec(struct ast_channel *chan, void *data) { int res = 0; char *s, *appname, *endargs, args[MAXRESULT]; struct ast_app *app; if (ast_strlen_zero(data)) return 0; s = ast_strdupa(data); 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"); } } return 0; } static int execif_exec(struct ast_channel *chan, void *data) { int res = 0; char *truedata = NULL, *falsedata = NULL, *end, *firstcomma, *firstquestion; struct ast_app *app = NULL; AST_DECLARE_APP_ARGS(expr, AST_APP_ARG(expr); AST_APP_ARG(remainder); ); AST_DECLARE_APP_ARGS(apps, AST_APP_ARG(t); AST_APP_ARG(f); ); char *parse = ast_strdupa(data); firstcomma = strchr(parse, ','); firstquestion = strchr(parse, '?'); if ((firstcomma != NULL && firstquestion != NULL && firstcomma < firstquestion) || (firstquestion == NULL)) { /* Deprecated syntax */ AST_DECLARE_APP_ARGS(depr, AST_APP_ARG(expr); AST_APP_ARG(appname); AST_APP_ARG(appargs); ); AST_STANDARD_APP_ARGS(depr, parse); ast_log(LOG_WARNING, "Deprecated syntax found. Please upgrade to using ExecIf(?%s(%s))\n", depr.appname, depr.appargs); /* Make the two syntaxes look the same */ expr.expr = depr.expr; apps.t = depr.appname; apps.f = NULL; truedata = depr.appargs; } else { /* Preferred syntax */ AST_NONSTANDARD_RAW_ARGS(expr, parse, '?'); if (ast_strlen_zero(expr.remainder)) { ast_log(LOG_ERROR, "Usage: ExecIf(?()[:(