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
27 <support_level>core</support_level>
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
36 #include "asterisk/paths.h" /* ast_config_AST_CONFIG_DIR */
37 #include "asterisk/pbx.h"
38 #include "asterisk/config.h"
39 #include "asterisk/module.h"
40 #include "asterisk/logger.h"
41 #include "asterisk/cli.h"
42 #include "asterisk/channel.h" /* AST_MAX_EXTENSION */
43 #include "asterisk/callerid.h"
45 static const char config[] = "extensions.conf";
46 static const char registrar[] = "pbx_config";
47 static char userscontext[AST_MAX_EXTENSION] = "default";
49 static int static_config = 0;
50 static int write_protect_config = 1;
51 static int autofallthrough_config = 1;
52 static int clearglobalvars_config = 0;
53 static int extenpatternmatchnew_config = 0;
54 static char *overrideswitch_config = NULL;
56 AST_MUTEX_DEFINE_STATIC(save_dialplan_lock);
58 AST_MUTEX_DEFINE_STATIC(reload_lock);
60 static struct ast_context *local_contexts = NULL;
61 static struct ast_hashtab *local_table = NULL;
63 * Prototypes for our completion functions
65 static char *complete_dialplan_remove_include(struct ast_cli_args *);
66 static char *complete_dialplan_add_include(struct ast_cli_args *);
67 static char *complete_dialplan_remove_ignorepat(struct ast_cli_args *);
68 static char *complete_dialplan_add_ignorepat(struct ast_cli_args *);
69 static char *complete_dialplan_remove_extension(struct ast_cli_args *);
70 static char *complete_dialplan_add_extension(struct ast_cli_args *);
73 * Implementation of functions provided by this module
77 * REMOVE INCLUDE command stuff
79 static char *handle_cli_dialplan_remove_include(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
83 e->command = "dialplan remove include";
85 "Usage: dialplan remove include <context> from <context>\n"
86 " Remove an included context from another context.\n";
89 return complete_dialplan_remove_include(a);
92 if (a->argc != 6 || strcmp(a->argv[4], "from"))
95 if (!ast_context_remove_include(a->argv[5], a->argv[3], registrar)) {
96 ast_cli(a->fd, "We are not including '%s' into '%s' now\n",
97 a->argv[3], a->argv[5]);
101 ast_cli(a->fd, "Failed to remove '%s' include from '%s' context\n",
102 a->argv[3], a->argv[5]);
106 /*! \brief return true if 'name' is included by context c */
107 static int lookup_ci(struct ast_context *c, const char *name)
109 struct ast_include *i = NULL;
111 if (ast_rdlock_context(c)) /* error, skip */
113 while ( (i = ast_walk_context_includes(c, i)) )
114 if (!strcmp(name, ast_get_include_name(i)))
116 ast_unlock_context(c);
117 return i ? -1 /* success */ : 0;
120 /*! \brief return true if 'name' is in the ignorepats for context c */
121 static int lookup_c_ip(struct ast_context *c, const char *name)
123 struct ast_ignorepat *ip = NULL;
125 if (ast_rdlock_context(c)) /* error, skip */
127 while ( (ip = ast_walk_context_ignorepats(c, ip)) )
128 if (!strcmp(name, ast_get_ignorepat_name(ip)))
130 ast_unlock_context(c);
131 return ip ? -1 /* success */ : 0;
134 /*! \brief moves to the n-th word in the string, or empty string if none */
135 static const char *skip_words(const char *p, int n)
138 for (;n && *p; p++) {
139 if (isblank(*p) /* XXX order is important */ && !in_blank) {
140 n--; /* one word is gone */
142 } else if (/* !is_blank(*p), we know already, && */ in_blank) {
149 /*! \brief match the first 'len' chars of word. len==0 always succeeds */
150 static int partial_match(const char *s, const char *word, int len)
152 return (len == 0 || !strncmp(s, word, len));
155 /*! \brief split extension\@context in two parts, return -1 on error.
156 * The return string is malloc'ed and pointed by *ext
158 static int split_ec(const char *src, char **ext, char ** const ctx, char ** const cid)
160 char *i, *c, *e = ast_strdup(src); /* now src is not used anymore */
163 return -1; /* malloc error */
164 /* now, parse values from 'exten@context' */
167 if (c == NULL) /* no context part */
168 *ctx = ""; /* it is not overwritten, anyways */
169 else { /* found context, check for duplicity ... */
172 if (strchr(c, '@')) { /* two @, not allowed */
177 if (cid && (i = strchr(e, '/'))) {
181 /* Signal none detected */
187 /* _X_ is the string we need to complete */
188 static char *complete_dialplan_remove_include(struct ast_cli_args *a)
192 int len = strlen(a->word); /* how many bytes to match */
193 struct ast_context *c = NULL;
195 if (a->pos == 3) { /* "dialplan remove include _X_" */
196 if (ast_wrlock_contexts()) {
197 ast_log(LOG_ERROR, "Failed to lock context list\n");
200 /* walk contexts and their includes, return the n-th match */
201 while (!res && (c = ast_walk_contexts(c))) {
202 struct ast_include *i = NULL;
204 if (ast_rdlock_context(c)) /* error ? skip this one */
207 while ( !res && (i = ast_walk_context_includes(c, i)) ) {
208 const char *i_name = ast_get_include_name(i);
209 struct ast_context *nc = NULL;
210 int already_served = 0;
212 if (!partial_match(i_name, a->word, len))
213 continue; /* not matched */
215 /* check if this include is already served or not */
217 /* go through all contexts again till we reach actual
218 * context or already_served = 1
220 while ( (nc = ast_walk_contexts(nc)) && nc != c && !already_served)
221 already_served = lookup_ci(nc, i_name);
223 if (!already_served && ++which > a->n)
224 res = strdup(i_name);
226 ast_unlock_context(c);
229 ast_unlock_contexts();
231 } else if (a->pos == 4) { /* "dialplan remove include CTX _X_" */
233 * complete as 'from', but only if previous context is really
236 char *context, *dupline;
237 const char *s = skip_words(a->line, 3); /* skip 'dialplan' 'remove' 'include' */
241 context = dupline = strdup(s);
243 ast_log(LOG_ERROR, "Out of free memory\n");
246 strsep(&dupline, " ");
248 if (ast_rdlock_contexts()) {
249 ast_log(LOG_ERROR, "Failed to lock contexts list\n");
254 /* go through all contexts and check if is included ... */
255 while (!res && (c = ast_walk_contexts(c)))
256 if (lookup_ci(c, context)) /* context is really included, complete "from" command */
257 res = strdup("from");
258 ast_unlock_contexts();
260 ast_log(LOG_WARNING, "%s not included anywhere\n", context);
263 } else if (a->pos == 5) { /* "dialplan remove include CTX from _X_" */
265 * Context from which we removing include ...
267 char *context, *dupline, *from;
268 const char *s = skip_words(a->line, 3); /* skip 'dialplan' 'remove' 'include' */
269 context = dupline = strdup(s);
271 ast_log(LOG_ERROR, "Out of free memory\n");
275 strsep(&dupline, " "); /* skip context */
277 /* fourth word must be 'from' */
278 from = strsep(&dupline, " ");
279 if (!from || strcmp(from, "from")) {
284 if (ast_rdlock_contexts()) {
285 ast_log(LOG_ERROR, "Failed to lock context list\n");
290 /* walk through all contexts ... */
292 while ( !res && (c = ast_walk_contexts(c))) {
293 const char *c_name = ast_get_context_name(c);
294 if (!partial_match(c_name, a->word, len)) /* not a good target */
296 /* walk through all includes and check if it is our context */
297 if (lookup_ci(c, context) && ++which > a->n)
298 res = strdup(c_name);
300 ast_unlock_contexts();
309 * REMOVE EXTENSION command stuff
311 static char *handle_cli_dialplan_remove_extension(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
313 int removing_priority = 0;
314 char *exten, *context, *cid;
315 char *ret = CLI_FAILURE;
319 e->command = "dialplan remove extension";
321 "Usage: dialplan remove extension exten[/cid]@context [priority]\n"
322 " Remove an extension from a given context. If a priority\n"
323 " is given, only that specific priority from the given extension\n"
324 " will be removed.\n";
327 return complete_dialplan_remove_extension(a);
330 if (a->argc != 5 && a->argc != 4)
331 return CLI_SHOWUSAGE;
334 * Priority input checking ...
337 const char *c = a->argv[4];
339 /* check for digits in whole parameter for right priority ...
340 * why? because atoi (strtol) returns 0 if any characters in
341 * string and whole extension will be removed, it's not good
343 if (!strcmp("hint", c))
344 removing_priority = PRIORITY_HINT;
346 while (*c && isdigit(*c))
348 if (*c) { /* non-digit in string */
349 ast_cli(a->fd, "Invalid priority '%s'\n", a->argv[4]);
352 removing_priority = atoi(a->argv[4]);
355 if (removing_priority == 0) {
356 ast_cli(a->fd, "If you want to remove whole extension, please " \
357 "omit priority argument\n");
362 /* XXX original overwrote argv[3] */
364 * Format exten@context checking ...
366 if (split_ec(a->argv[3], &exten, &context, &cid))
367 return CLI_FAILURE; /* XXX malloc failure */
368 if ((!strlen(exten)) || (!(strlen(context)))) {
369 ast_cli(a->fd, "Missing extension or context name in third argument '%s'\n",
375 if (!ast_context_remove_extension_callerid(context, exten, removing_priority,
376 /* Do NOT substitute S_OR; it is NOT the same thing */
377 cid ? cid : (removing_priority ? "" : NULL), cid ? 1 : 0, registrar)) {
378 if (!removing_priority)
379 ast_cli(a->fd, "Whole extension %s@%s removed\n",
382 ast_cli(a->fd, "Extension %s@%s with priority %d removed\n",
383 exten, context, removing_priority);
388 ast_cli(a->fd, "Failed to remove extension %s/%s@%s\n", exten, cid, context);
390 ast_cli(a->fd, "Failed to remove extension %s@%s\n", exten, context);
398 static char *complete_dialplan_remove_extension(struct ast_cli_args *a)
403 if (a->pos == 3) { /* 'dialplan remove extension _X_' (exten@context ... */
404 struct ast_context *c = NULL;
405 char *context = NULL, *exten = NULL, *cid = NULL;
406 int le = 0; /* length of extension */
407 int lc = 0; /* length of context */
408 int lcid = 0; /* length of cid */
410 lc = split_ec(a->word, &exten, &context, &cid);
411 if (lc) { /* error */
415 lc = strlen(context);
416 lcid = cid ? strlen(cid) : -1;
418 if (ast_rdlock_contexts()) {
419 ast_log(LOG_ERROR, "Failed to lock context list\n");
423 /* find our context ... */
424 while ( (c = ast_walk_contexts(c)) ) { /* match our context if any */
425 struct ast_exten *e = NULL;
427 if (!partial_match(ast_get_context_name(c), context, lc))
428 continue; /* context not matched */
429 while ( (e = ast_walk_context_extensions(c, e)) ) { /* try to complete extensions ... */
430 if ( !strchr(a->word, '/') ||
431 (!strchr(a->word, '@') && partial_match(ast_get_extension_cidmatch(e), cid, lcid)) ||
432 (strchr(a->word, '@') && !strcmp(ast_get_extension_cidmatch(e), cid))) {
433 if ( ((strchr(a->word, '/') || strchr(a->word, '@')) && !strcmp(ast_get_extension_name(e), exten)) ||
434 (!strchr(a->word, '/') && !strchr(a->word, '@') && partial_match(ast_get_extension_name(e), exten, le))) { /* n-th match */
435 if (++which > a->n) {
436 /* If there is an extension then return exten@context. */
437 if (ast_get_extension_matchcid(e) && (!strchr(a->word, '@') || strchr(a->word, '/'))) {
438 if (asprintf(&ret, "%s/%s@%s", ast_get_extension_name(e), ast_get_extension_cidmatch(e), ast_get_context_name(c)) < 0) {
439 ast_log(LOG_WARNING, "asprintf() failed: %s\n", strerror(errno));
443 } else if (!ast_get_extension_matchcid(e) && !strchr(a->word, '/')) {
444 if (asprintf(&ret, "%s@%s", ast_get_extension_name(e), ast_get_context_name(c)) < 0) {
445 ast_log(LOG_WARNING, "asprintf() failed: %s\n", strerror(errno));
454 if (e) /* got a match */
458 ast_unlock_contexts();
461 } else if (a->pos == 4) { /* 'dialplan remove extension EXT _X_' (priority) */
462 char *exten = NULL, *context, *cid, *p;
463 struct ast_context *c;
465 const char *s = skip_words(a->line, 3); /* skip 'dialplan' 'remove' 'extension' */
466 int i = split_ec(s, &exten, &context, &cid); /* parse ext@context */
470 if ( (p = strchr(exten, ' ')) ) /* remove space after extension */
472 if ( (p = strchr(context, ' ')) ) /* remove space after context */
475 lc = strlen(context);
476 len = strlen(a->word);
477 if (le == 0 || lc == 0)
480 if (ast_rdlock_contexts()) {
481 ast_log(LOG_ERROR, "Failed to lock context list\n");
487 while ( (c = ast_walk_contexts(c)) ) {
488 /* XXX locking on c ? */
490 if (strcmp(ast_get_context_name(c), context) != 0)
492 /* got it, we must match here */
494 while ( (e = ast_walk_context_extensions(c, e)) ) {
495 struct ast_exten *priority;
498 if (cid && strcmp(ast_get_extension_cidmatch(e), cid) != 0) {
501 if (strcmp(ast_get_extension_name(e), exten) != 0)
505 while ( !ret && (priority = ast_walk_extension_priorities(e, priority)) ) {
506 snprintf(buffer, sizeof(buffer), "%u", ast_get_extension_priority(priority));
507 if (partial_match(buffer, a->word, len) && ++which > a->n) /* n-th match */
508 ret = strdup(buffer);
514 ast_unlock_contexts();
522 * Include context ...
524 static char *handle_cli_dialplan_add_include(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
528 e->command = "dialplan add include";
530 "Usage: dialplan add include <context> into <context>\n"
531 " Include a context in another context.\n";
534 return complete_dialplan_add_include(a);
537 if (a->argc != 6) /* dialplan add include CTX in CTX */
538 return CLI_SHOWUSAGE;
540 /* fifth arg must be 'into' ... */
541 if (strcmp(a->argv[4], "into"))
542 return CLI_SHOWUSAGE;
544 if (ast_context_add_include(a->argv[5], a->argv[3], registrar)) {
547 ast_cli(a->fd, "Out of memory for context addition\n");
551 ast_cli(a->fd, "Failed to lock context(s) list, please try again later\n");
555 ast_cli(a->fd, "Context '%s' already included in '%s' context\n",
556 a->argv[3], a->argv[5]);
561 ast_cli(a->fd, "There is no existence of context '%s'\n",
562 errno == ENOENT ? a->argv[5] : a->argv[3]);
566 ast_cli(a->fd, "Failed to include '%s' in '%s' context\n",
567 a->argv[3], a->argv[5]);
573 /* show some info ... */
574 ast_cli(a->fd, "Context '%s' included in '%s' context\n",
575 a->argv[3], a->argv[5]);
580 static char *complete_dialplan_add_include(struct ast_cli_args *a)
582 struct ast_context *c;
585 int len = strlen(a->word);
587 if (a->pos == 3) { /* 'dialplan add include _X_' (context) ... */
588 if (ast_rdlock_contexts()) {
589 ast_log(LOG_ERROR, "Failed to lock context list\n");
592 for (c = NULL; !ret && (c = ast_walk_contexts(c)); )
593 if (partial_match(ast_get_context_name(c), a->word, len) && ++which > a->n)
594 ret = strdup(ast_get_context_name(c));
595 ast_unlock_contexts();
597 } else if (a->pos == 4) { /* dialplan add include CTX _X_ */
598 /* complete as 'into' if context exists or we are unable to check */
599 char *context, *dupline;
600 const char *s = skip_words(a->line, 3); /* should not fail */
602 if (a->n != 0) /* only once */
605 /* parse context from line ... */
606 context = dupline = strdup(s);
608 ast_log(LOG_ERROR, "Out of free memory\n");
609 return strdup("into");
611 strsep(&dupline, " ");
613 /* check for context existence ... */
614 if (ast_rdlock_contexts()) {
615 ast_log(LOG_ERROR, "Failed to lock context list\n");
616 /* our fault, we can't check, so complete 'into' ... */
617 ret = strdup("into");
619 struct ast_context *ctx;
620 for (ctx = NULL; !ret && (ctx = ast_walk_contexts(ctx)); )
621 if (!strcmp(context, ast_get_context_name(ctx)))
622 ret = strdup("into"); /* found */
623 ast_unlock_contexts();
627 } else if (a->pos == 5) { /* 'dialplan add include CTX into _X_' (dst context) */
628 char *context, *dupline, *into;
629 const char *s = skip_words(a->line, 3); /* should not fail */
630 context = dupline = strdup(s);
632 ast_log(LOG_ERROR, "Out of free memory\n");
635 strsep(&dupline, " "); /* skip context */
636 into = strsep(&dupline, " ");
637 /* error if missing context or fifth word is not 'into' */
638 if (!strlen(context) || strcmp(into, "into")) {
639 ast_log(LOG_ERROR, "bad context %s or missing into %s\n",
644 if (ast_rdlock_contexts()) {
645 ast_log(LOG_ERROR, "Failed to lock context list\n");
649 for (c = NULL; (c = ast_walk_contexts(c)); )
650 if (!strcmp(context, ast_get_context_name(c)))
652 if (c) { /* first context exists, go on... */
653 /* go through all contexts ... */
654 for (c = NULL; !ret && (c = ast_walk_contexts(c)); ) {
655 if (!strcmp(context, ast_get_context_name(c)))
656 continue; /* skip ourselves */
657 if (partial_match(ast_get_context_name(c), a->word, len) &&
658 !lookup_ci(c, context) /* not included yet */ &&
660 ret = strdup(ast_get_context_name(c));
663 ast_log(LOG_ERROR, "context %s not found\n", context);
665 ast_unlock_contexts();
675 * \brief 'save dialplan' CLI command implementation functions ...
677 static char *handle_cli_dialplan_save(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
679 char filename[256], overrideswitch[256] = "";
680 struct ast_context *c;
681 struct ast_config *cfg;
682 struct ast_variable *v;
683 int incomplete = 0; /* incomplete config write? */
685 struct ast_flags config_flags = { 0 };
686 const char *base, *slash;
690 e->command = "dialplan save";
692 "Usage: dialplan save [/path/to/extension/file]\n"
693 " Save dialplan created by pbx_config module.\n"
695 "Example: dialplan save (/etc/asterisk/extensions.conf)\n"
696 " dialplan save /home/markster (/home/markster/extensions.conf)\n";
702 if (! (static_config && !write_protect_config)) {
704 "I can't save dialplan now, see '%s' example file.\n",
709 if (a->argc != 2 && a->argc != 3)
710 return CLI_SHOWUSAGE;
712 if (ast_mutex_lock(&save_dialplan_lock)) {
714 "Failed to lock dialplan saving (another proccess saving?)\n");
717 /* XXX the code here is quite loose, a pathname with .conf in it
718 * is assumed to be a complete pathname
720 if (a->argc == 3) { /* have config path. Look for *.conf */
722 if (!strstr(a->argv[2], ".conf")) { /*no, this is assumed to be a pathname */
723 /* if filename ends with '/', do not add one */
724 slash = (*(a->argv[2] + strlen(a->argv[2]) -1) == '/') ? "/" : "";
725 } else { /* yes, complete file name */
729 /* no config file, default one */
730 base = ast_config_AST_CONFIG_DIR;
733 snprintf(filename, sizeof(filename), "%s%s%s", base, slash, config);
735 cfg = ast_config_load("extensions.conf", config_flags);
737 ast_cli(a->fd, "Failed to load extensions.conf\n");
738 ast_mutex_unlock(&save_dialplan_lock);
742 /* try to lock contexts list */
743 if (ast_rdlock_contexts()) {
744 ast_cli(a->fd, "Failed to lock contexts list\n");
745 ast_mutex_unlock(&save_dialplan_lock);
746 ast_config_destroy(cfg);
750 /* create new file ... */
751 if (!(output = fopen(filename, "wt"))) {
752 ast_cli(a->fd, "Failed to create file '%s'\n",
754 ast_unlock_contexts();
755 ast_mutex_unlock(&save_dialplan_lock);
756 ast_config_destroy(cfg);
760 /* fireout general info */
761 if (overrideswitch_config) {
762 snprintf(overrideswitch, sizeof(overrideswitch), "overrideswitch=%s\n", overrideswitch_config);
764 fprintf(output, "[general]\nstatic=%s\nwriteprotect=%s\nautofallthrough=%s\nclearglobalvars=%s\n%sextenpatternmatchnew=%s\n\n",
765 static_config ? "yes" : "no",
766 write_protect_config ? "yes" : "no",
767 autofallthrough_config ? "yes" : "no",
768 clearglobalvars_config ? "yes" : "no",
769 overrideswitch_config ? overrideswitch : "",
770 extenpatternmatchnew_config ? "yes" : "no");
772 if ((v = ast_variable_browse(cfg, "globals"))) {
773 fprintf(output, "[globals]\n");
775 fprintf(output, "%s => %s\n", v->name, v->value);
778 fprintf(output, "\n");
781 ast_config_destroy(cfg);
783 #define PUT_CTX_HDR do { \
784 if (!context_header_written) { \
785 fprintf(output, "[%s]\n", ast_get_context_name(c)); \
786 context_header_written = 1; \
790 /* walk all contexts */
791 for (c = NULL; (c = ast_walk_contexts(c)); ) {
792 int context_header_written = 0;
793 struct ast_exten *ext, *last_written_e = NULL;
794 struct ast_include *i;
795 struct ast_ignorepat *ip;
798 /* try to lock context and fireout all info */
799 if (ast_rdlock_context(c)) { /* lock failure */
803 /* registered by this module? */
804 /* XXX do we need this ? */
805 if (!strcmp(ast_get_context_registrar(c), registrar)) {
806 fprintf(output, "[%s]\n", ast_get_context_name(c));
807 context_header_written = 1;
810 /* walk extensions ... */
811 for (ext = NULL; (ext = ast_walk_context_extensions(c, ext)); ) {
812 struct ast_exten *p = NULL;
814 /* fireout priorities */
815 while ( (p = ast_walk_extension_priorities(ext, p)) ) {
816 if (strcmp(ast_get_extension_registrar(p), registrar) != 0) /* not this source */
819 /* make empty line between different extensions */
820 if (last_written_e != NULL &&
821 strcmp(ast_get_extension_name(last_written_e),
822 ast_get_extension_name(p)))
823 fprintf(output, "\n");
828 if (ast_get_extension_priority(p) == PRIORITY_HINT) { /* easy */
829 fprintf(output, "exten => %s,hint,%s\n",
830 ast_get_extension_name(p),
831 ast_get_extension_app(p));
833 const char *sep, *cid;
834 const char *el = ast_get_extension_label(p);
835 char label[128] = "";
837 if (ast_get_extension_matchcid(p)) {
839 cid = ast_get_extension_cidmatch(p);
843 if (el && (snprintf(label, sizeof(label), "(%s)", el) != (strlen(el) + 2)))
844 incomplete = 1; /* error encountered or label > 125 chars */
846 fprintf(output, "exten => %s%s%s,%d%s,%s(%s)\n",
847 ast_get_extension_name(p), (ast_strlen_zero(sep) ? "" : sep), (ast_strlen_zero(cid) ? "" : cid),
848 ast_get_extension_priority(p), label,
849 ast_get_extension_app(p), (ast_strlen_zero(ast_get_extension_app_data(p)) ? "" : (const char *)ast_get_extension_app_data(p)));
854 /* written any extensions? ok, write space between exten & inc */
856 fprintf(output, "\n");
858 /* walk through includes */
859 for (i = NULL; (i = ast_walk_context_includes(c, i)) ; ) {
860 if (strcmp(ast_get_include_registrar(i), registrar) != 0)
861 continue; /* not mine */
863 fprintf(output, "include => %s\n", ast_get_include_name(i));
865 if (ast_walk_context_includes(c, NULL))
866 fprintf(output, "\n");
868 /* walk through switches */
869 for (sw = NULL; (sw = ast_walk_context_switches(c, sw)) ; ) {
870 if (strcmp(ast_get_switch_registrar(sw), registrar) != 0)
871 continue; /* not mine */
873 fprintf(output, "switch => %s/%s\n",
874 ast_get_switch_name(sw), ast_get_switch_data(sw));
877 if (ast_walk_context_switches(c, NULL))
878 fprintf(output, "\n");
880 /* fireout ignorepats ... */
881 for (ip = NULL; (ip = ast_walk_context_ignorepats(c, ip)); ) {
882 if (strcmp(ast_get_ignorepat_registrar(ip), registrar) != 0)
883 continue; /* not mine */
885 fprintf(output, "ignorepat => %s\n",
886 ast_get_ignorepat_name(ip));
889 ast_unlock_context(c);
892 ast_unlock_contexts();
893 ast_mutex_unlock(&save_dialplan_lock);
897 ast_cli(a->fd, "Saved dialplan is incomplete\n");
901 ast_cli(a->fd, "Dialplan successfully saved into '%s'\n",
907 * \brief ADD EXTENSION command stuff
909 static char *handle_cli_dialplan_add_extension(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
914 char *cidmatch, *app, *app_data;
916 const char *into_context;
920 e->command = "dialplan add extension";
922 "Usage: dialplan add extension <exten>,<priority>,<app> into <context> [replace]\n"
924 " app can be either:\n"
926 " app-name(app-data)\n"
927 " app-name,<app-data>\n"
929 " This command will add the new extension into <context>. If\n"
930 " an extension with the same priority already exists and the\n"
931 " 'replace' option is given we will replace the extension.\n"
933 "Example: dialplan add extension 6123,1,Dial,IAX/216.207.245.56/6123 into local\n"
934 " Now, you can dial 6123 and talk to Markster :)\n";
937 return complete_dialplan_add_extension(a);
940 /* check for arguments at first */
941 if (a->argc != 6 && a->argc != 7)
942 return CLI_SHOWUSAGE;
943 if (strcmp(a->argv[4], "into"))
944 return CLI_SHOWUSAGE;
946 if (strcmp(a->argv[6], "replace"))
947 return CLI_SHOWUSAGE;
949 whole_exten = ast_strdupa(a->argv[3]);
950 exten = strsep(&whole_exten,",");
951 if (strchr(exten, '/')) {
953 strsep(&cidmatch,"/");
957 prior = strsep(&whole_exten,",");
959 if (!strcmp(prior, "hint")) {
960 iprior = PRIORITY_HINT;
962 if (sscanf(prior, "%30d", &iprior) != 1) {
963 ast_cli(a->fd, "'%s' is not a valid priority\n", prior);
970 if ((start = strchr(app, '(')) && (end = strrchr(app, ')'))) {
971 *start = *end = '\0';
972 app_data = start + 1;
974 app_data = strchr(app, ',');
983 if (!exten || !prior || !app) {
984 return CLI_SHOWUSAGE;
990 into_context = a->argv[5];
992 if (!ast_context_find(into_context)) {
993 ast_cli(a->fd, "Context '%s' did not exist prior to add extension - the context will be created.\n", into_context);
996 if (!ast_context_find_or_create(NULL, NULL, into_context, registrar)) {
997 ast_cli(a->fd, "Failed to add '%s,%s,%s(%s)' extension into '%s' context\n",
998 exten, prior, app, app_data, into_context);
1002 if (ast_add_extension(into_context, a->argc == 7 ? 1 : 0, exten, iprior, NULL, cidmatch, app,
1003 ast_strdup(app_data), ast_free_ptr, registrar)) {
1006 ast_cli(a->fd, "Out of free memory\n");
1010 ast_cli(a->fd, "Failed to lock context(s) list, please try again later\n");
1014 ast_cli(a->fd, "No existence of '%s' context\n", into_context);
1018 ast_cli(a->fd, "Extension %s@%s with priority %s already exists\n",
1019 exten, into_context, prior);
1023 ast_cli(a->fd, "Failed to add '%s,%s,%s(%s)' extension into '%s' context\n",
1024 exten, prior, app, app_data, into_context);
1031 ast_cli(a->fd, "Extension %s@%s (%s) replace by '%s,%s,%s(%s)'\n",
1032 exten, into_context, prior, exten, prior, app, app_data);
1034 ast_cli(a->fd, "Extension '%s,%s,%s(%s)' added into '%s' context\n",
1035 exten, prior, app, app_data, into_context);
1041 /*! dialplan add extension 6123,1,Dial,IAX/212.71.138.13/6123 into local */
1042 static char *complete_dialplan_add_extension(struct ast_cli_args *a)
1046 if (a->pos == 4) { /* complete 'into' word ... */
1047 return (a->n == 0) ? strdup("into") : NULL;
1048 } else if (a->pos == 5) { /* complete context */
1049 struct ast_context *c = NULL;
1050 int len = strlen(a->word);
1053 /* try to lock contexts list ... */
1054 if (ast_rdlock_contexts()) {
1055 ast_log(LOG_WARNING, "Failed to lock contexts list\n");
1059 /* walk through all contexts */
1060 while ( !res && (c = ast_walk_contexts(c)) )
1061 if (partial_match(ast_get_context_name(c), a->word, len) && ++which > a->n)
1062 res = strdup(ast_get_context_name(c));
1063 ast_unlock_contexts();
1065 } else if (a->pos == 6) {
1066 return a->n == 0 ? strdup("replace") : NULL;
1072 * IGNOREPAT CLI stuff
1074 static char *handle_cli_dialplan_add_ignorepat(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1078 e->command = "dialplan add ignorepat";
1080 "Usage: dialplan add ignorepat <pattern> into <context>\n"
1081 " This command adds a new ignore pattern into context <context>\n"
1083 "Example: dialplan add ignorepat _3XX into local\n";
1086 return complete_dialplan_add_ignorepat(a);
1090 return CLI_SHOWUSAGE;
1092 if (strcmp(a->argv[4], "into"))
1093 return CLI_SHOWUSAGE;
1095 if (ast_context_add_ignorepat(a->argv[5], a->argv[3], registrar)) {
1098 ast_cli(a->fd, "Out of free memory\n");
1102 ast_cli(a->fd, "There is no existence of '%s' context\n", a->argv[5]);
1106 ast_cli(a->fd, "Ignore pattern '%s' already included in '%s' context\n",
1107 a->argv[3], a->argv[5]);
1111 ast_cli(a->fd, "Failed to lock context(s) list, please, try again later\n");
1115 ast_cli(a->fd, "Failed to add ingore pattern '%s' into '%s' context\n",
1116 a->argv[3], a->argv[5]);
1122 ast_cli(a->fd, "Ignore pattern '%s' added into '%s' context\n",
1123 a->argv[3], a->argv[5]);
1128 static char *complete_dialplan_add_ignorepat(struct ast_cli_args *a)
1131 return a->n == 0 ? strdup("into") : NULL;
1132 else if (a->pos == 5) {
1133 struct ast_context *c;
1135 char *dupline, *ignorepat = NULL;
1138 int len = strlen(a->word);
1140 /* XXX skip first three words 'dialplan' 'add' 'ignorepat' */
1141 s = skip_words(a->line, 3);
1144 dupline = strdup(s);
1146 ast_log(LOG_ERROR, "Malloc failure\n");
1149 ignorepat = strsep(&dupline, " ");
1151 if (ast_rdlock_contexts()) {
1152 ast_log(LOG_ERROR, "Failed to lock contexts list\n");
1156 for (c = NULL; !ret && (c = ast_walk_contexts(c));) {
1159 if (!partial_match(ast_get_context_name(c), a->word, len))
1160 continue; /* not mine */
1161 if (ignorepat) /* there must be one, right ? */
1162 found = lookup_c_ip(c, ignorepat);
1163 if (!found && ++which > a->n)
1164 ret = strdup(ast_get_context_name(c));
1168 ast_unlock_contexts();
1175 static char *handle_cli_dialplan_remove_ignorepat(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1179 e->command = "dialplan remove ignorepat";
1181 "Usage: dialplan remove ignorepat <pattern> from <context>\n"
1182 " This command removes an ignore pattern from context <context>\n"
1184 "Example: dialplan remove ignorepat _3XX from local\n";
1187 return complete_dialplan_remove_ignorepat(a);
1191 return CLI_SHOWUSAGE;
1193 if (strcmp(a->argv[4], "from"))
1194 return CLI_SHOWUSAGE;
1196 if (ast_context_remove_ignorepat(a->argv[5], a->argv[3], registrar)) {
1199 ast_cli(a->fd, "Failed to lock context(s) list, please try again later\n");
1203 ast_cli(a->fd, "There is no existence of '%s' context\n", a->argv[5]);
1207 ast_cli(a->fd, "There is no existence of '%s' ignore pattern in '%s' context\n",
1208 a->argv[3], a->argv[5]);
1212 ast_cli(a->fd, "Failed to remove ignore pattern '%s' from '%s' context\n",
1213 a->argv[3], a->argv[5]);
1219 ast_cli(a->fd, "Ignore pattern '%s' removed from '%s' context\n",
1220 a->argv[3], a->argv[5]);
1224 static char *complete_dialplan_remove_ignorepat(struct ast_cli_args *a)
1226 struct ast_context *c;
1231 int len = strlen(a->word);
1232 if (ast_rdlock_contexts()) {
1233 ast_log(LOG_WARNING, "Failed to lock contexts list\n");
1237 for (c = NULL; !ret && (c = ast_walk_contexts(c));) {
1238 struct ast_ignorepat *ip;
1240 if (ast_rdlock_context(c)) /* error, skip it */
1243 for (ip = NULL; !ret && (ip = ast_walk_context_ignorepats(c, ip));) {
1244 if (partial_match(ast_get_ignorepat_name(ip), a->word, len) && ++which > a->n) {
1246 struct ast_context *cw = NULL;
1248 while ( (cw = ast_walk_contexts(cw)) && cw != c && !found) {
1249 /* XXX do i stop on c, or skip it ? */
1250 found = lookup_c_ip(cw, ast_get_ignorepat_name(ip));
1253 ret = strdup(ast_get_ignorepat_name(ip));
1256 ast_unlock_context(c);
1258 ast_unlock_contexts();
1260 } else if (a->pos == 4) {
1261 return a->n == 0 ? strdup("from") : NULL;
1262 } else if (a->pos == 5) { /* XXX check this */
1263 char *dupline, *duplinet, *ignorepat;
1264 int len = strlen(a->word);
1266 dupline = strdup(a->line);
1268 ast_log(LOG_WARNING, "Out of free memory\n");
1273 strsep(&duplinet, " ");
1274 strsep(&duplinet, " ");
1275 ignorepat = strsep(&duplinet, " ");
1282 if (ast_rdlock_contexts()) {
1283 ast_log(LOG_WARNING, "Failed to lock contexts list\n");
1288 for (c = NULL; !ret && (c = ast_walk_contexts(c)); ) {
1289 if (ast_rdlock_context(c)) /* fail, skip it */
1291 if (!partial_match(ast_get_context_name(c), a->word, len))
1293 if (lookup_c_ip(c, ignorepat) && ++which > a->n)
1294 ret = strdup(ast_get_context_name(c));
1295 ast_unlock_context(c);
1297 ast_unlock_contexts();
1305 static int pbx_load_module(void);
1307 static char *handle_cli_dialplan_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1311 e->command = "dialplan reload";
1313 "Usage: dialplan reload\n"
1314 " Reload extensions.conf without reloading any other\n"
1315 " modules. This command does not delete global variables\n"
1316 " unless clearglobalvars is set to yes in extensions.conf\n";
1323 return CLI_SHOWUSAGE;
1325 if (clearglobalvars_config)
1326 pbx_builtin_clear_globals();
1329 ast_cli(a->fd, "Dialplan reloaded.\n");
1334 * CLI entries for commands provided by this module
1336 static struct ast_cli_entry cli_pbx_config[] = {
1337 AST_CLI_DEFINE(handle_cli_dialplan_add_extension, "Add new extension into context"),
1338 AST_CLI_DEFINE(handle_cli_dialplan_remove_extension, "Remove a specified extension"),
1339 AST_CLI_DEFINE(handle_cli_dialplan_add_ignorepat, "Add new ignore pattern"),
1340 AST_CLI_DEFINE(handle_cli_dialplan_remove_ignorepat, "Remove ignore pattern from context"),
1341 AST_CLI_DEFINE(handle_cli_dialplan_add_include, "Include context in other context"),
1342 AST_CLI_DEFINE(handle_cli_dialplan_remove_include, "Remove a specified include from context"),
1343 AST_CLI_DEFINE(handle_cli_dialplan_reload, "Reload extensions and *only* extensions"),
1344 AST_CLI_DEFINE(handle_cli_dialplan_save, "Save current dialplan into a file")
1347 static struct ast_cli_entry cli_dialplan_save =
1348 AST_CLI_DEFINE(handle_cli_dialplan_save, "Save dialplan");
1351 * Standard module functions ...
1353 static int unload_module(void)
1355 if (static_config && !write_protect_config)
1356 ast_cli_unregister(&cli_dialplan_save);
1357 if (overrideswitch_config) {
1358 ast_free(overrideswitch_config);
1360 ast_cli_unregister_multiple(cli_pbx_config, ARRAY_LEN(cli_pbx_config));
1361 ast_context_destroy(NULL, registrar);
1365 /*!\note Protect against misparsing based upon commas in the middle of fields
1366 * like character classes. We've taken steps to permit pretty much every other
1367 * printable character in a character class, so properly handling a comma at
1368 * this level is a natural extension. This is almost like the standard
1369 * application parser in app.c, except that it handles square brackets. */
1370 static char *pbx_strsep(char **destructible, const char *delim)
1375 if (!destructible || !*destructible) {
1378 res = *destructible;
1379 for (; **destructible; (*destructible)++) {
1380 if (**destructible == '[' && !strchr(delim, '[')) {
1382 } else if (**destructible == ']' && !strchr(delim, ']')) {
1386 } else if (**destructible == '\\' && !strchr(delim, '\\')) {
1388 } else if (strchr(delim, **destructible) && !square) {
1389 **destructible = '\0';
1394 if (**destructible == '\0') {
1395 *destructible = NULL;
1400 static int pbx_load_config(const char *config_file)
1402 struct ast_config *cfg;
1406 char realvalue[256];
1408 char realvalue[8192];
1411 struct ast_context *con;
1412 struct ast_variable *v;
1415 const char *newpm, *ovsw;
1416 struct ast_flags config_flags = { 0 };
1417 char lastextension[256];
1418 cfg = ast_config_load(config_file, config_flags);
1419 if (!cfg || cfg == CONFIG_STATUS_FILEINVALID)
1422 /* Use existing config to populate the PBX table */
1423 static_config = ast_true(ast_variable_retrieve(cfg, "general", "static"));
1424 write_protect_config = ast_true(ast_variable_retrieve(cfg, "general", "writeprotect"));
1425 if ((aft = ast_variable_retrieve(cfg, "general", "autofallthrough")))
1426 autofallthrough_config = ast_true(aft);
1427 if ((newpm = ast_variable_retrieve(cfg, "general", "extenpatternmatchnew")))
1428 extenpatternmatchnew_config = ast_true(newpm);
1429 clearglobalvars_config = ast_true(ast_variable_retrieve(cfg, "general", "clearglobalvars"));
1430 if ((ovsw = ast_variable_retrieve(cfg, "general", "overrideswitch"))) {
1431 if (overrideswitch_config) {
1432 ast_free(overrideswitch_config);
1434 if (!ast_strlen_zero(ovsw)) {
1435 overrideswitch_config = ast_strdup(ovsw);
1437 overrideswitch_config = NULL;
1441 ast_copy_string(userscontext, ast_variable_retrieve(cfg, "general", "userscontext") ?: "default", sizeof(userscontext));
1443 for (v = ast_variable_browse(cfg, "globals"); v; v = v->next) {
1444 pbx_substitute_variables_helper(NULL, v->value, realvalue, sizeof(realvalue) - 1);
1445 pbx_builtin_setvar_helper(NULL, v->name, realvalue);
1447 for (cxt = ast_category_browse(cfg, NULL);
1449 cxt = ast_category_browse(cfg, cxt)) {
1450 /* All categories but "general" or "globals" are considered contexts */
1451 if (!strcasecmp(cxt, "general") || !strcasecmp(cxt, "globals")) {
1454 if (!(con = ast_context_find_or_create(&local_contexts, local_table, cxt, registrar))) {
1458 /* Reset continuation items at the beginning of each context */
1459 lastextension[0] = '\0';
1462 for (v = ast_variable_browse(cfg, cxt); v; v = v->next) {
1464 char realext[256] = "";
1465 char *stringp, *ext;
1468 /* get filename for error reporting from top level or an #include */
1469 vfile = !*v->file ? config_file : v->file;
1471 if (!strncasecmp(v->name, "same", 4)) {
1472 if (ast_strlen_zero(lastextension)) {
1474 "No previous pattern in the first entry of context '%s' to match '%s' at line %d of %s!\n",
1475 cxt, v->name, v->lineno, vfile);
1478 if ((stringp = tc = ast_strdup(v->value))) {
1479 ast_copy_string(realext, lastextension, sizeof(realext));
1480 goto process_extension;
1482 } else if (!strcasecmp(v->name, "exten")) {
1485 char *pri, *appl, *data, *cidmatch;
1487 if (!(stringp = tc = ast_strdup(v->value))) {
1491 ext = S_OR(pbx_strsep(&stringp, ","), "");
1492 pbx_substitute_variables_helper(NULL, ext, realext, sizeof(realext) - 1);
1493 ast_copy_string(lastextension, realext, sizeof(lastextension));
1496 if ((cidmatch = strchr(realext, '/'))) {
1498 ast_shrink_phone_number(cidmatch);
1500 pri = ast_strip(S_OR(strsep(&stringp, ","), ""));
1501 if ((label = strchr(pri, '('))) {
1503 if ((end = strchr(label, ')'))) {
1506 ast_log(LOG_WARNING,
1507 "Label missing trailing ')' at line %d of %s\n",
1513 if ((plus = strchr(pri, '+'))) {
1516 if (!strcmp(pri,"hint")) {
1517 ipri = PRIORITY_HINT;
1518 } else if (!strcmp(pri, "next") || !strcmp(pri, "n")) {
1522 ast_log(LOG_WARNING,
1523 "Can't use 'next' priority on the first entry at line %d of %s!\n",
1528 } else if (!strcmp(pri, "same") || !strcmp(pri, "s")) {
1532 ast_log(LOG_WARNING,
1533 "Can't use 'same' priority on the first entry at line %d of %s!\n",
1538 } else if (sscanf(pri, "%30d", &ipri) != 1 &&
1539 (ipri = ast_findlabel_extension2(NULL, con, realext, pri, cidmatch)) < 1) {
1540 ast_log(LOG_WARNING,
1541 "Invalid priority/label '%s' at line %d of %s\n",
1542 pri, v->lineno, vfile);
1546 } else if (ipri < 1) {
1547 ast_log(LOG_WARNING, "Invalid priority '%s' at line %d of %s\n",
1548 pri, v->lineno, vfile);
1552 appl = S_OR(stringp, "");
1553 /* Find the first occurrence of '(' */
1554 if (!strchr(appl, '(')) {
1558 char *orig_appl = ast_strdup(appl);
1565 appl = strsep(&stringp, "(");
1567 /* check if there are variables or expressions without an application, like: exten => 100,hint,DAHDI/g0/${GLOBAL(var)} */
1568 if (strstr(appl, "${") || strstr(appl, "$[")){
1569 /* set appl to original one */
1570 strcpy(appl, orig_appl);
1573 /* no variable before application found -> go ahead */
1575 data = S_OR(stringp, "");
1576 if ((end = strrchr(data, ')'))) {
1579 ast_log(LOG_WARNING,
1580 "No closing parenthesis found? '%s(%s' at line %d of %s\n",
1581 appl, data, v->lineno, vfile);
1584 ast_free(orig_appl);
1587 appl = ast_skip_blanks(appl);
1593 if (!ast_opt_dont_warn && (!strcmp(realext, "_.") || !strcmp(realext, "_!"))) {
1594 ast_log(LOG_WARNING,
1595 "The use of '%s' for an extension is strongly discouraged and can have unexpected behavior. Please use '_X%c' instead at line %d of %s\n",
1596 realext, realext[1], v->lineno, vfile);
1598 if (ast_add_extension2(con, 0, realext, ipri, label, cidmatch, appl, ast_strdup(data), ast_free_ptr, registrar)) {
1599 ast_log(LOG_WARNING,
1600 "Unable to register extension at line %d of %s\n",
1605 } else if (!strcasecmp(v->name, "include")) {
1606 pbx_substitute_variables_helper(NULL, v->value, realvalue, sizeof(realvalue) - 1);
1607 if (ast_context_add_include2(con, realvalue, registrar)) {
1610 ast_log(LOG_WARNING, "Out of memory for context addition\n");
1614 ast_log(LOG_WARNING, "Failed to lock context(s) list, please try again later\n");
1618 ast_log(LOG_WARNING,
1619 "Context '%s' already included in '%s' context on include at line %d of %s\n",
1620 v->value, cxt, v->lineno, vfile);
1625 ast_log(LOG_WARNING,
1626 "There is no existence of context '%s' included at line %d of %s\n",
1627 errno == ENOENT ? v->value : cxt, v->lineno, vfile);
1631 ast_log(LOG_WARNING,
1632 "Failed to include '%s' in '%s' context at line %d of %s\n",
1633 v->value, cxt, v->lineno, vfile);
1637 } else if (!strcasecmp(v->name, "ignorepat")) {
1638 pbx_substitute_variables_helper(NULL, v->value, realvalue, sizeof(realvalue) - 1);
1639 if (ast_context_add_ignorepat2(con, realvalue, registrar)) {
1640 ast_log(LOG_WARNING,
1641 "Unable to include ignorepat '%s' in context '%s' at line %d of %s\n",
1642 v->value, cxt, v->lineno, vfile);
1644 } else if (!strcasecmp(v->name, "switch") || !strcasecmp(v->name, "lswitch") || !strcasecmp(v->name, "eswitch")) {
1646 stringp = realvalue;
1648 if (!strcasecmp(v->name, "switch")) {
1649 pbx_substitute_variables_helper(NULL, v->value, realvalue, sizeof(realvalue) - 1);
1651 ast_copy_string(realvalue, v->value, sizeof(realvalue));
1653 appl = strsep(&stringp, "/");
1654 data = S_OR(stringp, "");
1655 if (ast_context_add_switch2(con, appl, data, !strcasecmp(v->name, "eswitch"), registrar)) {
1656 ast_log(LOG_WARNING,
1657 "Unable to include switch '%s' in context '%s' at line %d of %s\n",
1658 v->value, cxt, v->lineno, vfile);
1661 ast_log(LOG_WARNING,
1662 "==!!== Unknown directive: %s at line %d of %s -- IGNORING!!!\n",
1663 v->name, v->lineno, vfile);
1667 ast_config_destroy(cfg);
1671 static void append_interface(char *iface, int maxlen, char *add)
1673 int len = strlen(iface);
1674 if (strlen(add) + len < maxlen - 2) {
1675 if (strlen(iface)) {
1677 strcpy(iface + len + 1, add);
1683 static void pbx_load_users(void)
1685 struct ast_config *cfg;
1687 const char *dahdichan;
1688 const char *hasexten, *altexts;
1691 char dahdicopy[256];
1692 char *ext, altcopy[256];
1695 int start, finish, x;
1696 struct ast_context *con = NULL;
1697 struct ast_flags config_flags = { 0 };
1699 cfg = ast_config_load("users.conf", config_flags);
1703 for (cat = ast_category_browse(cfg, NULL); cat ; cat = ast_category_browse(cfg, cat)) {
1704 if (!strcasecmp(cat, "general"))
1707 if (ast_true(ast_config_option(cfg, cat, "hassip"))) {
1708 snprintf(tmp, sizeof(tmp), "SIP/%s", cat);
1709 append_interface(iface, sizeof(iface), tmp);
1711 if (ast_true(ast_config_option(cfg, cat, "hasiax"))) {
1712 snprintf(tmp, sizeof(tmp), "IAX2/%s", cat);
1713 append_interface(iface, sizeof(iface), tmp);
1715 if (ast_true(ast_config_option(cfg, cat, "hash323"))) {
1716 snprintf(tmp, sizeof(tmp), "H323/%s", cat);
1717 append_interface(iface, sizeof(iface), tmp);
1719 hasexten = ast_config_option(cfg, cat, "hasexten");
1720 if (hasexten && !ast_true(hasexten))
1722 hasvoicemail = ast_true(ast_config_option(cfg, cat, "hasvoicemail"));
1723 dahdichan = ast_variable_retrieve(cfg, cat, "dahdichan");
1725 dahdichan = ast_variable_retrieve(cfg, "general", "dahdichan");
1726 if (!ast_strlen_zero(dahdichan)) {
1727 ast_copy_string(dahdicopy, dahdichan, sizeof(dahdicopy));
1729 chan = strsep(&c, ",");
1731 if (sscanf(chan, "%30d-%30d", &start, &finish) == 2) {
1733 } else if (sscanf(chan, "%30d", &start)) {
1737 start = 0; finish = 0;
1739 if (finish < start) {
1744 for (x = start; x <= finish; x++) {
1745 snprintf(tmp, sizeof(tmp), "DAHDI/%d", x);
1746 append_interface(iface, sizeof(iface), tmp);
1748 chan = strsep(&c, ",");
1751 if (!ast_strlen_zero(iface)) {
1752 /* Only create a context here when it is really needed. Otherwise default empty context
1753 created by pbx_config may conflict with the one explicitly created by pbx_ael */
1755 con = ast_context_find_or_create(&local_contexts, local_table, userscontext, registrar);
1758 ast_log(LOG_ERROR, "Can't find/create user context '%s'\n", userscontext);
1763 ast_add_extension2(con, 0, cat, -1, NULL, NULL, iface, NULL, NULL, registrar);
1764 /* If voicemail, use "stdexten" else use plain old dial */
1766 if (ast_opt_stdexten_macro) {
1767 /* Use legacy stdexten macro method. */
1768 snprintf(tmp, sizeof(tmp), "stdexten,%s,${HINT}", cat);
1769 ast_add_extension2(con, 0, cat, 1, NULL, NULL, "Macro", ast_strdup(tmp), ast_free_ptr, registrar);
1771 snprintf(tmp, sizeof(tmp), "%s,stdexten(${HINT})", cat);
1772 ast_add_extension2(con, 0, cat, 1, NULL, NULL, "Gosub", ast_strdup(tmp), ast_free_ptr, registrar);
1775 ast_add_extension2(con, 0, cat, 1, NULL, NULL, "Dial", ast_strdup("${HINT}"), ast_free_ptr, registrar);
1777 altexts = ast_variable_retrieve(cfg, cat, "alternateexts");
1778 if (!ast_strlen_zero(altexts)) {
1779 snprintf(tmp, sizeof(tmp), "%s,1", cat);
1780 ast_copy_string(altcopy, altexts, sizeof(altcopy));
1782 ext = strsep(&c, ",");
1784 ast_add_extension2(con, 0, ext, 1, NULL, NULL, "Goto", ast_strdup(tmp), ast_free_ptr, registrar);
1785 ext = strsep(&c, ",");
1790 ast_config_destroy(cfg);
1793 static int pbx_load_module(void)
1795 struct ast_context *con;
1797 ast_mutex_lock(&reload_lock);
1800 local_table = ast_hashtab_create(17, ast_hashtab_compare_contexts, ast_hashtab_resize_java, ast_hashtab_newsize_java, ast_hashtab_hash_contexts, 0);
1802 if (!pbx_load_config(config)) {
1803 ast_mutex_unlock(&reload_lock);
1804 return AST_MODULE_LOAD_DECLINE;
1809 ast_merge_contexts_and_delete(&local_contexts, local_table, registrar);
1810 local_table = NULL; /* the local table has been moved into the global one. */
1811 local_contexts = NULL;
1813 ast_mutex_unlock(&reload_lock);
1815 for (con = NULL; (con = ast_walk_contexts(con));)
1816 ast_context_verify_includes(con);
1818 pbx_set_overrideswitch(overrideswitch_config);
1819 pbx_set_autofallthrough(autofallthrough_config);
1820 pbx_set_extenpatternmatchnew(extenpatternmatchnew_config);
1822 return AST_MODULE_LOAD_SUCCESS;
1825 static int load_module(void)
1827 if (static_config && !write_protect_config)
1828 ast_cli_register(&cli_dialplan_save);
1829 ast_cli_register_multiple(cli_pbx_config, ARRAY_LEN(cli_pbx_config));
1831 if (pbx_load_module())
1832 return AST_MODULE_LOAD_DECLINE;
1834 return AST_MODULE_LOAD_SUCCESS;
1837 static int reload(void)
1839 if (clearglobalvars_config)
1840 pbx_builtin_clear_globals();
1841 return pbx_load_module();
1844 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Text Extension Configuration",
1845 .load = load_module,
1846 .unload = unload_module,