2 * Asterisk -- A telephony toolkit for Linux.
6 * Copyright (c) 2003-2004 Tilghman Lesher. All rights reserved.
8 * Tilghman Lesher <asterisk__app_random__20040111@the-tilghman.com>
10 * This code is released by the author with no restrictions on usage or distribution.
14 #include <asterisk/file.h>
15 #include <asterisk/logger.h>
16 #include <asterisk/options.h>
17 #include <asterisk/channel.h>
18 #include <asterisk/pbx.h>
19 #include <asterisk/module.h>
26 static char *tdesc = "Random goto";
28 static char *app_random = "Random";
30 static char *random_synopsis = "Conditionally branches, based upon a probability";
32 static char *random_descrip =
33 "Random([probability]:[[context|]extension|]priority)\n"
34 " probability := INTEGER in the range 1 to 100\n";
40 static int random_exec(struct ast_channel *chan, void *data)
46 char *exten, *pri, *context;
48 int probint, priorityint;
51 ast_log(LOG_WARNING, "Random requires an argument ([probability]:[[context|]extension|]priority)\n");
55 s = ast_strdupa((void *) data);
57 prob = strsep(&ts,":");
58 if ((!prob) || (sscanf(prob, "%d", &probint) != 1))
61 if ((random() % 100) + probint > 100) {
62 context = strsep(&s, "|");
63 exten = strsep(&s, "|");
70 pri = strsep(&s, "|");
78 ast_log(LOG_WARNING, "No label specified\n");
81 } else if (sscanf(pri, "%d", &priorityint) != 1) {
82 ast_log(LOG_WARNING, "Priority '%s' must be a number > 0\n", pri);
86 /* At this point we have a priority and */
87 /* maybe an extension and a context */
88 chan->priority = priorityint - 1;
89 if (exten && strcasecmp(exten, "BYEXTENSION"))
90 strncpy(chan->exten, exten, sizeof(chan->exten)-1);
92 strncpy(chan->context, context, sizeof(chan->context)-1);
93 if (option_verbose > 2)
94 ast_verbose( VERBOSE_PREFIX_3 "Random branches to (%s,%s,%d)\n",
95 chan->context,chan->exten, chan->priority+1);
101 int unload_module(void)
103 STANDARD_HANGUP_LOCALUSERS;
104 return ast_unregister_application(app_random);
107 int load_module(void)
109 srandom((unsigned int)getpid() + (unsigned int)time(NULL));
110 return ast_register_application(app_random, random_exec, random_synopsis, random_descrip);
113 char *description(void)
121 STANDARD_USECOUNT(res);
127 return ASTERISK_GPL_KEY;