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;
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);
470 len = strlen(a->word);
471 if (le == 0 || lc == 0)
474 if (ast_rdlock_contexts()) {
475 ast_log(LOG_ERROR, "Failed to lock context list\n");
481 while ( (c = ast_walk_contexts(c)) ) {
482 /* XXX locking on c ? */
484 if (strcmp(ast_get_context_name(c), context) != 0)
486 /* got it, we must match here */
488 while ( (e = ast_walk_context_extensions(c, e)) ) {
489 struct ast_exten *priority;
492 if (cid && strcmp(ast_get_extension_cidmatch(e), cid) != 0) {
495 if (strcmp(ast_get_extension_name(e), exten) != 0)
499 while ( !ret && (priority = ast_walk_extension_priorities(e, priority)) ) {
500 snprintf(buffer, sizeof(buffer), "%u", ast_get_extension_priority(priority));
501 if (partial_match(buffer, a->word, len) && ++which > a->n) /* n-th match */
502 ret = strdup(buffer);
508 ast_unlock_contexts();
516 * Include context ...
518 static char *handle_cli_dialplan_add_include(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
522 e->command = "dialplan add include";
524 "Usage: dialplan add include <context> into <context>\n"
525 " Include a context in another context.\n";
528 return complete_dialplan_add_include(a);
531 if (a->argc != 6) /* dialplan add include CTX in CTX */
532 return CLI_SHOWUSAGE;
534 /* fifth arg must be 'into' ... */
535 if (strcmp(a->argv[4], "into"))
536 return CLI_SHOWUSAGE;
538 if (ast_context_add_include(a->argv[5], a->argv[3], registrar)) {
541 ast_cli(a->fd, "Out of memory for context addition\n");
545 ast_cli(a->fd, "Failed to lock context(s) list, please try again later\n");
549 ast_cli(a->fd, "Context '%s' already included in '%s' context\n",
550 a->argv[3], a->argv[5]);
555 ast_cli(a->fd, "There is no existence of context '%s'\n",
556 errno == ENOENT ? a->argv[5] : a->argv[3]);
560 ast_cli(a->fd, "Failed to include '%s' in '%s' context\n",
561 a->argv[3], a->argv[5]);
567 /* show some info ... */
568 ast_cli(a->fd, "Context '%s' included in '%s' context\n",
569 a->argv[3], a->argv[5]);
574 static char *complete_dialplan_add_include(struct ast_cli_args *a)
576 struct ast_context *c;
579 int len = strlen(a->word);
581 if (a->pos == 3) { /* 'dialplan add include _X_' (context) ... */
582 if (ast_rdlock_contexts()) {
583 ast_log(LOG_ERROR, "Failed to lock context list\n");
586 for (c = NULL; !ret && (c = ast_walk_contexts(c)); )
587 if (partial_match(ast_get_context_name(c), a->word, len) && ++which > a->n)
588 ret = strdup(ast_get_context_name(c));
589 ast_unlock_contexts();
591 } else if (a->pos == 4) { /* dialplan add include CTX _X_ */
592 /* complete as 'into' if context exists or we are unable to check */
593 char *context, *dupline;
594 const char *s = skip_words(a->line, 3); /* should not fail */
596 if (a->n != 0) /* only once */
599 /* parse context from line ... */
600 context = dupline = strdup(s);
602 ast_log(LOG_ERROR, "Out of free memory\n");
603 return strdup("into");
605 strsep(&dupline, " ");
607 /* check for context existence ... */
608 if (ast_rdlock_contexts()) {
609 ast_log(LOG_ERROR, "Failed to lock context list\n");
610 /* our fault, we can't check, so complete 'into' ... */
611 ret = strdup("into");
613 struct ast_context *ctx;
614 for (ctx = NULL; !ret && (ctx = ast_walk_contexts(ctx)); )
615 if (!strcmp(context, ast_get_context_name(ctx)))
616 ret = strdup("into"); /* found */
617 ast_unlock_contexts();
621 } else if (a->pos == 5) { /* 'dialplan add include CTX into _X_' (dst context) */
622 char *context, *dupline, *into;
623 const char *s = skip_words(a->line, 3); /* should not fail */
624 context = dupline = strdup(s);
626 ast_log(LOG_ERROR, "Out of free memory\n");
629 strsep(&dupline, " "); /* skip context */
630 into = strsep(&dupline, " ");
631 /* error if missing context or fifth word is not 'into' */
632 if (!strlen(context) || strcmp(into, "into")) {
633 ast_log(LOG_ERROR, "bad context %s or missing into %s\n",
638 if (ast_rdlock_contexts()) {
639 ast_log(LOG_ERROR, "Failed to lock context list\n");
643 for (c = NULL; (c = ast_walk_contexts(c)); )
644 if (!strcmp(context, ast_get_context_name(c)))
646 if (c) { /* first context exists, go on... */
647 /* go through all contexts ... */
648 for (c = NULL; !ret && (c = ast_walk_contexts(c)); ) {
649 if (!strcmp(context, ast_get_context_name(c)))
650 continue; /* skip ourselves */
651 if (partial_match(ast_get_context_name(c), a->word, len) &&
652 !lookup_ci(c, context) /* not included yet */ &&
654 ret = strdup(ast_get_context_name(c));
657 ast_log(LOG_ERROR, "context %s not found\n", context);
659 ast_unlock_contexts();
669 * \brief 'save dialplan' CLI command implementation functions ...
671 static char *handle_cli_dialplan_save(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
673 char filename[256], overrideswitch[256] = "";
674 struct ast_context *c;
675 struct ast_config *cfg;
676 struct ast_variable *v;
677 int incomplete = 0; /* incomplete config write? */
679 struct ast_flags config_flags = { 0 };
680 const char *base, *slash, *file;
684 e->command = "dialplan save";
686 "Usage: dialplan save [/path/to/extension/file]\n"
687 " Save dialplan created by pbx_config module.\n"
689 "Example: dialplan save (/etc/asterisk/extensions.conf)\n"
690 " dialplan save /home/markster (/home/markster/extensions.conf)\n";
696 if (! (static_config && !write_protect_config)) {
698 "I can't save dialplan now, see '%s' example file.\n",
703 if (a->argc != 2 && a->argc != 3)
704 return CLI_SHOWUSAGE;
706 if (ast_mutex_lock(&save_dialplan_lock)) {
708 "Failed to lock dialplan saving (another proccess saving?)\n");
711 /* XXX the code here is quite loose, a pathname with .conf in it
712 * is assumed to be a complete pathname
714 if (a->argc == 3) { /* have config path. Look for *.conf */
716 if (!strstr(a->argv[2], ".conf")) { /*no, this is assumed to be a pathname */
717 /* if filename ends with '/', do not add one */
718 slash = (*(a->argv[2] + strlen(a->argv[2]) -1) == '/') ? "/" : "";
719 file = config; /* default: 'extensions.conf' */
720 } else { /* yes, complete file name */
725 /* no config file, default one */
726 base = ast_config_AST_CONFIG_DIR;
730 snprintf(filename, sizeof(filename), "%s%s%s", base, slash, config);
732 cfg = ast_config_load("extensions.conf", config_flags);
734 /* try to lock contexts list */
735 if (ast_rdlock_contexts()) {
736 ast_cli(a->fd, "Failed to lock contexts list\n");
737 ast_mutex_unlock(&save_dialplan_lock);
738 ast_config_destroy(cfg);
742 /* create new file ... */
743 if (!(output = fopen(filename, "wt"))) {
744 ast_cli(a->fd, "Failed to create file '%s'\n",
746 ast_unlock_contexts();
747 ast_mutex_unlock(&save_dialplan_lock);
748 ast_config_destroy(cfg);
752 /* fireout general info */
753 if (overrideswitch_config) {
754 snprintf(overrideswitch, sizeof(overrideswitch), "overrideswitch=%s\n", overrideswitch_config);
756 fprintf(output, "[general]\nstatic=%s\nwriteprotect=%s\nautofallthrough=%s\nclearglobalvars=%s\n%sextenpatternmatchnew=%s\n\n",
757 static_config ? "yes" : "no",
758 write_protect_config ? "yes" : "no",
759 autofallthrough_config ? "yes" : "no",
760 clearglobalvars_config ? "yes" : "no",
761 overrideswitch_config ? overrideswitch : "",
762 extenpatternmatchnew_config ? "yes" : "no");
764 if ((v = ast_variable_browse(cfg, "globals"))) {
765 fprintf(output, "[globals]\n");
767 fprintf(output, "%s => %s\n", v->name, v->value);
770 fprintf(output, "\n");
773 ast_config_destroy(cfg);
775 #define PUT_CTX_HDR do { \
776 if (!context_header_written) { \
777 fprintf(output, "[%s]\n", ast_get_context_name(c)); \
778 context_header_written = 1; \
782 /* walk all contexts */
783 for (c = NULL; (c = ast_walk_contexts(c)); ) {
784 int context_header_written = 0;
785 struct ast_exten *ext, *last_written_e = NULL;
786 struct ast_include *i;
787 struct ast_ignorepat *ip;
790 /* try to lock context and fireout all info */
791 if (ast_rdlock_context(c)) { /* lock failure */
795 /* registered by this module? */
796 /* XXX do we need this ? */
797 if (!strcmp(ast_get_context_registrar(c), registrar)) {
798 fprintf(output, "[%s]\n", ast_get_context_name(c));
799 context_header_written = 1;
802 /* walk extensions ... */
803 for (ext = NULL; (ext = ast_walk_context_extensions(c, ext)); ) {
804 struct ast_exten *p = NULL;
806 /* fireout priorities */
807 while ( (p = ast_walk_extension_priorities(ext, p)) ) {
808 if (strcmp(ast_get_extension_registrar(p), registrar) != 0) /* not this source */
811 /* make empty line between different extensions */
812 if (last_written_e != NULL &&
813 strcmp(ast_get_extension_name(last_written_e),
814 ast_get_extension_name(p)))
815 fprintf(output, "\n");
820 if (ast_get_extension_priority(p) == PRIORITY_HINT) { /* easy */
821 fprintf(output, "exten => %s,hint,%s\n",
822 ast_get_extension_name(p),
823 ast_get_extension_app(p));
825 const char *sep, *cid;
826 const char *el = ast_get_extension_label(p);
827 char label[128] = "";
829 if (ast_get_extension_matchcid(p)) {
831 cid = ast_get_extension_cidmatch(p);
835 if (el && (snprintf(label, sizeof(label), "(%s)", el) != (strlen(el) + 2)))
836 incomplete = 1; /* error encountered or label > 125 chars */
838 fprintf(output, "exten => %s%s%s,%d%s,%s(%s)\n",
839 ast_get_extension_name(p), (ast_strlen_zero(sep) ? "" : sep), (ast_strlen_zero(cid) ? "" : cid),
840 ast_get_extension_priority(p), label,
841 ast_get_extension_app(p), (ast_strlen_zero(ast_get_extension_app_data(p)) ? "" : (const char *)ast_get_extension_app_data(p)));
846 /* written any extensions? ok, write space between exten & inc */
848 fprintf(output, "\n");
850 /* walk through includes */
851 for (i = NULL; (i = ast_walk_context_includes(c, i)) ; ) {
852 if (strcmp(ast_get_include_registrar(i), registrar) != 0)
853 continue; /* not mine */
855 fprintf(output, "include => %s\n", ast_get_include_name(i));
857 if (ast_walk_context_includes(c, NULL))
858 fprintf(output, "\n");
860 /* walk through switches */
861 for (sw = NULL; (sw = ast_walk_context_switches(c, sw)) ; ) {
862 if (strcmp(ast_get_switch_registrar(sw), registrar) != 0)
863 continue; /* not mine */
865 fprintf(output, "switch => %s/%s\n",
866 ast_get_switch_name(sw), ast_get_switch_data(sw));
869 if (ast_walk_context_switches(c, NULL))
870 fprintf(output, "\n");
872 /* fireout ignorepats ... */
873 for (ip = NULL; (ip = ast_walk_context_ignorepats(c, ip)); ) {
874 if (strcmp(ast_get_ignorepat_registrar(ip), registrar) != 0)
875 continue; /* not mine */
877 fprintf(output, "ignorepat => %s\n",
878 ast_get_ignorepat_name(ip));
881 ast_unlock_context(c);
884 ast_unlock_contexts();
885 ast_mutex_unlock(&save_dialplan_lock);
889 ast_cli(a->fd, "Saved dialplan is incomplete\n");
893 ast_cli(a->fd, "Dialplan successfully saved into '%s'\n",
899 * \brief ADD EXTENSION command stuff
901 static char *handle_cli_dialplan_add_extension(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
906 char *cidmatch, *app, *app_data;
911 e->command = "dialplan add extension";
913 "Usage: dialplan add extension <exten>,<priority>,<app>,<app-data>\n"
914 " into <context> [replace]\n\n"
915 " This command will add new extension into <context>. If there is an\n"
916 " existence of extension with the same priority and last 'replace'\n"
917 " arguments is given here we simply replace this extension.\n"
919 "Example: dialplan add extension 6123,1,Dial,IAX/216.207.245.56/6123 into local\n"
920 " Now, you can dial 6123 and talk to Markster :)\n";
923 return complete_dialplan_add_extension(a);
926 /* check for arguments at first */
927 if (a->argc != 6 && a->argc != 7)
928 return CLI_SHOWUSAGE;
929 if (strcmp(a->argv[4], "into"))
930 return CLI_SHOWUSAGE;
932 if (strcmp(a->argv[6], "replace"))
933 return CLI_SHOWUSAGE;
935 whole_exten = ast_strdupa(a->argv[3]);
936 exten = strsep(&whole_exten,",");
937 if (strchr(exten, '/')) {
939 strsep(&cidmatch,"/");
943 prior = strsep(&whole_exten,",");
945 if (!strcmp(prior, "hint")) {
946 iprior = PRIORITY_HINT;
948 if (sscanf(prior, "%30d", &iprior) != 1) {
949 ast_cli(a->fd, "'%s' is not a valid priority\n", prior);
955 if (app && (start = strchr(app, '(')) && (end = strrchr(app, ')'))) {
956 *start = *end = '\0';
957 app_data = start + 1;
960 app_data = strchr(app, ',');
969 if (!exten || !prior || !app || (!app_data && iprior != PRIORITY_HINT))
970 return CLI_SHOWUSAGE;
974 if (ast_add_extension(a->argv[5], a->argc == 7 ? 1 : 0, exten, iprior, NULL, cidmatch, app,
975 (void *)strdup(app_data), ast_free_ptr, registrar)) {
978 ast_cli(a->fd, "Out of free memory\n");
982 ast_cli(a->fd, "Failed to lock context(s) list, please try again later\n");
986 ast_cli(a->fd, "No existence of '%s' context\n", a->argv[5]);
990 ast_cli(a->fd, "Extension %s@%s with priority %s already exists\n",
991 exten, a->argv[5], prior);
995 ast_cli(a->fd, "Failed to add '%s,%s,%s,%s' extension into '%s' context\n",
996 exten, prior, app, app_data, a->argv[5]);
1003 ast_cli(a->fd, "Extension %s@%s (%s) replace by '%s,%s,%s,%s'\n",
1004 exten, a->argv[5], prior, exten, prior, app, app_data);
1006 ast_cli(a->fd, "Extension '%s,%s,%s,%s' added into '%s' context\n",
1007 exten, prior, app, app_data, a->argv[5]);
1012 /*! dialplan add extension 6123,1,Dial,IAX/212.71.138.13/6123 into local */
1013 static char *complete_dialplan_add_extension(struct ast_cli_args *a)
1017 if (a->pos == 4) { /* complete 'into' word ... */
1018 return (a->n == 0) ? strdup("into") : NULL;
1019 } else if (a->pos == 5) { /* complete context */
1020 struct ast_context *c = NULL;
1021 int len = strlen(a->word);
1024 /* try to lock contexts list ... */
1025 if (ast_rdlock_contexts()) {
1026 ast_log(LOG_WARNING, "Failed to lock contexts list\n");
1030 /* walk through all contexts */
1031 while ( !res && (c = ast_walk_contexts(c)) )
1032 if (partial_match(ast_get_context_name(c), a->word, len) && ++which > a->n)
1033 res = strdup(ast_get_context_name(c));
1034 ast_unlock_contexts();
1036 } else if (a->pos == 6) {
1037 return a->n == 0 ? strdup("replace") : NULL;
1043 * IGNOREPAT CLI stuff
1045 static char *handle_cli_dialplan_add_ignorepat(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1049 e->command = "dialplan add ignorepat";
1051 "Usage: dialplan add ignorepat <pattern> into <context>\n"
1052 " This command adds a new ignore pattern into context <context>\n"
1054 "Example: dialplan add ignorepat _3XX into local\n";
1057 return complete_dialplan_add_ignorepat(a);
1061 return CLI_SHOWUSAGE;
1063 if (strcmp(a->argv[4], "into"))
1064 return CLI_SHOWUSAGE;
1066 if (ast_context_add_ignorepat(a->argv[5], a->argv[3], registrar)) {
1069 ast_cli(a->fd, "Out of free memory\n");
1073 ast_cli(a->fd, "There is no existence of '%s' context\n", a->argv[5]);
1077 ast_cli(a->fd, "Ignore pattern '%s' already included in '%s' context\n",
1078 a->argv[3], a->argv[5]);
1082 ast_cli(a->fd, "Failed to lock context(s) list, please, try again later\n");
1086 ast_cli(a->fd, "Failed to add ingore pattern '%s' into '%s' context\n",
1087 a->argv[3], a->argv[5]);
1093 ast_cli(a->fd, "Ignore pattern '%s' added into '%s' context\n",
1094 a->argv[3], a->argv[5]);
1099 static char *complete_dialplan_add_ignorepat(struct ast_cli_args *a)
1102 return a->n == 0 ? strdup("into") : NULL;
1103 else if (a->pos == 5) {
1104 struct ast_context *c;
1106 char *dupline, *ignorepat = NULL;
1109 int len = strlen(a->word);
1111 /* XXX skip first three words 'dialplan' 'add' 'ignorepat' */
1112 s = skip_words(a->line, 3);
1115 dupline = strdup(s);
1117 ast_log(LOG_ERROR, "Malloc failure\n");
1120 ignorepat = strsep(&dupline, " ");
1122 if (ast_rdlock_contexts()) {
1123 ast_log(LOG_ERROR, "Failed to lock contexts list\n");
1127 for (c = NULL; !ret && (c = ast_walk_contexts(c));) {
1130 if (!partial_match(ast_get_context_name(c), a->word, len))
1131 continue; /* not mine */
1132 if (ignorepat) /* there must be one, right ? */
1133 found = lookup_c_ip(c, ignorepat);
1134 if (!found && ++which > a->n)
1135 ret = strdup(ast_get_context_name(c));
1139 ast_unlock_contexts();
1146 static char *handle_cli_dialplan_remove_ignorepat(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1150 e->command = "dialplan remove ignorepat";
1152 "Usage: dialplan remove ignorepat <pattern> from <context>\n"
1153 " This command removes an ignore pattern from context <context>\n"
1155 "Example: dialplan remove ignorepat _3XX from local\n";
1158 return complete_dialplan_remove_ignorepat(a);
1162 return CLI_SHOWUSAGE;
1164 if (strcmp(a->argv[4], "from"))
1165 return CLI_SHOWUSAGE;
1167 if (ast_context_remove_ignorepat(a->argv[5], a->argv[3], registrar)) {
1170 ast_cli(a->fd, "Failed to lock context(s) list, please try again later\n");
1174 ast_cli(a->fd, "There is no existence of '%s' context\n", a->argv[5]);
1178 ast_cli(a->fd, "There is no existence of '%s' ignore pattern in '%s' context\n",
1179 a->argv[3], a->argv[5]);
1183 ast_cli(a->fd, "Failed to remove ignore pattern '%s' from '%s' context\n",
1184 a->argv[3], a->argv[5]);
1190 ast_cli(a->fd, "Ignore pattern '%s' removed from '%s' context\n",
1191 a->argv[3], a->argv[5]);
1195 static char *complete_dialplan_remove_ignorepat(struct ast_cli_args *a)
1197 struct ast_context *c;
1202 int len = strlen(a->word);
1203 if (ast_rdlock_contexts()) {
1204 ast_log(LOG_WARNING, "Failed to lock contexts list\n");
1208 for (c = NULL; !ret && (c = ast_walk_contexts(c));) {
1209 struct ast_ignorepat *ip;
1211 if (ast_rdlock_context(c)) /* error, skip it */
1214 for (ip = NULL; !ret && (ip = ast_walk_context_ignorepats(c, ip));) {
1215 if (partial_match(ast_get_ignorepat_name(ip), a->word, len) && ++which > a->n) {
1217 struct ast_context *cw = NULL;
1219 while ( (cw = ast_walk_contexts(cw)) && cw != c && !found) {
1220 /* XXX do i stop on c, or skip it ? */
1221 found = lookup_c_ip(cw, ast_get_ignorepat_name(ip));
1224 ret = strdup(ast_get_ignorepat_name(ip));
1227 ast_unlock_context(c);
1229 ast_unlock_contexts();
1231 } else if (a->pos == 4) {
1232 return a->n == 0 ? strdup("from") : NULL;
1233 } else if (a->pos == 5) { /* XXX check this */
1234 char *dupline, *duplinet, *ignorepat;
1235 int len = strlen(a->word);
1237 dupline = strdup(a->line);
1239 ast_log(LOG_WARNING, "Out of free memory\n");
1244 strsep(&duplinet, " ");
1245 strsep(&duplinet, " ");
1246 ignorepat = strsep(&duplinet, " ");
1253 if (ast_rdlock_contexts()) {
1254 ast_log(LOG_WARNING, "Failed to lock contexts list\n");
1259 for (c = NULL; !ret && (c = ast_walk_contexts(c)); ) {
1260 if (ast_rdlock_context(c)) /* fail, skip it */
1262 if (!partial_match(ast_get_context_name(c), a->word, len))
1264 if (lookup_c_ip(c, ignorepat) && ++which > a->n)
1265 ret = strdup(ast_get_context_name(c));
1266 ast_unlock_context(c);
1268 ast_unlock_contexts();
1276 static int pbx_load_module(void);
1278 static char *handle_cli_dialplan_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1282 e->command = "dialplan reload";
1284 "Usage: dialplan reload\n"
1285 " Reload extensions.conf without reloading any other\n"
1286 " modules. This command does not delete global variables\n"
1287 " unless clearglobalvars is set to yes in extensions.conf\n";
1294 return CLI_SHOWUSAGE;
1296 if (clearglobalvars_config)
1297 pbx_builtin_clear_globals();
1300 ast_cli(a->fd, "Dialplan reloaded.\n");
1305 * CLI entries for commands provided by this module
1307 static struct ast_cli_entry cli_pbx_config[] = {
1308 AST_CLI_DEFINE(handle_cli_dialplan_add_extension, "Add new extension into context"),
1309 AST_CLI_DEFINE(handle_cli_dialplan_remove_extension, "Remove a specified extension"),
1310 AST_CLI_DEFINE(handle_cli_dialplan_add_ignorepat, "Add new ignore pattern"),
1311 AST_CLI_DEFINE(handle_cli_dialplan_remove_ignorepat, "Remove ignore pattern from context"),
1312 AST_CLI_DEFINE(handle_cli_dialplan_add_include, "Include context in other context"),
1313 AST_CLI_DEFINE(handle_cli_dialplan_remove_include, "Remove a specified include from context"),
1314 AST_CLI_DEFINE(handle_cli_dialplan_reload, "Reload extensions and *only* extensions")
1317 static struct ast_cli_entry cli_dialplan_save =
1318 AST_CLI_DEFINE(handle_cli_dialplan_save, "Save dialplan");
1321 * Standard module functions ...
1323 static int unload_module(void)
1325 if (static_config && !write_protect_config)
1326 ast_cli_unregister(&cli_dialplan_save);
1327 if (overrideswitch_config) {
1328 ast_free(overrideswitch_config);
1330 ast_cli_unregister_multiple(cli_pbx_config, ARRAY_LEN(cli_pbx_config));
1331 ast_context_destroy(NULL, registrar);
1335 /*!\note Protect against misparsing based upon commas in the middle of fields
1336 * like character classes. We've taken steps to permit pretty much every other
1337 * printable character in a character class, so properly handling a comma at
1338 * this level is a natural extension. This is almost like the standard
1339 * application parser in app.c, except that it handles square brackets. */
1340 static char *pbx_strsep(char **destructible, const char *delim)
1343 char *res = *destructible;
1344 for (; destructible && *destructible && **destructible; (*destructible)++) {
1345 if (**destructible == '[' && !strchr(delim, '[')) {
1347 } else if (**destructible == ']' && !strchr(delim, ']')) {
1351 } else if (**destructible == '\\' && !strchr(delim, '\\')) {
1353 } else if (strchr(delim, **destructible) && !square) {
1354 **destructible = '\0';
1359 if (destructible && *destructible && **destructible == '\0') {
1360 *destructible = NULL;
1365 static int pbx_load_config(const char *config_file)
1367 struct ast_config *cfg;
1371 char realvalue[256];
1373 char realvalue[8192];
1376 struct ast_context *con;
1377 struct ast_variable *v;
1380 const char *newpm, *ovsw;
1381 struct ast_flags config_flags = { 0 };
1382 char lastextension[256];
1383 cfg = ast_config_load(config_file, config_flags);
1384 if (!cfg || cfg == CONFIG_STATUS_FILEINVALID)
1387 /* Use existing config to populate the PBX table */
1388 static_config = ast_true(ast_variable_retrieve(cfg, "general", "static"));
1389 write_protect_config = ast_true(ast_variable_retrieve(cfg, "general", "writeprotect"));
1390 if ((aft = ast_variable_retrieve(cfg, "general", "autofallthrough")))
1391 autofallthrough_config = ast_true(aft);
1392 if ((newpm = ast_variable_retrieve(cfg, "general", "extenpatternmatchnew")))
1393 extenpatternmatchnew_config = ast_true(newpm);
1394 clearglobalvars_config = ast_true(ast_variable_retrieve(cfg, "general", "clearglobalvars"));
1395 if ((ovsw = ast_variable_retrieve(cfg, "general", "overrideswitch"))) {
1396 if (overrideswitch_config) {
1397 ast_free(overrideswitch_config);
1399 if (!ast_strlen_zero(ovsw)) {
1400 overrideswitch_config = ast_strdup(ovsw);
1402 overrideswitch_config = NULL;
1406 ast_copy_string(userscontext, ast_variable_retrieve(cfg, "general", "userscontext") ?: "default", sizeof(userscontext));
1408 for (v = ast_variable_browse(cfg, "globals"); v; v = v->next) {
1409 pbx_substitute_variables_helper(NULL, v->value, realvalue, sizeof(realvalue) - 1);
1410 pbx_builtin_setvar_helper(NULL, v->name, realvalue);
1412 for (cxt = ast_category_browse(cfg, NULL);
1414 cxt = ast_category_browse(cfg, cxt)) {
1415 /* All categories but "general" or "globals" are considered contexts */
1416 if (!strcasecmp(cxt, "general") || !strcasecmp(cxt, "globals")) {
1419 if (!(con = ast_context_find_or_create(&local_contexts, local_table, cxt, registrar))) {
1423 /* Reset continuation items at the beginning of each context */
1424 lastextension[0] = '\0';
1427 for (v = ast_variable_browse(cfg, cxt); v; v = v->next) {
1429 char realext[256] = "";
1430 char *stringp, *ext;
1432 if (!strncasecmp(v->name, "same", 4)) {
1433 if (ast_strlen_zero(lastextension)) {
1434 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);
1437 if ((stringp = tc = ast_strdup(v->value))) {
1438 ast_copy_string(realext, lastextension, sizeof(realext));
1439 goto process_extension;
1441 } else if (!strcasecmp(v->name, "exten")) {
1443 char *plus, *firstp;
1444 char *pri, *appl, *data, *cidmatch;
1446 if (!(stringp = tc = ast_strdup(v->value))) {
1450 ext = S_OR(pbx_strsep(&stringp, ","), "");
1451 pbx_substitute_variables_helper(NULL, ext, realext, sizeof(realext) - 1);
1452 ast_copy_string(lastextension, realext, sizeof(lastextension));
1455 if ((cidmatch = strchr(realext, '/'))) {
1457 ast_shrink_phone_number(cidmatch);
1459 pri = S_OR(strsep(&stringp, ","), "");
1460 pri = ast_skip_blanks(pri);
1461 pri = ast_trim_blanks(pri);
1462 if ((label = strchr(pri, '('))) {
1464 if ((end = strchr(label, ')'))) {
1467 ast_log(LOG_WARNING, "Label missing trailing ')' at line %d\n", v->lineno);
1470 if ((plus = strchr(pri, '+'))) {
1473 if (!strcmp(pri,"hint")) {
1474 ipri = PRIORITY_HINT;
1475 } else if (!strcmp(pri, "next") || !strcmp(pri, "n")) {
1479 ast_log(LOG_WARNING, "Can't use 'next' priority on the first entry at line %d!\n", v->lineno);
1481 } else if (!strcmp(pri, "same") || !strcmp(pri, "s")) {
1485 ast_log(LOG_WARNING, "Can't use 'same' priority on the first entry at line %d!\n", v->lineno);
1487 } else if (sscanf(pri, "%30d", &ipri) != 1 &&
1488 (ipri = ast_findlabel_extension2(NULL, con, realext, pri, cidmatch)) < 1) {
1489 ast_log(LOG_WARNING, "Invalid priority/label '%s' at line %d\n", pri, v->lineno);
1492 appl = S_OR(stringp, "");
1493 /* Find the first occurrence of '(' */
1494 if (!(firstp = strchr(appl, '('))) {
1498 char *orig_appl = ast_strdup(appl);
1503 appl = strsep(&stringp, "(");
1505 /* check if there are variables or expressions without an application, like: exten => 100,hint,DAHDI/g0/${GLOBAL(var)} */
1506 if (strstr(appl, "${") || strstr(appl, "$[")){
1507 /* set appl to original one */
1508 strcpy(appl, orig_appl);
1511 /* no variable before application found -> go ahead */
1513 data = S_OR(stringp, "");
1514 if ((end = strrchr(data, ')'))) {
1517 ast_log(LOG_WARNING, "No closing parenthesis found? '%s(%s' at line %d\n", appl, data, v->lineno);
1520 ast_free(orig_appl);
1523 appl = ast_skip_blanks(appl);
1529 if (!ast_opt_dont_warn && !strcmp(realext, "_.")) {
1530 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);
1532 if (ast_add_extension2(con, 0, realext, ipri, label, cidmatch, appl, strdup(data), ast_free_ptr, registrar)) {
1533 ast_log(LOG_WARNING, "Unable to register extension at line %d\n", v->lineno);
1537 } else if (!strcasecmp(v->name, "include")) {
1538 pbx_substitute_variables_helper(NULL, v->value, realvalue, sizeof(realvalue) - 1);
1539 if (ast_context_add_include2(con, realvalue, registrar)) {
1542 ast_log(LOG_WARNING, "Out of memory for context addition\n");
1546 ast_log(LOG_WARNING, "Failed to lock context(s) list, please try again later\n");
1550 ast_log(LOG_WARNING, "Context '%s' already included in '%s' context on include at line %d\n",
1551 v->value, cxt, v->lineno);
1556 ast_log(LOG_WARNING, "There is no existence of context '%s' included at line %d\n",
1557 errno == ENOENT ? v->value : cxt, v->lineno);
1561 ast_log(LOG_WARNING, "Failed to include '%s' in '%s' context at line %d\n",
1562 v->value, cxt, v->lineno);
1566 } else if (!strcasecmp(v->name, "ignorepat")) {
1567 pbx_substitute_variables_helper(NULL, v->value, realvalue, sizeof(realvalue) - 1);
1568 if (ast_context_add_ignorepat2(con, realvalue, registrar)) {
1569 ast_log(LOG_WARNING, "Unable to include ignorepat '%s' in context '%s' at line %d\n", v->value, cxt, v->lineno);
1571 } else if (!strcasecmp(v->name, "switch") || !strcasecmp(v->name, "lswitch") || !strcasecmp(v->name, "eswitch")) {
1572 char *stringp = realvalue;
1575 if (!strcasecmp(v->name, "switch")) {
1576 pbx_substitute_variables_helper(NULL, v->value, realvalue, sizeof(realvalue) - 1);
1578 ast_copy_string(realvalue, v->value, sizeof(realvalue));
1580 appl = strsep(&stringp, "/");
1581 data = S_OR(stringp, "");
1582 if (ast_context_add_switch2(con, appl, data, !strcasecmp(v->name, "eswitch"), registrar)) {
1583 ast_log(LOG_WARNING, "Unable to include switch '%s' in context '%s' at line %d\n", v->value, cxt, v->lineno);
1586 ast_log(LOG_WARNING, "==!!== Unknown directive: %s at line %d -- IGNORING!!!\n", v->name, v->lineno);
1590 ast_config_destroy(cfg);
1594 static void append_interface(char *iface, int maxlen, char *add)
1596 int len = strlen(iface);
1597 if (strlen(add) + len < maxlen - 2) {
1598 if (strlen(iface)) {
1600 strcpy(iface + len + 1, add);
1606 static void pbx_load_users(void)
1608 struct ast_config *cfg;
1610 const char *dahdichan;
1611 const char *hasexten, *altexts;
1614 char dahdicopy[256];
1615 char *ext, altcopy[256];
1619 int start, finish, x;
1620 struct ast_context *con = NULL;
1621 struct ast_flags config_flags = { 0 };
1623 cfg = ast_config_load("users.conf", config_flags);
1627 for (cat = ast_category_browse(cfg, NULL); cat ; cat = ast_category_browse(cfg, cat)) {
1628 if (!strcasecmp(cat, "general"))
1631 len = sizeof(iface);
1632 if (ast_true(ast_config_option(cfg, cat, "hassip"))) {
1633 snprintf(tmp, sizeof(tmp), "SIP/%s", cat);
1634 append_interface(iface, sizeof(iface), tmp);
1636 if (ast_true(ast_config_option(cfg, cat, "hasiax"))) {
1637 snprintf(tmp, sizeof(tmp), "IAX2/%s", cat);
1638 append_interface(iface, sizeof(iface), tmp);
1640 if (ast_true(ast_config_option(cfg, cat, "hash323"))) {
1641 snprintf(tmp, sizeof(tmp), "H323/%s", cat);
1642 append_interface(iface, sizeof(iface), tmp);
1644 hasexten = ast_config_option(cfg, cat, "hasexten");
1645 if (hasexten && !ast_true(hasexten))
1647 hasvoicemail = ast_true(ast_config_option(cfg, cat, "hasvoicemail"));
1648 dahdichan = ast_variable_retrieve(cfg, cat, "dahdichan");
1650 dahdichan = ast_variable_retrieve(cfg, "general", "dahdichan");
1651 if (!ast_strlen_zero(dahdichan)) {
1652 ast_copy_string(dahdicopy, dahdichan, sizeof(dahdicopy));
1654 chan = strsep(&c, ",");
1656 if (sscanf(chan, "%30d-%30d", &start, &finish) == 2) {
1658 } else if (sscanf(chan, "%30d", &start)) {
1662 start = 0; finish = 0;
1664 if (finish < start) {
1669 for (x = start; x <= finish; x++) {
1670 snprintf(tmp, sizeof(tmp), "DAHDI/%d", x);
1671 append_interface(iface, sizeof(iface), tmp);
1673 chan = strsep(&c, ",");
1676 if (!ast_strlen_zero(iface)) {
1677 /* Only create a context here when it is really needed. Otherwise default empty context
1678 created by pbx_config may conflict with the one explicitly created by pbx_ael */
1680 con = ast_context_find_or_create(&local_contexts, local_table, userscontext, registrar);
1683 ast_log(LOG_ERROR, "Can't find/create user context '%s'\n", userscontext);
1688 ast_add_extension2(con, 0, cat, -1, NULL, NULL, iface, NULL, NULL, registrar);
1689 /* If voicemail, use "stdexten" else use plain old dial */
1691 snprintf(tmp, sizeof(tmp), "stdexten,%s,${HINT}", cat);
1692 ast_add_extension2(con, 0, cat, 1, NULL, NULL, "Macro", strdup(tmp), ast_free_ptr, registrar);
1694 ast_add_extension2(con, 0, cat, 1, NULL, NULL, "Dial", strdup("${HINT}"), ast_free_ptr, registrar);
1696 altexts = ast_variable_retrieve(cfg, cat, "alternateexts");
1697 if (!ast_strlen_zero(altexts)) {
1698 snprintf(tmp, sizeof(tmp), "%s,1", cat);
1699 ast_copy_string(altcopy, altexts, sizeof(altcopy));
1701 ext = strsep(&c, ",");
1703 ast_add_extension2(con, 0, ext, 1, NULL, NULL, "Goto", strdup(tmp), ast_free_ptr, registrar);
1704 ext = strsep(&c, ",");
1709 ast_config_destroy(cfg);
1712 static int pbx_load_module(void)
1714 struct ast_context *con;
1717 local_table = ast_hashtab_create(17, ast_hashtab_compare_contexts, ast_hashtab_resize_java, ast_hashtab_newsize_java, ast_hashtab_hash_contexts, 0);
1719 if (!pbx_load_config(config))
1720 return AST_MODULE_LOAD_DECLINE;
1724 ast_merge_contexts_and_delete(&local_contexts, local_table, registrar);
1725 local_table = NULL; /* the local table has been moved into the global one. */
1726 local_contexts = NULL;
1728 for (con = NULL; (con = ast_walk_contexts(con));)
1729 ast_context_verify_includes(con);
1731 pbx_set_overrideswitch(overrideswitch_config);
1732 pbx_set_autofallthrough(autofallthrough_config);
1733 pbx_set_extenpatternmatchnew(extenpatternmatchnew_config);
1735 return AST_MODULE_LOAD_SUCCESS;
1738 static int load_module(void)
1740 if (pbx_load_module())
1741 return AST_MODULE_LOAD_DECLINE;
1743 if (static_config && !write_protect_config)
1744 ast_cli_register(&cli_dialplan_save);
1745 ast_cli_register_multiple(cli_pbx_config, ARRAY_LEN(cli_pbx_config));
1747 return AST_MODULE_LOAD_SUCCESS;
1750 static int reload(void)
1752 if (clearglobalvars_config)
1753 pbx_builtin_clear_globals();
1754 return pbx_load_module();
1757 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Text Extension Configuration",
1758 .load = load_module,
1759 .unload = unload_module,