static int find_matching_endwhile(struct ast_channel *chan) { struct ast_context *c; int res=-1; if (ast_rdlock_contexts()) { ast_log(LOG_ERROR, "Failed to lock contexts list\n"); return -1; } for (c=ast_walk_contexts(NULL); c; c=ast_walk_contexts(c)) { struct ast_exten *e; if (!ast_rdlock_context(c)) { if (!strcmp(ast_get_context_name(c), ast_channel_context(chan))) { /* This is the matching context we want */ int cur_priority = ast_channel_priority(chan) + 1, level=1; for (e = find_matching_priority(c, ast_channel_exten(chan), cur_priority, S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL)); e; e = find_matching_priority(c, ast_channel_exten(chan), ++cur_priority, S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL))) { if (!strcasecmp(ast_get_extension_app(e), "WHILE")) { level++; } else if (!strcasecmp(ast_get_extension_app(e), "ENDWHILE")) { level--; } if (level == 0) { res = cur_priority; break; } } } ast_unlock_context(c); if (res > 0) { break; } } } ast_unlock_contexts(); return res; }
void ast_channel_log(char *title, struct ast_channel *chan) /* for debug, this is handy enough to justify keeping it in the source */ { ast_log(LOG_NOTICE, "______ %s (%lx)______\n", title, (unsigned long) chan); ast_log(LOG_NOTICE, "CHAN: name: %s; appl: %s; data: %s; contxt: %s; exten: %s; pri: %d;\n", ast_channel_name(chan), ast_channel_appl(chan), ast_channel_data(chan), ast_channel_context(chan), ast_channel_exten(chan), ast_channel_priority(chan)); ast_log(LOG_NOTICE, "CHAN: acctcode: %s; dialcontext: %s; amaflags: %x; maccontxt: %s; macexten: %s; macpri: %d;\n", ast_channel_accountcode(chan), ast_channel_dialcontext(chan), ast_channel_amaflags(chan), ast_channel_macrocontext(chan), ast_channel_macroexten(chan), ast_channel_macropriority(chan)); ast_log(LOG_NOTICE, "CHAN: masq: %p; masqr: %p; uniqueID: %s; linkedID:%s\n", ast_channel_masq(chan), ast_channel_masqr(chan), ast_channel_uniqueid(chan), ast_channel_linkedid(chan)); if (ast_channel_masqr(chan)) { ast_log(LOG_NOTICE, "CHAN: masquerading as: %s; cdr: %p;\n", ast_channel_name(ast_channel_masqr(chan)), ast_channel_cdr(ast_channel_masqr(chan))); } ast_log(LOG_NOTICE, "===== done ====\n"); }
static u_char *ast_var_channels_table(struct variable *vp, oid *name, size_t *length, int exact, size_t *var_len, WriteMethod **write_method) { static unsigned long long_ret; static u_char bits_ret[2]; static char string_ret[256]; struct ast_channel *chan, *bridge; struct timeval tval; u_char *ret = NULL; int i, bit; struct ast_str *out = ast_str_alloca(2048); struct ast_channel_iterator *iter; if (header_simple_table(vp, name, length, exact, var_len, write_method, ast_active_channels())) return NULL; i = name[*length - 1] - 1; if (!(iter = ast_channel_iterator_all_new())) { return NULL; } while ((chan = ast_channel_iterator_next(iter)) && i) { ast_channel_unref(chan); i--; } iter = ast_channel_iterator_destroy(iter); if (chan == NULL) { return NULL; } *var_len = sizeof(long_ret); ast_channel_lock(chan); switch (vp->magic) { case ASTCHANINDEX: long_ret = name[*length - 1]; ret = (u_char *)&long_ret; break; case ASTCHANNAME: if (!ast_strlen_zero(ast_channel_name(chan))) { ast_copy_string(string_ret, ast_channel_name(chan), sizeof(string_ret)); *var_len = strlen(string_ret); ret = (u_char *)string_ret; } break; case ASTCHANLANGUAGE: if (!ast_strlen_zero(ast_channel_language(chan))) { ast_copy_string(string_ret, ast_channel_language(chan), sizeof(string_ret)); *var_len = strlen(string_ret); ret = (u_char *)string_ret; } break; case ASTCHANTYPE: ast_copy_string(string_ret, ast_channel_tech(chan)->type, sizeof(string_ret)); *var_len = strlen(string_ret); ret = (u_char *)string_ret; break; case ASTCHANMUSICCLASS: if (!ast_strlen_zero(ast_channel_musicclass(chan))) { ast_copy_string(string_ret, ast_channel_musicclass(chan), sizeof(string_ret)); *var_len = strlen(string_ret); ret = (u_char *)string_ret; } break; case ASTCHANBRIDGE: if ((bridge = ast_bridged_channel(chan)) != NULL) { ast_copy_string(string_ret, ast_channel_name(bridge), sizeof(string_ret)); *var_len = strlen(string_ret); ret = (u_char *)string_ret; } break; case ASTCHANMASQ: if (ast_channel_masq(chan) && !ast_strlen_zero(ast_channel_name(ast_channel_masq(chan)))) { ast_copy_string(string_ret, ast_channel_name(ast_channel_masq(chan)), sizeof(string_ret)); *var_len = strlen(string_ret); ret = (u_char *)string_ret; } break; case ASTCHANMASQR: if (ast_channel_masqr(chan) && !ast_strlen_zero(ast_channel_name(ast_channel_masqr(chan)))) { ast_copy_string(string_ret, ast_channel_name(ast_channel_masqr(chan)), sizeof(string_ret)); *var_len = strlen(string_ret); ret = (u_char *)string_ret; } break; case ASTCHANWHENHANGUP: if (!ast_tvzero(*ast_channel_whentohangup(chan))) { gettimeofday(&tval, NULL); long_ret = difftime(ast_channel_whentohangup(chan)->tv_sec, tval.tv_sec) * 100 - tval.tv_usec / 10000; ret= (u_char *)&long_ret; } break; case ASTCHANAPP: if (ast_channel_appl(chan)) { ast_copy_string(string_ret, ast_channel_appl(chan), sizeof(string_ret)); *var_len = strlen(string_ret); ret = (u_char *)string_ret; } break; case ASTCHANDATA: if (ast_channel_data(chan)) { ast_copy_string(string_ret, ast_channel_data(chan), sizeof(string_ret)); *var_len = strlen(string_ret); ret = (u_char *)string_ret; } break; case ASTCHANCONTEXT: ast_copy_string(string_ret, ast_channel_context(chan), sizeof(string_ret)); *var_len = strlen(string_ret); ret = (u_char *)string_ret; break; case ASTCHANMACROCONTEXT: ast_copy_string(string_ret, ast_channel_macrocontext(chan), sizeof(string_ret)); *var_len = strlen(string_ret); ret = (u_char *)string_ret; break; case ASTCHANMACROEXTEN: ast_copy_string(string_ret, ast_channel_macroexten(chan), sizeof(string_ret)); *var_len = strlen(string_ret); ret = (u_char *)string_ret; break; case ASTCHANMACROPRI: long_ret = ast_channel_macropriority(chan); ret = (u_char *)&long_ret; break; case ASTCHANEXTEN: ast_copy_string(string_ret, ast_channel_exten(chan), sizeof(string_ret)); *var_len = strlen(string_ret); ret = (u_char *)string_ret; break; case ASTCHANPRI: long_ret = ast_channel_priority(chan); ret = (u_char *)&long_ret; break; case ASTCHANACCOUNTCODE: if (!ast_strlen_zero(ast_channel_accountcode(chan))) { ast_copy_string(string_ret, ast_channel_accountcode(chan), sizeof(string_ret)); *var_len = strlen(string_ret); ret = (u_char *)string_ret; } break; case ASTCHANFORWARDTO: if (!ast_strlen_zero(ast_channel_call_forward(chan))) { ast_copy_string(string_ret, ast_channel_call_forward(chan), sizeof(string_ret)); *var_len = strlen(string_ret); ret = (u_char *)string_ret; } break; case ASTCHANUNIQUEID: ast_copy_string(string_ret, ast_channel_uniqueid(chan), sizeof(string_ret)); *var_len = strlen(string_ret); ret = (u_char *)string_ret; break; case ASTCHANCALLGROUP: long_ret = ast_channel_callgroup(chan); ret = (u_char *)&long_ret; break; case ASTCHANPICKUPGROUP: long_ret = ast_channel_pickupgroup(chan); ret = (u_char *)&long_ret; break; case ASTCHANSTATE: long_ret = ast_channel_state(chan) & 0xffff; ret = (u_char *)&long_ret; break; case ASTCHANMUTED: long_ret = ast_channel_state(chan) & AST_STATE_MUTE ? 1 : 2; ret = (u_char *)&long_ret; break; case ASTCHANRINGS: long_ret = ast_channel_rings(chan); ret = (u_char *)&long_ret; break; case ASTCHANCIDDNID: if (ast_channel_dialed(chan)->number.str) { ast_copy_string(string_ret, ast_channel_dialed(chan)->number.str, sizeof(string_ret)); *var_len = strlen(string_ret); ret = (u_char *)string_ret; } break; case ASTCHANCIDNUM: if (ast_channel_caller(chan)->id.number.valid && ast_channel_caller(chan)->id.number.str) { ast_copy_string(string_ret, ast_channel_caller(chan)->id.number.str, sizeof(string_ret)); *var_len = strlen(string_ret); ret = (u_char *)string_ret; } break; case ASTCHANCIDNAME: if (ast_channel_caller(chan)->id.name.valid && ast_channel_caller(chan)->id.name.str) { ast_copy_string(string_ret, ast_channel_caller(chan)->id.name.str, sizeof(string_ret)); *var_len = strlen(string_ret); ret = (u_char *)string_ret; } break; case ASTCHANCIDANI: if (ast_channel_caller(chan)->ani.number.valid && ast_channel_caller(chan)->ani.number.str) { ast_copy_string(string_ret, ast_channel_caller(chan)->ani.number.str, sizeof(string_ret)); *var_len = strlen(string_ret); ret = (u_char *)string_ret; } break; case ASTCHANCIDRDNIS: if (ast_channel_redirecting(chan)->from.number.valid && ast_channel_redirecting(chan)->from.number.str) { ast_copy_string(string_ret, ast_channel_redirecting(chan)->from.number.str, sizeof(string_ret)); *var_len = strlen(string_ret); ret = (u_char *)string_ret; } break; case ASTCHANCIDPRES: long_ret = ast_party_id_presentation(&ast_channel_caller(chan)->id); ret = (u_char *)&long_ret; break; case ASTCHANCIDANI2: long_ret = ast_channel_caller(chan)->ani2; ret = (u_char *)&long_ret; break; case ASTCHANCIDTON: long_ret = ast_channel_caller(chan)->id.number.plan; ret = (u_char *)&long_ret; break; case ASTCHANCIDTNS: long_ret = ast_channel_dialed(chan)->transit_network_select; ret = (u_char *)&long_ret; break; case ASTCHANAMAFLAGS: long_ret = ast_channel_amaflags(chan); ret = (u_char *)&long_ret; break; case ASTCHANADSI: long_ret = ast_channel_adsicpe(chan); ret = (u_char *)&long_ret; break; case ASTCHANTONEZONE: if (ast_channel_zone(chan)) { ast_copy_string(string_ret, ast_channel_zone(chan)->country, sizeof(string_ret)); *var_len = strlen(string_ret); ret = (u_char *)string_ret; } break; case ASTCHANHANGUPCAUSE: long_ret = ast_channel_hangupcause(chan); ret = (u_char *)&long_ret; break; case ASTCHANVARIABLES: if (pbx_builtin_serialize_variables(chan, &out)) { *var_len = ast_str_strlen(out); ret = (u_char *)ast_str_buffer(out); } break; case ASTCHANFLAGS: bits_ret[0] = 0; for (bit = 0; bit < 8; bit++) bits_ret[0] |= ((ast_channel_flags(chan)->flags & (1 << bit)) >> bit) << (7 - bit); bits_ret[1] = 0; for (bit = 0; bit < 8; bit++) bits_ret[1] |= (((ast_channel_flags(chan)->flags >> 8) & (1 << bit)) >> bit) << (7 - bit); *var_len = 2; ret = bits_ret; break; case ASTCHANTRANSFERCAP: long_ret = ast_channel_transfercapability(chan); ret = (u_char *)&long_ret; default: break; } ast_channel_unlock(chan); chan = ast_channel_unref(chan); return ret; }
static int serialize_showchan(struct ast_channel *c, char *buf, size_t size) { long elapsed_seconds = 0; int hour = 0, min = 0, sec = 0; struct ast_str *format_buf = ast_str_alloca(64); char cgrp[256]; char pgrp[256]; struct ast_str *write_transpath = ast_str_alloca(256); struct ast_str *read_transpath = ast_str_alloca(256); struct ast_bridge *bridge; memset(buf, 0, size); if (!c) return 0; elapsed_seconds = ast_channel_get_duration(c); hour = elapsed_seconds / 3600; min = (elapsed_seconds % 3600) / 60; sec = elapsed_seconds % 60; ast_channel_lock(c); bridge = ast_channel_get_bridge(c); ast_channel_unlock(c); snprintf(buf,size, "Name= %s\n" "Type= %s\n" "UniqueID= %s\n" "LinkedID= %s\n" "CallerIDNum= %s\n" "CallerIDName= %s\n" "ConnectedLineIDNum= %s\n" "ConnectedLineIDName=%s\n" "DNIDDigits= %s\n" "RDNIS= %s\n" "Parkinglot= %s\n" "Language= %s\n" "State= %s (%u)\n" "Rings= %d\n" "NativeFormat= %s\n" "WriteFormat= %s\n" "ReadFormat= %s\n" "RawWriteFormat= %s\n" "RawReadFormat= %s\n" "WriteTranscode= %s %s\n" "ReadTranscode= %s %s\n" "1stFileDescriptor= %d\n" "Framesin= %u %s\n" "Framesout= %u %s\n" "TimetoHangup= %ld\n" "ElapsedTime= %dh%dm%ds\n" "BridgeID= %s\n" "Context= %s\n" "Extension= %s\n" "Priority= %d\n" "CallGroup= %s\n" "PickupGroup= %s\n" "Application= %s\n" "Data= %s\n" "Blocking_in= %s\n", ast_channel_name(c), ast_channel_tech(c)->type, ast_channel_uniqueid(c), ast_channel_linkedid(c), S_COR(ast_channel_caller(c)->id.number.valid, ast_channel_caller(c)->id.number.str, "(N/A)"), S_COR(ast_channel_caller(c)->id.name.valid, ast_channel_caller(c)->id.name.str, "(N/A)"), S_COR(ast_channel_connected(c)->id.number.valid, ast_channel_connected(c)->id.number.str, "(N/A)"), S_COR(ast_channel_connected(c)->id.name.valid, ast_channel_connected(c)->id.name.str, "(N/A)"), S_OR(ast_channel_dialed(c)->number.str, "(N/A)"), S_COR(ast_channel_redirecting(c)->from.number.valid, ast_channel_redirecting(c)->from.number.str, "(N/A)"), ast_channel_parkinglot(c), ast_channel_language(c), ast_state2str(ast_channel_state(c)), ast_channel_state(c), ast_channel_rings(c), ast_format_cap_get_names(ast_channel_nativeformats(c), &format_buf), ast_format_get_name(ast_channel_writeformat(c)), ast_format_get_name(ast_channel_readformat(c)), ast_format_get_name(ast_channel_rawwriteformat(c)), ast_format_get_name(ast_channel_rawreadformat(c)), ast_channel_writetrans(c) ? "Yes" : "No", ast_translate_path_to_str(ast_channel_writetrans(c), &write_transpath), ast_channel_readtrans(c) ? "Yes" : "No", ast_translate_path_to_str(ast_channel_readtrans(c), &read_transpath), ast_channel_fd(c, 0), ast_channel_fin(c) & ~DEBUGCHAN_FLAG, (ast_channel_fin(c) & DEBUGCHAN_FLAG) ? " (DEBUGGED)" : "", ast_channel_fout(c) & ~DEBUGCHAN_FLAG, (ast_channel_fout(c) & DEBUGCHAN_FLAG) ? " (DEBUGGED)" : "", (long)ast_channel_whentohangup(c)->tv_sec, hour, min, sec, bridge ? bridge->uniqueid : "(Not bridged)", ast_channel_context(c), ast_channel_exten(c), ast_channel_priority(c), ast_print_group(cgrp, sizeof(cgrp), ast_channel_callgroup(c)), ast_print_group(pgrp, sizeof(pgrp), ast_channel_pickupgroup(c)), ast_channel_appl(c) ? ast_channel_appl(c) : "(N/A)", ast_channel_data(c) ? S_OR(ast_channel_data(c), "(Empty)") : "(None)", (ast_test_flag(ast_channel_flags(c), AST_FLAG_BLOCKING) ? ast_channel_blockproc(c) : "(Not Blocking)")); ao2_cleanup(bridge); return 0; }
/*! * \brief [lua_CFunction] This function is part of the 'application' metatable * and is used to execute applications similar to pbx_exec() (for access from * lua, don't call directly) * * \param L the lua_State to use * \return nothing * * This funciton is executed as the '()' operator for apps accessed through the * 'app' table. * * \code * app.playback('demo-congrats') * \endcode */ static int lua_pbx_exec(lua_State *L) { int res, nargs = lua_gettop(L); const char *data = ""; char *app_name, *context, *exten; char tmp[80], tmp2[80], tmp3[LUA_EXT_DATA_SIZE]; int priority, autoservice; struct ast_app *app; struct ast_channel *chan; lua_getfield(L, 1, "name"); app_name = ast_strdupa(lua_tostring(L, -1)); lua_pop(L, 1); if (!(app = pbx_findapp(app_name))) { lua_pushstring(L, "application '"); lua_pushstring(L, app_name); lua_pushstring(L, "' not found"); lua_concat(L, 3); return lua_error(L); } lua_getfield(L, LUA_REGISTRYINDEX, "channel"); chan = lua_touserdata(L, -1); lua_pop(L, 1); context = ast_strdupa(ast_channel_context(chan)); exten = ast_strdupa(ast_channel_exten(chan)); priority = ast_channel_priority(chan); lua_concat_args(L, 2, nargs); data = lua_tostring(L, -1); ast_verb(3, "Executing [%s@%s:%d] %s(\"%s\", \"%s\")\n", exten, context, priority, term_color(tmp, app_name, COLOR_BRCYAN, 0, sizeof(tmp)), term_color(tmp2, ast_channel_name(chan), COLOR_BRMAGENTA, 0, sizeof(tmp2)), term_color(tmp3, data, COLOR_BRMAGENTA, 0, sizeof(tmp3))); lua_getfield(L, LUA_REGISTRYINDEX, "autoservice"); autoservice = lua_toboolean(L, -1); lua_pop(L, 1); if (autoservice) ast_autoservice_stop(chan); res = pbx_exec(chan, app, data); lua_pop(L, 1); /* pop data */ data = ""; if (autoservice) ast_autoservice_start(chan); /* error executing an application, report it */ if (res) { lua_pushinteger(L, res); return lua_error(L); } if (strcmp(context, ast_channel_context(chan))) { lua_pushstring(L, context); lua_pushstring(L, ast_channel_context(chan)); lua_pushliteral(L, "context"); } else if (strcmp(exten, ast_channel_exten(chan))) { lua_pushstring(L, exten); lua_pushstring(L, ast_channel_exten(chan)); lua_pushliteral(L, "exten"); } else if (priority != ast_channel_priority(chan)) { lua_pushinteger(L, priority); lua_pushinteger(L, ast_channel_priority(chan)); lua_pushliteral(L, "priority"); } else { /* no goto - restore the original position back * to lua state, in case this was a recursive dialplan * call (a dialplan application re-entering dialplan) */ lua_update_registry(L, context, exten, priority); return 0; } /* goto detected - construct error message */ lua_insert(L, -3); lua_pushliteral(L, " changed from "); lua_insert(L, -3); lua_pushliteral(L, " to "); lua_insert(L, -2); lua_concat(L, 5); ast_debug(2, "Goto detected: %s\n", lua_tostring(L, -1)); lua_pop(L, 1); /* let the lua engine know it needs to return control to the pbx */ lua_pushinteger(L, LUA_GOTO_DETECTED); lua_error(L); return 0; }
static int _macro_exec(struct ast_channel *chan, const char *data, int exclusive) { const char *s; char *tmp; char *cur, *rest; char *macro; char fullmacro[80]; char varname[80]; char runningapp[80], runningdata[1024]; char *oldargs[MAX_ARGS + 1] = { NULL, }; int argc, x; int res=0; char oldexten[256]=""; int oldpriority, gosub_level = 0; char pc[80], depthc[12]; char oldcontext[AST_MAX_CONTEXT] = ""; const char *inhangupc; int offset, depth = 0, maxdepth = 7; int setmacrocontext=0; int autoloopflag, inhangup = 0; struct ast_str *tmp_subst = NULL; char *save_macro_exten; char *save_macro_context; char *save_macro_priority; char *save_macro_offset; struct ast_datastore *macro_store = ast_channel_datastore_find(chan, ¯o_ds_info, NULL); if (ast_strlen_zero(data)) { ast_log(LOG_WARNING, "Macro() requires arguments. See \"core show application macro\" for help.\n"); return -1; } do { if (macro_store) { break; } if (!(macro_store = ast_datastore_alloc(¯o_ds_info, NULL))) { ast_log(LOG_WARNING, "Unable to allocate new datastore.\n"); break; } /* Just the existence of this datastore is enough. */ macro_store->inheritance = DATASTORE_INHERIT_FOREVER; ast_channel_datastore_add(chan, macro_store); } while (0); /* does the user want a deeper rabbit hole? */ ast_channel_lock(chan); if ((s = pbx_builtin_getvar_helper(chan, "MACRO_RECURSION"))) { sscanf(s, "%30d", &maxdepth); } /* Count how many levels deep the rabbit hole goes */ if ((s = pbx_builtin_getvar_helper(chan, "MACRO_DEPTH"))) { sscanf(s, "%30d", &depth); } /* Used for detecting whether to return when a Macro is called from another Macro after hangup */ if (strcmp(ast_channel_exten(chan), "h") == 0) pbx_builtin_setvar_helper(chan, "MACRO_IN_HANGUP", "1"); if ((inhangupc = pbx_builtin_getvar_helper(chan, "MACRO_IN_HANGUP"))) { sscanf(inhangupc, "%30d", &inhangup); } ast_channel_unlock(chan); if (depth >= maxdepth) { ast_log(LOG_ERROR, "Macro(): possible infinite loop detected. Returning early.\n"); return 0; } snprintf(depthc, sizeof(depthc), "%d", depth + 1); tmp = ast_strdupa(data); rest = tmp; macro = strsep(&rest, ","); if (ast_strlen_zero(macro)) { ast_log(LOG_WARNING, "Invalid macro name specified\n"); return 0; } snprintf(fullmacro, sizeof(fullmacro), "macro-%s", macro); if (!ast_exists_extension(chan, fullmacro, "s", 1, S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL))) { if (!ast_context_find(fullmacro)) ast_log(LOG_WARNING, "No such context '%s' for macro '%s'. Was called by %s@%s\n", fullmacro, macro, ast_channel_exten(chan), ast_channel_context(chan)); else ast_log(LOG_WARNING, "Context '%s' for macro '%s' lacks 's' extension, priority 1\n", fullmacro, macro); return 0; } /* If we are to run the macro exclusively, take the mutex */ if (exclusive) { ast_debug(1, "Locking macrolock for '%s'\n", fullmacro); ast_autoservice_start(chan); if (ast_context_lockmacro(fullmacro)) { ast_log(LOG_WARNING, "Failed to lock macro '%s' as in-use\n", fullmacro); ast_autoservice_stop(chan); return 0; } ast_autoservice_stop(chan); } if (!(tmp_subst = ast_str_create(16))) { return -1; } /* Save old info */ oldpriority = ast_channel_priority(chan); ast_copy_string(oldexten, ast_channel_exten(chan), sizeof(oldexten)); ast_copy_string(oldcontext, ast_channel_context(chan), sizeof(oldcontext)); if (ast_strlen_zero(ast_channel_macrocontext(chan))) { ast_channel_macrocontext_set(chan, ast_channel_context(chan)); ast_channel_macroexten_set(chan, ast_channel_exten(chan)); ast_channel_macropriority_set(chan, ast_channel_priority(chan)); setmacrocontext=1; } argc = 1; /* Save old macro variables */ save_macro_exten = ast_strdup(pbx_builtin_getvar_helper(chan, "MACRO_EXTEN")); pbx_builtin_setvar_helper(chan, "MACRO_EXTEN", oldexten); save_macro_context = ast_strdup(pbx_builtin_getvar_helper(chan, "MACRO_CONTEXT")); pbx_builtin_setvar_helper(chan, "MACRO_CONTEXT", oldcontext); save_macro_priority = ast_strdup(pbx_builtin_getvar_helper(chan, "MACRO_PRIORITY")); snprintf(pc, sizeof(pc), "%d", oldpriority); pbx_builtin_setvar_helper(chan, "MACRO_PRIORITY", pc); save_macro_offset = ast_strdup(pbx_builtin_getvar_helper(chan, "MACRO_OFFSET")); pbx_builtin_setvar_helper(chan, "MACRO_OFFSET", NULL); pbx_builtin_setvar_helper(chan, "MACRO_DEPTH", depthc); /* Setup environment for new run */ ast_channel_exten_set(chan, "s"); ast_channel_context_set(chan, fullmacro); ast_channel_priority_set(chan, 1); ast_channel_lock(chan); while((cur = strsep(&rest, ",")) && (argc < MAX_ARGS)) { const char *argp; /* Save copy of old arguments if we're overwriting some, otherwise let them pass through to the other macro */ snprintf(varname, sizeof(varname), "ARG%d", argc); if ((argp = pbx_builtin_getvar_helper(chan, varname))) { oldargs[argc] = ast_strdup(argp); } pbx_builtin_setvar_helper(chan, varname, cur); argc++; } ast_channel_unlock(chan); autoloopflag = ast_test_flag(ast_channel_flags(chan), AST_FLAG_IN_AUTOLOOP); ast_set_flag(ast_channel_flags(chan), AST_FLAG_IN_AUTOLOOP); while (ast_exists_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))) { struct ast_context *c; struct ast_exten *e; int foundx; runningapp[0] = '\0'; runningdata[0] = '\0'; /* What application will execute? */ if (ast_rdlock_contexts()) { ast_log(LOG_WARNING, "Failed to lock contexts list\n"); } else { for (c = ast_walk_contexts(NULL), e = NULL; c; c = ast_walk_contexts(c)) { if (!strcmp(ast_get_context_name(c), ast_channel_context(chan))) { if (ast_rdlock_context(c)) { ast_log(LOG_WARNING, "Unable to lock context?\n"); } else { e = find_matching_priority(c, 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)); if (e) { /* This will only be undefined for pbx_realtime, which is majorly broken. */ ast_copy_string(runningapp, ast_get_extension_app(e), sizeof(runningapp)); ast_copy_string(runningdata, ast_get_extension_app_data(e), sizeof(runningdata)); } ast_unlock_context(c); } break; } } } ast_unlock_contexts(); /* Reset the macro depth, if it was changed in the last iteration */ pbx_builtin_setvar_helper(chan, "MACRO_DEPTH", depthc); 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), &foundx, 1); if (res) { /* Something bad happened, or a hangup has been requested. */ if (((res >= '0') && (res <= '9')) || ((res >= 'A') && (res <= 'F')) || (res == '*') || (res == '#')) { /* Just return result as to the previous application as if it had been dialed */ ast_debug(1, "Oooh, got something to jump out with ('%c')!\n", res); break; } switch(res) { case MACRO_EXIT_RESULT: res = 0; goto out; default: ast_debug(2, "Spawn extension (%s,%s,%d) exited non-zero on '%s' in macro '%s'\n", ast_channel_context(chan), ast_channel_exten(chan), ast_channel_priority(chan), ast_channel_name(chan), macro); ast_verb(2, "Spawn extension (%s, %s, %d) exited non-zero on '%s' in macro '%s'\n", ast_channel_context(chan), ast_channel_exten(chan), ast_channel_priority(chan), ast_channel_name(chan), macro); goto out; } } ast_debug(1, "Executed application: %s\n", runningapp); if (!strcasecmp(runningapp, "GOSUB")) { gosub_level++; ast_debug(1, "Incrementing gosub_level\n"); } else if (!strcasecmp(runningapp, "GOSUBIF")) { char *cond, *app_arg; char *app2; ast_str_substitute_variables(&tmp_subst, 0, chan, runningdata); app2 = ast_str_buffer(tmp_subst); cond = strsep(&app2, "?"); app_arg = strsep(&app2, ":"); if (pbx_checkcondition(cond)) { if (!ast_strlen_zero(app_arg)) { gosub_level++; ast_debug(1, "Incrementing gosub_level\n"); } } else { if (!ast_strlen_zero(app2)) { gosub_level++; ast_debug(1, "Incrementing gosub_level\n"); } } } else if (!strcasecmp(runningapp, "RETURN")) { gosub_level--; ast_debug(1, "Decrementing gosub_level\n"); } else if (!strcasecmp(runningapp, "STACKPOP")) { gosub_level--; ast_debug(1, "Decrementing gosub_level\n"); } else if (!strncasecmp(runningapp, "EXEC", 4)) { /* Must evaluate args to find actual app */ char *tmp2, *tmp3 = NULL; ast_str_substitute_variables(&tmp_subst, 0, chan, runningdata); tmp2 = ast_str_buffer(tmp_subst); if (!strcasecmp(runningapp, "EXECIF")) { if ((tmp3 = strchr(tmp2, '|'))) { *tmp3++ = '\0'; } if (!pbx_checkcondition(tmp2)) { tmp3 = NULL; } } else { tmp3 = tmp2; } if (tmp3) { ast_debug(1, "Last app: %s\n", tmp3); } if (tmp3 && !strncasecmp(tmp3, "GOSUB", 5)) { gosub_level++; ast_debug(1, "Incrementing gosub_level\n"); } else if (tmp3 && !strncasecmp(tmp3, "RETURN", 6)) { gosub_level--; ast_debug(1, "Decrementing gosub_level\n"); } else if (tmp3 && !strncasecmp(tmp3, "STACKPOP", 8)) { gosub_level--; ast_debug(1, "Decrementing gosub_level\n"); } } if (gosub_level == 0 && strcasecmp(ast_channel_context(chan), fullmacro)) { ast_verb(2, "Channel '%s' jumping out of macro '%s'\n", ast_channel_name(chan), macro); break; } /* don't stop executing extensions when we're in "h" */ if (ast_check_hangup(chan) && !inhangup) { ast_debug(1, "Extension %s, macroexten %s, priority %d returned normally even though call was hung up\n", ast_channel_exten(chan), ast_channel_macroexten(chan), ast_channel_priority(chan)); goto out; } ast_channel_priority_set(chan, ast_channel_priority(chan) + 1); } out: /* Don't let the channel change now. */ ast_channel_lock(chan); /* Reset the depth back to what it was when the routine was entered (like if we called Macro recursively) */ snprintf(depthc, sizeof(depthc), "%d", depth); pbx_builtin_setvar_helper(chan, "MACRO_DEPTH", depthc); ast_set2_flag(ast_channel_flags(chan), autoloopflag, AST_FLAG_IN_AUTOLOOP); for (x = 1; x < argc; x++) { /* Restore old arguments and delete ours */ snprintf(varname, sizeof(varname), "ARG%d", x); if (oldargs[x]) { pbx_builtin_setvar_helper(chan, varname, oldargs[x]); ast_free(oldargs[x]); } else { pbx_builtin_setvar_helper(chan, varname, NULL); } } /* Restore macro variables */ pbx_builtin_setvar_helper(chan, "MACRO_EXTEN", save_macro_exten); pbx_builtin_setvar_helper(chan, "MACRO_CONTEXT", save_macro_context); pbx_builtin_setvar_helper(chan, "MACRO_PRIORITY", save_macro_priority); if (save_macro_exten) ast_free(save_macro_exten); if (save_macro_context) ast_free(save_macro_context); if (save_macro_priority) ast_free(save_macro_priority); if (setmacrocontext) { ast_channel_macrocontext_set(chan, ""); ast_channel_macroexten_set(chan, ""); ast_channel_macropriority_set(chan, 0); } if (!strcasecmp(ast_channel_context(chan), fullmacro)) { const char *offsets; /* If we're leaving the macro normally, restore original information */ ast_channel_priority_set(chan, oldpriority); ast_channel_context_set(chan, oldcontext); ast_channel_exten_set(chan, oldexten); if ((offsets = pbx_builtin_getvar_helper(chan, "MACRO_OFFSET"))) { /* Handle macro offset if it's set by checking the availability of step n + offset + 1, otherwise continue normally if there is any problem */ if (sscanf(offsets, "%30d", &offset) == 1) { if (ast_exists_extension(chan, ast_channel_context(chan), ast_channel_exten(chan), ast_channel_priority(chan) + offset + 1, S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL))) { ast_channel_priority_set(chan, ast_channel_priority(chan) + offset); } } } } pbx_builtin_setvar_helper(chan, "MACRO_OFFSET", save_macro_offset); if (save_macro_offset) ast_free(save_macro_offset); /* Unlock the macro */ if (exclusive) { ast_debug(1, "Unlocking macrolock for '%s'\n", fullmacro); if (ast_context_unlockmacro(fullmacro)) { ast_log(LOG_ERROR, "Failed to unlock macro '%s' - that isn't good\n", fullmacro); res = 0; } } ast_channel_unlock(chan); ast_free(tmp_subst); return res; }
static int func_channel_write_real(struct ast_channel *chan, const char *function, char *data, const char *value) { int ret = 0; signed char gainset; if (!strcasecmp(data, "language")) locked_string_field_set(chan, language, value); else if (!strcasecmp(data, "parkinglot")) locked_string_field_set(chan, parkinglot, value); else if (!strcasecmp(data, "musicclass")) locked_string_field_set(chan, musicclass, value); else if (!strcasecmp(data, "accountcode")) locked_string_field_set(chan, accountcode, value); else if (!strcasecmp(data, "userfield")) locked_string_field_set(chan, userfield, value); else if (!strcasecmp(data, "after_bridge_goto")) { if (ast_strlen_zero(value)) { ast_bridge_discard_after_goto(chan); } else { ast_bridge_set_after_go_on(chan, ast_channel_context(chan), ast_channel_exten(chan), ast_channel_priority(chan), value); } } else if (!strcasecmp(data, "amaflags")) { ast_channel_lock(chan); if (isdigit(*value)) { int amaflags; sscanf(value, "%30d", &amaflags); ast_channel_amaflags_set(chan, amaflags); } else if (!strcasecmp(value,"OMIT")){ ast_channel_amaflags_set(chan, 1); } else if (!strcasecmp(value,"BILLING")){ ast_channel_amaflags_set(chan, 2); } else if (!strcasecmp(value,"DOCUMENTATION")){ ast_channel_amaflags_set(chan, 3); } ast_channel_unlock(chan); } else if (!strcasecmp(data, "peeraccount")) locked_string_field_set(chan, peeraccount, value); else if (!strcasecmp(data, "hangupsource")) /* XXX - should we be forcing this here? */ ast_set_hangupsource(chan, value, 0); #ifdef CHANNEL_TRACE else if (!strcasecmp(data, "trace")) { ast_channel_lock(chan); if (ast_true(value)) ret = ast_channel_trace_enable(chan); else if (ast_false(value)) ret = ast_channel_trace_disable(chan); else { ret = -1; ast_log(LOG_WARNING, "Invalid value for CHANNEL(trace).\n"); } ast_channel_unlock(chan); } #endif else if (!strcasecmp(data, "tonezone")) { struct ast_tone_zone *new_zone; if (!(new_zone = ast_get_indication_zone(value))) { ast_log(LOG_ERROR, "Unknown country code '%s' for tonezone. Check indications.conf for available country codes.\n", value); ret = -1; } else { ast_channel_lock(chan); if (ast_channel_zone(chan)) { ast_channel_zone_set(chan, ast_tone_zone_unref(ast_channel_zone(chan))); } ast_channel_zone_set(chan, ast_tone_zone_ref(new_zone)); ast_channel_unlock(chan); new_zone = ast_tone_zone_unref(new_zone); } } else if (!strcasecmp(data, "dtmf_features")) { ret = ast_bridge_features_ds_set_string(chan, value); } else if (!strcasecmp(data, "callgroup")) { ast_channel_lock(chan); ast_channel_callgroup_set(chan, ast_get_group(value)); ast_channel_unlock(chan); } else if (!strcasecmp(data, "pickupgroup")) { ast_channel_lock(chan); ast_channel_pickupgroup_set(chan, ast_get_group(value)); ast_channel_unlock(chan); } else if (!strcasecmp(data, "namedcallgroup")) { struct ast_namedgroups *groups = ast_get_namedgroups(value); ast_channel_lock(chan); ast_channel_named_callgroups_set(chan, groups); ast_channel_unlock(chan); ast_unref_namedgroups(groups); } else if (!strcasecmp(data, "namedpickupgroup")) { struct ast_namedgroups *groups = ast_get_namedgroups(value); ast_channel_lock(chan); ast_channel_named_pickupgroups_set(chan, groups); ast_channel_unlock(chan); ast_unref_namedgroups(groups); } else if (!strcasecmp(data, "txgain")) { sscanf(value, "%4hhd", &gainset); ast_channel_setoption(chan, AST_OPTION_TXGAIN, &gainset, sizeof(gainset), 0); } else if (!strcasecmp(data, "rxgain")) { sscanf(value, "%4hhd", &gainset); ast_channel_setoption(chan, AST_OPTION_RXGAIN, &gainset, sizeof(gainset), 0); } else if (!strcasecmp(data, "transfercapability")) { unsigned short i; ast_channel_lock(chan); for (i = 0; i < 0x20; i++) { if (!strcasecmp(transfercapability_table[i], value) && strcmp(value, "UNK")) { ast_channel_transfercapability_set(chan, i); break; } } ast_channel_unlock(chan); } else if (!strcasecmp(data, "hangup_handler_pop")) { /* Pop one hangup handler before pushing the new handler. */ ast_pbx_hangup_handler_pop(chan); ast_pbx_hangup_handler_push(chan, value); } else if (!strcasecmp(data, "hangup_handler_push")) { ast_pbx_hangup_handler_push(chan, value); } else if (!strcasecmp(data, "hangup_handler_wipe")) { /* Pop all hangup handlers before pushing the new handler. */ while (ast_pbx_hangup_handler_pop(chan)) { } ast_pbx_hangup_handler_push(chan, value); } else if (!strncasecmp(data, "secure_bridge_", 14)) { struct ast_datastore *ds; struct ast_secure_call_store *store; if (!chan || !value) { return -1; } ast_channel_lock(chan); if (!(ds = ast_channel_datastore_find(chan, &secure_call_info, NULL))) { if (!(ds = ast_datastore_alloc(&secure_call_info, NULL))) { ast_channel_unlock(chan); return -1; } if (!(store = ast_calloc(1, sizeof(*store)))) { ast_channel_unlock(chan); ast_free(ds); return -1; } ds->data = store; ast_channel_datastore_add(chan, ds); } else { store = ds->data; } if (!strcasecmp(data, "secure_bridge_signaling")) { store->signaling = ast_true(value) ? 1 : 0; } else if (!strcasecmp(data, "secure_bridge_media")) { store->media = ast_true(value) ? 1 : 0; } ast_channel_unlock(chan); } else if (!strcasecmp(data, "max_forwards")) { int max_forwards; if (sscanf(value, "%d", &max_forwards) != 1) { ast_log(LOG_WARNING, "Unable to set max forwards to '%s'\n", value); ret = -1; } else { ast_channel_lock(chan); ret = ast_max_forwards_set(chan, max_forwards); ast_channel_unlock(chan); } } else if (!ast_channel_tech(chan)->func_channel_write || ast_channel_tech(chan)->func_channel_write(chan, function, data, value)) { ast_log(LOG_WARNING, "Unknown or unavailable item requested: '%s'\n", data); ret = -1; } return ret; }
/*! * \brief Bridge channels together * \param s * \param m * * Make sure valid channels were specified, * send errors if any of the channels could not be found/locked, answer channels if needed, * create the placeholder channels and grab the other channels * make the channels compatible, send error if we fail doing so * setup the bridge thread object and start the bridge. * * \retval 0 */ static int action_bridge(struct mansession *s, const struct message *m) { const char *channela = astman_get_header(m, "Channel1"); const char *channelb = astman_get_header(m, "Channel2"); enum play_tone_action playtone = parse_playtone(astman_get_header(m, "Tone")); RAII_VAR(struct ast_channel *, chana, NULL, ao2_cleanup); RAII_VAR(struct ast_channel *, chanb, NULL, ao2_cleanup); const char *chana_exten; const char *chana_context; int chana_priority; const char *chanb_exten; const char *chanb_context; int chanb_priority; struct ast_bridge *bridge; char buf[256]; RAII_VAR(struct ast_features_xfer_config *, xfer_cfg_a, NULL, ao2_cleanup); RAII_VAR(struct ast_features_xfer_config *, xfer_cfg_b, NULL, ao2_cleanup); /* make sure valid channels were specified */ if (ast_strlen_zero(channela) || ast_strlen_zero(channelb)) { astman_send_error(s, m, "Missing channel parameter in request"); return 0; } ast_debug(1, "Performing Bridge action on %s and %s\n", channela, channelb); /* Start with chana */ chana = ast_channel_get_by_name_prefix(channela, strlen(channela)); if (!chana) { snprintf(buf, sizeof(buf), "Channel1 does not exist: %s", channela); astman_send_error(s, m, buf); return 0; } ast_channel_lock(chana); xfer_cfg_a = ast_get_chan_features_xfer_config(chana); chana_exten = ast_strdupa(ast_channel_exten(chana)); chana_context = ast_strdupa(ast_channel_context(chana)); chana_priority = ast_channel_priority(chana); if (!ast_test_flag(ast_channel_flags(chana), AST_FLAG_IN_AUTOLOOP)) { chana_priority++; } ast_channel_unlock(chana); chanb = ast_channel_get_by_name_prefix(channelb, strlen(channelb)); if (!chanb) { snprintf(buf, sizeof(buf), "Channel2 does not exist: %s", channelb); astman_send_error(s, m, buf); return 0; } ast_channel_lock(chanb); xfer_cfg_b = ast_get_chan_features_xfer_config(chanb); chanb_exten = ast_strdupa(ast_channel_exten(chanb)); chanb_context = ast_strdupa(ast_channel_context(chanb)); chanb_priority = ast_channel_priority(chanb); if (!ast_test_flag(ast_channel_flags(chanb), AST_FLAG_IN_AUTOLOOP)) { chanb_priority++; } ast_channel_unlock(chanb); bridge = ast_bridge_basic_new(); if (!bridge) { astman_send_error(s, m, "Unable to create bridge\n"); return 0; } ast_bridge_set_after_go_on(chana, chana_context, chana_exten, chana_priority, NULL); if (ast_bridge_add_channel(bridge, chana, NULL, playtone & PLAYTONE_CHANNEL1, xfer_cfg_a ? xfer_cfg_a->xfersound : NULL)) { snprintf(buf, sizeof(buf), "Unable to add Channel1 to bridge: %s", ast_channel_name(chana)); astman_send_error(s, m, buf); ast_bridge_destroy(bridge, 0); return 0; } ast_bridge_set_after_go_on(chanb, chanb_context, chanb_exten, chanb_priority, NULL); if (ast_bridge_add_channel(bridge, chanb, NULL, playtone & PLAYTONE_CHANNEL2, xfer_cfg_b ? xfer_cfg_b->xfersound : NULL)) { snprintf(buf, sizeof(buf), "Unable to add Channel2 to bridge: %s", ast_channel_name(chanb)); astman_send_error(s, m, buf); ast_bridge_destroy(bridge, 0); return 0; } astman_send_ack(s, m, "Channels have been bridged"); ao2_cleanup(bridge); return 0; }
struct ast_channel_snapshot *ast_channel_snapshot_create(struct ast_channel *chan) { struct ast_channel_snapshot *snapshot; struct ast_bridge *bridge; char nativeformats[256]; struct ast_str *write_transpath = ast_str_alloca(256); struct ast_str *read_transpath = ast_str_alloca(256); struct ast_party_id effective_connected_id; struct ast_callid *callid; /* no snapshots for dummy channels */ if (!ast_channel_tech(chan)) { return NULL; } snapshot = ao2_alloc(sizeof(*snapshot), channel_snapshot_dtor); if (!snapshot || ast_string_field_init(snapshot, 1024)) { ao2_cleanup(snapshot); return NULL; } ast_string_field_set(snapshot, name, ast_channel_name(chan)); ast_string_field_set(snapshot, type, ast_channel_tech(chan)->type); ast_string_field_set(snapshot, accountcode, ast_channel_accountcode(chan)); ast_string_field_set(snapshot, peeraccount, ast_channel_peeraccount(chan)); ast_string_field_set(snapshot, userfield, ast_channel_userfield(chan)); ast_string_field_set(snapshot, uniqueid, ast_channel_uniqueid(chan)); ast_string_field_set(snapshot, linkedid, ast_channel_linkedid(chan)); ast_string_field_set(snapshot, hangupsource, ast_channel_hangupsource(chan)); if (ast_channel_appl(chan)) { ast_string_field_set(snapshot, appl, ast_channel_appl(chan)); } if (ast_channel_data(chan)) { ast_string_field_set(snapshot, data, ast_channel_data(chan)); } ast_string_field_set(snapshot, context, ast_channel_context(chan)); ast_string_field_set(snapshot, exten, ast_channel_exten(chan)); ast_string_field_set(snapshot, caller_name, S_COR(ast_channel_caller(chan)->id.name.valid, ast_channel_caller(chan)->id.name.str, "")); ast_string_field_set(snapshot, caller_number, S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, "")); ast_string_field_set(snapshot, caller_dnid, S_OR(ast_channel_dialed(chan)->number.str, "")); ast_string_field_set(snapshot, caller_subaddr, S_COR(ast_channel_caller(chan)->id.subaddress.valid, ast_channel_caller(chan)->id.subaddress.str, "")); ast_string_field_set(snapshot, dialed_subaddr, S_COR(ast_channel_dialed(chan)->subaddress.valid, ast_channel_dialed(chan)->subaddress.str, "")); ast_string_field_set(snapshot, caller_ani, S_COR(ast_channel_caller(chan)->ani.number.valid, ast_channel_caller(chan)->ani.number.str, "")); ast_string_field_set(snapshot, caller_rdnis, S_COR(ast_channel_redirecting(chan)->from.number.valid, ast_channel_redirecting(chan)->from.number.str, "")); ast_string_field_set(snapshot, caller_dnid, S_OR(ast_channel_dialed(chan)->number.str, "")); ast_string_field_set(snapshot, connected_name, S_COR(ast_channel_connected(chan)->id.name.valid, ast_channel_connected(chan)->id.name.str, "")); ast_string_field_set(snapshot, connected_number, S_COR(ast_channel_connected(chan)->id.number.valid, ast_channel_connected(chan)->id.number.str, "")); ast_string_field_set(snapshot, language, ast_channel_language(chan)); if ((bridge = ast_channel_get_bridge(chan))) { ast_string_field_set(snapshot, bridgeid, bridge->uniqueid); ao2_cleanup(bridge); } ast_string_field_set(snapshot, nativeformats, ast_getformatname_multiple(nativeformats, sizeof(nativeformats), ast_channel_nativeformats(chan))); ast_string_field_set(snapshot, readformat, ast_getformatname(ast_channel_readformat(chan))); ast_string_field_set(snapshot, writeformat, ast_getformatname(ast_channel_writeformat(chan))); ast_string_field_set(snapshot, writetrans, ast_translate_path_to_str(ast_channel_writetrans(chan), &write_transpath)); ast_string_field_set(snapshot, readtrans, ast_translate_path_to_str(ast_channel_readtrans(chan), &read_transpath)); effective_connected_id = ast_channel_connected_effective_id(chan); ast_string_field_set(snapshot, effective_name, S_COR(effective_connected_id.name.valid, effective_connected_id.name.str, "")); ast_string_field_set(snapshot, effective_number, S_COR(effective_connected_id.number.valid, effective_connected_id.number.str, "")); if ((callid = ast_channel_callid(chan))) { ast_callid_strnprint(snapshot->callid, sizeof(snapshot->callid), callid); ast_callid_unref(callid); } snapshot->creationtime = ast_channel_creationtime(chan); snapshot->hanguptime = *(ast_channel_whentohangup(chan)); snapshot->state = ast_channel_state(chan); snapshot->priority = ast_channel_priority(chan); snapshot->amaflags = ast_channel_amaflags(chan); snapshot->hangupcause = ast_channel_hangupcause(chan); ast_copy_flags(&snapshot->flags, ast_channel_flags(chan), 0xFFFFFFFF); snapshot->caller_pres = ast_party_id_presentation(&ast_channel_caller(chan)->id); snapshot->callgroup = ast_channel_callgroup(chan); snapshot->pickupgroup = ast_channel_pickupgroup(chan); ast_set_flag(&snapshot->softhangup_flags, ast_channel_softhangup_internal_flag(chan)); snapshot->manager_vars = ast_channel_get_manager_vars(chan); snapshot->channel_vars = ast_channel_get_vars(chan); snapshot->tech_properties = ast_channel_tech(chan)->properties; return snapshot; }
static int _while_exec(struct ast_channel *chan, const char *data, int end) { int res=0; const char *while_pri = NULL; char *my_name = NULL; const char *condition = NULL, *label = NULL; char varname[VAR_SIZE], end_varname[VAR_SIZE]; const char *prefix = "WHILE"; size_t size=0; int used_index_i = -1, x=0; char used_index[VAR_SIZE] = "0", new_index[VAR_SIZE] = "0"; if (!chan) { /* huh ? */ return -1; } #if 0 /* don't want run away loops if the chan isn't even up this is up for debate since it slows things down a tad ...... Debate is over... this prevents While/EndWhile from working within the "h" extension. Not good. */ if (ast_waitfordigit(chan,1) < 0) return -1; #endif for (x=0;;x++) { if (get_index(chan, prefix, x)) { used_index_i = x; } else break; } snprintf(used_index, VAR_SIZE, "%d", used_index_i); snprintf(new_index, VAR_SIZE, "%d", used_index_i + 1); if (!end) condition = ast_strdupa(data); size = strlen(ast_channel_context(chan)) + strlen(ast_channel_exten(chan)) + 32; my_name = ast_alloca(size); memset(my_name, 0, size); snprintf(my_name, size, "%s_%s_%d", ast_channel_context(chan), ast_channel_exten(chan), ast_channel_priority(chan)); ast_channel_lock(chan); if (end) { label = used_index; } else if (!(label = pbx_builtin_getvar_helper(chan, my_name))) { label = new_index; pbx_builtin_setvar_helper(chan, my_name, label); } snprintf(varname, VAR_SIZE, "%s_%s", prefix, label); if ((while_pri = pbx_builtin_getvar_helper(chan, varname)) && !end) { while_pri = ast_strdupa(while_pri); snprintf(end_varname,VAR_SIZE,"END_%s",varname); } ast_channel_unlock(chan); if ((!end && !pbx_checkcondition(condition)) || (end == 2)) { /* Condition Met (clean up helper vars) */ const char *goto_str; pbx_builtin_setvar_helper(chan, varname, NULL); pbx_builtin_setvar_helper(chan, my_name, NULL); snprintf(end_varname,VAR_SIZE,"END_%s",varname); ast_channel_lock(chan); if ((goto_str = pbx_builtin_getvar_helper(chan, end_varname))) { ast_parseable_goto(chan, goto_str); pbx_builtin_setvar_helper(chan, end_varname, NULL); } else { int pri = find_matching_endwhile(chan); if (pri > 0) { ast_verb(3, "Jumping to priority %d\n", pri); ast_channel_priority_set(chan, pri); } else { ast_log(LOG_WARNING, "Couldn't find matching EndWhile? (While at %s@%s priority %d)\n", ast_channel_context(chan), ast_channel_exten(chan), ast_channel_priority(chan)); } } ast_channel_unlock(chan); return res; } if (!end && !while_pri) { char *goto_str; size = strlen(ast_channel_context(chan)) + strlen(ast_channel_exten(chan)) + 32; goto_str = ast_alloca(size); memset(goto_str, 0, size); snprintf(goto_str, size, "%s,%s,%d", ast_channel_context(chan), ast_channel_exten(chan), ast_channel_priority(chan)); pbx_builtin_setvar_helper(chan, varname, goto_str); } else if (end && while_pri) { /* END of loop */ snprintf(end_varname, VAR_SIZE, "END_%s", varname); if (! pbx_builtin_getvar_helper(chan, end_varname)) { char *goto_str; size = strlen(ast_channel_context(chan)) + strlen(ast_channel_exten(chan)) + 32; goto_str = ast_alloca(size); memset(goto_str, 0, size); snprintf(goto_str, size, "%s,%s,%d", ast_channel_context(chan), ast_channel_exten(chan), ast_channel_priority(chan)+1); pbx_builtin_setvar_helper(chan, end_varname, goto_str); } ast_parseable_goto(chan, while_pri); } return res; }