app_exec.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (c) 2004 - 2005, Tilghman Lesher. All rights reserved.
  5. *
  6. * Tilghman Lesher <app_exec__v001@the-tilghman.com>
  7. *
  8. * This code is released by the author with no restrictions on usage.
  9. *
  10. * See http://www.asterisk.org for more information about
  11. * the Asterisk project. Please do not directly contact
  12. * any of the maintainers of this project for assistance;
  13. * the project provides a web site, mailing lists and IRC
  14. * channels for your use.
  15. *
  16. */
  17. /*! \file
  18. *
  19. * \brief Exec application
  20. *
  21. * \author Tilghman Lesher <app_exec__v001@the-tilghman.com>
  22. *
  23. * \ingroup applications
  24. */
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <unistd.h>
  28. #include <string.h>
  29. #include "asterisk.h"
  30. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  31. #include "asterisk/file.h"
  32. #include "asterisk/logger.h"
  33. #include "asterisk/options.h"
  34. #include "asterisk/channel.h"
  35. #include "asterisk/pbx.h"
  36. #include "asterisk/module.h"
  37. /* Maximum length of any variable */
  38. #define MAXRESULT 1024
  39. static char *tdesc = "Executes applications";
  40. static char *app_exec = "Exec";
  41. static char *exec_synopsis = "Executes internal application";
  42. static char *exec_descrip =
  43. "Usage: Exec(appname(arguments))\n"
  44. " Allows an arbitrary application to be invoked even when not\n"
  45. "hardcoded into the dialplan. To invoke external applications\n"
  46. "see the application System. Returns whatever value the\n"
  47. "app returns or a non-zero value if the app cannot be found.\n";
  48. STANDARD_LOCAL_USER;
  49. LOCAL_USER_DECL;
  50. static int exec_exec(struct ast_channel *chan, void *data)
  51. {
  52. int res=0;
  53. struct localuser *u;
  54. char *s, *appname, *endargs, args[MAXRESULT];
  55. struct ast_app *app;
  56. LOCAL_USER_ADD(u);
  57. memset(args, 0, MAXRESULT);
  58. /* Check and parse arguments */
  59. if (data) {
  60. s = ast_strdupa((char *)data);
  61. if (s) {
  62. appname = strsep(&s, "(");
  63. if (s) {
  64. endargs = strrchr(s, ')');
  65. if (endargs)
  66. *endargs = '\0';
  67. pbx_substitute_variables_helper(chan, s, args, MAXRESULT - 1);
  68. }
  69. if (appname) {
  70. app = pbx_findapp(appname);
  71. if (app) {
  72. res = pbx_exec(chan, app, args, 1);
  73. } else {
  74. ast_log(LOG_WARNING, "Could not find application (%s)\n", appname);
  75. res = -1;
  76. }
  77. }
  78. } else {
  79. ast_log(LOG_ERROR, "Out of memory\n");
  80. res = -1;
  81. }
  82. }
  83. LOCAL_USER_REMOVE(u);
  84. return res;
  85. }
  86. int unload_module(void)
  87. {
  88. int res;
  89. res = ast_unregister_application(app_exec);
  90. STANDARD_HANGUP_LOCALUSERS;
  91. return res;
  92. }
  93. int load_module(void)
  94. {
  95. return ast_register_application(app_exec, exec_exec, exec_synopsis, exec_descrip);
  96. }
  97. char *description(void)
  98. {
  99. return tdesc;
  100. }
  101. int usecount(void)
  102. {
  103. int res;
  104. STANDARD_USECOUNT(res);
  105. return res;
  106. }
  107. char *key()
  108. {
  109. return ASTERISK_GPL_KEY;
  110. }