Add LOCAL_PEEK function, as requested by lmadsen.
[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 ${foo}, since the value of <replaceable>n</replaceable> peeks under 0 levels of
157                         stack frames; in other words, 0 is the current level.  If <replaceable>n</replaceable> exceeds
158                         the available number of stack frames, then an empty string is returned.</para>
159                 </description>
160                 <see-also>
161                         <ref type="application">Gosub</ref>
162                         <ref type="application">GosubIf</ref>
163                         <ref type="application">Return</ref>
164                 </see-also>
165         </function>
166  ***/
167
168 static const char *app_gosub = "Gosub";
169 static const char *app_gosubif = "GosubIf";
170 static const char *app_return = "Return";
171 static const char *app_pop = "StackPop";
172
173 static void gosub_free(void *data);
174
175 static struct ast_datastore_info stack_info = {
176         .type = "GOSUB",
177         .destroy = gosub_free,
178 };
179
180 struct gosub_stack_frame {
181         AST_LIST_ENTRY(gosub_stack_frame) entries;
182         /* 100 arguments is all that we support anyway, but this will handle up to 255 */
183         unsigned char arguments;
184         struct varshead varshead;
185         int priority;
186         char *context;
187         char extension[0];
188 };
189
190 static int frame_set_var(struct ast_channel *chan, struct gosub_stack_frame *frame, const char *var, const char *value)
191 {
192         struct ast_var_t *variables;
193         int found = 0;
194
195         /* Does this variable already exist? */
196         AST_LIST_TRAVERSE(&frame->varshead, variables, entries) {
197                 if (!strcmp(var, ast_var_name(variables))) {
198                         found = 1;
199                         break;
200                 }
201         }
202
203         if (!ast_strlen_zero(value)) {
204                 if (!found) {
205                         variables = ast_var_assign(var, "");
206                         AST_LIST_INSERT_HEAD(&frame->varshead, variables, entries);
207                         pbx_builtin_pushvar_helper(chan, var, value);
208                 } else
209                         pbx_builtin_setvar_helper(chan, var, value);
210
211                 manager_event(EVENT_FLAG_DIALPLAN, "VarSet", 
212                         "Channel: %s\r\n"
213                         "Variable: LOCAL(%s)\r\n"
214                         "Value: %s\r\n"
215                         "Uniqueid: %s\r\n", 
216                         chan->name, var, value, chan->uniqueid);
217         }
218         return 0;
219 }
220
221 static void gosub_release_frame(struct ast_channel *chan, struct gosub_stack_frame *frame)
222 {
223         struct ast_var_t *vardata;
224
225         /* If chan is not defined, then we're calling it as part of gosub_free,
226          * and the channel variables will be deallocated anyway.  Otherwise, we're
227          * just releasing a single frame, so we need to clean up the arguments for
228          * that frame, so that we re-expose the variables from the previous frame
229          * that were hidden by this one.
230          */
231         while ((vardata = AST_LIST_REMOVE_HEAD(&frame->varshead, entries))) {
232                 if (chan)
233                         pbx_builtin_setvar_helper(chan, ast_var_name(vardata), NULL);   
234                 ast_var_delete(vardata);
235         }
236
237         ast_free(frame);
238 }
239
240 static struct gosub_stack_frame *gosub_allocate_frame(const char *context, const char *extension, int priority, unsigned char arguments)
241 {
242         struct gosub_stack_frame *new = NULL;
243         int len_extension = strlen(extension), len_context = strlen(context);
244
245         if ((new = ast_calloc(1, sizeof(*new) + 2 + len_extension + len_context))) {
246                 AST_LIST_HEAD_INIT_NOLOCK(&new->varshead);
247                 strcpy(new->extension, extension);
248                 new->context = new->extension + len_extension + 1;
249                 strcpy(new->context, context);
250                 new->priority = priority;
251                 new->arguments = arguments;
252         }
253         return new;
254 }
255
256 static void gosub_free(void *data)
257 {
258         AST_LIST_HEAD(, gosub_stack_frame) *oldlist = data;
259         struct gosub_stack_frame *oldframe;
260         AST_LIST_LOCK(oldlist);
261         while ((oldframe = AST_LIST_REMOVE_HEAD(oldlist, entries))) {
262                 gosub_release_frame(NULL, oldframe);
263         }
264         AST_LIST_UNLOCK(oldlist);
265         AST_LIST_HEAD_DESTROY(oldlist);
266         ast_free(oldlist);
267 }
268
269 static int pop_exec(struct ast_channel *chan, void *data)
270 {
271         struct ast_datastore *stack_store = ast_channel_datastore_find(chan, &stack_info, NULL);
272         struct gosub_stack_frame *oldframe;
273         AST_LIST_HEAD(, gosub_stack_frame) *oldlist;
274
275         if (!stack_store) {
276                 ast_log(LOG_WARNING, "%s called with no gosub stack allocated.\n", app_pop);
277                 return 0;
278         }
279
280         oldlist = stack_store->data;
281         AST_LIST_LOCK(oldlist);
282         oldframe = AST_LIST_REMOVE_HEAD(oldlist, entries);
283         AST_LIST_UNLOCK(oldlist);
284
285         if (oldframe) {
286                 gosub_release_frame(chan, oldframe);
287         } else {
288                 ast_debug(1, "%s called with an empty gosub stack\n", app_pop);
289         }
290         return 0;
291 }
292
293 static int return_exec(struct ast_channel *chan, void *data)
294 {
295         struct ast_datastore *stack_store = ast_channel_datastore_find(chan, &stack_info, NULL);
296         struct gosub_stack_frame *oldframe;
297         AST_LIST_HEAD(, gosub_stack_frame) *oldlist;
298         char *retval = data;
299
300         if (!stack_store) {
301                 ast_log(LOG_ERROR, "Return without Gosub: stack is unallocated\n");
302                 return -1;
303         }
304
305         oldlist = stack_store->data;
306         AST_LIST_LOCK(oldlist);
307         oldframe = AST_LIST_REMOVE_HEAD(oldlist, entries);
308         AST_LIST_UNLOCK(oldlist);
309
310         if (!oldframe) {
311                 ast_log(LOG_ERROR, "Return without Gosub: stack is empty\n");
312                 return -1;
313         }
314
315         ast_explicit_goto(chan, oldframe->context, oldframe->extension, oldframe->priority);
316         gosub_release_frame(chan, oldframe);
317
318         /* Set a return value, if any */
319         pbx_builtin_setvar_helper(chan, "GOSUB_RETVAL", S_OR(retval, ""));
320         return 0;
321 }
322
323 static int gosub_exec(struct ast_channel *chan, void *data)
324 {
325         struct ast_datastore *stack_store = ast_channel_datastore_find(chan, &stack_info, NULL);
326         AST_LIST_HEAD(, gosub_stack_frame) *oldlist;
327         struct gosub_stack_frame *newframe;
328         char argname[15], *tmp = ast_strdupa(data), *label, *endparen;
329         int i;
330         AST_DECLARE_APP_ARGS(args2,
331                 AST_APP_ARG(argval)[100];
332         );
333
334         if (ast_strlen_zero(data)) {
335                 ast_log(LOG_ERROR, "%s requires an argument: %s([[context,]exten,]priority[(arg1[,...][,argN])])\n", app_gosub, app_gosub);
336                 return -1;
337         }
338
339         if (!stack_store) {
340                 ast_debug(1, "Channel %s has no datastore, so we're allocating one.\n", chan->name);
341                 stack_store = ast_datastore_alloc(&stack_info, NULL);
342                 if (!stack_store) {
343                         ast_log(LOG_ERROR, "Unable to allocate new datastore.  Gosub will fail.\n");
344                         return -1;
345                 }
346
347                 oldlist = ast_calloc(1, sizeof(*oldlist));
348                 if (!oldlist) {
349                         ast_log(LOG_ERROR, "Unable to allocate datastore list head.  Gosub will fail.\n");
350                         ast_datastore_free(stack_store);
351                         return -1;
352                 }
353
354                 stack_store->data = oldlist;
355                 AST_LIST_HEAD_INIT(oldlist);
356                 ast_channel_datastore_add(chan, stack_store);
357         }
358
359         /* Separate the arguments from the label */
360         /* NOTE:  you cannot use ast_app_separate_args for this, because '(' cannot be used as a delimiter. */
361         label = strsep(&tmp, "(");
362         if (tmp) {
363                 endparen = strrchr(tmp, ')');
364                 if (endparen)
365                         *endparen = '\0';
366                 else
367                         ast_log(LOG_WARNING, "Ouch.  No closing paren: '%s'?\n", (char *)data);
368                 AST_STANDARD_APP_ARGS(args2, tmp);
369         } else
370                 args2.argc = 0;
371
372         /* Create the return address, but don't save it until we know that the Gosub destination exists */
373         newframe = gosub_allocate_frame(chan->context, chan->exten, chan->priority + 1, args2.argc);
374
375         if (!newframe)
376                 return -1;
377
378         if (ast_parseable_goto(chan, label)) {
379                 ast_log(LOG_ERROR, "Gosub address is invalid: '%s'\n", (char *)data);
380                 ast_free(newframe);
381                 return -1;
382         }
383
384         /* Now that we know for certain that we're going to a new location, set our arguments */
385         for (i = 0; i < args2.argc; i++) {
386                 snprintf(argname, sizeof(argname), "ARG%d", i + 1);
387                 frame_set_var(chan, newframe, argname, args2.argval[i]);
388                 ast_debug(1, "Setting '%s' to '%s'\n", argname, args2.argval[i]);
389         }
390         snprintf(argname, sizeof(argname), "%d", args2.argc);
391         frame_set_var(chan, newframe, "ARGC", argname);
392
393         /* And finally, save our return address */
394         oldlist = stack_store->data;
395         AST_LIST_LOCK(oldlist);
396         AST_LIST_INSERT_HEAD(oldlist, newframe, entries);
397         AST_LIST_UNLOCK(oldlist);
398
399         return 0;
400 }
401
402 static int gosubif_exec(struct ast_channel *chan, void *data)
403 {
404         char *args;
405         int res=0;
406         AST_DECLARE_APP_ARGS(cond,
407                 AST_APP_ARG(ition);
408                 AST_APP_ARG(labels);
409         );
410         AST_DECLARE_APP_ARGS(label,
411                 AST_APP_ARG(iftrue);
412                 AST_APP_ARG(iffalse);
413         );
414
415         if (ast_strlen_zero(data)) {
416                 ast_log(LOG_WARNING, "GosubIf requires an argument: GosubIf(cond?label1(args):label2(args)\n");
417                 return 0;
418         }
419
420         args = ast_strdupa(data);
421         AST_NONSTANDARD_APP_ARGS(cond, args, '?');
422         if (cond.argc != 2) {
423                 ast_log(LOG_WARNING, "GosubIf requires an argument: GosubIf(cond?label1(args):label2(args)\n");
424                 return 0;
425         }
426
427         AST_NONSTANDARD_APP_ARGS(label, cond.labels, ':');
428
429         if (pbx_checkcondition(cond.ition)) {
430                 if (!ast_strlen_zero(label.iftrue))
431                         res = gosub_exec(chan, label.iftrue);
432         } else if (!ast_strlen_zero(label.iffalse)) {
433                 res = gosub_exec(chan, label.iffalse);
434         }
435
436         return res;
437 }
438
439 static int local_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
440 {
441         struct ast_datastore *stack_store = ast_channel_datastore_find(chan, &stack_info, NULL);
442         AST_LIST_HEAD(, gosub_stack_frame) *oldlist;
443         struct gosub_stack_frame *frame;
444         struct ast_var_t *variables;
445
446         if (!stack_store)
447                 return -1;
448
449         oldlist = stack_store->data;
450         AST_LIST_LOCK(oldlist);
451         frame = AST_LIST_FIRST(oldlist);
452         AST_LIST_TRAVERSE(&frame->varshead, variables, entries) {
453                 if (!strcmp(data, ast_var_name(variables))) {
454                         const char *tmp;
455                         ast_channel_lock(chan);
456                         tmp = pbx_builtin_getvar_helper(chan, data);
457                         ast_copy_string(buf, S_OR(tmp, ""), len);
458                         ast_channel_unlock(chan);
459                         break;
460                 }
461         }
462         AST_LIST_UNLOCK(oldlist);
463         return 0;
464 }
465
466 static int local_write(struct ast_channel *chan, const char *cmd, char *var, const char *value)
467 {
468         struct ast_datastore *stack_store = ast_channel_datastore_find(chan, &stack_info, NULL);
469         AST_LIST_HEAD(, gosub_stack_frame) *oldlist;
470         struct gosub_stack_frame *frame;
471
472         if (!stack_store) {
473                 ast_log(LOG_ERROR, "Tried to set LOCAL(%s), but we aren't within a Gosub routine\n", var);
474                 return -1;
475         }
476
477         oldlist = stack_store->data;
478         AST_LIST_LOCK(oldlist);
479         frame = AST_LIST_FIRST(oldlist);
480
481         if (frame)
482                 frame_set_var(chan, frame, var, value);
483
484         AST_LIST_UNLOCK(oldlist);
485
486         return 0;
487 }
488
489 static struct ast_custom_function local_function = {
490         .name = "LOCAL",
491         .write = local_write,
492         .read = local_read,
493 };
494
495 static int peek_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
496 {
497         int found = 0, n;
498         struct ast_var_t *variables;
499         AST_DECLARE_APP_ARGS(args,
500                 AST_APP_ARG(n);
501                 AST_APP_ARG(name);
502         );
503
504         if (!chan) {
505                 ast_log(LOG_ERROR, "LOCAL_PEEK must be called on an active channel\n");
506                 return -1;
507         }
508
509         AST_STANDARD_APP_ARGS(args, data);
510         n = atoi(args.n);
511         *buf = '\0';
512
513         ast_channel_lock(chan);
514         AST_LIST_TRAVERSE(&chan->varshead, variables, entries) {
515                 if (!strcmp(args.name, ast_var_name(variables)) && ++found > n) {
516                         ast_copy_string(buf, ast_var_value(variables), len);
517                         break;
518                 }
519         }
520         ast_channel_unlock(chan);
521         return 0;
522 }
523
524 static struct ast_custom_function peek_function = {
525         .name = "LOCAL_PEEK",
526         .synopsis = "Peeks at variables within the variable stack",
527         .syntax = "LOCAL_PEEK(<n>|<varname>)",
528         .read = peek_read,
529 };
530
531 static int handle_gosub(struct ast_channel *chan, AGI *agi, int argc, char **argv)
532 {
533         int old_priority, priority;
534         char old_context[AST_MAX_CONTEXT], old_extension[AST_MAX_EXTENSION];
535         struct ast_app *theapp;
536         char *gosub_args;
537
538         if (argc < 4 || argc > 5) {
539                 return RESULT_SHOWUSAGE;
540         }
541
542         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] : "");
543
544         if (sscanf(argv[3], "%d", &priority) != 1 || priority < 1) {
545                 /* Lookup the priority label */
546                 if ((priority = ast_findlabel_extension(chan, argv[1], argv[2], argv[3], chan->cid.cid_num)) < 0) {
547                         ast_log(LOG_ERROR, "Priority '%s' not found in '%s@%s'\n", argv[3], argv[2], argv[1]);
548                         ast_agi_send(agi->fd, chan, "200 result=-1 Gosub label not found\n");
549                         return RESULT_FAILURE;
550                 }
551         } else if (!ast_exists_extension(chan, argv[1], argv[2], priority, chan->cid.cid_num)) {
552                 ast_agi_send(agi->fd, chan, "200 result=-1 Gosub label not found\n");
553                 return RESULT_FAILURE;
554         }
555
556         /* Save previous location, since we're going to change it */
557         ast_copy_string(old_context, chan->context, sizeof(old_context));
558         ast_copy_string(old_extension, chan->exten, sizeof(old_extension));
559         old_priority = chan->priority;
560
561         if (!(theapp = pbx_findapp("Gosub"))) {
562                 ast_log(LOG_ERROR, "Gosub() cannot be found in the list of loaded applications\n");
563                 ast_agi_send(agi->fd, chan, "503 result=-2 Gosub is not loaded\n");
564                 return RESULT_FAILURE;
565         }
566
567         /* Apparently, if you run ast_pbx_run on a channel that already has a pbx
568          * structure, you need to add 1 to the priority to get it to go to the
569          * right place.  But if it doesn't have a pbx structure, then leaving off
570          * the 1 is the right thing to do.  See how this code differs when we
571          * call a Gosub for the CALLEE channel in Dial or Queue.
572          */
573         if (argc == 5) {
574                 if (asprintf(&gosub_args, "%s,%s,%d(%s)", argv[1], argv[2], priority + 1, argv[4]) < 0) {
575                         ast_log(LOG_WARNING, "asprintf() failed: %s\n", strerror(errno));
576                         gosub_args = NULL;
577                 }
578         } else {
579                 if (asprintf(&gosub_args, "%s,%s,%d", argv[1], argv[2], priority + 1) < 0) {
580                         ast_log(LOG_WARNING, "asprintf() failed: %s\n", strerror(errno));
581                         gosub_args = NULL;
582                 }
583         }
584
585         if (gosub_args) {
586                 int res;
587
588                 ast_debug(1, "Trying gosub with arguments '%s'\n", gosub_args);
589                 ast_copy_string(chan->context, "app_stack_gosub_virtual_context", sizeof(chan->context));
590                 ast_copy_string(chan->exten, "s", sizeof(chan->exten));
591                 chan->priority = 0;
592
593                 if ((res = pbx_exec(chan, theapp, gosub_args)) == 0) {
594                         struct ast_pbx *pbx = chan->pbx;
595                         /* Suppress warning about PBX already existing */
596                         chan->pbx = NULL;
597                         ast_agi_send(agi->fd, chan, "100 result=0 Trying...\n");
598                         ast_pbx_run(chan);
599                         ast_agi_send(agi->fd, chan, "200 result=0 Gosub complete\n");
600                         if (chan->pbx) {
601                                 ast_free(chan->pbx);
602                         }
603                         chan->pbx = pbx;
604                 } else {
605                         ast_agi_send(agi->fd, chan, "200 result=%d Gosub failed\n", res);
606                 }
607                 ast_free(gosub_args);
608         } else {
609                 ast_agi_send(agi->fd, chan, "503 result=-2 Memory allocation failure\n");
610                 return RESULT_FAILURE;
611         }
612
613         /* Restore previous location */
614         ast_copy_string(chan->context, old_context, sizeof(chan->context));
615         ast_copy_string(chan->exten, old_extension, sizeof(chan->exten));
616         chan->priority = old_priority;
617
618         return RESULT_SUCCESS;
619 }
620
621 static char usage_gosub[] =
622 " Usage: GOSUB <context> <extension> <priority> [<optional-argument>]\n"
623 "   Cause the channel to execute the specified dialplan subroutine, returning\n"
624 " to the dialplan with execution of a Return()\n";
625
626 struct agi_command gosub_agi_command =
627         { { "gosub", NULL }, handle_gosub, "Execute a dialplan subroutine", usage_gosub , 0 };
628
629 static int unload_module(void)
630 {
631         struct ast_context *con;
632
633         if (ast_agi_unregister(ast_module_info->self, &gosub_agi_command) == 1) {
634                 if ((con = ast_context_find("app_stack_gosub_virtual_context"))) {
635                         ast_context_remove_extension2(con, "s", 1, NULL, 0);
636                         ast_context_destroy(con, "app_stack"); /* leave nothing behind */
637                 }
638         }
639
640         ast_unregister_application(app_return);
641         ast_unregister_application(app_pop);
642         ast_unregister_application(app_gosubif);
643         ast_unregister_application(app_gosub);
644         ast_custom_function_unregister(&local_function);
645         ast_custom_function_unregister(&peek_function);
646
647         return 0;
648 }
649
650 static int load_module(void)
651 {
652         struct ast_context *con;
653
654         if (ast_agi_register(ast_module_info->self, &gosub_agi_command) == 1) {
655                 if (!(con = ast_context_find_or_create(NULL, NULL, "app_stack_gosub_virtual_context", "app_stack"))) {
656                         ast_log(LOG_ERROR, "Virtual context 'app_stack_gosub_virtual_context' does not exist and unable to create\n");
657                         return AST_MODULE_LOAD_DECLINE;
658                 } else {
659                         ast_add_extension2(con, 1, "s", 1, NULL, NULL, "KeepAlive", ast_strdup(""), ast_free_ptr, "app_stack");
660                 }
661         }
662
663         ast_register_application_xml(app_pop, pop_exec);
664         ast_register_application_xml(app_return, return_exec);
665         ast_register_application_xml(app_gosubif, gosubif_exec);
666         ast_register_application_xml(app_gosub, gosub_exec);
667         ast_custom_function_register(&local_function);
668         ast_custom_function_register(&peek_function);
669
670         return 0;
671 }
672
673 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Dialplan subroutines (Gosub, Return, etc)");