Wed Aug 7 17:15:33 2019

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: 370642 $")
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(target->name, 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", target->name, chan->name);
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 find_channel_by_group(void *obj, void *arg, void *data, int flags)
00256 {
00257    struct ast_channel *target = obj;/*!< Potential pickup target */
00258    struct ast_channel *chan = data;/*!< Channel wanting to pickup call */
00259 
00260    ast_channel_lock(target);
00261    if (chan != target && (chan->pickupgroup & target->callgroup)
00262       && ast_can_pickup(target)) {
00263       /* Return with the channel still locked on purpose */
00264       return CMP_MATCH | CMP_STOP;
00265    }
00266    ast_channel_unlock(target);
00267 
00268    return 0;
00269 }
00270 
00271 static int pickup_by_group(struct ast_channel *chan)
00272 {
00273    struct ast_channel *target;/*!< Potential pickup target */
00274    int res = -1;
00275 
00276    /* The found channel is already locked. */
00277    target = ast_channel_callback(find_channel_by_group, NULL, chan, 0);
00278    if (target) {
00279       ast_log(LOG_NOTICE, "pickup %s attempt by %s\n", target->name, chan->name);
00280       res = ast_do_pickup(chan, target);
00281       ast_channel_unlock(target);
00282       target = ast_channel_unref(target);
00283    }
00284 
00285    return res;
00286 }
00287 
00288 /* application entry point for Pickup() */
00289 static int pickup_exec(struct ast_channel *chan, const char *data)
00290 {
00291    char *tmp = ast_strdupa(data);
00292    char *exten = NULL, *context = NULL;
00293 
00294    if (ast_strlen_zero(data)) {
00295       return pickup_by_group(chan) ? 0 : -1;
00296    }
00297 
00298    /* Parse extension (and context if there) */
00299    while (!ast_strlen_zero(tmp) && (exten = strsep(&tmp, "&"))) {
00300       if ((context = strchr(exten, '@')))
00301          *context++ = '\0';
00302       if (!ast_strlen_zero(context) && !strcasecmp(context, PICKUPMARK)) {
00303          if (!pickup_by_mark(chan, exten)) {
00304             /* Pickup successful.  Stop the dialplan this channel is a zombie. */
00305             return -1;
00306          }
00307       } else {
00308          if (!pickup_by_exten(chan, exten, !ast_strlen_zero(context) ? context : chan->context)) {
00309             /* Pickup successful.  Stop the dialplan this channel is a zombie. */
00310             return -1;
00311          }
00312       }
00313       ast_log(LOG_NOTICE, "No target channel found for %s.\n", exten);
00314    }
00315 
00316    /* Pickup failed.  Keep going in the dialplan. */
00317    return 0;
00318 }
00319 
00320 /* Find channel for pick up specified by partial channel name */ 
00321 static int find_by_part(void *obj, void *arg, void *data, int flags)
00322 {
00323    struct ast_channel *target = obj;/*!< Potential pickup target */
00324    const char *part = data;
00325    int len = strlen(part);
00326 
00327    ast_channel_lock(target);
00328    if (len <= strlen(target->name) && !strncmp(target->name, part, len)
00329       && ast_can_pickup(target)) {
00330       /* Return with the channel still locked on purpose */
00331       return CMP_MATCH | CMP_STOP;
00332    }
00333    ast_channel_unlock(target);
00334 
00335    return 0;
00336 }
00337 
00338 /* Attempt to pick up specified by partial channel name */ 
00339 static int pickup_by_part(struct ast_channel *chan, const char *part)
00340 {
00341    struct ast_channel *target;/*!< Potential pickup target */
00342    int res = -1;
00343 
00344    /* The found channel is already locked. */
00345    target = ast_channel_callback(find_by_part, NULL, (char *) part, 0);
00346    if (target) {
00347       res = ast_do_pickup(chan, target);
00348       ast_channel_unlock(target);
00349       target = ast_channel_unref(target);
00350    }
00351 
00352    return res;
00353 }
00354 
00355 /* application entry point for PickupChan() */
00356 static int pickupchan_exec(struct ast_channel *chan, const char *data)
00357 {
00358    int partial_pickup = 0;
00359    char *pickup = NULL;
00360    char *parse = ast_strdupa(data);
00361    AST_DECLARE_APP_ARGS(args,
00362       AST_APP_ARG(channel);
00363       AST_APP_ARG(options);
00364    );
00365    AST_STANDARD_APP_ARGS(args, parse);
00366 
00367    if (ast_strlen_zero(args.channel)) {
00368       ast_log(LOG_WARNING, "PickupChan requires an argument (channel)!\n");
00369       /* Pickup failed.  Keep going in the dialplan. */
00370       return 0;
00371    }
00372 
00373    if (!ast_strlen_zero(args.options) && strchr(args.options, 'p')) {
00374       partial_pickup = 1;
00375    }
00376 
00377    /* Parse channel */
00378    while (!ast_strlen_zero(args.channel) && (pickup = strsep(&args.channel, "&"))) {
00379       if (!strncasecmp(chan->name, pickup, strlen(pickup))) {
00380          ast_log(LOG_NOTICE, "Cannot pickup your own channel %s.\n", pickup);
00381       } else {
00382          if (partial_pickup) {
00383             if (!pickup_by_part(chan, pickup)) {
00384                /* Pickup successful.  Stop the dialplan this channel is a zombie. */
00385                return -1;
00386             }
00387          } else if (!pickup_by_channel(chan, pickup)) {
00388             /* Pickup successful.  Stop the dialplan this channel is a zombie. */
00389             return -1;
00390          }
00391          ast_log(LOG_NOTICE, "No target channel found for %s.\n", pickup);
00392       }
00393    }
00394 
00395    /* Pickup failed.  Keep going in the dialplan. */
00396    return 0;
00397 }
00398 
00399 static int unload_module(void)
00400 {
00401    int res;
00402 
00403    res = ast_unregister_application(app);
00404    res |= ast_unregister_application(app2);
00405 
00406    return res;
00407 }
00408 
00409 static int load_module(void)
00410 {
00411    int res;
00412 
00413    res = ast_register_application_xml(app, pickup_exec);
00414    res |= ast_register_application_xml(app2, pickupchan_exec);
00415 
00416    return res;
00417 }
00418 
00419 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Directed Call Pickup Application");

Generated on 7 Aug 2019 for Asterisk - The Open Source Telephony Project by  doxygen 1.6.1