00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include "asterisk.h"
00011
00012 #include <string.h>
00013 #include <stdlib.h>
00014 #include <stdio.h>
00015
00016 #include "asterisk/lock.h"
00017 #include "asterisk/file.h"
00018 #include "asterisk/logger.h"
00019 #include "asterisk/channel.h"
00020 #include "asterisk/pbx.h"
00021 #include "asterisk/module.h"
00022 #include "asterisk/translate.h"
00023 #include "asterisk/utils.h"
00024 #include "asterisk/options.h"
00025 #include "asterisk/app.h"
00026 #include "asterisk/cli.h"
00027 #include "asterisk/localtime.h"
00028 #include "asterisk/say.h"
00029
00030 static char *app = "Cycle";
00031 static char *synopsis = "";
00032 static char *descrip = "\n";
00033
00034 static int counter = 0;
00035 static ast_mutex_t lock;
00036
00037 static int playback_exec(struct ast_channel *chan, void *data)
00038 {
00039 int res = 0;
00040 struct ast_module_user *u;
00041 char *tmp;
00042
00043 ast_log(LOG_ERROR, "Cycle(%s)\n", (char*)data);
00044
00045 AST_DECLARE_APP_ARGS(args,
00046 AST_APP_ARG(fnprefix);
00047 AST_APP_ARG(maxnum);
00048 AST_APP_ARG(opts);
00049 );
00050
00051 if (ast_strlen_zero(data)) {
00052 ast_log(LOG_ERROR, "Cycle requires 2 args: fnprefix and maxnum\n");
00053 return -1;
00054 }
00055
00056 AST_STANDARD_APP_ARGS(args, data);
00057
00058
00059 if (args.fnprefix == NULL )
00060 {
00061 ast_log(LOG_ERROR, "Cycle: fnprefix not set\n");
00062 return -1;
00063 }
00064
00065 int maxnum = atoi(args.maxnum);
00066
00067 if (maxnum < 1)
00068 {
00069 ast_log(LOG_ERROR, "Cycle: maxnum must be integer >= 1, but set to '%d' ('%s')\n", maxnum, args.maxnum);
00070 return -1;
00071 }
00072
00073 tmp = ast_strdupa(data);
00074 u = ast_module_user_add(chan);
00075
00076
00077 if (chan->_state != AST_STATE_UP)
00078 res = ast_answer(chan);
00079 if( res )
00080 goto done;
00081
00082 if (!res) {
00083 char *str;
00084 char *cycle;
00085
00086 ast_mutex_lock(&lock);
00087 {
00088 counter++;
00089 if (counter > maxnum)
00090 counter = 1;
00091 asprintf(&str, "%s%d", args.fnprefix, counter);
00092 asprintf(&cycle, "%d", counter);
00093 }
00094 ast_mutex_unlock(&lock);
00095
00096 ast_stopstream(chan);
00097 res = ast_streamfile(chan, str, chan->language);
00098 if (!res) {
00099 res = ast_waitstream(chan, "");
00100 ast_stopstream(chan);
00101 } else
00102 res = 0;
00103 pbx_builtin_setvar_helper(chan, "CYCLE", cycle);
00104 }
00105 done:
00106 ast_module_user_remove(u);
00107 return res;
00108 }
00109
00110 static int unload_module(void)
00111 {
00112 int res;
00113 res = ast_unregister_application(app);
00114 ast_module_user_hangup_all();
00115 return res;
00116 }
00117
00118 static int load_module(void)
00119 {
00120 return ast_register_application(app, playback_exec, synopsis, descrip);
00121 }
00122
00123 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Sound File Playback Application");