Fri Jun 19 12:09:44 2009

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 struct inheritable_audiohook {
00040    AST_LIST_ENTRY(inheritable_audiohook) list;
00041    char source[1];
00042 };
00043 
00044 struct audiohook_inheritance_datastore {
00045    AST_LIST_HEAD (, inheritable_audiohook) allowed_list;
00046 };
00047 
00048 static void audiohook_inheritance_fixup(void *data, struct ast_channel *old_chan, struct ast_channel *new_chan);
00049 static void audiohook_inheritance_destroy (void *data);
00050 static const struct ast_datastore_info audiohook_inheritance_info = {
00051    .type = "audiohook inheritance",
00052    .destroy = audiohook_inheritance_destroy,
00053    .chan_fixup = audiohook_inheritance_fixup,
00054 };
00055 
00056 /*! \brief Move audiohooks as defined by previous calls to the AUDIOHOOK_INHERIT function
00057  *
00058  * Move allowed audiohooks from the old channel to the new channel.
00059  *
00060  * \param data The ast_datastore containing audiohook inheritance information that will be moved
00061  * \param old_chan The "clone" channel from a masquerade. We are moving the audiohook in question off of this channel
00062  * \param new_chan The "original" channel from a masquerade. We are moving the audiohook in question to this channel
00063  * \return Void
00064  */
00065 static void audiohook_inheritance_fixup(void *data, struct ast_channel *old_chan, struct ast_channel *new_chan)
00066 {
00067    struct inheritable_audiohook *audiohook = NULL;
00068    struct audiohook_inheritance_datastore *datastore = data;
00069 
00070    ast_debug(2, "inheritance fixup occurring for channels %s(%p) and %s(%p)", old_chan->name, old_chan, new_chan->name, new_chan);
00071 
00072    AST_LIST_TRAVERSE(&datastore->allowed_list, audiohook, list) {
00073       ast_audiohook_move_by_source(old_chan, new_chan, audiohook->source);
00074       ast_debug(3, "Moved audiohook %s from %s(%p) to %s(%p)\n",
00075          audiohook->source, old_chan->name, old_chan, new_chan->name, new_chan);
00076    }
00077    return;
00078 }
00079 
00080 /*! \brief Destroy dynamically allocated data on an audiohook_inheritance_datastore
00081  *
00082  * \param data Pointer to the audiohook_inheritance_datastore in question.
00083  * \return Void
00084  */
00085 static void audiohook_inheritance_destroy(void *data)
00086 {
00087    struct audiohook_inheritance_datastore *audiohook_inheritance_datastore = data;
00088    struct inheritable_audiohook *inheritable_audiohook = NULL;
00089 
00090    while ((inheritable_audiohook = AST_LIST_REMOVE_HEAD(&audiohook_inheritance_datastore->allowed_list, list))) {
00091       ast_free(inheritable_audiohook);
00092    }
00093 }
00094 
00095 /*! \brief create an audiohook_inheritance_datastore and attach it to a channel
00096  *
00097  * \param chan The channel to which we wish to attach the new datastore
00098  * \return Returns the newly created audiohook_inheritance_datastore or NULL on error
00099  */
00100 static struct audiohook_inheritance_datastore *setup_inheritance_datastore(struct ast_channel *chan)
00101 {
00102    struct ast_datastore *datastore = NULL;
00103    struct audiohook_inheritance_datastore *audiohook_inheritance_datastore = NULL;
00104 
00105    if (!(datastore = ast_datastore_alloc(&audiohook_inheritance_info, NULL))) {
00106       return NULL;
00107    }
00108 
00109    if (!(audiohook_inheritance_datastore = ast_calloc(1, sizeof(*audiohook_inheritance_datastore)))) {
00110       ast_datastore_free(datastore);
00111       return NULL;
00112    }
00113 
00114    datastore->data = audiohook_inheritance_datastore;
00115    ast_channel_lock(chan);
00116    ast_channel_datastore_add(chan, datastore);
00117    ast_channel_unlock(chan);
00118    return audiohook_inheritance_datastore;
00119 }
00120 
00121 /*! \brief Create a new inheritable_audiohook structure and add it to an audiohook_inheritance_datastore
00122  *
00123  * \param audiohook_inheritance_datastore The audiohook_inheritance_datastore we want to add the new inheritable_audiohook to
00124  * \param source The audiohook source for the newly created inheritable_audiohook
00125  * \retval 0 Success
00126  * \retval non-zero Failure
00127  */
00128 static int setup_inheritable_audiohook(struct audiohook_inheritance_datastore *audiohook_inheritance_datastore, const char *source)
00129 {
00130    struct inheritable_audiohook *inheritable_audiohook = NULL;
00131 
00132    inheritable_audiohook = ast_calloc(1, sizeof(*inheritable_audiohook) + strlen(source));
00133 
00134    if (!inheritable_audiohook) {
00135       return -1;
00136    }
00137 
00138    strcpy(inheritable_audiohook->source, source);
00139    AST_LIST_INSERT_TAIL(&audiohook_inheritance_datastore->allowed_list, inheritable_audiohook, list);
00140    ast_debug(3, "Set audiohook %s to be inheritable\n", source);
00141    return 0;
00142 }
00143 
00144 /*! \brief Set the permissibility of inheritance for a particular audiohook source on a channel
00145  *
00146  * For details regarding what happens in the function, see the inline comments
00147  *
00148  * \param chan The channel we are operating on
00149  * \param function The name of the dialplan function (AUDIOHOOK_INHERIT)
00150  * \param data The audiohook source for which we are setting inheritance permissions
00151  * \param value The value indicating the permission for audiohook inheritance
00152  */
00153 static int func_inheritance_write(struct ast_channel *chan, const char *function, char *data, const char *value)
00154 {
00155    int allow;
00156    struct ast_datastore *datastore = NULL;
00157    struct audiohook_inheritance_datastore *inheritance_datastore = NULL;
00158    struct inheritable_audiohook *inheritable_audiohook;
00159 
00160    /* Step 1: Get data from function call */
00161    if (ast_strlen_zero(data)) {
00162       ast_log(LOG_WARNING, "No argument provided to INHERITANCE function.\n");
00163       return -1;
00164    }
00165 
00166    if (ast_strlen_zero(value)) {
00167       ast_log(LOG_WARNING, "No value provided to INHERITANCE function.\n");
00168       return -1;
00169    }
00170 
00171    allow = ast_true(value);
00172 
00173    /* Step 2: retrieve or set up datastore */
00174    ast_channel_lock(chan);
00175    if (!(datastore = ast_channel_datastore_find(chan, &audiohook_inheritance_info, NULL))) {
00176       ast_channel_unlock(chan);
00177       /* In the case where we cannot find the datastore, we can take a few shortcuts */
00178       if (!allow) {
00179          ast_debug(1, "Audiohook %s is already set to not be inheritable on channel %s\n", data, chan->name);
00180          return 0;
00181       } else if (!(inheritance_datastore = setup_inheritance_datastore(chan))) {
00182          ast_log(LOG_WARNING, "Unable to set up audiohook inheritance datastore on channel %s\n", chan->name);
00183          return -1;
00184       } else {
00185          return setup_inheritable_audiohook(inheritance_datastore, data);
00186       }
00187    } else {
00188       inheritance_datastore = datastore->data;
00189    }
00190    ast_channel_unlock(chan);
00191 
00192    /* Step 3: Traverse the list to see if we're trying something redundant */
00193 
00194    AST_LIST_TRAVERSE_SAFE_BEGIN(&inheritance_datastore->allowed_list, inheritable_audiohook, list) {
00195       if (!strcasecmp(inheritable_audiohook->source, data)) {
00196          if (allow) {
00197             ast_debug(2, "Audiohook source %s is already set up to be inherited from channel %s\n", data, chan->name);
00198             return 0;
00199          } else {
00200             ast_debug(2, "Removing inheritability of audiohook %s from channel %s\n", data, chan->name);
00201             AST_LIST_REMOVE_CURRENT(list);
00202             ast_free(inheritable_audiohook);
00203             return 0;
00204          }
00205       }
00206    }
00207    AST_LIST_TRAVERSE_SAFE_END;
00208 
00209    /* Step 4: There is no step 4 */
00210 
00211    /* Step 5: This means we are addressing an audiohook source which we have not encountered yet for the channel. Create a new inheritable
00212     * audiohook structure if we're allowing inheritance, or just return if not
00213     */
00214 
00215    if (allow) {
00216       return setup_inheritable_audiohook(inheritance_datastore, data);
00217    } else {
00218       ast_debug(1, "Audiohook %s is already set to not be inheritable on channel %s\n", data, chan->name);
00219       return 0;
00220    }
00221 }
00222 
00223 static struct ast_custom_function inheritance_function = {
00224    .name = "AUDIOHOOK_INHERIT",
00225    .synopsis = "Set whether an audiohook may be inherited to another channel",
00226    .syntax = "AUDIOHOOK_INHERIT(source)",
00227    .desc =
00228       "By enabling audiohook inheritance on the channel, you are giving\n"
00229       "permission for an audiohook to be inherited by a descendent channel.\n"
00230       "Inheritance may be be disabled at any point as well.\n"
00231       "\n"
00232       "  Example scenario:\n"
00233       "  exten => 2000,1,MixMonitor(blah.wav)\n"
00234       "  exten => 2000,n,Set(AUDIOHOOK_INHERIT(MixMonitor)=yes)\n"
00235       "  exten => 2000,n,Dial(SIP/2000)\n"
00236       "\n"
00237       "  exten => 4000,1,Dial(SIP/4000)\n"
00238       "\n"
00239       "  exten => 5000,1,MixMonitor(blah2.wav)\n"
00240       "  exten => 5000,n,Dial(SIP/5000)\n"
00241       "\n"
00242       "  In this basic dialplan scenario, let's consider the following sample calls\n"
00243       "  Call 1: Caller dials 2000. The person who answers then executes an attended\n"
00244       "          transfer to 4000.\n"
00245       "  Result: Since extension 2000 set MixMonitor to be inheritable, after the\n"
00246       "          transfer to 400 has completed, the call will continue to be recorded\n"
00247       "           to blah.wav\n"
00248       "\n"
00249       "  Call 2: Caller dials 5000. The person who answers then executes an attended\n"
00250       "          transfer to 4000.\n"
00251       "  Result: Since extension 5000 did not set MixMonitor to be inheritable, the\n"
00252       "          recording will stop once the call has been transferred to 4000.\n",
00253    .write = func_inheritance_write,
00254 };
00255 
00256 static int unload_module(void)
00257 {
00258    return ast_custom_function_unregister(&inheritance_function);
00259 }
00260 
00261 static int load_module(void)
00262 {
00263    if (ast_custom_function_register(&inheritance_function)) {
00264       return AST_MODULE_LOAD_DECLINE;
00265    } else {
00266       return AST_MODULE_LOAD_SUCCESS;
00267    }
00268 }
00269 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Audiohook inheritance function");

Generated on Fri Jun 19 12:09:44 2009 for Asterisk - the Open Source PBX by  doxygen 1.4.7