app_exec.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Asterisk -- A telephony toolkit for Linux.
  3. *
  4. * Exec application
  5. *
  6. * Copyright (c) 2004 Tilghman Lesher. All rights reserved.
  7. *
  8. * Tilghman Lesher <app_exec__v001@the-tilghman.com>
  9. *
  10. * $Id$
  11. *
  12. * This code is released by the author with no restrictions on usage.
  13. *
  14. */
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <unistd.h>
  18. #include <string.h>
  19. #include <asterisk/file.h>
  20. #include <asterisk/logger.h>
  21. #include <asterisk/options.h>
  22. #include <asterisk/channel.h>
  23. #include <asterisk/pbx.h>
  24. #include <asterisk/module.h>
  25. /* Maximum length of any variable */
  26. #define MAXRESULT 1024
  27. static char *tdesc = "Executes applications";
  28. static char *app_exec = "Exec";
  29. static char *exec_synopsis = "Exec(Appname(arguments))";
  30. static char *exec_descrip =
  31. "Exec(appname(arguments))\n"
  32. " Allows an arbitrary application to be invoked even when not\n"
  33. "hardcoded into the dialplan. Returns whatever value the\n"
  34. "app returns or -2 when the app cannot be found.\n";
  35. STANDARD_LOCAL_USER;
  36. LOCAL_USER_DECL;
  37. static int exec_exec(struct ast_channel *chan, void *data)
  38. {
  39. int res=0;
  40. struct localuser *u;
  41. char *s, *appname, *endargs, args[MAXRESULT];
  42. struct ast_app *app;
  43. LOCAL_USER_ADD(u);
  44. memset(args, 0, MAXRESULT);
  45. /* Check and parse arguments */
  46. if (data) {
  47. s = ast_strdupa((char *)data);
  48. if (s) {
  49. appname = strsep(&s, "(");
  50. if (s) {
  51. endargs = strrchr(s, ')');
  52. if (endargs)
  53. *endargs = '\0';
  54. pbx_substitute_variables_helper(chan, s, args, MAXRESULT - 1);
  55. }
  56. if (appname) {
  57. app = pbx_findapp(appname);
  58. if (app) {
  59. res = pbx_exec(chan, app, args, 1);
  60. } else {
  61. ast_log(LOG_WARNING, "Could not find application (%s)\n", appname);
  62. res = -2;
  63. }
  64. }
  65. } else {
  66. ast_log(LOG_ERROR, "Out of memory\n");
  67. res = -1;
  68. }
  69. }
  70. LOCAL_USER_REMOVE(u);
  71. return res;
  72. }
  73. int unload_module(void)
  74. {
  75. STANDARD_HANGUP_LOCALUSERS;
  76. return ast_unregister_application(app_exec);
  77. }
  78. int load_module(void)
  79. {
  80. return ast_register_application(app_exec, exec_exec, exec_synopsis, exec_descrip);
  81. }
  82. char *description(void)
  83. {
  84. return tdesc;
  85. }
  86. int usecount(void)
  87. {
  88. int res;
  89. STANDARD_USECOUNT(res);
  90. return res;
  91. }
  92. char *key()
  93. {
  94. return ASTERISK_GPL_KEY;
  95. }