| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#include "asterisk.h" |
| 32 |
|
| 33 |
#include "asterisk/module.h" |
| 34 |
#include "asterisk/channel.h" |
| 35 |
#include "asterisk/pbx.h" |
| 36 |
#include "asterisk/utils.h" |
| 37 |
#include "asterisk/app.h" |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
static int acf_rand_exec(struct ast_channel *chan, const char *cmd, |
| 62 |
char *parse, char *buffer, size_t buflen) |
| 63 |
{ |
| 64 |
int min_int, response_int, max_int; |
| 65 |
AST_DECLARE_APP_ARGS(args, |
| 66 |
AST_APP_ARG(min); |
| 67 |
AST_APP_ARG(max); |
| 68 |
); |
| 69 |
|
| 70 |
AST_STANDARD_APP_ARGS(args, parse); |
| 71 |
|
| 72 |
if (ast_strlen_zero(args.min) || sscanf(args.min, "%30d", &min_int) != 1) |
| 73 |
min_int = 0; |
| 74 |
|
| 75 |
if (ast_strlen_zero(args.max) || sscanf(args.max, "%30d", &max_int) != 1) |
| 76 |
max_int = RAND_MAX; |
| 77 |
|
| 78 |
if (max_int < min_int) { |
| 79 |
int tmp = max_int; |
| 80 |
|
| 81 |
max_int = min_int; |
| 82 |
min_int = tmp; |
| 83 |
ast_debug(1, "max<min\n"); |
| 84 |
} |
| 85 |
|
| 86 |
response_int = min_int + (ast_random() % (max_int - min_int + 1)); |
| 87 |
ast_debug(1, "%d was the lucky number in range [%d,%d]\n", response_int, min_int, max_int); |
| 88 |
snprintf(buffer, buflen, "%d", response_int); |
| 89 |
|
| 90 |
return 0; |
| 91 |
} |
| 92 |
|
| 93 |
static struct ast_custom_function acf_rand = { |
| 94 |
.name = "RAND", |
| 95 |
.read = acf_rand_exec, |
| 96 |
.read_max = 12, |
| 97 |
}; |
| 98 |
|
| 99 |
static int unload_module(void) |
| 100 |
{ |
| 101 |
ast_custom_function_unregister(&acf_rand); |
| 102 |
|
| 103 |
return 0; |
| 104 |
} |
| 105 |
|
| 106 |
static int load_module(void) |
| 107 |
{ |
| 108 |
return ast_custom_function_register(&acf_rand); |
| 109 |
} |
| 110 |
|
| 111 |
AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Random number dialplan function"); |