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