app_random.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Asterisk -- A telephony toolkit for Linux.
  3. *
  4. * Random application
  5. *
  6. * Copyright (c) 2003-2004 Tilghman Lesher. All rights reserved.
  7. *
  8. * Tilghman Lesher <asterisk__app_random__20040111@the-tilghman.com>
  9. *
  10. * This code is released by the author with no restrictions on usage or distribution.
  11. *
  12. */
  13. #include <asterisk/file.h>
  14. #include <asterisk/logger.h>
  15. #include <asterisk/options.h>
  16. #include <asterisk/channel.h>
  17. #include <asterisk/pbx.h>
  18. #include <asterisk/module.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <unistd.h>
  22. #include <string.h>
  23. static char *tdesc = "Random goto";
  24. static char *app_random = "Random";
  25. static char *random_synopsis = "Conditionally branches, based upon a probability";
  26. static char *random_descrip =
  27. "Random([probability]:[[context|]extension|]priority)\n"
  28. " probability := INTEGER in the range 1 to 100\n";
  29. STANDARD_LOCAL_USER;
  30. LOCAL_USER_DECL;
  31. static int random_exec(struct ast_channel *chan, void *data)
  32. {
  33. int res=0;
  34. struct localuser *u;
  35. char *s;
  36. char *exten, *pri, *context;
  37. char *prob;
  38. int probint, priorityint;
  39. if (!data) {
  40. ast_log(LOG_WARNING, "Random requires an argument ([probability]:[[context|]extension|]priority)\n");
  41. return -1;
  42. }
  43. LOCAL_USER_ADD(u);
  44. s = ast_strdupa((void *) data);
  45. prob = strsep(&s,":");
  46. if ((!prob) || (sscanf(prob, "%d", &probint) != 1))
  47. probint = 0;
  48. if ((random() % 100) + probint > 100) {
  49. context = strsep(&s, "|");
  50. exten = strsep(&s, "|");
  51. if (!exten) {
  52. /* Only a priority */
  53. pri = context;
  54. exten = NULL;
  55. context = NULL;
  56. } else {
  57. pri = strsep(&s, "|");
  58. if (!pri) {
  59. pri = exten;
  60. exten = context;
  61. context = NULL;
  62. }
  63. }
  64. if (!pri) {
  65. ast_log(LOG_WARNING, "No label specified\n");
  66. LOCAL_USER_REMOVE(u);
  67. return -1;
  68. } else if (sscanf(pri, "%d", &priorityint) != 1) {
  69. ast_log(LOG_WARNING, "Priority '%s' must be a number > 0\n", pri);
  70. LOCAL_USER_REMOVE(u);
  71. return -1;
  72. }
  73. /* At this point we have a priority and */
  74. /* maybe an extension and a context */
  75. chan->priority = priorityint - 1;
  76. if (exten && strcasecmp(exten, "BYEXTENSION"))
  77. strncpy(chan->exten, exten, sizeof(chan->exten)-1);
  78. if (context)
  79. strncpy(chan->context, context, sizeof(chan->context)-1);
  80. if (option_verbose > 2)
  81. ast_verbose( VERBOSE_PREFIX_3 "Random branches to (%s,%s,%d)\n",
  82. chan->context,chan->exten, chan->priority+1);
  83. LOCAL_USER_REMOVE(u);
  84. }
  85. return res;
  86. }
  87. int unload_module(void)
  88. {
  89. STANDARD_HANGUP_LOCALUSERS;
  90. return ast_unregister_application(app_random);
  91. }
  92. int load_module(void)
  93. {
  94. return ast_register_application(app_random, random_exec, random_synopsis, random_descrip);
  95. }
  96. char *description(void)
  97. {
  98. return tdesc;
  99. }
  100. int usecount(void)
  101. {
  102. int res;
  103. STANDARD_USECOUNT(res);
  104. return res;
  105. }
  106. char *key()
  107. {
  108. return ASTERISK_GPL_KEY;
  109. }