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 <stdio.h>
00027 #include <stdlib.h>
00028 #include <unistd.h>
00029 #include <string.h>
00030
00031 #include "asterisk.h"
00032
00033 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 7221 $")
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 static char *tdesc = "Random goto";
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
00052 STANDARD_LOCAL_USER;
00053
00054 LOCAL_USER_DECL;
00055
00056 static char random_state[256];
00057
00058 static int random_exec(struct ast_channel *chan, void *data)
00059 {
00060 int res=0;
00061 struct localuser *u;
00062
00063 char *s;
00064 char *prob;
00065 int probint;
00066
00067 if (ast_strlen_zero(data)) {
00068 ast_log(LOG_WARNING, "Random requires an argument ([probability]:[[context|]extension|]priority)\n");
00069 return -1;
00070 }
00071
00072 LOCAL_USER_ADD(u);
00073
00074 s = ast_strdupa(data);
00075 if (!s) {
00076 ast_log(LOG_ERROR, "Out of memory!\n");
00077 LOCAL_USER_REMOVE(u);
00078 return -1;
00079 }
00080
00081 prob = strsep(&s,":");
00082 if ((!prob) || (sscanf(prob, "%d", &probint) != 1))
00083 probint = 0;
00084
00085 if ((random() % 100) + probint > 100) {
00086 res = ast_parseable_goto(chan, s);
00087 if (option_verbose > 2)
00088 ast_verbose( VERBOSE_PREFIX_3 "Random branches to (%s,%s,%d)\n",
00089 chan->context,chan->exten, chan->priority+1);
00090 }
00091 LOCAL_USER_REMOVE(u);
00092 return res;
00093 }
00094
00095 int unload_module(void)
00096 {
00097 int res;
00098
00099 res = ast_unregister_application(app_random);
00100
00101 STANDARD_HANGUP_LOCALUSERS;
00102
00103 return res;
00104 }
00105
00106 int load_module(void)
00107 {
00108 initstate((getppid() * 65535 + getpid()) % RAND_MAX, random_state, 256);
00109 return ast_register_application(app_random, random_exec, random_synopsis, random_descrip);
00110 }
00111
00112 char *description(void)
00113 {
00114 return tdesc;
00115 }
00116
00117 int usecount(void)
00118 {
00119 int res;
00120 STANDARD_USECOUNT(res);
00121 return res;
00122 }
00123
00124 char *key()
00125 {
00126 return ASTERISK_GPL_KEY;
00127 }