app_readexten.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 2007-2008, Trinity College Computing Center
00005  * Written by David Chappell <David.Chappell@trincoll.edu>
00006  *
00007  * See http://www.asterisk.org for more information about
00008  * the Asterisk project. Please do not directly contact
00009  * any of the maintainers of this project for assistance;
00010  * the project provides a web site, mailing lists and IRC
00011  * channels for your use.
00012  *
00013  * This program is free software, distributed under the terms of
00014  * the GNU General Public License Version 2. See the LICENSE file
00015  * at the top of the source tree.
00016  */
00017 
00018 /*! \file
00019  *
00020  * \brief Trivial application to read an extension into a variable
00021  *
00022  * \author David Chappell <David.Chappell@trincoll.edu>
00023  * 
00024  * \ingroup applications
00025  */
00026 
00027 /*** MODULEINFO
00028    <support_level>core</support_level>
00029  ***/
00030  
00031 #include "asterisk.h"
00032 
00033 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 357542 $")
00034 
00035 #include "asterisk/file.h"
00036 #include "asterisk/pbx.h"
00037 #include "asterisk/app.h"
00038 #include "asterisk/module.h"
00039 #include "asterisk/indications.h"
00040 #include "asterisk/channel.h"
00041 
00042 /*** DOCUMENTATION
00043    <application name="ReadExten" language="en_US">
00044       <synopsis>
00045          Read an extension into a variable.
00046       </synopsis>
00047       <syntax>
00048          <parameter name="variable" required="true" />
00049          <parameter name="filename">
00050             <para>File to play before reading digits or tone with option <literal>i</literal></para>
00051          </parameter>
00052          <parameter name="context">
00053             <para>Context in which to match extensions.</para>
00054          </parameter>
00055          <parameter name="option">
00056             <optionlist>
00057                <option name="s">
00058                   <para>Return immediately if the channel is not answered.</para>
00059                </option>
00060                <option name="i">
00061                   <para>Play <replaceable>filename</replaceable> as an indication tone from your
00062                   <filename>indications.conf</filename> or a directly specified list of
00063                   frequencies and durations.</para>
00064                </option>
00065                <option name="n">
00066                   <para>Read digits even if the channel is not answered.</para>
00067                </option>
00068             </optionlist>
00069          </parameter>
00070          <parameter name="timeout">
00071             <para>An integer number of seconds to wait for a digit response. If
00072             greater than <literal>0</literal>, that value will override the default timeout.</para>
00073          </parameter>
00074       </syntax>
00075       <description>
00076          <para>Reads a <literal>#</literal> terminated string of digits from the user into the given variable.</para>
00077          <para>Will set READEXTENSTATUS on exit with one of the following statuses:</para>
00078          <variablelist>
00079             <variable name="READEXTENSTATUS">
00080                <value name="OK">
00081                   A valid extension exists in ${variable}.
00082                </value>
00083                <value name="TIMEOUT">
00084                   No extension was entered in the specified time.  Also sets ${variable} to "t".
00085                </value>
00086                <value name="INVALID">
00087                   An invalid extension, ${INVALID_EXTEN}, was entered.  Also sets ${variable} to "i".
00088                </value>
00089                <value name="SKIP">
00090                   Line was not up and the option 's' was specified.
00091                </value>
00092                <value name="ERROR">
00093                   Invalid arguments were passed.
00094                </value>
00095             </variable>
00096          </variablelist>
00097       </description>
00098    </application>
00099  ***/
00100 
00101 enum readexten_option_flags {
00102    OPT_SKIP = (1 << 0),
00103    OPT_INDICATION = (1 << 1),
00104    OPT_NOANSWER = (1 << 2),
00105 };
00106 
00107 AST_APP_OPTIONS(readexten_app_options, {
00108    AST_APP_OPTION('s', OPT_SKIP),
00109    AST_APP_OPTION('i', OPT_INDICATION),
00110    AST_APP_OPTION('n', OPT_NOANSWER),
00111 });
00112 
00113 static char *app = "ReadExten";
00114 
00115 static int readexten_exec(struct ast_channel *chan, const char *data)
00116 {
00117    int res = 0;
00118    char exten[256] = "";
00119    int maxdigits = sizeof(exten) - 1;
00120    int timeout = 0, digit_timeout = 0, x = 0;
00121    char *argcopy = NULL, *status = "";
00122    struct ast_tone_zone_sound *ts = NULL;
00123    struct ast_flags flags = {0};
00124 
00125     AST_DECLARE_APP_ARGS(arglist,
00126       AST_APP_ARG(variable);
00127       AST_APP_ARG(filename);
00128       AST_APP_ARG(context);
00129       AST_APP_ARG(options);
00130       AST_APP_ARG(timeout);
00131    );
00132 
00133    if (ast_strlen_zero(data)) {
00134       ast_log(LOG_WARNING, "ReadExten requires at least one argument\n");
00135       pbx_builtin_setvar_helper(chan, "READEXTENSTATUS", "ERROR");
00136       return 0;
00137    }
00138 
00139    argcopy = ast_strdupa(data);
00140    AST_STANDARD_APP_ARGS(arglist, argcopy);
00141 
00142    if (ast_strlen_zero(arglist.variable)) {
00143       ast_log(LOG_WARNING, "Usage: ReadExten(variable[,filename[,context[,options[,timeout]]]])\n");
00144       pbx_builtin_setvar_helper(chan, "READEXTENSTATUS", "ERROR");
00145       return 0;
00146    }
00147 
00148    if (ast_strlen_zero(arglist.filename)) {
00149       arglist.filename = NULL;
00150    }
00151 
00152    if (ast_strlen_zero(arglist.context)) {
00153       arglist.context = ast_strdupa(ast_channel_context(chan));
00154    }
00155 
00156    if (!ast_strlen_zero(arglist.options)) {
00157       ast_app_parse_options(readexten_app_options, &flags, NULL, arglist.options);
00158    }
00159 
00160    if (!ast_strlen_zero(arglist.timeout)) {
00161       timeout = atoi(arglist.timeout);
00162       if (timeout > 0)
00163          timeout *= 1000;
00164    }
00165 
00166    if (timeout <= 0)
00167       timeout = ast_channel_pbx(chan) ? ast_channel_pbx(chan)->rtimeoutms : 10000;
00168 
00169    if (digit_timeout <= 0)
00170       digit_timeout = ast_channel_pbx(chan) ? ast_channel_pbx(chan)->dtimeoutms : 5000;
00171 
00172    if (ast_test_flag(&flags, OPT_INDICATION) && !ast_strlen_zero(arglist.filename)) {
00173       ts = ast_get_indication_tone(ast_channel_zone(chan), arglist.filename);
00174    }
00175 
00176    do {
00177       if (ast_channel_state(chan) != AST_STATE_UP) {
00178          if (ast_test_flag(&flags, OPT_SKIP)) {
00179             /* At the user's option, skip if the line is not up */
00180             pbx_builtin_setvar_helper(chan, arglist.variable, "");
00181             status = "SKIP";
00182             break;
00183          } else if (!ast_test_flag(&flags, OPT_NOANSWER)) {
00184             /* Otherwise answer unless we're supposed to read while on-hook */
00185             res = ast_answer(chan);
00186          }
00187       }
00188 
00189       if (res < 0) {
00190          status = "HANGUP";
00191          break;
00192       }
00193 
00194       ast_playtones_stop(chan);
00195       ast_stopstream(chan);
00196 
00197       if (ts && ts->data[0]) {
00198          res = ast_playtones_start(chan, 0, ts->data, 0);
00199       } else if (arglist.filename) {
00200          if (ast_test_flag(&flags, OPT_INDICATION) && ast_fileexists(arglist.filename, NULL, ast_channel_language(chan)) <= 0) {
00201             /*
00202              * We were asked to play an indication that did not exist in the config.
00203              * If no such file exists, play it as a tonelist.  With any luck they won't
00204              * have a file named "350+440.ulaw"
00205              * (but honestly, who would do something so silly?)
00206              */
00207             res = ast_playtones_start(chan, 0, arglist.filename, 0);
00208          } else {
00209             res = ast_streamfile(chan, arglist.filename, ast_channel_language(chan));
00210          }
00211       }
00212 
00213       for (x = 0; x < maxdigits; x++) {
00214          ast_debug(3, "extension so far: '%s', timeout: %d\n", exten, timeout);
00215          res = ast_waitfordigit(chan, timeout);
00216 
00217          ast_playtones_stop(chan);
00218          ast_stopstream(chan);
00219          timeout = digit_timeout;
00220 
00221          if (res < 1) {    /* timeout expired or hangup */
00222             if (ast_check_hangup(chan)) {
00223                status = "HANGUP";
00224             } else if (x == 0) {
00225                pbx_builtin_setvar_helper(chan, arglist.variable, "t");
00226                status = "TIMEOUT";
00227             }
00228             break;
00229          }
00230 
00231          exten[x] = res;
00232          if (!ast_matchmore_extension(chan, arglist.context, exten, 1 /* priority */,
00233             S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL))) {
00234             if (!ast_exists_extension(chan, arglist.context, exten, 1,
00235                S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL))
00236                && res == '#') {
00237                exten[x] = '\0';
00238             }
00239             break;
00240          }
00241       }
00242 
00243       if (!ast_strlen_zero(status))
00244          break;
00245 
00246       if (ast_exists_extension(chan, arglist.context, exten, 1,
00247          S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL))) {
00248          ast_debug(3, "User entered valid extension '%s'\n", exten);
00249          pbx_builtin_setvar_helper(chan, arglist.variable, exten);
00250          status = "OK";
00251       } else {
00252          ast_debug(3, "User dialed invalid extension '%s' in context '%s' on %s\n", exten, arglist.context, ast_channel_name(chan));
00253          pbx_builtin_setvar_helper(chan, arglist.variable, "i");
00254          pbx_builtin_setvar_helper(chan, "INVALID_EXTEN", exten);
00255          status = "INVALID";
00256       }
00257    } while (0);
00258 
00259    if (ts) {
00260       ts = ast_tone_zone_sound_unref(ts);
00261    }
00262 
00263    pbx_builtin_setvar_helper(chan, "READEXTENSTATUS", status);
00264 
00265    return status[0] == 'H' ? -1 : 0;
00266 }
00267 
00268 static int unload_module(void)
00269 {
00270    int res = ast_unregister_application(app);
00271    return res;
00272 }
00273 
00274 static int load_module(void)
00275 {
00276    int res = ast_register_application_xml(app, readexten_exec);
00277    return res;
00278 }
00279 
00280 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Read and evaluate extension validity");

Generated on Thu Apr 16 06:27:12 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6