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