Merge anthm's MacroIf patch (bug #2912)
[asterisk/asterisk.git] / apps / app_macro.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Macro Implementation
5  * 
6  * Copyright (C) 2003-2004, Digium, Inc.
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
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>
21 #include <asterisk/config.h>
22 #include <asterisk/utils.h>
23 #include <asterisk/lock.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <string.h>
27 #include <stdlib.h>
28
29 #define MAX_ARGS 80
30
31 static char *tdesc = "Extension Macros";
32
33 static char *descrip =
34 "  Macro(macroname|arg1|arg2...): Executes a macro using the context\n"
35 "'macro-<macroname>', jumping to the 's' extension of that context and\n"
36 "executing each step, then returning when the steps end.  The calling\n"
37 "extension, context, and priority are stored in ${MACRO_EXTEN}, \n"
38 "${MACRO_CONTEXT} and ${MACRO_PRIORITY} respectively.  Arguments become\n"
39 "${ARG1}, ${ARG2}, etc in the macro context.  Macro returns -1 if\n"
40 "any step in the macro returns -1, and 0 otherwise.  If you Goto out\n"
41 "of the Macro context, the Macro will terminate and control will be return\n"
42 "at the location of the Goto.  Otherwise if ${MACRO_OFFSET} is set at\n"
43 "termination, Macro will attempt to continue at priority\n"
44 "MACRO_OFFSET + N + 1 if such a step exists, and N + 1 otherwise.\n";
45
46 static char *if_descrip =
47 "  MacroIf(<expr>?label_a[|arg1][:label_b[|arg1]]):\n"
48 "Executes macro defined in <label_a> if <expr> is true\n"
49 "(otherwise <label_b> if provided)\n";
50
51 static char *app = "Macro";
52 static char *if_app = "MacroIf";
53
54 static char *synopsis = "Macro Implementation";
55 static char *if_synopsis = "Conditional Macro Implementation";
56
57 STANDARD_LOCAL_USER;
58
59 LOCAL_USER_DECL;
60
61 static int macro_exec(struct ast_channel *chan, void *data)
62 {
63   char *tmp;
64   char *cur, *rest;
65   char *macro;
66   char fullmacro[80];
67   char varname[80];
68   char *oldargs[MAX_ARGS + 1] = { NULL, };
69   int argc, x;
70   int res=0;
71   char oldexten[256]="";
72   int oldpriority;
73   char pc[80];
74   char oldcontext[256] = "";
75   char *offsets;
76   int offset;
77   int setmacrocontext=0;
78   
79   char *save_macro_exten;
80   char *save_macro_context;
81   char *save_macro_priority;
82   char *save_macro_offset;
83   struct localuser *u;
84   
85   if (!data || ast_strlen_zero(data)) {
86     ast_log(LOG_WARNING, "Invalid Macro incantation\n");
87     return 0;
88   }
89
90   tmp = ast_strdupa((char *) data);
91   rest = tmp;
92   macro = strsep(&rest, "|");
93   if (!macro || ast_strlen_zero(macro)) {
94         ast_log(LOG_WARNING, "Invalid macro name specified\n");
95         return 0;
96   }
97   snprintf(fullmacro, sizeof(fullmacro), "macro-%s", macro);
98   if (!ast_exists_extension(chan, fullmacro, "s", 1, chan->cid.cid_num)) {
99         if (!ast_context_find(fullmacro)) 
100                 ast_log(LOG_WARNING, "No such context '%s' for macro '%s'\n", fullmacro, macro);
101         else
102                 ast_log(LOG_WARNING, "Context '%s' for macro '%s' lacks 's' extension, priority 1\n", fullmacro, macro);
103         return 0;
104   }
105
106   LOCAL_USER_ADD(u);
107   /* Save old info */
108   oldpriority = chan->priority;
109   strncpy(oldexten, chan->exten, sizeof(oldexten) - 1);
110   strncpy(oldcontext, chan->context, sizeof(oldcontext) - 1);
111   if (ast_strlen_zero(chan->macrocontext)) {
112         strncpy(chan->macrocontext, chan->context, sizeof(chan->macrocontext) - 1);
113         strncpy(chan->macroexten, chan->exten, sizeof(chan->macroexten) - 1);
114         chan->macropriority = chan->priority;
115         setmacrocontext=1;
116   }
117   argc = 1;
118   /* Save old macro variables */
119   save_macro_exten = pbx_builtin_getvar_helper(chan, "MACRO_EXTEN");
120   if (save_macro_exten) save_macro_exten = strdup(save_macro_exten);
121   pbx_builtin_setvar_helper(chan, "MACRO_EXTEN", oldexten);
122
123   save_macro_context = pbx_builtin_getvar_helper(chan, "MACRO_CONTEXT");
124   if (save_macro_context) save_macro_context = strdup(save_macro_context);
125   pbx_builtin_setvar_helper(chan, "MACRO_CONTEXT", oldcontext);
126
127   save_macro_priority = pbx_builtin_getvar_helper(chan, "MACRO_PRIORITY");
128   if (save_macro_priority) save_macro_priority = strdup(save_macro_priority);
129   snprintf(pc, sizeof(pc), "%d", oldpriority);
130   pbx_builtin_setvar_helper(chan, "MACRO_PRIORITY", pc);
131   
132   save_macro_offset = pbx_builtin_getvar_helper(chan, "MACRO_OFFSET");
133   if (save_macro_offset) save_macro_offset = strdup(save_macro_offset);
134   pbx_builtin_setvar_helper(chan, "MACRO_OFFSET", NULL);
135
136   /* Setup environment for new run */
137   chan->exten[0] = 's';
138   chan->exten[1] = '\0';
139   strncpy(chan->context, fullmacro, sizeof(chan->context) - 1);
140   chan->priority = 1;
141
142   while((cur = strsep(&rest, "|")) && (argc < MAX_ARGS)) {
143         /* Save copy of old arguments if we're overwriting some, otherwise
144            let them pass through to the other macro */
145         snprintf(varname, sizeof(varname), "ARG%d", argc);
146         oldargs[argc] = pbx_builtin_getvar_helper(chan, varname);
147         if (oldargs[argc])
148                 oldargs[argc] = strdup(oldargs[argc]);
149         pbx_builtin_setvar_helper(chan, varname, cur);
150         argc++;
151   }
152   while(ast_exists_extension(chan, chan->context, chan->exten, chan->priority, chan->cid.cid_num)) {
153         if ((res = ast_spawn_extension(chan, chan->context, chan->exten, chan->priority, chan->cid.cid_num))) {
154                 /* Something bad happened, or a hangup has been requested. */
155                 if (((res >= '0') && (res <= '9')) || ((res >= 'A') && (res <= 'F'))) {
156                         /* Just return result as to the previous application as if it had been dialed */
157                         ast_log(LOG_DEBUG, "Oooh, got something to jump out with ('%c')!\n", res);
158                         break;
159                 }
160                 switch(res) {
161                 case AST_PBX_KEEPALIVE:
162                         if (option_debug)
163                                 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);
164                         else if (option_verbose > 1)
165                                 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);
166                         goto out;
167                         break;
168                 default:
169                         if (option_debug)
170                                 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);
171                         else if (option_verbose > 1)
172                                 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);
173                         goto out;
174                 }
175         }
176         if (strcasecmp(chan->context, fullmacro)) {
177                 if (option_verbose > 1)
178                         ast_verbose(VERBOSE_PREFIX_2 "Channel '%s' jumping out of macro '%s'\n", chan->name, macro);
179                 break;
180         }
181         /* don't stop executing extensions when we're in "h" */
182         if (chan->_softhangup && strcasecmp(oldexten,"h")) {
183                 ast_log(LOG_DEBUG, "Extension %s, priority %d returned normally even though call was hung up\n",
184                         chan->exten, chan->priority);
185                 goto out;
186         }
187         chan->priority++;
188   }
189 out:
190   for (x=1;x<argc;x++) {
191         /* Restore old arguments and delete ours */
192         snprintf(varname, sizeof(varname), "ARG%d", x);
193         if (oldargs[x]) {
194                 pbx_builtin_setvar_helper(chan, varname, oldargs[x]);
195                 free(oldargs[x]);
196         } else {
197                 pbx_builtin_setvar_helper(chan, varname, NULL);
198         }
199   }
200
201   /* Restore macro variables */
202   pbx_builtin_setvar_helper(chan, "MACRO_EXTEN", save_macro_exten);
203   if (save_macro_exten) free(save_macro_exten);
204   pbx_builtin_setvar_helper(chan, "MACRO_CONTEXT", save_macro_context);
205   if (save_macro_context) free(save_macro_context);
206   pbx_builtin_setvar_helper(chan, "MACRO_PRIORITY", save_macro_priority);
207   if (save_macro_priority) free(save_macro_priority);
208   if (setmacrocontext) {
209         chan->macrocontext[0] = '\0';
210         chan->macroexten[0] = '\0';
211         chan->macropriority = 0;
212   }
213
214   if (!strcasecmp(chan->context, fullmacro)) {
215         /* If we're leaving the macro normally, restore original information */
216         chan->priority = oldpriority;
217         strncpy(chan->context, oldcontext, sizeof(chan->context) - 1);
218         if (!(chan->_softhangup & AST_SOFTHANGUP_ASYNCGOTO)) {
219                 /* Copy the extension, so long as we're not in softhangup, where we could be given an asyncgoto */
220                 strncpy(chan->exten, oldexten, sizeof(chan->exten) - 1);
221                 if ((offsets = pbx_builtin_getvar_helper(chan, "MACRO_OFFSET"))) {
222                         /* Handle macro offset if it's set by checking the availability of step n + offset + 1, otherwise continue
223                            normally if there is any problem */
224                         if (sscanf(offsets, "%d", &offset) == 1) {
225                                 if (ast_exists_extension(chan, chan->context, chan->exten, chan->priority + offset + 1, chan->cid.cid_num)) {
226                                         chan->priority += offset;
227                                 }
228                         }
229                 }
230         }
231   }
232
233   pbx_builtin_setvar_helper(chan, "MACRO_OFFSET", save_macro_offset);
234   if (save_macro_offset) free(save_macro_offset);
235   LOCAL_USER_REMOVE(u);
236   return res;
237 }
238
239 static int macroif_exec(struct ast_channel *chan, void *data) 
240 {
241         char *expr = NULL, *label_a = NULL, *label_b = NULL;
242         int res = 0;
243
244         if((expr = ast_strdupa((char *) data))) {
245                 if ((label_a = strchr(expr, '?'))) {
246                         *label_a = '\0';
247                         label_a++;
248                         if ((label_b = strchr(label_a, ':'))) {
249                                 *label_b = '\0';
250                                 label_b++;
251                         }
252                         if (ast_true(expr))
253                                 macro_exec(chan, label_a);
254                         else if (label_b) 
255                                 macro_exec(chan, label_b);
256                         
257                 } else
258                         ast_log(LOG_WARNING, "Invalid Syntax.\n");
259         } else 
260                 ast_log(LOG_ERROR, "Out of Memory!\n");
261         return res;
262 }
263                         
264 int unload_module(void)
265 {
266         STANDARD_HANGUP_LOCALUSERS;
267         ast_unregister_application(if_app);
268         return ast_unregister_application(app);
269 }
270
271 int load_module(void)
272 {
273         ast_register_application(if_app, macroif_exec, if_synopsis, if_descrip);
274         return ast_register_application(app, macro_exec, synopsis, descrip);
275 }
276
277 char *description(void)
278 {
279         return tdesc;
280 }
281
282 int usecount(void)
283 {
284         int res;
285         STANDARD_USECOUNT(res);
286         return res;
287 }
288
289 char *key()
290 {
291         return ASTERISK_GPL_KEY;
292 }