Sat Mar 10 01:53:56 2012

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

Generated on Sat Mar 10 01:53:56 2012 for Asterisk - The Open Source Telephony Project by  doxygen 1.4.7