pbx_app.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2016, CFWare, LLC
  5. *
  6. * Corey Farrell <git@cfware.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*! \file
  19. *
  20. * \brief Custom function management routines.
  21. *
  22. * \author Corey Farrell <git@cfware.com>
  23. */
  24. /*** MODULEINFO
  25. <support_level>core</support_level>
  26. ***/
  27. #include "asterisk.h"
  28. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  29. #include "asterisk/_private.h"
  30. #include "asterisk/cli.h"
  31. #include "asterisk/linkedlists.h"
  32. #include "asterisk/module.h"
  33. #include "asterisk/pbx.h"
  34. #include "asterisk/stasis_channels.h"
  35. #include "asterisk/strings.h"
  36. #include "asterisk/term.h"
  37. #include "asterisk/utils.h"
  38. #include "asterisk/xmldoc.h"
  39. #include "pbx_private.h"
  40. /*! \brief ast_app: A registered application */
  41. struct ast_app {
  42. int (*execute)(struct ast_channel *chan, const char *data);
  43. AST_DECLARE_STRING_FIELDS(
  44. AST_STRING_FIELD(synopsis); /*!< Synopsis text for 'show applications' */
  45. AST_STRING_FIELD(description); /*!< Description (help text) for 'show application &lt;name&gt;' */
  46. AST_STRING_FIELD(syntax); /*!< Syntax text for 'core show applications' */
  47. AST_STRING_FIELD(arguments); /*!< Arguments description */
  48. AST_STRING_FIELD(seealso); /*!< See also */
  49. );
  50. #ifdef AST_XML_DOCS
  51. enum ast_doc_src docsrc; /*!< Where the documentation come from. */
  52. #endif
  53. AST_RWLIST_ENTRY(ast_app) list; /*!< Next app in list */
  54. struct ast_module *module; /*!< Module this app belongs to */
  55. char name[0]; /*!< Name of the application */
  56. };
  57. /*!
  58. * \brief Registered applications container.
  59. *
  60. * It is sorted by application name.
  61. */
  62. static AST_RWLIST_HEAD_STATIC(apps, ast_app);
  63. static struct ast_app *pbx_findapp_nolock(const char *name)
  64. {
  65. struct ast_app *cur;
  66. int cmp;
  67. AST_RWLIST_TRAVERSE(&apps, cur, list) {
  68. cmp = strcasecmp(name, cur->name);
  69. if (cmp > 0) {
  70. continue;
  71. }
  72. if (!cmp) {
  73. /* Found it. */
  74. break;
  75. }
  76. /* Not in container. */
  77. cur = NULL;
  78. break;
  79. }
  80. return cur;
  81. }
  82. struct ast_app *pbx_findapp(const char *app)
  83. {
  84. struct ast_app *ret;
  85. AST_RWLIST_RDLOCK(&apps);
  86. ret = pbx_findapp_nolock(app);
  87. AST_RWLIST_UNLOCK(&apps);
  88. return ret;
  89. }
  90. /*! \brief Dynamically register a new dial plan application */
  91. int ast_register_application2(const char *app, int (*execute)(struct ast_channel *, const char *), const char *synopsis, const char *description, void *mod)
  92. {
  93. struct ast_app *tmp;
  94. struct ast_app *cur;
  95. int length;
  96. #ifdef AST_XML_DOCS
  97. char *tmpxml;
  98. #endif
  99. AST_RWLIST_WRLOCK(&apps);
  100. cur = pbx_findapp_nolock(app);
  101. if (cur) {
  102. ast_log(LOG_WARNING, "Already have an application '%s'\n", app);
  103. AST_RWLIST_UNLOCK(&apps);
  104. return -1;
  105. }
  106. length = sizeof(*tmp) + strlen(app) + 1;
  107. if (!(tmp = ast_calloc(1, length))) {
  108. AST_RWLIST_UNLOCK(&apps);
  109. return -1;
  110. }
  111. if (ast_string_field_init(tmp, 128)) {
  112. AST_RWLIST_UNLOCK(&apps);
  113. ast_free(tmp);
  114. return -1;
  115. }
  116. strcpy(tmp->name, app);
  117. tmp->execute = execute;
  118. tmp->module = mod;
  119. #ifdef AST_XML_DOCS
  120. /* Try to lookup the docs in our XML documentation database */
  121. if (ast_strlen_zero(synopsis) && ast_strlen_zero(description)) {
  122. /* load synopsis */
  123. tmpxml = ast_xmldoc_build_synopsis("application", app, ast_module_name(tmp->module));
  124. ast_string_field_set(tmp, synopsis, tmpxml);
  125. ast_free(tmpxml);
  126. /* load description */
  127. tmpxml = ast_xmldoc_build_description("application", app, ast_module_name(tmp->module));
  128. ast_string_field_set(tmp, description, tmpxml);
  129. ast_free(tmpxml);
  130. /* load syntax */
  131. tmpxml = ast_xmldoc_build_syntax("application", app, ast_module_name(tmp->module));
  132. ast_string_field_set(tmp, syntax, tmpxml);
  133. ast_free(tmpxml);
  134. /* load arguments */
  135. tmpxml = ast_xmldoc_build_arguments("application", app, ast_module_name(tmp->module));
  136. ast_string_field_set(tmp, arguments, tmpxml);
  137. ast_free(tmpxml);
  138. /* load seealso */
  139. tmpxml = ast_xmldoc_build_seealso("application", app, ast_module_name(tmp->module));
  140. ast_string_field_set(tmp, seealso, tmpxml);
  141. ast_free(tmpxml);
  142. tmp->docsrc = AST_XML_DOC;
  143. } else {
  144. #endif
  145. ast_string_field_set(tmp, synopsis, synopsis);
  146. ast_string_field_set(tmp, description, description);
  147. #ifdef AST_XML_DOCS
  148. tmp->docsrc = AST_STATIC_DOC;
  149. }
  150. #endif
  151. /* Store in alphabetical order */
  152. AST_RWLIST_TRAVERSE_SAFE_BEGIN(&apps, cur, list) {
  153. if (strcasecmp(tmp->name, cur->name) < 0) {
  154. AST_RWLIST_INSERT_BEFORE_CURRENT(tmp, list);
  155. break;
  156. }
  157. }
  158. AST_RWLIST_TRAVERSE_SAFE_END;
  159. if (!cur)
  160. AST_RWLIST_INSERT_TAIL(&apps, tmp, list);
  161. ast_verb(2, "Registered application '" COLORIZE_FMT "'\n", COLORIZE(COLOR_BRCYAN, 0, tmp->name));
  162. AST_RWLIST_UNLOCK(&apps);
  163. return 0;
  164. }
  165. static void print_app_docs(struct ast_app *aa, int fd)
  166. {
  167. #ifdef AST_XML_DOCS
  168. char *synopsis = NULL, *description = NULL, *arguments = NULL, *seealso = NULL;
  169. if (aa->docsrc == AST_XML_DOC) {
  170. synopsis = ast_xmldoc_printable(S_OR(aa->synopsis, "Not available"), 1);
  171. description = ast_xmldoc_printable(S_OR(aa->description, "Not available"), 1);
  172. arguments = ast_xmldoc_printable(S_OR(aa->arguments, "Not available"), 1);
  173. seealso = ast_xmldoc_printable(S_OR(aa->seealso, "Not available"), 1);
  174. if (!synopsis || !description || !arguments || !seealso) {
  175. goto free_docs;
  176. }
  177. ast_cli(fd, "\n"
  178. "%s -= Info about application '%s' =- %s\n\n"
  179. COLORIZE_FMT "\n"
  180. "%s\n\n"
  181. COLORIZE_FMT "\n"
  182. "%s\n\n"
  183. COLORIZE_FMT "\n"
  184. "%s%s%s\n\n"
  185. COLORIZE_FMT "\n"
  186. "%s\n\n"
  187. COLORIZE_FMT "\n"
  188. "%s\n",
  189. ast_term_color(COLOR_MAGENTA, 0), aa->name, ast_term_reset(),
  190. COLORIZE(COLOR_MAGENTA, 0, "[Synopsis]"), synopsis,
  191. COLORIZE(COLOR_MAGENTA, 0, "[Description]"), description,
  192. COLORIZE(COLOR_MAGENTA, 0, "[Syntax]"),
  193. ast_term_color(COLOR_CYAN, 0), S_OR(aa->syntax, "Not available"), ast_term_reset(),
  194. COLORIZE(COLOR_MAGENTA, 0, "[Arguments]"), arguments,
  195. COLORIZE(COLOR_MAGENTA, 0, "[See Also]"), seealso);
  196. free_docs:
  197. ast_free(synopsis);
  198. ast_free(description);
  199. ast_free(arguments);
  200. ast_free(seealso);
  201. } else
  202. #endif
  203. {
  204. ast_cli(fd, "\n"
  205. "%s -= Info about application '%s' =- %s\n\n"
  206. COLORIZE_FMT "\n"
  207. COLORIZE_FMT "\n\n"
  208. COLORIZE_FMT "\n"
  209. COLORIZE_FMT "\n\n"
  210. COLORIZE_FMT "\n"
  211. COLORIZE_FMT "\n\n"
  212. COLORIZE_FMT "\n"
  213. COLORIZE_FMT "\n\n"
  214. COLORIZE_FMT "\n"
  215. COLORIZE_FMT "\n",
  216. ast_term_color(COLOR_MAGENTA, 0), aa->name, ast_term_reset(),
  217. COLORIZE(COLOR_MAGENTA, 0, "[Synopsis]"),
  218. COLORIZE(COLOR_CYAN, 0, S_OR(aa->synopsis, "Not available")),
  219. COLORIZE(COLOR_MAGENTA, 0, "[Description]"),
  220. COLORIZE(COLOR_CYAN, 0, S_OR(aa->description, "Not available")),
  221. COLORIZE(COLOR_MAGENTA, 0, "[Syntax]"),
  222. COLORIZE(COLOR_CYAN, 0, S_OR(aa->syntax, "Not available")),
  223. COLORIZE(COLOR_MAGENTA, 0, "[Arguments]"),
  224. COLORIZE(COLOR_CYAN, 0, S_OR(aa->arguments, "Not available")),
  225. COLORIZE(COLOR_MAGENTA, 0, "[See Also]"),
  226. COLORIZE(COLOR_CYAN, 0, S_OR(aa->seealso, "Not available")));
  227. }
  228. }
  229. /*
  230. * \brief 'show application' CLI command implementation function...
  231. */
  232. static char *handle_show_application(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  233. {
  234. struct ast_app *aa;
  235. int app, no_registered_app = 1;
  236. switch (cmd) {
  237. case CLI_INIT:
  238. e->command = "core show application";
  239. e->usage =
  240. "Usage: core show application <application> [<application> [<application> [...]]]\n"
  241. " Describes a particular application.\n";
  242. return NULL;
  243. case CLI_GENERATE:
  244. /*
  245. * There is a possibility to show informations about more than one
  246. * application at one time. You can type 'show application Dial Echo' and
  247. * you will see informations about these two applications ...
  248. */
  249. return ast_complete_applications(a->line, a->word, -1);
  250. }
  251. if (a->argc < 4) {
  252. return CLI_SHOWUSAGE;
  253. }
  254. AST_RWLIST_RDLOCK(&apps);
  255. AST_RWLIST_TRAVERSE(&apps, aa, list) {
  256. /* Check for each app that was supplied as an argument */
  257. for (app = 3; app < a->argc; app++) {
  258. if (strcasecmp(aa->name, a->argv[app])) {
  259. continue;
  260. }
  261. /* We found it! */
  262. no_registered_app = 0;
  263. print_app_docs(aa, a->fd);
  264. }
  265. }
  266. AST_RWLIST_UNLOCK(&apps);
  267. /* we found at least one app? no? */
  268. if (no_registered_app) {
  269. ast_cli(a->fd, "Your application(s) is (are) not registered\n");
  270. return CLI_FAILURE;
  271. }
  272. return CLI_SUCCESS;
  273. }
  274. static char *handle_show_applications(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  275. {
  276. struct ast_app *aa;
  277. int like = 0, describing = 0;
  278. int total_match = 0; /* Number of matches in like clause */
  279. int total_apps = 0; /* Number of apps registered */
  280. switch (cmd) {
  281. case CLI_INIT:
  282. e->command = "core show applications [like|describing]";
  283. e->usage =
  284. "Usage: core show applications [{like|describing} <text>]\n"
  285. " List applications which are currently available.\n"
  286. " If 'like', <text> will be a substring of the app name\n"
  287. " If 'describing', <text> will be a substring of the description\n";
  288. return NULL;
  289. case CLI_GENERATE:
  290. return NULL;
  291. }
  292. AST_RWLIST_RDLOCK(&apps);
  293. if (AST_RWLIST_EMPTY(&apps)) {
  294. ast_cli(a->fd, "There are no registered applications\n");
  295. AST_RWLIST_UNLOCK(&apps);
  296. return CLI_SUCCESS;
  297. }
  298. /* core list applications like <keyword> */
  299. if ((a->argc == 5) && (!strcmp(a->argv[3], "like"))) {
  300. like = 1;
  301. } else if ((a->argc > 4) && (!strcmp(a->argv[3], "describing"))) {
  302. describing = 1;
  303. }
  304. /* core list applications describing <keyword1> [<keyword2>] [...] */
  305. if ((!like) && (!describing)) {
  306. ast_cli(a->fd, " -= Registered Asterisk Applications =-\n");
  307. } else {
  308. ast_cli(a->fd, " -= Matching Asterisk Applications =-\n");
  309. }
  310. AST_RWLIST_TRAVERSE(&apps, aa, list) {
  311. int printapp = 0;
  312. total_apps++;
  313. if (like) {
  314. if (strcasestr(aa->name, a->argv[4])) {
  315. printapp = 1;
  316. total_match++;
  317. }
  318. } else if (describing) {
  319. if (aa->description) {
  320. /* Match all words on command line */
  321. int i;
  322. printapp = 1;
  323. for (i = 4; i < a->argc; i++) {
  324. if (!strcasestr(aa->description, a->argv[i])) {
  325. printapp = 0;
  326. } else {
  327. total_match++;
  328. }
  329. }
  330. }
  331. } else {
  332. printapp = 1;
  333. }
  334. if (printapp) {
  335. ast_cli(a->fd," %20s: %s\n", aa->name, aa->synopsis ? aa->synopsis : "<Synopsis not available>");
  336. }
  337. }
  338. if ((!like) && (!describing)) {
  339. ast_cli(a->fd, " -= %d Applications Registered =-\n",total_apps);
  340. } else {
  341. ast_cli(a->fd, " -= %d Applications Matching =-\n",total_match);
  342. }
  343. AST_RWLIST_UNLOCK(&apps);
  344. return CLI_SUCCESS;
  345. }
  346. int ast_unregister_application(const char *app)
  347. {
  348. struct ast_app *cur;
  349. int cmp;
  350. /* Anticipate need for conlock in unreference_cached_app(), in order to avoid
  351. * possible deadlock with pbx_extension_helper()/pbx_findapp()
  352. */
  353. ast_rdlock_contexts();
  354. AST_RWLIST_WRLOCK(&apps);
  355. AST_RWLIST_TRAVERSE_SAFE_BEGIN(&apps, cur, list) {
  356. cmp = strcasecmp(app, cur->name);
  357. if (cmp > 0) {
  358. continue;
  359. }
  360. if (!cmp) {
  361. /* Found it. */
  362. unreference_cached_app(cur);
  363. AST_RWLIST_REMOVE_CURRENT(list);
  364. ast_verb(2, "Unregistered application '%s'\n", cur->name);
  365. ast_string_field_free_memory(cur);
  366. ast_free(cur);
  367. break;
  368. }
  369. /* Not in container. */
  370. cur = NULL;
  371. break;
  372. }
  373. AST_RWLIST_TRAVERSE_SAFE_END;
  374. AST_RWLIST_UNLOCK(&apps);
  375. ast_unlock_contexts();
  376. return cur ? 0 : -1;
  377. }
  378. char *ast_complete_applications(const char *line, const char *word, int state)
  379. {
  380. struct ast_app *app;
  381. int which = 0;
  382. int cmp;
  383. char *ret = NULL;
  384. size_t wordlen = strlen(word);
  385. AST_RWLIST_RDLOCK(&apps);
  386. AST_RWLIST_TRAVERSE(&apps, app, list) {
  387. cmp = strncasecmp(word, app->name, wordlen);
  388. if (cmp < 0) {
  389. /* No more matches. */
  390. break;
  391. } else if (!cmp) {
  392. /* Found match. */
  393. if (state != -1) {
  394. if (++which <= state) {
  395. /* Not enough matches. */
  396. continue;
  397. }
  398. ret = ast_strdup(app->name);
  399. break;
  400. }
  401. if (ast_cli_completion_add(ast_strdup(app->name))) {
  402. break;
  403. }
  404. }
  405. }
  406. AST_RWLIST_UNLOCK(&apps);
  407. return ret;
  408. }
  409. const char *app_name(struct ast_app *app)
  410. {
  411. return app->name;
  412. }
  413. /*
  414. \note This function is special. It saves the stack so that no matter
  415. how many times it is called, it returns to the same place */
  416. int pbx_exec(struct ast_channel *c, /*!< Channel */
  417. struct ast_app *app, /*!< Application */
  418. const char *data) /*!< Data for execution */
  419. {
  420. int res;
  421. struct ast_module_user *u = NULL;
  422. const char *saved_c_appl;
  423. const char *saved_c_data;
  424. /* save channel values */
  425. saved_c_appl= ast_channel_appl(c);
  426. saved_c_data= ast_channel_data(c);
  427. ast_channel_lock(c);
  428. ast_channel_appl_set(c, app->name);
  429. ast_channel_data_set(c, data);
  430. ast_channel_publish_snapshot(c);
  431. ast_channel_unlock(c);
  432. if (app->module)
  433. u = __ast_module_user_add(app->module, c);
  434. res = app->execute(c, S_OR(data, ""));
  435. if (app->module && u)
  436. __ast_module_user_remove(app->module, u);
  437. /* restore channel values */
  438. ast_channel_appl_set(c, saved_c_appl);
  439. ast_channel_data_set(c, saved_c_data);
  440. return res;
  441. }
  442. static struct ast_cli_entry app_cli[] = {
  443. AST_CLI_DEFINE(handle_show_applications, "Shows registered dialplan applications"),
  444. AST_CLI_DEFINE(handle_show_application, "Describe a specific dialplan application"),
  445. };
  446. static void unload_pbx_app(void)
  447. {
  448. ast_cli_unregister_multiple(app_cli, ARRAY_LEN(app_cli));
  449. }
  450. int load_pbx_app(void)
  451. {
  452. ast_cli_register_multiple(app_cli, ARRAY_LEN(app_cli));
  453. ast_register_cleanup(unload_pbx_app);
  454. return 0;
  455. }