Wed Jan 8 2020 09:49:40

Asterisk developer's documentation


app_privacy.c
Go to the documentation of this file.
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 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 328209 $")
35 
36 #include "asterisk/lock.h"
37 #include "asterisk/file.h"
38 #include "asterisk/utils.h"
39 #include "asterisk/channel.h"
40 #include "asterisk/pbx.h"
41 #include "asterisk/module.h"
42 #include "asterisk/translate.h"
43 #include "asterisk/image.h"
44 #include "asterisk/callerid.h"
45 #include "asterisk/app.h"
46 #include "asterisk/config.h"
47 
48 /*** DOCUMENTATION
49  <application name="PrivacyManager" language="en_US">
50  <synopsis>
51  Require phone number to be entered, if no CallerID sent
52  </synopsis>
53  <syntax>
54  <parameter name="maxretries">
55  <para>Total tries caller is allowed to input a callerid. Defaults to <literal>3</literal>.</para>
56  </parameter>
57  <parameter name="minlength">
58  <para>Minimum allowable digits in the input callerid number. Defaults to <literal>10</literal>.</para>
59  </parameter>
60  <parameter name="options">
61  <para>Position reserved for options.</para>
62  </parameter>
63  <parameter name="context">
64  <para>Context to check the given callerid against patterns.</para>
65  </parameter>
66  </syntax>
67  <description>
68  <para>If no Caller*ID is sent, PrivacyManager answers the channel and asks
69  the caller to enter their phone number. The caller is given
70  <replaceable>maxretries</replaceable> attempts to do so. The application does
71  <emphasis>nothing</emphasis> if Caller*ID was received on the channel.</para>
72  <para>The application sets the following channel variable upon completion:</para>
73  <variablelist>
74  <variable name="PRIVACYMGRSTATUS">
75  <para>The status of the privacy manager's attempt to collect a phone number from the user.</para>
76  <value name="SUCCESS"/>
77  <value name="FAILED"/>
78  </variable>
79  </variablelist>
80  </description>
81  <see-also>
82  <ref type="application">Zapateller</ref>
83  </see-also>
84  </application>
85  ***/
86 
87 
88 static char *app = "PrivacyManager";
89 
90 static int privacy_exec(struct ast_channel *chan, const char *data)
91 {
92  int res=0;
93  int retries;
94  int maxretries = 3;
95  int minlength = 10;
96  int x = 0;
97  char phone[30];
98  char *parse = NULL;
100  AST_APP_ARG(maxretries);
101  AST_APP_ARG(minlength);
102  AST_APP_ARG(options);
103  AST_APP_ARG(checkcontext);
104  );
105 
106  if (chan->caller.id.number.valid
107  && !ast_strlen_zero(chan->caller.id.number.str)) {
108  ast_verb(3, "CallerID number present: Skipping\n");
109  } else {
110  /*Answer the channel if it is not already*/
111  if (chan->_state != AST_STATE_UP) {
112  if ((res = ast_answer(chan))) {
113  return -1;
114  }
115  }
116 
117  parse = ast_strdupa(data);
118 
119  AST_STANDARD_APP_ARGS(args, parse);
120 
121  if (!ast_strlen_zero(args.maxretries)) {
122  if (sscanf(args.maxretries, "%30d", &x) == 1 && x > 0) {
123  maxretries = x;
124  } else {
125  ast_log(LOG_WARNING, "Invalid max retries argument: '%s'\n", args.maxretries);
126  }
127  }
128  if (!ast_strlen_zero(args.minlength)) {
129  if (sscanf(args.minlength, "%30d", &x) == 1 && x > 0) {
130  minlength = x;
131  } else {
132  ast_log(LOG_WARNING, "Invalid min length argument: '%s'\n", args.minlength);
133  }
134  }
135 
136  /* Play unidentified call */
137  res = ast_safe_sleep(chan, 1000);
138  if (!res) {
139  res = ast_streamfile(chan, "privacy-unident", chan->language);
140  }
141  if (!res) {
142  res = ast_waitstream(chan, "");
143  }
144 
145  /* Ask for 10 digit number, give 3 attempts */
146  for (retries = 0; retries < maxretries; retries++) {
147  if (!res) {
148  res = ast_streamfile(chan, "privacy-prompt", chan->language);
149  }
150  if (!res) {
151  res = ast_waitstream(chan, "");
152  }
153 
154  if (!res) {
155  res = ast_readstring(chan, phone, sizeof(phone) - 1, /* digit timeout ms */ 3200, /* first digit timeout */ 5000, "#");
156  }
157 
158  if (res < 0) {
159  break;
160  }
161 
162  /* Make sure we get at least digits */
163  if (strlen(phone) >= minlength ) {
164  /* if we have a checkcontext argument, do pattern matching */
165  if (!ast_strlen_zero(args.checkcontext)) {
166  if (!ast_exists_extension(NULL, args.checkcontext, phone, 1, NULL)) {
167  res = ast_streamfile(chan, "privacy-incorrect", chan->language);
168  if (!res) {
169  res = ast_waitstream(chan, "");
170  }
171  } else {
172  break;
173  }
174  } else {
175  break;
176  }
177  } else {
178  res = ast_streamfile(chan, "privacy-incorrect", chan->language);
179  if (!res) {
180  res = ast_waitstream(chan, "");
181  }
182  }
183  }
184 
185  /* Got a number, play sounds and send them on their way */
186  if ((retries < maxretries) && res >= 0) {
187  res = ast_streamfile(chan, "privacy-thankyou", chan->language);
188  if (!res) {
189  res = ast_waitstream(chan, "");
190  }
191 
192  /*
193  * This is a caller entered number that is going to be used locally.
194  * Therefore, the given number presentation is allowed and should
195  * be passed out to other channels. This is the point of the
196  * privacy application.
197  */
200  chan->caller.id.number.plan = 0;/* Unknown */
201 
202  ast_set_callerid(chan, phone, "Privacy Manager", NULL);
203 
204  ast_verb(3, "Changed Caller*ID number to '%s'\n", phone);
205 
206  pbx_builtin_setvar_helper(chan, "PRIVACYMGRSTATUS", "SUCCESS");
207  } else {
208  pbx_builtin_setvar_helper(chan, "PRIVACYMGRSTATUS", "FAILED");
209  }
210  }
211 
212  return 0;
213 }
214 
215 static int unload_module(void)
216 {
217  return ast_unregister_application(app);
218 }
219 
220 static int load_module(void)
221 {
223 }
224 
225 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Require phone number to be entered, if no CallerID sent");
int ast_safe_sleep(struct ast_channel *chan, int ms)
Wait for a specified amount of time, looking for hangups.
Definition: channel.c:1916
int presentation
Q.931 encoded presentation-indicator encoded field.
Definition: channel.h:227
void ast_set_callerid(struct ast_channel *chan, const char *cid_num, const char *cid_name, const char *cid_ani)
Set caller ID number, name and ANI and generate AMI event.
Definition: channel.c:7051
Main Channel structure associated with a channel.
Definition: channel.h:742
char * str
Subscriber phone number (Malloced)
Definition: channel.h:241
#define AST_MODULE_INFO_STANDARD(keystr, desc)
Definition: module.h:396
int ast_streamfile(struct ast_channel *c, const char *filename, const char *preflang)
Streams a file.
Definition: file.c:946
Asterisk main include file. File version handling, generic pbx functions.
struct ast_party_caller caller
Channel Caller ID information.
Definition: channel.h:804
int presentation
Q.931 presentation-indicator and screening-indicator encoded fields.
Definition: channel.h:245
CallerID (and other GR30) management and generation Includes code and algorithms from the Zapata libr...
Support for translation of data formats. translate.c.
struct ast_party_name name
Subscriber name.
Definition: channel.h:290
#define LOG_WARNING
Definition: logger.h:144
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application&#39;s arguments.
Definition: app.h:572
static char phone[80]
Definition: pbx_dundi.c:198
Configuration File Parser.
Generic File Format Support. Should be included by clients of the file handling routines. File service providers should instead include mod_format.h.
int ast_unregister_application(const char *app)
Unregister an application.
Definition: pbx.c:7705
#define AST_PRES_ALLOWED_USER_NUMBER_NOT_SCREENED
Definition: callerid.h:329
#define ast_verb(level,...)
Definition: logger.h:243
Utility functions.
struct ast_party_id id
Caller party ID.
Definition: channel.h:370
General Asterisk channel definitions for image handling.
static char * app
Definition: app_privacy.c:88
General Asterisk PBX channel definitions.
static int privacy_exec(struct ast_channel *chan, const char *data)
Definition: app_privacy.c:90
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:63
int ast_exists_extension(struct ast_channel *c, const char *context, const char *exten, int priority, const char *callerid)
Determine whether an extension exists.
Definition: pbx.c:5400
Core PBX routines and definitions.
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: utils.h:663
static int load_module(void)
Definition: app_privacy.c:220
static struct @350 args
int plan
Q.931 Type-Of-Number and Numbering-Plan encoded fields.
Definition: channel.h:243
static int maxretries
Definition: res_adsi.c:61
enum ast_channel_state _state
Definition: channel.h:839
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
static void parse(struct mgcp_request *req)
Definition: chan_mgcp.c:1858
int pbx_builtin_setvar_helper(struct ast_channel *chan, const char *name, const char *value)
Add a variable to the channel variable stack, removing the most recently set value for the same name...
Definition: pbx.c:10546
static int unload_module(void)
Definition: app_privacy.c:215
int ast_waitstream(struct ast_channel *c, const char *breakon)
Waits for a stream to stop or digit to be pressed.
Definition: file.c:1343
int ast_answer(struct ast_channel *chan)
Answer a channel.
Definition: channel.c:3086
Application convenience functions, designed to give consistent look and feel to Asterisk apps...
const char * data
Description of a tone.
Definition: indications.h:53
#define AST_APP_ARG(name)
Define an application argument.
Definition: app.h:555
#define AST_STANDARD_APP_ARGS(args, parse)
Performs the &#39;standard&#39; argument separation process for an application.
Definition: app.h:604
int ast_readstring(struct ast_channel *c, char *s, int len, int timeout, int rtimeout, char *enders)
Reads multiple digits.
Definition: channel.c:5837
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:38
Asterisk module definitions.
unsigned char valid
TRUE if the number information is valid/present.
Definition: channel.h:247
const ast_string_field language
Definition: channel.h:787
#define ast_register_application_xml(app, execute)
Register an application using XML documentation.
Definition: module.h:437
#define ASTERISK_FILE_VERSION(file, version)
Register/unregister a source code file with the core.
Definition: asterisk.h:180
struct ast_party_number number
Subscriber phone number.
Definition: channel.h:292