/* * app_contest.c Radio style contest * * Copyright (c) 2004-2007 Anthony Minessale II * * Permission is hereby granted, free of charge, to any person * obtaining a copy of this software and associated documentation * files (the "Software"), to deal in the Software without * restriction, including without limitation the rights to use, * copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following * conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR * OTHER DEALINGS IN THE SOFTWARE. */ #include #include #include #include #include #include #include #include #include #include #include static char *tdesc = "contest"; static char *app = "contest"; static char *synopsis = "contest"; static char *desc = ""; static int callerno = 0; static int winner = 0; static int ready = 0; AST_MUTEX_DEFINE_STATIC(mylock); static void try_your_luck(void) { ast_mutex_lock(&mylock); callerno++; ast_mutex_unlock(&mylock); } STANDARD_LOCAL_USER; LOCAL_USER_DECL; static int contest_exec(struct ast_channel *chan, void *data) { int res = -1; struct localuser *u; if (!data) { ast_log(LOG_WARNING, "contest requires an argument\n"); return -1; } LOCAL_USER_ADD(u); try_your_luck(); if (ready) { ast_log(LOG_NOTICE, "Caller %d\n", callerno); if (callerno == winner) { ready = 0; res = 0; } else { ast_streamfile(chan, "invalid", chan->language); ast_waitstream(chan, ""); res = -1; } } LOCAL_USER_REMOVE(u); return res; } static int contest_cli(int fd, int argc, char **argv) { if (argc == 2) { winner = atoi(argv[1]); callerno = 0; ready = winner ? 1 : 0; } ast_cli(fd, "ready=%d callerno=%d winner=%d\n", ready, callerno, winner); return 0; } static struct ast_cli_entry cli_contest = { { "contest", NULL, NULL }, contest_cli, "start a contest", "contest "}; int unload_module(void) { STANDARD_HANGUP_LOCALUSERS; ast_cli_unregister(&cli_contest); return ast_unregister_application(app); } int load_module(void) { ast_cli_register(&cli_contest); return ast_register_application(app, contest_exec, synopsis, desc); } char *description(void) { return tdesc; } int usecount(void) { int res; STANDARD_USECOUNT(res); return res; } char *key() { return ASTERISK_GPL_KEY; }