8c56948414a276bb8a034e0e6bfc0fee0c72cdb8
[asterisk/asterisk.git] / apps / app_page.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (c) 2004 - 2006 Digium, Inc.  All rights reserved.
5  *
6  * Mark Spencer <markster@digium.com>
7  *
8  * This code is released under the GNU General Public License
9  * version 2.0.  See LICENSE for more information.
10  *
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.
16  *
17  */
18
19 /*! \file
20  *
21  * \brief page() - Paging application
22  *
23  * \author Mark Spencer <markster@digium.com>
24  *
25  * \ingroup applications
26  */
27
28 /*** MODULEINFO
29         <depend>app_confbridge</depend>
30         <support_level>core</support_level>
31  ***/
32
33 #include "asterisk.h"
34
35 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
36
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"
46
47 /*** DOCUMENTATION
48         <application name="Page" language="en_US">
49                 <synopsis>
50                         Page series of phones
51                 </synopsis>
52                 <syntax>
53                         <parameter name="Technology/Resource" required="true" argsep="&amp;">
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>
59                                 </argument>
60                                 <argument name="Technology2/Resource2" multiple="true">
61                                         <para>Optional extra devices to dial inparallel</para>
62                                         <para>If you need more then one enter them as Technology2/Resource2&amp;
63                                         Technology3/Resourse3&amp;.....</para>
64                                 </argument>
65                         </parameter>
66                         <parameter name="options">
67                                 <optionlist>
68                                         <option name="d">
69                                                 <para>Full duplex audio</para>
70                                         </option>
71                                         <option name="i">
72                                                 <para>Ignore attempts to forward the call</para>
73                                         </option>
74                                         <option name="q">
75                                                 <para>Quiet, do not play beep to caller</para>
76                                         </option>
77                                         <option name="r">
78                                                 <para>Record the page into a file (ConfBridge option <literal>r</literal>)</para>
79                                         </option>
80                                         <option name="s">
81                                                 <para>Only dial a channel if its device state says that it is <literal>NOT_INUSE</literal></para>
82                                         </option>
83                                         <option name="A">
84                                                 <argument name="x" required="true">
85                                                         <para>The announcement to playback in all devices</para>
86                                                 </argument>
87                                                 <para>Play an announcement simultaneously to all paged participants</para>
88                                         </option>
89                                         <option name="n">
90                                                 <para>Do not play simultaneous announcement to caller (implies <literal>A(x)</literal>)</para>
91                                         </option>
92                                 </optionlist>
93                         </parameter>
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 intercom calls that have not been answered will be hung up by the
97                                 system.</para>
98                         </parameter>
99                 </syntax>
100                 <description>
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 callers leaves.</para>
105                 </description>
106                 <see-also>
107                         <ref type="application">ConfBridge</ref>
108                 </see-also>
109         </application>
110  ***/
111 static const char * const app_page= "Page";
112
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),
121 };
122
123 enum {
124         OPT_ARG_ANNOUNCE = 0,
125         OPT_ARG_ARRAY_SIZE = 1,
126 };
127
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),
136 });
137
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;
142 };
143
144 static void page_state_callback(struct ast_dial *dial)
145 {
146         struct ast_channel *chan;
147         struct page_options *options;
148
149         if (ast_dial_state(dial) != AST_DIAL_RESULT_ANSWERED ||
150             !(chan = ast_dial_answered(dial)) ||
151             !(options = ast_dial_get_user_data(dial))) {
152                 return;
153         }
154
155         ast_func_write(chan, "CONFBRIDGE(bridge,template)", "default_bridge");
156
157         if (ast_test_flag(&options->flags, PAGE_RECORD)) {
158                 ast_func_write(chan, "CONFBRIDGE(bridge,record_conference)", "yes");
159         }
160
161         ast_func_write(chan, "CONFBRIDGE(user,quiet)", "yes");
162         ast_func_write(chan, "CONFBRIDGE(user,end_marked)", "yes");
163
164         if (!ast_test_flag(&options->flags, PAGE_DUPLEX)) {
165                 ast_func_write(chan, "CONFBRIDGE(user,startmuted)", "yes");
166         }
167
168         if (ast_test_flag(&options->flags, PAGE_ANNOUNCE) && !ast_strlen_zero(options->opts[OPT_ARG_ANNOUNCE])) {
169                 ast_func_write(chan, "CONFBRIDGE(user,announcement)", options->opts[OPT_ARG_ANNOUNCE]);
170         }
171 }
172
173 static int page_exec(struct ast_channel *chan, const char *data)
174 {
175         char *tech, *resource, *tmp;
176         char confbridgeopts[128], originator[AST_CHANNEL_NAME];
177         struct page_options options = { { 0, }, { 0, } };
178         unsigned int confid = ast_random();
179         struct ast_app *app;
180         int res = 0, pos = 0, i = 0;
181         struct ast_dial **dial_list;
182         unsigned int num_dials;
183         int timeout = 0;
184         char *parse;
185
186         AST_DECLARE_APP_ARGS(args,
187                 AST_APP_ARG(devices);
188                 AST_APP_ARG(options);
189                 AST_APP_ARG(timeout);
190         );
191
192         if (ast_strlen_zero(data)) {
193                 ast_log(LOG_WARNING, "This application requires at least one argument (destination(s) to page)\n");
194                 return -1;
195         }
196
197         if (!(app = pbx_findapp("ConfBridge"))) {
198                 ast_log(LOG_WARNING, "There is no ConfBridge application available!\n");
199                 return -1;
200         };
201
202         parse = ast_strdupa(data);
203
204         AST_STANDARD_APP_ARGS(args, parse);
205
206         ast_copy_string(originator, ast_channel_name(chan), sizeof(originator));
207         if ((tmp = strchr(originator, '-'))) {
208                 *tmp = '\0';
209         }
210
211         if (!ast_strlen_zero(args.options)) {
212                 ast_app_parse_options(page_opts, &options.flags, options.opts, args.options);
213         }
214
215         if (!ast_strlen_zero(args.timeout)) {
216                 timeout = atoi(args.timeout);
217         }
218
219         snprintf(confbridgeopts, sizeof(confbridgeopts), "ConfBridge,%u", confid);
220
221         /* Count number of extensions in list by number of ampersands + 1 */
222         num_dials = 1;
223         tmp = args.devices;
224         while (*tmp) {
225                 if (*tmp == '&') {
226                         num_dials++;
227                 }
228                 tmp++;
229         }
230
231         if (!(dial_list = ast_calloc(num_dials, sizeof(struct ast_dial *)))) {
232                 ast_log(LOG_ERROR, "Can't allocate %ld bytes for dial list\n", (long)(sizeof(struct ast_dial *) * num_dials));
233                 return -1;
234         }
235
236         /* Go through parsing/calling each device */
237         while ((tech = strsep(&args.devices, "&"))) {
238                 int state = 0;
239                 struct ast_dial *dial = NULL;
240
241                 /* don't call the originating device */
242                 if (!strcasecmp(tech, originator))
243                         continue;
244
245                 /* If no resource is available, continue on */
246                 if (!(resource = strchr(tech, '/'))) {
247                         ast_log(LOG_WARNING, "Incomplete destination '%s' supplied.\n", tech);
248                         continue;
249                 }
250
251                 /* Ensure device is not in use if skip option is enabled */
252                 if (ast_test_flag(&options.flags, PAGE_SKIP)) {
253                         state = ast_device_state(tech);
254                         if (state == AST_DEVICE_UNKNOWN) {
255                                 ast_log(LOG_WARNING, "Destination '%s' has device state '%s'. Paging anyway.\n", tech, ast_devstate2str(state));
256                         } else if (state != AST_DEVICE_NOT_INUSE) {
257                                 ast_log(LOG_WARNING, "Destination '%s' has device state '%s'.\n", tech, ast_devstate2str(state));
258                                 continue;
259                         }
260                 }
261
262                 *resource++ = '\0';
263
264                 /* Create a dialing structure */
265                 if (!(dial = ast_dial_create())) {
266                         ast_log(LOG_WARNING, "Failed to create dialing structure.\n");
267                         continue;
268                 }
269
270                 /* Append technology and resource */
271                 if (ast_dial_append(dial, tech, resource) == -1) {
272                         ast_log(LOG_ERROR, "Failed to add %s to outbound dial\n", tech);
273                         ast_dial_destroy(dial);
274                         continue;
275                 }
276
277                 /* Set ANSWER_EXEC as global option */
278                 ast_dial_option_global_enable(dial, AST_DIAL_OPTION_ANSWER_EXEC, confbridgeopts);
279
280                 if (timeout) {
281                         ast_dial_set_global_timeout(dial, timeout * 1000);
282                 }
283
284                 if (ast_test_flag(&options.flags, PAGE_IGNORE_FORWARDS)) {
285                         ast_dial_option_global_enable(dial, AST_DIAL_OPTION_DISABLE_CALL_FORWARDING, NULL);
286                 }
287
288                 ast_dial_set_state_callback(dial, &page_state_callback);
289                 ast_dial_set_user_data(dial, &options);
290
291                 /* Run this dial in async mode */
292                 ast_dial_run(dial, chan, 1);
293
294                 /* Put in our dialing array */
295                 dial_list[pos++] = dial;
296         }
297
298         if (!ast_test_flag(&options.flags, PAGE_QUIET)) {
299                 res = ast_streamfile(chan, "beep", ast_channel_language(chan));
300                 if (!res)
301                         res = ast_waitstream(chan, "");
302         }
303
304         if (!res) {
305                 ast_func_write(chan, "CONFBRIDGE(bridge,template)", "default_bridge");
306
307                 if (ast_test_flag(&options.flags, PAGE_RECORD)) {
308                         ast_func_write(chan, "CONFBRIDGE(bridge,record_conference)", "yes");
309                 }
310
311                 ast_func_write(chan, "CONFBRIDGE(user,quiet)", "yes");
312                 ast_func_write(chan, "CONFBRIDGE(user,marked)", "yes");
313
314                 snprintf(confbridgeopts, sizeof(confbridgeopts), "%u", confid);
315
316                 pbx_exec(chan, app, confbridgeopts);
317         }
318
319         /* Go through each dial attempt cancelling, joining, and destroying */
320         for (i = 0; i < pos; i++) {
321                 struct ast_dial *dial = dial_list[i];
322
323                 /* We have to wait for the async thread to exit as it's possible ConfBridge won't throw them out immediately */
324                 ast_dial_join(dial);
325
326                 /* Hangup all channels */
327                 ast_dial_hangup(dial);
328
329                 /* Destroy dialing structure */
330                 ast_dial_destroy(dial);
331         }
332
333         ast_free(dial_list);
334
335         return -1;
336 }
337
338 static int unload_module(void)
339 {
340         return ast_unregister_application(app_page);
341 }
342
343 static int load_module(void)
344 {
345         return ast_register_application_xml(app_page, page_exec);
346 }
347
348 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Page Multiple Phones");
349