pbx_config.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786
  1. /*
  2. * Asterisk -- A telephony toolkit for Linux.
  3. *
  4. * Populate and remember extensions from static config file
  5. *
  6. * Copyright (C) 1999, Mark Spencer
  7. *
  8. * Mark Spencer <markster@linux-support.net>
  9. *
  10. * This program is free software, distributed under the terms of
  11. * the GNU General Public License
  12. */
  13. #include <sys/types.h>
  14. #include <asterisk/pbx.h>
  15. #include <asterisk/config.h>
  16. #include <asterisk/module.h>
  17. #include <asterisk/logger.h>
  18. #include <asterisk/cli.h>
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <ctype.h>
  23. #include <errno.h>
  24. /* For where to put dynamic tables */
  25. #include "../asterisk.h"
  26. #include "../astconf.h"
  27. #ifdef __AST_DEBUG_MALLOC
  28. static void FREE(void *ptr)
  29. {
  30. free(ptr);
  31. }
  32. #else
  33. #define FREE free
  34. #endif
  35. static char *dtext = "Text Extension Configuration";
  36. static char *config = "extensions.conf";
  37. static char *registrar = "pbx_config";
  38. static int static_config = 0;
  39. static int write_protect_config = 1;
  40. AST_MUTEX_DEFINE_STATIC(save_dialplan_lock);
  41. static struct ast_context *local_contexts = NULL;
  42. /*
  43. * Help for commands provided by this module ...
  44. */
  45. static char context_dont_include_help[] =
  46. "Usage: dont include context in include\n"
  47. " Remove include from context.\n";
  48. static char context_remove_extension_help[] =
  49. "Usage: remove extension exten@context [priority]\n"
  50. " Remove whole extension from context. If priority is set, we are only\n"
  51. " removing extension with given priority.\n";
  52. static char context_add_include_help[] =
  53. "Usage: include context in context\n"
  54. " Include context in other context.\n";
  55. static char save_dialplan_help[] =
  56. "Usage: save dialplan [/path/to/extension/file]\n"
  57. " Save dialplan created by pbx_config module.\n"
  58. "\n"
  59. "Example: save dialplan (/etc/asterisk/extensions.conf)\n"
  60. " save dialplan /home/markster (/home/markster/extensions.conf)\n";
  61. static char context_add_extension_help[] =
  62. "Usage: add extension <exten>,<priority>,<app>,<app-data> into <context>\n"
  63. " [replace]\n\n"
  64. " This command will add new extension into <context>. If there is an\n"
  65. " existence of extension with the same priority and last 'replace'\n"
  66. " arguments is given here we simply replace this extension.\n"
  67. "\n"
  68. "Example: add extension 6123,1,Dial,IAX/216.207.245.56/6123 into local\n"
  69. " Now, you can dial 6123 and talk to Markster :)\n";
  70. static char context_add_ignorepat_help[] =
  71. "Usage: add ignorepat <pattern> into <context>\n"
  72. " This command add new ignore pattern into context <context>\n"
  73. "\n"
  74. "Example: add ignorepat _3XX into local\n";
  75. static char context_remove_ignorepat_help[] =
  76. "Usage: remove ignorepat <pattern> from <context>\n"
  77. " This command remove ignore pattern from context <context>\n"
  78. "\n"
  79. "Example: remove ignorepat _3XX from local\n";
  80. static char reload_extensions_help[] =
  81. "Usage: reload extensions.conf without reloading any other modules\n"
  82. " This command does not delete global variables\n"
  83. "\n"
  84. "Example: extensions reload\n";
  85. /*
  86. * Static code
  87. */
  88. static char *process_quotes_and_slashes(char *start, char find, char replace_with)
  89. {
  90. char *dataPut = start;
  91. int inEscape = 0;
  92. int inQuotes = 0;
  93. for (; *start; start++) {
  94. if (inEscape) {
  95. *dataPut++ = *start; /* Always goes verbatim */
  96. inEscape = 0;
  97. } else {
  98. if (*start == '\\') {
  99. inEscape = 1; /* Do not copy \ into the data */
  100. } else if (*start == '\'') {
  101. inQuotes = 1-inQuotes; /* Do not copy ' into the data */
  102. } else {
  103. /* Replace , with |, unless in quotes */
  104. *dataPut++ = inQuotes ? *start : ((*start==find) ? replace_with : *start);
  105. }
  106. }
  107. }
  108. *dataPut = 0;
  109. return dataPut;
  110. }
  111. /*
  112. * Implementation of functions provided by this module
  113. */
  114. /*
  115. * REMOVE INCLUDE command stuff
  116. */
  117. static int handle_context_dont_include(int fd, int argc, char *argv[])
  118. {
  119. if (argc != 5) return RESULT_SHOWUSAGE;
  120. if (strcmp(argv[3], "in")) return RESULT_SHOWUSAGE;
  121. if (!ast_context_remove_include(argv[4], argv[2], registrar)) {
  122. ast_cli(fd, "We are not including '%s' in '%s' now\n",
  123. argv[2], argv[4]);
  124. return RESULT_SUCCESS;
  125. }
  126. ast_cli(fd, "Failed to remove '%s' include from '%s' context\n",
  127. argv[2], argv[4]);
  128. return RESULT_FAILURE;
  129. }
  130. static char *complete_context_dont_include(char *line, char *word,
  131. int pos, int state)
  132. {
  133. int which = 0;
  134. /*
  135. * Context completion ...
  136. */
  137. if (pos == 2) {
  138. struct ast_context *c;
  139. if (ast_lock_contexts()) {
  140. ast_log(LOG_ERROR, "Failed to lock context list\n");
  141. return NULL;
  142. }
  143. /* walk pbx_get_contexts ... */
  144. c = ast_walk_contexts(NULL);
  145. while (c) {
  146. struct ast_include *i;
  147. if (ast_lock_context(c)) {
  148. c = ast_walk_contexts(c);
  149. continue;
  150. }
  151. i = ast_walk_context_includes(c, NULL);
  152. while (i) {
  153. if (!strlen(word) ||
  154. !strncmp(ast_get_include_name(i), word, strlen(word))) {
  155. struct ast_context *nc;
  156. int already_served = 0;
  157. /* check if this include is already served or not */
  158. /* go through all contexts again till we reach actuall
  159. * context or already_served = 1
  160. */
  161. nc = ast_walk_contexts(NULL);
  162. while (nc && nc != c && !already_served) {
  163. if (!ast_lock_context(nc)) {
  164. struct ast_include *ni;
  165. ni = ast_walk_context_includes(nc, NULL);
  166. while (ni && !already_served) {
  167. if (!strcmp(ast_get_include_name(i),
  168. ast_get_include_name(ni)))
  169. already_served = 1;
  170. ni = ast_walk_context_includes(nc, ni);
  171. }
  172. ast_unlock_context(nc);
  173. }
  174. nc = ast_walk_contexts(nc);
  175. }
  176. if (!already_served) {
  177. if (++which > state) {
  178. char *res =
  179. strdup(ast_get_include_name(i));
  180. ast_unlock_context(c);
  181. ast_unlock_contexts();
  182. return res;
  183. }
  184. }
  185. }
  186. i = ast_walk_context_includes(c, i);
  187. }
  188. ast_unlock_context(c);
  189. c = ast_walk_contexts(c);
  190. }
  191. ast_unlock_contexts();
  192. return NULL;
  193. }
  194. /*
  195. * 'in' completion ... (complete only if previous context is really
  196. * included somewhere)
  197. */
  198. if (pos == 3) {
  199. struct ast_context *c;
  200. char *context, *dupline, *duplinet;
  201. if (state > 0) return NULL;
  202. /* take 'context' from line ... */
  203. if (!(dupline = strdup(line))) {
  204. ast_log(LOG_ERROR, "Out of free memory\n");
  205. return NULL;
  206. }
  207. duplinet = dupline;
  208. strsep(&duplinet, " "); /* skip 'dont' */
  209. strsep(&duplinet, " "); /* skip 'include' */
  210. context = strsep(&duplinet, " ");
  211. if (!context) {
  212. free(dupline);
  213. return NULL;
  214. }
  215. if (ast_lock_contexts()) {
  216. ast_log(LOG_WARNING, "Failed to lock contexts list\n");
  217. free(dupline);
  218. return NULL;
  219. }
  220. /* go through all contexts and check if is included ... */
  221. c = ast_walk_contexts(NULL);
  222. while (c) {
  223. struct ast_include *i;
  224. if (ast_lock_context(c)) {
  225. free(dupline);
  226. ast_unlock_contexts();
  227. return NULL;
  228. }
  229. i = ast_walk_context_includes(c, NULL);
  230. while (i) {
  231. /* is it our context? */
  232. if (!strcmp(ast_get_include_name(i), context)) {
  233. /* yes, it is, context is really included, so
  234. * complete "in" command
  235. */
  236. free(dupline);
  237. ast_unlock_context(c);
  238. ast_unlock_contexts();
  239. return strdup("in");
  240. }
  241. i = ast_walk_context_includes(c, i);
  242. }
  243. ast_unlock_context(c);
  244. c = ast_walk_contexts(c);
  245. }
  246. free(dupline);
  247. ast_unlock_contexts();
  248. return NULL;
  249. }
  250. /*
  251. * Context from which we removing include ...
  252. */
  253. if (pos == 4) {
  254. struct ast_context *c;
  255. char *context, *dupline, *duplinet, *in;
  256. if (!(dupline = strdup(line))) {
  257. ast_log(LOG_ERROR, "Out of free memory\n");
  258. return NULL;
  259. }
  260. duplinet = dupline;
  261. strsep(&duplinet, " "); /* skip 'dont' */
  262. strsep(&duplinet, " "); /* skip 'include' */
  263. if (!(context = strsep(&duplinet, " "))) {
  264. free(dupline);
  265. return NULL;
  266. }
  267. /* third word must be in */
  268. in = strsep(&duplinet, " ");
  269. if (!in ||
  270. strcmp(in, "in")) {
  271. free(dupline);
  272. return NULL;
  273. }
  274. if (ast_lock_contexts()) {
  275. ast_log(LOG_ERROR, "Failed to lock context list\n");
  276. free(dupline);
  277. return NULL;
  278. }
  279. /* walk through all contexts ... */
  280. c = ast_walk_contexts(NULL);
  281. while (c) {
  282. struct ast_include *i;
  283. if (ast_lock_context(c)) {
  284. free(dupline);
  285. return NULL;
  286. }
  287. /* walk through all includes and check if it is our context */
  288. i = ast_walk_context_includes(c, NULL);
  289. while (i) {
  290. /* is in this context included another on which we want to
  291. * remove?
  292. */
  293. if (!strcmp(context, ast_get_include_name(i))) {
  294. /* yes, it's included, is matching our word too? */
  295. if (!strncmp(ast_get_context_name(c),
  296. word, strlen(word))) {
  297. /* check state for completion */
  298. if (++which > state) {
  299. char *res = strdup(ast_get_context_name(c));
  300. free(dupline);
  301. ast_unlock_context(c);
  302. ast_unlock_contexts();
  303. return res;
  304. }
  305. }
  306. break;
  307. }
  308. i = ast_walk_context_includes(c, i);
  309. }
  310. ast_unlock_context(c);
  311. c = ast_walk_contexts(c);
  312. }
  313. free(dupline);
  314. ast_unlock_contexts();
  315. return NULL;
  316. }
  317. return NULL;
  318. }
  319. /*
  320. * REMOVE EXTENSION command stuff
  321. */
  322. static int handle_context_remove_extension(int fd, int argc, char *argv[])
  323. {
  324. int removing_priority = 0;
  325. char *exten, *context;
  326. if (argc != 4 && argc != 3) return RESULT_SHOWUSAGE;
  327. /*
  328. * Priority input checking ...
  329. */
  330. if (argc == 4) {
  331. char *c = argv[3];
  332. /* check for digits in whole parameter for right priority ...
  333. * why? because atoi (strtol) returns 0 if any characters in
  334. * string and whole extension will be removed, it's not good
  335. */
  336. if (strcmp("hint", c)) {
  337. while (*c != '\0') {
  338. if (!isdigit(*c++)) {
  339. ast_cli(fd, "Invalid priority '%s'\n", argv[3]);
  340. return RESULT_FAILURE;
  341. }
  342. }
  343. removing_priority = atoi(argv[3]);
  344. } else
  345. removing_priority = PRIORITY_HINT;
  346. if (removing_priority == 0) {
  347. ast_cli(fd, "If you want to remove whole extension, please " \
  348. "omit priority argument\n");
  349. return RESULT_FAILURE;
  350. }
  351. }
  352. /*
  353. * Format exten@context checking ...
  354. */
  355. if (!(context = strchr(argv[2], (int)'@'))) {
  356. ast_cli(fd, "First argument must be in exten@context format\n");
  357. return RESULT_FAILURE;
  358. }
  359. *context++ = '\0';
  360. exten = argv[2];
  361. if ((!strlen(exten)) || (!(strlen(context)))) {
  362. ast_cli(fd, "Missing extension or context name in second argument '%s@%s'\n",
  363. exten == NULL ? "?" : exten, context == NULL ? "?" : context);
  364. return RESULT_FAILURE;
  365. }
  366. if (!ast_context_remove_extension(context, exten, removing_priority, registrar)) {
  367. if (!removing_priority)
  368. ast_cli(fd, "Whole extension %s@%s removed\n",
  369. exten, context);
  370. else
  371. ast_cli(fd, "Extension %s@%s with priority %d removed\n",
  372. exten, context, removing_priority);
  373. return RESULT_SUCCESS;
  374. }
  375. ast_cli(fd, "Failed to remove extension %s@%s\n", exten, context);
  376. return RESULT_FAILURE;
  377. }
  378. #define BROKEN_READLINE 1
  379. #ifdef BROKEN_READLINE
  380. /*
  381. * There is one funny thing, when you have word like 300@ and you hit
  382. * <tab>, you arguments will like as your word is '300 ', so it '@'
  383. * characters acts sometimes as word delimiter and sometimes as a part
  384. * of word
  385. *
  386. * This fix function, allocates new word variable and store here every
  387. * time xxx@yyy always as one word and correct pos is set too
  388. *
  389. * It's ugly, I know, but I'm waiting for Mark suggestion if upper is
  390. * bug or feature ...
  391. */
  392. static int fix_complete_args(char *line, char **word, int *pos)
  393. {
  394. char *_line, *_strsep_line, *_previous_word = NULL, *_word = NULL;
  395. int words = 0;
  396. _line = strdup(line);
  397. _strsep_line = _line;
  398. while (_strsep_line) {
  399. _previous_word = _word;
  400. _word = strsep(&_strsep_line, " ");
  401. if (_word && strlen(_word)) words++;
  402. }
  403. if (_word || _previous_word) {
  404. if (_word) {
  405. if (!strlen(_word)) words++;
  406. *word = strdup(_word);
  407. } else
  408. *word = strdup(_previous_word);
  409. *pos = words - 1;
  410. free(_line);
  411. return 0;
  412. }
  413. free(_line);
  414. return -1;
  415. }
  416. #endif /* BROKEN_READLINE */
  417. static char *complete_context_remove_extension(char *line, char *word, int pos,
  418. int state)
  419. {
  420. char *ret = NULL;
  421. int which = 0;
  422. #ifdef BROKEN_READLINE
  423. /*
  424. * Fix arguments, *word is a new allocated structure, REMEMBER to
  425. * free *word when you want to return from this function ...
  426. */
  427. if (fix_complete_args(line, &word, &pos)) {
  428. ast_log(LOG_ERROR, "Out of free memory\n");
  429. return NULL;
  430. }
  431. #endif
  432. /*
  433. * exten@context completion ...
  434. */
  435. if (pos == 2) {
  436. struct ast_context *c;
  437. struct ast_exten *e;
  438. char *context = NULL, *exten = NULL, *delim = NULL;
  439. /* now, parse values from word = exten@context */
  440. if ((delim = strchr(word, (int)'@'))) {
  441. /* check for duplicity ... */
  442. if (delim != strrchr(word, (int)'@')) {
  443. #ifdef BROKEN_READLINE
  444. free(word);
  445. #endif
  446. return NULL;
  447. }
  448. *delim = '\0';
  449. exten = strdup(word);
  450. context = strdup(delim + 1);
  451. *delim = '@';
  452. } else {
  453. exten = strdup(word);
  454. }
  455. #ifdef BROKEN_READLINE
  456. free(word);
  457. #endif
  458. if (ast_lock_contexts()) {
  459. ast_log(LOG_ERROR, "Failed to lock context list\n");
  460. free(context); free(exten);
  461. return NULL;
  462. }
  463. /* find our context ... */
  464. c = ast_walk_contexts(NULL);
  465. while (c) {
  466. /* our context? */
  467. if ( (!context || !strlen(context)) || /* if no input, all contexts ... */
  468. (context && !strncmp(ast_get_context_name(c),
  469. context, strlen(context))) ) { /* if input, compare ... */
  470. /* try to complete extensions ... */
  471. e = ast_walk_context_extensions(c, NULL);
  472. while (e) {
  473. /* our extension? */
  474. if ( (!exten || !strlen(exten)) || /* if not input, all extensions ... */
  475. (exten && !strncmp(ast_get_extension_name(e), exten,
  476. strlen(exten))) ) { /* if input, compare ... */
  477. if (++which > state) {
  478. /* If there is an extension then return
  479. * exten@context.
  480. */
  481. if (exten) {
  482. ret = malloc(strlen(ast_get_extension_name(e)) +
  483. strlen(ast_get_context_name(c)) + 2);
  484. if (ret)
  485. sprintf(ret, "%s@%s", ast_get_extension_name(e),
  486. ast_get_context_name(c));
  487. }
  488. free(exten); free(context);
  489. ast_unlock_contexts();
  490. return ret;
  491. }
  492. }
  493. e = ast_walk_context_extensions(c, e);
  494. }
  495. }
  496. c = ast_walk_contexts(c);
  497. }
  498. ast_unlock_contexts();
  499. free(exten); free(context);
  500. return NULL;
  501. }
  502. /*
  503. * Complete priority ...
  504. */
  505. if (pos == 3) {
  506. char *delim, *exten, *context, *dupline, *duplinet, *ec;
  507. struct ast_context *c;
  508. dupline = strdup(line);
  509. if (!dupline) {
  510. #ifdef BROKEN_READLINE
  511. free(word);
  512. #endif
  513. return NULL;
  514. }
  515. duplinet = dupline;
  516. strsep(&duplinet, " "); /* skip 'remove' */
  517. strsep(&duplinet, " "); /* skip 'extension */
  518. if (!(ec = strsep(&duplinet, " "))) {
  519. free(dupline);
  520. #ifdef BROKEN_READLINE
  521. free(word);
  522. #endif
  523. return NULL;
  524. }
  525. /* wrong exten@context format? */
  526. if (!(delim = strchr(ec, (int)'@')) ||
  527. (strchr(ec, (int)'@') != strrchr(ec, (int)'@'))) {
  528. #ifdef BROKEN_READLINE
  529. free(word);
  530. #endif
  531. free(dupline);
  532. return NULL;
  533. }
  534. /* check if there is exten and context too ... */
  535. *delim = '\0';
  536. if ((!strlen(ec)) || (!strlen(delim + 1))) {
  537. #ifdef BROKEN_READLINE
  538. free(word);
  539. #endif
  540. free(dupline);
  541. return NULL;
  542. }
  543. exten = strdup(ec);
  544. context = strdup(delim + 1);
  545. free(dupline);
  546. if (ast_lock_contexts()) {
  547. ast_log(LOG_ERROR, "Failed to lock context list\n");
  548. #ifdef BROKEN_READLINE
  549. free(word);
  550. #endif
  551. free(exten); free(context);
  552. return NULL;
  553. }
  554. /* walk contexts */
  555. c = ast_walk_contexts(NULL);
  556. while (c) {
  557. if (!strcmp(ast_get_context_name(c), context)) {
  558. struct ast_exten *e;
  559. /* walk extensions */
  560. free(context);
  561. e = ast_walk_context_extensions(c, NULL);
  562. while (e) {
  563. if (!strcmp(ast_get_extension_name(e), exten)) {
  564. struct ast_exten *priority;
  565. char buffer[10];
  566. free(exten);
  567. priority = ast_walk_extension_priorities(e, NULL);
  568. /* serve priorities */
  569. do {
  570. snprintf(buffer, 10, "%u",
  571. ast_get_extension_priority(priority));
  572. if (!strncmp(word, buffer, strlen(word))) {
  573. if (++which > state) {
  574. #ifdef BROKEN_READLINE
  575. free(word);
  576. #endif
  577. ast_unlock_contexts();
  578. return strdup(buffer);
  579. }
  580. }
  581. priority = ast_walk_extension_priorities(e,
  582. priority);
  583. } while (priority);
  584. #ifdef BROKEN_READLINE
  585. free(word);
  586. #endif
  587. ast_unlock_contexts();
  588. return NULL;
  589. }
  590. e = ast_walk_context_extensions(c, e);
  591. }
  592. #ifdef BROKEN_READLINE
  593. free(word);
  594. #endif
  595. free(exten);
  596. ast_unlock_contexts();
  597. return NULL;
  598. }
  599. c = ast_walk_contexts(c);
  600. }
  601. #ifdef BROKEN_READLINE
  602. free(word);
  603. #endif
  604. free(exten); free(context);
  605. ast_unlock_contexts();
  606. return NULL;
  607. }
  608. #ifdef BROKEN_READLINE
  609. free(word);
  610. #endif
  611. return NULL;
  612. }
  613. /*
  614. * Include context ...
  615. */
  616. static int handle_context_add_include(int fd, int argc, char *argv[])
  617. {
  618. if (argc != 5) return RESULT_SHOWUSAGE;
  619. /* third arg must be 'in' ... */
  620. if (strcmp(argv[3], "in")) return RESULT_SHOWUSAGE;
  621. if (ast_context_add_include(argv[4], argv[2], registrar)) {
  622. switch (errno) {
  623. case ENOMEM:
  624. ast_cli(fd, "Out of memory for context addition\n"); break;
  625. case EBUSY:
  626. ast_cli(fd, "Failed to lock context(s) list, please try again later\n"); break;
  627. case EEXIST:
  628. ast_cli(fd, "Context '%s' already included in '%s' context\n",
  629. argv[1], argv[3]); break;
  630. case ENOENT:
  631. case EINVAL:
  632. ast_cli(fd, "There is no existence of context '%s'\n",
  633. errno == ENOENT ? argv[4] : argv[2]); break;
  634. default:
  635. ast_cli(fd, "Failed to include '%s' in '%s' context\n",
  636. argv[1], argv[3]); break;
  637. }
  638. return RESULT_FAILURE;
  639. }
  640. /* show some info ... */
  641. ast_cli(fd, "Context '%s' included in '%s' context\n",
  642. argv[2], argv[3]);
  643. return RESULT_SUCCESS;
  644. }
  645. static char *complete_context_add_include(char *line, char *word, int pos,
  646. int state)
  647. {
  648. struct ast_context *c;
  649. int which = 0;
  650. /* server context for inclusion ... */
  651. if (pos == 1)
  652. {
  653. if (ast_lock_contexts()) {
  654. ast_log(LOG_ERROR, "Failed to lock context list\n");
  655. return NULL;
  656. }
  657. /* server all contexts */
  658. c = ast_walk_contexts(NULL);
  659. while (c) {
  660. if ((!strlen(word) ||
  661. !strncmp(ast_get_context_name(c), word, strlen(word))) &&
  662. ++which > state)
  663. {
  664. char *context = strdup(ast_get_context_name(c));
  665. ast_unlock_contexts();
  666. return context;
  667. }
  668. c = ast_walk_contexts(c);
  669. }
  670. ast_unlock_contexts();
  671. }
  672. /* complete 'in' only if context exist ... */
  673. if (pos == 2)
  674. {
  675. char *context, *dupline, *duplinet;
  676. if (state != 0) return NULL;
  677. /* parse context from line ... */
  678. if (!(dupline = strdup(line))) {
  679. ast_log(LOG_ERROR, "Out of free memory\n");
  680. if (state == 0) return strdup("in");
  681. return NULL;
  682. }
  683. duplinet = dupline;
  684. strsep(&duplinet, " ");
  685. context = strsep(&duplinet, " ");
  686. if (context) {
  687. struct ast_context *c;
  688. int context_existence = 0;
  689. /* check for context existence ... */
  690. if (ast_lock_contexts()) {
  691. ast_log(LOG_ERROR, "Failed to lock context list\n");
  692. free(dupline);
  693. /* our fault, we can't check, so complete 'in' ... */
  694. return strdup("in");
  695. }
  696. c = ast_walk_contexts(NULL);
  697. while (c && !context_existence) {
  698. if (!strcmp(context, ast_get_context_name(c))) {
  699. context_existence = 1;
  700. continue;
  701. }
  702. c = ast_walk_contexts(c);
  703. }
  704. /* if context exists, return 'into' ... */
  705. if (context_existence) {
  706. free(dupline);
  707. ast_unlock_contexts();
  708. return strdup("into");
  709. }
  710. ast_unlock_contexts();
  711. }
  712. free(dupline);
  713. return NULL;
  714. }
  715. /* serve context into which we include another context */
  716. if (pos == 3)
  717. {
  718. char *context, *dupline, *duplinet, *in;
  719. int context_existence = 0;
  720. if (!(dupline = strdup(line))) {
  721. ast_log(LOG_ERROR, "Out of free memory\n");
  722. return NULL;
  723. }
  724. duplinet = dupline;
  725. strsep(&duplinet, " "); /* skip 'include' */
  726. context = strsep(&duplinet, " ");
  727. in = strsep(&duplinet, " ");
  728. /* given some context and third word is in? */
  729. if (!strlen(context) || strcmp(in, "in")) {
  730. free(dupline);
  731. return NULL;
  732. }
  733. if (ast_lock_contexts()) {
  734. ast_log(LOG_ERROR, "Failed to lock context list\n");
  735. free(dupline);
  736. return NULL;
  737. }
  738. /* check for context existence ... */
  739. c = ast_walk_contexts(NULL);
  740. while (c && !context_existence) {
  741. if (!strcmp(context, ast_get_context_name(c))) {
  742. context_existence = 1;
  743. continue;
  744. }
  745. c = ast_walk_contexts(c);
  746. }
  747. if (!context_existence) {
  748. free(dupline);
  749. ast_unlock_contexts();
  750. return NULL;
  751. }
  752. /* go through all contexts ... */
  753. c = ast_walk_contexts(NULL);
  754. while (c) {
  755. /* must be different contexts ... */
  756. if (strcmp(context, ast_get_context_name(c))) {
  757. if (!ast_lock_context(c)) {
  758. struct ast_include *i;
  759. int included = 0;
  760. /* check for duplicity inclusion ... */
  761. i = ast_walk_context_includes(c, NULL);
  762. while (i && !included) {
  763. if (!strcmp(ast_get_include_name(i), context))
  764. included = 1;
  765. i = ast_walk_context_includes(c, i);
  766. }
  767. ast_unlock_context(c);
  768. /* not included yet, so show possibility ... */
  769. if (!included &&
  770. !strncmp(ast_get_context_name(c), word, strlen(word))){
  771. if (++which > state) {
  772. char *res = strdup(ast_get_context_name(c));
  773. free(dupline);
  774. ast_unlock_contexts();
  775. return res;
  776. }
  777. }
  778. }
  779. }
  780. c = ast_walk_contexts(c);
  781. }
  782. ast_unlock_contexts();
  783. free(dupline);
  784. return NULL;
  785. }
  786. return NULL;
  787. }
  788. /*
  789. * 'save dialplan' CLI command implementation functions ...
  790. */
  791. static int handle_save_dialplan(int fd, int argc, char *argv[])
  792. {
  793. char filename[256];
  794. struct ast_context *c;
  795. struct ast_config *cfg;
  796. struct ast_variable *v;
  797. int context_header_written;
  798. int incomplete = 0; /* incomplete config write? */
  799. FILE *output;
  800. if (! (static_config && !write_protect_config)) {
  801. ast_cli(fd,
  802. "I can't save dialplan now, see '%s' example file.\n",
  803. config);
  804. return RESULT_FAILURE;
  805. }
  806. if (argc != 2 && argc != 3) return RESULT_SHOWUSAGE;
  807. if (ast_mutex_lock(&save_dialplan_lock)) {
  808. ast_cli(fd,
  809. "Failed to lock dialplan saving (another proccess saving?)\n");
  810. return RESULT_FAILURE;
  811. }
  812. /* have config path? */
  813. if (argc == 3) {
  814. /* is there extension.conf too? */
  815. if (!strstr(argv[2], ".conf")) {
  816. /* no, only directory path, check for last '/' occurence */
  817. if (*(argv[2] + strlen(argv[2]) -1) == '/')
  818. snprintf(filename, sizeof(filename), "%s%s",
  819. argv[2], config);
  820. else
  821. /* without config extensions.conf, add it */
  822. snprintf(filename, sizeof(filename), "%s/%s",
  823. argv[2], config);
  824. } else
  825. /* there is an .conf */
  826. snprintf(filename, sizeof(filename), argv[2]);
  827. } else
  828. /* no config file, default one */
  829. snprintf(filename, sizeof(filename), "%s/%s",
  830. (char *)ast_config_AST_CONFIG_DIR, config);
  831. cfg = ast_load("extensions.conf");
  832. /* try to lock contexts list */
  833. if (ast_lock_contexts()) {
  834. ast_cli(fd, "Failed to lock contexts list\n");
  835. ast_mutex_unlock(&save_dialplan_lock);
  836. ast_destroy(cfg);
  837. return RESULT_FAILURE;
  838. }
  839. /* create new file ... */
  840. if (!(output = fopen(filename, "wt"))) {
  841. ast_cli(fd, "Failed to create file '%s'\n",
  842. filename);
  843. ast_unlock_contexts();
  844. ast_mutex_unlock(&save_dialplan_lock);
  845. ast_destroy(cfg);
  846. return RESULT_FAILURE;
  847. }
  848. /* fireout general info */
  849. fprintf(output, "[general]\nstatic=%s\nwriteprotect=%s\n\n",
  850. static_config ? "yes" : "no",
  851. write_protect_config ? "yes" : "no");
  852. if ((v = ast_variable_browse(cfg, "globals"))) {
  853. fprintf(output, "[globals]\n");
  854. while(v) {
  855. fprintf(output, "%s => %s\n", v->name, v->value);
  856. v = v->next;
  857. }
  858. fprintf(output, "\n");
  859. }
  860. ast_destroy(cfg);
  861. /* walk all contexts */
  862. c = ast_walk_contexts(NULL);
  863. while (c) {
  864. context_header_written = 0;
  865. /* try to lock context and fireout all info */
  866. if (!ast_lock_context(c)) {
  867. struct ast_exten *e, *last_written_e = NULL;
  868. struct ast_include *i;
  869. struct ast_ignorepat *ip;
  870. struct ast_sw *sw;
  871. /* registered by this module? */
  872. if (!strcmp(ast_get_context_registrar(c), registrar)) {
  873. fprintf(output, "[%s]\n", ast_get_context_name(c));
  874. context_header_written = 1;
  875. }
  876. /* walk extensions ... */
  877. e = ast_walk_context_extensions(c, NULL);
  878. while (e) {
  879. struct ast_exten *p;
  880. /* fireout priorities */
  881. p = ast_walk_extension_priorities(e, NULL);
  882. while (p) {
  883. if (!strcmp(ast_get_extension_registrar(p),
  884. registrar)) {
  885. /* make empty line between different extensions */
  886. if (last_written_e != NULL &&
  887. strcmp(ast_get_extension_name(last_written_e),
  888. ast_get_extension_name(p)))
  889. fprintf(output, "\n");
  890. last_written_e = p;
  891. if (!context_header_written) {
  892. fprintf(output, "[%s]\n", ast_get_context_name(c));
  893. context_header_written = 1;
  894. }
  895. if (ast_get_extension_priority(p)!=PRIORITY_HINT) {
  896. char *tempdata = NULL, *startdata;
  897. tempdata = strdup((char *)ast_get_extension_app_data(p));
  898. if (tempdata) {
  899. startdata = tempdata;
  900. while (*tempdata) {
  901. if (*tempdata == '|')
  902. *tempdata = ',';
  903. tempdata++;
  904. }
  905. tempdata = startdata;
  906. }
  907. if (ast_get_extension_matchcid(p))
  908. fprintf(output, "exten => %s/%s,%d,%s(%s)\n",
  909. ast_get_extension_name(p),
  910. ast_get_extension_cidmatch(p),
  911. ast_get_extension_priority(p),
  912. ast_get_extension_app(p),
  913. tempdata);
  914. else
  915. fprintf(output, "exten => %s,%d,%s(%s)\n",
  916. ast_get_extension_name(p),
  917. ast_get_extension_priority(p),
  918. ast_get_extension_app(p),
  919. tempdata);
  920. if (tempdata)
  921. free(tempdata);
  922. } else
  923. fprintf(output, "exten => %s,hint,%s\n",
  924. ast_get_extension_name(p),
  925. ast_get_extension_app(p));
  926. }
  927. p = ast_walk_extension_priorities(e, p);
  928. }
  929. e = ast_walk_context_extensions(c, e);
  930. }
  931. /* written any extensions? ok, write space between exten & inc */
  932. if (last_written_e) fprintf(output, "\n");
  933. /* walk through includes */
  934. i = ast_walk_context_includes(c, NULL);
  935. while (i) {
  936. if (!strcmp(ast_get_include_registrar(i), registrar)) {
  937. if (!context_header_written) {
  938. fprintf(output, "[%s]\n", ast_get_context_name(c));
  939. context_header_written = 1;
  940. }
  941. fprintf(output, "include => %s\n",
  942. ast_get_include_name(i));
  943. }
  944. i = ast_walk_context_includes(c, i);
  945. }
  946. if (ast_walk_context_includes(c, NULL))
  947. fprintf(output, "\n");
  948. /* walk through switches */
  949. sw = ast_walk_context_switches(c, NULL);
  950. while (sw) {
  951. if (!strcmp(ast_get_switch_registrar(sw), registrar)) {
  952. if (!context_header_written) {
  953. fprintf(output, "[%s]\n", ast_get_context_name(c));
  954. context_header_written = 1;
  955. }
  956. fprintf(output, "switch => %s/%s\n",
  957. ast_get_switch_name(sw),
  958. ast_get_switch_data(sw));
  959. }
  960. sw = ast_walk_context_switches(c, sw);
  961. }
  962. if (ast_walk_context_switches(c, NULL))
  963. fprintf(output, "\n");
  964. /* fireout ignorepats ... */
  965. ip = ast_walk_context_ignorepats(c, NULL);
  966. while (ip) {
  967. if (!strcmp(ast_get_ignorepat_registrar(ip), registrar)) {
  968. if (!context_header_written) {
  969. fprintf(output, "[%s]\n", ast_get_context_name(c));
  970. context_header_written = 1;
  971. }
  972. fprintf(output, "ignorepat => %s\n",
  973. ast_get_ignorepat_name(ip));
  974. }
  975. ip = ast_walk_context_ignorepats(c, ip);
  976. }
  977. ast_unlock_context(c);
  978. } else
  979. incomplete = 1;
  980. c = ast_walk_contexts(c);
  981. }
  982. ast_unlock_contexts();
  983. ast_mutex_unlock(&save_dialplan_lock);
  984. fclose(output);
  985. if (incomplete) {
  986. ast_cli(fd, "Saved dialplan is incomplete\n");
  987. return RESULT_FAILURE;
  988. }
  989. ast_cli(fd, "Dialplane successfully saved into '%s'\n",
  990. filename);
  991. return RESULT_SUCCESS;
  992. }
  993. /*
  994. * ADD EXTENSION command stuff
  995. */
  996. static int handle_context_add_extension(int fd, int argc, char *argv[])
  997. {
  998. char *whole_exten;
  999. char *exten, *prior;
  1000. int iprior = -2;
  1001. char *cidmatch, *app, *app_data;
  1002. char *start, *end;
  1003. /* check for arguments at first */
  1004. if (argc != 5 && argc != 6) return RESULT_SHOWUSAGE;
  1005. if (strcmp(argv[3], "into")) return RESULT_SHOWUSAGE;
  1006. if (argc == 6) if (strcmp(argv[5], "replace")) return RESULT_SHOWUSAGE;
  1007. whole_exten = argv[2];
  1008. exten = strsep(&whole_exten,",");
  1009. if (strchr(exten, '/')) {
  1010. cidmatch = exten;
  1011. strsep(&cidmatch,"/");
  1012. } else {
  1013. cidmatch = NULL;
  1014. }
  1015. prior = strsep(&whole_exten,",");
  1016. if (prior) {
  1017. if (!strcmp(prior, "hint")) {
  1018. iprior = PRIORITY_HINT;
  1019. } else {
  1020. if (sscanf(prior, "%i", &iprior) != 1) {
  1021. ast_cli(fd, "'%s' is not a valid priority\n", prior);
  1022. prior = NULL;
  1023. }
  1024. }
  1025. }
  1026. app = whole_exten;
  1027. if (app && (start = strchr(app, '(')) && (end = strrchr(app, ')'))) {
  1028. *start = *end = '\0';
  1029. app_data = start + 1;
  1030. process_quotes_and_slashes(app_data, ',', '|');
  1031. } else {
  1032. if (app) {
  1033. app_data = strchr(app, ',');
  1034. if (app_data) {
  1035. *app_data = '\0';
  1036. app_data++;
  1037. }
  1038. } else
  1039. app_data = NULL;
  1040. }
  1041. if (!exten || !prior || !app || (!app_data && iprior != PRIORITY_HINT)) return RESULT_SHOWUSAGE;
  1042. if (!app_data)
  1043. app_data="";
  1044. if (ast_add_extension(argv[4], argc == 6 ? 1 : 0, exten, iprior, cidmatch, app,
  1045. (void *)strdup(app_data), free, registrar)) {
  1046. switch (errno) {
  1047. case ENOMEM:
  1048. ast_cli(fd, "Out of free memory\n"); break;
  1049. case EBUSY:
  1050. ast_cli(fd, "Failed to lock context(s) list, please try again later\n"); break;
  1051. case ENOENT:
  1052. ast_cli(fd, "No existence of '%s' context\n", argv[4]); break;
  1053. case EEXIST:
  1054. ast_cli(fd, "Extension %s@%s with priority %s already exists\n",
  1055. exten, argv[4], prior); break;
  1056. default:
  1057. ast_cli(fd, "Failed to add '%s,%s,%s,%s' extension into '%s' context\n",
  1058. exten, prior, app, app_data, argv[4]); break;
  1059. }
  1060. return RESULT_FAILURE;
  1061. }
  1062. if (argc == 6)
  1063. ast_cli(fd, "Extension %s@%s (%s) replace by '%s,%s,%s,%s'\n",
  1064. exten, argv[4], prior, exten, prior, app, app_data);
  1065. else
  1066. ast_cli(fd, "Extension '%s,%s,%s,%s' added into '%s' context\n",
  1067. exten, prior, app, app_data, argv[4]);
  1068. return RESULT_SUCCESS;
  1069. }
  1070. /* add extension 6123,1,Dial,IAX/212.71.138.13/6123 into local */
  1071. static char *complete_context_add_extension(char *line, char *word,
  1072. int pos, int state)
  1073. {
  1074. int which = 0;
  1075. /* complete 'into' word ... */
  1076. if (pos == 3) {
  1077. if (state == 0) return strdup("into");
  1078. return NULL;
  1079. }
  1080. /* complete context */
  1081. if (pos == 4) {
  1082. struct ast_context *c;
  1083. /* try to lock contexts list ... */
  1084. if (ast_lock_contexts()) {
  1085. ast_log(LOG_WARNING, "Failed to lock contexts list\n");
  1086. return NULL;
  1087. }
  1088. /* walk through all contexts */
  1089. c = ast_walk_contexts(NULL);
  1090. while (c) {
  1091. /* matching context? */
  1092. if (!strncmp(ast_get_context_name(c), word, strlen(word))) {
  1093. if (++which > state) {
  1094. char *res = strdup(ast_get_context_name(c));
  1095. ast_unlock_contexts();
  1096. return res;
  1097. }
  1098. }
  1099. c = ast_walk_contexts(c);
  1100. }
  1101. ast_unlock_contexts();
  1102. return NULL;
  1103. }
  1104. if (pos == 5) return state == 0 ? strdup("replace") : NULL;
  1105. return NULL;
  1106. }
  1107. /*
  1108. * IGNOREPAT CLI stuff
  1109. */
  1110. static int handle_context_add_ignorepat(int fd, int argc, char *argv[])
  1111. {
  1112. if (argc != 5) return RESULT_SHOWUSAGE;
  1113. if (strcmp(argv[3], "into")) return RESULT_SHOWUSAGE;
  1114. if (ast_context_add_ignorepat(argv[4], argv[2], registrar)) {
  1115. switch (errno) {
  1116. case ENOMEM:
  1117. ast_cli(fd, "Out of free memory\n"); break;
  1118. case ENOENT:
  1119. ast_cli(fd, "There is no existence of '%s' context\n", argv[4]);
  1120. break;
  1121. case EEXIST:
  1122. ast_cli(fd, "Ignore pattern '%s' already included in '%s' context\n",
  1123. argv[2], argv[4]);
  1124. break;
  1125. case EBUSY:
  1126. ast_cli(fd, "Failed to lock context(s) list, please, try again later\n");
  1127. break;
  1128. default:
  1129. ast_cli(fd, "Failed to add ingore pattern '%s' into '%s' context\n",
  1130. argv[2], argv[4]);
  1131. break;
  1132. }
  1133. return RESULT_FAILURE;
  1134. }
  1135. ast_cli(fd, "Ignore pattern '%s' added into '%s' context\n",
  1136. argv[2], argv[4]);
  1137. return RESULT_SUCCESS;
  1138. }
  1139. static char *complete_context_add_ignorepat(char *line, char *word,
  1140. int pos, int state)
  1141. {
  1142. if (pos == 3) return state == 0 ? strdup("into") : NULL;
  1143. if (pos == 4) {
  1144. struct ast_context *c;
  1145. int which = 0;
  1146. char *dupline, *duplinet, *ignorepat = NULL;
  1147. dupline = strdup(line);
  1148. duplinet = dupline;
  1149. if (duplinet) {
  1150. strsep(&duplinet, " "); /* skip 'add' */
  1151. strsep(&duplinet, " "); /* skip 'ignorepat' */
  1152. ignorepat = strsep(&duplinet, " ");
  1153. }
  1154. if (ast_lock_contexts()) {
  1155. ast_log(LOG_ERROR, "Failed to lock contexts list\n");
  1156. return NULL;
  1157. }
  1158. c = ast_walk_contexts(NULL);
  1159. while (c) {
  1160. if (!strncmp(ast_get_context_name(c), word, strlen(word))) {
  1161. int serve_context = 1;
  1162. if (ignorepat) {
  1163. if (!ast_lock_context(c)) {
  1164. struct ast_ignorepat *ip;
  1165. ip = ast_walk_context_ignorepats(c, NULL);
  1166. while (ip && serve_context) {
  1167. if (!strcmp(ast_get_ignorepat_name(ip), ignorepat))
  1168. serve_context = 0;
  1169. ip = ast_walk_context_ignorepats(c, ip);
  1170. }
  1171. ast_unlock_context(c);
  1172. }
  1173. }
  1174. if (serve_context) {
  1175. if (++which > state) {
  1176. char *context = strdup(ast_get_context_name(c));
  1177. if (dupline) free(dupline);
  1178. ast_unlock_contexts();
  1179. return context;
  1180. }
  1181. }
  1182. }
  1183. c = ast_walk_contexts(c);
  1184. }
  1185. if (dupline) free(dupline);
  1186. ast_unlock_contexts();
  1187. return NULL;
  1188. }
  1189. return NULL;
  1190. }
  1191. static int handle_context_remove_ignorepat(int fd, int argc, char *argv[])
  1192. {
  1193. if (argc != 5) return RESULT_SHOWUSAGE;
  1194. if (strcmp(argv[3], "from")) return RESULT_SHOWUSAGE;
  1195. if (ast_context_remove_ignorepat(argv[4], argv[2], registrar)) {
  1196. switch (errno) {
  1197. case EBUSY:
  1198. ast_cli(fd, "Failed to lock context(s) list, please try again later\n");
  1199. break;
  1200. case ENOENT:
  1201. ast_cli(fd, "There is no existence of '%s' context\n", argv[4]);
  1202. break;
  1203. case EINVAL:
  1204. ast_cli(fd, "There is no existence of '%s' ignore pattern in '%s' context\n",
  1205. argv[2], argv[4]);
  1206. break;
  1207. default:
  1208. ast_cli(fd, "Failed to remove ignore pattern '%s' from '%s' context\n", argv[2], argv[4]);
  1209. break;
  1210. }
  1211. return RESULT_FAILURE;
  1212. }
  1213. ast_cli(fd, "Ignore pattern '%s' removed from '%s' context\n",
  1214. argv[2], argv[4]);
  1215. return RESULT_SUCCESS;
  1216. }
  1217. static int pbx_load_module(void);
  1218. static int handle_reload_extensions(int fd, int argc, char *argv[])
  1219. {
  1220. if (argc!=2) return RESULT_SHOWUSAGE;
  1221. pbx_load_module();
  1222. return RESULT_SUCCESS;
  1223. }
  1224. static char *complete_context_remove_ignorepat(char *line, char *word,
  1225. int pos, int state)
  1226. {
  1227. struct ast_context *c;
  1228. int which = 0;
  1229. if (pos == 2) {
  1230. if (ast_lock_contexts()) {
  1231. ast_log(LOG_WARNING, "Failed to lock contexts list\n");
  1232. return NULL;
  1233. }
  1234. c = ast_walk_contexts(NULL);
  1235. while (c) {
  1236. if (!ast_lock_context(c)) {
  1237. struct ast_ignorepat *ip;
  1238. ip = ast_walk_context_ignorepats(c, NULL);
  1239. while (ip) {
  1240. if (!strncmp(ast_get_ignorepat_name(ip), word, strlen(word))) {
  1241. if (which + 1 > state) {
  1242. struct ast_context *cw;
  1243. int already_served = 0;
  1244. cw = ast_walk_contexts(NULL);
  1245. while (cw && cw != c && !already_served) {
  1246. if (!ast_lock_context(cw)) {
  1247. struct ast_ignorepat *ipw;
  1248. ipw = ast_walk_context_ignorepats(cw, NULL);
  1249. while (ipw) {
  1250. if (!strcmp(ast_get_ignorepat_name(ipw),
  1251. ast_get_ignorepat_name(ip))) already_served = 1;
  1252. ipw = ast_walk_context_ignorepats(cw, ipw);
  1253. }
  1254. ast_unlock_context(cw);
  1255. }
  1256. cw = ast_walk_contexts(cw);
  1257. }
  1258. if (!already_served) {
  1259. char *ret = strdup(ast_get_ignorepat_name(ip));
  1260. ast_unlock_context(c);
  1261. ast_unlock_contexts();
  1262. return ret;
  1263. }
  1264. } else
  1265. which++;
  1266. }
  1267. ip = ast_walk_context_ignorepats(c, ip);
  1268. }
  1269. ast_unlock_context(c);
  1270. }
  1271. c = ast_walk_contexts(c);
  1272. }
  1273. ast_unlock_contexts();
  1274. return NULL;
  1275. }
  1276. if (pos == 3) return state == 0 ? strdup("from") : NULL;
  1277. if (pos == 4) {
  1278. char *dupline, *duplinet, *ignorepat;
  1279. dupline = strdup(line);
  1280. if (!dupline) {
  1281. ast_log(LOG_WARNING, "Out of free memory\n");
  1282. return NULL;
  1283. }
  1284. duplinet = dupline;
  1285. strsep(&duplinet, " ");
  1286. strsep(&duplinet, " ");
  1287. ignorepat = strsep(&duplinet, " ");
  1288. if (!ignorepat) {
  1289. free(dupline);
  1290. return NULL;
  1291. }
  1292. if (ast_lock_contexts()) {
  1293. ast_log(LOG_WARNING, "Failed to lock contexts list\n");
  1294. free(dupline);
  1295. return NULL;
  1296. }
  1297. c = ast_walk_contexts(NULL);
  1298. while (c) {
  1299. if (!ast_lock_context(c)) {
  1300. struct ast_ignorepat *ip;
  1301. ip = ast_walk_context_ignorepats(c, NULL);
  1302. while (ip) {
  1303. if (!strcmp(ast_get_ignorepat_name(ip), ignorepat)) {
  1304. if (!strncmp(ast_get_context_name(c), word, strlen(word))) {
  1305. if (++which > state) {
  1306. char *ret = strdup(ast_get_context_name(c));
  1307. free(dupline);
  1308. ast_unlock_context(c);
  1309. ast_unlock_contexts();
  1310. return ret;
  1311. }
  1312. }
  1313. }
  1314. ip = ast_walk_context_ignorepats(c, ip);
  1315. }
  1316. ast_unlock_context(c);
  1317. }
  1318. c = ast_walk_contexts(c);
  1319. }
  1320. free(dupline);
  1321. ast_unlock_contexts();
  1322. return NULL;
  1323. }
  1324. return NULL;
  1325. }
  1326. /*
  1327. * CLI entries for commands provided by this module
  1328. */
  1329. static struct ast_cli_entry context_dont_include_cli =
  1330. { { "dont", "include", NULL }, handle_context_dont_include,
  1331. "Remove a specified include from context", context_dont_include_help,
  1332. complete_context_dont_include };
  1333. static struct ast_cli_entry context_remove_extension_cli =
  1334. { { "remove", "extension", NULL }, handle_context_remove_extension,
  1335. "Remove a specified extension", context_remove_extension_help,
  1336. complete_context_remove_extension };
  1337. static struct ast_cli_entry context_add_include_cli =
  1338. { { "include", "context", NULL }, handle_context_add_include,
  1339. "Include context in other context", context_add_include_help,
  1340. complete_context_add_include };
  1341. static struct ast_cli_entry save_dialplan_cli =
  1342. { { "save", "dialplan", NULL }, handle_save_dialplan,
  1343. "Save dialplan", save_dialplan_help };
  1344. static struct ast_cli_entry context_add_extension_cli =
  1345. { { "add", "extension", NULL }, handle_context_add_extension,
  1346. "Add new extension into context", context_add_extension_help,
  1347. complete_context_add_extension };
  1348. static struct ast_cli_entry context_add_ignorepat_cli =
  1349. { { "add", "ignorepat", NULL }, handle_context_add_ignorepat,
  1350. "Add new ignore pattern", context_add_ignorepat_help,
  1351. complete_context_add_ignorepat };
  1352. static struct ast_cli_entry context_remove_ignorepat_cli =
  1353. { { "remove", "ignorepat", NULL }, handle_context_remove_ignorepat,
  1354. "Remove ignore pattern from context", context_remove_ignorepat_help,
  1355. complete_context_remove_ignorepat };
  1356. static struct ast_cli_entry reload_extensions_cli =
  1357. { { "extensions", "reload", NULL}, handle_reload_extensions,
  1358. "Reload extensions and *only* extensions", reload_extensions_help };
  1359. /*
  1360. * Standard module functions ...
  1361. */
  1362. int unload_module(void)
  1363. {
  1364. ast_cli_unregister(&context_add_extension_cli);
  1365. if (static_config && !write_protect_config)
  1366. ast_cli_unregister(&save_dialplan_cli);
  1367. ast_cli_unregister(&context_add_include_cli);
  1368. ast_cli_unregister(&context_dont_include_cli);
  1369. ast_cli_unregister(&context_remove_extension_cli);
  1370. ast_cli_unregister(&context_remove_ignorepat_cli);
  1371. ast_cli_unregister(&context_add_ignorepat_cli);
  1372. ast_cli_unregister(&reload_extensions_cli);
  1373. ast_context_destroy(NULL, registrar);
  1374. return 0;
  1375. }
  1376. static int pbx_load_module(void)
  1377. {
  1378. struct ast_config *cfg;
  1379. struct ast_variable *v;
  1380. char *cxt, *ext, *pri, *appl, *data, *tc, *cidmatch;
  1381. struct ast_context *con;
  1382. char *start, *end;
  1383. char realvalue[256];
  1384. cfg = ast_load(config);
  1385. if (cfg) {
  1386. /* Use existing config to populate the PBX table */
  1387. static_config = ast_true(ast_variable_retrieve(cfg, "general",
  1388. "static"));
  1389. write_protect_config = ast_true(ast_variable_retrieve(cfg, "general",
  1390. "writeprotect"));
  1391. v = ast_variable_browse(cfg, "globals");
  1392. while(v) {
  1393. memset(realvalue, 0, sizeof(realvalue));
  1394. pbx_substitute_variables_helper(NULL, v->value, realvalue, sizeof(realvalue) - 1);
  1395. pbx_builtin_setvar_helper(NULL, v->name, realvalue);
  1396. v = v->next;
  1397. }
  1398. cxt = ast_category_browse(cfg, NULL);
  1399. while(cxt) {
  1400. /* All categories but "general" or "globals" are considered contexts */
  1401. if (!strcasecmp(cxt, "general") || !strcasecmp(cxt, "globals")) {
  1402. cxt = ast_category_browse(cfg, cxt);
  1403. continue;
  1404. }
  1405. if ((con=ast_context_create(&local_contexts,cxt, registrar))) {
  1406. v = ast_variable_browse(cfg, cxt);
  1407. while(v) {
  1408. if (!strcasecmp(v->name, "exten")) {
  1409. char *stringp=NULL;
  1410. int ipri = -2;
  1411. char realext[256]="";
  1412. tc = strdup(v->value);
  1413. if(tc!=NULL){
  1414. stringp=tc;
  1415. ext = strsep(&stringp, ",");
  1416. if (!ext)
  1417. ext="";
  1418. pri = strsep(&stringp, ",");
  1419. if (!pri)
  1420. pri="";
  1421. if (!strcmp(pri,"hint"))
  1422. ipri=PRIORITY_HINT;
  1423. else {
  1424. if (sscanf(pri, "%i", &ipri) != 1) {
  1425. ast_log(LOG_WARNING, "Invalid priority '%s' at line %d\n", pri, v->lineno);
  1426. ipri = 0;
  1427. }
  1428. }
  1429. appl = stringp;
  1430. if (!appl)
  1431. appl="";
  1432. if (!(start = strchr(appl, '('))) {
  1433. if (stringp)
  1434. appl = strsep(&stringp, ",");
  1435. else
  1436. appl = "";
  1437. }
  1438. if (start && (end = strrchr(appl, ')'))) {
  1439. *start = *end = '\0';
  1440. data = start + 1;
  1441. process_quotes_and_slashes(data, ',', '|');
  1442. } else if (stringp!=NULL && *stringp=='"') {
  1443. stringp++;
  1444. data = strsep(&stringp, "\"");
  1445. stringp++;
  1446. } else {
  1447. if (stringp)
  1448. data = strsep(&stringp, ",");
  1449. else
  1450. data = "";
  1451. }
  1452. pbx_substitute_variables_helper(NULL, ext, realext, sizeof(realext)-1);
  1453. stringp = realext;
  1454. ext = strsep(&stringp, "/");
  1455. cidmatch = stringp;
  1456. if (!data)
  1457. data="";
  1458. while(*appl && (*appl < 33)) appl++;
  1459. if (ipri) {
  1460. if (!strcmp(realext, "_."))
  1461. ast_log(LOG_WARNING, "The use of '_.' for an extension is strongly discouraged and can have unexpected behavior. Please use '_X.' instead at line %d\n", v->lineno);
  1462. if (ast_add_extension2(con, 0, realext, ipri, cidmatch, appl, strdup(data), FREE, registrar)) {
  1463. ast_log(LOG_WARNING, "Unable to register extension at line %d\n", v->lineno);
  1464. }
  1465. }
  1466. free(tc);
  1467. } else fprintf(stderr,"Error strdup returned NULL in %s\n",__PRETTY_FUNCTION__);
  1468. } else if(!strcasecmp(v->name, "include")) {
  1469. memset(realvalue, 0, sizeof(realvalue));
  1470. pbx_substitute_variables_helper(NULL, v->value, realvalue, sizeof(realvalue) - 1);
  1471. if (ast_context_add_include2(con, realvalue, registrar))
  1472. ast_log(LOG_WARNING, "Unable to include context '%s' in context '%s'\n", v->value, cxt);
  1473. } else if(!strcasecmp(v->name, "ignorepat")) {
  1474. memset(realvalue, 0, sizeof(realvalue));
  1475. pbx_substitute_variables_helper(NULL, v->value, realvalue, sizeof(realvalue) - 1);
  1476. if (ast_context_add_ignorepat2(con, realvalue, registrar))
  1477. ast_log(LOG_WARNING, "Unable to include ignorepat '%s' in context '%s'\n", v->value, cxt);
  1478. } else if (!strcasecmp(v->name, "switch")) {
  1479. char *stringp=NULL;
  1480. memset(realvalue, 0, sizeof(realvalue));
  1481. pbx_substitute_variables_helper(NULL, v->value, realvalue, sizeof(realvalue) - 1);
  1482. tc = realvalue;
  1483. stringp=tc;
  1484. appl = strsep(&stringp, "/");
  1485. data = strsep(&stringp, "");
  1486. if (!data)
  1487. data = "";
  1488. if (ast_context_add_switch2(con, appl, data, registrar))
  1489. ast_log(LOG_WARNING, "Unable to include switch '%s' in context '%s'\n", v->value, cxt);
  1490. }
  1491. v = v->next;
  1492. }
  1493. }
  1494. cxt = ast_category_browse(cfg, cxt);
  1495. }
  1496. ast_destroy(cfg);
  1497. }
  1498. ast_merge_contexts_and_delete(&local_contexts,registrar);
  1499. for (con = ast_walk_contexts(NULL); con; con = ast_walk_contexts(con))
  1500. ast_context_verify_includes(con);
  1501. return 0;
  1502. }
  1503. int load_module(void)
  1504. {
  1505. if (pbx_load_module()) return -1;
  1506. ast_cli_register(&context_remove_extension_cli);
  1507. ast_cli_register(&context_dont_include_cli);
  1508. ast_cli_register(&context_add_include_cli);
  1509. if (static_config && !write_protect_config)
  1510. ast_cli_register(&save_dialplan_cli);
  1511. ast_cli_register(&context_add_extension_cli);
  1512. ast_cli_register(&context_add_ignorepat_cli);
  1513. ast_cli_register(&context_remove_ignorepat_cli);
  1514. ast_cli_register(&reload_extensions_cli);
  1515. return 0;
  1516. }
  1517. int reload(void)
  1518. {
  1519. ast_context_destroy(NULL, registrar);
  1520. /* For martin's global variables, don't clear them on reload */
  1521. #if 0
  1522. pbx_builtin_clear_globals();
  1523. #endif
  1524. pbx_load_module();
  1525. return 0;
  1526. }
  1527. int usecount(void)
  1528. {
  1529. return 0;
  1530. }
  1531. char *description(void)
  1532. {
  1533. return dtext;
  1534. }
  1535. char *key(void)
  1536. {
  1537. return ASTERISK_GPL_KEY;
  1538. }