app_directed_pickup.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 2005, Joshua Colp
00005  *
00006  * Joshua Colp <jcolp@digium.com>
00007  *
00008  * Portions merged from app_pickupchan, which was
00009  * Copyright (C) 2008, Gary Cook
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  * This program is free software, distributed under the terms of
00018  * the GNU General Public License Version 2. See the LICENSE file
00019  * at the top of the source tree.
00020  */
00021 
00022 /*! \file
00023  *
00024  * \brief Directed Call Pickup Support
00025  *
00026  * \author Joshua Colp <jcolp@digium.com>
00027  * \author Gary Cook
00028  *
00029  * \ingroup applications
00030  */
00031 
00032 /*** MODULEINFO
00033    <support_level>core</support_level>
00034  ***/
00035 
00036 #include "asterisk.h"
00037 
00038 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 403013 $")
00039 
00040 #include "asterisk/file.h"
00041 #include "asterisk/channel.h"
00042 #include "asterisk/pbx.h"
00043 #include "asterisk/module.h"
00044 #include "asterisk/lock.h"
00045 #include "asterisk/app.h"
00046 #include "asterisk/pickup.h"
00047 #include "asterisk/manager.h"
00048 #include "asterisk/callerid.h"
00049 
00050 #define PICKUPMARK "PICKUPMARK"
00051 
00052 /*** DOCUMENTATION
00053    <application name="Pickup" language="en_US">
00054       <synopsis>
00055          Directed extension call pickup.
00056       </synopsis>
00057       <syntax>
00058          <parameter name="targets" argsep="&amp;">
00059             <argument name="extension" argsep="@" required="true">
00060                <para>Specification of the pickup target.</para>
00061                <argument name="extension" required="true"/>
00062                <argument name="context" />
00063             </argument>
00064             <argument name="extension2" argsep="@" multiple="true">
00065                <para>Additional specifications of pickup targets.</para>
00066                <argument name="extension2" required="true"/>
00067                <argument name="context2"/>
00068             </argument>
00069          </parameter>
00070       </syntax>
00071       <description>
00072          <para>This application can pickup a specified ringing channel.  The channel
00073          to pickup can be specified in the following ways.</para>
00074          <para>1) If no <replaceable>extension</replaceable> targets are specified,
00075          the application will pickup a channel matching the pickup group of the
00076          requesting channel.</para>
00077          <para>2) If the <replaceable>extension</replaceable> is specified with a
00078          <replaceable>context</replaceable> of the special string
00079          <literal>PICKUPMARK</literal> (for example 10@PICKUPMARK), the application
00080          will pickup a channel which has defined the channel variable
00081          <variable>PICKUPMARK</variable> with the same value as
00082          <replaceable>extension</replaceable> (in this example,
00083          <literal>10</literal>).</para>
00084          <para>3) If the <replaceable>extension</replaceable> is specified
00085          with or without a <replaceable>context</replaceable>, the channel with a
00086          matching <replaceable>extension</replaceable> and <replaceable>context</replaceable>
00087          will be picked up.  If no <replaceable>context</replaceable> is specified,
00088          the current context will be used.</para>
00089          <note><para>The <replaceable>extension</replaceable> is typically set on
00090          matching channels by the dial application that created the channel.  The
00091          <replaceable>context</replaceable> is set on matching channels by the
00092          channel driver for the device.</para></note>
00093       </description>
00094    </application>
00095    <application name="PickupChan" language="en_US">
00096       <synopsis>
00097          Pickup a ringing channel.
00098       </synopsis>
00099       <syntax >
00100          <parameter name="channel" argsep="&amp;" required="true">
00101             <argument name="channel" required="true" />
00102             <argument name="channel2" required="false" multiple="true" />
00103             <para>List of channel names or channel uniqueids to pickup if ringing.
00104                For example, a channel name could be <literal>SIP/bob</literal> or
00105                <literal>SIP/bob-00000000</literal> to find
00106                <literal>SIP/bob-00000000</literal>.
00107             </para>
00108          </parameter>
00109          <parameter name="options" required="false">
00110             <optionlist>
00111                <option name="p">
00112                   <para>Supplied channel names are prefixes.  For example,
00113                      <literal>SIP/bob</literal> will match
00114                      <literal>SIP/bob-00000000</literal> and
00115                      <literal>SIP/bobby-00000000</literal>.
00116                   </para>
00117                </option>
00118             </optionlist>
00119          </parameter>
00120       </syntax>
00121       <description>
00122          <para>Pickup a specified <replaceable>channel</replaceable> if ringing.</para>
00123       </description>
00124    </application>
00125  ***/
00126 
00127 static const char app[] = "Pickup";
00128 static const char app2[] = "PickupChan";
00129 
00130 struct pickup_by_name_args {
00131    /*! Channel attempting to pickup a call. */
00132    struct ast_channel *chan;
00133    /*! Channel uniqueid or partial channel name to match. */
00134    const char *name;
00135    /*! Length of partial channel name to match. */
00136    size_t len;
00137 };
00138 
00139 static int find_by_name(void *obj, void *arg, void *data, int flags)
00140 {
00141    struct ast_channel *target = obj;/*!< Potential pickup target */
00142    struct pickup_by_name_args *args = data;
00143 
00144    if (args->chan == target) {
00145       /* The channel attempting to pickup a call cannot pickup itself. */
00146       return 0;
00147    }
00148 
00149    ast_channel_lock(target);
00150    if (!strncasecmp(ast_channel_name(target), args->name, args->len)
00151       && ast_can_pickup(target)) {
00152       /* Return with the channel still locked on purpose */
00153       return CMP_MATCH | CMP_STOP;
00154    }
00155    ast_channel_unlock(target);
00156 
00157    return 0;
00158 }
00159 
00160 static int find_by_uniqueid(void *obj, void *arg, void *data, int flags)
00161 {
00162    struct ast_channel *target = obj;/*!< Potential pickup target */
00163    struct pickup_by_name_args *args = data;
00164 
00165    if (args->chan == target) {
00166       /* The channel attempting to pickup a call cannot pickup itself. */
00167       return 0;
00168    }
00169 
00170    ast_channel_lock(target);
00171    if (!strcasecmp(ast_channel_uniqueid(target), args->name)
00172       && ast_can_pickup(target)) {
00173       /* Return with the channel still locked on purpose */
00174       return CMP_MATCH | CMP_STOP;
00175    }
00176    ast_channel_unlock(target);
00177 
00178    return 0;
00179 }
00180 
00181 /*! \brief Helper Function to walk through ALL channels checking NAME and STATE */
00182 static struct ast_channel *find_by_channel(struct ast_channel *chan, const char *channame)
00183 {
00184    struct ast_channel *target;
00185    char *chkchan;
00186    struct pickup_by_name_args pickup_args;
00187 
00188    pickup_args.chan = chan;
00189 
00190    if (strchr(channame, '-')) {
00191       /*
00192        * Use the given channel name string as-is.  This allows a full channel
00193        * name with a typical sequence number to be used as well as still
00194        * allowing the odd partial channel name that has a '-' in it to still
00195        * work, i.e. Local/bob@en-phone.
00196        */
00197       pickup_args.len = strlen(channame);
00198       pickup_args.name = channame;
00199    } else {
00200       /*
00201        * Append a '-' for the comparison so we check the channel name less
00202        * a sequence number, i.e Find SIP/bob- and not SIP/bobby.
00203        */
00204       pickup_args.len = strlen(channame) + 1;
00205       chkchan = ast_alloca(pickup_args.len + 1);
00206       strcpy(chkchan, channame);/* Safe */
00207       strcat(chkchan, "-");
00208       pickup_args.name = chkchan;
00209    }
00210    target = ast_channel_callback(find_by_name, NULL, &pickup_args, 0);
00211    if (target) {
00212       return target;
00213    }
00214 
00215    /* Now try a search for uniqueid. */
00216    pickup_args.name = channame;
00217    pickup_args.len = 0;
00218    return ast_channel_callback(find_by_uniqueid, NULL, &pickup_args, 0);
00219 }
00220 
00221 /*! \brief Attempt to pick up named channel. */
00222 static int pickup_by_channel(struct ast_channel *chan, const char *name)
00223 {
00224    int res = -1;
00225    struct ast_channel *target;/*!< Potential pickup target */
00226 
00227    /* The found channel is already locked. */
00228    target = find_by_channel(chan, name);
00229    if (target) {
00230       res = ast_do_pickup(chan, target);
00231       ast_channel_unlock(target);
00232       target = ast_channel_unref(target);
00233    }
00234 
00235    return res;
00236 }
00237 
00238 /* Attempt to pick up specified extension with context */
00239 static int pickup_by_exten(struct ast_channel *chan, const char *exten, const char *context)
00240 {
00241    struct ast_channel *target = NULL;/*!< Potential pickup target */
00242    struct ast_channel_iterator *iter;
00243    int res = -1;
00244 
00245    if (!(iter = ast_channel_iterator_by_exten_new(exten, context))) {
00246       return -1;
00247    }
00248 
00249    while ((target = ast_channel_iterator_next(iter))) {
00250       ast_channel_lock(target);
00251       if ((chan != target) && ast_can_pickup(target)) {
00252          ast_log(LOG_NOTICE, "%s pickup by %s\n", ast_channel_name(target), ast_channel_name(chan));
00253          break;
00254       }
00255       ast_channel_unlock(target);
00256       target = ast_channel_unref(target);
00257    }
00258 
00259    ast_channel_iterator_destroy(iter);
00260 
00261    if (target) {
00262       res = ast_do_pickup(chan, target);
00263       ast_channel_unlock(target);
00264       target = ast_channel_unref(target);
00265    }
00266 
00267    return res;
00268 }
00269 
00270 static int find_by_mark(void *obj, void *arg, void *data, int flags)
00271 {
00272    struct ast_channel *target = obj;/*!< Potential pickup target */
00273    struct ast_channel *chan = arg;
00274    const char *mark = data;
00275    const char *tmp;
00276 
00277    if (chan == target) {
00278       /* The channel attempting to pickup a call cannot pickup itself. */
00279       return 0;
00280    }
00281 
00282    ast_channel_lock(target);
00283    tmp = pbx_builtin_getvar_helper(target, PICKUPMARK);
00284    if (tmp && !strcasecmp(tmp, mark) && ast_can_pickup(target)) {
00285       /* Return with the channel still locked on purpose */
00286       return CMP_MATCH | CMP_STOP;
00287    }
00288    ast_channel_unlock(target);
00289 
00290    return 0;
00291 }
00292 
00293 /* Attempt to pick up specified mark */
00294 static int pickup_by_mark(struct ast_channel *chan, const char *mark)
00295 {
00296    struct ast_channel *target;/*!< Potential pickup target */
00297    int res = -1;
00298 
00299    /* The found channel is already locked. */
00300    target = ast_channel_callback(find_by_mark, chan, (char *) mark, 0);
00301    if (target) {
00302       res = ast_do_pickup(chan, target);
00303       ast_channel_unlock(target);
00304       target = ast_channel_unref(target);
00305    }
00306 
00307    return res;
00308 }
00309 
00310 static int pickup_by_group(struct ast_channel *chan)
00311 {
00312    struct ast_channel *target;/*!< Potential pickup target */
00313    int res = -1;
00314 
00315    /* The found channel is already locked. */
00316    target = ast_pickup_find_by_group(chan);
00317    if (target) {
00318       ast_log(LOG_NOTICE, "pickup %s attempt by %s\n", ast_channel_name(target), ast_channel_name(chan));
00319       res = ast_do_pickup(chan, target);
00320       ast_channel_unlock(target);
00321       target = ast_channel_unref(target);
00322    }
00323 
00324    return res;
00325 }
00326 
00327 /* application entry point for Pickup() */
00328 static int pickup_exec(struct ast_channel *chan, const char *data)
00329 {
00330    char *parse;
00331    char *exten;
00332    char *context;
00333 
00334    if (ast_strlen_zero(data)) {
00335       return pickup_by_group(chan) ? 0 : -1;
00336    }
00337 
00338    /* Parse extension (and context if there) */
00339    parse = ast_strdupa(data);
00340    for (;;) {
00341       if (ast_strlen_zero(parse)) {
00342          break;
00343       }
00344       exten = strsep(&parse, "&");
00345       if (ast_strlen_zero(exten)) {
00346          continue;
00347       }
00348 
00349       context = strchr(exten, '@');
00350       if (context) {
00351          *context++ = '\0';
00352       }
00353       if (!ast_strlen_zero(context) && !strcasecmp(context, PICKUPMARK)) {
00354          if (!pickup_by_mark(chan, exten)) {
00355             /* Pickup successful.  Stop the dialplan this channel is a zombie. */
00356             return -1;
00357          }
00358       } else {
00359          if (ast_strlen_zero(context)) {
00360             context = (char *) ast_channel_context(chan);
00361          }
00362          if (!pickup_by_exten(chan, exten, context)) {
00363             /* Pickup successful.  Stop the dialplan this channel is a zombie. */
00364             return -1;
00365          }
00366       }
00367       ast_log(LOG_NOTICE, "No target channel found for %s@%s.\n", exten, context);
00368    }
00369 
00370    /* Pickup failed.  Keep going in the dialplan. */
00371    return 0;
00372 }
00373 
00374 /* Find channel for pick up specified by partial channel name */
00375 static struct ast_channel *find_by_part(struct ast_channel *chan, const char *part)
00376 {
00377    struct ast_channel *target;
00378    struct pickup_by_name_args pickup_args;
00379 
00380    pickup_args.chan = chan;
00381 
00382    /* Try a partial channel name search. */
00383    pickup_args.name = part;
00384    pickup_args.len = strlen(part);
00385    target = ast_channel_callback(find_by_name, NULL, &pickup_args, 0);
00386    if (target) {
00387       return target;
00388    }
00389 
00390    /* Now try a search for uniqueid. */
00391    pickup_args.name = part;
00392    pickup_args.len = 0;
00393    return ast_channel_callback(find_by_uniqueid, NULL, &pickup_args, 0);
00394 }
00395 
00396 /* Attempt to pick up specified by partial channel name */
00397 static int pickup_by_part(struct ast_channel *chan, const char *part)
00398 {
00399    struct ast_channel *target;/*!< Potential pickup target */
00400    int res = -1;
00401 
00402    /* The found channel is already locked. */
00403    target = find_by_part(chan, part);
00404    if (target) {
00405       res = ast_do_pickup(chan, target);
00406       ast_channel_unlock(target);
00407       target = ast_channel_unref(target);
00408    }
00409 
00410    return res;
00411 }
00412 
00413 enum OPT_PICKUPCHAN_FLAGS {
00414    OPT_PICKUPCHAN_PARTIAL =   (1 << 0),   /* Channel name is a partial name. */
00415 };
00416 
00417 AST_APP_OPTIONS(pickupchan_opts, BEGIN_OPTIONS
00418    AST_APP_OPTION('p', OPT_PICKUPCHAN_PARTIAL),
00419 END_OPTIONS);
00420 
00421 /* application entry point for PickupChan() */
00422 static int pickupchan_exec(struct ast_channel *chan, const char *data)
00423 {
00424    char *pickup = NULL;
00425    char *parse = ast_strdupa(data);
00426    AST_DECLARE_APP_ARGS(args,
00427       AST_APP_ARG(channel);
00428       AST_APP_ARG(options);
00429       AST_APP_ARG(other);  /* Any remining unused arguments */
00430    );
00431    struct ast_flags opts;
00432 
00433    AST_STANDARD_APP_ARGS(args, parse);
00434 
00435    if (ast_strlen_zero(args.channel)) {
00436       ast_log(LOG_WARNING, "PickupChan requires an argument (channel)!\n");
00437       /* Pickup failed.  Keep going in the dialplan. */
00438       return 0;
00439    }
00440    if (ast_app_parse_options(pickupchan_opts, &opts, NULL, args.options)) {
00441       /*
00442        * General invalid option syntax.
00443        * Pickup failed.  Keep going in the dialplan.
00444        */
00445       return 0;
00446    }
00447 
00448    /* Parse channel */
00449    for (;;) {
00450       if (ast_strlen_zero(args.channel)) {
00451          break;
00452       }
00453       pickup = strsep(&args.channel, "&");
00454       if (ast_strlen_zero(pickup)) {
00455          continue;
00456       }
00457 
00458       if (ast_test_flag(&opts, OPT_PICKUPCHAN_PARTIAL)) {
00459          if (!pickup_by_part(chan, pickup)) {
00460             /* Pickup successful.  Stop the dialplan this channel is a zombie. */
00461             return -1;
00462          }
00463       } else if (!pickup_by_channel(chan, pickup)) {
00464          /* Pickup successful.  Stop the dialplan this channel is a zombie. */
00465          return -1;
00466       }
00467       ast_log(LOG_NOTICE, "No target channel found for %s.\n", pickup);
00468    }
00469 
00470    /* Pickup failed.  Keep going in the dialplan. */
00471    return 0;
00472 }
00473 
00474 static int unload_module(void)
00475 {
00476    int res;
00477 
00478    res = ast_unregister_application(app);
00479    res |= ast_unregister_application(app2);
00480 
00481    return res;
00482 }
00483 
00484 static int load_module(void)
00485 {
00486    int res;
00487 
00488    res = ast_register_application_xml(app, pickup_exec);
00489    res |= ast_register_application_xml(app2, pickupchan_exec);
00490 
00491    return res;
00492 }
00493 
00494 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Directed Call Pickup Application");

Generated on Thu Apr 16 06:27:09 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6