Thu Apr 28 2011 17:15:15

Asterisk developer's documentation


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 #include "asterisk.h"
00028 
00029 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 281722 $")
00030 
00031 #include "asterisk/file.h"
00032 #include "asterisk/pbx.h"
00033 #include "asterisk/app.h"
00034 #include "asterisk/module.h"
00035 #include "asterisk/indications.h"
00036 #include "asterisk/channel.h"
00037 
00038 /*** DOCUMENTATION
00039    <application name="ReadExten" language="en_US">
00040       <synopsis>
00041          Read an extension into a variable.
00042       </synopsis>
00043       <syntax>
00044          <parameter name="variable" required="true" />
00045          <parameter name="filename">
00046             <para>File to play before reading digits or tone with option <literal>i</literal></para>
00047          </parameter>
00048          <parameter name="context">
00049             <para>Context in which to match extensions.</para>
00050          </parameter>
00051          <parameter name="option">
00052             <optionlist>
00053                <option name="s">
00054                   <para>Return immediately if the channel is not answered.</para>
00055                </option>
00056                <option name="i">
00057                   <para>Play <replaceable>filename</replaceable> as an indication tone from your
00058                   <filename>indications.conf</filename></para>
00059                </option>
00060                <option name="n">
00061                   <para>Read digits even if the channel is not answered.</para>
00062                </option>
00063             </optionlist>
00064          </parameter>
00065          <parameter name="timeout">
00066             <para>An integer number of seconds to wait for a digit response. If
00067             greater than <literal>0</literal>, that value will override the default timeout.</para>
00068          </parameter>
00069       </syntax>
00070       <description>
00071          <para>Reads a <literal>#</literal> terminated string of digits from the user into the given variable.</para>
00072          <para>Will set READEXTENSTATUS on exit with one of the following statuses:</para>
00073          <variablelist>
00074             <variable name="READEXTENSTATUS">
00075                <value name="OK">
00076                   A valid extension exists in ${variable}.
00077                </value>
00078                <value name="TIMEOUT">
00079                   No extension was entered in the specified time.  Also sets ${variable} to "t".
00080                </value>
00081                <value name="INVALID">
00082                   An invalid extension, ${INVALID_EXTEN}, was entered.  Also sets ${variable} to "i".
00083                </value>
00084                <value name="SKIP">
00085                   Line was not up and the option 's' was specified.
00086                </value>
00087                <value name="ERROR">
00088                   Invalid arguments were passed.
00089                </value>
00090             </variable>
00091          </variablelist>
00092       </description>
00093    </application>
00094    <function name="VALID_EXTEN" language="en_US">
00095       <synopsis>
00096          Determine whether an extension exists or not.
00097       </synopsis>
00098       <syntax>
00099          <parameter name="context">
00100             <para>Defaults to the current context</para>
00101          </parameter>
00102          <parameter name="extension" required="true" />
00103          <parameter name="priority">
00104             <para>Priority defaults to <literal>1</literal>.</para>
00105          </parameter>
00106       </syntax>
00107       <description>
00108          <para>Returns a true value if the indicated <replaceable>context</replaceable>,
00109          <replaceable>extension</replaceable>, and <replaceable>priority</replaceable> exist.</para>
00110       </description>
00111    </function>
00112  ***/
00113 
00114 enum {
00115    OPT_SKIP = (1 << 0),
00116    OPT_INDICATION = (1 << 1),
00117    OPT_NOANSWER = (1 << 2),
00118 } readexten_option_flags;
00119 
00120 AST_APP_OPTIONS(readexten_app_options, {
00121    AST_APP_OPTION('s', OPT_SKIP),
00122    AST_APP_OPTION('i', OPT_INDICATION),
00123    AST_APP_OPTION('n', OPT_NOANSWER),
00124 });
00125 
00126 static char *app = "ReadExten";
00127 
00128 static int readexten_exec(struct ast_channel *chan, void *data)
00129 {
00130    int res = 0;
00131    char exten[256] = "";
00132    int maxdigits = sizeof(exten) - 1;
00133    int timeout = 0, digit_timeout = 0, x = 0;
00134    char *argcopy = NULL, *status = "";
00135    struct ast_tone_zone_sound *ts = NULL;
00136    struct ast_flags flags = {0};
00137 
00138     AST_DECLARE_APP_ARGS(arglist,
00139       AST_APP_ARG(variable);
00140       AST_APP_ARG(filename);
00141       AST_APP_ARG(context);
00142       AST_APP_ARG(options);
00143       AST_APP_ARG(timeout);
00144    );
00145    
00146    if (ast_strlen_zero(data)) {
00147       ast_log(LOG_WARNING, "ReadExten requires at least one argument\n");
00148       pbx_builtin_setvar_helper(chan, "READEXTENSTATUS", "ERROR");
00149       return 0;
00150    }
00151 
00152    argcopy = ast_strdupa(data);
00153    AST_STANDARD_APP_ARGS(arglist, argcopy);
00154 
00155    if (ast_strlen_zero(arglist.variable)) {
00156       ast_log(LOG_WARNING, "Usage: ReadExten(variable[,filename[,context[,options[,timeout]]]])\n");
00157       pbx_builtin_setvar_helper(chan, "READEXTENSTATUS", "ERROR");
00158       return 0;
00159    }
00160 
00161    if (ast_strlen_zero(arglist.filename))
00162       arglist.filename = NULL;
00163 
00164    if (ast_strlen_zero(arglist.context))
00165       arglist.context = chan->context;
00166 
00167    if (!ast_strlen_zero(arglist.options))
00168       ast_app_parse_options(readexten_app_options, &flags, NULL, arglist.options);
00169 
00170    if (!ast_strlen_zero(arglist.timeout)) {
00171       timeout = atoi(arglist.timeout);
00172       if (timeout > 0)
00173          timeout *= 1000;
00174    }
00175 
00176    if (timeout <= 0)
00177       timeout = chan->pbx ? chan->pbx->rtimeoutms : 10000;
00178 
00179    if (digit_timeout <= 0)
00180       digit_timeout = chan->pbx ? chan->pbx->dtimeoutms : 5000;
00181 
00182    if (ast_test_flag(&flags, OPT_INDICATION) && !ast_strlen_zero(arglist.filename)) {
00183       ts = ast_get_indication_tone(chan->zone, arglist.filename);
00184    }
00185 
00186    do {
00187       if (chan->_state != AST_STATE_UP) {
00188          if (ast_test_flag(&flags, OPT_SKIP)) {
00189             /* At the user's option, skip if the line is not up */
00190             pbx_builtin_setvar_helper(chan, arglist.variable, "");
00191             status = "SKIP";
00192             break;
00193          } else if (!ast_test_flag(&flags, OPT_NOANSWER)) {
00194             /* Otherwise answer unless we're supposed to read while on-hook */
00195             res = ast_answer(chan);
00196          }
00197       }
00198 
00199       if (res < 0) {
00200          status = "HANGUP";
00201          break;
00202       }
00203 
00204       ast_playtones_stop(chan);
00205       ast_stopstream(chan);
00206 
00207       if (ts && ts->data[0])
00208          res = ast_playtones_start(chan, 0, ts->data, 0);
00209       else if (arglist.filename)
00210          res = ast_streamfile(chan, arglist.filename, chan->language);
00211 
00212       for (x = 0; x < maxdigits; x++) {
00213          ast_debug(3, "extension so far: '%s', timeout: %d\n", exten, timeout);
00214          res = ast_waitfordigit(chan, timeout);
00215 
00216          ast_playtones_stop(chan);
00217          ast_stopstream(chan);
00218          timeout = digit_timeout;
00219 
00220          if (res < 1) {    /* timeout expired or hangup */
00221             if (ast_check_hangup(chan)) {
00222                status = "HANGUP";
00223             } else if (x == 0) {
00224                pbx_builtin_setvar_helper(chan, arglist.variable, "t");
00225                status = "TIMEOUT";
00226             }
00227             break;
00228          }
00229 
00230          exten[x] = res;
00231          if (!ast_matchmore_extension(chan, arglist.context, exten, 1 /* priority */, chan->cid.cid_num)) {
00232             if (!ast_exists_extension(chan, arglist.context, exten, 1, chan->cid.cid_num) && res == '#') {
00233                exten[x] = '\0';
00234             }
00235             break;
00236          }
00237       }
00238 
00239       if (!ast_strlen_zero(status))
00240          break;
00241 
00242       if (ast_exists_extension(chan, arglist.context, exten, 1, chan->cid.cid_num)) {
00243          ast_debug(3, "User entered valid extension '%s'\n", exten);
00244          pbx_builtin_setvar_helper(chan, arglist.variable, exten);
00245          status = "OK";
00246       } else {
00247          ast_debug(3, "User dialed invalid extension '%s' in context '%s' on %s\n", exten, arglist.context, chan->name);
00248          pbx_builtin_setvar_helper(chan, arglist.variable, "i");
00249          pbx_builtin_setvar_helper(chan, "INVALID_EXTEN", exten);
00250          status = "INVALID";
00251       }
00252    } while (0);
00253 
00254    if (ts) {
00255       ts = ast_tone_zone_sound_unref(ts);
00256    }
00257 
00258    pbx_builtin_setvar_helper(chan, "READEXTENSTATUS", status);
00259 
00260    return status[0] == 'H' ? -1 : 0;
00261 }
00262 
00263 static int acf_isexten_exec(struct ast_channel *chan, const char *cmd, char *parse, char *buffer, size_t buflen)
00264 {
00265    int priority_int;
00266    AST_DECLARE_APP_ARGS(args,
00267       AST_APP_ARG(context);
00268       AST_APP_ARG(extension);
00269       AST_APP_ARG(priority);
00270    );
00271 
00272    AST_STANDARD_APP_ARGS(args, parse);
00273 
00274    if (ast_strlen_zero(args.context))
00275       args.context = chan->context;
00276 
00277    if (ast_strlen_zero(args.extension)) {
00278       ast_log(LOG_WARNING, "Syntax: VALID_EXTEN([<context>],<extension>[,<priority>]) - missing argument <extension>!\n");
00279       return -1;
00280    }
00281 
00282    if (ast_strlen_zero(args.priority))
00283       priority_int = 1;
00284    else
00285       priority_int = atoi(args.priority);
00286 
00287    if (ast_exists_extension(chan, args.context, args.extension, priority_int, chan->cid.cid_num))
00288        ast_copy_string(buffer, "1", buflen);
00289    else
00290        ast_copy_string(buffer, "0", buflen);
00291 
00292    return 0;
00293 }
00294 
00295 static struct ast_custom_function acf_isexten = {
00296    .name = "VALID_EXTEN",
00297    .read = acf_isexten_exec,
00298 };
00299 
00300 static int unload_module(void)
00301 {
00302    int res = ast_unregister_application(app);
00303    res |= ast_custom_function_unregister(&acf_isexten);
00304 
00305    return res; 
00306 }
00307 
00308 static int load_module(void)
00309 {
00310    int res = ast_register_application_xml(app, readexten_exec);
00311    res |= ast_custom_function_register(&acf_isexten);
00312    return res;
00313 }
00314 
00315 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Read and evaluate extension validity");