2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2008, Digium, Inc.
6 * Mark Michelson <mmichelson@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
18 * Please follow coding guidelines
19 * http://svn.digium.com/view/asterisk/trunk/doc/CODING-GUIDELINES
24 * \brief Audiohook inheritance function
26 * \author Mark Michelson <mmichelson@digium.com>
32 <support_level>core</support_level>
36 #include "asterisk/datastore.h"
37 #include "asterisk/channel.h"
38 #include "asterisk/logger.h"
39 #include "asterisk/audiohook.h"
40 #include "asterisk/pbx.h"
41 #include "asterisk/module.h"
44 <function name = "AUDIOHOOK_INHERIT" language="en_US">
46 Set whether an audiohook may be inherited to another channel
49 <parameter name="source" required="true">
50 <para>The built-in sources in Asterisk are</para>
52 <enum name="MixMonitor" />
53 <enum name="Chanspy" />
54 <enum name="Volume" />
56 <enum name="pitch_shift" />
57 <enum name="JACK_HOOK" />
60 <para>Note that the names are not case-sensitive</para>
64 <para>By enabling audiohook inheritance on the channel, you are giving
65 permission for an audiohook to be inherited by a descendent channel.
66 Inheritance may be be disabled at any point as well.</para>
68 <para>Example scenario:</para>
69 <para>exten => 2000,1,MixMonitor(blah.wav)</para>
70 <para>exten => 2000,n,Set(AUDIOHOOK_INHERIT(MixMonitor)=yes)</para>
71 <para>exten => 2000,n,Dial(SIP/2000)</para>
74 <para>exten => 4000,1,Dial(SIP/4000)</para>
77 <para>exten => 5000,1,MixMonitor(blah2.wav)</para>
78 <para>exten => 5000,n,Dial(SIP/5000)</para>
81 <para>In this basic dialplan scenario, let's consider the following sample calls</para>
82 <para>Call 1: Caller dials 2000. The person who answers then executes an attended</para>
83 <para> transfer to 4000.</para>
84 <para>Result: Since extension 2000 set MixMonitor to be inheritable, after the</para>
85 <para> transfer to 4000 has completed, the call will continue to be recorded
89 <para>Call 2: Caller dials 5000. The person who answers then executes an attended</para>
90 <para> transfer to 4000.</para>
91 <para>Result: Since extension 5000 did not set MixMonitor to be inheritable, the</para>
92 <para> recording will stop once the call has been transferred to 4000.</para>
97 struct inheritable_audiohook {
98 AST_LIST_ENTRY(inheritable_audiohook) list;
102 struct audiohook_inheritance_datastore {
103 AST_LIST_HEAD (, inheritable_audiohook) allowed_list;
106 static void audiohook_inheritance_fixup(void *data, struct ast_channel *old_chan, struct ast_channel *new_chan);
107 static void audiohook_inheritance_destroy (void *data);
108 static const struct ast_datastore_info audiohook_inheritance_info = {
109 .type = "audiohook inheritance",
110 .destroy = audiohook_inheritance_destroy,
111 .chan_fixup = audiohook_inheritance_fixup,
114 /*! \brief Move audiohooks as defined by previous calls to the AUDIOHOOK_INHERIT function
116 * Move allowed audiohooks from the old channel to the new channel.
118 * \param data The ast_datastore containing audiohook inheritance information that will be moved
119 * \param old_chan The "clone" channel from a masquerade. We are moving the audiohook in question off of this channel
120 * \param new_chan The "original" channel from a masquerade. We are moving the audiohook in question to this channel
123 static void audiohook_inheritance_fixup(void *data, struct ast_channel *old_chan, struct ast_channel *new_chan)
125 struct inheritable_audiohook *audiohook = NULL;
126 struct audiohook_inheritance_datastore *datastore = data;
128 ast_debug(2, "inheritance fixup occurring for channels %s(%p) and %s(%p)", ast_channel_name(old_chan), old_chan, ast_channel_name(new_chan), new_chan);
130 AST_LIST_TRAVERSE(&datastore->allowed_list, audiohook, list) {
131 ast_audiohook_move_by_source(old_chan, new_chan, audiohook->source);
132 ast_debug(3, "Moved audiohook %s from %s(%p) to %s(%p)\n",
133 audiohook->source, ast_channel_name(old_chan), old_chan, ast_channel_name(new_chan), new_chan);
138 /*! \brief Destroy dynamically allocated data on an audiohook_inheritance_datastore
140 * \param data Pointer to the audiohook_inheritance_datastore in question.
143 static void audiohook_inheritance_destroy(void *data)
145 struct audiohook_inheritance_datastore *audiohook_inheritance_datastore = data;
146 struct inheritable_audiohook *inheritable_audiohook = NULL;
148 while ((inheritable_audiohook = AST_LIST_REMOVE_HEAD(&audiohook_inheritance_datastore->allowed_list, list))) {
149 ast_free(inheritable_audiohook);
152 ast_free(audiohook_inheritance_datastore);
155 /*! \brief create an audiohook_inheritance_datastore and attach it to a channel
157 * \param chan The channel to which we wish to attach the new datastore
158 * \return Returns the newly created audiohook_inheritance_datastore or NULL on error
160 static struct audiohook_inheritance_datastore *setup_inheritance_datastore(struct ast_channel *chan)
162 struct ast_datastore *datastore = NULL;
163 struct audiohook_inheritance_datastore *audiohook_inheritance_datastore = NULL;
165 if (!(datastore = ast_datastore_alloc(&audiohook_inheritance_info, NULL))) {
169 if (!(audiohook_inheritance_datastore = ast_calloc(1, sizeof(*audiohook_inheritance_datastore)))) {
170 ast_datastore_free(datastore);
174 datastore->data = audiohook_inheritance_datastore;
175 ast_channel_lock(chan);
176 ast_channel_datastore_add(chan, datastore);
177 ast_channel_unlock(chan);
178 return audiohook_inheritance_datastore;
181 /*! \brief Create a new inheritable_audiohook structure and add it to an audiohook_inheritance_datastore
183 * \param audiohook_inheritance_datastore The audiohook_inheritance_datastore we want to add the new inheritable_audiohook to
184 * \param source The audiohook source for the newly created inheritable_audiohook
186 * \retval non-zero Failure
188 static int setup_inheritable_audiohook(struct audiohook_inheritance_datastore *audiohook_inheritance_datastore, const char *source)
190 struct inheritable_audiohook *inheritable_audiohook = NULL;
192 inheritable_audiohook = ast_calloc(1, sizeof(*inheritable_audiohook) + strlen(source));
194 if (!inheritable_audiohook) {
198 strcpy(inheritable_audiohook->source, source);
199 AST_LIST_INSERT_TAIL(&audiohook_inheritance_datastore->allowed_list, inheritable_audiohook, list);
200 ast_debug(3, "Set audiohook %s to be inheritable\n", source);
204 /*! \brief Set the permissibility of inheritance for a particular audiohook source on a channel
206 * For details regarding what happens in the function, see the inline comments
208 * \param chan The channel we are operating on
209 * \param function The name of the dialplan function (AUDIOHOOK_INHERIT)
210 * \param data The audiohook source for which we are setting inheritance permissions
211 * \param value The value indicating the permission for audiohook inheritance
213 static int func_inheritance_write(struct ast_channel *chan, const char *function, char *data, const char *value)
216 struct ast_datastore *datastore = NULL;
217 struct audiohook_inheritance_datastore *inheritance_datastore = NULL;
218 struct inheritable_audiohook *inheritable_audiohook;
220 /* Step 1: Get data from function call */
221 if (ast_strlen_zero(data)) {
222 ast_log(LOG_WARNING, "No argument provided to INHERITANCE function.\n");
226 if (ast_strlen_zero(value)) {
227 ast_log(LOG_WARNING, "No value provided to INHERITANCE function.\n");
231 allow = ast_true(value);
233 /* Step 2: retrieve or set up datastore */
234 ast_channel_lock(chan);
235 if (!(datastore = ast_channel_datastore_find(chan, &audiohook_inheritance_info, NULL))) {
236 ast_channel_unlock(chan);
237 /* In the case where we cannot find the datastore, we can take a few shortcuts */
239 ast_debug(1, "Audiohook %s is already set to not be inheritable on channel %s\n", data, ast_channel_name(chan));
241 } else if (!(inheritance_datastore = setup_inheritance_datastore(chan))) {
242 ast_log(LOG_WARNING, "Unable to set up audiohook inheritance datastore on channel %s\n", ast_channel_name(chan));
245 return setup_inheritable_audiohook(inheritance_datastore, data);
248 inheritance_datastore = datastore->data;
250 ast_channel_unlock(chan);
252 /* Step 3: Traverse the list to see if we're trying something redundant */
254 AST_LIST_TRAVERSE_SAFE_BEGIN(&inheritance_datastore->allowed_list, inheritable_audiohook, list) {
255 if (!strcasecmp(inheritable_audiohook->source, data)) {
257 ast_debug(2, "Audiohook source %s is already set up to be inherited from channel %s\n", data, ast_channel_name(chan));
260 ast_debug(2, "Removing inheritability of audiohook %s from channel %s\n", data, ast_channel_name(chan));
261 AST_LIST_REMOVE_CURRENT(list);
262 ast_free(inheritable_audiohook);
267 AST_LIST_TRAVERSE_SAFE_END;
269 /* Step 4: There is no step 4 */
271 /* Step 5: This means we are addressing an audiohook source which we have not encountered yet for the channel. Create a new inheritable
272 * audiohook structure if we're allowing inheritance, or just return if not
276 return setup_inheritable_audiohook(inheritance_datastore, data);
278 ast_debug(1, "Audiohook %s is already set to not be inheritable on channel %s\n", data, ast_channel_name(chan));
283 static struct ast_custom_function inheritance_function = {
284 .name = "AUDIOHOOK_INHERIT",
285 .write = func_inheritance_write,
288 static int unload_module(void)
290 return ast_custom_function_unregister(&inheritance_function);
293 static int load_module(void)
295 if (ast_custom_function_register(&inheritance_function)) {
296 return AST_MODULE_LOAD_DECLINE;
298 return AST_MODULE_LOAD_SUCCESS;
301 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Audiohook inheritance function");