Optionally record audio of the page command for re-pages/playback. #6827 (JeffSaxe)
[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 <stdio.h>
33 #include <stdlib.h>
34 #include <unistd.h>
35 #include <string.h>
36 #include <errno.h>
37
38 #include "asterisk.h"
39
40 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
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 -- you may want to first Set(MEETME_RECORDINGFILE=ReplayLastPage)\n";
65
66 LOCAL_USER_DECL;
67
68 enum {
69         PAGE_DUPLEX = (1 << 0),
70         PAGE_QUIET = (1 << 1),
71         PAGE_RECORD = (1 << 2),
72 } page_opt_flags;
73
74 AST_APP_OPTIONS(page_opts, {
75         AST_APP_OPTION('d', PAGE_DUPLEX),
76         AST_APP_OPTION('q', PAGE_QUIET),
77         AST_APP_OPTION('r', PAGE_RECORD),
78 });
79
80 struct calloutdata {
81         char cidnum[64];
82         char cidname[64];
83         char tech[64];
84         char resource[256];
85         char meetmeopts[64];
86         struct ast_variable *variables;
87 };
88
89 static void *page_thread(void *data)
90 {
91         struct calloutdata *cd = data;
92         ast_pbx_outgoing_app(cd->tech, AST_FORMAT_SLINEAR, cd->resource, 30000,
93                 "MeetMe", cd->meetmeopts, NULL, 0, cd->cidnum, cd->cidname, cd->variables, NULL, NULL);
94         free(cd);
95         return NULL;
96 }
97
98 static void launch_page(struct ast_channel *chan, const char *meetmeopts, const char *tech, const char *resource)
99 {
100         struct calloutdata *cd;
101         const char *varname;
102         struct ast_variable *lastvar = NULL;
103         struct ast_var_t *varptr;
104         pthread_t t;
105         pthread_attr_t attr;
106         if ((cd = ast_calloc(1, sizeof(*cd)))) {
107                 ast_copy_string(cd->cidnum, chan->cid.cid_num ? chan->cid.cid_num : "", sizeof(cd->cidnum));
108                 ast_copy_string(cd->cidname, chan->cid.cid_name ? chan->cid.cid_name : "", sizeof(cd->cidname));
109                 ast_copy_string(cd->tech, tech, sizeof(cd->tech));
110                 ast_copy_string(cd->resource, resource, sizeof(cd->resource));
111                 ast_copy_string(cd->meetmeopts, meetmeopts, sizeof(cd->meetmeopts));
112
113                 AST_LIST_TRAVERSE(&chan->varshead, varptr, entries) {
114                         if (!(varname = ast_var_full_name(varptr)))
115                                 continue;
116                         if (varname[0] == '_') {
117                                 struct ast_variable *newvar = NULL;
118
119                                 if (varname[1] == '_') {
120                                         newvar = ast_variable_new(varname, ast_var_value(varptr));
121                                 } else {
122                                         newvar = ast_variable_new(&varname[1], ast_var_value(varptr));
123                                 }
124
125                                 if (newvar) {
126                                         if (lastvar)
127                                                 lastvar->next = newvar;
128                                         else
129                                                 cd->variables = newvar;
130                                         lastvar = newvar;
131                                 }
132                         }
133                 }
134
135                 pthread_attr_init(&attr);
136                 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
137                 if (ast_pthread_create(&t, &attr, page_thread, cd)) {
138                         ast_log(LOG_WARNING, "Unable to create paging thread: %s\n", strerror(errno));
139                         free(cd);
140                 }
141         }
142 }
143
144 static int page_exec(struct ast_channel *chan, void *data)
145 {
146         struct localuser *u;
147         char *options;
148         char *tech, *resource;
149         char meetmeopts[80];
150         struct ast_flags flags = { 0 };
151         unsigned int confid = ast_random();
152         struct ast_app *app;
153         char *tmp;
154         int res=0;
155         char originator[AST_CHANNEL_NAME];
156
157         if (ast_strlen_zero(data)) {
158                 ast_log(LOG_WARNING, "This application requires at least one argument (destination(s) to page)\n");
159                 return -1;
160         }
161
162         LOCAL_USER_ADD(u);
163
164         if (!(app = pbx_findapp("MeetMe"))) {
165                 ast_log(LOG_WARNING, "There is no MeetMe application available!\n");
166                 LOCAL_USER_REMOVE(u);
167                 return -1;
168         };
169
170         if (!(options = ast_strdupa(data))) {
171                 LOCAL_USER_REMOVE(u);
172                 return -1;
173         }
174
175         ast_copy_string(originator, chan->name, sizeof(originator));
176         if ((tmp = strchr(originator, '-')))
177                 *tmp = '\0';
178
179         tmp = strsep(&options, "|");
180         if (options)
181                 ast_app_parse_options(page_opts, &flags, NULL, options);
182
183         snprintf(meetmeopts, sizeof(meetmeopts), "%ud|%s%sqxdw", confid, (ast_test_flag(&flags, PAGE_DUPLEX) ? "" : "m"),
184                 (ast_test_flag(&flags, PAGE_RECORD) ? "r" : "") );
185
186         while ((tech = strsep(&tmp, "&"))) {
187                 /* don't call the originating device */
188                 if (!strcasecmp(tech, originator))
189                         continue;
190
191                 if ((resource = strchr(tech, '/'))) {
192                         *resource++ = '\0';
193                         launch_page(chan, meetmeopts, tech, resource);
194                 } else {
195                         ast_log(LOG_WARNING, "Incomplete destination '%s' supplied.\n", tech);
196                 }
197         }
198
199         if (!ast_test_flag(&flags, PAGE_QUIET)) {
200                 res = ast_streamfile(chan, "beep", chan->language);
201                 if (!res)
202                         res = ast_waitstream(chan, "");
203         }
204         if (!res) {
205                 snprintf(meetmeopts, sizeof(meetmeopts), "%ud|A%s%sqxd", confid, (ast_test_flag(&flags, PAGE_DUPLEX) ? "" : "t"), 
206                         (ast_test_flag(&flags, PAGE_RECORD) ? "r" : "") );
207                 pbx_exec(chan, app, meetmeopts);
208         }
209
210         LOCAL_USER_REMOVE(u);
211
212         return -1;
213 }
214
215 static int unload_module(void *mod)
216 {
217         int res;
218
219         res =  ast_unregister_application(app_page);
220
221         STANDARD_HANGUP_LOCALUSERS;
222
223         return res;
224 }
225
226 static int load_module(void *mod)
227 {
228         return ast_register_application(app_page, page_exec, page_synopsis, page_descrip);
229 }
230
231 static const char *description(void)
232 {
233         return "Page Multiple Phones";
234 }
235
236 static const char *key(void)
237 {
238         return ASTERISK_GPL_KEY;
239 }
240
241 STD_MOD1;
242