/* * CallWeaver -- An open source telephony toolkit. * * Copyright (c) 2003 - 2005 Tilghman Lesher. All rights reserved. * * Tilghman Lesher * * This code is released by the author with no restrictions on usage or distribution. * * See http://www.callweaver.org for more information about * the CallWeaver project. Please do not directly contact * any of the maintainers of this project for assistance; * the project provides a web site, mailing lists and IRC * channels for your use. * */ /*! \file * * \brief Random application * * \author Tilghman Lesher */ #ifdef HAVE_CONFIG_H #include "confdefs.h" #endif #include #include #include #include #include "callweaver.h" CALLWEAVER_FILE_VERSION("$HeadURL$", "$Revision$") #include "callweaver/file.h" #include "callweaver/logger.h" #include "callweaver/options.h" #include "callweaver/channel.h" #include "callweaver/pbx.h" #include "callweaver/module.h" static char *tdesc = "Random goto"; static void *random_app; static const char *random_name = "Random"; static const char *random_synopsis = "Conditionally branches, based upon a probability"; static const char *random_syntax = "Random([probability]:[[context, ]extension, ]priority)"; static const char *random_descrip = "Conditionally branches, based upon a probability\n" " probability := INTEGER in the range 1 to 100\n"; STANDARD_LOCAL_USER; LOCAL_USER_DECL; static char random_state[256]; static int random_exec(struct cw_channel *chan, int argc, char **argv) { int res=0; struct localuser *u; char *s, *context, *exten; int probint; if (argc < 1 || argc > 3) { cw_log(LOG_ERROR, "Syntax: %s\n", random_syntax); return -1; } LOCAL_USER_ADD(u); if ((s = strchr(argv[0], ':'))) { probint = atoi(argv[0]); argv[0] = s + 1; } /* FIXME: is this really what was intended? */ if ((random() % 100) + probint > 100) { exten = (argc > 1 ? argv[argc-2] : NULL); context = (argc > 2 ? argv[argc-3] : NULL); res = cw_explicit_gotolabel(chan, context, exten, argv[argc-1]); if (!res && option_verbose > 2) cw_verbose( VERBOSE_PREFIX_3 "Random branches to (%s,%s,%d)\n", chan->context,chan->exten, chan->priority+1); } LOCAL_USER_REMOVE(u); return res; } int unload_module(void) { int res = 0; STANDARD_HANGUP_LOCALUSERS; res |= cw_unregister_application(random_app); return res; } int load_module(void) { initstate((getppid() * 65535 + getpid()) % RAND_MAX, random_state, 256); random_app = cw_register_application(random_name, random_exec, random_synopsis, random_syntax, random_descrip); return 0; } char *description(void) { return tdesc; } int usecount(void) { /* Don't allow unload, since rand(3) depends upon this module being here. */ return 1; // int res; // STANDARD_USECOUNT(res); // return res; }