re-doxygen some comments
[asterisk-bristuff.git] / apps / app_exec.c
blob0aafac343424e901f91a81ade231627bff414a48
1 /*
2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (c) 2004 - 2005, Tilghman Lesher. All rights reserved.
5 * Portions copyright (c) 2006, Philipp Dunkel.
7 * Tilghman Lesher <app_exec__v002@the-tilghman.com>
9 * This code is released by the author with no restrictions on usage.
11 * See http://www.asterisk.org for more information about
12 * the Asterisk project. Please do not directly contact
13 * any of the maintainers of this project for assistance;
14 * the project provides a web site, mailing lists and IRC
15 * channels for your use.
19 /*! \file
21 * \brief Exec application
23 * \author Tilghman Lesher <app_exec__v002@the-tilghman.com>
24 * \author Philipp Dunkel <philipp.dunkel@ebox.at>
26 * \ingroup applications
29 #include "asterisk.h"
31 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <unistd.h>
36 #include <string.h>
38 #include "asterisk/file.h"
39 #include "asterisk/logger.h"
40 #include "asterisk/options.h"
41 #include "asterisk/channel.h"
42 #include "asterisk/pbx.h"
43 #include "asterisk/module.h"
45 /* Maximum length of any variable */
46 #define MAXRESULT 1024
48 /*! Note
50 * The key difference between these two apps is exit status. In a
51 * nutshell, Exec tries to be transparent as possible, behaving
52 * in exactly the same way as if the application it calls was
53 * directly invoked from the dialplan.
55 * TryExec, on the other hand, provides a way to execute applications
56 * and catch any possible fatal error without actually fatally
57 * affecting the dialplan.
60 static char *app_exec = "Exec";
61 static char *exec_synopsis = "Executes dialplan application";
62 static char *exec_descrip =
63 "Usage: Exec(appname(arguments))\n"
64 " Allows an arbitrary application to be invoked even when not\n"
65 "hardcoded into the dialplan. If the underlying application\n"
66 "terminates the dialplan, or if the application cannot be found,\n"
67 "Exec will terminate the dialplan.\n"
68 " To invoke external applications, see the application System.\n"
69 " If you would like to catch any error instead, see TryExec.\n";
71 static char *app_tryexec = "TryExec";
72 static char *tryexec_synopsis = "Executes dialplan application, always returning";
73 static char *tryexec_descrip =
74 "Usage: TryExec(appname(arguments))\n"
75 " Allows an arbitrary application to be invoked even when not\n"
76 "hardcoded into the dialplan. To invoke external applications\n"
77 "see the application System. Always returns to the dialplan.\n"
78 "The channel variable TRYSTATUS will be set to:\n"
79 " SUCCESS if the application returned zero\n"
80 " FAILED if the application returned non-zero\n"
81 " NOAPP if the application was not found or was not specified\n";
83 static char *app_execif = "ExecIf";
84 static char *execif_synopsis = "Executes dialplan application, conditionally";
85 static char *execif_descrip =
86 "Usage: ExecIF (<expr>|<app>|<data>)\n"
87 "If <expr> is true, execute and return the result of <app>(<data>).\n"
88 "If <expr> is true, but <app> is not found, then the application\n"
89 "will return a non-zero value.\n";
91 static int exec_exec(struct ast_channel *chan, void *data)
93 int res=0;
94 struct ast_module_user *u;
95 char *s, *appname, *endargs, args[MAXRESULT] = "";
96 struct ast_app *app;
98 u = ast_module_user_add(chan);
100 /* Check and parse arguments */
101 if (data) {
102 s = ast_strdupa(data);
103 appname = strsep(&s, "(");
104 if (s) {
105 endargs = strrchr(s, ')');
106 if (endargs)
107 *endargs = '\0';
108 pbx_substitute_variables_helper(chan, s, args, MAXRESULT - 1);
110 if (appname) {
111 app = pbx_findapp(appname);
112 if (app) {
113 res = pbx_exec(chan, app, args);
114 } else {
115 ast_log(LOG_WARNING, "Could not find application (%s)\n", appname);
116 res = -1;
121 ast_module_user_remove(u);
122 return res;
125 static int tryexec_exec(struct ast_channel *chan, void *data)
127 int res=0;
128 struct ast_module_user *u;
129 char *s, *appname, *endargs, args[MAXRESULT] = "";
130 struct ast_app *app;
132 u = ast_module_user_add(chan);
134 /* Check and parse arguments */
135 if (data) {
136 s = ast_strdupa(data);
137 appname = strsep(&s, "(");
138 if (s) {
139 endargs = strrchr(s, ')');
140 if (endargs)
141 *endargs = '\0';
142 pbx_substitute_variables_helper(chan, s, args, MAXRESULT - 1);
144 if (appname) {
145 app = pbx_findapp(appname);
146 if (app) {
147 res = pbx_exec(chan, app, args);
148 pbx_builtin_setvar_helper(chan, "TRYSTATUS", res ? "FAILED" : "SUCCESS");
149 } else {
150 ast_log(LOG_WARNING, "Could not find application (%s)\n", appname);
151 pbx_builtin_setvar_helper(chan, "TRYSTATUS", "NOAPP");
156 ast_module_user_remove(u);
157 return 0;
160 static int execif_exec(struct ast_channel *chan, void *data)
162 int res = 0;
163 struct ast_module_user *u;
164 char *myapp = NULL;
165 char *mydata = NULL;
166 char *expr = NULL;
167 struct ast_app *app = NULL;
169 u = ast_module_user_add(chan);
171 expr = ast_strdupa(data);
173 if ((myapp = strchr(expr,'|'))) {
174 *myapp = '\0';
175 myapp++;
176 if ((mydata = strchr(myapp,'|'))) {
177 *mydata = '\0';
178 mydata++;
179 } else
180 mydata = "";
182 if (pbx_checkcondition(expr)) {
183 if ((app = pbx_findapp(myapp))) {
184 res = pbx_exec(chan, app, mydata);
185 } else {
186 ast_log(LOG_WARNING, "Count not find application! (%s)\n", myapp);
187 res = -1;
190 } else {
191 ast_log(LOG_ERROR,"Invalid Syntax.\n");
192 res = -1;
195 ast_module_user_remove(u);
197 return res;
200 static int unload_module(void)
202 int res;
204 res = ast_unregister_application(app_exec);
205 res |= ast_unregister_application(app_tryexec);
206 res |= ast_unregister_application(app_execif);
208 ast_module_user_hangup_all();
210 return res;
213 static int load_module(void)
215 int res = ast_register_application(app_exec, exec_exec, exec_synopsis, exec_descrip);
216 res |= ast_register_application(app_tryexec, tryexec_exec, tryexec_synopsis, tryexec_descrip);
217 res |= ast_register_application(app_execif, execif_exec, execif_synopsis, execif_descrip);
218 return res;
221 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Executes dialplan applications");