pbx_config.c 43 KB

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