app_privacy.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * Asterisk -- A telephony toolkit for Linux.
  3. *
  4. * Block all calls without Caller*ID, require phone # to be entered
  5. *
  6. * Copyright (C) 1999, Mark Spencer
  7. *
  8. * Mark Spencer <markster@linux-support.net>
  9. *
  10. * This program is free software, distributed under the terms of
  11. * the GNU General Public License
  12. */
  13. #include <asterisk/lock.h>
  14. #include <asterisk/file.h>
  15. #include <asterisk/logger.h>
  16. #include <asterisk/options.h>
  17. #include <asterisk/channel.h>
  18. #include <asterisk/pbx.h>
  19. #include <asterisk/module.h>
  20. #include <asterisk/translate.h>
  21. #include <asterisk/image.h>
  22. #include <asterisk/callerid.h>
  23. #include <asterisk/app.h>
  24. #include <asterisk/config.h>
  25. #include <string.h>
  26. #include <stdlib.h>
  27. #define PRIV_CONFIG "privacy.conf"
  28. static char *tdesc = "Require phone number to be entered, if no CallerID sent";
  29. static char *app = "PrivacyManager";
  30. static char *synopsis = "Require phone number to be entered, if no CallerID sent";
  31. static char *descrip =
  32. " PrivacyManager: If no Caller*ID is sent, PrivacyManager answers the\n"
  33. "channel and asks the caller to enter their 10 digit phone number.\n"
  34. "The caller is given 3 attempts. If after 3 attempts, they do not enter\n"
  35. "their 10 digit phone number, and if there exists a priority n + 101,\n"
  36. "where 'n' is the priority of the current instance, then the\n"
  37. "channel will be setup to continue at that priority level.\n"
  38. "Otherwise, it returns 0. Does nothing if Caller*ID was received on the\n"
  39. "channel.\n";
  40. STANDARD_LOCAL_USER;
  41. LOCAL_USER_DECL;
  42. static int
  43. privacy_exec (struct ast_channel *chan, void *data)
  44. {
  45. int res=0;
  46. int retries;
  47. int maxretries = 3;
  48. int x;
  49. char *s;
  50. char phone[10];
  51. char new_cid[144];
  52. struct localuser *u;
  53. struct ast_config *cfg;
  54. LOCAL_USER_ADD (u);
  55. if (chan->callerid)
  56. {
  57. if (option_verbose > 2)
  58. ast_verbose (VERBOSE_PREFIX_3 "CallerID Present: Skipping\n");
  59. }
  60. else
  61. {
  62. /*Answer the channel if it is not already*/
  63. if (chan->_state != AST_STATE_UP) {
  64. res = ast_answer(chan);
  65. if (res) {
  66. LOCAL_USER_REMOVE(u);
  67. return -1;
  68. }
  69. }
  70. /*Read in the config file*/
  71. cfg = ast_load(PRIV_CONFIG);
  72. /*Play unidentified call*/
  73. res = ast_safe_sleep(chan, 1000);
  74. if (!res)
  75. res = ast_streamfile(chan, "privacy-unident", chan->language);
  76. if (!res)
  77. res = ast_waitstream(chan, "");
  78. if (cfg && (s = ast_variable_retrieve(cfg, "general", "maxretries"))) {
  79. if (sscanf(s, "%d", &x) == 1) {
  80. maxretries = x;
  81. } else {
  82. ast_log(LOG_WARNING, "Invalid max retries argument\n");
  83. }
  84. }
  85. /*Ask for 10 digit number, give 3 attempts*/
  86. for (retries = 0; retries < maxretries; retries++) {
  87. if (!res)
  88. res = ast_app_getdata(chan, "privacy-prompt", phone, sizeof(phone), 0);
  89. if (res < 0)
  90. break;
  91. /*Make sure we get 10 digits*/
  92. if (strlen(phone) == 10)
  93. break;
  94. else {
  95. res = ast_streamfile(chan, "privacy-incorrect", chan->language);
  96. if (!res)
  97. res = ast_waitstream(chan, "");
  98. }
  99. }
  100. /*Got a number, play sounds and send them on their way*/
  101. if ((retries < maxretries) && !res) {
  102. res = ast_streamfile(chan, "privacy-thankyou", chan->language);
  103. if (!res)
  104. res = ast_waitstream(chan, "");
  105. snprintf (new_cid, sizeof (new_cid), "\"%s\" <%s>", "Privacy Manager", phone);
  106. ast_set_callerid (chan, new_cid, 0);
  107. if (option_verbose > 2)
  108. ast_verbose (VERBOSE_PREFIX_3 "Changed Caller*ID to %s\n",new_cid);
  109. } else {
  110. /*Send the call to n+101 priority, where n is the current priority*/
  111. if (ast_exists_extension(chan, chan->context, chan->exten, chan->priority + 101, chan->callerid))
  112. chan->priority+=100;
  113. }
  114. if (cfg)
  115. ast_destroy(cfg);
  116. }
  117. LOCAL_USER_REMOVE (u);
  118. return 0;
  119. }
  120. int
  121. unload_module (void)
  122. {
  123. STANDARD_HANGUP_LOCALUSERS;
  124. return ast_unregister_application (app);
  125. }
  126. int
  127. load_module (void)
  128. {
  129. return ast_register_application (app, privacy_exec, synopsis,
  130. descrip);
  131. }
  132. char *
  133. description (void)
  134. {
  135. return tdesc;
  136. }
  137. int
  138. usecount (void)
  139. {
  140. int res;
  141. STANDARD_USECOUNT (res);
  142. return res;
  143. }
  144. char *
  145. key ()
  146. {
  147. return ASTERISK_GPL_KEY;
  148. }