func_rand.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2006, Digium, Inc.
  5. * Copyright (C) 2006, Claude Patry
  6. *
  7. * See http://www.asterisk.org for more information about
  8. * the Asterisk project. Please do not directly contact
  9. * any of the maintainers of this project for assistance;
  10. * the project provides a web site, mailing lists and IRC
  11. * channels for your use.
  12. *
  13. * This program is free software, distributed under the terms of
  14. * the GNU General Public License Version 2. See the LICENSE file
  15. * at the top of the source tree.
  16. */
  17. /*! \file
  18. *
  19. * \brief Generate Random Number
  20. *
  21. * \author Claude Patry <cpatry@gmail.com>
  22. * \author Tilghman Lesher ( http://asterisk.drunkcoder.com/ )
  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/utils.h"
  34. #include "asterisk/app.h"
  35. /*** DOCUMENTATION
  36. <function name="RAND" language="en_US">
  37. <synopsis>
  38. Choose a random number in a range.
  39. </synopsis>
  40. <syntax>
  41. <parameter name="min" />
  42. <parameter name="max" />
  43. </syntax>
  44. <description>
  45. <para>Choose a random number between <replaceable>min</replaceable> and <replaceable>max</replaceable>.
  46. <replaceable>min</replaceable> defaults to <literal>0</literal>, if not specified, while <replaceable>max</replaceable> defaults
  47. to <literal>RAND_MAX</literal> (2147483647 on many systems).</para>
  48. <para>Example: Set(junky=${RAND(1,8)});
  49. Sets junky to a random number between 1 and 8, inclusive.</para>
  50. </description>
  51. </function>
  52. ***/
  53. static int acf_rand_exec(struct ast_channel *chan, const char *cmd,
  54. char *parse, char *buffer, size_t buflen)
  55. {
  56. int min_int, response_int, max_int;
  57. AST_DECLARE_APP_ARGS(args,
  58. AST_APP_ARG(min);
  59. AST_APP_ARG(max);
  60. );
  61. AST_STANDARD_APP_ARGS(args, parse);
  62. if (ast_strlen_zero(args.min) || sscanf(args.min, "%30d", &min_int) != 1)
  63. min_int = 0;
  64. if (ast_strlen_zero(args.max) || sscanf(args.max, "%30d", &max_int) != 1)
  65. max_int = RAND_MAX;
  66. if (max_int < min_int) {
  67. int tmp = max_int;
  68. max_int = min_int;
  69. min_int = tmp;
  70. ast_debug(1, "max<min\n");
  71. }
  72. response_int = min_int + (ast_random() % (max_int - min_int + 1));
  73. ast_debug(1, "%d was the lucky number in range [%d,%d]\n", response_int, min_int, max_int);
  74. snprintf(buffer, buflen, "%d", response_int);
  75. return 0;
  76. }
  77. static struct ast_custom_function acf_rand = {
  78. .name = "RAND",
  79. .read = acf_rand_exec,
  80. .read_max = 12,
  81. };
  82. static int unload_module(void)
  83. {
  84. ast_custom_function_unregister(&acf_rand);
  85. return 0;
  86. }
  87. static int load_module(void)
  88. {
  89. return ast_custom_function_register(&acf_rand);
  90. }
  91. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Random number dialplan function");