Debian Codegraph @

apps/app_exec.c

raw symbols SCIP
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (c) 2004 - 2005, Tilghman Lesher. All rights reserved.
5 * Portions copyright (c) 2006, Philipp Dunkel.
6 *
7 * Tilghman Lesher <[email protected]>
8 *
9 * This code is released by the author with no restrictions on usage.
10 *
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.
16 *
17 */
18
19 /*! \file
20 *
21 * \brief Exec application
22 *
23 * \author Tilghman Lesher <[email protected]>
24 * \author Philipp Dunkel <[email protected]>
25 *
26 * \ingroup applications
27 */
28
29 /*** MODULEINFO
30 <support_level>core</support_level>
31 ***/
32 #include "asterisk.h"
33
34 #include "asterisk/file.h"
35 #include "asterisk/channel.h"
36 #include "asterisk/pbx.h"
37 #include "asterisk/module.h"
38 #include "asterisk/app.h"
39
40 /*** DOCUMENTATION
41 <application name="Exec" language="en_US">
42 <since>
43 <version>1.0.0</version>
44 </since>
45 <synopsis>
46 Executes dialplan application.
47 </synopsis>
48 <syntax>
49 <parameter name="appname" required="true" hasparams="true">
50 <para>Application name and arguments of the dialplan application to execute.</para>
51 <argument name="arguments" required="true" />
52 </parameter>
53 </syntax>
54 <description>
55 <para>Allows an arbitrary application to be invoked even when not
56 hard coded into the dialplan. If the underlying application
57 terminates the dialplan, or if the application cannot be found,
58 Exec will terminate the dialplan.</para>
59 <para>To invoke external applications, see the application System.
60 If you would like to catch any error instead, see TryExec.</para>
61 </description>
62 </application>
63 <application name="TryExec" language="en_US">
64 <since>
65 <version>1.4.0</version>
66 </since>
67 <synopsis>
68 Executes dialplan application, always returning.
69 </synopsis>
70 <syntax>
71 <parameter name="appname" required="true" hasparams="true">
72 <argument name="arguments" required="true" />
73 </parameter>
74 </syntax>
75 <description>
76 <para>Allows an arbitrary application to be invoked even when not
77 hard coded into the dialplan. To invoke external applications
78 see the application System. Always returns to the dialplan.
79 The channel variable TRYSTATUS will be set to one of:
80 </para>
81 <variablelist>
82 <variable name="TRYSTATUS">
83 <value name="SUCCESS">
84 If the application returned zero.
85 </value>
86 <value name="FAILED">
87 If the application returned non-zero.
88 </value>
89 <value name="NOAPP">
90 If the application was not found or was not specified.
91 </value>
92 </variable>
93 </variablelist>
94 </description>
95 </application>
96 <application name="ExecIf" language="en_US">
97 <since>
98 <version>1.2.0</version>
99 </since>
100 <synopsis>
101 Executes dialplan application, conditionally.
102 </synopsis>
103 <syntax argsep="?">
104 <parameter name="expression" required="true" />
105 <parameter name="execapp" required="true" argsep=":">
106 <argument name="appiftrue" required="true" hasparams="true">
107 <argument name="args" required="true" />
108 </argument>
109 <argument name="appiffalse" required="false" hasparams="true">
110 <argument name="args" required="true" />
111 </argument>
112 </parameter>
113 </syntax>
114 <description>
115 <para>If <replaceable>expr</replaceable> is true, execute and return the
116 result of <replaceable>appiftrue(args)</replaceable>.</para>
117 <para>If <replaceable>expr</replaceable> is true, but <replaceable>appiftrue</replaceable> is not found,
118 then the application will return a non-zero value.</para>
119 </description>
120 </application>
121 ***/
122
123 /* Maximum length of any variable */
124 #define MAXRESULT 1024
125
126 /*! Note
127 *
128 * The key difference between these two apps is exit status. In a
129 * nutshell, Exec tries to be transparent as possible, behaving
130 * in exactly the same way as if the application it calls was
131 * directly invoked from the dialplan.
132 *
133 * TryExec, on the other hand, provides a way to execute applications
134 * and catch any possible fatal error without actually fatally
135 * affecting the dialplan.
136 */
137
138 static const char app_exec[] = "Exec";
139 static const char app_tryexec[] = "TryExec";
140 static const char app_execif[] = "ExecIf";
141
142 static int exec_exec(struct ast_channel *chan, const char *data)
143 {
144 int res = 0;
145 char *s, *appname, *endargs;
146 struct ast_app *app;
147 struct ast_str *args = NULL;
148
149 if (ast_strlen_zero(data))
150 return 0;
151
152 s = ast_strdupa(data);
153 appname = strsep(&s, "(");
154 if (s) {
155 endargs = strrchr(s, ')');
156 if (endargs)
157 *endargs = '\0';
158 if ((args = ast_str_create(16))) {
159 ast_str_substitute_variables(&args, 0, chan, s);
160 }
161 }
162 if (appname) {
163 app = pbx_findapp(appname);
164 if (app) {
165 res = pbx_exec(chan, app, args ? ast_str_buffer(args) : NULL);
166 } else {
167 ast_log(LOG_WARNING, "Could not find application (%s)\n", appname);
168 res = -1;
169 }
170 }
171
172 ast_free(args);
173 return res;
174 }
175
176 static int tryexec_exec(struct ast_channel *chan, const char *data)
177 {
178 int res = 0;
179 char *s, *appname, *endargs;
180 struct ast_app *app;
181 struct ast_str *args = NULL;
182
183 if (ast_strlen_zero(data))
184 return 0;
185
186 s = ast_strdupa(data);
187 appname = strsep(&s, "(");
188 if (s) {
189 endargs = strrchr(s, ')');
190 if (endargs)
191 *endargs = '\0';
192 if ((args = ast_str_create(16))) {
193 ast_str_substitute_variables(&args, 0, chan, s);
194 }
195 }
196 if (appname) {
197 app = pbx_findapp(appname);
198 if (app) {
199 res = pbx_exec(chan, app, args ? ast_str_buffer(args) : NULL);
200 pbx_builtin_setvar_helper(chan, "TRYSTATUS", res ? "FAILED" : "SUCCESS");
201 } else {
202 ast_log(LOG_WARNING, "Could not find application (%s)\n", appname);
203 pbx_builtin_setvar_helper(chan, "TRYSTATUS", "NOAPP");
204 }
205 }
206
207 ast_free(args);
208 return 0;
209 }
210
211 static int execif_exec(struct ast_channel *chan, const char *data)
212 {
213 int res = 0;
214 char *truedata = NULL, *falsedata = NULL, *end, *firstcomma, *firstquestion;
215 struct ast_app *app = NULL;
216 AST_DECLARE_APP_ARGS(expr,
217 AST_APP_ARG(expr);
218 AST_APP_ARG(remainder);
219 );
220 AST_DECLARE_APP_ARGS(apps,
221 AST_APP_ARG(t);
222 AST_APP_ARG(f);
223 );
224 char *parse = ast_strdupa(data);
225
226 firstcomma = strchr(parse, ',');
227 firstquestion = strchr(parse, '?');
228
229 if ((firstcomma != NULL && firstquestion != NULL && firstcomma < firstquestion) || (firstquestion == NULL)) {
230 /* Deprecated syntax */
231 AST_DECLARE_APP_ARGS(depr,
232 AST_APP_ARG(expr);
233 AST_APP_ARG(appname);
234 AST_APP_ARG(appargs);
235 );
236 AST_STANDARD_APP_ARGS(depr, parse);
237
238 ast_log(LOG_WARNING, "Deprecated syntax found. Please upgrade to using ExecIf(<expr>?%s(%s))\n", depr.appname, depr.appargs);
239
240 /* Make the two syntaxes look the same */
241 expr.expr = depr.expr;
242 apps.t = depr.appname;
243 apps.f = NULL;
244 truedata = depr.appargs;
245 } else {
246 /* Preferred syntax */
247
248 AST_NONSTANDARD_RAW_ARGS(expr, parse, '?');
249 if (ast_strlen_zero(expr.remainder)) {
250 ast_log(LOG_ERROR, "Usage: ExecIf(<expr>?<appiftrue>(<args>)[:<appiffalse>(<args)])\n");
251 return -1;
252 }
253
254 AST_NONSTANDARD_RAW_ARGS(apps, expr.remainder, ':');
255
256 if (apps.t && (truedata = strchr(apps.t, '('))) {
257 *truedata++ = '\0';
258 if ((end = strrchr(truedata, ')'))) {
259 *end = '\0';
260 }
261 }
262
263 if (apps.f && (falsedata = strchr(apps.f, '('))) {
264 *falsedata++ = '\0';
265 if ((end = strrchr(falsedata, ')'))) {
266 *end = '\0';
267 }
268 }
269 }
270
271 if (pbx_checkcondition(expr.expr)) {
272 if (!ast_strlen_zero(apps.t) && (app = pbx_findapp(apps.t))) {
273 res = pbx_exec(chan, app, S_OR(truedata, ""));
274 } else {
275 ast_log(LOG_WARNING, "Could not find application! (%s)\n", apps.t);
276 res = -1;
277 }
278 } else if (!ast_strlen_zero(apps.f)) {
279 if ((app = pbx_findapp(apps.f))) {
280 res = pbx_exec(chan, app, S_OR(falsedata, ""));
281 } else {
282 ast_log(LOG_WARNING, "Could not find application! (%s)\n", apps.f);
283 res = -1;
284 }
285 }
286
287 return res;
288 }
289
290 static int unload_module(void)
291 {
292 int res;
293
294 res = ast_unregister_application(app_exec);
295 res |= ast_unregister_application(app_tryexec);
296 res |= ast_unregister_application(app_execif);
297
298 return res;
299 }
300
301 static int load_module(void)
302 {
303 int res = ast_register_application_xml(app_exec, exec_exec);
304 res |= ast_register_application_xml(app_tryexec, tryexec_exec);
305 res |= ast_register_application_xml(app_execif, execif_exec);
306 return res;
307 }
308
309 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Executes dialplan applications");