| 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 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
static char *if_app = "If"; |
| 151 |
static char *elseif_app = "ElseIf"; |
| 152 |
static char *else_app = "Else"; |
| 153 |
static char *stop_app = "EndIf"; |
| 154 |
static char *exit_app = "ExitIf"; |
| 155 |
|
| 156 |
#define VAR_SIZE 64 |
| 157 |
|
| 158 |
static const char *get_index(struct ast_channel *chan, const char *prefix, int idx) |
| 159 |
{ |
| 160 |
char varname[VAR_SIZE]; |
| 161 |
|
| 162 |
snprintf(varname, VAR_SIZE, "%s_%d", prefix, idx); |
| 163 |
return pbx_builtin_getvar_helper(chan, varname); |
| 164 |
} |
| 165 |
|
| 166 |
static struct ast_exten *find_matching_priority(struct ast_context *c, const char *exten, int priority, const char *callerid) |
| 167 |
{ |
| 168 |
struct ast_exten *e; |
| 169 |
struct ast_context *c2; |
| 170 |
int idx; |
| 171 |
|
| 172 |
for (e = ast_walk_context_extensions(c, NULL); e; e = ast_walk_context_extensions(c, e)) { |
| 173 |
if (ast_extension_match(ast_get_extension_name(e), exten)) { |
| 174 |
int needmatch = ast_get_extension_matchcid(e); |
| 175 |
if ((needmatch && ast_extension_match(ast_get_extension_cidmatch(e), callerid)) || |
| 176 |
(!needmatch)) { |
| 177 |
|
| 178 |
struct ast_exten *p; |
| 179 |
for (p = ast_walk_extension_priorities(e, NULL); p; p = ast_walk_extension_priorities(e, p)) { |
| 180 |
if (priority != ast_get_extension_priority(p)) |
| 181 |
continue; |
| 182 |
return p; |
| 183 |
} |
| 184 |
} |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
for (idx = 0; idx < ast_context_includes_count(c); idx++) { |
| 190 |
const struct ast_include *i = ast_context_includes_get(c, idx); |
| 191 |
|
| 192 |
for (c2 = ast_walk_contexts(NULL); c2; c2 = ast_walk_contexts(c2)) { |
| 193 |
if (!strcmp(ast_get_context_name(c2), ast_get_include_name(i))) { |
| 194 |
e = find_matching_priority(c2, exten, priority, callerid); |
| 195 |
if (e) |
| 196 |
return e; |
| 197 |
} |
| 198 |
} |
| 199 |
} |
| 200 |
return NULL; |
| 201 |
} |
| 202 |
|
| 203 |
static int find_matching_endif(struct ast_channel *chan, const char *otherapp) |
| 204 |
{ |
| 205 |
struct ast_context *c; |
| 206 |
int res = -1; |
| 207 |
|
| 208 |
if (ast_rdlock_contexts()) { |
| 209 |
ast_log(LOG_ERROR, "Failed to lock contexts list\n"); |
| 210 |
return -1; |
| 211 |
} |
| 212 |
|
| 213 |
for (c = ast_walk_contexts(NULL); c; c = ast_walk_contexts(c)) { |
| 214 |
struct ast_exten *e; |
| 215 |
|
| 216 |
if (!ast_rdlock_context(c)) { |
| 217 |
if (!strcmp(ast_get_context_name(c), ast_channel_context(chan))) { |
| 218 |
|
| 219 |
|
| 220 |
int cur_priority = ast_channel_priority(chan) + 1, level = 1; |
| 221 |
|
| 222 |
for (e = find_matching_priority(c, ast_channel_exten(chan), cur_priority, |
| 223 |
S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL)); |
| 224 |
e; |
| 225 |
e = find_matching_priority(c, ast_channel_exten(chan), ++cur_priority, |
| 226 |
S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL))) { |
| 227 |
|
| 228 |
if (!strcasecmp(ast_get_extension_app(e), "IF")) { |
| 229 |
level++; |
| 230 |
} else if (!strcasecmp(ast_get_extension_app(e), "ENDIF")) { |
| 231 |
level--; |
| 232 |
} |
| 233 |
|
| 234 |
if (!otherapp && level == 0) { |
| 235 |
res = cur_priority; |
| 236 |
break; |
| 237 |
} else if (otherapp && level == 1 && !strcasecmp(ast_get_extension_app(e), otherapp)) { |
| 238 |
res = cur_priority; |
| 239 |
break; |
| 240 |
} |
| 241 |
} |
| 242 |
} |
| 243 |
ast_unlock_context(c); |
| 244 |
if (res > 0) { |
| 245 |
break; |
| 246 |
} |
| 247 |
} |
| 248 |
} |
| 249 |
ast_unlock_contexts(); |
| 250 |
return res; |
| 251 |
} |
| 252 |
|
| 253 |
static int if_helper(struct ast_channel *chan, const char *data, int end) |
| 254 |
{ |
| 255 |
int res = 0; |
| 256 |
const char *if_pri = NULL; |
| 257 |
char *my_name = NULL; |
| 258 |
const char *label = NULL; |
| 259 |
char varname[VAR_SIZE + 3]; |
| 260 |
char end_varname[sizeof(varname) + 4]; |
| 261 |
const char *prefix = "IF"; |
| 262 |
size_t size = 0; |
| 263 |
int used_index_i = -1, x = 0; |
| 264 |
char used_index[VAR_SIZE] = "0", new_index[VAR_SIZE] = "0"; |
| 265 |
|
| 266 |
if (!chan) { |
| 267 |
return -1; |
| 268 |
} |
| 269 |
|
| 270 |
for (x = 0 ;; x++) { |
| 271 |
if (get_index(chan, prefix, x)) { |
| 272 |
used_index_i = x; |
| 273 |
} else { |
| 274 |
break; |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
snprintf(used_index, sizeof(used_index), "%d", used_index_i); |
| 279 |
snprintf(new_index, sizeof(new_index), "%d", used_index_i + 1); |
| 280 |
|
| 281 |
size = strlen(ast_channel_context(chan)) + strlen(ast_channel_exten(chan)) + 32; |
| 282 |
my_name = ast_alloca(size); |
| 283 |
memset(my_name, 0, size); |
| 284 |
snprintf(my_name, size, "%s_%s_%d", ast_channel_context(chan), ast_channel_exten(chan), ast_channel_priority(chan)); |
| 285 |
|
| 286 |
ast_channel_lock(chan); |
| 287 |
if (end > 1) { |
| 288 |
label = used_index; |
| 289 |
} else if (!(label = pbx_builtin_getvar_helper(chan, my_name))) { |
| 290 |
label = new_index; |
| 291 |
pbx_builtin_setvar_helper(chan, my_name, label); |
| 292 |
} |
| 293 |
snprintf(varname, sizeof(varname), "%s_%s", prefix, label); |
| 294 |
if ((if_pri = pbx_builtin_getvar_helper(chan, varname)) && !end) { |
| 295 |
if_pri = ast_strdupa(if_pri); |
| 296 |
snprintf(end_varname,sizeof(end_varname),"END_%s",varname); |
| 297 |
} |
| 298 |
ast_channel_unlock(chan); |
| 299 |
|
| 300 |
if ((end <= 1 && !pbx_checkcondition(ast_strdupa(data))) || (end > 1)) { |
| 301 |
|
| 302 |
const char *goto_str; |
| 303 |
int pri, endifpri; |
| 304 |
pbx_builtin_setvar_helper(chan, varname, NULL); |
| 305 |
pbx_builtin_setvar_helper(chan, my_name, NULL); |
| 306 |
snprintf(end_varname,sizeof(end_varname),"END_%s",varname); |
| 307 |
ast_channel_lock(chan); |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
endifpri = end == 2 ? ast_channel_priority(chan) : find_matching_endif(chan, NULL); |
| 314 |
if ((goto_str = pbx_builtin_getvar_helper(chan, end_varname))) { |
| 315 |
ast_parseable_goto(chan, goto_str); |
| 316 |
pbx_builtin_setvar_helper(chan, end_varname, NULL); |
| 317 |
} else if (end <= 1 && (pri = find_matching_endif(chan, "ElseIf")) > 0 && pri < endifpri) { |
| 318 |
pri--; |
| 319 |
|
| 320 |
ast_verb(3, "Taking conditional false branch, jumping to priority %d\n", pri); |
| 321 |
ast_channel_priority_set(chan, pri); |
| 322 |
} else if (end <= 1 && (pri = find_matching_endif(chan, "Else")) > 0 && pri < endifpri) { |
| 323 |
|
| 324 |
|
| 325 |
ast_verb(3, "Taking absolute false branch, jumping to priority %d\n", pri); |
| 326 |
ast_channel_priority_set(chan, pri); |
| 327 |
} else { |
| 328 |
pri = endifpri; |
| 329 |
if (pri > 0) { |
| 330 |
ast_verb(3, "Exiting conditional, jumping to priority %d\n", pri); |
| 331 |
ast_channel_priority_set(chan, pri); |
| 332 |
} else if (end == 4) { |
| 333 |
ast_log(LOG_WARNING, "Couldn't find matching EndIf? (If at %s@%s priority %d)\n", ast_channel_context(chan), ast_channel_exten(chan), ast_channel_priority(chan)); |
| 334 |
} |
| 335 |
} |
| 336 |
ast_channel_unlock(chan); |
| 337 |
return res; |
| 338 |
} |
| 339 |
|
| 340 |
if (end <= 1 && !if_pri) { |
| 341 |
char *goto_str; |
| 342 |
size = strlen(ast_channel_context(chan)) + strlen(ast_channel_exten(chan)) + 32; |
| 343 |
goto_str = ast_alloca(size); |
| 344 |
memset(goto_str, 0, size); |
| 345 |
snprintf(goto_str, size, "%s,%s,%d", ast_channel_context(chan), ast_channel_exten(chan), ast_channel_priority(chan)); |
| 346 |
pbx_builtin_setvar_helper(chan, varname, goto_str); |
| 347 |
} else if (end > 1 && if_pri) { |
| 348 |
|
| 349 |
snprintf(end_varname, sizeof(end_varname), "END_%s", varname); |
| 350 |
if (!pbx_builtin_getvar_helper(chan, end_varname)) { |
| 351 |
char *goto_str; |
| 352 |
size = strlen(ast_channel_context(chan)) + strlen(ast_channel_exten(chan)) + 32; |
| 353 |
goto_str = ast_alloca(size); |
| 354 |
memset(goto_str, 0, size); |
| 355 |
snprintf(goto_str, size, "%s,%s,%d", ast_channel_context(chan), ast_channel_exten(chan), ast_channel_priority(chan)+1); |
| 356 |
pbx_builtin_setvar_helper(chan, end_varname, goto_str); |
| 357 |
} |
| 358 |
ast_parseable_goto(chan, if_pri); |
| 359 |
} |
| 360 |
|
| 361 |
return res; |
| 362 |
} |
| 363 |
|
| 364 |
static int if_exec(struct ast_channel *chan, const char *data) { |
| 365 |
return if_helper(chan, data, 0); |
| 366 |
} |
| 367 |
|
| 368 |
static int elseif_exec(struct ast_channel *chan, const char *data) { |
| 369 |
return if_helper(chan, data, 1); |
| 370 |
} |
| 371 |
|
| 372 |
static int end_exec(struct ast_channel *chan, const char *data) { |
| 373 |
return if_helper(chan, data, 2); |
| 374 |
} |
| 375 |
|
| 376 |
static int else_exec(struct ast_channel *chan, const char *data) { |
| 377 |
return if_helper(chan, data, 3); |
| 378 |
} |
| 379 |
|
| 380 |
static int exit_exec(struct ast_channel *chan, const char *data) { |
| 381 |
return if_helper(chan, data, 4); |
| 382 |
} |
| 383 |
|
| 384 |
static int unload_module(void) |
| 385 |
{ |
| 386 |
int res; |
| 387 |
|
| 388 |
res = ast_unregister_application(if_app); |
| 389 |
res |= ast_unregister_application(elseif_app); |
| 390 |
res |= ast_unregister_application(stop_app); |
| 391 |
res |= ast_unregister_application(else_app); |
| 392 |
res |= ast_unregister_application(exit_app); |
| 393 |
|
| 394 |
return res; |
| 395 |
} |
| 396 |
|
| 397 |
static int load_module(void) |
| 398 |
{ |
| 399 |
int res; |
| 400 |
|
| 401 |
res = ast_register_application_xml(if_app, if_exec); |
| 402 |
res |= ast_register_application_xml(elseif_app, elseif_exec); |
| 403 |
res |= ast_register_application_xml(stop_app, end_exec); |
| 404 |
res |= ast_register_application_xml(else_app, else_exec); |
| 405 |
res |= ast_register_application_xml(exit_app, exit_exec); |
| 406 |
|
| 407 |
return res; |
| 408 |
} |
| 409 |
|
| 410 |
AST_MODULE_INFO_STANDARD_EXTENDED(ASTERISK_GPL_KEY, "If Branch and Conditional Execution"); |