app_macro.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. * \ingroup applications
  23. */
  24. #include <stdlib.h>
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <unistd.h>
  28. #include <sys/types.h>
  29. #include "asterisk.h"
  30. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  31. #include "asterisk/file.h"
  32. #include "asterisk/logger.h"
  33. #include "asterisk/channel.h"
  34. #include "asterisk/pbx.h"
  35. #include "asterisk/module.h"
  36. #include "asterisk/options.h"
  37. #include "asterisk/config.h"
  38. #include "asterisk/utils.h"
  39. #include "asterisk/lock.h"
  40. #define MAX_ARGS 80
  41. /* special result value used to force macro exit */
  42. #define MACRO_EXIT_RESULT 1024
  43. static char *tdesc = "Extension Macros";
  44. static char *descrip =
  45. " Macro(macroname|arg1|arg2...): Executes a macro using the context\n"
  46. "'macro-<macroname>', jumping to the 's' extension of that context and\n"
  47. "executing each step, then returning when the steps end. \n"
  48. "The calling extension, context, and priority are stored in ${MACRO_EXTEN}, \n"
  49. "${MACRO_CONTEXT} and ${MACRO_PRIORITY} respectively. Arguments become\n"
  50. "${ARG1}, ${ARG2}, etc in the macro context.\n"
  51. "If you Goto out of the Macro context, the Macro will terminate and control\n"
  52. "will be returned at the location of the Goto.\n"
  53. "If ${MACRO_OFFSET} is set at termination, Macro will attempt to continue\n"
  54. "at priority MACRO_OFFSET + N + 1 if such a step exists, and N + 1 otherwise.\n";
  55. static char *if_descrip =
  56. " MacroIf(<expr>?macroname_a[|arg1][:macroname_b[|arg1]])\n"
  57. "Executes macro defined in <macroname_a> if <expr> is true\n"
  58. "(otherwise <macroname_b> if provided)\n"
  59. "Arguments and return values as in application macro()\n";
  60. static char *exit_descrip =
  61. " MacroExit():\n"
  62. "Causes the currently running macro to exit as if it had\n"
  63. "ended normally by running out of priorities to execute.\n"
  64. "If used outside a macro, will likely cause unexpected\n"
  65. "behavior.\n";
  66. static char *app = "Macro";
  67. static char *if_app = "MacroIf";
  68. static char *exit_app = "MacroExit";
  69. static char *synopsis = "Macro Implementation";
  70. static char *if_synopsis = "Conditional Macro Implementation";
  71. static char *exit_synopsis = "Exit From Macro";
  72. STANDARD_LOCAL_USER;
  73. LOCAL_USER_DECL;
  74. static int macro_exec(struct ast_channel *chan, void *data)
  75. {
  76. char *tmp;
  77. char *cur, *rest;
  78. char *macro;
  79. char fullmacro[80];
  80. char varname[80];
  81. char *oldargs[MAX_ARGS + 1] = { NULL, };
  82. int argc, x;
  83. int res=0;
  84. char oldexten[256]="";
  85. int oldpriority;
  86. char pc[80], depthc[12];
  87. char oldcontext[AST_MAX_CONTEXT] = "";
  88. char *offsets;
  89. int offset, depth;
  90. int setmacrocontext=0;
  91. int autoloopflag;
  92. char *save_macro_exten;
  93. char *save_macro_context;
  94. char *save_macro_priority;
  95. char *save_macro_offset;
  96. struct localuser *u;
  97. if (ast_strlen_zero(data)) {
  98. ast_log(LOG_WARNING, "Macro() requires arguments. See \"show application macro\" for help.\n");
  99. return -1;
  100. }
  101. LOCAL_USER_ADD(u);
  102. /* Count how many levels deep the rabbit hole goes */
  103. tmp = pbx_builtin_getvar_helper(chan, "MACRO_DEPTH");
  104. if (tmp) {
  105. sscanf(tmp, "%d", &depth);
  106. } else {
  107. depth = 0;
  108. }
  109. if (depth >= 7) {
  110. ast_log(LOG_ERROR, "Macro(): possible infinite loop detected. Returning early.\n");
  111. LOCAL_USER_REMOVE(u);
  112. return 0;
  113. }
  114. snprintf(depthc, sizeof(depthc), "%d", depth + 1);
  115. pbx_builtin_setvar_helper(chan, "MACRO_DEPTH", depthc);
  116. tmp = ast_strdupa(data);
  117. rest = tmp;
  118. macro = strsep(&rest, "|");
  119. if (ast_strlen_zero(macro)) {
  120. ast_log(LOG_WARNING, "Invalid macro name specified\n");
  121. LOCAL_USER_REMOVE(u);
  122. return 0;
  123. }
  124. snprintf(fullmacro, sizeof(fullmacro), "macro-%s", macro);
  125. if (!ast_exists_extension(chan, fullmacro, "s", 1, chan->cid.cid_num)) {
  126. if (!ast_context_find(fullmacro))
  127. ast_log(LOG_WARNING, "No such context '%s' for macro '%s'\n", fullmacro, macro);
  128. else
  129. ast_log(LOG_WARNING, "Context '%s' for macro '%s' lacks 's' extension, priority 1\n", fullmacro, macro);
  130. LOCAL_USER_REMOVE(u);
  131. return 0;
  132. }
  133. /* Save old info */
  134. oldpriority = chan->priority;
  135. ast_copy_string(oldexten, chan->exten, sizeof(oldexten));
  136. ast_copy_string(oldcontext, chan->context, sizeof(oldcontext));
  137. if (ast_strlen_zero(chan->macrocontext)) {
  138. ast_copy_string(chan->macrocontext, chan->context, sizeof(chan->macrocontext));
  139. ast_copy_string(chan->macroexten, chan->exten, sizeof(chan->macroexten));
  140. chan->macropriority = chan->priority;
  141. setmacrocontext=1;
  142. }
  143. argc = 1;
  144. /* Save old macro variables */
  145. save_macro_exten = pbx_builtin_getvar_helper(chan, "MACRO_EXTEN");
  146. if (save_macro_exten)
  147. save_macro_exten = strdup(save_macro_exten);
  148. pbx_builtin_setvar_helper(chan, "MACRO_EXTEN", oldexten);
  149. save_macro_context = pbx_builtin_getvar_helper(chan, "MACRO_CONTEXT");
  150. if (save_macro_context)
  151. save_macro_context = strdup(save_macro_context);
  152. pbx_builtin_setvar_helper(chan, "MACRO_CONTEXT", oldcontext);
  153. save_macro_priority = pbx_builtin_getvar_helper(chan, "MACRO_PRIORITY");
  154. if (save_macro_priority)
  155. save_macro_priority = strdup(save_macro_priority);
  156. snprintf(pc, sizeof(pc), "%d", oldpriority);
  157. pbx_builtin_setvar_helper(chan, "MACRO_PRIORITY", pc);
  158. save_macro_offset = pbx_builtin_getvar_helper(chan, "MACRO_OFFSET");
  159. if (save_macro_offset)
  160. save_macro_offset = strdup(save_macro_offset);
  161. pbx_builtin_setvar_helper(chan, "MACRO_OFFSET", NULL);
  162. /* Setup environment for new run */
  163. chan->exten[0] = 's';
  164. chan->exten[1] = '\0';
  165. ast_copy_string(chan->context, fullmacro, sizeof(chan->context));
  166. chan->priority = 1;
  167. while((cur = strsep(&rest, "|")) && (argc < MAX_ARGS)) {
  168. /* Save copy of old arguments if we're overwriting some, otherwise
  169. let them pass through to the other macro */
  170. snprintf(varname, sizeof(varname), "ARG%d", argc);
  171. oldargs[argc] = pbx_builtin_getvar_helper(chan, varname);
  172. if (oldargs[argc])
  173. oldargs[argc] = strdup(oldargs[argc]);
  174. pbx_builtin_setvar_helper(chan, varname, cur);
  175. argc++;
  176. }
  177. autoloopflag = ast_test_flag(chan, AST_FLAG_IN_AUTOLOOP);
  178. ast_set_flag(chan, AST_FLAG_IN_AUTOLOOP);
  179. while(ast_exists_extension(chan, chan->context, chan->exten, chan->priority, chan->cid.cid_num)) {
  180. /* Reset the macro depth, if it was changed in the last iteration */
  181. pbx_builtin_setvar_helper(chan, "MACRO_DEPTH", depthc);
  182. if ((res = ast_spawn_extension(chan, chan->context, chan->exten, chan->priority, chan->cid.cid_num))) {
  183. /* Something bad happened, or a hangup has been requested. */
  184. if (((res >= '0') && (res <= '9')) || ((res >= 'A') && (res <= 'F')) ||
  185. (res == '*') || (res == '#')) {
  186. /* Just return result as to the previous application as if it had been dialed */
  187. ast_log(LOG_DEBUG, "Oooh, got something to jump out with ('%c')!\n", res);
  188. break;
  189. }
  190. switch(res) {
  191. case MACRO_EXIT_RESULT:
  192. res = 0;
  193. goto out;
  194. case AST_PBX_KEEPALIVE:
  195. if (option_debug)
  196. ast_log(LOG_DEBUG, "Spawn extension (%s,%s,%d) exited KEEPALIVE in macro %s on '%s'\n", chan->context, chan->exten, chan->priority, macro, chan->name);
  197. else if (option_verbose > 1)
  198. ast_verbose( VERBOSE_PREFIX_2 "Spawn extension (%s, %s, %d) exited KEEPALIVE in macro '%s' on '%s'\n", chan->context, chan->exten, chan->priority, macro, chan->name);
  199. goto out;
  200. break;
  201. default:
  202. if (option_debug)
  203. ast_log(LOG_DEBUG, "Spawn extension (%s,%s,%d) exited non-zero on '%s' in macro '%s'\n", chan->context, chan->exten, chan->priority, chan->name, macro);
  204. else if (option_verbose > 1)
  205. ast_verbose( VERBOSE_PREFIX_2 "Spawn extension (%s, %s, %d) exited non-zero on '%s' in macro '%s'\n", chan->context, chan->exten, chan->priority, chan->name, macro);
  206. goto out;
  207. }
  208. }
  209. if (strcasecmp(chan->context, fullmacro)) {
  210. if (option_verbose > 1)
  211. ast_verbose(VERBOSE_PREFIX_2 "Channel '%s' jumping out of macro '%s'\n", chan->name, macro);
  212. break;
  213. }
  214. /* don't stop executing extensions when we're in "h" */
  215. if (chan->_softhangup && strcasecmp(oldexten,"h")) {
  216. ast_log(LOG_DEBUG, "Extension %s, priority %d returned normally even though call was hung up\n",
  217. chan->exten, chan->priority);
  218. goto out;
  219. }
  220. chan->priority++;
  221. }
  222. out:
  223. /* Reset the depth back to what it was when the routine was entered (like if we called Macro recursively) */
  224. snprintf(depthc, sizeof(depthc), "%d", depth);
  225. pbx_builtin_setvar_helper(chan, "MACRO_DEPTH", depthc);
  226. ast_set2_flag(chan, autoloopflag, AST_FLAG_IN_AUTOLOOP);
  227. for (x=1; x<argc; x++) {
  228. /* Restore old arguments and delete ours */
  229. snprintf(varname, sizeof(varname), "ARG%d", x);
  230. if (oldargs[x]) {
  231. pbx_builtin_setvar_helper(chan, varname, oldargs[x]);
  232. free(oldargs[x]);
  233. } else {
  234. pbx_builtin_setvar_helper(chan, varname, NULL);
  235. }
  236. }
  237. /* Restore macro variables */
  238. pbx_builtin_setvar_helper(chan, "MACRO_EXTEN", save_macro_exten);
  239. if (save_macro_exten)
  240. free(save_macro_exten);
  241. pbx_builtin_setvar_helper(chan, "MACRO_CONTEXT", save_macro_context);
  242. if (save_macro_context)
  243. free(save_macro_context);
  244. pbx_builtin_setvar_helper(chan, "MACRO_PRIORITY", save_macro_priority);
  245. if (save_macro_priority)
  246. free(save_macro_priority);
  247. if (setmacrocontext) {
  248. chan->macrocontext[0] = '\0';
  249. chan->macroexten[0] = '\0';
  250. chan->macropriority = 0;
  251. }
  252. if (!strcasecmp(chan->context, fullmacro)) {
  253. /* If we're leaving the macro normally, restore original information */
  254. chan->priority = oldpriority;
  255. ast_copy_string(chan->context, oldcontext, sizeof(chan->context));
  256. if (!(chan->_softhangup & AST_SOFTHANGUP_ASYNCGOTO)) {
  257. /* Copy the extension, so long as we're not in softhangup, where we could be given an asyncgoto */
  258. ast_copy_string(chan->exten, oldexten, sizeof(chan->exten));
  259. if ((offsets = pbx_builtin_getvar_helper(chan, "MACRO_OFFSET"))) {
  260. /* Handle macro offset if it's set by checking the availability of step n + offset + 1, otherwise continue
  261. normally if there is any problem */
  262. if (sscanf(offsets, "%d", &offset) == 1) {
  263. if (ast_exists_extension(chan, chan->context, chan->exten, chan->priority + offset + 1, chan->cid.cid_num)) {
  264. chan->priority += offset;
  265. }
  266. }
  267. }
  268. }
  269. }
  270. pbx_builtin_setvar_helper(chan, "MACRO_OFFSET", save_macro_offset);
  271. if (save_macro_offset)
  272. free(save_macro_offset);
  273. LOCAL_USER_REMOVE(u);
  274. return res;
  275. }
  276. static int macroif_exec(struct ast_channel *chan, void *data)
  277. {
  278. char *expr = NULL, *label_a = NULL, *label_b = NULL;
  279. int res = 0;
  280. struct localuser *u;
  281. LOCAL_USER_ADD(u);
  282. expr = ast_strdupa(data);
  283. if (!expr) {
  284. ast_log(LOG_ERROR, "Out of Memory!\n");
  285. LOCAL_USER_REMOVE(u);
  286. return -1;
  287. }
  288. if ((label_a = strchr(expr, '?'))) {
  289. *label_a = '\0';
  290. label_a++;
  291. if ((label_b = strchr(label_a, ':'))) {
  292. *label_b = '\0';
  293. label_b++;
  294. }
  295. if (ast_true(expr))
  296. macro_exec(chan, label_a);
  297. else if (label_b)
  298. macro_exec(chan, label_b);
  299. } else
  300. ast_log(LOG_WARNING, "Invalid Syntax.\n");
  301. LOCAL_USER_REMOVE(u);
  302. return res;
  303. }
  304. static int macro_exit_exec(struct ast_channel *chan, void *data)
  305. {
  306. return MACRO_EXIT_RESULT;
  307. }
  308. int unload_module(void)
  309. {
  310. int res;
  311. res = ast_unregister_application(if_app);
  312. res |= ast_unregister_application(exit_app);
  313. res |= ast_unregister_application(app);
  314. STANDARD_HANGUP_LOCALUSERS;
  315. return res;
  316. }
  317. int load_module(void)
  318. {
  319. int res;
  320. res = ast_register_application(exit_app, macro_exit_exec, exit_synopsis, exit_descrip);
  321. res |= ast_register_application(if_app, macroif_exec, if_synopsis, if_descrip);
  322. res |= ast_register_application(app, macro_exec, synopsis, descrip);
  323. return res;
  324. }
  325. char *description(void)
  326. {
  327. return tdesc;
  328. }
  329. int usecount(void)
  330. {
  331. int res;
  332. STANDARD_USECOUNT(res);
  333. return res;
  334. }
  335. char *key()
  336. {
  337. return ASTERISK_GPL_KEY;
  338. }