Migrate AMI VarSet events raised by GoSub local variables
[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 type="module">res_agi</use>
30         <support_level>core</support_level>
31  ***/
32
33 #include "asterisk.h"
34  
35 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
36
37 #include "asterisk/pbx.h"
38 #include "asterisk/module.h"
39 #include "asterisk/app.h"
40 #include "asterisk/manager.h"
41 #include "asterisk/channel.h"
42 #include "asterisk/agi.h"
43 #include "asterisk/stasis_channels.h"
44
45 /*** DOCUMENTATION
46         <application name="Gosub" language="en_US">
47                 <synopsis>
48                         Jump to label, saving return address.
49                 </synopsis>
50                 <syntax>
51                         <parameter name="context" />
52                         <parameter name="exten" />
53                         <parameter name="priority" required="true" hasparams="optional">
54                                 <argument name="arg1" multiple="true" required="true" />
55                                 <argument name="argN" />
56                         </parameter>
57                 </syntax>
58                 <description>
59                         <para>Jumps to the label specified, saving the return address.</para>
60                 </description>
61                 <see-also>
62                         <ref type="application">GosubIf</ref>
63                         <ref type="application">Macro</ref>
64                         <ref type="application">Goto</ref>
65                         <ref type="application">Return</ref>
66                         <ref type="application">StackPop</ref>
67                 </see-also>
68         </application>
69         <application name="GosubIf" language="en_US">
70                 <synopsis>
71                         Conditionally jump to label, saving return address.
72                 </synopsis>
73                 <syntax argsep="?">
74                         <parameter name="condition" required="true" />
75                         <parameter name="destination" required="true" argsep=":">
76                                 <argument name="labeliftrue" hasparams="optional">
77                                         <para>Continue at <replaceable>labeliftrue</replaceable> if the condition is true.
78                                         Takes the form similar to Goto() of [[context,]extension,]priority.</para>
79                                         <argument name="arg1" required="true" multiple="true" />
80                                         <argument name="argN" />
81                                 </argument>
82                                 <argument name="labeliffalse" hasparams="optional">
83                                         <para>Continue at <replaceable>labeliffalse</replaceable> if the condition is false.
84                                         Takes the form similar to Goto() of [[context,]extension,]priority.</para>
85                                         <argument name="arg1" required="true" multiple="true" />
86                                         <argument name="argN" />
87                                 </argument>
88                         </parameter>
89                 </syntax>
90                 <description>
91                         <para>If the condition is true, then jump to labeliftrue.  If false, jumps to
92                         labeliffalse, if specified.  In either case, a jump saves the return point
93                         in the dialplan, to be returned to with a Return.</para>
94                 </description>
95                 <see-also>
96                         <ref type="application">Gosub</ref>
97                         <ref type="application">Return</ref>
98                         <ref type="application">MacroIf</ref>
99                         <ref type="function">IF</ref>
100                         <ref type="application">GotoIf</ref>
101                         <ref type="application">Goto</ref>
102                 </see-also>
103         </application>
104         <application name="Return" language="en_US">
105                 <synopsis>
106                         Return from gosub routine.
107                 </synopsis>
108                 <syntax>
109                         <parameter name="value">
110                                 <para>Return value.</para>
111                         </parameter>
112                 </syntax>
113                 <description>
114                         <para>Jumps to the last label on the stack, removing it. The return <replaceable>value</replaceable>, if
115                         any, is saved in the channel variable <variable>GOSUB_RETVAL</variable>.</para>
116                 </description>
117                 <see-also>
118                         <ref type="application">Gosub</ref>
119                         <ref type="application">StackPop</ref>
120                 </see-also>
121         </application>
122         <application name="StackPop" language="en_US">
123                 <synopsis>
124                         Remove one address from gosub stack.
125                 </synopsis>
126                 <syntax />
127                 <description>
128                         <para>Removes last label on the stack, discarding it.</para>
129                 </description>
130                 <see-also>
131                         <ref type="application">Return</ref>
132                         <ref type="application">Gosub</ref>
133                 </see-also>
134         </application>
135         <function name="LOCAL" language="en_US">
136                 <synopsis>
137                         Manage variables local to the gosub stack frame.
138                 </synopsis>
139                 <syntax>
140                         <parameter name="varname" required="true" />
141                 </syntax>
142                 <description>
143                         <para>Read and write a variable local to the gosub stack frame, once we Return() it will be lost
144                         (or it will go back to whatever value it had before the Gosub()).</para>
145                 </description>
146                 <see-also>
147                         <ref type="application">Gosub</ref>
148                         <ref type="application">GosubIf</ref>
149                         <ref type="application">Return</ref>
150                 </see-also>
151         </function>
152         <function name="LOCAL_PEEK" language="en_US">
153                 <synopsis>
154                         Retrieve variables hidden by the local gosub stack frame.
155                 </synopsis>
156                 <syntax>
157                         <parameter name="n" required="true" />
158                         <parameter name="varname" required="true" />
159                 </syntax>
160                 <description>
161                         <para>Read a variable <replaceable>varname</replaceable> hidden by
162                         <replaceable>n</replaceable> levels of gosub stack frames.  Note that ${LOCAL_PEEK(0,foo)}
163                         is the same as <variable>foo</variable>, since the value of <replaceable>n</replaceable>
164                         peeks under 0 levels of stack frames; in other words, 0 is the current level.  If
165                         <replaceable>n</replaceable> exceeds the available number of stack frames, then an empty
166                         string is returned.</para>
167                 </description>
168                 <see-also>
169                         <ref type="application">Gosub</ref>
170                         <ref type="application">GosubIf</ref>
171                         <ref type="application">Return</ref>
172                 </see-also>
173         </function>
174         <function name="STACK_PEEK" language="en_US">
175                 <synopsis>
176                         View info about the location which called Gosub
177                 </synopsis>
178                 <syntax>
179                         <parameter name="n" required="true" />
180                         <parameter name="which" required="true" />
181                         <parameter name="suppress" required="false" />
182                 </syntax>
183                 <description>
184                         <para>Read the calling <literal>c</literal>ontext, <literal>e</literal>xtension,
185                         <literal>p</literal>riority, or <literal>l</literal>abel, as specified by
186                         <replaceable>which</replaceable>, by going up <replaceable>n</replaceable> frames
187                         in the Gosub stack.  If <replaceable>suppress</replaceable> is true, then if the
188                         number of available stack frames is exceeded, then no error message will be
189                         printed.</para>
190                 </description>
191         </function>
192         <agi name="gosub" language="en_US">
193                 <synopsis>
194                         Cause the channel to execute the specified dialplan subroutine.
195                 </synopsis>
196                 <syntax>
197                         <parameter name="context" required="true" />
198                         <parameter name="extension" required="true" />
199                         <parameter name="priority" required="true" />
200                         <parameter name="optional-argument" />
201                 </syntax>
202                 <description>
203                         <para>Cause the channel to execute the specified dialplan subroutine,
204                         returning to the dialplan with execution of a Return().</para>
205                 </description>
206                 <see-also>
207                         <ref type="application">GoSub</ref>
208                 </see-also>
209         </agi>
210         <managerEvent language="en_US" name="VarSet">
211                 <managerEventInstance class="EVENT_FLAG_DIALPLAN">
212                         <synopsis>Raised when a variable local to the gosub stack frame is set due to a subroutine call.</synopsis>
213                         <syntax>
214                                 <xi:include xpointer="xpointer(/docs/managerEvent[@name='Newchannel']/managerEventInstance/syntax/parameter)" />
215                                 <parameter name="Variable">
216                                         <para>The LOCAL variable being set.</para>
217                                         <note><para>The variable name will always be enclosed with
218                                         <literal>LOCAL()</literal></para></note>
219                                 </parameter>
220                                 <parameter name="Value">
221                                         <para>The new value of the variable.</para>
222                                 </parameter>
223                         </syntax>
224                         <see-also>
225                                 <ref type="application">GoSub</ref>
226                                 <ref type="agi">gosub</ref>
227                                 <ref type="function">LOCAL</ref>
228                                 <ref type="function">LOCAL_PEEK</ref>
229                         </see-also>
230                 </managerEventInstance>
231         </managerEvent>
232  ***/
233
234 static const char app_gosub[] = "Gosub";
235 static const char app_gosubif[] = "GosubIf";
236 static const char app_return[] = "Return";
237 static const char app_pop[] = "StackPop";
238
239 static void gosub_free(void *data);
240
241 static const struct ast_datastore_info stack_info = {
242         .type = "GOSUB",
243         .destroy = gosub_free,
244 };
245
246 struct gosub_stack_frame {
247         AST_LIST_ENTRY(gosub_stack_frame) entries;
248         /* 100 arguments is all that we support anyway, but this will handle up to 255 */
249         unsigned char arguments;
250         struct varshead varshead;
251         int priority;
252         /*! TRUE if the return location marks the end of a special routine. */
253         unsigned int is_special:1;
254         char *context;
255         char extension[0];
256 };
257
258 AST_LIST_HEAD(gosub_stack_list, gosub_stack_frame);
259
260 static int frame_set_var(struct ast_channel *chan, struct gosub_stack_frame *frame, const char *var, const char *value)
261 {
262         struct ast_var_t *variables;
263         int found = 0;
264         int len;
265         RAII_VAR(char *, local_buffer, NULL, ast_free);
266
267         /* Does this variable already exist? */
268         AST_LIST_TRAVERSE(&frame->varshead, variables, entries) {
269                 if (!strcmp(var, ast_var_name(variables))) {
270                         found = 1;
271                         break;
272                 }
273         }
274
275         if (!found) {
276                 variables = ast_var_assign(var, "");
277                 AST_LIST_INSERT_HEAD(&frame->varshead, variables, entries);
278                 pbx_builtin_pushvar_helper(chan, var, value);
279         } else {
280                 pbx_builtin_setvar_helper(chan, var, value);
281         }
282
283         len = 8 + strlen(var); /* LOCAL() + var */
284         local_buffer = ast_malloc(len);
285         if (!local_buffer) {
286                 return 0;
287         }
288         sprintf(local_buffer, "LOCAL(%s)", var);
289         ast_channel_publish_varset(chan, local_buffer, value);
290         return 0;
291 }
292
293 static void gosub_release_frame(struct ast_channel *chan, struct gosub_stack_frame *frame)
294 {
295         struct ast_var_t *vardata;
296
297         /* If chan is not defined, then we're calling it as part of gosub_free,
298          * and the channel variables will be deallocated anyway.  Otherwise, we're
299          * just releasing a single frame, so we need to clean up the arguments for
300          * that frame, so that we re-expose the variables from the previous frame
301          * that were hidden by this one.
302          */
303         while ((vardata = AST_LIST_REMOVE_HEAD(&frame->varshead, entries))) {
304                 if (chan)
305                         pbx_builtin_setvar_helper(chan, ast_var_name(vardata), NULL);   
306                 ast_var_delete(vardata);
307         }
308
309         ast_free(frame);
310 }
311
312 static struct gosub_stack_frame *gosub_allocate_frame(const char *context, const char *extension, int priority, unsigned char arguments)
313 {
314         struct gosub_stack_frame *new = NULL;
315         int len_extension = strlen(extension), len_context = strlen(context);
316
317         if ((new = ast_calloc(1, sizeof(*new) + 2 + len_extension + len_context))) {
318                 AST_LIST_HEAD_INIT_NOLOCK(&new->varshead);
319                 strcpy(new->extension, extension);
320                 new->context = new->extension + len_extension + 1;
321                 strcpy(new->context, context);
322                 new->priority = priority;
323                 new->arguments = arguments;
324         }
325         return new;
326 }
327
328 static void gosub_free(void *data)
329 {
330         struct gosub_stack_list *oldlist = data;
331         struct gosub_stack_frame *oldframe;
332
333         AST_LIST_LOCK(oldlist);
334         while ((oldframe = AST_LIST_REMOVE_HEAD(oldlist, entries))) {
335                 gosub_release_frame(NULL, oldframe);
336         }
337         AST_LIST_UNLOCK(oldlist);
338         AST_LIST_HEAD_DESTROY(oldlist);
339         ast_free(oldlist);
340 }
341
342 static int pop_exec(struct ast_channel *chan, const char *data)
343 {
344         struct ast_datastore *stack_store;
345         struct gosub_stack_frame *oldframe;
346         struct gosub_stack_list *oldlist;
347         int res = 0;
348
349         ast_channel_lock(chan);
350         if (!(stack_store = ast_channel_datastore_find(chan, &stack_info, NULL))) {
351                 ast_log(LOG_WARNING, "%s called with no gosub stack allocated.\n", app_pop);
352                 ast_channel_unlock(chan);
353                 return 0;
354         }
355
356         oldlist = stack_store->data;
357         AST_LIST_LOCK(oldlist);
358         oldframe = AST_LIST_FIRST(oldlist);
359         if (oldframe) {
360                 if (oldframe->is_special) {
361                         ast_debug(1, "%s attempted to pop special return location.\n", app_pop);
362
363                         /* Abort the special routine dialplan execution.  Dialplan programming error. */
364                         res = -1;
365                 } else {
366                         AST_LIST_REMOVE_HEAD(oldlist, entries);
367                         gosub_release_frame(chan, oldframe);
368                 }
369         } else {
370                 ast_debug(1, "%s called with an empty gosub stack\n", app_pop);
371         }
372         AST_LIST_UNLOCK(oldlist);
373         ast_channel_unlock(chan);
374         return res;
375 }
376
377 static int return_exec(struct ast_channel *chan, const char *data)
378 {
379         struct ast_datastore *stack_store;
380         struct gosub_stack_frame *oldframe;
381         struct gosub_stack_list *oldlist;
382         const char *retval = data;
383         int res = 0;
384
385         ast_channel_lock(chan);
386         if (!(stack_store = ast_channel_datastore_find(chan, &stack_info, NULL))) {
387                 ast_log(LOG_ERROR, "Return without Gosub: stack is unallocated\n");
388                 ast_channel_unlock(chan);
389                 return -1;
390         }
391
392         oldlist = stack_store->data;
393         AST_LIST_LOCK(oldlist);
394         oldframe = AST_LIST_REMOVE_HEAD(oldlist, entries);
395         AST_LIST_UNLOCK(oldlist);
396
397         if (!oldframe) {
398                 ast_log(LOG_ERROR, "Return without Gosub: stack is empty\n");
399                 ast_channel_unlock(chan);
400                 return -1;
401         }
402         if (oldframe->is_special) {
403                 /* Exit from special routine. */
404                 res = -1;
405         }
406
407         /*
408          * We cannot use ast_explicit_goto() because we MUST restore
409          * what was there before.  Channels that do not have a PBX may
410          * not have the context or exten set.
411          */
412         ast_channel_context_set(chan, oldframe->context);
413         ast_channel_exten_set(chan, oldframe->extension);
414         if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_IN_AUTOLOOP)) {
415                 --oldframe->priority;
416         }
417         ast_channel_priority_set(chan, oldframe->priority);
418
419         gosub_release_frame(chan, oldframe);
420
421         /* Set a return value, if any */
422         pbx_builtin_setvar_helper(chan, "GOSUB_RETVAL", S_OR(retval, ""));
423         ast_channel_unlock(chan);
424         return res;
425 }
426
427 /*!
428  * \internal
429  * \brief Add missing context and/or exten to Gosub application argument string.
430  * \since 11.0
431  *
432  * \param chan Channel to obtain context/exten.
433  * \param args Gosub application argument string.
434  *
435  * \details
436  * Fills in the optional context and exten from the given channel.
437  * Convert: [[context,]exten,]priority[(arg1[,...][,argN])]
438  * To: context,exten,priority[(arg1[,...][,argN])]
439  *
440  * \retval expanded Gosub argument string on success.  Must be freed.
441  * \retval NULL on error.
442  *
443  * \note The parsing needs to be kept in sync with the
444  * gosub_exec() argument format.
445  */
446 static const char *expand_gosub_args(struct ast_channel *chan, const char *args)
447 {
448         int len;
449         char *parse;
450         char *label;
451         char *new_args;
452         const char *context;
453         const char *exten;
454         const char *pri;
455
456         /* Separate the context,exten,pri from the optional routine arguments. */
457         parse = ast_strdupa(args);
458         label = strsep(&parse, "(");
459         if (parse) {
460                 char *endparen;
461
462                 endparen = strrchr(parse, ')');
463                 if (endparen) {
464                         *endparen = '\0';
465                 } else {
466                         ast_log(LOG_WARNING, "Ouch.  No closing paren: '%s'?\n", args);
467                 }
468         }
469
470         /* Split context,exten,pri */
471         context = strsep(&label, ",");
472         exten = strsep(&label, ",");
473         pri = strsep(&label, ",");
474         if (!exten) {
475                 /* Only a priority in this one */
476                 pri = context;
477                 exten = NULL;
478                 context = NULL;
479         } else if (!pri) {
480                 /* Only an extension and priority in this one */
481                 pri = exten;
482                 exten = context;
483                 context = NULL;
484         }
485
486         ast_channel_lock(chan);
487         if (ast_strlen_zero(exten)) {
488                 exten = ast_channel_exten(chan);
489         }
490         if (ast_strlen_zero(context)) {
491                 context = ast_channel_context(chan);
492         }
493         len = strlen(context) + strlen(exten) + strlen(pri) + 3;
494         if (!ast_strlen_zero(parse)) {
495                 len += 2 + strlen(parse);
496         }
497         new_args = ast_malloc(len);
498         if (new_args) {
499                 if (ast_strlen_zero(parse)) {
500                         snprintf(new_args, len, "%s,%s,%s", context, exten, pri);
501                 } else {
502                         snprintf(new_args, len, "%s,%s,%s(%s)", context, exten, pri, parse);
503                 }
504         }
505         ast_channel_unlock(chan);
506
507         ast_debug(4, "Gosub args:%s new_args:%s\n", args, new_args ? new_args : "");
508
509         return new_args;
510 }
511
512 static int gosub_exec(struct ast_channel *chan, const char *data)
513 {
514         struct ast_datastore *stack_store;
515         struct gosub_stack_list *oldlist;
516         struct gosub_stack_frame *newframe;
517         struct gosub_stack_frame *lastframe;
518         char argname[15];
519         char *parse;
520         char *label;
521         char *caller_id;
522         char *orig_context;
523         char *orig_exten;
524         char *dest_context;
525         char *dest_exten;
526         int orig_priority;
527         int dest_priority;
528         int i;
529         int max_argc = 0;
530         AST_DECLARE_APP_ARGS(args2,
531                 AST_APP_ARG(argval)[100];
532         );
533
534         if (ast_strlen_zero(data)) {
535                 ast_log(LOG_ERROR, "%s requires an argument: %s([[context,]exten,]priority[(arg1[,...][,argN])])\n", app_gosub, app_gosub);
536                 return -1;
537         }
538
539         /*
540          * Separate the arguments from the label
541          *
542          * NOTE:  You cannot use ast_app_separate_args for this, because
543          * '(' cannot be used as a delimiter.
544          */
545         parse = ast_strdupa(data);
546         label = strsep(&parse, "(");
547         if (parse) {
548                 char *endparen;
549
550                 endparen = strrchr(parse, ')');
551                 if (endparen) {
552                         *endparen = '\0';
553                 } else {
554                         ast_log(LOG_WARNING, "Ouch.  No closing paren: '%s'?\n", data);
555                 }
556                 AST_STANDARD_RAW_ARGS(args2, parse);
557         } else {
558                 args2.argc = 0;
559         }
560
561         ast_channel_lock(chan);
562         orig_context = ast_strdupa(ast_channel_context(chan));
563         orig_exten = ast_strdupa(ast_channel_exten(chan));
564         orig_priority = ast_channel_priority(chan);
565         ast_channel_unlock(chan);
566
567         if (ast_parseable_goto(chan, label)) {
568                 ast_log(LOG_ERROR, "%s address is invalid: '%s'\n", app_gosub, data);
569                 goto error_exit;
570         }
571
572         ast_channel_lock(chan);
573         dest_context = ast_strdupa(ast_channel_context(chan));
574         dest_exten = ast_strdupa(ast_channel_exten(chan));
575         dest_priority = ast_channel_priority(chan);
576         if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_IN_AUTOLOOP)) {
577                 ++dest_priority;
578         }
579         caller_id = S_COR(ast_channel_caller(chan)->id.number.valid,
580                 ast_channel_caller(chan)->id.number.str, NULL);
581         if (caller_id) {
582                 caller_id = ast_strdupa(caller_id);
583         }
584         ast_channel_unlock(chan);
585
586         if (!ast_exists_extension(chan, dest_context, dest_exten, dest_priority, caller_id)) {
587                 ast_log(LOG_ERROR, "Attempt to reach a non-existent destination for %s: (Context:%s, Extension:%s, Priority:%d)\n",
588                         app_gosub, dest_context, dest_exten, dest_priority);
589                 goto error_exit;
590         }
591
592         /* Now we know that we're going to a new location */
593
594         ast_channel_lock(chan);
595
596         /* Find stack datastore return list. */
597         if (!(stack_store = ast_channel_datastore_find(chan, &stack_info, NULL))) {
598                 ast_debug(1, "Channel %s has no datastore, so we're allocating one.\n",
599                         ast_channel_name(chan));
600                 stack_store = ast_datastore_alloc(&stack_info, NULL);
601                 if (!stack_store) {
602                         ast_log(LOG_ERROR, "Unable to allocate new datastore.  %s failed.\n",
603                                 app_gosub);
604                         goto error_exit_locked;
605                 }
606
607                 oldlist = ast_calloc(1, sizeof(*oldlist));
608                 if (!oldlist) {
609                         ast_log(LOG_ERROR, "Unable to allocate datastore list head.  %s failed.\n",
610                                 app_gosub);
611                         ast_datastore_free(stack_store);
612                         goto error_exit_locked;
613                 }
614                 AST_LIST_HEAD_INIT(oldlist);
615
616                 stack_store->data = oldlist;
617                 ast_channel_datastore_add(chan, stack_store);
618         } else {
619                 oldlist = stack_store->data;
620         }
621
622         if ((lastframe = AST_LIST_FIRST(oldlist))) {
623                 max_argc = lastframe->arguments;
624         }
625
626         /* Mask out previous Gosub arguments in this invocation */
627         if (args2.argc > max_argc) {
628                 max_argc = args2.argc;
629         }
630
631         /* Create the return address */
632         newframe = gosub_allocate_frame(orig_context, orig_exten, orig_priority + 1, max_argc);
633         if (!newframe) {
634                 goto error_exit_locked;
635         }
636
637         /* Set our arguments */
638         for (i = 0; i < max_argc; i++) {
639                 snprintf(argname, sizeof(argname), "ARG%d", i + 1);
640                 frame_set_var(chan, newframe, argname, i < args2.argc ? args2.argval[i] : "");
641                 ast_debug(1, "Setting '%s' to '%s'\n", argname, i < args2.argc ? args2.argval[i] : "");
642         }
643         snprintf(argname, sizeof(argname), "%d", args2.argc);
644         frame_set_var(chan, newframe, "ARGC", argname);
645
646         /* And finally, save our return address */
647         AST_LIST_LOCK(oldlist);
648         AST_LIST_INSERT_HEAD(oldlist, newframe, entries);
649         AST_LIST_UNLOCK(oldlist);
650         ast_channel_unlock(chan);
651
652         return 0;
653
654 error_exit:
655         ast_channel_lock(chan);
656
657 error_exit_locked:
658         /* Restore the original dialplan location. */
659         ast_channel_context_set(chan, orig_context);
660         ast_channel_exten_set(chan, orig_exten);
661         ast_channel_priority_set(chan, orig_priority);
662         ast_channel_unlock(chan);
663         return -1;
664 }
665
666 static int gosubif_exec(struct ast_channel *chan, const char *data)
667 {
668         char *args;
669         int res=0;
670         AST_DECLARE_APP_ARGS(cond,
671                 AST_APP_ARG(ition);
672                 AST_APP_ARG(labels);
673         );
674         AST_DECLARE_APP_ARGS(label,
675                 AST_APP_ARG(iftrue);
676                 AST_APP_ARG(iffalse);
677         );
678
679         if (ast_strlen_zero(data)) {
680                 ast_log(LOG_WARNING, "GosubIf requires an argument: GosubIf(cond?label1(args):label2(args)\n");
681                 return 0;
682         }
683
684         args = ast_strdupa(data);
685         AST_NONSTANDARD_RAW_ARGS(cond, args, '?');
686         if (cond.argc != 2) {
687                 ast_log(LOG_WARNING, "GosubIf requires an argument: GosubIf(cond?label1(args):label2(args)\n");
688                 return 0;
689         }
690
691         AST_NONSTANDARD_RAW_ARGS(label, cond.labels, ':');
692
693         if (pbx_checkcondition(cond.ition)) {
694                 if (!ast_strlen_zero(label.iftrue))
695                         res = gosub_exec(chan, label.iftrue);
696         } else if (!ast_strlen_zero(label.iffalse)) {
697                 res = gosub_exec(chan, label.iffalse);
698         }
699
700         return res;
701 }
702
703 static int local_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
704 {
705         struct ast_datastore *stack_store;
706         struct gosub_stack_list *oldlist;
707         struct gosub_stack_frame *frame;
708         struct ast_var_t *variables;
709
710         ast_channel_lock(chan);
711         if (!(stack_store = ast_channel_datastore_find(chan, &stack_info, NULL))) {
712                 ast_channel_unlock(chan);
713                 return -1;
714         }
715
716         oldlist = stack_store->data;
717         AST_LIST_LOCK(oldlist);
718         if (!(frame = AST_LIST_FIRST(oldlist))) {
719                 /* Not within a Gosub routine */
720                 AST_LIST_UNLOCK(oldlist);
721                 ast_channel_unlock(chan);
722                 return -1;
723         }
724
725         AST_LIST_TRAVERSE(&frame->varshead, variables, entries) {
726                 if (!strcmp(data, ast_var_name(variables))) {
727                         const char *tmp;
728                         tmp = pbx_builtin_getvar_helper(chan, data);
729                         ast_copy_string(buf, S_OR(tmp, ""), len);
730                         break;
731                 }
732         }
733         AST_LIST_UNLOCK(oldlist);
734         ast_channel_unlock(chan);
735         return 0;
736 }
737
738 static int local_write(struct ast_channel *chan, const char *cmd, char *var, const char *value)
739 {
740         struct ast_datastore *stack_store;
741         struct gosub_stack_list *oldlist;
742         struct gosub_stack_frame *frame;
743
744         ast_channel_lock(chan);
745         if (!(stack_store = ast_channel_datastore_find(chan, &stack_info, NULL))) {
746                 ast_log(LOG_ERROR, "Tried to set LOCAL(%s), but we aren't within a Gosub routine\n", var);
747                 ast_channel_unlock(chan);
748                 return -1;
749         }
750
751         oldlist = stack_store->data;
752         AST_LIST_LOCK(oldlist);
753         frame = AST_LIST_FIRST(oldlist);
754
755         if (frame) {
756                 frame_set_var(chan, frame, var, value);
757         }
758
759         AST_LIST_UNLOCK(oldlist);
760         ast_channel_unlock(chan);
761
762         return 0;
763 }
764
765 static struct ast_custom_function local_function = {
766         .name = "LOCAL",
767         .write = local_write,
768         .read = local_read,
769 };
770
771 static int peek_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
772 {
773         int found = 0, n;
774         struct ast_var_t *variables;
775         AST_DECLARE_APP_ARGS(args,
776                 AST_APP_ARG(n);
777                 AST_APP_ARG(name);
778         );
779
780         if (!chan) {
781                 ast_log(LOG_ERROR, "LOCAL_PEEK must be called on an active channel\n");
782                 return -1;
783         }
784
785         AST_STANDARD_RAW_ARGS(args, data);
786         n = atoi(args.n);
787         *buf = '\0';
788
789         ast_channel_lock(chan);
790         AST_LIST_TRAVERSE(ast_channel_varshead(chan), variables, entries) {
791                 if (!strcmp(args.name, ast_var_name(variables)) && ++found > n) {
792                         ast_copy_string(buf, ast_var_value(variables), len);
793                         break;
794                 }
795         }
796         ast_channel_unlock(chan);
797         return 0;
798 }
799
800 static struct ast_custom_function peek_function = {
801         .name = "LOCAL_PEEK",
802         .read = peek_read,
803 };
804
805 static int stackpeek_read(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **str, ssize_t len)
806 {
807         struct ast_datastore *stack_store;
808         struct gosub_stack_list *oldlist;
809         struct gosub_stack_frame *frame;
810         int n;
811         AST_DECLARE_APP_ARGS(args,
812                 AST_APP_ARG(n);
813                 AST_APP_ARG(which);
814                 AST_APP_ARG(suppress);
815         );
816
817         if (!chan) {
818                 ast_log(LOG_ERROR, "STACK_PEEK must be called on an active channel\n");
819                 return -1;
820         }
821
822         data = ast_strdupa(data);
823         AST_STANDARD_APP_ARGS(args, data);
824
825         n = atoi(args.n);
826         if (n <= 0) {
827                 ast_log(LOG_ERROR, "STACK_PEEK must be called with a positive peek value\n");
828                 return -1;
829         }
830
831         ast_channel_lock(chan);
832         if (!(stack_store = ast_channel_datastore_find(chan, &stack_info, NULL))) {
833                 if (!ast_true(args.suppress)) {
834                         ast_log(LOG_ERROR, "STACK_PEEK called on a channel without a gosub stack\n");
835                 }
836                 ast_channel_unlock(chan);
837                 return -1;
838         }
839
840         oldlist = stack_store->data;
841
842         AST_LIST_LOCK(oldlist);
843         AST_LIST_TRAVERSE(oldlist, frame, entries) {
844                 if (--n == 0) {
845                         break;
846                 }
847         }
848
849         if (!frame) {
850                 /* Too deep */
851                 if (!ast_true(args.suppress)) {
852                         ast_log(LOG_ERROR, "Stack peek of '%s' is more stack frames than I have\n", args.n);
853                 }
854                 ast_channel_unlock(chan);
855                 return -1;
856         }
857
858         args.which = ast_skip_blanks(args.which);
859
860         switch (args.which[0]) {
861         case 'l': /* label */
862                 ast_str_set(str, len, "%s,%s,%d", frame->context, frame->extension, frame->priority - 1);
863                 break;
864         case 'c': /* context */
865                 ast_str_set(str, len, "%s", frame->context);
866                 break;
867         case 'e': /* extension */
868                 ast_str_set(str, len, "%s", frame->extension);
869                 break;
870         case 'p': /* priority */
871                 ast_str_set(str, len, "%d", frame->priority - 1);
872                 break;
873         default:
874                 ast_log(LOG_ERROR, "Unknown argument '%s' to STACK_PEEK\n", args.which);
875                 break;
876         }
877
878         AST_LIST_UNLOCK(oldlist);
879         ast_channel_unlock(chan);
880
881         return 0;
882 }
883
884 static struct ast_custom_function stackpeek_function = {
885         .name = "STACK_PEEK",
886         .read2 = stackpeek_read,
887 };
888
889 /*!
890  * \internal
891  * \brief Pop stack frames until remove a special return location.
892  * \since 11.0
893  *
894  * \param chan Channel to balance stack on.
895  *
896  * \note The channel is already locked when called.
897  *
898  * \return Nothing
899  */
900 static void balance_stack(struct ast_channel *chan)
901 {
902         struct ast_datastore *stack_store;
903         struct gosub_stack_list *oldlist;
904         struct gosub_stack_frame *oldframe;
905         int found;
906
907         stack_store = ast_channel_datastore_find(chan, &stack_info, NULL);
908         if (!stack_store) {
909                 ast_log(LOG_WARNING, "No %s stack allocated.\n", app_gosub);
910                 return;
911         }
912
913         oldlist = stack_store->data;
914         AST_LIST_LOCK(oldlist);
915         do {
916                 oldframe = AST_LIST_REMOVE_HEAD(oldlist, entries);
917                 if (!oldframe) {
918                         break;
919                 }
920                 found = oldframe->is_special;
921                 gosub_release_frame(chan, oldframe);
922         } while (!found);
923         AST_LIST_UNLOCK(oldlist);
924 }
925
926 /*!
927  * \internal
928  * \brief Run a subroutine on a channel.
929  * \since 11.0
930  *
931  * \note Absolutely _NO_ channel locks should be held before calling this function.
932  *
933  * \param chan Channel to execute subroutine on.
934  * \param sub_args Gosub application argument string.
935  * \param ignore_hangup TRUE if a hangup does not stop execution of the routine.
936  *
937  * \retval 0 success
938  * \retval -1 on error
939  */
940 static int gosub_run(struct ast_channel *chan, const char *sub_args, int ignore_hangup)
941 {
942         const char *saved_context;
943         const char *saved_exten;
944         int saved_priority;
945         int saved_hangup_flags;
946         int saved_autoloopflag;
947         int res;
948
949         ast_channel_lock(chan);
950
951         ast_verb(3, "%s Internal %s(%s) start\n",
952                 ast_channel_name(chan), app_gosub, sub_args);
953
954         /* Save non-hangup softhangup flags. */
955         saved_hangup_flags = ast_channel_softhangup_internal_flag(chan)
956                 & (AST_SOFTHANGUP_ASYNCGOTO | AST_SOFTHANGUP_UNBRIDGE);
957         if (saved_hangup_flags) {
958                 ast_channel_clear_softhangup(chan,
959                         AST_SOFTHANGUP_ASYNCGOTO | AST_SOFTHANGUP_UNBRIDGE);
960         }
961
962         /* Save autoloop flag */
963         saved_autoloopflag = ast_test_flag(ast_channel_flags(chan), AST_FLAG_IN_AUTOLOOP);
964         ast_set_flag(ast_channel_flags(chan), AST_FLAG_IN_AUTOLOOP);
965
966         /* Save current dialplan location */
967         saved_context = ast_strdupa(ast_channel_context(chan));
968         saved_exten = ast_strdupa(ast_channel_exten(chan));
969         saved_priority = ast_channel_priority(chan);
970
971         ast_debug(4, "%s Original location: %s,%s,%d\n", ast_channel_name(chan),
972                 saved_context, saved_exten, saved_priority);
973
974         ast_channel_unlock(chan);
975         res = gosub_exec(chan, sub_args);
976         ast_debug(4, "%s exited with status %d\n", app_gosub, res);
977         ast_channel_lock(chan);
978         if (!res) {
979                 struct ast_datastore *stack_store;
980
981                 /* Mark the return location as special. */
982                 stack_store = ast_channel_datastore_find(chan, &stack_info, NULL);
983                 if (!stack_store) {
984                         /* Should never happen! */
985                         ast_log(LOG_ERROR, "No %s stack!\n", app_gosub);
986                         res = -1;
987                 } else {
988                         struct gosub_stack_list *oldlist;
989                         struct gosub_stack_frame *cur;
990
991                         oldlist = stack_store->data;
992                         cur = AST_LIST_FIRST(oldlist);
993                         cur->is_special = 1;
994                 }
995         }
996         if (!res) {
997                 int found = 0;  /* set if we find at least one match */
998
999                 /*
1000                  * Run gosub body autoloop.
1001                  *
1002                  * Note that this loop is inverted from the normal execution
1003                  * loop because we just executed the Gosub application as the
1004                  * first extension of the autoloop.
1005                  */
1006                 do {
1007                         /* Check for hangup. */
1008                         if (ast_channel_softhangup_internal_flag(chan) & AST_SOFTHANGUP_UNBRIDGE) {
1009                                 saved_hangup_flags |= AST_SOFTHANGUP_UNBRIDGE;
1010                                 ast_channel_clear_softhangup(chan, AST_SOFTHANGUP_UNBRIDGE);
1011                         }
1012                         if (ast_check_hangup(chan)) {
1013                                 if (ast_channel_softhangup_internal_flag(chan) & AST_SOFTHANGUP_ASYNCGOTO) {
1014                                         ast_log(LOG_ERROR, "%s An async goto just messed up our execution location.\n",
1015                                                 ast_channel_name(chan));
1016                                         break;
1017                                 }
1018                                 if (!ignore_hangup) {
1019                                         break;
1020                                 }
1021                         }
1022
1023                         /* Next dialplan priority. */
1024                         ast_channel_priority_set(chan, ast_channel_priority(chan) + 1);
1025
1026                         ast_channel_unlock(chan);
1027                         res = ast_spawn_extension(chan, ast_channel_context(chan),
1028                                 ast_channel_exten(chan), ast_channel_priority(chan),
1029                                 S_COR(ast_channel_caller(chan)->id.number.valid,
1030                                         ast_channel_caller(chan)->id.number.str, NULL),
1031                                 &found, 1);
1032                         ast_channel_lock(chan);
1033                 } while (!res);
1034                 if (found && res) {
1035                         /* Something bad happened, or a hangup has been requested. */
1036                         ast_debug(1, "Spawn extension (%s,%s,%d) exited with %d on '%s'\n",
1037                                 ast_channel_context(chan), ast_channel_exten(chan),
1038                                 ast_channel_priority(chan), res, ast_channel_name(chan));
1039                         ast_verb(2, "Spawn extension (%s, %s, %d) exited non-zero on '%s'\n",
1040                                 ast_channel_context(chan), ast_channel_exten(chan),
1041                                 ast_channel_priority(chan), ast_channel_name(chan));
1042                 }
1043
1044                 /* Did the routine return? */
1045                 if (ast_channel_priority(chan) == saved_priority
1046                         && !strcmp(ast_channel_context(chan), saved_context)
1047                         && !strcmp(ast_channel_exten(chan), saved_exten)) {
1048                         ast_verb(3, "%s Internal %s(%s) complete GOSUB_RETVAL=%s\n",
1049                                 ast_channel_name(chan), app_gosub, sub_args,
1050                                 S_OR(pbx_builtin_getvar_helper(chan, "GOSUB_RETVAL"), ""));
1051                 } else {
1052                         ast_log(LOG_NOTICE, "%s Abnormal '%s(%s)' exit.  Popping routine return locations.\n",
1053                                 ast_channel_name(chan), app_gosub, sub_args);
1054                         balance_stack(chan);
1055                         pbx_builtin_setvar_helper(chan, "GOSUB_RETVAL", "");
1056                 }
1057
1058                 /* We executed the requested subroutine to the best of our ability. */
1059                 res = 0;
1060         }
1061
1062         ast_debug(4, "%s Ending location: %s,%s,%d\n", ast_channel_name(chan),
1063                 ast_channel_context(chan), ast_channel_exten(chan),
1064                 ast_channel_priority(chan));
1065
1066         /* Restore dialplan location */
1067         if (!(ast_channel_softhangup_internal_flag(chan) & AST_SOFTHANGUP_ASYNCGOTO)) {
1068                 ast_channel_context_set(chan, saved_context);
1069                 ast_channel_exten_set(chan, saved_exten);
1070                 ast_channel_priority_set(chan, saved_priority);
1071         }
1072
1073         /* Restore autoloop flag */
1074         ast_set2_flag(ast_channel_flags(chan), saved_autoloopflag, AST_FLAG_IN_AUTOLOOP);
1075
1076         /* Restore non-hangup softhangup flags. */
1077         if (saved_hangup_flags) {
1078                 ast_softhangup_nolock(chan, saved_hangup_flags);
1079         }
1080
1081         ast_channel_unlock(chan);
1082
1083         return res;
1084 }
1085
1086 static int handle_gosub(struct ast_channel *chan, AGI *agi, int argc, const char * const *argv)
1087 {
1088         int res;
1089         int priority;
1090         int old_autoloopflag;
1091         int old_priority;
1092         const char *old_context;
1093         const char *old_extension;
1094         char *gosub_args;
1095
1096         if (argc < 4 || argc > 5) {
1097                 return RESULT_SHOWUSAGE;
1098         }
1099
1100         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] : "");
1101
1102         if (sscanf(argv[3], "%30d", &priority) != 1 || priority < 1) {
1103                 /* Lookup the priority label */
1104                 priority = ast_findlabel_extension(chan, argv[1], argv[2], argv[3],
1105                         S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL));
1106                 if (priority < 0) {
1107                         ast_log(LOG_ERROR, "Priority '%s' not found in '%s@%s'\n", argv[3], argv[2], argv[1]);
1108                         ast_agi_send(agi->fd, chan, "200 result=-1 Gosub label not found\n");
1109                         return RESULT_FAILURE;
1110                 }
1111         } else if (!ast_exists_extension(chan, argv[1], argv[2], priority,
1112                 S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL))) {
1113                 ast_agi_send(agi->fd, chan, "200 result=-1 Gosub label not found\n");
1114                 return RESULT_FAILURE;
1115         }
1116
1117         if (argc == 5) {
1118                 if (ast_asprintf(&gosub_args, "%s,%s,%d(%s)", argv[1], argv[2], priority, argv[4]) < 0) {
1119                         gosub_args = NULL;
1120                 }
1121         } else {
1122                 if (ast_asprintf(&gosub_args, "%s,%s,%d", argv[1], argv[2], priority) < 0) {
1123                         gosub_args = NULL;
1124                 }
1125         }
1126         if (!gosub_args) {
1127                 ast_agi_send(agi->fd, chan, "503 result=-2 Memory allocation failure\n");
1128                 return RESULT_FAILURE;
1129         }
1130
1131         ast_channel_lock(chan);
1132
1133         ast_verb(3, "%s AGI %s(%s) start\n", ast_channel_name(chan), app_gosub, gosub_args);
1134
1135         /* Save autoloop flag */
1136         old_autoloopflag = ast_test_flag(ast_channel_flags(chan), AST_FLAG_IN_AUTOLOOP);
1137         ast_set_flag(ast_channel_flags(chan), AST_FLAG_IN_AUTOLOOP);
1138
1139         /* Save previous location, since we're going to change it */
1140         old_context = ast_strdupa(ast_channel_context(chan));
1141         old_extension = ast_strdupa(ast_channel_exten(chan));
1142         old_priority = ast_channel_priority(chan);
1143
1144         ast_debug(4, "%s Original location: %s,%s,%d\n", ast_channel_name(chan),
1145                 old_context, old_extension, old_priority);
1146         ast_channel_unlock(chan);
1147
1148         res = gosub_exec(chan, gosub_args);
1149         if (!res) {
1150                 struct ast_datastore *stack_store;
1151
1152                 /* Mark the return location as special. */
1153                 ast_channel_lock(chan);
1154                 stack_store = ast_channel_datastore_find(chan, &stack_info, NULL);
1155                 if (!stack_store) {
1156                         /* Should never happen! */
1157                         ast_log(LOG_ERROR, "No %s stack!\n", app_gosub);
1158                         res = -1;
1159                 } else {
1160                         struct gosub_stack_list *oldlist;
1161                         struct gosub_stack_frame *cur;
1162
1163                         oldlist = stack_store->data;
1164                         cur = AST_LIST_FIRST(oldlist);
1165                         cur->is_special = 1;
1166                 }
1167                 ast_channel_unlock(chan);
1168         }
1169         if (!res) {
1170                 struct ast_pbx *pbx;
1171                 struct ast_pbx_args args;
1172                 int abnormal_exit;
1173
1174                 memset(&args, 0, sizeof(args));
1175                 args.no_hangup_chan = 1;
1176
1177                 ast_channel_lock(chan);
1178
1179                 /* Next dialplan priority. */
1180                 ast_channel_priority_set(chan, ast_channel_priority(chan) + 1);
1181
1182                 /* Suppress warning about PBX already existing */
1183                 pbx = ast_channel_pbx(chan);
1184                 ast_channel_pbx_set(chan, NULL);
1185                 ast_channel_unlock(chan);
1186
1187                 ast_agi_send(agi->fd, chan, "100 result=0 Trying...\n");
1188                 ast_pbx_run_args(chan, &args);
1189
1190                 ast_channel_lock(chan);
1191                 ast_free(ast_channel_pbx(chan));
1192                 ast_channel_pbx_set(chan, pbx);
1193
1194                 /* Did the routine return? */
1195                 if (ast_channel_priority(chan) == old_priority
1196                         && !strcmp(ast_channel_context(chan), old_context)
1197                         && !strcmp(ast_channel_exten(chan), old_extension)) {
1198                         ast_verb(3, "%s AGI %s(%s) complete GOSUB_RETVAL=%s\n",
1199                                 ast_channel_name(chan), app_gosub, gosub_args,
1200                                 S_OR(pbx_builtin_getvar_helper(chan, "GOSUB_RETVAL"), ""));
1201                         abnormal_exit = 0;
1202                 } else {
1203                         ast_log(LOG_NOTICE, "%s Abnormal AGI %s(%s) exit.  Popping routine return locations.\n",
1204                                 ast_channel_name(chan), app_gosub, gosub_args);
1205                         balance_stack(chan);
1206                         pbx_builtin_setvar_helper(chan, "GOSUB_RETVAL", "");
1207                         abnormal_exit = 1;
1208                 }
1209                 ast_channel_unlock(chan);
1210
1211                 ast_agi_send(agi->fd, chan, "200 result=0 Gosub complete%s\n",
1212                         abnormal_exit ? " (abnormal exit)" : "");
1213         } else {
1214                 ast_agi_send(agi->fd, chan, "200 result=%d Gosub failed\n", res);
1215         }
1216
1217         /* Must use free because the memory was allocated by asprintf(). */
1218         free(gosub_args);
1219
1220         ast_channel_lock(chan);
1221         ast_debug(4, "%s Ending location: %s,%s,%d\n", ast_channel_name(chan),
1222                 ast_channel_context(chan), ast_channel_exten(chan),
1223                 ast_channel_priority(chan));
1224
1225         /* Restore previous location */
1226         ast_channel_context_set(chan, old_context);
1227         ast_channel_exten_set(chan, old_extension);
1228         ast_channel_priority_set(chan, old_priority);
1229
1230         /* Restore autoloop flag */
1231         ast_set2_flag(ast_channel_flags(chan), old_autoloopflag, AST_FLAG_IN_AUTOLOOP);
1232         ast_channel_unlock(chan);
1233
1234         return RESULT_SUCCESS;
1235 }
1236
1237 static struct agi_command gosub_agi_command =
1238         { { "gosub", NULL }, handle_gosub, NULL, NULL, 0 };
1239
1240 static int unload_module(void)
1241 {
1242         ast_install_stack_functions(NULL);
1243
1244         ast_agi_unregister(ast_module_info->self, &gosub_agi_command);
1245
1246         ast_unregister_application(app_return);
1247         ast_unregister_application(app_pop);
1248         ast_unregister_application(app_gosubif);
1249         ast_unregister_application(app_gosub);
1250         ast_custom_function_unregister(&local_function);
1251         ast_custom_function_unregister(&peek_function);
1252         ast_custom_function_unregister(&stackpeek_function);
1253
1254         return 0;
1255 }
1256
1257 static int load_module(void)
1258 {
1259         /* Setup the stack application callback functions. */
1260         static struct ast_app_stack_funcs funcs = {
1261                 .run_sub = gosub_run,
1262                 .expand_sub_args = expand_gosub_args,
1263         };
1264
1265         ast_agi_register(ast_module_info->self, &gosub_agi_command);
1266
1267         ast_register_application_xml(app_pop, pop_exec);
1268         ast_register_application_xml(app_return, return_exec);
1269         ast_register_application_xml(app_gosubif, gosubif_exec);
1270         ast_register_application_xml(app_gosub, gosub_exec);
1271         ast_custom_function_register(&local_function);
1272         ast_custom_function_register(&peek_function);
1273         ast_custom_function_register(&stackpeek_function);
1274
1275         funcs.module = ast_module_info->self,
1276         ast_install_stack_functions(&funcs);
1277
1278         return 0;
1279 }
1280
1281 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT | AST_MODFLAG_LOAD_ORDER, "Dialplan subroutines (Gosub, Return, etc)",
1282                 .load = load_module,
1283                 .unload = unload_module,
1284                 .load_pri = AST_MODPRI_APP_DEPEND,
1285                 .nonoptreq = "res_agi",
1286                 );