|
| 1 | +/* |
| 2 | + * Asterisk -- An open source telephony toolkit. |
| 3 | + * |
| 4 | + * Copyright 2022, Naveen Albert <asterisk@phreaknet.org> |
| 5 | + * |
| 6 | + * Naveen Albert <asterisk@phreaknet.org> |
| 7 | + * |
| 8 | + * See http://www.asterisk.org for more information about |
| 9 | + * the Asterisk project. Please do not directly contact |
| 10 | + * any of the maintainers of this project for assistance; |
| 11 | + * the project provides a web site, mailing lists and IRC |
| 12 | + * channels for your use. |
| 13 | + * |
| 14 | + * This program is free software, distributed under the terms of |
| 15 | + * the GNU General Public License Version 2. See the LICENSE file |
| 16 | + * at the top of the source tree. |
| 17 | + */ |
| 18 | + |
| 19 | +/*! \file |
| 20 | + * |
| 21 | + * \brief If Branch Implementation |
| 22 | + * |
| 23 | + * \author Naveen Albert <asterisk@phreaknet.org> |
| 24 | + * |
| 25 | + * \ingroup applications |
| 26 | + */ |
| 27 | + |
| 28 | +/*** MODULEINFO |
| 29 | + <support_level>extended</support_level> |
| 30 | + ***/ |
| 31 | + |
| 32 | +#include "asterisk.h" |
| 33 | + |
| 34 | +#include "asterisk/pbx.h" |
| 35 | +#include "asterisk/module.h" |
| 36 | +#include "asterisk/channel.h" |
| 37 | + |
| 38 | +/*** DOCUMENTATION |
| 39 | + <application name="If" language="en_US"> |
| 40 | + <synopsis> |
| 41 | + Start an if branch. |
| 42 | + </synopsis> |
| 43 | + <syntax> |
| 44 | + <parameter name="expr" required="true" /> |
| 45 | + </syntax> |
| 46 | + <description> |
| 47 | + <para>Start an If branch. Execution will continue inside the branch |
| 48 | + if expr is true.</para> |
| 49 | + <note><para>This application (and related applications) set variables |
| 50 | + internally during execution.</para></note> |
| 51 | + </description> |
| 52 | + <see-also> |
| 53 | + <ref type="application">ElseIf</ref> |
| 54 | + <ref type="application">Else</ref> |
| 55 | + <ref type="application">EndIf</ref> |
| 56 | + <ref type="application">ExitIf</ref> |
| 57 | + </see-also> |
| 58 | + </application> |
| 59 | + <application name="ElseIf" language="en_US"> |
| 60 | + <synopsis> |
| 61 | + Start an else if branch. |
| 62 | + </synopsis> |
| 63 | + <syntax> |
| 64 | + <parameter name="expr" required="true" /> |
| 65 | + </syntax> |
| 66 | + <description> |
| 67 | + <para>Start an optional ElseIf branch. Execution will continue inside the branch |
| 68 | + if expr is true and if previous If and ElseIf branches evaluated to false.</para> |
| 69 | + <para>Please note that execution inside a true If branch will fallthrough into |
| 70 | + ElseIf unless the If segment is terminated with an ExitIf call. This is only |
| 71 | + necessary with ElseIf but not with Else.</para> |
| 72 | + </description> |
| 73 | + <see-also> |
| 74 | + <ref type="application">If</ref> |
| 75 | + <ref type="application">Else</ref> |
| 76 | + <ref type="application">EndIf</ref> |
| 77 | + <ref type="application">ExitIf</ref> |
| 78 | + </see-also> |
| 79 | + </application> |
| 80 | + <application name="Else" language="en_US"> |
| 81 | + <synopsis> |
| 82 | + Define an optional else branch. |
| 83 | + </synopsis> |
| 84 | + <syntax> |
| 85 | + <parameter name="expr" required="true" /> |
| 86 | + </syntax> |
| 87 | + <description> |
| 88 | + <para>Start an Else branch. Execution will jump here if all previous |
| 89 | + If and ElseIf branches evaluated to false.</para> |
| 90 | + </description> |
| 91 | + <see-also> |
| 92 | + <ref type="application">If</ref> |
| 93 | + <ref type="application">ElseIf</ref> |
| 94 | + <ref type="application">EndIf</ref> |
| 95 | + <ref type="application">ExitIf</ref> |
| 96 | + </see-also> |
| 97 | + </application> |
| 98 | + <application name="EndIf" language="en_US"> |
| 99 | + <synopsis> |
| 100 | + End an if branch. |
| 101 | + </synopsis> |
| 102 | + <syntax /> |
| 103 | + <description> |
| 104 | + <para>Ends the branch begun by the preceding <literal>If()</literal> application.</para> |
| 105 | + </description> |
| 106 | + <see-also> |
| 107 | + <ref type="application">If</ref> |
| 108 | + <ref type="application">ElseIf</ref> |
| 109 | + <ref type="application">Else</ref> |
| 110 | + <ref type="application">ExitIf</ref> |
| 111 | + </see-also> |
| 112 | + </application> |
| 113 | + <application name="ExitIf" language="en_US"> |
| 114 | + <synopsis> |
| 115 | + End an If branch. |
| 116 | + </synopsis> |
| 117 | + <syntax /> |
| 118 | + <description> |
| 119 | + <para>Exits an <literal>If()</literal> branch, whether or not it has completed.</para> |
| 120 | + </description> |
| 121 | + <see-also> |
| 122 | + <ref type="application">If</ref> |
| 123 | + <ref type="application">ElseIf</ref> |
| 124 | + <ref type="application">Else</ref> |
| 125 | + <ref type="application">EndIf</ref> |
| 126 | + </see-also> |
| 127 | + </application> |
| 128 | + ***/ |
| 129 | + |
| 130 | +static char *if_app = "If"; |
| 131 | +static char *elseif_app = "ElseIf"; |
| 132 | +static char *else_app = "Else"; |
| 133 | +static char *stop_app = "EndIf"; |
| 134 | +static char *exit_app = "ExitIf"; |
| 135 | + |
| 136 | +#define VAR_SIZE 64 |
| 137 | + |
| 138 | +static const char *get_index(struct ast_channel *chan, const char *prefix, int idx) |
| 139 | +{ |
| 140 | + char varname[VAR_SIZE]; |
| 141 | + |
| 142 | + snprintf(varname, VAR_SIZE, "%s_%d", prefix, idx); |
| 143 | + return pbx_builtin_getvar_helper(chan, varname); |
| 144 | +} |
| 145 | + |
| 146 | +static struct ast_exten *find_matching_priority(struct ast_context *c, const char *exten, int priority, const char *callerid) |
| 147 | +{ |
| 148 | + struct ast_exten *e; |
| 149 | + struct ast_context *c2; |
| 150 | + int idx; |
| 151 | + |
| 152 | + for (e = ast_walk_context_extensions(c, NULL); e; e = ast_walk_context_extensions(c, e)) { |
| 153 | + if (ast_extension_match(ast_get_extension_name(e), exten)) { |
| 154 | + int needmatch = ast_get_extension_matchcid(e); |
| 155 | + if ((needmatch && ast_extension_match(ast_get_extension_cidmatch(e), callerid)) || |
| 156 | + (!needmatch)) { |
| 157 | + /* This is the matching extension we want */ |
| 158 | + struct ast_exten *p; |
| 159 | + for (p = ast_walk_extension_priorities(e, NULL); p; p = ast_walk_extension_priorities(e, p)) { |
| 160 | + if (priority != ast_get_extension_priority(p)) |
| 161 | + continue; |
| 162 | + return p; |
| 163 | + } |
| 164 | + } |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + /* No match; run through includes */ |
| 169 | + for (idx = 0; idx < ast_context_includes_count(c); idx++) { |
| 170 | + const struct ast_include *i = ast_context_includes_get(c, idx); |
| 171 | + |
| 172 | + for (c2 = ast_walk_contexts(NULL); c2; c2 = ast_walk_contexts(c2)) { |
| 173 | + if (!strcmp(ast_get_context_name(c2), ast_get_include_name(i))) { |
| 174 | + e = find_matching_priority(c2, exten, priority, callerid); |
| 175 | + if (e) |
| 176 | + return e; |
| 177 | + } |
| 178 | + } |
| 179 | + } |
| 180 | + return NULL; |
| 181 | +} |
| 182 | + |
| 183 | +static int find_matching_endif(struct ast_channel *chan, const char *otherapp) |
| 184 | +{ |
| 185 | + struct ast_context *c; |
| 186 | + int res = -1; |
| 187 | + |
| 188 | + if (ast_rdlock_contexts()) { |
| 189 | + ast_log(LOG_ERROR, "Failed to lock contexts list\n"); |
| 190 | + return -1; |
| 191 | + } |
| 192 | + |
| 193 | + for (c = ast_walk_contexts(NULL); c; c = ast_walk_contexts(c)) { |
| 194 | + struct ast_exten *e; |
| 195 | + |
| 196 | + if (!ast_rdlock_context(c)) { |
| 197 | + if (!strcmp(ast_get_context_name(c), ast_channel_context(chan))) { |
| 198 | + /* This is the matching context we want */ |
| 199 | + int cur_priority = ast_channel_priority(chan) + 1, level = 1; |
| 200 | + |
| 201 | + for (e = find_matching_priority(c, ast_channel_exten(chan), cur_priority, |
| 202 | + S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL)); |
| 203 | + e; |
| 204 | + e = find_matching_priority(c, ast_channel_exten(chan), ++cur_priority, |
| 205 | + S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL))) { |
| 206 | + if (!strcasecmp(ast_get_extension_app(e), "IF")) { |
| 207 | + level++; |
| 208 | + } else if (!strcasecmp(ast_get_extension_app(e), "ENDIF")) { |
| 209 | + level--; |
| 210 | + } |
| 211 | + |
| 212 | + if (!otherapp && level == 0) { |
| 213 | + res = cur_priority; |
| 214 | + break; |
| 215 | + } else if (otherapp && level == 1 && !strcasecmp(ast_get_extension_app(e), otherapp)) { |
| 216 | + res = cur_priority; |
| 217 | + break; |
| 218 | + } |
| 219 | + } |
| 220 | + } |
| 221 | + ast_unlock_context(c); |
| 222 | + if (res > 0) { |
| 223 | + break; |
| 224 | + } |
| 225 | + } |
| 226 | + } |
| 227 | + ast_unlock_contexts(); |
| 228 | + return res; |
| 229 | +} |
| 230 | + |
| 231 | +static int if_helper(struct ast_channel *chan, const char *data, int end) |
| 232 | +{ |
| 233 | + int res = 0; |
| 234 | + const char *if_pri = NULL; |
| 235 | + char *my_name = NULL; |
| 236 | + const char *label = NULL; |
| 237 | + char varname[VAR_SIZE], end_varname[VAR_SIZE + 4]; |
| 238 | + const char *prefix = "IF"; |
| 239 | + size_t size = 0; |
| 240 | + int used_index_i = -1, x = 0; |
| 241 | + char used_index[VAR_SIZE] = "0", new_index[VAR_SIZE] = "0"; |
| 242 | + |
| 243 | + if (!chan) { |
| 244 | + return -1; |
| 245 | + } |
| 246 | + |
| 247 | + for (x = 0 ;; x++) { |
| 248 | + if (get_index(chan, prefix, x)) { |
| 249 | + used_index_i = x; |
| 250 | + } else { |
| 251 | + break; |
| 252 | + } |
| 253 | + } |
| 254 | + |
| 255 | + snprintf(used_index, VAR_SIZE, "%d", used_index_i); |
| 256 | + snprintf(new_index, VAR_SIZE, "%d", used_index_i + 1); |
| 257 | + |
| 258 | + size = strlen(ast_channel_context(chan)) + strlen(ast_channel_exten(chan)) + 32; |
| 259 | + my_name = ast_alloca(size); |
| 260 | + memset(my_name, 0, size); |
| 261 | + snprintf(my_name, size, "%s_%s_%d", ast_channel_context(chan), ast_channel_exten(chan), ast_channel_priority(chan)); |
| 262 | + |
| 263 | + ast_channel_lock(chan); |
| 264 | + if (end > 1) { |
| 265 | + label = used_index; |
| 266 | + } else if (!(label = pbx_builtin_getvar_helper(chan, my_name))) { |
| 267 | + label = new_index; |
| 268 | + pbx_builtin_setvar_helper(chan, my_name, label); |
| 269 | + } |
| 270 | + snprintf(varname, sizeof(varname), "%s_%s", prefix, label); |
| 271 | + if ((if_pri = pbx_builtin_getvar_helper(chan, varname)) && !end) { |
| 272 | + if_pri = ast_strdupa(if_pri); |
| 273 | + snprintf(end_varname,sizeof(end_varname),"END_%s",varname); |
| 274 | + } |
| 275 | + ast_channel_unlock(chan); |
| 276 | + |
| 277 | + if ((end <= 1 && !pbx_checkcondition(ast_strdupa(data))) || (end > 1)) { |
| 278 | + /* Condition Met (clean up helper vars) */ |
| 279 | + const char *goto_str; |
| 280 | + int pri, endifpri; |
| 281 | + pbx_builtin_setvar_helper(chan, varname, NULL); |
| 282 | + pbx_builtin_setvar_helper(chan, my_name, NULL); |
| 283 | + snprintf(end_varname,sizeof(end_varname),"END_%s",varname); |
| 284 | + ast_channel_lock(chan); |
| 285 | + endifpri = find_matching_endif(chan, NULL); |
| 286 | + if ((goto_str = pbx_builtin_getvar_helper(chan, end_varname))) { |
| 287 | + ast_parseable_goto(chan, goto_str); |
| 288 | + pbx_builtin_setvar_helper(chan, end_varname, NULL); |
| 289 | + } else if (end <= 1 && (pri = find_matching_endif(chan, "ElseIf")) > 0 && pri < endifpri) { |
| 290 | + pri--; /* back up a priority, since it returned the priority after the ElseIf */ |
| 291 | + /* If is false, and ElseIf exists, so jump to ElseIf */ |
| 292 | + ast_verb(3, "Taking conditional false branch, jumping to priority %d\n", pri); |
| 293 | + ast_channel_priority_set(chan, pri); |
| 294 | + } else if (end <= 1 && (pri = find_matching_endif(chan, "Else")) > 0 && pri < endifpri) { |
| 295 | + /* don't need to back up a priority, because we don't actually need to execute Else, just jump to the priority after. Directly executing Else will exit the conditional. */ |
| 296 | + /* If is false, and Else exists, so jump to Else */ |
| 297 | + ast_verb(3, "Taking absolute false branch, jumping to priority %d\n", pri); |
| 298 | + ast_channel_priority_set(chan, pri); |
| 299 | + } else { |
| 300 | + pri = endifpri; |
| 301 | + if (pri > 0) { |
| 302 | + ast_verb(3, "Exiting conditional, jumping to priority %d\n", pri); |
| 303 | + ast_channel_priority_set(chan, pri); |
| 304 | + } else if (end == 4) { /* Condition added because of end > 0 instead of end == 4 */ |
| 305 | + 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)); |
| 306 | + } |
| 307 | + } |
| 308 | + ast_channel_unlock(chan); |
| 309 | + return res; |
| 310 | + } |
| 311 | + |
| 312 | + if (end <= 1 && !if_pri) { |
| 313 | + char *goto_str; |
| 314 | + size = strlen(ast_channel_context(chan)) + strlen(ast_channel_exten(chan)) + 32; |
| 315 | + goto_str = ast_alloca(size); |
| 316 | + memset(goto_str, 0, size); |
| 317 | + snprintf(goto_str, size, "%s,%s,%d", ast_channel_context(chan), ast_channel_exten(chan), ast_channel_priority(chan)); |
| 318 | + pbx_builtin_setvar_helper(chan, varname, goto_str); |
| 319 | + } else if (end > 1 && if_pri) { |
| 320 | + /* END of branch */ |
| 321 | + snprintf(end_varname, sizeof(end_varname), "END_%s", varname); |
| 322 | + if (!pbx_builtin_getvar_helper(chan, end_varname)) { |
| 323 | + char *goto_str; |
| 324 | + size = strlen(ast_channel_context(chan)) + strlen(ast_channel_exten(chan)) + 32; |
| 325 | + goto_str = ast_alloca(size); |
| 326 | + memset(goto_str, 0, size); |
| 327 | + snprintf(goto_str, size, "%s,%s,%d", ast_channel_context(chan), ast_channel_exten(chan), ast_channel_priority(chan)+1); |
| 328 | + pbx_builtin_setvar_helper(chan, end_varname, goto_str); |
| 329 | + } |
| 330 | + ast_parseable_goto(chan, if_pri); |
| 331 | + } |
| 332 | + |
| 333 | + return res; |
| 334 | +} |
| 335 | + |
| 336 | +static int if_exec(struct ast_channel *chan, const char *data) { |
| 337 | + return if_helper(chan, data, 0); |
| 338 | +} |
| 339 | + |
| 340 | +static int elseif_exec(struct ast_channel *chan, const char *data) { |
| 341 | + return if_helper(chan, data, 1); |
| 342 | +} |
| 343 | + |
| 344 | +static int end_exec(struct ast_channel *chan, const char *data) { |
| 345 | + return if_helper(chan, data, 2); |
| 346 | +} |
| 347 | + |
| 348 | +static int else_exec(struct ast_channel *chan, const char *data) { |
| 349 | + return if_helper(chan, data, 3); |
| 350 | +} |
| 351 | + |
| 352 | +static int exit_exec(struct ast_channel *chan, const char *data) { |
| 353 | + return if_helper(chan, data, 4); |
| 354 | +} |
| 355 | + |
| 356 | +static int unload_module(void) |
| 357 | +{ |
| 358 | + int res; |
| 359 | + |
| 360 | + res = ast_unregister_application(if_app); |
| 361 | + res |= ast_unregister_application(elseif_app); |
| 362 | + res |= ast_unregister_application(stop_app); |
| 363 | + res |= ast_unregister_application(else_app); |
| 364 | + res |= ast_unregister_application(exit_app); |
| 365 | + |
| 366 | + return res; |
| 367 | +} |
| 368 | + |
| 369 | +static int load_module(void) |
| 370 | +{ |
| 371 | + int res; |
| 372 | + |
| 373 | + res = ast_register_application_xml(if_app, if_exec); |
| 374 | + res |= ast_register_application_xml(elseif_app, elseif_exec); |
| 375 | + res |= ast_register_application_xml(stop_app, end_exec); |
| 376 | + res |= ast_register_application_xml(else_app, else_exec); |
| 377 | + res |= ast_register_application_xml(exit_app, exit_exec); |
| 378 | + |
| 379 | + return res; |
| 380 | +} |
| 381 | + |
| 382 | +AST_MODULE_INFO_STANDARD_EXTENDED(ASTERISK_GPL_KEY, "If Branch and Conditional Execution"); |
0 commit comments