f05b5d87107be037296a6dae31488811f569cc97
[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>dahdi</depend>
30         <depend>app_meetme</depend>
31         <support_level>core</support_level>
32  ***/
33
34 #include "asterisk.h"
35
36 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
37
38 #include "asterisk/channel.h"
39 #include "asterisk/pbx.h"
40 #include "asterisk/module.h"
41 #include "asterisk/file.h"
42 #include "asterisk/app.h"
43 #include "asterisk/chanvars.h"
44 #include "asterisk/utils.h"
45 #include "asterisk/devicestate.h"
46 #include "asterisk/dial.h"
47
48 /*** DOCUMENTATION
49         <application name="Page" language="en_US">
50                 <synopsis>
51                         Page series of phones
52                 </synopsis>
53                 <syntax>
54                         <parameter name="Technology/Resource" required="true" argsep="&amp;">
55                                 <argument name="Technology/Resource" required="true">
56                                         <para>Specification of the device(s) to dial. These must be in the format of
57                                         <literal>Technology/Resource</literal>, where <replaceable>Technology</replaceable>
58                                         represents a particular channel driver, and <replaceable>Resource</replaceable> represents a resource
59                                         available to that particular channel driver.</para>
60                                 </argument>
61                                 <argument name="Technology2/Resource2" multiple="true">
62                                         <para>Optional extra devices to dial inparallel</para>
63                                         <para>If you need more then one enter them as Technology2/Resource2&amp;
64                                         Technology3/Resourse3&amp;.....</para>
65                                 </argument>
66                         </parameter>
67                         <parameter name="options">
68                                 <optionlist>
69                                         <option name="d">
70                                                 <para>Full duplex audio</para>
71                                         </option>
72                                         <option name="i">
73                                                 <para>Ignore attempts to forward the call</para>
74                                         </option>
75                                         <option name="q">
76                                                 <para>Quiet, do not play beep to caller</para>
77                                         </option>
78                                         <option name="r">
79                                                 <para>Record the page into a file (meetme option <literal>r</literal>)</para>
80                                         </option>
81                                         <option name="s">
82                                                 <para>Only dial a channel if its device state says that it is <literal>NOT_INUSE</literal></para>
83                                         </option>
84                                         <option name="A">
85                                                 <argument name="x" required="true">
86                                                         <para>The announcement to playback in all devices</para>
87                                                 </argument>
88                                                 <para>Play an announcement simultaneously to all paged participants</para>
89                                         </option>
90                                         <option name="n">
91                                                 <para>Do not play simultaneous announcement to caller (implies <literal>A(x)</literal>)</para>
92                                         </option>
93                                 </optionlist>
94                         </parameter>
95                         <parameter name="timeout">
96                                 <para>Specify the length of time that the system will attempt to connect a call.
97                                 After this duration, any intercom calls that have not been answered will be hung up by the
98                                 system.</para>
99                         </parameter>
100                 </syntax>
101                 <description>
102                         <para>Places outbound calls to the given <replaceable>technology</replaceable>/<replaceable>resource</replaceable>
103                         and dumps them into a conference bridge as muted participants. The original
104                         caller is dumped into the conference as a speaker and the room is
105                         destroyed when the original callers leaves.</para>
106                 </description>
107                 <see-also>
108                         <ref type="application">MeetMe</ref>
109                 </see-also>
110         </application>
111  ***/
112 static const char * const app_page= "Page";
113
114 enum page_opt_flags {
115         PAGE_DUPLEX = (1 << 0),
116         PAGE_QUIET = (1 << 1),
117         PAGE_RECORD = (1 << 2),
118         PAGE_SKIP = (1 << 3),
119         PAGE_IGNORE_FORWARDS = (1 << 4),
120         PAGE_ANNOUNCE = (1 << 5),
121         PAGE_NOCALLERANNOUNCE = (1 << 6),
122 };
123
124 enum {
125         OPT_ARG_ANNOUNCE = 0,
126         OPT_ARG_ARRAY_SIZE = 1,
127 };
128
129 AST_APP_OPTIONS(page_opts, {
130         AST_APP_OPTION('d', PAGE_DUPLEX),
131         AST_APP_OPTION('q', PAGE_QUIET),
132         AST_APP_OPTION('r', PAGE_RECORD),
133         AST_APP_OPTION('s', PAGE_SKIP),
134         AST_APP_OPTION('i', PAGE_IGNORE_FORWARDS),
135         AST_APP_OPTION('i', PAGE_IGNORE_FORWARDS),
136         AST_APP_OPTION_ARG('A', PAGE_ANNOUNCE, OPT_ARG_ANNOUNCE),
137         AST_APP_OPTION('n', PAGE_NOCALLERANNOUNCE),
138 });
139
140
141 static int page_exec(struct ast_channel *chan, const char *data)
142 {
143         char *tech, *resource, *tmp;
144         char meetmeopts[128], originator[AST_CHANNEL_NAME], *opts[OPT_ARG_ARRAY_SIZE];
145         struct ast_flags flags = { 0 };
146         unsigned int confid = ast_random();
147         struct ast_app *app;
148         int res = 0, pos = 0, i = 0;
149         struct ast_dial **dial_list;
150         unsigned int num_dials;
151         int timeout = 0;
152         char *parse;
153
154         AST_DECLARE_APP_ARGS(args,
155                 AST_APP_ARG(devices);
156                 AST_APP_ARG(options);
157                 AST_APP_ARG(timeout);
158         );
159
160         if (ast_strlen_zero(data)) {
161                 ast_log(LOG_WARNING, "This application requires at least one argument (destination(s) to page)\n");
162                 return -1;
163         }
164
165         if (!(app = pbx_findapp("MeetMe"))) {
166                 ast_log(LOG_WARNING, "There is no MeetMe application available!\n");
167                 return -1;
168         };
169
170         parse = ast_strdupa(data);
171
172         AST_STANDARD_APP_ARGS(args, parse);
173
174         ast_copy_string(originator, ast_channel_name(chan), sizeof(originator));
175         if ((tmp = strchr(originator, '-'))) {
176                 *tmp = '\0';
177         }
178
179         if (!ast_strlen_zero(args.options)) {
180                 ast_app_parse_options(page_opts, &flags, opts, args.options);
181         }
182
183         if (!ast_strlen_zero(args.timeout)) {
184                 timeout = atoi(args.timeout);
185         }
186
187         if (ast_test_flag(&flags, PAGE_ANNOUNCE) && !ast_strlen_zero(opts[OPT_ARG_ANNOUNCE])) {
188                 snprintf(meetmeopts, sizeof(meetmeopts), "MeetMe,%ud,%s%sqxdw(5)G(%s)", confid, (ast_test_flag(&flags, PAGE_DUPLEX) ? "" : "m"),
189                                                  (ast_test_flag(&flags, PAGE_RECORD) ? "r" : ""), opts[OPT_ARG_ANNOUNCE] );
190         } else {
191                 snprintf(meetmeopts, sizeof(meetmeopts), "MeetMe,%ud,%s%sqxdw(5)", confid, (ast_test_flag(&flags, PAGE_DUPLEX) ? "" : "m"),
192                 (ast_test_flag(&flags, PAGE_RECORD) ? "r" : "") );
193         }
194
195         /* Count number of extensions in list by number of ampersands + 1 */
196         num_dials = 1;
197         tmp = args.devices;
198         while (*tmp) {
199                 if (*tmp == '&') {
200                         num_dials++;
201                 }
202                 tmp++;
203         }
204
205         if (!(dial_list = ast_calloc(num_dials, sizeof(struct ast_dial *)))) {
206                 ast_log(LOG_ERROR, "Can't allocate %ld bytes for dial list\n", (long)(sizeof(struct ast_dial *) * num_dials));
207                 return -1;
208         }
209
210         /* Go through parsing/calling each device */
211         while ((tech = strsep(&args.devices, "&"))) {
212                 int state = 0;
213                 struct ast_dial *dial = NULL;
214
215                 /* don't call the originating device */
216                 if (!strcasecmp(tech, originator))
217                         continue;
218
219                 /* If no resource is available, continue on */
220                 if (!(resource = strchr(tech, '/'))) {
221                         ast_log(LOG_WARNING, "Incomplete destination '%s' supplied.\n", tech);
222                         continue;
223                 }
224
225                 /* Ensure device is not in use if skip option is enabled */
226                 if (ast_test_flag(&flags, PAGE_SKIP)) {
227                         state = ast_device_state(tech);
228                         if (state == AST_DEVICE_UNKNOWN) {
229                                 ast_log(LOG_WARNING, "Destination '%s' has device state '%s'. Paging anyway.\n", tech, ast_devstate2str(state));
230                         } else if (state != AST_DEVICE_NOT_INUSE) {
231                                 ast_log(LOG_WARNING, "Destination '%s' has device state '%s'.\n", tech, ast_devstate2str(state));
232                                 continue;
233                         }
234                 }
235
236                 *resource++ = '\0';
237
238                 /* Create a dialing structure */
239                 if (!(dial = ast_dial_create())) {
240                         ast_log(LOG_WARNING, "Failed to create dialing structure.\n");
241                         continue;
242                 }
243
244                 /* Append technology and resource */
245                 if (ast_dial_append(dial, tech, resource) == -1) {
246                         ast_log(LOG_ERROR, "Failed to add %s to outbound dial\n", tech);
247                         continue;
248                 }
249
250                 /* Set ANSWER_EXEC as global option */
251                 ast_dial_option_global_enable(dial, AST_DIAL_OPTION_ANSWER_EXEC, meetmeopts);
252
253                 if (timeout) {
254                         ast_dial_set_global_timeout(dial, timeout * 1000);
255                 }
256
257                 if (ast_test_flag(&flags, PAGE_IGNORE_FORWARDS)) {
258                         ast_dial_option_global_enable(dial, AST_DIAL_OPTION_DISABLE_CALL_FORWARDING, NULL);
259                 }
260
261                 /* Run this dial in async mode */
262                 ast_dial_run(dial, chan, 1);
263
264                 /* Put in our dialing array */
265                 dial_list[pos++] = dial;
266         }
267
268         if (!ast_test_flag(&flags, PAGE_QUIET)) {
269                 res = ast_streamfile(chan, "beep", ast_channel_language(chan));
270                 if (!res)
271                         res = ast_waitstream(chan, "");
272         }
273
274         if (!res) {
275                 /* Default behaviour */
276                 snprintf(meetmeopts, sizeof(meetmeopts), "%ud,A%s%sqxd", confid, (ast_test_flag(&flags, PAGE_DUPLEX) ? "" : "t"), 
277                         (ast_test_flag(&flags, PAGE_RECORD) ? "r" : "") );
278                 if (ast_test_flag(&flags, PAGE_ANNOUNCE) && !ast_strlen_zero(opts[OPT_ARG_ANNOUNCE]) &&
279                                 !ast_test_flag(&flags, PAGE_NOCALLERANNOUNCE)) {
280                         snprintf(meetmeopts, sizeof(meetmeopts), "%ud,A%s%sqxdG(%s)", confid, (ast_test_flag(&flags, PAGE_DUPLEX) ? "" : "t"), 
281                           (ast_test_flag(&flags, PAGE_RECORD) ? "r" : ""), opts[OPT_ARG_ANNOUNCE] );
282                 }
283                 pbx_exec(chan, app, meetmeopts);
284         }
285
286         /* Go through each dial attempt cancelling, joining, and destroying */
287         for (i = 0; i < pos; i++) {
288                 struct ast_dial *dial = dial_list[i];
289
290                 /* We have to wait for the async thread to exit as it's possible Meetme won't throw them out immediately */
291                 ast_dial_join(dial);
292
293                 /* Hangup all channels */
294                 ast_dial_hangup(dial);
295
296                 /* Destroy dialing structure */
297                 ast_dial_destroy(dial);
298         }
299
300         return -1;
301 }
302
303 static int unload_module(void)
304 {
305         return ast_unregister_application(app_page);
306 }
307
308 static int load_module(void)
309 {
310         return ast_register_application_xml(app_page, page_exec);
311 }
312
313 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Page Multiple Phones");
314