Merged revisions 321330 via svnmerge from
[asterisk/asterisk.git] / apps / app_privacy.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 Block all calls without Caller*ID, require phone # to be entered
22  *
23  * \author Mark Spencer <markster@digium.com>
24  *
25  * \ingroup applications
26  */
27
28 #include "asterisk.h"
29
30 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
31
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"
43
44 /*** DOCUMENTATION
45         <application name="PrivacyManager" language="en_US">
46                 <synopsis>
47                         Require phone number to be entered, if no CallerID sent
48                 </synopsis>
49                 <syntax>
50                         <parameter name="maxretries">
51                                 <para>Total tries caller is allowed to input a callerid. Defaults to <literal>3</literal>.</para>
52                         </parameter>
53                         <parameter name="minlength">
54                                 <para>Minimum allowable digits in the input callerid number. Defaults to <literal>10</literal>.</para>
55                         </parameter>
56                         <parameter name="context">
57                                 <para>Context to check the given callerid against patterns.</para>
58                         </parameter>
59                 </syntax>
60                 <description>
61                         <para>If no Caller*ID is sent, PrivacyManager answers the channel and asks
62                         the caller to enter their phone number. The caller is given
63                         <replaceable>maxretries</replaceable> attempts to do so. The application does
64                         <emphasis>nothing</emphasis> if Caller*ID was received on the channel.</para>
65                         <para>The application sets the following channel variable upon completion:</para>
66                         <variablelist>
67                                 <variable name="PRIVACYMGRSTATUS">
68                                         <para>The status of the privacy manager's attempt to collect a phone number from the user.</para>
69                                         <value name="SUCCESS"/>
70                                         <value name="FAILED"/>
71                                 </variable>
72                         </variablelist>
73                 </description>
74                 <see-also>
75                         <ref type="application">Zapateller</ref>
76                 </see-also>
77         </application>
78  ***/
79
80
81 static char *app = "PrivacyManager";
82
83 static int privacy_exec(struct ast_channel *chan, const char *data)
84 {
85         int res=0;
86         int retries;
87         int maxretries = 3;
88         int minlength = 10;
89         int x = 0;
90         char phone[30];
91         char *parse = NULL;
92         AST_DECLARE_APP_ARGS(args,
93                 AST_APP_ARG(maxretries);
94                 AST_APP_ARG(minlength);
95                 AST_APP_ARG(checkcontext);
96
97                 /* This must be last in the list until we actually have some options again. :) */
98                 AST_APP_ARG(options);
99         );
100
101         if (chan->caller.id.number.valid
102                 && !ast_strlen_zero(chan->caller.id.number.str)) {
103                 ast_verb(3, "CallerID number present: Skipping\n");
104         } else {
105                 /*Answer the channel if it is not already*/
106                 if (chan->_state != AST_STATE_UP) {
107                         if ((res = ast_answer(chan))) {
108                                 return -1;
109                         }
110                 }
111
112                 parse = ast_strdupa(data);
113
114                 AST_STANDARD_APP_ARGS(args, parse);
115
116                 if (!ast_strlen_zero(args.maxretries)) {
117                         if (sscanf(args.maxretries, "%30d", &x) == 1 && x > 0) {
118                                 maxretries = x;
119                         } else {
120                                 ast_log(LOG_WARNING, "Invalid max retries argument: '%s'\n", args.maxretries);
121                         }
122                 }
123                 if (!ast_strlen_zero(args.minlength)) {
124                         if (sscanf(args.minlength, "%30d", &x) == 1 && x > 0) {
125                                 minlength = x;
126                         } else {
127                                 ast_log(LOG_WARNING, "Invalid min length argument: '%s'\n", args.minlength);
128                         }
129                 }
130
131                 /* Play unidentified call */
132                 res = ast_safe_sleep(chan, 1000);
133                 if (!res) {
134                         res = ast_streamfile(chan, "privacy-unident", chan->language);
135                 }
136                 if (!res) {
137                         res = ast_waitstream(chan, "");
138                 }
139
140                 /* Ask for 10 digit number, give 3 attempts */
141                 for (retries = 0; retries < maxretries; retries++) {
142                         if (!res) {
143                                 res = ast_streamfile(chan, "privacy-prompt", chan->language);
144                         }
145                         if (!res) {
146                                 res = ast_waitstream(chan, "");
147                         }
148
149                         if (!res) {
150                                 res = ast_readstring(chan, phone, sizeof(phone) - 1, /* digit timeout ms */ 3200, /* first digit timeout */ 5000, "#");
151                         }
152
153                         if (res < 0) {
154                                 break;
155                         }
156
157                         /* Make sure we get at least digits */
158                         if (strlen(phone) >= minlength ) {
159                                 /* if we have a checkcontext argument, do pattern matching */
160                                 if (!ast_strlen_zero(args.checkcontext)) {
161                                         if (!ast_exists_extension(NULL, args.checkcontext, phone, 1, NULL)) {
162                                                 res = ast_streamfile(chan, "privacy-incorrect", chan->language);
163                                                 if (!res) {
164                                                         res = ast_waitstream(chan, "");
165                                                 }
166                                         } else {
167                                                 break;
168                                         }
169                                 } else {
170                                         break;
171                                 }
172                         } else {
173                                 res = ast_streamfile(chan, "privacy-incorrect", chan->language);
174                                 if (!res) {
175                                         res = ast_waitstream(chan, "");
176                                 }
177                         }
178                 }
179
180                 /* Got a number, play sounds and send them on their way */
181                 if ((retries < maxretries) && res >= 0) {
182                         res = ast_streamfile(chan, "privacy-thankyou", chan->language);
183                         if (!res) {
184                                 res = ast_waitstream(chan, "");
185                         }
186
187                         /*
188                          * This is a caller entered number that is going to be used locally.
189                          * Therefore, the given number presentation is allowed and should
190                          * be passed out to other channels.  This is the point of the
191                          * privacy application.
192                          */
193                         chan->caller.id.name.presentation = AST_PRES_ALLOWED_USER_NUMBER_NOT_SCREENED;
194                         chan->caller.id.number.presentation = AST_PRES_ALLOWED_USER_NUMBER_NOT_SCREENED;
195                         chan->caller.id.number.plan = 0;/* Unknown */
196
197                         ast_set_callerid(chan, phone, "Privacy Manager", NULL);
198
199                         ast_verb(3, "Changed Caller*ID number to '%s'\n", phone);
200
201                         pbx_builtin_setvar_helper(chan, "PRIVACYMGRSTATUS", "SUCCESS");
202                 } else {
203                         pbx_builtin_setvar_helper(chan, "PRIVACYMGRSTATUS", "FAILED");
204                 }
205         }
206
207         return 0;
208 }
209
210 static int unload_module(void)
211 {
212         return ast_unregister_application(app);
213 }
214
215 static int load_module(void)
216 {
217         return ast_register_application_xml(app, privacy_exec);
218 }
219
220 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Require phone number to be entered, if no CallerID sent");