app_privacy.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 1999 - 2005, Digium, Inc.
00005  *
00006  * Mark Spencer <markster@digium.com>
00007  *
00008  * See http://www.asterisk.org for more information about
00009  * the Asterisk project. Please do not directly contact
00010  * any of the maintainers of this project for assistance;
00011  * the project provides a web site, mailing lists and IRC
00012  * channels for your use.
00013  *
00014  * This program is free software, distributed under the terms of
00015  * the GNU General Public License Version 2. See the LICENSE file
00016  * at the top of the source tree.
00017  */
00018 
00019 /*! \file
00020  *
00021  * \brief Block all calls without Caller*ID, require phone # to be entered
00022  *
00023  * \author Mark Spencer <markster@digium.com>
00024  *
00025  * \ingroup applications
00026  */
00027 
00028 /*** MODULEINFO
00029    <support_level>core</support_level>
00030  ***/
00031 
00032 #include "asterisk.h"
00033 
00034 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 357542 $")
00035 
00036 #include "asterisk/lock.h"
00037 #include "asterisk/file.h"
00038 #include "asterisk/utils.h"
00039 #include "asterisk/channel.h"
00040 #include "asterisk/pbx.h"
00041 #include "asterisk/module.h"
00042 #include "asterisk/translate.h"
00043 #include "asterisk/image.h"
00044 #include "asterisk/callerid.h"
00045 #include "asterisk/app.h"
00046 #include "asterisk/config.h"
00047 
00048 /*** DOCUMENTATION
00049    <application name="PrivacyManager" language="en_US">
00050       <synopsis>
00051          Require phone number to be entered, if no CallerID sent
00052       </synopsis>
00053       <syntax>
00054          <parameter name="maxretries">
00055             <para>Total tries caller is allowed to input a callerid. Defaults to <literal>3</literal>.</para>
00056          </parameter>
00057          <parameter name="minlength">
00058             <para>Minimum allowable digits in the input callerid number. Defaults to <literal>10</literal>.</para>
00059          </parameter>
00060          <parameter name="options">
00061             <para>Position reserved for options.</para>
00062          </parameter>
00063          <parameter name="context">
00064             <para>Context to check the given callerid against patterns.</para>
00065          </parameter>
00066       </syntax>
00067       <description>
00068          <para>If no Caller*ID is sent, PrivacyManager answers the channel and asks
00069          the caller to enter their phone number. The caller is given
00070          <replaceable>maxretries</replaceable> attempts to do so. The application does
00071          <emphasis>nothing</emphasis> if Caller*ID was received on the channel.</para>
00072          <para>The application sets the following channel variable upon completion:</para>
00073          <variablelist>
00074             <variable name="PRIVACYMGRSTATUS">
00075                <para>The status of the privacy manager's attempt to collect a phone number from the user.</para>
00076                <value name="SUCCESS"/>
00077                <value name="FAILED"/>
00078             </variable>
00079          </variablelist>
00080       </description>
00081       <see-also>
00082          <ref type="application">Zapateller</ref>
00083       </see-also>
00084    </application>
00085  ***/
00086 
00087 
00088 static char *app = "PrivacyManager";
00089 
00090 static int privacy_exec(struct ast_channel *chan, const char *data)
00091 {
00092    int res=0;
00093    int retries;
00094    int maxretries = 3;
00095    int minlength = 10;
00096    int x = 0;
00097    char phone[30];
00098    char *parse = NULL;
00099    AST_DECLARE_APP_ARGS(args,
00100       AST_APP_ARG(maxretries);
00101       AST_APP_ARG(minlength);
00102       AST_APP_ARG(options);
00103       AST_APP_ARG(checkcontext);
00104    );
00105 
00106    if (ast_channel_caller(chan)->id.number.valid
00107       && !ast_strlen_zero(ast_channel_caller(chan)->id.number.str)) {
00108       ast_verb(3, "CallerID number present: Skipping\n");
00109    } else {
00110       /*Answer the channel if it is not already*/
00111       if (ast_channel_state(chan) != AST_STATE_UP) {
00112          if ((res = ast_answer(chan))) {
00113             return -1;
00114          }
00115       }
00116 
00117       parse = ast_strdupa(data);
00118 
00119       AST_STANDARD_APP_ARGS(args, parse);
00120 
00121       if (!ast_strlen_zero(args.maxretries)) {
00122          if (sscanf(args.maxretries, "%30d", &x) == 1 && x > 0) {
00123             maxretries = x;
00124          } else {
00125             ast_log(LOG_WARNING, "Invalid max retries argument: '%s'\n", args.maxretries);
00126          }
00127       }
00128       if (!ast_strlen_zero(args.minlength)) {
00129          if (sscanf(args.minlength, "%30d", &x) == 1 && x > 0) {
00130             minlength = x;
00131          } else {
00132             ast_log(LOG_WARNING, "Invalid min length argument: '%s'\n", args.minlength);
00133          }
00134       }
00135 
00136       /* Play unidentified call */
00137       res = ast_safe_sleep(chan, 1000);
00138       if (!res) {
00139          res = ast_streamfile(chan, "privacy-unident", ast_channel_language(chan));
00140       }
00141       if (!res) {
00142          res = ast_waitstream(chan, "");
00143       }
00144 
00145       /* Ask for 10 digit number, give 3 attempts */
00146       for (retries = 0; retries < maxretries; retries++) {
00147          if (!res) {
00148             res = ast_streamfile(chan, "privacy-prompt", ast_channel_language(chan));
00149          }
00150          if (!res) {
00151             res = ast_waitstream(chan, "");
00152          }
00153 
00154          if (!res) {
00155             res = ast_readstring(chan, phone, sizeof(phone) - 1, /* digit timeout ms */ 3200, /* first digit timeout */ 5000, "#");
00156          }
00157 
00158          if (res < 0) {
00159             break;
00160          }
00161 
00162          /* Make sure we get at least digits */
00163          if (strlen(phone) >= minlength ) {
00164             /* if we have a checkcontext argument, do pattern matching */
00165             if (!ast_strlen_zero(args.checkcontext)) {
00166                if (!ast_exists_extension(NULL, args.checkcontext, phone, 1, NULL)) {
00167                   res = ast_streamfile(chan, "privacy-incorrect", ast_channel_language(chan));
00168                   if (!res) {
00169                      res = ast_waitstream(chan, "");
00170                   }
00171                } else {
00172                   break;
00173                }
00174             } else {
00175                break;
00176             }
00177          } else {
00178             res = ast_streamfile(chan, "privacy-incorrect", ast_channel_language(chan));
00179             if (!res) {
00180                res = ast_waitstream(chan, "");
00181             }
00182          }
00183       }
00184 
00185       /* Got a number, play sounds and send them on their way */
00186       if ((retries < maxretries) && res >= 0) {
00187          res = ast_streamfile(chan, "privacy-thankyou", ast_channel_language(chan));
00188          if (!res) {
00189             res = ast_waitstream(chan, "");
00190          }
00191 
00192          /*
00193           * This is a caller entered number that is going to be used locally.
00194           * Therefore, the given number presentation is allowed and should
00195           * be passed out to other channels.  This is the point of the
00196           * privacy application.
00197           */
00198          ast_channel_caller(chan)->id.name.presentation = AST_PRES_ALLOWED_USER_NUMBER_NOT_SCREENED;
00199          ast_channel_caller(chan)->id.number.presentation = AST_PRES_ALLOWED_USER_NUMBER_NOT_SCREENED;
00200          ast_channel_caller(chan)->id.number.plan = 0;/* Unknown */
00201 
00202          ast_set_callerid(chan, phone, "Privacy Manager", NULL);
00203 
00204          ast_verb(3, "Changed Caller*ID number to '%s'\n", phone);
00205 
00206          pbx_builtin_setvar_helper(chan, "PRIVACYMGRSTATUS", "SUCCESS");
00207       } else {
00208          pbx_builtin_setvar_helper(chan, "PRIVACYMGRSTATUS", "FAILED");
00209       }
00210    }
00211 
00212    return 0;
00213 }
00214 
00215 static int unload_module(void)
00216 {
00217    return ast_unregister_application(app);
00218 }
00219 
00220 static int load_module(void)
00221 {
00222    return ast_register_application_xml(app, privacy_exec);
00223 }
00224 
00225 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Require phone number to be entered, if no CallerID sent");

Generated on Thu Apr 16 06:27:11 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6