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