res_http_media_cache.c: Compare unaltered MIME types.
[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 #include "asterisk/channel.h"
36 #include "asterisk/pbx.h"
37 #include "asterisk/module.h"
38 #include "asterisk/file.h"
39 #include "asterisk/app.h"
40 #include "asterisk/chanvars.h"
41 #include "asterisk/utils.h"
42 #include "asterisk/devicestate.h"
43 #include "asterisk/dial.h"
44
45 /*** DOCUMENTATION
46         <application name="Page" language="en_US">
47                 <synopsis>
48                         Page series of phones
49                 </synopsis>
50                 <syntax>
51                         <parameter name="Technology/Resource" required="false" argsep="&amp;">
52                                 <argument name="Technology/Resource" required="true">
53                                         <para>Specification of the device(s) to dial. These must be in the format of
54                                         <literal>Technology/Resource</literal>, where <replaceable>Technology</replaceable>
55                                         represents a particular channel driver, and <replaceable>Resource</replaceable> represents a resource
56                                         available to that particular channel driver.</para>
57                                 </argument>
58                                 <argument name="Technology2/Resource2" multiple="true">
59                                         <para>Optional extra devices to dial in parallel</para>
60                                         <para>If you need more than one, enter them as Technology2/Resource2&amp;
61                                         Technology3/Resource3&amp;.....</para>
62                                 </argument>
63                         </parameter>
64                         <parameter name="options">
65                                 <optionlist>
66                                 <option name="b" argsep="^">
67                                         <para>Before initiating an outgoing call, Gosub to the specified
68                                         location using the newly created channel.  The Gosub will be
69                                         executed for each destination channel.</para>
70                                         <argument name="context" required="false" />
71                                         <argument name="exten" required="false" />
72                                         <argument name="priority" required="true" hasparams="optional" argsep="^">
73                                                 <argument name="arg1" multiple="true" required="true" />
74                                                 <argument name="argN" />
75                                         </argument>
76                                 </option>
77                                 <option name="B" argsep="^">
78                                         <para>Before initiating the outgoing call(s), Gosub to the specified
79                                         location using the current channel.</para>
80                                         <argument name="context" required="false" />
81                                         <argument name="exten" required="false" />
82                                         <argument name="priority" required="true" hasparams="optional" argsep="^">
83                                                 <argument name="arg1" multiple="true" required="true" />
84                                                 <argument name="argN" />
85                                         </argument>
86                                 </option>
87                                         <option name="d">
88                                                 <para>Full duplex audio</para>
89                                         </option>
90                                         <option name="i">
91                                                 <para>Ignore attempts to forward the call</para>
92                                         </option>
93                                         <option name="q">
94                                                 <para>Quiet, do not play beep to caller</para>
95                                         </option>
96                                         <option name="r">
97                                                 <para>Record the page into a file (<literal>CONFBRIDGE(bridge,record_conference)</literal>)</para>
98                                         </option>
99                                         <option name="s">
100                                                 <para>Only dial a channel if its device state says that it is <literal>NOT_INUSE</literal></para>
101                                         </option>
102                                         <option name="A">
103                                                 <argument name="x" required="true">
104                                                         <para>The announcement to playback to all devices</para>
105                                                 </argument>
106                                                 <para>Play an announcement to all paged participants</para>
107                                         </option>
108                                         <option name="n">
109                                                 <para>Do not play announcement to caller (alters <literal>A(x)</literal> behavior)</para>
110                                         </option>
111                                 </optionlist>
112                         </parameter>
113                         <parameter name="timeout">
114                                 <para>Specify the length of time that the system will attempt to connect a call.
115                                 After this duration, any page calls that have not been answered will be hung up by the
116                                 system.</para>
117                         </parameter>
118                 </syntax>
119                 <description>
120                         <para>Places outbound calls to the given <replaceable>technology</replaceable>/<replaceable>resource</replaceable>
121                         and dumps them into a conference bridge as muted participants. The original
122                         caller is dumped into the conference as a speaker and the room is
123                         destroyed when the original caller leaves.</para>
124                 </description>
125                 <see-also>
126                         <ref type="application">ConfBridge</ref>
127                 </see-also>
128         </application>
129  ***/
130 static const char * const app_page= "Page";
131
132 enum page_opt_flags {
133         PAGE_DUPLEX = (1 << 0),
134         PAGE_QUIET = (1 << 1),
135         PAGE_RECORD = (1 << 2),
136         PAGE_SKIP = (1 << 3),
137         PAGE_IGNORE_FORWARDS = (1 << 4),
138         PAGE_ANNOUNCE = (1 << 5),
139         PAGE_NOCALLERANNOUNCE = (1 << 6),
140         PAGE_PREDIAL_CALLEE = (1 << 7),
141         PAGE_PREDIAL_CALLER = (1 << 8),
142 };
143
144 enum {
145         OPT_ARG_ANNOUNCE = 0,
146         OPT_ARG_PREDIAL_CALLEE = 1,
147         OPT_ARG_PREDIAL_CALLER = 2,
148         OPT_ARG_ARRAY_SIZE = 3,
149 };
150
151 AST_APP_OPTIONS(page_opts, {
152         AST_APP_OPTION_ARG('b', PAGE_PREDIAL_CALLEE, OPT_ARG_PREDIAL_CALLEE),
153         AST_APP_OPTION_ARG('B', PAGE_PREDIAL_CALLER, OPT_ARG_PREDIAL_CALLER),
154         AST_APP_OPTION('d', PAGE_DUPLEX),
155         AST_APP_OPTION('q', PAGE_QUIET),
156         AST_APP_OPTION('r', PAGE_RECORD),
157         AST_APP_OPTION('s', PAGE_SKIP),
158         AST_APP_OPTION('i', PAGE_IGNORE_FORWARDS),
159         AST_APP_OPTION_ARG('A', PAGE_ANNOUNCE, OPT_ARG_ANNOUNCE),
160         AST_APP_OPTION('n', PAGE_NOCALLERANNOUNCE),
161 });
162
163 #define PAGE_BEEP "beep"
164
165 /* We use this structure as a way to pass this to all dialed channels */
166 struct page_options {
167         char *opts[OPT_ARG_ARRAY_SIZE];
168         struct ast_flags flags;
169 };
170
171 /*!
172  * \internal
173  * \brief Setup the page bridge profile.
174  *
175  * \param chan Setup bridge profile on this channel.
176  * \param options Options to setup bridge profile.
177  *
178  * \return Nothing
179  */
180 static void setup_profile_bridge(struct ast_channel *chan, struct page_options *options)
181 {
182         /* Use default_bridge as a starting point */
183         ast_func_write(chan, "CONFBRIDGE(bridge,template)", "");
184         if (ast_test_flag(&options->flags, PAGE_RECORD)) {
185                 ast_func_write(chan, "CONFBRIDGE(bridge,record_conference)", "yes");
186         }
187 }
188
189 /*!
190  * \internal
191  * \brief Setup the paged user profile.
192  *
193  * \param chan Setup user profile on this channel.
194  * \param options Options to setup paged user profile.
195  *
196  * \return Nothing
197  */
198 static void setup_profile_paged(struct ast_channel *chan, struct page_options *options)
199 {
200         /* Use default_user as a starting point */
201         ast_func_write(chan, "CONFBRIDGE(user,template)", "");
202         ast_func_write(chan, "CONFBRIDGE(user,quiet)", "yes");
203         ast_func_write(chan, "CONFBRIDGE(user,end_marked)", "yes");
204         if (!ast_test_flag(&options->flags, PAGE_DUPLEX)) {
205                 ast_func_write(chan, "CONFBRIDGE(user,startmuted)", "yes");
206         }
207         if (ast_test_flag(&options->flags, PAGE_ANNOUNCE)
208                 && !ast_strlen_zero(options->opts[OPT_ARG_ANNOUNCE])) {
209                 ast_func_write(chan, "CONFBRIDGE(user,announcement)", options->opts[OPT_ARG_ANNOUNCE]);
210         }
211 }
212
213 /*!
214  * \internal
215  * \brief Setup the caller user profile.
216  *
217  * \param chan Setup user profile on this channel.
218  * \param options Options to setup caller user profile.
219  *
220  * \return Nothing
221  */
222 static void setup_profile_caller(struct ast_channel *chan, struct page_options *options)
223 {
224         /* Use default_user as a starting point if not already setup. */
225         ast_func_write(chan, "CONFBRIDGE(user,template)", "");
226         ast_func_write(chan, "CONFBRIDGE(user,quiet)", "yes");
227         ast_func_write(chan, "CONFBRIDGE(user,marked)", "yes");
228         if (!ast_test_flag(&options->flags, PAGE_NOCALLERANNOUNCE)
229                 && ast_test_flag(&options->flags, PAGE_ANNOUNCE)
230                 && !ast_strlen_zero(options->opts[OPT_ARG_ANNOUNCE])) {
231                 ast_func_write(chan, "CONFBRIDGE(user,announcement)", options->opts[OPT_ARG_ANNOUNCE]);
232         }
233 }
234
235 static void page_state_callback(struct ast_dial *dial)
236 {
237         struct ast_channel *chan;
238         struct page_options *options;
239
240         if (ast_dial_state(dial) != AST_DIAL_RESULT_ANSWERED ||
241             !(chan = ast_dial_answered(dial)) ||
242             !(options = ast_dial_get_user_data(dial))) {
243                 return;
244         }
245
246         setup_profile_bridge(chan, options);
247         setup_profile_paged(chan, options);
248 }
249
250 static int page_exec(struct ast_channel *chan, const char *data)
251 {
252         char *tech;
253         char *resource;
254         char *tmp;
255         char *predial_callee = NULL;
256         char confbridgeopts[128];
257         char originator[AST_CHANNEL_NAME];
258         struct page_options options = { { 0, }, { 0, } };
259         unsigned int confid = ast_random();
260         struct ast_app *app;
261         int res = 0;
262         int pos = 0;
263         int i = 0;
264         struct ast_dial **dial_list;
265         unsigned int num_dials;
266         int timeout = 0;
267         char *parse;
268
269         AST_DECLARE_APP_ARGS(args,
270                 AST_APP_ARG(devices);
271                 AST_APP_ARG(options);
272                 AST_APP_ARG(timeout);
273         );
274
275         if (!(app = pbx_findapp("ConfBridge"))) {
276                 ast_log(LOG_WARNING, "There is no ConfBridge application available!\n");
277                 return -1;
278         };
279
280         parse = ast_strdupa(data ?: "");
281
282         AST_STANDARD_APP_ARGS(args, parse);
283
284         ast_copy_string(originator, ast_channel_name(chan), sizeof(originator));
285         if ((tmp = strchr(originator, '-'))) {
286                 *tmp = '\0';
287         }
288
289         if (!ast_strlen_zero(args.options)) {
290                 ast_app_parse_options(page_opts, &options.flags, options.opts, args.options);
291         }
292
293         if (!ast_strlen_zero(args.timeout)) {
294                 timeout = atoi(args.timeout);
295         }
296
297         snprintf(confbridgeopts, sizeof(confbridgeopts), "ConfBridge,%u", confid);
298
299         /* Count number of extensions in list by number of ampersands + 1 */
300         num_dials = 1;
301         tmp = args.devices ?: "";
302         while (*tmp) {
303                 if (*tmp == '&') {
304                         num_dials++;
305                 }
306                 tmp++;
307         }
308
309         if (!(dial_list = ast_calloc(num_dials, sizeof(struct ast_dial *)))) {
310                 ast_log(LOG_ERROR, "Can't allocate %ld bytes for dial list\n", (long)(sizeof(struct ast_dial *) * num_dials));
311                 return -1;
312         }
313
314         /* PREDIAL: Preprocess any callee gosub arguments. */
315         if (ast_test_flag(&options.flags, PAGE_PREDIAL_CALLEE)
316                 && !ast_strlen_zero(options.opts[OPT_ARG_PREDIAL_CALLEE])) {
317                 ast_replace_subargument_delimiter(options.opts[OPT_ARG_PREDIAL_CALLEE]);
318                 predial_callee =
319                         (char *) ast_app_expand_sub_args(chan, options.opts[OPT_ARG_PREDIAL_CALLEE]);
320         }
321
322         /* PREDIAL: Run gosub on the caller's channel */
323         if (ast_test_flag(&options.flags, PAGE_PREDIAL_CALLER)
324                 && !ast_strlen_zero(options.opts[OPT_ARG_PREDIAL_CALLER])) {
325                 ast_replace_subargument_delimiter(options.opts[OPT_ARG_PREDIAL_CALLER]);
326                 ast_app_exec_sub(NULL, chan, options.opts[OPT_ARG_PREDIAL_CALLER], 0);
327         }
328
329         /* Go through parsing/calling each device */
330         while ((tech = strsep(&args.devices, "&"))) {
331                 int state = 0;
332                 struct ast_dial *dial = NULL;
333
334                 tech = ast_strip(tech);
335                 if (ast_strlen_zero(tech)) {
336                         /* No tech/resource in this position. */
337                         continue;
338                 }
339
340                 /* don't call the originating device */
341                 if (!strcasecmp(tech, originator)) {
342                         continue;
343                 }
344
345                 /* If no resource is available, continue on */
346                 if (!(resource = strchr(tech, '/'))) {
347                         ast_log(LOG_WARNING, "Incomplete destination: '%s' supplied.\n", tech);
348                         continue;
349                 }
350
351                 /* Ensure device is not in use if skip option is enabled */
352                 if (ast_test_flag(&options.flags, PAGE_SKIP)) {
353                         state = ast_device_state(tech);
354                         if (state == AST_DEVICE_UNKNOWN) {
355                                 ast_verb(3, "Destination '%s' has device state '%s'. Paging anyway.\n",
356                                         tech, ast_devstate2str(state));
357                         } else if (state != AST_DEVICE_NOT_INUSE) {
358                                 ast_verb(3, "Destination '%s' has device state '%s'.\n",
359                                         tech, ast_devstate2str(state));
360                                 continue;
361                         }
362                 }
363
364                 *resource++ = '\0';
365
366                 /* Create a dialing structure */
367                 if (!(dial = ast_dial_create())) {
368                         ast_log(LOG_WARNING, "Failed to create dialing structure.\n");
369                         continue;
370                 }
371
372                 /* Append technology and resource */
373                 if (ast_dial_append(dial, tech, resource, NULL) == -1) {
374                         ast_log(LOG_ERROR, "Failed to add %s/%s to outbound dial\n", tech, resource);
375                         ast_dial_destroy(dial);
376                         continue;
377                 }
378
379                 /* Set ANSWER_EXEC as global option */
380                 ast_dial_option_global_enable(dial, AST_DIAL_OPTION_ANSWER_EXEC, confbridgeopts);
381
382                 if (predial_callee) {
383                         ast_dial_option_global_enable(dial, AST_DIAL_OPTION_PREDIAL, predial_callee);
384                 }
385
386                 if (timeout) {
387                         ast_dial_set_global_timeout(dial, timeout * 1000);
388                 }
389
390                 if (ast_test_flag(&options.flags, PAGE_IGNORE_FORWARDS)) {
391                         ast_dial_option_global_enable(dial, AST_DIAL_OPTION_DISABLE_CALL_FORWARDING, NULL);
392                 }
393
394                 ast_dial_set_state_callback(dial, &page_state_callback);
395                 ast_dial_set_user_data(dial, &options);
396
397                 /* Run this dial in async mode */
398                 ast_dial_run(dial, chan, 1);
399
400                 /* Put in our dialing array */
401                 dial_list[pos++] = dial;
402         }
403
404         ast_free(predial_callee);
405
406         if (!ast_test_flag(&options.flags, PAGE_QUIET)) {
407                 if (!ast_fileexists(PAGE_BEEP, NULL, NULL)) {
408                         ast_log(LOG_WARNING, "Missing required sound file: '" PAGE_BEEP "'\n");
409                 } else {
410                         res = ast_streamfile(chan, PAGE_BEEP, ast_channel_language(chan));
411                         if (!res) {
412                                 res = ast_waitstream(chan, "");
413                         }
414                 }
415         }
416
417         if (!res) {
418                 setup_profile_bridge(chan, &options);
419                 setup_profile_caller(chan, &options);
420
421                 snprintf(confbridgeopts, sizeof(confbridgeopts), "%u", confid);
422                 pbx_exec(chan, app, confbridgeopts);
423         }
424
425         /* Go through each dial attempt cancelling, joining, and destroying */
426         for (i = 0; i < pos; i++) {
427                 struct ast_dial *dial = dial_list[i];
428
429                 /* We have to wait for the async thread to exit as it's possible ConfBridge won't throw them out immediately */
430                 ast_dial_join(dial);
431
432                 /* Hangup all channels */
433                 ast_dial_hangup(dial);
434
435                 /* Destroy dialing structure */
436                 ast_dial_destroy(dial);
437         }
438
439         ast_free(dial_list);
440
441         return -1;
442 }
443
444 static int unload_module(void)
445 {
446         return ast_unregister_application(app_page);
447 }
448
449 static int load_module(void)
450 {
451         return ast_register_application_xml(app_page, page_exec);
452 }
453
454 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Page Multiple Phones",
455         .support_level = AST_MODULE_SUPPORT_CORE,
456         .load = load_module,
457         .unload = unload_module,
458         .requires = "app_confbridge",
459 );