This rather large commit changes the way modules are loaded.
[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                 if (!(parse = ast_strdupa(data)))
111                         return -1;
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                 LOCAL_USER_REMOVE(u);
128                 return -1;
129         }
130
131         ast_answer(chan);
132         ast_safe_sleep(chan, 200);
133         for (res = 0; !res;) {
134                 if (ast_strlen_zero(filename)) {
135                         if (ast_app_getdata(chan, "dictate/enter_filename", filein, sizeof(filein), 0) || 
136                                 ast_strlen_zero(filein)) {
137                                 res = -1;
138                                 break;
139                         }
140                 } else {
141                         ast_copy_string(filein, filename, sizeof(filein));
142                         filename = "";
143                 }
144                 mkdir(base, 0755);
145                 len = strlen(base) + strlen(filein) + 2;
146                 if (!path || len > maxlen) {
147                         path = alloca(len);
148                         memset(path, 0, len);
149                         maxlen = len;
150                 } else {
151                         memset(path, 0, maxlen);
152                 }
153
154                 snprintf(path, len, "%s/%s", base, filein);
155                 fs = ast_writefile(path, "raw", NULL, O_CREAT|O_APPEND, 0, 0700);
156                 mode = DMODE_PLAY;
157                 memset(&flags, 0, sizeof(flags));
158                 ast_set_flag(&flags, DFLAG_PAUSE);
159                 digit = play_and_wait(chan, "dictate/forhelp", AST_DIGIT_ANY);
160                 done = 0;
161                 speed = 1;
162                 res = 0;
163                 lastop = 0;
164                 samples = 0;
165                 while (!done && ((res = ast_waitfor(chan, -1)) > -1) && fs && (f = ast_read(chan))) {
166                         if (digit) {
167                                 struct ast_frame fr = {AST_FRAME_DTMF, digit};
168                                 ast_queue_frame(chan, &fr);
169                                 digit = 0;
170                         }
171                         if ((f->frametype == AST_FRAME_DTMF)) {
172                                 int got = 1;
173                                 switch(mode) {
174                                 case DMODE_PLAY:
175                                         switch(f->subclass) {
176                                         case '1':
177                                                 ast_set_flag(&flags, DFLAG_PAUSE);
178                                                 mode = DMODE_RECORD;
179                                                 break;
180                                         case '2':
181                                                 speed++;
182                                                 if (speed > 4) {
183                                                         speed = 1;
184                                                 }
185                                                 res = ast_say_number(chan, speed, AST_DIGIT_ANY, chan->language, (char *) NULL);
186                                                 break;
187                                         case '7':
188                                                 samples -= ffactor;
189                                                 if(samples < 0) {
190                                                         samples = 0;
191                                                 }
192                                                 ast_seekstream(fs, samples, SEEK_SET);
193                                                 break;
194                                         case '8':
195                                                 samples += ffactor;
196                                                 ast_seekstream(fs, samples, SEEK_SET);
197                                                 break;
198                                                 
199                                         default:
200                                                 got = 0;
201                                         }
202                                         break;
203                                 case DMODE_RECORD:
204                                         switch(f->subclass) {
205                                         case '1':
206                                                 ast_set_flag(&flags, DFLAG_PAUSE);
207                                                 mode = DMODE_PLAY;
208                                                 break;
209                                         case '8':
210                                                 ast_toggle_flag(&flags, DFLAG_TRUNC);
211                                                 lastop = 0;
212                                                 break;
213                                         default:
214                                                 got = 0;
215                                         }
216                                         break;
217                                 default:
218                                         got = 0;
219                                 }
220                                 if (!got) {
221                                         switch(f->subclass) {
222                                         case '#':
223                                                 done = 1;
224                                                 continue;
225                                                 break;
226                                         case '*':
227                                                 ast_toggle_flag(&flags, DFLAG_PAUSE);
228                                                 if (ast_test_flag(&flags, DFLAG_PAUSE)) {
229                                                         digit = play_and_wait(chan, "dictate/pause", AST_DIGIT_ANY);
230                                                 } else {
231                                                         digit = play_and_wait(chan, mode == DMODE_PLAY ? "dictate/playback" : "dictate/record", AST_DIGIT_ANY);
232                                                 }
233                                                 break;
234                                         case '0':
235                                                 ast_set_flag(&flags, DFLAG_PAUSE);
236                                                 digit = play_and_wait(chan, "dictate/paused", AST_DIGIT_ANY);
237                                                 switch(mode) {
238                                                 case DMODE_PLAY:
239                                                         digit = play_and_wait(chan, "dictate/play_help", AST_DIGIT_ANY);
240                                                         break;
241                                                 case DMODE_RECORD:
242                                                         digit = play_and_wait(chan, "dictate/record_help", AST_DIGIT_ANY);
243                                                         break;
244                                                 }
245                                                 if (digit == 0) {
246                                                         digit = play_and_wait(chan, "dictate/both_help", AST_DIGIT_ANY);
247                                                 } else if (digit < 0) {
248                                                         done = 1;
249                                                         break;
250                                                 }
251                                                 break;
252                                         }
253                                 }
254                                 
255                         } else if (f->frametype == AST_FRAME_VOICE) {
256                                 switch(mode) {
257                                         struct ast_frame *fr;
258                                         int x;
259                                 case DMODE_PLAY:
260                                         if (lastop != DMODE_PLAY) {
261                                                 if (ast_test_flag(&flags, DFLAG_PAUSE)) {
262                                                         digit = play_and_wait(chan, "dictate/playback_mode", AST_DIGIT_ANY);
263                                                         if (digit == 0) {
264                                                                 digit = play_and_wait(chan, "dictate/paused", AST_DIGIT_ANY);
265                                                         } else if (digit < 0) {
266                                                                 break;
267                                                         }
268                                                 }
269                                                 if (lastop != DFLAG_PLAY) {
270                                                         lastop = DFLAG_PLAY;
271                                                         ast_closestream(fs);
272                                                         fs = ast_openstream(chan, path, chan->language);
273                                                         ast_seekstream(fs, samples, SEEK_SET);
274                                                         chan->stream = NULL;
275                                                 }
276                                                 lastop = DMODE_PLAY;
277                                         }
278
279                                         if (!ast_test_flag(&flags, DFLAG_PAUSE)) {
280                                                 for (x = 0; x < speed; x++) {
281                                                         if ((fr = ast_readframe(fs))) {
282                                                                 ast_write(chan, fr);
283                                                                 samples += fr->samples;
284                                                                 ast_frfree(fr);
285                                                                 fr = NULL;
286                                                         } else {
287                                                                 samples = 0;
288                                                                 ast_seekstream(fs, 0, SEEK_SET);
289                                                         }
290                                                 }
291                                         }
292                                         break;
293                                 case DMODE_RECORD:
294                                         if (lastop != DMODE_RECORD) {
295                                                 int oflags = O_CREAT | O_WRONLY;
296                                                 if (ast_test_flag(&flags, DFLAG_PAUSE)) {                                               
297                                                         digit = play_and_wait(chan, "dictate/record_mode", AST_DIGIT_ANY);
298                                                         if (digit == 0) {
299                                                                 digit = play_and_wait(chan, "dictate/paused", AST_DIGIT_ANY);
300                                                         } else if (digit < 0) {
301                                                                 break;
302                                                         }
303                                                 }
304                                                 lastop = DMODE_RECORD;
305                                                 ast_closestream(fs);
306                                                 if ( ast_test_flag(&flags, DFLAG_TRUNC)) {
307                                                         oflags |= O_TRUNC;
308                                                         digit = play_and_wait(chan, "dictate/truncating_audio", AST_DIGIT_ANY);
309                                                 } else {
310                                                         oflags |= O_APPEND;
311                                                 }
312                                                 fs = ast_writefile(path, "raw", NULL, oflags, 0, 0700);
313                                                 if (ast_test_flag(&flags, DFLAG_TRUNC)) {
314                                                         ast_seekstream(fs, 0, SEEK_SET);
315                                                         ast_clear_flag(&flags, DFLAG_TRUNC);
316                                                 } else {
317                                                         ast_seekstream(fs, 0, SEEK_END);
318                                                 }
319                                         }
320                                         if (!ast_test_flag(&flags, DFLAG_PAUSE)) {
321                                                 res = ast_writestream(fs, f);
322                                         }
323                                         break;
324                                 }
325                                 
326                         }
327
328                         ast_frfree(f);
329                 }
330         }
331         if (oldr) {
332                 ast_set_read_format(chan, oldr);
333         }
334         LOCAL_USER_REMOVE(u);
335         return res;
336 }
337
338 static int unload_module(void *mod)
339 {
340         int res;
341         res = ast_unregister_application(app);
342         return res;
343 }
344
345 static int load_module(void *mod)
346 {
347         __mod_desc = mod;
348         return ast_register_application(app, dictate_exec, synopsis, desc);
349 }
350
351 static const char *description(void)
352 {
353         return "Virtual Dictation Machine";
354 }
355
356 static const char *key(void)
357 {
358         return ASTERISK_GPL_KEY;
359 }
360
361 STD_MOD(MOD_1, NULL, NULL, NULL);