Sat Apr 26 2014 22:01:26

Asterisk developer's documentation


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: 373220 $")
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/features.h"
00047 #include "asterisk/manager.h"
00048 #include "asterisk/callerid.h"
00049 #include "asterisk/cel.h"
00050 
00051 #define PICKUPMARK "PICKUPMARK"
00052 
00053 /*** DOCUMENTATION
00054    <application name="Pickup" language="en_US">
00055       <synopsis>
00056          Directed extension call pickup.
00057       </synopsis>
00058       <syntax>
00059          <parameter name="targets" argsep="&amp;">
00060             <argument name="extension" argsep="@" required="true">
00061                <para>Specification of the pickup target.</para>
00062                <argument name="extension" required="true"/>
00063                <argument name="context" />
00064             </argument>
00065             <argument name="extension2" argsep="@" multiple="true">
00066                <para>Additional specifications of pickup targets.</para>
00067                <argument name="extension2" required="true"/>
00068                <argument name="context2"/>
00069             </argument>
00070          </parameter>
00071       </syntax>
00072       <description>
00073          <para>This application can pickup a specified ringing channel.  The channel
00074          to pickup can be specified in the following ways.</para>
00075          <para>1) If no <replaceable>extension</replaceable> targets are specified,
00076          the application will pickup a channel matching the pickup group of the
00077          requesting channel.</para>
00078          <para>2) If the <replaceable>extension</replaceable> is specified with a
00079          <replaceable>context</replaceable> of the special string
00080          <literal>PICKUPMARK</literal> (for example 10@PICKUPMARK), the application
00081          will pickup a channel which has defined the channel variable
00082          <variable>PICKUPMARK</variable> with the same value as
00083          <replaceable>extension</replaceable> (in this example,
00084          <literal>10</literal>).</para>
00085          <para>3) If the <replaceable>extension</replaceable> is specified
00086          with or without a <replaceable>context</replaceable>, the channel with a
00087          matching <replaceable>extension</replaceable> and <replaceable>context</replaceable>
00088          will be picked up.  If no <replaceable>context</replaceable> is specified,
00089          the current context will be used.</para>
00090          <note><para>The <replaceable>extension</replaceable> is typically set on
00091          matching channels by the dial application that created the channel.  The
00092          <replaceable>context</replaceable> is set on matching channels by the
00093          channel driver for the device.</para></note>
00094       </description>
00095    </application>
00096    <application name="PickupChan" language="en_US">
00097       <synopsis>
00098          Pickup a ringing channel.
00099       </synopsis>
00100       <syntax >
00101          <parameter name="Technology/Resource" argsep="&amp;" required="true">
00102             <argument name="Technology/Resource" required="true" />
00103             <argument name="Technology2/Resource2" required="false" multiple="true" />
00104          </parameter>
00105          <parameter name="options" required="false">
00106             <optionlist>
00107                <option name="p">
00108                   <para>Channel name specified partial name. Used when find channel by callid.</para>
00109                </option>
00110             </optionlist>
00111          </parameter>
00112       </syntax>
00113       <description>
00114          <para>This will pickup a specified <replaceable>channel</replaceable> if ringing.</para>
00115       </description>
00116    </application>
00117  ***/
00118 
00119 static const char app[] = "Pickup";
00120 static const char app2[] = "PickupChan";
00121 
00122 struct pickup_by_name_args {
00123    const char *name;
00124    size_t len;
00125 };
00126 
00127 static int pickup_by_name_cb(void *obj, void *arg, void *data, int flags)
00128 {
00129    struct ast_channel *target = obj;/*!< Potential pickup target */
00130    struct pickup_by_name_args *args = data;
00131 
00132    ast_channel_lock(target);
00133    if (!strncasecmp(ast_channel_name(target), args->name, args->len) && ast_can_pickup(target)) {
00134       /* Return with the channel still locked on purpose */
00135       return CMP_MATCH | CMP_STOP;
00136    }
00137    ast_channel_unlock(target);
00138 
00139    return 0;
00140 }
00141 
00142 /*! \brief Helper Function to walk through ALL channels checking NAME and STATE */
00143 static struct ast_channel *my_ast_get_channel_by_name_locked(const char *channame)
00144 {
00145    char *chkchan;
00146    struct pickup_by_name_args pickup_args;
00147 
00148    /* Check if channel name contains a '-'.
00149     * In this case the channel name will be interpreted as full channel name.
00150     */
00151    if (strchr(channame, '-')) {
00152       /* check full channel name */
00153       pickup_args.len = strlen(channame);
00154       pickup_args.name = channame;
00155    } else {
00156       /* need to append a '-' for the comparison so we check full channel name,
00157        * i.e SIP/hgc- , use a temporary variable so original stays the same for
00158        * debugging.
00159        */
00160       pickup_args.len = strlen(channame) + 1;
00161       chkchan = ast_alloca(pickup_args.len + 1);
00162       strcpy(chkchan, channame);
00163       strcat(chkchan, "-");
00164       pickup_args.name = chkchan;
00165    }
00166 
00167    return ast_channel_callback(pickup_by_name_cb, NULL, &pickup_args, 0);
00168 }
00169 
00170 /*! \brief Attempt to pick up named channel, does not use context */
00171 static int pickup_by_channel(struct ast_channel *chan, char *pickup)
00172 {
00173    int res = -1;
00174    struct ast_channel *target;/*!< Potential pickup target */
00175 
00176    target = my_ast_get_channel_by_name_locked(pickup);
00177    if (target) {
00178       /* Just check that we are not picking up the SAME as target. (i.e. ourself) */
00179       if (chan != target) {
00180          res = ast_do_pickup(chan, target);
00181       }
00182       ast_channel_unlock(target);
00183       target = ast_channel_unref(target);
00184    }
00185 
00186    return res;
00187 }
00188 
00189 /* Attempt to pick up specified extension with context */
00190 static int pickup_by_exten(struct ast_channel *chan, const char *exten, const char *context)
00191 {
00192    struct ast_channel *target = NULL;/*!< Potential pickup target */
00193    struct ast_channel_iterator *iter;
00194    int res = -1;
00195 
00196    if (!(iter = ast_channel_iterator_by_exten_new(exten, context))) {
00197       return -1;
00198    }
00199 
00200    while ((target = ast_channel_iterator_next(iter))) {
00201       ast_channel_lock(target);
00202       if ((chan != target) && ast_can_pickup(target)) {
00203          ast_log(LOG_NOTICE, "%s pickup by %s\n", ast_channel_name(target), ast_channel_name(chan));
00204          break;
00205       }
00206       ast_channel_unlock(target);
00207       target = ast_channel_unref(target);
00208    }
00209 
00210    ast_channel_iterator_destroy(iter);
00211 
00212    if (target) {
00213       res = ast_do_pickup(chan, target);
00214       ast_channel_unlock(target);
00215       target = ast_channel_unref(target);
00216    }
00217 
00218    return res;
00219 }
00220 
00221 static int find_by_mark(void *obj, void *arg, void *data, int flags)
00222 {
00223    struct ast_channel *target = obj;/*!< Potential pickup target */
00224    const char *mark = data;
00225    const char *tmp;
00226 
00227    ast_channel_lock(target);
00228    tmp = pbx_builtin_getvar_helper(target, PICKUPMARK);
00229    if (tmp && !strcasecmp(tmp, mark) && ast_can_pickup(target)) {
00230       /* Return with the channel still locked on purpose */
00231       return CMP_MATCH | CMP_STOP;
00232    }
00233    ast_channel_unlock(target);
00234 
00235    return 0;
00236 }
00237 
00238 /* Attempt to pick up specified mark */
00239 static int pickup_by_mark(struct ast_channel *chan, const char *mark)
00240 {
00241    struct ast_channel *target;/*!< Potential pickup target */
00242    int res = -1;
00243 
00244    /* The found channel is already locked. */
00245    target = ast_channel_callback(find_by_mark, NULL, (char *) mark, 0);
00246    if (target) {
00247       res = ast_do_pickup(chan, target);
00248       ast_channel_unlock(target);
00249       target = ast_channel_unref(target);
00250    }
00251 
00252    return res;
00253 }
00254 
00255 static int pickup_by_group(struct ast_channel *chan)
00256 {
00257    struct ast_channel *target;/*!< Potential pickup target */
00258    int res = -1;
00259 
00260    /* The found channel is already locked. */
00261    target = ast_pickup_find_by_group(chan);
00262    if (target) {
00263       ast_log(LOG_NOTICE, "pickup %s attempt by %s\n", ast_channel_name(target), ast_channel_name(chan));
00264       res = ast_do_pickup(chan, target);
00265       ast_channel_unlock(target);
00266       target = ast_channel_unref(target);
00267    }
00268 
00269    return res;
00270 }
00271 
00272 /* application entry point for Pickup() */
00273 static int pickup_exec(struct ast_channel *chan, const char *data)
00274 {
00275    char *tmp;
00276    char *exten;
00277    char *context;
00278 
00279    if (ast_strlen_zero(data)) {
00280       return pickup_by_group(chan) ? 0 : -1;
00281    }
00282 
00283    /* Parse extension (and context if there) */
00284    tmp = ast_strdupa(data);
00285    while (!ast_strlen_zero(tmp) && (exten = strsep(&tmp, "&"))) {
00286       if ((context = strchr(exten, '@')))
00287          *context++ = '\0';
00288       if (!ast_strlen_zero(context) && !strcasecmp(context, PICKUPMARK)) {
00289          if (!pickup_by_mark(chan, exten)) {
00290             /* Pickup successful.  Stop the dialplan this channel is a zombie. */
00291             return -1;
00292          }
00293       } else {
00294          if (ast_strlen_zero(context)) {
00295             context = (char *) ast_channel_context(chan);
00296          }
00297          if (!pickup_by_exten(chan, exten, context)) {
00298             /* Pickup successful.  Stop the dialplan this channel is a zombie. */
00299             return -1;
00300          }
00301       }
00302       ast_log(LOG_NOTICE, "No target channel found for %s@%s.\n", exten, context);
00303    }
00304 
00305    /* Pickup failed.  Keep going in the dialplan. */
00306    return 0;
00307 }
00308 
00309 /* Find channel for pick up specified by partial channel name */ 
00310 static int find_by_part(void *obj, void *arg, void *data, int flags)
00311 {
00312    struct ast_channel *target = obj;/*!< Potential pickup target */
00313    const char *part = data;
00314    int len = strlen(part);
00315 
00316    ast_channel_lock(target);
00317    if (len <= strlen(ast_channel_name(target)) && !strncmp(ast_channel_name(target), part, len)
00318       && ast_can_pickup(target)) {
00319       /* Return with the channel still locked on purpose */
00320       return CMP_MATCH | CMP_STOP;
00321    }
00322    ast_channel_unlock(target);
00323 
00324    return 0;
00325 }
00326 
00327 /* Attempt to pick up specified by partial channel name */ 
00328 static int pickup_by_part(struct ast_channel *chan, const char *part)
00329 {
00330    struct ast_channel *target;/*!< Potential pickup target */
00331    int res = -1;
00332 
00333    /* The found channel is already locked. */
00334    target = ast_channel_callback(find_by_part, NULL, (char *) part, 0);
00335    if (target) {
00336       res = ast_do_pickup(chan, target);
00337       ast_channel_unlock(target);
00338       target = ast_channel_unref(target);
00339    }
00340 
00341    return res;
00342 }
00343 
00344 /* application entry point for PickupChan() */
00345 static int pickupchan_exec(struct ast_channel *chan, const char *data)
00346 {
00347    int partial_pickup = 0;
00348    char *pickup = NULL;
00349    char *parse = ast_strdupa(data);
00350    AST_DECLARE_APP_ARGS(args,
00351       AST_APP_ARG(channel);
00352       AST_APP_ARG(options);
00353    );
00354    AST_STANDARD_APP_ARGS(args, parse);
00355 
00356    if (ast_strlen_zero(args.channel)) {
00357       ast_log(LOG_WARNING, "PickupChan requires an argument (channel)!\n");
00358       /* Pickup failed.  Keep going in the dialplan. */
00359       return 0;
00360    }
00361 
00362    if (!ast_strlen_zero(args.options) && strchr(args.options, 'p')) {
00363       partial_pickup = 1;
00364    }
00365 
00366    /* Parse channel */
00367    while (!ast_strlen_zero(args.channel) && (pickup = strsep(&args.channel, "&"))) {
00368       if (!strncasecmp(ast_channel_name(chan), pickup, strlen(pickup))) {
00369          ast_log(LOG_NOTICE, "Cannot pickup your own channel %s.\n", pickup);
00370       } else {
00371          if (partial_pickup) {
00372             if (!pickup_by_part(chan, pickup)) {
00373                /* Pickup successful.  Stop the dialplan this channel is a zombie. */
00374                return -1;
00375             }
00376          } else if (!pickup_by_channel(chan, pickup)) {
00377             /* Pickup successful.  Stop the dialplan this channel is a zombie. */
00378             return -1;
00379          }
00380          ast_log(LOG_NOTICE, "No target channel found for %s.\n", pickup);
00381       }
00382    }
00383 
00384    /* Pickup failed.  Keep going in the dialplan. */
00385    return 0;
00386 }
00387 
00388 static int unload_module(void)
00389 {
00390    int res;
00391 
00392    res = ast_unregister_application(app);
00393    res |= ast_unregister_application(app2);
00394 
00395    return res;
00396 }
00397 
00398 static int load_module(void)
00399 {
00400    int res;
00401 
00402    res = ast_register_application_xml(app, pickup_exec);
00403    res |= ast_register_application_xml(app2, pickupchan_exec);
00404 
00405    return res;
00406 }
00407 
00408 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Directed Call Pickup Application");