func_evalexten.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2021, Naveen Albert
  5. *
  6. * See http://www.asterisk.org for more information about
  7. * the Asterisk project. Please do not directly contact
  8. * any of the maintainers of this project for assistance;
  9. * the project provides a web site, mailing lists and IRC
  10. * channels for your use.
  11. *
  12. * This program is free software, distributed under the terms of
  13. * the GNU General Public License Version 2. See the LICENSE file
  14. * at the top of the source tree.
  15. */
  16. /*! \file
  17. *
  18. * \brief Dialplan extension evaluation function
  19. *
  20. * \author Naveen Albert <asterisk@phreaknet.org>
  21. *
  22. * \ingroup functions
  23. */
  24. /*** MODULEINFO
  25. <support_level>extended</support_level>
  26. ***/
  27. #include "asterisk.h"
  28. #include "asterisk/module.h"
  29. #include "asterisk/channel.h"
  30. #include "asterisk/pbx.h"
  31. #include "asterisk/utils.h"
  32. #include "asterisk/app.h"
  33. /*** DOCUMENTATION
  34. <function name="EVAL_EXTEN" language="en_US">
  35. <synopsis>
  36. Evaluates the contents of a dialplan extension and returns it as a string.
  37. </synopsis>
  38. <syntax>
  39. <parameter name="context" />
  40. <parameter name="extensions" />
  41. <parameter name="priority" required="true" />
  42. </syntax>
  43. <description>
  44. <para>The EVAL_EXTEN function looks up a dialplan entry by context,extension,priority,
  45. evaluates the contents of a Return statement to resolve any variable or function
  46. references, and returns the result as a string.</para>
  47. <para>You can use this function to create simple user-defined lookup tables or
  48. user-defined functions.</para>
  49. <example title="Custom dialplan functions">
  50. [call-types]
  51. exten => _1NNN,1,Return(internal)
  52. exten => _NXXNXXXXXX,1,Return(external)
  53. [udf]
  54. exten => calleridlen,1,Return(${LEN(${CALLERID(num)})})
  55. [default]
  56. exten => _X!,1,Verbose(Call type ${EVAL_EXTEN(call-types,${EXTEN},1)} - ${EVAL_EXTEN(udf,calleridlen,1)})
  57. </example>
  58. <para>Any variables in the evaluated data will be resolved in the context of
  59. that extension. For example, <literal>${EXTEN}</literal> would refer to the
  60. EVAL_EXTEN extension, not the extension in the context invoking the function.
  61. This behavior is similar to other applications, e.g. <literal>Gosub</literal>.</para>
  62. <example title="Choosing which prompt to use">
  63. same => n,Read(input,${EVAL_EXTEN(prompts,${CALLERID(num)},1)})
  64. [prompts]
  65. exten => _X!,1,Return(default)
  66. exten => _20X,1,Return(welcome)
  67. exten => _2XX,1,Return(${DB(promptsettings/${EXTEN})})
  68. exten => _3XX,1,Return(${ODBC_MYFUNC(${EXTEN})})
  69. </example>
  70. <para>Extensions on which EVAL_EXTEN is invoked are not different from other
  71. extensions. However, the application at that extension is not executed.
  72. Only the application data is parsed and evaluated.</para>
  73. <para>A limitation of this function is that the application at the specified
  74. extension isn't actually executed, and thus unlike a Gosub, you can't pass
  75. arguments in the EVAL_EXTEN function.</para>
  76. </description>
  77. <see-also>
  78. <ref type="function">EVAL</ref>
  79. </see-also>
  80. </function>
  81. ***/
  82. static int eval_exten_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
  83. {
  84. char *exten, *pri, *context, *parse;
  85. int ipri;
  86. char tmpbuf[len];
  87. if (ast_strlen_zero(data)) {
  88. ast_log(LOG_WARNING, "The EVAL_EXTEN function requires an extension\n");
  89. return -1;
  90. }
  91. parse = ast_strdupa(data);
  92. /* Split context,exten,pri */
  93. context = strsep(&parse, ",");
  94. exten = strsep(&parse, ",");
  95. pri = strsep(&parse, ",");
  96. if (pbx_parse_location(chan, &context, &exten, &pri, &ipri, NULL, NULL)) {
  97. return -1;
  98. }
  99. if (ast_strlen_zero(exten) || ast_strlen_zero(context)) { /* only lock if we really need to */
  100. ast_channel_lock(chan);
  101. if (ast_strlen_zero(exten)) {
  102. exten = ast_strdupa(ast_channel_exten(chan));
  103. }
  104. if (ast_strlen_zero(context)) {
  105. context = ast_strdupa(ast_channel_context(chan));
  106. }
  107. ast_channel_unlock(chan);
  108. }
  109. if (ast_get_extension_data(tmpbuf, len, chan, context, exten, ipri)) {
  110. return -1; /* EVAL_EXTEN failed */
  111. }
  112. pbx_substitute_variables_helper_full_location(chan, (chan) ? ast_channel_varshead(chan) : NULL, tmpbuf, buf, len, NULL, context, exten, ipri);
  113. return 0;
  114. }
  115. static struct ast_custom_function eval_exten_function = {
  116. .name = "EVAL_EXTEN",
  117. .read = eval_exten_read,
  118. };
  119. static int unload_module(void)
  120. {
  121. return ast_custom_function_unregister(&eval_exten_function);
  122. }
  123. static int load_module(void)
  124. {
  125. return ast_custom_function_register(&eval_exten_function);
  126. }
  127. AST_MODULE_INFO_STANDARD_EXTENDED(ASTERISK_GPL_KEY, "Extension evaluation function");