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.
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
31 ASTERISK_FILE_VERSION(__FILE__
, "$Revision$")
33 #include "asterisk/file.h"
34 #include "asterisk/channel.h"
35 #include "asterisk/pbx.h"
36 #include "asterisk/module.h"
37 #include "asterisk/app.h"
39 /* Maximum length of any variable */
40 #define MAXRESULT 1024
44 * The key difference between these two apps is exit status. In a
45 * nutshell, Exec tries to be transparent as possible, behaving
46 * in exactly the same way as if the application it calls was
47 * directly invoked from the dialplan.
49 * TryExec, on the other hand, provides a way to execute applications
50 * and catch any possible fatal error without actually fatally
51 * affecting the dialplan.
54 static char *app_exec
= "Exec";
55 static char *exec_synopsis
= "Executes dialplan application";
56 static char *exec_descrip
=
57 " Exec(appname(arguments)):\n"
58 "Allows an arbitrary application to be invoked even when not\n"
59 "hardcoded into the dialplan. If the underlying application\n"
60 "terminates the dialplan, or if the application cannot be found,\n"
61 "Exec will terminate the dialplan.\n"
62 " To invoke external applications, see the application System.\n"
63 " If you would like to catch any error instead, see TryExec.\n";
65 static char *app_tryexec
= "TryExec";
66 static char *tryexec_synopsis
= "Executes dialplan application, always returning";
67 static char *tryexec_descrip
=
68 " TryExec(appname(arguments)):\n"
69 "Allows an arbitrary application to be invoked even when not\n"
70 "hardcoded into the dialplan. To invoke external applications\n"
71 "see the application System. Always returns to the dialplan.\n"
72 "The channel variable TRYSTATUS will be set to one of:\n"
73 " SUCCESS if the application returned zero\n"
74 " FAILED if the application returned non-zero\n"
75 " NOAPP if the application was not found or was not specified\n";
77 static char *app_execif
= "ExecIf";
78 static char *execif_synopsis
= "Executes dialplan application, conditionally";
79 static char *execif_descrip
=
80 " ExecIF (<expr>?<appiftrue>(<args>)[:<appiffalse>(<args>)])\n"
81 "If <expr> is true, execute and return the result of <appiftrue>(<args>).\n"
82 "If <expr> is true, but <appiftrue> is not found, then the application\n"
83 "will return a non-zero value.\n";
85 static int exec_exec(struct ast_channel
*chan
, void *data
)
88 char *s
, *appname
, *endargs
, args
[MAXRESULT
];
91 if (ast_strlen_zero(data
))
94 s
= ast_strdupa(data
);
96 appname
= strsep(&s
, "(");
98 endargs
= strrchr(s
, ')');
101 pbx_substitute_variables_helper(chan
, s
, args
, MAXRESULT
- 1);
104 app
= pbx_findapp(appname
);
106 res
= pbx_exec(chan
, app
, args
);
108 ast_log(LOG_WARNING
, "Could not find application (%s)\n", appname
);
116 static int tryexec_exec(struct ast_channel
*chan
, void *data
)
119 char *s
, *appname
, *endargs
, args
[MAXRESULT
];
122 if (ast_strlen_zero(data
))
125 s
= ast_strdupa(data
);
127 appname
= strsep(&s
, "(");
129 endargs
= strrchr(s
, ')');
132 pbx_substitute_variables_helper(chan
, s
, args
, MAXRESULT
- 1);
135 app
= pbx_findapp(appname
);
137 res
= pbx_exec(chan
, app
, args
);
138 pbx_builtin_setvar_helper(chan
, "TRYSTATUS", res
? "FAILED" : "SUCCESS");
140 ast_log(LOG_WARNING
, "Could not find application (%s)\n", appname
);
141 pbx_builtin_setvar_helper(chan
, "TRYSTATUS", "NOAPP");
148 static int execif_exec(struct ast_channel
*chan
, void *data
)
151 char *truedata
= NULL
, *falsedata
= NULL
, *end
, *firstcomma
, *firstquestion
;
152 struct ast_app
*app
= NULL
;
153 AST_DECLARE_APP_ARGS(expr
,
155 AST_APP_ARG(remainder
);
157 AST_DECLARE_APP_ARGS(apps
,
161 char *parse
= ast_strdupa(data
);
163 firstcomma
= strchr(parse
, ',');
164 firstquestion
= strchr(parse
, '?');
166 if ((firstcomma
!= NULL
&& firstquestion
!= NULL
&& firstcomma
< firstquestion
) || (firstquestion
== NULL
)) {
167 /* Deprecated syntax */
168 AST_DECLARE_APP_ARGS(depr
,
170 AST_APP_ARG(appname
);
171 AST_APP_ARG(appargs
);
173 AST_STANDARD_APP_ARGS(depr
, parse
);
175 ast_log(LOG_WARNING
, "Deprecated syntax found. Please upgrade to using ExecIf(<expr>?%s(%s))\n", depr
.appname
, depr
.appargs
);
177 /* Make the two syntaxes look the same */
178 expr
.expr
= depr
.expr
;
179 apps
.t
= depr
.appname
;
181 truedata
= depr
.appargs
;
183 /* Preferred syntax */
185 AST_NONSTANDARD_APP_ARGS(expr
, parse
, '?');
186 if (ast_strlen_zero(expr
.remainder
)) {
187 ast_log(LOG_ERROR
, "Usage: ExecIf(<expr>?<appiftrue>(<args>)[:<appiffalse>(<args)])\n");
191 AST_NONSTANDARD_APP_ARGS(apps
, expr
.remainder
, ':');
193 if (apps
.t
&& (truedata
= strchr(apps
.t
, '('))) {
195 if ((end
= strrchr(truedata
, ')'))) {
200 if (apps
.f
&& (falsedata
= strchr(apps
.f
, '('))) {
202 if ((end
= strrchr(falsedata
, ')'))) {
208 if (pbx_checkcondition(expr
.expr
)) {
209 if (!ast_strlen_zero(apps
.t
) && (app
= pbx_findapp(apps
.t
))) {
210 res
= pbx_exec(chan
, app
, S_OR(truedata
, ""));
212 ast_log(LOG_WARNING
, "Could not find application! (%s)\n", apps
.t
);
215 } else if (!ast_strlen_zero(apps
.f
)) {
216 if ((app
= pbx_findapp(apps
.f
))) {
217 res
= pbx_exec(chan
, app
, S_OR(falsedata
, ""));
219 ast_log(LOG_WARNING
, "Could not find application! (%s)\n", apps
.f
);
227 static int unload_module(void)
231 res
= ast_unregister_application(app_exec
);
232 res
|= ast_unregister_application(app_tryexec
);
233 res
|= ast_unregister_application(app_execif
);
238 static int load_module(void)
240 int res
= ast_register_application(app_exec
, exec_exec
, exec_synopsis
, exec_descrip
);
241 res
|= ast_register_application(app_tryexec
, tryexec_exec
, tryexec_synopsis
, tryexec_descrip
);
242 res
|= ast_register_application(app_execif
, execif_exec
, execif_synopsis
, execif_descrip
);
246 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY
, "Executes dialplan applications");