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