Fri Jul 24 00:40:39 2009

Asterisk developer's documentation


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 #include "asterisk.h"
00029 
00030 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 121197 $")
00031 
00032 #include "asterisk/lock.h"
00033 #include "asterisk/file.h"
00034 #include "asterisk/utils.h"
00035 #include "asterisk/channel.h"
00036 #include "asterisk/pbx.h"
00037 #include "asterisk/module.h"
00038 #include "asterisk/translate.h"
00039 #include "asterisk/image.h"
00040 #include "asterisk/callerid.h"
00041 #include "asterisk/app.h"
00042 #include "asterisk/config.h"
00043 
00044 static char *app = "PrivacyManager";
00045 
00046 static char *synopsis = "Require phone number to be entered, if no CallerID sent";
00047 
00048 static char *descrip =
00049   "  PrivacyManager([maxretries][,minlength][,context]): If no Caller*ID \n"
00050   "is sent, PrivacyManager answers the channel and asks the caller to\n"
00051   "enter their phone number. The caller is given 'maxretries' attempts to do so.\n"
00052   "The application does nothing if Caller*ID was received on the channel.\n"
00053   "   maxretries  default 3  -maximum number of attempts the caller is allowed \n"
00054   "               to input a callerid.\n"
00055   "   minlength   default 10 -minimum allowable digits in the input callerid number.\n"
00056   "   context     context to check the given Caller*ID against patterns.\n"
00057   "The application sets the following channel variable upon completion: \n"
00058   "PRIVACYMGRSTATUS  The status of the privacy manager's attempt to collect \n"
00059   "                  a phone number from the user. A text string that is either:\n" 
00060   "          SUCCESS | FAILED \n"
00061 ;
00062 
00063 
00064 static int privacy_exec (struct ast_channel *chan, void *data)
00065 {
00066    int res=0;
00067    int retries;
00068    int maxretries = 3;
00069    int minlength = 10;
00070    int x = 0;
00071    char phone[30];
00072    char *parse = NULL;
00073    AST_DECLARE_APP_ARGS(args,
00074       AST_APP_ARG(maxretries);
00075       AST_APP_ARG(minlength);
00076       AST_APP_ARG(options);
00077       AST_APP_ARG(checkcontext);
00078    );
00079 
00080    if (!ast_strlen_zero(chan->cid.cid_num)) {
00081       ast_verb(3, "CallerID Present: Skipping\n");
00082    } else {
00083       /*Answer the channel if it is not already*/
00084       if (chan->_state != AST_STATE_UP) {
00085          if ((res = ast_answer(chan)))
00086             return -1;
00087       }
00088 
00089       if (!ast_strlen_zero(data)) {
00090          parse = ast_strdupa(data);
00091          
00092          AST_STANDARD_APP_ARGS(args, parse);
00093 
00094          if (args.maxretries) {
00095             if (sscanf(args.maxretries, "%d", &x) == 1)
00096                maxretries = x;
00097             else
00098                ast_log(LOG_WARNING, "Invalid max retries argument\n");
00099          }
00100          if (args.minlength) {
00101             if (sscanf(args.minlength, "%d", &x) == 1)
00102                minlength = x;
00103             else
00104                ast_log(LOG_WARNING, "Invalid min length argument\n");
00105          }
00106       }     
00107 
00108       /* Play unidentified call */
00109       res = ast_safe_sleep(chan, 1000);
00110       if (!res)
00111          res = ast_streamfile(chan, "privacy-unident", chan->language);
00112       if (!res)
00113          res = ast_waitstream(chan, "");
00114 
00115       /* Ask for 10 digit number, give 3 attempts */
00116       for (retries = 0; retries < maxretries; retries++) {
00117          if (!res)
00118             res = ast_streamfile(chan, "privacy-prompt", chan->language);
00119          if (!res)
00120             res = ast_waitstream(chan, "");
00121 
00122          if (!res ) 
00123             res = ast_readstring(chan, phone, sizeof(phone) - 1, /* digit timeout ms */ 3200, /* first digit timeout */ 5000, "#");
00124 
00125          if (res < 0)
00126             break;
00127 
00128          /* Make sure we get at least digits */
00129          if (strlen(phone) >= minlength ) {
00130             /* if we have a checkcontext argument, do pattern matching */
00131             if (!ast_strlen_zero(args.checkcontext)) {
00132                if (!ast_exists_extension(NULL, args.checkcontext, phone, 1, NULL)) {
00133                   res = ast_streamfile(chan, "privacy-incorrect", chan->language);
00134                   if (!res) {
00135                      res = ast_waitstream(chan, "");
00136                   }
00137                } else {
00138                   break;
00139                }
00140             } else {
00141                break;
00142             }
00143          } else {
00144             res = ast_streamfile(chan, "privacy-incorrect", chan->language);
00145             if (!res)
00146                res = ast_waitstream(chan, "");
00147          }
00148       }
00149       
00150       /* Got a number, play sounds and send them on their way */
00151       if ((retries < maxretries) && res >= 0 ) {
00152          res = ast_streamfile(chan, "privacy-thankyou", chan->language);
00153          if (!res)
00154             res = ast_waitstream(chan, "");
00155 
00156          ast_set_callerid (chan, phone, "Privacy Manager", NULL); 
00157 
00158          /* Clear the unavailable presence bit so if it came in on PRI
00159           * the caller id will now be passed out to other channels
00160           */
00161          chan->cid.cid_pres &= (AST_PRES_UNAVAILABLE ^ 0xFF);
00162 
00163          ast_verb(3, "Changed Caller*ID to %s, callerpres to %d\n",phone,chan->cid.cid_pres);
00164 
00165          pbx_builtin_setvar_helper(chan, "PRIVACYMGRSTATUS", "SUCCESS");
00166       } else {
00167          pbx_builtin_setvar_helper(chan, "PRIVACYMGRSTATUS", "FAILED");
00168       }
00169    }
00170 
00171    return 0;
00172 }
00173 
00174 static int unload_module(void)
00175 {
00176    return ast_unregister_application (app);
00177 }
00178 
00179 static int load_module(void)
00180 {
00181    return ast_register_application(app, privacy_exec, synopsis, descrip);
00182 }
00183 
00184 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Require phone number to be entered, if no CallerID sent");

Generated on Fri Jul 24 00:40:39 2009 for Asterisk - the Open Source PBX by  doxygen 1.4.7