pbx_config.c 52 KB

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