00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #include "asterisk.h"
00032 #include "asterisk/channel.h"
00033 #include "asterisk/audiohook.h"
00034 #include "asterisk/pbx.h"
00035 #include "asterisk/module.h"
00036
00037 struct inheritable_audiohook {
00038 AST_LIST_ENTRY(inheritable_audiohook) list;
00039 char source[1];
00040 };
00041
00042 struct audiohook_inheritance_datastore {
00043 AST_LIST_HEAD (, inheritable_audiohook) allowed_list;
00044 };
00045
00046 static void audiohook_inheritance_fixup(void *data, struct ast_channel *old_chan, struct ast_channel *new_chan);
00047 static void audiohook_inheritance_destroy (void *data);
00048 static const struct ast_datastore_info audiohook_inheritance_info = {
00049 .type = "audiohook inheritance",
00050 .destroy = audiohook_inheritance_destroy,
00051 .chan_fixup = audiohook_inheritance_fixup,
00052 };
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063 static void audiohook_inheritance_fixup(void *data, struct ast_channel *old_chan, struct ast_channel *new_chan)
00064 {
00065 struct inheritable_audiohook *audiohook = NULL;
00066 struct audiohook_inheritance_datastore *datastore = data;
00067
00068 ast_debug(2, "inheritance fixup occurring for channels %s(%p) and %s(%p)", old_chan->name, old_chan, new_chan->name, new_chan);
00069
00070 AST_LIST_TRAVERSE(&datastore->allowed_list, audiohook, list) {
00071 ast_audiohook_move_by_source(old_chan, new_chan, audiohook->source);
00072 ast_debug(3, "Moved audiohook %s from %s(%p) to %s(%p)\n",
00073 audiohook->source, old_chan->name, old_chan, new_chan->name, new_chan);
00074 }
00075 return;
00076 }
00077
00078
00079
00080
00081
00082
00083 static void audiohook_inheritance_destroy(void *data)
00084 {
00085 struct audiohook_inheritance_datastore *audiohook_inheritance_datastore = data;
00086 struct inheritable_audiohook *inheritable_audiohook = NULL;
00087
00088 while ((inheritable_audiohook = AST_LIST_REMOVE_HEAD(&audiohook_inheritance_datastore->allowed_list, list))) {
00089 ast_free(inheritable_audiohook);
00090 }
00091 }
00092
00093
00094
00095
00096
00097
00098 static struct audiohook_inheritance_datastore *setup_inheritance_datastore(struct ast_channel *chan)
00099 {
00100 struct ast_datastore *datastore = NULL;
00101 struct audiohook_inheritance_datastore *audiohook_inheritance_datastore = NULL;
00102
00103 if (!(datastore = ast_channel_datastore_alloc(&audiohook_inheritance_info, NULL))) {
00104 return NULL;
00105 }
00106
00107 if (!(audiohook_inheritance_datastore = ast_calloc(1, sizeof(*audiohook_inheritance_datastore)))) {
00108 ast_channel_datastore_free(datastore);
00109 return NULL;
00110 }
00111
00112 datastore->data = audiohook_inheritance_datastore;
00113 ast_channel_lock(chan);
00114 ast_channel_datastore_add(chan, datastore);
00115 ast_channel_unlock(chan);
00116 return audiohook_inheritance_datastore;
00117 }
00118
00119
00120
00121
00122
00123
00124
00125
00126 static int setup_inheritable_audiohook(struct audiohook_inheritance_datastore *audiohook_inheritance_datastore, const char *source)
00127 {
00128 struct inheritable_audiohook *inheritable_audiohook = NULL;
00129
00130 inheritable_audiohook = ast_calloc(1, sizeof(*inheritable_audiohook) + strlen(source));
00131
00132 if (!inheritable_audiohook) {
00133 return -1;
00134 }
00135
00136 strcpy(inheritable_audiohook->source, source);
00137 AST_LIST_INSERT_TAIL(&audiohook_inheritance_datastore->allowed_list, inheritable_audiohook, list);
00138 ast_debug(3, "Set audiohook %s to be inheritable\n", source);
00139 return 0;
00140 }
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151 static int func_inheritance_write(struct ast_channel *chan, const char *function, char *data, const char *value)
00152 {
00153 int allow;
00154 struct ast_datastore *datastore = NULL;
00155 struct audiohook_inheritance_datastore *inheritance_datastore = NULL;
00156 struct inheritable_audiohook *inheritable_audiohook;
00157
00158
00159 if (ast_strlen_zero(data)) {
00160 ast_log(LOG_WARNING, "No argument provided to INHERITANCE function.\n");
00161 return -1;
00162 }
00163
00164 if (ast_strlen_zero(value)) {
00165 ast_log(LOG_WARNING, "No value provided to INHERITANCE function.\n");
00166 return -1;
00167 }
00168
00169 allow = ast_true(value);
00170
00171
00172 ast_channel_lock(chan);
00173 if (!(datastore = ast_channel_datastore_find(chan, &audiohook_inheritance_info, NULL))) {
00174 ast_channel_unlock(chan);
00175
00176 if (!allow) {
00177 ast_debug(1, "Audiohook %s is already set to not be inheritable on channel %s\n", data, chan->name);
00178 return 0;
00179 } else if (!(inheritance_datastore = setup_inheritance_datastore(chan))) {
00180 ast_log(LOG_WARNING, "Unable to set up audiohook inheritance datastore on channel %s\n", chan->name);
00181 return -1;
00182 } else {
00183 return setup_inheritable_audiohook(inheritance_datastore, data);
00184 }
00185 } else {
00186 inheritance_datastore = datastore->data;
00187 }
00188 ast_channel_unlock(chan);
00189
00190
00191
00192 AST_LIST_TRAVERSE_SAFE_BEGIN(&inheritance_datastore->allowed_list, inheritable_audiohook, list) {
00193 if (!strcasecmp(inheritable_audiohook->source, data)) {
00194 if (allow) {
00195 ast_debug(2, "Audiohook source %s is already set up to be inherited from channel %s\n", data, chan->name);
00196 return 0;
00197 } else {
00198 ast_debug(2, "Removing inheritability of audiohook %s from channel %s\n", data, chan->name);
00199 AST_LIST_REMOVE_CURRENT(list);
00200 ast_free(inheritable_audiohook);
00201 return 0;
00202 }
00203 }
00204 }
00205 AST_LIST_TRAVERSE_SAFE_END;
00206
00207
00208
00209
00210
00211
00212
00213 if (allow) {
00214 return setup_inheritable_audiohook(inheritance_datastore, data);
00215 } else {
00216 ast_debug(1, "Audiohook %s is already set to not be inheritable on channel %s\n", data, chan->name);
00217 return 0;
00218 }
00219 }
00220
00221 static struct ast_custom_function inheritance_function = {
00222 .name = "AUDIOHOOK_INHERIT",
00223 .synopsis = "Set whether an audiohook may be inherited to another channel",
00224 .syntax = "AUDIOHOOK_INHERIT(source)",
00225 .desc =
00226 "By enabling audiohook inheritance on the channel, you are giving\n"
00227 "permission for an audiohook to be inherited by a descendent channel.\n"
00228 "Inheritance may be be disabled at any point as well.\n"
00229 "\n"
00230 " Example scenario:\n"
00231 " exten => 2000,1,MixMonitor(blah.wav)\n"
00232 " exten => 2000,n,Set(AUDIOHOOK_INHERIT(MixMonitor)=yes)\n"
00233 " exten => 2000,n,Dial(SIP/2000)\n"
00234 "\n"
00235 " exten => 4000,1,Dial(SIP/4000)\n"
00236 "\n"
00237 " exten => 5000,1,MixMonitor(blah2.wav)\n"
00238 " exten => 5000,n,Dial(SIP/5000)\n"
00239 "\n"
00240 " In this basic dialplan scenario, let's consider the following sample calls\n"
00241 " Call 1: Caller dials 2000. The person who answers then executes an attended\n"
00242 " transfer to 4000.\n"
00243 " Result: Since extension 2000 set MixMonitor to be inheritable, after the\n"
00244 " transfer to 400 has completed, the call will continue to be recorded\n"
00245 " to blah.wav\n"
00246 "\n"
00247 " Call 2: Caller dials 5000. The person who answers then executes an attended\n"
00248 " transfer to 4000.\n"
00249 " Result: Since extension 5000 did not set MixMonitor to be inheritable, the\n"
00250 " recording will stop once the call has been transferred to 4000.\n",
00251 .write = func_inheritance_write,
00252 };
00253
00254 static int unload_module(void)
00255 {
00256 return ast_custom_function_unregister(&inheritance_function);
00257 }
00258
00259 static int load_module(void)
00260 {
00261 if (ast_custom_function_register(&inheritance_function)) {
00262 return AST_MODULE_LOAD_DECLINE;
00263 } else {
00264 return AST_MODULE_LOAD_SUCCESS;
00265 }
00266 }
00267 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Audiohook inheritance function");