app_macro.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. * Asterisk -- A telephony toolkit for Linux.
  3. *
  4. * Macro Implementation
  5. *
  6. * Copyright (C) 2003, Digium
  7. *
  8. * Mark Spencer <markster@digium.com>
  9. *
  10. * This program is free software, distributed under the terms of
  11. * the GNU General Public License
  12. */
  13. #include <sys/types.h>
  14. #include <asterisk/file.h>
  15. #include <asterisk/logger.h>
  16. #include <asterisk/channel.h>
  17. #include <asterisk/pbx.h>
  18. #include <asterisk/module.h>
  19. #include <asterisk/options.h>
  20. #include <asterisk/utils.h>
  21. #include <asterisk/lock.h>
  22. #include <stdlib.h>
  23. #include <unistd.h>
  24. #include <string.h>
  25. #include <stdlib.h>
  26. #define MAX_ARGS 80
  27. static char *tdesc = "Extension Macros";
  28. static char *descrip =
  29. " Macro(macroname|arg1|arg2...): Executes a macro using the context\n"
  30. "'macro-<macroname>', jumping to the 's' extension of that context and\n"
  31. "executing each step, then returning when the steps end. The calling\n"
  32. "extension, context, and priority are stored in ${MACRO_EXTEN}, \n"
  33. "${MACRO_CONTEXT} and ${MACRO_PRIORITY} respectively. Arguments become\n"
  34. "${ARG1}, ${ARG2}, etc in the macro context. Macro returns -1 if\n"
  35. "any step in the macro returns -1, and 0 otherwise. If you Goto out\n"
  36. "of the Macro context, the Macro will terminate and control will be return\n"
  37. "at the location of the Goto. Otherwise if ${MACRO_OFFSET} is set at\n"
  38. "termination, Macro will attempt to continue at priority\n"
  39. "MACRO_OFFSET + N + 1 if such a step exists, and N + 1 otherwise.\n";
  40. static char *app = "Macro";
  41. static char *synopsis = "Macro Implementation";
  42. STANDARD_LOCAL_USER;
  43. LOCAL_USER_DECL;
  44. static int macro_exec(struct ast_channel *chan, void *data)
  45. {
  46. char tmp[256] = "";
  47. char *cur, *rest;
  48. char *macro;
  49. char fullmacro[80];
  50. char varname[80];
  51. char *oldargs[MAX_ARGS + 1] = { NULL, };
  52. int argc, x;
  53. int res=0;
  54. char oldexten[256]="";
  55. int oldpriority;
  56. char pc[80];
  57. char oldcontext[256] = "";
  58. char *offsets;
  59. int offset;
  60. int setmacrocontext=0;
  61. char *save_macro_exten;
  62. char *save_macro_context;
  63. char *save_macro_priority;
  64. char *save_macro_offset;
  65. if (!data || ast_strlen_zero(data)) {
  66. ast_log(LOG_WARNING, "Invalid Macro incantation\n");
  67. return 0;
  68. }
  69. strncpy(tmp, data, sizeof(tmp) - 1);
  70. rest = tmp;
  71. macro = strsep(&rest, "|");
  72. if (!macro || ast_strlen_zero(macro)) {
  73. ast_log(LOG_WARNING, "Invalid macro name specified\n");
  74. return 0;
  75. }
  76. snprintf(fullmacro, sizeof(fullmacro), "macro-%s", macro);
  77. if (!ast_exists_extension(chan, fullmacro, "s", 1, chan->callerid)) {
  78. if (!ast_context_find(fullmacro))
  79. ast_log(LOG_WARNING, "No such context '%s' for macro '%s'\n", fullmacro, macro);
  80. else
  81. ast_log(LOG_WARNING, "Context '%s' for macro '%s' lacks 's' extension, priority 1\n", fullmacro, macro);
  82. return 0;
  83. }
  84. /* Save old info */
  85. oldpriority = chan->priority;
  86. strncpy(oldexten, chan->exten, sizeof(oldexten) - 1);
  87. strncpy(oldcontext, chan->context, sizeof(oldcontext) - 1);
  88. if (ast_strlen_zero(chan->macrocontext)) {
  89. strncpy(chan->macrocontext, chan->context, sizeof(chan->macrocontext) - 1);
  90. strncpy(chan->macroexten, chan->exten, sizeof(chan->macroexten) - 1);
  91. chan->macropriority = chan->priority;
  92. setmacrocontext=1;
  93. }
  94. argc = 1;
  95. /* Save old macro variables */
  96. save_macro_exten = pbx_builtin_getvar_helper(chan, "MACRO_EXTEN");
  97. if (save_macro_exten) save_macro_exten = strdup(save_macro_exten);
  98. pbx_builtin_setvar_helper(chan, "MACRO_EXTEN", oldexten);
  99. save_macro_context = pbx_builtin_getvar_helper(chan, "MACRO_CONTEXT");
  100. if (save_macro_context) save_macro_context = strdup(save_macro_context);
  101. pbx_builtin_setvar_helper(chan, "MACRO_CONTEXT", oldcontext);
  102. save_macro_priority = pbx_builtin_getvar_helper(chan, "MACRO_PRIORITY");
  103. if (save_macro_priority) save_macro_priority = strdup(save_macro_priority);
  104. snprintf(pc, sizeof(pc), "%d", oldpriority);
  105. pbx_builtin_setvar_helper(chan, "MACRO_PRIORITY", pc);
  106. save_macro_offset = pbx_builtin_getvar_helper(chan, "MACRO_OFFSET");
  107. if (save_macro_offset) save_macro_offset = strdup(save_macro_offset);
  108. pbx_builtin_setvar_helper(chan, "MACRO_OFFSET", NULL);
  109. /* Setup environment for new run */
  110. chan->exten[0] = 's';
  111. chan->exten[1] = '\0';
  112. strncpy(chan->context, fullmacro, sizeof(chan->context) - 1);
  113. chan->priority = 1;
  114. while((cur = strsep(&rest, "|")) && (argc < MAX_ARGS)) {
  115. /* Save copy of old arguments if we're overwriting some, otherwise
  116. let them pass through to the other macro */
  117. snprintf(varname, sizeof(varname), "ARG%d", argc);
  118. oldargs[argc] = pbx_builtin_getvar_helper(chan, varname);
  119. if (oldargs[argc])
  120. oldargs[argc] = strdup(oldargs[argc]);
  121. pbx_builtin_setvar_helper(chan, varname, cur);
  122. argc++;
  123. }
  124. while(ast_exists_extension(chan, chan->context, chan->exten, chan->priority, chan->callerid)) {
  125. if ((res = ast_spawn_extension(chan, chan->context, chan->exten, chan->priority, chan->callerid))) {
  126. /* Something bad happened, or a hangup has been requested. */
  127. if (((res >= '0') && (res <= '9')) || ((res >= 'A') && (res <= 'F')) ||
  128. (res == '*') || (res == '#')) {
  129. /* Just return result as to the previous application as if it had been dialed */
  130. ast_log(LOG_DEBUG, "Oooh, got something to jump out with ('%c')!\n", res);
  131. break;
  132. }
  133. switch(res) {
  134. case AST_PBX_KEEPALIVE:
  135. if (option_debug)
  136. 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);
  137. else if (option_verbose > 1)
  138. 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);
  139. goto out;
  140. break;
  141. default:
  142. if (option_debug)
  143. 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);
  144. else if (option_verbose > 1)
  145. 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);
  146. goto out;
  147. }
  148. }
  149. if (strcasecmp(chan->context, fullmacro)) {
  150. if (option_verbose > 1)
  151. ast_verbose(VERBOSE_PREFIX_2 "Channel '%s' jumping out of macro '%s'\n", chan->name, macro);
  152. break;
  153. }
  154. /* don't stop executing extensions when we're in "h" */
  155. if (chan->_softhangup && strcasecmp(oldexten,"h")) {
  156. ast_log(LOG_DEBUG, "Extension %s, priority %d returned normally even though call was hung up\n",
  157. chan->exten, chan->priority);
  158. goto out;
  159. }
  160. chan->priority++;
  161. }
  162. out:
  163. for (x=1;x<argc;x++) {
  164. /* Restore old arguments and delete ours */
  165. snprintf(varname, sizeof(varname), "ARG%d", x);
  166. if (oldargs[x]) {
  167. pbx_builtin_setvar_helper(chan, varname, oldargs[x]);
  168. free(oldargs[x]);
  169. } else {
  170. pbx_builtin_setvar_helper(chan, varname, NULL);
  171. }
  172. }
  173. /* Restore macro variables */
  174. pbx_builtin_setvar_helper(chan, "MACRO_EXTEN", save_macro_exten);
  175. if (save_macro_exten) free(save_macro_exten);
  176. pbx_builtin_setvar_helper(chan, "MACRO_CONTEXT", save_macro_context);
  177. if (save_macro_context) free(save_macro_context);
  178. pbx_builtin_setvar_helper(chan, "MACRO_PRIORITY", save_macro_priority);
  179. if (save_macro_priority) free(save_macro_priority);
  180. if (setmacrocontext) {
  181. chan->macrocontext[0] = '\0';
  182. chan->macroexten[0] = '\0';
  183. chan->macropriority = 0;
  184. }
  185. if (!strcasecmp(chan->context, fullmacro)) {
  186. /* If we're leaving the macro normally, restore original information */
  187. chan->priority = oldpriority;
  188. strncpy(chan->context, oldcontext, sizeof(chan->context) - 1);
  189. if (!(chan->_softhangup & AST_SOFTHANGUP_ASYNCGOTO)) {
  190. /* Copy the extension, so long as we're not in softhangup, where we could be given an asyncgoto */
  191. strncpy(chan->exten, oldexten, sizeof(chan->exten) - 1);
  192. if ((offsets = pbx_builtin_getvar_helper(chan, "MACRO_OFFSET"))) {
  193. /* Handle macro offset if it's set by checking the availability of step n + offset + 1, otherwise continue
  194. normally if there is any problem */
  195. if (sscanf(offsets, "%d", &offset) == 1) {
  196. if (ast_exists_extension(chan, chan->context, chan->exten, chan->priority + offset + 1, chan->callerid)) {
  197. chan->priority += offset;
  198. }
  199. }
  200. }
  201. }
  202. }
  203. pbx_builtin_setvar_helper(chan, "MACRO_OFFSET", save_macro_offset);
  204. if (save_macro_offset) free(save_macro_offset);
  205. return res;
  206. }
  207. int unload_module(void)
  208. {
  209. STANDARD_HANGUP_LOCALUSERS;
  210. return ast_unregister_application(app);
  211. }
  212. int load_module(void)
  213. {
  214. return ast_register_application(app, macro_exec, synopsis, descrip);
  215. }
  216. char *description(void)
  217. {
  218. return tdesc;
  219. }
  220. int usecount(void)
  221. {
  222. int res;
  223. STANDARD_USECOUNT(res);
  224. return res;
  225. }
  226. char *key()
  227. {
  228. return ASTERISK_GPL_KEY;
  229. }