2 * Asterisk -- A telephony toolkit for Linux.
4 * Block all calls without Caller*ID, require phone # to be entered
6 * Copyright (C) 1999-2004, Digium, Inc.
8 * Mark Spencer <markster@digium.com>
10 * This program is free software, distributed under the terms of
11 * the GNU General Public License
14 #include <asterisk/lock.h>
15 #include <asterisk/file.h>
16 #include <asterisk/logger.h>
17 #include <asterisk/options.h>
18 #include <asterisk/channel.h>
19 #include <asterisk/pbx.h>
20 #include <asterisk/module.h>
21 #include <asterisk/translate.h>
22 #include <asterisk/image.h>
23 #include <asterisk/callerid.h>
24 #include <asterisk/app.h>
25 #include <asterisk/config.h>
29 #define PRIV_CONFIG "privacy.conf"
31 static char *tdesc = "Require phone number to be entered, if no CallerID sent";
33 static char *app = "PrivacyManager";
35 static char *synopsis = "Require phone number to be entered, if no CallerID sent";
37 static char *descrip =
38 " PrivacyManager: If no Caller*ID is sent, PrivacyManager answers the\n"
39 "channel and asks the caller to enter their phone number.\n"
40 "The caller is given 3 attempts. If after 3 attempts, they do not enter\n"
41 "at least a 10 digit phone number, and if there exists a priority n + 101,\n"
42 "where 'n' is the priority of the current instance, then the\n"
43 "channel will be setup to continue at that priority level.\n"
44 "Otherwise, it returns 0. Does nothing if Caller*ID was received on the\n"
46 " Configuration file privacy.conf contains two variables:\n"
47 " maxretries default 3 -maximum number of attempts the caller is allowed to input a callerid.\n"
48 " minlength default 10 -minimum allowable digits in the input callerid number.\n"
58 privacy_exec (struct ast_channel *chan, void *data)
69 struct ast_config *cfg;
72 if (chan->cid.cid_num && !ast_strlen_zero(chan->cid.cid_num)) {
73 if (option_verbose > 2)
74 ast_verbose (VERBOSE_PREFIX_3 "CallerID Present: Skipping\n");
76 /*Answer the channel if it is not already*/
77 if (chan->_state != AST_STATE_UP) {
78 res = ast_answer(chan);
84 /*Read in the config file*/
85 cfg = ast_load(PRIV_CONFIG);
88 /*Play unidentified call*/
89 res = ast_safe_sleep(chan, 1000);
91 res = ast_streamfile(chan, "privacy-unident", chan->language);
93 res = ast_waitstream(chan, "");
95 if (cfg && (s = ast_variable_retrieve(cfg, "general", "maxretries"))) {
96 if (sscanf(s, "%d", &x) == 1) {
99 ast_log(LOG_WARNING, "Invalid max retries argument\n");
102 if (cfg && (s = ast_variable_retrieve(cfg, "general", "minlength"))) {
103 if (sscanf(s, "%d", &x) == 1) {
106 ast_log(LOG_WARNING, "Invalid min length argument\n");
110 /*Ask for 10 digit number, give 3 attempts*/
111 for (retries = 0; retries < maxretries; retries++) {
113 res = ast_streamfile(chan, "privacy-prompt", chan->language);
115 res = ast_waitstream(chan, "");
118 res = ast_readstring(chan, phone, sizeof(phone) - 1, /* digit timeout ms */ 3200, /* first digit timeout */ 5000, "#");
123 /*Make sure we get at least digits*/
124 if (strlen(phone) >= minlength )
127 res = ast_streamfile(chan, "privacy-incorrect", chan->language);
129 res = ast_waitstream(chan, "");
133 /*Got a number, play sounds and send them on their way*/
134 if ((retries < maxretries) && res == 1 ) {
135 res = ast_streamfile(chan, "privacy-thankyou", chan->language);
137 res = ast_waitstream(chan, "");
138 ast_set_callerid (chan, phone, "Privacy Manager", NULL);
139 if (option_verbose > 2)
140 ast_verbose (VERBOSE_PREFIX_3 "Changed Caller*ID to %s\n",new_cid);
142 /*Send the call to n+101 priority, where n is the current priority*/
143 if (ast_exists_extension(chan, chan->context, chan->exten, chan->priority + 101, chan->cid.cid_num))
150 LOCAL_USER_REMOVE (u);
157 STANDARD_HANGUP_LOCALUSERS;
158 return ast_unregister_application (app);
164 return ast_register_application (app, privacy_exec, synopsis,
178 STANDARD_USECOUNT (res);
185 return ASTERISK_GPL_KEY;