00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include "asterisk.h"
00031
00032 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 371592 $")
00033
00034 #include <ctype.h>
00035
00036 #include "asterisk/paths.h"
00037 #include "asterisk/pbx.h"
00038 #include "asterisk/config.h"
00039 #include "asterisk/module.h"
00040 #include "asterisk/logger.h"
00041 #include "asterisk/cli.h"
00042 #include "asterisk/channel.h"
00043 #include "asterisk/callerid.h"
00044
00045 static const char config[] = "extensions.conf";
00046 static const char registrar[] = "pbx_config";
00047 static char userscontext[AST_MAX_EXTENSION] = "default";
00048
00049 static int static_config = 0;
00050 static int write_protect_config = 1;
00051 static int autofallthrough_config = 1;
00052 static int clearglobalvars_config = 0;
00053 static int extenpatternmatchnew_config = 0;
00054 static char *overrideswitch_config = NULL;
00055
00056 AST_MUTEX_DEFINE_STATIC(save_dialplan_lock);
00057
00058 AST_MUTEX_DEFINE_STATIC(reload_lock);
00059
00060 static struct ast_context *local_contexts = NULL;
00061 static struct ast_hashtab *local_table = NULL;
00062
00063
00064
00065 static char *complete_dialplan_remove_include(struct ast_cli_args *);
00066 static char *complete_dialplan_add_include(struct ast_cli_args *);
00067 static char *complete_dialplan_remove_ignorepat(struct ast_cli_args *);
00068 static char *complete_dialplan_add_ignorepat(struct ast_cli_args *);
00069 static char *complete_dialplan_remove_extension(struct ast_cli_args *);
00070 static char *complete_dialplan_add_extension(struct ast_cli_args *);
00071 static char *complete_dialplan_remove_context(struct ast_cli_args *);
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081 static char *handle_cli_dialplan_remove_context(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
00082 {
00083 struct ast_context *con;
00084
00085 switch (cmd) {
00086 case CLI_INIT:
00087 e->command = "dialplan remove context";
00088 e->usage =
00089 "Usage: dialplan remove context <context>\n"
00090 " Removes all extensions from a specified context.\n";
00091 return NULL;
00092 case CLI_GENERATE:
00093 return complete_dialplan_remove_context(a);
00094 }
00095
00096 if (a->argc != 4) {
00097 return CLI_SHOWUSAGE;
00098 }
00099
00100 con = ast_context_find(a->argv[3]);
00101
00102 if (!con) {
00103 ast_cli(a->fd, "There is no such context as '%s'\n",
00104 a->argv[3]);
00105 return CLI_SUCCESS;
00106 } else {
00107 ast_context_destroy(con, registrar);
00108 ast_cli(a->fd, "Removing context '%s'\n",
00109 a->argv[3]);
00110 return CLI_SUCCESS;
00111 }
00112 }
00113
00114
00115
00116 static char *handle_cli_dialplan_remove_include(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
00117 {
00118 switch (cmd) {
00119 case CLI_INIT:
00120 e->command = "dialplan remove include";
00121 e->usage =
00122 "Usage: dialplan remove include <context> from <context>\n"
00123 " Remove an included context from another context.\n";
00124 return NULL;
00125 case CLI_GENERATE:
00126 return complete_dialplan_remove_include(a);
00127 }
00128
00129 if (a->argc != 6 || strcmp(a->argv[4], "from"))
00130 return CLI_SHOWUSAGE;
00131
00132 if (!ast_context_remove_include(a->argv[5], a->argv[3], registrar)) {
00133 ast_cli(a->fd, "We are not including '%s' into '%s' now\n",
00134 a->argv[3], a->argv[5]);
00135 return CLI_SUCCESS;
00136 }
00137
00138 ast_cli(a->fd, "Failed to remove '%s' include from '%s' context\n",
00139 a->argv[3], a->argv[5]);
00140 return CLI_FAILURE;
00141 }
00142
00143
00144 static int lookup_ci(struct ast_context *c, const char *name)
00145 {
00146 struct ast_include *i = NULL;
00147
00148 if (ast_rdlock_context(c))
00149 return 0;
00150 while ( (i = ast_walk_context_includes(c, i)) )
00151 if (!strcmp(name, ast_get_include_name(i)))
00152 break;
00153 ast_unlock_context(c);
00154 return i ? -1 : 0;
00155 }
00156
00157
00158 static int lookup_c_ip(struct ast_context *c, const char *name)
00159 {
00160 struct ast_ignorepat *ip = NULL;
00161
00162 if (ast_rdlock_context(c))
00163 return 0;
00164 while ( (ip = ast_walk_context_ignorepats(c, ip)) )
00165 if (!strcmp(name, ast_get_ignorepat_name(ip)))
00166 break;
00167 ast_unlock_context(c);
00168 return ip ? -1 : 0;
00169 }
00170
00171
00172 static const char *skip_words(const char *p, int n)
00173 {
00174 int in_blank = 0;
00175 for (;n && *p; p++) {
00176 if (isblank(*p) && !in_blank) {
00177 n--;
00178 in_blank = 1;
00179 } else if ( in_blank) {
00180 in_blank = 0;
00181 }
00182 }
00183 return p;
00184 }
00185
00186
00187 static int partial_match(const char *s, const char *word, int len)
00188 {
00189 return (len == 0 || !strncmp(s, word, len));
00190 }
00191
00192
00193
00194
00195 static int split_ec(const char *src, char **ext, char ** const ctx, char ** const cid)
00196 {
00197 char *i, *c, *e = ast_strdup(src);
00198
00199 if (e == NULL)
00200 return -1;
00201
00202 *ext = e;
00203 c = strchr(e, '@');
00204 if (c == NULL)
00205 *ctx = "";
00206 else {
00207 *c++ = '\0';
00208 *ctx = c;
00209 if (strchr(c, '@')) {
00210 free(e);
00211 return -1;
00212 }
00213 }
00214 if (cid && (i = strchr(e, '/'))) {
00215 *i++ = '\0';
00216 *cid = i;
00217 } else if (cid) {
00218
00219 *cid = NULL;
00220 }
00221 return 0;
00222 }
00223
00224
00225 static char *complete_dialplan_remove_include(struct ast_cli_args *a)
00226 {
00227 int which = 0;
00228 char *res = NULL;
00229 int len = strlen(a->word);
00230 struct ast_context *c = NULL;
00231
00232 if (a->pos == 3) {
00233 if (ast_wrlock_contexts()) {
00234 ast_log(LOG_ERROR, "Failed to lock context list\n");
00235 return NULL;
00236 }
00237
00238 while (!res && (c = ast_walk_contexts(c))) {
00239 struct ast_include *i = NULL;
00240
00241 if (ast_rdlock_context(c))
00242 continue;
00243
00244 while ( !res && (i = ast_walk_context_includes(c, i)) ) {
00245 const char *i_name = ast_get_include_name(i);
00246 struct ast_context *nc = NULL;
00247 int already_served = 0;
00248
00249 if (!partial_match(i_name, a->word, len))
00250 continue;
00251
00252
00253
00254
00255
00256
00257 while ( (nc = ast_walk_contexts(nc)) && nc != c && !already_served)
00258 already_served = lookup_ci(nc, i_name);
00259
00260 if (!already_served && ++which > a->n)
00261 res = strdup(i_name);
00262 }
00263 ast_unlock_context(c);
00264 }
00265
00266 ast_unlock_contexts();
00267 return res;
00268 } else if (a->pos == 4) {
00269
00270
00271
00272
00273 char *context, *dupline;
00274 const char *s = skip_words(a->line, 3);
00275
00276 if (a->n > 0)
00277 return NULL;
00278 context = dupline = strdup(s);
00279 if (!dupline) {
00280 ast_log(LOG_ERROR, "Out of free memory\n");
00281 return NULL;
00282 }
00283 strsep(&dupline, " ");
00284
00285 if (ast_rdlock_contexts()) {
00286 ast_log(LOG_ERROR, "Failed to lock contexts list\n");
00287 free(context);
00288 return NULL;
00289 }
00290
00291
00292 while (!res && (c = ast_walk_contexts(c)))
00293 if (lookup_ci(c, context))
00294 res = strdup("from");
00295 ast_unlock_contexts();
00296 if (!res)
00297 ast_log(LOG_WARNING, "%s not included anywhere\n", context);
00298 free(context);
00299 return res;
00300 } else if (a->pos == 5) {
00301
00302
00303
00304 char *context, *dupline, *from;
00305 const char *s = skip_words(a->line, 3);
00306 context = dupline = strdup(s);
00307 if (!dupline) {
00308 ast_log(LOG_ERROR, "Out of free memory\n");
00309 return NULL;
00310 }
00311
00312 strsep(&dupline, " ");
00313
00314
00315 from = strsep(&dupline, " ");
00316 if (!from || strcmp(from, "from")) {
00317 free(context);
00318 return NULL;
00319 }
00320
00321 if (ast_rdlock_contexts()) {
00322 ast_log(LOG_ERROR, "Failed to lock context list\n");
00323 free(context);
00324 return NULL;
00325 }
00326
00327
00328 c = NULL;
00329 while ( !res && (c = ast_walk_contexts(c))) {
00330 const char *c_name = ast_get_context_name(c);
00331 if (!partial_match(c_name, a->word, len))
00332 continue;
00333
00334 if (lookup_ci(c, context) && ++which > a->n)
00335 res = strdup(c_name);
00336 }
00337 ast_unlock_contexts();
00338 free(context);
00339 return res;
00340 }
00341
00342 return NULL;
00343 }
00344
00345
00346
00347
00348 static char *handle_cli_dialplan_remove_extension(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
00349 {
00350 int removing_priority = 0;
00351 char *exten, *context, *cid;
00352 char *ret = CLI_FAILURE;
00353
00354 switch (cmd) {
00355 case CLI_INIT:
00356 e->command = "dialplan remove extension";
00357 e->usage =
00358 "Usage: dialplan remove extension exten[/cid]@context [priority]\n"
00359 " Remove an extension from a given context. If a priority\n"
00360 " is given, only that specific priority from the given extension\n"
00361 " will be removed.\n";
00362 return NULL;
00363 case CLI_GENERATE:
00364 return complete_dialplan_remove_extension(a);
00365 }
00366
00367 if (a->argc != 5 && a->argc != 4)
00368 return CLI_SHOWUSAGE;
00369
00370
00371
00372
00373 if (a->argc == 5) {
00374 const char *c = a->argv[4];
00375
00376
00377
00378
00379
00380 if (!strcmp("hint", c))
00381 removing_priority = PRIORITY_HINT;
00382 else {
00383 while (*c && isdigit(*c))
00384 c++;
00385 if (*c) {
00386 ast_cli(a->fd, "Invalid priority '%s'\n", a->argv[4]);
00387 return CLI_FAILURE;
00388 }
00389 removing_priority = atoi(a->argv[4]);
00390 }
00391
00392 if (removing_priority == 0) {
00393 ast_cli(a->fd, "If you want to remove whole extension, please " \
00394 "omit priority argument\n");
00395 return CLI_FAILURE;
00396 }
00397 }
00398
00399
00400
00401
00402
00403 if (split_ec(a->argv[3], &exten, &context, &cid))
00404 return CLI_FAILURE;
00405 if ((!strlen(exten)) || (!(strlen(context)))) {
00406 ast_cli(a->fd, "Missing extension or context name in third argument '%s'\n",
00407 a->argv[3]);
00408 free(exten);
00409 return CLI_FAILURE;
00410 }
00411
00412 if (!ast_context_remove_extension_callerid(context, exten, removing_priority,
00413
00414 cid ? cid : (removing_priority ? "" : NULL), cid ? 1 : 0, registrar)) {
00415 if (!removing_priority)
00416 ast_cli(a->fd, "Whole extension %s@%s removed\n",
00417 exten, context);
00418 else
00419 ast_cli(a->fd, "Extension %s@%s with priority %d removed\n",
00420 exten, context, removing_priority);
00421
00422 ret = CLI_SUCCESS;
00423 } else {
00424 if (cid) {
00425 ast_cli(a->fd, "Failed to remove extension %s/%s@%s\n", exten, cid, context);
00426 } else {
00427 ast_cli(a->fd, "Failed to remove extension %s@%s\n", exten, context);
00428 }
00429 ret = CLI_FAILURE;
00430 }
00431 free(exten);
00432 return ret;
00433 }
00434
00435 static char *complete_dialplan_remove_extension(struct ast_cli_args *a)
00436 {
00437 char *ret = NULL;
00438 int which = 0;
00439
00440 if (a->pos == 3) {
00441 struct ast_context *c = NULL;
00442 char *context = NULL, *exten = NULL, *cid = NULL;
00443 int le = 0;
00444 int lc = 0;
00445 int lcid = 0;
00446
00447 lc = split_ec(a->word, &exten, &context, &cid);
00448 if (lc) {
00449 return NULL;
00450 }
00451 le = strlen(exten);
00452 lc = strlen(context);
00453 lcid = cid ? strlen(cid) : -1;
00454
00455 if (ast_rdlock_contexts()) {
00456 ast_log(LOG_ERROR, "Failed to lock context list\n");
00457 goto error2;
00458 }
00459
00460
00461 while ( (c = ast_walk_contexts(c)) ) {
00462 struct ast_exten *e = NULL;
00463
00464 if (!partial_match(ast_get_context_name(c), context, lc))
00465 continue;
00466 while ( (e = ast_walk_context_extensions(c, e)) ) {
00467 if ( !strchr(a->word, '/') ||
00468 (!strchr(a->word, '@') && partial_match(ast_get_extension_cidmatch(e), cid, lcid)) ||
00469 (strchr(a->word, '@') && !strcmp(ast_get_extension_cidmatch(e), cid))) {
00470 if ( ((strchr(a->word, '/') || strchr(a->word, '@')) && !strcmp(ast_get_extension_name(e), exten)) ||
00471 (!strchr(a->word, '/') && !strchr(a->word, '@') && partial_match(ast_get_extension_name(e), exten, le))) {
00472 if (++which > a->n) {
00473
00474 if (ast_get_extension_matchcid(e) && (!strchr(a->word, '@') || strchr(a->word, '/'))) {
00475 if (ast_asprintf(&ret, "%s/%s@%s", ast_get_extension_name(e), ast_get_extension_cidmatch(e), ast_get_context_name(c)) < 0) {
00476 ret = NULL;
00477 }
00478 break;
00479 } else if (!ast_get_extension_matchcid(e) && !strchr(a->word, '/')) {
00480 if (ast_asprintf(&ret, "%s@%s", ast_get_extension_name(e), ast_get_context_name(c)) < 0) {
00481 ret = NULL;
00482 }
00483 break;
00484 }
00485 }
00486 }
00487 }
00488 }
00489 if (e)
00490 break;
00491 }
00492
00493 ast_unlock_contexts();
00494 error2:
00495 free(exten);
00496 } else if (a->pos == 4) {
00497 char *exten = NULL, *context, *cid, *p;
00498 struct ast_context *c;
00499 int le, lc, len;
00500 const char *s = skip_words(a->line, 3);
00501 int i = split_ec(s, &exten, &context, &cid);
00502
00503 if (i)
00504 goto error3;
00505 if ( (p = strchr(exten, ' ')) )
00506 *p = '\0';
00507 if ( (p = strchr(context, ' ')) )
00508 *p = '\0';
00509 le = strlen(exten);
00510 lc = strlen(context);
00511 len = strlen(a->word);
00512 if (le == 0 || lc == 0)
00513 goto error3;
00514
00515 if (ast_rdlock_contexts()) {
00516 ast_log(LOG_ERROR, "Failed to lock context list\n");
00517 goto error3;
00518 }
00519
00520
00521 c = NULL;
00522 while ( (c = ast_walk_contexts(c)) ) {
00523
00524 struct ast_exten *e;
00525 if (strcmp(ast_get_context_name(c), context) != 0)
00526 continue;
00527
00528 e = NULL;
00529 while ( (e = ast_walk_context_extensions(c, e)) ) {
00530 struct ast_exten *priority;
00531 char buffer[10];
00532
00533 if (cid && strcmp(ast_get_extension_cidmatch(e), cid) != 0) {
00534 continue;
00535 }
00536 if (strcmp(ast_get_extension_name(e), exten) != 0)
00537 continue;
00538
00539 priority = NULL;
00540 while ( !ret && (priority = ast_walk_extension_priorities(e, priority)) ) {
00541 snprintf(buffer, sizeof(buffer), "%u", ast_get_extension_priority(priority));
00542 if (partial_match(buffer, a->word, len) && ++which > a->n)
00543 ret = strdup(buffer);
00544 }
00545 break;
00546 }
00547 break;
00548 }
00549 ast_unlock_contexts();
00550 error3:
00551 free(exten);
00552 }
00553 return ret;
00554 }
00555
00556
00557
00558
00559 static char *handle_cli_dialplan_add_include(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
00560 {
00561 const char *into_context;
00562
00563 switch (cmd) {
00564 case CLI_INIT:
00565 e->command = "dialplan add include";
00566 e->usage =
00567 "Usage: dialplan add include <context> into <context>\n"
00568 " Include a context in another context.\n";
00569 return NULL;
00570 case CLI_GENERATE:
00571 return complete_dialplan_add_include(a);
00572 }
00573
00574 if (a->argc != 6)
00575 return CLI_SHOWUSAGE;
00576
00577
00578 if (strcmp(a->argv[4], "into"))
00579 return CLI_SHOWUSAGE;
00580
00581 into_context = a->argv[5];
00582
00583 if (!ast_context_find(into_context)) {
00584 ast_cli(a->fd, "Context '%s' did not exist prior to add include - the context will be created.\n", into_context);
00585 }
00586
00587 if (!ast_context_find_or_create(NULL, NULL, into_context, registrar)) {
00588 ast_cli(a->fd, "ast_context_find_or_create() failed\n");
00589 ast_cli(a->fd, "Failed to include '%s' in '%s' context\n",a->argv[3], a->argv[5]);
00590 return CLI_FAILURE;
00591 }
00592
00593 if (ast_context_add_include(a->argv[5], a->argv[3], registrar)) {
00594 switch (errno) {
00595 case ENOMEM:
00596 ast_cli(a->fd, "Out of memory for context addition\n");
00597 break;
00598
00599 case EBUSY:
00600 ast_cli(a->fd, "Failed to lock context(s) list, please try again later\n");
00601 break;
00602
00603 case EEXIST:
00604 ast_cli(a->fd, "Context '%s' already included in '%s' context\n",
00605 a->argv[3], a->argv[5]);
00606 break;
00607
00608 case ENOENT:
00609 case EINVAL:
00610 ast_cli(a->fd, "There is no existence of context '%s'\n",
00611 errno == ENOENT ? a->argv[5] : a->argv[3]);
00612 break;
00613
00614 default:
00615 ast_cli(a->fd, "Failed to include '%s' in '%s' context\n",
00616 a->argv[3], a->argv[5]);
00617 break;
00618 }
00619 return CLI_FAILURE;
00620 }
00621
00622
00623 ast_cli(a->fd, "Context '%s' included in '%s' context\n",
00624 a->argv[3], a->argv[5]);
00625
00626 return CLI_SUCCESS;
00627 }
00628
00629 static char *complete_dialplan_add_include(struct ast_cli_args *a)
00630 {
00631 struct ast_context *c;
00632 int which = 0;
00633 char *ret = NULL;
00634 int len = strlen(a->word);
00635
00636 if (a->pos == 3) {
00637 if (ast_rdlock_contexts()) {
00638 ast_log(LOG_ERROR, "Failed to lock context list\n");
00639 return NULL;
00640 }
00641 for (c = NULL; !ret && (c = ast_walk_contexts(c)); )
00642 if (partial_match(ast_get_context_name(c), a->word, len) && ++which > a->n)
00643 ret = strdup(ast_get_context_name(c));
00644 ast_unlock_contexts();
00645 return ret;
00646 } else if (a->pos == 4) {
00647
00648 return (a->n == 0) ? strdup("into") : NULL;
00649 } else if (a->pos == 5) {
00650 char *context, *dupline, *into;
00651 const char *s = skip_words(a->line, 3);
00652 context = dupline = strdup(s);
00653
00654 if (!dupline) {
00655 ast_log(LOG_ERROR, "Out of free memory\n");
00656 return NULL;
00657 }
00658
00659 strsep(&dupline, " ");
00660 into = strsep(&dupline, " ");
00661
00662 if (!strlen(context) || strcmp(into, "into")) {
00663 ast_log(LOG_ERROR, "bad context %s or missing into %s\n",
00664 context, into);
00665 goto error3;
00666 }
00667
00668 if (ast_rdlock_contexts()) {
00669 ast_log(LOG_ERROR, "Failed to lock context list\n");
00670 goto error3;
00671 }
00672
00673 for (c = NULL; !ret && (c = ast_walk_contexts(c)); ) {
00674 if (!strcmp(context, ast_get_context_name(c)))
00675 continue;
00676 if (partial_match(ast_get_context_name(c), a->word, len) &&
00677 !lookup_ci(c, context) &&
00678 ++which > a->n) {
00679 ret = strdup(ast_get_context_name(c));
00680 }
00681 }
00682 ast_unlock_contexts();
00683 error3:
00684 free(context);
00685 return ret;
00686 }
00687
00688 return NULL;
00689 }
00690
00691
00692
00693
00694 static char *handle_cli_dialplan_save(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
00695 {
00696 char filename[256], overrideswitch[256] = "";
00697 struct ast_context *c;
00698 struct ast_config *cfg;
00699 struct ast_variable *v;
00700 int incomplete = 0;
00701 FILE *output;
00702 struct ast_flags config_flags = { 0 };
00703 const char *base, *slash;
00704
00705 switch (cmd) {
00706 case CLI_INIT:
00707 e->command = "dialplan save";
00708 e->usage =
00709 "Usage: dialplan save [/path/to/extension/file]\n"
00710 " Save dialplan created by pbx_config module.\n"
00711 "\n"
00712 "Example: dialplan save (/etc/asterisk/extensions.conf)\n"
00713 " dialplan save /home/markster (/home/markster/extensions.conf)\n";
00714 return NULL;
00715 case CLI_GENERATE:
00716 return NULL;
00717 }
00718
00719 if (! (static_config && !write_protect_config)) {
00720 ast_cli(a->fd,
00721 "I can't save dialplan now, see '%s' example file.\n",
00722 config);
00723 return CLI_FAILURE;
00724 }
00725
00726 if (a->argc != 2 && a->argc != 3)
00727 return CLI_SHOWUSAGE;
00728
00729 if (ast_mutex_lock(&save_dialplan_lock)) {
00730 ast_cli(a->fd,
00731 "Failed to lock dialplan saving (another proccess saving?)\n");
00732 return CLI_FAILURE;
00733 }
00734
00735
00736
00737 if (a->argc == 3) {
00738 base = a->argv[2];
00739 if (!strstr(a->argv[2], ".conf")) {
00740
00741 slash = (*(a->argv[2] + strlen(a->argv[2]) -1) == '/') ? "/" : "";
00742 } else {
00743 slash = "";
00744 }
00745 } else {
00746
00747 base = ast_config_AST_CONFIG_DIR;
00748 slash = "/";
00749 }
00750 snprintf(filename, sizeof(filename), "%s%s%s", base, slash, config);
00751
00752 cfg = ast_config_load("extensions.conf", config_flags);
00753 if (!cfg) {
00754 ast_cli(a->fd, "Failed to load extensions.conf\n");
00755 ast_mutex_unlock(&save_dialplan_lock);
00756 return CLI_FAILURE;
00757 }
00758
00759
00760 if (ast_rdlock_contexts()) {
00761 ast_cli(a->fd, "Failed to lock contexts list\n");
00762 ast_mutex_unlock(&save_dialplan_lock);
00763 ast_config_destroy(cfg);
00764 return CLI_FAILURE;
00765 }
00766
00767
00768 if (!(output = fopen(filename, "wt"))) {
00769 ast_cli(a->fd, "Failed to create file '%s'\n",
00770 filename);
00771 ast_unlock_contexts();
00772 ast_mutex_unlock(&save_dialplan_lock);
00773 ast_config_destroy(cfg);
00774 return CLI_FAILURE;
00775 }
00776
00777
00778 if (overrideswitch_config) {
00779 snprintf(overrideswitch, sizeof(overrideswitch), "overrideswitch=%s\n", overrideswitch_config);
00780 }
00781 fprintf(output, "[general]\nstatic=%s\nwriteprotect=%s\nautofallthrough=%s\nclearglobalvars=%s\n%sextenpatternmatchnew=%s\n\n",
00782 static_config ? "yes" : "no",
00783 write_protect_config ? "yes" : "no",
00784 autofallthrough_config ? "yes" : "no",
00785 clearglobalvars_config ? "yes" : "no",
00786 overrideswitch_config ? overrideswitch : "",
00787 extenpatternmatchnew_config ? "yes" : "no");
00788
00789 if ((v = ast_variable_browse(cfg, "globals"))) {
00790 fprintf(output, "[globals]\n");
00791 while(v) {
00792 fprintf(output, "%s => %s\n", v->name, v->value);
00793 v = v->next;
00794 }
00795 fprintf(output, "\n");
00796 }
00797
00798 ast_config_destroy(cfg);
00799
00800 #define PUT_CTX_HDR do { \
00801 if (!context_header_written) { \
00802 fprintf(output, "[%s]\n", ast_get_context_name(c)); \
00803 context_header_written = 1; \
00804 } \
00805 } while (0)
00806
00807
00808 for (c = NULL; (c = ast_walk_contexts(c)); ) {
00809 int context_header_written = 0;
00810 struct ast_exten *ext, *last_written_e = NULL;
00811 struct ast_include *i;
00812 struct ast_ignorepat *ip;
00813 struct ast_sw *sw;
00814
00815
00816 if (ast_rdlock_context(c)) {
00817 incomplete = 1;
00818 continue;
00819 }
00820
00821
00822 if (!strcmp(ast_get_context_registrar(c), registrar)) {
00823 fprintf(output, "[%s]\n", ast_get_context_name(c));
00824 context_header_written = 1;
00825 }
00826
00827
00828 for (ext = NULL; (ext = ast_walk_context_extensions(c, ext)); ) {
00829 struct ast_exten *p = NULL;
00830
00831
00832 while ( (p = ast_walk_extension_priorities(ext, p)) ) {
00833 if (strcmp(ast_get_extension_registrar(p), registrar) != 0)
00834 continue;
00835
00836
00837 if (last_written_e != NULL &&
00838 strcmp(ast_get_extension_name(last_written_e),
00839 ast_get_extension_name(p)))
00840 fprintf(output, "\n");
00841 last_written_e = p;
00842
00843 PUT_CTX_HDR;
00844
00845 if (ast_get_extension_priority(p) == PRIORITY_HINT) {
00846 fprintf(output, "exten => %s,hint,%s\n",
00847 ast_get_extension_name(p),
00848 ast_get_extension_app(p));
00849 } else {
00850 const char *sep, *cid;
00851 const char *el = ast_get_extension_label(p);
00852 char label[128] = "";
00853
00854 if (ast_get_extension_matchcid(p)) {
00855 sep = "/";
00856 cid = ast_get_extension_cidmatch(p);
00857 } else
00858 sep = cid = "";
00859
00860 if (el && (snprintf(label, sizeof(label), "(%s)", el) != (strlen(el) + 2)))
00861 incomplete = 1;
00862
00863 fprintf(output, "exten => %s%s%s,%d%s,%s(%s)\n",
00864 ast_get_extension_name(p), (ast_strlen_zero(sep) ? "" : sep), (ast_strlen_zero(cid) ? "" : cid),
00865 ast_get_extension_priority(p), label,
00866 ast_get_extension_app(p), (ast_strlen_zero(ast_get_extension_app_data(p)) ? "" : (const char *)ast_get_extension_app_data(p)));
00867 }
00868 }
00869 }
00870
00871
00872 if (last_written_e)
00873 fprintf(output, "\n");
00874
00875
00876 for (i = NULL; (i = ast_walk_context_includes(c, i)) ; ) {
00877 if (strcmp(ast_get_include_registrar(i), registrar) != 0)
00878 continue;
00879 PUT_CTX_HDR;
00880 fprintf(output, "include => %s\n", ast_get_include_name(i));
00881 }
00882 if (ast_walk_context_includes(c, NULL))
00883 fprintf(output, "\n");
00884
00885
00886 for (sw = NULL; (sw = ast_walk_context_switches(c, sw)) ; ) {
00887 if (strcmp(ast_get_switch_registrar(sw), registrar) != 0)
00888 continue;
00889 PUT_CTX_HDR;
00890 fprintf(output, "switch => %s/%s\n",
00891 ast_get_switch_name(sw), ast_get_switch_data(sw));
00892 }
00893
00894 if (ast_walk_context_switches(c, NULL))
00895 fprintf(output, "\n");
00896
00897
00898 for (ip = NULL; (ip = ast_walk_context_ignorepats(c, ip)); ) {
00899 if (strcmp(ast_get_ignorepat_registrar(ip), registrar) != 0)
00900 continue;
00901 PUT_CTX_HDR;
00902 fprintf(output, "ignorepat => %s\n",
00903 ast_get_ignorepat_name(ip));
00904 }
00905
00906 ast_unlock_context(c);
00907 }
00908
00909 ast_unlock_contexts();
00910 ast_mutex_unlock(&save_dialplan_lock);
00911 fclose(output);
00912
00913 if (incomplete) {
00914 ast_cli(a->fd, "Saved dialplan is incomplete\n");
00915 return CLI_FAILURE;
00916 }
00917
00918 ast_cli(a->fd, "Dialplan successfully saved into '%s'\n",
00919 filename);
00920 return CLI_SUCCESS;
00921 }
00922
00923
00924
00925
00926 static char *handle_cli_dialplan_add_extension(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
00927 {
00928 char *whole_exten;
00929 char *exten, *prior;
00930 int iprior = -2;
00931 char *cidmatch, *app, *app_data;
00932 char *start, *end;
00933 const char *into_context;
00934
00935 switch (cmd) {
00936 case CLI_INIT:
00937 e->command = "dialplan add extension";
00938 e->usage =
00939 "Usage: dialplan add extension <exten>,<priority>,<app> into <context> [replace]\n"
00940 "\n"
00941 " app can be either:\n"
00942 " app-name\n"
00943 " app-name(app-data)\n"
00944 " app-name,<app-data>\n"
00945 "\n"
00946 " This command will add the new extension into <context>. If\n"
00947 " an extension with the same priority already exists and the\n"
00948 " 'replace' option is given we will replace the extension.\n"
00949 "\n"
00950 "Example: dialplan add extension 6123,1,Dial,IAX/216.207.245.56/6123 into local\n"
00951 " Now, you can dial 6123 and talk to Markster :)\n";
00952 return NULL;
00953 case CLI_GENERATE:
00954 return complete_dialplan_add_extension(a);
00955 }
00956
00957
00958 if (a->argc != 6 && a->argc != 7)
00959 return CLI_SHOWUSAGE;
00960 if (strcmp(a->argv[4], "into"))
00961 return CLI_SHOWUSAGE;
00962 if (a->argc == 7)
00963 if (strcmp(a->argv[6], "replace"))
00964 return CLI_SHOWUSAGE;
00965
00966 whole_exten = ast_strdupa(a->argv[3]);
00967 exten = strsep(&whole_exten,",");
00968 if (strchr(exten, '/')) {
00969 cidmatch = exten;
00970 strsep(&cidmatch,"/");
00971 } else {
00972 cidmatch = NULL;
00973 }
00974 prior = strsep(&whole_exten,",");
00975 if (prior) {
00976 if (!strcmp(prior, "hint")) {
00977 iprior = PRIORITY_HINT;
00978 } else {
00979 if (sscanf(prior, "%30d", &iprior) != 1) {
00980 ast_cli(a->fd, "'%s' is not a valid priority\n", prior);
00981 prior = NULL;
00982 }
00983 }
00984 }
00985 app = whole_exten;
00986 if (app) {
00987 if ((start = strchr(app, '(')) && (end = strrchr(app, ')'))) {
00988 *start = *end = '\0';
00989 app_data = start + 1;
00990 } else {
00991 app_data = strchr(app, ',');
00992 if (app_data) {
00993 *app_data++ = '\0';
00994 }
00995 }
00996 } else {
00997 app_data = NULL;
00998 }
00999
01000 if (!exten || !prior || !app) {
01001 return CLI_SHOWUSAGE;
01002 }
01003
01004 if (!app_data) {
01005 app_data = "";
01006 }
01007 into_context = a->argv[5];
01008
01009 if (!ast_context_find(into_context)) {
01010 ast_cli(a->fd, "Context '%s' did not exist prior to add extension - the context will be created.\n", into_context);
01011 }
01012
01013 if (!ast_context_find_or_create(NULL, NULL, into_context, registrar)) {
01014 ast_cli(a->fd, "Failed to add '%s,%s,%s(%s)' extension into '%s' context\n",
01015 exten, prior, app, app_data, into_context);
01016 return CLI_FAILURE;
01017 }
01018
01019 if (ast_add_extension(into_context, a->argc == 7 ? 1 : 0, exten, iprior, NULL, cidmatch, app,
01020 ast_strdup(app_data), ast_free_ptr, registrar)) {
01021 switch (errno) {
01022 case ENOMEM:
01023 ast_cli(a->fd, "Out of free memory\n");
01024 break;
01025
01026 case EBUSY:
01027 ast_cli(a->fd, "Failed to lock context(s) list, please try again later\n");
01028 break;
01029
01030 case ENOENT:
01031 ast_cli(a->fd, "No existence of '%s' context\n", into_context);
01032 break;
01033
01034 case EEXIST:
01035 ast_cli(a->fd, "Extension %s@%s with priority %s already exists\n",
01036 exten, into_context, prior);
01037 break;
01038
01039 default:
01040 ast_cli(a->fd, "Failed to add '%s,%s,%s(%s)' extension into '%s' context\n",
01041 exten, prior, app, app_data, into_context);
01042 break;
01043 }
01044 return CLI_FAILURE;
01045 }
01046
01047 if (a->argc == 7) {
01048 ast_cli(a->fd, "Extension %s@%s (%s) replace by '%s,%s,%s(%s)'\n",
01049 exten, into_context, prior, exten, prior, app, app_data);
01050 } else {
01051 ast_cli(a->fd, "Extension '%s,%s,%s(%s)' added into '%s' context\n",
01052 exten, prior, app, app_data, into_context);
01053 }
01054
01055 return CLI_SUCCESS;
01056 }
01057
01058 static char *complete_dialplan_remove_context(struct ast_cli_args *a)
01059 {
01060 struct ast_context *c = NULL;
01061 int len = strlen(a->word);
01062 char *res = NULL;
01063 int which = 0;
01064
01065 if (a->pos != 3) {
01066 return NULL;
01067 }
01068
01069
01070
01071 if (ast_rdlock_contexts()) {
01072 ast_log(LOG_WARNING, "Failed to lock contexts list\n");
01073 return NULL;
01074 }
01075
01076
01077 while ( !res && (c = ast_walk_contexts(c)) ) {
01078 if (partial_match(ast_get_context_name(c), a->word, len) && ++which > a->n) {
01079 res = strdup(ast_get_context_name(c));
01080 }
01081 }
01082 ast_unlock_contexts();
01083 return res;
01084 }
01085
01086
01087 static char *complete_dialplan_add_extension(struct ast_cli_args *a)
01088 {
01089 int which = 0;
01090
01091 if (a->pos == 4) {
01092 return (a->n == 0) ? strdup("into") : NULL;
01093 } else if (a->pos == 5) {
01094 struct ast_context *c = NULL;
01095 int len = strlen(a->word);
01096 char *res = NULL;
01097
01098
01099 if (ast_rdlock_contexts()) {
01100 ast_log(LOG_WARNING, "Failed to lock contexts list\n");
01101 return NULL;
01102 }
01103
01104
01105 while ( !res && (c = ast_walk_contexts(c)) )
01106 if (partial_match(ast_get_context_name(c), a->word, len) && ++which > a->n)
01107 res = strdup(ast_get_context_name(c));
01108 ast_unlock_contexts();
01109 return res;
01110 } else if (a->pos == 6) {
01111 return a->n == 0 ? strdup("replace") : NULL;
01112 }
01113 return NULL;
01114 }
01115
01116
01117
01118
01119 static char *handle_cli_dialplan_add_ignorepat(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
01120 {
01121 switch (cmd) {
01122 case CLI_INIT:
01123 e->command = "dialplan add ignorepat";
01124 e->usage =
01125 "Usage: dialplan add ignorepat <pattern> into <context>\n"
01126 " This command adds a new ignore pattern into context <context>\n"
01127 "\n"
01128 "Example: dialplan add ignorepat _3XX into local\n";
01129 return NULL;
01130 case CLI_GENERATE:
01131 return complete_dialplan_add_ignorepat(a);
01132 }
01133
01134 if (a->argc != 6)
01135 return CLI_SHOWUSAGE;
01136
01137 if (strcmp(a->argv[4], "into"))
01138 return CLI_SHOWUSAGE;
01139
01140 if (ast_context_add_ignorepat(a->argv[5], a->argv[3], registrar)) {
01141 switch (errno) {
01142 case ENOMEM:
01143 ast_cli(a->fd, "Out of free memory\n");
01144 break;
01145
01146 case ENOENT:
01147 ast_cli(a->fd, "There is no existence of '%s' context\n", a->argv[5]);
01148 break;
01149
01150 case EEXIST:
01151 ast_cli(a->fd, "Ignore pattern '%s' already included in '%s' context\n",
01152 a->argv[3], a->argv[5]);
01153 break;
01154
01155 case EBUSY:
01156 ast_cli(a->fd, "Failed to lock context(s) list, please, try again later\n");
01157 break;
01158
01159 default:
01160 ast_cli(a->fd, "Failed to add ingore pattern '%s' into '%s' context\n",
01161 a->argv[3], a->argv[5]);
01162 break;
01163 }
01164 return CLI_FAILURE;
01165 }
01166
01167 ast_cli(a->fd, "Ignore pattern '%s' added into '%s' context\n",
01168 a->argv[3], a->argv[5]);
01169
01170 return CLI_SUCCESS;
01171 }
01172
01173 static char *complete_dialplan_add_ignorepat(struct ast_cli_args *a)
01174 {
01175 if (a->pos == 4)
01176 return a->n == 0 ? strdup("into") : NULL;
01177 else if (a->pos == 5) {
01178 struct ast_context *c;
01179 int which = 0;
01180 char *dupline, *ignorepat = NULL;
01181 const char *s;
01182 char *ret = NULL;
01183 int len = strlen(a->word);
01184
01185
01186 s = skip_words(a->line, 3);
01187 if (s == NULL)
01188 return NULL;
01189 dupline = strdup(s);
01190 if (!dupline) {
01191 ast_log(LOG_ERROR, "Malloc failure\n");
01192 return NULL;
01193 }
01194 ignorepat = strsep(&dupline, " ");
01195
01196 if (ast_rdlock_contexts()) {
01197 ast_log(LOG_ERROR, "Failed to lock contexts list\n");
01198 return NULL;
01199 }
01200
01201 for (c = NULL; !ret && (c = ast_walk_contexts(c));) {
01202 int found = 0;
01203
01204 if (!partial_match(ast_get_context_name(c), a->word, len))
01205 continue;
01206 if (ignorepat)
01207 found = lookup_c_ip(c, ignorepat);
01208 if (!found && ++which > a->n)
01209 ret = strdup(ast_get_context_name(c));
01210 }
01211
01212 free(ignorepat);
01213 ast_unlock_contexts();
01214 return ret;
01215 }
01216
01217 return NULL;
01218 }
01219
01220 static char *handle_cli_dialplan_remove_ignorepat(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
01221 {
01222 switch (cmd) {
01223 case CLI_INIT:
01224 e->command = "dialplan remove ignorepat";
01225 e->usage =
01226 "Usage: dialplan remove ignorepat <pattern> from <context>\n"
01227 " This command removes an ignore pattern from context <context>\n"
01228 "\n"
01229 "Example: dialplan remove ignorepat _3XX from local\n";
01230 return NULL;
01231 case CLI_GENERATE:
01232 return complete_dialplan_remove_ignorepat(a);
01233 }
01234
01235 if (a->argc != 6)
01236 return CLI_SHOWUSAGE;
01237
01238 if (strcmp(a->argv[4], "from"))
01239 return CLI_SHOWUSAGE;
01240
01241 if (ast_context_remove_ignorepat(a->argv[5], a->argv[3], registrar)) {
01242 switch (errno) {
01243 case EBUSY:
01244 ast_cli(a->fd, "Failed to lock context(s) list, please try again later\n");
01245 break;
01246
01247 case ENOENT:
01248 ast_cli(a->fd, "There is no existence of '%s' context\n", a->argv[5]);
01249 break;
01250
01251 case EINVAL:
01252 ast_cli(a->fd, "There is no existence of '%s' ignore pattern in '%s' context\n",
01253 a->argv[3], a->argv[5]);
01254 break;
01255
01256 default:
01257 ast_cli(a->fd, "Failed to remove ignore pattern '%s' from '%s' context\n",
01258 a->argv[3], a->argv[5]);
01259 break;
01260 }
01261 return CLI_FAILURE;
01262 }
01263
01264 ast_cli(a->fd, "Ignore pattern '%s' removed from '%s' context\n",
01265 a->argv[3], a->argv[5]);
01266 return CLI_SUCCESS;
01267 }
01268
01269 static char *complete_dialplan_remove_ignorepat(struct ast_cli_args *a)
01270 {
01271 struct ast_context *c;
01272 int which = 0;
01273 char *ret = NULL;
01274
01275 if (a->pos == 3) {
01276 int len = strlen(a->word);
01277 if (ast_rdlock_contexts()) {
01278 ast_log(LOG_WARNING, "Failed to lock contexts list\n");
01279 return NULL;
01280 }
01281
01282 for (c = NULL; !ret && (c = ast_walk_contexts(c));) {
01283 struct ast_ignorepat *ip;
01284
01285 if (ast_rdlock_context(c))
01286 continue;
01287
01288 for (ip = NULL; !ret && (ip = ast_walk_context_ignorepats(c, ip));) {
01289 if (partial_match(ast_get_ignorepat_name(ip), a->word, len) && ++which > a->n) {
01290
01291 struct ast_context *cw = NULL;
01292 int found = 0;
01293 while ( (cw = ast_walk_contexts(cw)) && cw != c && !found) {
01294
01295 found = lookup_c_ip(cw, ast_get_ignorepat_name(ip));
01296 }
01297 if (!found)
01298 ret = strdup(ast_get_ignorepat_name(ip));
01299 }
01300 }
01301 ast_unlock_context(c);
01302 }
01303 ast_unlock_contexts();
01304 return ret;
01305 } else if (a->pos == 4) {
01306 return a->n == 0 ? strdup("from") : NULL;
01307 } else if (a->pos == 5) {
01308 char *dupline, *duplinet, *ignorepat;
01309 int len = strlen(a->word);
01310
01311 dupline = strdup(a->line);
01312 if (!dupline) {
01313 ast_log(LOG_WARNING, "Out of free memory\n");
01314 return NULL;
01315 }
01316
01317 duplinet = dupline;
01318 strsep(&duplinet, " ");
01319 strsep(&duplinet, " ");
01320 ignorepat = strsep(&duplinet, " ");
01321
01322 if (!ignorepat) {
01323 free(dupline);
01324 return NULL;
01325 }
01326
01327 if (ast_rdlock_contexts()) {
01328 ast_log(LOG_WARNING, "Failed to lock contexts list\n");
01329 free(dupline);
01330 return NULL;
01331 }
01332
01333 for (c = NULL; !ret && (c = ast_walk_contexts(c)); ) {
01334 if (ast_rdlock_context(c))
01335 continue;
01336 if (!partial_match(ast_get_context_name(c), a->word, len))
01337 continue;
01338 if (lookup_c_ip(c, ignorepat) && ++which > a->n)
01339 ret = strdup(ast_get_context_name(c));
01340 ast_unlock_context(c);
01341 }
01342 ast_unlock_contexts();
01343 free(dupline);
01344 return NULL;
01345 }
01346
01347 return NULL;
01348 }
01349
01350 static int pbx_load_module(void);
01351
01352 static char *handle_cli_dialplan_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
01353 {
01354 switch (cmd) {
01355 case CLI_INIT:
01356 e->command = "dialplan reload";
01357 e->usage =
01358 "Usage: dialplan reload\n"
01359 " Reload extensions.conf without reloading any other\n"
01360 " modules. This command does not delete global variables\n"
01361 " unless clearglobalvars is set to yes in extensions.conf\n";
01362 return NULL;
01363 case CLI_GENERATE:
01364 return NULL;
01365 }
01366
01367 if (a->argc != 2)
01368 return CLI_SHOWUSAGE;
01369
01370 if (clearglobalvars_config)
01371 pbx_builtin_clear_globals();
01372
01373 pbx_load_module();
01374 ast_cli(a->fd, "Dialplan reloaded.\n");
01375 return CLI_SUCCESS;
01376 }
01377
01378
01379
01380
01381 static struct ast_cli_entry cli_pbx_config[] = {
01382 AST_CLI_DEFINE(handle_cli_dialplan_add_extension, "Add new extension into context"),
01383 AST_CLI_DEFINE(handle_cli_dialplan_remove_extension, "Remove a specified extension"),
01384 AST_CLI_DEFINE(handle_cli_dialplan_remove_context, "Remove a specified context"),
01385 AST_CLI_DEFINE(handle_cli_dialplan_add_ignorepat, "Add new ignore pattern"),
01386 AST_CLI_DEFINE(handle_cli_dialplan_remove_ignorepat, "Remove ignore pattern from context"),
01387 AST_CLI_DEFINE(handle_cli_dialplan_add_include, "Include context in other context"),
01388 AST_CLI_DEFINE(handle_cli_dialplan_remove_include, "Remove a specified include from context"),
01389 AST_CLI_DEFINE(handle_cli_dialplan_reload, "Reload extensions and *only* extensions"),
01390 AST_CLI_DEFINE(handle_cli_dialplan_save, "Save current dialplan into a file")
01391 };
01392
01393 static struct ast_cli_entry cli_dialplan_save =
01394 AST_CLI_DEFINE(handle_cli_dialplan_save, "Save dialplan");
01395
01396
01397
01398
01399 static int unload_module(void)
01400 {
01401 if (static_config && !write_protect_config)
01402 ast_cli_unregister(&cli_dialplan_save);
01403 if (overrideswitch_config) {
01404 ast_free(overrideswitch_config);
01405 }
01406 ast_cli_unregister_multiple(cli_pbx_config, ARRAY_LEN(cli_pbx_config));
01407 ast_context_destroy(NULL, registrar);
01408 return 0;
01409 }
01410
01411
01412
01413
01414
01415
01416 static char *pbx_strsep(char **destructible, const char *delim)
01417 {
01418 int square = 0;
01419 char *res;
01420
01421 if (!destructible || !*destructible) {
01422 return NULL;
01423 }
01424 res = *destructible;
01425 for (; **destructible; (*destructible)++) {
01426 if (**destructible == '[' && !strchr(delim, '[')) {
01427 square++;
01428 } else if (**destructible == ']' && !strchr(delim, ']')) {
01429 if (square) {
01430 square--;
01431 }
01432 } else if (**destructible == '\\' && !strchr(delim, '\\')) {
01433 (*destructible)++;
01434 } else if (strchr(delim, **destructible) && !square) {
01435 **destructible = '\0';
01436 (*destructible)++;
01437 break;
01438 }
01439 }
01440 if (**destructible == '\0') {
01441 *destructible = NULL;
01442 }
01443 return res;
01444 }
01445
01446 static int pbx_load_config(const char *config_file)
01447 {
01448 struct ast_config *cfg;
01449 char *end;
01450 char *label;
01451 #ifdef LOW_MEMORY
01452 char realvalue[256];
01453 #else
01454 char realvalue[8192];
01455 #endif
01456 int lastpri = -2;
01457 struct ast_context *con;
01458 struct ast_variable *v;
01459 const char *cxt;
01460 const char *aft;
01461 const char *newpm, *ovsw;
01462 struct ast_flags config_flags = { 0 };
01463 char lastextension[256];
01464 cfg = ast_config_load(config_file, config_flags);
01465 if (!cfg || cfg == CONFIG_STATUS_FILEINVALID)
01466 return 0;
01467
01468
01469 static_config = ast_true(ast_variable_retrieve(cfg, "general", "static"));
01470 write_protect_config = ast_true(ast_variable_retrieve(cfg, "general", "writeprotect"));
01471 if ((aft = ast_variable_retrieve(cfg, "general", "autofallthrough")))
01472 autofallthrough_config = ast_true(aft);
01473 if ((newpm = ast_variable_retrieve(cfg, "general", "extenpatternmatchnew")))
01474 extenpatternmatchnew_config = ast_true(newpm);
01475 clearglobalvars_config = ast_true(ast_variable_retrieve(cfg, "general", "clearglobalvars"));
01476 if ((ovsw = ast_variable_retrieve(cfg, "general", "overrideswitch"))) {
01477 if (overrideswitch_config) {
01478 ast_free(overrideswitch_config);
01479 }
01480 if (!ast_strlen_zero(ovsw)) {
01481 overrideswitch_config = ast_strdup(ovsw);
01482 } else {
01483 overrideswitch_config = NULL;
01484 }
01485 }
01486
01487 ast_copy_string(userscontext, ast_variable_retrieve(cfg, "general", "userscontext") ?: "default", sizeof(userscontext));
01488
01489 for (v = ast_variable_browse(cfg, "globals"); v; v = v->next) {
01490 pbx_substitute_variables_helper(NULL, v->value, realvalue, sizeof(realvalue) - 1);
01491 pbx_builtin_setvar_helper(NULL, v->name, realvalue);
01492 }
01493 for (cxt = ast_category_browse(cfg, NULL);
01494 cxt;
01495 cxt = ast_category_browse(cfg, cxt)) {
01496
01497 if (!strcasecmp(cxt, "general") || !strcasecmp(cxt, "globals")) {
01498 continue;
01499 }
01500 if (!(con = ast_context_find_or_create(&local_contexts, local_table, cxt, registrar))) {
01501 continue;
01502 }
01503
01504
01505 lastextension[0] = '\0';
01506 lastpri = -2;
01507
01508 for (v = ast_variable_browse(cfg, cxt); v; v = v->next) {
01509 char *tc = NULL;
01510 char realext[256] = "";
01511 char *stringp, *ext;
01512 const char *vfile;
01513
01514
01515 vfile = !*v->file ? config_file : v->file;
01516
01517 if (!strncasecmp(v->name, "same", 4)) {
01518 if (ast_strlen_zero(lastextension)) {
01519 ast_log(LOG_ERROR,
01520 "No previous pattern in the first entry of context '%s' to match '%s' at line %d of %s!\n",
01521 cxt, v->name, v->lineno, vfile);
01522 continue;
01523 }
01524 if ((stringp = tc = ast_strdup(v->value))) {
01525 ast_copy_string(realext, lastextension, sizeof(realext));
01526 goto process_extension;
01527 }
01528 } else if (!strcasecmp(v->name, "exten")) {
01529 int ipri;
01530 char *plus;
01531 char *pri, *appl, *data, *cidmatch;
01532
01533 if (!(stringp = tc = ast_strdup(v->value))) {
01534 continue;
01535 }
01536
01537 ext = S_OR(pbx_strsep(&stringp, ","), "");
01538 pbx_substitute_variables_helper(NULL, ext, realext, sizeof(realext) - 1);
01539 ast_copy_string(lastextension, realext, sizeof(lastextension));
01540 process_extension:
01541 ipri = -2;
01542 if ((cidmatch = strchr(realext, '/'))) {
01543 *cidmatch++ = '\0';
01544 ast_shrink_phone_number(cidmatch);
01545 }
01546 pri = ast_strip(S_OR(strsep(&stringp, ","), ""));
01547 if ((label = strchr(pri, '('))) {
01548 *label++ = '\0';
01549 if ((end = strchr(label, ')'))) {
01550 *end = '\0';
01551 } else {
01552 ast_log(LOG_WARNING,
01553 "Label missing trailing ')' at line %d of %s\n",
01554 v->lineno, vfile);
01555 ast_free(tc);
01556 continue;
01557 }
01558 }
01559 if ((plus = strchr(pri, '+'))) {
01560 *plus++ = '\0';
01561 }
01562 if (!strcmp(pri,"hint")) {
01563 ipri = PRIORITY_HINT;
01564 } else if (!strcmp(pri, "next") || !strcmp(pri, "n")) {
01565 if (lastpri > -2) {
01566 ipri = lastpri + 1;
01567 } else {
01568 ast_log(LOG_WARNING,
01569 "Can't use 'next' priority on the first entry at line %d of %s!\n",
01570 v->lineno, vfile);
01571 ast_free(tc);
01572 continue;
01573 }
01574 } else if (!strcmp(pri, "same") || !strcmp(pri, "s")) {
01575 if (lastpri > -2) {
01576 ipri = lastpri;
01577 } else {
01578 ast_log(LOG_WARNING,
01579 "Can't use 'same' priority on the first entry at line %d of %s!\n",
01580 v->lineno, vfile);
01581 ast_free(tc);
01582 continue;
01583 }
01584 } else if (sscanf(pri, "%30d", &ipri) != 1 &&
01585 (ipri = ast_findlabel_extension2(NULL, con, realext, pri, cidmatch)) < 1) {
01586 ast_log(LOG_WARNING,
01587 "Invalid priority/label '%s' at line %d of %s\n",
01588 pri, v->lineno, vfile);
01589 ipri = 0;
01590 ast_free(tc);
01591 continue;
01592 } else if (ipri < 1) {
01593 ast_log(LOG_WARNING, "Invalid priority '%s' at line %d of %s\n",
01594 pri, v->lineno, vfile);
01595 ast_free(tc);
01596 continue;
01597 }
01598 appl = S_OR(stringp, "");
01599
01600 if (!strchr(appl, '(')) {
01601
01602 data = "";
01603 } else {
01604 char *orig_appl = ast_strdup(appl);
01605
01606 if (!orig_appl) {
01607 ast_free(tc);
01608 continue;
01609 }
01610
01611 appl = strsep(&stringp, "(");
01612
01613
01614 if (strstr(appl, "${") || strstr(appl, "$[")){
01615
01616 strcpy(appl, orig_appl);
01617
01618 data = "";
01619
01620 } else {
01621 data = S_OR(stringp, "");
01622 if ((end = strrchr(data, ')'))) {
01623 *end = '\0';
01624 } else {
01625 ast_log(LOG_WARNING,
01626 "No closing parenthesis found? '%s(%s' at line %d of %s\n",
01627 appl, data, v->lineno, vfile);
01628 }
01629 }
01630 ast_free(orig_appl);
01631 }
01632
01633 appl = ast_skip_blanks(appl);
01634 if (ipri) {
01635 if (plus) {
01636 ipri += atoi(plus);
01637 }
01638 lastpri = ipri;
01639 if (!ast_opt_dont_warn && (!strcmp(realext, "_.") || !strcmp(realext, "_!"))) {
01640 ast_log(LOG_WARNING,
01641 "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",
01642 realext, realext[1], v->lineno, vfile);
01643 }
01644 if (ast_add_extension2(con, 0, realext, ipri, label, cidmatch, appl, ast_strdup(data), ast_free_ptr, registrar)) {
01645 ast_log(LOG_WARNING,
01646 "Unable to register extension at line %d of %s\n",
01647 v->lineno, vfile);
01648 }
01649 }
01650 ast_free(tc);
01651 } else if (!strcasecmp(v->name, "include")) {
01652 pbx_substitute_variables_helper(NULL, v->value, realvalue, sizeof(realvalue) - 1);
01653 if (ast_context_add_include2(con, realvalue, registrar)) {
01654 switch (errno) {
01655 case ENOMEM:
01656 ast_log(LOG_WARNING, "Out of memory for context addition\n");
01657 break;
01658
01659 case EBUSY:
01660 ast_log(LOG_WARNING, "Failed to lock context(s) list, please try again later\n");
01661 break;
01662
01663 case EEXIST:
01664 ast_log(LOG_WARNING,
01665 "Context '%s' already included in '%s' context on include at line %d of %s\n",
01666 v->value, cxt, v->lineno, vfile);
01667 break;
01668
01669 case ENOENT:
01670 case EINVAL:
01671 ast_log(LOG_WARNING,
01672 "There is no existence of context '%s' included at line %d of %s\n",
01673 errno == ENOENT ? v->value : cxt, v->lineno, vfile);
01674 break;
01675
01676 default:
01677 ast_log(LOG_WARNING,
01678 "Failed to include '%s' in '%s' context at line %d of %s\n",
01679 v->value, cxt, v->lineno, vfile);
01680 break;
01681 }
01682 }
01683 } else if (!strcasecmp(v->name, "ignorepat")) {
01684 pbx_substitute_variables_helper(NULL, v->value, realvalue, sizeof(realvalue) - 1);
01685 if (ast_context_add_ignorepat2(con, realvalue, registrar)) {
01686 ast_log(LOG_WARNING,
01687 "Unable to include ignorepat '%s' in context '%s' at line %d of %s\n",
01688 v->value, cxt, v->lineno, vfile);
01689 }
01690 } else if (!strcasecmp(v->name, "switch") || !strcasecmp(v->name, "lswitch") || !strcasecmp(v->name, "eswitch")) {
01691 char *appl, *data;
01692 stringp = realvalue;
01693
01694 if (!strcasecmp(v->name, "switch")) {
01695 pbx_substitute_variables_helper(NULL, v->value, realvalue, sizeof(realvalue) - 1);
01696 } else {
01697 ast_copy_string(realvalue, v->value, sizeof(realvalue));
01698 }
01699 appl = strsep(&stringp, "/");
01700 data = S_OR(stringp, "");
01701 if (ast_context_add_switch2(con, appl, data, !strcasecmp(v->name, "eswitch"), registrar)) {
01702 ast_log(LOG_WARNING,
01703 "Unable to include switch '%s' in context '%s' at line %d of %s\n",
01704 v->value, cxt, v->lineno, vfile);
01705 }
01706 } else {
01707 ast_log(LOG_WARNING,
01708 "==!!== Unknown directive: %s at line %d of %s -- IGNORING!!!\n",
01709 v->name, v->lineno, vfile);
01710 }
01711 }
01712 }
01713 ast_config_destroy(cfg);
01714 return 1;
01715 }
01716
01717 static void append_interface(char *iface, int maxlen, char *add)
01718 {
01719 int len = strlen(iface);
01720 if (strlen(add) + len < maxlen - 2) {
01721 if (strlen(iface)) {
01722 iface[len] = '&';
01723 strcpy(iface + len + 1, add);
01724 } else
01725 strcpy(iface, add);
01726 }
01727 }
01728
01729 static void pbx_load_users(void)
01730 {
01731 struct ast_config *cfg;
01732 char *cat, *chan;
01733 const char *dahdichan;
01734 const char *hasexten, *altexts;
01735 char tmp[256];
01736 char iface[256];
01737 char dahdicopy[256];
01738 char *ext, altcopy[256];
01739 char *c;
01740 int hasvoicemail;
01741 int start, finish, x;
01742 struct ast_context *con = NULL;
01743 struct ast_flags config_flags = { 0 };
01744
01745 cfg = ast_config_load("users.conf", config_flags);
01746 if (!cfg)
01747 return;
01748
01749 for (cat = ast_category_browse(cfg, NULL); cat ; cat = ast_category_browse(cfg, cat)) {
01750 if (!strcasecmp(cat, "general"))
01751 continue;
01752 iface[0] = '\0';
01753 if (ast_true(ast_config_option(cfg, cat, "hassip"))) {
01754 snprintf(tmp, sizeof(tmp), "SIP/%s", cat);
01755 append_interface(iface, sizeof(iface), tmp);
01756 }
01757 if (ast_true(ast_config_option(cfg, cat, "hasiax"))) {
01758 snprintf(tmp, sizeof(tmp), "IAX2/%s", cat);
01759 append_interface(iface, sizeof(iface), tmp);
01760 }
01761 if (ast_true(ast_config_option(cfg, cat, "hash323"))) {
01762 snprintf(tmp, sizeof(tmp), "H323/%s", cat);
01763 append_interface(iface, sizeof(iface), tmp);
01764 }
01765 hasexten = ast_config_option(cfg, cat, "hasexten");
01766 if (hasexten && !ast_true(hasexten))
01767 continue;
01768 hasvoicemail = ast_true(ast_config_option(cfg, cat, "hasvoicemail"));
01769 dahdichan = ast_variable_retrieve(cfg, cat, "dahdichan");
01770 if (!dahdichan)
01771 dahdichan = ast_variable_retrieve(cfg, "general", "dahdichan");
01772 if (!ast_strlen_zero(dahdichan)) {
01773 ast_copy_string(dahdicopy, dahdichan, sizeof(dahdicopy));
01774 c = dahdicopy;
01775 chan = strsep(&c, ",");
01776 while (chan) {
01777 if (sscanf(chan, "%30d-%30d", &start, &finish) == 2) {
01778
01779 } else if (sscanf(chan, "%30d", &start)) {
01780
01781 finish = start;
01782 } else {
01783 start = 0; finish = 0;
01784 }
01785 if (finish < start) {
01786 x = finish;
01787 finish = start;
01788 start = x;
01789 }
01790 for (x = start; x <= finish; x++) {
01791 snprintf(tmp, sizeof(tmp), "DAHDI/%d", x);
01792 append_interface(iface, sizeof(iface), tmp);
01793 }
01794 chan = strsep(&c, ",");
01795 }
01796 }
01797 if (!ast_strlen_zero(iface)) {
01798
01799
01800 if (!con)
01801 con = ast_context_find_or_create(&local_contexts, local_table, userscontext, registrar);
01802
01803 if (!con) {
01804 ast_log(LOG_ERROR, "Can't find/create user context '%s'\n", userscontext);
01805 return;
01806 }
01807
01808
01809 ast_add_extension2(con, 0, cat, -1, NULL, NULL, iface, NULL, NULL, registrar);
01810
01811 if (hasvoicemail) {
01812 if (ast_opt_stdexten_macro) {
01813
01814 snprintf(tmp, sizeof(tmp), "stdexten,%s,${HINT}", cat);
01815 ast_add_extension2(con, 0, cat, 1, NULL, NULL, "Macro", ast_strdup(tmp), ast_free_ptr, registrar);
01816 } else {
01817 snprintf(tmp, sizeof(tmp), "%s,stdexten(${HINT})", cat);
01818 ast_add_extension2(con, 0, cat, 1, NULL, NULL, "Gosub", ast_strdup(tmp), ast_free_ptr, registrar);
01819 }
01820 } else {
01821 ast_add_extension2(con, 0, cat, 1, NULL, NULL, "Dial", ast_strdup("${HINT}"), ast_free_ptr, registrar);
01822 }
01823 altexts = ast_variable_retrieve(cfg, cat, "alternateexts");
01824 if (!ast_strlen_zero(altexts)) {
01825 snprintf(tmp, sizeof(tmp), "%s,1", cat);
01826 ast_copy_string(altcopy, altexts, sizeof(altcopy));
01827 c = altcopy;
01828 ext = strsep(&c, ",");
01829 while (ext) {
01830 ast_add_extension2(con, 0, ext, 1, NULL, NULL, "Goto", ast_strdup(tmp), ast_free_ptr, registrar);
01831 ext = strsep(&c, ",");
01832 }
01833 }
01834 }
01835 }
01836 ast_config_destroy(cfg);
01837 }
01838
01839 static int pbx_load_module(void)
01840 {
01841 struct ast_context *con;
01842
01843 ast_mutex_lock(&reload_lock);
01844
01845 if (!local_table)
01846 local_table = ast_hashtab_create(17, ast_hashtab_compare_contexts, ast_hashtab_resize_java, ast_hashtab_newsize_java, ast_hashtab_hash_contexts, 0);
01847
01848 if (!pbx_load_config(config)) {
01849 ast_mutex_unlock(&reload_lock);
01850 return AST_MODULE_LOAD_DECLINE;
01851 }
01852
01853 pbx_load_users();
01854
01855 ast_merge_contexts_and_delete(&local_contexts, local_table, registrar);
01856 local_table = NULL;
01857 local_contexts = NULL;
01858
01859 ast_mutex_unlock(&reload_lock);
01860
01861 for (con = NULL; (con = ast_walk_contexts(con));)
01862 ast_context_verify_includes(con);
01863
01864 pbx_set_overrideswitch(overrideswitch_config);
01865 pbx_set_autofallthrough(autofallthrough_config);
01866 pbx_set_extenpatternmatchnew(extenpatternmatchnew_config);
01867
01868 return AST_MODULE_LOAD_SUCCESS;
01869 }
01870
01871 static int load_module(void)
01872 {
01873 if (static_config && !write_protect_config)
01874 ast_cli_register(&cli_dialplan_save);
01875 ast_cli_register_multiple(cli_pbx_config, ARRAY_LEN(cli_pbx_config));
01876
01877 if (pbx_load_module())
01878 return AST_MODULE_LOAD_DECLINE;
01879
01880 return AST_MODULE_LOAD_SUCCESS;
01881 }
01882
01883 static int reload(void)
01884 {
01885 if (clearglobalvars_config)
01886 pbx_builtin_clear_globals();
01887 return pbx_load_module();
01888 }
01889
01890 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Text Extension Configuration",
01891 .load = load_module,
01892 .unload = unload_module,
01893 .reload = reload,
01894 );