00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #include "asterisk.h"
00034
00035 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 380894 $")
00036
00037 #include "asterisk/channel.h"
00038 #include "asterisk/pbx.h"
00039 #include "asterisk/module.h"
00040 #include "asterisk/file.h"
00041 #include "asterisk/app.h"
00042 #include "asterisk/chanvars.h"
00043 #include "asterisk/utils.h"
00044 #include "asterisk/devicestate.h"
00045 #include "asterisk/dial.h"
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111 static const char * const app_page= "Page";
00112
00113 enum page_opt_flags {
00114 PAGE_DUPLEX = (1 << 0),
00115 PAGE_QUIET = (1 << 1),
00116 PAGE_RECORD = (1 << 2),
00117 PAGE_SKIP = (1 << 3),
00118 PAGE_IGNORE_FORWARDS = (1 << 4),
00119 PAGE_ANNOUNCE = (1 << 5),
00120 PAGE_NOCALLERANNOUNCE = (1 << 6),
00121 };
00122
00123 enum {
00124 OPT_ARG_ANNOUNCE = 0,
00125 OPT_ARG_ARRAY_SIZE = 1,
00126 };
00127
00128 AST_APP_OPTIONS(page_opts, {
00129 AST_APP_OPTION('d', PAGE_DUPLEX),
00130 AST_APP_OPTION('q', PAGE_QUIET),
00131 AST_APP_OPTION('r', PAGE_RECORD),
00132 AST_APP_OPTION('s', PAGE_SKIP),
00133 AST_APP_OPTION('i', PAGE_IGNORE_FORWARDS),
00134 AST_APP_OPTION_ARG('A', PAGE_ANNOUNCE, OPT_ARG_ANNOUNCE),
00135 AST_APP_OPTION('n', PAGE_NOCALLERANNOUNCE),
00136 });
00137
00138
00139 struct page_options {
00140 char *opts[OPT_ARG_ARRAY_SIZE];
00141 struct ast_flags flags;
00142 };
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153 static void setup_profile_bridge(struct ast_channel *chan, struct page_options *options)
00154 {
00155
00156 ast_func_write(chan, "CONFBRIDGE(bridge,template)", "");
00157 if (ast_test_flag(&options->flags, PAGE_RECORD)) {
00158 ast_func_write(chan, "CONFBRIDGE(bridge,record_conference)", "yes");
00159 }
00160 }
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171 static void setup_profile_paged(struct ast_channel *chan, struct page_options *options)
00172 {
00173
00174 ast_func_write(chan, "CONFBRIDGE(user,template)", "");
00175 ast_func_write(chan, "CONFBRIDGE(user,quiet)", "yes");
00176 ast_func_write(chan, "CONFBRIDGE(user,end_marked)", "yes");
00177 if (!ast_test_flag(&options->flags, PAGE_DUPLEX)) {
00178 ast_func_write(chan, "CONFBRIDGE(user,startmuted)", "yes");
00179 }
00180 if (ast_test_flag(&options->flags, PAGE_ANNOUNCE)
00181 && !ast_strlen_zero(options->opts[OPT_ARG_ANNOUNCE])) {
00182 ast_func_write(chan, "CONFBRIDGE(user,announcement)", options->opts[OPT_ARG_ANNOUNCE]);
00183 }
00184 }
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195 static void setup_profile_caller(struct ast_channel *chan, struct page_options *options)
00196 {
00197
00198 ast_func_write(chan, "CONFBRIDGE(user,template)", "");
00199 ast_func_write(chan, "CONFBRIDGE(user,quiet)", "yes");
00200 ast_func_write(chan, "CONFBRIDGE(user,marked)", "yes");
00201 if (!ast_test_flag(&options->flags, PAGE_NOCALLERANNOUNCE)
00202 && ast_test_flag(&options->flags, PAGE_ANNOUNCE)
00203 && !ast_strlen_zero(options->opts[OPT_ARG_ANNOUNCE])) {
00204 ast_func_write(chan, "CONFBRIDGE(user,announcement)", options->opts[OPT_ARG_ANNOUNCE]);
00205 }
00206 }
00207
00208 static void page_state_callback(struct ast_dial *dial)
00209 {
00210 struct ast_channel *chan;
00211 struct page_options *options;
00212
00213 if (ast_dial_state(dial) != AST_DIAL_RESULT_ANSWERED ||
00214 !(chan = ast_dial_answered(dial)) ||
00215 !(options = ast_dial_get_user_data(dial))) {
00216 return;
00217 }
00218
00219 setup_profile_bridge(chan, options);
00220 setup_profile_paged(chan, options);
00221 }
00222
00223 static int page_exec(struct ast_channel *chan, const char *data)
00224 {
00225 char *tech, *resource, *tmp;
00226 char confbridgeopts[128], originator[AST_CHANNEL_NAME];
00227 struct page_options options = { { 0, }, { 0, } };
00228 unsigned int confid = ast_random();
00229 struct ast_app *app;
00230 int res = 0, pos = 0, i = 0;
00231 struct ast_dial **dial_list;
00232 unsigned int num_dials;
00233 int timeout = 0;
00234 char *parse;
00235
00236 AST_DECLARE_APP_ARGS(args,
00237 AST_APP_ARG(devices);
00238 AST_APP_ARG(options);
00239 AST_APP_ARG(timeout);
00240 );
00241
00242 if (ast_strlen_zero(data)) {
00243 ast_log(LOG_WARNING, "This application requires at least one argument (destination(s) to page)\n");
00244 return -1;
00245 }
00246
00247 if (!(app = pbx_findapp("ConfBridge"))) {
00248 ast_log(LOG_WARNING, "There is no ConfBridge application available!\n");
00249 return -1;
00250 };
00251
00252 parse = ast_strdupa(data);
00253
00254 AST_STANDARD_APP_ARGS(args, parse);
00255
00256 ast_copy_string(originator, ast_channel_name(chan), sizeof(originator));
00257 if ((tmp = strchr(originator, '-'))) {
00258 *tmp = '\0';
00259 }
00260
00261 if (!ast_strlen_zero(args.options)) {
00262 ast_app_parse_options(page_opts, &options.flags, options.opts, args.options);
00263 }
00264
00265 if (!ast_strlen_zero(args.timeout)) {
00266 timeout = atoi(args.timeout);
00267 }
00268
00269 snprintf(confbridgeopts, sizeof(confbridgeopts), "ConfBridge,%u", confid);
00270
00271
00272 num_dials = 1;
00273 tmp = args.devices;
00274 while (*tmp) {
00275 if (*tmp == '&') {
00276 num_dials++;
00277 }
00278 tmp++;
00279 }
00280
00281 if (!(dial_list = ast_calloc(num_dials, sizeof(struct ast_dial *)))) {
00282 ast_log(LOG_ERROR, "Can't allocate %ld bytes for dial list\n", (long)(sizeof(struct ast_dial *) * num_dials));
00283 return -1;
00284 }
00285
00286
00287 while ((tech = strsep(&args.devices, "&"))) {
00288 int state = 0;
00289 struct ast_dial *dial = NULL;
00290
00291
00292 if (!strcasecmp(tech, originator))
00293 continue;
00294
00295
00296 if (!(resource = strchr(tech, '/'))) {
00297 ast_log(LOG_WARNING, "Incomplete destination '%s' supplied.\n", tech);
00298 continue;
00299 }
00300
00301
00302 if (ast_test_flag(&options.flags, PAGE_SKIP)) {
00303 state = ast_device_state(tech);
00304 if (state == AST_DEVICE_UNKNOWN) {
00305 ast_log(LOG_WARNING, "Destination '%s' has device state '%s'. Paging anyway.\n", tech, ast_devstate2str(state));
00306 } else if (state != AST_DEVICE_NOT_INUSE) {
00307 ast_log(LOG_WARNING, "Destination '%s' has device state '%s'.\n", tech, ast_devstate2str(state));
00308 continue;
00309 }
00310 }
00311
00312 *resource++ = '\0';
00313
00314
00315 if (!(dial = ast_dial_create())) {
00316 ast_log(LOG_WARNING, "Failed to create dialing structure.\n");
00317 continue;
00318 }
00319
00320
00321 if (ast_dial_append(dial, tech, resource) == -1) {
00322 ast_log(LOG_ERROR, "Failed to add %s to outbound dial\n", tech);
00323 ast_dial_destroy(dial);
00324 continue;
00325 }
00326
00327
00328 ast_dial_option_global_enable(dial, AST_DIAL_OPTION_ANSWER_EXEC, confbridgeopts);
00329
00330 if (timeout) {
00331 ast_dial_set_global_timeout(dial, timeout * 1000);
00332 }
00333
00334 if (ast_test_flag(&options.flags, PAGE_IGNORE_FORWARDS)) {
00335 ast_dial_option_global_enable(dial, AST_DIAL_OPTION_DISABLE_CALL_FORWARDING, NULL);
00336 }
00337
00338 ast_dial_set_state_callback(dial, &page_state_callback);
00339 ast_dial_set_user_data(dial, &options);
00340
00341
00342 ast_dial_run(dial, chan, 1);
00343
00344
00345 dial_list[pos++] = dial;
00346 }
00347
00348 if (!ast_test_flag(&options.flags, PAGE_QUIET)) {
00349 res = ast_streamfile(chan, "beep", ast_channel_language(chan));
00350 if (!res)
00351 res = ast_waitstream(chan, "");
00352 }
00353
00354 if (!res) {
00355 setup_profile_bridge(chan, &options);
00356 setup_profile_caller(chan, &options);
00357
00358 snprintf(confbridgeopts, sizeof(confbridgeopts), "%u", confid);
00359 pbx_exec(chan, app, confbridgeopts);
00360 }
00361
00362
00363 for (i = 0; i < pos; i++) {
00364 struct ast_dial *dial = dial_list[i];
00365
00366
00367 ast_dial_join(dial);
00368
00369
00370 ast_dial_hangup(dial);
00371
00372
00373 ast_dial_destroy(dial);
00374 }
00375
00376 ast_free(dial_list);
00377
00378 return -1;
00379 }
00380
00381 static int unload_module(void)
00382 {
00383 return ast_unregister_application(app_page);
00384 }
00385
00386 static int load_module(void)
00387 {
00388 return ast_register_application_xml(app_page, page_exec);
00389 }
00390
00391 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Page Multiple Phones");
00392