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>
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "asterisk.h"
ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
Kevin P. Fleming
committed
#include "asterisk/file.h"
#include "asterisk/logger.h"
#include "asterisk/options.h"
#include "asterisk/channel.h"
#include "asterisk/pbx.h"
#include "asterisk/module.h"
/* Maximum length of any variable */
#define MAXRESULT 1024
Tilghman Lesher
committed
static char *tdesc = "Executes dialplan applications";
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";
"Usage: 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 =
"Usage: 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:\n"
" SUCCESS if the application returned zero\n"
" FAILED if the application returned non-zero\n"
" NOAPP if the application was not found or was not specified\n"
" NOMEMORY if there was not enough memory to execute.\n";
LOCAL_USER_DECL;
static int exec_exec(struct ast_channel *chan, void *data)
{
int res=0;
struct localuser *u;
Tilghman Lesher
committed
char *s, *appname, *endargs, args[MAXRESULT] = "";
struct ast_app *app;
LOCAL_USER_ADD(u);
/* Check and parse arguments */
if (data) {
if ((s = ast_strdupa(data))) {
Russell Bryant
committed
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);
Russell Bryant
committed
} else {
ast_log(LOG_WARNING, "Could not find application (%s)\n", appname);
res = -1;
}
Russell Bryant
committed
res = -1;
}
LOCAL_USER_REMOVE(u);
return res;
}
Tilghman Lesher
committed
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
static int tryexec_exec(struct ast_channel *chan, void *data)
{
int res=0;
struct localuser *u;
char *s, *appname, *endargs, args[MAXRESULT] = "";
struct ast_app *app;
LOCAL_USER_ADD(u);
/* Check and parse arguments */
if (data) {
if ((s = ast_strdupa(data))) {
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");
}
}
} else {
ast_log(LOG_ERROR, "Out of memory\n");
pbx_builtin_setvar_helper(chan, "TRYSTATUS", "NOMEMORY");
}
}
LOCAL_USER_REMOVE(u);
return 0;
}
static int unload_module(void *mod)
Russell Bryant
committed
int res;
res = ast_unregister_application(app_exec);
Tilghman Lesher
committed
res |= ast_unregister_application(app_tryexec);
Russell Bryant
committed
Russell Bryant
committed
return res;
static int load_module(void *mod)
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);
return res;
static const char *description(void)
static const char *key(void)
STD_MOD1;