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 #include "asterisk.h"
00027
00028 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 211528 $")
00029
00030 #include <stdio.h>
00031 #include <stdlib.h>
00032 #include <unistd.h>
00033 #include <string.h>
00034
00035 #include "asterisk/file.h"
00036 #include "asterisk/logger.h"
00037 #include "asterisk/options.h"
00038 #include "asterisk/channel.h"
00039 #include "asterisk/pbx.h"
00040 #include "asterisk/module.h"
00041
00042
00043
00044 static char *app_random = "Random";
00045
00046 static char *random_synopsis = "Conditionally branches, based upon a probability";
00047
00048 static char *random_descrip =
00049 "Random([probability]:[[context|]extension|]priority)\n"
00050 " probability := INTEGER in the range 1 to 100\n"
00051 "DEPRECATED: Use GotoIf($[${RAND(1,100)} > <number>]?<label>)\n";
00052
00053
00054 static int random_exec(struct ast_channel *chan, void *data)
00055 {
00056 int res=0;
00057 struct ast_module_user *u;
00058
00059 char *s;
00060 char *prob;
00061 int probint;
00062 static int deprecated = 0;
00063
00064 if (ast_strlen_zero(data)) {
00065 ast_log(LOG_WARNING, "Random requires an argument ([probability]:[[context|]extension|]priority)\n");
00066 return -1;
00067 }
00068
00069 u = ast_module_user_add(chan);
00070
00071 s = ast_strdupa(data);
00072
00073 prob = strsep(&s,":");
00074 if ((!prob) || (sscanf(prob, "%30d", &probint) != 1))
00075 probint = 0;
00076
00077 if (!deprecated) {
00078 deprecated = 1;
00079 ast_log(LOG_WARNING, "Random is deprecated in Asterisk 1.4. Replace with GotoIf($[${RAND(0,99)} + %d >= 100]?%s)\n", probint, s);
00080 }
00081
00082 if ((ast_random() % 100) + probint >= 100) {
00083 res = ast_parseable_goto(chan, s);
00084 if (option_verbose > 2)
00085 ast_verbose( VERBOSE_PREFIX_3 "Random branches to (%s,%s,%d)\n",
00086 chan->context,chan->exten, chan->priority+1);
00087 }
00088 ast_module_user_remove(u);
00089 return res;
00090 }
00091
00092 static int unload_module(void)
00093 {
00094 int res;
00095
00096 res = ast_unregister_application(app_random);
00097
00098 ast_module_user_hangup_all();
00099
00100 return res;
00101 }
00102
00103 static int load_module(void)
00104 {
00105 return ast_register_application(app_random, random_exec, random_synopsis, random_descrip);
00106 }
00107
00108 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Random goto");