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
00032
00033
00034
00035 #include "asterisk.h"
00036 #include "asterisk/datastore.h"
00037 #include "asterisk/channel.h"
00038 #include "asterisk/logger.h"
00039 #include "asterisk/audiohook.h"
00040 #include "asterisk/pbx.h"
00041 #include "asterisk/module.h"
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097 struct inheritable_audiohook {
00098 AST_LIST_ENTRY(inheritable_audiohook) list;
00099 char source[1];
00100 };
00101
00102 struct audiohook_inheritance_datastore {
00103 AST_LIST_HEAD (, inheritable_audiohook) allowed_list;
00104 };
00105
00106 static void audiohook_inheritance_fixup(void *data, struct ast_channel *old_chan, struct ast_channel *new_chan);
00107 static void audiohook_inheritance_destroy (void *data);
00108 static const struct ast_datastore_info audiohook_inheritance_info = {
00109 .type = "audiohook inheritance",
00110 .destroy = audiohook_inheritance_destroy,
00111 .chan_fixup = audiohook_inheritance_fixup,
00112 };
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123 static void audiohook_inheritance_fixup(void *data, struct ast_channel *old_chan, struct ast_channel *new_chan)
00124 {
00125 struct inheritable_audiohook *audiohook = NULL;
00126 struct audiohook_inheritance_datastore *datastore = data;
00127
00128 ast_debug(2, "inheritance fixup occurring for channels %s(%p) and %s(%p)", old_chan->name, old_chan, new_chan->name, new_chan);
00129
00130 AST_LIST_TRAVERSE(&datastore->allowed_list, audiohook, list) {
00131 ast_audiohook_move_by_source(old_chan, new_chan, audiohook->source);
00132 ast_debug(3, "Moved audiohook %s from %s(%p) to %s(%p)\n",
00133 audiohook->source, old_chan->name, old_chan, new_chan->name, new_chan);
00134 }
00135 return;
00136 }
00137
00138
00139
00140
00141
00142
00143 static void audiohook_inheritance_destroy(void *data)
00144 {
00145 struct audiohook_inheritance_datastore *audiohook_inheritance_datastore = data;
00146 struct inheritable_audiohook *inheritable_audiohook = NULL;
00147
00148 while ((inheritable_audiohook = AST_LIST_REMOVE_HEAD(&audiohook_inheritance_datastore->allowed_list, list))) {
00149 ast_free(inheritable_audiohook);
00150 }
00151
00152 ast_free(audiohook_inheritance_datastore);
00153 }
00154
00155
00156
00157
00158
00159
00160 static struct audiohook_inheritance_datastore *setup_inheritance_datastore(struct ast_channel *chan)
00161 {
00162 struct ast_datastore *datastore = NULL;
00163 struct audiohook_inheritance_datastore *audiohook_inheritance_datastore = NULL;
00164
00165 if (!(datastore = ast_datastore_alloc(&audiohook_inheritance_info, NULL))) {
00166 return NULL;
00167 }
00168
00169 if (!(audiohook_inheritance_datastore = ast_calloc(1, sizeof(*audiohook_inheritance_datastore)))) {
00170 ast_datastore_free(datastore);
00171 return NULL;
00172 }
00173
00174 datastore->data = audiohook_inheritance_datastore;
00175 ast_channel_lock(chan);
00176 ast_channel_datastore_add(chan, datastore);
00177 ast_channel_unlock(chan);
00178 return audiohook_inheritance_datastore;
00179 }
00180
00181
00182
00183
00184
00185
00186
00187
00188 static int setup_inheritable_audiohook(struct audiohook_inheritance_datastore *audiohook_inheritance_datastore, const char *source)
00189 {
00190 struct inheritable_audiohook *inheritable_audiohook = NULL;
00191
00192 inheritable_audiohook = ast_calloc(1, sizeof(*inheritable_audiohook) + strlen(source));
00193
00194 if (!inheritable_audiohook) {
00195 return -1;
00196 }
00197
00198 strcpy(inheritable_audiohook->source, source);
00199 AST_LIST_INSERT_TAIL(&audiohook_inheritance_datastore->allowed_list, inheritable_audiohook, list);
00200 ast_debug(3, "Set audiohook %s to be inheritable\n", source);
00201 return 0;
00202 }
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213 static int func_inheritance_write(struct ast_channel *chan, const char *function, char *data, const char *value)
00214 {
00215 int allow;
00216 struct ast_datastore *datastore = NULL;
00217 struct audiohook_inheritance_datastore *inheritance_datastore = NULL;
00218 struct inheritable_audiohook *inheritable_audiohook;
00219
00220
00221 if (ast_strlen_zero(data)) {
00222 ast_log(LOG_WARNING, "No argument provided to INHERITANCE function.\n");
00223 return -1;
00224 }
00225
00226 if (ast_strlen_zero(value)) {
00227 ast_log(LOG_WARNING, "No value provided to INHERITANCE function.\n");
00228 return -1;
00229 }
00230
00231 allow = ast_true(value);
00232
00233
00234 ast_channel_lock(chan);
00235 if (!(datastore = ast_channel_datastore_find(chan, &audiohook_inheritance_info, NULL))) {
00236 ast_channel_unlock(chan);
00237
00238 if (!allow) {
00239 ast_debug(1, "Audiohook %s is already set to not be inheritable on channel %s\n", data, chan->name);
00240 return 0;
00241 } else if (!(inheritance_datastore = setup_inheritance_datastore(chan))) {
00242 ast_log(LOG_WARNING, "Unable to set up audiohook inheritance datastore on channel %s\n", chan->name);
00243 return -1;
00244 } else {
00245 return setup_inheritable_audiohook(inheritance_datastore, data);
00246 }
00247 } else {
00248 inheritance_datastore = datastore->data;
00249 }
00250 ast_channel_unlock(chan);
00251
00252
00253
00254 AST_LIST_TRAVERSE_SAFE_BEGIN(&inheritance_datastore->allowed_list, inheritable_audiohook, list) {
00255 if (!strcasecmp(inheritable_audiohook->source, data)) {
00256 if (allow) {
00257 ast_debug(2, "Audiohook source %s is already set up to be inherited from channel %s\n", data, chan->name);
00258 return 0;
00259 } else {
00260 ast_debug(2, "Removing inheritability of audiohook %s from channel %s\n", data, chan->name);
00261 AST_LIST_REMOVE_CURRENT(list);
00262 ast_free(inheritable_audiohook);
00263 return 0;
00264 }
00265 }
00266 }
00267 AST_LIST_TRAVERSE_SAFE_END;
00268
00269
00270
00271
00272
00273
00274
00275 if (allow) {
00276 return setup_inheritable_audiohook(inheritance_datastore, data);
00277 } else {
00278 ast_debug(1, "Audiohook %s is already set to not be inheritable on channel %s\n", data, chan->name);
00279 return 0;
00280 }
00281 }
00282
00283 static struct ast_custom_function inheritance_function = {
00284 .name = "AUDIOHOOK_INHERIT",
00285 .write = func_inheritance_write,
00286 };
00287
00288 static int unload_module(void)
00289 {
00290 return ast_custom_function_unregister(&inheritance_function);
00291 }
00292
00293 static int load_module(void)
00294 {
00295 if (ast_custom_function_register(&inheritance_function)) {
00296 return AST_MODULE_LOAD_DECLINE;
00297 } else {
00298 return AST_MODULE_LOAD_SUCCESS;
00299 }
00300 }
00301 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Audiohook inheritance function");