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: 251887 $")
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 char *app_exec = "Exec";
00129 static char *app_tryexec = "TryExec";
00130 static char *app_execif = "ExecIf";
00131
00132 static int exec_exec(struct ast_channel *chan, void *data)
00133 {
00134 int res = 0;
00135 char *s, *appname, *endargs, args[MAXRESULT];
00136 struct ast_app *app;
00137
00138 if (ast_strlen_zero(data))
00139 return 0;
00140
00141 s = ast_strdupa(data);
00142 args[0] = 0;
00143 appname = strsep(&s, "(");
00144 if (s) {
00145 endargs = strrchr(s, ')');
00146 if (endargs)
00147 *endargs = '\0';
00148 pbx_substitute_variables_helper(chan, s, args, MAXRESULT - 1);
00149 }
00150 if (appname) {
00151 app = pbx_findapp(appname);
00152 if (app) {
00153 res = pbx_exec(chan, app, args);
00154 } else {
00155 ast_log(LOG_WARNING, "Could not find application (%s)\n", appname);
00156 res = -1;
00157 }
00158 }
00159
00160 return res;
00161 }
00162
00163 static int tryexec_exec(struct ast_channel *chan, void *data)
00164 {
00165 int res = 0;
00166 char *s, *appname, *endargs, args[MAXRESULT];
00167 struct ast_app *app;
00168
00169 if (ast_strlen_zero(data))
00170 return 0;
00171
00172 s = ast_strdupa(data);
00173 args[0] = 0;
00174 appname = strsep(&s, "(");
00175 if (s) {
00176 endargs = strrchr(s, ')');
00177 if (endargs)
00178 *endargs = '\0';
00179 pbx_substitute_variables_helper(chan, s, args, MAXRESULT - 1);
00180 }
00181 if (appname) {
00182 app = pbx_findapp(appname);
00183 if (app) {
00184 res = pbx_exec(chan, app, args);
00185 pbx_builtin_setvar_helper(chan, "TRYSTATUS", res ? "FAILED" : "SUCCESS");
00186 } else {
00187 ast_log(LOG_WARNING, "Could not find application (%s)\n", appname);
00188 pbx_builtin_setvar_helper(chan, "TRYSTATUS", "NOAPP");
00189 }
00190 }
00191
00192 return 0;
00193 }
00194
00195 static int execif_exec(struct ast_channel *chan, void *data)
00196 {
00197 int res = 0;
00198 char *truedata = NULL, *falsedata = NULL, *end, *firstcomma, *firstquestion;
00199 struct ast_app *app = NULL;
00200 AST_DECLARE_APP_ARGS(expr,
00201 AST_APP_ARG(expr);
00202 AST_APP_ARG(remainder);
00203 );
00204 AST_DECLARE_APP_ARGS(apps,
00205 AST_APP_ARG(t);
00206 AST_APP_ARG(f);
00207 );
00208 char *parse = ast_strdupa(data);
00209
00210 firstcomma = strchr(parse, ',');
00211 firstquestion = strchr(parse, '?');
00212
00213 if ((firstcomma != NULL && firstquestion != NULL && firstcomma < firstquestion) || (firstquestion == NULL)) {
00214
00215 AST_DECLARE_APP_ARGS(depr,
00216 AST_APP_ARG(expr);
00217 AST_APP_ARG(appname);
00218 AST_APP_ARG(appargs);
00219 );
00220 AST_STANDARD_APP_ARGS(depr, parse);
00221
00222 ast_log(LOG_WARNING, "Deprecated syntax found. Please upgrade to using ExecIf(<expr>?%s(%s))\n", depr.appname, depr.appargs);
00223
00224
00225 expr.expr = depr.expr;
00226 apps.t = depr.appname;
00227 apps.f = NULL;
00228 truedata = depr.appargs;
00229 } else {
00230
00231
00232 AST_NONSTANDARD_RAW_ARGS(expr, parse, '?');
00233 if (ast_strlen_zero(expr.remainder)) {
00234 ast_log(LOG_ERROR, "Usage: ExecIf(<expr>?<appiftrue>(<args>)[:<appiffalse>(<args)])\n");
00235 return -1;
00236 }
00237
00238 AST_NONSTANDARD_RAW_ARGS(apps, expr.remainder, ':');
00239
00240 if (apps.t && (truedata = strchr(apps.t, '('))) {
00241 *truedata++ = '\0';
00242 if ((end = strrchr(truedata, ')'))) {
00243 *end = '\0';
00244 }
00245 }
00246
00247 if (apps.f && (falsedata = strchr(apps.f, '('))) {
00248 *falsedata++ = '\0';
00249 if ((end = strrchr(falsedata, ')'))) {
00250 *end = '\0';
00251 }
00252 }
00253 }
00254
00255 if (pbx_checkcondition(expr.expr)) {
00256 if (!ast_strlen_zero(apps.t) && (app = pbx_findapp(apps.t))) {
00257 res = pbx_exec(chan, app, S_OR(truedata, ""));
00258 } else {
00259 ast_log(LOG_WARNING, "Could not find application! (%s)\n", apps.t);
00260 res = -1;
00261 }
00262 } else if (!ast_strlen_zero(apps.f)) {
00263 if ((app = pbx_findapp(apps.f))) {
00264 res = pbx_exec(chan, app, S_OR(falsedata, ""));
00265 } else {
00266 ast_log(LOG_WARNING, "Could not find application! (%s)\n", apps.f);
00267 res = -1;
00268 }
00269 }
00270
00271 return res;
00272 }
00273
00274 static int unload_module(void)
00275 {
00276 int res;
00277
00278 res = ast_unregister_application(app_exec);
00279 res |= ast_unregister_application(app_tryexec);
00280 res |= ast_unregister_application(app_execif);
00281
00282 return res;
00283 }
00284
00285 static int load_module(void)
00286 {
00287 int res = ast_register_application_xml(app_exec, exec_exec);
00288 res |= ast_register_application_xml(app_tryexec, tryexec_exec);
00289 res |= ast_register_application_xml(app_execif, execif_exec);
00290 return res;
00291 }
00292
00293 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Executes dialplan applications");