Sat Aug 6 00:39:29 2011

Asterisk developer's documentation


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  */
00025 
00026 #include "asterisk.h"
00027 
00028 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 211528 $")
00029 
00030 #include <stdlib.h>
00031 #include <string.h>
00032 #include <stdio.h>
00033 #include <sys/types.h>
00034 
00035 #include "asterisk/module.h"
00036 #include "asterisk/channel.h"
00037 #include "asterisk/pbx.h"
00038 #include "asterisk/logger.h"
00039 #include "asterisk/utils.h"
00040 #include "asterisk/app.h"
00041 
00042 static int acf_rand_exec(struct ast_channel *chan, char *cmd,
00043           char *parse, char *buffer, size_t buflen)
00044 {
00045    struct ast_module_user *u;
00046    int min_int, response_int, max_int;
00047    AST_DECLARE_APP_ARGS(args,
00048               AST_APP_ARG(min);
00049               AST_APP_ARG(max);
00050    );
00051 
00052    u = ast_module_user_add(chan);
00053 
00054    AST_STANDARD_APP_ARGS(args, parse);
00055 
00056    if (ast_strlen_zero(args.min) || sscanf(args.min, "%30d", &min_int) != 1)
00057       min_int = 0;
00058 
00059    if (ast_strlen_zero(args.max) || sscanf(args.max, "%30d", &max_int) != 1)
00060       max_int = RAND_MAX;
00061 
00062    if (max_int < min_int) {
00063       int tmp = max_int;
00064 
00065       max_int = min_int;
00066       min_int = tmp;
00067       ast_log(LOG_DEBUG, "max<min\n");
00068    }
00069 
00070    response_int = min_int + (ast_random() % (max_int - min_int + 1));
00071    ast_log(LOG_DEBUG, "%d was the lucky number in range [%d,%d]\n",
00072       response_int, min_int, max_int);
00073    snprintf(buffer, buflen, "%d", response_int);
00074 
00075    ast_module_user_remove(u);
00076 
00077    return 0;
00078 }
00079 
00080 static struct ast_custom_function acf_rand = {
00081    .name = "RAND",
00082    .synopsis = "Choose a random number in a range",
00083    .syntax = "RAND([min][|max])",
00084    .desc =
00085       "Choose a random number between min and max.  Min defaults to 0, if not\n"
00086       "specified, while max defaults to RAND_MAX (2147483647 on many systems).\n"
00087       "  Example:  Set(junky=${RAND(1|8)}); \n"
00088       "  Sets junky to a random number between 1 and 8, inclusive.\n",
00089    .read = acf_rand_exec,
00090 };
00091 
00092 static int unload_module(void)
00093 {
00094    ast_custom_function_unregister(&acf_rand);
00095 
00096    return 0;
00097 }
00098 
00099 static int load_module(void)
00100 {
00101    return ast_custom_function_register(&acf_rand);
00102 }
00103 
00104 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Random number dialplan function");

Generated on Sat Aug 6 00:39:29 2011 for Asterisk - the Open Source PBX by  doxygen 1.4.7