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