pbx_config.c 52 KB

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