func_rand.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 2006, Digium, Inc.
00005  * Copyright (C) 2006, Claude Patry
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 Generate Random Number
00021  * 
00022  * \author Claude Patry <cpatry@gmail.com>
00023  * \author Tilghman Lesher ( http://asterisk.drunkcoder.com/ )
00024  * \ingroup functions
00025  */
00026 
00027 /*** MODULEINFO
00028    <support_level>core</support_level>
00029  ***/
00030 
00031 #include "asterisk.h"
00032 
00033 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 389251 $")
00034 
00035 #include "asterisk/module.h"
00036 #include "asterisk/channel.h"
00037 #include "asterisk/pbx.h"
00038 #include "asterisk/utils.h"
00039 #include "asterisk/app.h"
00040 
00041 /*** DOCUMENTATION
00042    <function name="RAND" language="en_US">
00043       <synopsis>
00044          Choose a random number in a range.        
00045       </synopsis>
00046       <syntax>
00047          <parameter name="min" />
00048          <parameter name="max" />
00049       </syntax>
00050       <description>
00051          <para>Choose a random number between <replaceable>min</replaceable> and <replaceable>max</replaceable>. 
00052          <replaceable>min</replaceable> defaults to <literal>0</literal>, if not specified, while <replaceable>max</replaceable> defaults 
00053          to <literal>RAND_MAX</literal> (2147483647 on many systems).</para>
00054          <para>Example:  Set(junky=${RAND(1,8)});
00055          Sets junky to a random number between 1 and 8, inclusive.</para>
00056       </description>
00057    </function>
00058  ***/
00059 static int acf_rand_exec(struct ast_channel *chan, const char *cmd,
00060           char *parse, char *buffer, size_t buflen)
00061 {
00062    int min_int, response_int, max_int;
00063    AST_DECLARE_APP_ARGS(args,
00064               AST_APP_ARG(min);
00065               AST_APP_ARG(max);
00066    );
00067 
00068    AST_STANDARD_APP_ARGS(args, parse);
00069 
00070    if (ast_strlen_zero(args.min) || sscanf(args.min, "%30d", &min_int) != 1)
00071       min_int = 0;
00072 
00073    if (ast_strlen_zero(args.max) || sscanf(args.max, "%30d", &max_int) != 1)
00074       max_int = RAND_MAX;
00075 
00076    if (max_int < min_int) {
00077       int tmp = max_int;
00078 
00079       max_int = min_int;
00080       min_int = tmp;
00081       ast_debug(1, "max<min\n");
00082    }
00083 
00084    response_int = min_int + (ast_random() % (max_int - min_int + 1));
00085    ast_debug(1, "%d was the lucky number in range [%d,%d]\n", response_int, min_int, max_int);
00086    snprintf(buffer, buflen, "%d", response_int);
00087 
00088    return 0;
00089 }
00090 
00091 static struct ast_custom_function acf_rand = {
00092    .name = "RAND",
00093    .read = acf_rand_exec,
00094    .read_max = 12,
00095 };
00096 
00097 static int unload_module(void)
00098 {
00099    ast_custom_function_unregister(&acf_rand);
00100 
00101    return 0;
00102 }
00103 
00104 static int load_module(void)
00105 {
00106    return ast_custom_function_register(&acf_rand);
00107 }
00108 
00109 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Random number dialplan function");

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