static enum ast_test_result_state test_2way_function(struct ast_test *test, struct ast_channel *c, const char *encode1, const char *encode2, const char *decode1, const char *decode2) { struct ast_str *str = ast_str_create(16), *expression = ast_str_alloca(120); int okay; ast_str_set(&expression, 0, "%s%s%s", encode1, "foobarbaz", encode2); ast_str_substitute_variables(&str, 0, c, ast_str_buffer(expression)); ast_str_set(&expression, 0, "%s%s%s", decode1, ast_str_buffer(str), decode2); ast_str_substitute_variables(&str, 0, c, ast_str_buffer(expression)); okay = !strcmp(ast_str_buffer(str), "foobarbaz"); ast_test_status_update(test, "Testing '%s%s' and '%s%s' . . . . . %s\n", encode1, encode2, decode1, decode2, okay ? "passed" : "FAILED"); if (!okay) { ast_test_status_update(test, " '%s' != 'foobarbaz'\n", ast_str_buffer(str)); } ast_free(str); return okay ? AST_TEST_PASS : AST_TEST_FAIL; }
static int tryexec_exec(struct ast_channel *chan, const char *data) { int res = 0; char *s, *appname, *endargs; struct ast_app *app; struct ast_str *args = NULL; if (ast_strlen_zero(data)) return 0; s = ast_strdupa(data); appname = strsep(&s, "("); if (s) { endargs = strrchr(s, ')'); if (endargs) *endargs = '\0'; if ((args = ast_str_create(16))) { ast_str_substitute_variables(&args, 0, chan, s); } } if (appname) { app = pbx_findapp(appname); if (app) { res = pbx_exec(chan, app, args ? ast_str_buffer(args) : NULL); pbx_builtin_setvar_helper(chan, "TRYSTATUS", res ? "FAILED" : "SUCCESS"); } else { ast_log(LOG_WARNING, "Could not find application (%s)\n", appname); pbx_builtin_setvar_helper(chan, "TRYSTATUS", "NOAPP"); } } ast_free(args); return 0; }
static enum ast_test_result_state test_chan_variable(struct ast_test *test, struct ast_channel *c, const char *varname) { const char *values[] = { "one", "three", "reallylongdinosaursoundingthingwithwordsinit" }; int i, okay = 1; char workspace[4096]; struct ast_str *str = ast_str_create(16); struct ast_str *var = ast_str_create(16); ast_str_set(&var, 0, "${%s}", varname); for (i = 0; i < ARRAY_LEN(values); i++) { pbx_builtin_setvar_helper(c, varname, values[i]); ast_str_substitute_variables(&str, 0, c, ast_str_buffer(var)); pbx_substitute_variables_helper(c, ast_str_buffer(var), workspace, sizeof(workspace)); ast_test_status_update(test, "Testing '%s' . . . . . %s\n", ast_str_buffer(var), okay ? "passed" : "FAILED"); if (strcmp(values[i], ast_str_buffer(str)) != 0 || strcmp(values[i], workspace) != 0) { ast_test_status_update(test, "%s != %s != %s\n", values[i], ast_str_buffer(str), workspace); okay = 0; } } ast_free(str); ast_free(var); return okay ? AST_TEST_PASS : AST_TEST_FAIL; }
static enum ast_test_result_state test_chan_string(struct ast_test *test, struct ast_channel *c, char *cfield, size_t cfieldsize, const char *expression) { const char *values[] = { "one", "three", "reallylongdinosaursoundingthingwithwordsinit" }; int i, okay = 1; char workspace[4096]; struct ast_str *str = ast_str_create(16); for (i = 0; i < ARRAY_LEN(values); i++) { ast_copy_string(cfield, values[i], cfieldsize); ast_str_substitute_variables(&str, 0, c, expression); pbx_substitute_variables_helper(c, expression, workspace, sizeof(workspace)); ast_test_status_update(test, "Testing '%s' . . . . . %s\n", expression, okay ? "passed" : "FAILED"); if (strcmp(cfield, ast_str_buffer(str)) != 0 || strcmp(cfield, workspace) != 0) { ast_test_status_update(test, "%s != %s != %s\n", cfield, ast_str_buffer(str), workspace); okay = 0; } } ast_free(str); return okay ? AST_TEST_PASS : AST_TEST_FAIL; }
static int update2_curl(const char *url, const char *unused, const struct ast_variable *lookup_fields, const struct ast_variable *update_fields) { struct ast_str *query, *buffer; char buf1[200], buf2[200]; const struct ast_variable *field; char *stringp; unsigned int start = 1; int rowcount = -1; if (!ast_custom_function_find("CURL")) { ast_log(LOG_ERROR, "func_curl.so must be loaded in order to use res_config_curl.so!!\n"); return -1; } if (!(query = ast_str_thread_get(&query_buf, 1000))) return -1; if (!(buffer = ast_str_thread_get(&result_buf, 16))) { return -1; } ast_str_set(&query, 0, "${CURL(%s/update?", url); for (field = lookup_fields; field; field = field->next) { ast_uri_encode(field->name, buf1, sizeof(buf1), ast_uri_http); ast_uri_encode(field->value, buf2, sizeof(buf2), ast_uri_http); ast_str_append(&query, 0, "%s%s=%s", !start ? "" : "&", buf1, buf2); start = 0; } ast_str_append(&query, 0, ","); start = 1; for (field = update_fields; field; field = field->next) { ast_uri_encode(field->name, buf1, sizeof(buf1), ast_uri_http); ast_uri_encode(field->value, buf2, sizeof(buf2), ast_uri_http); ast_str_append(&query, 0, "%s%s=%s", !start ? "" : "&", buf1, buf2); start = 0; } ast_str_append(&query, 0, ")}"); /* Proxies work, by setting CURLOPT options in the [globals] section of * extensions.conf. Unfortunately, this means preloading pbx_config.so * so that they have an opportunity to be set prior to startup realtime * queries. */ ast_str_substitute_variables(&buffer, 0, NULL, ast_str_buffer(query)); /* Line oriented output */ stringp = ast_str_buffer(buffer); while (*stringp <= ' ') { stringp++; } sscanf(stringp, "%30d", &rowcount); if (rowcount >= 0) { return (int)rowcount; } return -1; }
static int require_curl(const char *url, const char *unused, va_list ap) { struct ast_str *query, *buffer; char *elm, field[256]; int type, size, i = 0; if (!ast_custom_function_find("CURL")) { ast_log(LOG_ERROR, "func_curl.so must be loaded in order to use res_config_curl.so!!\n"); return -1; } if (!(query = ast_str_thread_get(&query_buf, 100))) { return -1; } if (!(buffer = ast_str_thread_get(&result_buf, 16))) { return -1; } ast_str_set(&query, 0, "${CURL(%s/require,", url); while ((elm = va_arg(ap, char *))) { type = va_arg(ap, require_type); size = va_arg(ap, int); ast_uri_encode(elm, field, sizeof(field), ast_uri_http); ast_str_append(&query, 0, "%s%s=%s%%3A%d", i > 0 ? "&" : "", field, type == RQ_CHAR ? "char" : type == RQ_INTEGER1 ? "integer1" : type == RQ_UINTEGER1 ? "uinteger1" : type == RQ_INTEGER2 ? "integer2" : type == RQ_UINTEGER2 ? "uinteger2" : type == RQ_INTEGER3 ? "integer3" : type == RQ_UINTEGER3 ? "uinteger3" : type == RQ_INTEGER4 ? "integer4" : type == RQ_UINTEGER4 ? "uinteger4" : type == RQ_INTEGER8 ? "integer8" : type == RQ_UINTEGER8 ? "uinteger8" : type == RQ_DATE ? "date" : type == RQ_DATETIME ? "datetime" : type == RQ_FLOAT ? "float" : "unknown", size); i++; } ast_str_append(&query, 0, ")}"); ast_str_substitute_variables(&buffer, 0, NULL, ast_str_buffer(query)); return atoi(ast_str_buffer(buffer)); }
/*! * \brief Execute an DELETE query * \param url * \param unused * \param keyfield where clause field * \param lookup value of field for where clause * \param fields list containing one or more field/value set(s) * * Delete a row from a database table, prepare the sql statement using keyfield and lookup * control the number of records to change. Additional params to match rows are stored in ap list. * Sub-in the values to the prepared statement and execute it. * * \retval number of rows affected * \retval -1 on failure */ static int destroy_curl(const char *url, const char *unused, const char *keyfield, const char *lookup, const struct ast_variable *fields) { struct ast_str *query, *buffer; char buf1[200], buf2[200]; const struct ast_variable *field; char *stringp; int start = 1, rowcount = -1; if (!ast_custom_function_find("CURL")) { ast_log(LOG_ERROR, "func_curl.so must be loaded in order to use res_config_curl.so!!\n"); return -1; } if (!(query = ast_str_thread_get(&query_buf, 1000))) { return -1; } if (!(buffer = ast_str_thread_get(&result_buf, 16))) { return -1; } ast_uri_encode(keyfield, buf1, sizeof(buf1), ast_uri_http); ast_uri_encode(lookup, buf2, sizeof(buf2), ast_uri_http); ast_str_set(&query, 0, "${CURL(%s/destroy,%s=%s&", url, buf1, buf2); for (field = fields; field; field = field->next) { ast_uri_encode(field->name, buf1, sizeof(buf1), ast_uri_http); ast_uri_encode(field->value, buf2, sizeof(buf2), ast_uri_http); ast_str_append(&query, 0, "%s%s=%s", !start ? "&" : "", buf1, buf2); start = 0; } ast_str_append(&query, 0, ")}"); ast_str_substitute_variables(&buffer, 0, NULL, ast_str_buffer(query)); /* Line oriented output */ stringp = ast_str_buffer(buffer); while (*stringp <= ' ') { stringp++; } sscanf(stringp, "%30d", &rowcount); if (rowcount >= 0) { return (int)rowcount; } return -1; }
static enum ast_test_result_state test_chan_function(struct ast_test *test, struct ast_channel *c, const char *expression) { int okay = 1; char workspace[4096]; struct ast_str *str = ast_str_create(16); ast_str_substitute_variables(&str, 0, c, expression); pbx_substitute_variables_helper(c, expression, workspace, sizeof(workspace)); ast_test_status_update(test, "Testing '%s' . . . . . %s\n", expression, okay ? "passed" : "FAILED"); if (strcmp(workspace, ast_str_buffer(str)) != 0) { ast_test_status_update(test, "test_chan_function, expr: '%s' ... %s != %s\n", expression, ast_str_buffer(str), workspace); okay = 0; } ast_free(str); return okay ? AST_TEST_PASS : AST_TEST_FAIL; }
static enum ast_test_result_state test_chan_integer(struct ast_test *test, struct ast_channel *c, int *ifield, const char *expression) { int i, okay = 1, value1 = -1, value2 = -1; char workspace[4096]; struct ast_str *str = ast_str_create(16); ast_test_status_update(test, "Testing '%s' . . . . . %s\n", expression, okay ? "passed" : "FAILED"); for (i = 0; i < 256; i++) { *ifield = i; ast_str_substitute_variables(&str, 0, c, expression); pbx_substitute_variables_helper(c, expression, workspace, sizeof(workspace)); if (sscanf(workspace, "%d", &value1) != 1 || value1 != i || sscanf(ast_str_buffer(str), "%d", &value2) != 1 || value2 != i) { ast_test_status_update(test, "%s != %s and/or %d != %d != %d\n", ast_str_buffer(str), workspace, value1, value2, i); okay = 0; } } ast_free(str); return okay ? AST_TEST_PASS : AST_TEST_FAIL; }
static enum ast_test_result_state test_expected_result(struct ast_test *test, struct ast_channel *c, const char *expression, const char *result) { struct ast_str *str = ast_str_create(16); int okay; ast_str_substitute_variables(&str, 0, c, expression); okay = !strcmp(ast_str_buffer(str), result); ast_test_status_update(test, "Testing '%s' ('%s') == '%s' . . . . . %s\n", ast_str_buffer(str), expression, result, okay ? "passed" : "FAILED"); if (!okay) { ast_test_status_update(test, "test_expected_result: '%s' != '%s'\n", ast_str_buffer(str), result); } ast_free(str); return okay ? AST_TEST_PASS : AST_TEST_FAIL; }
/*! * \brief Execute a curl query and return ast_variable list * \param url The base URL from which to retrieve data * \param unused Not currently used * \param fields list containing one or more field/operator/value set. * * \retval var on success * \retval NULL on failure */ static struct ast_variable *realtime_curl(const char *url, const char *unused, const struct ast_variable *fields) { struct ast_str *query, *buffer; char buf1[256], buf2[256]; const struct ast_variable *field; char *stringp, *pair, *key; unsigned int start = 1; struct ast_variable *var = NULL, *prev = NULL; if (!ast_custom_function_find("CURL")) { ast_log(LOG_ERROR, "func_curl.so must be loaded in order to use res_config_curl.so!!\n"); return NULL; } if (!(query = ast_str_thread_get(&query_buf, 16))) { return NULL; } if (!(buffer = ast_str_thread_get(&result_buf, 16))) { return NULL; } ast_str_set(&query, 0, "${CURL(%s/single,", url); for (field = fields; field; field = field->next) { ast_uri_encode(field->name, buf1, sizeof(buf1), ast_uri_http); ast_uri_encode(field->value, buf2, sizeof(buf2), ast_uri_http); ast_str_append(&query, 0, "%s%s=%s", !start ? "&" : "", buf1, buf2); start = 0; } ast_str_append(&query, 0, ")}"); ast_str_substitute_variables(&buffer, 0, NULL, ast_str_buffer(query)); /* Remove any trailing newline characters */ if ((stringp = strchr(ast_str_buffer(buffer), '\r')) || (stringp = strchr(ast_str_buffer(buffer), '\n'))) { *stringp = '\0'; } stringp = ast_str_buffer(buffer); while ((pair = strsep(&stringp, "&"))) { key = strsep(&pair, "="); ast_uri_decode(key, ast_uri_http); if (pair) { ast_uri_decode(pair, ast_uri_http); } if (!ast_strlen_zero(key)) { if (prev) { prev->next = ast_variable_new(key, S_OR(pair, ""), ""); if (prev->next) { prev = prev->next; } } else { prev = var = ast_variable_new(key, S_OR(pair, ""), ""); } } } return var; }
static struct ast_config *config_curl(const char *url, const char *unused, const char *file, struct ast_config *cfg, struct ast_flags flags, const char *sugg_incl, const char *who_asked) { struct ast_str *query, *buffer; char buf1[200]; char *stringp, *line, *pair, *key; int last_cat_metric = -1, cat_metric = -1; struct ast_category *cat = NULL; char *cur_cat = ""; char *category = "", *var_name = "", *var_val = ""; struct ast_flags loader_flags = { 0 }; if (!ast_custom_function_find("CURL")) { ast_log(LOG_ERROR, "func_curl.so must be loaded in order to use res_config_curl.so!!\n"); return NULL; } if (!(query = ast_str_thread_get(&query_buf, 100))) { return NULL; } if (!(buffer = ast_str_thread_get(&result_buf, 16))) { return NULL; } ast_uri_encode(file, buf1, sizeof(buf1), ast_uri_http); ast_str_set(&query, 0, "${CURL(%s/static?file=%s)}", url, buf1); /* Do the CURL query */ ast_str_substitute_variables(&buffer, 0, NULL, ast_str_buffer(query)); /* Line oriented output */ stringp = ast_str_buffer(buffer); cat = ast_config_get_current_category(cfg); while ((line = strsep(&stringp, "\r\n"))) { if (ast_strlen_zero(line)) { continue; } while ((pair = strsep(&line, "&"))) { key = strsep(&pair, "="); ast_uri_decode(key, ast_uri_http); if (pair) { ast_uri_decode(pair, ast_uri_http); } if (!strcasecmp(key, "category")) { category = S_OR(pair, ""); } else if (!strcasecmp(key, "var_name")) { var_name = S_OR(pair, ""); } else if (!strcasecmp(key, "var_val")) { var_val = S_OR(pair, ""); } else if (!strcasecmp(key, "cat_metric")) { cat_metric = pair ? atoi(pair) : 0; } } if (!strcmp(var_name, "#include")) { if (!ast_config_internal_load(var_val, cfg, loader_flags, "", who_asked)) return NULL; } if (!cat || strcmp(category, cur_cat) || last_cat_metric != cat_metric) { if (!(cat = ast_category_new(category, "", 99999))) break; cur_cat = category; last_cat_metric = cat_metric; ast_category_append(cfg, cat); } ast_variable_append(cat, ast_variable_new(var_name, var_val, "")); } return cfg; }
/*! * \brief Excute an Select query and return ast_config list * \param url * \param unused * \param fields list containing one or more field/operator/value set. * * \retval struct ast_config pointer on success * \retval NULL on failure */ static struct ast_config *realtime_multi_curl(const char *url, const char *unused, const struct ast_variable *fields) { struct ast_str *query, *buffer; char buf1[256], buf2[256]; const struct ast_variable *field; char *stringp, *line, *pair, *key, *initfield = NULL; int start = 1; struct ast_variable *var = NULL; struct ast_config *cfg = NULL; struct ast_category *cat = NULL; if (!ast_custom_function_find("CURL")) { ast_log(LOG_ERROR, "func_curl.so must be loaded in order to use res_config_curl.so!!\n"); return NULL; } if (!(query = ast_str_thread_get(&query_buf, 16))) { return NULL; } if (!(buffer = ast_str_thread_get(&result_buf, 16))) { return NULL; } ast_str_set(&query, 0, "${CURL(%s/multi,", url); for (field = fields; field; field = field->next) { if (start) { char *op; initfield = ast_strdupa(field->name); if ((op = strchr(initfield, ' '))) *op = '\0'; } ast_uri_encode(field->name, buf1, sizeof(buf1), ast_uri_http); ast_uri_encode(field->value, buf2, sizeof(buf2), ast_uri_http); ast_str_append(&query, 0, "%s%s=%s", !start ? "&" : "", buf1, buf2); start = 0; } ast_str_append(&query, 0, ")}"); /* Do the CURL query */ ast_str_substitute_variables(&buffer, 0, NULL, ast_str_buffer(query)); if (!(cfg = ast_config_new())) { return NULL; } /* Line oriented output */ stringp = ast_str_buffer(buffer); while ((line = strsep(&stringp, "\r\n"))) { if (ast_strlen_zero(line)) { continue; } if (!(cat = ast_category_new("", "", 99999))) { continue; } while ((pair = strsep(&line, "&"))) { key = strsep(&pair, "="); ast_uri_decode(key, ast_uri_http); if (pair) { ast_uri_decode(pair, ast_uri_http); } if (!strcasecmp(key, initfield) && pair) { ast_category_rename(cat, pair); } if (!ast_strlen_zero(key)) { var = ast_variable_new(key, S_OR(pair, ""), ""); ast_variable_append(cat, var); } } ast_category_append(cfg, cat); } return cfg; }
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; }