Debian Codegraph @

apps/app_if.c

raw symbols SCIP
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright 2022, Naveen Albert <[email protected]>
5 *
6 * Naveen Albert <[email protected]>
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 <[email protected]>
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 <since>
41 <version>18.16.0</version>
42 <version>20.1.0</version>
43 </since>
44 <synopsis>
45 Start an if branch.
46 </synopsis>
47 <syntax>
48 <parameter name="expr" required="true" />
49 </syntax>
50 <description>
51 <para>Start an If branch. Execution will continue inside the branch
52 if expr is true.</para>
53 <note><para>This application (and related applications) set variables
54 internally during execution.</para></note>
55 </description>
56 <see-also>
57 <ref type="application">ElseIf</ref>
58 <ref type="application">Else</ref>
59 <ref type="application">EndIf</ref>
60 <ref type="application">ExitIf</ref>
61 </see-also>
62 </application>
63 <application name="ElseIf" language="en_US">
64 <since>
65 <version>18.16.0</version>
66 <version>20.1.0</version>
67 </since>
68 <synopsis>
69 Start an else if branch.
70 </synopsis>
71 <syntax>
72 <parameter name="expr" required="true" />
73 </syntax>
74 <description>
75 <para>Start an optional ElseIf branch. Execution will continue inside the branch
76 if expr is true and if previous If and ElseIf branches evaluated to false.</para>
77 <para>Please note that execution inside a true If branch will fallthrough into
78 ElseIf unless the If segment is terminated with an ExitIf call. This is only
79 necessary with ElseIf but not with Else.</para>
80 </description>
81 <see-also>
82 <ref type="application">If</ref>
83 <ref type="application">Else</ref>
84 <ref type="application">EndIf</ref>
85 <ref type="application">ExitIf</ref>
86 </see-also>
87 </application>
88 <application name="Else" language="en_US">
89 <since>
90 <version>18.16.0</version>
91 <version>20.1.0</version>
92 </since>
93 <synopsis>
94 Define an optional else branch.
95 </synopsis>
96 <syntax>
97 <parameter name="expr" required="true" />
98 </syntax>
99 <description>
100 <para>Start an Else branch. Execution will jump here if all previous
101 If and ElseIf branches evaluated to false.</para>
102 </description>
103 <see-also>
104 <ref type="application">If</ref>
105 <ref type="application">ElseIf</ref>
106 <ref type="application">EndIf</ref>
107 <ref type="application">ExitIf</ref>
108 </see-also>
109 </application>
110 <application name="EndIf" language="en_US">
111 <since>
112 <version>18.16.0</version>
113 <version>20.1.0</version>
114 </since>
115 <synopsis>
116 End an if branch.
117 </synopsis>
118 <syntax />
119 <description>
120 <para>Ends the branch begun by the preceding <literal>If()</literal> application.</para>
121 </description>
122 <see-also>
123 <ref type="application">If</ref>
124 <ref type="application">ElseIf</ref>
125 <ref type="application">Else</ref>
126 <ref type="application">ExitIf</ref>
127 </see-also>
128 </application>
129 <application name="ExitIf" language="en_US">
130 <since>
131 <version>18.16.0</version>
132 <version>20.1.0</version>
133 </since>
134 <synopsis>
135 End an If branch.
136 </synopsis>
137 <syntax />
138 <description>
139 <para>Exits an <literal>If()</literal> branch, whether or not it has completed.</para>
140 </description>
141 <see-also>
142 <ref type="application">If</ref>
143 <ref type="application">ElseIf</ref>
144 <ref type="application">Else</ref>
145 <ref type="application">EndIf</ref>
146 </see-also>
147 </application>
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 /* This is the matching extension we want */
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 /* No match; run through includes */
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 /* This is the matching context we want */
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]; /* + IF_ */
260 char end_varname[sizeof(varname) + 4]; /* + END_ + sizeof(varname) */
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 /* Condition Met (clean up helper vars) */
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 /* For EndIf, simply go to the next priority.
309 * We do not add 1 to ast_channel_priority because the dialplan will
310 * auto-increment the priority when we return, so just keep the priority as is.
311 * For ExitIf or false If() condition, we need to find the end of the current
312 * If branch (at same indentation) and branch there. */
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--; /* back up a priority, since it returned the priority after the ElseIf */
319 /* If is false, and ElseIf exists, so jump to ElseIf */
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 /* 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. */
324 /* If is false, and Else exists, so jump to Else */
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) { /* Condition added because of end > 0 instead of 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 /* END of branch */
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");