remove almost all of the checks of the result from ast_strdupa() or alloca().
[asterisk/asterisk.git] / apps / app_dictate.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2005, Anthony Minessale II
5  *
6  * Anthony Minessale II <anthmct@yahoo.com>
7  *
8  * Donated by Sangoma Technologies <http://www.samgoma.com>
9  *
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.
15  *
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.
19  */
20
21 /*! \file
22  *
23  * \brief Virtual Dictation Machine Application For Asterisk
24  *
25  * \author Anthony Minessale II <anthmct@yahoo.com>
26  *
27  * \ingroup applications
28  */
29
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <unistd.h>
34 #include <sys/stat.h>   /* for mkdir */
35
36 #include "asterisk.h"
37
38 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
39
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"
48
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";
53
54 LOCAL_USER_DECL;
55
56 typedef enum {
57         DFLAG_RECORD = (1 << 0),
58         DFLAG_PLAY = (1 << 1),
59         DFLAG_TRUNC = (1 << 2),
60         DFLAG_PAUSE = (1 << 3),
61 } dflags;
62
63 typedef enum {
64         DMODE_INIT,
65         DMODE_RECORD,
66         DMODE_PLAY
67 } dmodes;
68
69 #define ast_toggle_flag(it,flag) if(ast_test_flag(it, flag)) ast_clear_flag(it, flag); else ast_set_flag(it, flag)
70
71 static int play_and_wait(struct ast_channel *chan, char *file, char *digits) 
72 {
73         int res = -1;
74         if (!ast_streamfile(chan, file, chan->language)) {
75                 res = ast_waitstream(chan, digits);
76         }
77         return res;
78 }
79
80 static int dictate_exec(struct ast_channel *chan, void *data)
81 {
82         char *path = NULL, filein[256], *filename = "";
83         char *parse;
84         AST_DECLARE_APP_ARGS(args,
85                 AST_APP_ARG(base);
86                 AST_APP_ARG(filename);
87         );
88         char dftbase[256];
89         char *base;
90         struct ast_flags flags = {0};
91         struct ast_filestream *fs;
92         struct ast_frame *f = NULL;
93         struct localuser *u;
94         int ffactor = 320 * 80,
95                 res = 0,
96                 done = 0,
97                 oldr = 0,
98                 lastop = 0,
99                 samples = 0,
100                 speed = 1,
101                 digit = 0,
102                 len = 0,
103                 maxlen = 0,
104                 mode = 0;
105                 
106         LOCAL_USER_ADD(u);
107         
108         snprintf(dftbase, sizeof(dftbase), "%s/dictate", ast_config_AST_SPOOL_DIR);
109         if (!ast_strlen_zero(data)) {
110                 parse = ast_strdupa(data);
111                 AST_STANDARD_APP_ARGS(args, parse);
112         } else
113                 args.argc = 0;
114         
115         if (args.argc && !ast_strlen_zero(args.base)) {
116                 base = args.base;
117         } else {
118                 base = dftbase;
119         }
120         if (args.argc > 1 && args.filename) {
121                 filename = args.filename;
122         } 
123         oldr = chan->readformat;
124         if ((res = ast_set_read_format(chan, AST_FORMAT_SLINEAR)) < 0) {
125                 ast_log(LOG_WARNING, "Unable to set to linear mode.\n");
126                 LOCAL_USER_REMOVE(u);
127                 return -1;
128         }
129
130         ast_answer(chan);
131         ast_safe_sleep(chan, 200);
132         for (res = 0; !res;) {
133                 if (ast_strlen_zero(filename)) {
134                         if (ast_app_getdata(chan, "dictate/enter_filename", filein, sizeof(filein), 0) || 
135                                 ast_strlen_zero(filein)) {
136                                 res = -1;
137                                 break;
138                         }
139                 } else {
140                         ast_copy_string(filein, filename, sizeof(filein));
141                         filename = "";
142                 }
143                 mkdir(base, 0755);
144                 len = strlen(base) + strlen(filein) + 2;
145                 if (!path || len > maxlen) {
146                         path = alloca(len);
147                         memset(path, 0, len);
148                         maxlen = len;
149                 } else {
150                         memset(path, 0, maxlen);
151                 }
152
153                 snprintf(path, len, "%s/%s", base, filein);
154                 fs = ast_writefile(path, "raw", NULL, O_CREAT|O_APPEND, 0, 0700);
155                 mode = DMODE_PLAY;
156                 memset(&flags, 0, sizeof(flags));
157                 ast_set_flag(&flags, DFLAG_PAUSE);
158                 digit = play_and_wait(chan, "dictate/forhelp", AST_DIGIT_ANY);
159                 done = 0;
160                 speed = 1;
161                 res = 0;
162                 lastop = 0;
163                 samples = 0;
164                 while (!done && ((res = ast_waitfor(chan, -1)) > -1) && fs && (f = ast_read(chan))) {
165                         if (digit) {
166                                 struct ast_frame fr = {AST_FRAME_DTMF, digit};
167                                 ast_queue_frame(chan, &fr);
168                                 digit = 0;
169                         }
170                         if ((f->frametype == AST_FRAME_DTMF)) {
171                                 int got = 1;
172                                 switch(mode) {
173                                 case DMODE_PLAY:
174                                         switch(f->subclass) {
175                                         case '1':
176                                                 ast_set_flag(&flags, DFLAG_PAUSE);
177                                                 mode = DMODE_RECORD;
178                                                 break;
179                                         case '2':
180                                                 speed++;
181                                                 if (speed > 4) {
182                                                         speed = 1;
183                                                 }
184                                                 res = ast_say_number(chan, speed, AST_DIGIT_ANY, chan->language, (char *) NULL);
185                                                 break;
186                                         case '7':
187                                                 samples -= ffactor;
188                                                 if(samples < 0) {
189                                                         samples = 0;
190                                                 }
191                                                 ast_seekstream(fs, samples, SEEK_SET);
192                                                 break;
193                                         case '8':
194                                                 samples += ffactor;
195                                                 ast_seekstream(fs, samples, SEEK_SET);
196                                                 break;
197                                                 
198                                         default:
199                                                 got = 0;
200                                         }
201                                         break;
202                                 case DMODE_RECORD:
203                                         switch(f->subclass) {
204                                         case '1':
205                                                 ast_set_flag(&flags, DFLAG_PAUSE);
206                                                 mode = DMODE_PLAY;
207                                                 break;
208                                         case '8':
209                                                 ast_toggle_flag(&flags, DFLAG_TRUNC);
210                                                 lastop = 0;
211                                                 break;
212                                         default:
213                                                 got = 0;
214                                         }
215                                         break;
216                                 default:
217                                         got = 0;
218                                 }
219                                 if (!got) {
220                                         switch(f->subclass) {
221                                         case '#':
222                                                 done = 1;
223                                                 continue;
224                                                 break;
225                                         case '*':
226                                                 ast_toggle_flag(&flags, DFLAG_PAUSE);
227                                                 if (ast_test_flag(&flags, DFLAG_PAUSE)) {
228                                                         digit = play_and_wait(chan, "dictate/pause", AST_DIGIT_ANY);
229                                                 } else {
230                                                         digit = play_and_wait(chan, mode == DMODE_PLAY ? "dictate/playback" : "dictate/record", AST_DIGIT_ANY);
231                                                 }
232                                                 break;
233                                         case '0':
234                                                 ast_set_flag(&flags, DFLAG_PAUSE);
235                                                 digit = play_and_wait(chan, "dictate/paused", AST_DIGIT_ANY);
236                                                 switch(mode) {
237                                                 case DMODE_PLAY:
238                                                         digit = play_and_wait(chan, "dictate/play_help", AST_DIGIT_ANY);
239                                                         break;
240                                                 case DMODE_RECORD:
241                                                         digit = play_and_wait(chan, "dictate/record_help", AST_DIGIT_ANY);
242                                                         break;
243                                                 }
244                                                 if (digit == 0) {
245                                                         digit = play_and_wait(chan, "dictate/both_help", AST_DIGIT_ANY);
246                                                 } else if (digit < 0) {
247                                                         done = 1;
248                                                         break;
249                                                 }
250                                                 break;
251                                         }
252                                 }
253                                 
254                         } else if (f->frametype == AST_FRAME_VOICE) {
255                                 switch(mode) {
256                                         struct ast_frame *fr;
257                                         int x;
258                                 case DMODE_PLAY:
259                                         if (lastop != DMODE_PLAY) {
260                                                 if (ast_test_flag(&flags, DFLAG_PAUSE)) {
261                                                         digit = play_and_wait(chan, "dictate/playback_mode", AST_DIGIT_ANY);
262                                                         if (digit == 0) {
263                                                                 digit = play_and_wait(chan, "dictate/paused", AST_DIGIT_ANY);
264                                                         } else if (digit < 0) {
265                                                                 break;
266                                                         }
267                                                 }
268                                                 if (lastop != DFLAG_PLAY) {
269                                                         lastop = DFLAG_PLAY;
270                                                         ast_closestream(fs);
271                                                         fs = ast_openstream(chan, path, chan->language);
272                                                         ast_seekstream(fs, samples, SEEK_SET);
273                                                         chan->stream = NULL;
274                                                 }
275                                                 lastop = DMODE_PLAY;
276                                         }
277
278                                         if (!ast_test_flag(&flags, DFLAG_PAUSE)) {
279                                                 for (x = 0; x < speed; x++) {
280                                                         if ((fr = ast_readframe(fs))) {
281                                                                 ast_write(chan, fr);
282                                                                 samples += fr->samples;
283                                                                 ast_frfree(fr);
284                                                                 fr = NULL;
285                                                         } else {
286                                                                 samples = 0;
287                                                                 ast_seekstream(fs, 0, SEEK_SET);
288                                                         }
289                                                 }
290                                         }
291                                         break;
292                                 case DMODE_RECORD:
293                                         if (lastop != DMODE_RECORD) {
294                                                 int oflags = O_CREAT | O_WRONLY;
295                                                 if (ast_test_flag(&flags, DFLAG_PAUSE)) {                                               
296                                                         digit = play_and_wait(chan, "dictate/record_mode", AST_DIGIT_ANY);
297                                                         if (digit == 0) {
298                                                                 digit = play_and_wait(chan, "dictate/paused", AST_DIGIT_ANY);
299                                                         } else if (digit < 0) {
300                                                                 break;
301                                                         }
302                                                 }
303                                                 lastop = DMODE_RECORD;
304                                                 ast_closestream(fs);
305                                                 if ( ast_test_flag(&flags, DFLAG_TRUNC)) {
306                                                         oflags |= O_TRUNC;
307                                                         digit = play_and_wait(chan, "dictate/truncating_audio", AST_DIGIT_ANY);
308                                                 } else {
309                                                         oflags |= O_APPEND;
310                                                 }
311                                                 fs = ast_writefile(path, "raw", NULL, oflags, 0, 0700);
312                                                 if (ast_test_flag(&flags, DFLAG_TRUNC)) {
313                                                         ast_seekstream(fs, 0, SEEK_SET);
314                                                         ast_clear_flag(&flags, DFLAG_TRUNC);
315                                                 } else {
316                                                         ast_seekstream(fs, 0, SEEK_END);
317                                                 }
318                                         }
319                                         if (!ast_test_flag(&flags, DFLAG_PAUSE)) {
320                                                 res = ast_writestream(fs, f);
321                                         }
322                                         break;
323                                 }
324                                 
325                         }
326
327                         ast_frfree(f);
328                 }
329         }
330         if (oldr) {
331                 ast_set_read_format(chan, oldr);
332         }
333         LOCAL_USER_REMOVE(u);
334         return res;
335 }
336
337 static int unload_module(void *mod)
338 {
339         int res;
340         res = ast_unregister_application(app);
341         return res;
342 }
343
344 static int load_module(void *mod)
345 {
346         __mod_desc = mod;
347         return ast_register_application(app, dictate_exec, synopsis, desc);
348 }
349
350 static const char *description(void)
351 {
352         return "Virtual Dictation Machine";
353 }
354
355 static const char *key(void)
356 {
357         return ASTERISK_GPL_KEY;
358 }
359
360 STD_MOD(MOD_1, NULL, NULL, NULL);