Fix0ring build
[asterisk/asterisk.git] / apps / app_stack.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (c) 2004-2006 Tilghman Lesher <app_stack_v003@the-tilghman.com>.
5  *
6  * This code is released by the author with no restrictions on usage.
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
19 /*! \file
20  *
21  * \brief Stack applications Gosub, Return, etc.
22  *
23  * \author Tilghman Lesher <app_stack_v003@the-tilghman.com>
24  * 
25  * \ingroup applications
26  */
27
28 /*** MODULEINFO
29         <use>res_agi</use>
30  ***/
31
32 #include "asterisk.h"
33  
34 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
35
36 #include "asterisk/pbx.h"
37 #include "asterisk/module.h"
38 #include "asterisk/app.h"
39 #include "asterisk/manager.h"
40 #include "asterisk/channel.h"
41 #include "asterisk/agi.h"
42
43 /*** DOCUMENTATION
44         <application name="Gosub" language="en_US">
45                 <synopsis>
46                         Jump to label, saving return address.
47                 </synopsis>
48                 <syntax>
49                         <parameter name="context" />
50                         <parameter name="exten" />
51                         <parameter name="priority" required="true" hasparams="optional">
52                                 <argument name="arg1" multiple="true" required="true" />
53                                 <argument name="argN" />
54                         </parameter>
55                 </syntax>
56                 <description>
57                         <para>Jumps to the label specified, saving the return address.</para>
58                 </description>
59                 <see-also>
60                         <ref type="application">GosubIf</ref>
61                         <ref type="application">Macro</ref>
62                         <ref type="application">Goto</ref>
63                         <ref type="application">Return</ref>
64                         <ref type="application">StackPop</ref>
65                 </see-also>
66         </application>
67         <application name="GosubIf" language="en_US">
68                 <synopsis>
69                         Conditionally jump to label, saving return address.
70                 </synopsis>
71                 <syntax argsep="?">
72                         <parameter name="condition" required="true" />
73                         <parameter name="destination" required="true" argsep=":">
74                                 <argument name="labeliftrue" hasparams="optional">
75                                         <argument name="arg1" required="true" multiple="true" />
76                                         <argument name="argN" />
77                                 </argument>
78                                 <argument name="labeliffalse" hasparams="optional">
79                                         <argument name="arg1" required="true" multiple="true" />
80                                         <argument name="argN" />
81                                 </argument>
82                         </parameter>
83                 </syntax>
84                 <description>
85                         <para>If the condition is true, then jump to labeliftrue.  If false, jumps to
86                         labeliffalse, if specified.  In either case, a jump saves the return point
87                         in the dialplan, to be returned to with a Return.</para>
88                 </description>
89                 <see-also>
90                         <ref type="application">Gosub</ref>
91                         <ref type="application">Return</ref>
92                         <ref type="application">MacroIf</ref>
93                         <ref type="function">IF</ref>
94                         <ref type="application">GotoIf</ref>
95                 </see-also>
96         </application>
97         <application name="Return" language="en_US">
98                 <synopsis>
99                         Return from gosub routine.
100                 </synopsis>
101                 <syntax>
102                         <parameter name="value">
103                                 <para>Return value.</para>
104                         </parameter>
105                 </syntax>
106                 <description>
107                         <para>Jumps to the last label on the stack, removing it. The return <replaceable>value</replaceable>, if
108                         any, is saved in the channel variable <variable>GOSUB_RETVAL</variable>.</para>
109                 </description>
110                 <see-also>
111                         <ref type="application">Gosub</ref>
112                         <ref type="application">StackPop</ref>
113                 </see-also>
114         </application>
115         <application name="StackPop" language="en_US">
116                 <synopsis>
117                         Remove one address from gosub stack.
118                 </synopsis>
119                 <syntax />
120                 <description>
121                         <para>Removes last label on the stack, discarding it.</para>
122                 </description>
123                 <see-also>
124                         <ref type="application">Return</ref>
125                         <ref type="application">Gosub</ref>
126                 </see-also>
127         </application>
128         <function name="LOCAL" language="en_US">
129                 <synopsis>
130                         Manage variables local to the gosub stack frame.
131                 </synopsis>
132                 <syntax>
133                         <parameter name="varname" required="true" />
134                 </syntax>
135                 <description>
136                         <para>Read and write a variable local to the gosub stack frame, once we Return() it will be lost
137                         (or it will go back to whatever value it had before the Gosub()).</para>
138                 </description>
139                 <see-also>
140                         <ref type="application">Gosub</ref>
141                         <ref type="application">GosubIf</ref>
142                         <ref type="application">Return</ref>
143                 </see-also>
144         </function>
145         <function name="LOCAL_PEEK" language="en_US">
146                 <synopsis>
147                         Retrieve variables hidden by the local gosub stack frame.
148                 </synopsis>
149                 <syntax>
150                         <parameter name="n" required="true" />
151                         <parameter name="varname" required="true" />
152                 </syntax>
153                 <description>
154                         <para>Read a variable <replaceable>varname</replaceable> hidden by
155                         <replaceable>n</replaceable> levels of gosub stack frames.  Note that ${LOCAL_PEEK(0,foo)}
156                         is the same as <variable>foo</variable>, since the value of <replaceable>n</replaceable>
157                         peeks under 0 levels of stack frames; in other words, 0 is the current level.  If
158                         <replaceable>n</replaceable> exceeds the available number of stack frames, then an empty
159                         string is returned.</para>
160                 </description>
161                 <see-also>
162                         <ref type="application">Gosub</ref>
163                         <ref type="application">GosubIf</ref>
164                         <ref type="application">Return</ref>
165                 </see-also>
166         </function>
167  ***/
168
169 static const char *app_gosub = "Gosub";
170 static const char *app_gosubif = "GosubIf";
171 static const char *app_return = "Return";
172 static const char *app_pop = "StackPop";
173
174 static void gosub_free(void *data);
175
176 static struct ast_datastore_info stack_info = {
177         .type = "GOSUB",
178         .destroy = gosub_free,
179 };
180
181 struct gosub_stack_frame {
182         AST_LIST_ENTRY(gosub_stack_frame) entries;
183         /* 100 arguments is all that we support anyway, but this will handle up to 255 */
184         unsigned char arguments;
185         struct varshead varshead;
186         int priority;
187         char *context;
188         char extension[0];
189 };
190
191 static int frame_set_var(struct ast_channel *chan, struct gosub_stack_frame *frame, const char *var, const char *value)
192 {
193         struct ast_var_t *variables;
194         int found = 0;
195
196         /* Does this variable already exist? */
197         AST_LIST_TRAVERSE(&frame->varshead, variables, entries) {
198                 if (!strcmp(var, ast_var_name(variables))) {
199                         found = 1;
200                         break;
201                 }
202         }
203
204         if (!ast_strlen_zero(value)) {
205                 if (!found) {
206                         variables = ast_var_assign(var, "");
207                         AST_LIST_INSERT_HEAD(&frame->varshead, variables, entries);
208                         pbx_builtin_pushvar_helper(chan, var, value);
209                 } else
210                         pbx_builtin_setvar_helper(chan, var, value);
211
212                 manager_event(EVENT_FLAG_DIALPLAN, "VarSet", 
213                         "Channel: %s\r\n"
214                         "Variable: LOCAL(%s)\r\n"
215                         "Value: %s\r\n"
216                         "Uniqueid: %s\r\n", 
217                         chan->name, var, value, chan->uniqueid);
218         }
219         return 0;
220 }
221
222 static void gosub_release_frame(struct ast_channel *chan, struct gosub_stack_frame *frame)
223 {
224         struct ast_var_t *vardata;
225
226         /* If chan is not defined, then we're calling it as part of gosub_free,
227          * and the channel variables will be deallocated anyway.  Otherwise, we're
228          * just releasing a single frame, so we need to clean up the arguments for
229          * that frame, so that we re-expose the variables from the previous frame
230          * that were hidden by this one.
231          */
232         while ((vardata = AST_LIST_REMOVE_HEAD(&frame->varshead, entries))) {
233                 if (chan)
234                         pbx_builtin_setvar_helper(chan, ast_var_name(vardata), NULL);   
235                 ast_var_delete(vardata);
236         }
237
238         ast_free(frame);
239 }
240
241 static struct gosub_stack_frame *gosub_allocate_frame(const char *context, const char *extension, int priority, unsigned char arguments)
242 {
243         struct gosub_stack_frame *new = NULL;
244         int len_extension = strlen(extension), len_context = strlen(context);
245
246         if ((new = ast_calloc(1, sizeof(*new) + 2 + len_extension + len_context))) {
247                 AST_LIST_HEAD_INIT_NOLOCK(&new->varshead);
248                 strcpy(new->extension, extension);
249                 new->context = new->extension + len_extension + 1;
250                 strcpy(new->context, context);
251                 new->priority = priority;
252                 new->arguments = arguments;
253         }
254         return new;
255 }
256
257 static void gosub_free(void *data)
258 {
259         AST_LIST_HEAD(, gosub_stack_frame) *oldlist = data;
260         struct gosub_stack_frame *oldframe;
261         AST_LIST_LOCK(oldlist);
262         while ((oldframe = AST_LIST_REMOVE_HEAD(oldlist, entries))) {
263                 gosub_release_frame(NULL, oldframe);
264         }
265         AST_LIST_UNLOCK(oldlist);
266         AST_LIST_HEAD_DESTROY(oldlist);
267         ast_free(oldlist);
268 }
269
270 static int pop_exec(struct ast_channel *chan, void *data)
271 {
272         struct ast_datastore *stack_store = ast_channel_datastore_find(chan, &stack_info, NULL);
273         struct gosub_stack_frame *oldframe;
274         AST_LIST_HEAD(, gosub_stack_frame) *oldlist;
275
276         if (!stack_store) {
277                 ast_log(LOG_WARNING, "%s called with no gosub stack allocated.\n", app_pop);
278                 return 0;
279         }
280
281         oldlist = stack_store->data;
282         AST_LIST_LOCK(oldlist);
283         oldframe = AST_LIST_REMOVE_HEAD(oldlist, entries);
284         AST_LIST_UNLOCK(oldlist);
285
286         if (oldframe) {
287                 gosub_release_frame(chan, oldframe);
288         } else {
289                 ast_debug(1, "%s called with an empty gosub stack\n", app_pop);
290         }
291         return 0;
292 }
293
294 static int return_exec(struct ast_channel *chan, void *data)
295 {
296         struct ast_datastore *stack_store = ast_channel_datastore_find(chan, &stack_info, NULL);
297         struct gosub_stack_frame *oldframe;
298         AST_LIST_HEAD(, gosub_stack_frame) *oldlist;
299         char *retval = data;
300
301         if (!stack_store) {
302                 ast_log(LOG_ERROR, "Return without Gosub: stack is unallocated\n");
303                 return -1;
304         }
305
306         oldlist = stack_store->data;
307         AST_LIST_LOCK(oldlist);
308         oldframe = AST_LIST_REMOVE_HEAD(oldlist, entries);
309         AST_LIST_UNLOCK(oldlist);
310
311         if (!oldframe) {
312                 ast_log(LOG_ERROR, "Return without Gosub: stack is empty\n");
313                 return -1;
314         }
315
316         ast_explicit_goto(chan, oldframe->context, oldframe->extension, oldframe->priority);
317         gosub_release_frame(chan, oldframe);
318
319         /* Set a return value, if any */
320         pbx_builtin_setvar_helper(chan, "GOSUB_RETVAL", S_OR(retval, ""));
321         return 0;
322 }
323
324 static int gosub_exec(struct ast_channel *chan, void *data)
325 {
326         struct ast_datastore *stack_store = ast_channel_datastore_find(chan, &stack_info, NULL);
327         AST_LIST_HEAD(, gosub_stack_frame) *oldlist;
328         struct gosub_stack_frame *newframe;
329         char argname[15], *tmp = ast_strdupa(data), *label, *endparen;
330         int i;
331         AST_DECLARE_APP_ARGS(args2,
332                 AST_APP_ARG(argval)[100];
333         );
334
335         if (ast_strlen_zero(data)) {
336                 ast_log(LOG_ERROR, "%s requires an argument: %s([[context,]exten,]priority[(arg1[,...][,argN])])\n", app_gosub, app_gosub);
337                 return -1;
338         }
339
340         if (!stack_store) {
341                 ast_debug(1, "Channel %s has no datastore, so we're allocating one.\n", chan->name);
342                 stack_store = ast_datastore_alloc(&stack_info, NULL);
343                 if (!stack_store) {
344                         ast_log(LOG_ERROR, "Unable to allocate new datastore.  Gosub will fail.\n");
345                         return -1;
346                 }
347
348                 oldlist = ast_calloc(1, sizeof(*oldlist));
349                 if (!oldlist) {
350                         ast_log(LOG_ERROR, "Unable to allocate datastore list head.  Gosub will fail.\n");
351                         ast_datastore_free(stack_store);
352                         return -1;
353                 }
354
355                 stack_store->data = oldlist;
356                 AST_LIST_HEAD_INIT(oldlist);
357                 ast_channel_datastore_add(chan, stack_store);
358         }
359
360         /* Separate the arguments from the label */
361         /* NOTE:  you cannot use ast_app_separate_args for this, because '(' cannot be used as a delimiter. */
362         label = strsep(&tmp, "(");
363         if (tmp) {
364                 endparen = strrchr(tmp, ')');
365                 if (endparen)
366                         *endparen = '\0';
367                 else
368                         ast_log(LOG_WARNING, "Ouch.  No closing paren: '%s'?\n", (char *)data);
369                 AST_STANDARD_APP_ARGS(args2, tmp);
370         } else
371                 args2.argc = 0;
372
373         /* Create the return address, but don't save it until we know that the Gosub destination exists */
374         newframe = gosub_allocate_frame(chan->context, chan->exten, chan->priority + 1, args2.argc);
375
376         if (!newframe) {
377                 return -1;
378         }
379
380         if (ast_parseable_goto(chan, label)) {
381                 ast_log(LOG_ERROR, "Gosub address is invalid: '%s'\n", (char *)data);
382                 ast_free(newframe);
383                 return -1;
384         }
385
386         if (!ast_exists_extension(chan, chan->context, chan->exten, ast_test_flag(chan, AST_FLAG_IN_AUTOLOOP) ? chan->priority + 1 : chan->priority, chan->cid.cid_num)) {
387                 ast_log(LOG_ERROR, "Attempt to reach a non-existent destination for gosub: (Context:%s, Extension:%s, Priority:%d)\n",
388                                 chan->context, chan->exten, chan->priority);
389                 ast_copy_string(chan->context, newframe->context, sizeof(chan->context));
390                 ast_copy_string(chan->exten, newframe->extension, sizeof(chan->exten));
391                 chan->priority = newframe->priority;
392                 ast_free(newframe);
393                 return -1;
394         }
395
396         /* Now that we know for certain that we're going to a new location, set our arguments */
397         for (i = 0; i < args2.argc; i++) {
398                 snprintf(argname, sizeof(argname), "ARG%d", i + 1);
399                 frame_set_var(chan, newframe, argname, args2.argval[i]);
400                 ast_debug(1, "Setting '%s' to '%s'\n", argname, args2.argval[i]);
401         }
402         snprintf(argname, sizeof(argname), "%d", args2.argc);
403         frame_set_var(chan, newframe, "ARGC", argname);
404
405         /* And finally, save our return address */
406         oldlist = stack_store->data;
407         AST_LIST_LOCK(oldlist);
408         AST_LIST_INSERT_HEAD(oldlist, newframe, entries);
409         AST_LIST_UNLOCK(oldlist);
410
411         return 0;
412 }
413
414 static int gosubif_exec(struct ast_channel *chan, void *data)
415 {
416         char *args;
417         int res=0;
418         AST_DECLARE_APP_ARGS(cond,
419                 AST_APP_ARG(ition);
420                 AST_APP_ARG(labels);
421         );
422         AST_DECLARE_APP_ARGS(label,
423                 AST_APP_ARG(iftrue);
424                 AST_APP_ARG(iffalse);
425         );
426
427         if (ast_strlen_zero(data)) {
428                 ast_log(LOG_WARNING, "GosubIf requires an argument: GosubIf(cond?label1(args):label2(args)\n");
429                 return 0;
430         }
431
432         args = ast_strdupa(data);
433         AST_NONSTANDARD_APP_ARGS(cond, args, '?');
434         if (cond.argc != 2) {
435                 ast_log(LOG_WARNING, "GosubIf requires an argument: GosubIf(cond?label1(args):label2(args)\n");
436                 return 0;
437         }
438
439         AST_NONSTANDARD_APP_ARGS(label, cond.labels, ':');
440
441         if (pbx_checkcondition(cond.ition)) {
442                 if (!ast_strlen_zero(label.iftrue))
443                         res = gosub_exec(chan, label.iftrue);
444         } else if (!ast_strlen_zero(label.iffalse)) {
445                 res = gosub_exec(chan, label.iffalse);
446         }
447
448         return res;
449 }
450
451 static int local_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
452 {
453         struct ast_datastore *stack_store = ast_channel_datastore_find(chan, &stack_info, NULL);
454         AST_LIST_HEAD(, gosub_stack_frame) *oldlist;
455         struct gosub_stack_frame *frame;
456         struct ast_var_t *variables;
457
458         if (!stack_store)
459                 return -1;
460
461         oldlist = stack_store->data;
462         AST_LIST_LOCK(oldlist);
463         frame = AST_LIST_FIRST(oldlist);
464         AST_LIST_TRAVERSE(&frame->varshead, variables, entries) {
465                 if (!strcmp(data, ast_var_name(variables))) {
466                         const char *tmp;
467                         ast_channel_lock(chan);
468                         tmp = pbx_builtin_getvar_helper(chan, data);
469                         ast_copy_string(buf, S_OR(tmp, ""), len);
470                         ast_channel_unlock(chan);
471                         break;
472                 }
473         }
474         AST_LIST_UNLOCK(oldlist);
475         return 0;
476 }
477
478 static int local_write(struct ast_channel *chan, const char *cmd, char *var, const char *value)
479 {
480         struct ast_datastore *stack_store = ast_channel_datastore_find(chan, &stack_info, NULL);
481         AST_LIST_HEAD(, gosub_stack_frame) *oldlist;
482         struct gosub_stack_frame *frame;
483
484         if (!stack_store) {
485                 ast_log(LOG_ERROR, "Tried to set LOCAL(%s), but we aren't within a Gosub routine\n", var);
486                 return -1;
487         }
488
489         oldlist = stack_store->data;
490         AST_LIST_LOCK(oldlist);
491         frame = AST_LIST_FIRST(oldlist);
492
493         if (frame)
494                 frame_set_var(chan, frame, var, value);
495
496         AST_LIST_UNLOCK(oldlist);
497
498         return 0;
499 }
500
501 static struct ast_custom_function local_function = {
502         .name = "LOCAL",
503         .write = local_write,
504         .read = local_read,
505 };
506
507 static int peek_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
508 {
509         int found = 0, n;
510         struct ast_var_t *variables;
511         AST_DECLARE_APP_ARGS(args,
512                 AST_APP_ARG(n);
513                 AST_APP_ARG(name);
514         );
515
516         if (!chan) {
517                 ast_log(LOG_ERROR, "LOCAL_PEEK must be called on an active channel\n");
518                 return -1;
519         }
520
521         AST_STANDARD_APP_ARGS(args, data);
522         n = atoi(args.n);
523         *buf = '\0';
524
525         ast_channel_lock(chan);
526         AST_LIST_TRAVERSE(&chan->varshead, variables, entries) {
527                 if (!strcmp(args.name, ast_var_name(variables)) && ++found > n) {
528                         ast_copy_string(buf, ast_var_value(variables), len);
529                         break;
530                 }
531         }
532         ast_channel_unlock(chan);
533         return 0;
534 }
535
536 static struct ast_custom_function peek_function = {
537         .name = "LOCAL_PEEK",
538         .read = peek_read,
539 };
540
541 static int handle_gosub(struct ast_channel *chan, AGI *agi, int argc, char **argv)
542 {
543         int old_priority, priority;
544         char old_context[AST_MAX_CONTEXT], old_extension[AST_MAX_EXTENSION];
545         struct ast_app *theapp;
546         char *gosub_args;
547
548         if (argc < 4 || argc > 5) {
549                 return RESULT_SHOWUSAGE;
550         }
551
552         ast_debug(1, "Gosub called with %d arguments: 0:%s 1:%s 2:%s 3:%s 4:%s\n", argc, argv[0], argv[1], argv[2], argv[3], argc == 5 ? argv[4] : "");
553
554         if (sscanf(argv[3], "%d", &priority) != 1 || priority < 1) {
555                 /* Lookup the priority label */
556                 if ((priority = ast_findlabel_extension(chan, argv[1], argv[2], argv[3], chan->cid.cid_num)) < 0) {
557                         ast_log(LOG_ERROR, "Priority '%s' not found in '%s@%s'\n", argv[3], argv[2], argv[1]);
558                         ast_agi_send(agi->fd, chan, "200 result=-1 Gosub label not found\n");
559                         return RESULT_FAILURE;
560                 }
561         } else if (!ast_exists_extension(chan, argv[1], argv[2], priority, chan->cid.cid_num)) {
562                 ast_agi_send(agi->fd, chan, "200 result=-1 Gosub label not found\n");
563                 return RESULT_FAILURE;
564         }
565
566         /* Save previous location, since we're going to change it */
567         ast_copy_string(old_context, chan->context, sizeof(old_context));
568         ast_copy_string(old_extension, chan->exten, sizeof(old_extension));
569         old_priority = chan->priority;
570
571         if (!(theapp = pbx_findapp("Gosub"))) {
572                 ast_log(LOG_ERROR, "Gosub() cannot be found in the list of loaded applications\n");
573                 ast_agi_send(agi->fd, chan, "503 result=-2 Gosub is not loaded\n");
574                 return RESULT_FAILURE;
575         }
576
577         /* Apparently, if you run ast_pbx_run on a channel that already has a pbx
578          * structure, you need to add 1 to the priority to get it to go to the
579          * right place.  But if it doesn't have a pbx structure, then leaving off
580          * the 1 is the right thing to do.  See how this code differs when we
581          * call a Gosub for the CALLEE channel in Dial or Queue.
582          */
583         if (argc == 5) {
584                 if (asprintf(&gosub_args, "%s,%s,%d(%s)", argv[1], argv[2], priority + 1, argv[4]) < 0) {
585                         ast_log(LOG_WARNING, "asprintf() failed: %s\n", strerror(errno));
586                         gosub_args = NULL;
587                 }
588         } else {
589                 if (asprintf(&gosub_args, "%s,%s,%d", argv[1], argv[2], priority + 1) < 0) {
590                         ast_log(LOG_WARNING, "asprintf() failed: %s\n", strerror(errno));
591                         gosub_args = NULL;
592                 }
593         }
594
595         if (gosub_args) {
596                 int res;
597
598                 ast_debug(1, "Trying gosub with arguments '%s'\n", gosub_args);
599
600                 if ((res = pbx_exec(chan, theapp, gosub_args)) == 0) {
601                         struct ast_pbx *pbx = chan->pbx;
602                         struct ast_pbx_args args;
603                         memset(&args, 0, sizeof(args));
604                         args.no_hangup_chan = 1;
605                         /* Suppress warning about PBX already existing */
606                         chan->pbx = NULL;
607                         ast_agi_send(agi->fd, chan, "100 result=0 Trying...\n");
608                         ast_pbx_run_args(chan, &args);
609                         ast_agi_send(agi->fd, chan, "200 result=0 Gosub complete\n");
610                         if (chan->pbx) {
611                                 ast_free(chan->pbx);
612                         }
613                         chan->pbx = pbx;
614                 } else {
615                         ast_agi_send(agi->fd, chan, "200 result=%d Gosub failed\n", res);
616                 }
617                 ast_free(gosub_args);
618         } else {
619                 ast_agi_send(agi->fd, chan, "503 result=-2 Memory allocation failure\n");
620                 return RESULT_FAILURE;
621         }
622
623         /* Restore previous location */
624         ast_copy_string(chan->context, old_context, sizeof(chan->context));
625         ast_copy_string(chan->exten, old_extension, sizeof(chan->exten));
626         chan->priority = old_priority;
627
628         return RESULT_SUCCESS;
629 }
630
631 static char usage_gosub[] =
632 " Usage: GOSUB <context> <extension> <priority> [<optional-argument>]\n"
633 "   Cause the channel to execute the specified dialplan subroutine, returning\n"
634 " to the dialplan with execution of a Return()\n";
635
636 struct agi_command gosub_agi_command =
637         { { "gosub", NULL }, handle_gosub, "Execute a dialplan subroutine", usage_gosub , 0 };
638
639 static int unload_module(void)
640 {
641         if (ast_agi_unregister) {
642                  ast_agi_unregister(ast_module_info->self, &gosub_agi_command);
643         }
644
645         ast_unregister_application(app_return);
646         ast_unregister_application(app_pop);
647         ast_unregister_application(app_gosubif);
648         ast_unregister_application(app_gosub);
649         ast_custom_function_unregister(&local_function);
650         ast_custom_function_unregister(&peek_function);
651
652         return 0;
653 }
654
655 static int load_module(void)
656 {
657         if (ast_agi_register) {
658                  ast_agi_register(ast_module_info->self, &gosub_agi_command);
659         }
660
661         ast_register_application_xml(app_pop, pop_exec);
662         ast_register_application_xml(app_return, return_exec);
663         ast_register_application_xml(app_gosubif, gosubif_exec);
664         ast_register_application_xml(app_gosub, gosub_exec);
665         ast_custom_function_register(&local_function);
666         ast_custom_function_register(&peek_function);
667
668         return 0;
669 }
670
671 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Dialplan subroutines (Gosub, Return, etc)");