static struct ast_exten *find_matching_priority(struct ast_context *c, const char *exten, int priority, const char *callerid) { struct ast_exten *e; struct ast_include *i; struct ast_context *c2; for (e=ast_walk_context_extensions(c, NULL); e; e=ast_walk_context_extensions(c, e)) { if (ast_extension_match(ast_get_extension_name(e), exten)) { int needmatch = ast_get_extension_matchcid(e); if ((needmatch && ast_extension_match(ast_get_extension_cidmatch(e), callerid)) || (!needmatch)) { /* This is the matching extension we want */ struct ast_exten *p; for (p=ast_walk_extension_priorities(e, NULL); p; p=ast_walk_extension_priorities(e, p)) { if (priority != ast_get_extension_priority(p)) continue; return p; } } } } /* No match; run through includes */ for (i=ast_walk_context_includes(c, NULL); i; i=ast_walk_context_includes(c, i)) { for (c2=ast_walk_contexts(NULL); c2; c2=ast_walk_contexts(c2)) { if (!strcmp(ast_get_context_name(c2), ast_get_include_name(i))) { e = find_matching_priority(c2, exten, priority, callerid); if (e) return e; } } } return NULL; }
static char *complete_context_add_ignorepat(char *line, char *word, int pos, int state) { if (pos == 3) return state == 0 ? strdup("into") : NULL; if (pos == 4) { struct ast_context *c; int which = 0; char *dupline, *duplinet, *ignorepat = NULL; dupline = strdup(line); duplinet = dupline; if (duplinet) { strsep(&duplinet, " "); /* skip 'add' */ strsep(&duplinet, " "); /* skip 'ignorepat' */ ignorepat = strsep(&duplinet, " "); } if (ast_lock_contexts()) { ast_log(LOG_ERROR, "Failed to lock contexts list\n"); return NULL; } c = ast_walk_contexts(NULL); while (c) { if (!strncmp(ast_get_context_name(c), word, strlen(word))) { int serve_context = 1; if (ignorepat) { if (!ast_lock_context(c)) { struct ast_ignorepat *ip; ip = ast_walk_context_ignorepats(c, NULL); while (ip && serve_context) { if (!strcmp(ast_get_ignorepat_name(ip), ignorepat)) serve_context = 0; ip = ast_walk_context_ignorepats(c, ip); } ast_unlock_context(c); } } if (serve_context) { if (++which > state) { char *context = strdup(ast_get_context_name(c)); if (dupline) free(dupline); ast_unlock_contexts(); return context; } } } c = ast_walk_contexts(c); } if (dupline) free(dupline); ast_unlock_contexts(); return NULL; } return NULL; }
static int pbx_load_module(void) { int errs=0, sem_err=0, sem_warn=0, sem_note=0; char *rfilename; struct ast_context *local_contexts=NULL, *con; struct ast_hashtab *local_table=NULL; struct pval *parse_tree; ast_log(LOG_NOTICE, "Starting AEL load process.\n"); if (config[0] == '/') rfilename = (char *)config; else { rfilename = alloca(strlen(config) + strlen(ast_config_AST_CONFIG_DIR) + 2); sprintf(rfilename, "%s/%s", ast_config_AST_CONFIG_DIR, config); } if (access(rfilename,R_OK) != 0) { ast_log(LOG_NOTICE, "File %s not found; AEL declining load\n", rfilename); return AST_MODULE_LOAD_DECLINE; } parse_tree = ael2_parse(rfilename, &errs); ast_log(LOG_NOTICE, "AEL load process: parsed config file name '%s'.\n", rfilename); ael2_semantic_check(parse_tree, &sem_err, &sem_warn, &sem_note); if (errs == 0 && sem_err == 0) { ast_log(LOG_NOTICE, "AEL load process: checked config file name '%s'.\n", rfilename); local_table = ast_hashtab_create(11, ast_hashtab_compare_contexts, ast_hashtab_resize_java, ast_hashtab_newsize_java, ast_hashtab_hash_contexts, 0); ast_compile_ael2(&local_contexts, local_table, parse_tree); ast_log(LOG_NOTICE, "AEL load process: compiled config file name '%s'.\n", rfilename); ast_merge_contexts_and_delete(&local_contexts, local_table, registrar); local_table = NULL; /* it's the dialplan global now */ local_contexts = NULL; ast_log(LOG_NOTICE, "AEL load process: merged config file name '%s'.\n", rfilename); for (con = ast_walk_contexts(NULL); con; con = ast_walk_contexts(con)) ast_context_verify_includes(con); ast_log(LOG_NOTICE, "AEL load process: verified config file name '%s'.\n", rfilename); } else { ast_log(LOG_ERROR, "Sorry, but %d syntax errors and %d semantic errors were detected. It doesn't make sense to compile.\n", errs, sem_err); destroy_pval(parse_tree); /* free up the memory */ return AST_MODULE_LOAD_DECLINE; } destroy_pval(parse_tree); /* free up the memory */ return AST_MODULE_LOAD_SUCCESS; }
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), chan->context)) { /* This is the matching context we want */ int cur_priority = chan->priority + 1, level=1; for (e = find_matching_priority(c, chan->exten, cur_priority, S_COR(chan->caller.id.number.valid, chan->caller.id.number.str, NULL)); e; e = find_matching_priority(c, chan->exten, ++cur_priority, S_COR(chan->caller.id.number.valid, chan->caller.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; }
/* add extension 6123,1,Dial,IAX/212.71.138.13/6123 into local */ static char *complete_context_add_extension(char *line, char *word, int pos, int state) { int which = 0; /* complete 'into' word ... */ if (pos == 3) { if (state == 0) return strdup("into"); return NULL; } /* complete context */ if (pos == 4) { struct ast_context *c; /* try to lock contexts list ... */ if (ast_lock_contexts()) { ast_log(LOG_WARNING, "Failed to lock contexts list\n"); return NULL; } /* walk through all contexts */ c = ast_walk_contexts(NULL); while (c) { /* matching context? */ if (!strncmp(ast_get_context_name(c), word, strlen(word))) { if (++which > state) { char *res = strdup(ast_get_context_name(c)); ast_unlock_contexts(); return res; } } c = ast_walk_contexts(c); } ast_unlock_contexts(); return NULL; } if (pos == 5) return state == 0 ? strdup("replace") : NULL; return NULL; }
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; }
/* * 'save dialplan' CLI command implementation functions ... */ static int handle_save_dialplan(int fd, int argc, char *argv[]) { char filename[256]; struct ast_context *c; struct ast_config *cfg; struct ast_variable *v; int context_header_written; int incomplete = 0; /* incomplete config write? */ FILE *output; if (! (static_config && !write_protect_config)) { ast_cli(fd, "I can't save dialplan now, see '%s' example file.\n", config); return RESULT_FAILURE; } if (argc != 2 && argc != 3) return RESULT_SHOWUSAGE; if (ast_mutex_lock(&save_dialplan_lock)) { ast_cli(fd, "Failed to lock dialplan saving (another proccess saving?)\n"); return RESULT_FAILURE; } /* have config path? */ if (argc == 3) { /* is there extension.conf too? */ if (!strstr(argv[2], ".conf")) { /* no, only directory path, check for last '/' occurence */ if (*(argv[2] + strlen(argv[2]) -1) == '/') snprintf(filename, sizeof(filename), "%s%s", argv[2], config); else /* without config extensions.conf, add it */ snprintf(filename, sizeof(filename), "%s/%s", argv[2], config); } else /* there is an .conf */ snprintf(filename, sizeof(filename), argv[2]); } else /* no config file, default one */ snprintf(filename, sizeof(filename), "%s/%s", (char *)ast_config_AST_CONFIG_DIR, config); cfg = ast_load("extensions.conf"); /* try to lock contexts list */ if (ast_lock_contexts()) { ast_cli(fd, "Failed to lock contexts list\n"); ast_mutex_unlock(&save_dialplan_lock); ast_destroy(cfg); return RESULT_FAILURE; } /* create new file ... */ if (!(output = fopen(filename, "wt"))) { ast_cli(fd, "Failed to create file '%s'\n", filename); ast_unlock_contexts(); ast_mutex_unlock(&save_dialplan_lock); ast_destroy(cfg); return RESULT_FAILURE; } /* fireout general info */ fprintf(output, "[general]\nstatic=%s\nwriteprotect=%s\n\n", static_config ? "yes" : "no", write_protect_config ? "yes" : "no"); if ((v = ast_variable_browse(cfg, "globals"))) { fprintf(output, "[globals]\n"); while(v) { fprintf(output, "%s => %s\n", v->name, v->value); v = v->next; } fprintf(output, "\n"); } ast_destroy(cfg); /* walk all contexts */ c = ast_walk_contexts(NULL); while (c) { context_header_written = 0; /* try to lock context and fireout all info */ if (!ast_lock_context(c)) { struct ast_exten *e, *last_written_e = NULL; struct ast_include *i; struct ast_ignorepat *ip; struct ast_sw *sw; /* registered by this module? */ if (!strcmp(ast_get_context_registrar(c), registrar)) { fprintf(output, "[%s]\n", ast_get_context_name(c)); context_header_written = 1; } /* walk extensions ... */ e = ast_walk_context_extensions(c, NULL); while (e) { struct ast_exten *p; /* fireout priorities */ p = ast_walk_extension_priorities(e, NULL); while (p) { if (!strcmp(ast_get_extension_registrar(p), registrar)) { /* make empty line between different extensions */ if (last_written_e != NULL && strcmp(ast_get_extension_name(last_written_e), ast_get_extension_name(p))) fprintf(output, "\n"); last_written_e = p; if (!context_header_written) { fprintf(output, "[%s]\n", ast_get_context_name(c)); context_header_written = 1; } if (ast_get_extension_priority(p)!=PRIORITY_HINT) { char *tempdata = NULL, *startdata; tempdata = strdup((char *)ast_get_extension_app_data(p)); if (tempdata) { startdata = tempdata; while (*tempdata) { if (*tempdata == '|') *tempdata = ','; tempdata++; } tempdata = startdata; } if (ast_get_extension_matchcid(p)) fprintf(output, "exten => %s/%s,%d,%s(%s)\n", ast_get_extension_name(p), ast_get_extension_cidmatch(p), ast_get_extension_priority(p), ast_get_extension_app(p), tempdata); else fprintf(output, "exten => %s,%d,%s(%s)\n", ast_get_extension_name(p), ast_get_extension_priority(p), ast_get_extension_app(p), tempdata); if (tempdata) free(tempdata); } else fprintf(output, "exten => %s,hint,%s\n", ast_get_extension_name(p), ast_get_extension_app(p)); } p = ast_walk_extension_priorities(e, p); } e = ast_walk_context_extensions(c, e); } /* written any extensions? ok, write space between exten & inc */ if (last_written_e) fprintf(output, "\n"); /* walk through includes */ i = ast_walk_context_includes(c, NULL); while (i) { if (!strcmp(ast_get_include_registrar(i), registrar)) { if (!context_header_written) { fprintf(output, "[%s]\n", ast_get_context_name(c)); context_header_written = 1; } fprintf(output, "include => %s\n", ast_get_include_name(i)); } i = ast_walk_context_includes(c, i); } if (ast_walk_context_includes(c, NULL)) fprintf(output, "\n"); /* walk through switches */ sw = ast_walk_context_switches(c, NULL); while (sw) { if (!strcmp(ast_get_switch_registrar(sw), registrar)) { if (!context_header_written) { fprintf(output, "[%s]\n", ast_get_context_name(c)); context_header_written = 1; } fprintf(output, "switch => %s/%s\n", ast_get_switch_name(sw), ast_get_switch_data(sw)); } sw = ast_walk_context_switches(c, sw); } if (ast_walk_context_switches(c, NULL)) fprintf(output, "\n"); /* fireout ignorepats ... */ ip = ast_walk_context_ignorepats(c, NULL); while (ip) { if (!strcmp(ast_get_ignorepat_registrar(ip), registrar)) { if (!context_header_written) { fprintf(output, "[%s]\n", ast_get_context_name(c)); context_header_written = 1; } fprintf(output, "ignorepat => %s\n", ast_get_ignorepat_name(ip)); } ip = ast_walk_context_ignorepats(c, ip); } ast_unlock_context(c); } else incomplete = 1; c = ast_walk_contexts(c); } ast_unlock_contexts(); ast_mutex_unlock(&save_dialplan_lock); fclose(output); if (incomplete) { ast_cli(fd, "Saved dialplan is incomplete\n"); return RESULT_FAILURE; } ast_cli(fd, "Dialplane successfully saved into '%s'\n", filename); return RESULT_SUCCESS; }
static char *complete_context_add_include(char *line, char *word, int pos, int state) { struct ast_context *c; int which = 0; /* server context for inclusion ... */ if (pos == 1) { if (ast_lock_contexts()) { ast_log(LOG_ERROR, "Failed to lock context list\n"); return NULL; } /* server all contexts */ c = ast_walk_contexts(NULL); while (c) { if ((!strlen(word) || !strncmp(ast_get_context_name(c), word, strlen(word))) && ++which > state) { char *context = strdup(ast_get_context_name(c)); ast_unlock_contexts(); return context; } c = ast_walk_contexts(c); } ast_unlock_contexts(); } /* complete 'in' only if context exist ... */ if (pos == 2) { char *context, *dupline, *duplinet; if (state != 0) return NULL; /* parse context from line ... */ if (!(dupline = strdup(line))) { ast_log(LOG_ERROR, "Out of free memory\n"); if (state == 0) return strdup("in"); return NULL; } duplinet = dupline; strsep(&duplinet, " "); context = strsep(&duplinet, " "); if (context) { struct ast_context *c; int context_existence = 0; /* check for context existence ... */ if (ast_lock_contexts()) { ast_log(LOG_ERROR, "Failed to lock context list\n"); free(dupline); /* our fault, we can't check, so complete 'in' ... */ return strdup("in"); } c = ast_walk_contexts(NULL); while (c && !context_existence) { if (!strcmp(context, ast_get_context_name(c))) { context_existence = 1; continue; } c = ast_walk_contexts(c); } /* if context exists, return 'into' ... */ if (context_existence) { free(dupline); ast_unlock_contexts(); return strdup("into"); } ast_unlock_contexts(); } free(dupline); return NULL; } /* serve context into which we include another context */ if (pos == 3) { char *context, *dupline, *duplinet, *in; int context_existence = 0; if (!(dupline = strdup(line))) { ast_log(LOG_ERROR, "Out of free memory\n"); return NULL; } duplinet = dupline; strsep(&duplinet, " "); /* skip 'include' */ context = strsep(&duplinet, " "); in = strsep(&duplinet, " "); /* given some context and third word is in? */ if (!strlen(context) || strcmp(in, "in")) { free(dupline); return NULL; } if (ast_lock_contexts()) { ast_log(LOG_ERROR, "Failed to lock context list\n"); free(dupline); return NULL; } /* check for context existence ... */ c = ast_walk_contexts(NULL); while (c && !context_existence) { if (!strcmp(context, ast_get_context_name(c))) { context_existence = 1; continue; } c = ast_walk_contexts(c); } if (!context_existence) { free(dupline); ast_unlock_contexts(); return NULL; } /* go through all contexts ... */ c = ast_walk_contexts(NULL); while (c) { /* must be different contexts ... */ if (strcmp(context, ast_get_context_name(c))) { if (!ast_lock_context(c)) { struct ast_include *i; int included = 0; /* check for duplicity inclusion ... */ i = ast_walk_context_includes(c, NULL); while (i && !included) { if (!strcmp(ast_get_include_name(i), context)) included = 1; i = ast_walk_context_includes(c, i); } ast_unlock_context(c); /* not included yet, so show possibility ... */ if (!included && !strncmp(ast_get_context_name(c), word, strlen(word))){ if (++which > state) { char *res = strdup(ast_get_context_name(c)); free(dupline); ast_unlock_contexts(); return res; } } } } c = ast_walk_contexts(c); } ast_unlock_contexts(); free(dupline); return NULL; } return NULL; }
static char *complete_context_remove_extension(char *line, char *word, int pos, int state) { char *ret = NULL; int which = 0; #ifdef BROKEN_READLINE /* * Fix arguments, *word is a new allocated structure, REMEMBER to * free *word when you want to return from this function ... */ if (fix_complete_args(line, &word, &pos)) { ast_log(LOG_ERROR, "Out of free memory\n"); return NULL; } #endif /* * exten@context completion ... */ if (pos == 2) { struct ast_context *c; struct ast_exten *e; char *context = NULL, *exten = NULL, *delim = NULL; /* now, parse values from word = exten@context */ if ((delim = strchr(word, (int)'@'))) { /* check for duplicity ... */ if (delim != strrchr(word, (int)'@')) { #ifdef BROKEN_READLINE free(word); #endif return NULL; } *delim = '\0'; exten = strdup(word); context = strdup(delim + 1); *delim = '@'; } else { exten = strdup(word); } #ifdef BROKEN_READLINE free(word); #endif if (ast_lock_contexts()) { ast_log(LOG_ERROR, "Failed to lock context list\n"); free(context); free(exten); return NULL; } /* find our context ... */ c = ast_walk_contexts(NULL); while (c) { /* our context? */ if ( (!context || !strlen(context)) || /* if no input, all contexts ... */ (context && !strncmp(ast_get_context_name(c), context, strlen(context))) ) { /* if input, compare ... */ /* try to complete extensions ... */ e = ast_walk_context_extensions(c, NULL); while (e) { /* our extension? */ if ( (!exten || !strlen(exten)) || /* if not input, all extensions ... */ (exten && !strncmp(ast_get_extension_name(e), exten, strlen(exten))) ) { /* if input, compare ... */ if (++which > state) { /* If there is an extension then return * exten@context. */ if (exten) { ret = malloc(strlen(ast_get_extension_name(e)) + strlen(ast_get_context_name(c)) + 2); if (ret) sprintf(ret, "%s@%s", ast_get_extension_name(e), ast_get_context_name(c)); } free(exten); free(context); ast_unlock_contexts(); return ret; } } e = ast_walk_context_extensions(c, e); } } c = ast_walk_contexts(c); } ast_unlock_contexts(); free(exten); free(context); return NULL; } /* * Complete priority ... */ if (pos == 3) { char *delim, *exten, *context, *dupline, *duplinet, *ec; struct ast_context *c; dupline = strdup(line); if (!dupline) { #ifdef BROKEN_READLINE free(word); #endif return NULL; } duplinet = dupline; strsep(&duplinet, " "); /* skip 'remove' */ strsep(&duplinet, " "); /* skip 'extension */ if (!(ec = strsep(&duplinet, " "))) { free(dupline); #ifdef BROKEN_READLINE free(word); #endif return NULL; } /* wrong exten@context format? */ if (!(delim = strchr(ec, (int)'@')) || (strchr(ec, (int)'@') != strrchr(ec, (int)'@'))) { #ifdef BROKEN_READLINE free(word); #endif free(dupline); return NULL; } /* check if there is exten and context too ... */ *delim = '\0'; if ((!strlen(ec)) || (!strlen(delim + 1))) { #ifdef BROKEN_READLINE free(word); #endif free(dupline); return NULL; } exten = strdup(ec); context = strdup(delim + 1); free(dupline); if (ast_lock_contexts()) { ast_log(LOG_ERROR, "Failed to lock context list\n"); #ifdef BROKEN_READLINE free(word); #endif free(exten); free(context); return NULL; } /* walk contexts */ c = ast_walk_contexts(NULL); while (c) { if (!strcmp(ast_get_context_name(c), context)) { struct ast_exten *e; /* walk extensions */ free(context); e = ast_walk_context_extensions(c, NULL); while (e) { if (!strcmp(ast_get_extension_name(e), exten)) { struct ast_exten *priority; char buffer[10]; free(exten); priority = ast_walk_extension_priorities(e, NULL); /* serve priorities */ do { snprintf(buffer, 10, "%u", ast_get_extension_priority(priority)); if (!strncmp(word, buffer, strlen(word))) { if (++which > state) { #ifdef BROKEN_READLINE free(word); #endif ast_unlock_contexts(); return strdup(buffer); } } priority = ast_walk_extension_priorities(e, priority); } while (priority); #ifdef BROKEN_READLINE free(word); #endif ast_unlock_contexts(); return NULL; } e = ast_walk_context_extensions(c, e); } #ifdef BROKEN_READLINE free(word); #endif free(exten); ast_unlock_contexts(); return NULL; } c = ast_walk_contexts(c); } #ifdef BROKEN_READLINE free(word); #endif free(exten); free(context); ast_unlock_contexts(); return NULL; } #ifdef BROKEN_READLINE free(word); #endif return NULL; }
static int pbx_load_module(void) { struct ast_config *cfg; struct ast_variable *v; char *cxt, *ext, *pri, *appl, *data, *tc, *cidmatch; struct ast_context *con; char *start, *end; char realvalue[256]; cfg = ast_load(config); if (cfg) { /* Use existing config to populate the PBX table */ static_config = ast_true(ast_variable_retrieve(cfg, "general", "static")); write_protect_config = ast_true(ast_variable_retrieve(cfg, "general", "writeprotect")); v = ast_variable_browse(cfg, "globals"); while(v) { memset(realvalue, 0, sizeof(realvalue)); pbx_substitute_variables_helper(NULL, v->value, realvalue, sizeof(realvalue) - 1); pbx_builtin_setvar_helper(NULL, v->name, realvalue); v = v->next; } cxt = ast_category_browse(cfg, NULL); while(cxt) { /* All categories but "general" or "globals" are considered contexts */ if (!strcasecmp(cxt, "general") || !strcasecmp(cxt, "globals")) { cxt = ast_category_browse(cfg, cxt); continue; } if ((con=ast_context_create(&local_contexts,cxt, registrar))) { v = ast_variable_browse(cfg, cxt); while(v) { if (!strcasecmp(v->name, "exten")) { char *stringp=NULL; int ipri = -2; char realext[256]=""; tc = strdup(v->value); if(tc!=NULL){ stringp=tc; ext = strsep(&stringp, ","); if (!ext) ext=""; pri = strsep(&stringp, ","); if (!pri) pri=""; if (!strcmp(pri,"hint")) ipri=PRIORITY_HINT; else { if (sscanf(pri, "%i", &ipri) != 1) { ast_log(LOG_WARNING, "Invalid priority '%s' at line %d\n", pri, v->lineno); ipri = 0; } } appl = stringp; if (!appl) appl=""; if (!(start = strchr(appl, '('))) { if (stringp) appl = strsep(&stringp, ","); else appl = ""; } if (start && (end = strrchr(appl, ')'))) { *start = *end = '\0'; data = start + 1; process_quotes_and_slashes(data, ',', '|'); } else if (stringp!=NULL && *stringp=='"') { stringp++; data = strsep(&stringp, "\""); stringp++; } else { if (stringp) data = strsep(&stringp, ","); else data = ""; } cidmatch = strchr(ext, '/'); if (cidmatch) { *cidmatch = '\0'; cidmatch++; } stringp=ext; strsep(&stringp, "/"); if (!data) data=""; while(*appl && (*appl < 33)) appl++; pbx_substitute_variables_helper(NULL, ext, realext, sizeof(realext) - 1); if (ipri) { if (ast_add_extension2(con, 0, realext, ipri, cidmatch, appl, strdup(data), FREE, registrar)) { ast_log(LOG_WARNING, "Unable to register extension at line %d\n", v->lineno); } } free(tc); } else fprintf(stderr,"Error strdup returned NULL in %s\n",__PRETTY_FUNCTION__); } else if(!strcasecmp(v->name, "include")) { memset(realvalue, 0, sizeof(realvalue)); pbx_substitute_variables_helper(NULL, v->value, realvalue, sizeof(realvalue) - 1); if (ast_context_add_include2(con, realvalue, registrar)) ast_log(LOG_WARNING, "Unable to include context '%s' in context '%s'\n", v->value, cxt); } else if(!strcasecmp(v->name, "ignorepat")) { memset(realvalue, 0, sizeof(realvalue)); pbx_substitute_variables_helper(NULL, v->value, realvalue, sizeof(realvalue) - 1); if (ast_context_add_ignorepat2(con, realvalue, registrar)) ast_log(LOG_WARNING, "Unable to include ignorepat '%s' in context '%s'\n", v->value, cxt); } else if (!strcasecmp(v->name, "switch")) { char *stringp=NULL; memset(realvalue, 0, sizeof(realvalue)); pbx_substitute_variables_helper(NULL, v->value, realvalue, sizeof(realvalue) - 1); tc = realvalue; stringp=tc; appl = strsep(&stringp, "/"); data = strsep(&stringp, ""); if (!data) data = ""; if (ast_context_add_switch2(con, appl, data, registrar)) ast_log(LOG_WARNING, "Unable to include switch '%s' in context '%s'\n", v->value, cxt); } v = v->next; } } cxt = ast_category_browse(cfg, cxt); } ast_destroy(cfg); } ast_merge_contexts_and_delete(&local_contexts,registrar); for (con = ast_walk_contexts(NULL); con; con = ast_walk_contexts(con)) ast_context_verify_includes(con); return 0; }
static char *complete_context_dont_include(char *line, char *word, int pos, int state) { int which = 0; /* * Context completion ... */ if (pos == 2) { struct ast_context *c; if (ast_lock_contexts()) { ast_log(LOG_ERROR, "Failed to lock context list\n"); return NULL; } /* walk pbx_get_contexts ... */ c = ast_walk_contexts(NULL); while (c) { struct ast_include *i; if (ast_lock_context(c)) { c = ast_walk_contexts(c); continue; } i = ast_walk_context_includes(c, NULL); while (i) { if (!strlen(word) || !strncmp(ast_get_include_name(i), word, strlen(word))) { struct ast_context *nc; int already_served = 0; /* check if this include is already served or not */ /* go through all contexts again till we reach actuall * context or already_served = 1 */ nc = ast_walk_contexts(NULL); while (nc && nc != c && !already_served) { if (!ast_lock_context(nc)) { struct ast_include *ni; ni = ast_walk_context_includes(nc, NULL); while (ni && !already_served) { if (!strcmp(ast_get_include_name(i), ast_get_include_name(ni))) already_served = 1; ni = ast_walk_context_includes(nc, ni); } ast_unlock_context(nc); } nc = ast_walk_contexts(nc); } if (!already_served) { if (++which > state) { char *res = strdup(ast_get_include_name(i)); ast_unlock_context(c); ast_unlock_contexts(); return res; } } } i = ast_walk_context_includes(c, i); } ast_unlock_context(c); c = ast_walk_contexts(c); } ast_unlock_contexts(); return NULL; } /* * 'in' completion ... (complete only if previous context is really * included somewhere) */ if (pos == 3) { struct ast_context *c; char *context, *dupline, *duplinet; if (state > 0) return NULL; /* take 'context' from line ... */ if (!(dupline = strdup(line))) { ast_log(LOG_ERROR, "Out of free memory\n"); return NULL; } duplinet = dupline; strsep(&duplinet, " "); /* skip 'dont' */ strsep(&duplinet, " "); /* skip 'include' */ context = strsep(&duplinet, " "); if (!context) { free(dupline); return NULL; } if (ast_lock_contexts()) { ast_log(LOG_WARNING, "Failed to lock contexts list\n"); free(dupline); return NULL; } /* go through all contexts and check if is included ... */ c = ast_walk_contexts(NULL); while (c) { struct ast_include *i; if (ast_lock_context(c)) { free(dupline); ast_unlock_contexts(); return NULL; } i = ast_walk_context_includes(c, NULL); while (i) { /* is it our context? */ if (!strcmp(ast_get_include_name(i), context)) { /* yes, it is, context is really included, so * complete "in" command */ free(dupline); ast_unlock_context(c); ast_unlock_contexts(); return strdup("in"); } i = ast_walk_context_includes(c, i); } ast_unlock_context(c); c = ast_walk_contexts(c); } free(dupline); ast_unlock_contexts(); return NULL; } /* * Context from which we removing include ... */ if (pos == 4) { struct ast_context *c; char *context, *dupline, *duplinet, *in; if (!(dupline = strdup(line))) { ast_log(LOG_ERROR, "Out of free memory\n"); return NULL; } duplinet = dupline; strsep(&duplinet, " "); /* skip 'dont' */ strsep(&duplinet, " "); /* skip 'include' */ if (!(context = strsep(&duplinet, " "))) { free(dupline); return NULL; } /* third word must be in */ in = strsep(&duplinet, " "); if (!in || strcmp(in, "in")) { free(dupline); return NULL; } if (ast_lock_contexts()) { ast_log(LOG_ERROR, "Failed to lock context list\n"); free(dupline); return NULL; } /* walk through all contexts ... */ c = ast_walk_contexts(NULL); while (c) { struct ast_include *i; if (ast_lock_context(c)) { free(dupline); return NULL; } /* walk through all includes and check if it is our context */ i = ast_walk_context_includes(c, NULL); while (i) { /* is in this context included another on which we want to * remove? */ if (!strcmp(context, ast_get_include_name(i))) { /* yes, it's included, is matching our word too? */ if (!strncmp(ast_get_context_name(c), word, strlen(word))) { /* check state for completion */ if (++which > state) { char *res = strdup(ast_get_context_name(c)); free(dupline); ast_unlock_context(c); ast_unlock_contexts(); return res; } } break; } i = ast_walk_context_includes(c, i); } ast_unlock_context(c); c = ast_walk_contexts(c); } free(dupline); ast_unlock_contexts(); return NULL; } return NULL; }
static char *complete_context_remove_ignorepat(char *line, char *word, int pos, int state) { struct ast_context *c; int which = 0; if (pos == 2) { if (ast_lock_contexts()) { ast_log(LOG_WARNING, "Failed to lock contexts list\n"); return NULL; } c = ast_walk_contexts(NULL); while (c) { if (!ast_lock_context(c)) { struct ast_ignorepat *ip; ip = ast_walk_context_ignorepats(c, NULL); while (ip) { if (!strncmp(ast_get_ignorepat_name(ip), word, strlen(word))) { if (which + 1 > state) { struct ast_context *cw; int already_served = 0; cw = ast_walk_contexts(NULL); while (cw && cw != c && !already_served) { if (!ast_lock_context(cw)) { struct ast_ignorepat *ipw; ipw = ast_walk_context_ignorepats(cw, NULL); while (ipw) { if (!strcmp(ast_get_ignorepat_name(ipw), ast_get_ignorepat_name(ip))) already_served = 1; ipw = ast_walk_context_ignorepats(cw, ipw); } ast_unlock_context(cw); } cw = ast_walk_contexts(cw); } if (!already_served) { char *ret = strdup(ast_get_ignorepat_name(ip)); ast_unlock_context(c); ast_unlock_contexts(); return ret; } } else which++; } ip = ast_walk_context_ignorepats(c, ip); } ast_unlock_context(c); } c = ast_walk_contexts(c); } ast_unlock_contexts(); return NULL; } if (pos == 3) return state == 0 ? strdup("from") : NULL; if (pos == 4) { char *dupline, *duplinet, *ignorepat; dupline = strdup(line); if (!dupline) { ast_log(LOG_WARNING, "Out of free memory\n"); return NULL; } duplinet = dupline; strsep(&duplinet, " "); strsep(&duplinet, " "); ignorepat = strsep(&duplinet, " "); if (!ignorepat) { free(dupline); return NULL; } if (ast_lock_contexts()) { ast_log(LOG_WARNING, "Failed to lock contexts list\n"); free(dupline); return NULL; } c = ast_walk_contexts(NULL); while (c) { if (!ast_lock_context(c)) { struct ast_ignorepat *ip; ip = ast_walk_context_ignorepats(c, NULL); while (ip) { if (!strcmp(ast_get_ignorepat_name(ip), ignorepat)) { if (!strncmp(ast_get_context_name(c), word, strlen(word))) { if (++which > state) { char *ret = strdup(ast_get_context_name(c)); free(dupline); ast_unlock_context(c); ast_unlock_contexts(); return ret; } } } ip = ast_walk_context_ignorepats(c, ip); } ast_unlock_context(c); } c = ast_walk_contexts(c); } free(dupline); ast_unlock_contexts(); return NULL; } return NULL; }