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