00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include "asterisk.h"
00030
00031 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 251884 $")
00032
00033 #include "asterisk/file.h"
00034 #include "asterisk/channel.h"
00035 #include "asterisk/pbx.h"
00036 #include "asterisk/module.h"
00037 #include "asterisk/app.h"
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114 #define MAXRESULT 1024
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128 static const char app_exec[] = "Exec";
00129 static const char app_tryexec[] = "TryExec";
00130 static const char app_execif[] = "ExecIf";
00131
00132 static int exec_exec(struct ast_channel *chan, const char *data)
00133 {
00134 int res = 0;
00135 char *s, *appname, *endargs;
00136 struct ast_app *app;
00137 struct ast_str *args = NULL;
00138
00139 if (ast_strlen_zero(data))
00140 return 0;
00141
00142 s = ast_strdupa(data);
00143 appname = strsep(&s, "(");
00144 if (s) {
00145 endargs = strrchr(s, ')');
00146 if (endargs)
00147 *endargs = '\0';
00148 if ((args = ast_str_create(16))) {
00149 ast_str_substitute_variables(&args, 0, chan, s);
00150 }
00151 }
00152 if (appname) {
00153 app = pbx_findapp(appname);
00154 if (app) {
00155 res = pbx_exec(chan, app, args ? ast_str_buffer(args) : NULL);
00156 } else {
00157 ast_log(LOG_WARNING, "Could not find application (%s)\n", appname);
00158 res = -1;
00159 }
00160 }
00161
00162 ast_free(args);
00163 return res;
00164 }
00165
00166 static int tryexec_exec(struct ast_channel *chan, const char *data)
00167 {
00168 int res = 0;
00169 char *s, *appname, *endargs;
00170 struct ast_app *app;
00171 struct ast_str *args = NULL;
00172
00173 if (ast_strlen_zero(data))
00174 return 0;
00175
00176 s = ast_strdupa(data);
00177 appname = strsep(&s, "(");
00178 if (s) {
00179 endargs = strrchr(s, ')');
00180 if (endargs)
00181 *endargs = '\0';
00182 if ((args = ast_str_create(16))) {
00183 ast_str_substitute_variables(&args, 0, chan, s);
00184 }
00185 }
00186 if (appname) {
00187 app = pbx_findapp(appname);
00188 if (app) {
00189 res = pbx_exec(chan, app, args ? ast_str_buffer(args) : NULL);
00190 pbx_builtin_setvar_helper(chan, "TRYSTATUS", res ? "FAILED" : "SUCCESS");
00191 } else {
00192 ast_log(LOG_WARNING, "Could not find application (%s)\n", appname);
00193 pbx_builtin_setvar_helper(chan, "TRYSTATUS", "NOAPP");
00194 }
00195 }
00196
00197 ast_free(args);
00198 return 0;
00199 }
00200
00201 static int execif_exec(struct ast_channel *chan, const char *data)
00202 {
00203 int res = 0;
00204 char *truedata = NULL, *falsedata = NULL, *end, *firstcomma, *firstquestion;
00205 struct ast_app *app = NULL;
00206 AST_DECLARE_APP_ARGS(expr,
00207 AST_APP_ARG(expr);
00208 AST_APP_ARG(remainder);
00209 );
00210 AST_DECLARE_APP_ARGS(apps,
00211 AST_APP_ARG(t);
00212 AST_APP_ARG(f);
00213 );
00214 char *parse = ast_strdupa(data);
00215
00216 firstcomma = strchr(parse, ',');
00217 firstquestion = strchr(parse, '?');
00218
00219 if ((firstcomma != NULL && firstquestion != NULL && firstcomma < firstquestion) || (firstquestion == NULL)) {
00220
00221 AST_DECLARE_APP_ARGS(depr,
00222 AST_APP_ARG(expr);
00223 AST_APP_ARG(appname);
00224 AST_APP_ARG(appargs);
00225 );
00226 AST_STANDARD_APP_ARGS(depr, parse);
00227
00228 ast_log(LOG_WARNING, "Deprecated syntax found. Please upgrade to using ExecIf(<expr>?%s(%s))\n", depr.appname, depr.appargs);
00229
00230
00231 expr.expr = depr.expr;
00232 apps.t = depr.appname;
00233 apps.f = NULL;
00234 truedata = depr.appargs;
00235 } else {
00236
00237
00238 AST_NONSTANDARD_RAW_ARGS(expr, parse, '?');
00239 if (ast_strlen_zero(expr.remainder)) {
00240 ast_log(LOG_ERROR, "Usage: ExecIf(<expr>?<appiftrue>(<args>)[:<appiffalse>(<args)])\n");
00241 return -1;
00242 }
00243
00244 AST_NONSTANDARD_RAW_ARGS(apps, expr.remainder, ':');
00245
00246 if (apps.t && (truedata = strchr(apps.t, '('))) {
00247 *truedata++ = '\0';
00248 if ((end = strrchr(truedata, ')'))) {
00249 *end = '\0';
00250 }
00251 }
00252
00253 if (apps.f && (falsedata = strchr(apps.f, '('))) {
00254 *falsedata++ = '\0';
00255 if ((end = strrchr(falsedata, ')'))) {
00256 *end = '\0';
00257 }
00258 }
00259 }
00260
00261 if (pbx_checkcondition(expr.expr)) {
00262 if (!ast_strlen_zero(apps.t) && (app = pbx_findapp(apps.t))) {
00263 res = pbx_exec(chan, app, S_OR(truedata, ""));
00264 } else {
00265 ast_log(LOG_WARNING, "Could not find application! (%s)\n", apps.t);
00266 res = -1;
00267 }
00268 } else if (!ast_strlen_zero(apps.f)) {
00269 if ((app = pbx_findapp(apps.f))) {
00270 res = pbx_exec(chan, app, S_OR(falsedata, ""));
00271 } else {
00272 ast_log(LOG_WARNING, "Could not find application! (%s)\n", apps.f);
00273 res = -1;
00274 }
00275 }
00276
00277 return res;
00278 }
00279
00280 static int unload_module(void)
00281 {
00282 int res;
00283
00284 res = ast_unregister_application(app_exec);
00285 res |= ast_unregister_application(app_tryexec);
00286 res |= ast_unregister_application(app_execif);
00287
00288 return res;
00289 }
00290
00291 static int load_module(void)
00292 {
00293 int res = ast_register_application_xml(app_exec, exec_exec);
00294 res |= ast_register_application_xml(app_tryexec, tryexec_exec);
00295 res |= ast_register_application_xml(app_execif, execif_exec);
00296 return res;
00297 }
00298
00299 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Executes dialplan applications");