Mon Jun 18 2012 03:55:19

Asterisk developer's documentation


app_page.c
Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (c) 2004 - 2006 Digium, Inc.  All rights reserved.
00005  *
00006  * Mark Spencer <markster@digium.com>
00007  *
00008  * This code is released under the GNU General Public License
00009  * version 2.0.  See LICENSE for more information.
00010  *
00011  * See http://www.asterisk.org for more information about
00012  * the Asterisk project. Please do not directly contact
00013  * any of the maintainers of this project for assistance;
00014  * the project provides a web site, mailing lists and IRC
00015  * channels for your use.
00016  *
00017  */
00018 
00019 /*! \file
00020  *
00021  * \brief page() - Paging application
00022  *
00023  * \author Mark Spencer <markster@digium.com>
00024  *
00025  * \ingroup applications
00026  */
00027 
00028 /*** MODULEINFO
00029    <depend>dahdi</depend>
00030    <depend>app_meetme</depend>
00031    <support_level>core</support_level>
00032  ***/
00033 
00034 #include "asterisk.h"
00035 
00036 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 360363 $")
00037 
00038 #include "asterisk/channel.h"
00039 #include "asterisk/pbx.h"
00040 #include "asterisk/module.h"
00041 #include "asterisk/file.h"
00042 #include "asterisk/app.h"
00043 #include "asterisk/chanvars.h"
00044 #include "asterisk/utils.h"
00045 #include "asterisk/devicestate.h"
00046 #include "asterisk/dial.h"
00047 
00048 /*** DOCUMENTATION
00049    <application name="Page" language="en_US">
00050       <synopsis>
00051          Page series of phones
00052       </synopsis>
00053       <syntax>
00054          <parameter name="Technology/Resource" required="true" argsep="&amp;">
00055             <argument name="Technology/Resource" required="true">
00056                <para>Specification of the device(s) to dial. These must be in the format of
00057                <literal>Technology/Resource</literal>, where <replaceable>Technology</replaceable>
00058                represents a particular channel driver, and <replaceable>Resource</replaceable> represents a resource
00059                available to that particular channel driver.</para>
00060             </argument>
00061             <argument name="Technology2/Resource2" multiple="true">
00062                <para>Optional extra devices to dial inparallel</para>
00063                <para>If you need more then one enter them as Technology2/Resource2&amp;
00064                Technology3/Resourse3&amp;.....</para>
00065             </argument>
00066          </parameter>
00067          <parameter name="options">
00068             <optionlist>
00069                <option name="d">
00070                   <para>Full duplex audio</para>
00071                </option>
00072                <option name="i">
00073                   <para>Ignore attempts to forward the call</para>
00074                </option>
00075                <option name="q">
00076                   <para>Quiet, do not play beep to caller</para>
00077                </option>
00078                <option name="r">
00079                   <para>Record the page into a file (meetme option <literal>r</literal>)</para>
00080                </option>
00081                <option name="s">
00082                   <para>Only dial a channel if its device state says that it is <literal>NOT_INUSE</literal></para>
00083                </option>
00084                <option name="A">
00085                   <argument name="x" required="true">
00086                      <para>The announcement to playback in all devices</para>
00087                   </argument>
00088                   <para>Play an announcement simultaneously to all paged participants</para>
00089                </option>
00090                <option name="n">
00091                   <para>Do not play simultaneous announcement to caller (implies <literal>A(x)</literal>)</para>
00092                </option>
00093             </optionlist>
00094          </parameter>
00095          <parameter name="timeout">
00096             <para>Specify the length of time that the system will attempt to connect a call.
00097             After this duration, any intercom calls that have not been answered will be hung up by the
00098             system.</para>
00099          </parameter>
00100       </syntax>
00101       <description>
00102          <para>Places outbound calls to the given <replaceable>technology</replaceable>/<replaceable>resource</replaceable>
00103          and dumps them into a conference bridge as muted participants. The original
00104          caller is dumped into the conference as a speaker and the room is
00105          destroyed when the original callers leaves.</para>
00106       </description>
00107       <see-also>
00108          <ref type="application">MeetMe</ref>
00109       </see-also>
00110    </application>
00111  ***/
00112 static const char * const app_page= "Page";
00113 
00114 enum page_opt_flags {
00115    PAGE_DUPLEX = (1 << 0),
00116    PAGE_QUIET = (1 << 1),
00117    PAGE_RECORD = (1 << 2),
00118    PAGE_SKIP = (1 << 3),
00119    PAGE_IGNORE_FORWARDS = (1 << 4),
00120    PAGE_ANNOUNCE = (1 << 5),
00121    PAGE_NOCALLERANNOUNCE = (1 << 6),
00122 };
00123 
00124 enum {
00125    OPT_ARG_ANNOUNCE = 0,
00126    OPT_ARG_ARRAY_SIZE = 1,
00127 };
00128 
00129 AST_APP_OPTIONS(page_opts, {
00130    AST_APP_OPTION('d', PAGE_DUPLEX),
00131    AST_APP_OPTION('q', PAGE_QUIET),
00132    AST_APP_OPTION('r', PAGE_RECORD),
00133    AST_APP_OPTION('s', PAGE_SKIP),
00134    AST_APP_OPTION('i', PAGE_IGNORE_FORWARDS),
00135    AST_APP_OPTION_ARG('A', PAGE_ANNOUNCE, OPT_ARG_ANNOUNCE),
00136    AST_APP_OPTION('n', PAGE_NOCALLERANNOUNCE),
00137 });
00138 
00139 
00140 static int page_exec(struct ast_channel *chan, const char *data)
00141 {
00142    char *tech, *resource, *tmp;
00143    char meetmeopts[128], originator[AST_CHANNEL_NAME], *opts[OPT_ARG_ARRAY_SIZE];
00144    struct ast_flags flags = { 0 };
00145    unsigned int confid = ast_random();
00146    struct ast_app *app;
00147    int res = 0, pos = 0, i = 0;
00148    struct ast_dial **dial_list;
00149    unsigned int num_dials;
00150    int timeout = 0;
00151    char *parse;
00152 
00153    AST_DECLARE_APP_ARGS(args,
00154       AST_APP_ARG(devices);
00155       AST_APP_ARG(options);
00156       AST_APP_ARG(timeout);
00157    );
00158 
00159    if (ast_strlen_zero(data)) {
00160       ast_log(LOG_WARNING, "This application requires at least one argument (destination(s) to page)\n");
00161       return -1;
00162    }
00163 
00164    if (!(app = pbx_findapp("MeetMe"))) {
00165       ast_log(LOG_WARNING, "There is no MeetMe application available!\n");
00166       return -1;
00167    };
00168 
00169    parse = ast_strdupa(data);
00170 
00171    AST_STANDARD_APP_ARGS(args, parse);
00172 
00173    ast_copy_string(originator, chan->name, sizeof(originator));
00174    if ((tmp = strchr(originator, '-'))) {
00175       *tmp = '\0';
00176    }
00177 
00178    if (!ast_strlen_zero(args.options)) {
00179       ast_app_parse_options(page_opts, &flags, opts, args.options);
00180    }
00181 
00182    if (!ast_strlen_zero(args.timeout)) {
00183       timeout = atoi(args.timeout);
00184    }
00185 
00186    if (ast_test_flag(&flags, PAGE_ANNOUNCE) && !ast_strlen_zero(opts[OPT_ARG_ANNOUNCE])) {
00187       snprintf(meetmeopts, sizeof(meetmeopts), "MeetMe,%ud,%s%sqxdw(5)G(%s)", confid, (ast_test_flag(&flags, PAGE_DUPLEX) ? "" : "m"),
00188                    (ast_test_flag(&flags, PAGE_RECORD) ? "r" : ""), opts[OPT_ARG_ANNOUNCE] );
00189    } else {
00190       snprintf(meetmeopts, sizeof(meetmeopts), "MeetMe,%ud,%s%sqxdw(5)", confid, (ast_test_flag(&flags, PAGE_DUPLEX) ? "" : "m"),
00191       (ast_test_flag(&flags, PAGE_RECORD) ? "r" : "") );
00192    }
00193 
00194    /* Count number of extensions in list by number of ampersands + 1 */
00195    num_dials = 1;
00196    tmp = args.devices;
00197    while (*tmp) {
00198       if (*tmp == '&') {
00199          num_dials++;
00200       }
00201       tmp++;
00202    }
00203 
00204    if (!(dial_list = ast_calloc(num_dials, sizeof(struct ast_dial *)))) {
00205       ast_log(LOG_ERROR, "Can't allocate %ld bytes for dial list\n", (long)(sizeof(struct ast_dial *) * num_dials));
00206       return -1;
00207    }
00208 
00209    /* Go through parsing/calling each device */
00210    while ((tech = strsep(&args.devices, "&"))) {
00211       int state = 0;
00212       struct ast_dial *dial = NULL;
00213 
00214       /* don't call the originating device */
00215       if (!strcasecmp(tech, originator))
00216          continue;
00217 
00218       /* If no resource is available, continue on */
00219       if (!(resource = strchr(tech, '/'))) {
00220          ast_log(LOG_WARNING, "Incomplete destination '%s' supplied.\n", tech);
00221          continue;
00222       }
00223 
00224       /* Ensure device is not in use if skip option is enabled */
00225       if (ast_test_flag(&flags, PAGE_SKIP)) {
00226          state = ast_device_state(tech);
00227          if (state == AST_DEVICE_UNKNOWN) {
00228             ast_log(LOG_WARNING, "Destination '%s' has device state '%s'. Paging anyway.\n", tech, ast_devstate2str(state));
00229          } else if (state != AST_DEVICE_NOT_INUSE) {
00230             ast_log(LOG_WARNING, "Destination '%s' has device state '%s'.\n", tech, ast_devstate2str(state));
00231             continue;
00232          }
00233       }
00234 
00235       *resource++ = '\0';
00236 
00237       /* Create a dialing structure */
00238       if (!(dial = ast_dial_create())) {
00239          ast_log(LOG_WARNING, "Failed to create dialing structure.\n");
00240          continue;
00241       }
00242 
00243       /* Append technology and resource */
00244       if (ast_dial_append(dial, tech, resource) == -1) {
00245          ast_log(LOG_ERROR, "Failed to add %s to outbound dial\n", tech);
00246          continue;
00247       }
00248 
00249       /* Set ANSWER_EXEC as global option */
00250       ast_dial_option_global_enable(dial, AST_DIAL_OPTION_ANSWER_EXEC, meetmeopts);
00251 
00252       if (timeout) {
00253          ast_dial_set_global_timeout(dial, timeout * 1000);
00254       }
00255 
00256       if (ast_test_flag(&flags, PAGE_IGNORE_FORWARDS)) {
00257          ast_dial_option_global_enable(dial, AST_DIAL_OPTION_DISABLE_CALL_FORWARDING, NULL);
00258       }
00259 
00260       /* Run this dial in async mode */
00261       ast_dial_run(dial, chan, 1);
00262 
00263       /* Put in our dialing array */
00264       dial_list[pos++] = dial;
00265    }
00266 
00267    if (!ast_test_flag(&flags, PAGE_QUIET)) {
00268       res = ast_streamfile(chan, "beep", chan->language);
00269       if (!res)
00270          res = ast_waitstream(chan, "");
00271    }
00272 
00273    if (!res) {
00274       /* Default behaviour */
00275       snprintf(meetmeopts, sizeof(meetmeopts), "%ud,A%s%sqxd", confid, (ast_test_flag(&flags, PAGE_DUPLEX) ? "" : "t"), 
00276          (ast_test_flag(&flags, PAGE_RECORD) ? "r" : "") );
00277       if (ast_test_flag(&flags, PAGE_ANNOUNCE) && !ast_strlen_zero(opts[OPT_ARG_ANNOUNCE]) &&
00278             !ast_test_flag(&flags, PAGE_NOCALLERANNOUNCE)) {
00279          snprintf(meetmeopts, sizeof(meetmeopts), "%ud,A%s%sqxdG(%s)", confid, (ast_test_flag(&flags, PAGE_DUPLEX) ? "" : "t"), 
00280            (ast_test_flag(&flags, PAGE_RECORD) ? "r" : ""), opts[OPT_ARG_ANNOUNCE] );
00281       }
00282       pbx_exec(chan, app, meetmeopts);
00283    }
00284 
00285    /* Go through each dial attempt cancelling, joining, and destroying */
00286    for (i = 0; i < pos; i++) {
00287       struct ast_dial *dial = dial_list[i];
00288 
00289       /* We have to wait for the async thread to exit as it's possible Meetme won't throw them out immediately */
00290       ast_dial_join(dial);
00291 
00292       /* Hangup all channels */
00293       ast_dial_hangup(dial);
00294 
00295       /* Destroy dialing structure */
00296       ast_dial_destroy(dial);
00297    }
00298 
00299    ast_free(dial_list);
00300 
00301    return -1;
00302 }
00303 
00304 static int unload_module(void)
00305 {
00306    return ast_unregister_application(app_page);
00307 }
00308 
00309 static int load_module(void)
00310 {
00311    return ast_register_application_xml(app_page, page_exec);
00312 }
00313 
00314 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Page Multiple Phones");
00315