app_privacy.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2005, Digium, Inc.
  5. *
  6. * Mark Spencer <markster@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*! \file
  19. *
  20. * \brief Block all calls without Caller*ID, require phone # to be entered
  21. *
  22. * \author Mark Spencer <markster@digium.com>
  23. *
  24. * \ingroup applications
  25. */
  26. #include "asterisk.h"
  27. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  28. #include "asterisk/lock.h"
  29. #include "asterisk/file.h"
  30. #include "asterisk/utils.h"
  31. #include "asterisk/channel.h"
  32. #include "asterisk/pbx.h"
  33. #include "asterisk/module.h"
  34. #include "asterisk/translate.h"
  35. #include "asterisk/image.h"
  36. #include "asterisk/callerid.h"
  37. #include "asterisk/app.h"
  38. #include "asterisk/config.h"
  39. static char *app = "PrivacyManager";
  40. static char *synopsis = "Require phone number to be entered, if no CallerID sent";
  41. static char *descrip =
  42. " PrivacyManager([maxretries][,minlength]): If no Caller*ID \n"
  43. "is sent, PrivacyManager answers the channel and asks the caller to\n"
  44. "enter their phone number. The caller is given 'maxretries' attempts to do so.\n"
  45. "The application does nothing if Caller*ID was received on the channel.\n"
  46. " maxretries default 3 -maximum number of attempts the caller is allowed \n"
  47. " to input a callerid.\n"
  48. " minlength default 10 -minimum allowable digits in the input callerid number.\n"
  49. "The application sets the following channel variable upon completion: \n"
  50. "PRIVACYMGRSTATUS The status of the privacy manager's attempt to collect \n"
  51. " a phone number from the user. A text string that is either:\n"
  52. " SUCCESS | FAILED \n"
  53. ;
  54. static int privacy_exec (struct ast_channel *chan, void *data)
  55. {
  56. int res=0;
  57. int retries;
  58. int maxretries = 3;
  59. int minlength = 10;
  60. int x = 0;
  61. char phone[30];
  62. char *parse = NULL;
  63. AST_DECLARE_APP_ARGS(args,
  64. AST_APP_ARG(maxretries);
  65. AST_APP_ARG(minlength);
  66. AST_APP_ARG(options);
  67. );
  68. if (!ast_strlen_zero(chan->cid.cid_num)) {
  69. ast_verb(3, "CallerID Present: Skipping\n");
  70. } else {
  71. /*Answer the channel if it is not already*/
  72. if (chan->_state != AST_STATE_UP) {
  73. if ((res = ast_answer(chan)))
  74. return -1;
  75. }
  76. if (!ast_strlen_zero(data)) {
  77. parse = ast_strdupa(data);
  78. AST_STANDARD_APP_ARGS(args, parse);
  79. if (args.maxretries) {
  80. if (sscanf(args.maxretries, "%30d", &x) == 1)
  81. maxretries = x;
  82. else
  83. ast_log(LOG_WARNING, "Invalid max retries argument\n");
  84. }
  85. if (args.minlength) {
  86. if (sscanf(args.minlength, "%30d", &x) == 1)
  87. minlength = x;
  88. else
  89. ast_log(LOG_WARNING, "Invalid min length argument\n");
  90. }
  91. }
  92. /* Play unidentified call */
  93. res = ast_safe_sleep(chan, 1000);
  94. if (!res)
  95. res = ast_streamfile(chan, "privacy-unident", chan->language);
  96. if (!res)
  97. res = ast_waitstream(chan, "");
  98. /* Ask for 10 digit number, give 3 attempts */
  99. for (retries = 0; retries < maxretries; retries++) {
  100. if (!res)
  101. res = ast_streamfile(chan, "privacy-prompt", chan->language);
  102. if (!res)
  103. res = ast_waitstream(chan, "");
  104. if (!res )
  105. res = ast_readstring(chan, phone, sizeof(phone) - 1, /* digit timeout ms */ 3200, /* first digit timeout */ 5000, "#");
  106. if (res < 0)
  107. break;
  108. /* Make sure we get at least digits */
  109. if (strlen(phone) >= minlength )
  110. break;
  111. else {
  112. res = ast_streamfile(chan, "privacy-incorrect", chan->language);
  113. if (!res)
  114. res = ast_waitstream(chan, "");
  115. }
  116. }
  117. /* Got a number, play sounds and send them on their way */
  118. if ((retries < maxretries) && res >= 0 ) {
  119. res = ast_streamfile(chan, "privacy-thankyou", chan->language);
  120. if (!res)
  121. res = ast_waitstream(chan, "");
  122. ast_set_callerid (chan, phone, "Privacy Manager", NULL);
  123. /* Clear the unavailable presence bit so if it came in on PRI
  124. * the caller id will now be passed out to other channels
  125. */
  126. chan->cid.cid_pres &= (AST_PRES_UNAVAILABLE ^ 0xFF);
  127. ast_verb(3, "Changed Caller*ID to %s, callerpres to %d\n",phone,chan->cid.cid_pres);
  128. pbx_builtin_setvar_helper(chan, "PRIVACYMGRSTATUS", "SUCCESS");
  129. } else {
  130. pbx_builtin_setvar_helper(chan, "PRIVACYMGRSTATUS", "FAILED");
  131. }
  132. }
  133. return 0;
  134. }
  135. static int unload_module(void)
  136. {
  137. return ast_unregister_application (app);
  138. }
  139. static int load_module(void)
  140. {
  141. return ast_register_application(app, privacy_exec, synopsis, descrip);
  142. }
  143. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Require phone number to be entered, if no CallerID sent");