2 * Asterisk -- A telephony toolkit for Linux.
6 * Copyright (C) 2003, Digium
8 * Mark Spencer <markster@digium.com>
10 * This program is free software, distributed under the terms of
11 * the GNU General Public License
14 #include <sys/types.h>
15 #include <asterisk/file.h>
16 #include <asterisk/logger.h>
17 #include <asterisk/channel.h>
18 #include <asterisk/pbx.h>
19 #include <asterisk/module.h>
20 #include <asterisk/options.h>
30 static char *tdesc = "Extension Macros";
32 static char *descrip =
33 " Macro(macroname|arg1|arg2...): Executes a macro using the context\n"
34 "'macro-<macroname>', jumping to the 's' extension of that context and\n"
35 "executing each step, then returning when the steps end. The calling\n"
36 "extension, context, and priority are stored in ${MACRO_EXTEN}, \n"
37 "${MACRO_CONTEXT} and ${MACRO_PRIORITY} respectively. Arguments become\n"
38 "${ARG1}, ${ARG2}, etc in the macro context. Macro returns -1 if\n"
39 "any step in the macro returns -1, and 0 otherwise. If you Goto out\n"
40 "of the Macro context, the Macro will terminate and control will be return\n"
41 "at the location of the Goto. Otherwise if ${MACRO_OFFSET} is set at\n"
42 "termination, Macro will attempt to continue at priority\n"
43 "MACRO_OFFSET + N + 1 if such a step exists, and N + 1 otherwise.\n";
45 static char *app = "Macro";
47 static char *synopsis = "Macro Implementation";
53 static int macro_exec(struct ast_channel *chan, void *data)
60 char *oldargs[MAX_ARGS + 1] = { NULL, };
63 char oldexten[256]="";
66 char oldcontext[256] = "";
69 int setmacrocontext=0;
71 char *save_macro_exten;
72 char *save_macro_context;
73 char *save_macro_priority;
74 char *save_macro_offset;
76 if (!data || !strlen(data)) {
77 ast_log(LOG_WARNING, "Invalid Macro incantation\n");
80 strncpy(tmp, data, sizeof(tmp) - 1);
82 macro = strsep(&rest, "|");
83 if (!macro || !strlen(macro)) {
84 ast_log(LOG_WARNING, "Invalid macro name specified\n");
87 snprintf(fullmacro, sizeof(fullmacro), "macro-%s", macro);
88 if (!ast_exists_extension(chan, fullmacro, "s", 1, chan->callerid)) {
89 if (!ast_context_find(fullmacro))
90 ast_log(LOG_WARNING, "No such context '%s' for macro '%s'\n", fullmacro, macro);
92 ast_log(LOG_WARNING, "Context '%s' for macro '%s' lacks 's' extension, priority 1\n", fullmacro, macro);
96 oldpriority = chan->priority;
97 strncpy(oldexten, chan->exten, sizeof(oldexten) - 1);
98 strncpy(oldcontext, chan->context, sizeof(oldcontext) - 1);
99 if (!strlen(chan->macrocontext)) {
100 strncpy(chan->macrocontext, chan->context, sizeof(chan->macrocontext) - 1);
101 strncpy(chan->macroexten, chan->exten, sizeof(chan->macroexten) - 1);
102 chan->macropriority = chan->priority;
106 /* Save old macro variables */
107 save_macro_exten = pbx_builtin_getvar_helper(chan, "MACRO_EXTEN");
108 if (save_macro_exten) save_macro_exten = strdup(save_macro_exten);
109 pbx_builtin_setvar_helper(chan, "MACRO_EXTEN", oldexten);
111 save_macro_context = pbx_builtin_getvar_helper(chan, "MACRO_CONTEXT");
112 if (save_macro_context) save_macro_context = strdup(save_macro_context);
113 pbx_builtin_setvar_helper(chan, "MACRO_CONTEXT", oldcontext);
115 save_macro_priority = pbx_builtin_getvar_helper(chan, "MACRO_PRIORITY");
116 if (save_macro_priority) save_macro_priority = strdup(save_macro_priority);
117 snprintf(pc, sizeof(pc), "%d", oldpriority);
118 pbx_builtin_setvar_helper(chan, "MACRO_PRIORITY", pc);
120 save_macro_offset = pbx_builtin_getvar_helper(chan, "MACRO_OFFSET");
121 if (save_macro_offset) save_macro_offset = strdup(save_macro_offset);
122 pbx_builtin_setvar_helper(chan, "MACRO_OFFSET", NULL);
124 /* Setup environment for new run */
125 strcpy(chan->exten, "s");
126 strncpy(chan->context, fullmacro, sizeof(chan->context));
129 while((cur = strsep(&rest, "|")) && (argc < MAX_ARGS)) {
130 /* Save copy of old arguments if we're overwriting some, otherwise
131 let them pass through to the other macro */
132 snprintf(varname, sizeof(varname), "ARG%d", argc);
133 oldargs[argc] = pbx_builtin_getvar_helper(chan, varname);
135 oldargs[argc] = strdup(oldargs[argc]);
136 pbx_builtin_setvar_helper(chan, varname, cur);
139 while(ast_exists_extension(chan, chan->context, chan->exten, chan->priority, chan->callerid)) {
140 if ((res = ast_spawn_extension(chan, chan->context, chan->exten, chan->priority, chan->callerid))) {
141 /* Something bad happened, or a hangup has been requested. */
142 if (((res >= '0') && (res <= '9')) || ((res >= 'A') && (res <= 'F'))) {
143 /* Just return result as to the previous application as if it had been dialed */
144 ast_log(LOG_DEBUG, "Oooh, got something to jump out with ('%c')!\n", res);
148 case AST_PBX_KEEPALIVE:
150 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);
151 else if (option_verbose > 1)
152 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);
157 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);
158 else if (option_verbose > 1)
159 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);
163 if (strcasecmp(chan->context, fullmacro)) {
164 if (option_verbose > 1)
165 ast_verbose(VERBOSE_PREFIX_2 "Channel '%s' jumping out of macro '%s'\n", chan->name, macro);
168 /* don't stop executing extensions when we're in "h" */
169 if (chan->_softhangup && strcasecmp(oldexten,"h")) {
170 ast_log(LOG_DEBUG, "Extension %s, priority %d returned normally even though call was hung up\n",
171 chan->exten, chan->priority);
177 for (x=1;x<argc;x++) {
178 /* Restore old arguments and delete ours */
179 snprintf(varname, sizeof(varname), "ARG%d", x);
181 pbx_builtin_setvar_helper(chan, varname, oldargs[x]);
184 pbx_builtin_setvar_helper(chan, varname, NULL);
188 /* Restore macro variables */
189 pbx_builtin_setvar_helper(chan, "MACRO_EXTEN", save_macro_exten);
190 if (save_macro_exten) free(save_macro_exten);
191 pbx_builtin_setvar_helper(chan, "MACRO_CONTEXT", save_macro_context);
192 if (save_macro_context) free(save_macro_context);
193 pbx_builtin_setvar_helper(chan, "MACRO_PRIORITY", save_macro_priority);
194 if (save_macro_priority) free(save_macro_priority);
195 if (setmacrocontext) {
196 strcpy(chan->macrocontext, "");
197 strcpy(chan->macroexten, "");
198 chan->macropriority = 0;
201 if (!strcasecmp(chan->context, fullmacro)) {
202 /* If we're leaving the macro normally, restore original information */
203 chan->priority = oldpriority;
204 strncpy(chan->context, oldcontext, sizeof(chan->context) - 1);
205 if (!(chan->_softhangup & AST_SOFTHANGUP_ASYNCGOTO)) {
206 /* Copy the extension, so long as we're not in softhangup, where we could be given an asyncgoto */
207 strncpy(chan->exten, oldexten, sizeof(chan->exten) - 1);
208 if ((offsets = pbx_builtin_getvar_helper(chan, "MACRO_OFFSET"))) {
209 /* Handle macro offset if it's set by checking the availability of step n + offset + 1, otherwise continue
210 normally if there is any problem */
211 if (sscanf(offsets, "%d", &offset) == 1) {
212 if (ast_exists_extension(chan, chan->context, chan->exten, chan->priority + offset + 1, chan->callerid)) {
213 chan->priority += offset;
220 pbx_builtin_setvar_helper(chan, "MACRO_OFFSET", save_macro_offset);
221 if (save_macro_offset) free(save_macro_offset);
225 int unload_module(void)
227 STANDARD_HANGUP_LOCALUSERS;
228 return ast_unregister_application(app);
231 int load_module(void)
233 return ast_register_application(app, macro_exec, synopsis, descrip);
236 char *description(void)
244 STANDARD_USECOUNT(res);
250 return ASTERISK_GPL_KEY;