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.
21 * \brief Block all calls without Caller*ID, require phone # to be entered
23 * \author Mark Spencer <markster@digium.com>
25 * \ingroup applications
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
)
73 AST_DECLARE_APP_ARGS(args
,
74 AST_APP_ARG(maxretries
);
75 AST_APP_ARG(minlength
);
77 AST_APP_ARG(checkcontext
);
80 if (!ast_strlen_zero(chan
->cid
.cid_num
)) {
81 ast_verb(3, "CallerID Present: Skipping\n");
83 /*Answer the channel if it is not already*/
84 if (chan
->_state
!= AST_STATE_UP
) {
85 if ((res
= ast_answer(chan
)))
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)
98 ast_log(LOG_WARNING
, "Invalid max retries argument\n");
100 if (args
.minlength
) {
101 if (sscanf(args
.minlength
, "%d", &x
) == 1)
104 ast_log(LOG_WARNING
, "Invalid min length argument\n");
108 /* Play unidentified call */
109 res
= ast_safe_sleep(chan
, 1000);
111 res
= ast_streamfile(chan
, "privacy-unident", chan
->language
);
113 res
= ast_waitstream(chan
, "");
115 /* Ask for 10 digit number, give 3 attempts */
116 for (retries
= 0; retries
< maxretries
; retries
++) {
118 res
= ast_streamfile(chan
, "privacy-prompt", chan
->language
);
120 res
= ast_waitstream(chan
, "");
123 res
= ast_readstring(chan
, phone
, sizeof(phone
) - 1, /* digit timeout ms */ 3200, /* first digit timeout */ 5000, "#");
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
);
135 res
= ast_waitstream(chan
, "");
144 res
= ast_streamfile(chan
, "privacy-incorrect", chan
->language
);
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
);
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");
167 pbx_builtin_setvar_helper(chan
, "PRIVACYMGRSTATUS", "FAILED");
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");