app_macro.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2005, Digium, Inc.
  5. *
  6. * Mark Spencer <markster@digium.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 Dial plan macro Implementation
  21. *
  22. * \author Mark Spencer <markster@digium.com>
  23. *
  24. * \ingroup applications
  25. */
  26. /*** MODULEINFO
  27. <support_level>core</support_level>
  28. <replacement>app_stack (GoSub)</replacement>
  29. ***/
  30. #include "asterisk.h"
  31. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  32. #include "asterisk/file.h"
  33. #include "asterisk/channel.h"
  34. #include "asterisk/pbx.h"
  35. #include "asterisk/module.h"
  36. #include "asterisk/config.h"
  37. #include "asterisk/utils.h"
  38. #include "asterisk/lock.h"
  39. #include "asterisk/app.h"
  40. /*** DOCUMENTATION
  41. <application name="Macro" language="en_US">
  42. <synopsis>
  43. Macro Implementation.
  44. </synopsis>
  45. <syntax>
  46. <parameter name="name" required="true">
  47. <para>The name of the macro</para>
  48. </parameter>
  49. <parameter name="args">
  50. <argument name="arg1" required="true" />
  51. <argument name="arg2" multiple="true" />
  52. </parameter>
  53. </syntax>
  54. <description>
  55. <para>Executes a macro using the context macro-<replaceable>name</replaceable>,
  56. jumping to the <literal>s</literal> extension of that context and executing each step,
  57. then returning when the steps end.</para>
  58. <para>The calling extension, context, and priority are stored in <variable>MACRO_EXTEN</variable>,
  59. <variable>MACRO_CONTEXT</variable> and <variable>MACRO_PRIORITY</variable> respectively. Arguments
  60. become <variable>ARG1</variable>, <variable>ARG2</variable>, etc in the macro context.</para>
  61. <para>If you Goto out of the Macro context, the Macro will terminate and control will be returned
  62. at the location of the Goto.</para>
  63. <para>If <variable>MACRO_OFFSET</variable> is set at termination, Macro will attempt to continue
  64. at priority MACRO_OFFSET + N + 1 if such a step exists, and N + 1 otherwise.</para>
  65. <warning><para>Because of the way Macro is implemented (it executes the priorities contained within
  66. it via sub-engine), and a fixed per-thread memory stack allowance, macros are limited to 7 levels
  67. of nesting (macro calling macro calling macro, etc.); It may be possible that stack-intensive
  68. applications in deeply nested macros could cause asterisk to crash earlier than this limit.
  69. It is advised that if you need to deeply nest macro calls, that you use the Gosub application
  70. (now allows arguments like a Macro) with explict Return() calls instead.</para></warning>
  71. <warning><para>Use of the application <literal>WaitExten</literal> within a macro will not function
  72. as expected. Please use the <literal>Read</literal> application in order to read DTMF from a channel
  73. currently executing a macro.</para></warning>
  74. </description>
  75. <see-also>
  76. <ref type="application">MacroExit</ref>
  77. <ref type="application">Goto</ref>
  78. <ref type="application">Gosub</ref>
  79. </see-also>
  80. </application>
  81. <application name="MacroIf" language="en_US">
  82. <synopsis>
  83. Conditional Macro implementation.
  84. </synopsis>
  85. <syntax argsep="?">
  86. <parameter name="expr" required="true" />
  87. <parameter name="destination" required="true" argsep=":">
  88. <argument name="macroiftrue" required="true">
  89. <argument name="macroiftrue" required="true" />
  90. <argument name="arg1" multiple="true" />
  91. </argument>
  92. <argument name="macroiffalse">
  93. <argument name="macroiffalse" required="true" />
  94. <argument name="arg1" multiple="true" />
  95. </argument>
  96. </parameter>
  97. </syntax>
  98. <description>
  99. <para>Executes macro defined in <replaceable>macroiftrue</replaceable> if
  100. <replaceable>expr</replaceable> is true (otherwise <replaceable>macroiffalse</replaceable>
  101. if provided)</para>
  102. <para>Arguments and return values as in application Macro()</para>
  103. <xi:include xpointer="xpointer(/docs/application[@name='Macro']/description/warning[2])" />
  104. </description>
  105. <see-also>
  106. <ref type="application">GotoIf</ref>
  107. <ref type="application">GosubIf</ref>
  108. <ref type="function">IF</ref>
  109. </see-also>
  110. </application>
  111. <application name="MacroExclusive" language="en_US">
  112. <synopsis>
  113. Exclusive Macro Implementation.
  114. </synopsis>
  115. <syntax>
  116. <parameter name="name" required="true">
  117. <para>The name of the macro</para>
  118. </parameter>
  119. <parameter name="arg1" />
  120. <parameter name="arg2" multiple="true" />
  121. </syntax>
  122. <description>
  123. <para>Executes macro defined in the context macro-<replaceable>name</replaceable>.
  124. Only one call at a time may run the macro. (we'll wait if another call is busy
  125. executing in the Macro)</para>
  126. <para>Arguments and return values as in application Macro()</para>
  127. <xi:include xpointer="xpointer(/docs/application[@name='Macro']/description/warning[2])" />
  128. </description>
  129. <see-also>
  130. <ref type="application">Macro</ref>
  131. </see-also>
  132. </application>
  133. <application name="MacroExit" language="en_US">
  134. <synopsis>
  135. Exit from Macro.
  136. </synopsis>
  137. <syntax />
  138. <description>
  139. <para>Causes the currently running macro to exit as if it had
  140. ended normally by running out of priorities to execute.
  141. If used outside a macro, will likely cause unexpected behavior.</para>
  142. </description>
  143. <see-also>
  144. <ref type="application">Macro</ref>
  145. </see-also>
  146. </application>
  147. ***/
  148. #define MAX_ARGS 80
  149. /* special result value used to force macro exit */
  150. #define MACRO_EXIT_RESULT 1024
  151. static char *app = "Macro";
  152. static char *if_app = "MacroIf";
  153. static char *exclusive_app = "MacroExclusive";
  154. static char *exit_app = "MacroExit";
  155. static void macro_fixup(void *data, struct ast_channel *old_chan, struct ast_channel *new_chan);
  156. static const struct ast_datastore_info macro_ds_info = {
  157. .type = "MACRO",
  158. .chan_fixup = macro_fixup,
  159. };
  160. static void macro_fixup(void *data, struct ast_channel *old_chan, struct ast_channel *new_chan)
  161. {
  162. int i;
  163. char varname[10];
  164. pbx_builtin_setvar_helper(new_chan, "MACRO_DEPTH", "0");
  165. pbx_builtin_setvar_helper(new_chan, "MACRO_CONTEXT", NULL);
  166. pbx_builtin_setvar_helper(new_chan, "MACRO_EXTEN", NULL);
  167. pbx_builtin_setvar_helper(new_chan, "MACRO_PRIORITY", NULL);
  168. pbx_builtin_setvar_helper(new_chan, "MACRO_OFFSET", NULL);
  169. for (i = 1; i < 100; i++) {
  170. snprintf(varname, sizeof(varname), "ARG%d", i);
  171. while (pbx_builtin_getvar_helper(new_chan, varname)) {
  172. /* Kill all levels of arguments */
  173. pbx_builtin_setvar_helper(new_chan, varname, NULL);
  174. }
  175. }
  176. }
  177. static struct ast_exten *find_matching_priority(struct ast_context *c, const char *exten, int priority, const char *callerid)
  178. {
  179. struct ast_exten *e;
  180. struct ast_include *i;
  181. struct ast_context *c2;
  182. for (e=ast_walk_context_extensions(c, NULL); e; e=ast_walk_context_extensions(c, e)) {
  183. if (ast_extension_match(ast_get_extension_name(e), exten)) {
  184. int needmatch = ast_get_extension_matchcid(e);
  185. if ((needmatch && ast_extension_match(ast_get_extension_cidmatch(e), callerid)) ||
  186. (!needmatch)) {
  187. /* This is the matching extension we want */
  188. struct ast_exten *p;
  189. for (p=ast_walk_extension_priorities(e, NULL); p; p=ast_walk_extension_priorities(e, p)) {
  190. if (priority != ast_get_extension_priority(p))
  191. continue;
  192. return p;
  193. }
  194. }
  195. }
  196. }
  197. /* No match; run through includes */
  198. for (i=ast_walk_context_includes(c, NULL); i; i=ast_walk_context_includes(c, i)) {
  199. for (c2=ast_walk_contexts(NULL); c2; c2=ast_walk_contexts(c2)) {
  200. if (!strcmp(ast_get_context_name(c2), ast_get_include_name(i))) {
  201. e = find_matching_priority(c2, exten, priority, callerid);
  202. if (e)
  203. return e;
  204. }
  205. }
  206. }
  207. return NULL;
  208. }
  209. static int _macro_exec(struct ast_channel *chan, const char *data, int exclusive)
  210. {
  211. const char *s;
  212. char *tmp;
  213. char *cur, *rest;
  214. char *macro;
  215. char fullmacro[80];
  216. char varname[80];
  217. char runningapp[80], runningdata[1024];
  218. char *oldargs[MAX_ARGS + 1] = { NULL, };
  219. int argc, x;
  220. int res=0;
  221. char oldexten[256]="";
  222. int oldpriority, gosub_level = 0;
  223. char pc[80], depthc[12];
  224. char oldcontext[AST_MAX_CONTEXT] = "";
  225. const char *inhangupc;
  226. int offset, depth = 0, maxdepth = 7;
  227. int setmacrocontext=0;
  228. int autoloopflag, inhangup = 0;
  229. struct ast_str *tmp_subst = NULL;
  230. char *save_macro_exten;
  231. char *save_macro_context;
  232. char *save_macro_priority;
  233. char *save_macro_offset;
  234. int save_in_subroutine;
  235. struct ast_datastore *macro_store = ast_channel_datastore_find(chan, &macro_ds_info, NULL);
  236. if (ast_strlen_zero(data)) {
  237. ast_log(LOG_WARNING, "Macro() requires arguments. See \"core show application macro\" for help.\n");
  238. return -1;
  239. }
  240. do {
  241. if (macro_store) {
  242. break;
  243. }
  244. if (!(macro_store = ast_datastore_alloc(&macro_ds_info, NULL))) {
  245. ast_log(LOG_WARNING, "Unable to allocate new datastore.\n");
  246. break;
  247. }
  248. /* Just the existence of this datastore is enough. */
  249. macro_store->inheritance = DATASTORE_INHERIT_FOREVER;
  250. ast_channel_datastore_add(chan, macro_store);
  251. } while (0);
  252. /* does the user want a deeper rabbit hole? */
  253. ast_channel_lock(chan);
  254. if ((s = pbx_builtin_getvar_helper(chan, "MACRO_RECURSION"))) {
  255. sscanf(s, "%30d", &maxdepth);
  256. }
  257. /* Count how many levels deep the rabbit hole goes */
  258. if ((s = pbx_builtin_getvar_helper(chan, "MACRO_DEPTH"))) {
  259. sscanf(s, "%30d", &depth);
  260. }
  261. /* Used for detecting whether to return when a Macro is called from another Macro after hangup */
  262. if (strcmp(ast_channel_exten(chan), "h") == 0)
  263. pbx_builtin_setvar_helper(chan, "MACRO_IN_HANGUP", "1");
  264. if ((inhangupc = pbx_builtin_getvar_helper(chan, "MACRO_IN_HANGUP"))) {
  265. sscanf(inhangupc, "%30d", &inhangup);
  266. }
  267. ast_channel_unlock(chan);
  268. if (depth >= maxdepth) {
  269. ast_log(LOG_ERROR, "Macro(): possible infinite loop detected. Returning early.\n");
  270. return 0;
  271. }
  272. snprintf(depthc, sizeof(depthc), "%d", depth + 1);
  273. tmp = ast_strdupa(data);
  274. rest = tmp;
  275. macro = strsep(&rest, ",");
  276. if (ast_strlen_zero(macro)) {
  277. ast_log(LOG_WARNING, "Invalid macro name specified\n");
  278. return 0;
  279. }
  280. snprintf(fullmacro, sizeof(fullmacro), "macro-%s", macro);
  281. if (!ast_exists_extension(chan, fullmacro, "s", 1,
  282. S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL))) {
  283. if (!ast_context_find(fullmacro))
  284. ast_log(LOG_WARNING, "No such context '%s' for macro '%s'. Was called by %s@%s\n", fullmacro, macro, ast_channel_exten(chan), ast_channel_context(chan));
  285. else
  286. ast_log(LOG_WARNING, "Context '%s' for macro '%s' lacks 's' extension, priority 1\n", fullmacro, macro);
  287. return 0;
  288. }
  289. /* If we are to run the macro exclusively, take the mutex */
  290. if (exclusive) {
  291. ast_debug(1, "Locking macrolock for '%s'\n", fullmacro);
  292. ast_autoservice_start(chan);
  293. if (ast_context_lockmacro(fullmacro)) {
  294. ast_log(LOG_WARNING, "Failed to lock macro '%s' as in-use\n", fullmacro);
  295. ast_autoservice_stop(chan);
  296. return 0;
  297. }
  298. ast_autoservice_stop(chan);
  299. }
  300. if (!(tmp_subst = ast_str_create(16))) {
  301. return -1;
  302. }
  303. /* Save old info */
  304. ast_channel_lock(chan);
  305. oldpriority = ast_channel_priority(chan);
  306. ast_copy_string(oldexten, ast_channel_exten(chan), sizeof(oldexten));
  307. ast_copy_string(oldcontext, ast_channel_context(chan), sizeof(oldcontext));
  308. if (ast_strlen_zero(ast_channel_macrocontext(chan))) {
  309. ast_channel_macrocontext_set(chan, ast_channel_context(chan));
  310. ast_channel_macroexten_set(chan, ast_channel_exten(chan));
  311. ast_channel_macropriority_set(chan, ast_channel_priority(chan));
  312. setmacrocontext=1;
  313. }
  314. argc = 1;
  315. /* Save old macro variables */
  316. save_macro_exten = ast_strdup(pbx_builtin_getvar_helper(chan, "MACRO_EXTEN"));
  317. pbx_builtin_setvar_helper(chan, "MACRO_EXTEN", oldexten);
  318. save_macro_context = ast_strdup(pbx_builtin_getvar_helper(chan, "MACRO_CONTEXT"));
  319. pbx_builtin_setvar_helper(chan, "MACRO_CONTEXT", oldcontext);
  320. save_macro_priority = ast_strdup(pbx_builtin_getvar_helper(chan, "MACRO_PRIORITY"));
  321. snprintf(pc, sizeof(pc), "%d", oldpriority);
  322. pbx_builtin_setvar_helper(chan, "MACRO_PRIORITY", pc);
  323. save_macro_offset = ast_strdup(pbx_builtin_getvar_helper(chan, "MACRO_OFFSET"));
  324. pbx_builtin_setvar_helper(chan, "MACRO_OFFSET", NULL);
  325. pbx_builtin_setvar_helper(chan, "MACRO_DEPTH", depthc);
  326. save_in_subroutine = ast_test_flag(ast_channel_flags(chan), AST_FLAG_SUBROUTINE_EXEC);
  327. ast_set_flag(ast_channel_flags(chan), AST_FLAG_SUBROUTINE_EXEC);
  328. /* Setup environment for new run */
  329. ast_channel_exten_set(chan, "s");
  330. ast_channel_context_set(chan, fullmacro);
  331. ast_channel_priority_set(chan, 1);
  332. while((cur = strsep(&rest, ",")) && (argc < MAX_ARGS)) {
  333. const char *argp;
  334. /* Save copy of old arguments if we're overwriting some, otherwise
  335. let them pass through to the other macro */
  336. snprintf(varname, sizeof(varname), "ARG%d", argc);
  337. if ((argp = pbx_builtin_getvar_helper(chan, varname))) {
  338. oldargs[argc] = ast_strdup(argp);
  339. }
  340. pbx_builtin_setvar_helper(chan, varname, cur);
  341. argc++;
  342. }
  343. autoloopflag = ast_test_flag(ast_channel_flags(chan), AST_FLAG_IN_AUTOLOOP);
  344. ast_set_flag(ast_channel_flags(chan), AST_FLAG_IN_AUTOLOOP);
  345. ast_channel_unlock(chan);
  346. while (ast_exists_extension(chan, ast_channel_context(chan), ast_channel_exten(chan), ast_channel_priority(chan),
  347. S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL))) {
  348. struct ast_context *c;
  349. struct ast_exten *e;
  350. int foundx;
  351. runningapp[0] = '\0';
  352. runningdata[0] = '\0';
  353. /* What application will execute? */
  354. if (ast_rdlock_contexts()) {
  355. ast_log(LOG_WARNING, "Failed to lock contexts list\n");
  356. } else {
  357. for (c = ast_walk_contexts(NULL), e = NULL; c; c = ast_walk_contexts(c)) {
  358. if (!strcmp(ast_get_context_name(c), ast_channel_context(chan))) {
  359. if (ast_rdlock_context(c)) {
  360. ast_log(LOG_WARNING, "Unable to lock context?\n");
  361. } else {
  362. e = find_matching_priority(c, ast_channel_exten(chan), ast_channel_priority(chan),
  363. S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL));
  364. if (e) { /* This will only be undefined for pbx_realtime, which is majorly broken. */
  365. ast_copy_string(runningapp, ast_get_extension_app(e), sizeof(runningapp));
  366. ast_copy_string(runningdata, ast_get_extension_app_data(e), sizeof(runningdata));
  367. }
  368. ast_unlock_context(c);
  369. }
  370. break;
  371. }
  372. }
  373. }
  374. ast_unlock_contexts();
  375. /* Reset the macro depth, if it was changed in the last iteration */
  376. pbx_builtin_setvar_helper(chan, "MACRO_DEPTH", depthc);
  377. res = ast_spawn_extension(chan, ast_channel_context(chan), ast_channel_exten(chan), ast_channel_priority(chan),
  378. S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL),
  379. &foundx, 1);
  380. if (res) {
  381. /* Something bad happened, or a hangup has been requested. */
  382. if (((res >= '0') && (res <= '9')) || ((res >= 'A') && (res <= 'F')) ||
  383. (res == '*') || (res == '#')) {
  384. /* Just return result as to the previous application as if it had been dialed */
  385. ast_debug(1, "Oooh, got something to jump out with ('%c')!\n", res);
  386. break;
  387. }
  388. switch(res) {
  389. case MACRO_EXIT_RESULT:
  390. res = 0;
  391. goto out;
  392. default:
  393. ast_debug(2, "Spawn extension (%s,%s,%d) exited non-zero on '%s' in macro '%s'\n", ast_channel_context(chan), ast_channel_exten(chan), ast_channel_priority(chan), ast_channel_name(chan), macro);
  394. ast_verb(2, "Spawn extension (%s, %s, %d) exited non-zero on '%s' in macro '%s'\n", ast_channel_context(chan), ast_channel_exten(chan), ast_channel_priority(chan), ast_channel_name(chan), macro);
  395. goto out;
  396. }
  397. }
  398. ast_debug(1, "Executed application: %s\n", runningapp);
  399. if (!strcasecmp(runningapp, "GOSUB")) {
  400. gosub_level++;
  401. ast_debug(1, "Incrementing gosub_level\n");
  402. } else if (!strcasecmp(runningapp, "GOSUBIF")) {
  403. char *cond, *app_arg;
  404. char *app2;
  405. ast_str_substitute_variables(&tmp_subst, 0, chan, runningdata);
  406. app2 = ast_str_buffer(tmp_subst);
  407. cond = strsep(&app2, "?");
  408. app_arg = strsep(&app2, ":");
  409. if (pbx_checkcondition(cond)) {
  410. if (!ast_strlen_zero(app_arg)) {
  411. gosub_level++;
  412. ast_debug(1, "Incrementing gosub_level\n");
  413. }
  414. } else {
  415. if (!ast_strlen_zero(app2)) {
  416. gosub_level++;
  417. ast_debug(1, "Incrementing gosub_level\n");
  418. }
  419. }
  420. } else if (!strcasecmp(runningapp, "RETURN")) {
  421. gosub_level--;
  422. ast_debug(1, "Decrementing gosub_level\n");
  423. } else if (!strcasecmp(runningapp, "STACKPOP")) {
  424. gosub_level--;
  425. ast_debug(1, "Decrementing gosub_level\n");
  426. } else if (!strncasecmp(runningapp, "EXEC", 4)) {
  427. /* Must evaluate args to find actual app */
  428. char *tmp2, *tmp3 = NULL;
  429. ast_str_substitute_variables(&tmp_subst, 0, chan, runningdata);
  430. tmp2 = ast_str_buffer(tmp_subst);
  431. if (!strcasecmp(runningapp, "EXECIF")) {
  432. if ((tmp3 = strchr(tmp2, '|'))) {
  433. *tmp3++ = '\0';
  434. }
  435. if (!pbx_checkcondition(tmp2)) {
  436. tmp3 = NULL;
  437. }
  438. } else {
  439. tmp3 = tmp2;
  440. }
  441. if (tmp3) {
  442. ast_debug(1, "Last app: %s\n", tmp3);
  443. }
  444. if (tmp3 && !strncasecmp(tmp3, "GOSUB", 5)) {
  445. gosub_level++;
  446. ast_debug(1, "Incrementing gosub_level\n");
  447. } else if (tmp3 && !strncasecmp(tmp3, "RETURN", 6)) {
  448. gosub_level--;
  449. ast_debug(1, "Decrementing gosub_level\n");
  450. } else if (tmp3 && !strncasecmp(tmp3, "STACKPOP", 8)) {
  451. gosub_level--;
  452. ast_debug(1, "Decrementing gosub_level\n");
  453. }
  454. }
  455. if (gosub_level == 0 && strcasecmp(ast_channel_context(chan), fullmacro)) {
  456. ast_verb(2, "Channel '%s' jumping out of macro '%s'\n", ast_channel_name(chan), macro);
  457. break;
  458. }
  459. /* don't stop executing extensions when we're in "h" */
  460. if (ast_check_hangup(chan) && !inhangup) {
  461. ast_debug(1, "Extension %s, macroexten %s, priority %d returned normally even though call was hung up\n",
  462. ast_channel_exten(chan),
  463. ast_channel_macroexten(chan),
  464. ast_channel_priority(chan));
  465. goto out;
  466. }
  467. ast_channel_priority_set(chan, ast_channel_priority(chan) + 1);
  468. }
  469. out:
  470. /* Don't let the channel change now. */
  471. ast_channel_lock(chan);
  472. /* Reset the depth back to what it was when the routine was entered (like if we called Macro recursively) */
  473. snprintf(depthc, sizeof(depthc), "%d", depth);
  474. pbx_builtin_setvar_helper(chan, "MACRO_DEPTH", depthc);
  475. ast_set2_flag(ast_channel_flags(chan), autoloopflag, AST_FLAG_IN_AUTOLOOP);
  476. ast_set2_flag(ast_channel_flags(chan), save_in_subroutine, AST_FLAG_SUBROUTINE_EXEC);
  477. for (x = 1; x < argc; x++) {
  478. /* Restore old arguments and delete ours */
  479. snprintf(varname, sizeof(varname), "ARG%d", x);
  480. pbx_builtin_setvar_helper(chan, varname, oldargs[x]);
  481. ast_free(oldargs[x]);
  482. }
  483. /* Restore macro variables */
  484. pbx_builtin_setvar_helper(chan, "MACRO_EXTEN", save_macro_exten);
  485. pbx_builtin_setvar_helper(chan, "MACRO_CONTEXT", save_macro_context);
  486. pbx_builtin_setvar_helper(chan, "MACRO_PRIORITY", save_macro_priority);
  487. ast_free(save_macro_exten);
  488. ast_free(save_macro_context);
  489. ast_free(save_macro_priority);
  490. if (setmacrocontext) {
  491. ast_channel_macrocontext_set(chan, "");
  492. ast_channel_macroexten_set(chan, "");
  493. ast_channel_macropriority_set(chan, 0);
  494. }
  495. if (!strcasecmp(ast_channel_context(chan), fullmacro)
  496. && !(ast_channel_softhangup_internal_flag(chan) & AST_SOFTHANGUP_ASYNCGOTO)) {
  497. const char *offsets;
  498. /* If we're leaving the macro normally, restore original information */
  499. ast_channel_priority_set(chan, oldpriority);
  500. ast_channel_context_set(chan, oldcontext);
  501. ast_channel_exten_set(chan, oldexten);
  502. if ((offsets = pbx_builtin_getvar_helper(chan, "MACRO_OFFSET"))) {
  503. /* Handle macro offset if it's set by checking the availability of step n + offset + 1, otherwise continue
  504. normally if there is any problem */
  505. if (sscanf(offsets, "%30d", &offset) == 1) {
  506. if (ast_exists_extension(chan, ast_channel_context(chan), ast_channel_exten(chan),
  507. ast_channel_priority(chan) + offset + 1,
  508. S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL))) {
  509. ast_channel_priority_set(chan, ast_channel_priority(chan) + offset);
  510. }
  511. }
  512. }
  513. }
  514. pbx_builtin_setvar_helper(chan, "MACRO_OFFSET", save_macro_offset);
  515. ast_free(save_macro_offset);
  516. /* Unlock the macro */
  517. if (exclusive) {
  518. ast_debug(1, "Unlocking macrolock for '%s'\n", fullmacro);
  519. if (ast_context_unlockmacro(fullmacro)) {
  520. ast_log(LOG_ERROR, "Failed to unlock macro '%s' - that isn't good\n", fullmacro);
  521. res = 0;
  522. }
  523. }
  524. ast_channel_unlock(chan);
  525. ast_free(tmp_subst);
  526. return res;
  527. }
  528. static int macro_exec(struct ast_channel *chan, const char *data)
  529. {
  530. return _macro_exec(chan, data, 0);
  531. }
  532. static int macroexclusive_exec(struct ast_channel *chan, const char *data)
  533. {
  534. return _macro_exec(chan, data, 1);
  535. }
  536. static int macroif_exec(struct ast_channel *chan, const char *data)
  537. {
  538. char *expr = NULL, *label_a = NULL, *label_b = NULL;
  539. int res = 0;
  540. expr = ast_strdupa(data);
  541. if ((label_a = strchr(expr, '?'))) {
  542. *label_a = '\0';
  543. label_a++;
  544. if ((label_b = strchr(label_a, ':'))) {
  545. *label_b = '\0';
  546. label_b++;
  547. }
  548. if (pbx_checkcondition(expr))
  549. res = macro_exec(chan, label_a);
  550. else if (label_b)
  551. res = macro_exec(chan, label_b);
  552. } else
  553. ast_log(LOG_WARNING, "Invalid Syntax.\n");
  554. return res;
  555. }
  556. static int macro_exit_exec(struct ast_channel *chan, const char *data)
  557. {
  558. return MACRO_EXIT_RESULT;
  559. }
  560. static int unload_module(void)
  561. {
  562. int res;
  563. res = ast_unregister_application(if_app);
  564. res |= ast_unregister_application(exit_app);
  565. res |= ast_unregister_application(app);
  566. res |= ast_unregister_application(exclusive_app);
  567. return res;
  568. }
  569. static int load_module(void)
  570. {
  571. int res;
  572. res = ast_register_application_xml(exit_app, macro_exit_exec);
  573. res |= ast_register_application_xml(if_app, macroif_exec);
  574. res |= ast_register_application_xml(exclusive_app, macroexclusive_exec);
  575. res |= ast_register_application_xml(app, macro_exec);
  576. return res;
  577. }
  578. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Extension Macros");