| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
#include "asterisk.h" |
| 33 |
|
| 34 |
#include "asterisk/pbx.h" |
| 35 |
#include "asterisk/module.h" |
| 36 |
#include "asterisk/channel.h" |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
static char *start_app = "While"; |
| 113 |
static char *stop_app = "EndWhile"; |
| 114 |
static char *exit_app = "ExitWhile"; |
| 115 |
static char *continue_app = "ContinueWhile"; |
| 116 |
|
| 117 |
#define VAR_SIZE 64 |
| 118 |
|
| 119 |
|
| 120 |
static const char *get_index(struct ast_channel *chan, const char *prefix, int idx) { |
| 121 |
char varname[VAR_SIZE]; |
| 122 |
|
| 123 |
snprintf(varname, VAR_SIZE, "%s_%d", prefix, idx); |
| 124 |
return pbx_builtin_getvar_helper(chan, varname); |
| 125 |
} |
| 126 |
|
| 127 |
static struct ast_exten *find_matching_priority(struct ast_context *c, const char *exten, int priority, const char *callerid) |
| 128 |
{ |
| 129 |
struct ast_exten *e; |
| 130 |
struct ast_context *c2; |
| 131 |
int idx; |
| 132 |
|
| 133 |
for (e=ast_walk_context_extensions(c, NULL); e; e=ast_walk_context_extensions(c, e)) { |
| 134 |
if (ast_extension_match(ast_get_extension_name(e), exten)) { |
| 135 |
int needmatch = ast_get_extension_matchcid(e); |
| 136 |
if ((needmatch && ast_extension_match(ast_get_extension_cidmatch(e), callerid)) || |
| 137 |
(!needmatch)) { |
| 138 |
|
| 139 |
struct ast_exten *p; |
| 140 |
for (p=ast_walk_extension_priorities(e, NULL); p; p=ast_walk_extension_priorities(e, p)) { |
| 141 |
if (priority != ast_get_extension_priority(p)) |
| 142 |
continue; |
| 143 |
return p; |
| 144 |
} |
| 145 |
} |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
for (idx = 0; idx < ast_context_includes_count(c); idx++) { |
| 151 |
const struct ast_include *i = ast_context_includes_get(c, idx); |
| 152 |
|
| 153 |
for (c2=ast_walk_contexts(NULL); c2; c2=ast_walk_contexts(c2)) { |
| 154 |
if (!strcmp(ast_get_context_name(c2), ast_get_include_name(i))) { |
| 155 |
e = find_matching_priority(c2, exten, priority, callerid); |
| 156 |
if (e) |
| 157 |
return e; |
| 158 |
} |
| 159 |
} |
| 160 |
} |
| 161 |
return NULL; |
| 162 |
} |
| 163 |
|
| 164 |
static int find_matching_endwhile(struct ast_channel *chan) |
| 165 |
{ |
| 166 |
struct ast_context *c; |
| 167 |
int res=-1; |
| 168 |
|
| 169 |
if (ast_rdlock_contexts()) { |
| 170 |
ast_log(LOG_ERROR, "Failed to lock contexts list\n"); |
| 171 |
return -1; |
| 172 |
} |
| 173 |
|
| 174 |
for (c=ast_walk_contexts(NULL); c; c=ast_walk_contexts(c)) { |
| 175 |
struct ast_exten *e; |
| 176 |
|
| 177 |
if (!ast_rdlock_context(c)) { |
| 178 |
if (!strcmp(ast_get_context_name(c), ast_channel_context(chan))) { |
| 179 |
|
| 180 |
int cur_priority = ast_channel_priority(chan) + 1, level=1; |
| 181 |
|
| 182 |
for (e = find_matching_priority(c, ast_channel_exten(chan), cur_priority, |
| 183 |
S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL)); |
| 184 |
e; |
| 185 |
e = find_matching_priority(c, ast_channel_exten(chan), ++cur_priority, |
| 186 |
S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL))) { |
| 187 |
if (!strcasecmp(ast_get_extension_app(e), "WHILE")) { |
| 188 |
level++; |
| 189 |
} else if (!strcasecmp(ast_get_extension_app(e), "ENDWHILE")) { |
| 190 |
level--; |
| 191 |
} |
| 192 |
|
| 193 |
if (level == 0) { |
| 194 |
res = cur_priority; |
| 195 |
break; |
| 196 |
} |
| 197 |
} |
| 198 |
} |
| 199 |
ast_unlock_context(c); |
| 200 |
if (res > 0) { |
| 201 |
break; |
| 202 |
} |
| 203 |
} |
| 204 |
} |
| 205 |
ast_unlock_contexts(); |
| 206 |
return res; |
| 207 |
} |
| 208 |
|
| 209 |
static int _while_exec(struct ast_channel *chan, const char *data, int end) |
| 210 |
{ |
| 211 |
int res=0; |
| 212 |
const char *while_pri = NULL; |
| 213 |
char *my_name = NULL; |
| 214 |
const char *condition = NULL, *label = NULL; |
| 215 |
char varname[VAR_SIZE], end_varname[VAR_SIZE]; |
| 216 |
const char *prefix = "WHILE"; |
| 217 |
size_t size=0; |
| 218 |
int used_index_i = -1, x=0; |
| 219 |
char used_index[VAR_SIZE] = "0", new_index[VAR_SIZE] = "0"; |
| 220 |
|
| 221 |
if (!chan) { |
| 222 |
|
| 223 |
return -1; |
| 224 |
} |
| 225 |
|
| 226 |
#if 0 |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
if (ast_waitfordigit(chan,1) < 0) |
| 234 |
return -1; |
| 235 |
#endif |
| 236 |
|
| 237 |
for (x=0;;x++) { |
| 238 |
if (get_index(chan, prefix, x)) { |
| 239 |
used_index_i = x; |
| 240 |
} else |
| 241 |
break; |
| 242 |
} |
| 243 |
|
| 244 |
snprintf(used_index, VAR_SIZE, "%d", used_index_i); |
| 245 |
snprintf(new_index, VAR_SIZE, "%d", used_index_i + 1); |
| 246 |
|
| 247 |
if (!end) |
| 248 |
condition = ast_strdupa(data); |
| 249 |
|
| 250 |
size = strlen(ast_channel_context(chan)) + strlen(ast_channel_exten(chan)) + 32; |
| 251 |
my_name = ast_alloca(size); |
| 252 |
memset(my_name, 0, size); |
| 253 |
snprintf(my_name, size, "%s_%s_%d", ast_channel_context(chan), ast_channel_exten(chan), ast_channel_priority(chan)); |
| 254 |
|
| 255 |
ast_channel_lock(chan); |
| 256 |
if (end) { |
| 257 |
label = used_index; |
| 258 |
} else if (!(label = pbx_builtin_getvar_helper(chan, my_name))) { |
| 259 |
label = new_index; |
| 260 |
pbx_builtin_setvar_helper(chan, my_name, label); |
| 261 |
} |
| 262 |
snprintf(varname, VAR_SIZE, "%s_%s", prefix, label); |
| 263 |
if ((while_pri = pbx_builtin_getvar_helper(chan, varname)) && !end) { |
| 264 |
while_pri = ast_strdupa(while_pri); |
| 265 |
snprintf(end_varname,VAR_SIZE,"END_%s",varname); |
| 266 |
} |
| 267 |
ast_channel_unlock(chan); |
| 268 |
|
| 269 |
|
| 270 |
if ((!end && !pbx_checkcondition(condition)) || (end == 2)) { |
| 271 |
|
| 272 |
const char *goto_str; |
| 273 |
pbx_builtin_setvar_helper(chan, varname, NULL); |
| 274 |
pbx_builtin_setvar_helper(chan, my_name, NULL); |
| 275 |
snprintf(end_varname,VAR_SIZE,"END_%s",varname); |
| 276 |
ast_channel_lock(chan); |
| 277 |
if ((goto_str = pbx_builtin_getvar_helper(chan, end_varname))) { |
| 278 |
ast_parseable_goto(chan, goto_str); |
| 279 |
pbx_builtin_setvar_helper(chan, end_varname, NULL); |
| 280 |
} else { |
| 281 |
int pri = find_matching_endwhile(chan); |
| 282 |
if (pri > 0) { |
| 283 |
ast_verb(3, "Jumping to priority %d\n", pri); |
| 284 |
ast_channel_priority_set(chan, pri); |
| 285 |
} else { |
| 286 |
ast_log(LOG_WARNING, "Couldn't find matching EndWhile? (While at %s@%s priority %d)\n", ast_channel_context(chan), ast_channel_exten(chan), ast_channel_priority(chan)); |
| 287 |
} |
| 288 |
} |
| 289 |
ast_channel_unlock(chan); |
| 290 |
return res; |
| 291 |
} |
| 292 |
|
| 293 |
if (!end && !while_pri) { |
| 294 |
char *goto_str; |
| 295 |
size = strlen(ast_channel_context(chan)) + strlen(ast_channel_exten(chan)) + 32; |
| 296 |
goto_str = ast_alloca(size); |
| 297 |
memset(goto_str, 0, size); |
| 298 |
snprintf(goto_str, size, "%s,%s,%d", ast_channel_context(chan), ast_channel_exten(chan), ast_channel_priority(chan)); |
| 299 |
pbx_builtin_setvar_helper(chan, varname, goto_str); |
| 300 |
} |
| 301 |
|
| 302 |
else if (end && while_pri) { |
| 303 |
|
| 304 |
snprintf(end_varname, VAR_SIZE, "END_%s", varname); |
| 305 |
if (! pbx_builtin_getvar_helper(chan, end_varname)) { |
| 306 |
char *goto_str; |
| 307 |
size = strlen(ast_channel_context(chan)) + strlen(ast_channel_exten(chan)) + 32; |
| 308 |
goto_str = ast_alloca(size); |
| 309 |
memset(goto_str, 0, size); |
| 310 |
snprintf(goto_str, size, "%s,%s,%d", ast_channel_context(chan), ast_channel_exten(chan), ast_channel_priority(chan)+1); |
| 311 |
pbx_builtin_setvar_helper(chan, end_varname, goto_str); |
| 312 |
} |
| 313 |
ast_parseable_goto(chan, while_pri); |
| 314 |
} |
| 315 |
|
| 316 |
return res; |
| 317 |
} |
| 318 |
|
| 319 |
static int while_start_exec(struct ast_channel *chan, const char *data) { |
| 320 |
return _while_exec(chan, data, 0); |
| 321 |
} |
| 322 |
|
| 323 |
static int while_end_exec(struct ast_channel *chan, const char *data) { |
| 324 |
return _while_exec(chan, data, 1); |
| 325 |
} |
| 326 |
|
| 327 |
static int while_exit_exec(struct ast_channel *chan, const char *data) { |
| 328 |
return _while_exec(chan, data, 2); |
| 329 |
} |
| 330 |
|
| 331 |
static int while_continue_exec(struct ast_channel *chan, const char *data) |
| 332 |
{ |
| 333 |
int x; |
| 334 |
const char *prefix = "WHILE", *while_pri=NULL; |
| 335 |
|
| 336 |
for (x = 0; ; x++) { |
| 337 |
const char *tmp = get_index(chan, prefix, x); |
| 338 |
if (tmp) |
| 339 |
while_pri = tmp; |
| 340 |
else |
| 341 |
break; |
| 342 |
} |
| 343 |
|
| 344 |
if (while_pri) |
| 345 |
ast_parseable_goto(chan, while_pri); |
| 346 |
|
| 347 |
return 0; |
| 348 |
} |
| 349 |
|
| 350 |
static int unload_module(void) |
| 351 |
{ |
| 352 |
int res; |
| 353 |
|
| 354 |
res = ast_unregister_application(start_app); |
| 355 |
res |= ast_unregister_application(stop_app); |
| 356 |
res |= ast_unregister_application(exit_app); |
| 357 |
res |= ast_unregister_application(continue_app); |
| 358 |
|
| 359 |
return res; |
| 360 |
} |
| 361 |
|
| 362 |
static int load_module(void) |
| 363 |
{ |
| 364 |
int res; |
| 365 |
|
| 366 |
res = ast_register_application_xml(start_app, while_start_exec); |
| 367 |
res |= ast_register_application_xml(stop_app, while_end_exec); |
| 368 |
res |= ast_register_application_xml(exit_app, while_exit_exec); |
| 369 |
res |= ast_register_application_xml(continue_app, while_continue_exec); |
| 370 |
|
| 371 |
return res; |
| 372 |
} |
| 373 |
|
| 374 |
AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "While Loops and Conditional Execution"); |