func_dialplan.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. ***/
  49. static int isexten_function_read(struct ast_channel *chan, const char *cmd, char *data,
  50. char *buf, size_t len)
  51. {
  52. char *parse;
  53. AST_DECLARE_APP_ARGS(args,
  54. AST_APP_ARG(context);
  55. AST_APP_ARG(exten);
  56. AST_APP_ARG(priority);
  57. );
  58. strcpy(buf, "0");
  59. if (ast_strlen_zero(data)) {
  60. ast_log(LOG_ERROR, "DIALPLAN_EXISTS() requires an argument\n");
  61. return -1;
  62. }
  63. parse = ast_strdupa(data);
  64. AST_STANDARD_APP_ARGS(args, parse);
  65. if (!ast_strlen_zero(args.priority)) {
  66. int priority_num;
  67. if (sscanf(args.priority, "%30d", &priority_num) == 1 && priority_num > 0) {
  68. int res;
  69. res = ast_exists_extension(chan, args.context, args.exten, priority_num,
  70. S_COR(chan->caller.id.number.valid, chan->caller.id.number.str, NULL));
  71. if (res)
  72. strcpy(buf, "1");
  73. } else {
  74. int res;
  75. res = ast_findlabel_extension(chan, args.context, args.exten, args.priority,
  76. S_COR(chan->caller.id.number.valid, chan->caller.id.number.str, NULL));
  77. if (res > 0)
  78. strcpy(buf, "1");
  79. }
  80. } else if (!ast_strlen_zero(args.exten)) {
  81. int res;
  82. res = ast_exists_extension(chan, args.context, args.exten, 1,
  83. S_COR(chan->caller.id.number.valid, chan->caller.id.number.str, NULL));
  84. if (res)
  85. strcpy(buf, "1");
  86. } else if (!ast_strlen_zero(args.context)) {
  87. if (ast_context_find(args.context))
  88. strcpy(buf, "1");
  89. } else {
  90. ast_log(LOG_ERROR, "Invalid arguments provided to DIALPLAN_EXISTS\n");
  91. return -1;
  92. }
  93. return 0;
  94. }
  95. static struct ast_custom_function isexten_function = {
  96. .name = "DIALPLAN_EXISTS",
  97. .read = isexten_function_read,
  98. .read_max = 2,
  99. };
  100. static int unload_module(void)
  101. {
  102. return ast_custom_function_unregister(&isexten_function);
  103. }
  104. static int load_module(void)
  105. {
  106. return ast_custom_function_register(&isexten_function);
  107. }
  108. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Dialplan Context/Extension/Priority Checking Functions");