1884f4347f94ef80e86c6e937c770d2d42ae4fd1
[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  ***/
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 (meetme 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                                 </optionlist>
84                         </parameter>
85                         <parameter name="timeout">
86                                 <para>Specify the length of time that the system will attempt to connect a call.
87                                 After this duration, any intercom calls that have not been answered will be hung up by the
88                                 system.</para>
89                         </parameter>
90                 </syntax>
91                 <description>
92                         <para>Places outbound calls to the given <replaceable>technology</replaceable>/<replaceable>resource</replaceable>
93                         and dumps them into a conference bridge as muted participants. The original
94                         caller is dumped into the conference as a speaker and the room is
95                         destroyed when the original callers leaves.</para>
96                 </description>
97                 <see-also>
98                         <ref type="application">MeetMe</ref>
99                 </see-also>
100         </application>
101  ***/
102 static const char *app_page= "Page";
103
104 enum {
105         PAGE_DUPLEX = (1 << 0),
106         PAGE_QUIET = (1 << 1),
107         PAGE_RECORD = (1 << 2),
108         PAGE_SKIP = (1 << 3),
109         PAGE_IGNORE_FORWARDS = (1 << 4),
110 } page_opt_flags;
111
112 AST_APP_OPTIONS(page_opts, {
113         AST_APP_OPTION('d', PAGE_DUPLEX),
114         AST_APP_OPTION('q', PAGE_QUIET),
115         AST_APP_OPTION('r', PAGE_RECORD),
116         AST_APP_OPTION('s', PAGE_SKIP),
117         AST_APP_OPTION('i', PAGE_IGNORE_FORWARDS),
118 });
119
120 #define MAX_DIALS 128
121
122 static int page_exec(struct ast_channel *chan, void *data)
123 {
124         char *tech, *resource, *tmp;
125         char meetmeopts[88], originator[AST_CHANNEL_NAME], *opts[0];
126         struct ast_flags flags = { 0 };
127         unsigned int confid = ast_random();
128         struct ast_app *app;
129         int res = 0, pos = 0, i = 0;
130         struct ast_dial *dials[MAX_DIALS];
131         int timeout = 0;
132         char *parse;
133
134         AST_DECLARE_APP_ARGS(args,
135                 AST_APP_ARG(devices);
136                 AST_APP_ARG(options);
137                 AST_APP_ARG(timeout);
138         );
139
140         if (ast_strlen_zero(data)) {
141                 ast_log(LOG_WARNING, "This application requires at least one argument (destination(s) to page)\n");
142                 return -1;
143         }
144
145         if (!(app = pbx_findapp("MeetMe"))) {
146                 ast_log(LOG_WARNING, "There is no MeetMe application available!\n");
147                 return -1;
148         };
149
150         parse = ast_strdupa(data);
151
152         AST_STANDARD_APP_ARGS(args, parse);
153
154         ast_copy_string(originator, chan->name, sizeof(originator));
155         if ((tmp = strchr(originator, '-'))) {
156                 *tmp = '\0';
157         }
158
159         if (!ast_strlen_zero(args.options)) {
160                 ast_app_parse_options(page_opts, &flags, opts, args.options);
161         }
162
163         if (!ast_strlen_zero(args.timeout)) {
164                 timeout = atoi(args.timeout);
165         }
166
167         snprintf(meetmeopts, sizeof(meetmeopts), "MeetMe,%ud,%s%sqxdw(5)", confid, (ast_test_flag(&flags, PAGE_DUPLEX) ? "" : "m"),
168                 (ast_test_flag(&flags, PAGE_RECORD) ? "r" : "") );
169
170         /* Go through parsing/calling each device */
171         while ((tech = strsep(&args.devices, "&"))) {
172                 int state = 0;
173                 struct ast_dial *dial = NULL;
174
175                 /* don't call the originating device */
176                 if (!strcasecmp(tech, originator))
177                         continue;
178
179                 /* If no resource is available, continue on */
180                 if (!(resource = strchr(tech, '/'))) {
181                         ast_log(LOG_WARNING, "Incomplete destination '%s' supplied.\n", tech);
182                         continue;
183                 }
184
185                 /* Ensure device is not in use if skip option is enabled */
186                 if (ast_test_flag(&flags, PAGE_SKIP)) {
187                         state = ast_device_state(tech);
188                         if (state == AST_DEVICE_UNKNOWN) {
189                                 ast_log(LOG_WARNING, "Destination '%s' has device state '%s'. Paging anyway.\n", tech, ast_devstate2str(state));
190                         } else if (state != AST_DEVICE_NOT_INUSE) {
191                                 ast_log(LOG_WARNING, "Destination '%s' has device state '%s'.\n", tech, ast_devstate2str(state));
192                                 continue;
193                         }
194                 }
195
196                 *resource++ = '\0';
197
198                 /* Create a dialing structure */
199                 if (!(dial = ast_dial_create())) {
200                         ast_log(LOG_WARNING, "Failed to create dialing structure.\n");
201                         continue;
202                 }
203
204                 /* Append technology and resource */
205                 if (ast_dial_append(dial, tech, resource) == -1) {
206                         ast_log(LOG_ERROR, "Failed to add %s to outbound dial\n", tech);
207                         continue;
208                 }
209
210                 /* Set ANSWER_EXEC as global option */
211                 ast_dial_option_global_enable(dial, AST_DIAL_OPTION_ANSWER_EXEC, meetmeopts);
212
213                 if (timeout) {
214                         ast_dial_set_global_timeout(dial, timeout * 1000);
215                 }
216
217                 if (ast_test_flag(&flags, PAGE_IGNORE_FORWARDS)) {
218                         ast_dial_option_global_enable(dial, AST_DIAL_OPTION_DISABLE_CALL_FORWARDING, NULL);
219                 }
220
221                 /* Run this dial in async mode */
222                 ast_dial_run(dial, chan, 1);
223
224                 /* Put in our dialing array */
225                 dials[pos++] = dial;
226         }
227
228         if (!ast_test_flag(&flags, PAGE_QUIET)) {
229                 res = ast_streamfile(chan, "beep", chan->language);
230                 if (!res)
231                         res = ast_waitstream(chan, "");
232         }
233
234         if (!res) {
235                 snprintf(meetmeopts, sizeof(meetmeopts), "%ud,A%s%sqxd", confid, (ast_test_flag(&flags, PAGE_DUPLEX) ? "" : "t"), 
236                         (ast_test_flag(&flags, PAGE_RECORD) ? "r" : "") );
237                 pbx_exec(chan, app, meetmeopts);
238         }
239
240         /* Go through each dial attempt cancelling, joining, and destroying */
241         for (i = 0; i < pos; i++) {
242                 struct ast_dial *dial = dials[i];
243
244                 /* We have to wait for the async thread to exit as it's possible Meetme won't throw them out immediately */
245                 ast_dial_join(dial);
246
247                 /* Hangup all channels */
248                 ast_dial_hangup(dial);
249
250                 /* Destroy dialing structure */
251                 ast_dial_destroy(dial);
252         }
253
254         return -1;
255 }
256
257 static int unload_module(void)
258 {
259         return ast_unregister_application(app_page);
260 }
261
262 static int load_module(void)
263 {
264         return ast_register_application_xml(app_page, page_exec);
265 }
266
267 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Page Multiple Phones");
268