Asterisk - The Open Source Telephony Project  18.5.0
app_exec.c
Go to the documentation of this file.
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  <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;
208  AST_APP_ARG(expr);
209  AST_APP_ARG(remainder);
210  );
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 */
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 
288 
289  return res;
290 }
291 
292 static int load_module(void)
293 {
297  return res;
298 }
299 
300 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Executes dialplan applications");
static int exec_exec(struct ast_channel *chan, const char *data)
Definition: app_exec.c:133
static int load_module(void)
Definition: app_exec.c:292
static const char app_tryexec[]
Definition: app_exec.c:130
Main Channel structure associated with a channel.
#define AST_MODULE_INFO_STANDARD(keystr, desc)
Definition: module.h:567
Asterisk main include file. File version handling, generic pbx functions.
static int unload_module(void)
Definition: app_exec.c:281
int pbx_exec(struct ast_channel *c, struct ast_app *app, const char *data)
Execute an application.
Definition: pbx_app.c:471
Registered applications container.
Definition: pbx_app.c:67
#define AST_STANDARD_APP_ARGS(args, parse)
Performs the &#39;standard&#39; argument separation process for an application.
#define LOG_WARNING
Definition: logger.h:274
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:714
int pbx_checkcondition(const char *condition)
Evaluate a condition.
Definition: pbx.c:8321
void ast_str_substitute_variables(struct ast_str **buf, ssize_t maxlen, struct ast_channel *chan, const char *templ)
static const char app_execif[]
Definition: app_exec.c:131
Generic File Format Support. Should be included by clients of the file handling routines. File service providers should instead include mod_format.h.
const char * args
#define NULL
Definition: resample.c:96
char * end
Definition: eagi_proxy.c:73
int ast_unregister_application(const char *app)
Unregister an application.
Definition: pbx_app.c:392
static int execif_exec(struct ast_channel *chan, const char *data)
Definition: app_exec.c:202
static int tryexec_exec(struct ast_channel *chan, const char *data)
Definition: app_exec.c:167
static const char app_exec[]
Definition: app_exec.c:129
#define ast_strlen_zero(foo)
Definition: strings.h:52
#define ast_log
Definition: astobj2.c:42
General Asterisk PBX channel definitions.
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:300
Core PBX routines and definitions.
#define AST_NONSTANDARD_RAW_ARGS(args, parse, sep)
#define LOG_ERROR
Definition: logger.h:285
The descriptor of a dynamic string XXX storage will be optimized later if needed We use the ts field ...
Definition: strings.h:584
static void parse(struct mgcp_request *req)
Definition: chan_mgcp.c:1872
#define ast_free(a)
Definition: astmm.h:182
int pbx_builtin_setvar_helper(struct ast_channel *chan, const char *name, const char *value)
Add a variable to the channel variable stack, removing the most recently set value for the same name...
char * strsep(char **str, const char *delims)
#define S_OR(a, b)
returns the equivalent of logic or for strings: first one if not empty, otherwise second one...
Definition: strings.h:79
ast_app: A registered application
Definition: pbx_app.c:45
union ast_frame::@263 data
struct ast_app * pbx_findapp(const char *app)
Look up an application.
Definition: ael_main.c:165
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
static const char app[]
Definition: app_mysql.c:62
Asterisk module definitions.
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application&#39;s arguments.
Application convenience functions, designed to give consistent look and feel to Asterisk apps...
#define ast_register_application_xml(app, execute)
Register an application using XML documentation.
Definition: module.h:626
#define ast_str_create(init_len)
Create a malloc&#39;ed dynamic length string.
Definition: strings.h:620
#define AST_APP_ARG(name)
Define an application argument.