| 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 |
#include "asterisk.h" |
| 32 |
|
| 33 |
#include "asterisk/file.h" |
| 34 |
#include "asterisk/channel.h" |
| 35 |
#include "asterisk/pbx.h" |
| 36 |
#include "asterisk/config.h" |
| 37 |
#include "asterisk/module.h" |
| 38 |
#include "asterisk/lock.h" |
| 39 |
#include "asterisk/utils.h" |
| 40 |
#include "asterisk/app.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 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
AST_THREADSTORAGE(buf1); |
| 194 |
AST_THREADSTORAGE(buf2); |
| 195 |
AST_THREADSTORAGE(buf3); |
| 196 |
|
| 197 |
static int function_realtime_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len) |
| 198 |
{ |
| 199 |
struct ast_variable *var, *head; |
| 200 |
struct ast_str *out; |
| 201 |
size_t resultslen; |
| 202 |
int n; |
| 203 |
AST_DECLARE_APP_ARGS(args, |
| 204 |
AST_APP_ARG(family); |
| 205 |
AST_APP_ARG(fieldmatch); |
| 206 |
AST_APP_ARG(value); |
| 207 |
AST_APP_ARG(delim1); |
| 208 |
AST_APP_ARG(delim2); |
| 209 |
); |
| 210 |
|
| 211 |
if (ast_strlen_zero(data)) { |
| 212 |
ast_log(LOG_WARNING, "Syntax: REALTIME(family,fieldmatch[,matchvalue[,delim1[,delim2]]]) - missing argument!\n"); |
| 213 |
return -1; |
| 214 |
} |
| 215 |
|
| 216 |
AST_STANDARD_APP_ARGS(args, data); |
| 217 |
|
| 218 |
if (!args.delim1) |
| 219 |
args.delim1 = ","; |
| 220 |
if (!args.delim2) |
| 221 |
args.delim2 = "="; |
| 222 |
|
| 223 |
if (chan) |
| 224 |
ast_autoservice_start(chan); |
| 225 |
|
| 226 |
head = ast_load_realtime_all(args.family, args.fieldmatch, args.value, SENTINEL); |
| 227 |
|
| 228 |
if (!head) { |
| 229 |
if (chan) |
| 230 |
ast_autoservice_stop(chan); |
| 231 |
return -1; |
| 232 |
} |
| 233 |
|
| 234 |
resultslen = 0; |
| 235 |
n = 0; |
| 236 |
for (var = head; var; n++, var = var->next) |
| 237 |
resultslen += strlen(var->name) + strlen(var->value); |
| 238 |
|
| 239 |
resultslen += n * (strlen(args.delim1) + strlen(args.delim2)) + 1; |
| 240 |
|
| 241 |
if (resultslen > len) { |
| 242 |
ast_log(LOG_WARNING, "Failed to fetch. Realtime data is too large: need %zu, have %zu.\n", resultslen, len); |
| 243 |
if (chan) { |
| 244 |
ast_autoservice_stop(chan); |
| 245 |
} |
| 246 |
return -1; |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
out = ast_str_alloca(resultslen); |
| 252 |
for (var = head; var; var = var->next) |
| 253 |
ast_str_append(&out, 0, "%s%s%s%s", var->name, args.delim2, var->value, args.delim1); |
| 254 |
ast_copy_string(buf, ast_str_buffer(out), len); |
| 255 |
|
| 256 |
ast_variables_destroy(head); |
| 257 |
|
| 258 |
if (chan) |
| 259 |
ast_autoservice_stop(chan); |
| 260 |
|
| 261 |
return 0; |
| 262 |
} |
| 263 |
|
| 264 |
static int function_realtime_write(struct ast_channel *chan, const char *cmd, char *data, const char *value) |
| 265 |
{ |
| 266 |
int res = 0; |
| 267 |
AST_DECLARE_APP_ARGS(args, |
| 268 |
AST_APP_ARG(family); |
| 269 |
AST_APP_ARG(fieldmatch); |
| 270 |
AST_APP_ARG(value); |
| 271 |
AST_APP_ARG(field); |
| 272 |
); |
| 273 |
|
| 274 |
if (ast_strlen_zero(data)) { |
| 275 |
ast_log(LOG_WARNING, "Syntax: %s(family,fieldmatch,matchvalue,updatecol) - missing argument!\n", cmd); |
| 276 |
return -1; |
| 277 |
} |
| 278 |
|
| 279 |
AST_STANDARD_APP_ARGS(args, data); |
| 280 |
|
| 281 |
if (ast_strlen_zero(args.fieldmatch) || ast_strlen_zero(args.field)) { |
| 282 |
ast_log(LOG_WARNING, "Syntax: %s(family,fieldmatch,matchvalue,updatecol) - missing argument!\n", cmd); |
| 283 |
return -1; |
| 284 |
} |
| 285 |
|
| 286 |
if (chan) { |
| 287 |
ast_autoservice_start(chan); |
| 288 |
} |
| 289 |
|
| 290 |
res = ast_update_realtime(args.family, args.fieldmatch, args.value, args.field, (char *)value, SENTINEL); |
| 291 |
|
| 292 |
if (res < 0) { |
| 293 |
ast_log(LOG_WARNING, "Failed to update. Check the debug log for possible data repository related entries.\n"); |
| 294 |
} |
| 295 |
|
| 296 |
if (chan) { |
| 297 |
ast_autoservice_stop(chan); |
| 298 |
} |
| 299 |
|
| 300 |
return res; |
| 301 |
} |
| 302 |
|
| 303 |
static int realtimefield_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len) |
| 304 |
{ |
| 305 |
struct ast_variable *var, *head; |
| 306 |
struct ast_str *escapebuf = ast_str_thread_get(&buf1, 16); |
| 307 |
struct ast_str *fields = ast_str_thread_get(&buf2, 16); |
| 308 |
struct ast_str *values = ast_str_thread_get(&buf3, 16); |
| 309 |
int first = 0; |
| 310 |
enum { rtfield, rthash } which; |
| 311 |
AST_DECLARE_APP_ARGS(args, |
| 312 |
AST_APP_ARG(family); |
| 313 |
AST_APP_ARG(fieldmatch); |
| 314 |
AST_APP_ARG(value); |
| 315 |
AST_APP_ARG(fieldname); |
| 316 |
); |
| 317 |
|
| 318 |
if (!strcmp(cmd, "REALTIME_FIELD")) { |
| 319 |
which = rtfield; |
| 320 |
} else { |
| 321 |
which = rthash; |
| 322 |
} |
| 323 |
|
| 324 |
if (ast_strlen_zero(data)) { |
| 325 |
ast_log(LOG_WARNING, "Syntax: %s(family,fieldmatch,matchvalue%s) - missing argument!\n", cmd, which == rtfield ? ",fieldname" : ""); |
| 326 |
return -1; |
| 327 |
} |
| 328 |
|
| 329 |
AST_STANDARD_APP_ARGS(args, data); |
| 330 |
|
| 331 |
if ((which == rtfield && args.argc != 4) || (which == rthash && args.argc != 3)) { |
| 332 |
ast_log(LOG_WARNING, "Syntax: %s(family,fieldmatch,matchvalue%s) - missing argument!\n", cmd, which == rtfield ? ",fieldname" : ""); |
| 333 |
return -1; |
| 334 |
} |
| 335 |
|
| 336 |
if (chan) { |
| 337 |
ast_autoservice_start(chan); |
| 338 |
} |
| 339 |
|
| 340 |
if (!(head = ast_load_realtime_all(args.family, args.fieldmatch, args.value, SENTINEL))) { |
| 341 |
if (chan) { |
| 342 |
ast_autoservice_stop(chan); |
| 343 |
} |
| 344 |
return -1; |
| 345 |
} |
| 346 |
|
| 347 |
ast_str_reset(fields); |
| 348 |
ast_str_reset(values); |
| 349 |
|
| 350 |
for (var = head; var; var = var->next) { |
| 351 |
if (which == rtfield) { |
| 352 |
ast_debug(1, "Comparing %s to %s\n", var->name, args.fieldname); |
| 353 |
if (!strcasecmp(var->name, args.fieldname)) { |
| 354 |
ast_debug(1, "Match! Value is %s\n", var->value); |
| 355 |
ast_copy_string(buf, var->value, len); |
| 356 |
break; |
| 357 |
} |
| 358 |
} else if (which == rthash) { |
| 359 |
ast_debug(1, "Setting hash key %s to value %s\n", var->name, var->value); |
| 360 |
ast_str_append(&fields, 0, "%s%s", first ? "" : ",", ast_str_set_escapecommas(&escapebuf, 0, var->name, INT_MAX)); |
| 361 |
ast_str_append(&values, 0, "%s%s", first ? "" : ",", ast_str_set_escapecommas(&escapebuf, 0, var->value, INT_MAX)); |
| 362 |
first = 0; |
| 363 |
} |
| 364 |
} |
| 365 |
ast_variables_destroy(head); |
| 366 |
|
| 367 |
if (which == rthash) { |
| 368 |
pbx_builtin_setvar_helper(chan, "~ODBCFIELDS~", ast_str_buffer(fields)); |
| 369 |
ast_copy_string(buf, ast_str_buffer(values), len); |
| 370 |
} |
| 371 |
|
| 372 |
if (chan) { |
| 373 |
ast_autoservice_stop(chan); |
| 374 |
} |
| 375 |
|
| 376 |
return 0; |
| 377 |
} |
| 378 |
|
| 379 |
static int function_realtime_store(struct ast_channel *chan, const char *cmd, char *data, const char *value) |
| 380 |
{ |
| 381 |
int res = 0; |
| 382 |
char storeid[32]; |
| 383 |
char *valcopy; |
| 384 |
AST_DECLARE_APP_ARGS(a, |
| 385 |
AST_APP_ARG(family); |
| 386 |
AST_APP_ARG(f)[30]; |
| 387 |
); |
| 388 |
|
| 389 |
AST_DECLARE_APP_ARGS(v, |
| 390 |
AST_APP_ARG(v)[30]; |
| 391 |
); |
| 392 |
|
| 393 |
if (ast_strlen_zero(data)) { |
| 394 |
ast_log(LOG_WARNING, "Syntax: REALTIME_STORE(family,field1,field2,...,field30) - missing argument!\n"); |
| 395 |
return -1; |
| 396 |
} |
| 397 |
|
| 398 |
if (chan) |
| 399 |
ast_autoservice_start(chan); |
| 400 |
|
| 401 |
valcopy = ast_strdupa(value); |
| 402 |
AST_STANDARD_APP_ARGS(a, data); |
| 403 |
AST_STANDARD_APP_ARGS(v, valcopy); |
| 404 |
|
| 405 |
res = ast_store_realtime(a.family, |
| 406 |
a.f[0], v.v[0], a.f[1], v.v[1], a.f[2], v.v[2], a.f[3], v.v[3], a.f[4], v.v[4], |
| 407 |
a.f[5], v.v[5], a.f[6], v.v[6], a.f[7], v.v[7], a.f[8], v.v[8], a.f[9], v.v[9], |
| 408 |
a.f[10], v.v[10], a.f[11], v.v[11], a.f[12], v.v[12], a.f[13], v.v[13], a.f[14], v.v[14], |
| 409 |
a.f[15], v.v[15], a.f[16], v.v[16], a.f[17], v.v[17], a.f[18], v.v[18], a.f[19], v.v[19], |
| 410 |
a.f[20], v.v[20], a.f[21], v.v[21], a.f[22], v.v[22], a.f[23], v.v[23], a.f[24], v.v[24], |
| 411 |
a.f[25], v.v[25], a.f[26], v.v[26], a.f[27], v.v[27], a.f[28], v.v[28], a.f[29], v.v[29], SENTINEL |
| 412 |
); |
| 413 |
|
| 414 |
if (res < 0) { |
| 415 |
ast_log(LOG_WARNING, "Failed to store. Check the debug log for possible data repository related entries.\n"); |
| 416 |
} else { |
| 417 |
snprintf(storeid, sizeof(storeid), "%d", res); |
| 418 |
pbx_builtin_setvar_helper(chan, "RTSTOREID", storeid); |
| 419 |
} |
| 420 |
|
| 421 |
if (chan) |
| 422 |
ast_autoservice_stop(chan); |
| 423 |
|
| 424 |
return 0; |
| 425 |
} |
| 426 |
|
| 427 |
static int function_realtime_readdestroy(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len) |
| 428 |
{ |
| 429 |
struct ast_variable *var, *head; |
| 430 |
struct ast_str *out; |
| 431 |
size_t resultslen; |
| 432 |
int n; |
| 433 |
AST_DECLARE_APP_ARGS(args, |
| 434 |
AST_APP_ARG(family); |
| 435 |
AST_APP_ARG(fieldmatch); |
| 436 |
AST_APP_ARG(value); |
| 437 |
AST_APP_ARG(delim1); |
| 438 |
AST_APP_ARG(delim2); |
| 439 |
); |
| 440 |
|
| 441 |
if (ast_strlen_zero(data)) { |
| 442 |
ast_log(LOG_WARNING, "Syntax: REALTIME_DESTROY(family,fieldmatch[,matchvalue[,delim1[,delim2]]]) - missing argument!\n"); |
| 443 |
return -1; |
| 444 |
} |
| 445 |
|
| 446 |
AST_STANDARD_APP_ARGS(args, data); |
| 447 |
|
| 448 |
if (!args.delim1) |
| 449 |
args.delim1 = ","; |
| 450 |
if (!args.delim2) |
| 451 |
args.delim2 = "="; |
| 452 |
|
| 453 |
if (chan) |
| 454 |
ast_autoservice_start(chan); |
| 455 |
|
| 456 |
head = ast_load_realtime_all(args.family, args.fieldmatch, args.value, SENTINEL); |
| 457 |
|
| 458 |
if (!head) { |
| 459 |
if (chan) |
| 460 |
ast_autoservice_stop(chan); |
| 461 |
return -1; |
| 462 |
} |
| 463 |
|
| 464 |
if (len > 0) { |
| 465 |
resultslen = 0; |
| 466 |
n = 0; |
| 467 |
for (var = head; var; n++, var = var->next) { |
| 468 |
resultslen += strlen(var->name) + strlen(var->value); |
| 469 |
} |
| 470 |
|
| 471 |
resultslen += n * (strlen(args.delim1) + strlen(args.delim2)) + 1; |
| 472 |
|
| 473 |
if (resultslen > len) { |
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
ast_log(LOG_WARNING, "Failed to fetch/destroy. Realtime data is too large: need %zu, have %zu.\n", resultslen, len); |
| 479 |
if (chan) { |
| 480 |
ast_autoservice_stop(chan); |
| 481 |
} |
| 482 |
return -1; |
| 483 |
} |
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
out = ast_str_alloca(resultslen); |
| 488 |
for (var = head; var; var = var->next) { |
| 489 |
ast_str_append(&out, 0, "%s%s%s%s", var->name, args.delim2, var->value, args.delim1); |
| 490 |
} |
| 491 |
ast_copy_string(buf, ast_str_buffer(out), len); |
| 492 |
} |
| 493 |
|
| 494 |
ast_destroy_realtime(args.family, args.fieldmatch, args.value, SENTINEL); |
| 495 |
ast_variables_destroy(head); |
| 496 |
|
| 497 |
if (chan) |
| 498 |
ast_autoservice_stop(chan); |
| 499 |
|
| 500 |
return 0; |
| 501 |
} |
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
static int function_realtime_writedestroy(struct ast_channel *chan, const char *cmd, char *data, const char *value) |
| 508 |
{ |
| 509 |
return function_realtime_readdestroy(chan, cmd, data, NULL, 0); |
| 510 |
} |
| 511 |
|
| 512 |
static struct ast_custom_function realtime_function = { |
| 513 |
.name = "REALTIME", |
| 514 |
.read = function_realtime_read, |
| 515 |
.write = function_realtime_write, |
| 516 |
}; |
| 517 |
|
| 518 |
static struct ast_custom_function realtimefield_function = { |
| 519 |
.name = "REALTIME_FIELD", |
| 520 |
.read = realtimefield_read, |
| 521 |
.write = function_realtime_write, |
| 522 |
}; |
| 523 |
|
| 524 |
static struct ast_custom_function realtimehash_function = { |
| 525 |
.name = "REALTIME_HASH", |
| 526 |
.read = realtimefield_read, |
| 527 |
}; |
| 528 |
|
| 529 |
static struct ast_custom_function realtime_store_function = { |
| 530 |
.name = "REALTIME_STORE", |
| 531 |
.write = function_realtime_store, |
| 532 |
}; |
| 533 |
|
| 534 |
static struct ast_custom_function realtime_destroy_function = { |
| 535 |
.name = "REALTIME_DESTROY", |
| 536 |
.read = function_realtime_readdestroy, |
| 537 |
.write = function_realtime_writedestroy, |
| 538 |
}; |
| 539 |
|
| 540 |
static int unload_module(void) |
| 541 |
{ |
| 542 |
int res = 0; |
| 543 |
res |= ast_custom_function_unregister(&realtime_function); |
| 544 |
res |= ast_custom_function_unregister(&realtime_store_function); |
| 545 |
res |= ast_custom_function_unregister(&realtime_destroy_function); |
| 546 |
res |= ast_custom_function_unregister(&realtimefield_function); |
| 547 |
res |= ast_custom_function_unregister(&realtimehash_function); |
| 548 |
return res; |
| 549 |
} |
| 550 |
|
| 551 |
static int load_module(void) |
| 552 |
{ |
| 553 |
int res = 0; |
| 554 |
res |= ast_custom_function_register(&realtime_function); |
| 555 |
res |= ast_custom_function_register(&realtime_store_function); |
| 556 |
res |= ast_custom_function_register_escalating(&realtime_destroy_function, AST_CFE_READ); |
| 557 |
res |= ast_custom_function_register(&realtimefield_function); |
| 558 |
res |= ast_custom_function_register(&realtimehash_function); |
| 559 |
return res; |
| 560 |
} |
| 561 |
|
| 562 |
AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Read/Write/Store/Destroy values from a RealTime repository"); |