Sat Nov 1 06:28:35 2008

Asterisk developer's documentation


pbx_config.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 1999 - 2006, Digium, Inc.
00005  *
00006  * Mark Spencer <markster@digium.com>
00007  *
00008  * See http://www.asterisk.org for more information about
00009  * the Asterisk project. Please do not directly contact
00010  * any of the maintainers of this project for assistance;
00011  * the project provides a web site, mailing lists and IRC
00012  * channels for your use.
00013  *
00014  * This program is free software, distributed under the terms of
00015  * the GNU General Public License Version 2. See the LICENSE file
00016  * at the top of the source tree.
00017  */
00018 
00019 /*! \file
00020  *
00021  * \brief Populate and remember extensions from static config file
00022  *
00023  * 
00024  */
00025 
00026 #include "asterisk.h"
00027 
00028 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 143612 $")
00029 
00030 #include <sys/types.h>
00031 #include <stdlib.h>
00032 #include <stdio.h>
00033 #include <string.h>
00034 #include <ctype.h>
00035 #include <errno.h>
00036 
00037 #include "asterisk/pbx.h"
00038 #include "asterisk/config.h"
00039 #include "asterisk/options.h"
00040 #include "asterisk/module.h"
00041 #include "asterisk/logger.h"
00042 #include "asterisk/cli.h"
00043 #include "asterisk/callerid.h"
00044 
00045 static char *config = "extensions.conf";
00046 static 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 
00054 AST_MUTEX_DEFINE_STATIC(save_dialplan_lock);
00055 
00056 static struct ast_context *local_contexts = NULL;
00057 
00058 /*
00059  * Help for commands provided by this module ...
00060  */
00061 static char context_add_extension_help[] =
00062 "Usage: dialplan add extension <exten>,<priority>,<app>,<app-data>\n"
00063 "       into <context> [replace]\n\n"
00064 "       This command will add new extension into <context>. If there is an\n"
00065 "       existence of extension with the same priority and last 'replace'\n"
00066 "       arguments is given here we simply replace this extension.\n"
00067 "\n"
00068 "Example: dialplan add extension 6123,1,Dial,IAX/216.207.245.56/6123 into local\n"
00069 "         Now, you can dial 6123 and talk to Markster :)\n";
00070 
00071 static char context_remove_extension_help[] =
00072 "Usage: dialplan remove extension exten[/cid]@context [priority]\n"
00073 "       Remove an extension from a given context. If a priority\n"
00074 "       is given, only that specific priority from the given extension\n"
00075 "       will be removed.\n";
00076 
00077 static char context_add_ignorepat_help[] =
00078 "Usage: dialplan add ignorepat <pattern> into <context>\n"
00079 "       This command adds a new ignore pattern into context <context>\n"
00080 "\n"
00081 "Example: dialplan add ignorepat _3XX into local\n";
00082 
00083 static char context_remove_ignorepat_help[] =
00084 "Usage: dialplan remove ignorepat <pattern> from <context>\n"
00085 "       This command removes an ignore pattern from context <context>\n"
00086 "\n"
00087 "Example: dialplan remove ignorepat _3XX from local\n";
00088 
00089 static char context_add_include_help[] =
00090 "Usage: dialplan add include <context> into <context>\n"
00091 "       Include a context in another context.\n";
00092 
00093 static char context_remove_include_help[] =
00094 "Usage: dialplan remove include <context> from <context>\n"
00095 "       Remove an included context from another context.\n";
00096 
00097 static char save_dialplan_help[] =
00098 "Usage: dialplan save [/path/to/extension/file]\n"
00099 "       Save dialplan created by pbx_config module.\n"
00100 "\n"
00101 "Example: dialplan save                 (/etc/asterisk/extensions.conf)\n"
00102 "         dialplan save /home/markster  (/home/markster/extensions.conf)\n";
00103 
00104 static char reload_extensions_help[] =
00105 "Usage: dialplan reload\n"
00106 "       reload extensions.conf without reloading any other modules\n"
00107 "       This command does not delete global variables unless\n"
00108 "       clearglobalvars is set to yes in extensions.conf\n";
00109 
00110 /*
00111  * Implementation of functions provided by this module
00112  */
00113 
00114 /*!
00115  * REMOVE INCLUDE command stuff
00116  */
00117 static int handle_context_dont_include_deprecated(int fd, int argc, char *argv[])
00118 {
00119    if (argc != 5)
00120       return RESULT_SHOWUSAGE;
00121 
00122    if (strcmp(argv[3], "into"))
00123       return RESULT_SHOWUSAGE;
00124 
00125    if (!ast_context_remove_include(argv[4], argv[2], registrar)) {
00126       ast_cli(fd, "We are not including '%s' into '%s' now\n",
00127          argv[2], argv[4]);
00128       return RESULT_SUCCESS;
00129    }
00130 
00131    ast_cli(fd, "Failed to remove '%s' include from '%s' context\n",
00132       argv[2], argv[4]);
00133    return RESULT_FAILURE;
00134 }
00135 
00136 static int handle_context_remove_include(int fd, int argc, char *argv[])
00137 {
00138    if (argc != 6) {
00139       return RESULT_SHOWUSAGE;
00140    }
00141 
00142    if (strcmp(argv[4], "from")) {
00143       return RESULT_SHOWUSAGE;
00144    }
00145 
00146    if (!ast_context_remove_include(argv[5], argv[3], registrar)) {
00147       ast_cli(fd, "The dialplan no longer includes '%s' into '%s'\n",
00148          argv[3], argv[5]);
00149       return RESULT_SUCCESS;
00150    }
00151 
00152    ast_cli(fd, "Failed to remove '%s' include from '%s' context\n",
00153       argv[3], argv[5]);
00154 
00155    return RESULT_FAILURE;
00156 }
00157 
00158 /*! \brief return true if 'name' is included by context c */
00159 static int lookup_ci(struct ast_context *c, const char *name)
00160 {
00161    struct ast_include *i = NULL;
00162 
00163    if (ast_lock_context(c))   /* error, skip */
00164       return 0;
00165    while ( (i = ast_walk_context_includes(c, i)) )
00166       if (!strcmp(name, ast_get_include_name(i)))
00167          break;
00168    ast_unlock_context(c);
00169    return i ? -1 /* success */ : 0;
00170 }
00171 
00172 /*! \brief return true if 'name' is in the ignorepats for context c */
00173 static int lookup_c_ip(struct ast_context *c, const char *name)
00174 {
00175    struct ast_ignorepat *ip = NULL;
00176 
00177    if (ast_lock_context(c))   /* error, skip */
00178       return 0;
00179    while ( (ip = ast_walk_context_ignorepats(c, ip)) )
00180       if (!strcmp(name, ast_get_ignorepat_name(ip)))
00181          break;
00182    ast_unlock_context(c);
00183    return ip ? -1 /* success */ : 0;
00184 }
00185 
00186 /*! \brief moves to the n-th word in the string, or empty string if none */
00187 static const char *skip_words(const char *p, int n)
00188 {
00189    int in_blank = 0;
00190    for (;n && *p; p++) {
00191       if (isblank(*p) /* XXX order is important */ && !in_blank) {
00192          n--;  /* one word is gone */
00193          in_blank = 1;
00194       } else if (/* !is_blank(*p), we know already, && */ in_blank) {
00195          in_blank = 0;
00196       }
00197    }
00198    return p;
00199 }
00200 
00201 /*! \brief match the first 'len' chars of word. len==0 always succeeds */
00202 static int partial_match(const char *s, const char *word, int len)
00203 {
00204    return (len == 0 || !strncmp(s, word, len));
00205 }
00206 
00207 /*! \brief split extension\@context in two parts, return -1 on error.
00208  * The return string is malloc'ed and pointed by *ext
00209  */
00210 static int split_ec(const char *src, char **ext, char ** const ctx, char ** const cid)
00211 {
00212    char *i, *c, *e = ast_strdup(src); /* now src is not used anymore */
00213 
00214    if (e == NULL)
00215       return -1;  /* malloc error */
00216    /* now, parse values from 'exten@context' */
00217    *ext = e;
00218    c = strchr(e, '@');
00219    if (c == NULL) /* no context part */
00220       *ctx = "";  /* it is not overwritten, anyways */
00221    else {   /* found context, check for duplicity ... */
00222       *c++ = '\0';
00223       *ctx = c;
00224       if (strchr(c, '@')) { /* two @, not allowed */
00225          free(e);
00226          return -1;
00227       }
00228    }
00229    if (cid && (i = strchr(e, '/'))) {
00230       *i++ = '\0';
00231       *cid = i;
00232    } else if (cid) {
00233       /* Signal none detected */
00234       *cid = NULL;
00235    }
00236    return 0;
00237 }
00238 
00239 /* _X_ is the string we need to complete */
00240 static char *complete_context_dont_include_deprecated(const char *line, const char *word,
00241    int pos, int state)
00242 {
00243    int which = 0;
00244    char *res = NULL;
00245    int len = strlen(word); /* how many bytes to match */
00246    struct ast_context *c = NULL;
00247 
00248    if (pos == 2) {      /* "dont include _X_" */
00249       if (ast_wrlock_contexts()) {
00250          ast_log(LOG_ERROR, "Failed to lock context list\n");
00251          return NULL;
00252       }
00253       /* walk contexts and their includes, return the n-th match */
00254       while (!res && (c = ast_walk_contexts(c))) {
00255          struct ast_include *i = NULL;
00256 
00257          if (ast_lock_context(c))   /* error ? skip this one */
00258             continue;
00259 
00260          while ( !res && (i = ast_walk_context_includes(c, i)) ) {
00261             const char *i_name = ast_get_include_name(i);
00262             struct ast_context *nc = NULL;
00263             int already_served = 0;
00264 
00265             if (!partial_match(i_name, word, len))
00266                continue;   /* not matched */
00267 
00268             /* check if this include is already served or not */
00269 
00270             /* go through all contexts again till we reach actual
00271              * context or already_served = 1
00272              */
00273             while ( (nc = ast_walk_contexts(nc)) && nc != c && !already_served)
00274                already_served = lookup_ci(nc, i_name);
00275 
00276             if (!already_served && ++which > state)
00277                res = strdup(i_name);
00278          }
00279          ast_unlock_context(c);
00280       }
00281 
00282       ast_unlock_contexts();
00283       return res;
00284    } else if (pos == 3) { /* "dont include CTX _X_" */
00285       /*
00286        * complete as 'in', but only if previous context is really
00287        * included somewhere
00288        */
00289       char *context, *dupline;
00290       const char *s = skip_words(line, 2); /* skip 'dont' 'include' */
00291 
00292       if (state > 0)
00293          return NULL;
00294       context = dupline = strdup(s);
00295       if (!dupline) {
00296          ast_log(LOG_ERROR, "Out of free memory\n");
00297          return NULL;
00298       }
00299       strsep(&dupline, " ");
00300 
00301       if (ast_rdlock_contexts()) {
00302          ast_log(LOG_ERROR, "Failed to lock contexts list\n");
00303          free(context);
00304          return NULL;
00305       }
00306 
00307       /* go through all contexts and check if is included ... */
00308       while (!res && (c = ast_walk_contexts(c)))
00309          if (lookup_ci(c, context)) /* context is really included, complete "in" command */
00310             res = strdup("in");
00311       ast_unlock_contexts();
00312       if (!res)
00313          ast_log(LOG_WARNING, "%s not included anywhere\n", context);
00314       free(context);
00315       return res;
00316    } else if (pos == 4) { /* "dont include CTX in _X_" */
00317       /*
00318        * Context from which we removing include ... 
00319        */
00320       char *context, *dupline, *in;
00321       const char *s = skip_words(line, 2); /* skip 'dont' 'include' */
00322       context = dupline = strdup(s);
00323       if (!dupline) {
00324          ast_log(LOG_ERROR, "Out of free memory\n");
00325          return NULL;
00326       }
00327 
00328       strsep(&dupline, " "); /* skip context */
00329 
00330       /* third word must be 'in' */
00331       in = strsep(&dupline, " ");
00332       if (!in || strcmp(in, "in")) {
00333          free(context);
00334          return NULL;
00335       }
00336 
00337       if (ast_rdlock_contexts()) {
00338          ast_log(LOG_ERROR, "Failed to lock context list\n");
00339          free(context);
00340          return NULL;
00341       }
00342 
00343       /* walk through all contexts ... */
00344       c = NULL;
00345       while ( !res && (c = ast_walk_contexts(c))) {
00346          const char *c_name = ast_get_context_name(c);
00347          if (!partial_match(c_name, word, len)) /* not a good target */
00348             continue;
00349          /* walk through all includes and check if it is our context */ 
00350          if (lookup_ci(c, context) && ++which > state)
00351             res = strdup(c_name);
00352       }
00353       ast_unlock_contexts();
00354       free(context);
00355       return res;
00356    }
00357 
00358    return NULL;
00359 }
00360 
00361 static char *complete_context_remove_include(const char *line, const char *word,
00362    int pos, int state)
00363 {
00364    int which = 0;
00365    char *res = NULL;
00366    int len = strlen(word); /* how many bytes to match */
00367    struct ast_context *c = NULL;
00368 
00369    if (pos == 3) {      /* "dialplan remove include _X_" */
00370       if (ast_rdlock_contexts()) {
00371          ast_log(LOG_ERROR, "Failed to lock context list\n");
00372          return NULL;
00373       }
00374       /* walk contexts and their includes, return the n-th match */
00375       while (!res && (c = ast_walk_contexts(c))) {
00376          struct ast_include *i = NULL;
00377 
00378          if (ast_lock_context(c))   /* error ? skip this one */
00379             continue;
00380 
00381          while ( !res && (i = ast_walk_context_includes(c, i)) ) {
00382             const char *i_name = ast_get_include_name(i);
00383             struct ast_context *nc = NULL;
00384             int already_served = 0;
00385 
00386             if (!partial_match(i_name, word, len))
00387                continue;   /* not matched */
00388 
00389             /* check if this include is already served or not */
00390 
00391             /* go through all contexts again till we reach actual
00392              * context or already_served = 1
00393              */
00394             while ( (nc = ast_walk_contexts(nc)) && nc != c && !already_served)
00395                already_served = lookup_ci(nc, i_name);
00396 
00397             if (!already_served && ++which > state)
00398                res = strdup(i_name);
00399          }
00400          ast_unlock_context(c);
00401       }
00402 
00403       ast_unlock_contexts();
00404       return res;
00405    } else if (pos == 4) { /* "dialplan remove include CTX _X_" */
00406       /*
00407        * complete as 'from', but only if previous context is really
00408        * included somewhere
00409        */
00410       char *context, *dupline;
00411       const char *s = skip_words(line, 3); /* skip 'dialplan' 'remove' 'include' */
00412 
00413       if (state > 0)
00414          return NULL;
00415       context = dupline = strdup(s);
00416       if (!dupline) {
00417          ast_log(LOG_ERROR, "Out of free memory\n");
00418          return NULL;
00419       }
00420       strsep(&dupline, " ");
00421 
00422       if (ast_rdlock_contexts()) {
00423          ast_log(LOG_ERROR, "Failed to lock contexts list\n");
00424          free(context);
00425          return NULL;
00426       }
00427 
00428       /* go through all contexts and check if is included ... */
00429       while (!res && (c = ast_walk_contexts(c)))
00430          if (lookup_ci(c, context)) /* context is really included, complete "from" command */
00431             res = strdup("from");
00432       ast_unlock_contexts();
00433       if (!res)
00434          ast_log(LOG_WARNING, "%s not included anywhere\n", context);
00435       free(context);
00436       return res;
00437    } else if (pos == 5) { /* "dialplan remove include CTX from _X_" */
00438       /*
00439        * Context from which we removing include ... 
00440        */
00441       char *context, *dupline, *from;
00442       const char *s = skip_words(line, 3); /* skip 'dialplan' 'remove' 'include' */
00443       context = dupline = strdup(s);
00444       if (!dupline) {
00445          ast_log(LOG_ERROR, "Out of free memory\n");
00446          return NULL;
00447       }
00448 
00449       strsep(&dupline, " "); /* skip context */
00450 
00451       /* fourth word must be 'from' */
00452       from = strsep(&dupline, " ");
00453       if (!from || strcmp(from, "from")) {
00454          free(context);
00455          return NULL;
00456       }
00457 
00458       if (ast_rdlock_contexts()) {
00459          ast_log(LOG_ERROR, "Failed to lock context list\n");
00460          free(context);
00461          return NULL;
00462       }
00463 
00464       /* walk through all contexts ... */
00465       c = NULL;
00466       while ( !res && (c = ast_walk_contexts(c))) {
00467          const char *c_name = ast_get_context_name(c);
00468          if (!partial_match(c_name, word, len)) /* not a good target */
00469             continue;
00470          /* walk through all includes and check if it is our context */ 
00471          if (lookup_ci(c, context) && ++which > state)
00472             res = strdup(c_name);
00473       }
00474       ast_unlock_contexts();
00475       free(context);
00476       return res;
00477    }
00478 
00479    return NULL;
00480 }
00481 
00482 /*!
00483  * REMOVE EXTENSION command stuff
00484  */
00485 static int handle_context_remove_extension_deprecated(int fd, int argc, char *argv[])
00486 {
00487    int removing_priority = 0;
00488    char *exten, *context, *cid;
00489    int ret = RESULT_FAILURE;
00490 
00491    if (argc != 4 && argc != 3) return RESULT_SHOWUSAGE;
00492 
00493    /*
00494     * Priority input checking ...
00495     */
00496    if (argc == 4) {
00497       char *c = argv[3];
00498 
00499       /* check for digits in whole parameter for right priority ...
00500        * why? because atoi (strtol) returns 0 if any characters in
00501        * string and whole extension will be removed, it's not good
00502        */
00503       if (!strcmp("hint", c))
00504          removing_priority = PRIORITY_HINT;
00505       else {
00506          while (*c && isdigit(*c))
00507             c++;
00508          if (*c) { /* non-digit in string */
00509             ast_cli(fd, "Invalid priority '%s'\n", argv[3]);
00510             return RESULT_FAILURE;
00511          }
00512          removing_priority = atoi(argv[3]);
00513       }
00514 
00515       if (removing_priority == 0) {
00516          ast_cli(fd, "If you want to remove whole extension, please " \
00517             "omit priority argument\n");
00518          return RESULT_FAILURE;
00519       }
00520    }
00521 
00522    /* XXX original overwrote argv[2] */
00523    /*
00524     * Format exten@context checking ...
00525     */
00526    if (split_ec(argv[2], &exten, &context, &cid))
00527       return RESULT_FAILURE; /* XXX malloc failure */
00528    if ((!strlen(exten)) || (!(strlen(context)))) {
00529       ast_cli(fd, "Missing extension or context name in second argument '%s'\n",
00530          argv[2]);
00531       free(exten);
00532       return RESULT_FAILURE;
00533    }
00534 
00535    if (!ast_context_remove_extension_callerid(context, exten, removing_priority,
00536          /* Do NOT substitute S_OR; it is NOT the same thing */
00537          cid ? cid : (removing_priority ? "" : NULL), cid ? 1 : 0, registrar)) {
00538       if (!removing_priority)
00539          ast_cli(fd, "Whole extension %s@%s removed\n",
00540             exten, context);
00541       else
00542          ast_cli(fd, "Extension %s@%s with priority %d removed\n",
00543             exten, context, removing_priority);
00544          
00545       ret = RESULT_SUCCESS;
00546    } else {
00547       ast_cli(fd, "Failed to remove extension %s@%s\n", exten, context);
00548       ret = RESULT_FAILURE;
00549    }
00550    free(exten);
00551    return ret;
00552 }
00553 
00554 static int handle_context_remove_extension(int fd, int argc, char *argv[])
00555 {
00556    int removing_priority = 0;
00557    char *exten, *context, *cid;
00558    int ret = RESULT_FAILURE;
00559 
00560    if (argc != 5 && argc != 4) return RESULT_SHOWUSAGE;
00561 
00562    /*
00563     * Priority input checking ...
00564     */
00565    if (argc == 5) {
00566       char *c = argv[4];
00567 
00568       /* check for digits in whole parameter for right priority ...
00569        * why? because atoi (strtol) returns 0 if any characters in
00570        * string and whole extension will be removed, it's not good
00571        */
00572       if (!strcmp("hint", c))
00573          removing_priority = PRIORITY_HINT;
00574       else {
00575          while (*c && isdigit(*c))
00576             c++;
00577          if (*c) { /* non-digit in string */
00578             ast_cli(fd, "Invalid priority '%s'\n", argv[4]);
00579             return RESULT_FAILURE;
00580          }
00581          removing_priority = atoi(argv[4]);
00582       }
00583 
00584       if (removing_priority == 0) {
00585          ast_cli(fd, "If you want to remove whole extension, please " \
00586             "omit priority argument\n");
00587          return RESULT_FAILURE;
00588       }
00589    }
00590 
00591    /* XXX original overwrote argv[3] */
00592    /*
00593     * Format exten@context checking ...
00594     */
00595    if (split_ec(argv[3], &exten, &context, &cid))
00596       return RESULT_FAILURE; /* XXX malloc failure */
00597    if ((!strlen(exten)) || (!(strlen(context)))) {
00598       ast_cli(fd, "Missing extension or context name in third argument '%s'\n",
00599          argv[3]);
00600       free(exten);
00601       return RESULT_FAILURE;
00602    }
00603 
00604    if (!ast_context_remove_extension_callerid(context, exten, removing_priority,
00605          /* Do NOT substitute S_OR; it is NOT the same thing */
00606          cid ? cid : (removing_priority ? "" : NULL), cid ? 1 : 0, registrar)) {
00607       if (!removing_priority)
00608          ast_cli(fd, "Whole extension %s@%s removed\n",
00609             exten, context);
00610       else
00611          ast_cli(fd, "Extension %s@%s with priority %d removed\n",
00612             exten, context, removing_priority);
00613          
00614       ret = RESULT_SUCCESS;
00615    } else {
00616       ast_cli(fd, "Failed to remove extension %s@%s\n", exten, context);
00617       ret = RESULT_FAILURE;
00618    }
00619    free(exten);
00620    return ret;
00621 }
00622 
00623 #define BROKEN_READLINE 1
00624 
00625 #ifdef BROKEN_READLINE
00626 /*
00627  * There is one funny thing, when you have word like 300@ and you hit
00628  * <tab>, you arguments will like as your word is '300 ', so it '@'
00629  * characters acts sometimes as word delimiter and sometimes as a part
00630  * of word
00631  *
00632  * This fix function, allocates new word variable and store here every
00633  * time xxx@yyy always as one word and correct pos is set too
00634  *
00635  * It's ugly, I know, but I'm waiting for Mark suggestion if upper is
00636  * bug or feature ...
00637  */
00638 static int fix_complete_args(const char *line, char **word, int *pos)
00639 {
00640    char *_line, *_strsep_line, *_previous_word = NULL, *_word = NULL;
00641    int words = 0;
00642 
00643    _line = strdup(line);
00644 
00645    _strsep_line = _line;
00646    while (_strsep_line) {
00647       _previous_word = _word;
00648       _word = strsep(&_strsep_line, " ");
00649 
00650       if (_word && strlen(_word)) words++;
00651    }
00652 
00653 
00654    if (_word || _previous_word) {
00655       if (_word) {
00656          if (!strlen(_word)) words++;
00657          *word = strdup(_word);
00658       } else
00659          *word = strdup(_previous_word);
00660       *pos = words - 1;
00661       free(_line);
00662       return 0;
00663    }
00664 
00665    free(_line);
00666    return -1;
00667 }
00668 #endif /* BROKEN_READLINE */
00669 
00670 static char *complete_context_remove_extension_deprecated(const char *line, const char *word, int pos,
00671    int state)
00672 {
00673    char *ret = NULL;
00674    int which = 0;
00675 
00676 #ifdef BROKEN_READLINE
00677    char *word2;
00678    /*
00679     * Fix arguments, *word is a new allocated structure, REMEMBER to
00680     * free *word when you want to return from this function ...
00681     */
00682    if (fix_complete_args(line, &word2, &pos)) {
00683       ast_log(LOG_ERROR, "Out of free memory\n");
00684       return NULL;
00685    }
00686    word = word2;
00687 #endif
00688 
00689    if (pos == 2) { /* 'remove extension _X_' (exten/cid@context ... */
00690       struct ast_context *c = NULL;
00691       char *context = NULL, *exten = NULL, *cid = NULL;
00692       int le = 0; /* length of extension */
00693       int lc = 0; /* length of context */
00694       int lcid = 0; /* length of cid */
00695 
00696       lc = split_ec(word, &exten, &context, &cid);
00697 #ifdef BROKEN_READLINE
00698       free(word2);
00699 #endif
00700       if (lc)  /* error */
00701          return NULL;
00702       le = strlen(exten);
00703       lc = strlen(context);
00704       lcid = cid ? strlen(cid) : -1;
00705 
00706       if (ast_rdlock_contexts()) {
00707          ast_log(LOG_ERROR, "Failed to lock context list\n");
00708          goto error2;
00709       }
00710 
00711       /* find our context ... */
00712       while ( (c = ast_walk_contexts(c)) ) { /* match our context if any */
00713          struct ast_exten *e = NULL;
00714          /* XXX locking ? */
00715          if (!partial_match(ast_get_context_name(c), context, lc))
00716             continue;   /* context not matched */
00717          while ( (e = ast_walk_context_extensions(c, e)) ) { /* try to complete extensions ... */
00718             if ( !strchr(word, '/') ||
00719                   (!strchr(word, '@') && partial_match(ast_get_extension_cidmatch(e), cid, lcid)) ||
00720                   (strchr(word, '@') && !strcmp(ast_get_extension_cidmatch(e), cid))) {
00721                if ( ((strchr(word, '/') || strchr(word, '@')) && !strcmp(ast_get_extension_name(e), exten)) ||
00722                    (!strchr(word, '/') && !strchr(word, '@') && partial_match(ast_get_extension_name(e), exten, le))) { /* n-th match */
00723                   if (++which > state) {
00724                      /* If there is an extension then return exten@context. */
00725                      if (ast_get_extension_matchcid(e) && (!strchr(word, '@') || strchr(word, '/'))) {
00726                         asprintf(&ret, "%s/%s@%s", ast_get_extension_name(e), ast_get_extension_cidmatch(e), ast_get_context_name(c));
00727                         break;
00728                      } else if (!ast_get_extension_matchcid(e) && !strchr(word, '/')) {
00729                         asprintf(&ret, "%s@%s", ast_get_extension_name(e), ast_get_context_name(c));
00730                         break;
00731                      }
00732                   }
00733                }
00734             }
00735          }
00736          if (e)   /* got a match */
00737             break;
00738       }
00739 
00740       ast_unlock_contexts();
00741    error2:
00742       if (exten)
00743          free(exten);
00744    } else if (pos == 3) { /* 'remove extension EXT _X_' (priority) */
00745       char *exten = NULL, *context, *cid, *p;
00746       struct ast_context *c;
00747       int le, lc, lcid, len;
00748       const char *s = skip_words(line, 2); /* skip 'remove' 'extension' */
00749       int i = split_ec(s, &exten, &context, &cid); /* parse ext@context */
00750 
00751       if (i)   /* error */
00752          goto error3;
00753       if ( (p = strchr(exten, ' ')) ) /* remove space after extension */
00754          *p = '\0';
00755       if ( (p = strchr(context, ' ')) ) /* remove space after context */
00756          *p = '\0';
00757       le = strlen(exten);
00758       lc = strlen(context);
00759       lcid = strlen(cid);
00760       len = strlen(word);
00761       if (le == 0 || lc == 0)
00762          goto error3;
00763 
00764       if (ast_rdlock_contexts()) {
00765          ast_log(LOG_ERROR, "Failed to lock context list\n");
00766          goto error3;
00767       }
00768 
00769       /* walk contexts */
00770       c = NULL;
00771       while ( (c = ast_walk_contexts(c)) ) {
00772          /* XXX locking on c ? */
00773          struct ast_exten *e;
00774          if (strcmp(ast_get_context_name(c), context) != 0)
00775             continue;
00776          /* got it, we must match here */
00777          e = NULL;
00778          while ( (e = ast_walk_context_extensions(c, e)) ) {
00779             struct ast_exten *priority;
00780             char buffer[10];
00781 
00782             if (cid && strcmp(ast_get_extension_cidmatch(e), cid) != 0) {
00783                continue;
00784             }
00785             if (strcmp(ast_get_extension_name(e), exten) != 0)
00786                continue;
00787             /* XXX lock e ? */
00788             priority = NULL;
00789             while ( !ret && (priority = ast_walk_extension_priorities(e, priority)) ) {
00790                snprintf(buffer, sizeof(buffer), "%u", ast_get_extension_priority(priority));
00791                if (partial_match(buffer, word, len) && ++which > state) /* n-th match */
00792                   ret = strdup(buffer);
00793             }
00794             break;
00795          }
00796          break;
00797       }
00798       ast_unlock_contexts();
00799    error3:
00800       if (exten)
00801          free(exten);
00802    }
00803 #ifdef BROKEN_READLINE
00804    free(word2);
00805 #endif
00806    return ret; 
00807 }
00808 
00809 static char *complete_context_remove_extension(const char *line, const char *word, int pos,
00810    int state)
00811 {
00812    char *ret = NULL;
00813    int which = 0;
00814 
00815 #ifdef BROKEN_READLINE
00816    char *word2;
00817    /*
00818     * Fix arguments, *word is a new allocated structure, REMEMBER to
00819     * free *word when you want to return from this function ...
00820     */
00821    if (fix_complete_args(line, &word2, &pos)) {
00822       ast_log(LOG_ERROR, "Out of free memory\n");
00823       return NULL;
00824    }
00825    word = word2;
00826 #endif
00827 
00828    if (pos == 3) { /* 'dialplan remove extension _X_' (exten@context ... */
00829       struct ast_context *c = NULL;
00830       char *context = NULL, *exten = NULL, *cid = NULL;
00831       int le = 0; /* length of extension */
00832       int lc = 0; /* length of context */
00833       int lcid = 0; /* length of cid */
00834 
00835       lc = split_ec(word, &exten, &context, &cid);
00836       if (lc)  { /* error */
00837 #ifdef BROKEN_READLINE
00838          free(word2);
00839 #endif
00840          return NULL;
00841       }
00842       le = strlen(exten);
00843       lc = strlen(context);
00844       lcid = cid ? strlen(cid) : -1;
00845 
00846       if (ast_rdlock_contexts()) {
00847          ast_log(LOG_ERROR, "Failed to lock context list\n");
00848          goto error2;
00849       }
00850 
00851       /* find our context ... */
00852       while ( (c = ast_walk_contexts(c)) ) { /* match our context if any */
00853          struct ast_exten *e = NULL;
00854          /* XXX locking ? */
00855          if (!partial_match(ast_get_context_name(c), context, lc))
00856             continue;   /* context not matched */
00857          while ( (e = ast_walk_context_extensions(c, e)) ) { /* try to complete extensions ... */
00858             if ( !strchr(word, '/') ||
00859                   (!strchr(word, '@') && partial_match(ast_get_extension_cidmatch(e), cid, lcid)) ||
00860                   (strchr(word, '@') && !strcmp(ast_get_extension_cidmatch(e), cid))) {
00861                if ( ((strchr(word, '/') || strchr(word, '@')) && !strcmp(ast_get_extension_name(e), exten)) ||
00862                    (!strchr(word, '/') && !strchr(word, '@') && partial_match(ast_get_extension_name(e), exten, le))) { /* n-th match */
00863                   if (++which > state) {
00864                      /* If there is an extension then return exten@context. */
00865                      if (ast_get_extension_matchcid(e) && (!strchr(word, '@') || strchr(word, '/'))) {
00866                         asprintf(&ret, "%s/%s@%s", ast_get_extension_name(e), ast_get_extension_cidmatch(e), ast_get_context_name(c));
00867                         break;
00868                      } else if (!ast_get_extension_matchcid(e) && !strchr(word, '/')) {
00869                         asprintf(&ret, "%s@%s", ast_get_extension_name(e), ast_get_context_name(c));
00870                         break;
00871                      }
00872                   }
00873                }
00874             }
00875          }
00876          if (e)   /* got a match */
00877             break;
00878       }
00879 #ifdef BROKEN_READLINE
00880       free(word2);
00881 #endif
00882 
00883       ast_unlock_contexts();
00884    error2:
00885       if (exten)
00886          free(exten);
00887    } else if (pos == 4) { /* 'dialplan remove extension EXT _X_' (priority) */
00888       char *exten = NULL, *context, *cid, *p;
00889       struct ast_context *c;
00890       int le, lc, lcid, len;
00891       const char *s = skip_words(line, 3); /* skip 'dialplan' 'remove' 'extension' */
00892       int i = split_ec(s, &exten, &context, &cid); /* parse ext@context */
00893 
00894       if (i)   /* error */
00895          goto error3;
00896       if ( (p = strchr(exten, ' ')) ) /* remove space after extension */
00897          *p = '\0';
00898       if ( (p = strchr(context, ' ')) ) /* remove space after context */
00899          *p = '\0';
00900       le = strlen(exten);
00901       lc = strlen(context);
00902       lcid = cid ? strlen(cid) : -1;
00903       len = strlen(word);
00904       if (le == 0 || lc == 0)
00905          goto error3;
00906 
00907       if (ast_rdlock_contexts()) {
00908          ast_log(LOG_ERROR, "Failed to lock context list\n");
00909          goto error3;
00910       }
00911 
00912       /* walk contexts */
00913       c = NULL;
00914       while ( (c = ast_walk_contexts(c)) ) {
00915          /* XXX locking on c ? */
00916          struct ast_exten *e;
00917          if (strcmp(ast_get_context_name(c), context) != 0)
00918             continue;
00919          /* got it, we must match here */
00920          e = NULL;
00921          while ( (e = ast_walk_context_extensions(c, e)) ) {
00922             struct ast_exten *priority;
00923             char buffer[10];
00924 
00925             if (cid && strcmp(ast_get_extension_cidmatch(e), cid) != 0) {
00926                continue;
00927             }
00928             if (strcmp(ast_get_extension_name(e), exten) != 0)
00929                continue;
00930             /* XXX lock e ? */
00931             priority = NULL;
00932             while ( !ret && (priority = ast_walk_extension_priorities(e, priority)) ) {
00933                snprintf(buffer, sizeof(buffer), "%u", ast_get_extension_priority(priority));
00934                if (partial_match(buffer, word, len) && ++which > state) /* n-th match */
00935                   ret = strdup(buffer);
00936             }
00937             break;
00938          }
00939          break;
00940       }
00941       ast_unlock_contexts();
00942    error3:
00943       if (exten)
00944          free(exten);
00945 #ifdef BROKEN_READLINE
00946       free(word2);
00947 #endif
00948    }
00949    return ret; 
00950 }
00951 
00952 /*!
00953  * Include context ...
00954  */
00955 static int handle_context_add_include_deprecated(int fd, int argc, char *argv[])
00956 {
00957    if (argc != 5) /* include context CTX in CTX */
00958       return RESULT_SHOWUSAGE;
00959 
00960    /* third arg must be 'in' ... */
00961    if (strcmp(argv[3], "in") && strcmp(argv[3], "into")) /* XXX why both ? */
00962       return RESULT_SHOWUSAGE;
00963 
00964    if (ast_context_add_include(argv[4], argv[2], registrar)) {
00965       switch (errno) {
00966       case ENOMEM:
00967          ast_cli(fd, "Out of memory for context addition\n");
00968          break;
00969 
00970       case EBUSY:
00971          ast_cli(fd, "Failed to lock context(s) list, please try again later\n");
00972          break;
00973 
00974       case EEXIST:
00975          ast_cli(fd, "Context '%s' already included in '%s' context\n",
00976             argv[2], argv[4]);
00977          break;
00978 
00979       case ENOENT:
00980       case EINVAL:
00981          ast_cli(fd, "There is no existence of context '%s'\n",
00982             errno == ENOENT ? argv[4] : argv[2]);
00983          break;
00984 
00985       default:
00986          ast_cli(fd, "Failed to include '%s' in '%s' context\n",
00987             argv[2], argv[4]);
00988          break;
00989       }
00990       return RESULT_FAILURE;
00991    }
00992 
00993    /* show some info ... */
00994    ast_cli(fd, "Context '%s' included in '%s' context\n",
00995       argv[2], argv[4]);
00996 
00997    return RESULT_SUCCESS;
00998 }
00999 
01000 static int handle_context_add_include(int fd, int argc, char *argv[])
01001 {
01002    if (argc != 6) /* dialplan add include CTX in CTX */
01003       return RESULT_SHOWUSAGE;
01004 
01005    /* fifth arg must be 'into' ... */
01006    if (strcmp(argv[4], "into"))
01007       return RESULT_SHOWUSAGE;
01008 
01009    if (ast_context_add_include(argv[5], argv[3], registrar)) {
01010       switch (errno) {
01011       case ENOMEM:
01012          ast_cli(fd, "Out of memory for context addition\n");
01013          break;
01014 
01015       case EBUSY:
01016          ast_cli(fd, "Failed to lock context(s) list, please try again later\n");
01017          break;
01018 
01019       case EEXIST:
01020          ast_cli(fd, "Context '%s' already included in '%s' context\n",
01021             argv[3], argv[5]);
01022          break;
01023 
01024       case ENOENT:
01025       case EINVAL:
01026          ast_cli(fd, "There is no existence of context '%s'\n",
01027             errno == ENOENT ? argv[5] : argv[3]);
01028          break;
01029 
01030       default:
01031          ast_cli(fd, "Failed to include '%s' in '%s' context\n",
01032             argv[3], argv[5]);
01033          break;
01034       }
01035       return RESULT_FAILURE;
01036    }
01037 
01038    /* show some info ... */
01039    ast_cli(fd, "Context '%s' included in '%s' context\n",
01040       argv[3], argv[5]);
01041 
01042    return RESULT_SUCCESS;
01043 }
01044 
01045 static char *complete_context_add_include_deprecated(const char *line, const char *word, int pos,
01046     int state)
01047 {
01048    struct ast_context *c;
01049    int which = 0;
01050    char *ret = NULL;
01051    int len = strlen(word);
01052 
01053    if (pos == 2) {      /* 'include context _X_' (context) ... */
01054       if (ast_rdlock_contexts()) {
01055          ast_log(LOG_ERROR, "Failed to lock context list\n");
01056          return NULL;
01057       }
01058       for (c = NULL; !ret && (c = ast_walk_contexts(c)); )
01059          if (partial_match(ast_get_context_name(c), word, len) && ++which > state)
01060             ret = strdup(ast_get_context_name(c));
01061       ast_unlock_contexts();
01062       return ret;
01063    } else if (pos == 3) { /* include context CTX _X_ */
01064       /* complete  as 'in' if context exists or we are unable to check */
01065       char *context, *dupline;
01066       struct ast_context *c;
01067       const char *s = skip_words(line, 2);   /* should not fail */
01068 
01069       if (state != 0)   /* only once */
01070          return NULL;
01071 
01072       /* parse context from line ... */
01073       context = dupline = strdup(s);
01074       if (!context) {
01075          ast_log(LOG_ERROR, "Out of free memory\n");
01076          return strdup("in");
01077       }
01078       strsep(&dupline, " ");
01079 
01080       /* check for context existence ... */
01081       if (ast_rdlock_contexts()) {
01082          ast_log(LOG_ERROR, "Failed to lock context list\n");
01083          /* our fault, we can't check, so complete 'in' ... */
01084          ret = strdup("in");
01085       } else {
01086          for (c = NULL; !ret && (c = ast_walk_contexts(c)); )
01087             if (!strcmp(context, ast_get_context_name(c)))
01088                ret = strdup("in"); /* found */
01089          ast_unlock_contexts();
01090       }
01091       free(context);
01092       return ret;
01093    } else if (pos == 4) { /* 'include context CTX in _X_' (dst context) */
01094       char *context, *dupline, *in;
01095       const char