app_exec.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (c) 2004 - 2005, Tilghman Lesher. All rights reserved.
  5. * Portions copyright (c) 2006, Philipp Dunkel.
  6. *
  7. * Tilghman Lesher <app_exec__v002@the-tilghman.com>
  8. *
  9. * This code is released by the author with no restrictions on usage.
  10. *
  11. * See http://www.asterisk.org for more information about
  12. * the Asterisk project. Please do not directly contact
  13. * any of the maintainers of this project for assistance;
  14. * the project provides a web site, mailing lists and IRC
  15. * channels for your use.
  16. *
  17. */
  18. /*! \file
  19. *
  20. * \brief Exec application
  21. *
  22. * \author Tilghman Lesher <app_exec__v002@the-tilghman.com>
  23. * \author Philipp Dunkel <philipp.dunkel@ebox.at>
  24. *
  25. * \ingroup applications
  26. */
  27. /*** MODULEINFO
  28. <support_level>core</support_level>
  29. ***/
  30. #include "asterisk.h"
  31. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  32. #include "asterisk/file.h"
  33. #include "asterisk/channel.h"
  34. #include "asterisk/pbx.h"
  35. #include "asterisk/module.h"
  36. #include "asterisk/app.h"
  37. /*** DOCUMENTATION
  38. <application name="Exec" language="en_US">
  39. <synopsis>
  40. Executes dialplan application.
  41. </synopsis>
  42. <syntax>
  43. <parameter name="appname" required="true" hasparams="true">
  44. <para>Application name and arguments of the dialplan application to execute.</para>
  45. <argument name="arguments" required="true" />
  46. </parameter>
  47. </syntax>
  48. <description>
  49. <para>Allows an arbitrary application to be invoked even when not
  50. hard coded into the dialplan. If the underlying application
  51. terminates the dialplan, or if the application cannot be found,
  52. Exec will terminate the dialplan.</para>
  53. <para>To invoke external applications, see the application System.
  54. If you would like to catch any error instead, see TryExec.</para>
  55. </description>
  56. </application>
  57. <application name="TryExec" language="en_US">
  58. <synopsis>
  59. Executes dialplan application, always returning.
  60. </synopsis>
  61. <syntax>
  62. <parameter name="appname" required="true" hasparams="true">
  63. <argument name="arguments" required="true" />
  64. </parameter>
  65. </syntax>
  66. <description>
  67. <para>Allows an arbitrary application to be invoked even when not
  68. hard coded into the dialplan. To invoke external applications
  69. see the application System. Always returns to the dialplan.
  70. The channel variable TRYSTATUS will be set to one of:
  71. </para>
  72. <variablelist>
  73. <variable name="TRYSTATUS">
  74. <value name="SUCCESS">
  75. If the application returned zero.
  76. </value>
  77. <value name="FAILED">
  78. If the application returned non-zero.
  79. </value>
  80. <value name="NOAPP">
  81. If the application was not found or was not specified.
  82. </value>
  83. </variable>
  84. </variablelist>
  85. </description>
  86. </application>
  87. <application name="ExecIf" language="en_US">
  88. <synopsis>
  89. Executes dialplan application, conditionally.
  90. </synopsis>
  91. <syntax argsep="?">
  92. <parameter name="expression" required="true" />
  93. <parameter name="execapp" required="true" argsep=":">
  94. <argument name="appiftrue" required="true" hasparams="true">
  95. <argument name="args" required="true" />
  96. </argument>
  97. <argument name="appiffalse" required="false" hasparams="true">
  98. <argument name="args" required="true" />
  99. </argument>
  100. </parameter>
  101. </syntax>
  102. <description>
  103. <para>If <replaceable>expr</replaceable> is true, execute and return the
  104. result of <replaceable>appiftrue(args)</replaceable>.</para>
  105. <para>If <replaceable>expr</replaceable> is true, but <replaceable>appiftrue</replaceable> is not found,
  106. then the application will return a non-zero value.</para>
  107. </description>
  108. </application>
  109. ***/
  110. /* Maximum length of any variable */
  111. #define MAXRESULT 1024
  112. /*! Note
  113. *
  114. * The key difference between these two apps is exit status. In a
  115. * nutshell, Exec tries to be transparent as possible, behaving
  116. * in exactly the same way as if the application it calls was
  117. * directly invoked from the dialplan.
  118. *
  119. * TryExec, on the other hand, provides a way to execute applications
  120. * and catch any possible fatal error without actually fatally
  121. * affecting the dialplan.
  122. */
  123. static const char app_exec[] = "Exec";
  124. static const char app_tryexec[] = "TryExec";
  125. static const char app_execif[] = "ExecIf";
  126. static int exec_exec(struct ast_channel *chan, const char *data)
  127. {
  128. int res = 0;
  129. char *s, *appname, *endargs;
  130. struct ast_app *app;
  131. struct ast_str *args = NULL;
  132. if (ast_strlen_zero(data))
  133. return 0;
  134. s = ast_strdupa(data);
  135. appname = strsep(&s, "(");
  136. if (s) {
  137. endargs = strrchr(s, ')');
  138. if (endargs)
  139. *endargs = '\0';
  140. if ((args = ast_str_create(16))) {
  141. ast_str_substitute_variables(&args, 0, chan, s);
  142. }
  143. }
  144. if (appname) {
  145. app = pbx_findapp(appname);
  146. if (app) {
  147. res = pbx_exec(chan, app, args ? ast_str_buffer(args) : NULL);
  148. } else {
  149. ast_log(LOG_WARNING, "Could not find application (%s)\n", appname);
  150. res = -1;
  151. }
  152. }
  153. ast_free(args);
  154. return res;
  155. }
  156. static int tryexec_exec(struct ast_channel *chan, const char *data)
  157. {
  158. int res = 0;
  159. char *s, *appname, *endargs;
  160. struct ast_app *app;
  161. struct ast_str *args = NULL;
  162. if (ast_strlen_zero(data))
  163. return 0;
  164. s = ast_strdupa(data);
  165. appname = strsep(&s, "(");
  166. if (s) {
  167. endargs = strrchr(s, ')');
  168. if (endargs)
  169. *endargs = '\0';
  170. if ((args = ast_str_create(16))) {
  171. ast_str_substitute_variables(&args, 0, chan, s);
  172. }
  173. }
  174. if (appname) {
  175. app = pbx_findapp(appname);
  176. if (app) {
  177. res = pbx_exec(chan, app, args ? ast_str_buffer(args) : NULL);
  178. pbx_builtin_setvar_helper(chan, "TRYSTATUS", res ? "FAILED" : "SUCCESS");
  179. } else {
  180. ast_log(LOG_WARNING, "Could not find application (%s)\n", appname);
  181. pbx_builtin_setvar_helper(chan, "TRYSTATUS", "NOAPP");
  182. }
  183. }
  184. ast_free(args);
  185. return 0;
  186. }
  187. static int execif_exec(struct ast_channel *chan, const char *data)
  188. {
  189. int res = 0;
  190. char *truedata = NULL, *falsedata = NULL, *end, *firstcomma, *firstquestion;
  191. struct ast_app *app = NULL;
  192. AST_DECLARE_APP_ARGS(expr,
  193. AST_APP_ARG(expr);
  194. AST_APP_ARG(remainder);
  195. );
  196. AST_DECLARE_APP_ARGS(apps,
  197. AST_APP_ARG(t);
  198. AST_APP_ARG(f);
  199. );
  200. char *parse = ast_strdupa(data);
  201. firstcomma = strchr(parse, ',');
  202. firstquestion = strchr(parse, '?');
  203. if ((firstcomma != NULL && firstquestion != NULL && firstcomma < firstquestion) || (firstquestion == NULL)) {
  204. /* Deprecated syntax */
  205. AST_DECLARE_APP_ARGS(depr,
  206. AST_APP_ARG(expr);
  207. AST_APP_ARG(appname);
  208. AST_APP_ARG(appargs);
  209. );
  210. AST_STANDARD_APP_ARGS(depr, parse);
  211. ast_log(LOG_WARNING, "Deprecated syntax found. Please upgrade to using ExecIf(<expr>?%s(%s))\n", depr.appname, depr.appargs);
  212. /* Make the two syntaxes look the same */
  213. expr.expr = depr.expr;
  214. apps.t = depr.appname;
  215. apps.f = NULL;
  216. truedata = depr.appargs;
  217. } else {
  218. /* Preferred syntax */
  219. AST_NONSTANDARD_RAW_ARGS(expr, parse, '?');
  220. if (ast_strlen_zero(expr.remainder)) {
  221. ast_log(LOG_ERROR, "Usage: ExecIf(<expr>?<appiftrue>(<args>)[:<appiffalse>(<args)])\n");
  222. return -1;
  223. }
  224. AST_NONSTANDARD_RAW_ARGS(apps, expr.remainder, ':');
  225. if (apps.t && (truedata = strchr(apps.t, '('))) {
  226. *truedata++ = '\0';
  227. if ((end = strrchr(truedata, ')'))) {
  228. *end = '\0';
  229. }
  230. }
  231. if (apps.f && (falsedata = strchr(apps.f, '('))) {
  232. *falsedata++ = '\0';
  233. if ((end = strrchr(falsedata, ')'))) {
  234. *end = '\0';
  235. }
  236. }
  237. }
  238. if (pbx_checkcondition(expr.expr)) {
  239. if (!ast_strlen_zero(apps.t) && (app = pbx_findapp(apps.t))) {
  240. res = pbx_exec(chan, app, S_OR(truedata, ""));
  241. } else {
  242. ast_log(LOG_WARNING, "Could not find application! (%s)\n", apps.t);
  243. res = -1;
  244. }
  245. } else if (!ast_strlen_zero(apps.f)) {
  246. if ((app = pbx_findapp(apps.f))) {
  247. res = pbx_exec(chan, app, S_OR(falsedata, ""));
  248. } else {
  249. ast_log(LOG_WARNING, "Could not find application! (%s)\n", apps.f);
  250. res = -1;
  251. }
  252. }
  253. return res;
  254. }
  255. static int unload_module(void)
  256. {
  257. int res;
  258. res = ast_unregister_application(app_exec);
  259. res |= ast_unregister_application(app_tryexec);
  260. res |= ast_unregister_application(app_execif);
  261. return res;
  262. }
  263. static int load_module(void)
  264. {
  265. int res = ast_register_application_xml(app_exec, exec_exec);
  266. res |= ast_register_application_xml(app_tryexec, tryexec_exec);
  267. res |= ast_register_application_xml(app_execif, execif_exec);
  268. return res;
  269. }
  270. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Executes dialplan applications");