Thu Apr 28 2011 17:15:22

Asterisk developer's documentation


func_audiohookinherit.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 2008, Digium, Inc.
00005  *
00006  * Mark Michelson <mmichelson@digium.com>
00007  *
00008  * See http://www.asterisk.org for more information about
00009  * the Asterisk project. Please do not directly contact
00010  * any of the maintainers of this project for assistance;
00011  * the project provides a web site, mailing lists and IRC
00012  * channels for your use.
00013  *
00014  * This program is free software, distributed under the terms of
00015  * the GNU General Public License Version 2. See the LICENSE file
00016  * at the top of the source tree.
00017  *
00018  * Please follow coding guidelines
00019  * http://svn.digium.com/view/asterisk/trunk/doc/CODING-GUIDELINES
00020  */
00021 
00022 /*! \file
00023  *
00024  * \brief Audiohook inheritance function
00025  *
00026  * \author Mark Michelson <mmichelson@digium.com>
00027  *
00028  * \ingroup functions
00029  */
00030 
00031 #include "asterisk.h"
00032 #include "asterisk/datastore.h"
00033 #include "asterisk/channel.h"
00034 #include "asterisk/logger.h"
00035 #include "asterisk/audiohook.h"
00036 #include "asterisk/pbx.h"
00037 #include "asterisk/module.h"
00038 
00039 /*** DOCUMENTATION
00040    <function name = "AUDIOHOOK_INHERIT" language="en_US">
00041       <synopsis>
00042          Set whether an audiohook may be inherited to another channel
00043       </synopsis>
00044       <syntax>
00045          <parameter name="source" required="true">
00046             <para>The built-in sources in Asterisk are</para>
00047             <enumlist>
00048                <enum name="MixMonitor" />
00049                <enum name="Chanspy" />
00050                <enum name="Volume" />
00051                <enum name="Speex" />
00052                <enum name="JACK_HOOK" />
00053             </enumlist>
00054             <para>Note that the names are not case-sensitive</para>
00055          </parameter>
00056       </syntax>
00057       <description>
00058          <para>By enabling audiohook inheritance on the channel, you are giving
00059          permission for an audiohook to be inherited by a descendent channel.
00060          Inheritance may be be disabled at any point as well.</para>
00061 
00062          <para>Example scenario:</para>
00063          <para>exten => 2000,1,MixMonitor(blah.wav)</para>
00064          <para>exten => 2000,n,Set(AUDIOHOOK_INHERIT(MixMonitor)=yes)</para>
00065          <para>exten => 2000,n,Dial(SIP/2000)</para>
00066          <para>
00067          </para>
00068          <para>exten => 4000,1,Dial(SIP/4000)</para>
00069          <para>
00070          </para>
00071          <para>exten => 5000,1,MixMonitor(blah2.wav)</para>
00072          <para>exten => 5000,n,Dial(SIP/5000)</para>
00073          <para>
00074          </para>
00075          <para>In this basic dialplan scenario, let's consider the following sample calls</para>
00076          <para>Call 1: Caller dials 2000. The person who answers then executes an attended</para>
00077          <para>        transfer to 4000.</para>
00078          <para>Result: Since extension 2000 set MixMonitor to be inheritable, after the</para>
00079          <para>        transfer to 4000 has completed, the call will continue to be recorded
00080          to blah.wav</para>
00081          <para>
00082          </para>
00083          <para>Call 2: Caller dials 5000. The person who answers then executes an attended</para>
00084          <para>        transfer to 4000.</para>
00085          <para>Result: Since extension 5000 did not set MixMonitor to be inheritable, the</para>
00086          <para>        recording will stop once the call has been transferred to 4000.</para>
00087       </description>
00088    </function>
00089  ***/
00090 
00091 struct inheritable_audiohook {
00092    AST_LIST_ENTRY(inheritable_audiohook) list;
00093    char source[1];
00094 };
00095 
00096 struct audiohook_inheritance_datastore {
00097    AST_LIST_HEAD (, inheritable_audiohook) allowed_list;
00098 };
00099 
00100 static void audiohook_inheritance_fixup(void *data, struct ast_channel *old_chan, struct ast_channel *new_chan);
00101 static void audiohook_inheritance_destroy (void *data);
00102 static const struct ast_datastore_info audiohook_inheritance_info = {
00103    .type = "audiohook inheritance",
00104    .destroy = audiohook_inheritance_destroy,
00105    .chan_fixup = audiohook_inheritance_fixup,
00106 };
00107 
00108 /*! \brief Move audiohooks as defined by previous calls to the AUDIOHOOK_INHERIT function
00109  *
00110  * Move allowed audiohooks from the old channel to the new channel.
00111  *
00112  * \param data The ast_datastore containing audiohook inheritance information that will be moved
00113  * \param old_chan The "clone" channel from a masquerade. We are moving the audiohook in question off of this channel
00114  * \param new_chan The "original" channel from a masquerade. We are moving the audiohook in question to this channel
00115  * \return Void
00116  */
00117 static void audiohook_inheritance_fixup(void *data, struct ast_channel *old_chan, struct ast_channel *new_chan)
00118 {
00119    struct inheritable_audiohook *audiohook = NULL;
00120    struct audiohook_inheritance_datastore *datastore = data;
00121 
00122    ast_debug(2, "inheritance fixup occurring for channels %s(%p) and %s(%p)", old_chan->name, old_chan, new_chan->name, new_chan);
00123 
00124    AST_LIST_TRAVERSE(&datastore->allowed_list, audiohook, list) {
00125       ast_audiohook_move_by_source(old_chan, new_chan, audiohook->source);
00126       ast_debug(3, "Moved audiohook %s from %s(%p) to %s(%p)\n",
00127          audiohook->source, old_chan->name, old_chan, new_chan->name, new_chan);
00128    }
00129    return;
00130 }
00131 
00132 /*! \brief Destroy dynamically allocated data on an audiohook_inheritance_datastore
00133  *
00134  * \param data Pointer to the audiohook_inheritance_datastore in question.
00135  * \return Void
00136  */
00137 static void audiohook_inheritance_destroy(void *data)
00138 {
00139    struct audiohook_inheritance_datastore *audiohook_inheritance_datastore = data;
00140    struct inheritable_audiohook *inheritable_audiohook = NULL;
00141 
00142    while ((inheritable_audiohook = AST_LIST_REMOVE_HEAD(&audiohook_inheritance_datastore->allowed_list, list))) {
00143       ast_free(inheritable_audiohook);
00144    }
00145 
00146    ast_free(audiohook_inheritance_datastore);
00147 }
00148 
00149 /*! \brief create an audiohook_inheritance_datastore and attach it to a channel
00150  *
00151  * \param chan The channel to which we wish to attach the new datastore
00152  * \return Returns the newly created audiohook_inheritance_datastore or NULL on error
00153  */
00154 static struct audiohook_inheritance_datastore *setup_inheritance_datastore(struct ast_channel *chan)
00155 {
00156    struct ast_datastore *datastore = NULL;
00157    struct audiohook_inheritance_datastore *audiohook_inheritance_datastore = NULL;
00158 
00159    if (!(datastore = ast_datastore_alloc(&audiohook_inheritance_info, NULL))) {
00160       return NULL;
00161    }
00162 
00163    if (!(audiohook_inheritance_datastore = ast_calloc(1, sizeof(*audiohook_inheritance_datastore)))) {
00164       ast_datastore_free(datastore);
00165       return NULL;
00166    }
00167 
00168    datastore->data = audiohook_inheritance_datastore;
00169    ast_channel_lock(chan);
00170    ast_channel_datastore_add(chan, datastore);
00171    ast_channel_unlock(chan);
00172    return audiohook_inheritance_datastore;
00173 }
00174 
00175 /*! \brief Create a new inheritable_audiohook structure and add it to an audiohook_inheritance_datastore
00176  *
00177  * \param audiohook_inheritance_datastore The audiohook_inheritance_datastore we want to add the new inheritable_audiohook to
00178  * \param source The audiohook source for the newly created inheritable_audiohook
00179  * \retval 0 Success
00180  * \retval non-zero Failure
00181  */
00182 static int setup_inheritable_audiohook(struct audiohook_inheritance_datastore *audiohook_inheritance_datastore, const char *source)
00183 {
00184    struct inheritable_audiohook *inheritable_audiohook = NULL;
00185 
00186    inheritable_audiohook = ast_calloc(1, sizeof(*inheritable_audiohook) + strlen(source));
00187 
00188    if (!inheritable_audiohook) {
00189       return -1;
00190    }
00191 
00192    strcpy(inheritable_audiohook->source, source);
00193    AST_LIST_INSERT_TAIL(&audiohook_inheritance_datastore->allowed_list, inheritable_audiohook, list);
00194    ast_debug(3, "Set audiohook %s to be inheritable\n", source);
00195    return 0;
00196 }
00197 
00198 /*! \brief Set the permissibility of inheritance for a particular audiohook source on a channel
00199  *
00200  * For details regarding what happens in the function, see the inline comments
00201  *
00202  * \param chan The channel we are operating on
00203  * \param function The name of the dialplan function (AUDIOHOOK_INHERIT)
00204  * \param data The audiohook source for which we are setting inheritance permissions
00205  * \param value The value indicating the permission for audiohook inheritance
00206  */
00207 static int func_inheritance_write(struct ast_channel *chan, const char *function, char *data, const char *value)
00208 {
00209    int allow;
00210    struct ast_datastore *datastore = NULL;
00211    struct audiohook_inheritance_datastore *inheritance_datastore = NULL;
00212    struct inheritable_audiohook *inheritable_audiohook;
00213 
00214    /* Step 1: Get data from function call */
00215    if (ast_strlen_zero(data)) {
00216       ast_log(LOG_WARNING, "No argument provided to INHERITANCE function.\n");
00217       return -1;
00218    }
00219 
00220    if (ast_strlen_zero(value)) {
00221       ast_log(LOG_WARNING, "No value provided to INHERITANCE function.\n");
00222       return -1;
00223    }
00224 
00225    allow = ast_true(value);
00226 
00227    /* Step 2: retrieve or set up datastore */
00228    ast_channel_lock(chan);
00229    if (!(datastore = ast_channel_datastore_find(chan, &audiohook_inheritance_info, NULL))) {
00230       ast_channel_unlock(chan);
00231       /* In the case where we cannot find the datastore, we can take a few shortcuts */
00232       if (!allow) {
00233          ast_debug(1, "Audiohook %s is already set to not be inheritable on channel %s\n", data, chan->name);
00234          return 0;
00235       } else if (!(inheritance_datastore = setup_inheritance_datastore(chan))) {
00236          ast_log(LOG_WARNING, "Unable to set up audiohook inheritance datastore on channel %s\n", chan->name);
00237          return -1;
00238       } else {
00239          return setup_inheritable_audiohook(inheritance_datastore, data);
00240       }
00241    } else {
00242       inheritance_datastore = datastore->data;
00243    }
00244    ast_channel_unlock(chan);
00245 
00246    /* Step 3: Traverse the list to see if we're trying something redundant */
00247 
00248    AST_LIST_TRAVERSE_SAFE_BEGIN(&inheritance_datastore->allowed_list, inheritable_audiohook, list) {
00249       if (!strcasecmp(inheritable_audiohook->source, data)) {
00250          if (allow) {
00251             ast_debug(2, "Audiohook source %s is already set up to be inherited from channel %s\n", data, chan->name);
00252             return 0;
00253          } else {
00254             ast_debug(2, "Removing inheritability of audiohook %s from channel %s\n", data, chan->name);
00255             AST_LIST_REMOVE_CURRENT(list);
00256             ast_free(inheritable_audiohook);
00257             return 0;
00258          }
00259       }
00260    }
00261    AST_LIST_TRAVERSE_SAFE_END;
00262 
00263    /* Step 4: There is no step 4 */
00264 
00265    /* Step 5: This means we are addressing an audiohook source which we have not encountered yet for the channel. Create a new inheritable
00266     * audiohook structure if we're allowing inheritance, or just return if not
00267     */
00268 
00269    if (allow) {
00270       return setup_inheritable_audiohook(inheritance_datastore, data);
00271    } else {
00272       ast_debug(1, "Audiohook %s is already set to not be inheritable on channel %s\n", data, chan->name);
00273       return 0;
00274    }
00275 }
00276 
00277 static struct ast_custom_function inheritance_function = {
00278    .name = "AUDIOHOOK_INHERIT",
00279    .write = func_inheritance_write,
00280 };
00281 
00282 static int unload_module(void)
00283 {
00284    return ast_custom_function_unregister(&inheritance_function);
00285 }
00286 
00287 static int load_module(void)
00288 {
00289    if (ast_custom_function_register(&inheritance_function)) {
00290       return AST_MODULE_LOAD_DECLINE;
00291    } else {
00292       return AST_MODULE_LOAD_SUCCESS;
00293    }
00294 }
00295 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Audiohook inheritance function");