Fix 'd' option for app_dial and add new option to Answer application
[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 "asterisk.h"
31
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
33
34 #include <sys/stat.h>
35
36 #include "asterisk/paths.h" /* use ast_config_AST_SPOOL_DIR */
37 #include "asterisk/file.h"
38 #include "asterisk/pbx.h"
39 #include "asterisk/module.h"
40 #include "asterisk/say.h"
41 #include "asterisk/app.h"
42
43 /*** DOCUMENTATION
44         <application name="Dictate" language="en_US">
45                 <synopsis>
46                         Virtual Dictation Machine.
47                 </synopsis>
48                 <syntax>
49                         <parameter name="base_dir" />
50                         <parameter name="filename" />
51                 </syntax>
52                 <description>
53                         <para>Start dictation machine using optional <replaceable>base_dir</replaceable> for files.</para>
54                 </description>
55         </application>
56  ***/
57
58 static char *app = "Dictate";
59
60 typedef enum {
61         DFLAG_RECORD = (1 << 0),
62         DFLAG_PLAY = (1 << 1),
63         DFLAG_TRUNC = (1 << 2),
64         DFLAG_PAUSE = (1 << 3),
65 } dflags;
66
67 typedef enum {
68         DMODE_INIT,
69         DMODE_RECORD,
70         DMODE_PLAY
71 } dmodes;
72
73 #define ast_toggle_flag(it,flag) if(ast_test_flag(it, flag)) ast_clear_flag(it, flag); else ast_set_flag(it, flag)
74
75 static int play_and_wait(struct ast_channel *chan, char *file, char *digits)
76 {
77         int res = -1;
78         if (!ast_streamfile(chan, file, chan->language)) {
79                 res = ast_waitstream(chan, digits);
80         }
81         return res;
82 }
83
84 static int dictate_exec(struct ast_channel *chan, void *data)
85 {
86         char *path = NULL, filein[256], *filename = "";
87         char *parse;
88         AST_DECLARE_APP_ARGS(args,
89                 AST_APP_ARG(base);
90                 AST_APP_ARG(filename);
91         );
92         char dftbase[256];
93         char *base;
94         struct ast_flags flags = {0};
95         struct ast_filestream *fs;
96         struct ast_frame *f = NULL;
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         snprintf(dftbase, sizeof(dftbase), "%s/dictate", ast_config_AST_SPOOL_DIR);
110         if (!ast_strlen_zero(data)) {
111                 parse = ast_strdupa(data);
112                 AST_STANDARD_APP_ARGS(args, parse);
113         } else
114                 args.argc = 0;
115
116         if (args.argc && !ast_strlen_zero(args.base)) {
117                 base = args.base;
118         } else {
119                 base = dftbase;
120         }
121         if (args.argc > 1 && args.filename) {
122                 filename = args.filename;
123         }
124         oldr = chan->readformat;
125         if ((res = ast_set_read_format(chan, AST_FORMAT_SLINEAR)) < 0) {
126                 ast_log(LOG_WARNING, "Unable to set to linear mode.\n");
127                 return -1;
128         }
129
130         if (chan->_state != AST_STATE_UP) {
131                 ast_answer(chan);
132         }
133         ast_safe_sleep(chan, 200);
134         for (res = 0; !res;) {
135                 if (ast_strlen_zero(filename)) {
136                         if (ast_app_getdata(chan, "dictate/enter_filename", filein, sizeof(filein), 0) ||
137                                 ast_strlen_zero(filein)) {
138                                 res = -1;
139                                 break;
140                         }
141                 } else {
142                         ast_copy_string(filein, filename, sizeof(filein));
143                         filename = "";
144                 }
145                 ast_mkdir(base, 0755);
146                 len = strlen(base) + strlen(filein) + 2;
147                 if (!path || len > maxlen) {
148                         path = alloca(len);
149                         memset(path, 0, len);
150                         maxlen = len;
151                 } else {
152                         memset(path, 0, maxlen);
153                 }
154
155                 snprintf(path, len, "%s/%s", base, filein);
156                 fs = ast_writefile(path, "raw", NULL, O_CREAT|O_APPEND, 0, AST_FILE_MODE);
157                 mode = DMODE_PLAY;
158                 memset(&flags, 0, sizeof(flags));
159                 ast_set_flag(&flags, DFLAG_PAUSE);
160                 digit = play_and_wait(chan, "dictate/forhelp", AST_DIGIT_ANY);
161                 done = 0;
162                 speed = 1;
163                 res = 0;
164                 lastop = 0;
165                 samples = 0;
166                 while (!done && ((res = ast_waitfor(chan, -1)) > -1) && fs && (f = ast_read(chan))) {
167                         if (digit) {
168                                 struct ast_frame fr = {AST_FRAME_DTMF, digit};
169                                 ast_queue_frame(chan, &fr);
170                                 digit = 0;
171                         }
172                         if ((f->frametype == AST_FRAME_DTMF)) {
173                                 int got = 1;
174                                 switch(mode) {
175                                 case DMODE_PLAY:
176                                         switch(f->subclass) {
177                                         case '1':
178                                                 ast_set_flag(&flags, DFLAG_PAUSE);
179                                                 mode = DMODE_RECORD;
180                                                 break;
181                                         case '2':
182                                                 speed++;
183                                                 if (speed > 4) {
184                                                         speed = 1;
185                                                 }
186                                                 res = ast_say_number(chan, speed, AST_DIGIT_ANY, chan->language, NULL);
187                                                 break;
188                                         case '7':
189                                                 samples -= ffactor;
190                                                 if(samples < 0) {
191                                                         samples = 0;
192                                                 }
193                                                 ast_seekstream(fs, samples, SEEK_SET);
194                                                 break;
195                                         case '8':
196                                                 samples += ffactor;
197                                                 ast_seekstream(fs, samples, SEEK_SET);
198                                                 break;
199                                                 
200                                         default:
201                                                 got = 0;
202                                         }
203                                         break;
204                                 case DMODE_RECORD:
205                                         switch(f->subclass) {
206                                         case '1':
207                                                 ast_set_flag(&flags, DFLAG_PAUSE);
208                                                 mode = DMODE_PLAY;
209                                                 break;
210                                         case '8':
211                                                 ast_toggle_flag(&flags, DFLAG_TRUNC);
212                                                 lastop = 0;
213                                                 break;
214                                         default:
215                                                 got = 0;
216                                         }
217                                         break;
218                                 default:
219                                         got = 0;
220                                 }
221                                 if (!got) {
222                                         switch(f->subclass) {
223                                         case '#':
224                                                 done = 1;
225                                                 continue;
226                                                 break;
227                                         case '*':
228                                                 ast_toggle_flag(&flags, DFLAG_PAUSE);
229                                                 if (ast_test_flag(&flags, DFLAG_PAUSE)) {
230                                                         digit = play_and_wait(chan, "dictate/pause", AST_DIGIT_ANY);
231                                                 } else {
232                                                         digit = play_and_wait(chan, mode == DMODE_PLAY ? "dictate/playback" : "dictate/record", AST_DIGIT_ANY);
233                                                 }
234                                                 break;
235                                         case '0':
236                                                 ast_set_flag(&flags, DFLAG_PAUSE);
237                                                 digit = play_and_wait(chan, "dictate/paused", AST_DIGIT_ANY);
238                                                 switch(mode) {
239                                                 case DMODE_PLAY:
240                                                         digit = play_and_wait(chan, "dictate/play_help", AST_DIGIT_ANY);
241                                                         break;
242                                                 case DMODE_RECORD:
243                                                         digit = play_and_wait(chan, "dictate/record_help", AST_DIGIT_ANY);
244                                                         break;
245                                                 }
246                                                 if (digit == 0) {
247                                                         digit = play_and_wait(chan, "dictate/both_help", AST_DIGIT_ANY);
248                                                 } else if (digit < 0) {
249                                                         done = 1;
250                                                         break;
251                                                 }
252                                                 break;
253                                         }
254                                 }
255                                 
256                         } else if (f->frametype == AST_FRAME_VOICE) {
257                                 switch(mode) {
258                                         struct ast_frame *fr;
259                                         int x;
260                                 case DMODE_PLAY:
261                                         if (lastop != DMODE_PLAY) {
262                                                 if (ast_test_flag(&flags, DFLAG_PAUSE)) {
263                                                         digit = play_and_wait(chan, "dictate/playback_mode", AST_DIGIT_ANY);
264                                                         if (digit == 0) {
265                                                                 digit = play_and_wait(chan, "dictate/paused", AST_DIGIT_ANY);
266                                                         } else if (digit < 0) {
267                                                                 break;
268                                                         }
269                                                 }
270                                                 if (lastop != DFLAG_PLAY) {
271                                                         lastop = DFLAG_PLAY;
272                                                         ast_closestream(fs);
273                                                         if (!(fs = ast_openstream(chan, path, chan->language)))
274                                                                 break;
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, AST_FILE_MODE);
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         return 0;
337 }
338
339 static int unload_module(void)
340 {
341         int res;
342         res = ast_unregister_application(app);
343         return res;
344 }
345
346 static int load_module(void)
347 {
348         return ast_register_application_xml(app, dictate_exec);
349 }
350
351 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Virtual Dictation Machine");