make app_queue 1.2 jump compliant (issue #5580)
[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(filename[|skipms[|ffchar[|rewchar[|stopchar[|pausechar[|restartchar[|option]]]]]]]):\n"
52 "  Plays back a given filename (do not put extension). Options may also\n"
53 "  be included following a pipe symbol.  You can use * and # to rewind and\n"
54 "  fast forward the playback specified. If 'stopchar' is added the file will\n"
55 "  terminate playback when 'stopchar' is pressed. If 'restartchar' is added, the file\n"
56 "  will restart when 'restartchar' is pressed.\n"
57 "  The option string may contain the following character:\n"
58 "       'j' -- jump to +101 priority if the file requested isn't found.\n"
59 "  This application sets the following channel variable upon completion:\n"
60 "     CPLAYBACKSTATUS       The status of the attempt as a text string, one of\n"
61 "             SUCCESS | USERSTOPPED | ERROR\n"
62 "  Example:  exten => 1234,1,ControlPlayback(file|4000|*|#|1|0|5)\n\n";
63
64 STANDARD_LOCAL_USER;
65
66 LOCAL_USER_DECL;
67
68 static int is_on_phonepad(char key)
69 {
70         return key == 35 || key == 42 || (key >= 48 && key <= 57);
71 }
72
73 static int controlplayback_exec(struct ast_channel *chan, void *data)
74 {
75         int res = 0, priority_jump = 0;
76         int skipms = 0;
77         struct localuser *u;
78         char *tmp;
79         int argc;
80         char *argv[8];
81         enum arg_ids {
82                 arg_file = 0,
83                 arg_skip = 1,
84                 arg_fwd = 2,
85                 arg_rev = 3,
86                 arg_stop = 4,
87                 arg_pause = 5,
88                 arg_restart = 6,
89                 options = 7,
90         };
91         
92         if (ast_strlen_zero(data)) {
93                 ast_log(LOG_WARNING, "ControlPlayback requires an argument (filename)\n");
94                 return -1;
95         }
96
97         LOCAL_USER_ADD(u);
98         
99         tmp = ast_strdupa(data);
100         memset(argv, 0, sizeof(argv));
101
102         argc = ast_app_separate_args(tmp, '|', argv, sizeof(argv) / sizeof(argv[0]));
103
104         if (argc < 1) {
105                 ast_log(LOG_WARNING, "ControlPlayback requires an argument (filename)\n");
106                 LOCAL_USER_REMOVE(u);
107                 return -1;
108         }
109
110         skipms = argv[arg_skip] ? atoi(argv[arg_skip]) : 3000;
111         if (!skipms)
112                 skipms = 3000;
113
114         if (!argv[arg_fwd] || !is_on_phonepad(*argv[arg_fwd]))
115                 argv[arg_fwd] = "#";
116         if (!argv[arg_rev] || !is_on_phonepad(*argv[arg_rev]))
117                 argv[arg_rev] = "*";
118         if (argv[arg_stop] && !is_on_phonepad(*argv[arg_stop]))
119                 argv[arg_stop] = NULL;
120         if (argv[arg_pause] && !is_on_phonepad(*argv[arg_pause]))
121                 argv[arg_pause] = NULL;
122         if (argv[arg_restart] && !is_on_phonepad(*argv[arg_restart]))
123                 argv[arg_restart] = NULL;
124
125         if (argv[options]) {
126                 if (strchr(argv[options], 'j'))
127                         priority_jump = 1;
128         }
129
130         res = ast_control_streamfile(chan, argv[arg_file], argv[arg_fwd], argv[arg_rev], argv[arg_stop], argv[arg_pause], argv[arg_restart], skipms);
131
132         /* If we stopped on one of our stop keys, return 0  */
133         if (argv[arg_stop] && strchr(argv[arg_stop], res)) {
134                 res = 0;
135                 pbx_builtin_setvar_helper(chan, "CPLAYBACKSTATUS", "USERSTOPPED");
136         } else {
137                 if (res < 0) {
138                         if (priority_jump || option_priority_jumping) {
139                                 if (!ast_goto_if_exists(chan, chan->context, chan->exten, chan->priority + 101)) {
140                                         ast_log(LOG_WARNING, "ControlPlayback tried to jump to priority n+101 as requested, but priority didn't exist\n");
141                                 }
142                         }
143                         res = 0;
144                         pbx_builtin_setvar_helper(chan, "CPLAYBACKSTATUS", "ERROR");
145                 } else
146                         pbx_builtin_setvar_helper(chan, "CPLAYBACKSTATUS", "SUCCESS");
147         }
148
149         LOCAL_USER_REMOVE(u);
150
151         return res;
152 }
153
154 int unload_module(void)
155 {
156         int res;
157
158         res = ast_unregister_application(app);
159
160         STANDARD_HANGUP_LOCALUSERS;
161
162         return res;
163 }
164
165 int load_module(void)
166 {
167         return ast_register_application(app, controlplayback_exec, synopsis, descrip);
168 }
169
170 char *description(void)
171 {
172         return (char *) tdesc;
173 }
174
175 int usecount(void)
176 {
177         int res;
178
179         STANDARD_USECOUNT(res);
180         return res;
181 }
182
183 char *key()
184 {
185         return ASTERISK_GPL_KEY;
186 }