pbx_config.c 60 KB

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