convert a bunch of apps to use ast_goto_if_exists() (issue #5138)
[asterisk/asterisk.git] / apps / app_playback.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Trivial application to playback a sound file
5  * 
6  * Copyright (C) 1999-2005, Mark Spencer
7  *
8  * Mark Spencer <markster@digium.com>
9  *
10  * This program is free software, distributed under the terms of
11  * the GNU General Public License
12  */
13  
14 #include <string.h>
15 #include <stdlib.h>
16
17 #include "asterisk.h"
18
19 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
20
21 #include "asterisk/lock.h"
22 #include "asterisk/file.h"
23 #include "asterisk/logger.h"
24 #include "asterisk/channel.h"
25 #include "asterisk/pbx.h"
26 #include "asterisk/module.h"
27 #include "asterisk/translate.h"
28 #include "asterisk/utils.h"
29
30 static char *tdesc = "Sound File Playback Application";
31
32 static char *app = "Playback";
33
34 static char *synopsis = "Play a file";
35
36 static char *descrip = 
37 "  Playback(filename[&filename2...][|option]):  Plays back given filenames (do not put\n"
38 "extension). Options may also be  included following a pipe symbol. The 'skip'\n"
39 "option causes the playback of the message to  be  skipped  if  the  channel\n"
40 "is not in the 'up' state (i.e. it hasn't been  answered  yet. If 'skip' is \n"
41 "specified, the application will return immediately should the channel not be\n"
42 "off hook.  Otherwise, unless 'noanswer' is specified, the channel channel will\n"
43 "be answered before the sound is played. Not all channels support playing\n"
44 "messages while still hook. Returns -1 if the channel was hung up.  If the\n"
45 "file does not exist, will jump to priority n+101 if present.\n";
46
47 STANDARD_LOCAL_USER;
48
49 LOCAL_USER_DECL;
50
51 static int playback_exec(struct ast_channel *chan, void *data)
52 {
53         int res = 0;
54         struct localuser *u;
55         char *tmp = NULL;
56         char *options = NULL;
57         int option_skip=0;
58         int option_noanswer = 0;
59         char *stringp = NULL;
60         char *front = NULL, *back = NULL;
61         if (!data || ast_strlen_zero((char *)data) || !(tmp = ast_strdupa(data))) {
62                 ast_log(LOG_WARNING, "Playback requires an argument (filename)\n");
63                 return -1;
64         }
65         stringp = tmp;
66         strsep(&stringp, "|");
67         options = strsep(&stringp, "|");
68         if (options && !strcasecmp(options, "skip"))
69                 option_skip = 1;
70         if (options && !strcasecmp(options, "noanswer"))
71                 option_noanswer = 1;
72         LOCAL_USER_ADD(u);
73         if (chan->_state != AST_STATE_UP) {
74                 if (option_skip) {
75                         /* At the user's option, skip if the line is not up */
76                         LOCAL_USER_REMOVE(u);
77                         return 0;
78                 } else if (!option_noanswer)
79                         /* Otherwise answer unless we're supposed to send this while on-hook */
80                         res = ast_answer(chan);
81         }
82         if (!res) {
83                 ast_stopstream(chan);
84                 front = tmp;
85                 while (!res && front) {
86                         if ((back = strchr(front, '&'))) {
87                                 *back = '\0';
88                                 back++;
89                         }
90                         res = ast_streamfile(chan, front, chan->language);
91                         if (!res) { 
92                                 res = ast_waitstream(chan, ""); 
93                                 ast_stopstream(chan);
94                         } else {
95                                 ast_log(LOG_WARNING, "ast_streamfile failed on %s for %s\n", chan->name, (char *)data);
96                                 ast_goto_if_exists(chan, chan->context, chan->exten, chan->priority + 101);
97                                 res = 0;
98                         }
99                         front = back;
100                 }
101         }
102         LOCAL_USER_REMOVE(u);
103         return res;
104 }
105
106 int unload_module(void)
107 {
108         STANDARD_HANGUP_LOCALUSERS;
109         return ast_unregister_application(app);
110 }
111
112 int load_module(void)
113 {
114         return ast_register_application(app, playback_exec, synopsis, descrip);
115 }
116
117 char *description(void)
118 {
119         return tdesc;
120 }
121
122 int usecount(void)
123 {
124         int res;
125         STANDARD_USECOUNT(res);
126         return res;
127 }
128
129 char *key()
130 {
131         return ASTERISK_GPL_KEY;
132 }