| 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 |
#include "asterisk.h" |
| 31 |
|
| 32 |
#include "asterisk/_private.h" |
| 33 |
#include "asterisk/app.h" |
| 34 |
#include "asterisk/ast_expr.h" |
| 35 |
#include "asterisk/chanvars.h" |
| 36 |
#include "asterisk/cli.h" |
| 37 |
#include "asterisk/linkedlists.h" |
| 38 |
#include "asterisk/lock.h" |
| 39 |
#include "asterisk/module.h" |
| 40 |
#include "asterisk/paths.h" |
| 41 |
#include "asterisk/pbx.h" |
| 42 |
#include "asterisk/format_cache.h" |
| 43 |
#include "asterisk/stasis_channels.h" |
| 44 |
#include "asterisk/test.h" |
| 45 |
#include "pbx_private.h" |
| 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 |
AST_RWLOCK_DEFINE_STATIC(globalslock); |
| 118 |
static struct varshead globals = AST_LIST_HEAD_NOLOCK_INIT_VALUE; |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
static int parse_variable_name(char *var, int *offset, int *length, int *isfunc) |
| 126 |
{ |
| 127 |
int parens = 0; |
| 128 |
|
| 129 |
*offset = 0; |
| 130 |
*length = INT_MAX; |
| 131 |
*isfunc = 0; |
| 132 |
for (; *var; var++) { |
| 133 |
if (*var == '(') { |
| 134 |
(*isfunc)++; |
| 135 |
parens++; |
| 136 |
} else if (*var == ')') { |
| 137 |
parens--; |
| 138 |
} else if (*var == ':' && parens == 0) { |
| 139 |
*var++ = '\0'; |
| 140 |
sscanf(var, "%30d:%30d", offset, length); |
| 141 |
return 1; |
| 142 |
} |
| 143 |
} |
| 144 |
return 0; |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
static char *substring(const char *value, int offset, int length, char *workspace, size_t workspace_len) |
| 159 |
{ |
| 160 |
char *ret = workspace; |
| 161 |
int lr; |
| 162 |
|
| 163 |
ast_copy_string(workspace, value, workspace_len); |
| 164 |
|
| 165 |
lr = strlen(ret); |
| 166 |
|
| 167 |
|
| 168 |
if (offset == 0 && length >= lr) |
| 169 |
return ret; |
| 170 |
|
| 171 |
if (offset < 0) { |
| 172 |
offset = lr + offset; |
| 173 |
if (offset < 0) |
| 174 |
offset = 0; |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
if (offset >= lr) |
| 179 |
return ret + lr; |
| 180 |
|
| 181 |
ret += offset; |
| 182 |
if (length >= 0 && length < lr - offset) |
| 183 |
ret[length] = '\0'; |
| 184 |
else if (length < 0) { |
| 185 |
if (lr > offset - length) |
| 186 |
ret[lr + length - offset] = '\0'; |
| 187 |
else |
| 188 |
ret[0] = '\0'; |
| 189 |
} |
| 190 |
|
| 191 |
return ret; |
| 192 |
} |
| 193 |
|
| 194 |
static const char *ast_str_substring(struct ast_str *value, int offset, int length) |
| 195 |
{ |
| 196 |
int lr; |
| 197 |
|
| 198 |
lr = ast_str_strlen(value); |
| 199 |
|
| 200 |
ast_assert(lr == strlen(ast_str_buffer(value))); |
| 201 |
|
| 202 |
|
| 203 |
if (offset == 0 && length >= lr) |
| 204 |
return ast_str_buffer(value); |
| 205 |
|
| 206 |
if (offset < 0) { |
| 207 |
offset = lr + offset; |
| 208 |
if (offset < 0) |
| 209 |
offset = 0; |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
if (offset >= lr) { |
| 214 |
ast_str_reset(value); |
| 215 |
return ast_str_buffer(value); |
| 216 |
} |
| 217 |
|
| 218 |
if (offset > 0) { |
| 219 |
|
| 220 |
memmove(ast_str_buffer(value), ast_str_buffer(value) + offset, ast_str_strlen(value) - offset + 1); |
| 221 |
lr -= offset; |
| 222 |
} |
| 223 |
|
| 224 |
if (length >= 0 && length < lr) { |
| 225 |
ast_str_truncate(value, length); |
| 226 |
} else if (length < 0) { |
| 227 |
if (lr > -length) { |
| 228 |
ast_str_truncate(value, lr + length); |
| 229 |
} else { |
| 230 |
ast_str_reset(value); |
| 231 |
} |
| 232 |
} else { |
| 233 |
|
| 234 |
ast_str_update(value); |
| 235 |
} |
| 236 |
|
| 237 |
return ast_str_buffer(value); |
| 238 |
} |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
void pbx_retrieve_variable(struct ast_channel *c, const char *var, char **ret, char *workspace, int workspacelen, struct varshead *headp) |
| 247 |
{ |
| 248 |
struct ast_str *str = ast_str_create(16); |
| 249 |
const char *cret; |
| 250 |
|
| 251 |
cret = ast_str_retrieve_variable(&str, 0, c, headp, var); |
| 252 |
ast_copy_string(workspace, ast_str_buffer(str), workspacelen); |
| 253 |
*ret = cret ? workspace : NULL; |
| 254 |
ast_free(str); |
| 255 |
} |
| 256 |
|
| 257 |
const char *ast_str_retrieve_variable(struct ast_str **str, ssize_t maxlen, struct ast_channel *c, struct varshead *headp, const char *var) |
| 258 |
{ |
| 259 |
const char not_found = '\0'; |
| 260 |
char *tmpvar; |
| 261 |
const char *ret; |
| 262 |
const char *s; |
| 263 |
int offset, length; |
| 264 |
int i, need_substring; |
| 265 |
struct varshead *places[2] = { headp, &globals }; |
| 266 |
char workspace[20]; |
| 267 |
|
| 268 |
if (c) { |
| 269 |
ast_channel_lock(c); |
| 270 |
places[0] = ast_channel_varshead(c); |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
tmpvar = ast_strdupa(var); |
| 278 |
need_substring = parse_variable_name(tmpvar, &offset, &length, &i ); |
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
s = ¬_found; |
| 296 |
if (c) { |
| 297 |
|
| 298 |
if (!strncmp(var, "CALL", 4)) { |
| 299 |
if (!strncmp(var + 4, "ING", 3)) { |
| 300 |
if (!strcmp(var + 7, "PRES")) { |
| 301 |
ast_str_set(str, maxlen, "%d", |
| 302 |
ast_party_id_presentation(&ast_channel_caller(c)->id)); |
| 303 |
s = ast_str_buffer(*str); |
| 304 |
} else if (!strcmp(var + 7, "ANI2")) { |
| 305 |
ast_str_set(str, maxlen, "%d", ast_channel_caller(c)->ani2); |
| 306 |
s = ast_str_buffer(*str); |
| 307 |
} else if (!strcmp(var + 7, "TON")) { |
| 308 |
ast_str_set(str, maxlen, "%d", ast_channel_caller(c)->id.number.plan); |
| 309 |
s = ast_str_buffer(*str); |
| 310 |
} else if (!strcmp(var + 7, "TNS")) { |
| 311 |
ast_str_set(str, maxlen, "%d", ast_channel_dialed(c)->transit_network_select); |
| 312 |
s = ast_str_buffer(*str); |
| 313 |
} |
| 314 |
} |
| 315 |
} else if (!strcmp(var, "HINT")) { |
| 316 |
s = ast_str_get_hint(str, maxlen, NULL, 0, c, ast_channel_context(c), ast_channel_exten(c)) ? ast_str_buffer(*str) : NULL; |
| 317 |
} else if (!strcmp(var, "HINTNAME")) { |
| 318 |
s = ast_str_get_hint(NULL, 0, str, maxlen, c, ast_channel_context(c), ast_channel_exten(c)) ? ast_str_buffer(*str) : NULL; |
| 319 |
} else if (!strcmp(var, "EXTEN")) { |
| 320 |
s = ast_channel_exten(c); |
| 321 |
} else if (!strcmp(var, "CONTEXT")) { |
| 322 |
s = ast_channel_context(c); |
| 323 |
} else if (!strcmp(var, "PRIORITY")) { |
| 324 |
ast_str_set(str, maxlen, "%d", ast_channel_priority(c)); |
| 325 |
s = ast_str_buffer(*str); |
| 326 |
} else if (!strcmp(var, "CHANNEL")) { |
| 327 |
s = ast_channel_name(c); |
| 328 |
} else if (!strcmp(var, "UNIQUEID")) { |
| 329 |
s = ast_channel_uniqueid(c); |
| 330 |
} else if (!strcmp(var, "HANGUPCAUSE")) { |
| 331 |
ast_str_set(str, maxlen, "%d", ast_channel_hangupcause(c)); |
| 332 |
s = ast_str_buffer(*str); |
| 333 |
} |
| 334 |
} |
| 335 |
if (s == ¬_found) { |
| 336 |
if (!strcmp(var, "EPOCH")) { |
| 337 |
ast_str_set(str, maxlen, "%d", (int) time(NULL)); |
| 338 |
s = ast_str_buffer(*str); |
| 339 |
} else if (!strcmp(var, "SYSTEMNAME")) { |
| 340 |
s = ast_config_AST_SYSTEM_NAME; |
| 341 |
} else if (!strcmp(var, "ASTCACHEDIR")) { |
| 342 |
s = ast_config_AST_CACHE_DIR; |
| 343 |
} else if (!strcmp(var, "ASTETCDIR")) { |
| 344 |
s = ast_config_AST_CONFIG_DIR; |
| 345 |
} else if (!strcmp(var, "ASTMODDIR")) { |
| 346 |
s = ast_config_AST_MODULE_DIR; |
| 347 |
} else if (!strcmp(var, "ASTVARLIBDIR")) { |
| 348 |
s = ast_config_AST_VAR_DIR; |
| 349 |
} else if (!strcmp(var, "ASTDBDIR")) { |
| 350 |
s = ast_config_AST_DB; |
| 351 |
} else if (!strcmp(var, "ASTKEYDIR")) { |
| 352 |
s = ast_config_AST_KEY_DIR; |
| 353 |
} else if (!strcmp(var, "ASTDATADIR")) { |
| 354 |
s = ast_config_AST_DATA_DIR; |
| 355 |
} else if (!strcmp(var, "ASTAGIDIR")) { |
| 356 |
s = ast_config_AST_AGI_DIR; |
| 357 |
} else if (!strcmp(var, "ASTSPOOLDIR")) { |
| 358 |
s = ast_config_AST_SPOOL_DIR; |
| 359 |
} else if (!strcmp(var, "ASTRUNDIR")) { |
| 360 |
s = ast_config_AST_RUN_DIR; |
| 361 |
} else if (!strcmp(var, "ASTLOGDIR")) { |
| 362 |
s = ast_config_AST_LOG_DIR; |
| 363 |
} else if (!strcmp(var, "ASTSBINDIR")) { |
| 364 |
s = ast_config_AST_SBIN_DIR; |
| 365 |
} else if (!strcmp(var, "ENTITYID")) { |
| 366 |
ast_eid_to_str(workspace, sizeof(workspace), &ast_eid_default); |
| 367 |
s = workspace; |
| 368 |
} |
| 369 |
} |
| 370 |
|
| 371 |
for (i = 0; s == ¬_found && i < ARRAY_LEN(places); i++) { |
| 372 |
struct ast_var_t *variables; |
| 373 |
if (!places[i]) |
| 374 |
continue; |
| 375 |
if (places[i] == &globals) |
| 376 |
ast_rwlock_rdlock(&globalslock); |
| 377 |
AST_LIST_TRAVERSE(places[i], variables, entries) { |
| 378 |
if (!strcmp(ast_var_name(variables), var)) { |
| 379 |
s = ast_var_value(variables); |
| 380 |
break; |
| 381 |
} |
| 382 |
} |
| 383 |
if (places[i] == &globals) |
| 384 |
ast_rwlock_unlock(&globalslock); |
| 385 |
} |
| 386 |
if (s == ¬_found || s == NULL) { |
| 387 |
ast_debug(5, "Result of '%s' is NULL\n", var); |
| 388 |
ret = NULL; |
| 389 |
} else { |
| 390 |
ast_debug(5, "Result of '%s' is '%s'\n", var, s); |
| 391 |
if (s != ast_str_buffer(*str)) { |
| 392 |
ast_str_set(str, maxlen, "%s", s); |
| 393 |
} |
| 394 |
ret = ast_str_buffer(*str); |
| 395 |
if (need_substring) { |
| 396 |
ret = ast_str_substring(*str, offset, length); |
| 397 |
ast_debug(2, "Final result of '%s' is '%s'\n", var, ret); |
| 398 |
} |
| 399 |
} |
| 400 |
|
| 401 |
if (c) { |
| 402 |
ast_channel_unlock(c); |
| 403 |
} |
| 404 |
return ret; |
| 405 |
} |
| 406 |
|
| 407 |
void ast_str_substitute_variables_full2(struct ast_str **buf, ssize_t maxlen, |
| 408 |
struct ast_channel *c, struct varshead *headp, const char *templ, |
| 409 |
size_t *used, int use_both) |
| 410 |
{ |
| 411 |
|
| 412 |
const char *whereweare; |
| 413 |
struct ast_str *substr1 = ast_str_create(16); |
| 414 |
struct ast_str *substr2 = NULL; |
| 415 |
struct ast_str *substr3 = ast_str_create(16); |
| 416 |
|
| 417 |
ast_str_reset(*buf); |
| 418 |
|
| 419 |
if (!substr1 || !substr3) { |
| 420 |
if (used) { |
| 421 |
*used = ast_str_strlen(*buf); |
| 422 |
} |
| 423 |
ast_free(substr1); |
| 424 |
ast_free(substr3); |
| 425 |
return; |
| 426 |
} |
| 427 |
|
| 428 |
whereweare = templ; |
| 429 |
while (!ast_strlen_zero(whereweare)) { |
| 430 |
const char *nextvar = NULL; |
| 431 |
const char *nextexp = NULL; |
| 432 |
const char *nextthing; |
| 433 |
const char *vars; |
| 434 |
const char *vare; |
| 435 |
char *finalvars; |
| 436 |
int pos; |
| 437 |
int brackets; |
| 438 |
int needsub; |
| 439 |
int len; |
| 440 |
|
| 441 |
|
| 442 |
ast_str_reset(substr3); |
| 443 |
|
| 444 |
|
| 445 |
nextthing = strchr((char *)whereweare, '$'); |
| 446 |
if (nextthing) { |
| 447 |
pos = nextthing - whereweare; |
| 448 |
switch (nextthing[1]) { |
| 449 |
case '{': |
| 450 |
|
| 451 |
nextvar = nextthing; |
| 452 |
break; |
| 453 |
case '[': |
| 454 |
|
| 455 |
nextexp = nextthing; |
| 456 |
break; |
| 457 |
default: |
| 458 |
|
| 459 |
++pos; |
| 460 |
break; |
| 461 |
} |
| 462 |
} else { |
| 463 |
|
| 464 |
pos = strlen(whereweare); |
| 465 |
} |
| 466 |
|
| 467 |
if (pos) { |
| 468 |
|
| 469 |
ast_str_append_substr(buf, maxlen, whereweare, pos); |
| 470 |
|
| 471 |
whereweare += pos; |
| 472 |
} |
| 473 |
|
| 474 |
if (nextvar) { |
| 475 |
int offset; |
| 476 |
int offset2; |
| 477 |
int isfunction; |
| 478 |
int res; |
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
vars = vare = nextvar + 2; |
| 484 |
brackets = 1; |
| 485 |
needsub = 0; |
| 486 |
|
| 487 |
|
| 488 |
while (brackets && *vare) { |
| 489 |
if ((vare[0] == '$') && (vare[1] == '{')) { |
| 490 |
needsub++; |
| 491 |
brackets++; |
| 492 |
vare++; |
| 493 |
} else if (vare[0] == '{') { |
| 494 |
brackets++; |
| 495 |
} else if (vare[0] == '}') { |
| 496 |
brackets--; |
| 497 |
} else if ((vare[0] == '$') && (vare[1] == '[')) { |
| 498 |
needsub++; |
| 499 |
vare++; |
| 500 |
} |
| 501 |
vare++; |
| 502 |
} |
| 503 |
len = vare - vars; |
| 504 |
if (brackets) { |
| 505 |
ast_log(LOG_WARNING, "Error in extension logic (missing '}')\n"); |
| 506 |
} else { |
| 507 |
|
| 508 |
--len; |
| 509 |
} |
| 510 |
|
| 511 |
|
| 512 |
whereweare = vare; |
| 513 |
|
| 514 |
|
| 515 |
ast_str_set_substr(&substr1, 0, vars, len); |
| 516 |
ast_debug(5, "Evaluating '%s' (from '%s' len %d)\n", |
| 517 |
ast_str_buffer(substr1), vars, len); |
| 518 |
|
| 519 |
|
| 520 |
if (needsub) { |
| 521 |
if (!substr2) { |
| 522 |
substr2 = ast_str_create(16); |
| 523 |
if (!substr2) { |
| 524 |
continue; |
| 525 |
} |
| 526 |
} |
| 527 |
ast_str_substitute_variables_full2(&substr2, 0, c, headp, |
| 528 |
ast_str_buffer(substr1), NULL, use_both); |
| 529 |
finalvars = ast_str_buffer(substr2); |
| 530 |
} else { |
| 531 |
finalvars = ast_str_buffer(substr1); |
| 532 |
} |
| 533 |
|
| 534 |
parse_variable_name(finalvars, &offset, &offset2, &isfunction); |
| 535 |
if (isfunction) { |
| 536 |
|
| 537 |
res = -1; |
| 538 |
if (c) { |
| 539 |
res = ast_func_read2(c, finalvars, &substr3, 0); |
| 540 |
ast_debug(2, "Function %s result is '%s' from channel\n", |
| 541 |
finalvars, res ? "" : ast_str_buffer(substr3)); |
| 542 |
} |
| 543 |
if (!c || (c && res < 0 && use_both)) { |
| 544 |
struct varshead old; |
| 545 |
struct ast_channel *bogus; |
| 546 |
|
| 547 |
bogus = ast_dummy_channel_alloc(); |
| 548 |
if (bogus) { |
| 549 |
old = *ast_channel_varshead(bogus); |
| 550 |
if (headp) { |
| 551 |
*ast_channel_varshead(bogus) = *headp; |
| 552 |
} |
| 553 |
res = ast_func_read2(bogus, finalvars, &substr3, 0); |
| 554 |
|
| 555 |
if (headp) { |
| 556 |
*ast_channel_varshead(bogus) = old; |
| 557 |
} |
| 558 |
ast_channel_unref(bogus); |
| 559 |
} else { |
| 560 |
ast_log(LOG_ERROR, "Unable to allocate bogus channel for function value substitution.\n"); |
| 561 |
res = -1; |
| 562 |
} |
| 563 |
ast_debug(2, "Function %s result is '%s' from headp\n", |
| 564 |
finalvars, res ? "" : ast_str_buffer(substr3)); |
| 565 |
} |
| 566 |
} else { |
| 567 |
const char *result; |
| 568 |
if (c) { |
| 569 |
result = ast_str_retrieve_variable(&substr3, 0, c, NULL, finalvars); |
| 570 |
ast_debug(2, "Variable %s result is '%s' from channel\n", |
| 571 |
finalvars, S_OR(result, "")); |
| 572 |
} |
| 573 |
if (!c || (c && !result && use_both)) { |
| 574 |
result = ast_str_retrieve_variable(&substr3, 0, NULL, headp, finalvars); |
| 575 |
ast_debug(2, "Variable %s result is '%s' from headp\n", |
| 576 |
finalvars, S_OR(result, "")); |
| 577 |
} |
| 578 |
res = (result ? 0 : -1); |
| 579 |
} |
| 580 |
if (!res) { |
| 581 |
ast_str_substring(substr3, offset, offset2); |
| 582 |
ast_str_append(buf, maxlen, "%s", ast_str_buffer(substr3)); |
| 583 |
} |
| 584 |
} else if (nextexp) { |
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
vars = vare = nextexp + 2; |
| 589 |
brackets = 1; |
| 590 |
needsub = 0; |
| 591 |
|
| 592 |
|
| 593 |
while (brackets && *vare) { |
| 594 |
if ((vare[0] == '$') && (vare[1] == '[')) { |
| 595 |
needsub++; |
| 596 |
brackets++; |
| 597 |
vare++; |
| 598 |
} else if (vare[0] == '[') { |
| 599 |
brackets++; |
| 600 |
} else if (vare[0] == ']') { |
| 601 |
brackets--; |
| 602 |
} else if ((vare[0] == '$') && (vare[1] == '{')) { |
| 603 |
needsub++; |
| 604 |
vare++; |
| 605 |
} |
| 606 |
vare++; |
| 607 |
} |
| 608 |
len = vare - vars; |
| 609 |
if (brackets) { |
| 610 |
ast_log(LOG_WARNING, "Error in extension logic (missing ']')\n"); |
| 611 |
} else { |
| 612 |
|
| 613 |
--len; |
| 614 |
} |
| 615 |
|
| 616 |
|
| 617 |
whereweare = vare; |
| 618 |
|
| 619 |
|
| 620 |
ast_str_set_substr(&substr1, 0, vars, len); |
| 621 |
|
| 622 |
|
| 623 |
if (needsub) { |
| 624 |
if (!substr2) { |
| 625 |
substr2 = ast_str_create(16); |
| 626 |
if (!substr2) { |
| 627 |
continue; |
| 628 |
} |
| 629 |
} |
| 630 |
ast_str_substitute_variables_full2(&substr2, 0, c, headp, |
| 631 |
ast_str_buffer(substr1), NULL, use_both); |
| 632 |
finalvars = ast_str_buffer(substr2); |
| 633 |
} else { |
| 634 |
finalvars = ast_str_buffer(substr1); |
| 635 |
} |
| 636 |
|
| 637 |
if (ast_str_expr(&substr3, 0, c, finalvars)) { |
| 638 |
ast_debug(2, "Expression result is '%s'\n", ast_str_buffer(substr3)); |
| 639 |
} |
| 640 |
ast_str_append(buf, maxlen, "%s", ast_str_buffer(substr3)); |
| 641 |
} |
| 642 |
} |
| 643 |
if (used) { |
| 644 |
*used = ast_str_strlen(*buf); |
| 645 |
} |
| 646 |
ast_free(substr1); |
| 647 |
ast_free(substr2); |
| 648 |
ast_free(substr3); |
| 649 |
} |
| 650 |
|
| 651 |
void ast_str_substitute_variables_full(struct ast_str **buf, ssize_t maxlen, |
| 652 |
struct ast_channel *chan, struct varshead *headp, const char *templ, size_t *used) |
| 653 |
{ |
| 654 |
ast_str_substitute_variables_full2(buf, maxlen, chan, headp, templ, used, 0); |
| 655 |
} |
| 656 |
|
| 657 |
void ast_str_substitute_variables(struct ast_str **buf, ssize_t maxlen, struct ast_channel *chan, const char *templ) |
| 658 |
{ |
| 659 |
ast_str_substitute_variables_full(buf, maxlen, chan, NULL, templ, NULL); |
| 660 |
} |
| 661 |
|
| 662 |
void ast_str_substitute_variables_varshead(struct ast_str **buf, ssize_t maxlen, struct varshead *headp, const char *templ) |
| 663 |
{ |
| 664 |
ast_str_substitute_variables_full(buf, maxlen, NULL, headp, templ, NULL); |
| 665 |
} |
| 666 |
|
| 667 |
void pbx_substitute_variables_helper_full(struct ast_channel *c, struct varshead *headp, const char *cp1, char *cp2, int count, size_t *used) |
| 668 |
{ |
| 669 |
pbx_substitute_variables_helper_full_location(c, headp, cp1, cp2, count, used, NULL, NULL, 0); |
| 670 |
} |
| 671 |
|
| 672 |
|
| 673 |
AST_THREADSTORAGE(varsub_recurse_level); |
| 674 |
|
| 675 |
#define MAX_VARIABLE_SUB_RECURSE_DEPTH 15 |
| 676 |
|
| 677 |
void pbx_substitute_variables_helper_full_location(struct ast_channel *c, struct varshead *headp, const char *cp1, char *cp2, int count, size_t *used, const char *context, const char *exten, int pri) |
| 678 |
{ |
| 679 |
|
| 680 |
const char *whereweare; |
| 681 |
const char *orig_cp2 = cp2; |
| 682 |
char ltmp[VAR_BUF_SIZE]; |
| 683 |
char var[VAR_BUF_SIZE]; |
| 684 |
int *recurse_depth; |
| 685 |
|
| 686 |
*cp2 = 0; |
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
recurse_depth = ast_threadstorage_get(&varsub_recurse_level, sizeof(*recurse_depth)); |
| 691 |
if (!recurse_depth) { |
| 692 |
return; |
| 693 |
} |
| 694 |
if ((*recurse_depth)++ >= MAX_VARIABLE_SUB_RECURSE_DEPTH) { |
| 695 |
ast_log(LOG_ERROR, "Exceeded maximum variable substitution recursion depth (%d) - possible infinite recursion in dialplan?\n", MAX_VARIABLE_SUB_RECURSE_DEPTH); |
| 696 |
(*recurse_depth)--; |
| 697 |
return; |
| 698 |
} |
| 699 |
|
| 700 |
whereweare = cp1; |
| 701 |
while (!ast_strlen_zero(whereweare) && count) { |
| 702 |
char *nextvar = NULL; |
| 703 |
char *nextexp = NULL; |
| 704 |
char *nextthing; |
| 705 |
char *vars; |
| 706 |
char *vare; |
| 707 |
int length; |
| 708 |
int pos; |
| 709 |
int brackets; |
| 710 |
int needsub; |
| 711 |
int len; |
| 712 |
|
| 713 |
|
| 714 |
nextthing = strchr((char *)whereweare, '$'); |
| 715 |
if (nextthing) { |
| 716 |
pos = nextthing - whereweare; |
| 717 |
switch (nextthing[1]) { |
| 718 |
case '{': |
| 719 |
|
| 720 |
nextvar = nextthing; |
| 721 |
break; |
| 722 |
case '[': |
| 723 |
|
| 724 |
nextexp = nextthing; |
| 725 |
break; |
| 726 |
default: |
| 727 |
|
| 728 |
++pos; |
| 729 |
break; |
| 730 |
} |
| 731 |
} else { |
| 732 |
|
| 733 |
pos = strlen(whereweare); |
| 734 |
} |
| 735 |
|
| 736 |
if (pos) { |
| 737 |
|
| 738 |
if (pos > count) |
| 739 |
pos = count; |
| 740 |
|
| 741 |
|
| 742 |
memcpy(cp2, whereweare, pos); |
| 743 |
|
| 744 |
count -= pos; |
| 745 |
cp2 += pos; |
| 746 |
whereweare += pos; |
| 747 |
*cp2 = 0; |
| 748 |
} |
| 749 |
|
| 750 |
if (nextvar) { |
| 751 |
int offset; |
| 752 |
int offset2; |
| 753 |
int isfunction; |
| 754 |
char *cp4 = NULL; |
| 755 |
char workspace[VAR_BUF_SIZE] = ""; |
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
vars = vare = nextvar + 2; |
| 761 |
brackets = 1; |
| 762 |
needsub = 0; |
| 763 |
|
| 764 |
|
| 765 |
while (brackets && *vare) { |
| 766 |
if ((vare[0] == '$') && (vare[1] == '{')) { |
| 767 |
needsub++; |
| 768 |
brackets++; |
| 769 |
vare++; |
| 770 |
} else if (vare[0] == '{') { |
| 771 |
brackets++; |
| 772 |
} else if (vare[0] == '}') { |
| 773 |
brackets--; |
| 774 |
} else if ((vare[0] == '$') && (vare[1] == '[')) { |
| 775 |
needsub++; |
| 776 |
vare++; |
| 777 |
} |
| 778 |
vare++; |
| 779 |
} |
| 780 |
len = vare - vars; |
| 781 |
if (brackets) { |
| 782 |
ast_log(LOG_WARNING, "Error in extension logic (missing '}')\n"); |
| 783 |
} else { |
| 784 |
|
| 785 |
--len; |
| 786 |
} |
| 787 |
|
| 788 |
|
| 789 |
whereweare = vare; |
| 790 |
|
| 791 |
|
| 792 |
ast_copy_string(var, vars, len + 1); |
| 793 |
|
| 794 |
|
| 795 |
if (needsub) { |
| 796 |
pbx_substitute_variables_helper_full_location(c, headp, var, ltmp, VAR_BUF_SIZE - 1, NULL, context, exten, pri); |
| 797 |
vars = ltmp; |
| 798 |
} else { |
| 799 |
vars = var; |
| 800 |
} |
| 801 |
|
| 802 |
parse_variable_name(vars, &offset, &offset2, &isfunction); |
| 803 |
if (isfunction) { |
| 804 |
|
| 805 |
if (c || !headp) |
| 806 |
cp4 = ast_func_read(c, vars, workspace, VAR_BUF_SIZE) ? NULL : workspace; |
| 807 |
else { |
| 808 |
struct varshead old; |
| 809 |
struct ast_channel *bogus; |
| 810 |
|
| 811 |
bogus = ast_dummy_channel_alloc(); |
| 812 |
if (bogus) { |
| 813 |
old = *ast_channel_varshead(bogus); |
| 814 |
*ast_channel_varshead(bogus) = *headp; |
| 815 |
cp4 = ast_func_read(bogus, vars, workspace, VAR_BUF_SIZE) ? NULL : workspace; |
| 816 |
|
| 817 |
*ast_channel_varshead(bogus) = old; |
| 818 |
ast_channel_unref(bogus); |
| 819 |
} else { |
| 820 |
ast_log(LOG_ERROR, "Unable to allocate bogus channel for function value substitution.\n"); |
| 821 |
cp4 = NULL; |
| 822 |
} |
| 823 |
} |
| 824 |
ast_debug(2, "Function %s result is '%s'\n", vars, cp4 ? cp4 : "(null)"); |
| 825 |
} else { |
| 826 |
|
| 827 |
|
| 828 |
if (exten && !strcmp(vars, "EXTEN")) { |
| 829 |
ast_copy_string(workspace, exten, VAR_BUF_SIZE); |
| 830 |
cp4 = workspace; |
| 831 |
} else if (context && !strcmp(vars, "CONTEXT")) { |
| 832 |
ast_copy_string(workspace, context, VAR_BUF_SIZE); |
| 833 |
cp4 = workspace; |
| 834 |
} else if (pri && !strcmp(vars, "PRIORITY")) { |
| 835 |
snprintf(workspace, VAR_BUF_SIZE, "%d", pri); |
| 836 |
cp4 = workspace; |
| 837 |
} else { |
| 838 |
pbx_retrieve_variable(c, vars, &cp4, workspace, VAR_BUF_SIZE, headp); |
| 839 |
} |
| 840 |
} |
| 841 |
if (cp4) { |
| 842 |
cp4 = substring(cp4, offset, offset2, workspace, VAR_BUF_SIZE); |
| 843 |
|
| 844 |
length = strlen(cp4); |
| 845 |
if (length > count) |
| 846 |
length = count; |
| 847 |
memcpy(cp2, cp4, length); |
| 848 |
count -= length; |
| 849 |
cp2 += length; |
| 850 |
*cp2 = 0; |
| 851 |
} |
| 852 |
} else if (nextexp) { |
| 853 |
|
| 854 |
|
| 855 |
|
| 856 |
vars = vare = nextexp + 2; |
| 857 |
brackets = 1; |
| 858 |
needsub = 0; |
| 859 |
|
| 860 |
|
| 861 |
while (brackets && *vare) { |
| 862 |
if ((vare[0] == '$') && (vare[1] == '[')) { |
| 863 |
needsub++; |
| 864 |
brackets++; |
| 865 |
vare++; |
| 866 |
} else if (vare[0] == '[') { |
| 867 |
brackets++; |
| 868 |
} else if (vare[0] == ']') { |
| 869 |
brackets--; |
| 870 |
} else if ((vare[0] == '$') && (vare[1] == '{')) { |
| 871 |
needsub++; |
| 872 |
vare++; |
| 873 |
} |
| 874 |
vare++; |
| 875 |
} |
| 876 |
len = vare - vars; |
| 877 |
if (brackets) { |
| 878 |
ast_log(LOG_WARNING, "Error in extension logic (missing ']')\n"); |
| 879 |
} else { |
| 880 |
|
| 881 |
--len; |
| 882 |
} |
| 883 |
|
| 884 |
|
| 885 |
whereweare = vare; |
| 886 |
|
| 887 |
|
| 888 |
ast_copy_string(var, vars, len + 1); |
| 889 |
|
| 890 |
|
| 891 |
if (needsub) { |
| 892 |
pbx_substitute_variables_helper_full_location(c, headp, var, ltmp, VAR_BUF_SIZE - 1, NULL, context, exten, pri); |
| 893 |
vars = ltmp; |
| 894 |
} else { |
| 895 |
vars = var; |
| 896 |
} |
| 897 |
|
| 898 |
length = ast_expr(vars, cp2, count, c); |
| 899 |
if (length) { |
| 900 |
ast_debug(1, "Expression result is '%s'\n", cp2); |
| 901 |
count -= length; |
| 902 |
cp2 += length; |
| 903 |
*cp2 = 0; |
| 904 |
} |
| 905 |
} |
| 906 |
} |
| 907 |
if (used) { |
| 908 |
*used = cp2 - orig_cp2; |
| 909 |
} |
| 910 |
(*recurse_depth)--; |
| 911 |
} |
| 912 |
|
| 913 |
void pbx_substitute_variables_helper(struct ast_channel *c, const char *cp1, char *cp2, int count) |
| 914 |
{ |
| 915 |
pbx_substitute_variables_helper_full(c, (c) ? ast_channel_varshead(c) : NULL, cp1, cp2, count, NULL); |
| 916 |
} |
| 917 |
|
| 918 |
void pbx_substitute_variables_varshead(struct varshead *headp, const char *cp1, char *cp2, int count) |
| 919 |
{ |
| 920 |
pbx_substitute_variables_helper_full(NULL, headp, cp1, cp2, count, NULL); |
| 921 |
} |
| 922 |
|
| 923 |
|
| 924 |
static char *handle_show_globals(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 925 |
{ |
| 926 |
int i = 0; |
| 927 |
struct ast_var_t *newvariable; |
| 928 |
|
| 929 |
switch (cmd) { |
| 930 |
case CLI_INIT: |
| 931 |
e->command = "dialplan show globals"; |
| 932 |
e->usage = |
| 933 |
"Usage: dialplan show globals\n" |
| 934 |
" List current global dialplan variables and their values\n"; |
| 935 |
return NULL; |
| 936 |
case CLI_GENERATE: |
| 937 |
return NULL; |
| 938 |
} |
| 939 |
|
| 940 |
ast_rwlock_rdlock(&globalslock); |
| 941 |
AST_LIST_TRAVERSE (&globals, newvariable, entries) { |
| 942 |
i++; |
| 943 |
ast_cli(a->fd, " %s=%s\n", ast_var_name(newvariable), ast_var_value(newvariable)); |
| 944 |
} |
| 945 |
ast_rwlock_unlock(&globalslock); |
| 946 |
ast_cli(a->fd, "\n -- %d variable(s)\n", i); |
| 947 |
|
| 948 |
return CLI_SUCCESS; |
| 949 |
} |
| 950 |
|
| 951 |
|
| 952 |
static char *handle_show_chanvar(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 953 |
{ |
| 954 |
struct ast_channel *chan; |
| 955 |
struct ast_var_t *var; |
| 956 |
|
| 957 |
switch (cmd) { |
| 958 |
case CLI_INIT: |
| 959 |
e->command = "dialplan show chanvar"; |
| 960 |
e->usage = |
| 961 |
"Usage: dialplan show chanvar <channel>\n" |
| 962 |
" List current channel variables and their values\n"; |
| 963 |
return NULL; |
| 964 |
case CLI_GENERATE: |
| 965 |
return ast_complete_channels(a->line, a->word, a->pos, a->n, 3); |
| 966 |
} |
| 967 |
|
| 968 |
if (a->argc != e->args + 1) { |
| 969 |
return CLI_SHOWUSAGE; |
| 970 |
} |
| 971 |
|
| 972 |
chan = ast_channel_get_by_name(a->argv[e->args]); |
| 973 |
if (!chan) { |
| 974 |
ast_cli(a->fd, "Channel '%s' not found\n", a->argv[e->args]); |
| 975 |
return CLI_FAILURE; |
| 976 |
} |
| 977 |
|
| 978 |
ast_channel_lock(chan); |
| 979 |
AST_LIST_TRAVERSE(ast_channel_varshead(chan), var, entries) { |
| 980 |
ast_cli(a->fd, "%s=%s\n", ast_var_name(var), ast_var_value(var)); |
| 981 |
} |
| 982 |
ast_channel_unlock(chan); |
| 983 |
|
| 984 |
ast_channel_unref(chan); |
| 985 |
return CLI_SUCCESS; |
| 986 |
} |
| 987 |
|
| 988 |
static const struct ast_channel_tech mock_channel_tech = { |
| 989 |
}; |
| 990 |
|
| 991 |
static int cli_chan = 0; |
| 992 |
|
| 993 |
|
| 994 |
static char *handle_eval_function(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 995 |
{ |
| 996 |
struct ast_channel *c = NULL; |
| 997 |
const char *fn, *substituted; |
| 998 |
int ret; |
| 999 |
char workspace[1024]; |
| 1000 |
RAII_VAR(struct ast_format_cap *, caps, NULL, ao2_cleanup); |
| 1001 |
|
| 1002 |
switch (cmd) { |
| 1003 |
case CLI_INIT: |
| 1004 |
e->command = "dialplan eval function"; |
| 1005 |
e->usage = |
| 1006 |
"Usage: dialplan eval function <name(args)>\n" |
| 1007 |
" Evaluate a dialplan function call\n" |
| 1008 |
" A dummy channel is used to evaluate\n" |
| 1009 |
" the function call, so only global\n" |
| 1010 |
" variables should be used.\n"; |
| 1011 |
return NULL; |
| 1012 |
case CLI_GENERATE: |
| 1013 |
return NULL; |
| 1014 |
} |
| 1015 |
|
| 1016 |
if (a->argc != e->args + 1) { |
| 1017 |
return CLI_SHOWUSAGE; |
| 1018 |
} |
| 1019 |
|
| 1020 |
if (a->argc != e->args + 1 && a->argc != e->args + 2) { |
| 1021 |
return CLI_SHOWUSAGE; |
| 1022 |
} |
| 1023 |
|
| 1024 |
caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT); |
| 1025 |
if (!caps) { |
| 1026 |
ast_log(LOG_WARNING, "Could not allocate an empty format capabilities structure\n"); |
| 1027 |
return CLI_FAILURE; |
| 1028 |
} |
| 1029 |
|
| 1030 |
if (ast_format_cap_append(caps, ast_format_ulaw, 0)) { |
| 1031 |
ast_log(LOG_WARNING, "Failed to append a ulaw format to capabilities for channel nativeformats\n"); |
| 1032 |
return CLI_FAILURE; |
| 1033 |
} |
| 1034 |
|
| 1035 |
if (ast_format_cap_append(caps, ast_format_alaw, 0)) { |
| 1036 |
ast_log(LOG_WARNING, "Failed to append an alaw format to capabilities for channel nativeformats\n"); |
| 1037 |
return CLI_FAILURE; |
| 1038 |
} |
| 1039 |
|
| 1040 |
if (ast_format_cap_append(caps, ast_format_h264, 0)) { |
| 1041 |
ast_log(LOG_WARNING, "Failed to append an h264 format to capabilities for channel nativeformats\n"); |
| 1042 |
return CLI_FAILURE; |
| 1043 |
} |
| 1044 |
|
| 1045 |
c = ast_channel_alloc(0, AST_STATE_DOWN, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, "CLIEval/%d", ++cli_chan); |
| 1046 |
if (!c) { |
| 1047 |
ast_cli(a->fd, "Unable to allocate mock channel for application execution.\n"); |
| 1048 |
return CLI_FAILURE; |
| 1049 |
} |
| 1050 |
ast_channel_tech_set(c, &mock_channel_tech); |
| 1051 |
ast_channel_nativeformats_set(c, caps); |
| 1052 |
ast_channel_set_writeformat(c, ast_format_slin); |
| 1053 |
ast_channel_set_rawwriteformat(c, ast_format_slin); |
| 1054 |
ast_channel_set_readformat(c, ast_format_slin); |
| 1055 |
ast_channel_set_rawreadformat(c, ast_format_slin); |
| 1056 |
ast_channel_unlock(c); |
| 1057 |
|
| 1058 |
fn = a->argv[3]; |
| 1059 |
pbx_substitute_variables_helper(c, fn, workspace, sizeof(workspace)); |
| 1060 |
substituted = ast_strdupa(workspace); |
| 1061 |
workspace[0] = '\0'; |
| 1062 |
ret = ast_func_read(c, substituted, workspace, sizeof(workspace)); |
| 1063 |
|
| 1064 |
ast_hangup(c); |
| 1065 |
|
| 1066 |
ast_cli(a->fd, "Return Value: %s (%d)\n", ret ? "Failure" : "Success", ret); |
| 1067 |
ast_cli(a->fd, "Result: %s\n", workspace); |
| 1068 |
|
| 1069 |
return CLI_SUCCESS; |
| 1070 |
} |
| 1071 |
|
| 1072 |
static char *handle_set_global(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 1073 |
{ |
| 1074 |
switch (cmd) { |
| 1075 |
case CLI_INIT: |
| 1076 |
e->command = "dialplan set global"; |
| 1077 |
e->usage = |
| 1078 |
"Usage: dialplan set global <name> <value>\n" |
| 1079 |
" Set global dialplan variable <name> to <value>\n"; |
| 1080 |
return NULL; |
| 1081 |
case CLI_GENERATE: |
| 1082 |
return NULL; |
| 1083 |
} |
| 1084 |
|
| 1085 |
if (a->argc != e->args + 2) |
| 1086 |
return CLI_SHOWUSAGE; |
| 1087 |
|
| 1088 |
pbx_builtin_setvar_helper(NULL, a->argv[3], a->argv[4]); |
| 1089 |
ast_cli(a->fd, "\n -- Global variable '%s' set to '%s'\n", a->argv[3], a->argv[4]); |
| 1090 |
|
| 1091 |
return CLI_SUCCESS; |
| 1092 |
} |
| 1093 |
|
| 1094 |
static char *handle_set_chanvar(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 1095 |
{ |
| 1096 |
struct ast_channel *chan; |
| 1097 |
const char *chan_name, *var_name, *var_value; |
| 1098 |
|
| 1099 |
switch (cmd) { |
| 1100 |
case CLI_INIT: |
| 1101 |
e->command = "dialplan set chanvar"; |
| 1102 |
e->usage = |
| 1103 |
"Usage: dialplan set chanvar <channel> <varname> <value>\n" |
| 1104 |
" Set channel variable <varname> to <value>\n"; |
| 1105 |
return NULL; |
| 1106 |
case CLI_GENERATE: |
| 1107 |
return ast_complete_channels(a->line, a->word, a->pos, a->n, 3); |
| 1108 |
} |
| 1109 |
|
| 1110 |
if (a->argc != e->args + 3) |
| 1111 |
return CLI_SHOWUSAGE; |
| 1112 |
|
| 1113 |
chan_name = a->argv[e->args]; |
| 1114 |
var_name = a->argv[e->args + 1]; |
| 1115 |
var_value = a->argv[e->args + 2]; |
| 1116 |
|
| 1117 |
if (!(chan = ast_channel_get_by_name(chan_name))) { |
| 1118 |
ast_cli(a->fd, "Channel '%s' not found\n", chan_name); |
| 1119 |
return CLI_FAILURE; |
| 1120 |
} |
| 1121 |
|
| 1122 |
pbx_builtin_setvar_helper(chan, var_name, var_value); |
| 1123 |
|
| 1124 |
chan = ast_channel_unref(chan); |
| 1125 |
|
| 1126 |
ast_cli(a->fd, "\n -- Channel variable '%s' set to '%s' for '%s'\n", var_name, var_value, chan_name); |
| 1127 |
|
| 1128 |
return CLI_SUCCESS; |
| 1129 |
} |
| 1130 |
|
| 1131 |
int pbx_builtin_serialize_variables(struct ast_channel *chan, struct ast_str **buf) |
| 1132 |
{ |
| 1133 |
struct ast_var_t *variables; |
| 1134 |
const char *var, *val; |
| 1135 |
int total = 0; |
| 1136 |
|
| 1137 |
if (!chan) |
| 1138 |
return 0; |
| 1139 |
|
| 1140 |
ast_str_reset(*buf); |
| 1141 |
|
| 1142 |
ast_channel_lock(chan); |
| 1143 |
|
| 1144 |
AST_LIST_TRAVERSE(ast_channel_varshead(chan), variables, entries) { |
| 1145 |
if ((var = ast_var_name(variables)) && (val = ast_var_value(variables)) |
| 1146 |
|
| 1147 |
) { |
| 1148 |
if (ast_str_append(buf, 0, "%s=%s\n", var, val) < 0) { |
| 1149 |
ast_log(LOG_ERROR, "Data Buffer Size Exceeded!\n"); |
| 1150 |
break; |
| 1151 |
} else |
| 1152 |
total++; |
| 1153 |
} else |
| 1154 |
break; |
| 1155 |
} |
| 1156 |
|
| 1157 |
ast_channel_unlock(chan); |
| 1158 |
|
| 1159 |
return total; |
| 1160 |
} |
| 1161 |
|
| 1162 |
const char *pbx_builtin_getvar_helper(struct ast_channel *chan, const char *name) |
| 1163 |
{ |
| 1164 |
struct ast_var_t *variables; |
| 1165 |
const char *ret = NULL; |
| 1166 |
int i; |
| 1167 |
struct varshead *places[2] = { NULL, &globals }; |
| 1168 |
|
| 1169 |
if (!name) |
| 1170 |
return NULL; |
| 1171 |
|
| 1172 |
if (chan) { |
| 1173 |
ast_channel_lock(chan); |
| 1174 |
places[0] = ast_channel_varshead(chan); |
| 1175 |
} |
| 1176 |
|
| 1177 |
for (i = 0; i < 2; i++) { |
| 1178 |
if (!places[i]) |
| 1179 |
continue; |
| 1180 |
if (places[i] == &globals) |
| 1181 |
ast_rwlock_rdlock(&globalslock); |
| 1182 |
AST_LIST_TRAVERSE(places[i], variables, entries) { |
| 1183 |
if (!strcmp(name, ast_var_name(variables))) { |
| 1184 |
ret = ast_var_value(variables); |
| 1185 |
break; |
| 1186 |
} |
| 1187 |
} |
| 1188 |
if (places[i] == &globals) |
| 1189 |
ast_rwlock_unlock(&globalslock); |
| 1190 |
if (ret) |
| 1191 |
break; |
| 1192 |
} |
| 1193 |
|
| 1194 |
if (chan) |
| 1195 |
ast_channel_unlock(chan); |
| 1196 |
|
| 1197 |
return ret; |
| 1198 |
} |
| 1199 |
|
| 1200 |
void pbx_builtin_pushvar_helper(struct ast_channel *chan, const char *name, const char *value) |
| 1201 |
{ |
| 1202 |
struct ast_var_t *newvariable; |
| 1203 |
struct varshead *headp; |
| 1204 |
|
| 1205 |
if (name[strlen(name)-1] == ')') { |
| 1206 |
char *function = ast_strdupa(name); |
| 1207 |
|
| 1208 |
ast_log(LOG_WARNING, "Cannot push a value onto a function\n"); |
| 1209 |
ast_func_write(chan, function, value); |
| 1210 |
return; |
| 1211 |
} |
| 1212 |
|
| 1213 |
if (chan) { |
| 1214 |
ast_channel_lock(chan); |
| 1215 |
headp = ast_channel_varshead(chan); |
| 1216 |
} else { |
| 1217 |
ast_rwlock_wrlock(&globalslock); |
| 1218 |
headp = &globals; |
| 1219 |
} |
| 1220 |
|
| 1221 |
if (value && (newvariable = ast_var_assign(name, value))) { |
| 1222 |
if (headp == &globals) |
| 1223 |
ast_verb(2, "Setting global variable '%s' to '%s'\n", name, value); |
| 1224 |
AST_LIST_INSERT_HEAD(headp, newvariable, entries); |
| 1225 |
} |
| 1226 |
|
| 1227 |
if (chan) |
| 1228 |
ast_channel_unlock(chan); |
| 1229 |
else |
| 1230 |
ast_rwlock_unlock(&globalslock); |
| 1231 |
} |
| 1232 |
|
| 1233 |
int pbx_builtin_setvar_helper(struct ast_channel *chan, const char *name, const char *value) |
| 1234 |
{ |
| 1235 |
struct ast_var_t *newvariable; |
| 1236 |
struct varshead *headp; |
| 1237 |
const char *nametail = name; |
| 1238 |
|
| 1239 |
int old_value_existed = 0; |
| 1240 |
|
| 1241 |
if (name[strlen(name) - 1] == ')') { |
| 1242 |
char *function = ast_strdupa(name); |
| 1243 |
|
| 1244 |
return ast_func_write(chan, function, value); |
| 1245 |
} |
| 1246 |
|
| 1247 |
if (chan) { |
| 1248 |
ast_channel_lock(chan); |
| 1249 |
headp = ast_channel_varshead(chan); |
| 1250 |
} else { |
| 1251 |
ast_rwlock_wrlock(&globalslock); |
| 1252 |
headp = &globals; |
| 1253 |
} |
| 1254 |
|
| 1255 |
|
| 1256 |
if (*nametail == '_') { |
| 1257 |
nametail++; |
| 1258 |
if (*nametail == '_') |
| 1259 |
nametail++; |
| 1260 |
} |
| 1261 |
|
| 1262 |
AST_LIST_TRAVERSE_SAFE_BEGIN(headp, newvariable, entries) { |
| 1263 |
if (strcmp(ast_var_name(newvariable), nametail) == 0) { |
| 1264 |
|
| 1265 |
AST_LIST_REMOVE_CURRENT(entries); |
| 1266 |
old_value_existed = !ast_strlen_zero(ast_var_value(newvariable)); |
| 1267 |
ast_var_delete(newvariable); |
| 1268 |
break; |
| 1269 |
} |
| 1270 |
} |
| 1271 |
AST_LIST_TRAVERSE_SAFE_END; |
| 1272 |
|
| 1273 |
if (value && (newvariable = ast_var_assign(name, value))) { |
| 1274 |
if (headp == &globals) { |
| 1275 |
ast_verb(2, "Setting global variable '%s' to '%s'\n", name, value); |
| 1276 |
} |
| 1277 |
AST_LIST_INSERT_HEAD(headp, newvariable, entries); |
| 1278 |
ast_channel_publish_varset(chan, name, value); |
| 1279 |
} else if (old_value_existed) { |
| 1280 |
|
| 1281 |
ast_channel_publish_varset(chan, name, ""); |
| 1282 |
} |
| 1283 |
|
| 1284 |
if (chan) |
| 1285 |
ast_channel_unlock(chan); |
| 1286 |
else |
| 1287 |
ast_rwlock_unlock(&globalslock); |
| 1288 |
return 0; |
| 1289 |
} |
| 1290 |
|
| 1291 |
int pbx_builtin_setvar(struct ast_channel *chan, const char *data) |
| 1292 |
{ |
| 1293 |
char *name, *value, *mydata; |
| 1294 |
|
| 1295 |
if (ast_strlen_zero(data)) { |
| 1296 |
ast_log(LOG_WARNING, "Set requires one variable name/value pair.\n"); |
| 1297 |
return 0; |
| 1298 |
} |
| 1299 |
|
| 1300 |
mydata = ast_strdupa(data); |
| 1301 |
name = strsep(&mydata, "="); |
| 1302 |
value = mydata; |
| 1303 |
if (!value) { |
| 1304 |
ast_log(LOG_WARNING, "Set requires an '=' to be a valid assignment.\n"); |
| 1305 |
return 0; |
| 1306 |
} |
| 1307 |
|
| 1308 |
if (strchr(name, ' ')) { |
| 1309 |
ast_log(LOG_WARNING, "Please avoid unnecessary spaces on variables as it may lead to unexpected results ('%s' set to '%s').\n", name, mydata); |
| 1310 |
} |
| 1311 |
|
| 1312 |
pbx_builtin_setvar_helper(chan, name, value); |
| 1313 |
|
| 1314 |
return 0; |
| 1315 |
} |
| 1316 |
|
| 1317 |
int pbx_builtin_setvar_multiple(struct ast_channel *chan, const char *vdata) |
| 1318 |
{ |
| 1319 |
char *data; |
| 1320 |
int x; |
| 1321 |
AST_DECLARE_APP_ARGS(args, |
| 1322 |
AST_APP_ARG(pair)[99]; |
| 1323 |
); |
| 1324 |
AST_DECLARE_APP_ARGS(pair, |
| 1325 |
AST_APP_ARG(name); |
| 1326 |
AST_APP_ARG(value); |
| 1327 |
); |
| 1328 |
|
| 1329 |
if (ast_strlen_zero(vdata)) { |
| 1330 |
ast_log(LOG_WARNING, "MSet requires at least one variable name/value pair.\n"); |
| 1331 |
return 0; |
| 1332 |
} |
| 1333 |
|
| 1334 |
data = ast_strdupa(vdata); |
| 1335 |
AST_STANDARD_APP_ARGS(args, data); |
| 1336 |
|
| 1337 |
for (x = 0; x < args.argc; x++) { |
| 1338 |
AST_NONSTANDARD_APP_ARGS(pair, args.pair[x], '='); |
| 1339 |
if (pair.argc == 2) { |
| 1340 |
pbx_builtin_setvar_helper(chan, pair.name, pair.value); |
| 1341 |
if (strchr(pair.name, ' ')) |
| 1342 |
ast_log(LOG_WARNING, "Please avoid unnecessary spaces on variables as it may lead to unexpected results ('%s' set to '%s').\n", pair.name, pair.value); |
| 1343 |
} else if (!chan) { |
| 1344 |
ast_log(LOG_WARNING, "MSet: ignoring entry '%s' with no '='\n", pair.name); |
| 1345 |
} else { |
| 1346 |
ast_log(LOG_WARNING, "MSet: ignoring entry '%s' with no '=' (in %s@%s:%d\n", pair.name, ast_channel_exten(chan), ast_channel_context(chan), ast_channel_priority(chan)); |
| 1347 |
} |
| 1348 |
} |
| 1349 |
|
| 1350 |
return 0; |
| 1351 |
} |
| 1352 |
|
| 1353 |
void pbx_builtin_clear_globals(void) |
| 1354 |
{ |
| 1355 |
struct ast_var_t *vardata; |
| 1356 |
|
| 1357 |
ast_rwlock_wrlock(&globalslock); |
| 1358 |
while ((vardata = AST_LIST_REMOVE_HEAD(&globals, entries))) |
| 1359 |
ast_var_delete(vardata); |
| 1360 |
ast_rwlock_unlock(&globalslock); |
| 1361 |
} |
| 1362 |
|
| 1363 |
#ifdef TEST_FRAMEWORK |
| 1364 |
AST_TEST_DEFINE(test_variable_substrings) |
| 1365 |
{ |
| 1366 |
int i, res = AST_TEST_PASS; |
| 1367 |
struct ast_channel *chan; |
| 1368 |
struct ast_str *str; |
| 1369 |
|
| 1370 |
const char *test_strings[][5] = { |
| 1371 |
{"somevaluehere", "CALLERID(num):0:25", "somevaluehere"}, |
| 1372 |
{"somevaluehere", "CALLERID(num):0:5", "somev"}, |
| 1373 |
{"somevaluehere", "CALLERID(num):4:5", "value"}, |
| 1374 |
{"somevaluehere", "CALLERID(num):0:-4", "somevalue"}, |
| 1375 |
{"somevaluehere", "CALLERID(num):-4", "here"}, |
| 1376 |
}; |
| 1377 |
|
| 1378 |
switch (cmd) { |
| 1379 |
case TEST_INIT: |
| 1380 |
info->name = "variable_substrings"; |
| 1381 |
info->category = "/main/pbx/"; |
| 1382 |
info->summary = "Test variable substring resolution"; |
| 1383 |
info->description = "Verify that variable substrings are calculated correctly"; |
| 1384 |
return AST_TEST_NOT_RUN; |
| 1385 |
case TEST_EXECUTE: |
| 1386 |
break; |
| 1387 |
} |
| 1388 |
|
| 1389 |
if (!(chan = ast_dummy_channel_alloc())) { |
| 1390 |
ast_test_status_update(test, "Unable to allocate dummy channel\n"); |
| 1391 |
return AST_TEST_FAIL; |
| 1392 |
} |
| 1393 |
|
| 1394 |
if (!(str = ast_str_create(64))) { |
| 1395 |
ast_test_status_update(test, "Unable to allocate dynamic string buffer\n"); |
| 1396 |
ast_channel_release(chan); |
| 1397 |
return AST_TEST_FAIL; |
| 1398 |
} |
| 1399 |
|
| 1400 |
for (i = 0; i < ARRAY_LEN(test_strings); i++) { |
| 1401 |
char substituted[512], tmp[512] = ""; |
| 1402 |
|
| 1403 |
ast_set_callerid(chan, test_strings[i][0], NULL, test_strings[i][0]); |
| 1404 |
|
| 1405 |
snprintf(tmp, sizeof(tmp), "${%s}", test_strings[i][1]); |
| 1406 |
|
| 1407 |
|
| 1408 |
ast_str_substitute_variables(&str, 0, chan, tmp); |
| 1409 |
ast_debug(1, "Comparing STR %s with %s\n", ast_str_buffer(str), test_strings[i][2]); |
| 1410 |
if (strcmp(test_strings[i][2], ast_str_buffer(str))) { |
| 1411 |
ast_test_status_update(test, "Format string '%s' substituted to '%s' using str sub. Expected '%s'.\n", test_strings[i][0], ast_str_buffer(str), test_strings[i][2]); |
| 1412 |
res = AST_TEST_FAIL; |
| 1413 |
} |
| 1414 |
|
| 1415 |
|
| 1416 |
pbx_substitute_variables_helper(chan, tmp, substituted, sizeof(substituted) - 1); |
| 1417 |
ast_debug(1, "Comparing PBX %s with %s\n", substituted, test_strings[i][2]); |
| 1418 |
if (strcmp(test_strings[i][2], substituted)) { |
| 1419 |
ast_test_status_update(test, "Format string '%s' substituted to '%s' using pbx sub. Expected '%s'.\n", test_strings[i][0], substituted, test_strings[i][2]); |
| 1420 |
res = AST_TEST_FAIL; |
| 1421 |
} |
| 1422 |
} |
| 1423 |
ast_free(str); |
| 1424 |
|
| 1425 |
ast_channel_release(chan); |
| 1426 |
|
| 1427 |
return res; |
| 1428 |
} |
| 1429 |
#endif |
| 1430 |
|
| 1431 |
static struct ast_cli_entry vars_cli[] = { |
| 1432 |
AST_CLI_DEFINE(handle_show_globals, "Show global dialplan variables"), |
| 1433 |
AST_CLI_DEFINE(handle_show_chanvar, "Show channel variables"), |
| 1434 |
AST_CLI_DEFINE(handle_eval_function, "Evaluate dialplan function"), |
| 1435 |
AST_CLI_DEFINE(handle_set_global, "Set global dialplan variable"), |
| 1436 |
AST_CLI_DEFINE(handle_set_chanvar, "Set a channel variable"), |
| 1437 |
}; |
| 1438 |
|
| 1439 |
static void unload_pbx_variables(void) |
| 1440 |
{ |
| 1441 |
ast_cli_unregister_multiple(vars_cli, ARRAY_LEN(vars_cli)); |
| 1442 |
ast_unregister_application("Set"); |
| 1443 |
ast_unregister_application("MSet"); |
| 1444 |
pbx_builtin_clear_globals(); |
| 1445 |
AST_TEST_UNREGISTER(test_variable_substrings); |
| 1446 |
} |
| 1447 |
|
| 1448 |
int load_pbx_variables(void) |
| 1449 |
{ |
| 1450 |
int res = 0; |
| 1451 |
|
| 1452 |
res |= ast_cli_register_multiple(vars_cli, ARRAY_LEN(vars_cli)); |
| 1453 |
res |= ast_register_application2("Set", pbx_builtin_setvar, NULL, NULL, NULL); |
| 1454 |
res |= ast_register_application2("MSet", pbx_builtin_setvar_multiple, NULL, NULL, NULL); |
| 1455 |
ast_register_cleanup(unload_pbx_variables); |
| 1456 |
AST_TEST_REGISTER(test_variable_substrings); |
| 1457 |
|
| 1458 |
return res; |
| 1459 |
} |