Add "dialplan remove context" and modify "dialplan add include"
[asterisk/asterisk.git] / pbx / pbx_config.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2006, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  *
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.
13  *
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.
17  */
18
19 /*! \file
20  *
21  * \brief Populate and remember extensions from static config file
22  *
23  * 
24  */
25
26 /*** MODULEINFO
27         <support_level>core</support_level>
28  ***/
29
30 #include "asterisk.h"
31
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
33
34 #include <ctype.h>
35
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"
44
45 static const char config[] = "extensions.conf";
46 static const char registrar[] = "pbx_config";
47 static char userscontext[AST_MAX_EXTENSION] = "default";
48
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;
55
56 AST_MUTEX_DEFINE_STATIC(save_dialplan_lock);
57
58 AST_MUTEX_DEFINE_STATIC(reload_lock);
59
60 static struct ast_context *local_contexts = NULL;
61 static struct ast_hashtab *local_table = NULL;
62 /*
63  * Prototypes for our completion functions
64  */
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 *);
71 static char *complete_dialplan_remove_context(struct ast_cli_args *);
72
73 /*
74  * Implementation of functions provided by this module
75  */
76
77 /*!
78  * * REMOVE context command stuff
79  */
80
81 static char *handle_cli_dialplan_remove_context(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
82 {
83         struct ast_context *con;
84
85         switch (cmd) {
86         case CLI_INIT:
87                 e->command = "dialplan remove context";
88                 e->usage =
89                         "Usage: dialplan remove context <context>\n"
90                         "       Removes all extensions from a specified context.\n";
91                 return NULL;
92         case CLI_GENERATE:
93                 return complete_dialplan_remove_context(a);
94         }
95
96         if (a->argc != 4) {
97                 return CLI_SHOWUSAGE;
98         }
99
100         con = ast_context_find(a->argv[3]);
101
102         if (!con) {
103                 ast_cli(a->fd, "There is no such context as '%s'\n",
104                         a->argv[3]);
105                 return CLI_SUCCESS;
106         } else {
107                 ast_context_destroy(con, registrar);
108                 ast_cli(a->fd, "Removing context '%s'\n",
109                         a->argv[3]);
110                 return CLI_SUCCESS;
111         }
112 }
113 /*!
114  * REMOVE INCLUDE command stuff
115  */
116 static char *handle_cli_dialplan_remove_include(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
117 {
118         switch (cmd) {
119         case CLI_INIT:
120                 e->command = "dialplan remove include";
121                 e->usage =
122                         "Usage: dialplan remove include <context> from <context>\n"
123                         "       Remove an included context from another context.\n";
124                 return NULL;
125         case CLI_GENERATE:
126                 return complete_dialplan_remove_include(a);
127         }
128
129         if (a->argc != 6 || strcmp(a->argv[4], "from"))
130                 return CLI_SHOWUSAGE;
131
132         if (!ast_context_remove_include(a->argv[5], a->argv[3], registrar)) {
133                 ast_cli(a->fd, "We are not including '%s' into '%s' now\n",
134                         a->argv[3], a->argv[5]);
135                 return CLI_SUCCESS;
136         }
137
138         ast_cli(a->fd, "Failed to remove '%s' include from '%s' context\n",
139                 a->argv[3], a->argv[5]);
140         return CLI_FAILURE;
141 }
142
143 /*! \brief return true if 'name' is included by context c */
144 static int lookup_ci(struct ast_context *c, const char *name)
145 {
146         struct ast_include *i = NULL;
147
148         if (ast_rdlock_context(c))      /* error, skip */
149                 return 0;
150         while ( (i = ast_walk_context_includes(c, i)) )
151                 if (!strcmp(name, ast_get_include_name(i)))
152                         break;
153         ast_unlock_context(c);
154         return i ? -1 /* success */ : 0;
155 }
156
157 /*! \brief return true if 'name' is in the ignorepats for context c */
158 static int lookup_c_ip(struct ast_context *c, const char *name)
159 {
160         struct ast_ignorepat *ip = NULL;
161
162         if (ast_rdlock_context(c))      /* error, skip */
163                 return 0;
164         while ( (ip = ast_walk_context_ignorepats(c, ip)) )
165                 if (!strcmp(name, ast_get_ignorepat_name(ip)))
166                         break;
167         ast_unlock_context(c);
168         return ip ? -1 /* success */ : 0;
169 }
170
171 /*! \brief moves to the n-th word in the string, or empty string if none */
172 static const char *skip_words(const char *p, int n)
173 {
174         int in_blank = 0;
175         for (;n && *p; p++) {
176                 if (isblank(*p) /* XXX order is important */ && !in_blank) {
177                         n--;    /* one word is gone */
178                         in_blank = 1;
179                 } else if (/* !is_blank(*p), we know already, && */ in_blank) {
180                         in_blank = 0;
181                 }
182         }
183         return p;
184 }
185
186 /*! \brief match the first 'len' chars of word. len==0 always succeeds */
187 static int partial_match(const char *s, const char *word, int len)
188 {
189         return (len == 0 || !strncmp(s, word, len));
190 }
191
192 /*! \brief split extension\@context in two parts, return -1 on error.
193  * The return string is malloc'ed and pointed by *ext
194  */
195 static int split_ec(const char *src, char **ext, char ** const ctx, char ** const cid)
196 {
197         char *i, *c, *e = ast_strdup(src); /* now src is not used anymore */
198
199         if (e == NULL)
200                 return -1;      /* malloc error */
201         /* now, parse values from 'exten@context' */
202         *ext = e;
203         c = strchr(e, '@');
204         if (c == NULL)  /* no context part */
205                 *ctx = "";      /* it is not overwritten, anyways */
206         else {  /* found context, check for duplicity ... */
207                 *c++ = '\0';
208                 *ctx = c;
209                 if (strchr(c, '@')) { /* two @, not allowed */
210                         free(e);
211                         return -1;
212                 }
213         }
214         if (cid && (i = strchr(e, '/'))) {
215                 *i++ = '\0';
216                 *cid = i;
217         } else if (cid) {
218                 /* Signal none detected */
219                 *cid = NULL;
220         }
221         return 0;
222 }
223
224 /* _X_ is the string we need to complete */
225 static char *complete_dialplan_remove_include(struct ast_cli_args *a)
226 {
227         int which = 0;
228         char *res = NULL;
229         int len = strlen(a->word); /* how many bytes to match */
230         struct ast_context *c = NULL;
231
232         if (a->pos == 3) {              /* "dialplan remove include _X_" */
233                 if (ast_wrlock_contexts()) {
234                         ast_log(LOG_ERROR, "Failed to lock context list\n");
235                         return NULL;
236                 }
237                 /* walk contexts and their includes, return the n-th match */
238                 while (!res && (c = ast_walk_contexts(c))) {
239                         struct ast_include *i = NULL;
240
241                         if (ast_rdlock_context(c))      /* error ? skip this one */
242                                 continue;
243
244                         while ( !res && (i = ast_walk_context_includes(c, i)) ) {
245                                 const char *i_name = ast_get_include_name(i);
246                                 struct ast_context *nc = NULL;
247                                 int already_served = 0;
248
249                                 if (!partial_match(i_name, a->word, len))
250                                         continue;       /* not matched */
251
252                                 /* check if this include is already served or not */
253
254                                 /* go through all contexts again till we reach actual
255                                  * context or already_served = 1
256                                  */
257                                 while ( (nc = ast_walk_contexts(nc)) && nc != c && !already_served)
258                                         already_served = lookup_ci(nc, i_name);
259
260                                 if (!already_served && ++which > a->n)
261                                         res = strdup(i_name);
262                         }
263                         ast_unlock_context(c);
264                 }
265
266                 ast_unlock_contexts();
267                 return res;
268         } else if (a->pos == 4) { /* "dialplan remove include CTX _X_" */
269                 /*
270                  * complete as 'from', but only if previous context is really
271                  * included somewhere
272                  */
273                 char *context, *dupline;
274                 const char *s = skip_words(a->line, 3); /* skip 'dialplan' 'remove' 'include' */
275
276                 if (a->n > 0)
277                         return NULL;
278                 context = dupline = strdup(s);
279                 if (!dupline) {
280                         ast_log(LOG_ERROR, "Out of free memory\n");
281                         return NULL;
282                 }
283                 strsep(&dupline, " ");
284
285                 if (ast_rdlock_contexts()) {
286                         ast_log(LOG_ERROR, "Failed to lock contexts list\n");
287                         free(context);
288                         return NULL;
289                 }
290
291                 /* go through all contexts and check if is included ... */
292                 while (!res && (c = ast_walk_contexts(c)))
293                         if (lookup_ci(c, context)) /* context is really included, complete "from" command */
294                                 res = strdup("from");
295                 ast_unlock_contexts();
296                 if (!res)
297                         ast_log(LOG_WARNING, "%s not included anywhere\n", context);
298                 free(context);
299                 return res;
300         } else if (a->pos == 5) { /* "dialplan remove include CTX from _X_" */
301                 /*
302                  * Context from which we removing include ... 
303                  */
304                 char *context, *dupline, *from;
305                 const char *s = skip_words(a->line, 3); /* skip 'dialplan' 'remove' 'include' */
306                 context = dupline = strdup(s);
307                 if (!dupline) {
308                         ast_log(LOG_ERROR, "Out of free memory\n");
309                         return NULL;
310                 }
311
312                 strsep(&dupline, " "); /* skip context */
313
314                 /* fourth word must be 'from' */
315                 from = strsep(&dupline, " ");
316                 if (!from || strcmp(from, "from")) {
317                         free(context);
318                         return NULL;
319                 }
320
321                 if (ast_rdlock_contexts()) {
322                         ast_log(LOG_ERROR, "Failed to lock context list\n");
323                         free(context);
324                         return NULL;
325                 }
326
327                 /* walk through all contexts ... */
328                 c = NULL;
329                 while ( !res && (c = ast_walk_contexts(c))) {
330                         const char *c_name = ast_get_context_name(c);
331                         if (!partial_match(c_name, a->word, len))       /* not a good target */
332                                 continue;
333                         /* walk through all includes and check if it is our context */  
334                         if (lookup_ci(c, context) && ++which > a->n)
335                                 res = strdup(c_name);
336                 }
337                 ast_unlock_contexts();
338                 free(context);
339                 return res;
340         }
341
342         return NULL;
343 }
344
345 /*!
346  * REMOVE EXTENSION command stuff
347  */
348 static char *handle_cli_dialplan_remove_extension(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
349 {
350         int removing_priority = 0;
351         char *exten, *context, *cid;
352         char *ret = CLI_FAILURE;
353
354         switch (cmd) {
355         case CLI_INIT:
356                 e->command = "dialplan remove extension";
357                 e->usage =
358                         "Usage: dialplan remove extension exten[/cid]@context [priority]\n"
359                         "       Remove an extension from a given context. If a priority\n"
360                         "       is given, only that specific priority from the given extension\n"
361                         "       will be removed.\n";
362                 return NULL;
363         case CLI_GENERATE:
364                 return complete_dialplan_remove_extension(a);
365         }
366
367         if (a->argc != 5 && a->argc != 4)
368                 return CLI_SHOWUSAGE;
369
370         /*
371          * Priority input checking ...
372          */
373         if (a->argc == 5) {
374                 const char *c = a->argv[4];
375
376                 /* check for digits in whole parameter for right priority ...
377                  * why? because atoi (strtol) returns 0 if any characters in
378                  * string and whole extension will be removed, it's not good
379                  */
380                 if (!strcmp("hint", c))
381                         removing_priority = PRIORITY_HINT;
382                 else {
383                         while (*c && isdigit(*c))
384                                 c++;
385                         if (*c) { /* non-digit in string */
386                                 ast_cli(a->fd, "Invalid priority '%s'\n", a->argv[4]);
387                                 return CLI_FAILURE;
388                         }
389                         removing_priority = atoi(a->argv[4]);
390                 }
391
392                 if (removing_priority == 0) {
393                         ast_cli(a->fd, "If you want to remove whole extension, please " \
394                                 "omit priority argument\n");
395                         return CLI_FAILURE;
396                 }
397         }
398
399         /* XXX original overwrote argv[3] */
400         /*
401          * Format exten@context checking ...
402          */
403         if (split_ec(a->argv[3], &exten, &context, &cid))
404                 return CLI_FAILURE; /* XXX malloc failure */
405         if ((!strlen(exten)) || (!(strlen(context)))) {
406                 ast_cli(a->fd, "Missing extension or context name in third argument '%s'\n",
407                         a->argv[3]);
408                 free(exten);
409                 return CLI_FAILURE;
410         }
411
412         if (!ast_context_remove_extension_callerid(context, exten, removing_priority,
413                         /* Do NOT substitute S_OR; it is NOT the same thing */
414                         cid ? cid : (removing_priority ? "" : NULL), cid ? 1 : 0, registrar)) {
415                 if (!removing_priority)
416                         ast_cli(a->fd, "Whole extension %s@%s removed\n",
417                                 exten, context);
418                 else
419                         ast_cli(a->fd, "Extension %s@%s with priority %d removed\n",
420                                 exten, context, removing_priority);
421                         
422                 ret = CLI_SUCCESS;
423         } else {
424                 if (cid) {
425                         ast_cli(a->fd, "Failed to remove extension %s/%s@%s\n", exten, cid, context);
426                 } else {
427                         ast_cli(a->fd, "Failed to remove extension %s@%s\n", exten, context);
428                 }
429                 ret = CLI_FAILURE;
430         }
431         free(exten);
432         return ret;
433 }
434
435 static char *complete_dialplan_remove_extension(struct ast_cli_args *a)
436 {
437         char *ret = NULL;
438         int which = 0;
439
440         if (a->pos == 3) { /* 'dialplan remove extension _X_' (exten@context ... */
441                 struct ast_context *c = NULL;
442                 char *context = NULL, *exten = NULL, *cid = NULL;
443                 int le = 0;     /* length of extension */
444                 int lc = 0;     /* length of context */
445                 int lcid = 0; /* length of cid */
446
447                 lc = split_ec(a->word, &exten, &context, &cid);
448                 if (lc) { /* error */
449                         return NULL;
450                 }
451                 le = strlen(exten);
452                 lc = strlen(context);
453                 lcid = cid ? strlen(cid) : -1;
454
455                 if (ast_rdlock_contexts()) {
456                         ast_log(LOG_ERROR, "Failed to lock context list\n");
457                         goto error2;
458                 }
459
460                 /* find our context ... */
461                 while ( (c = ast_walk_contexts(c)) ) {  /* match our context if any */
462                         struct ast_exten *e = NULL;
463                         /* XXX locking ? */
464                         if (!partial_match(ast_get_context_name(c), context, lc))
465                                 continue;       /* context not matched */
466                         while ( (e = ast_walk_context_extensions(c, e)) ) { /* try to complete extensions ... */
467                                 if ( !strchr(a->word, '/') ||
468                                                 (!strchr(a->word, '@') && partial_match(ast_get_extension_cidmatch(e), cid, lcid)) ||
469                                                 (strchr(a->word, '@') && !strcmp(ast_get_extension_cidmatch(e), cid))) {
470                                         if ( ((strchr(a->word, '/') || strchr(a->word, '@')) && !strcmp(ast_get_extension_name(e), exten)) ||
471                                                  (!strchr(a->word, '/') && !strchr(a->word, '@') && partial_match(ast_get_extension_name(e), exten, le))) { /* n-th match */
472                                                 if (++which > a->n) {
473                                                         /* If there is an extension then return exten@context. */
474                                                         if (ast_get_extension_matchcid(e) && (!strchr(a->word, '@') || strchr(a->word, '/'))) {
475                                                                 if (asprintf(&ret, "%s/%s@%s", ast_get_extension_name(e), ast_get_extension_cidmatch(e), ast_get_context_name(c)) < 0) {
476                                                                         ast_log(LOG_WARNING, "asprintf() failed: %s\n", strerror(errno));
477                                                                         ret = NULL;
478                                                                 }
479                                                                 break;
480                                                         } else if (!ast_get_extension_matchcid(e) && !strchr(a->word, '/')) {
481                                                                 if (asprintf(&ret, "%s@%s", ast_get_extension_name(e), ast_get_context_name(c)) < 0) {
482                                                                         ast_log(LOG_WARNING, "asprintf() failed: %s\n", strerror(errno));
483                                                                         ret = NULL;
484                                                                 }
485                                                                 break;
486                                                         }
487                                                 }
488                                         }
489                                 }
490                         }
491                         if (e)  /* got a match */
492                                 break;
493                 }
494
495                 ast_unlock_contexts();
496         error2:
497                 free(exten);
498         } else if (a->pos == 4) { /* 'dialplan remove extension EXT _X_' (priority) */
499                 char *exten = NULL, *context, *cid, *p;
500                 struct ast_context *c;
501                 int le, lc, len;
502                 const char *s = skip_words(a->line, 3); /* skip 'dialplan' 'remove' 'extension' */
503                 int i = split_ec(s, &exten, &context, &cid);    /* parse ext@context */
504
505                 if (i)  /* error */
506                         goto error3;
507                 if ( (p = strchr(exten, ' ')) ) /* remove space after extension */
508                         *p = '\0';
509                 if ( (p = strchr(context, ' ')) ) /* remove space after context */
510                         *p = '\0';
511                 le = strlen(exten);
512                 lc = strlen(context);
513                 len = strlen(a->word);
514                 if (le == 0 || lc == 0)
515                         goto error3;
516
517                 if (ast_rdlock_contexts()) {
518                         ast_log(LOG_ERROR, "Failed to lock context list\n");
519                         goto error3;
520                 }
521
522                 /* walk contexts */
523                 c = NULL;
524                 while ( (c = ast_walk_contexts(c)) ) {
525                         /* XXX locking on c ? */
526                         struct ast_exten *e;
527                         if (strcmp(ast_get_context_name(c), context) != 0)
528                                 continue;
529                         /* got it, we must match here */
530                         e = NULL;
531                         while ( (e = ast_walk_context_extensions(c, e)) ) {
532                                 struct ast_exten *priority;
533                                 char buffer[10];
534
535                                 if (cid && strcmp(ast_get_extension_cidmatch(e), cid) != 0) {
536                                         continue;
537                                 }
538                                 if (strcmp(ast_get_extension_name(e), exten) != 0)
539                                         continue;
540                                 /* XXX lock e ? */
541                                 priority = NULL;
542                                 while ( !ret && (priority = ast_walk_extension_priorities(e, priority)) ) {
543                                         snprintf(buffer, sizeof(buffer), "%u", ast_get_extension_priority(priority));
544                                         if (partial_match(buffer, a->word, len) && ++which > a->n) /* n-th match */
545                                                 ret = strdup(buffer);
546                                 }
547                                 break;
548                         }
549                         break;
550                 }
551                 ast_unlock_contexts();
552         error3:
553                 free(exten);
554         }
555         return ret; 
556 }
557
558 /*!
559  * Include context ...
560  */
561 static char *handle_cli_dialplan_add_include(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
562 {
563         const char *into_context;
564
565         switch (cmd) {
566         case CLI_INIT:
567                 e->command = "dialplan add include";
568                 e->usage =
569                         "Usage: dialplan add include <context> into <context>\n"
570                         "       Include a context in another context.\n";
571                 return NULL;
572         case CLI_GENERATE:
573                 return complete_dialplan_add_include(a);
574         }
575
576         if (a->argc != 6) /* dialplan add include CTX in CTX */
577                 return CLI_SHOWUSAGE;
578
579         /* fifth arg must be 'into' ... */
580         if (strcmp(a->argv[4], "into"))
581                 return CLI_SHOWUSAGE;
582
583         into_context = a->argv[5];
584
585         if (!ast_context_find(into_context)) {
586                 ast_cli(a->fd, "Context '%s' did not exist prior to add include - the context will be created.\n", into_context);
587         }
588
589         if (!ast_context_find_or_create(NULL, NULL, into_context, registrar)) {
590                 ast_cli(a->fd, "ast_context_find_or_create() failed\n");
591                 ast_cli(a->fd, "Failed to include '%s' in '%s' context\n",a->argv[3], a->argv[5]);
592                 return CLI_FAILURE;
593         }
594
595         if (ast_context_add_include(a->argv[5], a->argv[3], registrar)) {
596                 switch (errno) {
597                 case ENOMEM:
598                         ast_cli(a->fd, "Out of memory for context addition\n");
599                         break;
600
601                 case EBUSY:
602                         ast_cli(a->fd, "Failed to lock context(s) list, please try again later\n");
603                         break;
604
605                 case EEXIST:
606                         ast_cli(a->fd, "Context '%s' already included in '%s' context\n",
607                                 a->argv[3], a->argv[5]);
608                         break;
609
610                 case ENOENT:
611                 case EINVAL:
612                         ast_cli(a->fd, "There is no existence of context '%s'\n",
613                                 errno == ENOENT ? a->argv[5] : a->argv[3]);
614                         break;
615
616                 default:
617                         ast_cli(a->fd, "Failed to include '%s' in '%s' context\n",
618                                 a->argv[3], a->argv[5]);
619                         break;
620                 }
621                 return CLI_FAILURE;
622         }
623
624         /* show some info ... */
625         ast_cli(a->fd, "Context '%s' included in '%s' context\n",
626                 a->argv[3], a->argv[5]);
627
628         return CLI_SUCCESS;
629 }
630
631 static char *complete_dialplan_add_include(struct ast_cli_args *a)
632 {
633         struct ast_context *c;
634         int which = 0;
635         char *ret = NULL;
636         int len = strlen(a->word);
637
638         if (a->pos == 3) {              /* 'dialplan add include _X_' (context) ... */
639                 if (ast_rdlock_contexts()) {
640                         ast_log(LOG_ERROR, "Failed to lock context list\n");
641                         return NULL;
642                 }
643                 for (c = NULL; !ret && (c = ast_walk_contexts(c)); )
644                         if (partial_match(ast_get_context_name(c), a->word, len) && ++which > a->n)
645                                 ret = strdup(ast_get_context_name(c));
646                 ast_unlock_contexts();
647                 return ret;
648         } else if (a->pos == 4) { /* dialplan add include CTX _X_ */
649                 /* always complete  as 'into' */
650                 return (a->n == 0) ? strdup("into") : NULL;
651         } else if (a->pos == 5) { /* 'dialplan add include CTX into _X_' (dst context) */
652                 char *context, *dupline, *into;
653                 const char *s = skip_words(a->line, 3); /* should not fail */
654                 context = dupline = strdup(s);
655
656                 if (!dupline) {
657                         ast_log(LOG_ERROR, "Out of free memory\n");
658                         return NULL;
659                 }
660
661                 strsep(&dupline, " "); /* skip context */
662                 into = strsep(&dupline, " ");
663                 /* error if missing context or fifth word is not 'into' */
664                 if (!strlen(context) || strcmp(into, "into")) {
665                         ast_log(LOG_ERROR, "bad context %s or missing into %s\n",
666                                 context, into);
667                         goto error3;
668                 }
669
670                 if (ast_rdlock_contexts()) {
671                         ast_log(LOG_ERROR, "Failed to lock context list\n");
672                         goto error3;
673                 }
674
675                 for (c = NULL; !ret && (c = ast_walk_contexts(c)); ) {
676                         if (!strcmp(context, ast_get_context_name(c)))
677                                 continue; /* skip ourselves */
678                         if (partial_match(ast_get_context_name(c), a->word, len) &&
679                                         !lookup_ci(c, context) /* not included yet */ &&
680                                         ++which > a->n) {
681                                 ret = strdup(ast_get_context_name(c));
682                         }
683                 }
684                 ast_unlock_contexts();
685         error3:
686                 free(context);
687                 return ret;
688         }
689
690         return NULL;
691 }
692
693 /*!
694  * \brief 'save dialplan' CLI command implementation functions ...
695  */
696 static char *handle_cli_dialplan_save(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
697 {
698         char filename[256], overrideswitch[256] = "";
699         struct ast_context *c;
700         struct ast_config *cfg;
701         struct ast_variable *v;
702         int incomplete = 0; /* incomplete config write? */
703         FILE *output;
704         struct ast_flags config_flags = { 0 };
705         const char *base, *slash;
706
707         switch (cmd) {
708         case CLI_INIT:
709                 e->command = "dialplan save";
710                 e->usage =
711                         "Usage: dialplan save [/path/to/extension/file]\n"
712                         "       Save dialplan created by pbx_config module.\n"
713                         "\n"
714                         "Example: dialplan save                 (/etc/asterisk/extensions.conf)\n"
715                         "         dialplan save /home/markster  (/home/markster/extensions.conf)\n";
716                 return NULL;
717         case CLI_GENERATE:
718                 return NULL;
719         }
720
721         if (! (static_config && !write_protect_config)) {
722                 ast_cli(a->fd,
723                         "I can't save dialplan now, see '%s' example file.\n",
724                         config);
725                 return CLI_FAILURE;
726         }
727
728         if (a->argc != 2 && a->argc != 3)
729                 return CLI_SHOWUSAGE;
730
731         if (ast_mutex_lock(&save_dialplan_lock)) {
732                 ast_cli(a->fd,
733                         "Failed to lock dialplan saving (another proccess saving?)\n");
734                 return CLI_FAILURE;
735         }
736         /* XXX the code here is quite loose, a pathname with .conf in it
737          * is assumed to be a complete pathname
738          */
739         if (a->argc == 3) {     /* have config path. Look for *.conf */
740                 base = a->argv[2];
741                 if (!strstr(a->argv[2], ".conf")) { /*no, this is assumed to be a pathname */
742                         /* if filename ends with '/', do not add one */
743                         slash = (*(a->argv[2] + strlen(a->argv[2]) -1) == '/') ? "/" : "";
744                 } else {        /* yes, complete file name */
745                         slash = "";
746                 }
747         } else {
748                 /* no config file, default one */
749                 base = ast_config_AST_CONFIG_DIR;
750                 slash = "/";
751         }
752         snprintf(filename, sizeof(filename), "%s%s%s", base, slash, config);
753
754         cfg = ast_config_load("extensions.conf", config_flags);
755         if (!cfg) {
756                 ast_cli(a->fd, "Failed to load extensions.conf\n");
757                 ast_mutex_unlock(&save_dialplan_lock);
758                 return CLI_FAILURE;
759         }
760
761         /* try to lock contexts list */
762         if (ast_rdlock_contexts()) {
763                 ast_cli(a->fd, "Failed to lock contexts list\n");
764                 ast_mutex_unlock(&save_dialplan_lock);
765                 ast_config_destroy(cfg);
766                 return CLI_FAILURE;
767         }
768
769         /* create new file ... */
770         if (!(output = fopen(filename, "wt"))) {
771                 ast_cli(a->fd, "Failed to create file '%s'\n",
772                         filename);
773                 ast_unlock_contexts();
774                 ast_mutex_unlock(&save_dialplan_lock);
775                 ast_config_destroy(cfg);
776                 return CLI_FAILURE;
777         }
778
779         /* fireout general info */
780         if (overrideswitch_config) {
781                 snprintf(overrideswitch, sizeof(overrideswitch), "overrideswitch=%s\n", overrideswitch_config);
782         }
783         fprintf(output, "[general]\nstatic=%s\nwriteprotect=%s\nautofallthrough=%s\nclearglobalvars=%s\n%sextenpatternmatchnew=%s\n\n",
784                 static_config ? "yes" : "no",
785                 write_protect_config ? "yes" : "no",
786                 autofallthrough_config ? "yes" : "no",
787                                 clearglobalvars_config ? "yes" : "no",
788                                 overrideswitch_config ? overrideswitch : "",
789                                 extenpatternmatchnew_config ? "yes" : "no");
790
791         if ((v = ast_variable_browse(cfg, "globals"))) {
792                 fprintf(output, "[globals]\n");
793                 while(v) {
794                         fprintf(output, "%s => %s\n", v->name, v->value);
795                         v = v->next;
796                 }
797                 fprintf(output, "\n");
798         }
799
800         ast_config_destroy(cfg);
801         
802 #define PUT_CTX_HDR     do { \
803         if (!context_header_written) {  \
804                 fprintf(output, "[%s]\n", ast_get_context_name(c));     \
805                 context_header_written = 1;     \
806         }       \
807         } while (0)
808
809         /* walk all contexts */
810         for (c = NULL; (c = ast_walk_contexts(c)); ) {
811                 int context_header_written = 0;
812                 struct ast_exten *ext, *last_written_e = NULL;
813                 struct ast_include *i;
814                 struct ast_ignorepat *ip;
815                 struct ast_sw *sw;
816
817                 /* try to lock context and fireout all info */  
818                 if (ast_rdlock_context(c)) { /* lock failure */
819                         incomplete = 1;
820                         continue;
821                 }
822                 /* registered by this module? */
823                 /* XXX do we need this ? */
824                 if (!strcmp(ast_get_context_registrar(c), registrar)) {
825                         fprintf(output, "[%s]\n", ast_get_context_name(c));
826                         context_header_written = 1;
827                 }
828
829                 /* walk extensions ... */
830                 for (ext = NULL; (ext = ast_walk_context_extensions(c, ext)); ) {
831                         struct ast_exten *p = NULL;
832
833                         /* fireout priorities */
834                         while ( (p = ast_walk_extension_priorities(ext, p)) ) {
835                                 if (strcmp(ast_get_extension_registrar(p), registrar) != 0) /* not this source */
836                                         continue;
837                 
838                                 /* make empty line between different extensions */      
839                                 if (last_written_e != NULL &&
840                                             strcmp(ast_get_extension_name(last_written_e),
841                                                     ast_get_extension_name(p)))
842                                         fprintf(output, "\n");
843                                 last_written_e = p;
844                         
845                                 PUT_CTX_HDR;
846
847                                 if (ast_get_extension_priority(p) == PRIORITY_HINT) { /* easy */
848                                         fprintf(output, "exten => %s,hint,%s\n",
849                                                     ast_get_extension_name(p),
850                                                     ast_get_extension_app(p));
851                                 } else {
852                                         const char *sep, *cid;
853                                         const char *el = ast_get_extension_label(p);
854                                         char label[128] = "";
855  
856                                         if (ast_get_extension_matchcid(p)) {
857                                                 sep = "/";
858                                                 cid = ast_get_extension_cidmatch(p);
859                                         } else
860                                                 sep = cid = "";
861                                 
862                                         if (el && (snprintf(label, sizeof(label), "(%s)", el) != (strlen(el) + 2)))
863                                                 incomplete = 1; /* error encountered or label > 125 chars */
864                                         
865                                         fprintf(output, "exten => %s%s%s,%d%s,%s(%s)\n",
866                                             ast_get_extension_name(p), (ast_strlen_zero(sep) ? "" : sep), (ast_strlen_zero(cid) ? "" : cid),
867                                             ast_get_extension_priority(p), label,
868                                             ast_get_extension_app(p), (ast_strlen_zero(ast_get_extension_app_data(p)) ? "" : (const char *)ast_get_extension_app_data(p)));
869                                 }
870                         }
871                 }
872
873                 /* written any extensions? ok, write space between exten & inc */
874                 if (last_written_e)
875                         fprintf(output, "\n");
876
877                 /* walk through includes */
878                 for (i = NULL; (i = ast_walk_context_includes(c, i)) ; ) {
879                         if (strcmp(ast_get_include_registrar(i), registrar) != 0)
880                                 continue; /* not mine */
881                         PUT_CTX_HDR;
882                         fprintf(output, "include => %s\n", ast_get_include_name(i));
883                 }
884                 if (ast_walk_context_includes(c, NULL))
885                         fprintf(output, "\n");
886
887                 /* walk through switches */
888                 for (sw = NULL; (sw = ast_walk_context_switches(c, sw)) ; ) {
889                         if (strcmp(ast_get_switch_registrar(sw), registrar) != 0)
890                                 continue; /* not mine */
891                         PUT_CTX_HDR;
892                         fprintf(output, "switch => %s/%s\n",
893                                     ast_get_switch_name(sw), ast_get_switch_data(sw));
894                 }
895
896                 if (ast_walk_context_switches(c, NULL))
897                         fprintf(output, "\n");
898
899                 /* fireout ignorepats ... */
900                 for (ip = NULL; (ip = ast_walk_context_ignorepats(c, ip)); ) {
901                         if (strcmp(ast_get_ignorepat_registrar(ip), registrar) != 0)
902                                 continue; /* not mine */
903                         PUT_CTX_HDR;
904                         fprintf(output, "ignorepat => %s\n",
905                                                 ast_get_ignorepat_name(ip));
906                 }
907
908                 ast_unlock_context(c);
909         }       
910
911         ast_unlock_contexts();
912         ast_mutex_unlock(&save_dialplan_lock);
913         fclose(output);
914
915         if (incomplete) {
916                 ast_cli(a->fd, "Saved dialplan is incomplete\n");
917                 return CLI_FAILURE;
918         }
919
920         ast_cli(a->fd, "Dialplan successfully saved into '%s'\n",
921                 filename);
922         return CLI_SUCCESS;
923 }
924
925 /*!
926  * \brief ADD EXTENSION command stuff
927  */
928 static char *handle_cli_dialplan_add_extension(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
929 {
930         char *whole_exten;
931         char *exten, *prior;
932         int iprior = -2;
933         char *cidmatch, *app, *app_data;
934         char *start, *end;
935         const char *into_context;
936
937         switch (cmd) {
938         case CLI_INIT:
939                 e->command = "dialplan add extension";
940                 e->usage =
941                         "Usage: dialplan add extension <exten>,<priority>,<app> into <context> [replace]\n"
942                         "\n"
943                         "       app can be either:\n"
944                         "         app-name\n"
945                         "         app-name(app-data)\n"
946                         "         app-name,<app-data>\n"
947                         "\n"
948                         "       This command will add the new extension into <context>.  If\n"
949                         "       an extension with the same priority already exists and the\n"
950                         "       'replace' option is given we will replace the extension.\n"
951                         "\n"
952                         "Example: dialplan add extension 6123,1,Dial,IAX/216.207.245.56/6123 into local\n"
953                         "         Now, you can dial 6123 and talk to Markster :)\n";
954                 return NULL;
955         case CLI_GENERATE:
956                 return complete_dialplan_add_extension(a);
957         }
958
959         /* check for arguments at first */
960         if (a->argc != 6 && a->argc != 7)
961                 return CLI_SHOWUSAGE;
962         if (strcmp(a->argv[4], "into"))
963                 return CLI_SHOWUSAGE;
964         if (a->argc == 7)
965                 if (strcmp(a->argv[6], "replace"))
966                         return CLI_SHOWUSAGE;
967
968         whole_exten = ast_strdupa(a->argv[3]);
969         exten = strsep(&whole_exten,",");
970         if (strchr(exten, '/')) {
971                 cidmatch = exten;
972                 strsep(&cidmatch,"/");
973         } else {
974                 cidmatch = NULL;
975         }
976         prior = strsep(&whole_exten,",");
977         if (prior) {
978                 if (!strcmp(prior, "hint")) {
979                         iprior = PRIORITY_HINT;
980                 } else {
981                         if (sscanf(prior, "%30d", &iprior) != 1) {
982                                 ast_cli(a->fd, "'%s' is not a valid priority\n", prior);
983                                 prior = NULL;
984                         }
985                 }
986         }
987         app = whole_exten;
988         if (app) {
989                 if ((start = strchr(app, '(')) && (end = strrchr(app, ')'))) {
990                         *start = *end = '\0';
991                         app_data = start + 1;
992                 } else {
993                         app_data = strchr(app, ',');
994                         if (app_data) {
995                                 *app_data++ = '\0';
996                         }
997                 }
998         } else {
999                 app_data = NULL;
1000         }
1001
1002         if (!exten || !prior || !app) {
1003                 return CLI_SHOWUSAGE;
1004         }
1005
1006         if (!app_data) {
1007                 app_data = "";
1008         }
1009         into_context = a->argv[5];
1010
1011         if (!ast_context_find(into_context)) {
1012                 ast_cli(a->fd, "Context '%s' did not exist prior to add extension - the context will be created.\n", into_context);
1013         }
1014
1015         if (!ast_context_find_or_create(NULL, NULL, into_context, registrar)) {
1016                 ast_cli(a->fd, "Failed to add '%s,%s,%s(%s)' extension into '%s' context\n",
1017                         exten, prior, app, app_data, into_context);
1018                 return CLI_FAILURE;
1019         }
1020
1021         if (ast_add_extension(into_context, a->argc == 7 ? 1 : 0, exten, iprior, NULL, cidmatch, app,
1022                 ast_strdup(app_data), ast_free_ptr, registrar)) {
1023                 switch (errno) {
1024                 case ENOMEM:
1025                         ast_cli(a->fd, "Out of free memory\n");
1026                         break;
1027
1028                 case EBUSY:
1029                         ast_cli(a->fd, "Failed to lock context(s) list, please try again later\n");
1030                         break;
1031
1032                 case ENOENT:
1033                         ast_cli(a->fd, "No existence of '%s' context\n", into_context);
1034                         break;
1035
1036                 case EEXIST:
1037                         ast_cli(a->fd, "Extension %s@%s with priority %s already exists\n",
1038                                 exten, into_context, prior);
1039                         break;
1040
1041                 default:
1042                         ast_cli(a->fd, "Failed to add '%s,%s,%s(%s)' extension into '%s' context\n",
1043                                         exten, prior, app, app_data, into_context);
1044                         break;
1045                 }
1046                 return CLI_FAILURE;
1047         }
1048
1049         if (a->argc == 7) {
1050                 ast_cli(a->fd, "Extension %s@%s (%s) replace by '%s,%s,%s(%s)'\n",
1051                         exten, into_context, prior, exten, prior, app, app_data);
1052         } else {
1053                 ast_cli(a->fd, "Extension '%s,%s,%s(%s)' added into '%s' context\n",
1054                         exten, prior, app, app_data, into_context);
1055         }
1056
1057         return CLI_SUCCESS;
1058 }
1059
1060 static char *complete_dialplan_remove_context(struct ast_cli_args *a)
1061 {
1062         struct ast_context *c = NULL;
1063         int len = strlen(a->word);
1064         char *res = NULL;
1065         int which = 0;
1066
1067         if (a->pos != 3) {
1068                 return NULL;
1069         }
1070
1071
1072         /* try to lock contexts list ... */
1073         if (ast_rdlock_contexts()) {
1074                 ast_log(LOG_WARNING, "Failed to lock contexts list\n");
1075                 return NULL;
1076         }
1077
1078         /* walk through all contexts */
1079         while ( !res && (c = ast_walk_contexts(c)) ) {
1080                 if (partial_match(ast_get_context_name(c), a->word, len) && ++which > a->n) {
1081                         res = strdup(ast_get_context_name(c));
1082                 }
1083         }
1084         ast_unlock_contexts();
1085         return res;
1086 }
1087
1088 /*! dialplan add extension 6123,1,Dial,IAX/212.71.138.13/6123 into local */
1089 static char *complete_dialplan_add_extension(struct ast_cli_args *a)
1090 {
1091         int which = 0;
1092
1093         if (a->pos == 4) {              /* complete 'into' word ... */
1094                 return (a->n == 0) ? strdup("into") : NULL;
1095         } else if (a->pos == 5) { /* complete context */
1096                 struct ast_context *c = NULL;
1097                 int len = strlen(a->word);
1098                 char *res = NULL;
1099
1100                 /* try to lock contexts list ... */
1101                 if (ast_rdlock_contexts()) {
1102                         ast_log(LOG_WARNING, "Failed to lock contexts list\n");
1103                         return NULL;
1104                 }
1105
1106                 /* walk through all contexts */
1107                 while ( !res && (c = ast_walk_contexts(c)) )
1108                         if (partial_match(ast_get_context_name(c), a->word, len) && ++which > a->n)
1109                                 res = strdup(ast_get_context_name(c));
1110                 ast_unlock_contexts();
1111                 return res;
1112         } else if (a->pos == 6) {
1113                 return a->n == 0 ? strdup("replace") : NULL;
1114         }
1115         return NULL;
1116 }
1117
1118 /*!
1119  * IGNOREPAT CLI stuff
1120  */
1121 static char *handle_cli_dialplan_add_ignorepat(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1122 {
1123         switch (cmd) {
1124         case CLI_INIT:
1125                 e->command = "dialplan add ignorepat";
1126                 e->usage =
1127                         "Usage: dialplan add ignorepat <pattern> into <context>\n"
1128                         "       This command adds a new ignore pattern into context <context>\n"
1129                         "\n"
1130                         "Example: dialplan add ignorepat _3XX into local\n";
1131                 return NULL;
1132         case CLI_GENERATE:
1133                 return complete_dialplan_add_ignorepat(a);
1134         }
1135
1136         if (a->argc != 6)
1137                 return CLI_SHOWUSAGE;
1138
1139         if (strcmp(a->argv[4], "into"))
1140                 return CLI_SHOWUSAGE;
1141
1142         if (ast_context_add_ignorepat(a->argv[5], a->argv[3], registrar)) {
1143                 switch (errno) {
1144                 case ENOMEM:
1145                         ast_cli(a->fd, "Out of free memory\n");
1146                         break;
1147
1148                 case ENOENT:
1149                         ast_cli(a->fd, "There is no existence of '%s' context\n", a->argv[5]);
1150                         break;
1151
1152                 case EEXIST:
1153                         ast_cli(a->fd, "Ignore pattern '%s' already included in '%s' context\n",
1154                                 a->argv[3], a->argv[5]);
1155                         break;
1156
1157                 case EBUSY:
1158                         ast_cli(a->fd, "Failed to lock context(s) list, please, try again later\n");
1159                         break;
1160
1161                 default:
1162                         ast_cli(a->fd, "Failed to add ingore pattern '%s' into '%s' context\n",
1163                                 a->argv[3], a->argv[5]);
1164                         break;
1165                 }
1166                 return CLI_FAILURE;
1167         }
1168
1169         ast_cli(a->fd, "Ignore pattern '%s' added into '%s' context\n",
1170                 a->argv[3], a->argv[5]);
1171
1172         return CLI_SUCCESS;
1173 }
1174
1175 static char *complete_dialplan_add_ignorepat(struct ast_cli_args *a)
1176 {
1177         if (a->pos == 4)
1178                 return a->n == 0 ? strdup("into") : NULL;
1179         else if (a->pos == 5) {
1180                 struct ast_context *c;
1181                 int which = 0;
1182                 char *dupline, *ignorepat = NULL;
1183                 const char *s;
1184                 char *ret = NULL;
1185                 int len = strlen(a->word);
1186
1187                 /* XXX skip first three words 'dialplan' 'add' 'ignorepat' */
1188                 s = skip_words(a->line, 3);
1189                 if (s == NULL)
1190                         return NULL;
1191                 dupline = strdup(s);
1192                 if (!dupline) {
1193                         ast_log(LOG_ERROR, "Malloc failure\n");
1194                         return NULL;
1195                 }
1196                 ignorepat = strsep(&dupline, " ");
1197
1198                 if (ast_rdlock_contexts()) {
1199                         ast_log(LOG_ERROR, "Failed to lock contexts list\n");
1200                         return NULL;
1201                 }
1202
1203                 for (c = NULL; !ret && (c = ast_walk_contexts(c));) {
1204                         int found = 0;
1205
1206                         if (!partial_match(ast_get_context_name(c), a->word, len))
1207                                 continue; /* not mine */
1208                         if (ignorepat) /* there must be one, right ? */
1209                                 found = lookup_c_ip(c, ignorepat);
1210                         if (!found && ++which > a->n)
1211                                 ret = strdup(ast_get_context_name(c));
1212                 }
1213
1214                 free(ignorepat);
1215                 ast_unlock_contexts();
1216                 return ret;
1217         }
1218
1219         return NULL;
1220 }
1221
1222 static char *handle_cli_dialplan_remove_ignorepat(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1223 {
1224         switch (cmd) {
1225         case CLI_INIT:
1226                 e->command = "dialplan remove ignorepat";
1227                 e->usage =
1228                         "Usage: dialplan remove ignorepat <pattern> from <context>\n"
1229                         "       This command removes an ignore pattern from context <context>\n"
1230                         "\n"
1231                         "Example: dialplan remove ignorepat _3XX from local\n";
1232                 return NULL;
1233         case CLI_GENERATE:
1234                 return complete_dialplan_remove_ignorepat(a);
1235         }
1236
1237         if (a->argc != 6)
1238                 return CLI_SHOWUSAGE;
1239
1240         if (strcmp(a->argv[4], "from"))
1241                 return CLI_SHOWUSAGE;
1242
1243         if (ast_context_remove_ignorepat(a->argv[5], a->argv[3], registrar)) {
1244                 switch (errno) {
1245                 case EBUSY:
1246                         ast_cli(a->fd, "Failed to lock context(s) list, please try again later\n");
1247                         break;
1248
1249                 case ENOENT:
1250                         ast_cli(a->fd, "There is no existence of '%s' context\n", a->argv[5]);
1251                         break;
1252
1253                 case EINVAL:
1254                         ast_cli(a->fd, "There is no existence of '%s' ignore pattern in '%s' context\n",
1255                                         a->argv[3], a->argv[5]);
1256                         break;
1257
1258                 default:
1259                         ast_cli(a->fd, "Failed to remove ignore pattern '%s' from '%s' context\n",
1260                                         a->argv[3], a->argv[5]);
1261                         break;
1262                 }
1263                 return CLI_FAILURE;
1264         }
1265
1266         ast_cli(a->fd, "Ignore pattern '%s' removed from '%s' context\n",
1267                 a->argv[3], a->argv[5]);
1268         return CLI_SUCCESS;
1269 }
1270
1271 static char *complete_dialplan_remove_ignorepat(struct ast_cli_args *a)
1272 {
1273         struct ast_context *c;
1274         int which = 0;
1275         char *ret = NULL;
1276
1277         if (a->pos == 3) {
1278                 int len = strlen(a->word);
1279                 if (ast_rdlock_contexts()) {
1280                         ast_log(LOG_WARNING, "Failed to lock contexts list\n");
1281                         return NULL;
1282                 }
1283
1284                 for (c = NULL; !ret && (c = ast_walk_contexts(c));) {
1285                         struct ast_ignorepat *ip;
1286
1287                         if (ast_rdlock_context(c))      /* error, skip it */
1288                                 continue;
1289                         
1290                         for (ip = NULL; !ret && (ip = ast_walk_context_ignorepats(c, ip));) {
1291                                 if (partial_match(ast_get_ignorepat_name(ip), a->word, len) && ++which > a->n) {
1292                                         /* n-th match */
1293                                         struct ast_context *cw = NULL;
1294                                         int found = 0;
1295                                         while ( (cw = ast_walk_contexts(cw)) && cw != c && !found) {
1296                                                 /* XXX do i stop on c, or skip it ? */
1297                                                 found = lookup_c_ip(cw, ast_get_ignorepat_name(ip));
1298                                         }
1299                                         if (!found)
1300                                                 ret = strdup(ast_get_ignorepat_name(ip));
1301                                 }
1302                         }
1303                         ast_unlock_context(c);
1304                 }
1305                 ast_unlock_contexts();
1306                 return ret;
1307         } else if (a->pos == 4) {
1308                  return a->n == 0 ? strdup("from") : NULL;
1309         } else if (a->pos == 5) { /* XXX check this */
1310                 char *dupline, *duplinet, *ignorepat;
1311                 int len = strlen(a->word);
1312
1313                 dupline = strdup(a->line);
1314                 if (!dupline) {
1315                         ast_log(LOG_WARNING, "Out of free memory\n");
1316                         return NULL;
1317                 }
1318
1319                 duplinet = dupline;
1320                 strsep(&duplinet, " ");
1321                 strsep(&duplinet, " ");
1322                 ignorepat = strsep(&duplinet, " ");
1323
1324                 if (!ignorepat) {
1325                         free(dupline);
1326                         return NULL;
1327                 }
1328
1329                 if (ast_rdlock_contexts()) {
1330                         ast_log(LOG_WARNING, "Failed to lock contexts list\n");
1331                         free(dupline);
1332                         return NULL;
1333                 }
1334
1335                 for (c = NULL; !ret && (c = ast_walk_contexts(c)); ) {
1336                         if (ast_rdlock_context(c))      /* fail, skip it */
1337                                 continue;
1338                         if (!partial_match(ast_get_context_name(c), a->word, len))
1339                                 continue;
1340                         if (lookup_c_ip(c, ignorepat) && ++which > a->n)
1341                                 ret = strdup(ast_get_context_name(c));
1342                         ast_unlock_context(c);
1343                 }
1344                 ast_unlock_contexts();
1345                 free(dupline);
1346                 return NULL;
1347         }
1348
1349         return NULL;
1350 }
1351
1352 static int pbx_load_module(void);
1353
1354 static char *handle_cli_dialplan_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1355 {
1356         switch (cmd) {
1357         case CLI_INIT:
1358                 e->command = "dialplan reload";
1359                 e->usage =
1360                         "Usage: dialplan reload\n"
1361                         "       Reload extensions.conf without reloading any other\n"
1362                         "       modules.  This command does not delete global variables\n"
1363                         "       unless clearglobalvars is set to yes in extensions.conf\n";
1364                 return NULL;
1365         case CLI_GENERATE:
1366                 return NULL;
1367         }
1368
1369         if (a->argc != 2)
1370                 return CLI_SHOWUSAGE;
1371
1372         if (clearglobalvars_config)
1373                 pbx_builtin_clear_globals();
1374
1375         pbx_load_module();
1376         ast_cli(a->fd, "Dialplan reloaded.\n");
1377         return CLI_SUCCESS;
1378 }
1379
1380 /*!
1381  * CLI entries for commands provided by this module
1382  */
1383 static struct ast_cli_entry cli_pbx_config[] = {
1384         AST_CLI_DEFINE(handle_cli_dialplan_add_extension,    "Add new extension into context"),
1385         AST_CLI_DEFINE(handle_cli_dialplan_remove_extension, "Remove a specified extension"),
1386         AST_CLI_DEFINE(handle_cli_dialplan_remove_context,   "Remove a specified context"),
1387         AST_CLI_DEFINE(handle_cli_dialplan_add_ignorepat,    "Add new ignore pattern"),
1388         AST_CLI_DEFINE(handle_cli_dialplan_remove_ignorepat, "Remove ignore pattern from context"),
1389         AST_CLI_DEFINE(handle_cli_dialplan_add_include,      "Include context in other context"),
1390         AST_CLI_DEFINE(handle_cli_dialplan_remove_include,   "Remove a specified include from context"),
1391         AST_CLI_DEFINE(handle_cli_dialplan_reload,           "Reload extensions and *only* extensions"),
1392         AST_CLI_DEFINE(handle_cli_dialplan_save,             "Save current dialplan into a file")
1393 };
1394
1395 static struct ast_cli_entry cli_dialplan_save =
1396         AST_CLI_DEFINE(handle_cli_dialplan_save, "Save dialplan");
1397
1398 /*!
1399  * Standard module functions ...
1400  */
1401 static int unload_module(void)
1402 {
1403         if (static_config && !write_protect_config)
1404                 ast_cli_unregister(&cli_dialplan_save);
1405         if (overrideswitch_config) {
1406                 ast_free(overrideswitch_config);
1407         }
1408         ast_cli_unregister_multiple(cli_pbx_config, ARRAY_LEN(cli_pbx_config));
1409         ast_context_destroy(NULL, registrar);
1410         return 0;
1411 }
1412
1413 /*!\note Protect against misparsing based upon commas in the middle of fields
1414  * like character classes.  We've taken steps to permit pretty much every other
1415  * printable character in a character class, so properly handling a comma at
1416  * this level is a natural extension.  This is almost like the standard
1417  * application parser in app.c, except that it handles square brackets. */
1418 static char *pbx_strsep(char **destructible, const char *delim)
1419 {
1420         int square = 0;
1421         char *res;
1422
1423         if (!destructible || !*destructible) {
1424                 return NULL;
1425         }
1426         res = *destructible;
1427         for (; **destructible; (*destructible)++) {
1428                 if (**destructible == '[' && !strchr(delim, '[')) {
1429                         square++;
1430                 } else if (**destructible == ']' && !strchr(delim, ']')) {
1431                         if (square) {
1432                                 square--;
1433                         }
1434                 } else if (**destructible == '\\' && !strchr(delim, '\\')) {
1435                         (*destructible)++;
1436                 } else if (strchr(delim, **destructible) && !square) {
1437                         **destructible = '\0';
1438                         (*destructible)++;
1439                         break;
1440                 }
1441         }
1442         if (**destructible == '\0') {
1443                 *destructible = NULL;
1444         }
1445         return res;
1446 }
1447
1448 static int pbx_load_config(const char *config_file)
1449 {
1450         struct ast_config *cfg;
1451         char *end;
1452         char *label;
1453 #ifdef LOW_MEMORY
1454         char realvalue[256];
1455 #else
1456         char realvalue[8192];
1457 #endif
1458         int lastpri = -2;
1459         struct ast_context *con;
1460         struct ast_variable *v;
1461         const char *cxt;
1462         const char *aft;
1463         const char *newpm, *ovsw;
1464         struct ast_flags config_flags = { 0 };
1465         char lastextension[256];
1466         cfg = ast_config_load(config_file, config_flags);
1467         if (!cfg || cfg == CONFIG_STATUS_FILEINVALID)
1468                 return 0;
1469
1470         /* Use existing config to populate the PBX table */
1471         static_config = ast_true(ast_variable_retrieve(cfg, "general", "static"));
1472         write_protect_config = ast_true(ast_variable_retrieve(cfg, "general", "writeprotect"));
1473         if ((aft = ast_variable_retrieve(cfg, "general", "autofallthrough")))
1474                 autofallthrough_config = ast_true(aft);
1475         if ((newpm = ast_variable_retrieve(cfg, "general", "extenpatternmatchnew")))
1476                 extenpatternmatchnew_config = ast_true(newpm);
1477         clearglobalvars_config = ast_true(ast_variable_retrieve(cfg, "general", "clearglobalvars"));
1478         if ((ovsw = ast_variable_retrieve(cfg, "general", "overrideswitch"))) {
1479                 if (overrideswitch_config) {
1480                         ast_free(overrideswitch_config);
1481                 }
1482                 if (!ast_strlen_zero(ovsw)) {
1483                         overrideswitch_config = ast_strdup(ovsw);
1484                 } else {
1485                         overrideswitch_config = NULL;
1486                 }
1487         }
1488
1489         ast_copy_string(userscontext, ast_variable_retrieve(cfg, "general", "userscontext") ?: "default", sizeof(userscontext));
1490                                                                     
1491         for (v = ast_variable_browse(cfg, "globals"); v; v = v->next) {
1492                 pbx_substitute_variables_helper(NULL, v->value, realvalue, sizeof(realvalue) - 1);
1493                 pbx_builtin_setvar_helper(NULL, v->name, realvalue);
1494         }
1495         for (cxt = ast_category_browse(cfg, NULL);
1496              cxt;
1497              cxt = ast_category_browse(cfg, cxt)) {
1498                 /* All categories but "general" or "globals" are considered contexts */
1499                 if (!strcasecmp(cxt, "general") || !strcasecmp(cxt, "globals")) {
1500                         continue;
1501                 }
1502                 if (!(con = ast_context_find_or_create(&local_contexts, local_table, cxt, registrar))) {
1503                         continue;
1504                 }
1505
1506                 /* Reset continuation items at the beginning of each context */
1507                 lastextension[0] = '\0';
1508                 lastpri = -2;
1509
1510                 for (v = ast_variable_browse(cfg, cxt); v; v = v->next) {
1511                         char *tc = NULL;
1512                         char realext[256] = "";
1513                         char *stringp, *ext;
1514                         const char *vfile;
1515
1516                         /* get filename for error reporting from top level or an #include */
1517                         vfile = !*v->file ? config_file : v->file;
1518
1519                         if (!strncasecmp(v->name, "same", 4)) {
1520                                 if (ast_strlen_zero(lastextension)) {
1521                                         ast_log(LOG_ERROR,
1522                                                 "No previous pattern in the first entry of context '%s' to match '%s' at line %d of %s!\n",
1523                                                 cxt, v->name, v->lineno, vfile);
1524                                         continue;
1525                                 }
1526                                 if ((stringp = tc = ast_strdup(v->value))) {
1527                                         ast_copy_string(realext, lastextension, sizeof(realext));
1528                                         goto process_extension;
1529                                 }
1530                         } else if (!strcasecmp(v->name, "exten")) {
1531                                 int ipri;
1532                                 char *plus;
1533                                 char *pri, *appl, *data, *cidmatch;
1534
1535                                 if (!(stringp = tc = ast_strdup(v->value))) {
1536                                         continue;
1537                                 }
1538
1539                                 ext = S_OR(pbx_strsep(&stringp, ","), "");
1540                                 pbx_substitute_variables_helper(NULL, ext, realext, sizeof(realext) - 1);
1541                                 ast_copy_string(lastextension, realext, sizeof(lastextension));
1542 process_extension:
1543                                 ipri = -2;
1544                                 if ((cidmatch = strchr(realext, '/'))) {
1545                                         *cidmatch++ = '\0';
1546                                         ast_shrink_phone_number(cidmatch);
1547                                 }
1548                                 pri = ast_strip(S_OR(strsep(&stringp, ","), ""));
1549                                 if ((label = strchr(pri, '('))) {
1550                                         *label++ = '\0';
1551                                         if ((end = strchr(label, ')'))) {
1552                                                 *end = '\0';
1553                                         } else {
1554                                                 ast_log(LOG_WARNING,
1555                                                         "Label missing trailing ')' at line %d of %s\n",
1556                                                         v->lineno, vfile);
1557                                                 ast_free(tc);
1558                                                 continue;
1559                                         }
1560                                 }
1561                                 if ((plus = strchr(pri, '+'))) {
1562                                         *plus++ = '\0';
1563                                 }
1564                                 if (!strcmp(pri,"hint")) {
1565                                         ipri = PRIORITY_HINT;
1566                                 } else if (!strcmp(pri, "next") || !strcmp(pri, "n")) {
1567                                         if (lastpri > -2) {
1568                                                 ipri = lastpri + 1;
1569                                         } else {
1570                                                 ast_log(LOG_WARNING,
1571                                                         "Can't use 'next' priority on the first entry at line %d of %s!\n",
1572                                                         v->lineno, vfile);
1573                                                 ast_free(tc);
1574                                                 continue;
1575                                         }
1576                                 } else if (!strcmp(pri, "same") || !strcmp(pri, "s")) {
1577                                         if (lastpri > -2) {
1578                                                 ipri = lastpri;
1579                                         } else {
1580                                                 ast_log(LOG_WARNING,
1581                                                         "Can't use 'same' priority on the first entry at line %d of %s!\n",
1582                                                         v->lineno, vfile);
1583                                                 ast_free(tc);
1584                                                 continue;
1585                                         }
1586                                 } else if (sscanf(pri, "%30d", &ipri) != 1 &&
1587                                            (ipri = ast_findlabel_extension2(NULL, con, realext, pri, cidmatch)) < 1) {
1588                                         ast_log(LOG_WARNING,
1589                                                 "Invalid priority/label '%s' at line %d of %s\n",
1590                                                 pri, v->lineno, vfile);
1591                                         ipri = 0;
1592                                         ast_free(tc);
1593                                         continue;
1594                                 } else if (ipri < 1) {
1595                                         ast_log(LOG_WARNING, "Invalid priority '%s' at line %d of %s\n",
1596                                                 pri, v->lineno, vfile);
1597                                         ast_free(tc);
1598                                         continue;
1599                                 }
1600                                 appl = S_OR(stringp, "");
1601                                 /* Find the first occurrence of '(' */
1602                                 if (!strchr(appl, '(')) {
1603                                         /* No arguments */
1604                                         data = "";
1605                                 } else {
1606                                         char *orig_appl = ast_strdup(appl);
1607
1608                                         if (!orig_appl) {
1609                                                 ast_free(tc);
1610                                                 continue;
1611                                         }
1612
1613                                         appl = strsep(&stringp, "(");
1614
1615                                         /* check if there are variables or expressions without an application, like: exten => 100,hint,DAHDI/g0/${GLOBAL(var)}  */
1616                                         if (strstr(appl, "${") || strstr(appl, "$[")){
1617                                                 /* set appl to original one */
1618                                                 strcpy(appl, orig_appl);
1619                                                 /* set no data */
1620                                                 data = "";
1621                                         /* no variable before application found -> go ahead */
1622                                         } else {
1623                                                 data = S_OR(stringp, "");
1624                                                 if ((end = strrchr(data, ')'))) {
1625                                                         *end = '\0';
1626                                                 } else {
1627                                                         ast_log(LOG_WARNING,
1628                                                                 "No closing parenthesis found? '%s(%s' at line %d of %s\n",
1629                                                                 appl, data, v->lineno, vfile);
1630                                                 }
1631                                         }
1632                                         ast_free(orig_appl);
1633                                 }
1634
1635                                 appl = ast_skip_blanks(appl);
1636                                 if (ipri) {
1637                                         if (plus) {
1638                                                 ipri += atoi(plus);
1639                                         }
1640                                         lastpri = ipri;
1641                                         if (!ast_opt_dont_warn && (!strcmp(realext, "_.") || !strcmp(realext, "_!"))) {
1642                                                 ast_log(LOG_WARNING,
1643                                                         "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",
1644                                                         realext, realext[1], v->lineno, vfile);
1645                                         }
1646                                         if (ast_add_extension2(con, 0, realext, ipri, label, cidmatch, appl, ast_strdup(data), ast_free_ptr, registrar)) {
1647                                                 ast_log(LOG_WARNING,
1648                                                         "Unable to register extension at line %d of %s\n",
1649                                                         v->lineno, vfile);
1650                                         }
1651                                 }
1652                                 ast_free(tc);
1653                         } else if (!strcasecmp(v->name, "include")) {
1654                                 pbx_substitute_variables_helper(NULL, v->value, realvalue, sizeof(realvalue) - 1);
1655                                 if (ast_context_add_include2(con, realvalue, registrar)) {
1656                                         switch (errno) {
1657                                         case ENOMEM:
1658                                                 ast_log(LOG_WARNING, "Out of memory for context addition\n");
1659                                                 break;
1660
1661                                         case EBUSY:
1662                                                 ast_log(LOG_WARNING, "Failed to lock context(s) list, please try again later\n");
1663                                                 break;
1664
1665                                         case EEXIST:
1666                                                 ast_log(LOG_WARNING,
1667                                                         "Context '%s' already included in '%s' context on include at line %d of %s\n",
1668                                                         v->value, cxt, v->lineno, vfile);
1669                                                 break;
1670
1671                                         case ENOENT:
1672                                         case EINVAL:
1673                                                 ast_log(LOG_WARNING,
1674                                                         "There is no existence of context '%s' included at line %d of %s\n",
1675                                                         errno == ENOENT ? v->value : cxt, v->lineno, vfile);
1676                                                 break;
1677
1678                                         default:
1679                                                 ast_log(LOG_WARNING,
1680                                                         "Failed to include '%s' in '%s' context at line %d of %s\n",
1681                                                         v->value, cxt, v->lineno, vfile);
1682                                                 break;
1683                                         }
1684                                 }
1685                         } else if (!strcasecmp(v->name, "ignorepat")) {
1686                                 pbx_substitute_variables_helper(NULL, v->value, realvalue, sizeof(realvalue) - 1);
1687                                 if (ast_context_add_ignorepat2(con, realvalue, registrar)) {
1688                                         ast_log(LOG_WARNING,
1689                                                 "Unable to include ignorepat '%s' in context '%s' at line %d of %s\n",
1690                                                 v->value, cxt, v->lineno, vfile);
1691                                 }
1692                         } else if (!strcasecmp(v->name, "switch") || !strcasecmp(v->name, "lswitch") || !strcasecmp(v->name, "eswitch")) {
1693                                 char *appl, *data;
1694                                 stringp = realvalue;
1695                                 
1696                                 if (!strcasecmp(v->name, "switch")) {
1697                                         pbx_substitute_variables_helper(NULL, v->value, realvalue, sizeof(realvalue) - 1);
1698                                 } else {
1699                                         ast_copy_string(realvalue, v->value, sizeof(realvalue));
1700                                 }
1701                                 appl = strsep(&stringp, "/");
1702                                 data = S_OR(stringp, "");
1703                                 if (ast_context_add_switch2(con, appl, data, !strcasecmp(v->name, "eswitch"), registrar)) {
1704                                         ast_log(LOG_WARNING,
1705                                                 "Unable to include switch '%s' in context '%s' at line %d of %s\n",
1706                                                 v->value, cxt, v->lineno, vfile);
1707                                 }
1708                         } else {
1709                                 ast_log(LOG_WARNING,
1710                                         "==!!== Unknown directive: %s at line %d of %s -- IGNORING!!!\n",
1711                                         v->name, v->lineno, vfile);
1712                         }
1713                 }
1714         }
1715         ast_config_destroy(cfg);
1716         return 1;
1717 }
1718
1719 static void append_interface(char *iface, int maxlen, char *add)
1720 {
1721         int len = strlen(iface);
1722         if (strlen(add) + len < maxlen - 2) {
1723                 if (strlen(iface)) {
1724                         iface[len] = '&';
1725                         strcpy(iface + len + 1, add);
1726                 } else
1727                         strcpy(iface, add);
1728         }
1729 }
1730
1731 static void pbx_load_users(void)
1732 {
1733         struct ast_config *cfg;
1734         char *cat, *chan;
1735         const char *dahdichan;
1736         const char *hasexten, *altexts;
1737         char tmp[256];
1738         char iface[256];
1739         char dahdicopy[256];
1740         char *ext, altcopy[256];
1741         char *c;
1742         int hasvoicemail;
1743         int start, finish, x;
1744         struct ast_context *con = NULL;
1745         struct ast_flags config_flags = { 0 };
1746         
1747         cfg = ast_config_load("users.conf", config_flags);
1748         if (!cfg)
1749                 return;
1750
1751         for (cat = ast_category_browse(cfg, NULL); cat ; cat = ast_category_browse(cfg, cat)) {
1752                 if (!strcasecmp(cat, "general"))
1753                         continue;
1754                 iface[0] = '\0';
1755                 if (ast_true(ast_config_option(cfg, cat, "hassip"))) {
1756                         snprintf(tmp, sizeof(tmp), "SIP/%s", cat);
1757                         append_interface(iface, sizeof(iface), tmp);
1758                 }
1759                 if (ast_true(ast_config_option(cfg, cat, "hasiax"))) {
1760                         snprintf(tmp, sizeof(tmp), "IAX2/%s", cat);
1761                         append_interface(iface, sizeof(iface), tmp);
1762                 }
1763                 if (ast_true(ast_config_option(cfg, cat, "hash323"))) {
1764                         snprintf(tmp, sizeof(tmp), "H323/%s", cat);
1765                         append_interface(iface, sizeof(iface), tmp);
1766                 }
1767                 hasexten = ast_config_option(cfg, cat, "hasexten");
1768                 if (hasexten && !ast_true(hasexten))
1769                         continue;
1770                 hasvoicemail = ast_true(ast_config_option(cfg, cat, "hasvoicemail"));
1771                 dahdichan = ast_variable_retrieve(cfg, cat, "dahdichan");
1772                 if (!dahdichan)
1773                         dahdichan = ast_variable_retrieve(cfg, "general", "dahdichan");
1774                 if (!ast_strlen_zero(dahdichan)) {
1775                         ast_copy_string(dahdicopy, dahdichan, sizeof(dahdicopy));
1776                         c = dahdicopy;
1777                         chan = strsep(&c, ",");
1778                         while (chan) {
1779                                 if (sscanf(chan, "%30d-%30d", &start, &finish) == 2) {
1780                                         /* Range */
1781                                 } else if (sscanf(chan, "%30d", &start)) {
1782                                         /* Just one */
1783                                         finish = start;
1784                                 } else {
1785                                         start = 0; finish = 0;
1786                                 }
1787                                 if (finish < start) {
1788                                         x = finish;
1789                                         finish = start;
1790                                         start = x;
1791                                 }
1792                                 for (x = start; x <= finish; x++) {
1793                                         snprintf(tmp, sizeof(tmp), "DAHDI/%d", x);
1794                                         append_interface(iface, sizeof(iface), tmp);
1795                                 }
1796                                 chan = strsep(&c, ",");
1797                         }
1798                 }
1799                 if (!ast_strlen_zero(iface)) {
1800                         /* Only create a context here when it is really needed. Otherwise default empty context
1801                         created by pbx_config may conflict with the one explicitly created by pbx_ael */
1802                         if (!con)
1803                                 con = ast_context_find_or_create(&local_contexts, local_table, userscontext, registrar);
1804
1805                         if (!con) {
1806                                 ast_log(LOG_ERROR, "Can't find/create user context '%s'\n", userscontext);
1807                                 return;
1808                         }
1809
1810                         /* Add hint */
1811                         ast_add_extension2(con, 0, cat, -1, NULL, NULL, iface, NULL, NULL, registrar);
1812                         /* If voicemail, use "stdexten" else use plain old dial */
1813                         if (hasvoicemail) {
1814                                 if (ast_opt_stdexten_macro) {
1815                                         /* Use legacy stdexten macro method. */
1816                                         snprintf(tmp, sizeof(tmp), "stdexten,%s,${HINT}", cat);
1817                                         ast_add_extension2(con, 0, cat, 1, NULL, NULL, "Macro", ast_strdup(tmp), ast_free_ptr, registrar);
1818                                 } else {
1819                                         snprintf(tmp, sizeof(tmp), "%s,stdexten(${HINT})", cat);
1820                                         ast_add_extension2(con, 0, cat, 1, NULL, NULL, "Gosub", ast_strdup(tmp), ast_free_ptr, registrar);
1821                                 }
1822                         } else {
1823                                 ast_add_extension2(con, 0, cat, 1, NULL, NULL, "Dial", ast_strdup("${HINT}"), ast_free_ptr, registrar);
1824                         }
1825                         altexts = ast_variable_retrieve(cfg, cat, "alternateexts");
1826                         if (!ast_strlen_zero(altexts)) {
1827                                 snprintf(tmp, sizeof(tmp), "%s,1", cat);
1828                                 ast_copy_string(altcopy, altexts, sizeof(altcopy));
1829                                 c = altcopy;
1830                                 ext = strsep(&c, ",");
1831                                 while (ext) {
1832                                         ast_add_extension2(con, 0, ext, 1, NULL, NULL, "Goto", ast_strdup(tmp), ast_free_ptr, registrar);
1833                                         ext = strsep(&c, ",");
1834                                 }
1835                         }
1836                 }
1837         }
1838         ast_config_destroy(cfg);
1839 }
1840
1841 static int pbx_load_module(void)
1842 {
1843         struct ast_context *con;
1844
1845         ast_mutex_lock(&reload_lock);
1846
1847         if (!local_table)
1848                 local_table = ast_hashtab_create(17, ast_hashtab_compare_contexts, ast_hashtab_resize_java, ast_hashtab_newsize_java, ast_hashtab_hash_contexts, 0);
1849
1850         if (!pbx_load_config(config)) {
1851                 ast_mutex_unlock(&reload_lock);
1852                 return AST_MODULE_LOAD_DECLINE;
1853         }
1854         
1855         pbx_load_users();
1856
1857         ast_merge_contexts_and_delete(&local_contexts, local_table, registrar);
1858         local_table = NULL; /* the local table has been moved into the global one. */
1859         local_contexts = NULL;
1860
1861         ast_mutex_unlock(&reload_lock);
1862
1863         for (con = NULL; (con = ast_walk_contexts(con));)
1864                 ast_context_verify_includes(con);
1865
1866         pbx_set_overrideswitch(overrideswitch_config);
1867         pbx_set_autofallthrough(autofallthrough_config);
1868         pbx_set_extenpatternmatchnew(extenpatternmatchnew_config);
1869
1870         return AST_MODULE_LOAD_SUCCESS;
1871 }
1872
1873 static int load_module(void)
1874 {
1875         if (static_config && !write_protect_config)
1876                 ast_cli_register(&cli_dialplan_save);
1877         ast_cli_register_multiple(cli_pbx_config, ARRAY_LEN(cli_pbx_config));
1878
1879         if (pbx_load_module())
1880                 return AST_MODULE_LOAD_DECLINE;
1881
1882         return AST_MODULE_LOAD_SUCCESS;
1883 }
1884
1885 static int reload(void)
1886 {
1887         if (clearglobalvars_config)
1888                 pbx_builtin_clear_globals();
1889         return pbx_load_module();
1890 }
1891
1892 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Text Extension Configuration",
1893                 .load = load_module,
1894                 .unload = unload_module,
1895                 .reload = reload,
1896                );