2003-11-10 04:47:43 +00:00
|
|
|
/*
|
2005-09-14 20:46:50 +00:00
|
|
|
* Asterisk -- An open source telephony toolkit.
|
2003-11-10 04:47:43 +00:00
|
|
|
*
|
2005-09-14 20:46:50 +00:00
|
|
|
* Copyright (C) 1999 - 2005, Digium, Inc.
|
2003-11-10 04:47:43 +00:00
|
|
|
*
|
|
|
|
* Mark Spencer <markster@digium.com>
|
|
|
|
*
|
2005-09-14 20:46:50 +00:00
|
|
|
* See http://www.asterisk.org for more information about
|
|
|
|
* the Asterisk project. Please do not directly contact
|
|
|
|
* any of the maintainers of this project for assistance;
|
|
|
|
* the project provides a web site, mailing lists and IRC
|
|
|
|
* channels for your use.
|
|
|
|
*
|
2003-11-10 04:47:43 +00:00
|
|
|
* This program is free software, distributed under the terms of
|
2005-09-14 20:46:50 +00:00
|
|
|
* the GNU General Public License Version 2. See the LICENSE file
|
|
|
|
* at the top of the source tree.
|
|
|
|
*/
|
|
|
|
|
2005-10-24 20:12:06 +00:00
|
|
|
/*! \file
|
2005-09-14 20:46:50 +00:00
|
|
|
*
|
2005-10-24 20:12:06 +00:00
|
|
|
* \brief Trivial application to read a variable
|
2005-12-30 21:18:06 +00:00
|
|
|
*
|
|
|
|
* \author Mark Spencer <markster@digium.com>
|
2005-09-14 20:46:50 +00:00
|
|
|
*
|
2005-11-06 15:09:47 +00:00
|
|
|
* \ingroup applications
|
2003-11-10 04:47:43 +00:00
|
|
|
*/
|
|
|
|
|
2005-06-06 22:39:32 +00:00
|
|
|
#include "asterisk.h"
|
|
|
|
|
|
|
|
ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
|
|
|
|
|
2006-06-07 18:54:56 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2005-04-21 06:02:45 +00:00
|
|
|
#include "asterisk/lock.h"
|
|
|
|
#include "asterisk/file.h"
|
|
|
|
#include "asterisk/logger.h"
|
|
|
|
#include "asterisk/channel.h"
|
|
|
|
#include "asterisk/pbx.h"
|
|
|
|
#include "asterisk/app.h"
|
|
|
|
#include "asterisk/module.h"
|
|
|
|
#include "asterisk/translate.h"
|
|
|
|
#include "asterisk/options.h"
|
|
|
|
#include "asterisk/utils.h"
|
2006-01-05 19:55:03 +00:00
|
|
|
#include "asterisk/indications.h"
|
|
|
|
|
|
|
|
enum {
|
|
|
|
OPT_SKIP = (1 << 0),
|
|
|
|
OPT_INDICATION = (1 << 1),
|
|
|
|
OPT_NOANSWER = (1 << 2),
|
|
|
|
} read_option_flags;
|
|
|
|
|
|
|
|
AST_APP_OPTIONS(read_app_options, {
|
|
|
|
AST_APP_OPTION('s', OPT_SKIP),
|
|
|
|
AST_APP_OPTION('i', OPT_INDICATION),
|
|
|
|
AST_APP_OPTION('n', OPT_NOANSWER),
|
|
|
|
});
|
2003-11-10 04:47:43 +00:00
|
|
|
|
|
|
|
static char *app = "Read";
|
|
|
|
|
|
|
|
static char *synopsis = "Read a variable";
|
|
|
|
|
|
|
|
static char *descrip =
|
2004-12-29 13:06:53 +00:00
|
|
|
" Read(variable[|filename][|maxdigits][|option][|attempts][|timeout])\n\n"
|
2004-12-29 13:42:14 +00:00
|
|
|
"Reads a #-terminated string of digits a certain number of times from the\n"
|
|
|
|
"user in to the given variable.\n"
|
2006-01-05 19:55:03 +00:00
|
|
|
" filename -- file to play before reading digits or tone with option i\n"
|
2004-06-30 16:04:28 +00:00
|
|
|
" maxdigits -- maximum acceptable number of digits. Stops reading after\n"
|
|
|
|
" maxdigits have been entered (without requiring the user to\n"
|
|
|
|
" press the '#' key).\n"
|
|
|
|
" Defaults to 0 - no limit - wait for the user press the '#' key.\n"
|
|
|
|
" Any value below 0 means the same. Max accepted value is 255.\n"
|
2006-01-05 19:55:03 +00:00
|
|
|
" option -- options are 's' , 'i', 'n'\n"
|
|
|
|
" 's' to return immediately if the line is not up,\n"
|
|
|
|
" 'i' to play filename as an indication tone from your indications.conf\n"
|
|
|
|
" 'n' to read digits even if the line is not up.\n"
|
2004-12-29 13:42:14 +00:00
|
|
|
" attempts -- if greater than 1, that many attempts will be made in the \n"
|
|
|
|
" event no data is entered.\n"
|
2006-10-05 01:40:06 +00:00
|
|
|
" timeout -- The number of seconds to wait for a digit response. If greater\n"
|
|
|
|
" than 0, that value will override the default timeout. Can be floating point.\n\n"
|
2005-11-07 22:01:22 +00:00
|
|
|
"Read should disconnect if the function fails or errors out.\n";
|
2003-11-10 04:47:43 +00:00
|
|
|
|
|
|
|
|
2004-12-29 13:06:53 +00:00
|
|
|
#define ast_next_data(instr,ptr,delim) if((ptr=strchr(instr,delim))) { *(ptr) = '\0' ; ptr++;}
|
|
|
|
|
2003-11-10 04:47:43 +00:00
|
|
|
static int read_exec(struct ast_channel *chan, void *data)
|
|
|
|
{
|
|
|
|
int res = 0;
|
2006-08-21 02:11:39 +00:00
|
|
|
struct ast_module_user *u;
|
2006-09-07 18:54:36 +00:00
|
|
|
char tmp[256] = "";
|
|
|
|
int maxdigits = 255;
|
|
|
|
int tries = 1, to = 0, x = 0;
|
2006-10-05 01:40:06 +00:00
|
|
|
double tosec;
|
2004-12-29 18:51:33 +00:00
|
|
|
char *argcopy = NULL;
|
2006-01-05 19:55:03 +00:00
|
|
|
struct tone_zone_sound *ts;
|
|
|
|
struct ast_flags flags = {0};
|
2004-12-29 13:06:53 +00:00
|
|
|
|
2006-01-05 19:55:03 +00:00
|
|
|
AST_DECLARE_APP_ARGS(arglist,
|
|
|
|
AST_APP_ARG(variable);
|
|
|
|
AST_APP_ARG(filename);
|
|
|
|
AST_APP_ARG(maxdigits);
|
|
|
|
AST_APP_ARG(options);
|
|
|
|
AST_APP_ARG(attempts);
|
|
|
|
AST_APP_ARG(timeout);
|
|
|
|
);
|
|
|
|
|
2005-10-26 19:48:14 +00:00
|
|
|
if (ast_strlen_zero(data)) {
|
2005-10-19 18:19:02 +00:00
|
|
|
ast_log(LOG_WARNING, "Read requires an argument (variable)\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-08-21 02:11:39 +00:00
|
|
|
u = ast_module_user_add(chan);
|
2005-10-19 18:19:02 +00:00
|
|
|
|
2006-05-10 13:22:15 +00:00
|
|
|
argcopy = ast_strdupa(data);
|
2004-12-29 13:06:53 +00:00
|
|
|
|
2006-01-05 19:55:03 +00:00
|
|
|
AST_STANDARD_APP_ARGS(arglist, argcopy);
|
2004-12-29 13:06:53 +00:00
|
|
|
|
2006-01-05 21:04:06 +00:00
|
|
|
if (!ast_strlen_zero(arglist.options)) {
|
2006-01-05 19:55:03 +00:00
|
|
|
ast_app_parse_options(read_app_options, &flags, NULL, arglist.options);
|
|
|
|
}
|
|
|
|
|
2006-01-05 21:04:06 +00:00
|
|
|
if (!ast_strlen_zero(arglist.attempts)) {
|
2006-01-05 19:55:03 +00:00
|
|
|
tries = atoi(arglist.attempts);
|
2006-01-05 21:04:06 +00:00
|
|
|
if (tries <= 0)
|
2004-12-29 13:06:53 +00:00
|
|
|
tries = 1;
|
|
|
|
}
|
|
|
|
|
2006-01-05 21:04:06 +00:00
|
|
|
if (!ast_strlen_zero(arglist.timeout)) {
|
2006-10-05 01:40:06 +00:00
|
|
|
tosec = atof(arglist.timeout);
|
|
|
|
if (tosec <= 0)
|
2004-12-29 13:06:53 +00:00
|
|
|
to = 0;
|
2005-06-01 17:23:50 +00:00
|
|
|
else
|
2006-10-05 01:40:06 +00:00
|
|
|
to = tosec * 1000.0;
|
2004-12-29 13:06:53 +00:00
|
|
|
}
|
|
|
|
|
2006-01-05 21:04:06 +00:00
|
|
|
if (ast_strlen_zero(arglist.filename)) {
|
2006-01-05 19:55:03 +00:00
|
|
|
arglist.filename = NULL;
|
|
|
|
}
|
2006-01-05 21:04:06 +00:00
|
|
|
if (!ast_strlen_zero(arglist.maxdigits)) {
|
2006-01-05 19:55:03 +00:00
|
|
|
maxdigits = atoi(arglist.maxdigits);
|
2004-12-29 13:42:14 +00:00
|
|
|
if ((maxdigits<1) || (maxdigits>255)) {
|
|
|
|
maxdigits = 255;
|
|
|
|
} else if (option_verbose > 2)
|
2005-04-29 17:00:33 +00:00
|
|
|
ast_verbose(VERBOSE_PREFIX_3 "Accepting a maximum of %d digits.\n", maxdigits);
|
2004-12-29 13:06:53 +00:00
|
|
|
}
|
2006-01-05 19:55:03 +00:00
|
|
|
if (ast_strlen_zero(arglist.variable)) {
|
2004-12-29 19:20:18 +00:00
|
|
|
ast_log(LOG_WARNING, "Invalid! Usage: Read(variable[|filename][|maxdigits][|option][|attempts][|timeout])\n\n");
|
2006-08-21 02:11:39 +00:00
|
|
|
ast_module_user_remove(u);
|
2003-11-10 04:54:02 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2006-01-05 19:55:03 +00:00
|
|
|
ts=NULL;
|
2006-01-05 21:04:06 +00:00
|
|
|
if (ast_test_flag(&flags,OPT_INDICATION)) {
|
|
|
|
if (!ast_strlen_zero(arglist.filename)) {
|
2006-01-05 19:55:03 +00:00
|
|
|
ts = ast_get_indication_tone(chan->zone,arglist.filename);
|
|
|
|
}
|
|
|
|
}
|
2003-11-10 04:47:43 +00:00
|
|
|
if (chan->_state != AST_STATE_UP) {
|
2006-01-05 19:55:03 +00:00
|
|
|
if (ast_test_flag(&flags,OPT_SKIP)) {
|
2004-06-30 16:04:28 +00:00
|
|
|
/* At the user's option, skip if the line is not up */
|
2006-01-05 19:55:03 +00:00
|
|
|
pbx_builtin_setvar_helper(chan, arglist.variable, "\0");
|
2006-08-21 02:11:39 +00:00
|
|
|
ast_module_user_remove(u);
|
2004-06-30 16:04:28 +00:00
|
|
|
return 0;
|
2006-01-05 19:55:03 +00:00
|
|
|
} else if (!ast_test_flag(&flags,OPT_NOANSWER)) {
|
2004-06-30 16:04:28 +00:00
|
|
|
/* Otherwise answer unless we're supposed to read while on-hook */
|
|
|
|
res = ast_answer(chan);
|
|
|
|
}
|
2003-11-10 04:47:43 +00:00
|
|
|
}
|
|
|
|
if (!res) {
|
2006-01-05 21:04:06 +00:00
|
|
|
while (tries && !res) {
|
2004-12-29 13:06:53 +00:00
|
|
|
ast_stopstream(chan);
|
2006-01-05 21:04:06 +00:00
|
|
|
if (ts && ts->data[0]) {
|
|
|
|
if (!to)
|
2006-01-05 19:55:03 +00:00
|
|
|
to = chan->pbx ? chan->pbx->rtimeout * 1000 : 6000;
|
|
|
|
res = ast_playtones_start(chan, 0, ts->data, 0);
|
2006-01-05 21:04:06 +00:00
|
|
|
for (x = 0; x < maxdigits; ) {
|
2006-01-05 19:55:03 +00:00
|
|
|
res = ast_waitfordigit(chan, to);
|
|
|
|
ast_playtones_stop(chan);
|
2006-01-05 21:04:06 +00:00
|
|
|
if (res < 1) {
|
2006-01-05 19:55:03 +00:00
|
|
|
tmp[x]='\0';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
tmp[x++] = res;
|
2006-01-05 21:04:06 +00:00
|
|
|
if (tmp[x-1] == '#') {
|
2006-01-05 19:55:03 +00:00
|
|
|
tmp[x-1] = '\0';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
res = ast_app_getdata(chan, arglist.filename, tmp, maxdigits, to);
|
|
|
|
}
|
2004-12-29 13:06:53 +00:00
|
|
|
if (res > -1) {
|
2006-01-05 19:55:03 +00:00
|
|
|
pbx_builtin_setvar_helper(chan, arglist.variable, tmp);
|
2004-12-29 13:06:53 +00:00
|
|
|
if (!ast_strlen_zero(tmp)) {
|
2004-12-29 13:42:14 +00:00
|
|
|
if (option_verbose > 2)
|
|
|
|
ast_verbose(VERBOSE_PREFIX_3 "User entered '%s'\n", tmp);
|
2004-12-29 13:06:53 +00:00
|
|
|
tries = 0;
|
|
|
|
} else {
|
|
|
|
tries--;
|
2004-12-29 13:42:14 +00:00
|
|
|
if (option_verbose > 2) {
|
|
|
|
if (tries)
|
2005-07-07 22:32:20 +00:00
|
|
|
ast_verbose(VERBOSE_PREFIX_3 "User entered nothing, %d chance%s left\n", tries, (tries != 1) ? "s" : "");
|
2004-12-29 13:42:14 +00:00
|
|
|
else
|
|
|
|
ast_verbose(VERBOSE_PREFIX_3 "User entered nothing.\n");
|
|
|
|
}
|
2004-12-29 13:06:53 +00:00
|
|
|
}
|
|
|
|
res = 0;
|
|
|
|
} else {
|
2006-09-07 16:40:15 +00:00
|
|
|
pbx_builtin_setvar_helper(chan, arglist.variable, tmp);
|
2004-12-29 13:42:14 +00:00
|
|
|
if (option_verbose > 2)
|
|
|
|
ast_verbose(VERBOSE_PREFIX_3 "User disconnected\n");
|
2004-12-29 13:06:53 +00:00
|
|
|
}
|
2004-09-02 13:52:58 +00:00
|
|
|
}
|
2003-11-10 04:47:43 +00:00
|
|
|
}
|
2006-08-21 02:11:39 +00:00
|
|
|
ast_module_user_remove(u);
|
2003-11-10 04:47:43 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2006-08-21 02:11:39 +00:00
|
|
|
static int unload_module(void)
|
2003-11-10 04:47:43 +00:00
|
|
|
{
|
2005-10-18 22:52:21 +00:00
|
|
|
int res;
|
|
|
|
|
|
|
|
res = ast_unregister_application(app);
|
|
|
|
|
2006-08-21 02:11:39 +00:00
|
|
|
ast_module_user_hangup_all();
|
2005-10-18 22:52:21 +00:00
|
|
|
|
|
|
|
return res;
|
2003-11-10 04:47:43 +00:00
|
|
|
}
|
|
|
|
|
2006-08-21 02:11:39 +00:00
|
|
|
static int load_module(void)
|
2003-11-10 04:47:43 +00:00
|
|
|
{
|
|
|
|
return ast_register_application(app, read_exec, synopsis, descrip);
|
|
|
|
}
|
|
|
|
|
2006-08-21 02:11:39 +00:00
|
|
|
AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Read Variable Application");
|