00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include "asterisk.h"
00029
00030 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 168561 $")
00031
00032 #include <stdlib.h>
00033 #include <stdio.h>
00034 #include <string.h>
00035
00036 #include "asterisk/lock.h"
00037 #include "asterisk/file.h"
00038 #include "asterisk/logger.h"
00039 #include "asterisk/channel.h"
00040 #include "asterisk/pbx.h"
00041 #include "asterisk/app.h"
00042 #include "asterisk/module.h"
00043 #include "asterisk/translate.h"
00044 #include "asterisk/options.h"
00045 #include "asterisk/utils.h"
00046 #include "asterisk/indications.h"
00047
00048 enum {
00049 OPT_SKIP = (1 << 0),
00050 OPT_INDICATION = (1 << 1),
00051 OPT_NOANSWER = (1 << 2),
00052 } read_option_flags;
00053
00054 AST_APP_OPTIONS(read_app_options, {
00055 AST_APP_OPTION('s', OPT_SKIP),
00056 AST_APP_OPTION('i', OPT_INDICATION),
00057 AST_APP_OPTION('n', OPT_NOANSWER),
00058 });
00059
00060 static char *app = "Read";
00061
00062 static char *synopsis = "Read a variable";
00063
00064 static char *descrip =
00065 " Read(variable[|filename][|maxdigits][|option][|attempts][|timeout])\n\n"
00066 "Reads a #-terminated string of digits a certain number of times from the\n"
00067 "user in to the given variable.\n"
00068 " filename -- file to play before reading digits or tone with option i\n"
00069 " maxdigits -- maximum acceptable number of digits. Stops reading after\n"
00070 " maxdigits have been entered (without requiring the user to\n"
00071 " press the '#' key).\n"
00072 " Defaults to 0 - no limit - wait for the user press the '#' key.\n"
00073 " Any value below 0 means the same. Max accepted value is 255.\n"
00074 " option -- options are 's' , 'i', 'n'\n"
00075 " 's' to return immediately if the line is not up,\n"
00076 " 'i' to play filename as an indication tone from your indications.conf\n"
00077 " 'n' to read digits even if the line is not up.\n"
00078 " attempts -- if greater than 1, that many attempts will be made in the \n"
00079 " event no data is entered.\n"
00080 " timeout -- An integer number of seconds to wait for a digit response. If greater\n"
00081 " than 0, that value will override the default timeout.\n\n"
00082 "Read should disconnect if the function fails or errors out.\n"
00083 "This application sets the following channel variable upon completion:\n"
00084 " READSTATUS The status of the READ attempt as a text string, one of\n"
00085 " SUCCESS Collected maxdigits, or string terminated with # (can be empty string)\n"
00086 " TIMEOUT Collected digits (if any) ended by timeout\n";
00087
00088
00089 #define ast_next_data(instr,ptr,delim) if((ptr=strchr(instr,delim))) { *(ptr) = '\0' ; ptr++;}
00090
00091 static int read_exec(struct ast_channel *chan, void *data)
00092 {
00093 int res = 0;
00094 struct ast_module_user *u;
00095 char tmp[256] = "";
00096 int maxdigits = 255;
00097 int tries = 1, to = 0, x = 0;
00098 char *argcopy = NULL;
00099 struct tone_zone_sound *ts;
00100 struct ast_flags flags = {0};
00101
00102 AST_DECLARE_APP_ARGS(arglist,
00103 AST_APP_ARG(variable);
00104 AST_APP_ARG(filename);
00105 AST_APP_ARG(maxdigits);
00106 AST_APP_ARG(options);
00107 AST_APP_ARG(attempts);
00108 AST_APP_ARG(timeout);
00109 );
00110
00111 if (ast_strlen_zero(data)) {
00112 ast_log(LOG_WARNING, "Read requires an argument (variable)\n");
00113 return -1;
00114 }
00115
00116 u = ast_module_user_add(chan);
00117
00118 argcopy = ast_strdupa(data);
00119
00120 AST_STANDARD_APP_ARGS(arglist, argcopy);
00121
00122 if (!ast_strlen_zero(arglist.options)) {
00123 ast_app_parse_options(read_app_options, &flags, NULL, arglist.options);
00124 }
00125
00126 if (!ast_strlen_zero(arglist.attempts)) {
00127 tries = atoi(arglist.attempts);
00128 if (tries <= 0)
00129 tries = 1;
00130 }
00131
00132 if (!ast_strlen_zero(arglist.timeout)) {
00133 to = atoi(arglist.timeout);
00134 if (to <= 0)
00135 to = 0;
00136 else
00137 to *= 1000;
00138 }
00139
00140 if (ast_strlen_zero(arglist.filename)) {
00141 arglist.filename = NULL;
00142 }
00143 if (!ast_strlen_zero(arglist.maxdigits)) {
00144 maxdigits = atoi(arglist.maxdigits);
00145 if ((maxdigits<1) || (maxdigits>255)) {
00146 maxdigits = 255;
00147 } else if (option_verbose > 2)
00148 ast_verbose(VERBOSE_PREFIX_3 "Accepting a maximum of %d digits.\n", maxdigits);
00149 }
00150 if (ast_strlen_zero(arglist.variable)) {
00151 ast_log(LOG_WARNING, "Invalid! Usage: Read(variable[|filename][|maxdigits][|option][|attempts][|timeout])\n\n");
00152 ast_module_user_remove(u);
00153 return -1;
00154 }
00155 ts=NULL;
00156 if (ast_test_flag(&flags,OPT_INDICATION)) {
00157 if (!ast_strlen_zero(arglist.filename)) {
00158 ts = ast_get_indication_tone(chan->zone,arglist.filename);
00159 }
00160 }
00161 if (chan->_state != AST_STATE_UP) {
00162 if (ast_test_flag(&flags,OPT_SKIP)) {
00163
00164 pbx_builtin_setvar_helper(chan, arglist.variable, "\0");
00165 ast_module_user_remove(u);
00166 return 0;
00167 } else if (!ast_test_flag(&flags,OPT_NOANSWER)) {
00168
00169 res = ast_answer(chan);
00170 }
00171 }
00172 if (!res) {
00173 pbx_builtin_setvar_helper(chan, "READSTATUS", "SUCCESS");
00174 while (tries && !res) {
00175 ast_stopstream(chan);
00176 if (ts && ts->data[0]) {
00177 if (!to)
00178 to = chan->pbx ? chan->pbx->rtimeout * 1000 : 6000;
00179 res = ast_playtones_start(chan, 0, ts->data, 0);
00180 for (x = 0; x < maxdigits; ) {
00181 res = ast_waitfordigit(chan, to);
00182 ast_playtones_stop(chan);
00183 if (res < 1) {
00184 tmp[x]='\0';
00185 pbx_builtin_setvar_helper(chan, "READSTATUS", "TIMEOUT");
00186 break;
00187 }
00188 tmp[x++] = res;
00189 if (tmp[x-1] == '#') {
00190 tmp[x-1] = '\0';
00191 break;
00192 }
00193 }
00194 } else {
00195 res = ast_app_getdata(chan, arglist.filename, tmp, maxdigits, to);
00196
00197 if (res == 1)
00198 pbx_builtin_setvar_helper(chan, "READSTATUS", "TIMEOUT");
00199 }
00200 if (res > -1) {
00201 pbx_builtin_setvar_helper(chan, arglist.variable, tmp);
00202 if (!ast_strlen_zero(tmp)) {
00203 if (option_verbose > 2)
00204 ast_verbose(VERBOSE_PREFIX_3 "User entered '%s'\n", tmp);
00205 tries = 0;
00206 } else {
00207 tries--;
00208 if (option_verbose > 2) {
00209 if (tries)
00210 ast_verbose(VERBOSE_PREFIX_3 "User entered nothing, %d chance%s left\n", tries, (tries != 1) ? "s" : "");
00211 else
00212 ast_verbose(VERBOSE_PREFIX_3 "User entered nothing.\n");
00213 }
00214 }
00215 res = 0;
00216 } else {
00217 pbx_builtin_setvar_helper(chan, arglist.variable, tmp);
00218 if (option_verbose > 2)
00219 ast_verbose(VERBOSE_PREFIX_3 "User disconnected\n");
00220 }
00221 }
00222 }
00223 ast_module_user_remove(u);
00224 return res;
00225 }
00226
00227 static int unload_module(void)
00228 {
00229 int res;
00230
00231 res = ast_unregister_application(app);
00232
00233 ast_module_user_hangup_all();
00234
00235 return res;
00236 }
00237
00238 static int load_module(void)
00239 {
00240 return ast_register_application(app, read_exec, synopsis, descrip);
00241 }
00242
00243 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Read Variable Application");