Trivial application to read a variable. More...
#include "asterisk.h"
#include "asterisk/file.h"
#include "asterisk/pbx.h"
#include "asterisk/channel.h"
#include "asterisk/app.h"
#include "asterisk/module.h"
#include "asterisk/indications.h"
Go to the source code of this file.
Enumerations | |
enum | read_option_flags { OPT_SKIP = (1 << 0), OPT_INDICATION = (1 << 1), OPT_NOANSWER = (1 << 2) } |
Functions | |
AST_APP_OPTIONS (read_app_options,{AST_APP_OPTION('s', OPT_SKIP), AST_APP_OPTION('i', OPT_INDICATION), AST_APP_OPTION('n', OPT_NOANSWER),}) | |
AST_LIST_HEAD (readmaskvars, ast_variable_list) | |
AST_MODULE_INFO_STANDARD (ASTERISK_GPL_KEY,"Read Variable Application") | |
static int | load_module (void) |
static int | unload_module (void) |
Variables | |
static char * | app = "Read" |
Trivial application to read a variable.
Definition in file app_read.c.
enum read_option_flags |
Definition at line 115 of file app_read.c.
00115 { 00116 OPT_SKIP = (1 << 0), 00117 OPT_INDICATION = (1 << 1), 00118 OPT_NOANSWER = (1 << 2), 00119 };
AST_APP_OPTIONS | ( | read_app_options | ) |
AST_LIST_HEAD | ( | readmaskvars, | ||
ast_variable_list | ||||
) |
Definition at line 129 of file app_read.c.
References ast_answer(), AST_APP_ARG, ast_app_getdata(), ast_app_parse_options(), ast_check_hangup(), AST_DECLARE_APP_ARGS, ast_get_indication_tone(), AST_GETDATA_COMPLETE, AST_GETDATA_EMPTY_END_TERMINATED, AST_GETDATA_INTERRUPTED, AST_GETDATA_TIMEOUT, AST_LIST_TRAVERSE, ast_log(), ast_playtones_start(), ast_playtones_stop(), AST_STANDARD_APP_ARGS, AST_STATE_UP, ast_stopstream(), ast_strdupa, ast_strlen_zero(), ast_test_flag, ast_tone_zone_sound_unref(), ast_verb, ast_waitfordigit(), ast_tone_zone_sound::data, ast_variable_list::list, LOG_WARNING, mask_string(), OPT_INDICATION, OPT_NOANSWER, OPT_SKIP, pbx_builtin_setvar_helper(), readmaskvarend, readmaskvarini, status, ast_variable_list::variable, and VERBOSITY_ATLEAST.
00132 { 00133 int res = 0; 00134 char tmp[256] = ""; 00135 char *digits; 00136 int maxdigits = 255; 00137 int tries = 1, to = 0, x = 0; 00138 double tosec; 00139 char *argcopy = NULL; 00140 struct ast_tone_zone_sound *ts = NULL; 00141 struct ast_flags flags = {0}; 00142 const char *status = "ERROR"; 00143 struct ast_variable_list *cur; 00144 00145 AST_DECLARE_APP_ARGS(arglist, 00146 AST_APP_ARG(variable); 00147 AST_APP_ARG(filename); 00148 AST_APP_ARG(maxdigits); 00149 AST_APP_ARG(options); 00150 AST_APP_ARG(attempts); 00151 AST_APP_ARG(timeout); 00152 ); 00153 00154 pbx_builtin_setvar_helper(chan, "READSTATUS", status); 00155 if (ast_strlen_zero(data)) { 00156 ast_log(LOG_WARNING, "Read requires an argument (variable)\n"); 00157 return 0; 00158 } 00159 00160 argcopy = ast_strdupa(data); 00161 00162 AST_STANDARD_APP_ARGS(arglist, argcopy); 00163 00164 if (!ast_strlen_zero(arglist.options)) { 00165 ast_app_parse_options(read_app_options, &flags, NULL, arglist.options); 00166 } 00167 00168 if (!ast_strlen_zero(arglist.attempts)) { 00169 tries = atoi(arglist.attempts); 00170 if (tries <= 0) 00171 tries = 1; 00172 } 00173 00174 if (!ast_strlen_zero(arglist.timeout)) { 00175 tosec = atof(arglist.timeout); 00176 if (tosec <= 0) 00177 to = 0; 00178 else 00179 to = tosec * 1000.0; 00180 } 00181 00182 if (ast_strlen_zero(arglist.filename)) { 00183 arglist.filename = NULL; 00184 } 00185 if (!ast_strlen_zero(arglist.maxdigits)) { 00186 maxdigits = atoi(arglist.maxdigits); 00187 if ((maxdigits < 1) || (maxdigits > 255)) { 00188 maxdigits = 255; 00189 } else 00190 ast_verb(3, "Accepting a maximum of %d digits.\n", maxdigits); 00191 } 00192 if (ast_strlen_zero(arglist.variable)) { 00193 ast_log(LOG_WARNING, "Invalid! Usage: Read(variable[,filename][,maxdigits][,option][,attempts][,timeout])\n\n"); 00194 return 0; 00195 } 00196 if (ast_test_flag(&flags, OPT_INDICATION)) { 00197 if (!ast_strlen_zero(arglist.filename)) { 00198 ts = ast_get_indication_tone(chan->zone, arglist.filename); 00199 } 00200 } 00201 if (chan->_state != AST_STATE_UP) { 00202 if (ast_test_flag(&flags, OPT_SKIP)) { 00203 /* At the user's option, skip if the line is not up */ 00204 pbx_builtin_setvar_helper(chan, arglist.variable, ""); 00205 pbx_builtin_setvar_helper(chan, "READSTATUS", "SKIPPED"); 00206 return 0; 00207 } else if (!ast_test_flag(&flags, OPT_NOANSWER)) { 00208 /* Otherwise answer unless we're supposed to read while on-hook */ 00209 res = ast_answer(chan); 00210 } 00211 } 00212 if (!res) { 00213 while (tries && !res) { 00214 ast_stopstream(chan); 00215 if (ts && ts->data[0]) { 00216 if (!to) 00217 to = chan->pbx ? chan->pbx->rtimeoutms : 6000; 00218 res = ast_playtones_start(chan, 0, ts->data, 0); 00219 for (x = 0; x < maxdigits; ) { 00220 res = ast_waitfordigit(chan, to); 00221 ast_playtones_stop(chan); 00222 if (res < 1) { 00223 if (res == 0) 00224 status = "TIMEOUT"; 00225 tmp[x]='\0'; 00226 break; 00227 } 00228 tmp[x++] = res; 00229 if (tmp[x-1] == '#') { 00230 tmp[x-1] = '\0'; 00231 status = "OK"; 00232 break; 00233 } 00234 if (x >= maxdigits) { 00235 status = "OK"; 00236 } 00237 } 00238 } else { 00239 res = ast_app_getdata(chan, arglist.filename, tmp, maxdigits, to); 00240 if (res == AST_GETDATA_COMPLETE || res == AST_GETDATA_EMPTY_END_TERMINATED) 00241 status = "OK"; 00242 else if (res == AST_GETDATA_TIMEOUT) 00243 status = "TIMEOUT"; 00244 else if (res == AST_GETDATA_INTERRUPTED) 00245 status = "INTERRUPTED"; 00246 } 00247 if (res > -1) { 00248 pbx_builtin_setvar_helper(chan, arglist.variable, tmp); 00249 if (!ast_strlen_zero(tmp)) { 00250 if (VERBOSITY_ATLEAST(3)) { 00251 digits = ast_strdupa(tmp); 00252 AST_LIST_TRAVERSE(&readmaskvars, cur, list) { 00253 if (!strncmp(arglist.variable, cur->variable, sizeof(cur->variable))) { 00254 mask_string(digits, readmaskvarini, readmaskvarend, 0); 00255 break; 00256 } 00257 } 00258 ast_verb(3, "User entered '%s'\n", digits); 00259 } 00260 tries = 0; 00261 } else { 00262 tries--; 00263 if (tries) 00264 ast_verb(3, "User entered nothing, %d chance%s left\n", tries, (tries != 1) ? "s" : ""); 00265 else 00266 ast_verb(3, "User entered nothing.\n"); 00267 } 00268 res = 0; 00269 } else { 00270 pbx_builtin_setvar_helper(chan, arglist.variable, tmp); 00271 ast_verb(3, "User disconnected\n"); 00272 } 00273 } 00274 } 00275 00276 if (ts) { 00277 ts = ast_tone_zone_sound_unref(ts); 00278 } 00279 00280 if (ast_check_hangup(chan)) 00281 status = "HANGUP"; 00282 pbx_builtin_setvar_helper(chan, "READSTATUS", status); 00283 return 0; 00284 }
AST_MODULE_INFO_STANDARD | ( | ASTERISK_GPL_KEY | , | |
"Read Variable Application" | ||||
) |
static int load_module | ( | void | ) | [static] |
Definition at line 291 of file app_read.c.
References ast_register_application_xml.
00292 { 00293 return ast_register_application_xml(app, read_exec); 00294 }
static int unload_module | ( | void | ) | [static] |
Definition at line 286 of file app_read.c.
References ast_unregister_application().
00287 { 00288 return ast_unregister_application(app); 00289 }
char* app = "Read" [static] |
Definition at line 127 of file app_read.c.