func_blacklist.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 Function to lookup the callerid number, and see if it is blacklisted
  21. *
  22. * \author Mark Spencer <markster@digium.com>
  23. *
  24. * \ingroup functions
  25. *
  26. */
  27. /*** MODULEINFO
  28. <support_level>core</support_level>
  29. ***/
  30. #include "asterisk.h"
  31. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  32. #include "asterisk/pbx.h"
  33. #include "asterisk/module.h"
  34. #include "asterisk/channel.h"
  35. #include "asterisk/astdb.h"
  36. /*** DOCUMENTATION
  37. <function name="BLACKLIST" language="en_US">
  38. <synopsis>
  39. Check if the callerid is on the blacklist.
  40. </synopsis>
  41. <syntax />
  42. <description>
  43. <para>Uses astdb to check if the Caller*ID is in family <literal>blacklist</literal>.
  44. Returns <literal>1</literal> or <literal>0</literal>.</para>
  45. </description>
  46. <see-also>
  47. <ref type="function">DB</ref>
  48. </see-also>
  49. </function>
  50. ***/
  51. static int blacklist_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
  52. {
  53. char blacklist[1];
  54. int bl = 0;
  55. if (!chan) {
  56. ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd);
  57. return -1;
  58. }
  59. if (ast_channel_caller(chan)->id.number.valid && ast_channel_caller(chan)->id.number.str) {
  60. if (!ast_db_get("blacklist", ast_channel_caller(chan)->id.number.str, blacklist, sizeof (blacklist)))
  61. bl = 1;
  62. }
  63. if (ast_channel_caller(chan)->id.name.valid && ast_channel_caller(chan)->id.name.str) {
  64. if (!ast_db_get("blacklist", ast_channel_caller(chan)->id.name.str, blacklist, sizeof (blacklist)))
  65. bl = 1;
  66. }
  67. snprintf(buf, len, "%d", bl);
  68. return 0;
  69. }
  70. static int blacklist_read2(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **str, ssize_t len)
  71. {
  72. /* 2 bytes is a single integer, plus terminating null */
  73. if (ast_str_size(*str) - ast_str_strlen(*str) < 2) {
  74. if (len > ast_str_size(*str) || len == 0) {
  75. ast_str_make_space(str, len ? len : ast_str_strlen(*str) + 2);
  76. }
  77. }
  78. if (ast_str_size(*str) - ast_str_strlen(*str) >= 2) {
  79. int res = blacklist_read(chan, cmd, data, ast_str_buffer(*str) + ast_str_strlen(*str), 2);
  80. ast_str_update(*str);
  81. return res;
  82. }
  83. return -1;
  84. }
  85. static struct ast_custom_function blacklist_function = {
  86. .name = "BLACKLIST",
  87. .read = blacklist_read,
  88. .read2 = blacklist_read2,
  89. };
  90. static int unload_module(void)
  91. {
  92. return ast_custom_function_unregister(&blacklist_function);
  93. }
  94. static int load_module(void)
  95. {
  96. return ast_custom_function_register(&blacklist_function);
  97. }
  98. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Look up Caller*ID name/number from blacklist database");