Asterisk - The Open Source Telephony Project  18.5.0
func_blacklist.c
Go to the documentation of this file.
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2005, Digium, Inc.
5  *
6  * Mark Spencer <[email protected]>
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 
19 /*! \file
20  *
21  * \brief Function to lookup the callerid number, and see if it is blacklisted
22  *
23  * \author Mark Spencer <[email protected]>
24  *
25  * \ingroup functions
26  *
27  */
28 
29 /*** MODULEINFO
30  <support_level>core</support_level>
31  ***/
32 
33 #include "asterisk.h"
34 
35 #include "asterisk/pbx.h"
36 #include "asterisk/module.h"
37 #include "asterisk/channel.h"
38 #include "asterisk/astdb.h"
39 
40 /*** DOCUMENTATION
41  <function name="BLACKLIST" language="en_US">
42  <synopsis>
43  Check if the callerid is on the blacklist.
44  </synopsis>
45  <syntax />
46  <description>
47  <para>Uses astdb to check if the Caller*ID is in family <literal>blacklist</literal>.
48  Returns <literal>1</literal> or <literal>0</literal>.</para>
49  </description>
50  <see-also>
51  <ref type="function">DB</ref>
52  </see-also>
53  </function>
54 
55 ***/
56 
57 static int blacklist_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
58 {
59  char blacklist[1];
60  int bl = 0;
61 
62  if (!chan) {
63  ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd);
64  return -1;
65  }
66 
67  if (ast_channel_caller(chan)->id.number.valid && ast_channel_caller(chan)->id.number.str) {
68  if (!ast_db_get("blacklist", ast_channel_caller(chan)->id.number.str, blacklist, sizeof (blacklist)))
69  bl = 1;
70  }
71  if (ast_channel_caller(chan)->id.name.valid && ast_channel_caller(chan)->id.name.str) {
72  if (!ast_db_get("blacklist", ast_channel_caller(chan)->id.name.str, blacklist, sizeof (blacklist)))
73  bl = 1;
74  }
75 
76  snprintf(buf, len, "%d", bl);
77  return 0;
78 }
79 
80 static int blacklist_read2(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **str, ssize_t len)
81 {
82  /* 2 bytes is a single integer, plus terminating null */
83  if (ast_str_size(*str) - ast_str_strlen(*str) < 2) {
84  if (len > ast_str_size(*str) || len == 0) {
85  ast_str_make_space(str, len ? len : ast_str_strlen(*str) + 2);
86  }
87  }
88  if (ast_str_size(*str) - ast_str_strlen(*str) >= 2) {
89  int res = blacklist_read(chan, cmd, data, ast_str_buffer(*str) + ast_str_strlen(*str), 2);
90  ast_str_update(*str);
91  return res;
92  }
93  return -1;
94 }
95 
97  .name = "BLACKLIST",
98  .read = blacklist_read,
99  .read2 = blacklist_read2,
100 };
101 
102 static int unload_module(void)
103 {
104  return ast_custom_function_unregister(&blacklist_function);
105 }
106 
107 static int load_module(void)
108 {
109  return ast_custom_function_register(&blacklist_function);
110 }
111 
112 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Look up Caller*ID name/number from blacklist database");
const char * name
Definition: pbx.h:119
struct ast_party_caller * ast_channel_caller(struct ast_channel *chan)
Main Channel structure associated with a channel.
#define AST_MODULE_INFO_STANDARD(keystr, desc)
Definition: module.h:567
Asterisk main include file. File version handling, generic pbx functions.
size_t ast_str_size(const struct ast_str *buf)
Returns the current maximum length (without reallocation) of the current buffer.
Definition: strings.h:699
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
#define LOG_WARNING
Definition: logger.h:274
#define ast_str_make_space(buf, new_len)
Definition: strings.h:780
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:714
static int blacklist_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
static int blacklist_read2(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **str, ssize_t len)
const char * str
Definition: app_jack.c:147
int ast_custom_function_unregister(struct ast_custom_function *acf)
Unregister a custom function.
Number structure.
Definition: app_followme.c:154
static int unload_module(void)
#define ast_log
Definition: astobj2.c:42
General Asterisk PBX channel definitions.
Data structure associated with a custom dialplan function.
Definition: pbx.h:118
Core PBX routines and definitions.
static struct ast_custom_function blacklist_function
The descriptor of a dynamic string XXX storage will be optimized later if needed We use the ts field ...
Definition: strings.h:584
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
static int load_module(void)
static const char name[]
Definition: cdr_mysql.c:74
int ast_db_get(const char *family, const char *key, char *value, int valuelen)
Get key value specified by family/key.
Definition: main/db.c:412
size_t ast_str_strlen(const struct ast_str *buf)
Returns the current length of the string stored within buf.
Definition: strings.h:688
void ast_str_update(struct ast_str *buf)
Update the length of the buffer, after using ast_str merely as a buffer.
Definition: strings.h:663
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
Asterisk module definitions.
Persistant data storage (akin to *doze registry)
#define ast_custom_function_register(acf)
Register a custom function.
Definition: pbx.h:1508