when loadable modules are disabled, if someone tries to load a module that does not...
[asterisk-bristuff.git] / apps / app_dictate.c
blob0c46118e47fdcd9d6b204a914fc701f4bc3cd3ab
1 /*
2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2005, Anthony Minessale II
6 * Anthony Minessale II <anthmct@yahoo.com>
8 * Donated by Sangoma Technologies <http://www.samgoma.com>
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.
16 * This program is free software, distributed under the terms of
17 * the GNU General Public License Version 2. See the LICENSE file
18 * at the top of the source tree.
21 /*! \file
23 * \brief Virtual Dictation Machine Application For Asterisk
25 * \author Anthony Minessale II <anthmct@yahoo.com>
27 * \ingroup applications
30 #include "asterisk.h"
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include <string.h>
37 #include <unistd.h>
38 #include <sys/stat.h>
40 #include "asterisk/file.h"
41 #include "asterisk/logger.h"
42 #include "asterisk/channel.h"
43 #include "asterisk/pbx.h"
44 #include "asterisk/module.h"
45 #include "asterisk/say.h"
46 #include "asterisk/lock.h"
47 #include "asterisk/app.h"
49 static char *app = "Dictate";
50 static char *synopsis = "Virtual Dictation Machine";
51 static char *desc = " Dictate([<base_dir>[|<filename>]])\n"
52 "Start dictation machine using optional base dir for files.\n";
55 typedef enum {
56 DFLAG_RECORD = (1 << 0),
57 DFLAG_PLAY = (1 << 1),
58 DFLAG_TRUNC = (1 << 2),
59 DFLAG_PAUSE = (1 << 3),
60 } dflags;
62 typedef enum {
63 DMODE_INIT,
64 DMODE_RECORD,
65 DMODE_PLAY
66 } dmodes;
68 #define ast_toggle_flag(it,flag) if(ast_test_flag(it, flag)) ast_clear_flag(it, flag); else ast_set_flag(it, flag)
70 static int play_and_wait(struct ast_channel *chan, char *file, char *digits)
72 int res = -1;
73 if (!ast_streamfile(chan, file, chan->language)) {
74 res = ast_waitstream(chan, digits);
76 return res;
79 static int dictate_exec(struct ast_channel *chan, void *data)
81 char *path = NULL, filein[256], *filename = "";
82 char *parse;
83 AST_DECLARE_APP_ARGS(args,
84 AST_APP_ARG(base);
85 AST_APP_ARG(filename);
87 char dftbase[256];
88 char *base;
89 struct ast_flags flags = {0};
90 struct ast_filestream *fs;
91 struct ast_frame *f = NULL;
92 struct ast_module_user *u;
93 int ffactor = 320 * 80,
94 res = 0,
95 done = 0,
96 oldr = 0,
97 lastop = 0,
98 samples = 0,
99 speed = 1,
100 digit = 0,
101 len = 0,
102 maxlen = 0,
103 mode = 0;
105 u = ast_module_user_add(chan);
107 snprintf(dftbase, sizeof(dftbase), "%s/dictate", ast_config_AST_SPOOL_DIR);
108 if (!ast_strlen_zero(data)) {
109 parse = ast_strdupa(data);
110 AST_STANDARD_APP_ARGS(args, parse);
111 } else
112 args.argc = 0;
114 if (args.argc && !ast_strlen_zero(args.base)) {
115 base = args.base;
116 } else {
117 base = dftbase;
119 if (args.argc > 1 && args.filename) {
120 filename = args.filename;
122 oldr = chan->readformat;
123 if ((res = ast_set_read_format(chan, AST_FORMAT_SLINEAR)) < 0) {
124 ast_log(LOG_WARNING, "Unable to set to linear mode.\n");
125 ast_module_user_remove(u);
126 return -1;
129 ast_answer(chan);
130 ast_safe_sleep(chan, 200);
131 for (res = 0; !res;) {
132 if (ast_strlen_zero(filename)) {
133 if (ast_app_getdata(chan, "dictate/enter_filename", filein, sizeof(filein), 0) ||
134 ast_strlen_zero(filein)) {
135 res = -1;
136 break;
138 } else {
139 ast_copy_string(filein, filename, sizeof(filein));
140 filename = "";
142 mkdir(base, 0755);
143 len = strlen(base) + strlen(filein) + 2;
144 if (!path || len > maxlen) {
145 path = alloca(len);
146 memset(path, 0, len);
147 maxlen = len;
148 } else {
149 memset(path, 0, maxlen);
152 snprintf(path, len, "%s/%s", base, filein);
153 fs = ast_writefile(path, "raw", NULL, O_CREAT|O_APPEND, 0, 0700);
154 mode = DMODE_PLAY;
155 memset(&flags, 0, sizeof(flags));
156 ast_set_flag(&flags, DFLAG_PAUSE);
157 digit = play_and_wait(chan, "dictate/forhelp", AST_DIGIT_ANY);
158 done = 0;
159 speed = 1;
160 res = 0;
161 lastop = 0;
162 samples = 0;
163 while (!done && ((res = ast_waitfor(chan, -1)) > -1) && fs && (f = ast_read(chan))) {
164 if (digit) {
165 struct ast_frame fr = {AST_FRAME_DTMF, digit};
166 ast_queue_frame(chan, &fr);
167 digit = 0;
169 if ((f->frametype == AST_FRAME_DTMF)) {
170 int got = 1;
171 switch(mode) {
172 case DMODE_PLAY:
173 switch(f->subclass) {
174 case '1':
175 ast_set_flag(&flags, DFLAG_PAUSE);
176 mode = DMODE_RECORD;
177 break;
178 case '2':
179 speed++;
180 if (speed > 4) {
181 speed = 1;
183 res = ast_say_number(chan, speed, AST_DIGIT_ANY, chan->language, (char *) NULL);
184 break;
185 case '7':
186 samples -= ffactor;
187 if(samples < 0) {
188 samples = 0;
190 ast_seekstream(fs, samples, SEEK_SET);
191 break;
192 case '8':
193 samples += ffactor;
194 ast_seekstream(fs, samples, SEEK_SET);
195 break;
197 default:
198 got = 0;
200 break;
201 case DMODE_RECORD:
202 switch(f->subclass) {
203 case '1':
204 ast_set_flag(&flags, DFLAG_PAUSE);
205 mode = DMODE_PLAY;
206 break;
207 case '8':
208 ast_toggle_flag(&flags, DFLAG_TRUNC);
209 lastop = 0;
210 break;
211 default:
212 got = 0;
214 break;
215 default:
216 got = 0;
218 if (!got) {
219 switch(f->subclass) {
220 case '#':
221 done = 1;
222 continue;
223 break;
224 case '*':
225 ast_toggle_flag(&flags, DFLAG_PAUSE);
226 if (ast_test_flag(&flags, DFLAG_PAUSE)) {
227 digit = play_and_wait(chan, "dictate/pause", AST_DIGIT_ANY);
228 } else {
229 digit = play_and_wait(chan, mode == DMODE_PLAY ? "dictate/playback" : "dictate/record", AST_DIGIT_ANY);
231 break;
232 case '0':
233 ast_set_flag(&flags, DFLAG_PAUSE);
234 digit = play_and_wait(chan, "dictate/paused", AST_DIGIT_ANY);
235 switch(mode) {
236 case DMODE_PLAY:
237 digit = play_and_wait(chan, "dictate/play_help", AST_DIGIT_ANY);
238 break;
239 case DMODE_RECORD:
240 digit = play_and_wait(chan, "dictate/record_help", AST_DIGIT_ANY);
241 break;
243 if (digit == 0) {
244 digit = play_and_wait(chan, "dictate/both_help", AST_DIGIT_ANY);
245 } else if (digit < 0) {
246 done = 1;
247 break;
249 break;
253 } else if (f->frametype == AST_FRAME_VOICE) {
254 switch(mode) {
255 struct ast_frame *fr;
256 int x;
257 case DMODE_PLAY:
258 if (lastop != DMODE_PLAY) {
259 if (ast_test_flag(&flags, DFLAG_PAUSE)) {
260 digit = play_and_wait(chan, "dictate/playback_mode", AST_DIGIT_ANY);
261 if (digit == 0) {
262 digit = play_and_wait(chan, "dictate/paused", AST_DIGIT_ANY);
263 } else if (digit < 0) {
264 break;
267 if (lastop != DFLAG_PLAY) {
268 lastop = DFLAG_PLAY;
269 ast_closestream(fs);
270 fs = ast_openstream(chan, path, chan->language);
271 ast_seekstream(fs, samples, SEEK_SET);
272 chan->stream = NULL;
274 lastop = DMODE_PLAY;
277 if (!ast_test_flag(&flags, DFLAG_PAUSE)) {
278 for (x = 0; x < speed; x++) {
279 if ((fr = ast_readframe(fs))) {
280 ast_write(chan, fr);
281 samples += fr->samples;
282 ast_frfree(fr);
283 fr = NULL;
284 } else {
285 samples = 0;
286 ast_seekstream(fs, 0, SEEK_SET);
290 break;
291 case DMODE_RECORD:
292 if (lastop != DMODE_RECORD) {
293 int oflags = O_CREAT | O_WRONLY;
294 if (ast_test_flag(&flags, DFLAG_PAUSE)) {
295 digit = play_and_wait(chan, "dictate/record_mode", AST_DIGIT_ANY);
296 if (digit == 0) {
297 digit = play_and_wait(chan, "dictate/paused", AST_DIGIT_ANY);
298 } else if (digit < 0) {
299 break;
302 lastop = DMODE_RECORD;
303 ast_closestream(fs);
304 if ( ast_test_flag(&flags, DFLAG_TRUNC)) {
305 oflags |= O_TRUNC;
306 digit = play_and_wait(chan, "dictate/truncating_audio", AST_DIGIT_ANY);
307 } else {
308 oflags |= O_APPEND;
310 fs = ast_writefile(path, "raw", NULL, oflags, 0, 0700);
311 if (ast_test_flag(&flags, DFLAG_TRUNC)) {
312 ast_seekstream(fs, 0, SEEK_SET);
313 ast_clear_flag(&flags, DFLAG_TRUNC);
314 } else {
315 ast_seekstream(fs, 0, SEEK_END);
318 if (!ast_test_flag(&flags, DFLAG_PAUSE)) {
319 res = ast_writestream(fs, f);
321 break;
326 ast_frfree(f);
329 if (oldr) {
330 ast_set_read_format(chan, oldr);
332 ast_module_user_remove(u);
333 return res;
336 static int unload_module(void)
338 int res;
339 res = ast_unregister_application(app);
340 return res;
343 static int load_module(void)
345 return ast_register_application(app, dictate_exec, synopsis, desc);
348 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Virtual Dictation Machine");