func_dialplan.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. #include "asterisk.h"
  26. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  27. #include "asterisk/module.h"
  28. #include "asterisk/channel.h"
  29. #include "asterisk/pbx.h"
  30. #include "asterisk/app.h"
  31. static int isexten_function_read(struct ast_channel *chan, const char *cmd, char *data,
  32. char *buf, size_t len)
  33. {
  34. char *parse;
  35. AST_DECLARE_APP_ARGS(args,
  36. AST_APP_ARG(context);
  37. AST_APP_ARG(exten);
  38. AST_APP_ARG(priority);
  39. );
  40. strcpy(buf, "0");
  41. if (ast_strlen_zero(data)) {
  42. ast_log(LOG_ERROR, "DIALPLAN_EXISTS() requires an argument\n");
  43. return -1;
  44. }
  45. parse = ast_strdupa(data);
  46. AST_STANDARD_APP_ARGS(args, parse);
  47. if (!ast_strlen_zero(args.priority)) {
  48. int priority_num;
  49. if (sscanf(args.priority, "%30d", &priority_num) == 1 && priority_num > 0) {
  50. int res;
  51. res = ast_exists_extension(chan, args.context, args.exten, priority_num,
  52. chan->cid.cid_num);
  53. if (res)
  54. strcpy(buf, "1");
  55. } else {
  56. int res;
  57. res = ast_findlabel_extension(chan, args.context, args.exten,
  58. args.priority, chan->cid.cid_num);
  59. if (res > 0)
  60. strcpy(buf, "1");
  61. }
  62. } else if (!ast_strlen_zero(args.exten)) {
  63. int res;
  64. res = ast_exists_extension(chan, args.context, args.exten, 1,
  65. chan->cid.cid_num);
  66. if (res)
  67. strcpy(buf, "1");
  68. } else if (!ast_strlen_zero(args.context)) {
  69. if (ast_context_find(args.context))
  70. strcpy(buf, "1");
  71. } else {
  72. ast_log(LOG_ERROR, "Invalid arguments provided to DIALPLAN_EXISTS\n");
  73. return -1;
  74. }
  75. return 0;
  76. }
  77. static struct ast_custom_function isexten_function = {
  78. .name = "DIALPLAN_EXISTS",
  79. .syntax = "DIALPLAN_EXISTS(context[,extension[,priority]])",
  80. .synopsis = "Checks the existence of a dialplan target.",
  81. .desc = "This function returns 1 if the target exits. Otherwise, it returns 0.\n",
  82. .read = isexten_function_read,
  83. };
  84. static int unload_module(void)
  85. {
  86. return ast_custom_function_unregister(&isexten_function);
  87. }
  88. static int load_module(void)
  89. {
  90. return ast_custom_function_register(&isexten_function);
  91. }
  92. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Dialplan Context/Extension/Priority Checking Functions");