app_while.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright 2004 - 2005, Anthony Minessale <anthmct@yahoo.com>
  5. *
  6. * Anthony Minessale <anthmct@yahoo.com>
  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. /*! \file
  19. *
  20. * \brief While Loop Implementation
  21. *
  22. * \author Anthony Minessale <anthmct@yahoo.com>
  23. *
  24. * \ingroup applications
  25. */
  26. /*** MODULEINFO
  27. <support_level>core</support_level>
  28. ***/
  29. #include "asterisk.h"
  30. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  31. #include "asterisk/pbx.h"
  32. #include "asterisk/module.h"
  33. #include "asterisk/channel.h"
  34. /*** DOCUMENTATION
  35. <application name="While" language="en_US">
  36. <synopsis>
  37. Start a while loop.
  38. </synopsis>
  39. <syntax>
  40. <parameter name="expr" required="true" />
  41. </syntax>
  42. <description>
  43. <para>Start a While Loop. Execution will return to this point when
  44. <literal>EndWhile()</literal> is called until expr is no longer true.</para>
  45. </description>
  46. <see-also>
  47. <ref type="application">EndWhile</ref>
  48. <ref type="application">ExitWhile</ref>
  49. <ref type="application">ContinueWhile</ref>
  50. </see-also>
  51. </application>
  52. <application name="EndWhile" language="en_US">
  53. <synopsis>
  54. End a while loop.
  55. </synopsis>
  56. <syntax />
  57. <description>
  58. <para>Return to the previous called <literal>While()</literal>.</para>
  59. </description>
  60. <see-also>
  61. <ref type="application">While</ref>
  62. <ref type="application">ExitWhile</ref>
  63. <ref type="application">ContinueWhile</ref>
  64. </see-also>
  65. </application>
  66. <application name="ExitWhile" language="en_US">
  67. <synopsis>
  68. End a While loop.
  69. </synopsis>
  70. <syntax />
  71. <description>
  72. <para>Exits a <literal>While()</literal> loop, whether or not the conditional has been satisfied.</para>
  73. </description>
  74. <see-also>
  75. <ref type="application">While</ref>
  76. <ref type="application">EndWhile</ref>
  77. <ref type="application">ContinueWhile</ref>
  78. </see-also>
  79. </application>
  80. <application name="ContinueWhile" language="en_US">
  81. <synopsis>
  82. Restart a While loop.
  83. </synopsis>
  84. <syntax />
  85. <description>
  86. <para>Returns to the top of the while loop and re-evaluates the conditional.</para>
  87. </description>
  88. <see-also>
  89. <ref type="application">While</ref>
  90. <ref type="application">EndWhile</ref>
  91. <ref type="application">ExitWhile</ref>
  92. </see-also>
  93. </application>
  94. ***/
  95. static char *start_app = "While";
  96. static char *stop_app = "EndWhile";
  97. static char *exit_app = "ExitWhile";
  98. static char *continue_app = "ContinueWhile";
  99. #define VAR_SIZE 64
  100. static const char *get_index(struct ast_channel *chan, const char *prefix, int idx) {
  101. char varname[VAR_SIZE];
  102. snprintf(varname, VAR_SIZE, "%s_%d", prefix, idx);
  103. return pbx_builtin_getvar_helper(chan, varname);
  104. }
  105. static struct ast_exten *find_matching_priority(struct ast_context *c, const char *exten, int priority, const char *callerid)
  106. {
  107. struct ast_exten *e;
  108. struct ast_include *i;
  109. struct ast_context *c2;
  110. for (e=ast_walk_context_extensions(c, NULL); e; e=ast_walk_context_extensions(c, e)) {
  111. if (ast_extension_match(ast_get_extension_name(e), exten)) {
  112. int needmatch = ast_get_extension_matchcid(e);
  113. if ((needmatch && ast_extension_match(ast_get_extension_cidmatch(e), callerid)) ||
  114. (!needmatch)) {
  115. /* This is the matching extension we want */
  116. struct ast_exten *p;
  117. for (p=ast_walk_extension_priorities(e, NULL); p; p=ast_walk_extension_priorities(e, p)) {
  118. if (priority != ast_get_extension_priority(p))
  119. continue;
  120. return p;
  121. }
  122. }
  123. }
  124. }
  125. /* No match; run through includes */
  126. for (i=ast_walk_context_includes(c, NULL); i; i=ast_walk_context_includes(c, i)) {
  127. for (c2=ast_walk_contexts(NULL); c2; c2=ast_walk_contexts(c2)) {
  128. if (!strcmp(ast_get_context_name(c2), ast_get_include_name(i))) {
  129. e = find_matching_priority(c2, exten, priority, callerid);
  130. if (e)
  131. return e;
  132. }
  133. }
  134. }
  135. return NULL;
  136. }
  137. static int find_matching_endwhile(struct ast_channel *chan)
  138. {
  139. struct ast_context *c;
  140. int res=-1;
  141. if (ast_rdlock_contexts()) {
  142. ast_log(LOG_ERROR, "Failed to lock contexts list\n");
  143. return -1;
  144. }
  145. for (c=ast_walk_contexts(NULL); c; c=ast_walk_contexts(c)) {
  146. struct ast_exten *e;
  147. if (!ast_rdlock_context(c)) {
  148. if (!strcmp(ast_get_context_name(c), ast_channel_context(chan))) {
  149. /* This is the matching context we want */
  150. int cur_priority = ast_channel_priority(chan) + 1, level=1;
  151. for (e = find_matching_priority(c, ast_channel_exten(chan), cur_priority,
  152. S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL));
  153. e;
  154. e = find_matching_priority(c, ast_channel_exten(chan), ++cur_priority,
  155. S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL))) {
  156. if (!strcasecmp(ast_get_extension_app(e), "WHILE")) {
  157. level++;
  158. } else if (!strcasecmp(ast_get_extension_app(e), "ENDWHILE")) {
  159. level--;
  160. }
  161. if (level == 0) {
  162. res = cur_priority;
  163. break;
  164. }
  165. }
  166. }
  167. ast_unlock_context(c);
  168. if (res > 0) {
  169. break;
  170. }
  171. }
  172. }
  173. ast_unlock_contexts();
  174. return res;
  175. }
  176. static int _while_exec(struct ast_channel *chan, const char *data, int end)
  177. {
  178. int res=0;
  179. const char *while_pri = NULL;
  180. char *my_name = NULL;
  181. const char *condition = NULL, *label = NULL;
  182. char varname[VAR_SIZE], end_varname[VAR_SIZE];
  183. const char *prefix = "WHILE";
  184. size_t size=0;
  185. int used_index_i = -1, x=0;
  186. char used_index[VAR_SIZE] = "0", new_index[VAR_SIZE] = "0";
  187. if (!chan) {
  188. /* huh ? */
  189. return -1;
  190. }
  191. #if 0
  192. /* don't want run away loops if the chan isn't even up
  193. this is up for debate since it slows things down a tad ......
  194. Debate is over... this prevents While/EndWhile from working
  195. within the "h" extension. Not good.
  196. */
  197. if (ast_waitfordigit(chan,1) < 0)
  198. return -1;
  199. #endif
  200. for (x=0;;x++) {
  201. if (get_index(chan, prefix, x)) {
  202. used_index_i = x;
  203. } else
  204. break;
  205. }
  206. snprintf(used_index, VAR_SIZE, "%d", used_index_i);
  207. snprintf(new_index, VAR_SIZE, "%d", used_index_i + 1);
  208. if (!end)
  209. condition = ast_strdupa(data);
  210. size = strlen(ast_channel_context(chan)) + strlen(ast_channel_exten(chan)) + 32;
  211. my_name = ast_alloca(size);
  212. memset(my_name, 0, size);
  213. snprintf(my_name, size, "%s_%s_%d", ast_channel_context(chan), ast_channel_exten(chan), ast_channel_priority(chan));
  214. ast_channel_lock(chan);
  215. if (end) {
  216. label = used_index;
  217. } else if (!(label = pbx_builtin_getvar_helper(chan, my_name))) {
  218. label = new_index;
  219. pbx_builtin_setvar_helper(chan, my_name, label);
  220. }
  221. snprintf(varname, VAR_SIZE, "%s_%s", prefix, label);
  222. if ((while_pri = pbx_builtin_getvar_helper(chan, varname)) && !end) {
  223. while_pri = ast_strdupa(while_pri);
  224. snprintf(end_varname,VAR_SIZE,"END_%s",varname);
  225. }
  226. ast_channel_unlock(chan);
  227. if ((!end && !pbx_checkcondition(condition)) || (end == 2)) {
  228. /* Condition Met (clean up helper vars) */
  229. const char *goto_str;
  230. pbx_builtin_setvar_helper(chan, varname, NULL);
  231. pbx_builtin_setvar_helper(chan, my_name, NULL);
  232. snprintf(end_varname,VAR_SIZE,"END_%s",varname);
  233. ast_channel_lock(chan);
  234. if ((goto_str = pbx_builtin_getvar_helper(chan, end_varname))) {
  235. ast_parseable_goto(chan, goto_str);
  236. pbx_builtin_setvar_helper(chan, end_varname, NULL);
  237. } else {
  238. int pri = find_matching_endwhile(chan);
  239. if (pri > 0) {
  240. ast_verb(3, "Jumping to priority %d\n", pri);
  241. ast_channel_priority_set(chan, pri);
  242. } else {
  243. 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));
  244. }
  245. }
  246. ast_channel_unlock(chan);
  247. return res;
  248. }
  249. if (!end && !while_pri) {
  250. char *goto_str;
  251. size = strlen(ast_channel_context(chan)) + strlen(ast_channel_exten(chan)) + 32;
  252. goto_str = ast_alloca(size);
  253. memset(goto_str, 0, size);
  254. snprintf(goto_str, size, "%s,%s,%d", ast_channel_context(chan), ast_channel_exten(chan), ast_channel_priority(chan));
  255. pbx_builtin_setvar_helper(chan, varname, goto_str);
  256. }
  257. else if (end && while_pri) {
  258. /* END of loop */
  259. snprintf(end_varname, VAR_SIZE, "END_%s", varname);
  260. if (! pbx_builtin_getvar_helper(chan, end_varname)) {
  261. char *goto_str;
  262. size = strlen(ast_channel_context(chan)) + strlen(ast_channel_exten(chan)) + 32;
  263. goto_str = ast_alloca(size);
  264. memset(goto_str, 0, size);
  265. snprintf(goto_str, size, "%s,%s,%d", ast_channel_context(chan), ast_channel_exten(chan), ast_channel_priority(chan)+1);
  266. pbx_builtin_setvar_helper(chan, end_varname, goto_str);
  267. }
  268. ast_parseable_goto(chan, while_pri);
  269. }
  270. return res;
  271. }
  272. static int while_start_exec(struct ast_channel *chan, const char *data) {
  273. return _while_exec(chan, data, 0);
  274. }
  275. static int while_end_exec(struct ast_channel *chan, const char *data) {
  276. return _while_exec(chan, data, 1);
  277. }
  278. static int while_exit_exec(struct ast_channel *chan, const char *data) {
  279. return _while_exec(chan, data, 2);
  280. }
  281. static int while_continue_exec(struct ast_channel *chan, const char *data)
  282. {
  283. int x;
  284. const char *prefix = "WHILE", *while_pri=NULL;
  285. for (x = 0; ; x++) {
  286. const char *tmp = get_index(chan, prefix, x);
  287. if (tmp)
  288. while_pri = tmp;
  289. else
  290. break;
  291. }
  292. if (while_pri)
  293. ast_parseable_goto(chan, while_pri);
  294. return 0;
  295. }
  296. static int unload_module(void)
  297. {
  298. int res;
  299. res = ast_unregister_application(start_app);
  300. res |= ast_unregister_application(stop_app);
  301. res |= ast_unregister_application(exit_app);
  302. res |= ast_unregister_application(continue_app);
  303. return res;
  304. }
  305. static int load_module(void)
  306. {
  307. int res;
  308. res = ast_register_application_xml(start_app, while_start_exec);
  309. res |= ast_register_application_xml(stop_app, while_end_exec);
  310. res |= ast_register_application_xml(exit_app, while_exit_exec);
  311. res |= ast_register_application_xml(continue_app, while_continue_exec);
  312. return res;
  313. }
  314. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "While Loops and Conditional Execution");