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