2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2006, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
21 * \brief Populate and remember extensions from static config file
28 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
32 #include "asterisk/paths.h" /* ast_config_AST_CONFIG_DIR */
33 #include "asterisk/pbx.h"
34 #include "asterisk/config.h"
35 #include "asterisk/module.h"
36 #include "asterisk/logger.h"
37 #include "asterisk/cli.h"
38 #include "asterisk/channel.h" /* AST_MAX_EXTENSION */
39 #include "asterisk/callerid.h"
41 static const char config[] = "extensions.conf";
42 static const char registrar[] = "pbx_config";
43 static char userscontext[AST_MAX_EXTENSION] = "default";
45 static int static_config = 0;
46 static int write_protect_config = 1;
47 static int autofallthrough_config = 1;
48 static int clearglobalvars_config = 0;
49 static int extenpatternmatchnew_config = 0;
50 static char *overrideswitch_config = NULL;
52 AST_MUTEX_DEFINE_STATIC(save_dialplan_lock);
54 static struct ast_context *local_contexts = NULL;
55 static struct ast_hashtab *local_table = NULL;
57 * Prototypes for our completion functions
59 static char *complete_dialplan_remove_include(struct ast_cli_args *);
60 static char *complete_dialplan_add_include(struct ast_cli_args *);
61 static char *complete_dialplan_remove_ignorepat(struct ast_cli_args *);
62 static char *complete_dialplan_add_ignorepat(struct ast_cli_args *);
63 static char *complete_dialplan_remove_extension(struct ast_cli_args *);
64 static char *complete_dialplan_add_extension(struct ast_cli_args *);
67 * Implementation of functions provided by this module
71 * REMOVE INCLUDE command stuff
73 static char *handle_cli_dialplan_remove_include(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
77 e->command = "dialplan remove include";
79 "Usage: dialplan remove include <context> from <context>\n"
80 " Remove an included context from another context.\n";
83 return complete_dialplan_remove_include(a);
86 if (strcmp(a->argv[4], "from"))
89 if (!ast_context_remove_include(a->argv[5], a->argv[3], registrar)) {
90 ast_cli(a->fd, "We are not including '%s' into '%s' now\n",
91 a->argv[3], a->argv[5]);
95 ast_cli(a->fd, "Failed to remove '%s' include from '%s' context\n",
96 a->argv[3], a->argv[5]);
100 /*! \brief return true if 'name' is included by context c */
101 static int lookup_ci(struct ast_context *c, const char *name)
103 struct ast_include *i = NULL;
105 if (ast_rdlock_context(c)) /* error, skip */
107 while ( (i = ast_walk_context_includes(c, i)) )
108 if (!strcmp(name, ast_get_include_name(i)))
110 ast_unlock_context(c);
111 return i ? -1 /* success */ : 0;
114 /*! \brief return true if 'name' is in the ignorepats for context c */
115 static int lookup_c_ip(struct ast_context *c, const char *name)
117 struct ast_ignorepat *ip = NULL;
119 if (ast_rdlock_context(c)) /* error, skip */
121 while ( (ip = ast_walk_context_ignorepats(c, ip)) )
122 if (!strcmp(name, ast_get_ignorepat_name(ip)))
124 ast_unlock_context(c);
125 return ip ? -1 /* success */ : 0;
128 /*! \brief moves to the n-th word in the string, or empty string if none */
129 static const char *skip_words(const char *p, int n)
132 for (;n && *p; p++) {
133 if (isblank(*p) /* XXX order is important */ && !in_blank) {
134 n--; /* one word is gone */
136 } else if (/* !is_blank(*p), we know already, && */ in_blank) {
143 /*! \brief match the first 'len' chars of word. len==0 always succeeds */
144 static int partial_match(const char *s, const char *word, int len)
146 return (len == 0 || !strncmp(s, word, len));
149 /*! \brief split extension\@context in two parts, return -1 on error.
150 * The return string is malloc'ed and pointed by *ext
152 static int split_ec(const char *src, char **ext, char ** const ctx, char ** const cid)
154 char *i, *c, *e = ast_strdup(src); /* now src is not used anymore */
157 return -1; /* malloc error */
158 /* now, parse values from 'exten@context' */
161 if (c == NULL) /* no context part */
162 *ctx = ""; /* it is not overwritten, anyways */
163 else { /* found context, check for duplicity ... */
166 if (strchr(c, '@')) { /* two @, not allowed */
171 if (cid && (i = strchr(e, '/'))) {
175 /* Signal none detected */
181 /* _X_ is the string we need to complete */
182 static char *complete_dialplan_remove_include(struct ast_cli_args *a)
186 int len = strlen(a->word); /* how many bytes to match */
187 struct ast_context *c = NULL;
189 if (a->pos == 3) { /* "dialplan remove include _X_" */
190 if (ast_wrlock_contexts()) {
191 ast_log(LOG_ERROR, "Failed to lock context list\n");
194 /* walk contexts and their includes, return the n-th match */
195 while (!res && (c = ast_walk_contexts(c))) {
196 struct ast_include *i = NULL;
198 if (ast_rdlock_context(c)) /* error ? skip this one */
201 while ( !res && (i = ast_walk_context_includes(c, i)) ) {
202 const char *i_name = ast_get_include_name(i);
203 struct ast_context *nc = NULL;
204 int already_served = 0;
206 if (!partial_match(i_name, a->word, len))
207 continue; /* not matched */
209 /* check if this include is already served or not */
211 /* go through all contexts again till we reach actual
212 * context or already_served = 1
214 while ( (nc = ast_walk_contexts(nc)) && nc != c && !already_served)
215 already_served = lookup_ci(nc, i_name);
217 if (!already_served && ++which > a->n)
218 res = strdup(i_name);
220 ast_unlock_context(c);
223 ast_unlock_contexts();
225 } else if (a->pos == 4) { /* "dialplan remove include CTX _X_" */
227 * complete as 'from', but only if previous context is really
230 char *context, *dupline;
231 const char *s = skip_words(a->line, 3); /* skip 'dialplan' 'remove' 'include' */
235 context = dupline = strdup(s);
237 ast_log(LOG_ERROR, "Out of free memory\n");
240 strsep(&dupline, " ");
242 if (ast_rdlock_contexts()) {
243 ast_log(LOG_ERROR, "Failed to lock contexts list\n");
248 /* go through all contexts and check if is included ... */
249 while (!res && (c = ast_walk_contexts(c)))
250 if (lookup_ci(c, context)) /* context is really included, complete "from" command */
251 res = strdup("from");
252 ast_unlock_contexts();
254 ast_log(LOG_WARNING, "%s not included anywhere\n", context);
257 } else if (a->pos == 5) { /* "dialplan remove include CTX from _X_" */
259 * Context from which we removing include ...
261 char *context, *dupline, *from;
262 const char *s = skip_words(a->line, 3); /* skip 'dialplan' 'remove' 'include' */
263 context = dupline = strdup(s);
265 ast_log(LOG_ERROR, "Out of free memory\n");
269 strsep(&dupline, " "); /* skip context */
271 /* fourth word must be 'from' */
272 from = strsep(&dupline, " ");
273 if (!from || strcmp(from, "from")) {
278 if (ast_rdlock_contexts()) {
279 ast_log(LOG_ERROR, "Failed to lock context list\n");
284 /* walk through all contexts ... */
286 while ( !res && (c = ast_walk_contexts(c))) {
287 const char *c_name = ast_get_context_name(c);
288 if (!partial_match(c_name, a->word, len)) /* not a good target */
290 /* walk through all includes and check if it is our context */
291 if (lookup_ci(c, context) && ++which > a->n)
292 res = strdup(c_name);
294 ast_unlock_contexts();
303 * REMOVE EXTENSION command stuff
305 static char *handle_cli_dialplan_remove_extension(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
307 int removing_priority = 0;
308 char *exten, *context, *cid;
309 char *ret = CLI_FAILURE;
313 e->command = "dialplan remove extension";
315 "Usage: dialplan remove extension exten[/cid]@context [priority]\n"
316 " Remove an extension from a given context. If a priority\n"
317 " is given, only that specific priority from the given extension\n"
318 " will be removed.\n";
321 return complete_dialplan_remove_extension(a);
324 if (a->argc != 5 && a->argc != 4)
325 return CLI_SHOWUSAGE;
328 * Priority input checking ...
331 const char *c = a->argv[4];
333 /* check for digits in whole parameter for right priority ...
334 * why? because atoi (strtol) returns 0 if any characters in
335 * string and whole extension will be removed, it's not good
337 if (!strcmp("hint", c))
338 removing_priority = PRIORITY_HINT;
340 while (*c && isdigit(*c))
342 if (*c) { /* non-digit in string */
343 ast_cli(a->fd, "Invalid priority '%s'\n", a->argv[4]);
346 removing_priority = atoi(a->argv[4]);
349 if (removing_priority == 0) {
350 ast_cli(a->fd, "If you want to remove whole extension, please " \
351 "omit priority argument\n");
356 /* XXX original overwrote argv[3] */
358 * Format exten@context checking ...
360 if (split_ec(a->argv[3], &exten, &context, &cid))
361 return CLI_FAILURE; /* XXX malloc failure */
362 if ((!strlen(exten)) || (!(strlen(context)))) {
363 ast_cli(a->fd, "Missing extension or context name in third argument '%s'\n",
369 if (!ast_context_remove_extension_callerid(context, exten, removing_priority,
370 /* Do NOT substitute S_OR; it is NOT the same thing */
371 cid ? cid : (removing_priority ? "" : NULL), cid ? 1 : 0, registrar)) {
372 if (!removing_priority)
373 ast_cli(a->fd, "Whole extension %s@%s removed\n",
376 ast_cli(a->fd, "Extension %s@%s with priority %d removed\n",
377 exten, context, removing_priority);
382 ast_cli(a->fd, "Failed to remove extension %s/%s@%s\n", exten, cid, context);
384 ast_cli(a->fd, "Failed to remove extension %s@%s\n", exten, context);
392 static char *complete_dialplan_remove_extension(struct ast_cli_args *a)
397 if (a->pos == 3) { /* 'dialplan remove extension _X_' (exten@context ... */
398 struct ast_context *c = NULL;
399 char *context = NULL, *exten = NULL, *cid = NULL;
400 int le = 0; /* length of extension */
401 int lc = 0; /* length of context */
402 int lcid = 0; /* length of cid */
404 lc = split_ec(a->word, &exten, &context, &cid);
405 if (lc) { /* error */
409 lc = strlen(context);
410 lcid = cid ? strlen(cid) : -1;
412 if (ast_rdlock_contexts()) {
413 ast_log(LOG_ERROR, "Failed to lock context list\n");
417 /* find our context ... */
418 while ( (c = ast_walk_contexts(c)) ) { /* match our context if any */
419 struct ast_exten *e = NULL;
421 if (!partial_match(ast_get_context_name(c), context, lc))
422 continue; /* context not matched */
423 while ( (e = ast_walk_context_extensions(c, e)) ) { /* try to complete extensions ... */
424 if ( !strchr(a->word, '/') ||
425 (!strchr(a->word, '@') && partial_match(ast_get_extension_cidmatch(e), cid, lcid)) ||
426 (strchr(a->word, '@') && !strcmp(ast_get_extension_cidmatch(e), cid))) {
427 if ( ((strchr(a->word, '/') || strchr(a->word, '@')) && !strcmp(ast_get_extension_name(e), exten)) ||
428 (!strchr(a->word, '/') && !strchr(a->word, '@') && partial_match(ast_get_extension_name(e), exten, le))) { /* n-th match */
429 if (++which > a->n) {
430 /* If there is an extension then return exten@context. */
431 if (ast_get_extension_matchcid(e) && (!strchr(a->word, '@') || strchr(a->word, '/'))) {
432 if (asprintf(&ret, "%s/%s@%s", ast_get_extension_name(e), ast_get_extension_cidmatch(e), ast_get_context_name(c)) < 0) {
433 ast_log(LOG_WARNING, "asprintf() failed: %s\n", strerror(errno));
437 } else if (!ast_get_extension_matchcid(e) && !strchr(a->word, '/')) {
438 if (asprintf(&ret, "%s@%s", ast_get_extension_name(e), ast_get_context_name(c)) < 0) {
439 ast_log(LOG_WARNING, "asprintf() failed: %s\n", strerror(errno));
448 if (e) /* got a match */
452 ast_unlock_contexts();
455 } else if (a->pos == 4) { /* 'dialplan remove extension EXT _X_' (priority) */
456 char *exten = NULL, *context, *cid, *p;
457 struct ast_context *c;
458 int le, lc, lcid, len;
459 const char *s = skip_words(a->line, 3); /* skip 'dialplan' 'remove' 'extension' */
460 int i = split_ec(s, &exten, &context, &cid); /* parse ext@context */
464 if ( (p = strchr(exten, ' ')) ) /* remove space after extension */
466 if ( (p = strchr(context, ' ')) ) /* remove space after context */
469 lc = strlen(context);
471 len = strlen(a->word);
472 if (le == 0 || lc == 0)
475 if (ast_rdlock_contexts()) {
476 ast_log(LOG_ERROR, "Failed to lock context list\n");
482 while ( (c = ast_walk_contexts(c)) ) {
483 /* XXX locking on c ? */
485 if (strcmp(ast_get_context_name(c), context) != 0)
487 /* got it, we must match here */
489 while ( (e = ast_walk_context_extensions(c, e)) ) {
490 struct ast_exten *priority;
493 if (cid && strcmp(ast_get_extension_cidmatch(e), cid) != 0) {
496 if (strcmp(ast_get_extension_name(e), exten) != 0)
500 while ( !ret && (priority = ast_walk_extension_priorities(e, priority)) ) {
501 snprintf(buffer, sizeof(buffer), "%u", ast_get_extension_priority(priority));
502 if (partial_match(buffer, a->word, len) && ++which > a->n) /* n-th match */
503 ret = strdup(buffer);
509 ast_unlock_contexts();
517 * Include context ...
519 static char *handle_cli_dialplan_add_include(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
523 e->command = "dialplan add include";
525 "Usage: dialplan add include <context> into <context>\n"
526 " Include a context in another context.\n";
529 return complete_dialplan_add_include(a);
532 if (a->argc != 6) /* dialplan add include CTX in CTX */
533 return CLI_SHOWUSAGE;
535 /* fifth arg must be 'into' ... */
536 if (strcmp(a->argv[4], "into"))
537 return CLI_SHOWUSAGE;
539 if (ast_context_add_include(a->argv[5], a->argv[3], registrar)) {
542 ast_cli(a->fd, "Out of memory for context addition\n");
546 ast_cli(a->fd, "Failed to lock context(s) list, please try again later\n");
550 ast_cli(a->fd, "Context '%s' already included in '%s' context\n",
551 a->argv[3], a->argv[5]);
556 ast_cli(a->fd, "There is no existence of context '%s'\n",
557 errno == ENOENT ? a->argv[5] : a->argv[3]);
561 ast_cli(a->fd, "Failed to include '%s' in '%s' context\n",
562 a->argv[3], a->argv[5]);
568 /* show some info ... */
569 ast_cli(a->fd, "Context '%s' included in '%s' context\n",
570 a->argv[3], a->argv[5]);
575 static char *complete_dialplan_add_include(struct ast_cli_args *a)
577 struct ast_context *c;
580 int len = strlen(a->word);
582 if (a->pos == 3) { /* 'dialplan add include _X_' (context) ... */
583 if (ast_rdlock_contexts()) {
584 ast_log(LOG_ERROR, "Failed to lock context list\n");
587 for (c = NULL; !ret && (c = ast_walk_contexts(c)); )
588 if (partial_match(ast_get_context_name(c), a->word, len) && ++which > a->n)
589 ret = strdup(ast_get_context_name(c));
590 ast_unlock_contexts();
592 } else if (a->pos == 4) { /* dialplan add include CTX _X_ */
593 /* complete as 'into' if context exists or we are unable to check */
594 char *context, *dupline;
595 const char *s = skip_words(a->line, 3); /* should not fail */
597 if (a->n != 0) /* only once */
600 /* parse context from line ... */
601 context = dupline = strdup(s);
603 ast_log(LOG_ERROR, "Out of free memory\n");
604 return strdup("into");
606 strsep(&dupline, " ");
608 /* check for context existence ... */
609 if (ast_rdlock_contexts()) {
610 ast_log(LOG_ERROR, "Failed to lock context list\n");
611 /* our fault, we can't check, so complete 'into' ... */
612 ret = strdup("into");
614 struct ast_context *ctx;
615 for (ctx = NULL; !ret && (ctx = ast_walk_contexts(ctx)); )
616 if (!strcmp(context, ast_get_context_name(ctx)))
617 ret = strdup("into"); /* found */
618 ast_unlock_contexts();
622 } else if (a->pos == 5) { /* 'dialplan add include CTX into _X_' (dst context) */
623 char *context, *dupline, *into;
624 const char *s = skip_words(a->line, 3); /* should not fail */
625 context = dupline = strdup(s);
627 ast_log(LOG_ERROR, "Out of free memory\n");
630 strsep(&dupline, " "); /* skip context */
631 into = strsep(&dupline, " ");
632 /* error if missing context or fifth word is not 'into' */
633 if (!strlen(context) || strcmp(into, "into")) {
634 ast_log(LOG_ERROR, "bad context %s or missing into %s\n",
639 if (ast_rdlock_contexts()) {
640 ast_log(LOG_ERROR, "Failed to lock context list\n");
644 for (c = NULL; (c = ast_walk_contexts(c)); )
645 if (!strcmp(context, ast_get_context_name(c)))
647 if (c) { /* first context exists, go on... */
648 /* go through all contexts ... */
649 for (c = NULL; !ret && (c = ast_walk_contexts(c)); ) {
650 if (!strcmp(context, ast_get_context_name(c)))
651 continue; /* skip ourselves */
652 if (partial_match(ast_get_context_name(c), a->word, len) &&
653 !lookup_ci(c, context) /* not included yet */ &&
655 ret = strdup(ast_get_context_name(c));
658 ast_log(LOG_ERROR, "context %s not found\n", context);
660 ast_unlock_contexts();
670 * \brief 'save dialplan' CLI command implementation functions ...
672 static char *handle_cli_dialplan_save(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
674 char filename[256], overrideswitch[256] = "";
675 struct ast_context *c;
676 struct ast_config *cfg;
677 struct ast_variable *v;
678 int incomplete = 0; /* incomplete config write? */
680 struct ast_flags config_flags = { 0 };
681 const char *base, *slash, *file;
685 e->command = "dialplan save";
687 "Usage: dialplan save [/path/to/extension/file]\n"
688 " Save dialplan created by pbx_config module.\n"
690 "Example: dialplan save (/etc/asterisk/extensions.conf)\n"
691 " dialplan save /home/markster (/home/markster/extensions.conf)\n";
697 if (! (static_config && !write_protect_config)) {
699 "I can't save dialplan now, see '%s' example file.\n",
704 if (a->argc != 2 && a->argc != 3)
705 return CLI_SHOWUSAGE;
707 if (ast_mutex_lock(&save_dialplan_lock)) {
709 "Failed to lock dialplan saving (another proccess saving?)\n");
712 /* XXX the code here is quite loose, a pathname with .conf in it
713 * is assumed to be a complete pathname
715 if (a->argc == 3) { /* have config path. Look for *.conf */
717 if (!strstr(a->argv[2], ".conf")) { /*no, this is assumed to be a pathname */
718 /* if filename ends with '/', do not add one */
719 slash = (*(a->argv[2] + strlen(a->argv[2]) -1) == '/') ? "/" : "";
720 file = config; /* default: 'extensions.conf' */
721 } else { /* yes, complete file name */
726 /* no config file, default one */
727 base = ast_config_AST_CONFIG_DIR;
731 snprintf(filename, sizeof(filename), "%s%s%s", base, slash, config);
733 cfg = ast_config_load("extensions.conf", config_flags);
735 /* try to lock contexts list */
736 if (ast_rdlock_contexts()) {
737 ast_cli(a->fd, "Failed to lock contexts list\n");
738 ast_mutex_unlock(&save_dialplan_lock);
739 ast_config_destroy(cfg);
743 /* create new file ... */
744 if (!(output = fopen(filename, "wt"))) {
745 ast_cli(a->fd, "Failed to create file '%s'\n",
747 ast_unlock_contexts();
748 ast_mutex_unlock(&save_dialplan_lock);
749 ast_config_destroy(cfg);
753 /* fireout general info */
754 if (overrideswitch_config) {
755 snprintf(overrideswitch, sizeof(overrideswitch), "overrideswitch=%s\n", overrideswitch_config);
757 fprintf(output, "[general]\nstatic=%s\nwriteprotect=%s\nautofallthrough=%s\nclearglobalvars=%s\n%sextenpatternmatchnew=%s\n\n",
758 static_config ? "yes" : "no",
759 write_protect_config ? "yes" : "no",
760 autofallthrough_config ? "yes" : "no",
761 clearglobalvars_config ? "yes" : "no",
762 overrideswitch_config ? overrideswitch : "",
763 extenpatternmatchnew_config ? "yes" : "no");
765 if ((v = ast_variable_browse(cfg, "globals"))) {
766 fprintf(output, "[globals]\n");
768 fprintf(output, "%s => %s\n", v->name, v->value);
771 fprintf(output, "\n");
774 ast_config_destroy(cfg);
776 #define PUT_CTX_HDR do { \
777 if (!context_header_written) { \
778 fprintf(output, "[%s]\n", ast_get_context_name(c)); \
779 context_header_written = 1; \
783 /* walk all contexts */
784 for (c = NULL; (c = ast_walk_contexts(c)); ) {
785 int context_header_written = 0;
786 struct ast_exten *ext, *last_written_e = NULL;
787 struct ast_include *i;
788 struct ast_ignorepat *ip;
791 /* try to lock context and fireout all info */
792 if (ast_rdlock_context(c)) { /* lock failure */
796 /* registered by this module? */
797 /* XXX do we need this ? */
798 if (!strcmp(ast_get_context_registrar(c), registrar)) {
799 fprintf(output, "[%s]\n", ast_get_context_name(c));
800 context_header_written = 1;
803 /* walk extensions ... */
804 for (ext = NULL; (ext = ast_walk_context_extensions(c, ext)); ) {
805 struct ast_exten *p = NULL;
807 /* fireout priorities */
808 while ( (p = ast_walk_extension_priorities(ext, p)) ) {
809 if (strcmp(ast_get_extension_registrar(p), registrar) != 0) /* not this source */
812 /* make empty line between different extensions */
813 if (last_written_e != NULL &&
814 strcmp(ast_get_extension_name(last_written_e),
815 ast_get_extension_name(p)))
816 fprintf(output, "\n");
821 if (ast_get_extension_priority(p) == PRIORITY_HINT) { /* easy */
822 fprintf(output, "exten => %s,hint,%s\n",
823 ast_get_extension_name(p),
824 ast_get_extension_app(p));
826 const char *sep, *cid;
827 const char *el = ast_get_extension_label(p);
828 char label[128] = "";
830 if (ast_get_extension_matchcid(p)) {
832 cid = ast_get_extension_cidmatch(p);
836 if (el && (snprintf(label, sizeof(label), "(%s)", el) != (strlen(el) + 2)))
837 incomplete = 1; /* error encountered or label > 125 chars */
839 fprintf(output, "exten => %s%s%s,%d%s,%s(%s)\n",
840 ast_get_extension_name(p), (ast_strlen_zero(sep) ? "" : sep), (ast_strlen_zero(cid) ? "" : cid),
841 ast_get_extension_priority(p), label,
842 ast_get_extension_app(p), (ast_strlen_zero(ast_get_extension_app_data(p)) ? "" : (const char *)ast_get_extension_app_data(p)));
847 /* written any extensions? ok, write space between exten & inc */
849 fprintf(output, "\n");
851 /* walk through includes */
852 for (i = NULL; (i = ast_walk_context_includes(c, i)) ; ) {
853 if (strcmp(ast_get_include_registrar(i), registrar) != 0)
854 continue; /* not mine */
856 fprintf(output, "include => %s\n", ast_get_include_name(i));
858 if (ast_walk_context_includes(c, NULL))
859 fprintf(output, "\n");
861 /* walk through switches */
862 for (sw = NULL; (sw = ast_walk_context_switches(c, sw)) ; ) {
863 if (strcmp(ast_get_switch_registrar(sw), registrar) != 0)
864 continue; /* not mine */
866 fprintf(output, "switch => %s/%s\n",
867 ast_get_switch_name(sw), ast_get_switch_data(sw));
870 if (ast_walk_context_switches(c, NULL))
871 fprintf(output, "\n");
873 /* fireout ignorepats ... */
874 for (ip = NULL; (ip = ast_walk_context_ignorepats(c, ip)); ) {
875 if (strcmp(ast_get_ignorepat_registrar(ip), registrar) != 0)
876 continue; /* not mine */
878 fprintf(output, "ignorepat => %s\n",
879 ast_get_ignorepat_name(ip));
882 ast_unlock_context(c);
885 ast_unlock_contexts();
886 ast_mutex_unlock(&save_dialplan_lock);
890 ast_cli(a->fd, "Saved dialplan is incomplete\n");
894 ast_cli(a->fd, "Dialplan successfully saved into '%s'\n",
900 * \brief ADD EXTENSION command stuff
902 static char *handle_cli_dialplan_add_extension(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
907 char *cidmatch, *app, *app_data;
912 e->command = "dialplan add extension";
914 "Usage: dialplan add extension <exten>,<priority>,<app>,<app-data>\n"
915 " into <context> [replace]\n\n"
916 " This command will add new extension into <context>. If there is an\n"
917 " existence of extension with the same priority and last 'replace'\n"
918 " arguments is given here we simply replace this extension.\n"
920 "Example: dialplan add extension 6123,1,Dial,IAX/216.207.245.56/6123 into local\n"
921 " Now, you can dial 6123 and talk to Markster :)\n";
924 return complete_dialplan_add_extension(a);
927 /* check for arguments at first */
928 if (a->argc != 6 && a->argc != 7)
929 return CLI_SHOWUSAGE;
930 if (strcmp(a->argv[4], "into"))
931 return CLI_SHOWUSAGE;
933 if (strcmp(a->argv[6], "replace"))
934 return CLI_SHOWUSAGE;
936 whole_exten = ast_strdupa(a->argv[3]);
937 exten = strsep(&whole_exten,",");
938 if (strchr(exten, '/')) {
940 strsep(&cidmatch,"/");
944 prior = strsep(&whole_exten,",");
946 if (!strcmp(prior, "hint")) {
947 iprior = PRIORITY_HINT;
949 if (sscanf(prior, "%30d", &iprior) != 1) {
950 ast_cli(a->fd, "'%s' is not a valid priority\n", prior);
956 if (app && (start = strchr(app, '(')) && (end = strrchr(app, ')'))) {
957 *start = *end = '\0';
958 app_data = start + 1;
961 app_data = strchr(app, ',');
970 if (!exten || !prior || !app || (!app_data && iprior != PRIORITY_HINT))
971 return CLI_SHOWUSAGE;
975 if (ast_add_extension(a->argv[5], a->argc == 7 ? 1 : 0, exten, iprior, NULL, cidmatch, app,
976 (void *)strdup(app_data), ast_free_ptr, registrar)) {
979 ast_cli(a->fd, "Out of free memory\n");
983 ast_cli(a->fd, "Failed to lock context(s) list, please try again later\n");
987 ast_cli(a->fd, "No existence of '%s' context\n", a->argv[5]);
991 ast_cli(a->fd, "Extension %s@%s with priority %s already exists\n",
992 exten, a->argv[5], prior);
996 ast_cli(a->fd, "Failed to add '%s,%s,%s,%s' extension into '%s' context\n",
997 exten, prior, app, app_data, a->argv[5]);
1004 ast_cli(a->fd, "Extension %s@%s (%s) replace by '%s,%s,%s,%s'\n",
1005 exten, a->argv[5], prior, exten, prior, app, app_data);
1007 ast_cli(a->fd, "Extension '%s,%s,%s,%s' added into '%s' context\n",
1008 exten, prior, app, app_data, a->argv[5]);
1013 /*! dialplan add extension 6123,1,Dial,IAX/212.71.138.13/6123 into local */
1014 static char *complete_dialplan_add_extension(struct ast_cli_args *a)
1018 if (a->pos == 4) { /* complete 'into' word ... */
1019 return (a->n == 0) ? strdup("into") : NULL;
1020 } else if (a->pos == 5) { /* complete context */
1021 struct ast_context *c = NULL;
1022 int len = strlen(a->word);
1025 /* try to lock contexts list ... */
1026 if (ast_rdlock_contexts()) {
1027 ast_log(LOG_WARNING, "Failed to lock contexts list\n");
1031 /* walk through all contexts */
1032 while ( !res && (c = ast_walk_contexts(c)) )
1033 if (partial_match(ast_get_context_name(c), a->word, len) && ++which > a->n)
1034 res = strdup(ast_get_context_name(c));
1035 ast_unlock_contexts();
1037 } else if (a->pos == 6) {
1038 return a->n == 0 ? strdup("replace") : NULL;
1044 * IGNOREPAT CLI stuff
1046 static char *handle_cli_dialplan_add_ignorepat(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1050 e->command = "dialplan add ignorepat";
1052 "Usage: dialplan add ignorepat <pattern> into <context>\n"
1053 " This command adds a new ignore pattern into context <context>\n"
1055 "Example: dialplan add ignorepat _3XX into local\n";
1058 return complete_dialplan_add_ignorepat(a);
1062 return CLI_SHOWUSAGE;
1064 if (strcmp(a->argv[4], "into"))
1065 return CLI_SHOWUSAGE;
1067 if (ast_context_add_ignorepat(a->argv[5], a->argv[3], registrar)) {
1070 ast_cli(a->fd, "Out of free memory\n");
1074 ast_cli(a->fd, "There is no existence of '%s' context\n", a->argv[5]);
1078 ast_cli(a->fd, "Ignore pattern '%s' already included in '%s' context\n",
1079 a->argv[3], a->argv[5]);
1083 ast_cli(a->fd, "Failed to lock context(s) list, please, try again later\n");
1087 ast_cli(a->fd, "Failed to add ingore pattern '%s' into '%s' context\n",
1088 a->argv[3], a->argv[5]);
1094 ast_cli(a->fd, "Ignore pattern '%s' added into '%s' context\n",
1095 a->argv[3], a->argv[5]);
1100 static char *complete_dialplan_add_ignorepat(struct ast_cli_args *a)
1103 return a->n == 0 ? strdup("into") : NULL;
1104 else if (a->pos == 5) {
1105 struct ast_context *c;
1107 char *dupline, *ignorepat = NULL;
1110 int len = strlen(a->word);
1112 /* XXX skip first three words 'dialplan' 'add' 'ignorepat' */
1113 s = skip_words(a->line, 3);
1116 dupline = strdup(s);
1118 ast_log(LOG_ERROR, "Malloc failure\n");
1121 ignorepat = strsep(&dupline, " ");
1123 if (ast_rdlock_contexts()) {
1124 ast_log(LOG_ERROR, "Failed to lock contexts list\n");
1128 for (c = NULL; !ret && (c = ast_walk_contexts(c));) {
1131 if (!partial_match(ast_get_context_name(c), a->word, len))
1132 continue; /* not mine */
1133 if (ignorepat) /* there must be one, right ? */
1134 found = lookup_c_ip(c, ignorepat);
1135 if (!found && ++which > a->n)
1136 ret = strdup(ast_get_context_name(c));
1140 ast_unlock_contexts();
1147 static char *handle_cli_dialplan_remove_ignorepat(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1151 e->command = "dialplan remove ignorepat";
1153 "Usage: dialplan remove ignorepat <pattern> from <context>\n"
1154 " This command removes an ignore pattern from context <context>\n"
1156 "Example: dialplan remove ignorepat _3XX from local\n";
1159 return complete_dialplan_remove_ignorepat(a);
1163 return CLI_SHOWUSAGE;
1165 if (strcmp(a->argv[4], "from"))
1166 return CLI_SHOWUSAGE;
1168 if (ast_context_remove_ignorepat(a->argv[5], a->argv[3], registrar)) {
1171 ast_cli(a->fd, "Failed to lock context(s) list, please try again later\n");
1175 ast_cli(a->fd, "There is no existence of '%s' context\n", a->argv[5]);
1179 ast_cli(a->fd, "There is no existence of '%s' ignore pattern in '%s' context\n",
1180 a->argv[3], a->argv[5]);
1184 ast_cli(a->fd, "Failed to remove ignore pattern '%s' from '%s' context\n",
1185 a->argv[3], a->argv[5]);
1191 ast_cli(a->fd, "Ignore pattern '%s' removed from '%s' context\n",
1192 a->argv[3], a->argv[5]);
1196 static char *complete_dialplan_remove_ignorepat(struct ast_cli_args *a)
1198 struct ast_context *c;
1203 int len = strlen(a->word);
1204 if (ast_rdlock_contexts()) {
1205 ast_log(LOG_WARNING, "Failed to lock contexts list\n");
1209 for (c = NULL; !ret && (c = ast_walk_contexts(c));) {
1210 struct ast_ignorepat *ip;
1212 if (ast_rdlock_context(c)) /* error, skip it */
1215 for (ip = NULL; !ret && (ip = ast_walk_context_ignorepats(c, ip));) {
1216 if (partial_match(ast_get_ignorepat_name(ip), a->word, len) && ++which > a->n) {
1218 struct ast_context *cw = NULL;
1220 while ( (cw = ast_walk_contexts(cw)) && cw != c && !found) {
1221 /* XXX do i stop on c, or skip it ? */
1222 found = lookup_c_ip(cw, ast_get_ignorepat_name(ip));
1225 ret = strdup(ast_get_ignorepat_name(ip));
1228 ast_unlock_context(c);
1230 ast_unlock_contexts();
1232 } else if (a->pos == 4) {
1233 return a->n == 0 ? strdup("from") : NULL;
1234 } else if (a->pos == 5) { /* XXX check this */
1235 char *dupline, *duplinet, *ignorepat;
1236 int len = strlen(a->word);
1238 dupline = strdup(a->line);
1240 ast_log(LOG_WARNING, "Out of free memory\n");
1245 strsep(&duplinet, " ");
1246 strsep(&duplinet, " ");
1247 ignorepat = strsep(&duplinet, " ");
1254 if (ast_rdlock_contexts()) {
1255 ast_log(LOG_WARNING, "Failed to lock contexts list\n");
1260 for (c = NULL; !ret && (c = ast_walk_contexts(c)); ) {
1261 if (ast_rdlock_context(c)) /* fail, skip it */
1263 if (!partial_match(ast_get_context_name(c), a->word, len))
1265 if (lookup_c_ip(c, ignorepat) && ++which > a->n)
1266 ret = strdup(ast_get_context_name(c));
1267 ast_unlock_context(c);
1269 ast_unlock_contexts();
1277 static int pbx_load_module(void);
1279 static char *handle_cli_dialplan_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1283 e->command = "dialplan reload";
1285 "Usage: dialplan reload\n"
1286 " Reload extensions.conf without reloading any other\n"
1287 " modules. This command does not delete global variables\n"
1288 " unless clearglobalvars is set to yes in extensions.conf\n";
1295 return CLI_SHOWUSAGE;
1297 if (clearglobalvars_config)
1298 pbx_builtin_clear_globals();
1301 ast_cli(a->fd, "Dialplan reloaded.\n");
1306 * CLI entries for commands provided by this module
1308 static struct ast_cli_entry cli_pbx_config[] = {
1309 AST_CLI_DEFINE(handle_cli_dialplan_add_extension, "Add new extension into context"),
1310 AST_CLI_DEFINE(handle_cli_dialplan_remove_extension, "Remove a specified extension"),
1311 AST_CLI_DEFINE(handle_cli_dialplan_add_ignorepat, "Add new ignore pattern"),
1312 AST_CLI_DEFINE(handle_cli_dialplan_remove_ignorepat, "Remove ignore pattern from context"),
1313 AST_CLI_DEFINE(handle_cli_dialplan_add_include, "Include context in other context"),
1314 AST_CLI_DEFINE(handle_cli_dialplan_remove_include, "Remove a specified include from context"),
1315 AST_CLI_DEFINE(handle_cli_dialplan_reload, "Reload extensions and *only* extensions")
1318 static struct ast_cli_entry cli_dialplan_save =
1319 AST_CLI_DEFINE(handle_cli_dialplan_save, "Save dialplan");
1322 * Standard module functions ...
1324 static int unload_module(void)
1326 if (static_config && !write_protect_config)
1327 ast_cli_unregister(&cli_dialplan_save);
1328 if (overrideswitch_config) {
1329 ast_free(overrideswitch_config);
1331 ast_cli_unregister_multiple(cli_pbx_config, ARRAY_LEN(cli_pbx_config));
1332 ast_context_destroy(NULL, registrar);
1336 /*!\note Protect against misparsing based upon commas in the middle of fields
1337 * like character classes. We've taken steps to permit pretty much every other
1338 * printable character in a character class, so properly handling a comma at
1339 * this level is a natural extension. This is almost like the standard
1340 * application parser in app.c, except that it handles square brackets. */
1341 static char *pbx_strsep(char **destructible, const char *delim)
1344 char *res = *destructible;
1345 for (; destructible && *destructible && **destructible; (*destructible)++) {
1346 if (**destructible == '[' && !strchr(delim, '[')) {
1348 } else if (**destructible == ']' && !strchr(delim, ']')) {
1352 } else if (**destructible == '\\' && !strchr(delim, '\\')) {
1354 } else if (strchr(delim, **destructible) && !square) {
1355 **destructible = '\0';
1360 if (destructible && *destructible && **destructible == '\0') {
1361 *destructible = NULL;
1366 static int pbx_load_config(const char *config_file)
1368 struct ast_config *cfg;
1372 char realvalue[256];
1374 char realvalue[8192];
1377 struct ast_context *con;
1378 struct ast_variable *v;
1381 const char *newpm, *ovsw;
1382 struct ast_flags config_flags = { 0 };
1383 char lastextension[256];
1384 cfg = ast_config_load(config_file, config_flags);
1385 if (!cfg || cfg == CONFIG_STATUS_FILEINVALID)
1388 /* Use existing config to populate the PBX table */
1389 static_config = ast_true(ast_variable_retrieve(cfg, "general", "static"));
1390 write_protect_config = ast_true(ast_variable_retrieve(cfg, "general", "writeprotect"));
1391 if ((aft = ast_variable_retrieve(cfg, "general", "autofallthrough")))
1392 autofallthrough_config = ast_true(aft);
1393 if ((newpm = ast_variable_retrieve(cfg, "general", "extenpatternmatchnew")))
1394 extenpatternmatchnew_config = ast_true(newpm);
1395 clearglobalvars_config = ast_true(ast_variable_retrieve(cfg, "general", "clearglobalvars"));
1396 if ((ovsw = ast_variable_retrieve(cfg, "general", "overrideswitch"))) {
1397 if (overrideswitch_config) {
1398 ast_free(overrideswitch_config);
1400 if (!ast_strlen_zero(ovsw)) {
1401 overrideswitch_config = ast_strdup(ovsw);
1403 overrideswitch_config = NULL;
1407 ast_copy_string(userscontext, ast_variable_retrieve(cfg, "general", "userscontext") ?: "default", sizeof(userscontext));
1409 for (v = ast_variable_browse(cfg, "globals"); v; v = v->next) {
1410 pbx_substitute_variables_helper(NULL, v->value, realvalue, sizeof(realvalue) - 1);
1411 pbx_builtin_setvar_helper(NULL, v->name, realvalue);
1413 for (cxt = ast_category_browse(cfg, NULL);
1415 cxt = ast_category_browse(cfg, cxt)) {
1416 /* All categories but "general" or "globals" are considered contexts */
1417 if (!strcasecmp(cxt, "general") || !strcasecmp(cxt, "globals")) {
1420 if (!(con = ast_context_find_or_create(&local_contexts, local_table, cxt, registrar))) {
1424 /* Reset continuation items at the beginning of each context */
1425 lastextension[0] = '\0';
1428 for (v = ast_variable_browse(cfg, cxt); v; v = v->next) {
1430 char realext[256] = "";
1431 char *stringp, *ext;
1433 if (!strncasecmp(v->name, "same", 4)) {
1434 if (ast_strlen_zero(lastextension)) {
1435 ast_log(LOG_ERROR, "No previous pattern in the first entry of context '%s' to match '%s' at line %d!\n", cxt, v->name, v->lineno);
1438 if ((stringp = tc = ast_strdup(v->value))) {
1439 ast_copy_string(realext, lastextension, sizeof(realext));
1440 goto process_extension;
1442 } else if (!strcasecmp(v->name, "exten")) {
1444 char *plus, *firstp;
1445 char *pri, *appl, *data, *cidmatch;
1447 if (!(stringp = tc = ast_strdup(v->value))) {
1451 ext = S_OR(pbx_strsep(&stringp, ","), "");
1452 pbx_substitute_variables_helper(NULL, ext, realext, sizeof(realext) - 1);
1453 ast_copy_string(lastextension, realext, sizeof(lastextension));
1456 if ((cidmatch = strchr(realext, '/'))) {
1458 ast_shrink_phone_number(cidmatch);
1460 pri = S_OR(strsep(&stringp, ","), "");
1461 pri = ast_skip_blanks(pri);
1462 pri = ast_trim_blanks(pri);
1463 if ((label = strchr(pri, '('))) {
1465 if ((end = strchr(label, ')'))) {
1468 ast_log(LOG_WARNING, "Label missing trailing ')' at line %d\n", v->lineno);
1471 if ((plus = strchr(pri, '+'))) {
1474 if (!strcmp(pri,"hint")) {
1475 ipri = PRIORITY_HINT;
1476 } else if (!strcmp(pri, "next") || !strcmp(pri, "n")) {
1480 ast_log(LOG_WARNING, "Can't use 'next' priority on the first entry at line %d!\n", v->lineno);
1482 } else if (!strcmp(pri, "same") || !strcmp(pri, "s")) {
1486 ast_log(LOG_WARNING, "Can't use 'same' priority on the first entry at line %d!\n", v->lineno);
1488 } else if (sscanf(pri, "%30d", &ipri) != 1 &&
1489 (ipri = ast_findlabel_extension2(NULL, con, realext, pri, cidmatch)) < 1) {
1490 ast_log(LOG_WARNING, "Invalid priority/label '%s' at line %d\n", pri, v->lineno);
1493 appl = S_OR(stringp, "");
1494 /* Find the first occurrence of '(' */
1495 if (!(firstp = strchr(appl, '('))) {
1499 char *orig_appl = ast_strdup(appl);
1504 appl = strsep(&stringp, "(");
1506 /* check if there are variables or expressions without an application, like: exten => 100,hint,DAHDI/g0/${GLOBAL(var)} */
1507 if (strstr(appl, "${") || strstr(appl, "$[")){
1508 /* set appl to original one */
1509 strcpy(appl, orig_appl);
1512 /* no variable before application found -> go ahead */
1514 data = S_OR(stringp, "");
1515 if ((end = strrchr(data, ')'))) {
1518 ast_log(LOG_WARNING, "No closing parenthesis found? '%s(%s' at line %d\n", appl, data, v->lineno);
1521 ast_free(orig_appl);
1524 appl = ast_skip_blanks(appl);
1530 if (!ast_opt_dont_warn && !strcmp(realext, "_.")) {
1531 ast_log(LOG_WARNING, "The use of '_.' for an extension is strongly discouraged and can have unexpected behavior. Please use '_X.' instead at line %d\n", v->lineno);
1533 if (ast_add_extension2(con, 0, realext, ipri, label, cidmatch, appl, strdup(data), ast_free_ptr, registrar)) {
1534 ast_log(LOG_WARNING, "Unable to register extension at line %d\n", v->lineno);
1538 } else if (!strcasecmp(v->name, "include")) {
1539 pbx_substitute_variables_helper(NULL, v->value, realvalue, sizeof(realvalue) - 1);
1540 if (ast_context_add_include2(con, realvalue, registrar)) {
1543 ast_log(LOG_WARNING, "Out of memory for context addition\n");
1547 ast_log(LOG_WARNING, "Failed to lock context(s) list, please try again later\n");
1551 ast_log(LOG_WARNING, "Context '%s' already included in '%s' context on include at line %d\n",
1552 v->value, cxt, v->lineno);
1557 ast_log(LOG_WARNING, "There is no existence of context '%s' included at line %d\n",
1558 errno == ENOENT ? v->value : cxt, v->lineno);
1562 ast_log(LOG_WARNING, "Failed to include '%s' in '%s' context at line %d\n",
1563 v->value, cxt, v->lineno);
1567 } else if (!strcasecmp(v->name, "ignorepat")) {
1568 pbx_substitute_variables_helper(NULL, v->value, realvalue, sizeof(realvalue) - 1);
1569 if (ast_context_add_ignorepat2(con, realvalue, registrar)) {
1570 ast_log(LOG_WARNING, "Unable to include ignorepat '%s' in context '%s' at line %d\n", v->value, cxt, v->lineno);
1572 } else if (!strcasecmp(v->name, "switch") || !strcasecmp(v->name, "lswitch") || !strcasecmp(v->name, "eswitch")) {
1573 char *stringp = realvalue;
1576 if (!strcasecmp(v->name, "switch")) {
1577 pbx_substitute_variables_helper(NULL, v->value, realvalue, sizeof(realvalue) - 1);
1579 ast_copy_string(realvalue, v->value, sizeof(realvalue));
1581 appl = strsep(&stringp, "/");
1582 data = S_OR(stringp, "");
1583 if (ast_context_add_switch2(con, appl, data, !strcasecmp(v->name, "eswitch"), registrar)) {
1584 ast_log(LOG_WARNING, "Unable to include switch '%s' in context '%s' at line %d\n", v->value, cxt, v->lineno);
1587 ast_log(LOG_WARNING, "==!!== Unknown directive: %s at line %d -- IGNORING!!!\n", v->name, v->lineno);
1591 ast_config_destroy(cfg);
1595 static void append_interface(char *iface, int maxlen, char *add)
1597 int len = strlen(iface);
1598 if (strlen(add) + len < maxlen - 2) {
1599 if (strlen(iface)) {
1601 strcpy(iface + len + 1, add);
1607 static void pbx_load_users(void)
1609 struct ast_config *cfg;
1611 const char *dahdichan;
1612 const char *hasexten, *altexts;
1615 char dahdicopy[256];
1616 char *ext, altcopy[256];
1620 int start, finish, x;
1621 struct ast_context *con = NULL;
1622 struct ast_flags config_flags = { 0 };
1624 cfg = ast_config_load("users.conf", config_flags);
1628 for (cat = ast_category_browse(cfg, NULL); cat ; cat = ast_category_browse(cfg, cat)) {
1629 if (!strcasecmp(cat, "general"))
1632 len = sizeof(iface);
1633 if (ast_true(ast_config_option(cfg, cat, "hassip"))) {
1634 snprintf(tmp, sizeof(tmp), "SIP/%s", cat);
1635 append_interface(iface, sizeof(iface), tmp);
1637 if (ast_true(ast_config_option(cfg, cat, "hasiax"))) {
1638 snprintf(tmp, sizeof(tmp), "IAX2/%s", cat);
1639 append_interface(iface, sizeof(iface), tmp);
1641 if (ast_true(ast_config_option(cfg, cat, "hash323"))) {
1642 snprintf(tmp, sizeof(tmp), "H323/%s", cat);
1643 append_interface(iface, sizeof(iface), tmp);
1645 hasexten = ast_config_option(cfg, cat, "hasexten");
1646 if (hasexten && !ast_true(hasexten))
1648 hasvoicemail = ast_true(ast_config_option(cfg, cat, "hasvoicemail"));
1649 dahdichan = ast_variable_retrieve(cfg, cat, "dahdichan");
1651 dahdichan = ast_variable_retrieve(cfg, "general", "dahdichan");
1652 if (!ast_strlen_zero(dahdichan)) {
1653 ast_copy_string(dahdicopy, dahdichan, sizeof(dahdicopy));
1655 chan = strsep(&c, ",");
1657 if (sscanf(chan, "%30d-%30d", &start, &finish) == 2) {
1659 } else if (sscanf(chan, "%30d", &start)) {
1663 start = 0; finish = 0;
1665 if (finish < start) {
1670 for (x = start; x <= finish; x++) {
1671 snprintf(tmp, sizeof(tmp), "DAHDI/%d", x);
1672 append_interface(iface, sizeof(iface), tmp);
1674 chan = strsep(&c, ",");
1677 if (!ast_strlen_zero(iface)) {
1678 /* Only create a context here when it is really needed. Otherwise default empty context
1679 created by pbx_config may conflict with the one explicitly created by pbx_ael */
1681 con = ast_context_find_or_create(&local_contexts, local_table, userscontext, registrar);
1684 ast_log(LOG_ERROR, "Can't find/create user context '%s'\n", userscontext);
1689 ast_add_extension2(con, 0, cat, -1, NULL, NULL, iface, NULL, NULL, registrar);
1690 /* If voicemail, use "stdexten" else use plain old dial */
1692 snprintf(tmp, sizeof(tmp), "stdexten,%s,${HINT}", cat);
1693 ast_add_extension2(con, 0, cat, 1, NULL, NULL, "Macro", strdup(tmp), ast_free_ptr, registrar);
1695 ast_add_extension2(con, 0, cat, 1, NULL, NULL, "Dial", strdup("${HINT}"), ast_free_ptr, registrar);
1697 altexts = ast_variable_retrieve(cfg, cat, "alternateexts");
1698 if (!ast_strlen_zero(altexts)) {
1699 snprintf(tmp, sizeof(tmp), "%s,1", cat);
1700 ast_copy_string(altcopy, altexts, sizeof(altcopy));
1702 ext = strsep(&c, ",");
1704 ast_add_extension2(con, 0, ext, 1, NULL, NULL, "Goto", strdup(tmp), ast_free_ptr, registrar);
1705 ext = strsep(&c, ",");
1710 ast_config_destroy(cfg);
1713 static int pbx_load_module(void)
1715 struct ast_context *con;
1718 local_table = ast_hashtab_create(17, ast_hashtab_compare_contexts, ast_hashtab_resize_java, ast_hashtab_newsize_java, ast_hashtab_hash_contexts, 0);
1720 if (!pbx_load_config(config))
1721 return AST_MODULE_LOAD_DECLINE;
1725 ast_merge_contexts_and_delete(&local_contexts, local_table, registrar);
1726 local_table = NULL; /* the local table has been moved into the global one. */
1727 local_contexts = NULL;
1729 for (con = NULL; (con = ast_walk_contexts(con));)
1730 ast_context_verify_includes(con);
1732 pbx_set_overrideswitch(overrideswitch_config);
1733 pbx_set_autofallthrough(autofallthrough_config);
1734 pbx_set_extenpatternmatchnew(extenpatternmatchnew_config);
1736 return AST_MODULE_LOAD_SUCCESS;
1739 static int load_module(void)
1741 if (pbx_load_module())
1742 return AST_MODULE_LOAD_DECLINE;
1744 if (static_config && !write_protect_config)
1745 ast_cli_register(&cli_dialplan_save);
1746 ast_cli_register_multiple(cli_pbx_config, ARRAY_LEN(cli_pbx_config));
1748 return AST_MODULE_LOAD_SUCCESS;
1751 static int reload(void)
1753 if (clearglobalvars_config)
1754 pbx_builtin_clear_globals();
1755 return pbx_load_module();
1758 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Text Extension Configuration",
1759 .load = load_module,
1760 .unload = unload_module,