| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
#include "asterisk.h" |
| 33 |
|
| 34 |
#include <sys/stat.h> |
| 35 |
|
| 36 |
#include "asterisk/module.h" |
| 37 |
#include "asterisk/pbx.h" |
| 38 |
#include "asterisk/channel.h" |
| 39 |
#include "asterisk/app.h" |
| 40 |
#include "asterisk/stasis_channels.h" |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
static void shared_variable_free(void *data); |
| 159 |
|
| 160 |
static const struct ast_datastore_info shared_variable_info = { |
| 161 |
.type = "SHARED_VARIABLES", |
| 162 |
.destroy = shared_variable_free, |
| 163 |
}; |
| 164 |
|
| 165 |
static void shared_variable_free(void *data) |
| 166 |
{ |
| 167 |
struct varshead *varshead = data; |
| 168 |
struct ast_var_t *var; |
| 169 |
|
| 170 |
while ((var = AST_LIST_REMOVE_HEAD(varshead, entries))) { |
| 171 |
ast_var_delete(var); |
| 172 |
} |
| 173 |
ast_free(varshead); |
| 174 |
} |
| 175 |
|
| 176 |
static int global_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len) |
| 177 |
{ |
| 178 |
const char *var = pbx_builtin_getvar_helper(NULL, data); |
| 179 |
|
| 180 |
*buf = '\0'; |
| 181 |
|
| 182 |
if (var) |
| 183 |
ast_copy_string(buf, var, len); |
| 184 |
|
| 185 |
return 0; |
| 186 |
} |
| 187 |
|
| 188 |
static int global_write(struct ast_channel *chan, const char *cmd, char *data, const char *value) |
| 189 |
{ |
| 190 |
pbx_builtin_setvar_helper(NULL, data, value); |
| 191 |
|
| 192 |
return 0; |
| 193 |
} |
| 194 |
|
| 195 |
static struct ast_custom_function global_function = { |
| 196 |
.name = "GLOBAL", |
| 197 |
.read = global_read, |
| 198 |
.write = global_write, |
| 199 |
}; |
| 200 |
|
| 201 |
static int global_delete_write(struct ast_channel *chan, const char *cmd, char *data, const char *value) |
| 202 |
{ |
| 203 |
pbx_builtin_setvar_helper(NULL, data, NULL); |
| 204 |
|
| 205 |
return 0; |
| 206 |
} |
| 207 |
|
| 208 |
static struct ast_custom_function global_delete_function = { |
| 209 |
.name = "GLOBAL_DELETE", |
| 210 |
.write = global_delete_write, |
| 211 |
}; |
| 212 |
|
| 213 |
static int global_exists_read(struct ast_channel *chan, const char *cmd, char *data, |
| 214 |
char *buf, size_t len) |
| 215 |
{ |
| 216 |
const char *var = pbx_builtin_getvar_helper(NULL, data); |
| 217 |
|
| 218 |
strcpy(buf, var ? "1" : "0"); |
| 219 |
|
| 220 |
return 0; |
| 221 |
} |
| 222 |
|
| 223 |
static struct ast_custom_function global_exists_function = { |
| 224 |
.name = "GLOBAL_EXISTS", |
| 225 |
.read = global_exists_read, |
| 226 |
}; |
| 227 |
|
| 228 |
static int shared_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len) |
| 229 |
{ |
| 230 |
struct ast_datastore *varstore; |
| 231 |
struct varshead *varshead; |
| 232 |
struct ast_var_t *var; |
| 233 |
AST_DECLARE_APP_ARGS(args, |
| 234 |
AST_APP_ARG(var); |
| 235 |
AST_APP_ARG(chan); |
| 236 |
); |
| 237 |
struct ast_channel *c_ref = NULL; |
| 238 |
|
| 239 |
if (ast_strlen_zero(data)) { |
| 240 |
ast_log(LOG_WARNING, "SHARED() requires an argument: SHARED(<var>[,<chan>])\n"); |
| 241 |
return -1; |
| 242 |
} |
| 243 |
|
| 244 |
AST_STANDARD_APP_ARGS(args, data); |
| 245 |
|
| 246 |
if (!ast_strlen_zero(args.chan)) { |
| 247 |
char *prefix = ast_alloca(strlen(args.chan) + 2); |
| 248 |
sprintf(prefix, "%s-", args.chan); |
| 249 |
if (!(c_ref = ast_channel_get_by_name(args.chan)) && !(c_ref = ast_channel_get_by_name_prefix(prefix, strlen(prefix)))) { |
| 250 |
ast_log(LOG_ERROR, "Channel '%s' not found! Variable '%s' will be blank.\n", args.chan, args.var); |
| 251 |
return -1; |
| 252 |
} |
| 253 |
chan = c_ref; |
| 254 |
} else if (!chan) { |
| 255 |
ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd); |
| 256 |
return -1; |
| 257 |
} |
| 258 |
|
| 259 |
ast_channel_lock(chan); |
| 260 |
|
| 261 |
if (!(varstore = ast_channel_datastore_find(chan, &shared_variable_info, NULL))) { |
| 262 |
ast_channel_unlock(chan); |
| 263 |
if (c_ref) { |
| 264 |
c_ref = ast_channel_unref(c_ref); |
| 265 |
} |
| 266 |
return -1; |
| 267 |
} |
| 268 |
|
| 269 |
varshead = varstore->data; |
| 270 |
*buf = '\0'; |
| 271 |
|
| 272 |
|
| 273 |
AST_LIST_TRAVERSE(varshead, var, entries) { |
| 274 |
if (!strcmp(args.var, ast_var_name(var))) { |
| 275 |
ast_copy_string(buf, ast_var_value(var), len); |
| 276 |
break; |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
ast_channel_unlock(chan); |
| 281 |
|
| 282 |
if (c_ref) { |
| 283 |
c_ref = ast_channel_unref(c_ref); |
| 284 |
} |
| 285 |
|
| 286 |
return 0; |
| 287 |
} |
| 288 |
|
| 289 |
static int shared_write(struct ast_channel *chan, const char *cmd, char *data, const char *value) |
| 290 |
{ |
| 291 |
struct ast_datastore *varstore; |
| 292 |
struct varshead *varshead; |
| 293 |
struct ast_var_t *var; |
| 294 |
AST_DECLARE_APP_ARGS(args, |
| 295 |
AST_APP_ARG(var); |
| 296 |
AST_APP_ARG(chan); |
| 297 |
); |
| 298 |
struct ast_channel *c_ref = NULL; |
| 299 |
int len; |
| 300 |
RAII_VAR(char *, shared_buffer, NULL, ast_free); |
| 301 |
|
| 302 |
if (ast_strlen_zero(data)) { |
| 303 |
ast_log(LOG_WARNING, "SHARED() requires an argument: SHARED(<var>[,<chan>])\n"); |
| 304 |
return -1; |
| 305 |
} |
| 306 |
|
| 307 |
AST_STANDARD_APP_ARGS(args, data); |
| 308 |
|
| 309 |
if (!ast_strlen_zero(args.chan)) { |
| 310 |
char *prefix = ast_alloca(strlen(args.chan) + 2); |
| 311 |
sprintf(prefix, "%s-", args.chan); |
| 312 |
if (!(c_ref = ast_channel_get_by_name(args.chan)) && !(c_ref = ast_channel_get_by_name_prefix(prefix, strlen(prefix)))) { |
| 313 |
ast_log(LOG_ERROR, "Channel '%s' not found! Variable '%s' not set to '%s'.\n", args.chan, args.var, value); |
| 314 |
return -1; |
| 315 |
} |
| 316 |
chan = c_ref; |
| 317 |
} else if (!chan) { |
| 318 |
ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd); |
| 319 |
return -1; |
| 320 |
} |
| 321 |
|
| 322 |
len = 9 + strlen(args.var); |
| 323 |
shared_buffer = ast_malloc(len); |
| 324 |
if (!shared_buffer) { |
| 325 |
if (c_ref) { |
| 326 |
ast_channel_unref(c_ref); |
| 327 |
} |
| 328 |
return -1; |
| 329 |
} |
| 330 |
|
| 331 |
ast_channel_lock(chan); |
| 332 |
|
| 333 |
if (!(varstore = ast_channel_datastore_find(chan, &shared_variable_info, NULL))) { |
| 334 |
if (!(varstore = ast_datastore_alloc(&shared_variable_info, NULL))) { |
| 335 |
ast_log(LOG_ERROR, "Unable to allocate new datastore. Shared variable not set.\n"); |
| 336 |
ast_channel_unlock(chan); |
| 337 |
if (c_ref) { |
| 338 |
c_ref = ast_channel_unref(c_ref); |
| 339 |
} |
| 340 |
return -1; |
| 341 |
} |
| 342 |
|
| 343 |
if (!(varshead = ast_calloc(1, sizeof(*varshead)))) { |
| 344 |
ast_log(LOG_ERROR, "Unable to allocate variable structure. Shared variable not set.\n"); |
| 345 |
ast_datastore_free(varstore); |
| 346 |
ast_channel_unlock(chan); |
| 347 |
if (c_ref) { |
| 348 |
c_ref = ast_channel_unref(c_ref); |
| 349 |
} |
| 350 |
return -1; |
| 351 |
} |
| 352 |
|
| 353 |
varstore->data = varshead; |
| 354 |
ast_channel_datastore_add(chan, varstore); |
| 355 |
} |
| 356 |
varshead = varstore->data; |
| 357 |
|
| 358 |
|
| 359 |
AST_LIST_TRAVERSE_SAFE_BEGIN(varshead, var, entries) { |
| 360 |
|
| 361 |
if (!strcmp(args.var, ast_var_name(var))) { |
| 362 |
AST_LIST_REMOVE_CURRENT(entries); |
| 363 |
ast_var_delete(var); |
| 364 |
break; |
| 365 |
} |
| 366 |
} |
| 367 |
AST_LIST_TRAVERSE_SAFE_END; |
| 368 |
|
| 369 |
if ((var = ast_var_assign(args.var, S_OR(value, "")))) { |
| 370 |
AST_LIST_INSERT_HEAD(varshead, var, entries); |
| 371 |
|
| 372 |
sprintf(shared_buffer, "SHARED(%s)", args.var); |
| 373 |
ast_channel_publish_varset(chan, shared_buffer, value); |
| 374 |
} |
| 375 |
|
| 376 |
ast_channel_unlock(chan); |
| 377 |
|
| 378 |
if (c_ref) { |
| 379 |
c_ref = ast_channel_unref(c_ref); |
| 380 |
} |
| 381 |
|
| 382 |
return 0; |
| 383 |
} |
| 384 |
|
| 385 |
static struct ast_custom_function shared_function = { |
| 386 |
.name = "SHARED", |
| 387 |
.read = shared_read, |
| 388 |
.write = shared_write, |
| 389 |
}; |
| 390 |
|
| 391 |
static int unload_module(void) |
| 392 |
{ |
| 393 |
int res = 0; |
| 394 |
|
| 395 |
res |= ast_custom_function_unregister(&global_function); |
| 396 |
res |= ast_custom_function_unregister(&global_delete_function); |
| 397 |
res |= ast_custom_function_unregister(&global_exists_function); |
| 398 |
res |= ast_custom_function_unregister(&shared_function); |
| 399 |
|
| 400 |
return res; |
| 401 |
} |
| 402 |
|
| 403 |
static int load_module(void) |
| 404 |
{ |
| 405 |
int res = 0; |
| 406 |
|
| 407 |
res |= ast_custom_function_register(&global_function); |
| 408 |
res |= ast_custom_function_register(&global_delete_function); |
| 409 |
res |= ast_custom_function_register(&global_exists_function); |
| 410 |
res |= ast_custom_function_register(&shared_function); |
| 411 |
|
| 412 |
return res; |
| 413 |
} |
| 414 |
|
| 415 |
AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Variable dialplan functions"); |