Replace direct access to channel name with accessor functions
[asterisk/asterisk.git] / funcs / func_audiohookinherit.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2008, Digium, Inc.
5  *
6  * Mark Michelson <mmichelson@digium.com>
7  *
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.
13  *
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.
17  *
18  * Please follow coding guidelines
19  * http://svn.digium.com/view/asterisk/trunk/doc/CODING-GUIDELINES
20  */
21
22 /*! \file
23  *
24  * \brief Audiohook inheritance function
25  *
26  * \author Mark Michelson <mmichelson@digium.com>
27  *
28  * \ingroup functions
29  */
30
31 /*** MODULEINFO
32         <support_level>core</support_level>
33  ***/
34
35 #include "asterisk.h"
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"
42
43 /*** DOCUMENTATION
44         <function name = "AUDIOHOOK_INHERIT" language="en_US">
45                 <synopsis>
46                         Set whether an audiohook may be inherited to another channel
47                 </synopsis>
48                 <syntax>
49                         <parameter name="source" required="true">
50                                 <para>The built-in sources in Asterisk are</para>
51                                 <enumlist>
52                                         <enum name="MixMonitor" />
53                                         <enum name="Chanspy" />
54                                         <enum name="Volume" />
55                                         <enum name="Speex" />
56                                         <enum name="JACK_HOOK" />
57                                 </enumlist>
58                                 <para>Note that the names are not case-sensitive</para>
59                         </parameter>
60                 </syntax>
61                 <description>
62                         <para>By enabling audiohook inheritance on the channel, you are giving
63                         permission for an audiohook to be inherited by a descendent channel.
64                         Inheritance may be be disabled at any point as well.</para>
65
66                         <para>Example scenario:</para>
67                         <para>exten => 2000,1,MixMonitor(blah.wav)</para>
68                         <para>exten => 2000,n,Set(AUDIOHOOK_INHERIT(MixMonitor)=yes)</para>
69                         <para>exten => 2000,n,Dial(SIP/2000)</para>
70                         <para>
71                         </para>
72                         <para>exten => 4000,1,Dial(SIP/4000)</para>
73                         <para>
74                         </para>
75                         <para>exten => 5000,1,MixMonitor(blah2.wav)</para>
76                         <para>exten => 5000,n,Dial(SIP/5000)</para>
77                         <para>
78                         </para>
79                         <para>In this basic dialplan scenario, let's consider the following sample calls</para>
80                         <para>Call 1: Caller dials 2000. The person who answers then executes an attended</para>
81                         <para>        transfer to 4000.</para>
82                         <para>Result: Since extension 2000 set MixMonitor to be inheritable, after the</para>
83                         <para>        transfer to 4000 has completed, the call will continue to be recorded
84                         to blah.wav</para>
85                         <para>
86                         </para>
87                         <para>Call 2: Caller dials 5000. The person who answers then executes an attended</para>
88                         <para>        transfer to 4000.</para>
89                         <para>Result: Since extension 5000 did not set MixMonitor to be inheritable, the</para>
90                         <para>        recording will stop once the call has been transferred to 4000.</para>
91                 </description>
92         </function>
93  ***/
94
95 struct inheritable_audiohook {
96         AST_LIST_ENTRY(inheritable_audiohook) list;
97         char source[1];
98 };
99
100 struct audiohook_inheritance_datastore {
101         AST_LIST_HEAD (, inheritable_audiohook) allowed_list;
102 };
103
104 static void audiohook_inheritance_fixup(void *data, struct ast_channel *old_chan, struct ast_channel *new_chan);
105 static void audiohook_inheritance_destroy (void *data);
106 static const struct ast_datastore_info audiohook_inheritance_info = {
107         .type = "audiohook inheritance",
108         .destroy = audiohook_inheritance_destroy,
109         .chan_fixup = audiohook_inheritance_fixup,
110 };
111
112 /*! \brief Move audiohooks as defined by previous calls to the AUDIOHOOK_INHERIT function
113  *
114  * Move allowed audiohooks from the old channel to the new channel.
115  *
116  * \param data The ast_datastore containing audiohook inheritance information that will be moved
117  * \param old_chan The "clone" channel from a masquerade. We are moving the audiohook in question off of this channel
118  * \param new_chan The "original" channel from a masquerade. We are moving the audiohook in question to this channel
119  * \return Void
120  */
121 static void audiohook_inheritance_fixup(void *data, struct ast_channel *old_chan, struct ast_channel *new_chan)
122 {
123         struct inheritable_audiohook *audiohook = NULL;
124         struct audiohook_inheritance_datastore *datastore = data;
125
126         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);
127
128         AST_LIST_TRAVERSE(&datastore->allowed_list, audiohook, list) {
129                 ast_audiohook_move_by_source(old_chan, new_chan, audiohook->source);
130                 ast_debug(3, "Moved audiohook %s from %s(%p) to %s(%p)\n",
131                         audiohook->source, ast_channel_name(old_chan), old_chan, ast_channel_name(new_chan), new_chan);
132         }
133         return;
134 }
135
136 /*! \brief Destroy dynamically allocated data on an audiohook_inheritance_datastore
137  *
138  * \param data Pointer to the audiohook_inheritance_datastore in question.
139  * \return Void
140  */
141 static void audiohook_inheritance_destroy(void *data)
142 {
143         struct audiohook_inheritance_datastore *audiohook_inheritance_datastore = data;
144         struct inheritable_audiohook *inheritable_audiohook = NULL;
145
146         while ((inheritable_audiohook = AST_LIST_REMOVE_HEAD(&audiohook_inheritance_datastore->allowed_list, list))) {
147                 ast_free(inheritable_audiohook);
148         }
149
150         ast_free(audiohook_inheritance_datastore);
151 }
152
153 /*! \brief create an audiohook_inheritance_datastore and attach it to a channel
154  *
155  * \param chan The channel to which we wish to attach the new datastore
156  * \return Returns the newly created audiohook_inheritance_datastore or NULL on error
157  */
158 static struct audiohook_inheritance_datastore *setup_inheritance_datastore(struct ast_channel *chan)
159 {
160         struct ast_datastore *datastore = NULL;
161         struct audiohook_inheritance_datastore *audiohook_inheritance_datastore = NULL;
162
163         if (!(datastore = ast_datastore_alloc(&audiohook_inheritance_info, NULL))) {
164                 return NULL;
165         }
166
167         if (!(audiohook_inheritance_datastore = ast_calloc(1, sizeof(*audiohook_inheritance_datastore)))) {
168                 ast_datastore_free(datastore);
169                 return NULL;
170         }
171
172         datastore->data = audiohook_inheritance_datastore;
173         ast_channel_lock(chan);
174         ast_channel_datastore_add(chan, datastore);
175         ast_channel_unlock(chan);
176         return audiohook_inheritance_datastore;
177 }
178
179 /*! \brief Create a new inheritable_audiohook structure and add it to an audiohook_inheritance_datastore
180  *
181  * \param audiohook_inheritance_datastore The audiohook_inheritance_datastore we want to add the new inheritable_audiohook to
182  * \param source The audiohook source for the newly created inheritable_audiohook
183  * \retval 0 Success
184  * \retval non-zero Failure
185  */
186 static int setup_inheritable_audiohook(struct audiohook_inheritance_datastore *audiohook_inheritance_datastore, const char *source)
187 {
188         struct inheritable_audiohook *inheritable_audiohook = NULL;
189
190         inheritable_audiohook = ast_calloc(1, sizeof(*inheritable_audiohook) + strlen(source));
191
192         if (!inheritable_audiohook) {
193                 return -1;
194         }
195
196         strcpy(inheritable_audiohook->source, source);
197         AST_LIST_INSERT_TAIL(&audiohook_inheritance_datastore->allowed_list, inheritable_audiohook, list);
198         ast_debug(3, "Set audiohook %s to be inheritable\n", source);
199         return 0;
200 }
201
202 /*! \brief Set the permissibility of inheritance for a particular audiohook source on a channel
203  *
204  * For details regarding what happens in the function, see the inline comments
205  *
206  * \param chan The channel we are operating on
207  * \param function The name of the dialplan function (AUDIOHOOK_INHERIT)
208  * \param data The audiohook source for which we are setting inheritance permissions
209  * \param value The value indicating the permission for audiohook inheritance
210  */
211 static int func_inheritance_write(struct ast_channel *chan, const char *function, char *data, const char *value)
212 {
213         int allow;
214         struct ast_datastore *datastore = NULL;
215         struct audiohook_inheritance_datastore *inheritance_datastore = NULL;
216         struct inheritable_audiohook *inheritable_audiohook;
217
218         /* Step 1: Get data from function call */
219         if (ast_strlen_zero(data)) {
220                 ast_log(LOG_WARNING, "No argument provided to INHERITANCE function.\n");
221                 return -1;
222         }
223
224         if (ast_strlen_zero(value)) {
225                 ast_log(LOG_WARNING, "No value provided to INHERITANCE function.\n");
226                 return -1;
227         }
228
229         allow = ast_true(value);
230
231         /* Step 2: retrieve or set up datastore */
232         ast_channel_lock(chan);
233         if (!(datastore = ast_channel_datastore_find(chan, &audiohook_inheritance_info, NULL))) {
234                 ast_channel_unlock(chan);
235                 /* In the case where we cannot find the datastore, we can take a few shortcuts */
236                 if (!allow) {
237                         ast_debug(1, "Audiohook %s is already set to not be inheritable on channel %s\n", data, ast_channel_name(chan));
238                         return 0;
239                 } else if (!(inheritance_datastore = setup_inheritance_datastore(chan))) {
240                         ast_log(LOG_WARNING, "Unable to set up audiohook inheritance datastore on channel %s\n", ast_channel_name(chan));
241                         return -1;
242                 } else {
243                         return setup_inheritable_audiohook(inheritance_datastore, data);
244                 }
245         } else {
246                 inheritance_datastore = datastore->data;
247         }
248         ast_channel_unlock(chan);
249
250         /* Step 3: Traverse the list to see if we're trying something redundant */
251
252         AST_LIST_TRAVERSE_SAFE_BEGIN(&inheritance_datastore->allowed_list, inheritable_audiohook, list) {
253                 if (!strcasecmp(inheritable_audiohook->source, data)) {
254                         if (allow) {
255                                 ast_debug(2, "Audiohook source %s is already set up to be inherited from channel %s\n", data, ast_channel_name(chan));
256                                 return 0;
257                         } else {
258                                 ast_debug(2, "Removing inheritability of audiohook %s from channel %s\n", data, ast_channel_name(chan));
259                                 AST_LIST_REMOVE_CURRENT(list);
260                                 ast_free(inheritable_audiohook);
261                                 return 0;
262                         }
263                 }
264         }
265         AST_LIST_TRAVERSE_SAFE_END;
266
267         /* Step 4: There is no step 4 */
268
269         /* Step 5: This means we are addressing an audiohook source which we have not encountered yet for the channel. Create a new inheritable
270          * audiohook structure if we're allowing inheritance, or just return if not
271          */
272
273         if (allow) {
274                 return setup_inheritable_audiohook(inheritance_datastore, data);
275         } else {
276                 ast_debug(1, "Audiohook %s is already set to not be inheritable on channel %s\n", data, ast_channel_name(chan));
277                 return 0;
278         }
279 }
280
281 static struct ast_custom_function inheritance_function = {
282         .name = "AUDIOHOOK_INHERIT",
283         .write = func_inheritance_write,
284 };
285
286 static int unload_module(void)
287 {
288         return ast_custom_function_unregister(&inheritance_function);
289 }
290
291 static int load_module(void)
292 {
293         if (ast_custom_function_register(&inheritance_function)) {
294                 return AST_MODULE_LOAD_DECLINE;
295         } else {
296                 return AST_MODULE_LOAD_SUCCESS;
297         }
298 }
299 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Audiohook inheritance function");