pbx_config.c 60 KB

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