since the module API is changing, it's a good time to const-ify the description(...
[asterisk/asterisk.git] / apps / app_controlplayback.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2005, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*! \file
20  * 
21  * \brief Trivial application to control playback of a sound file
22  *
23  * \author Mark Spencer <markster@digium.com>
24  * 
25  * \ingroup applications
26  */
27  
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <string.h>
31
32 #include "asterisk.h"
33
34 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
35
36 #include "asterisk/lock.h"
37 #include "asterisk/file.h"
38 #include "asterisk/logger.h"
39 #include "asterisk/channel.h"
40 #include "asterisk/pbx.h"
41 #include "asterisk/app.h"
42 #include "asterisk/module.h"
43 #include "asterisk/translate.h"
44 #include "asterisk/utils.h"
45 #include "asterisk/options.h"
46
47 static const char *tdesc = "Control Playback Application";
48
49 static const char *app = "ControlPlayback";
50
51 static const char *synopsis = "Play a file with fast forward and rewind";
52
53 static const char *descrip = 
54 "  ControlPlayback(file[|skipms[|ff[|rew[|stop[|pause[|restart|options]]]]]]]):\n"
55 "This application will play back the given filename. By default, the '*' key\n"
56 "can be used to rewind, and the '#' key can be used to fast-forward.\n"
57 "Parameters:\n"
58 "  skipms  - This is number of milliseconds to skip when rewinding or\n"
59 "            fast-forwarding.\n"
60 "  ff      - Fast-forward when this DTMF digit is received.\n"
61 "  rew     - Rewind when this DTMF digit is received.\n"
62 "  stop    - Stop playback when this DTMF digit is received.\n"
63 "  pause   - Pause playback when this DTMF digit is received.\n"
64 "  restart - Restart playback when this DTMF digit is received.\n"
65 "Options:\n"
66 "  j - Jump to priority n+101 if the requested file is not found.\n"
67 "This application sets the following channel variable upon completion:\n"
68 "  CPLAYBACKSTATUS -  This variable contains the status of the attempt as a text\n"
69 "                     string, one of: SUCCESS | USERSTOPPED | ERROR\n";
70
71 LOCAL_USER_DECL;
72
73 static int is_on_phonepad(char key)
74 {
75         return key == 35 || key == 42 || (key >= 48 && key <= 57);
76 }
77
78 static int controlplayback_exec(struct ast_channel *chan, void *data)
79 {
80         int res = 0, priority_jump = 0;
81         int skipms = 0;
82         struct localuser *u;
83         char *tmp;
84         int argc;
85         char *argv[8];
86         enum arg_ids {
87                 arg_file = 0,
88                 arg_skip = 1,
89                 arg_fwd = 2,
90                 arg_rev = 3,
91                 arg_stop = 4,
92                 arg_pause = 5,
93                 arg_restart = 6,
94                 options = 7,
95         };
96         
97         if (ast_strlen_zero(data)) {
98                 ast_log(LOG_WARNING, "ControlPlayback requires an argument (filename)\n");
99                 return -1;
100         }
101
102         LOCAL_USER_ADD(u);
103         
104         tmp = ast_strdupa(data);
105         memset(argv, 0, sizeof(argv));
106
107         argc = ast_app_separate_args(tmp, '|', argv, sizeof(argv) / sizeof(argv[0]));
108
109         if (argc < 1) {
110                 ast_log(LOG_WARNING, "ControlPlayback requires an argument (filename)\n");
111                 LOCAL_USER_REMOVE(u);
112                 return -1;
113         }
114
115         skipms = argv[arg_skip] ? atoi(argv[arg_skip]) : 3000;
116         if (!skipms)
117                 skipms = 3000;
118
119         if (!argv[arg_fwd] || !is_on_phonepad(*argv[arg_fwd]))
120                 argv[arg_fwd] = "#";
121         if (!argv[arg_rev] || !is_on_phonepad(*argv[arg_rev]))
122                 argv[arg_rev] = "*";
123         if (argv[arg_stop] && !is_on_phonepad(*argv[arg_stop]))
124                 argv[arg_stop] = NULL;
125         if (argv[arg_pause] && !is_on_phonepad(*argv[arg_pause]))
126                 argv[arg_pause] = NULL;
127         if (argv[arg_restart] && !is_on_phonepad(*argv[arg_restart]))
128                 argv[arg_restart] = NULL;
129
130         if (argv[options]) {
131                 if (strchr(argv[options], 'j'))
132                         priority_jump = 1;
133         }
134
135         res = ast_control_streamfile(chan, argv[arg_file], argv[arg_fwd], argv[arg_rev], argv[arg_stop], argv[arg_pause], argv[arg_restart], skipms);
136
137         /* If we stopped on one of our stop keys, return 0  */
138         if (argv[arg_stop] && strchr(argv[arg_stop], res)) {
139                 res = 0;
140                 pbx_builtin_setvar_helper(chan, "CPLAYBACKSTATUS", "USERSTOPPED");
141         } else {
142                 if (res < 0) {
143                         if (priority_jump || ast_opt_priority_jumping) {
144                                 if (ast_goto_if_exists(chan, chan->context, chan->exten, chan->priority + 101)) {
145                                         ast_log(LOG_WARNING, "ControlPlayback tried to jump to priority n+101 as requested, but priority didn't exist\n");
146                                 }
147                         }
148                         res = 0;
149                         pbx_builtin_setvar_helper(chan, "CPLAYBACKSTATUS", "ERROR");
150                 } else
151                         pbx_builtin_setvar_helper(chan, "CPLAYBACKSTATUS", "SUCCESS");
152         }
153
154         LOCAL_USER_REMOVE(u);
155
156         return res;
157 }
158
159 int unload_module(void)
160 {
161         int res;
162
163         res = ast_unregister_application(app);
164
165         STANDARD_HANGUP_LOCALUSERS;
166
167         return res;
168 }
169
170 int load_module(void)
171 {
172         return ast_register_application(app, controlplayback_exec, synopsis, descrip);
173 }
174
175 const char *description(void)
176 {
177         return (char *) tdesc;
178 }
179
180 int usecount(void)
181 {
182         int res;
183
184         STANDARD_USECOUNT(res);
185         return res;
186 }
187
188 const char *key()
189 {
190         return ASTERISK_GPL_KEY;
191 }