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 #include "asterisk.h"
00029
00030 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 160389 $")
00031
00032 #include "asterisk/pbx.h"
00033 #include "asterisk/module.h"
00034 #include "asterisk/channel.h"
00035
00036 static char *start_app = "While";
00037 static char *start_desc =
00038 " While(<expr>): Start a While Loop. Execution will return to this\n"
00039 "point when EndWhile() is called until expr is no longer true.\n";
00040
00041 static char *start_synopsis = "Start a while loop";
00042
00043
00044 static char *stop_app = "EndWhile";
00045 static char *stop_desc =
00046 " EndWhile(): Return to the previous called While()\n";
00047
00048 static char *stop_synopsis = "End a while loop";
00049
00050 static char *exit_app = "ExitWhile";
00051 static char *exit_desc =
00052 " ExitWhile(): Exits a While() loop, whether or not the conditional has been satisfied.\n";
00053 static char *exit_synopsis = "End a While loop";
00054
00055 static char *continue_app = "ContinueWhile";
00056 static char *continue_desc =
00057 " ContinueWhile(): Returns to the top of the while loop and re-evaluates the conditional.\n";
00058 static char *continue_synopsis = "Restart a While loop";
00059
00060 #define VAR_SIZE 64
00061
00062
00063 static const char *get_index(struct ast_channel *chan, const char *prefix, int index) {
00064 char varname[VAR_SIZE];
00065
00066 snprintf(varname, VAR_SIZE, "%s_%d", prefix, index);
00067 return pbx_builtin_getvar_helper(chan, varname);
00068 }
00069
00070 static struct ast_exten *find_matching_priority(struct ast_context *c, const char *exten, int priority, const char *callerid)
00071 {
00072 struct ast_exten *e;
00073 struct ast_include *i;
00074 struct ast_context *c2;
00075
00076 for (e=ast_walk_context_extensions(c, NULL); e; e=ast_walk_context_extensions(c, e)) {
00077 if (ast_extension_match(ast_get_extension_name(e), exten)) {
00078 int needmatch = ast_get_extension_matchcid(e);
00079 if ((needmatch && ast_extension_match(ast_get_extension_cidmatch(e), callerid)) ||
00080 (!needmatch)) {
00081
00082 struct ast_exten *p;
00083 for (p=ast_walk_extension_priorities(e, NULL); p; p=ast_walk_extension_priorities(e, p)) {
00084 if (priority != ast_get_extension_priority(p))
00085 continue;
00086 return p;
00087 }
00088 }
00089 }
00090 }
00091
00092
00093 for (i=ast_walk_context_includes(c, NULL); i; i=ast_walk_context_includes(c, i)) {
00094 for (c2=ast_walk_contexts(NULL); c2; c2=ast_walk_contexts(c2)) {
00095 if (!strcmp(ast_get_context_name(c2), ast_get_include_name(i))) {
00096 e = find_matching_priority(c2, exten, priority, callerid);
00097 if (e)
00098 return e;
00099 }
00100 }
00101 }
00102 return NULL;
00103 }
00104
00105 static int find_matching_endwhile(struct ast_channel *chan)
00106 {
00107 struct ast_context *c;
00108 int res=-1;
00109
00110 if (ast_rdlock_contexts()) {
00111 ast_log(LOG_ERROR, "Failed to lock contexts list\n");
00112 return -1;
00113 }
00114
00115 for (c=ast_walk_contexts(NULL); c; c=ast_walk_contexts(c)) {
00116 struct ast_exten *e;
00117
00118 if (!ast_rdlock_context(c)) {
00119 if (!strcmp(ast_get_context_name(c), chan->context)) {
00120
00121 int cur_priority = chan->priority + 1, level=1;
00122
00123 for (e = find_matching_priority(c, chan->exten, cur_priority, chan->cid.cid_num); e; e = find_matching_priority(c, chan->exten, ++cur_priority, chan->cid.cid_num)) {
00124 if (!strcasecmp(ast_get_extension_app(e), "WHILE")) {
00125 level++;
00126 } else if (!strcasecmp(ast_get_extension_app(e), "ENDWHILE")) {
00127 level--;
00128 }
00129
00130 if (level == 0) {
00131 res = cur_priority;
00132 break;
00133 }
00134 }
00135 }
00136 ast_unlock_context(c);
00137 if (res > 0) {
00138 break;
00139 }
00140 }
00141 }
00142 ast_unlock_contexts();
00143 return res;
00144 }
00145
00146 static int _while_exec(struct ast_channel *chan, void *data, int end)
00147 {
00148 int res=0;
00149 const char *while_pri = NULL;
00150 char *my_name = NULL;
00151 const char *condition = NULL, *label = NULL;
00152 char varname[VAR_SIZE], end_varname[VAR_SIZE];
00153 const char *prefix = "WHILE";
00154 size_t size=0;
00155 int used_index_i = -1, x=0;
00156 char used_index[VAR_SIZE] = "0", new_index[VAR_SIZE] = "0";
00157
00158 if (!chan) {
00159
00160 return -1;
00161 }
00162
00163 #if 0
00164
00165
00166
00167
00168
00169
00170 if (ast_waitfordigit(chan,1) < 0)
00171 return -1;
00172 #endif
00173
00174 for (x=0;;x++) {
00175 if (get_index(chan, prefix, x)) {
00176 used_index_i = x;
00177 } else
00178 break;
00179 }
00180
00181 snprintf(used_index, VAR_SIZE, "%d", used_index_i);
00182 snprintf(new_index, VAR_SIZE, "%d", used_index_i + 1);
00183
00184 if (!end)
00185 condition = ast_strdupa(data);
00186
00187 size = strlen(chan->context) + strlen(chan->exten) + 32;
00188 my_name = alloca(size);
00189 memset(my_name, 0, size);
00190 snprintf(my_name, size, "%s_%s_%d", chan->context, chan->exten, chan->priority);
00191
00192 if (ast_strlen_zero(label)) {
00193 if (end)
00194 label = used_index;
00195 else if (!(label = pbx_builtin_getvar_helper(chan, my_name))) {
00196 label = new_index;
00197 pbx_builtin_setvar_helper(chan, my_name, label);
00198 }
00199
00200 }
00201
00202 snprintf(varname, VAR_SIZE, "%s_%s", prefix, label);
00203 while_pri = pbx_builtin_getvar_helper(chan, varname);
00204
00205 if ((while_pri = pbx_builtin_getvar_helper(chan, varname)) && !end) {
00206 snprintf(end_varname,VAR_SIZE,"END_%s",varname);
00207 }
00208
00209
00210 if ((!end && !pbx_checkcondition(condition)) || (end == 2)) {
00211
00212 const char *goto_str;
00213 pbx_builtin_setvar_helper(chan, varname, NULL);
00214 pbx_builtin_setvar_helper(chan, my_name, NULL);
00215 snprintf(end_varname,VAR_SIZE,"END_%s",varname);
00216 if ((goto_str=pbx_builtin_getvar_helper(chan, end_varname))) {
00217 ast_parseable_goto(chan, goto_str);
00218 pbx_builtin_setvar_helper(chan, end_varname, NULL);
00219 } else {
00220 int pri = find_matching_endwhile(chan);
00221 if (pri > 0) {
00222 ast_verb(3, "Jumping to priority %d\n", pri);
00223 chan->priority = pri;
00224 } else {
00225 ast_log(LOG_WARNING, "Couldn't find matching EndWhile? (While at %s@%s priority %d)\n", chan->context, chan->exten, chan->priority);
00226 }
00227 }
00228 return res;
00229 }
00230
00231 if (!end && !while_pri) {
00232 char *goto_str;
00233 size = strlen(chan->context) + strlen(chan->exten) + 32;
00234 goto_str = alloca(size);
00235 memset(goto_str, 0, size);
00236 snprintf(goto_str, size, "%s,%s,%d", chan->context, chan->exten, chan->priority);
00237 pbx_builtin_setvar_helper(chan, varname, goto_str);
00238 }
00239
00240 else if (end && while_pri) {
00241
00242 snprintf(end_varname, VAR_SIZE, "END_%s", varname);
00243 if (! pbx_builtin_getvar_helper(chan, end_varname)) {
00244 char *goto_str;
00245 size = strlen(chan->context) + strlen(chan->exten) + 32;
00246 goto_str = alloca(size);
00247 memset(goto_str, 0, size);
00248 snprintf(goto_str, size, "%s,%s,%d", chan->context, chan->exten, chan->priority+1);
00249 pbx_builtin_setvar_helper(chan, end_varname, goto_str);
00250 }
00251 ast_parseable_goto(chan, while_pri);
00252 }
00253
00254 return res;
00255 }
00256
00257 static int while_start_exec(struct ast_channel *chan, void *data) {
00258 return _while_exec(chan, data, 0);
00259 }
00260
00261 static int while_end_exec(struct ast_channel *chan, void *data) {
00262 return _while_exec(chan, data, 1);
00263 }
00264
00265 static int while_exit_exec(struct ast_channel *chan, void *data) {
00266 return _while_exec(chan, data, 2);
00267 }
00268
00269 static int while_continue_exec(struct ast_channel *chan, void *data)
00270 {
00271 int x;
00272 const char *prefix = "WHILE", *while_pri=NULL;
00273
00274 for (x = 0; ; x++) {
00275 const char *tmp = get_index(chan, prefix, x);
00276 if (tmp)
00277 while_pri = tmp;
00278 else
00279 break;
00280 }
00281
00282 if (while_pri)
00283 ast_parseable_goto(chan, while_pri);
00284
00285 return 0;
00286 }
00287
00288 static int unload_module(void)
00289 {
00290 int res;
00291
00292 res = ast_unregister_application(start_app);
00293 res |= ast_unregister_application(stop_app);
00294 res |= ast_unregister_application(exit_app);
00295 res |= ast_unregister_application(continue_app);
00296
00297 return res;
00298 }
00299
00300 static int load_module(void)
00301 {
00302 int res;
00303
00304 res = ast_register_application(start_app, while_start_exec, start_synopsis, start_desc);
00305 res |= ast_register_application(stop_app, while_end_exec, stop_synopsis, stop_desc);
00306 res |= ast_register_application(exit_app, while_exit_exec, exit_synopsis, exit_desc);
00307 res |= ast_register_application(continue_app, while_continue_exec, continue_synopsis, continue_desc);
00308
00309 return res;
00310 }
00311
00312 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "While Loops and Conditional Execution");