00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include "asterisk.h"
00028
00029 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 211569 $")
00030
00031 #include "asterisk/module.h"
00032 #include "asterisk/channel.h"
00033 #include "asterisk/pbx.h"
00034 #include "asterisk/utils.h"
00035 #include "asterisk/app.h"
00036
00037 static int acf_rand_exec(struct ast_channel *chan, const char *cmd,
00038 char *parse, char *buffer, size_t buflen)
00039 {
00040 int min_int, response_int, max_int;
00041 AST_DECLARE_APP_ARGS(args,
00042 AST_APP_ARG(min);
00043 AST_APP_ARG(max);
00044 );
00045
00046 AST_STANDARD_APP_ARGS(args, parse);
00047
00048 if (ast_strlen_zero(args.min) || sscanf(args.min, "%30d", &min_int) != 1)
00049 min_int = 0;
00050
00051 if (ast_strlen_zero(args.max) || sscanf(args.max, "%30d", &max_int) != 1)
00052 max_int = RAND_MAX;
00053
00054 if (max_int < min_int) {
00055 int tmp = max_int;
00056
00057 max_int = min_int;
00058 min_int = tmp;
00059 ast_debug(1, "max<min\n");
00060 }
00061
00062 response_int = min_int + (ast_random() % (max_int - min_int + 1));
00063 ast_debug(1, "%d was the lucky number in range [%d,%d]\n", response_int, min_int, max_int);
00064 snprintf(buffer, buflen, "%d", response_int);
00065
00066 return 0;
00067 }
00068
00069 static struct ast_custom_function acf_rand = {
00070 .name = "RAND",
00071 .synopsis = "Choose a random number in a range",
00072 .syntax = "RAND([min][,max])",
00073 .desc =
00074 "Choose a random number between min and max. Min defaults to 0, if not\n"
00075 "specified, while max defaults to RAND_MAX (2147483647 on many systems).\n"
00076 " Example: Set(junky=${RAND(1,8)}); \n"
00077 " Sets junky to a random number between 1 and 8, inclusive.\n",
00078 .read = acf_rand_exec,
00079 };
00080
00081 static int unload_module(void)
00082 {
00083 ast_custom_function_unregister(&acf_rand);
00084
00085 return 0;
00086 }
00087
00088 static int load_module(void)
00089 {
00090 return ast_custom_function_register(&acf_rand);
00091 }
00092
00093 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Random number dialplan function");