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