(closes issue #12846)
[asterisk-bristuff.git] / apps / app_random.c
blob8484f656d8295d72d082b4c20cc4f2851465691d
1 /*
2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (c) 2003 - 2005 Tilghman Lesher. All rights reserved.
6 * Tilghman Lesher <asterisk__app_random__200508@the-tilghman.com>
8 * This code is released by the author with no restrictions on usage or distribution.
10 * See http://www.asterisk.org for more information about
11 * the Asterisk project. Please do not directly contact
12 * any of the maintainers of this project for assistance;
13 * the project provides a web site, mailing lists and IRC
14 * channels for your use.
18 /*! \file
20 * \brief Random application
22 * \author Tilghman Lesher <asterisk__app_random__200508@the-tilghman.com>
23 * \ingroup applications
26 #include "asterisk.h"
28 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <unistd.h>
33 #include <string.h>
35 #include "asterisk/file.h"
36 #include "asterisk/logger.h"
37 #include "asterisk/options.h"
38 #include "asterisk/channel.h"
39 #include "asterisk/pbx.h"
40 #include "asterisk/module.h"
42 /*! \todo The Random() app should be removed from trunk following the release of 1.4 */
44 static char *app_random = "Random";
46 static char *random_synopsis = "Conditionally branches, based upon a probability";
48 static char *random_descrip =
49 "Random([probability]:[[context|]extension|]priority)\n"
50 " probability := INTEGER in the range 1 to 100\n"
51 "DEPRECATED: Use GotoIf($[${RAND(1,100)} > <number>]?<label>)\n";
54 static int random_exec(struct ast_channel *chan, void *data)
56 int res=0;
57 struct ast_module_user *u;
59 char *s;
60 char *prob;
61 int probint;
62 static int deprecated = 0;
64 if (ast_strlen_zero(data)) {
65 ast_log(LOG_WARNING, "Random requires an argument ([probability]:[[context|]extension|]priority)\n");
66 return -1;
69 u = ast_module_user_add(chan);
71 s = ast_strdupa(data);
73 prob = strsep(&s,":");
74 if ((!prob) || (sscanf(prob, "%d", &probint) != 1))
75 probint = 0;
77 if (!deprecated) {
78 deprecated = 1;
79 ast_log(LOG_WARNING, "Random is deprecated in Asterisk 1.4. Replace with GotoIf($[${RAND(0,99)} + %d >= 100]?%s)\n", probint, s);
82 if ((ast_random() % 100) + probint >= 100) {
83 res = ast_parseable_goto(chan, s);
84 if (option_verbose > 2)
85 ast_verbose( VERBOSE_PREFIX_3 "Random branches to (%s,%s,%d)\n",
86 chan->context,chan->exten, chan->priority+1);
88 ast_module_user_remove(u);
89 return res;
92 static int unload_module(void)
94 int res;
96 res = ast_unregister_application(app_random);
98 ast_module_user_hangup_all();
100 return res;
103 static int load_module(void)
105 return ast_register_application(app_random, random_exec, random_synopsis, random_descrip);
108 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Random goto");