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