Wed Jan 8 2020 09:49:47

Asterisk developer's documentation


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 <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 
19 /*! \file
20  *
21  * \brief Function to lookup the callerid number, and see if it is blacklisted
22  *
23  * \author Mark Spencer <markster@digium.com>
24  *
25  * \ingroup functions
26  *
27  */
28 
29 /*** MODULEINFO
30  <support_level>core</support_level>
31  ***/
32 
33 #include "asterisk.h"
34 
35 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 411313 $")
36 
37 #include "asterisk/pbx.h"
38 #include "asterisk/module.h"
39 #include "asterisk/channel.h"
40 #include "asterisk/astdb.h"
41 
42 /*** DOCUMENTATION
43  <function name="BLACKLIST" language="en_US">
44  <synopsis>
45  Check if the callerid is on the blacklist.
46  </synopsis>
47  <syntax />
48  <description>
49  <para>Uses astdb to check if the Caller*ID is in family <literal>blacklist</literal>.
50  Returns <literal>1</literal> or <literal>0</literal>.</para>
51  </description>
52  <see-also>
53  <ref type="function">DB</ref>
54  </see-also>
55  </function>
56 
57 ***/
58 
59 static int blacklist_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
60 {
61  char blacklist[1];
62  int bl = 0;
63 
64  if (!chan) {
65  ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd);
66  return -1;
67  }
68 
69  if (chan->caller.id.number.valid && chan->caller.id.number.str) {
70  if (!ast_db_get("blacklist", chan->caller.id.number.str, blacklist, sizeof (blacklist)))
71  bl = 1;
72  }
73  if (chan->caller.id.name.valid && chan->caller.id.name.str) {
74  if (!ast_db_get("blacklist", chan->caller.id.name.str, blacklist, sizeof (blacklist)))
75  bl = 1;
76  }
77 
78  snprintf(buf, len, "%d", bl);
79  return 0;
80 }
81 
82 static int blacklist_read2(struct ast_channel *chan, const char *cmd, char *data, struct ast_str **str, ssize_t len)
83 {
84  /* 2 bytes is a single integer, plus terminating null */
85  if (ast_str_size(*str) - ast_str_strlen(*str) < 2) {
86  if (len > ast_str_size(*str) || len == 0) {
87  ast_str_make_space(str, len ? len : ast_str_strlen(*str) + 2);
88  }
89  }
90  if (ast_str_size(*str) - ast_str_strlen(*str) >= 2) {
91  int res = blacklist_read(chan, cmd, data, ast_str_buffer(*str) + ast_str_strlen(*str), 2);
92  ast_str_update(*str);
93  return res;
94  }
95  return -1;
96 }
97 
99  .name = "BLACKLIST",
100  .read = blacklist_read,
101  .read2 = blacklist_read2,
102 };
103 
104 static int unload_module(void)
105 {
106  return ast_custom_function_unregister(&blacklist_function);
107 }
108 
109 static int load_module(void)
110 {
111  return ast_custom_function_register(&blacklist_function);
112 }
113 
114 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Look up Caller*ID name/number from blacklist database");
Main Channel structure associated with a channel.
Definition: channel.h:742
char * str
Subscriber phone number (Malloced)
Definition: channel.h:241
#define AST_MODULE_INFO_STANDARD(keystr, desc)
Definition: module.h:396
Asterisk main include file. File version handling, generic pbx functions.
struct ast_party_caller caller
Channel Caller ID information.
Definition: channel.h:804
int ast_db_get(const char *family, const char *key, char *out, int outlen)
Get key value specified by family/key.
Definition: db.c:348
size_t ast_str_size(const struct ast_str *buf)
Returns the current maximum length (without reallocation) of the current buffer.
Definition: strings.h:482
struct ast_party_name name
Subscriber name.
Definition: channel.h:290
#define LOG_WARNING
Definition: logger.h:144
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:497
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)
char * str
Subscriber name (Malloced)
Definition: channel.h:214
const char * str
Definition: app_jack.c:144
int ast_custom_function_unregister(struct ast_custom_function *acf)
Unregister a custom function.
Definition: pbx.c:3814
struct ast_party_id id
Caller party ID.
Definition: channel.h:370
static int unload_module(void)
General Asterisk PBX channel definitions.
int ast_str_make_space(struct ast_str **buf, size_t new_len)
Definition: strings.h:588
Data structure associated with a custom dialplan function.
Definition: pbx.h:95
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:364
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
static int load_module(void)
size_t ast_str_strlen(const struct ast_str *buf)
Returns the current length of the string stored within buf.
Definition: strings.h:471
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:446
const char * name
Definition: pbx.h:96
unsigned char valid
TRUE if the name information is valid/present.
Definition: channel.h:229
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:38
Asterisk module definitions.
Persistant data storage (akin to *doze registry)
unsigned char valid
TRUE if the number information is valid/present.
Definition: channel.h:247
#define ast_custom_function_register(acf)
Register a custom function.
Definition: pbx.h:1164
#define ASTERISK_FILE_VERSION(file, version)
Register/unregister a source code file with the core.
Definition: asterisk.h:180
struct ast_party_number number
Subscriber phone number.
Definition: channel.h:292