Creating tag for the release of asterisk-1.6.1-beta1
[asterisk-bristuff.git] / apps / app_privacy.c
blob09f698861bf8ab7c57f1f8864ea7e25c40a2d543
1 /*
2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
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.
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.
19 /*! \file
21 * \brief Block all calls without Caller*ID, require phone # to be entered
23 * \author Mark Spencer <markster@digium.com>
25 * \ingroup applications
28 #include "asterisk.h"
30 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
32 #include "asterisk/lock.h"
33 #include "asterisk/file.h"
34 #include "asterisk/utils.h"
35 #include "asterisk/channel.h"
36 #include "asterisk/pbx.h"
37 #include "asterisk/module.h"
38 #include "asterisk/translate.h"
39 #include "asterisk/image.h"
40 #include "asterisk/callerid.h"
41 #include "asterisk/app.h"
42 #include "asterisk/config.h"
44 static char *app = "PrivacyManager";
46 static char *synopsis = "Require phone number to be entered, if no CallerID sent";
48 static char *descrip =
49 " PrivacyManager([maxretries][,minlength][,context]): If no Caller*ID \n"
50 "is sent, PrivacyManager answers the channel and asks the caller to\n"
51 "enter their phone number. The caller is given 'maxretries' attempts to do so.\n"
52 "The application does nothing if Caller*ID was received on the channel.\n"
53 " maxretries default 3 -maximum number of attempts the caller is allowed \n"
54 " to input a callerid.\n"
55 " minlength default 10 -minimum allowable digits in the input callerid number.\n"
56 " context context to check the given Caller*ID against patterns.\n"
57 "The application sets the following channel variable upon completion: \n"
58 "PRIVACYMGRSTATUS The status of the privacy manager's attempt to collect \n"
59 " a phone number from the user. A text string that is either:\n"
60 " SUCCESS | FAILED \n"
64 static int privacy_exec (struct ast_channel *chan, void *data)
66 int res=0;
67 int retries;
68 int maxretries = 3;
69 int minlength = 10;
70 int x = 0;
71 char phone[30];
72 char *parse = NULL;
73 AST_DECLARE_APP_ARGS(args,
74 AST_APP_ARG(maxretries);
75 AST_APP_ARG(minlength);
76 AST_APP_ARG(options);
77 AST_APP_ARG(checkcontext);
80 if (!ast_strlen_zero(chan->cid.cid_num)) {
81 ast_verb(3, "CallerID Present: Skipping\n");
82 } else {
83 /*Answer the channel if it is not already*/
84 if (chan->_state != AST_STATE_UP) {
85 if ((res = ast_answer(chan)))
86 return -1;
89 if (!ast_strlen_zero(data)) {
90 parse = ast_strdupa(data);
92 AST_STANDARD_APP_ARGS(args, parse);
94 if (args.maxretries) {
95 if (sscanf(args.maxretries, "%d", &x) == 1)
96 maxretries = x;
97 else
98 ast_log(LOG_WARNING, "Invalid max retries argument\n");
100 if (args.minlength) {
101 if (sscanf(args.minlength, "%d", &x) == 1)
102 minlength = x;
103 else
104 ast_log(LOG_WARNING, "Invalid min length argument\n");
108 /* Play unidentified call */
109 res = ast_safe_sleep(chan, 1000);
110 if (!res)
111 res = ast_streamfile(chan, "privacy-unident", chan->language);
112 if (!res)
113 res = ast_waitstream(chan, "");
115 /* Ask for 10 digit number, give 3 attempts */
116 for (retries = 0; retries < maxretries; retries++) {
117 if (!res)
118 res = ast_streamfile(chan, "privacy-prompt", chan->language);
119 if (!res)
120 res = ast_waitstream(chan, "");
122 if (!res )
123 res = ast_readstring(chan, phone, sizeof(phone) - 1, /* digit timeout ms */ 3200, /* first digit timeout */ 5000, "#");
125 if (res < 0)
126 break;
128 /* Make sure we get at least digits */
129 if (strlen(phone) >= minlength ) {
130 /* if we have a checkcontext argument, do pattern matching */
131 if (!ast_strlen_zero(args.checkcontext)) {
132 if (!ast_exists_extension(NULL, args.checkcontext, phone, 1, NULL)) {
133 res = ast_streamfile(chan, "privacy-incorrect", chan->language);
134 if (!res) {
135 res = ast_waitstream(chan, "");
137 } else {
138 break;
140 } else {
141 break;
143 } else {
144 res = ast_streamfile(chan, "privacy-incorrect", chan->language);
145 if (!res)
146 res = ast_waitstream(chan, "");
150 /* Got a number, play sounds and send them on their way */
151 if ((retries < maxretries) && res >= 0 ) {
152 res = ast_streamfile(chan, "privacy-thankyou", chan->language);
153 if (!res)
154 res = ast_waitstream(chan, "");
156 ast_set_callerid (chan, phone, "Privacy Manager", NULL);
158 /* Clear the unavailable presence bit so if it came in on PRI
159 * the caller id will now be passed out to other channels
161 chan->cid.cid_pres &= (AST_PRES_UNAVAILABLE ^ 0xFF);
163 ast_verb(3, "Changed Caller*ID to %s, callerpres to %d\n",phone,chan->cid.cid_pres);
165 pbx_builtin_setvar_helper(chan, "PRIVACYMGRSTATUS", "SUCCESS");
166 } else {
167 pbx_builtin_setvar_helper(chan, "PRIVACYMGRSTATUS", "FAILED");
171 return 0;
174 static int unload_module(void)
176 return ast_unregister_application (app);
179 static int load_module(void)
181 return ast_register_application(app, privacy_exec, synopsis, descrip);
184 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Require phone number to be entered, if no CallerID sent");