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