Newer
Older
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
/* Next dialplan priority. */
ast_channel_priority_set(chan, ast_channel_priority(chan) + 1);
ast_channel_unlock(chan);
res = ast_spawn_extension(chan, ast_channel_context(chan),
ast_channel_exten(chan), ast_channel_priority(chan),
S_COR(ast_channel_caller(chan)->id.number.valid,
ast_channel_caller(chan)->id.number.str, NULL),
&found, 1);
ast_channel_lock(chan);
} while (!res);
if (found && res) {
/* Something bad happened, or a hangup has been requested. */
ast_debug(1, "Spawn extension (%s,%s,%d) exited with %d on '%s'\n",
ast_channel_context(chan), ast_channel_exten(chan),
ast_channel_priority(chan), res, ast_channel_name(chan));
ast_verb(2, "Spawn extension (%s, %s, %d) exited non-zero on '%s'\n",
ast_channel_context(chan), ast_channel_exten(chan),
ast_channel_priority(chan), ast_channel_name(chan));
}
/* Did the routine return? */
if (ast_channel_priority(chan) == saved_priority
&& !strcmp(ast_channel_context(chan), saved_context)
&& !strcmp(ast_channel_exten(chan), saved_exten)) {
ast_verb(3, "%s Internal %s(%s) complete GOSUB_RETVAL=%s\n",
ast_channel_name(chan), app_gosub, sub_args,
S_OR(pbx_builtin_getvar_helper(chan, "GOSUB_RETVAL"), ""));
} else {
ast_log(LOG_NOTICE, "%s Abnormal '%s(%s)' exit. Popping routine return locations.\n",
ast_channel_name(chan), app_gosub, sub_args);
balance_stack(chan);
pbx_builtin_setvar_helper(chan, "GOSUB_RETVAL", "");
}
/* We executed the requested subroutine to the best of our ability. */
res = 0;
}
ast_debug(4, "%s Ending location: %s,%s,%d\n", ast_channel_name(chan),
ast_channel_context(chan), ast_channel_exten(chan),
ast_channel_priority(chan));
/* Restore dialplan location */
if (!(ast_channel_softhangup_internal_flag(chan) & AST_SOFTHANGUP_ASYNCGOTO)) {
ast_channel_context_set(chan, saved_context);
ast_channel_exten_set(chan, saved_exten);
ast_channel_priority_set(chan, saved_priority);
}
/* Restore autoloop flag */
ast_set2_flag(ast_channel_flags(chan), saved_autoloopflag, AST_FLAG_IN_AUTOLOOP);
/* Restore non-hangup softhangup flags. */
if (saved_hangup_flags) {
ast_softhangup_nolock(chan, saved_hangup_flags);
}
ast_channel_unlock(chan);
return res;
}
static int handle_gosub(struct ast_channel *chan, AGI *agi, int argc, const char * const *argv)
{
int res;
int priority;
int old_autoloopflag;
int old_priority;
const char *old_context;
const char *old_extension;
char *gosub_args;
if (argc < 4 || argc > 5) {
return RESULT_SHOWUSAGE;
}
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] : "");
if (sscanf(argv[3], "%30d", &priority) != 1 || priority < 1) {
/* Lookup the priority label */
priority = ast_findlabel_extension(chan, argv[1], argv[2], argv[3],
S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL));
ast_log(LOG_ERROR, "Priority '%s' not found in '%s@%s'\n", argv[3], argv[2], argv[1]);
ast_agi_send(agi->fd, chan, "200 result=-1 Gosub label not found\n");
return RESULT_FAILURE;
}
} else if (!ast_exists_extension(chan, argv[1], argv[2], priority,
S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL))) {
ast_agi_send(agi->fd, chan, "200 result=-1 Gosub label not found\n");
return RESULT_FAILURE;
}
if (argc == 5) {
if (asprintf(&gosub_args, "%s,%s,%d(%s)", argv[1], argv[2], priority, argv[4]) < 0) {
Kevin P. Fleming
committed
ast_log(LOG_WARNING, "asprintf() failed: %s\n", strerror(errno));
gosub_args = NULL;
}
} else {
if (asprintf(&gosub_args, "%s,%s,%d", argv[1], argv[2], priority) < 0) {
Kevin P. Fleming
committed
ast_log(LOG_WARNING, "asprintf() failed: %s\n", strerror(errno));
gosub_args = NULL;
}
}
if (!gosub_args) {
ast_agi_send(agi->fd, chan, "503 result=-2 Memory allocation failure\n");
return RESULT_FAILURE;
}
ast_channel_lock(chan);
ast_verb(3, "%s AGI %s(%s) start\n", ast_channel_name(chan), app_gosub, gosub_args);
/* Save autoloop flag */
old_autoloopflag = ast_test_flag(ast_channel_flags(chan), AST_FLAG_IN_AUTOLOOP);
ast_set_flag(ast_channel_flags(chan), AST_FLAG_IN_AUTOLOOP);
/* Save previous location, since we're going to change it */
old_context = ast_strdupa(ast_channel_context(chan));
old_extension = ast_strdupa(ast_channel_exten(chan));
old_priority = ast_channel_priority(chan);
ast_debug(4, "%s Original location: %s,%s,%d\n", ast_channel_name(chan),
old_context, old_extension, old_priority);
ast_channel_unlock(chan);
res = gosub_exec(chan, gosub_args);
if (!res) {
struct ast_datastore *stack_store;
/* Mark the return location as special. */
ast_channel_lock(chan);
stack_store = ast_channel_datastore_find(chan, &stack_info, NULL);
if (!stack_store) {
/* Should never happen! */
ast_log(LOG_ERROR, "No %s stack!\n", app_gosub);
res = -1;
} else {
struct gosub_stack_list *oldlist;
struct gosub_stack_frame *cur;
oldlist = stack_store->data;
cur = AST_LIST_FIRST(oldlist);
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
cur->is_special = 1;
}
ast_channel_unlock(chan);
}
if (!res) {
struct ast_pbx *pbx;
struct ast_pbx_args args;
int abnormal_exit;
memset(&args, 0, sizeof(args));
args.no_hangup_chan = 1;
ast_channel_lock(chan);
/* Next dialplan priority. */
ast_channel_priority_set(chan, ast_channel_priority(chan) + 1);
/* Suppress warning about PBX already existing */
pbx = ast_channel_pbx(chan);
ast_channel_pbx_set(chan, NULL);
ast_channel_unlock(chan);
ast_agi_send(agi->fd, chan, "100 result=0 Trying...\n");
ast_pbx_run_args(chan, &args);
ast_channel_lock(chan);
ast_free(ast_channel_pbx(chan));
ast_channel_pbx_set(chan, pbx);
/* Did the routine return? */
if (ast_channel_priority(chan) == old_priority
&& !strcmp(ast_channel_context(chan), old_context)
&& !strcmp(ast_channel_exten(chan), old_extension)) {
ast_verb(3, "%s AGI %s(%s) complete GOSUB_RETVAL=%s\n",
ast_channel_name(chan), app_gosub, gosub_args,
S_OR(pbx_builtin_getvar_helper(chan, "GOSUB_RETVAL"), ""));
abnormal_exit = 0;
} else {
ast_log(LOG_NOTICE, "%s Abnormal AGI %s(%s) exit. Popping routine return locations.\n",
ast_channel_name(chan), app_gosub, gosub_args);
balance_stack(chan);
pbx_builtin_setvar_helper(chan, "GOSUB_RETVAL", "");
abnormal_exit = 1;
}
ast_channel_unlock(chan);
ast_agi_send(agi->fd, chan, "200 result=0 Gosub complete%s\n",
abnormal_exit ? " (abnormal exit)" : "");
} else {
ast_agi_send(agi->fd, chan, "200 result=%d Gosub failed\n", res);
}
/* Must use free because the memory was allocated by asprintf(). */
free(gosub_args);
ast_channel_lock(chan);
ast_debug(4, "%s Ending location: %s,%s,%d\n", ast_channel_name(chan),
ast_channel_context(chan), ast_channel_exten(chan),
ast_channel_priority(chan));
/* Restore previous location */
ast_channel_context_set(chan, old_context);
ast_channel_exten_set(chan, old_extension);
ast_channel_priority_set(chan, old_priority);
/* Restore autoloop flag */
ast_set2_flag(ast_channel_flags(chan), old_autoloopflag, AST_FLAG_IN_AUTOLOOP);
ast_channel_unlock(chan);
return RESULT_SUCCESS;
}
static struct agi_command gosub_agi_command =
{ { "gosub", NULL }, handle_gosub, NULL, NULL, 0 };
static int unload_module(void)
ast_install_stack_functions(NULL);
Richard Mudgett
committed
ast_agi_unregister(ast_module_info->self, &gosub_agi_command);
ast_unregister_application(app_return);
ast_unregister_application(app_pop);
ast_unregister_application(app_gosubif);
ast_unregister_application(app_gosub);
ast_custom_function_unregister(&local_function);
ast_custom_function_unregister(&peek_function);
Tilghman Lesher
committed
ast_custom_function_unregister(&stackpeek_function);
static int load_module(void)
/* Setup the stack application callback functions. */
static struct ast_app_stack_funcs funcs = {
.run_sub = gosub_run,
.expand_sub_args = expand_gosub_args,
};
Richard Mudgett
committed
ast_agi_register(ast_module_info->self, &gosub_agi_command);
ast_register_application_xml(app_pop, pop_exec);
ast_register_application_xml(app_return, return_exec);
ast_register_application_xml(app_gosubif, gosubif_exec);
ast_register_application_xml(app_gosub, gosub_exec);
ast_custom_function_register(&local_function);
ast_custom_function_register(&peek_function);
Tilghman Lesher
committed
ast_custom_function_register(&stackpeek_function);
funcs.module = ast_module_info->self,
ast_install_stack_functions(&funcs);
Tilghman Lesher
committed
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT | AST_MODFLAG_LOAD_ORDER, "Dialplan subroutines (Gosub, Return, etc)",
.load = load_module,
.unload = unload_module,
Tilghman Lesher
committed
.load_pri = AST_MODPRI_APP_DEPEND,
.nonoptreq = "res_agi",
);