pbx_config.c 54 KB

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