[asterisk-commits] mmichelson: branch mmichelson/issue13538 r161720 - /team/mmichelson/issue1353...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Mon Dec 8 11:18:38 CST 2008


Author: mmichelson
Date: Mon Dec  8 11:18:37 2008
New Revision: 161720

URL: http://svn.digium.com/view/asterisk?view=rev&rev=161720
Log:
Adding the func_audiohookinherit.c file


Added:
    team/mmichelson/issue13538/funcs/func_audiohookinherit.c   (with props)

Added: team/mmichelson/issue13538/funcs/func_audiohookinherit.c
URL: http://svn.digium.com/view/asterisk/team/mmichelson/issue13538/funcs/func_audiohookinherit.c?view=auto&rev=161720
==============================================================================
--- team/mmichelson/issue13538/funcs/func_audiohookinherit.c (added)
+++ team/mmichelson/issue13538/funcs/func_audiohookinherit.c Mon Dec  8 11:18:37 2008
@@ -1,0 +1,208 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) <Year>, <Your Name Here>
+ *
+ * Mark Michelson <mmichelson at digium.com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ *
+ * Please follow coding guidelines 
+ * http://svn.digium.com/view/asterisk/trunk/doc/CODING-GUIDELINES
+ */
+
+/*! \file
+ *
+ * \brief Audiohook inheritance function
+ *
+ * \author\verbatim Mark Michelson <mmichelson at digium.com> \endverbatim
+ * 
+ * \ingroup functions
+ */
+
+#include "asterisk.h"
+#include "asterisk/datastore.h"
+#include "asterisk/channel.h"
+#include "asterisk/logger.h"
+#include "asterisk/audiohook.h"
+#include "asterisk/pbx.h"
+#include "asterisk/module.h"
+
+struct inheritable_audiohook {
+	AST_LIST_ENTRY(inheritable_audiohook) list;
+	char source[1];
+};
+
+struct audiohook_inheritance_datastore {
+	AST_LIST_HEAD (inheritable_audiohook_list, inheritable_audiohook) allowed_list;
+	struct inheritable_audiohook_list unallowed_list;
+};
+
+static void audiohook_inheritance_fixup(void *data, struct ast_channel *old_chan, struct ast_channel *new_chan);
+static void audiohook_inheritance_destroy (void *data);
+static const struct ast_datastore_info audiohook_inheritance_info = {
+	.type = "audiohook inheritance",
+	.destroy = audiohook_inheritance_destroy,
+	.chan_fixup = audiohook_inheritance_fixup,
+};
+
+static void audiohook_inheritance_fixup(void *data, struct ast_channel *old_chan, struct ast_channel *new_chan)
+{
+	struct inheritable_audiohook *old_audiohook = NULL;
+	struct inheritable_audiohook *new_audiohook = NULL;
+	struct audiohook_inheritance_datastore *old_datastore = data;
+	struct ast_datastore *datastore =  ast_channel_datastore_find(new_chan, &audiohook_inheritance_info, NULL);
+	struct audiohook_inheritance_datastore *new_datastore = datastore ? datastore->data : NULL;
+
+	ast_debug(2, "inheritance fixup occurring for channels %s(%p) and %s(%p)", old_chan->name, old_chan, new_chan->name, new_chan);
+
+	if (!new_datastore) {
+		ast_log(LOG_NOTICE, "The new channel has no datastore\n");
+		AST_LIST_TRAVERSE(&old_datastore->allowed_list, old_audiohook, list) {
+			ast_log(LOG_NOTICE, "Going to move audiohook %s from %s to %s\n", old_audiohook->source, old_chan->name, new_chan->name);
+			ast_audiohook_move_by_source(old_chan, new_chan, old_audiohook->source);
+			ast_debug(3, "Moved audiohook %s from %s to %s\n", old_audiohook->source, old_chan->name, new_chan->name);
+		}
+	} else {
+		ast_log(LOG_NOTICE, "There is a datastore on the new channel...\n");
+	}
+
+	AST_LIST_TRAVERSE(&old_datastore->allowed_list, old_audiohook, list) {
+		AST_LIST_TRAVERSE(&new_datastore->unallowed_list, new_audiohook, list) {
+			if (!strcasecmp(old_audiohook->source, new_audiohook->source)) {
+				break;
+			}
+		}
+		if (!new_audiohook) {
+			ast_audiohook_move_by_source(old_chan, new_chan, old_audiohook->source);
+			ast_debug(3, "Moved audiohook %s from %s to %s\n", old_audiohook->source, old_chan->name, new_chan->name);
+		} else {
+			ast_debug(3, "Audiohook %s prevented from being moved from %s to %s\n", old_audiohook->source, old_chan->name, new_chan->name);
+		}
+	}
+}
+
+static void audiohook_inheritance_destroy (void *data)
+{
+	struct audiohook_inheritance_datastore *audiohook_inheritance_datastore = data;
+	struct inheritable_audiohook *inheritable_audiohook = NULL;
+
+	while ((inheritable_audiohook = AST_LIST_REMOVE_HEAD(&audiohook_inheritance_datastore->allowed_list, list))) {
+		ast_free(inheritable_audiohook);
+	}
+	
+	while ((inheritable_audiohook = AST_LIST_REMOVE_HEAD(&audiohook_inheritance_datastore->unallowed_list, list))) {
+		ast_free(inheritable_audiohook);
+	}
+}
+
+static struct audiohook_inheritance_datastore *setup_inheritance_datastore(struct ast_channel *chan)
+{
+	struct ast_datastore *datastore = NULL;
+	struct audiohook_inheritance_datastore *audiohook_inheritance_datastore = NULL;
+
+	if (!(datastore = ast_datastore_alloc(&audiohook_inheritance_info, NULL))) {
+		return NULL;
+	}
+
+	if (!(audiohook_inheritance_datastore = ast_calloc(1, sizeof(*audiohook_inheritance_datastore)))) {
+		ast_datastore_free(datastore);
+		return NULL;
+	}
+
+	datastore->data = audiohook_inheritance_datastore;
+	ast_channel_datastore_add(chan, datastore);
+	return audiohook_inheritance_datastore;
+}
+
+static int setup_inheritable_audiohook(struct audiohook_inheritance_datastore *audiohook_inheritance_datastore, const int allow, const char *source)
+{
+	struct inheritable_audiohook *inheritable_audiohook = ast_calloc(1, sizeof(*inheritable_audiohook) + strlen(source));
+	struct inheritable_audiohook_list *inheritable_audiohook_list = allow ? &audiohook_inheritance_datastore->allowed_list : &audiohook_inheritance_datastore->unallowed_list;
+
+	if (!inheritable_audiohook) {
+		return -1;
+	}
+
+	strcpy(inheritable_audiohook->source, source);
+	AST_LIST_INSERT_TAIL(inheritable_audiohook_list, inheritable_audiohook, list);
+	return 0;
+}
+
+static int func_inheritance_write(struct ast_channel *chan, const char *function, char *data, const char *value)
+{
+	int allow;
+	struct ast_datastore *datastore = NULL;
+	struct audiohook_inheritance_datastore *inheritance_datastore = NULL;
+	struct inheritable_audiohook *inheritable_audiohook;
+
+	/* Step 1: Get data from function call */
+	if (ast_strlen_zero(data)) {
+		ast_log(LOG_WARNING, "No argument provided to INHERITANCE function.\n");
+		return -1;
+	}
+
+	if (ast_strlen_zero(value)) {
+		ast_log(LOG_WARNING, "No value provided to INHERITANCE function.\n");
+		return -1;
+	}
+
+	allow = !strcasecmp(data, "allow");
+
+	/* Step 2: retrieve or set up datastore */
+	if (!(datastore = ast_channel_datastore_find(chan, &audiohook_inheritance_info, NULL))) {
+		if (!(inheritance_datastore = setup_inheritance_datastore(chan))) {
+			ast_log(LOG_NOTICE, "Unable to set up audiohook inheritance datastore on channel %s\n", chan->name);
+			return -1;
+		}
+		return setup_inheritable_audiohook(inheritance_datastore, allow, value);
+	} else {
+		inheritance_datastore = datastore->data;
+	}
+
+	/* Step 3: Traverse the lists to see if we're trying something redundant */
+
+	if (allow) {
+		AST_LIST_TRAVERSE(&inheritance_datastore->allowed_list, inheritable_audiohook, list) {
+			if (!strcasecmp(inheritable_audiohook->source, value)) {
+				return 0;
+			}
+		}
+	} else {
+		AST_LIST_TRAVERSE(&inheritance_datastore->unallowed_list, inheritable_audiohook, list) {
+			if (!strcasecmp(inheritable_audiohook->source, value)) {
+				return 0;
+			}
+		}
+	}
+
+	/* Step 4: There is no step 4 */
+
+	/* Step 5: This means we need to create a new inheritable_audiohook and add it to the correct list */
+
+	return setup_inheritable_audiohook(inheritance_datastore, allow, value);
+}
+
+static struct ast_custom_function inheritance_function = {
+	.name = "INHERIT",
+	.write = func_inheritance_write,
+};
+
+static int unload_module(void)
+{
+	return ast_custom_function_unregister(&inheritance_function);
+}
+
+static int load_module(void) 
+{
+	return ast_custom_function_register(&inheritance_function);
+}
+AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Audiohook inheritance function");

Propchange: team/mmichelson/issue13538/funcs/func_audiohookinherit.c
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: team/mmichelson/issue13538/funcs/func_audiohookinherit.c
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Propchange: team/mmichelson/issue13538/funcs/func_audiohookinherit.c
------------------------------------------------------------------------------
    svn:mime-type = text/plain




More information about the asterisk-commits mailing list