(closes issue #12846)
[asterisk-bristuff.git] / apps / app_parkandannounce.c
blob9e9f1604d3ecfddfa9c6ae3e9acfae7a93bd8793
1 /*
2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2006, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
8 * Author: Ben Miller <bgmiller@dccinc.com>
9 * With TONS of help from Mark!
11 * See http://www.asterisk.org for more information about
12 * the Asterisk project. Please do not directly contact
13 * any of the maintainers of this project for assistance;
14 * the project provides a web site, mailing lists and IRC
15 * channels for your use.
17 * This program is free software, distributed under the terms of
18 * the GNU General Public License Version 2. See the LICENSE file
19 * at the top of the source tree.
22 /*! \file
24 * \brief ParkAndAnnounce application for Asterisk
26 * \author Ben Miller <bgmiller@dccinc.com>
27 * \arg With TONS of help from Mark!
29 * \ingroup applications
32 #include "asterisk.h"
34 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
36 #include <stdlib.h>
37 #include <stdio.h>
38 #include <string.h>
39 #include <unistd.h>
40 #include <sys/types.h>
42 #include "asterisk/file.h"
43 #include "asterisk/logger.h"
44 #include "asterisk/channel.h"
45 #include "asterisk/pbx.h"
46 #include "asterisk/module.h"
47 #include "asterisk/features.h"
48 #include "asterisk/options.h"
49 #include "asterisk/logger.h"
50 #include "asterisk/say.h"
51 #include "asterisk/lock.h"
52 #include "asterisk/utils.h"
54 static char *app = "ParkAndAnnounce";
56 static char *synopsis = "Park and Announce";
58 static char *descrip =
59 " ParkAndAnnounce(announce:template|timeout|dial|[return_context]):\n"
60 "Park a call into the parkinglot and announce the call to another channel.\n"
61 "\n"
62 "announce template: Colon-separated list of files to announce. The word PARKED\n"
63 " will be replaced by a say_digits of the extension in which\n"
64 " the call is parked.\n"
65 "timeout: Time in seconds before the call returns into the return\n"
66 " context.\n"
67 "dial: The app_dial style resource to call to make the\n"
68 " announcement. Console/dsp calls the console.\n"
69 "return_context: The goto-style label to jump the call back into after\n"
70 " timeout. Default <priority+1>.\n"
71 "\n"
72 "The variable ${PARKEDAT} will contain the parking extension into which the\n"
73 "call was placed. Use with the Local channel to allow the dialplan to make\n"
74 "use of this information.\n";
77 static int parkandannounce_exec(struct ast_channel *chan, void *data)
79 char *return_context;
80 int lot, timeout = 0, dres;
81 char *working, *context, *exten, *priority, *dial, *dialtech, *dialstr;
82 char *template, *tpl_working, *tpl_current;
83 char *tmp[100];
84 char buf[13];
85 int looptemp = 0,i = 0, res = 0;
86 char *s;
88 struct ast_channel *dchan;
89 struct outgoing_helper oh;
90 int outstate;
92 struct ast_module_user *u;
94 if (ast_strlen_zero(data)) {
95 ast_log(LOG_WARNING, "ParkAndAnnounce requires arguments: (announce:template|timeout|dial|[return_context])\n");
96 return -1;
99 u = ast_module_user_add(chan);
101 s = ast_strdupa(data);
103 template = strsep(&s,"|");
104 if(! template) {
105 ast_log(LOG_WARNING, "PARK: An announce template must be defined\n");
106 ast_module_user_remove(u);
107 return -1;
110 if(s) {
111 timeout = atoi(strsep(&s, "|"));
112 timeout *= 1000;
114 dial = strsep(&s, "|");
115 if(!dial) {
116 ast_log(LOG_WARNING, "PARK: A dial resource must be specified i.e: Console/dsp or Zap/g1/5551212\n");
117 ast_module_user_remove(u);
118 return -1;
119 } else {
120 dialtech = strsep(&dial, "/");
121 dialstr = dial;
122 ast_verbose( VERBOSE_PREFIX_3 "Dial Tech,String: (%s,%s)\n", dialtech,dialstr);
125 return_context = s;
127 if(return_context != NULL) {
128 /* set the return context. Code borrowed from the Goto builtin */
130 working = return_context;
131 context = strsep(&working, "|");
132 exten = strsep(&working, "|");
133 if(!exten) {
134 /* Only a priority in this one */
135 priority = context;
136 exten = NULL;
137 context = NULL;
138 } else {
139 priority = strsep(&working, "|");
140 if(!priority) {
141 /* Only an extension and priority in this one */
142 priority = exten;
143 exten = context;
144 context = NULL;
147 if(atoi(priority) < 0) {
148 ast_log(LOG_WARNING, "Priority '%s' must be a number > 0\n", priority);
149 ast_module_user_remove(u);
150 return -1;
152 /* At this point we have a priority and maybe an extension and a context */
153 chan->priority = atoi(priority);
154 if (exten)
155 ast_copy_string(chan->exten, exten, sizeof(chan->exten));
156 if (context)
157 ast_copy_string(chan->context, context, sizeof(chan->context));
158 } else { /* increment the priority by default*/
159 chan->priority++;
162 if(option_verbose > 2) {
163 ast_verbose( VERBOSE_PREFIX_3 "Return Context: (%s,%s,%d) ID: %s\n", chan->context,chan->exten, chan->priority, chan->cid.cid_num);
164 if(!ast_exists_extension(chan, chan->context, chan->exten, chan->priority, chan->cid.cid_num)) {
165 ast_verbose( VERBOSE_PREFIX_3 "Warning: Return Context Invalid, call will return to default|s\n");
169 /* we are using masq_park here to protect * from touching the channel once we park it. If the channel comes out of timeout
170 before we are done announcing and the channel is messed with, Kablooeee. So we use Masq to prevent this. */
172 res = ast_masq_park_call(chan, NULL, timeout, &lot);
173 if (res == -1) {
174 goto finish;
177 ast_verbose( VERBOSE_PREFIX_3 "Call Parking Called, lot: %d, timeout: %d, context: %s\n", lot, timeout, return_context);
179 /* Now place the call to the extention */
181 snprintf(buf, sizeof(buf), "%d", lot);
182 memset(&oh, 0, sizeof(oh));
183 oh.parent_channel = chan;
184 oh.vars = ast_variable_new("_PARKEDAT", buf);
185 dchan = __ast_request_and_dial(dialtech, AST_FORMAT_SLINEAR, dialstr,30000, &outstate, chan->cid.cid_num, chan->cid.cid_name, &oh);
187 if(dchan) {
188 if(dchan->_state == AST_STATE_UP) {
189 if(option_verbose > 3)
190 ast_verbose(VERBOSE_PREFIX_4 "Channel %s was answered.\n", dchan->name);
191 } else {
192 if(option_verbose > 3)
193 ast_verbose(VERBOSE_PREFIX_4 "Channel %s was never answered.\n", dchan->name);
194 ast_log(LOG_WARNING, "PARK: Channel %s was never answered for the announce.\n", dchan->name);
195 ast_hangup(dchan);
196 ast_module_user_remove(u);
197 return -1;
199 } else {
200 ast_log(LOG_WARNING, "PARK: Unable to allocate announce channel.\n");
201 ast_module_user_remove(u);
202 return -1;
205 ast_stopstream(dchan);
207 /* now we have the call placed and are ready to play stuff to it */
209 ast_verbose(VERBOSE_PREFIX_4 "Announce Template:%s\n", template);
211 tpl_working = template;
212 tpl_current = strsep(&tpl_working, ":");
214 while(tpl_current && looptemp < ARRAY_LEN(tmp)) {
215 tmp[looptemp]=tpl_current;
216 looptemp++;
217 tpl_current = strsep(&tpl_working,":");
220 for(i = 0; i < looptemp; i++) {
221 ast_verbose(VERBOSE_PREFIX_4 "Announce:%s\n", tmp[i]);
222 if(!strcmp(tmp[i], "PARKED")) {
223 ast_say_digits(dchan, lot, "", dchan->language);
224 } else {
225 dres = ast_streamfile(dchan, tmp[i], dchan->language);
226 if(!dres) {
227 dres = ast_waitstream(dchan, "");
228 } else {
229 ast_log(LOG_WARNING, "ast_streamfile of %s failed on %s\n", tmp[i], dchan->name);
230 dres = 0;
235 ast_stopstream(dchan);
236 ast_hangup(dchan);
238 finish:
239 ast_module_user_remove(u);
240 return res;
243 static int unload_module(void)
245 int res;
247 res = ast_unregister_application(app);
249 ast_module_user_hangup_all();
251 return res;
254 static int load_module(void)
256 /* return ast_register_application(app, park_exec); */
257 return ast_register_application(app, parkandannounce_exec, synopsis, descrip);
260 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Call Parking and Announce Application");