- mark some applications deprecated that already have replacements
[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 <stdlib.h>
30 #include <stdio.h>
31 #include <string.h>
32
33 #include "asterisk.h"
34
35 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
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 *tdesc = "Look up Caller*ID name/number from blacklist database";
51
52 static char *app = "LookupBlacklist";
53
54 static char *synopsis = "Look up Caller*ID name/number from blacklist database";
55
56 static char *descrip =
57   "  LookupBlacklist(options): Looks up the Caller*ID number on the active\n"
58   "channel in the Asterisk database (family 'blacklist').  \n"
59   "The option string may contain the following character:\n"
60   "     'j' -- jump to n+101 priority if the number/name is found in the blacklist\n"
61   "This application sets the following channel variable upon completion:\n"
62   "     LOOKUPBLSTATUS          The status of the Blacklist lookup as a text string, one of\n"
63   "             FOUND | NOTFOUND\n"
64   "Example: exten => 1234,1,LookupBlacklist()\n";
65
66 LOCAL_USER_DECL;
67
68 static int blacklist_read(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
69 {
70         char blacklist[1];
71         int bl = 0;
72
73         if (chan->cid.cid_num) {
74                 if (!ast_db_get("blacklist", chan->cid.cid_num, blacklist, sizeof (blacklist)))
75                         bl = 1;
76         }
77         if (chan->cid.cid_name) {
78                 if (!ast_db_get("blacklist", chan->cid.cid_name, blacklist, sizeof (blacklist)))
79                         bl = 1;
80         }
81
82         snprintf(buf, len, "%d", bl);
83         return 0;
84 }
85
86 static struct ast_custom_function blacklist_function = {
87         .name = "BLACKLIST",
88         .synopsis = "Check if the callerid is on the blacklist",
89         .desc = "Uses astdb to check if the Caller*ID is in family 'blacklist'.  Returns 1 or 0.\n",
90         .syntax = "BLACKLIST()",
91         .read = blacklist_read,
92 };
93
94 static int
95 lookupblacklist_exec (struct ast_channel *chan, void *data)
96 {
97         char blacklist[1];
98         struct localuser *u;
99         int bl = 0;
100         int priority_jump = 0;
101         static int dep_warning = 0;
102
103         LOCAL_USER_ADD(u);
104
105         if (!dep_warning) {
106                 dep_warning = 1;
107                 ast_log(LOG_WARNING, "LookupBlacklist is deprecated.  Please use ${BLACKLIST()} instead.\n");
108         }
109
110         if (!ast_strlen_zero(data)) {
111                 if (strchr(data, 'j'))
112                         priority_jump = 1;
113         }
114
115         if (chan->cid.cid_num) {
116                 if (!ast_db_get("blacklist", chan->cid.cid_num, blacklist, sizeof (blacklist))) {
117                         if (option_verbose > 2)
118                                 ast_log(LOG_NOTICE, "Blacklisted number %s found\n",chan->cid.cid_num);
119                         bl = 1;
120                 }
121         }
122         if (chan->cid.cid_name) {
123                 if (!ast_db_get("blacklist", chan->cid.cid_name, blacklist, sizeof (blacklist))) {
124                         if (option_verbose > 2)
125                                 ast_log (LOG_NOTICE,"Blacklisted name \"%s\" found\n",chan->cid.cid_name);
126                         bl = 1;
127                 }
128         }
129
130         if (bl) {
131                 if (priority_jump || ast_opt_priority_jumping) 
132                         ast_goto_if_exists(chan, chan->context, chan->exten, chan->priority + 101);
133                 pbx_builtin_setvar_helper(chan, "LOOKUPBLSTATUS", "FOUND");
134         } else
135                 pbx_builtin_setvar_helper(chan, "LOOKUPBLSTATUS", "NOTFOUND");  
136
137         LOCAL_USER_REMOVE(u);
138
139         return 0;
140 }
141
142 static int unload_module(void *mod)
143 {
144         int res;
145
146         res = ast_unregister_application(app);
147         res |= ast_custom_function_unregister(&blacklist_function);
148
149         STANDARD_HANGUP_LOCALUSERS;
150
151         return res;     
152 }
153
154 static int load_module(void *mod)
155 {
156         int res = ast_custom_function_register(&blacklist_function);
157         res |= ast_register_application (app, lookupblacklist_exec, synopsis,descrip);
158         return res;
159 }
160
161 static const char *description(void)
162 {
163         return tdesc;
164 }
165
166 static const char *key(void)
167 {
168         return ASTERISK_GPL_KEY;
169 }
170
171 STD_MOD1;