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: 180080 $")
00031
00032 #include "asterisk/file.h"
00033 #include "asterisk/pbx.h"
00034 #include "asterisk/channel.h"
00035 #include "asterisk/app.h"
00036 #include "asterisk/module.h"
00037 #include "asterisk/indications.h"
00038
00039 enum {
00040 OPT_SKIP = (1 << 0),
00041 OPT_INDICATION = (1 << 1),
00042 OPT_NOANSWER = (1 << 2),
00043 } read_option_flags;
00044
00045 AST_APP_OPTIONS(read_app_options, {
00046 AST_APP_OPTION('s', OPT_SKIP),
00047 AST_APP_OPTION('i', OPT_INDICATION),
00048 AST_APP_OPTION('n', OPT_NOANSWER),
00049 });
00050
00051 static char *app = "Read";
00052
00053 static char *synopsis = "Read a variable";
00054
00055 static char *descrip =
00056 " Read(variable[,filename[&filename2...]][,maxdigits][,option][,attempts][,timeout])\n\n"
00057 "Reads a #-terminated string of digits a certain number of times from the\n"
00058 "user in to the given variable.\n"
00059 " filename -- file(s) to play before reading digits or tone with option i\n"
00060 " maxdigits -- maximum acceptable number of digits. Stops reading after\n"
00061 " maxdigits have been entered (without requiring the user to\n"
00062 " press the '#' key).\n"
00063 " Defaults to 0 - no limit - wait for the user press the '#' key.\n"
00064 " Any value below 0 means the same. Max accepted value is 255.\n"
00065 " option -- options are 's' , 'i', 'n'\n"
00066 " 's' to return immediately if the line is not up,\n"
00067 " 'i' to play filename as an indication tone from your indications.conf\n"
00068 " 'n' to read digits even if the line is not up.\n"
00069 " attempts -- if greater than 1, that many attempts will be made in the \n"
00070 " event no data is entered.\n"
00071 " timeout -- The number of seconds to wait for a digit response. If greater\n"
00072 " than 0, that value will override the default timeout. Can be floating point.\n"
00073 "This application sets the following channel variable upon completion:\n"
00074 " READSTATUS - This is the status of the read operation.\n"
00075 " Possible values are:\n"
00076 " OK | ERROR | HANGUP | INTERRUPTED | SKIPPED | TIMEOUT\n";
00077
00078
00079 #define ast_next_data(instr,ptr,delim) if((ptr=strchr(instr,delim))) { *(ptr) = '\0' ; ptr++;}
00080
00081 static int read_exec(struct ast_channel *chan, void *data)
00082 {
00083 int res = 0;
00084 char tmp[256] = "";
00085 int maxdigits = 255;
00086 int tries = 1, to = 0, x = 0;
00087 double tosec;
00088 char *argcopy = NULL;
00089 struct tone_zone_sound *ts = NULL;
00090 struct ast_flags flags = {0};
00091 const char *status = "ERROR";
00092
00093 AST_DECLARE_APP_ARGS(arglist,
00094 AST_APP_ARG(variable);
00095 AST_APP_ARG(filename);
00096 AST_APP_ARG(maxdigits);
00097 AST_APP_ARG(options);
00098 AST_APP_ARG(attempts);
00099 AST_APP_ARG(timeout);
00100 );
00101
00102 pbx_builtin_setvar_helper(chan, "READSTATUS", status);
00103 if (ast_strlen_zero(data)) {
00104 ast_log(LOG_WARNING, "Read requires an argument (variable)\n");
00105 return 0;
00106 }
00107
00108 argcopy = ast_strdupa(data);
00109
00110 AST_STANDARD_APP_ARGS(arglist, argcopy);
00111
00112 if (!ast_strlen_zero(arglist.options)) {
00113 ast_app_parse_options(read_app_options, &flags, NULL, arglist.options);
00114 }
00115
00116 if (!ast_strlen_zero(arglist.attempts)) {
00117 tries = atoi(arglist.attempts);
00118 if (tries <= 0)
00119 tries = 1;
00120 }
00121
00122 if (!ast_strlen_zero(arglist.timeout)) {
00123 tosec = atof(arglist.timeout);
00124 if (tosec <= 0)
00125 to = 0;
00126 else
00127 to = tosec * 1000.0;
00128 }
00129
00130 if (ast_strlen_zero(arglist.filename)) {
00131 arglist.filename = NULL;
00132 }
00133 if (!ast_strlen_zero(arglist.maxdigits)) {
00134 maxdigits = atoi(arglist.maxdigits);
00135 if ((maxdigits < 1) || (maxdigits > 255)) {
00136 maxdigits = 255;
00137 } else
00138 ast_verb(3, "Accepting a maximum of %d digits.\n", maxdigits);
00139 }
00140 if (ast_strlen_zero(arglist.variable)) {
00141 ast_log(LOG_WARNING, "Invalid! Usage: Read(variable[,filename][,maxdigits][,option][,attempts][,timeout])\n\n");
00142 return 0;
00143 }
00144 if (ast_test_flag(&flags, OPT_INDICATION)) {
00145 if (! ast_strlen_zero(arglist.filename)) {
00146 ts = ast_get_indication_tone(chan->zone, arglist.filename);
00147 }
00148 }
00149 if (chan->_state != AST_STATE_UP) {
00150 if (ast_test_flag(&flags, OPT_SKIP)) {
00151
00152 pbx_builtin_setvar_helper(chan, arglist.variable, "");
00153 pbx_builtin_setvar_helper(chan, "READSTATUS", "SKIPPED");
00154 return 0;
00155 } else if (!ast_test_flag(&flags, OPT_NOANSWER)) {
00156
00157 res = ast_answer(chan);
00158 }
00159 }
00160 if (!res) {
00161 while (tries && !res) {
00162 ast_stopstream(chan);
00163 if (ts && ts->data[0]) {
00164 if (!to)
00165 to = chan->pbx ? chan->pbx->rtimeoutms : 6000;
00166 res = ast_playtones_start(chan, 0, ts->data, 0);
00167 for (x = 0; x < maxdigits; ) {
00168 res = ast_waitfordigit(chan, to);
00169 ast_playtones_stop(chan);
00170 if (res < 1) {
00171 if (res == 0)
00172 status = "TIMEOUT";
00173 tmp[x]='\0';
00174 break;
00175 }
00176 tmp[x++] = res;
00177 if (tmp[x-1] == '#') {
00178 tmp[x-1] = '\0';
00179 status = "OK";
00180 break;
00181 }
00182 if (x >= maxdigits) {
00183 status = "OK";
00184 }
00185 }
00186 } else {
00187 res = ast_app_getdata(chan, arglist.filename, tmp, maxdigits, to);
00188 if (res == AST_GETDATA_COMPLETE || res == AST_GETDATA_EMPTY_END_TERMINATED)
00189 status = "OK";
00190 else if (res == AST_GETDATA_TIMEOUT)
00191 status = "TIMEOUT";
00192 else if (res == AST_GETDATA_INTERRUPTED)
00193 status = "INTERRUPTED";
00194 }
00195 if (res > -1) {
00196 pbx_builtin_setvar_helper(chan, arglist.variable, tmp);
00197 if (!ast_strlen_zero(tmp)) {
00198 ast_verb(3, "User entered '%s'\n", tmp);
00199 tries = 0;
00200 } else {
00201 tries--;
00202 if (tries)
00203 ast_verb(3, "User entered nothing, %d chance%s left\n", tries, (tries != 1) ? "s" : "");
00204 else
00205 ast_verb(3, "User entered nothing.\n");
00206 }
00207 res = 0;
00208 } else {
00209 pbx_builtin_setvar_helper(chan, arglist.variable, tmp);
00210 ast_verb(3, "User disconnected\n");
00211 }
00212 }
00213 }
00214
00215 if (ast_check_hangup(chan))
00216 status = "HANGUP";
00217 pbx_builtin_setvar_helper(chan, "READSTATUS", status);
00218 return 0;
00219 }
00220
00221 static int unload_module(void)
00222 {
00223 return ast_unregister_application(app);
00224 }
00225
00226 static int load_module(void)
00227 {
00228 return ast_register_application(app, read_exec, synopsis, descrip);
00229 }
00230
00231 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Read Variable Application");