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: 156757 $")
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 idx) {
00064 char varname[VAR_SIZE];
00065
00066 snprintf(varname, VAR_SIZE, "%s_%d", prefix, idx);
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 ast_channel_lock(chan);
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 snprintf(varname, VAR_SIZE, "%s_%s", prefix, label);
00200 if ((while_pri = pbx_builtin_getvar_helper(chan, varname)) && !end) {
00201 while_pri = ast_strdupa(while_pri);
00202 snprintf(end_varname,VAR_SIZE,"END_%s",varname);
00203 }
00204 ast_channel_unlock(chan);
00205
00206
00207 if ((!end && !pbx_checkcondition(condition)) || (end == 2)) {
00208
00209 const char *goto_str;
00210 pbx_builtin_setvar_helper(chan, varname, NULL);
00211 pbx_builtin_setvar_helper(chan, my_name, NULL);
00212 snprintf(end_varname,VAR_SIZE,"END_%s",varname);
00213 ast_channel_lock(chan);
00214 if ((goto_str = pbx_builtin_getvar_helper(chan, end_varname))) {
00215 ast_parseable_goto(chan, goto_str);
00216 pbx_builtin_setvar_helper(chan, end_varname, NULL);
00217 } else {
00218 int pri = find_matching_endwhile(chan);
00219 if (pri > 0) {
00220 ast_verb(3, "Jumping to priority %d\n", pri);
00221 chan->priority = pri;
00222 } else {
00223 ast_log(LOG_WARNING, "Couldn't find matching EndWhile? (While at %s@%s priority %d)\n", chan->context, chan->exten, chan->priority);
00224 }
00225 }
00226 ast_channel_unlock(chan);
00227 return res;
00228 }
00229
00230 if (!end && !while_pri) {
00231 char *goto_str;
00232 size = strlen(chan->context) + strlen(chan->exten) + 32;
00233 goto_str = alloca(size);
00234 memset(goto_str, 0, size);
00235 snprintf(goto_str, size, "%s,%s,%d", chan->context, chan->exten, chan->priority);
00236 pbx_builtin_setvar_helper(chan, varname, goto_str);
00237 }
00238
00239 else if (end && while_pri) {
00240
00241 snprintf(end_varname, VAR_SIZE, "END_%s", varname);
00242 if (! pbx_builtin_getvar_helper(chan, end_varname)) {
00243 char *goto_str;
00244 size = strlen(chan->context) + strlen(chan->exten) + 32;
00245 goto_str = alloca(size);
00246 memset(goto_str, 0, size);
00247 snprintf(goto_str, size, "%s,%s,%d", chan->context, chan->exten, chan->priority+1);
00248 pbx_builtin_setvar_helper(chan, end_varname, goto_str);
00249 }
00250 ast_parseable_goto(chan, while_pri);
00251 }
00252
00253 return res;
00254 }
00255
00256 static int while_start_exec(struct ast_channel *chan, void *data) {
00257 return _while_exec(chan, data, 0);
00258 }
00259
00260 static int while_end_exec(struct ast_channel *chan, void *data) {
00261 return _while_exec(chan, data, 1);
00262 }
00263
00264 static int while_exit_exec(struct ast_channel *chan, void *data) {
00265 return _while_exec(chan, data, 2);
00266 }
00267
00268 static int while_continue_exec(struct ast_channel *chan, void *data)
00269 {
00270 int x;
00271 const char *prefix = "WHILE", *while_pri=NULL;
00272
00273 for (x = 0; ; x++) {
00274 const char *tmp = get_index(chan, prefix, x);
00275 if (tmp)
00276 while_pri = tmp;
00277 else
00278 break;
00279 }
00280
00281 if (while_pri)
00282 ast_parseable_goto(chan, while_pri);
00283
00284 return 0;
00285 }
00286
00287 static int unload_module(void)
00288 {
00289 int res;
00290
00291 res = ast_unregister_application(start_app);
00292 res |= ast_unregister_application(stop_app);
00293 res |= ast_unregister_application(exit_app);
00294 res |= ast_unregister_application(continue_app);
00295
00296 return res;
00297 }
00298
00299 static int load_module(void)
00300 {
00301 int res;
00302
00303 res = ast_register_application(start_app, while_start_exec, start_synopsis, start_desc);
00304 res |= ast_register_application(stop_app, while_end_exec, stop_synopsis, stop_desc);
00305 res |= ast_register_application(exit_app, while_exit_exec, exit_synopsis, exit_desc);
00306 res |= ast_register_application(continue_app, while_continue_exec, continue_synopsis, continue_desc);
00307
00308 return res;
00309 }
00310
00311 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "While Loops and Conditional Execution");