| 
									
										
										
										
											2002-12-05 20:13:04 +00:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2005-09-14 20:46:50 +00:00
										 |  |  |  * Asterisk -- An open source telephony toolkit. | 
					
						
							| 
									
										
										
										
											2002-12-05 20:13:04 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2006-04-10 23:01:22 +00:00
										 |  |  |  * Copyright (C) 1999 - 2006, Digium, Inc. | 
					
						
							| 
									
										
										
										
											2002-12-05 20:13:04 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2004-10-02 00:58:31 +00:00
										 |  |  |  * Mark Spencer <markster@digium.com> | 
					
						
							| 
									
										
										
										
											2002-12-05 20:13:04 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2005-09-14 20:46:50 +00:00
										 |  |  |  * Author: Ben Miller <bgmiller@dccinc.com> | 
					
						
							|  |  |  |  *    With TONS of help from Mark! | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * See http://www.asterisk.org for more information about
 | 
					
						
							|  |  |  |  * the Asterisk project. Please do not directly contact | 
					
						
							|  |  |  |  * any of the maintainers of this project for assistance; | 
					
						
							|  |  |  |  * the project provides a web site, mailing lists and IRC | 
					
						
							|  |  |  |  * channels for your use. | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2002-12-05 20:13:04 +00:00
										 |  |  |  * This program is free software, distributed under the terms of | 
					
						
							| 
									
										
										
										
											2005-09-14 20:46:50 +00:00
										 |  |  |  * the GNU General Public License Version 2. See the LICENSE file | 
					
						
							|  |  |  |  * at the top of the source tree. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-10-24 20:12:06 +00:00
										 |  |  | /*! \file
 | 
					
						
							| 
									
										
										
										
											2005-09-14 20:46:50 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2005-10-24 20:12:06 +00:00
										 |  |  |  * \brief ParkAndAnnounce application for Asterisk | 
					
						
							| 
									
										
										
										
											2005-12-30 21:18:06 +00:00
										 |  |  |  * | 
					
						
							|  |  |  |  * \author Ben Miller <bgmiller@dccinc.com> | 
					
						
							|  |  |  |  * \arg With TONS of help from Mark! | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2005-11-06 15:09:47 +00:00
										 |  |  |  * \ingroup applications | 
					
						
							| 
									
										
										
										
											2002-12-05 20:13:04 +00:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-06-07 18:54:56 +00:00
										 |  |  | #include "asterisk.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ASTERISK_FILE_VERSION(__FILE__, "$Revision$") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-04-21 06:02:45 +00:00
										 |  |  | #include "asterisk/file.h"
 | 
					
						
							|  |  |  | #include "asterisk/channel.h"
 | 
					
						
							|  |  |  | #include "asterisk/pbx.h"
 | 
					
						
							|  |  |  | #include "asterisk/module.h"
 | 
					
						
							|  |  |  | #include "asterisk/features.h"
 | 
					
						
							|  |  |  | #include "asterisk/say.h"
 | 
					
						
							|  |  |  | #include "asterisk/lock.h"
 | 
					
						
							| 
									
										
										
										
											2006-01-13 03:34:31 +00:00
										 |  |  | #include "asterisk/utils.h"
 | 
					
						
							| 
									
										
										
										
											2007-07-23 19:51:41 +00:00
										 |  |  | #include "asterisk/app.h"
 | 
					
						
							| 
									
										
										
										
											2002-12-05 20:13:04 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | static char *app = "ParkAndAnnounce"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static char *synopsis = "Park and Announce"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static char *descrip = | 
					
						
							| 
									
										
										
										
											2007-07-31 01:10:47 +00:00
										 |  |  | "  ParkAndAnnounce(announce:template,timeout,dial[,return_context]):\n" | 
					
						
							| 
									
										
										
										
											2006-01-22 07:18:02 +00:00
										 |  |  | "Park a call into the parkinglot and announce the call to another channel.\n" | 
					
						
							|  |  |  | "\n" | 
					
						
							|  |  |  | "announce template: Colon-separated list of files to announce.  The word PARKED\n" | 
					
						
							|  |  |  | "                   will be replaced by a say_digits of the extension in which\n" | 
					
						
							|  |  |  | "                   the call is parked.\n" | 
					
						
							|  |  |  | "timeout:           Time in seconds before the call returns into the return\n" | 
					
						
							|  |  |  | "                   context.\n" | 
					
						
							|  |  |  | "dial:              The app_dial style resource to call to make the\n" | 
					
						
							|  |  |  | "                   announcement.  Console/dsp calls the console.\n" | 
					
						
							|  |  |  | "return_context:    The goto-style label to jump the call back into after\n" | 
					
						
							|  |  |  | "                   timeout.  Default <priority+1>.\n" | 
					
						
							|  |  |  | "\n" | 
					
						
							|  |  |  | "The variable ${PARKEDAT} will contain the parking extension into which the\n" | 
					
						
							|  |  |  | "call was placed.  Use with the Local channel to allow the dialplan to make\n" | 
					
						
							|  |  |  | "use of this information.\n"; | 
					
						
							| 
									
										
										
										
											2002-12-05 20:13:04 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int parkandannounce_exec(struct ast_channel *chan, void *data) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2007-07-23 19:51:41 +00:00
										 |  |  | 	int res = -1; | 
					
						
							| 
									
										
										
										
											2006-10-25 14:44:50 +00:00
										 |  |  | 	int lot, timeout = 0, dres; | 
					
						
							| 
									
										
										
										
											2007-07-23 19:51:41 +00:00
										 |  |  | 	char *dialtech, *tmp[100], buf[13]; | 
					
						
							|  |  |  | 	int looptemp, i; | 
					
						
							| 
									
										
										
										
											2006-10-25 14:44:50 +00:00
										 |  |  | 	char *s; | 
					
						
							| 
									
										
										
										
											2004-05-06 03:20:05 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	struct ast_channel *dchan; | 
					
						
							| 
									
										
										
										
											2007-07-23 19:51:41 +00:00
										 |  |  | 	struct outgoing_helper oh = { 0, }; | 
					
						
							| 
									
										
										
										
											2004-05-06 03:20:05 +00:00
										 |  |  | 	int outstate; | 
					
						
							| 
									
										
										
										
											2007-07-23 19:51:41 +00:00
										 |  |  | 	AST_DECLARE_APP_ARGS(args, | 
					
						
							|  |  |  | 		AST_APP_ARG(template); | 
					
						
							|  |  |  | 		AST_APP_ARG(timeout); | 
					
						
							|  |  |  | 		AST_APP_ARG(dial); | 
					
						
							|  |  |  | 		AST_APP_ARG(return_context); | 
					
						
							|  |  |  | 	); | 
					
						
							| 
									
										
										
										
											2005-10-26 19:48:14 +00:00
										 |  |  | 	if (ast_strlen_zero(data)) { | 
					
						
							| 
									
										
										
										
											2004-05-06 03:20:05 +00:00
										 |  |  | 		ast_log(LOG_WARNING, "ParkAndAnnounce requires arguments: (announce:template|timeout|dial|[return_context])\n"); | 
					
						
							|  |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2002-12-05 20:13:04 +00:00
										 |  |  |    | 
					
						
							| 
									
										
										
										
											2006-10-25 14:44:50 +00:00
										 |  |  | 	s = ast_strdupa(data); | 
					
						
							| 
									
										
										
										
											2007-07-23 19:51:41 +00:00
										 |  |  | 	AST_STANDARD_APP_ARGS(args, s); | 
					
						
							| 
									
										
										
										
											2004-05-06 03:20:05 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-07-23 19:51:41 +00:00
										 |  |  | 	if (args.timeout) | 
					
						
							|  |  |  | 		timeout = atoi(args.timeout) * 1000; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (ast_strlen_zero(args.dial)) { | 
					
						
							| 
									
										
										
										
											2008-07-28 16:42:00 +00:00
										 |  |  | 		ast_log(LOG_WARNING, "PARK: A dial resource must be specified i.e: Console/dsp or DAHDI/g1/5551212\n"); | 
					
						
							| 
									
										
										
										
											2004-05-06 03:20:05 +00:00
										 |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-07-23 19:51:41 +00:00
										 |  |  | 	dialtech = strsep(&args.dial, "/"); | 
					
						
							| 
									
										
										
										
											2007-07-26 15:49:18 +00:00
										 |  |  | 	ast_verb(3, "Dial Tech,String: (%s,%s)\n", dialtech, args.dial); | 
					
						
							| 
									
										
										
										
											2007-07-23 19:51:41 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-22 15:04:44 +00:00
										 |  |  | 	if (!ast_strlen_zero(args.return_context)) { | 
					
						
							|  |  |  | 		ast_clear_flag(chan, AST_FLAG_IN_AUTOLOOP); | 
					
						
							| 
									
										
										
										
											2007-07-23 19:51:41 +00:00
										 |  |  | 		ast_parseable_goto(chan, args.return_context); | 
					
						
							| 
									
										
										
										
											2009-01-22 15:04:44 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2004-05-06 03:20:05 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-07-26 15:49:18 +00:00
										 |  |  | 	ast_verb(3, "Return Context: (%s,%s,%d) ID: %s\n", chan->context, chan->exten, chan->priority, chan->cid.cid_num); | 
					
						
							| 
									
										
										
										
											2007-07-23 19:51:41 +00:00
										 |  |  | 		if (!ast_exists_extension(chan, chan->context, chan->exten, chan->priority, chan->cid.cid_num)) { | 
					
						
							| 
									
										
										
										
											2007-07-26 15:49:18 +00:00
										 |  |  | 		ast_verb(3, "Warning: Return Context Invalid, call will return to default|s\n"); | 
					
						
							| 
									
										
										
										
											2004-05-06 03:20:05 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2007-07-23 19:51:41 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-05-06 03:20:05 +00:00
										 |  |  | 	/* we are using masq_park here to protect * from touching the channel once we park it.  If the channel comes out of timeout
 | 
					
						
							|  |  |  | 	before we are done announcing and the channel is messed with, Kablooeee.  So we use Masq to prevent this.  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-06-12 23:08:37 +00:00
										 |  |  | 	res = ast_masq_park_call(chan, NULL, timeout, &lot); | 
					
						
							|  |  |  | 	if (res == -1) | 
					
						
							|  |  |  | 		return res; | 
					
						
							| 
									
										
										
										
											2004-05-06 03:20:05 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-07-26 15:49:18 +00:00
										 |  |  | 	ast_verb(3, "Call Parking Called, lot: %d, timeout: %d, context: %s\n", lot, timeout, args.return_context); | 
					
						
							| 
									
										
										
										
											2004-05-06 03:20:05 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-07-23 19:51:41 +00:00
										 |  |  | 	/* Now place the call to the extension */ | 
					
						
							| 
									
										
										
										
											2004-05-06 03:20:05 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-01-22 07:18:02 +00:00
										 |  |  | 	snprintf(buf, sizeof(buf), "%d", lot); | 
					
						
							| 
									
										
										
										
											2005-11-01 21:11:57 +00:00
										 |  |  | 	oh.parent_channel = chan; | 
					
						
							| 
									
										
										
										
											2007-08-29 20:55:40 +00:00
										 |  |  | 	oh.vars = ast_variable_new("_PARKEDAT", buf, ""); | 
					
						
							| 
									
										
										
										
											2007-07-23 19:51:41 +00:00
										 |  |  | 	dchan = __ast_request_and_dial(dialtech, AST_FORMAT_SLINEAR, args.dial, 30000, &outstate, chan->cid.cid_num, chan->cid.cid_name, &oh); | 
					
						
							| 
									
										
										
										
											2004-05-06 03:20:05 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-07-23 19:51:41 +00:00
										 |  |  | 	if (dchan) { | 
					
						
							|  |  |  | 		if (dchan->_state == AST_STATE_UP) { | 
					
						
							| 
									
										
										
										
											2007-07-26 15:49:18 +00:00
										 |  |  | 			ast_verb(4, "Channel %s was answered.\n", dchan->name); | 
					
						
							| 
									
										
										
										
											2004-05-06 03:20:05 +00:00
										 |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2007-07-26 15:49:18 +00:00
										 |  |  | 			ast_verb(4, "Channel %s was never answered.\n", dchan->name); | 
					
						
							| 
									
										
										
										
											2008-04-25 20:20:10 +00:00
										 |  |  | 			ast_log(LOG_WARNING, "PARK: Channel %s was never answered for the announce.\n", dchan->name); | 
					
						
							| 
									
										
										
										
											2004-05-06 03:20:05 +00:00
										 |  |  | 			ast_hangup(dchan); | 
					
						
							|  |  |  | 			return -1; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		ast_log(LOG_WARNING, "PARK: Unable to allocate announce channel.\n"); | 
					
						
							|  |  |  | 		return -1;  | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ast_stopstream(dchan); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* now we have the call placed and are ready to play stuff to it */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-07-26 15:49:18 +00:00
										 |  |  | 	ast_verb(4, "Announce Template:%s\n", args.template); | 
					
						
							| 
									
										
										
										
											2004-05-06 03:20:05 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-08 16:40:28 +00:00
										 |  |  | 	for (looptemp = 0; looptemp < ARRAY_LEN(tmp); looptemp++) { | 
					
						
							| 
									
										
										
										
											2008-06-08 01:16:25 +00:00
										 |  |  | 		if ((tmp[looptemp] = strsep(&args.template, ":")) != NULL) | 
					
						
							|  |  |  | 			continue; | 
					
						
							|  |  |  | 		else | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2004-05-06 03:20:05 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-07-23 19:51:41 +00:00
										 |  |  | 	for (i = 0; i < looptemp; i++) { | 
					
						
							| 
									
										
										
										
											2007-07-26 15:49:18 +00:00
										 |  |  | 		ast_verb(4, "Announce:%s\n", tmp[i]); | 
					
						
							| 
									
										
										
										
											2007-07-23 19:51:41 +00:00
										 |  |  | 		if (!strcmp(tmp[i], "PARKED")) { | 
					
						
							| 
									
										
										
										
											2004-05-06 03:20:05 +00:00
										 |  |  | 			ast_say_digits(dchan, lot, "", dchan->language); | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			dres = ast_streamfile(dchan, tmp[i], dchan->language); | 
					
						
							| 
									
										
										
										
											2007-07-23 19:51:41 +00:00
										 |  |  | 			if (!dres) { | 
					
						
							| 
									
										
										
										
											2004-05-06 03:20:05 +00:00
										 |  |  | 				dres = ast_waitstream(dchan, ""); | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				ast_log(LOG_WARNING, "ast_streamfile of %s failed on %s\n", tmp[i], dchan->name); | 
					
						
							|  |  |  | 				dres = 0; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ast_stopstream(dchan);   | 
					
						
							|  |  |  | 	ast_hangup(dchan); | 
					
						
							| 
									
										
										
										
											2005-10-19 18:19:02 +00:00
										 |  |  | 	 | 
					
						
							| 
									
										
										
										
											2004-05-06 03:20:05 +00:00
										 |  |  | 	return res; | 
					
						
							| 
									
										
										
										
											2002-12-05 20:13:04 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-08-21 02:11:39 +00:00
										 |  |  | static int unload_module(void) | 
					
						
							| 
									
										
										
										
											2002-12-05 20:13:04 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2007-07-16 13:35:20 +00:00
										 |  |  | 	return ast_unregister_application(app); | 
					
						
							| 
									
										
										
										
											2002-12-05 20:13:04 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-08-21 02:11:39 +00:00
										 |  |  | static int load_module(void) | 
					
						
							| 
									
										
										
										
											2002-12-05 20:13:04 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2004-05-06 03:20:05 +00:00
										 |  |  | 	/* return ast_register_application(app, park_exec); */ | 
					
						
							|  |  |  | 	return ast_register_application(app, parkandannounce_exec, synopsis, descrip); | 
					
						
							| 
									
										
										
										
											2002-12-05 20:13:04 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-08-21 02:11:39 +00:00
										 |  |  | AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Call Parking and Announce Application"); |