2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (c) 2004 - 2006 Digium, Inc. All rights reserved.
6 * Mark Spencer <markster@digium.com>
8 * This code is released under the GNU General Public License
9 * version 2.0. See LICENSE for more information.
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.
21 * \brief page() - Paging application
23 * \author Mark Spencer <markster@digium.com>
25 * \ingroup applications
29 <depend>app_confbridge</depend>
30 <support_level>core</support_level>
35 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
37 #include "asterisk/channel.h"
38 #include "asterisk/pbx.h"
39 #include "asterisk/module.h"
40 #include "asterisk/file.h"
41 #include "asterisk/app.h"
42 #include "asterisk/chanvars.h"
43 #include "asterisk/utils.h"
44 #include "asterisk/devicestate.h"
45 #include "asterisk/dial.h"
48 <application name="Page" language="en_US">
53 <parameter name="Technology/Resource" required="true" argsep="&">
54 <argument name="Technology/Resource" required="true">
55 <para>Specification of the device(s) to dial. These must be in the format of
56 <literal>Technology/Resource</literal>, where <replaceable>Technology</replaceable>
57 represents a particular channel driver, and <replaceable>Resource</replaceable> represents a resource
58 available to that particular channel driver.</para>
60 <argument name="Technology2/Resource2" multiple="true">
61 <para>Optional extra devices to dial in parallel</para>
62 <para>If you need more than one, enter them as Technology2/Resource2&
63 Technology3/Resourse3&.....</para>
66 <parameter name="options">
69 <para>Full duplex audio</para>
72 <para>Ignore attempts to forward the call</para>
75 <para>Quiet, do not play beep to caller</para>
78 <para>Record the page into a file (<literal>CONFBRIDGE(bridge,record_conference)</literal>)</para>
81 <para>Only dial a channel if its device state says that it is <literal>NOT_INUSE</literal></para>
84 <argument name="x" required="true">
85 <para>The announcement to playback to all devices</para>
87 <para>Play an announcement to all paged participants</para>
90 <para>Do not play announcement to caller (alters <literal>A(x)</literal> behavior)</para>
94 <parameter name="timeout">
95 <para>Specify the length of time that the system will attempt to connect a call.
96 After this duration, any page calls that have not been answered will be hung up by the
101 <para>Places outbound calls to the given <replaceable>technology</replaceable>/<replaceable>resource</replaceable>
102 and dumps them into a conference bridge as muted participants. The original
103 caller is dumped into the conference as a speaker and the room is
104 destroyed when the original caller leaves.</para>
107 <ref type="application">ConfBridge</ref>
111 static const char * const app_page= "Page";
113 enum page_opt_flags {
114 PAGE_DUPLEX = (1 << 0),
115 PAGE_QUIET = (1 << 1),
116 PAGE_RECORD = (1 << 2),
117 PAGE_SKIP = (1 << 3),
118 PAGE_IGNORE_FORWARDS = (1 << 4),
119 PAGE_ANNOUNCE = (1 << 5),
120 PAGE_NOCALLERANNOUNCE = (1 << 6),
124 OPT_ARG_ANNOUNCE = 0,
125 OPT_ARG_ARRAY_SIZE = 1,
128 AST_APP_OPTIONS(page_opts, {
129 AST_APP_OPTION('d', PAGE_DUPLEX),
130 AST_APP_OPTION('q', PAGE_QUIET),
131 AST_APP_OPTION('r', PAGE_RECORD),
132 AST_APP_OPTION('s', PAGE_SKIP),
133 AST_APP_OPTION('i', PAGE_IGNORE_FORWARDS),
134 AST_APP_OPTION_ARG('A', PAGE_ANNOUNCE, OPT_ARG_ANNOUNCE),
135 AST_APP_OPTION('n', PAGE_NOCALLERANNOUNCE),
138 /* We use this structure as a way to pass this to all dialed channels */
139 struct page_options {
140 char *opts[OPT_ARG_ARRAY_SIZE];
141 struct ast_flags flags;
146 * \brief Setup the page bridge profile.
148 * \param chan Setup bridge profile on this channel.
149 * \param options Options to setup bridge profile.
153 static void setup_profile_bridge(struct ast_channel *chan, struct page_options *options)
155 /* Use default_bridge as a starting point */
156 ast_func_write(chan, "CONFBRIDGE(bridge,template)", "");
157 if (ast_test_flag(&options->flags, PAGE_RECORD)) {
158 ast_func_write(chan, "CONFBRIDGE(bridge,record_conference)", "yes");
164 * \brief Setup the paged user profile.
166 * \param chan Setup user profile on this channel.
167 * \param options Options to setup paged user profile.
171 static void setup_profile_paged(struct ast_channel *chan, struct page_options *options)
173 /* Use default_user as a starting point */
174 ast_func_write(chan, "CONFBRIDGE(user,template)", "");
175 ast_func_write(chan, "CONFBRIDGE(user,quiet)", "yes");
176 ast_func_write(chan, "CONFBRIDGE(user,end_marked)", "yes");
177 if (!ast_test_flag(&options->flags, PAGE_DUPLEX)) {
178 ast_func_write(chan, "CONFBRIDGE(user,startmuted)", "yes");
180 if (ast_test_flag(&options->flags, PAGE_ANNOUNCE)
181 && !ast_strlen_zero(options->opts[OPT_ARG_ANNOUNCE])) {
182 ast_func_write(chan, "CONFBRIDGE(user,announcement)", options->opts[OPT_ARG_ANNOUNCE]);
188 * \brief Setup the caller user profile.
190 * \param chan Setup user profile on this channel.
191 * \param options Options to setup caller user profile.
195 static void setup_profile_caller(struct ast_channel *chan, struct page_options *options)
197 /* Use default_user as a starting point if not already setup. */
198 ast_func_write(chan, "CONFBRIDGE(user,template)", "");
199 ast_func_write(chan, "CONFBRIDGE(user,quiet)", "yes");
200 ast_func_write(chan, "CONFBRIDGE(user,marked)", "yes");
201 if (!ast_test_flag(&options->flags, PAGE_NOCALLERANNOUNCE)
202 && ast_test_flag(&options->flags, PAGE_ANNOUNCE)
203 && !ast_strlen_zero(options->opts[OPT_ARG_ANNOUNCE])) {
204 ast_func_write(chan, "CONFBRIDGE(user,announcement)", options->opts[OPT_ARG_ANNOUNCE]);
208 static void page_state_callback(struct ast_dial *dial)
210 struct ast_channel *chan;
211 struct page_options *options;
213 if (ast_dial_state(dial) != AST_DIAL_RESULT_ANSWERED ||
214 !(chan = ast_dial_answered(dial)) ||
215 !(options = ast_dial_get_user_data(dial))) {
219 setup_profile_bridge(chan, options);
220 setup_profile_paged(chan, options);
223 static int page_exec(struct ast_channel *chan, const char *data)
225 char *tech, *resource, *tmp;
226 char confbridgeopts[128], originator[AST_CHANNEL_NAME];
227 struct page_options options = { { 0, }, { 0, } };
228 unsigned int confid = ast_random();
230 int res = 0, pos = 0, i = 0;
231 struct ast_dial **dial_list;
232 unsigned int num_dials;
236 AST_DECLARE_APP_ARGS(args,
237 AST_APP_ARG(devices);
238 AST_APP_ARG(options);
239 AST_APP_ARG(timeout);
242 if (ast_strlen_zero(data)) {
243 ast_log(LOG_WARNING, "This application requires at least one argument (destination(s) to page)\n");
247 if (!(app = pbx_findapp("ConfBridge"))) {
248 ast_log(LOG_WARNING, "There is no ConfBridge application available!\n");
252 parse = ast_strdupa(data);
254 AST_STANDARD_APP_ARGS(args, parse);
256 ast_copy_string(originator, ast_channel_name(chan), sizeof(originator));
257 if ((tmp = strchr(originator, '-'))) {
261 if (!ast_strlen_zero(args.options)) {
262 ast_app_parse_options(page_opts, &options.flags, options.opts, args.options);
265 if (!ast_strlen_zero(args.timeout)) {
266 timeout = atoi(args.timeout);
269 snprintf(confbridgeopts, sizeof(confbridgeopts), "ConfBridge,%u", confid);
271 /* Count number of extensions in list by number of ampersands + 1 */
281 if (!(dial_list = ast_calloc(num_dials, sizeof(struct ast_dial *)))) {
282 ast_log(LOG_ERROR, "Can't allocate %ld bytes for dial list\n", (long)(sizeof(struct ast_dial *) * num_dials));
286 /* Go through parsing/calling each device */
287 while ((tech = strsep(&args.devices, "&"))) {
289 struct ast_dial *dial = NULL;
291 /* don't call the originating device */
292 if (!strcasecmp(tech, originator))
295 /* If no resource is available, continue on */
296 if (!(resource = strchr(tech, '/'))) {
297 ast_log(LOG_WARNING, "Incomplete destination '%s' supplied.\n", tech);
301 /* Ensure device is not in use if skip option is enabled */
302 if (ast_test_flag(&options.flags, PAGE_SKIP)) {
303 state = ast_device_state(tech);
304 if (state == AST_DEVICE_UNKNOWN) {
305 ast_log(LOG_WARNING, "Destination '%s' has device state '%s'. Paging anyway.\n", tech, ast_devstate2str(state));
306 } else if (state != AST_DEVICE_NOT_INUSE) {
307 ast_log(LOG_WARNING, "Destination '%s' has device state '%s'.\n", tech, ast_devstate2str(state));
314 /* Create a dialing structure */
315 if (!(dial = ast_dial_create())) {
316 ast_log(LOG_WARNING, "Failed to create dialing structure.\n");
320 /* Append technology and resource */
321 if (ast_dial_append(dial, tech, resource) == -1) {
322 ast_log(LOG_ERROR, "Failed to add %s to outbound dial\n", tech);
323 ast_dial_destroy(dial);
327 /* Set ANSWER_EXEC as global option */
328 ast_dial_option_global_enable(dial, AST_DIAL_OPTION_ANSWER_EXEC, confbridgeopts);
331 ast_dial_set_global_timeout(dial, timeout * 1000);
334 if (ast_test_flag(&options.flags, PAGE_IGNORE_FORWARDS)) {
335 ast_dial_option_global_enable(dial, AST_DIAL_OPTION_DISABLE_CALL_FORWARDING, NULL);
338 ast_dial_set_state_callback(dial, &page_state_callback);
339 ast_dial_set_user_data(dial, &options);
341 /* Run this dial in async mode */
342 ast_dial_run(dial, chan, 1);
344 /* Put in our dialing array */
345 dial_list[pos++] = dial;
348 if (!ast_test_flag(&options.flags, PAGE_QUIET)) {
349 res = ast_streamfile(chan, "beep", ast_channel_language(chan));
351 res = ast_waitstream(chan, "");
355 setup_profile_bridge(chan, &options);
356 setup_profile_caller(chan, &options);
358 snprintf(confbridgeopts, sizeof(confbridgeopts), "%u", confid);
359 pbx_exec(chan, app, confbridgeopts);
362 /* Go through each dial attempt cancelling, joining, and destroying */
363 for (i = 0; i < pos; i++) {
364 struct ast_dial *dial = dial_list[i];
366 /* We have to wait for the async thread to exit as it's possible ConfBridge won't throw them out immediately */
369 /* Hangup all channels */
370 ast_dial_hangup(dial);
372 /* Destroy dialing structure */
373 ast_dial_destroy(dial);
381 static int unload_module(void)
383 return ast_unregister_application(app_page);
386 static int load_module(void)
388 return ast_register_application_xml(app_page, page_exec);
391 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Page Multiple Phones");