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