func_blacklist.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 1999 - 2005, Digium, Inc.
00005  *
00006  * Mark Spencer <markster@digium.com>
00007  *
00008  * See http://www.asterisk.org for more information about
00009  * the Asterisk project. Please do not directly contact
00010  * any of the maintainers of this project for assistance;
00011  * the project provides a web site, mailing lists and IRC
00012  * channels for your use.
00013  *
00014  * This program is free software, distributed under the terms of
00015  * the GNU General Public License Version 2. See the LICENSE file
00016  * at the top of the source tree.
00017  */
00018 
00019 /*! \file
00020  *
00021  * \brief Function to lookup the callerid number, and see if it is blacklisted
00022  *
00023  * \author Mark Spencer <markster@digium.com>
00024  *
00025  * \ingroup functions
00026  * 
00027  */
00028 
00029 /*** MODULEINFO
00030    <support_level>core</support_level>
00031  ***/
00032 
00033 #include "asterisk.h"
00034 
00035 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 411328 $")
00036 
00037 #include "asterisk/pbx.h"
00038 #include "asterisk/module.h"
00039 #include "asterisk/channel.h"
00040 #include "asterisk/astdb.h"
00041 
00042 /*** DOCUMENTATION
00043    <function name="BLACKLIST" language="en_US">
00044       <synopsis>
00045          Check if the callerid is on the blacklist.
00046       </synopsis>
00047       <syntax />
00048       <description>
00049          <para>Uses astdb to check if the Caller*ID is in family <literal>blacklist</literal>.
00050          Returns <literal>1</literal> or <literal>0</literal>.</para>
00051       </description>
00052       <see-also>
00053          <ref type="function">DB</ref>
00054       </see-also>
00055    </function>
00056 
00057 ***/
00058 
00059 static int blacklist_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
00060 {
00061    char blacklist[1];
00062    int bl = 0;
00063 
00064    if (!chan) {
00065       ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd);
00066       return -1;
00067    }
00068 
00069    if (ast_channel_caller(chan)->id.number.valid && ast_channel_caller(chan)->id.number.str) {
00070       if (!ast_db_get("blacklist", ast_channel_caller(chan)->id.number.str, blacklist, sizeof (blacklist)))
00071          bl = 1;
00072    }
00073    if (ast_channel_caller(chan)->id.name.valid && ast_channel_caller(chan)->id.name.str) {
00074       if (!ast_db_get("blacklist", ast_channel_caller(chan)->id.name.str, blacklist, sizeof (blacklist)))
00075          bl = 1;
00076    }
00077 
00078    snprintf(buf, len, "%d", bl);
00079    return 0;
00080 }
00081 
00082 static int blacklist_read2(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **str, ssize_t len)
00083 {
00084    /* 2 bytes is a single integer, plus terminating null */
00085    if (ast_str_size(*str) - ast_str_strlen(*str) < 2) {
00086       if (len > ast_str_size(*str) || len == 0) {
00087          ast_str_make_space(str, len ? len : ast_str_strlen(*str) + 2);
00088       }
00089    }
00090    if (ast_str_size(*str) - ast_str_strlen(*str) >= 2) {
00091       int res = blacklist_read(chan, cmd, data, ast_str_buffer(*str) + ast_str_strlen(*str), 2);
00092       ast_str_update(*str);
00093       return res;
00094    }
00095    return -1;
00096 }
00097 
00098 static struct ast_custom_function blacklist_function = {
00099    .name = "BLACKLIST",
00100    .read = blacklist_read,
00101    .read2 = blacklist_read2,
00102 };
00103 
00104 static int unload_module(void)
00105 {
00106    return ast_custom_function_unregister(&blacklist_function);
00107 }
00108 
00109 static int load_module(void)
00110 {
00111    return ast_custom_function_register(&blacklist_function);
00112 }
00113 
00114 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Look up Caller*ID name/number from blacklist database");

Generated on Thu Apr 16 06:27:35 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6