Add transfer support to CEL
[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 (ast_asprintf(&ret, "%s/%s@%s", ast_get_extension_name(e), ast_get_extension_cidmatch(e), ast_get_context_name(c)) < 0) {
476                                                                         ret = NULL;
477                                                                 }
478                                                                 break;
479                                                         } else if (!ast_get_extension_matchcid(e) && !strchr(a->word, '/')) {
480                                                                 if (ast_asprintf(&ret, "%s@%s", ast_get_extension_name(e), ast_get_context_name(c)) < 0) {
481                                                                         ret = NULL;
482                                                                 }
483                                                                 break;
484                                                         }
485                                                 }
486                                         }
487                                 }
488                         }
489                         if (e)  /* got a match */
490                                 break;
491                 }
492
493                 ast_unlock_contexts();
494         error2:
495                 free(exten);
496         } else if (a->pos == 4) { /* 'dialplan remove extension EXT _X_' (priority) */
497                 char *exten = NULL, *context, *cid, *p;
498                 struct ast_context *c;
499                 int le, lc, len;
500                 const char *s = skip_words(a->line, 3); /* skip 'dialplan' 'remove' 'extension' */
501                 int i = split_ec(s, &exten, &context, &cid);    /* parse ext@context */
502
503                 if (i)  /* error */
504                         goto error3;
505                 if ( (p = strchr(exten, ' ')) ) /* remove space after extension */
506                         *p = '\0';
507                 if ( (p = strchr(context, ' ')) ) /* remove space after context */
508                         *p = '\0';
509                 le = strlen(exten);
510                 lc = strlen(context);
511                 len = strlen(a->word);
512                 if (le == 0 || lc == 0)
513                         goto error3;
514
515                 if (ast_rdlock_contexts()) {
516                         ast_log(LOG_ERROR, "Failed to lock context list\n");
517                         goto error3;
518                 }
519
520                 /* walk contexts */
521                 c = NULL;
522                 while ( (c = ast_walk_contexts(c)) ) {
523                         /* XXX locking on c ? */
524                         struct ast_exten *e;
525                         if (strcmp(ast_get_context_name(c), context) != 0)
526                                 continue;
527                         /* got it, we must match here */
528                         e = NULL;
529                         while ( (e = ast_walk_context_extensions(c, e)) ) {
530                                 struct ast_exten *priority;
531                                 char buffer[10];
532
533                                 if (cid && strcmp(ast_get_extension_cidmatch(e), cid) != 0) {
534                                         continue;
535                                 }
536                                 if (strcmp(ast_get_extension_name(e), exten) != 0)
537                                         continue;
538                                 /* XXX lock e ? */
539                                 priority = NULL;
540                                 while ( !ret && (priority = ast_walk_extension_priorities(e, priority)) ) {
541                                         snprintf(buffer, sizeof(buffer), "%u", ast_get_extension_priority(priority));
542                                         if (partial_match(buffer, a->word, len) && ++which > a->n) /* n-th match */
543                                                 ret = strdup(buffer);
544                                 }
545                                 break;
546                         }
547                         break;
548                 }
549                 ast_unlock_contexts();
550         error3:
551                 free(exten);
552         }
553         return ret; 
554 }
555
556 /*!
557  * Include context ...
558  */
559 static char *handle_cli_dialplan_add_include(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
560 {
561         const char *into_context;
562
563         switch (cmd) {
564         case CLI_INIT:
565                 e->command = "dialplan add include";
566                 e->usage =
567                         "Usage: dialplan add include <context> into <context>\n"
568                         "       Include a context in another context.\n";
569                 return NULL;
570         case CLI_GENERATE:
571                 return complete_dialplan_add_include(a);
572         }
573
574         if (a->argc != 6) /* dialplan add include CTX in CTX */
575                 return CLI_SHOWUSAGE;
576
577         /* fifth arg must be 'into' ... */
578         if (strcmp(a->argv[4], "into"))
579                 return CLI_SHOWUSAGE;
580
581         into_context = a->argv[5];
582
583         if (!ast_context_find(into_context)) {
584                 ast_cli(a->fd, "Context '%s' did not exist prior to add include - the context will be created.\n", into_context);
585         }
586
587         if (!ast_context_find_or_create(NULL, NULL, into_context, registrar)) {
588                 ast_cli(a->fd, "ast_context_find_or_create() failed\n");
589                 ast_cli(a->fd, "Failed to include '%s' in '%s' context\n",a->argv[3], a->argv[5]);
590                 return CLI_FAILURE;
591         }
592
593         if (ast_context_add_include(a->argv[5], a->argv[3], registrar)) {
594                 switch (errno) {
595                 case ENOMEM:
596                         ast_cli(a->fd, "Out of memory for context addition\n");
597                         break;
598
599                 case EBUSY:
600                         ast_cli(a->fd, "Failed to lock context(s) list, please try again later\n");
601                         break;
602
603                 case EEXIST:
604                         ast_cli(a->fd, "Context '%s' already included in '%s' context\n",
605                                 a->argv[3], a->argv[5]);
606                         break;
607
608                 case ENOENT:
609                 case EINVAL:
610                         ast_cli(a->fd, "There is no existence of context '%s'\n",
611                                 errno == ENOENT ? a->argv[5] : a->argv[3]);
612                         break;
613
614                 default:
615                         ast_cli(a->fd, "Failed to include '%s' in '%s' context\n",
616                                 a->argv[3], a->argv[5]);
617                         break;
618                 }
619                 return CLI_FAILURE;
620         }
621
622         /* show some info ... */
623         ast_cli(a->fd, "Context '%s' included in '%s' context\n",
624                 a->argv[3], a->argv[5]);
625
626         return CLI_SUCCESS;
627 }
628
629 static char *complete_dialplan_add_include(struct ast_cli_args *a)
630 {
631         struct ast_context *c;
632         int which = 0;
633         char *ret = NULL;
634         int len = strlen(a->word);
635
636         if (a->pos == 3) {              /* 'dialplan add include _X_' (context) ... */
637                 if (ast_rdlock_contexts()) {
638                         ast_log(LOG_ERROR, "Failed to lock context list\n");
639                         return NULL;
640                 }
641                 for (c = NULL; !ret && (c = ast_walk_contexts(c)); )
642                         if (partial_match(ast_get_context_name(c), a->word, len) && ++which > a->n)
643                                 ret = strdup(ast_get_context_name(c));
644                 ast_unlock_contexts();
645                 return ret;
646         } else if (a->pos == 4) { /* dialplan add include CTX _X_ */
647                 /* always complete  as 'into' */
648                 return (a->n == 0) ? strdup("into") : NULL;
649         } else if (a->pos == 5) { /* 'dialplan add include CTX into _X_' (dst context) */
650                 char *context, *dupline, *into;
651                 const char *s = skip_words(a->line, 3); /* should not fail */
652                 context = dupline = strdup(s);
653
654                 if (!dupline) {
655                         ast_log(LOG_ERROR, "Out of free memory\n");
656                         return NULL;
657                 }
658
659                 strsep(&dupline, " "); /* skip context */
660                 into = strsep(&dupline, " ");
661                 /* error if missing context or fifth word is not 'into' */
662                 if (!strlen(context) || strcmp(into, "into")) {
663                         ast_log(LOG_ERROR, "bad context %s or missing into %s\n",
664                                 context, into);
665                         goto error3;
666                 }
667
668                 if (ast_rdlock_contexts()) {
669                         ast_log(LOG_ERROR, "Failed to lock context list\n");
670                         goto error3;
671                 }
672
673                 for (c = NULL; !ret && (c = ast_walk_contexts(c)); ) {
674                         if (!strcmp(context, ast_get_context_name(c)))
675                                 continue; /* skip ourselves */
676                         if (partial_match(ast_get_context_name(c), a->word, len) &&
677                                         !lookup_ci(c, context) /* not included yet */ &&
678                                         ++which > a->n) {
679                                 ret = strdup(ast_get_context_name(c));
680                         }
681                 }
682                 ast_unlock_contexts();
683         error3:
684                 free(context);
685                 return ret;
686         }
687
688         return NULL;
689 }
690
691 /*!
692  * \brief 'save dialplan' CLI command implementation functions ...
693  */
694 static char *handle_cli_dialplan_save(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
695 {
696         char filename[256], overrideswitch[256] = "";
697         struct ast_context *c;
698         struct ast_config *cfg;
699         struct ast_variable *v;
700         int incomplete = 0; /* incomplete config write? */
701         FILE *output;
702         struct ast_flags config_flags = { 0 };
703         const char *base, *slash;
704
705         switch (cmd) {
706         case CLI_INIT:
707                 e->command = "dialplan save";
708                 e->usage =
709                         "Usage: dialplan save [/path/to/extension/file]\n"
710                         "       Save dialplan created by pbx_config module.\n"
711                         "\n"
712                         "Example: dialplan save                 (/etc/asterisk/extensions.conf)\n"
713                         "         dialplan save /home/markster  (/home/markster/extensions.conf)\n";
714                 return NULL;
715         case CLI_GENERATE:
716                 return NULL;
717         }
718
719         if (! (static_config && !write_protect_config)) {
720                 ast_cli(a->fd,
721                         "I can't save dialplan now, see '%s' example file.\n",
722                         config);
723                 return CLI_FAILURE;
724         }
725
726         if (a->argc != 2 && a->argc != 3)
727                 return CLI_SHOWUSAGE;
728
729         if (ast_mutex_lock(&save_dialplan_lock)) {
730                 ast_cli(a->fd,
731                         "Failed to lock dialplan saving (another proccess saving?)\n");
732                 return CLI_FAILURE;
733         }
734         /* XXX the code here is quite loose, a pathname with .conf in it
735          * is assumed to be a complete pathname
736          */
737         if (a->argc == 3) {     /* have config path. Look for *.conf */
738                 base = a->argv[2];
739                 if (!strstr(a->argv[2], ".conf")) { /*no, this is assumed to be a pathname */
740                         /* if filename ends with '/', do not add one */
741                         slash = (*(a->argv[2] + strlen(a->argv[2]) -1) == '/') ? "/" : "";
742                 } else {        /* yes, complete file name */
743                         slash = "";
744                 }
745         } else {
746                 /* no config file, default one */
747                 base = ast_config_AST_CONFIG_DIR;
748                 slash = "/";
749         }
750         snprintf(filename, sizeof(filename), "%s%s%s", base, slash, config);
751
752         cfg = ast_config_load("extensions.conf", config_flags);
753         if (!cfg) {
754                 ast_cli(a->fd, "Failed to load extensions.conf\n");
755                 ast_mutex_unlock(&save_dialplan_lock);
756                 return CLI_FAILURE;
757         }
758
759         /* try to lock contexts list */
760         if (ast_rdlock_contexts()) {
761                 ast_cli(a->fd, "Failed to lock contexts list\n");
762                 ast_mutex_unlock(&save_dialplan_lock);
763                 ast_config_destroy(cfg);
764                 return CLI_FAILURE;
765         }
766
767         /* create new file ... */
768         if (!(output = fopen(filename, "wt"))) {
769                 ast_cli(a->fd, "Failed to create file '%s'\n",
770                         filename);
771                 ast_unlock_contexts();
772                 ast_mutex_unlock(&save_dialplan_lock);
773                 ast_config_destroy(cfg);
774                 return CLI_FAILURE;
775         }
776
777         /* fireout general info */
778         if (overrideswitch_config) {
779                 snprintf(overrideswitch, sizeof(overrideswitch), "overrideswitch=%s\n", overrideswitch_config);
780         }
781         fprintf(output, "[general]\nstatic=%s\nwriteprotect=%s\nautofallthrough=%s\nclearglobalvars=%s\n%sextenpatternmatchnew=%s\n\n",
782                 static_config ? "yes" : "no",
783                 write_protect_config ? "yes" : "no",
784                 autofallthrough_config ? "yes" : "no",
785                                 clearglobalvars_config ? "yes" : "no",
786                                 overrideswitch_config ? overrideswitch : "",
787                                 extenpatternmatchnew_config ? "yes" : "no");
788
789         if ((v = ast_variable_browse(cfg, "globals"))) {
790                 fprintf(output, "[globals]\n");
791                 while(v) {
792                         fprintf(output, "%s => %s\n", v->name, v->value);
793                         v = v->next;
794                 }
795                 fprintf(output, "\n");
796         }
797
798         ast_config_destroy(cfg);
799         
800 #define PUT_CTX_HDR     do { \
801         if (!context_header_written) {  \
802                 fprintf(output, "[%s]\n", ast_get_context_name(c));     \
803                 context_header_written = 1;     \
804         }       \
805         } while (0)
806
807         /* walk all contexts */
808         for (c = NULL; (c = ast_walk_contexts(c)); ) {
809                 int context_header_written = 0;
810                 struct ast_exten *ext, *last_written_e = NULL;
811                 struct ast_include *i;
812                 struct ast_ignorepat *ip;
813                 struct ast_sw *sw;
814
815                 /* try to lock context and fireout all info */  
816                 if (ast_rdlock_context(c)) { /* lock failure */
817                         incomplete = 1;
818                         continue;
819                 }
820                 /* registered by this module? */
821                 /* XXX do we need this ? */
822                 if (!strcmp(ast_get_context_registrar(c), registrar)) {
823                         fprintf(output, "[%s]\n", ast_get_context_name(c));
824                         context_header_written = 1;
825                 }
826
827                 /* walk extensions ... */
828                 for (ext = NULL; (ext = ast_walk_context_extensions(c, ext)); ) {
829                         struct ast_exten *p = NULL;
830
831                         /* fireout priorities */
832                         while ( (p = ast_walk_extension_priorities(ext, p)) ) {
833                                 if (strcmp(ast_get_extension_registrar(p), registrar) != 0) /* not this source */
834                                         continue;
835                 
836                                 /* make empty line between different extensions */      
837                                 if (last_written_e != NULL &&
838                                             strcmp(ast_get_extension_name(last_written_e),
839                                                     ast_get_extension_name(p)))
840                                         fprintf(output, "\n");
841                                 last_written_e = p;
842                         
843                                 PUT_CTX_HDR;
844
845                                 if (ast_get_extension_priority(p) == PRIORITY_HINT) { /* easy */
846                                         fprintf(output, "exten => %s,hint,%s\n",
847                                                     ast_get_extension_name(p),
848                                                     ast_get_extension_app(p));
849                                 } else {
850                                         const char *sep, *cid;
851                                         const char *el = ast_get_extension_label(p);
852                                         char label[128] = "";
853  
854                                         if (ast_get_extension_matchcid(p)) {
855                                                 sep = "/";
856                                                 cid = ast_get_extension_cidmatch(p);
857                                         } else
858                                                 sep = cid = "";
859                                 
860                                         if (el && (snprintf(label, sizeof(label), "(%s)", el) != (strlen(el) + 2)))
861                                                 incomplete = 1; /* error encountered or label > 125 chars */
862                                         
863                                         fprintf(output, "exten => %s%s%s,%d%s,%s(%s)\n",
864                                             ast_get_extension_name(p), (ast_strlen_zero(sep) ? "" : sep), (ast_strlen_zero(cid) ? "" : cid),
865                                             ast_get_extension_priority(p), label,
866                                             ast_get_extension_app(p), (ast_strlen_zero(ast_get_extension_app_data(p)) ? "" : (const char *)ast_get_extension_app_data(p)));
867                                 }
868                         }
869                 }
870
871                 /* written any extensions? ok, write space between exten & inc */
872                 if (last_written_e)
873                         fprintf(output, "\n");
874
875                 /* walk through includes */
876                 for (i = NULL; (i = ast_walk_context_includes(c, i)) ; ) {
877                         if (strcmp(ast_get_include_registrar(i), registrar) != 0)
878                                 continue; /* not mine */
879                         PUT_CTX_HDR;
880                         fprintf(output, "include => %s\n", ast_get_include_name(i));
881                 }
882                 if (ast_walk_context_includes(c, NULL))
883                         fprintf(output, "\n");
884
885                 /* walk through switches */
886                 for (sw = NULL; (sw = ast_walk_context_switches(c, sw)) ; ) {
887                         if (strcmp(ast_get_switch_registrar(sw), registrar) != 0)
888                                 continue; /* not mine */
889                         PUT_CTX_HDR;
890                         fprintf(output, "switch => %s/%s\n",
891                                     ast_get_switch_name(sw), ast_get_switch_data(sw));
892                 }
893
894                 if (ast_walk_context_switches(c, NULL))
895                         fprintf(output, "\n");
896
897                 /* fireout ignorepats ... */
898                 for (ip = NULL; (ip = ast_walk_context_ignorepats(c, ip)); ) {
899                         if (strcmp(ast_get_ignorepat_registrar(ip), registrar) != 0)
900                                 continue; /* not mine */
901                         PUT_CTX_HDR;
902                         fprintf(output, "ignorepat => %s\n",
903                                                 ast_get_ignorepat_name(ip));
904                 }
905
906                 ast_unlock_context(c);
907         }       
908
909         ast_unlock_contexts();
910         ast_mutex_unlock(&save_dialplan_lock);
911         fclose(output);
912
913         if (incomplete) {
914                 ast_cli(a->fd, "Saved dialplan is incomplete\n");
915                 return CLI_FAILURE;
916         }
917
918         ast_cli(a->fd, "Dialplan successfully saved into '%s'\n",
919                 filename);
920         return CLI_SUCCESS;
921 }
922
923 /*!
924  * \brief ADD EXTENSION command stuff
925  */
926 static char *handle_cli_dialplan_add_extension(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
927 {
928         char *whole_exten;
929         char *exten, *prior;
930         int iprior = -2;
931         char *cidmatch, *app, *app_data;
932         char *start, *end;
933         const char *into_context;
934
935         switch (cmd) {
936         case CLI_INIT:
937                 e->command = "dialplan add extension";
938                 e->usage =
939                         "Usage: dialplan add extension <exten>,<priority>,<app> into <context> [replace]\n"
940                         "\n"
941                         "       app can be either:\n"
942                         "         app-name\n"
943                         "         app-name(app-data)\n"
944                         "         app-name,<app-data>\n"
945                         "\n"
946                         "       This command will add the new extension into <context>.  If\n"
947                         "       an extension with the same priority already exists and the\n"
948                         "       'replace' option is given we will replace the extension.\n"
949                         "\n"
950                         "Example: dialplan add extension 6123,1,Dial,IAX/216.207.245.56/6123 into local\n"
951                         "         Now, you can dial 6123 and talk to Markster :)\n";
952                 return NULL;
953         case CLI_GENERATE:
954                 return complete_dialplan_add_extension(a);
955         }
956
957         /* check for arguments at first */
958         if (a->argc != 6 && a->argc != 7)
959                 return CLI_SHOWUSAGE;
960         if (strcmp(a->argv[4], "into"))
961                 return CLI_SHOWUSAGE;
962         if (a->argc == 7)
963                 if (strcmp(a->argv[6], "replace"))
964                         return CLI_SHOWUSAGE;
965
966         whole_exten = ast_strdupa(a->argv[3]);
967         exten = strsep(&whole_exten,",");
968         if (strchr(exten, '/')) {
969                 cidmatch = exten;
970                 strsep(&cidmatch,"/");
971         } else {
972                 cidmatch = NULL;
973         }
974         prior = strsep(&whole_exten,",");
975         if (prior) {
976                 if (!strcmp(prior, "hint")) {
977                         iprior = PRIORITY_HINT;
978                 } else {
979                         if (sscanf(prior, "%30d", &iprior) != 1) {
980                                 ast_cli(a->fd, "'%s' is not a valid priority\n", prior);
981                                 prior = NULL;
982                         }
983                 }
984         }
985         app = whole_exten;
986         if (app) {
987                 if ((start = strchr(app, '(')) && (end = strrchr(app, ')'))) {
988                         *start = *end = '\0';
989                         app_data = start + 1;
990                 } else {
991                         app_data = strchr(app, ',');
992                         if (app_data) {
993                                 *app_data++ = '\0';
994                         }
995                 }
996         } else {
997                 app_data = NULL;
998         }
999
1000         if (!exten || !prior || !app) {
1001                 return CLI_SHOWUSAGE;
1002         }
1003
1004         if (!app_data) {
1005                 app_data = "";
1006         }
1007         into_context = a->argv[5];
1008
1009         if (!ast_context_find(into_context)) {
1010                 ast_cli(a->fd, "Context '%s' did not exist prior to add extension - the context will be created.\n", into_context);
1011         }
1012
1013         if (!ast_context_find_or_create(NULL, NULL, into_context, registrar)) {
1014                 ast_cli(a->fd, "Failed to add '%s,%s,%s(%s)' extension into '%s' context\n",
1015                         exten, prior, app, app_data, into_context);
1016                 return CLI_FAILURE;
1017         }
1018
1019         if (ast_add_extension(into_context, a->argc == 7 ? 1 : 0, exten, iprior, NULL, cidmatch, app,
1020                 ast_strdup(app_data), ast_free_ptr, registrar)) {
1021                 switch (errno) {
1022                 case ENOMEM:
1023                         ast_cli(a->fd, "Out of free memory\n");
1024                         break;
1025
1026                 case EBUSY:
1027                         ast_cli(a->fd, "Failed to lock context(s) list, please try again later\n");
1028                         break;
1029
1030                 case ENOENT:
1031                         ast_cli(a->fd, "No existence of '%s' context\n", into_context);
1032                         break;
1033
1034                 case EEXIST:
1035                         ast_cli(a->fd, "Extension %s@%s with priority %s already exists\n",
1036                                 exten, into_context, prior);
1037                         break;
1038
1039                 default:
1040                         ast_cli(a->fd, "Failed to add '%s,%s,%s(%s)' extension into '%s' context\n",
1041                                         exten, prior, app, app_data, into_context);
1042                         break;
1043                 }
1044                 return CLI_FAILURE;
1045         }
1046
1047         if (a->argc == 7) {
1048                 ast_cli(a->fd, "Extension %s@%s (%s) replace by '%s,%s,%s(%s)'\n",
1049                         exten, into_context, prior, exten, prior, app, app_data);
1050         } else {
1051                 ast_cli(a->fd, "Extension '%s,%s,%s(%s)' added into '%s' context\n",
1052                         exten, prior, app, app_data, into_context);
1053         }
1054
1055         return CLI_SUCCESS;
1056 }
1057
1058 static char *complete_dialplan_remove_context(struct ast_cli_args *a)
1059 {
1060         struct ast_context *c = NULL;
1061         int len = strlen(a->word);
1062         char *res = NULL;
1063         int which = 0;
1064
1065         if (a->pos != 3) {
1066                 return NULL;
1067         }
1068
1069
1070         /* try to lock contexts list ... */
1071         if (ast_rdlock_contexts()) {
1072                 ast_log(LOG_WARNING, "Failed to lock contexts list\n");
1073                 return NULL;
1074         }
1075
1076         /* walk through all contexts */
1077         while ( !res && (c = ast_walk_contexts(c)) ) {
1078                 if (partial_match(ast_get_context_name(c), a->word, len) && ++which > a->n) {
1079                         res = strdup(ast_get_context_name(c));
1080                 }
1081         }
1082         ast_unlock_contexts();
1083         return res;
1084 }
1085
1086 /*! dialplan add extension 6123,1,Dial,IAX/212.71.138.13/6123 into local */
1087 static char *complete_dialplan_add_extension(struct ast_cli_args *a)
1088 {
1089         int which = 0;
1090
1091         if (a->pos == 4) {              /* complete 'into' word ... */
1092                 return (a->n == 0) ? strdup("into") : NULL;
1093         } else if (a->pos == 5) { /* complete context */
1094                 struct ast_context *c = NULL;
1095                 int len = strlen(a->word);
1096                 char *res = NULL;
1097
1098                 /* try to lock contexts list ... */
1099                 if (ast_rdlock_contexts()) {
1100                         ast_log(LOG_WARNING, "Failed to lock contexts list\n");
1101                         return NULL;
1102                 }
1103
1104                 /* walk through all contexts */
1105                 while ( !res && (c = ast_walk_contexts(c)) )
1106                         if (partial_match(ast_get_context_name(c), a->word, len) && ++which > a->n)
1107                                 res = strdup(ast_get_context_name(c));
1108                 ast_unlock_contexts();
1109                 return res;
1110         } else if (a->pos == 6) {
1111                 return a->n == 0 ? strdup("replace") : NULL;
1112         }
1113         return NULL;
1114 }
1115
1116 /*!
1117  * IGNOREPAT CLI stuff
1118  */
1119 static char *handle_cli_dialplan_add_ignorepat(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1120 {
1121         switch (cmd) {
1122         case CLI_INIT:
1123                 e->command = "dialplan add ignorepat";
1124                 e->usage =
1125                         "Usage: dialplan add ignorepat <pattern> into <context>\n"
1126                         "       This command adds a new ignore pattern into context <context>\n"
1127                         "\n"
1128                         "Example: dialplan add ignorepat _3XX into local\n";
1129                 return NULL;
1130         case CLI_GENERATE:
1131                 return complete_dialplan_add_ignorepat(a);
1132         }
1133
1134         if (a->argc != 6)
1135                 return CLI_SHOWUSAGE;
1136
1137         if (strcmp(a->argv[4], "into"))
1138                 return CLI_SHOWUSAGE;
1139
1140         if (ast_context_add_ignorepat(a->argv[5], a->argv[3], registrar)) {
1141                 switch (errno) {
1142                 case ENOMEM:
1143                         ast_cli(a->fd, "Out of free memory\n");
1144                         break;
1145
1146                 case ENOENT:
1147                         ast_cli(a->fd, "There is no existence of '%s' context\n", a->argv[5]);
1148                         break;
1149
1150                 case EEXIST:
1151                         ast_cli(a->fd, "Ignore pattern '%s' already included in '%s' context\n",
1152                                 a->argv[3], a->argv[5]);
1153                         break;
1154
1155                 case EBUSY:
1156                         ast_cli(a->fd, "Failed to lock context(s) list, please, try again later\n");
1157                         break;
1158
1159                 default:
1160                         ast_cli(a->fd, "Failed to add ingore pattern '%s' into '%s' context\n",
1161                                 a->argv[3], a->argv[5]);
1162                         break;
1163                 }
1164                 return CLI_FAILURE;
1165         }
1166
1167         ast_cli(a->fd, "Ignore pattern '%s' added into '%s' context\n",
1168                 a->argv[3], a->argv[5]);
1169
1170         return CLI_SUCCESS;
1171 }
1172
1173 static char *complete_dialplan_add_ignorepat(struct ast_cli_args *a)
1174 {
1175         if (a->pos == 4)
1176                 return a->n == 0 ? strdup("into") : NULL;
1177         else if (a->pos == 5) {
1178                 struct ast_context *c;
1179                 int which = 0;
1180                 char *dupline, *ignorepat = NULL;
1181                 const char *s;
1182                 char *ret = NULL;
1183                 int len = strlen(a->word);
1184
1185                 /* XXX skip first three words 'dialplan' 'add' 'ignorepat' */
1186                 s = skip_words(a->line, 3);
1187                 if (s == NULL)
1188                         return NULL;
1189                 dupline = strdup(s);
1190                 if (!dupline) {
1191                         ast_log(LOG_ERROR, "Malloc failure\n");
1192                         return NULL;
1193                 }
1194                 ignorepat = strsep(&dupline, " ");
1195
1196                 if (ast_rdlock_contexts()) {
1197                         ast_log(LOG_ERROR, "Failed to lock contexts list\n");
1198                         return NULL;
1199                 }
1200
1201                 for (c = NULL; !ret && (c = ast_walk_contexts(c));) {
1202                         int found = 0;
1203
1204                         if (!partial_match(ast_get_context_name(c), a->word, len))
1205                                 continue; /* not mine */
1206                         if (ignorepat) /* there must be one, right ? */
1207                                 found = lookup_c_ip(c, ignorepat);
1208                         if (!found && ++which > a->n)
1209                                 ret = strdup(ast_get_context_name(c));
1210                 }
1211
1212                 free(ignorepat);
1213                 ast_unlock_contexts();
1214                 return ret;
1215         }
1216
1217         return NULL;
1218 }
1219
1220 static char *handle_cli_dialplan_remove_ignorepat(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1221 {
1222         switch (cmd) {
1223         case CLI_INIT:
1224                 e->command = "dialplan remove ignorepat";
1225                 e->usage =
1226                         "Usage: dialplan remove ignorepat <pattern> from <context>\n"
1227                         "       This command removes an ignore pattern from context <context>\n"
1228                         "\n"
1229                         "Example: dialplan remove ignorepat _3XX from local\n";
1230                 return NULL;
1231         case CLI_GENERATE:
1232                 return complete_dialplan_remove_ignorepat(a);
1233         }
1234
1235         if (a->argc != 6)
1236                 return CLI_SHOWUSAGE;
1237
1238         if (strcmp(a->argv[4], "from"))
1239                 return CLI_SHOWUSAGE;
1240
1241         if (ast_context_remove_ignorepat(a->argv[5], a->argv[3], registrar)) {
1242                 switch (errno) {
1243                 case EBUSY:
1244                         ast_cli(a->fd, "Failed to lock context(s) list, please try again later\n");
1245                         break;
1246
1247                 case ENOENT:
1248                         ast_cli(a->fd, "There is no existence of '%s' context\n", a->argv[5]);
1249                         break;
1250
1251                 case EINVAL:
1252                         ast_cli(a->fd, "There is no existence of '%s' ignore pattern in '%s' context\n",
1253                                         a->argv[3], a->argv[5]);
1254                         break;
1255
1256                 default:
1257                         ast_cli(a->fd, "Failed to remove ignore pattern '%s' from '%s' context\n",
1258                                         a->argv[3], a->argv[5]);
1259                         break;
1260                 }
1261                 return CLI_FAILURE;
1262         }
1263
1264         ast_cli(a->fd, "Ignore pattern '%s' removed from '%s' context\n",
1265                 a->argv[3], a->argv[5]);
1266         return CLI_SUCCESS;
1267 }
1268
1269 static char *complete_dialplan_remove_ignorepat(struct ast_cli_args *a)
1270 {
1271         struct ast_context *c;
1272         int which = 0;
1273         char *ret = NULL;
1274
1275         if (a->pos == 3) {
1276                 int len = strlen(a->word);
1277                 if (ast_rdlock_contexts()) {
1278                         ast_log(LOG_WARNING, "Failed to lock contexts list\n");
1279                         return NULL;
1280                 }
1281
1282                 for (c = NULL; !ret && (c = ast_walk_contexts(c));) {
1283                         struct ast_ignorepat *ip;
1284
1285                         if (ast_rdlock_context(c))      /* error, skip it */
1286                                 continue;
1287                         
1288                         for (ip = NULL; !ret && (ip = ast_walk_context_ignorepats(c, ip));) {
1289                                 if (partial_match(ast_get_ignorepat_name(ip), a->word, len) && ++which > a->n) {
1290                                         /* n-th match */
1291                                         struct ast_context *cw = NULL;
1292                                         int found = 0;
1293                                         while ( (cw = ast_walk_contexts(cw)) && cw != c && !found) {
1294                                                 /* XXX do i stop on c, or skip it ? */
1295                                                 found = lookup_c_ip(cw, ast_get_ignorepat_name(ip));
1296                                         }
1297                                         if (!found)
1298                                                 ret = strdup(ast_get_ignorepat_name(ip));
1299                                 }
1300                         }
1301                         ast_unlock_context(c);
1302                 }
1303                 ast_unlock_contexts();
1304                 return ret;
1305         } else if (a->pos == 4) {
1306                  return a->n == 0 ? strdup("from") : NULL;
1307         } else if (a->pos == 5) { /* XXX check this */
1308                 char *dupline, *duplinet, *ignorepat;
1309                 int len = strlen(a->word);
1310
1311                 dupline = strdup(a->line);
1312                 if (!dupline) {
1313                         ast_log(LOG_WARNING, "Out of free memory\n");
1314                         return NULL;
1315                 }
1316
1317                 duplinet = dupline;
1318                 strsep(&duplinet, " ");
1319                 strsep(&duplinet, " ");
1320                 ignorepat = strsep(&duplinet, " ");
1321
1322                 if (!ignorepat) {
1323                         free(dupline);
1324                         return NULL;
1325                 }
1326
1327                 if (ast_rdlock_contexts()) {
1328                         ast_log(LOG_WARNING, "Failed to lock contexts list\n");
1329                         free(dupline);
1330                         return NULL;
1331                 }
1332
1333                 for (c = NULL; !ret && (c = ast_walk_contexts(c)); ) {
1334                         if (ast_rdlock_context(c))      /* fail, skip it */
1335                                 continue;
1336                         if (!partial_match(ast_get_context_name(c), a->word, len))
1337                                 continue;
1338                         if (lookup_c_ip(c, ignorepat) && ++which > a->n)
1339                                 ret = strdup(ast_get_context_name(c));
1340                         ast_unlock_context(c);
1341                 }
1342                 ast_unlock_contexts();
1343                 free(dupline);
1344                 return NULL;
1345         }
1346
1347         return NULL;
1348 }
1349
1350 static int pbx_load_module(void);
1351
1352 static char *handle_cli_dialplan_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1353 {
1354         switch (cmd) {
1355         case CLI_INIT:
1356                 e->command = "dialplan reload";
1357                 e->usage =
1358                         "Usage: dialplan reload\n"
1359                         "       Reload extensions.conf without reloading any other\n"
1360                         "       modules.  This command does not delete global variables\n"
1361                         "       unless clearglobalvars is set to yes in extensions.conf\n";
1362                 return NULL;
1363         case CLI_GENERATE:
1364                 return NULL;
1365         }
1366
1367         if (a->argc != 2)
1368                 return CLI_SHOWUSAGE;
1369
1370         if (clearglobalvars_config)
1371                 pbx_builtin_clear_globals();
1372
1373         pbx_load_module();
1374         ast_cli(a->fd, "Dialplan reloaded.\n");
1375         return CLI_SUCCESS;
1376 }
1377
1378 /*!
1379  * CLI entries for commands provided by this module
1380  */
1381 static struct ast_cli_entry cli_pbx_config[] = {
1382         AST_CLI_DEFINE(handle_cli_dialplan_add_extension,    "Add new extension into context"),
1383         AST_CLI_DEFINE(handle_cli_dialplan_remove_extension, "Remove a specified extension"),
1384         AST_CLI_DEFINE(handle_cli_dialplan_remove_context,   "Remove a specified context"),
1385         AST_CLI_DEFINE(handle_cli_dialplan_add_ignorepat,    "Add new ignore pattern"),
1386         AST_CLI_DEFINE(handle_cli_dialplan_remove_ignorepat, "Remove ignore pattern from context"),
1387         AST_CLI_DEFINE(handle_cli_dialplan_add_include,      "Include context in other context"),
1388         AST_CLI_DEFINE(handle_cli_dialplan_remove_include,   "Remove a specified include from context"),
1389         AST_CLI_DEFINE(handle_cli_dialplan_reload,           "Reload extensions and *only* extensions"),
1390         AST_CLI_DEFINE(handle_cli_dialplan_save,             "Save current dialplan into a file")
1391 };
1392
1393 static struct ast_cli_entry cli_dialplan_save =
1394         AST_CLI_DEFINE(handle_cli_dialplan_save, "Save dialplan");
1395
1396 /*!
1397  * Standard module functions ...
1398  */
1399 static int unload_module(void)
1400 {
1401         if (static_config && !write_protect_config)
1402                 ast_cli_unregister(&cli_dialplan_save);
1403         if (overrideswitch_config) {
1404                 ast_free(overrideswitch_config);
1405         }
1406         ast_cli_unregister_multiple(cli_pbx_config, ARRAY_LEN(cli_pbx_config));
1407         ast_context_destroy(NULL, registrar);
1408         return 0;
1409 }
1410
1411 /*!\note Protect against misparsing based upon commas in the middle of fields
1412  * like character classes.  We've taken steps to permit pretty much every other
1413  * printable character in a character class, so properly handling a comma at
1414  * this level is a natural extension.  This is almost like the standard
1415  * application parser in app.c, except that it handles square brackets. */
1416 static char *pbx_strsep(char **destructible, const char *delim)
1417 {
1418         int square = 0;
1419         char *res;
1420
1421         if (!destructible || !*destructible) {
1422                 return NULL;
1423         }
1424         res = *destructible;
1425         for (; **destructible; (*destructible)++) {
1426                 if (**destructible == '[' && !strchr(delim, '[')) {
1427                         square++;
1428                 } else if (**destructible == ']' && !strchr(delim, ']')) {
1429                         if (square) {
1430                                 square--;
1431                         }
1432                 } else if (**destructible == '\\' && !strchr(delim, '\\')) {
1433                         (*destructible)++;
1434                 } else if (strchr(delim, **destructible) && !square) {
1435                         **destructible = '\0';
1436                         (*destructible)++;
1437                         break;
1438                 }
1439         }
1440         if (**destructible == '\0') {
1441                 *destructible = NULL;
1442         }
1443         return res;
1444 }
1445
1446 static int pbx_load_config(const char *config_file)
1447 {
1448         struct ast_config *cfg;
1449         char *end;
1450         char *label;
1451 #ifdef LOW_MEMORY
1452         char realvalue[256];
1453 #else
1454         char realvalue[8192];
1455 #endif
1456         int lastpri = -2;
1457         struct ast_context *con;
1458         struct ast_variable *v;
1459         const char *cxt;
1460         const char *aft;
1461         const char *newpm, *ovsw;
1462         struct ast_flags config_flags = { 0 };
1463         char lastextension[256];
1464         cfg = ast_config_load(config_file, config_flags);
1465         if (!cfg || cfg == CONFIG_STATUS_FILEINVALID)
1466                 return 0;
1467
1468         /* Use existing config to populate the PBX table */
1469         static_config = ast_true(ast_variable_retrieve(cfg, "general", "static"));
1470         write_protect_config = ast_true(ast_variable_retrieve(cfg, "general", "writeprotect"));
1471         if ((aft = ast_variable_retrieve(cfg, "general", "autofallthrough")))
1472                 autofallthrough_config = ast_true(aft);
1473         if ((newpm = ast_variable_retrieve(cfg, "general", "extenpatternmatchnew")))
1474                 extenpatternmatchnew_config = ast_true(newpm);
1475         clearglobalvars_config = ast_true(ast_variable_retrieve(cfg, "general", "clearglobalvars"));
1476         if ((ovsw = ast_variable_retrieve(cfg, "general", "overrideswitch"))) {
1477                 if (overrideswitch_config) {
1478                         ast_free(overrideswitch_config);
1479                 }
1480                 if (!ast_strlen_zero(ovsw)) {
1481                         overrideswitch_config = ast_strdup(ovsw);
1482                 } else {
1483                         overrideswitch_config = NULL;
1484                 }
1485         }
1486
1487         ast_copy_string(userscontext, ast_variable_retrieve(cfg, "general", "userscontext") ?: "default", sizeof(userscontext));
1488                                                                     
1489         for (v = ast_variable_browse(cfg, "globals"); v; v = v->next) {
1490                 pbx_substitute_variables_helper(NULL, v->value, realvalue, sizeof(realvalue) - 1);
1491                 pbx_builtin_setvar_helper(NULL, v->name, realvalue);
1492         }
1493         for (cxt = ast_category_browse(cfg, NULL);
1494              cxt;
1495              cxt = ast_category_browse(cfg, cxt)) {
1496                 /* All categories but "general" or "globals" are considered contexts */
1497                 if (!strcasecmp(cxt, "general") || !strcasecmp(cxt, "globals")) {
1498                         continue;
1499                 }
1500                 if (!(con = ast_context_find_or_create(&local_contexts, local_table, cxt, registrar))) {
1501                         continue;
1502                 }
1503
1504                 /* Reset continuation items at the beginning of each context */
1505                 lastextension[0] = '\0';
1506                 lastpri = -2;
1507
1508                 for (v = ast_variable_browse(cfg, cxt); v; v = v->next) {
1509                         char *tc = NULL;
1510                         char realext[256] = "";
1511                         char *stringp, *ext;
1512                         const char *vfile;
1513
1514                         /* get filename for error reporting from top level or an #include */
1515                         vfile = !*v->file ? config_file : v->file;
1516
1517                         if (!strncasecmp(v->name, "same", 4)) {
1518                                 if (ast_strlen_zero(lastextension)) {
1519                                         ast_log(LOG_ERROR,
1520                                                 "No previous pattern in the first entry of context '%s' to match '%s' at line %d of %s!\n",
1521                                                 cxt, v->name, v->lineno, vfile);
1522                                         continue;
1523                                 }
1524                                 if ((stringp = tc = ast_strdup(v->value))) {
1525                                         ast_copy_string(realext, lastextension, sizeof(realext));
1526                                         goto process_extension;
1527                                 }
1528                         } else if (!strcasecmp(v->name, "exten")) {
1529                                 int ipri;
1530                                 char *plus;
1531                                 char *pri, *appl, *data, *cidmatch;
1532
1533                                 if (!(stringp = tc = ast_strdup(v->value))) {
1534                                         continue;
1535                                 }
1536
1537                                 ext = S_OR(pbx_strsep(&stringp, ","), "");
1538                                 pbx_substitute_variables_helper(NULL, ext, realext, sizeof(realext) - 1);
1539                                 ast_copy_string(lastextension, realext, sizeof(lastextension));
1540 process_extension:
1541                                 ipri = -2;
1542                                 if ((cidmatch = strchr(realext, '/'))) {
1543                                         *cidmatch++ = '\0';
1544                                         ast_shrink_phone_number(cidmatch);
1545                                 }
1546                                 pri = ast_strip(S_OR(strsep(&stringp, ","), ""));
1547                                 if ((label = strchr(pri, '('))) {
1548                                         *label++ = '\0';
1549                                         if ((end = strchr(label, ')'))) {
1550                                                 *end = '\0';
1551                                         } else {
1552                                                 ast_log(LOG_WARNING,
1553                                                         "Label missing trailing ')' at line %d of %s\n",
1554                                                         v->lineno, vfile);
1555                                                 ast_free(tc);
1556                                                 continue;
1557                                         }
1558                                 }
1559                                 if ((plus = strchr(pri, '+'))) {
1560                                         *plus++ = '\0';
1561                                 }
1562                                 if (!strcmp(pri,"hint")) {
1563                                         ipri = PRIORITY_HINT;
1564                                 } else if (!strcmp(pri, "next") || !strcmp(pri, "n")) {
1565                                         if (lastpri > -2) {
1566                                                 ipri = lastpri + 1;
1567                                         } else {
1568                                                 ast_log(LOG_WARNING,
1569                                                         "Can't use 'next' priority on the first entry at line %d of %s!\n",
1570                                                         v->lineno, vfile);
1571                                                 ast_free(tc);
1572                                                 continue;
1573                                         }
1574                                 } else if (!strcmp(pri, "same") || !strcmp(pri, "s")) {
1575                                         if (lastpri > -2) {
1576                                                 ipri = lastpri;
1577                                         } else {
1578                                                 ast_log(LOG_WARNING,
1579                                                         "Can't use 'same' priority on the first entry at line %d of %s!\n",
1580                                                         v->lineno, vfile);
1581                                                 ast_free(tc);
1582                                                 continue;
1583                                         }
1584                                 } else if (sscanf(pri, "%30d", &ipri) != 1 &&
1585                                            (ipri = ast_findlabel_extension2(NULL, con, realext, pri, cidmatch)) < 1) {
1586                                         ast_log(LOG_WARNING,
1587                                                 "Invalid priority/label '%s' at line %d of %s\n",
1588                                                 pri, v->lineno, vfile);
1589                                         ipri = 0;
1590                                         ast_free(tc);
1591                                         continue;
1592                                 } else if (ipri < 1) {
1593                                         ast_log(LOG_WARNING, "Invalid priority '%s' at line %d of %s\n",
1594                                                 pri, v->lineno, vfile);
1595                                         ast_free(tc);
1596                                         continue;
1597                                 }
1598                                 appl = S_OR(stringp, "");
1599                                 /* Find the first occurrence of '(' */
1600                                 if (!strchr(appl, '(')) {
1601                                         /* No arguments */
1602                                         data = "";
1603                                 } else {
1604                                         char *orig_appl = ast_strdup(appl);
1605
1606                                         if (!orig_appl) {
1607                                                 ast_free(tc);
1608                                                 continue;
1609                                         }
1610
1611                                         appl = strsep(&stringp, "(");
1612
1613                                         /* check if there are variables or expressions without an application, like: exten => 100,hint,DAHDI/g0/${GLOBAL(var)}  */
1614                                         if (strstr(appl, "${") || strstr(appl, "$[")){
1615                                                 /* set appl to original one */
1616                                                 strcpy(appl, orig_appl);
1617                                                 /* set no data */
1618                                                 data = "";
1619                                         /* no variable before application found -> go ahead */
1620                                         } else {
1621                                                 data = S_OR(stringp, "");
1622                                                 if ((end = strrchr(data, ')'))) {
1623                                                         *end = '\0';
1624                                                 } else {
1625                                                         ast_log(LOG_WARNING,
1626                                                                 "No closing parenthesis found? '%s(%s' at line %d of %s\n",
1627                                                                 appl, data, v->lineno, vfile);
1628                                                 }
1629                                         }
1630                                         ast_free(orig_appl);
1631                                 }
1632
1633                                 appl = ast_skip_blanks(appl);
1634                                 if (ipri) {
1635                                         if (plus) {
1636                                                 ipri += atoi(plus);
1637                                         }
1638                                         lastpri = ipri;
1639                                         if (!ast_opt_dont_warn && (!strcmp(realext, "_.") || !strcmp(realext, "_!"))) {
1640                                                 ast_log(LOG_WARNING,
1641                                                         "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",
1642                                                         realext, realext[1], v->lineno, vfile);
1643                                         }
1644                                         if (ast_add_extension2(con, 0, realext, ipri, label, cidmatch, appl, ast_strdup(data), ast_free_ptr, registrar)) {
1645                                                 ast_log(LOG_WARNING,
1646                                                         "Unable to register extension at line %d of %s\n",
1647                                                         v->lineno, vfile);
1648                                         }
1649                                 }
1650                                 ast_free(tc);
1651                         } else if (!strcasecmp(v->name, "include")) {
1652                                 pbx_substitute_variables_helper(NULL, v->value, realvalue, sizeof(realvalue) - 1);
1653                                 if (ast_context_add_include2(con, realvalue, registrar)) {
1654                                         switch (errno) {
1655                                         case ENOMEM:
1656                                                 ast_log(LOG_WARNING, "Out of memory for context addition\n");
1657                                                 break;
1658
1659                                         case EBUSY:
1660                                                 ast_log(LOG_WARNING, "Failed to lock context(s) list, please try again later\n");
1661                                                 break;
1662
1663                                         case EEXIST:
1664                                                 ast_log(LOG_WARNING,
1665                                                         "Context '%s' already included in '%s' context on include at line %d of %s\n",
1666                                                         v->value, cxt, v->lineno, vfile);
1667                                                 break;
1668
1669                                         case ENOENT:
1670                                         case EINVAL:
1671                                                 ast_log(LOG_WARNING,
1672                                                         "There is no existence of context '%s' included at line %d of %s\n",
1673                                                         errno == ENOENT ? v->value : cxt, v->lineno, vfile);
1674                                                 break;
1675
1676                                         default:
1677                                                 ast_log(LOG_WARNING,
1678                                                         "Failed to include '%s' in '%s' context at line %d of %s\n",
1679                                                         v->value, cxt, v->lineno, vfile);
1680                                                 break;
1681                                         }
1682                                 }
1683                         } else if (!strcasecmp(v->name, "ignorepat")) {
1684                                 pbx_substitute_variables_helper(NULL, v->value, realvalue, sizeof(realvalue) - 1);
1685                                 if (ast_context_add_ignorepat2(con, realvalue, registrar)) {
1686                                         ast_log(LOG_WARNING,
1687                                                 "Unable to include ignorepat '%s' in context '%s' at line %d of %s\n",
1688                                                 v->value, cxt, v->lineno, vfile);
1689                                 }
1690                         } else if (!strcasecmp(v->name, "switch") || !strcasecmp(v->name, "lswitch") || !strcasecmp(v->name, "eswitch")) {
1691                                 char *appl, *data;
1692                                 stringp = realvalue;
1693                                 
1694                                 if (!strcasecmp(v->name, "switch")) {
1695                                         pbx_substitute_variables_helper(NULL, v->value, realvalue, sizeof(realvalue) - 1);
1696                                 } else {
1697                                         ast_copy_string(realvalue, v->value, sizeof(realvalue));
1698                                 }
1699                                 appl = strsep(&stringp, "/");
1700                                 data = S_OR(stringp, "");
1701                                 if (ast_context_add_switch2(con, appl, data, !strcasecmp(v->name, "eswitch"), registrar)) {
1702                                         ast_log(LOG_WARNING,
1703                                                 "Unable to include switch '%s' in context '%s' at line %d of %s\n",
1704                                                 v->value, cxt, v->lineno, vfile);
1705                                 }
1706                         } else {
1707                                 ast_log(LOG_WARNING,
1708                                         "==!!== Unknown directive: %s at line %d of %s -- IGNORING!!!\n",
1709                                         v->name, v->lineno, vfile);
1710                         }
1711                 }
1712         }
1713         ast_config_destroy(cfg);
1714         return 1;
1715 }
1716
1717 static void append_interface(char *iface, int maxlen, char *add)
1718 {
1719         int len = strlen(iface);
1720         if (strlen(add) + len < maxlen - 2) {
1721                 if (strlen(iface)) {
1722                         iface[len] = '&';
1723                         strcpy(iface + len + 1, add);
1724                 } else
1725                         strcpy(iface, add);
1726         }
1727 }
1728
1729 static void pbx_load_users(void)
1730 {
1731         struct ast_config *cfg;
1732         char *cat, *chan;
1733         const char *dahdichan;
1734         const char *hasexten, *altexts;
1735         char tmp[256];
1736         char iface[256];
1737         char dahdicopy[256];
1738         char *ext, altcopy[256];
1739         char *c;
1740         int hasvoicemail;
1741         int start, finish, x;
1742         struct ast_context *con = NULL;
1743         struct ast_flags config_flags = { 0 };
1744         
1745         cfg = ast_config_load("users.conf", config_flags);
1746         if (!cfg)
1747                 return;
1748
1749         for (cat = ast_category_browse(cfg, NULL); cat ; cat = ast_category_browse(cfg, cat)) {
1750                 if (!strcasecmp(cat, "general"))
1751                         continue;
1752                 iface[0] = '\0';
1753                 if (ast_true(ast_config_option(cfg, cat, "hassip"))) {
1754                         snprintf(tmp, sizeof(tmp), "SIP/%s", cat);
1755                         append_interface(iface, sizeof(iface), tmp);
1756                 }
1757                 if (ast_true(ast_config_option(cfg, cat, "hasiax"))) {
1758                         snprintf(tmp, sizeof(tmp), "IAX2/%s", cat);
1759                         append_interface(iface, sizeof(iface), tmp);
1760                 }
1761                 if (ast_true(ast_config_option(cfg, cat, "hash323"))) {
1762                         snprintf(tmp, sizeof(tmp), "H323/%s", cat);
1763                         append_interface(iface, sizeof(iface), tmp);
1764                 }
1765                 hasexten = ast_config_option(cfg, cat, "hasexten");
1766                 if (hasexten && !ast_true(hasexten))
1767                         continue;
1768                 hasvoicemail = ast_true(ast_config_option(cfg, cat, "hasvoicemail"));
1769                 dahdichan = ast_variable_retrieve(cfg, cat, "dahdichan");
1770                 if (!dahdichan)
1771                         dahdichan = ast_variable_retrieve(cfg, "general", "dahdichan");
1772                 if (!ast_strlen_zero(dahdichan)) {
1773                         ast_copy_string(dahdicopy, dahdichan, sizeof(dahdicopy));
1774                         c = dahdicopy;
1775                         chan = strsep(&c, ",");
1776                         while (chan) {
1777                                 if (sscanf(chan, "%30d-%30d", &start, &finish) == 2) {
1778                                         /* Range */
1779                                 } else if (sscanf(chan, "%30d", &start)) {
1780                                         /* Just one */
1781                                         finish = start;
1782                                 } else {
1783                                         start = 0; finish = 0;
1784                                 }
1785                                 if (finish < start) {
1786                                         x = finish;
1787                                         finish = start;
1788                                         start = x;
1789                                 }
1790                                 for (x = start; x <= finish; x++) {
1791                                         snprintf(tmp, sizeof(tmp), "DAHDI/%d", x);
1792                                         append_interface(iface, sizeof(iface), tmp);
1793                                 }
1794                                 chan = strsep(&c, ",");
1795                         }
1796                 }
1797                 if (!ast_strlen_zero(iface)) {
1798                         /* Only create a context here when it is really needed. Otherwise default empty context
1799                         created by pbx_config may conflict with the one explicitly created by pbx_ael */
1800                         if (!con)
1801                                 con = ast_context_find_or_create(&local_contexts, local_table, userscontext, registrar);
1802
1803                         if (!con) {
1804                                 ast_log(LOG_ERROR, "Can't find/create user context '%s'\n", userscontext);
1805                                 return;
1806                         }
1807
1808                         /* Add hint */
1809                         ast_add_extension2(con, 0, cat, -1, NULL, NULL, iface, NULL, NULL, registrar);
1810                         /* If voicemail, use "stdexten" else use plain old dial */
1811                         if (hasvoicemail) {
1812                                 if (ast_opt_stdexten_macro) {
1813                                         /* Use legacy stdexten macro method. */
1814                                         snprintf(tmp, sizeof(tmp), "stdexten,%s,${HINT}", cat);
1815                                         ast_add_extension2(con, 0, cat, 1, NULL, NULL, "Macro", ast_strdup(tmp), ast_free_ptr, registrar);
1816                                 } else {
1817                                         snprintf(tmp, sizeof(tmp), "%s,stdexten(${HINT})", cat);
1818                                         ast_add_extension2(con, 0, cat, 1, NULL, NULL, "Gosub", ast_strdup(tmp), ast_free_ptr, registrar);
1819                                 }
1820                         } else {
1821                                 ast_add_extension2(con, 0, cat, 1, NULL, NULL, "Dial", ast_strdup("${HINT}"), ast_free_ptr, registrar);
1822                         }
1823                         altexts = ast_variable_retrieve(cfg, cat, "alternateexts");
1824                         if (!ast_strlen_zero(altexts)) {
1825                                 snprintf(tmp, sizeof(tmp), "%s,1", cat);
1826                                 ast_copy_string(altcopy, altexts, sizeof(altcopy));
1827                                 c = altcopy;
1828                                 ext = strsep(&c, ",");
1829                                 while (ext) {
1830                                         ast_add_extension2(con, 0, ext, 1, NULL, NULL, "Goto", ast_strdup(tmp), ast_free_ptr, registrar);
1831                                         ext = strsep(&c, ",");
1832                                 }
1833                         }
1834                 }
1835         }
1836         ast_config_destroy(cfg);
1837 }
1838
1839 static int pbx_load_module(void)
1840 {
1841         struct ast_context *con;
1842
1843         ast_mutex_lock(&reload_lock);
1844
1845         if (!local_table)
1846                 local_table = ast_hashtab_create(17, ast_hashtab_compare_contexts, ast_hashtab_resize_java, ast_hashtab_newsize_java, ast_hashtab_hash_contexts, 0);
1847
1848         if (!pbx_load_config(config)) {
1849                 ast_mutex_unlock(&reload_lock);
1850                 return AST_MODULE_LOAD_DECLINE;
1851         }
1852         
1853         pbx_load_users();
1854
1855         ast_merge_contexts_and_delete(&local_contexts, local_table, registrar);
1856         local_table = NULL; /* the local table has been moved into the global one. */
1857         local_contexts = NULL;
1858
1859         ast_mutex_unlock(&reload_lock);
1860
1861         for (con = NULL; (con = ast_walk_contexts(con));)
1862                 ast_context_verify_includes(con);
1863
1864         pbx_set_overrideswitch(overrideswitch_config);
1865         pbx_set_autofallthrough(autofallthrough_config);
1866         pbx_set_extenpatternmatchnew(extenpatternmatchnew_config);
1867
1868         return AST_MODULE_LOAD_SUCCESS;
1869 }
1870
1871 static int load_module(void)
1872 {
1873         if (static_config && !write_protect_config)
1874                 ast_cli_register(&cli_dialplan_save);
1875         ast_cli_register_multiple(cli_pbx_config, ARRAY_LEN(cli_pbx_config));
1876
1877         if (pbx_load_module())
1878                 return AST_MODULE_LOAD_DECLINE;
1879
1880         return AST_MODULE_LOAD_SUCCESS;
1881 }
1882
1883 static int reload(void)
1884 {
1885         if (clearglobalvars_config)
1886                 pbx_builtin_clear_globals();
1887         return pbx_load_module();
1888 }
1889
1890 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Text Extension Configuration",
1891                 .load = load_module,
1892                 .unload = unload_module,
1893                 .reload = reload,
1894                );