268ded2abcf829a17ad8f29f672bf7ead63a48a8
[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>zaptel</depend>
30  ***/
31
32 #include "asterisk.h"
33
34 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
35
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <unistd.h>
39 #include <string.h>
40 #include <errno.h>
41
42 #include "asterisk/options.h"
43 #include "asterisk/logger.h"
44 #include "asterisk/channel.h"
45 #include "asterisk/pbx.h"
46 #include "asterisk/module.h"
47 #include "asterisk/file.h"
48 #include "asterisk/app.h"
49 #include "asterisk/chanvars.h"
50 #include "asterisk/utils.h"
51
52 static const char *app_page= "Page";
53
54 static const char *page_synopsis = "Pages phones";
55
56 static const char *page_descrip =
57 "Page(Technology/Resource&Technology2/Resource2[|options])\n"
58 "  Places outbound calls to the given technology / resource and dumps\n"
59 "them into a conference bridge as muted participants.  The original\n"
60 "caller is dumped into the conference as a speaker and the room is\n"
61 "destroyed when the original caller leaves.  Valid options are:\n"
62 "        d - full duplex audio\n"
63 "        q - quiet, do not play beep to caller\n"
64 "        r - record the page into a file (see 'r' for app_meetme)\n";
65
66
67 enum {
68         PAGE_DUPLEX = (1 << 0),
69         PAGE_QUIET = (1 << 1),
70         PAGE_RECORD = (1 << 2),
71 } page_opt_flags;
72
73 AST_APP_OPTIONS(page_opts, {
74         AST_APP_OPTION('d', PAGE_DUPLEX),
75         AST_APP_OPTION('q', PAGE_QUIET),
76         AST_APP_OPTION('r', PAGE_RECORD),
77 });
78
79 struct calloutdata {
80         char cidnum[64];
81         char cidname[64];
82         char tech[64];
83         char resource[256];
84         char meetmeopts[64];
85         struct ast_variable *variables;
86 };
87
88 static void *page_thread(void *data)
89 {
90         struct calloutdata *cd = data;
91         ast_pbx_outgoing_app(cd->tech, AST_FORMAT_SLINEAR, cd->resource, 30000,
92                 "MeetMe", cd->meetmeopts, NULL, 0, cd->cidnum, cd->cidname, cd->variables, NULL, NULL);
93         free(cd);
94         return NULL;
95 }
96
97 static void launch_page(struct ast_channel *chan, const char *meetmeopts, const char *tech, const char *resource)
98 {
99         struct calloutdata *cd;
100         const char *varname;
101         struct ast_variable *lastvar = NULL;
102         struct ast_var_t *varptr;
103         pthread_t t;
104         pthread_attr_t attr;
105         if ((cd = ast_calloc(1, sizeof(*cd)))) {
106                 ast_copy_string(cd->cidnum, chan->cid.cid_num ? chan->cid.cid_num : "", sizeof(cd->cidnum));
107                 ast_copy_string(cd->cidname, chan->cid.cid_name ? chan->cid.cid_name : "", sizeof(cd->cidname));
108                 ast_copy_string(cd->tech, tech, sizeof(cd->tech));
109                 ast_copy_string(cd->resource, resource, sizeof(cd->resource));
110                 ast_copy_string(cd->meetmeopts, meetmeopts, sizeof(cd->meetmeopts));
111
112                 AST_LIST_TRAVERSE(&chan->varshead, varptr, entries) {
113                         if (!(varname = ast_var_full_name(varptr)))
114                                 continue;
115                         if (varname[0] == '_') {
116                                 struct ast_variable *newvar = NULL;
117
118                                 if (varname[1] == '_') {
119                                         newvar = ast_variable_new(varname, ast_var_value(varptr));
120                                 } else {
121                                         newvar = ast_variable_new(&varname[1], ast_var_value(varptr));
122                                 }
123
124                                 if (newvar) {
125                                         if (lastvar)
126                                                 lastvar->next = newvar;
127                                         else
128                                                 cd->variables = newvar;
129                                         lastvar = newvar;
130                                 }
131                         }
132                 }
133
134                 pthread_attr_init(&attr);
135                 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
136                 if (ast_pthread_create(&t, &attr, page_thread, cd)) {
137                         ast_log(LOG_WARNING, "Unable to create paging thread: %s\n", strerror(errno));
138                         free(cd);
139                 }
140         }
141 }
142
143 static int page_exec(struct ast_channel *chan, void *data)
144 {
145         struct ast_module_user *u;
146         char *options;
147         char *tech, *resource;
148         char meetmeopts[80];
149         struct ast_flags flags = { 0 };
150         unsigned int confid = ast_random();
151         struct ast_app *app;
152         char *tmp;
153         int res=0;
154         char originator[AST_CHANNEL_NAME];
155
156         if (ast_strlen_zero(data)) {
157                 ast_log(LOG_WARNING, "This application requires at least one argument (destination(s) to page)\n");
158                 return -1;
159         }
160
161         u = ast_module_user_add(chan);
162
163         if (!(app = pbx_findapp("MeetMe"))) {
164                 ast_log(LOG_WARNING, "There is no MeetMe application available!\n");
165                 ast_module_user_remove(u);
166                 return -1;
167         };
168
169         options = ast_strdupa(data);
170
171         ast_copy_string(originator, chan->name, sizeof(originator));
172         if ((tmp = strchr(originator, '-')))
173                 *tmp = '\0';
174
175         tmp = strsep(&options, "|");
176         if (options)
177                 ast_app_parse_options(page_opts, &flags, NULL, options);
178
179         snprintf(meetmeopts, sizeof(meetmeopts), "%ud|%s%sqxdw(5)", confid, (ast_test_flag(&flags, PAGE_DUPLEX) ? "" : "m"),
180                 (ast_test_flag(&flags, PAGE_RECORD) ? "r" : "") );
181
182         while ((tech = strsep(&tmp, "&"))) {
183                 /* don't call the originating device */
184                 if (!strcasecmp(tech, originator))
185                         continue;
186
187                 if ((resource = strchr(tech, '/'))) {
188                         *resource++ = '\0';
189                         launch_page(chan, meetmeopts, tech, resource);
190                 } else {
191                         ast_log(LOG_WARNING, "Incomplete destination '%s' supplied.\n", tech);
192                 }
193         }
194
195         if (!ast_test_flag(&flags, PAGE_QUIET)) {
196                 res = ast_streamfile(chan, "beep", chan->language);
197                 if (!res)
198                         res = ast_waitstream(chan, "");
199         }
200         if (!res) {
201                 snprintf(meetmeopts, sizeof(meetmeopts), "%ud|A%s%sqxd", confid, (ast_test_flag(&flags, PAGE_DUPLEX) ? "" : "t"), 
202                         (ast_test_flag(&flags, PAGE_RECORD) ? "r" : "") );
203                 pbx_exec(chan, app, meetmeopts);
204         }
205
206         ast_module_user_remove(u);
207
208         return -1;
209 }
210
211 static int unload_module(void)
212 {
213         int res;
214
215         res =  ast_unregister_application(app_page);
216
217         ast_module_user_hangup_all();
218
219         return res;
220 }
221
222 static int load_module(void)
223 {
224         return ast_register_application(app_page, page_exec, page_synopsis, page_descrip);
225 }
226
227 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Page Multiple Phones");
228