2004-01-12 01:56:38 +00:00
|
|
|
/*
|
|
|
|
* Asterisk -- A telephony toolkit for Linux.
|
|
|
|
*
|
|
|
|
* Random application
|
|
|
|
*
|
2005-01-21 07:06:25 +00:00
|
|
|
* Copyright (c) 2003 - 2005 Tilghman Lesher. All rights reserved.
|
2004-01-12 01:56:38 +00:00
|
|
|
*
|
2005-08-25 04:04:14 +00:00
|
|
|
* Tilghman Lesher <asterisk__app_random__200508@the-tilghman.com>
|
2004-01-12 01:56:38 +00:00
|
|
|
*
|
|
|
|
* This code is released by the author with no restrictions on usage or distribution.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2005-06-06 22:39:32 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "asterisk.h"
|
|
|
|
|
|
|
|
ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
|
|
|
|
|
2005-04-21 06:02:45 +00:00
|
|
|
#include "asterisk/file.h"
|
|
|
|
#include "asterisk/logger.h"
|
|
|
|
#include "asterisk/options.h"
|
|
|
|
#include "asterisk/channel.h"
|
|
|
|
#include "asterisk/pbx.h"
|
|
|
|
#include "asterisk/module.h"
|
2004-01-12 01:56:38 +00:00
|
|
|
|
|
|
|
static char *tdesc = "Random goto";
|
|
|
|
|
|
|
|
static char *app_random = "Random";
|
|
|
|
|
|
|
|
static char *random_synopsis = "Conditionally branches, based upon a probability";
|
|
|
|
|
|
|
|
static char *random_descrip =
|
|
|
|
"Random([probability]:[[context|]extension|]priority)\n"
|
|
|
|
" probability := INTEGER in the range 1 to 100\n";
|
|
|
|
|
|
|
|
STANDARD_LOCAL_USER;
|
|
|
|
|
|
|
|
LOCAL_USER_DECL;
|
|
|
|
|
2005-08-25 04:04:14 +00:00
|
|
|
static char random_state[256];
|
|
|
|
|
2004-01-12 01:56:38 +00:00
|
|
|
static int random_exec(struct ast_channel *chan, void *data)
|
|
|
|
{
|
|
|
|
int res=0;
|
|
|
|
struct localuser *u;
|
|
|
|
|
2004-03-01 20:44:48 +00:00
|
|
|
char *s;
|
2004-01-12 01:56:38 +00:00
|
|
|
char *prob;
|
2005-08-25 04:04:14 +00:00
|
|
|
int probint;
|
2004-01-12 01:56:38 +00:00
|
|
|
|
|
|
|
if (!data) {
|
|
|
|
ast_log(LOG_WARNING, "Random requires an argument ([probability]:[[context|]extension|]priority)\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
LOCAL_USER_ADD(u);
|
|
|
|
s = ast_strdupa((void *) data);
|
|
|
|
|
2004-03-01 20:44:48 +00:00
|
|
|
prob = strsep(&s,":");
|
2004-01-12 01:56:38 +00:00
|
|
|
if ((!prob) || (sscanf(prob, "%d", &probint) != 1))
|
|
|
|
probint = 0;
|
|
|
|
|
|
|
|
if ((random() % 100) + probint > 100) {
|
2005-08-25 04:04:14 +00:00
|
|
|
res = ast_parseable_goto(chan, s);
|
2004-01-12 01:56:38 +00:00
|
|
|
if (option_verbose > 2)
|
|
|
|
ast_verbose( VERBOSE_PREFIX_3 "Random branches to (%s,%s,%d)\n",
|
|
|
|
chan->context,chan->exten, chan->priority+1);
|
|
|
|
}
|
2005-08-25 04:04:14 +00:00
|
|
|
LOCAL_USER_REMOVE(u);
|
2004-01-12 01:56:38 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
int unload_module(void)
|
|
|
|
{
|
|
|
|
STANDARD_HANGUP_LOCALUSERS;
|
|
|
|
return ast_unregister_application(app_random);
|
|
|
|
}
|
|
|
|
|
|
|
|
int load_module(void)
|
|
|
|
{
|
2005-08-25 04:04:14 +00:00
|
|
|
initstate((getppid() * 65535 + getpid()) % RAND_MAX, random_state, 256);
|
2004-01-12 01:56:38 +00:00
|
|
|
return ast_register_application(app_random, random_exec, random_synopsis, random_descrip);
|
|
|
|
}
|
|
|
|
|
|
|
|
char *description(void)
|
|
|
|
{
|
|
|
|
return tdesc;
|
|
|
|
}
|
|
|
|
|
|
|
|
int usecount(void)
|
|
|
|
{
|
|
|
|
int res;
|
|
|
|
STANDARD_USECOUNT(res);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *key()
|
|
|
|
{
|
|
|
|
return ASTERISK_GPL_KEY;
|
|
|
|
}
|