SS7 marked the start of an open season for trunk again but here's something minor...
[asterisk/asterisk.git] / apps / app_lookupblacklist.c
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 App to lookup the callerid number, and see if it is blacklisted
22  *
23  * \author Mark Spencer <markster@digium.com>
24  *
25  * \ingroup applications
26  * 
27  */
28
29 #include "asterisk.h"
30
31 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
32
33 #include <stdlib.h>
34 #include <stdio.h>
35 #include <string.h>
36
37 #include "asterisk/lock.h"
38 #include "asterisk/file.h"
39 #include "asterisk/logger.h"
40 #include "asterisk/options.h"
41 #include "asterisk/channel.h"
42 #include "asterisk/pbx.h"
43 #include "asterisk/module.h"
44 #include "asterisk/translate.h"
45 #include "asterisk/image.h"
46 #include "asterisk/callerid.h"
47 #include "asterisk/astdb.h"
48 #include "asterisk/options.h"
49
50 static char *app = "LookupBlacklist";
51
52 static char *synopsis = "Look up Caller*ID name/number from blacklist database";
53
54 static char *descrip =
55   "  LookupBlacklist(options): Looks up the Caller*ID number on the active\n"
56   "channel in the Asterisk database (family 'blacklist').  \n"
57   "The option string may contain the following character:\n"
58   "     'j' -- jump to n+101 priority if the number/name is found in the blacklist\n"
59   "This application sets the following channel variable upon completion:\n"
60   "     LOOKUPBLSTATUS          The status of the Blacklist lookup as a text string, one of\n"
61   "             FOUND | NOTFOUND\n"
62   "Example: exten => 1234,1,LookupBlacklist()\n";
63
64
65 static int blacklist_read(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
66 {
67         char blacklist[1];
68         int bl = 0;
69
70         if (chan->cid.cid_num) {
71                 if (!ast_db_get("blacklist", chan->cid.cid_num, blacklist, sizeof (blacklist)))
72                         bl = 1;
73         }
74         if (chan->cid.cid_name) {
75                 if (!ast_db_get("blacklist", chan->cid.cid_name, blacklist, sizeof (blacklist)))
76                         bl = 1;
77         }
78
79         snprintf(buf, len, "%d", bl);
80         return 0;
81 }
82
83 static struct ast_custom_function blacklist_function = {
84         .name = "BLACKLIST",
85         .synopsis = "Check if the callerid is on the blacklist",
86         .desc = "Uses astdb to check if the Caller*ID is in family 'blacklist'.  Returns 1 or 0.\n",
87         .syntax = "BLACKLIST()",
88         .read = blacklist_read,
89 };
90
91 static int
92 lookupblacklist_exec (struct ast_channel *chan, void *data)
93 {
94         char blacklist[1];
95         struct ast_module_user *u;
96         int bl = 0;
97         int priority_jump = 0;
98         static int dep_warning = 0;
99
100         u = ast_module_user_add(chan);
101
102         if (!dep_warning) {
103                 dep_warning = 1;
104                 ast_log(LOG_WARNING, "LookupBlacklist is deprecated.  Please use ${BLACKLIST()} instead.\n");
105         }
106
107         if (!ast_strlen_zero(data)) {
108                 if (strchr(data, 'j'))
109                         priority_jump = 1;
110         }
111
112         if (chan->cid.cid_num) {
113                 if (!ast_db_get("blacklist", chan->cid.cid_num, blacklist, sizeof (blacklist))) {
114                         if (option_verbose > 2)
115                                 ast_log(LOG_NOTICE, "Blacklisted number %s found\n",chan->cid.cid_num);
116                         bl = 1;
117                 }
118         }
119         if (chan->cid.cid_name) {
120                 if (!ast_db_get("blacklist", chan->cid.cid_name, blacklist, sizeof (blacklist))) {
121                         if (option_verbose > 2)
122                                 ast_log (LOG_NOTICE,"Blacklisted name \"%s\" found\n",chan->cid.cid_name);
123                         bl = 1;
124                 }
125         }
126
127         if (bl) {
128                 if (priority_jump || ast_opt_priority_jumping) 
129                         ast_goto_if_exists(chan, chan->context, chan->exten, chan->priority + 101);
130                 pbx_builtin_setvar_helper(chan, "LOOKUPBLSTATUS", "FOUND");
131         } else
132                 pbx_builtin_setvar_helper(chan, "LOOKUPBLSTATUS", "NOTFOUND");  
133
134         ast_module_user_remove(u);
135
136         return 0;
137 }
138
139 static int unload_module(void)
140 {
141         int res;
142
143         res = ast_unregister_application(app);
144         res |= ast_custom_function_unregister(&blacklist_function);
145
146         ast_module_user_hangup_all();
147
148         return res;     
149 }
150
151 static int load_module(void)
152 {
153         int res = ast_custom_function_register(&blacklist_function);
154         res |= ast_register_application (app, lookupblacklist_exec, synopsis,descrip);
155         return res;
156 }
157
158 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Look up Caller*ID name/number from blacklist database");