func_dialplan.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2007, Digium, Inc.
  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 group functions check if a dialplan entry exists
  19. *
  20. * \author Gregory Nietsky AKA irroot <gregory@networksentry.co.za>
  21. * \author Russell Bryant <russell@digium.com>
  22. *
  23. * \ingroup functions
  24. */
  25. /*** MODULEINFO
  26. <support_level>core</support_level>
  27. ***/
  28. #include "asterisk.h"
  29. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  30. #include "asterisk/module.h"
  31. #include "asterisk/channel.h"
  32. #include "asterisk/pbx.h"
  33. #include "asterisk/app.h"
  34. /*** DOCUMENTATION
  35. <function name="DIALPLAN_EXISTS" language="en_US">
  36. <synopsis>
  37. Checks the existence of a dialplan target.
  38. </synopsis>
  39. <syntax>
  40. <parameter name="context" required="true" />
  41. <parameter name="extension" />
  42. <parameter name="priority" />
  43. </syntax>
  44. <description>
  45. <para>This function returns <literal>1</literal> if the target exits. Otherwise, it returns <literal>0</literal>.</para>
  46. </description>
  47. </function>
  48. <function name="VALID_EXTEN" language="en_US">
  49. <synopsis>
  50. Determine whether an extension exists or not.
  51. </synopsis>
  52. <syntax>
  53. <parameter name="context">
  54. <para>Defaults to the current context</para>
  55. </parameter>
  56. <parameter name="extension" required="true" />
  57. <parameter name="priority">
  58. <para>Priority defaults to <literal>1</literal>.</para>
  59. </parameter>
  60. </syntax>
  61. <description>
  62. <para>Returns a true value if the indicated <replaceable>context</replaceable>,
  63. <replaceable>extension</replaceable>, and <replaceable>priority</replaceable> exist.</para>
  64. <warning><para>This function has been deprecated in favor of the <literal>DIALPLAN_EXISTS()</literal> function</para></warning>
  65. </description>
  66. </function>
  67. ***/
  68. static int isexten_function_read(struct ast_channel *chan, const char *cmd, char *data,
  69. char *buf, size_t len)
  70. {
  71. char *parse;
  72. AST_DECLARE_APP_ARGS(args,
  73. AST_APP_ARG(context);
  74. AST_APP_ARG(exten);
  75. AST_APP_ARG(priority);
  76. );
  77. strcpy(buf, "0");
  78. if (ast_strlen_zero(data)) {
  79. ast_log(LOG_ERROR, "DIALPLAN_EXISTS() requires an argument\n");
  80. return -1;
  81. }
  82. parse = ast_strdupa(data);
  83. AST_STANDARD_APP_ARGS(args, parse);
  84. if (!ast_strlen_zero(args.priority)) {
  85. int priority_num;
  86. if (sscanf(args.priority, "%30d", &priority_num) == 1 && priority_num > 0) {
  87. int res;
  88. res = ast_exists_extension(chan, args.context, args.exten, priority_num,
  89. !chan ? NULL :
  90. S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL));
  91. if (res)
  92. strcpy(buf, "1");
  93. } else {
  94. int res;
  95. res = ast_findlabel_extension(chan, args.context, args.exten, args.priority,
  96. !chan ? NULL :
  97. S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL));
  98. if (res > 0)
  99. strcpy(buf, "1");
  100. }
  101. } else if (!ast_strlen_zero(args.exten)) {
  102. int res;
  103. res = ast_exists_extension(chan, args.context, args.exten, 1,
  104. !chan ? NULL :
  105. S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL));
  106. if (res)
  107. strcpy(buf, "1");
  108. } else if (!ast_strlen_zero(args.context)) {
  109. if (ast_context_find(args.context))
  110. strcpy(buf, "1");
  111. } else {
  112. ast_log(LOG_ERROR, "Invalid arguments provided to DIALPLAN_EXISTS\n");
  113. return -1;
  114. }
  115. return 0;
  116. }
  117. static int acf_isexten_exec(struct ast_channel *chan, const char *cmd, char *parse, char *buffer, size_t buflen)
  118. {
  119. int priority_int;
  120. AST_DECLARE_APP_ARGS(args,
  121. AST_APP_ARG(context);
  122. AST_APP_ARG(extension);
  123. AST_APP_ARG(priority);
  124. );
  125. if (!chan) {
  126. ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd);
  127. return -1;
  128. }
  129. AST_STANDARD_APP_ARGS(args, parse);
  130. if (ast_strlen_zero(args.context)) {
  131. args.context = ast_strdupa(ast_channel_context(chan));
  132. }
  133. if (ast_strlen_zero(args.extension)) {
  134. ast_log(LOG_WARNING, "Syntax: VALID_EXTEN([<context>],<extension>[,<priority>]) - missing argument <extension>!\n");
  135. return -1;
  136. }
  137. if (ast_strlen_zero(args.priority)) {
  138. priority_int = 1;
  139. } else {
  140. priority_int = atoi(args.priority);
  141. }
  142. if (ast_exists_extension(chan, args.context, args.extension, priority_int,
  143. S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL))) {
  144. ast_copy_string(buffer, "1", buflen);
  145. } else {
  146. ast_copy_string(buffer, "0", buflen);
  147. }
  148. return 0;
  149. }
  150. static struct ast_custom_function isexten_function = {
  151. .name = "DIALPLAN_EXISTS",
  152. .read = isexten_function_read,
  153. .read_max = 2,
  154. };
  155. static struct ast_custom_function acf_isexten = {
  156. .name = "VALID_EXTEN",
  157. .read = acf_isexten_exec,
  158. };
  159. static int unload_module(void)
  160. {
  161. int res = ast_custom_function_unregister(&isexten_function);
  162. res |= ast_custom_function_unregister(&acf_isexten);
  163. return res;
  164. }
  165. static int load_module(void)
  166. {
  167. int res = ast_custom_function_register(&isexten_function);
  168. res |= ast_custom_function_register(&acf_isexten);
  169. return res;
  170. }
  171. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Dialplan Context/Extension/Priority Checking Functions",
  172. .support_level = AST_MODULE_SUPPORT_CORE,
  173. .load = load_module,
  174. .unload = unload_module,
  175. .load_pri = AST_MODPRI_APP_DEPEND,
  176. );