app_privacy.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. * \ingroup applications
  23. */
  24. #include <stdlib.h>
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include "asterisk.h"
  28. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  29. #include "asterisk/lock.h"
  30. #include "asterisk/file.h"
  31. #include "asterisk/utils.h"
  32. #include "asterisk/logger.h"
  33. #include "asterisk/options.h"
  34. #include "asterisk/channel.h"
  35. #include "asterisk/pbx.h"
  36. #include "asterisk/module.h"
  37. #include "asterisk/translate.h"
  38. #include "asterisk/image.h"
  39. #include "asterisk/callerid.h"
  40. #include "asterisk/app.h"
  41. #include "asterisk/config.h"
  42. #define PRIV_CONFIG "privacy.conf"
  43. static char *tdesc = "Require phone number to be entered, if no CallerID sent";
  44. static char *app = "PrivacyManager";
  45. static char *synopsis = "Require phone number to be entered, if no CallerID sent";
  46. static char *descrip =
  47. " PrivacyManager([maxretries[|minlength[|options]]]): If no Caller*ID \n"
  48. "is sent, PrivacyManager answers the channel and asks the caller to\n"
  49. "enter their phone number. The caller is given 3 attempts to do so.\n"
  50. "The application does nothing if Caller*ID was received on the channel.\n"
  51. " Configuration file privacy.conf contains two variables:\n"
  52. " maxretries default 3 -maximum number of attempts the caller is allowed \n"
  53. " to input a callerid.\n"
  54. " minlength default 10 -minimum allowable digits in the input callerid number.\n"
  55. "If you don't want to use the config file and have an i/o operation with\n"
  56. "every call, you can also specify maxretries and minlength as application\n"
  57. "parameters. Doing so supercedes any values set in privacy.conf.\n"
  58. "The option string may contain the following character: \n"
  59. " 'j' -- jump to n+101 priority after <maxretries> failed attempts to collect\n"
  60. " the minlength number of digits.\n"
  61. "The application sets the following channel variable upon completion: \n"
  62. "PRIVACYMGRSTATUS The status of the privacy manager's attempt to collect \n"
  63. " a phone number from the user. A text string that is either:\n"
  64. " SUCCESS | FAILED \n"
  65. ;
  66. STANDARD_LOCAL_USER;
  67. LOCAL_USER_DECL;
  68. static int privacy_exec (struct ast_channel *chan, void *data)
  69. {
  70. int res=0;
  71. int retries;
  72. int maxretries = 3;
  73. int minlength = 10;
  74. int x = 0;
  75. char *s;
  76. char phone[30];
  77. struct localuser *u;
  78. struct ast_config *cfg = NULL;
  79. char *parse = NULL;
  80. int priority_jump = 0;
  81. AST_DECLARE_APP_ARGS(args,
  82. AST_APP_ARG(maxretries);
  83. AST_APP_ARG(minlength);
  84. AST_APP_ARG(options);
  85. );
  86. LOCAL_USER_ADD (u);
  87. if (!ast_strlen_zero(chan->cid.cid_num)) {
  88. if (option_verbose > 2)
  89. ast_verbose (VERBOSE_PREFIX_3 "CallerID Present: Skipping\n");
  90. } else {
  91. /*Answer the channel if it is not already*/
  92. if (chan->_state != AST_STATE_UP) {
  93. res = ast_answer(chan);
  94. if (res) {
  95. LOCAL_USER_REMOVE(u);
  96. return -1;
  97. }
  98. }
  99. if (!ast_strlen_zero((char *)data))
  100. {
  101. parse = ast_strdupa(data);
  102. if (!parse) {
  103. ast_log(LOG_ERROR, "Out of memory!\n");
  104. LOCAL_USER_REMOVE(u);
  105. return -1;
  106. }
  107. AST_STANDARD_APP_ARGS(args, parse);
  108. if (args.maxretries) {
  109. if (sscanf(args.maxretries, "%d", &x) == 1)
  110. maxretries = x;
  111. else
  112. ast_log(LOG_WARNING, "Invalid max retries argument\n");
  113. }
  114. if (args.minlength) {
  115. if (sscanf(args.minlength, "%d", &x) == 1)
  116. minlength = x;
  117. else
  118. ast_log(LOG_WARNING, "Invalid min length argument\n");
  119. }
  120. if (args.options)
  121. if (strchr(args.options, 'j'))
  122. priority_jump = 1;
  123. }
  124. if (!x)
  125. {
  126. /*Read in the config file*/
  127. cfg = ast_config_load(PRIV_CONFIG);
  128. if (cfg && (s = ast_variable_retrieve(cfg, "general", "maxretries"))) {
  129. if (sscanf(s, "%d", &x) == 1)
  130. maxretries = x;
  131. else
  132. ast_log(LOG_WARNING, "Invalid max retries argument\n");
  133. }
  134. if (cfg && (s = ast_variable_retrieve(cfg, "general", "minlength"))) {
  135. if (sscanf(s, "%d", &x) == 1)
  136. minlength = x;
  137. else
  138. ast_log(LOG_WARNING, "Invalid min length argument\n");
  139. }
  140. }
  141. /*Play unidentified call*/
  142. res = ast_safe_sleep(chan, 1000);
  143. if (!res)
  144. res = ast_streamfile(chan, "privacy-unident", chan->language);
  145. if (!res)
  146. res = ast_waitstream(chan, "");
  147. /*Ask for 10 digit number, give 3 attempts*/
  148. for (retries = 0; retries < maxretries; retries++) {
  149. if (!res)
  150. res = ast_streamfile(chan, "privacy-prompt", chan->language);
  151. if (!res)
  152. res = ast_waitstream(chan, "");
  153. if (!res )
  154. res = ast_readstring(chan, phone, sizeof(phone) - 1, /* digit timeout ms */ 3200, /* first digit timeout */ 5000, "#");
  155. if (res < 0)
  156. break;
  157. /*Make sure we get at least digits*/
  158. if (strlen(phone) >= minlength )
  159. break;
  160. else {
  161. res = ast_streamfile(chan, "privacy-incorrect", chan->language);
  162. if (!res)
  163. res = ast_waitstream(chan, "");
  164. }
  165. }
  166. /*Got a number, play sounds and send them on their way*/
  167. if ((retries < maxretries) && res == 1 ) {
  168. res = ast_streamfile(chan, "privacy-thankyou", chan->language);
  169. if (!res)
  170. res = ast_waitstream(chan, "");
  171. ast_set_callerid (chan, phone, "Privacy Manager", NULL);
  172. if (option_verbose > 2)
  173. ast_verbose (VERBOSE_PREFIX_3 "Changed Caller*ID to %s\n",phone);
  174. pbx_builtin_setvar_helper(chan, "PRIVACYMGRSTATUS", "SUCCESS");
  175. } else {
  176. if (priority_jump || option_priority_jumping)
  177. ast_goto_if_exists(chan, chan->context, chan->exten, chan->priority + 101);
  178. pbx_builtin_setvar_helper(chan, "PRIVACYMGRSTATUS", "FAILED");
  179. }
  180. if (cfg)
  181. ast_config_destroy(cfg);
  182. }
  183. LOCAL_USER_REMOVE (u);
  184. return 0;
  185. }
  186. int
  187. unload_module (void)
  188. {
  189. int res;
  190. res = ast_unregister_application (app);
  191. STANDARD_HANGUP_LOCALUSERS;
  192. return res;
  193. }
  194. int
  195. load_module (void)
  196. {
  197. return ast_register_application (app, privacy_exec, synopsis,
  198. descrip);
  199. }
  200. char *
  201. description (void)
  202. {
  203. return tdesc;
  204. }
  205. int
  206. usecount (void)
  207. {
  208. int res;
  209. STANDARD_USECOUNT (res);
  210. return res;
  211. }
  212. char *
  213. key ()
  214. {
  215. return ASTERISK_GPL_KEY;
  216. }