| 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 "cdrel.h" |
| 32 |
|
| 33 |
#include "asterisk/config.h" |
| 34 |
#include "asterisk/module.h" |
| 35 |
#include "asterisk/paths.h" |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
static const char *dirname_map[cdrel_backend_type_end][cdrel_record_type_end] = { |
| 42 |
[cdrel_backend_text] = { |
| 43 |
[cdrel_record_cdr] = "cdr-custom", |
| 44 |
[cdrel_record_cel] = "cel-custom", |
| 45 |
}, |
| 46 |
[cdrel_backend_db] = { |
| 47 |
[cdrel_record_cdr] = NULL, |
| 48 |
[cdrel_record_cel] = NULL, |
| 49 |
} |
| 50 |
}; |
| 51 |
|
| 52 |
struct field_parse_result { |
| 53 |
char *result; |
| 54 |
int functions; |
| 55 |
int csv_quote; |
| 56 |
int cdr; |
| 57 |
int is_literal; |
| 58 |
int unknown_functions; |
| 59 |
int parse_failed; |
| 60 |
}; |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
static const char *allowed_functions[] = { |
| 68 |
[cdrel_record_cdr] = "CSV_QUOTE CDR CALLERID CHANNEL", |
| 69 |
[cdrel_record_cel] = "CSV_QUOTE CALLERID CHANNEL eventtype eventtime eventenum userdeftype eventextra BRIDGEPEER", |
| 70 |
}; |
| 71 |
|
| 72 |
static const char *special_vars[] = { |
| 73 |
[cdrel_record_cdr] = "", |
| 74 |
[cdrel_record_cel] = "eventtype eventtime eventenum userdeftype eventextra BRIDGEPEER", |
| 75 |
}; |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
static struct field_parse_result parse_field(enum cdrel_record_type record_type, char *input_field_template) |
| 96 |
{ |
| 97 |
char *tmp_field = NULL; |
| 98 |
struct field_parse_result result = { 0, }; |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
if (input_field_template[0] == '"') { |
| 105 |
result.result = ast_strdup(ast_strip_quoted(ast_strdupa(input_field_template), "\"", "\"")); |
| 106 |
result.csv_quote = 1; |
| 107 |
result.is_literal = 1; |
| 108 |
return result; |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
tmp_field = ast_strip(ast_strdupa(input_field_template)); |
| 116 |
|
| 117 |
if (tmp_field[0] == '\'') { |
| 118 |
result.csv_quote = 1; |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
tmp_field = ast_strip(ast_strip_quoted(tmp_field, "\"'", "\"'")); |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
while (tmp_field[0] == '$') { |
| 139 |
char *ptr = NULL; |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
char func_name[65]; |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
tmp_field++; |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
tmp_field = ast_strip(ast_strip_quoted(tmp_field, "[{(", "]})")); |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
if (strstr(special_vars[record_type], tmp_field) != NULL) { |
| 164 |
result.functions++; |
| 165 |
break; |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
ptr = strchr(tmp_field, '('); |
| 177 |
if (!ptr) { |
| 178 |
result.parse_failed++; |
| 179 |
continue; |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
ast_copy_string(func_name, tmp_field, MIN(sizeof(func_name), ptr - tmp_field + 1)); |
| 194 |
if (strstr(allowed_functions[record_type], func_name) == NULL) { |
| 195 |
result.parse_failed++; |
| 196 |
result.unknown_functions++; |
| 197 |
continue; |
| 198 |
} |
| 199 |
result.functions++; |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
if (strcmp("CSV_QUOTE", func_name) == 0) { |
| 204 |
result.csv_quote = 1; |
| 205 |
} else if (strcmp("CDR", func_name) == 0) { |
| 206 |
result.cdr = 1; |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
tmp_field = ast_strip_quoted(ptr, "(", ")"); |
| 217 |
if (tmp_field[0] == '"' || tmp_field[0] == '\'') { |
| 218 |
result.result = ast_strdup(ast_strip_quoted(tmp_field, "\"'", "\"'")); |
| 219 |
result.csv_quote = 1; |
| 220 |
result.is_literal = 1; |
| 221 |
return result; |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
if (result.parse_failed) { |
| 231 |
tmp_field = input_field_template; |
| 232 |
} else { |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
if (result.functions == 0) { |
| 238 |
result.is_literal = 1; |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
result.result = ast_strdup(tmp_field); |
| 243 |
if (result.result == NULL) { |
| 244 |
result.parse_failed = 1; |
| 245 |
} |
| 246 |
|
| 247 |
return result; |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
static int parse_legacy_template(enum cdrel_record_type record_type, const char *config_filename, |
| 266 |
const char *output_filename, const char *input_template, struct ast_vector_string *fields) |
| 267 |
{ |
| 268 |
char *template = ast_strdupa(input_template); |
| 269 |
char *field_template = NULL; |
| 270 |
int res = 0; |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
while((field_template = ast_strsep(&template, ',', AST_STRSEP_TRIM))) { |
| 277 |
char *uservar = ""; |
| 278 |
char *literal = ""; |
| 279 |
|
| 280 |
struct field_parse_result result = parse_field(record_type, field_template); |
| 281 |
|
| 282 |
ast_debug(2, "field: '%s' literal: %d quote: %d cdr: %d failed: %d funcs: %d unknfuncs: %d\n", result.result, |
| 283 |
result.is_literal, result.csv_quote, result.cdr, |
| 284 |
result.parse_failed, result.functions, result.unknown_functions); |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
if (!result.result || result.parse_failed) { |
| 290 |
ast_free(result.result); |
| 291 |
return 0; |
| 292 |
} |
| 293 |
if (result.is_literal) { |
| 294 |
literal = "literal^"; |
| 295 |
} |
| 296 |
|
| 297 |
if (!get_registered_field_by_name(record_type, result.result)) { |
| 298 |
ast_debug(3, " %s->%s: field '%s' not found\n", cdrel_basename(config_filename), |
| 299 |
cdrel_basename(output_filename), result.result); |
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
if (result.cdr) { |
| 305 |
uservar = "uservar^"; |
| 306 |
} else { |
| 307 |
literal = "literal^"; |
| 308 |
} |
| 309 |
} |
| 310 |
res = ast_asprintf(&field_template, "%s(%s%s)", result.result, S_OR(literal,uservar), result.csv_quote ? "quote" : "noquote"); |
| 311 |
ast_free(result.result); |
| 312 |
|
| 313 |
if (!field_template || res < 0) { |
| 314 |
ast_free(field_template); |
| 315 |
return 0; |
| 316 |
} |
| 317 |
res = AST_VECTOR_APPEND(fields, field_template); |
| 318 |
if (res != 0) { |
| 319 |
ast_free(field_template); |
| 320 |
return 0; |
| 321 |
} |
| 322 |
ast_debug(2, " field template: %s\n", field_template); |
| 323 |
} |
| 324 |
|
| 325 |
return 1; |
| 326 |
} |
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
static struct cdrel_field *field_alloc(struct cdrel_config *config, const char *input_field_template) |
| 337 |
{ |
| 338 |
RAII_VAR(struct cdrel_field *, field, NULL, ast_free); |
| 339 |
const struct cdrel_field *registered_field = NULL; |
| 340 |
struct cdrel_field *rtn_field = NULL; |
| 341 |
char *field_name = NULL; |
| 342 |
char *data = NULL; |
| 343 |
char *tmp_data = NULL; |
| 344 |
char *closeparen = NULL; |
| 345 |
char *qualifier = NULL; |
| 346 |
enum cdrel_data_type forced_output_data_type = cdrel_data_type_end; |
| 347 |
struct ast_flags field_flags = { 0 }; |
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
if (config->backend_type == cdrel_backend_db && config->config_type == cdrel_config_legacy) { |
| 354 |
registered_field = get_registered_field_by_name(config->record_type, "literal"); |
| 355 |
ast_assert(registered_field != NULL); |
| 356 |
rtn_field = ast_calloc(1, sizeof(*field) + strlen(input_field_template) + 1); |
| 357 |
if (!rtn_field) { |
| 358 |
return NULL; |
| 359 |
} |
| 360 |
memcpy(rtn_field, registered_field, sizeof(*registered_field)); |
| 361 |
strcpy(rtn_field->data, input_field_template); |
| 362 |
return rtn_field; |
| 363 |
} |
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
if (input_field_template[0] == '"' || input_field_template[0] == '\'') { |
| 370 |
data = ast_strip_quoted(ast_strdupa(input_field_template), "\"'", "\"'"); |
| 371 |
ast_set_flag(&field_flags, cdrel_flag_literal); |
| 372 |
ast_debug(3, " Using qualifier 'literal' for field '%s' flags: %s\n", data, |
| 373 |
ast_str_tmp(128, cdrel_get_field_flags(&field_flags, &STR_TMP))); |
| 374 |
field_name = "literal"; |
| 375 |
} else { |
| 376 |
field_name = ast_strdupa(input_field_template); |
| 377 |
data = strchr(field_name, '('); |
| 378 |
|
| 379 |
if (data) { |
| 380 |
*data = '\0'; |
| 381 |
data++; |
| 382 |
closeparen = strchr(data, ')'); |
| 383 |
if (closeparen) { |
| 384 |
*closeparen = '\0'; |
| 385 |
} |
| 386 |
} |
| 387 |
} |
| 388 |
|
| 389 |
if (!ast_strlen_zero(data) && !ast_test_flag(&field_flags, cdrel_flag_literal)) { |
| 390 |
char *data_swap = NULL; |
| 391 |
tmp_data = ast_strdupa(data); |
| 392 |
|
| 393 |
while((qualifier = ast_strsep(&tmp_data, '^', AST_STRSEP_STRIP | AST_STRSEP_TRIM))) { |
| 394 |
enum cdrel_data_type fodt = cdrel_data_type_end; |
| 395 |
if (ast_strlen_zero(qualifier)) { |
| 396 |
continue; |
| 397 |
} |
| 398 |
fodt = cdrel_data_type_from_str(qualifier); |
| 399 |
if (fodt < cdrel_data_type_end) { |
| 400 |
ast_set_flag(&field_flags, cdrel_flag_type_forced); |
| 401 |
if (fodt == cdrel_type_uservar) { |
| 402 |
ast_set_flag(&field_flags, cdrel_flag_uservar); |
| 403 |
ast_debug(3, " Using qualifier '%s' for field '%s' flags: %s\n", qualifier, |
| 404 |
field_name, ast_str_tmp(128, cdrel_get_field_flags(&field_flags, &STR_TMP))); |
| 405 |
data_swap = ast_strdupa(field_name); |
| 406 |
field_name = "uservar"; |
| 407 |
} else if (fodt == cdrel_type_literal) { |
| 408 |
ast_set_flag(&field_flags, cdrel_flag_literal); |
| 409 |
ast_debug(3, " Using qualifier '%s' for field '%s' flags: %s\n", qualifier, |
| 410 |
field_name, ast_str_tmp(128, cdrel_get_field_flags(&field_flags, &STR_TMP))); |
| 411 |
data_swap = ast_strdupa(field_name); |
| 412 |
field_name = "literal"; |
| 413 |
} else { |
| 414 |
forced_output_data_type = fodt; |
| 415 |
ast_debug(3, " Using qualifier '%s' for field '%s' flags: %s\n", qualifier, |
| 416 |
field_name, ast_str_tmp(128, cdrel_get_field_flags(&field_flags, &STR_TMP))); |
| 417 |
} |
| 418 |
continue; |
| 419 |
} |
| 420 |
if (strcasecmp(qualifier, "quote") == 0) { |
| 421 |
ast_set_flag(&field_flags, cdrel_flag_quote); |
| 422 |
ast_debug(3, " Using qualifier '%s' for field '%s' flags: %s\n", qualifier, |
| 423 |
field_name, ast_str_tmp(128, cdrel_get_field_flags(&field_flags, &STR_TMP))); |
| 424 |
continue; |
| 425 |
} |
| 426 |
if (strcasecmp(qualifier, "noquote") == 0) { |
| 427 |
ast_set_flag(&field_flags, cdrel_flag_noquote); |
| 428 |
ast_debug(3, " Using qualifier '%s' for field '%s' flags: %s\n", qualifier, |
| 429 |
field_name, ast_str_tmp(128, cdrel_get_field_flags(&field_flags, &STR_TMP))); |
| 430 |
continue; |
| 431 |
} |
| 432 |
if (strchr(qualifier, '%') != NULL) { |
| 433 |
data_swap = ast_strdupa(qualifier); |
| 434 |
ast_set_flag(&field_flags, cdrel_flag_format_spec); |
| 435 |
forced_output_data_type = cdrel_type_string; |
| 436 |
ast_debug(3, " Using qualifier '%s' for field '%s' flags: %s\n", qualifier, |
| 437 |
field_name, ast_str_tmp(128, cdrel_get_field_flags(&field_flags, &STR_TMP))); |
| 438 |
} |
| 439 |
} |
| 440 |
if (ast_test_flag(&field_flags, cdrel_flag_quote) && ast_test_flag(&field_flags, cdrel_flag_noquote)) { |
| 441 |
ast_log(LOG_WARNING, "%s->%s: Field '%s(%s)' has both quote and noquote\n", |
| 442 |
config->config_filename, config->output_filename, field_name, data); |
| 443 |
return NULL; |
| 444 |
} |
| 445 |
data = data_swap; |
| 446 |
} |
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
if (ast_test_flag(&field_flags, cdrel_flag_literal)) { |
| 452 |
if (config->format_type == cdrel_format_json && !strchr(data, ':')) { |
| 453 |
ast_log(LOG_WARNING, "%s->%s: Literal field '%s' must be formatted as \"name: value\" when using the 'json' format\n", |
| 454 |
cdrel_basename(config->config_filename), cdrel_basename(config->output_filename), |
| 455 |
input_field_template); |
| 456 |
return NULL; |
| 457 |
} |
| 458 |
} |
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
registered_field = get_registered_field_by_name(config->record_type, field_name); |
| 464 |
if (!registered_field) { |
| 465 |
ast_log(LOG_WARNING, "%s->%s: Field '%s' not found\n", |
| 466 |
cdrel_basename(config->config_filename), cdrel_basename(config->output_filename), field_name); |
| 467 |
return NULL; |
| 468 |
} |
| 469 |
|
| 470 |
if (ast_test_flag(&field_flags, cdrel_flag_format_spec) |
| 471 |
&& registered_field->input_data_type != cdrel_type_timeval) { |
| 472 |
ast_log(LOG_WARNING, "%s->%s: Custom format '%s' ignored for field '%s'." |
| 473 |
" Only timeval types can use custom format strings.\n", |
| 474 |
cdrel_basename(config->config_filename), cdrel_basename(config->output_filename), |
| 475 |
data, field_name); |
| 476 |
forced_output_data_type = cdrel_data_type_end; |
| 477 |
ast_clear_flag(&field_flags, cdrel_flag_format_spec); |
| 478 |
data = NULL; |
| 479 |
} |
| 480 |
|
| 481 |
field = ast_calloc(1, sizeof(*registered_field) + strlen(input_field_template) + 1); |
| 482 |
if (!field) { |
| 483 |
return NULL; |
| 484 |
} |
| 485 |
memcpy(field, registered_field, sizeof(*field)); |
| 486 |
|
| 487 |
if (!ast_strlen_zero(data)) { |
| 488 |
strcpy(field->data, data); |
| 489 |
} |
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
if (field->input_data_type == cdrel_type_uservar) { |
| 496 |
field->name = field->data; |
| 497 |
} |
| 498 |
|
| 499 |
if (field->input_data_type == cdrel_type_literal && config->format_type == cdrel_format_json) { |
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
char *ptr = strchr(data, ':'); |
| 511 |
*ptr = '\0'; |
| 512 |
ptr++; |
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
strcpy(field->data, ast_strip_quoted(ptr, "\"", "\"")); |
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
ptr = field->data + strlen(field->data); |
| 522 |
ptr++; |
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
strcpy(ptr, data); |
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
field->name = ptr; |
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
} |
| 538 |
|
| 539 |
if (forced_output_data_type < cdrel_data_type_end) { |
| 540 |
field->output_data_type = forced_output_data_type; |
| 541 |
} |
| 542 |
field->flags = field_flags; |
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
if (!ast_test_flag(&field->flags, cdrel_flag_noquote)) { |
| 549 |
if (config->quoting_method == cdrel_quoting_method_all) { |
| 550 |
ast_set_flag(&field->flags, cdrel_flag_quote); |
| 551 |
} else if (config->quoting_method == cdrel_quoting_method_non_numeric) { |
| 552 |
if (field->output_data_type > cdrel_data_type_strings_end) { |
| 553 |
ast_set_flag(&field->flags, cdrel_flag_noquote); |
| 554 |
} else { |
| 555 |
ast_set_flag(&field->flags, cdrel_flag_quote); |
| 556 |
} |
| 557 |
} |
| 558 |
} |
| 559 |
|
| 560 |
if (config->quoting_method == cdrel_quoting_method_none) { |
| 561 |
ast_clear_flag(&field->flags, cdrel_flag_quote); |
| 562 |
ast_set_flag(&field->flags, cdrel_flag_noquote); |
| 563 |
} |
| 564 |
|
| 565 |
ast_debug(2, "%s->%s: Field '%s' processed -> name:'%s' input_type:%s output_type:%s flags:'%s' data:'%s'\n", |
| 566 |
cdrel_basename(config->config_filename), cdrel_basename(config->output_filename), input_field_template, |
| 567 |
field->name, DATA_TYPE_STR(field->input_data_type), |
| 568 |
DATA_TYPE_STR(field->output_data_type), |
| 569 |
ast_str_tmp(128, cdrel_get_field_flags(&field->flags, &STR_TMP)), |
| 570 |
field->data); |
| 571 |
|
| 572 |
rtn_field = field; |
| 573 |
field = NULL; |
| 574 |
return rtn_field; |
| 575 |
} |
| 576 |
|
| 577 |
static void field_template_vector_free(struct ast_vector_string *fields) { |
| 578 |
AST_VECTOR_RESET(fields, ast_free); |
| 579 |
AST_VECTOR_PTR_FREE(fields); |
| 580 |
} |
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
static int load_fields(struct cdrel_config *config, struct ast_vector_string *fields) |
| 592 |
{ |
| 593 |
int res = 0; |
| 594 |
int ix = 0; |
| 595 |
|
| 596 |
ast_debug(1, "%s->%s: Loading fields\n", cdrel_basename(config->config_filename), |
| 597 |
cdrel_basename(config->output_filename)); |
| 598 |
|
| 599 |
for (ix = 0; ix < AST_VECTOR_SIZE(fields); ix++) { |
| 600 |
char *field_name = AST_VECTOR_GET(fields, ix); |
| 601 |
struct cdrel_field *field = NULL; |
| 602 |
|
| 603 |
field = field_alloc(config, field_name); |
| 604 |
if (!field) { |
| 605 |
res = -1; |
| 606 |
continue; |
| 607 |
} |
| 608 |
|
| 609 |
if (AST_VECTOR_APPEND(&config->fields, field) != 0) { |
| 610 |
ast_free(field); |
| 611 |
return -1; |
| 612 |
} |
| 613 |
} |
| 614 |
|
| 615 |
return res; |
| 616 |
} |
| 617 |
|
| 618 |
static void config_free(struct cdrel_config *config) |
| 619 |
{ |
| 620 |
if (!config) { |
| 621 |
return; |
| 622 |
} |
| 623 |
|
| 624 |
if (config->insert) { |
| 625 |
sqlite3_finalize(config->insert); |
| 626 |
config->insert = NULL; |
| 627 |
} |
| 628 |
|
| 629 |
if (config->db) { |
| 630 |
sqlite3_close(config->db); |
| 631 |
config->db = NULL; |
| 632 |
} |
| 633 |
|
| 634 |
ast_mutex_destroy(&config->lock); |
| 635 |
ast_string_field_free_memory(config); |
| 636 |
AST_VECTOR_RESET(&config->fields, ast_free); |
| 637 |
AST_VECTOR_FREE(&config->fields); |
| 638 |
ast_free(config); |
| 639 |
} |
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
static struct cdrel_config *config_alloc(enum cdrel_record_type record_type, |
| 656 |
enum cdrel_backend_type backend_type, enum cdrel_config_type config_type, |
| 657 |
const char *config_filename, const char *output_filename, const char *template) |
| 658 |
{ |
| 659 |
RAII_VAR(struct cdrel_config *, config, NULL, config_free); |
| 660 |
struct cdrel_config *rtn_config = NULL; |
| 661 |
const char *file_suffix = ""; |
| 662 |
int res = 0; |
| 663 |
|
| 664 |
ast_debug(1, "%s->%s: Loading\n", cdrel_basename(config_filename), cdrel_basename(output_filename)); |
| 665 |
|
| 666 |
config = ast_calloc_with_stringfields(1, struct cdrel_config, 1024); |
| 667 |
if (!config) { |
| 668 |
return NULL; |
| 669 |
} |
| 670 |
|
| 671 |
if (ast_string_field_set(config, config_filename, config_filename) != 0) { |
| 672 |
return NULL; |
| 673 |
} |
| 674 |
|
| 675 |
config->record_type = record_type; |
| 676 |
config->backend_type = backend_type; |
| 677 |
config->dummy_channel_alloc = cdrel_dummy_channel_allocators[record_type]; |
| 678 |
config->config_type = config_type; |
| 679 |
|
| 680 |
|
| 681 |
config->format_type = cdrel_format_dsv; |
| 682 |
config->separator[0] = ','; |
| 683 |
switch(backend_type) { |
| 684 |
case cdrel_backend_text: |
| 685 |
config->quote[0] = '"'; |
| 686 |
config->quoting_method = cdrel_quoting_method_all; |
| 687 |
break; |
| 688 |
case cdrel_backend_db: |
| 689 |
config->quote[0] = '\0'; |
| 690 |
config->format_type = cdrel_format_sql; |
| 691 |
config->quoting_method = cdrel_quoting_method_none; |
| 692 |
if (!ast_ends_with(output_filename, ".db")) { |
| 693 |
file_suffix = ".db"; |
| 694 |
} |
| 695 |
break; |
| 696 |
default: |
| 697 |
ast_log(LOG_ERROR, "%s->%s: Unknown backend type '%d'\n", cdrel_basename(config_filename), |
| 698 |
cdrel_basename(output_filename), backend_type); |
| 699 |
break; |
| 700 |
} |
| 701 |
config->quote_escape[0] = config->quote[0]; |
| 702 |
|
| 703 |
res = ast_string_field_set(config, template, template); |
| 704 |
if (res != 0) { |
| 705 |
return NULL; |
| 706 |
} |
| 707 |
|
| 708 |
if (output_filename[0] == '/') { |
| 709 |
res = ast_string_field_build(config, output_filename, "%s%s", output_filename, file_suffix); |
| 710 |
} else { |
| 711 |
const char *subdir = dirname_map[backend_type][record_type]; |
| 712 |
res = ast_string_field_build(config, output_filename, "%s/%s%s%s%s", |
| 713 |
ast_config_AST_LOG_DIR, S_OR(subdir, ""), ast_strlen_zero(subdir) ? "" : "/", output_filename, file_suffix); |
| 714 |
} |
| 715 |
if (res != 0) { |
| 716 |
return NULL; |
| 717 |
} |
| 718 |
ast_mutex_init(&config->lock); |
| 719 |
|
| 720 |
rtn_config = config; |
| 721 |
config = NULL; |
| 722 |
return rtn_config; |
| 723 |
} |
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
static int load_database_columns(struct cdrel_config *config, const char *columns, int *column_count) |
| 736 |
{ |
| 737 |
char *col = NULL; |
| 738 |
char *cols = NULL; |
| 739 |
RAII_VAR(struct ast_str *, column_string, NULL, ast_free); |
| 740 |
|
| 741 |
ast_debug(1, "%s->%s: Loading columns\n", cdrel_basename(config->config_filename), |
| 742 |
cdrel_basename(config->output_filename)); |
| 743 |
|
| 744 |
if (!(column_string = ast_str_create(1024))) { |
| 745 |
return -1; |
| 746 |
} |
| 747 |
|
| 748 |
cols = ast_strdupa(columns); |
| 749 |
*column_count = 0; |
| 750 |
|
| 751 |
|
| 752 |
while((col = ast_strsep(&cols, ',', AST_STRSEP_TRIM))) { |
| 753 |
col = ast_strip(ast_strip_quoted(col, "'\"", "'\"")); |
| 754 |
if (ast_str_append(&column_string, 0, "%s%s", *column_count ? "," : "", col) <= 0) { |
| 755 |
return -1; |
| 756 |
} |
| 757 |
(*column_count)++; |
| 758 |
} |
| 759 |
|
| 760 |
if (ast_string_field_set(config, db_columns, ast_str_buffer(column_string)) != 0) { |
| 761 |
return -1; |
| 762 |
} |
| 763 |
|
| 764 |
return 0; |
| 765 |
} |
| 766 |
|
| 767 |
static char *make_stmt_placeholders(int columns) |
| 768 |
{ |
| 769 |
char *placeholders = ast_malloc(2 * columns), *c = placeholders; |
| 770 |
if (placeholders) { |
| 771 |
for (; columns; columns--) { |
| 772 |
*c++ = '?'; |
| 773 |
*c++ = ','; |
| 774 |
} |
| 775 |
*(c - 1) = 0; |
| 776 |
} |
| 777 |
return placeholders; |
| 778 |
} |
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
static int open_database(struct cdrel_config *config) |
| 789 |
{ |
| 790 |
char *sql = NULL; |
| 791 |
int res = 0; |
| 792 |
char *placeholders = NULL; |
| 793 |
|
| 794 |
ast_debug(1, "%s->%s: opening database\n", cdrel_basename(config->config_filename), |
| 795 |
cdrel_basename(config->output_filename)); |
| 796 |
res = sqlite3_open(config->output_filename, &config->db); |
| 797 |
if (res != SQLITE_OK) { |
| 798 |
ast_log(LOG_WARNING, "%s->%s: Could not open database\n", cdrel_basename(config->config_filename), |
| 799 |
cdrel_basename(config->output_filename)); |
| 800 |
return -1; |
| 801 |
} |
| 802 |
|
| 803 |
sqlite3_busy_timeout(config->db, config->busy_timeout); |
| 804 |
|
| 805 |
|
| 806 |
sql = sqlite3_mprintf("SELECT COUNT(*) FROM %q;", config->db_table); |
| 807 |
if (!sql) { |
| 808 |
return -1; |
| 809 |
} |
| 810 |
res = sqlite3_exec(config->db, sql, NULL, NULL, NULL); |
| 811 |
sqlite3_free(sql); |
| 812 |
if (res != SQLITE_OK) { |
| 813 |
|
| 814 |
|
| 815 |
|
| 816 |
|
| 817 |
sql = sqlite3_mprintf("CREATE TABLE %q (AcctId INTEGER PRIMARY KEY, %s)", |
| 818 |
config->db_table, config->db_columns); |
| 819 |
res = sqlite3_exec(config->db, sql, NULL, NULL, NULL); |
| 820 |
sqlite3_free(sql); |
| 821 |
if (res != SQLITE_OK) { |
| 822 |
ast_log(LOG_WARNING, "%s->%s: Unable to create table '%s': %s\n", |
| 823 |
cdrel_basename(config->config_filename), cdrel_basename(config->output_filename), |
| 824 |
config->db_table, sqlite3_errmsg(config->db)); |
| 825 |
return -1; |
| 826 |
} |
| 827 |
} else { |
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
|
| 832 |
sqlite3_stmt *get_stmt; |
| 833 |
int existing_columns = 0; |
| 834 |
int config_columns = AST_VECTOR_SIZE(&config->fields); |
| 835 |
|
| 836 |
sql = sqlite3_mprintf("SELECT * FROM %q;", config->db_table); |
| 837 |
if (!sql) { |
| 838 |
return -1; |
| 839 |
} |
| 840 |
res = sqlite3_prepare_v2(config->db, sql, -1, &get_stmt, NULL); |
| 841 |
sqlite3_free(sql); |
| 842 |
if (res != SQLITE_OK) { |
| 843 |
ast_log(LOG_WARNING, "%s->%s: Unable to get column count for table '%s': %s\n", |
| 844 |
cdrel_basename(config->config_filename), cdrel_basename(config->output_filename), |
| 845 |
config->db_table, sqlite3_errmsg(config->db)); |
| 846 |
return -1; |
| 847 |
} |
| 848 |
|
| 849 |
|
| 850 |
|
| 851 |
|
| 852 |
existing_columns = sqlite3_column_count(get_stmt); |
| 853 |
sqlite3_finalize(get_stmt); |
| 854 |
|
| 855 |
if ((config_columns + 1) != existing_columns) { |
| 856 |
ast_log(LOG_WARNING, "%s->%s: The number of fields in the config (%d) doesn't equal the" |
| 857 |
" nummber of data columns (%d) in the existing %s table. This config is disabled.\n", |
| 858 |
cdrel_basename(config->config_filename), cdrel_basename(config->output_filename), |
| 859 |
config_columns, existing_columns - 1, config->db_table); |
| 860 |
return -1; |
| 861 |
} |
| 862 |
} |
| 863 |
|
| 864 |
placeholders = make_stmt_placeholders(AST_VECTOR_SIZE(&config->fields)); |
| 865 |
if (!placeholders) { |
| 866 |
return -1; |
| 867 |
} |
| 868 |
|
| 869 |
|
| 870 |
sql = sqlite3_mprintf("INSERT INTO %q VALUES (NULL,%s)", config->db_table, placeholders); |
| 871 |
ast_free(placeholders); |
| 872 |
if (!sql) { |
| 873 |
return -1; |
| 874 |
} |
| 875 |
|
| 876 |
#if SQLITE_VERSION_NUMBER >= 3020000 |
| 877 |
res = sqlite3_prepare_v3(config->db, sql, -1, SQLITE_PREPARE_PERSISTENT, &config->insert, NULL); |
| 878 |
#else |
| 879 |
res = sqlite3_prepare_v2(config->db, sql, -1, &config->insert, NULL); |
| 880 |
#endif |
| 881 |
if (res != SQLITE_OK) { |
| 882 |
ast_log(LOG_ERROR, "%s->%s: Unable to prepare INSERT statement '%s': %s\n", |
| 883 |
cdrel_basename(config->config_filename), cdrel_basename(config->output_filename), |
| 884 |
sql, sqlite3_errmsg(config->db)); |
| 885 |
return -1; |
| 886 |
} |
| 887 |
|
| 888 |
return 0; |
| 889 |
} |
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
static struct cdrel_config *load_database_config(enum cdrel_record_type record_type, |
| 901 |
struct ast_category *category, const char *config_filename) |
| 902 |
{ |
| 903 |
const char *category_name = ast_category_get_name(category); |
| 904 |
RAII_VAR(struct cdrel_config *, config, NULL, config_free); |
| 905 |
struct cdrel_config *rtn_config = NULL; |
| 906 |
int res = 0; |
| 907 |
int column_count = 0; |
| 908 |
int value_count = 0; |
| 909 |
int field_check_passed = 0; |
| 910 |
const char *template = ast_variable_find(category, "values"); |
| 911 |
enum cdrel_config_type config_type; |
| 912 |
const char *value; |
| 913 |
char *tmp_fields = NULL; |
| 914 |
RAII_VAR(struct ast_vector_string *, field_templates, ast_calloc(1, sizeof(*field_templates)), field_template_vector_free); |
| 915 |
|
| 916 |
if (!ast_strlen_zero(template)) { |
| 917 |
config_type = cdrel_config_legacy; |
| 918 |
} else { |
| 919 |
template = ast_variable_find(category, "fields"); |
| 920 |
if (!ast_strlen_zero(template)) { |
| 921 |
config_type = cdrel_config_advanced; |
| 922 |
} |
| 923 |
} |
| 924 |
if (ast_strlen_zero(template)) { |
| 925 |
ast_log(LOG_WARNING, "%s->%s: Neither 'values' nor 'fields' specified\n", |
| 926 |
cdrel_basename(config_filename), cdrel_basename(category_name)); |
| 927 |
return NULL; |
| 928 |
} |
| 929 |
|
| 930 |
res = AST_VECTOR_INIT(field_templates, 25); |
| 931 |
if (res != 0) { |
| 932 |
return NULL; |
| 933 |
} |
| 934 |
|
| 935 |
|
| 936 |
|
| 937 |
|
| 938 |
|
| 939 |
if (config_type == cdrel_config_legacy) { |
| 940 |
field_check_passed = parse_legacy_template(record_type, config_filename, |
| 941 |
category_name, template, field_templates); |
| 942 |
if (field_check_passed) { |
| 943 |
config_type = cdrel_config_advanced; |
| 944 |
ast_log(LOG_NOTICE, "%s->%s: Legacy config upgraded to advanced\n", |
| 945 |
cdrel_basename(config_filename), cdrel_basename(category_name)); |
| 946 |
} else { |
| 947 |
AST_VECTOR_RESET(field_templates, ast_free); |
| 948 |
ast_log(LOG_NOTICE, "%s->%s: Unable to upgrade legacy config to advanced. Continuing to process as legacy\n", |
| 949 |
cdrel_basename(config_filename), cdrel_basename(category_name)); |
| 950 |
} |
| 951 |
} |
| 952 |
|
| 953 |
|
| 954 |
|
| 955 |
|
| 956 |
|
| 957 |
|
| 958 |
if (AST_VECTOR_SIZE(field_templates) == 0) { |
| 959 |
tmp_fields = ast_strdupa(template); |
| 960 |
while((value = ast_strsep(&tmp_fields, ',', AST_STRSEP_TRIM))) { |
| 961 |
res = AST_VECTOR_APPEND(field_templates, ast_strdup(value)); |
| 962 |
if (res != 0) { |
| 963 |
return NULL; |
| 964 |
} |
| 965 |
} |
| 966 |
} |
| 967 |
|
| 968 |
config = config_alloc(record_type, cdrel_backend_db, config_type, |
| 969 |
config_filename, category_name, template); |
| 970 |
if (!config) { |
| 971 |
return NULL; |
| 972 |
} |
| 973 |
|
| 974 |
config->busy_timeout = 1000; |
| 975 |
|
| 976 |
res = ast_string_field_set(config, db_table, |
| 977 |
S_OR(ast_variable_find(category, "table"), config->record_type == cdrel_record_cdr ? "cdr" : "cel")); |
| 978 |
if (res != 0) { |
| 979 |
return NULL; |
| 980 |
} |
| 981 |
|
| 982 |
|
| 983 |
if ((value = ast_variable_find(category, "busy_timeout")) != NULL) { |
| 984 |
if (ast_parse_arg(value, PARSE_INT32|PARSE_DEFAULT, &config->busy_timeout, 1000) != 0) { |
| 985 |
ast_log(LOG_WARNING, "%s->%s: Invalid busy_timeout value '%s' specified. Using 1000 instead.\n", |
| 986 |
cdrel_basename(config->config_filename), cdrel_basename(config->output_filename), value); |
| 987 |
} |
| 988 |
} |
| 989 |
|
| 990 |
|
| 991 |
value = ast_variable_find(category, "columns"); |
| 992 |
if (ast_strlen_zero(value)) { |
| 993 |
ast_log(LOG_WARNING, "%s->%s: The 'columns' parameter is missing", |
| 994 |
cdrel_basename(config->config_filename), cdrel_basename(config->output_filename)); |
| 995 |
return NULL; |
| 996 |
} |
| 997 |
|
| 998 |
if (load_database_columns(config, value, &column_count) != 0) { |
| 999 |
return NULL; |
| 1000 |
} |
| 1001 |
|
| 1002 |
if (AST_VECTOR_INIT(&config->fields, AST_VECTOR_SIZE(field_templates)) != 0) { |
| 1003 |
return NULL; |
| 1004 |
} |
| 1005 |
|
| 1006 |
if (load_fields(config, field_templates) != 0) { |
| 1007 |
return NULL; |
| 1008 |
} |
| 1009 |
|
| 1010 |
value_count = AST_VECTOR_SIZE(&config->fields); |
| 1011 |
|
| 1012 |
if (value_count != column_count) { |
| 1013 |
ast_log(LOG_WARNING, "%s->%s: There are %d columns but %d values\n", |
| 1014 |
cdrel_basename(config->config_filename), cdrel_basename(config->output_filename), |
| 1015 |
column_count, value_count); |
| 1016 |
return NULL; |
| 1017 |
} |
| 1018 |
|
| 1019 |
res = open_database(config); |
| 1020 |
if (res != 0) { |
| 1021 |
return NULL; |
| 1022 |
} |
| 1023 |
|
| 1024 |
ast_log(LOG_NOTICE, "%s->%s: Logging %s records to table '%s'\n", |
| 1025 |
cdrel_basename(config->config_filename), cdrel_basename(config->output_filename), |
| 1026 |
RECORD_TYPE_STR(config->record_type), |
| 1027 |
config->db_table); |
| 1028 |
|
| 1029 |
rtn_config = config; |
| 1030 |
config = NULL; |
| 1031 |
return rtn_config; |
| 1032 |
} |
| 1033 |
|
| 1034 |
|
| 1035 |
|
| 1036 |
|
| 1037 |
|
| 1038 |
|
| 1039 |
|
| 1040 |
|
| 1041 |
|
| 1042 |
|
| 1043 |
|
| 1044 |
|
| 1045 |
static int load_database_config_file(enum cdrel_record_type record_type, struct cdrel_configs *configs, |
| 1046 |
const char *config_filename, int reload) |
| 1047 |
{ |
| 1048 |
struct ast_config *cfg; |
| 1049 |
struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 }; |
| 1050 |
struct ast_category *category = NULL; |
| 1051 |
|
| 1052 |
ast_debug(1, "%s: %soading\n", config_filename, reload ? "Rel" : "L"); |
| 1053 |
cfg = ast_config_load(config_filename, config_flags); |
| 1054 |
if (!cfg || cfg == CONFIG_STATUS_FILEINVALID) { |
| 1055 |
ast_log(LOG_ERROR, "Unable to load %s. Not logging %ss to custom database\n", |
| 1056 |
config_filename, RECORD_TYPE_STR(record_type)); |
| 1057 |
return -1; |
| 1058 |
} else if (cfg == CONFIG_STATUS_FILEUNCHANGED) { |
| 1059 |
ast_debug(1, "%s: Config file unchanged, not reloading\n", config_filename); |
| 1060 |
return 1; |
| 1061 |
} |
| 1062 |
|
| 1063 |
while ((category = ast_category_browse_filtered(cfg, NULL, category, NULL))) { |
| 1064 |
struct cdrel_config *config = NULL; |
| 1065 |
|
| 1066 |
config = load_database_config(record_type, category, config_filename); |
| 1067 |
if (!config) { |
| 1068 |
continue; |
| 1069 |
} |
| 1070 |
|
| 1071 |
if (AST_VECTOR_APPEND(configs, config) != 0) { |
| 1072 |
config_free(config); |
| 1073 |
break; |
| 1074 |
} |
| 1075 |
} |
| 1076 |
|
| 1077 |
ast_config_destroy(cfg); |
| 1078 |
|
| 1079 |
ast_log(LOG_NOTICE, "%s: Loaded %d configs\n", config_filename, (int)AST_VECTOR_SIZE(configs)); |
| 1080 |
|
| 1081 |
|
| 1082 |
return AST_VECTOR_SIZE(configs) > 0 ? 0 : -1; |
| 1083 |
} |
| 1084 |
|
| 1085 |
|
| 1086 |
|
| 1087 |
|
| 1088 |
|
| 1089 |
|
| 1090 |
|
| 1091 |
|
| 1092 |
|
| 1093 |
|
| 1094 |
|
| 1095 |
static struct cdrel_config *load_text_file_legacy_config(enum cdrel_record_type record_type, |
| 1096 |
const char *config_filename, const char *output_filename, const char *template) |
| 1097 |
{ |
| 1098 |
struct cdrel_config *config = NULL; |
| 1099 |
int field_check_passed = 0; |
| 1100 |
int res = 0; |
| 1101 |
RAII_VAR(struct ast_vector_string *, fields, ast_calloc(1, sizeof(*fields)), field_template_vector_free); |
| 1102 |
|
| 1103 |
res = AST_VECTOR_INIT(fields, 25); |
| 1104 |
if (res != 0) { |
| 1105 |
return NULL; |
| 1106 |
} |
| 1107 |
|
| 1108 |
|
| 1109 |
|
| 1110 |
|
| 1111 |
|
| 1112 |
field_check_passed = parse_legacy_template(record_type, config_filename, |
| 1113 |
output_filename, template, fields); |
| 1114 |
|
| 1115 |
|
| 1116 |
|
| 1117 |
|
| 1118 |
if (!field_check_passed) { |
| 1119 |
config = config_alloc(record_type, cdrel_backend_text, cdrel_config_legacy, |
| 1120 |
config_filename, output_filename, template); |
| 1121 |
ast_log(LOG_NOTICE, "%s->%s: Logging legacy %s records\n", |
| 1122 |
cdrel_basename(config->config_filename), cdrel_basename(config->output_filename), |
| 1123 |
RECORD_TYPE_STR(config->record_type)); |
| 1124 |
return config; |
| 1125 |
} |
| 1126 |
|
| 1127 |
config = config_alloc(record_type, cdrel_backend_text, cdrel_config_advanced, |
| 1128 |
config_filename, output_filename, template); |
| 1129 |
if (!config) { |
| 1130 |
return NULL; |
| 1131 |
} |
| 1132 |
config->format_type = cdrel_format_dsv; |
| 1133 |
config->quote[0] = '"'; |
| 1134 |
config->quote_escape[0] = '"'; |
| 1135 |
config->separator[0] = ','; |
| 1136 |
config->quoting_method = cdrel_quoting_method_all; |
| 1137 |
|
| 1138 |
if (AST_VECTOR_INIT(&config->fields, AST_VECTOR_SIZE(fields)) != 0) { |
| 1139 |
return NULL; |
| 1140 |
} |
| 1141 |
|
| 1142 |
if (load_fields(config, fields) != 0) { |
| 1143 |
return NULL; |
| 1144 |
} |
| 1145 |
|
| 1146 |
ast_log(LOG_NOTICE, "%s->%s: Logging legacy %s records as advanced\n", |
| 1147 |
cdrel_basename(config->config_filename), cdrel_basename(config->output_filename), |
| 1148 |
RECORD_TYPE_STR(config->record_type)); |
| 1149 |
|
| 1150 |
return config; |
| 1151 |
} |
| 1152 |
|
| 1153 |
|
| 1154 |
|
| 1155 |
|
| 1156 |
|
| 1157 |
|
| 1158 |
|
| 1159 |
|
| 1160 |
|
| 1161 |
|
| 1162 |
static struct cdrel_config *load_text_file_advanced_config(enum cdrel_record_type record_type, |
| 1163 |
struct ast_category *category, const char *config_filename) |
| 1164 |
{ |
| 1165 |
const char *category_name = ast_category_get_name(category); |
| 1166 |
RAII_VAR(struct cdrel_config *, config, NULL, config_free); |
| 1167 |
struct cdrel_config *rtn_config = NULL; |
| 1168 |
const char *value; |
| 1169 |
int res = 0; |
| 1170 |
const char *fields_value = ast_variable_find(category, "fields"); |
| 1171 |
char *tmp_fields = NULL; |
| 1172 |
RAII_VAR(struct ast_vector_string *, fields, ast_calloc(1, sizeof(*fields)), field_template_vector_free); |
| 1173 |
|
| 1174 |
if (ast_strlen_zero(fields_value)) { |
| 1175 |
ast_log(LOG_WARNING, "%s->%s: Missing 'fields' parameter\n", |
| 1176 |
cdrel_basename(config_filename), category_name); |
| 1177 |
return NULL; |
| 1178 |
} |
| 1179 |
|
| 1180 |
config = config_alloc(record_type, cdrel_backend_text, cdrel_config_advanced, |
| 1181 |
config_filename, category_name, fields_value); |
| 1182 |
|
| 1183 |
value = ast_variable_find(category, "format"); |
| 1184 |
if (!ast_strlen_zero(value)) { |
| 1185 |
if (ast_strings_equal(value, "json")) { |
| 1186 |
config->format_type = cdrel_format_json; |
| 1187 |
config->separator[0] = ','; |
| 1188 |
config->quote[0] = '"'; |
| 1189 |
config->quote_escape[0] = '\\'; |
| 1190 |
config->quoting_method = cdrel_quoting_method_non_numeric; |
| 1191 |
} else if (ast_strings_equal(value, "dsv")) { |
| 1192 |
config->format_type = cdrel_format_dsv; |
| 1193 |
} else { |
| 1194 |
ast_log(LOG_WARNING, "%s->%s: Invalid format '%s'\n", |
| 1195 |
cdrel_basename(config->config_filename), cdrel_basename(config->output_filename), value); |
| 1196 |
return NULL; |
| 1197 |
} |
| 1198 |
} |
| 1199 |
|
| 1200 |
if (config->format_type != cdrel_format_json) { |
| 1201 |
value = ast_variable_find(category, "separator_character"); |
| 1202 |
if (!ast_strlen_zero(value)) { |
| 1203 |
ast_copy_string(config->separator, ast_unescape_c(ast_strdupa(value)), 2); |
| 1204 |
} |
| 1205 |
|
| 1206 |
value = ast_variable_find(category, "quote_character"); |
| 1207 |
if (!ast_strlen_zero(value)) { |
| 1208 |
ast_copy_string(config->quote, value, 2); |
| 1209 |
} |
| 1210 |
|
| 1211 |
value = ast_variable_find(category, "quote_escape_character"); |
| 1212 |
if (!ast_strlen_zero(value)) { |
| 1213 |
ast_copy_string(config->quote_escape, value, 2); |
| 1214 |
} |
| 1215 |
|
| 1216 |
value = ast_variable_find(category, "quoting_method"); |
| 1217 |
if (!ast_strlen_zero(value)) { |
| 1218 |
if (ast_strings_equal(value, "all")) { |
| 1219 |
config->quoting_method = cdrel_quoting_method_all; |
| 1220 |
} else if (ast_strings_equal(value, "minimal")) { |
| 1221 |
config->quoting_method = cdrel_quoting_method_minimal; |
| 1222 |
} else if (ast_strings_equal(value, "non_numeric")) { |
| 1223 |
config->quoting_method = cdrel_quoting_method_non_numeric; |
| 1224 |
} else if (ast_strings_equal(value, "none")) { |
| 1225 |
config->quoting_method = cdrel_quoting_method_none; |
| 1226 |
} else { |
| 1227 |
ast_log(LOG_WARNING, "%s->%s: Invalid quoting method '%s'\n", |
| 1228 |
cdrel_basename(config->config_filename), cdrel_basename(config->output_filename), value); |
| 1229 |
return NULL; |
| 1230 |
} |
| 1231 |
} |
| 1232 |
} |
| 1233 |
|
| 1234 |
res = AST_VECTOR_INIT(fields, 20); |
| 1235 |
if (res != 0) { |
| 1236 |
return NULL; |
| 1237 |
} |
| 1238 |
tmp_fields = ast_strdupa(fields_value); |
| 1239 |
while((value = ast_strsep(&tmp_fields, ',', AST_STRSEP_TRIM))) { |
| 1240 |
res = AST_VECTOR_APPEND(fields, ast_strdup(value)); |
| 1241 |
if (res != 0) { |
| 1242 |
return NULL; |
| 1243 |
} |
| 1244 |
} |
| 1245 |
|
| 1246 |
if (AST_VECTOR_INIT(&config->fields, AST_VECTOR_SIZE(fields)) != 0) { |
| 1247 |
return NULL; |
| 1248 |
} |
| 1249 |
|
| 1250 |
if (load_fields(config, fields) != 0) { |
| 1251 |
return NULL; |
| 1252 |
} |
| 1253 |
|
| 1254 |
ast_log(LOG_NOTICE, "%s->%s: Logging %s records\n", |
| 1255 |
cdrel_basename(config->config_filename), cdrel_basename(config->output_filename), |
| 1256 |
RECORD_TYPE_STR(config->record_type)); |
| 1257 |
|
| 1258 |
rtn_config = config; |
| 1259 |
config = NULL; |
| 1260 |
|
| 1261 |
return rtn_config; |
| 1262 |
} |
| 1263 |
|
| 1264 |
|
| 1265 |
|
| 1266 |
|
| 1267 |
|
| 1268 |
|
| 1269 |
|
| 1270 |
|
| 1271 |
|
| 1272 |
|
| 1273 |
|
| 1274 |
|
| 1275 |
static int load_text_file_legacy_mappings(enum cdrel_record_type record_type, |
| 1276 |
struct cdrel_configs *configs, struct ast_category *category, |
| 1277 |
const char *config_filename) |
| 1278 |
{ |
| 1279 |
struct ast_variable *var = NULL; |
| 1280 |
|
| 1281 |
for (var = ast_category_first(category); var; var = var->next) { |
| 1282 |
struct cdrel_config *config = NULL; |
| 1283 |
|
| 1284 |
if (ast_strlen_zero(var->name) || ast_strlen_zero(var->value)) { |
| 1285 |
ast_log(LOG_WARNING, "%s: %s mapping must have both a filename and a template at line %d\n", |
| 1286 |
cdrel_basename(config_filename), RECORD_TYPE_STR(config->record_type), var->lineno); |
| 1287 |
continue; |
| 1288 |
} |
| 1289 |
|
| 1290 |
config = load_text_file_legacy_config(record_type, config_filename, var->name, var->value); |
| 1291 |
if (!config) { |
| 1292 |
continue; |
| 1293 |
} |
| 1294 |
|
| 1295 |
if (AST_VECTOR_APPEND(configs, config) != 0) { |
| 1296 |
config_free(config); |
| 1297 |
return -1; |
| 1298 |
} |
| 1299 |
} |
| 1300 |
|
| 1301 |
return 0; |
| 1302 |
} |
| 1303 |
|
| 1304 |
|
| 1305 |
|
| 1306 |
|
| 1307 |
|
| 1308 |
|
| 1309 |
|
| 1310 |
|
| 1311 |
|
| 1312 |
|
| 1313 |
|
| 1314 |
static int load_text_file_config_file(enum cdrel_record_type record_type, |
| 1315 |
struct cdrel_configs *configs, const char *config_filename, int reload) |
| 1316 |
{ |
| 1317 |
struct ast_config *cfg; |
| 1318 |
struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 }; |
| 1319 |
struct ast_category *category = NULL; |
| 1320 |
|
| 1321 |
ast_debug(1, "%s: %soading\n", config_filename, reload ? "Rel" : "L"); |
| 1322 |
cfg = ast_config_load(config_filename, config_flags); |
| 1323 |
if (!cfg || cfg == CONFIG_STATUS_FILEINVALID) { |
| 1324 |
ast_log(LOG_ERROR, "Unable to load %s. Not logging %ss to custom files\n", |
| 1325 |
config_filename, RECORD_TYPE_STR(record_type)); |
| 1326 |
return -1; |
| 1327 |
} else if (cfg == CONFIG_STATUS_FILEUNCHANGED) { |
| 1328 |
ast_debug(1, "%s: Config file unchanged, not reloading\n", config_filename); |
| 1329 |
return 1; |
| 1330 |
} |
| 1331 |
|
| 1332 |
while ((category = ast_category_browse_filtered(cfg, NULL, category, NULL))) { |
| 1333 |
const char *category_name = ast_category_get_name(category); |
| 1334 |
|
| 1335 |
if (ast_strings_equal(category_name, "mappings")) { |
| 1336 |
load_text_file_legacy_mappings(record_type, configs, category, config_filename); |
| 1337 |
} else { |
| 1338 |
struct cdrel_config * config = load_text_file_advanced_config(record_type, category, |
| 1339 |
config_filename); |
| 1340 |
if (!config) { |
| 1341 |
continue; |
| 1342 |
} |
| 1343 |
if (AST_VECTOR_APPEND(configs, config) != 0) { |
| 1344 |
config_free(config); |
| 1345 |
return -1; |
| 1346 |
} |
| 1347 |
} |
| 1348 |
} |
| 1349 |
|
| 1350 |
ast_config_destroy(cfg); |
| 1351 |
|
| 1352 |
ast_log(LOG_NOTICE, "%s: Loaded %d configs\n", config_filename, (int)AST_VECTOR_SIZE(configs)); |
| 1353 |
|
| 1354 |
|
| 1355 |
return AST_VECTOR_SIZE(configs) > 0 ? 0 : -1; |
| 1356 |
} |
| 1357 |
|
| 1358 |
static int register_backend(enum cdrel_record_type record_type, const char *backend_name, void *log_cb) |
| 1359 |
{ |
| 1360 |
switch(record_type) { |
| 1361 |
case cdrel_record_cdr: |
| 1362 |
return ast_cdr_register(backend_name, "", log_cb); |
| 1363 |
case cdrel_backend_db: |
| 1364 |
return ast_cel_backend_register(backend_name, log_cb); |
| 1365 |
default: |
| 1366 |
return -1; |
| 1367 |
} |
| 1368 |
} |
| 1369 |
|
| 1370 |
static int unregister_backend(enum cdrel_record_type record_type, const char *backend_name) |
| 1371 |
{ |
| 1372 |
switch(record_type) { |
| 1373 |
case cdrel_record_cdr: |
| 1374 |
return ast_cdr_unregister(backend_name); |
| 1375 |
case cdrel_record_cel: |
| 1376 |
return ast_cel_backend_unregister(backend_name); |
| 1377 |
default: |
| 1378 |
return -1; |
| 1379 |
} |
| 1380 |
} |
| 1381 |
|
| 1382 |
static int load_config_file(enum cdrel_backend_type output_type, enum cdrel_record_type record_type, |
| 1383 |
struct cdrel_configs *configs, const char *filename, int reload) |
| 1384 |
{ |
| 1385 |
switch(output_type) { |
| 1386 |
case cdrel_backend_text: |
| 1387 |
return load_text_file_config_file(record_type, configs, filename, reload); |
| 1388 |
case cdrel_backend_db: |
| 1389 |
return load_database_config_file(record_type, configs, filename, reload); |
| 1390 |
default: |
| 1391 |
return -1; |
| 1392 |
} |
| 1393 |
} |
| 1394 |
|
| 1395 |
int cdrel_reload_module(enum cdrel_backend_type output_type, enum cdrel_record_type record_type, |
| 1396 |
struct cdrel_configs **configs, const char *filename) |
| 1397 |
{ |
| 1398 |
int res = 0; |
| 1399 |
struct cdrel_configs *old_configs = *configs; |
| 1400 |
struct cdrel_configs *new_configs = NULL; |
| 1401 |
|
| 1402 |
|
| 1403 |
|
| 1404 |
|
| 1405 |
|
| 1406 |
new_configs = ast_malloc(sizeof(*new_configs)); |
| 1407 |
if (!new_configs) { |
| 1408 |
return AST_MODULE_LOAD_DECLINE; |
| 1409 |
} |
| 1410 |
|
| 1411 |
if (AST_VECTOR_INIT(new_configs, AST_VECTOR_SIZE(old_configs)) != 0) { |
| 1412 |
return AST_MODULE_LOAD_DECLINE; |
| 1413 |
} |
| 1414 |
|
| 1415 |
res = load_config_file(output_type, record_type, new_configs, filename, 1); |
| 1416 |
if (res < 0) { |
| 1417 |
AST_VECTOR_RESET(new_configs, config_free); |
| 1418 |
AST_VECTOR_PTR_FREE(new_configs); |
| 1419 |
return AST_MODULE_LOAD_DECLINE; |
| 1420 |
} |
| 1421 |
|
| 1422 |
if (res == 0) { |
| 1423 |
|
| 1424 |
*configs = new_configs; |
| 1425 |
|
| 1426 |
|
| 1427 |
AST_VECTOR_RESET(old_configs, config_free); |
| 1428 |
AST_VECTOR_PTR_FREE(old_configs); |
| 1429 |
} |
| 1430 |
|
| 1431 |
return AST_MODULE_LOAD_SUCCESS; |
| 1432 |
} |
| 1433 |
|
| 1434 |
struct cdrel_configs *cdrel_load_module(enum cdrel_backend_type backend_type, |
| 1435 |
enum cdrel_record_type record_type, const char *filename, const char *backend_name, |
| 1436 |
void *log_cb) |
| 1437 |
{ |
| 1438 |
struct cdrel_configs *configs = ast_calloc(1, sizeof(*configs)); |
| 1439 |
if (!configs) { |
| 1440 |
return NULL; |
| 1441 |
} |
| 1442 |
ast_debug(1, "Loading %s %s\n", RECORD_TYPE_STR(record_type), MODULE_TYPE_STR(backend_type)); |
| 1443 |
|
| 1444 |
if (AST_VECTOR_INIT(configs, 5) != 0) { |
| 1445 |
cdrel_unload_module(backend_type, record_type, configs, backend_name); |
| 1446 |
return NULL; |
| 1447 |
} |
| 1448 |
|
| 1449 |
if (load_config_file(backend_type, record_type, configs, filename, 0) != 0) { |
| 1450 |
cdrel_unload_module(backend_type, record_type, configs, backend_name); |
| 1451 |
return NULL; |
| 1452 |
} |
| 1453 |
|
| 1454 |
if (register_backend(record_type, backend_name, log_cb)) { |
| 1455 |
cdrel_unload_module(backend_type, record_type, configs, backend_name); |
| 1456 |
return NULL; |
| 1457 |
} |
| 1458 |
|
| 1459 |
return configs; |
| 1460 |
} |
| 1461 |
|
| 1462 |
int cdrel_unload_module(enum cdrel_backend_type backend_type, enum cdrel_record_type record_type, |
| 1463 |
struct cdrel_configs *configs, const char *backend_name) |
| 1464 |
{ |
| 1465 |
if (unregister_backend(record_type, backend_name) != 0) { |
| 1466 |
return -1; |
| 1467 |
} |
| 1468 |
|
| 1469 |
AST_VECTOR_RESET(configs, config_free); |
| 1470 |
AST_VECTOR_PTR_FREE(configs); |
| 1471 |
|
| 1472 |
return 0; |
| 1473 |
} |