| 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 |
|
| 34 |
#include "asterisk/module.h" |
| 35 |
#include "asterisk/datastore.h" |
| 36 |
#include "asterisk/pbx.h" |
| 37 |
#include "asterisk/manager.h" |
| 38 |
#include "asterisk/strings.h" |
| 39 |
#include "asterisk/astobj2.h" |
| 40 |
#include "asterisk/vector.h" |
| 41 |
#include "asterisk/app.h" |
| 42 |
#include "asterisk/taskprocessor.h" |
| 43 |
#include "asterisk/message.h" |
| 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 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
struct msg_data { |
| 247 |
AST_DECLARE_STRING_FIELDS( |
| 248 |
AST_STRING_FIELD(name); |
| 249 |
AST_STRING_FIELD(value); |
| 250 |
); |
| 251 |
unsigned int send; |
| 252 |
}; |
| 253 |
|
| 254 |
AST_LIST_HEAD_NOLOCK(outhead, msg_data); |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
struct ast_msg { |
| 260 |
AST_DECLARE_STRING_FIELDS( |
| 261 |
|
| 262 |
AST_STRING_FIELD(to); |
| 263 |
|
| 264 |
AST_STRING_FIELD(from); |
| 265 |
|
| 266 |
AST_STRING_FIELD(body); |
| 267 |
|
| 268 |
AST_STRING_FIELD(context); |
| 269 |
|
| 270 |
AST_STRING_FIELD(exten); |
| 271 |
|
| 272 |
AST_STRING_FIELD(endpoint); |
| 273 |
|
| 274 |
AST_STRING_FIELD(tech); |
| 275 |
); |
| 276 |
|
| 277 |
struct ao2_container *vars; |
| 278 |
}; |
| 279 |
|
| 280 |
|
| 281 |
static ast_rwlock_t msg_techs_lock; |
| 282 |
|
| 283 |
|
| 284 |
AST_VECTOR(, const struct ast_msg_tech *) msg_techs; |
| 285 |
|
| 286 |
|
| 287 |
static ast_rwlock_t msg_handlers_lock; |
| 288 |
|
| 289 |
|
| 290 |
AST_VECTOR(, const struct ast_msg_handler *) msg_handlers; |
| 291 |
|
| 292 |
static struct ast_taskprocessor *msg_q_tp; |
| 293 |
|
| 294 |
static const char app_msg_send[] = "MessageSend"; |
| 295 |
|
| 296 |
static void msg_ds_destroy(void *data); |
| 297 |
|
| 298 |
static const struct ast_datastore_info msg_datastore = { |
| 299 |
.type = "message", |
| 300 |
.destroy = msg_ds_destroy, |
| 301 |
}; |
| 302 |
|
| 303 |
static int msg_func_read(struct ast_channel *chan, const char *function, |
| 304 |
char *data, char *buf, size_t len); |
| 305 |
static int msg_func_write(struct ast_channel *chan, const char *function, |
| 306 |
char *data, const char *value); |
| 307 |
|
| 308 |
static struct ast_custom_function msg_function = { |
| 309 |
.name = "MESSAGE", |
| 310 |
.read = msg_func_read, |
| 311 |
.write = msg_func_write, |
| 312 |
}; |
| 313 |
|
| 314 |
static int msg_data_func_read(struct ast_channel *chan, const char *function, |
| 315 |
char *data, char *buf, size_t len); |
| 316 |
static int msg_data_func_write(struct ast_channel *chan, const char *function, |
| 317 |
char *data, const char *value); |
| 318 |
|
| 319 |
static struct ast_custom_function msg_data_function = { |
| 320 |
.name = "MESSAGE_DATA", |
| 321 |
.read = msg_data_func_read, |
| 322 |
.write = msg_data_func_write, |
| 323 |
}; |
| 324 |
|
| 325 |
static struct ast_frame *chan_msg_read(struct ast_channel *chan); |
| 326 |
static int chan_msg_write(struct ast_channel *chan, struct ast_frame *fr); |
| 327 |
static int chan_msg_indicate(struct ast_channel *chan, int condition, |
| 328 |
const void *data, size_t datalen); |
| 329 |
static int chan_msg_send_digit_begin(struct ast_channel *chan, char digit); |
| 330 |
static int chan_msg_send_digit_end(struct ast_channel *chan, char digit, |
| 331 |
unsigned int duration); |
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
static struct ast_channel_tech msg_chan_tech_hack = { |
| 341 |
.type = "Message", |
| 342 |
.description = "Internal Text Message Processing", |
| 343 |
.read = chan_msg_read, |
| 344 |
.write = chan_msg_write, |
| 345 |
.indicate = chan_msg_indicate, |
| 346 |
.send_digit_begin = chan_msg_send_digit_begin, |
| 347 |
.send_digit_end = chan_msg_send_digit_end, |
| 348 |
}; |
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
static struct ast_frame *chan_msg_read(struct ast_channel *chan) |
| 359 |
{ |
| 360 |
return &ast_null_frame; |
| 361 |
} |
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
static int chan_msg_write(struct ast_channel *chan, struct ast_frame *fr) |
| 370 |
{ |
| 371 |
return 0; |
| 372 |
} |
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
static int chan_msg_indicate(struct ast_channel *chan, int condition, |
| 385 |
const void *data, size_t datalen) |
| 386 |
{ |
| 387 |
return 0; |
| 388 |
} |
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
static int chan_msg_send_digit_begin(struct ast_channel *chan, char digit) |
| 399 |
{ |
| 400 |
return 0; |
| 401 |
} |
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
static int chan_msg_send_digit_end(struct ast_channel *chan, char digit, |
| 412 |
unsigned int duration) |
| 413 |
{ |
| 414 |
return 0; |
| 415 |
} |
| 416 |
|
| 417 |
static void msg_ds_destroy(void *data) |
| 418 |
{ |
| 419 |
struct ast_msg *msg = data; |
| 420 |
|
| 421 |
ao2_ref(msg, -1); |
| 422 |
} |
| 423 |
|
| 424 |
static int msg_data_cmp_fn(void *obj, void *arg, int flags) |
| 425 |
{ |
| 426 |
const struct msg_data *one = obj, *two = arg; |
| 427 |
return !strcasecmp(one->name, two->name) ? CMP_MATCH | CMP_STOP : 0; |
| 428 |
} |
| 429 |
|
| 430 |
static void msg_data_destructor(void *obj) |
| 431 |
{ |
| 432 |
struct msg_data *data = obj; |
| 433 |
ast_string_field_free_memory(data); |
| 434 |
} |
| 435 |
|
| 436 |
static void msg_destructor(void *obj) |
| 437 |
{ |
| 438 |
struct ast_msg *msg = obj; |
| 439 |
|
| 440 |
ast_string_field_free_memory(msg); |
| 441 |
ao2_cleanup(msg->vars); |
| 442 |
} |
| 443 |
|
| 444 |
struct ast_msg *ast_msg_alloc(void) |
| 445 |
{ |
| 446 |
struct ast_msg *msg; |
| 447 |
|
| 448 |
if (!(msg = ao2_alloc(sizeof(*msg), msg_destructor))) { |
| 449 |
return NULL; |
| 450 |
} |
| 451 |
|
| 452 |
if (ast_string_field_init(msg, 128)) { |
| 453 |
ao2_ref(msg, -1); |
| 454 |
return NULL; |
| 455 |
} |
| 456 |
|
| 457 |
msg->vars = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_MUTEX, 0, |
| 458 |
NULL, msg_data_cmp_fn); |
| 459 |
if (!msg->vars) { |
| 460 |
ao2_ref(msg, -1); |
| 461 |
return NULL; |
| 462 |
} |
| 463 |
ast_string_field_set(msg, context, "default"); |
| 464 |
|
| 465 |
return msg; |
| 466 |
} |
| 467 |
|
| 468 |
struct ast_msg *ast_msg_ref(struct ast_msg *msg) |
| 469 |
{ |
| 470 |
ao2_ref(msg, 1); |
| 471 |
return msg; |
| 472 |
} |
| 473 |
|
| 474 |
struct ast_msg *ast_msg_destroy(struct ast_msg *msg) |
| 475 |
{ |
| 476 |
ao2_ref(msg, -1); |
| 477 |
return NULL; |
| 478 |
} |
| 479 |
|
| 480 |
int ast_msg_set_to(struct ast_msg *msg, const char *fmt, ...) |
| 481 |
{ |
| 482 |
va_list ap; |
| 483 |
|
| 484 |
va_start(ap, fmt); |
| 485 |
ast_string_field_build_va(msg, to, fmt, ap); |
| 486 |
va_end(ap); |
| 487 |
|
| 488 |
return 0; |
| 489 |
} |
| 490 |
|
| 491 |
int ast_msg_set_from(struct ast_msg *msg, const char *fmt, ...) |
| 492 |
{ |
| 493 |
va_list ap; |
| 494 |
|
| 495 |
va_start(ap, fmt); |
| 496 |
ast_string_field_build_va(msg, from, fmt, ap); |
| 497 |
va_end(ap); |
| 498 |
|
| 499 |
return 0; |
| 500 |
} |
| 501 |
|
| 502 |
int ast_msg_set_body(struct ast_msg *msg, const char *fmt, ...) |
| 503 |
{ |
| 504 |
va_list ap; |
| 505 |
|
| 506 |
va_start(ap, fmt); |
| 507 |
ast_string_field_build_va(msg, body, fmt, ap); |
| 508 |
va_end(ap); |
| 509 |
|
| 510 |
return 0; |
| 511 |
} |
| 512 |
|
| 513 |
int ast_msg_set_context(struct ast_msg *msg, const char *fmt, ...) |
| 514 |
{ |
| 515 |
va_list ap; |
| 516 |
|
| 517 |
va_start(ap, fmt); |
| 518 |
ast_string_field_build_va(msg, context, fmt, ap); |
| 519 |
va_end(ap); |
| 520 |
|
| 521 |
return 0; |
| 522 |
} |
| 523 |
|
| 524 |
int ast_msg_set_exten(struct ast_msg *msg, const char *fmt, ...) |
| 525 |
{ |
| 526 |
va_list ap; |
| 527 |
|
| 528 |
va_start(ap, fmt); |
| 529 |
ast_string_field_build_va(msg, exten, fmt, ap); |
| 530 |
va_end(ap); |
| 531 |
|
| 532 |
return 0; |
| 533 |
} |
| 534 |
|
| 535 |
int ast_msg_set_tech(struct ast_msg *msg, const char *fmt, ...) |
| 536 |
{ |
| 537 |
va_list ap; |
| 538 |
|
| 539 |
va_start(ap, fmt); |
| 540 |
ast_string_field_build_va(msg, tech, fmt, ap); |
| 541 |
va_end(ap); |
| 542 |
|
| 543 |
return 0; |
| 544 |
} |
| 545 |
|
| 546 |
int ast_msg_set_endpoint(struct ast_msg *msg, const char *fmt, ...) |
| 547 |
{ |
| 548 |
va_list ap; |
| 549 |
|
| 550 |
va_start(ap, fmt); |
| 551 |
ast_string_field_build_va(msg, endpoint, fmt, ap); |
| 552 |
va_end(ap); |
| 553 |
|
| 554 |
return 0; |
| 555 |
} |
| 556 |
|
| 557 |
const char *ast_msg_get_body(const struct ast_msg *msg) |
| 558 |
{ |
| 559 |
return msg->body; |
| 560 |
} |
| 561 |
|
| 562 |
const char *ast_msg_get_from(const struct ast_msg *msg) |
| 563 |
{ |
| 564 |
return msg->from; |
| 565 |
} |
| 566 |
|
| 567 |
const char *ast_msg_get_to(const struct ast_msg *msg) |
| 568 |
{ |
| 569 |
return msg->to; |
| 570 |
} |
| 571 |
|
| 572 |
const char *ast_msg_get_tech(const struct ast_msg *msg) |
| 573 |
{ |
| 574 |
return msg->tech; |
| 575 |
} |
| 576 |
|
| 577 |
const char *ast_msg_get_endpoint(const struct ast_msg *msg) |
| 578 |
{ |
| 579 |
return msg->endpoint; |
| 580 |
} |
| 581 |
|
| 582 |
static struct msg_data *msg_data_alloc(void) |
| 583 |
{ |
| 584 |
struct msg_data *data; |
| 585 |
|
| 586 |
if (!(data = ao2_alloc(sizeof(*data), msg_data_destructor))) { |
| 587 |
return NULL; |
| 588 |
} |
| 589 |
|
| 590 |
if (ast_string_field_init(data, 32)) { |
| 591 |
ao2_ref(data, -1); |
| 592 |
return NULL; |
| 593 |
} |
| 594 |
|
| 595 |
return data; |
| 596 |
} |
| 597 |
|
| 598 |
static struct msg_data *msg_data_find(struct ao2_container *vars, const char *name) |
| 599 |
{ |
| 600 |
struct msg_data tmp = { |
| 601 |
.name = name, |
| 602 |
}; |
| 603 |
return ao2_find(vars, &tmp, OBJ_POINTER); |
| 604 |
} |
| 605 |
|
| 606 |
static int msg_set_var_full(struct ast_msg *msg, const char *name, const char *value, unsigned int outbound) |
| 607 |
{ |
| 608 |
struct msg_data *data; |
| 609 |
|
| 610 |
if (!(data = msg_data_find(msg->vars, name))) { |
| 611 |
if (ast_strlen_zero(value)) { |
| 612 |
return 0; |
| 613 |
} |
| 614 |
if (!(data = msg_data_alloc())) { |
| 615 |
return -1; |
| 616 |
}; |
| 617 |
|
| 618 |
ast_string_field_set(data, name, name); |
| 619 |
ast_string_field_set(data, value, value); |
| 620 |
data->send = outbound; |
| 621 |
ao2_link(msg->vars, data); |
| 622 |
} else { |
| 623 |
if (ast_strlen_zero(value)) { |
| 624 |
ao2_unlink(msg->vars, data); |
| 625 |
} else { |
| 626 |
ast_string_field_set(data, value, value); |
| 627 |
data->send = outbound; |
| 628 |
} |
| 629 |
} |
| 630 |
|
| 631 |
ao2_ref(data, -1); |
| 632 |
|
| 633 |
return 0; |
| 634 |
} |
| 635 |
|
| 636 |
int ast_msg_set_var_outbound(struct ast_msg *msg, const char *name, const char *value) |
| 637 |
{ |
| 638 |
return msg_set_var_full(msg, name, value, 1); |
| 639 |
} |
| 640 |
|
| 641 |
int ast_msg_set_var(struct ast_msg *msg, const char *name, const char *value) |
| 642 |
{ |
| 643 |
return msg_set_var_full(msg, name, value, 0); |
| 644 |
} |
| 645 |
|
| 646 |
const char *ast_msg_get_var(struct ast_msg *msg, const char *name) |
| 647 |
{ |
| 648 |
struct msg_data *data; |
| 649 |
const char *val = NULL; |
| 650 |
|
| 651 |
if (!(data = msg_data_find(msg->vars, name))) { |
| 652 |
return NULL; |
| 653 |
} |
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
val = data->value; |
| 660 |
ao2_ref(data, -1); |
| 661 |
|
| 662 |
return val; |
| 663 |
} |
| 664 |
|
| 665 |
struct ast_msg_var_iterator { |
| 666 |
struct ao2_iterator iter; |
| 667 |
struct msg_data *current_used; |
| 668 |
}; |
| 669 |
|
| 670 |
struct ast_msg_var_iterator *ast_msg_var_iterator_init(const struct ast_msg *msg) |
| 671 |
{ |
| 672 |
struct ast_msg_var_iterator *iter; |
| 673 |
|
| 674 |
iter = ast_calloc(1, sizeof(*iter)); |
| 675 |
if (!iter) { |
| 676 |
return NULL; |
| 677 |
} |
| 678 |
|
| 679 |
iter->iter = ao2_iterator_init(msg->vars, 0); |
| 680 |
|
| 681 |
return iter; |
| 682 |
} |
| 683 |
|
| 684 |
static int ast_msg_var_iterator_get_next(const struct ast_msg *msg, |
| 685 |
struct ast_msg_var_iterator *iter, const char **name, const char **value, |
| 686 |
unsigned int send) |
| 687 |
{ |
| 688 |
struct msg_data *data; |
| 689 |
|
| 690 |
if (!iter) { |
| 691 |
return 0; |
| 692 |
} |
| 693 |
|
| 694 |
|
| 695 |
while ((data = ao2_iterator_next(&iter->iter)) && (data->send != send)) { |
| 696 |
ao2_ref(data, -1); |
| 697 |
} |
| 698 |
|
| 699 |
if (!data) { |
| 700 |
return 0; |
| 701 |
} |
| 702 |
|
| 703 |
if (data->send == send) { |
| 704 |
*name = data->name; |
| 705 |
*value = data->value; |
| 706 |
} |
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
iter->current_used = data; |
| 711 |
|
| 712 |
return 1; |
| 713 |
} |
| 714 |
|
| 715 |
int ast_msg_var_iterator_next(const struct ast_msg *msg, struct ast_msg_var_iterator *iter, const char **name, const char **value) |
| 716 |
{ |
| 717 |
return ast_msg_var_iterator_get_next(msg, iter, name, value, 1); |
| 718 |
} |
| 719 |
|
| 720 |
int ast_msg_var_iterator_next_received(const struct ast_msg *msg, |
| 721 |
struct ast_msg_var_iterator *iter, const char **name, const char **value) |
| 722 |
{ |
| 723 |
return ast_msg_var_iterator_get_next(msg, iter, name, value, 0); |
| 724 |
} |
| 725 |
|
| 726 |
void ast_msg_var_unref_current(struct ast_msg_var_iterator *iter) |
| 727 |
{ |
| 728 |
ao2_cleanup(iter->current_used); |
| 729 |
iter->current_used = NULL; |
| 730 |
} |
| 731 |
|
| 732 |
void ast_msg_var_iterator_destroy(struct ast_msg_var_iterator *iter) |
| 733 |
{ |
| 734 |
if (iter) { |
| 735 |
ao2_iterator_destroy(&iter->iter); |
| 736 |
ast_msg_var_unref_current(iter); |
| 737 |
ast_free(iter); |
| 738 |
} |
| 739 |
} |
| 740 |
|
| 741 |
static struct ast_channel *create_msg_q_chan(void) |
| 742 |
{ |
| 743 |
struct ast_channel *chan; |
| 744 |
struct ast_datastore *ds; |
| 745 |
|
| 746 |
chan = ast_channel_alloc(1, AST_STATE_UP, |
| 747 |
NULL, NULL, NULL, |
| 748 |
NULL, NULL, NULL, NULL, 0, |
| 749 |
"%s", "Message/ast_msg_queue"); |
| 750 |
|
| 751 |
if (!chan) { |
| 752 |
return NULL; |
| 753 |
} |
| 754 |
|
| 755 |
if (ast_opt_hide_messaging_ami_events) { |
| 756 |
msg_chan_tech_hack.properties |= AST_CHAN_TP_INTERNAL; |
| 757 |
} |
| 758 |
|
| 759 |
ast_channel_tech_set(chan, &msg_chan_tech_hack); |
| 760 |
ast_channel_unlock(chan); |
| 761 |
ast_channel_unlink(chan); |
| 762 |
|
| 763 |
if (!(ds = ast_datastore_alloc(&msg_datastore, NULL))) { |
| 764 |
ast_hangup(chan); |
| 765 |
return NULL; |
| 766 |
} |
| 767 |
|
| 768 |
ast_channel_lock(chan); |
| 769 |
ast_channel_datastore_add(chan, ds); |
| 770 |
ast_channel_unlock(chan); |
| 771 |
|
| 772 |
return chan; |
| 773 |
} |
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
static void msg_route(struct ast_channel *chan, struct ast_msg *msg) |
| 783 |
{ |
| 784 |
struct ast_pbx_args pbx_args; |
| 785 |
|
| 786 |
ast_explicit_goto(chan, msg->context, S_OR(msg->exten, "s"), 1); |
| 787 |
|
| 788 |
memset(&pbx_args, 0, sizeof(pbx_args)); |
| 789 |
pbx_args.no_hangup_chan = 1, |
| 790 |
ast_pbx_run_args(chan, &pbx_args); |
| 791 |
} |
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
static void chan_cleanup(struct ast_channel *chan) |
| 801 |
{ |
| 802 |
struct ast_datastore *msg_ds, *ds; |
| 803 |
struct varshead *headp; |
| 804 |
struct ast_var_t *vardata; |
| 805 |
struct ast_frame *cur; |
| 806 |
|
| 807 |
ast_channel_lock(chan); |
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
if ((msg_ds = ast_channel_datastore_find(chan, &msg_datastore, NULL)) && msg_ds->data) { |
| 814 |
ast_channel_datastore_remove(chan, msg_ds); |
| 815 |
ao2_ref(msg_ds->data, -1); |
| 816 |
msg_ds->data = NULL; |
| 817 |
} |
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
while ((ds = AST_LIST_REMOVE_HEAD(ast_channel_datastores(chan), entry))) { |
| 823 |
ast_datastore_free(ds); |
| 824 |
} |
| 825 |
|
| 826 |
|
| 827 |
|
| 828 |
|
| 829 |
headp = ast_channel_varshead(chan); |
| 830 |
while ((vardata = AST_LIST_REMOVE_HEAD(headp, entries))) { |
| 831 |
ast_var_delete(vardata); |
| 832 |
} |
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
while ((cur = AST_LIST_REMOVE_HEAD(ast_channel_readq(chan), frame_list))) { |
| 838 |
ast_frfree(cur); |
| 839 |
} |
| 840 |
|
| 841 |
|
| 842 |
|
| 843 |
|
| 844 |
if (msg_ds) { |
| 845 |
ast_channel_datastore_add(chan, msg_ds); |
| 846 |
} |
| 847 |
|
| 848 |
|
| 849 |
|
| 850 |
|
| 851 |
ast_channel_clear_softhangup(chan, AST_SOFTHANGUP_ALL); |
| 852 |
|
| 853 |
|
| 854 |
|
| 855 |
|
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
ast_channel_internal_alert_flush(chan); |
| 860 |
|
| 861 |
ast_channel_unlock(chan); |
| 862 |
} |
| 863 |
|
| 864 |
static void destroy_msg_q_chan(void *data) |
| 865 |
{ |
| 866 |
struct ast_channel **chan = data; |
| 867 |
|
| 868 |
if (!*chan) { |
| 869 |
return; |
| 870 |
} |
| 871 |
|
| 872 |
ast_channel_release(*chan); |
| 873 |
} |
| 874 |
|
| 875 |
AST_THREADSTORAGE_CUSTOM(msg_q_chan, NULL, destroy_msg_q_chan); |
| 876 |
|
| 877 |
|
| 878 |
static int dialplan_handle_msg_cb(struct ast_msg *msg) |
| 879 |
{ |
| 880 |
struct ast_channel **chan_p, *chan; |
| 881 |
struct ast_datastore *ds; |
| 882 |
|
| 883 |
if (!(chan_p = ast_threadstorage_get(&msg_q_chan, sizeof(struct ast_channel *)))) { |
| 884 |
return -1; |
| 885 |
} |
| 886 |
if (!*chan_p) { |
| 887 |
if (!(*chan_p = create_msg_q_chan())) { |
| 888 |
return -1; |
| 889 |
} |
| 890 |
} |
| 891 |
chan = *chan_p; |
| 892 |
|
| 893 |
ast_channel_lock(chan); |
| 894 |
if (!(ds = ast_channel_datastore_find(chan, &msg_datastore, NULL))) { |
| 895 |
ast_channel_unlock(chan); |
| 896 |
return -1; |
| 897 |
} |
| 898 |
ao2_ref(msg, +1); |
| 899 |
ds->data = msg; |
| 900 |
ast_channel_unlock(chan); |
| 901 |
|
| 902 |
msg_route(chan, msg); |
| 903 |
chan_cleanup(chan); |
| 904 |
|
| 905 |
return 0; |
| 906 |
} |
| 907 |
|
| 908 |
|
| 909 |
static int dialplan_has_destination_cb(const struct ast_msg *msg) |
| 910 |
{ |
| 911 |
if (ast_strlen_zero(msg->context)) { |
| 912 |
return 0; |
| 913 |
} |
| 914 |
|
| 915 |
return ast_exists_extension(NULL, msg->context, S_OR(msg->exten, "s"), 1, NULL); |
| 916 |
} |
| 917 |
|
| 918 |
static struct ast_msg_handler dialplan_msg_handler = { |
| 919 |
.name = "dialplan", |
| 920 |
.handle_msg = dialplan_handle_msg_cb, |
| 921 |
.has_destination = dialplan_has_destination_cb, |
| 922 |
}; |
| 923 |
|
| 924 |
|
| 925 |
|
| 926 |
|
| 927 |
|
| 928 |
|
| 929 |
|
| 930 |
|
| 931 |
|
| 932 |
|
| 933 |
static int msg_q_cb(void *data) |
| 934 |
{ |
| 935 |
struct ast_msg *msg = data; |
| 936 |
int res = 1; |
| 937 |
int i; |
| 938 |
|
| 939 |
ast_rwlock_rdlock(&msg_handlers_lock); |
| 940 |
for (i = 0; i < AST_VECTOR_SIZE(&msg_handlers); i++) { |
| 941 |
const struct ast_msg_handler *handler = AST_VECTOR_GET(&msg_handlers, i); |
| 942 |
|
| 943 |
if (!handler->has_destination(msg)) { |
| 944 |
ast_debug(5, "Handler %s doesn't want message, moving on\n", handler->name); |
| 945 |
continue; |
| 946 |
} |
| 947 |
|
| 948 |
ast_debug(5, "Dispatching message to %s handler\n", handler->name); |
| 949 |
res &= handler->handle_msg(msg); |
| 950 |
} |
| 951 |
ast_rwlock_unlock(&msg_handlers_lock); |
| 952 |
|
| 953 |
if (res != 0) { |
| 954 |
ast_log(LOG_WARNING, "No handler processed message from %s to %s\n", |
| 955 |
S_OR(msg->from, "<unknown>"), S_OR(msg->to, "<unknown>")); |
| 956 |
} |
| 957 |
|
| 958 |
ao2_ref(msg, -1); |
| 959 |
|
| 960 |
return res; |
| 961 |
} |
| 962 |
|
| 963 |
int ast_msg_has_destination(const struct ast_msg *msg) |
| 964 |
{ |
| 965 |
int i; |
| 966 |
int result = 0; |
| 967 |
|
| 968 |
ast_rwlock_rdlock(&msg_handlers_lock); |
| 969 |
for (i = 0; i < AST_VECTOR_SIZE(&msg_handlers); i++) { |
| 970 |
const struct ast_msg_handler *handler = AST_VECTOR_GET(&msg_handlers, i); |
| 971 |
|
| 972 |
ast_debug(5, "Seeing if %s can handle message\n", handler->name); |
| 973 |
if (handler->has_destination(msg)) { |
| 974 |
ast_debug(5, "%s can handle message\n", handler->name); |
| 975 |
result = 1; |
| 976 |
break; |
| 977 |
} |
| 978 |
} |
| 979 |
ast_rwlock_unlock(&msg_handlers_lock); |
| 980 |
|
| 981 |
return result; |
| 982 |
} |
| 983 |
|
| 984 |
int ast_msg_queue(struct ast_msg *msg) |
| 985 |
{ |
| 986 |
int res; |
| 987 |
res = ast_taskprocessor_push(msg_q_tp, msg_q_cb, msg); |
| 988 |
if (res == -1) { |
| 989 |
ao2_ref(msg, -1); |
| 990 |
} |
| 991 |
|
| 992 |
return res; |
| 993 |
} |
| 994 |
|
| 995 |
|
| 996 |
|
| 997 |
|
| 998 |
|
| 999 |
|
| 1000 |
|
| 1001 |
|
| 1002 |
|
| 1003 |
|
| 1004 |
|
| 1005 |
|
| 1006 |
static struct ast_datastore *msg_datastore_find_or_create(struct ast_channel *chan) |
| 1007 |
{ |
| 1008 |
struct ast_datastore *ds; |
| 1009 |
|
| 1010 |
if ((ds = ast_channel_datastore_find(chan, &msg_datastore, NULL))) { |
| 1011 |
return ds; |
| 1012 |
} |
| 1013 |
|
| 1014 |
if (!(ds = ast_datastore_alloc(&msg_datastore, NULL))) { |
| 1015 |
return NULL; |
| 1016 |
} |
| 1017 |
|
| 1018 |
if (!(ds->data = ast_msg_alloc())) { |
| 1019 |
ast_datastore_free(ds); |
| 1020 |
return NULL; |
| 1021 |
} |
| 1022 |
|
| 1023 |
ast_channel_datastore_add(chan, ds); |
| 1024 |
|
| 1025 |
return ds; |
| 1026 |
} |
| 1027 |
|
| 1028 |
static int msg_func_read(struct ast_channel *chan, const char *function, |
| 1029 |
char *data, char *buf, size_t len) |
| 1030 |
{ |
| 1031 |
struct ast_datastore *ds; |
| 1032 |
struct ast_msg *msg; |
| 1033 |
|
| 1034 |
if (!chan) { |
| 1035 |
ast_log(LOG_WARNING, "No channel was provided to %s function.\n", function); |
| 1036 |
return -1; |
| 1037 |
} |
| 1038 |
|
| 1039 |
ast_channel_lock(chan); |
| 1040 |
|
| 1041 |
if (!(ds = ast_channel_datastore_find(chan, &msg_datastore, NULL))) { |
| 1042 |
ast_channel_unlock(chan); |
| 1043 |
ast_log(LOG_ERROR, "No MESSAGE data found on the channel to read.\n"); |
| 1044 |
return -1; |
| 1045 |
} |
| 1046 |
|
| 1047 |
msg = ds->data; |
| 1048 |
ao2_ref(msg, +1); |
| 1049 |
ast_channel_unlock(chan); |
| 1050 |
|
| 1051 |
ao2_lock(msg); |
| 1052 |
|
| 1053 |
if (!strcasecmp(data, "to")) { |
| 1054 |
ast_copy_string(buf, msg->to, len); |
| 1055 |
} else if (!strcasecmp(data, "from")) { |
| 1056 |
ast_copy_string(buf, msg->from, len); |
| 1057 |
} else if (!strcasecmp(data, "body")) { |
| 1058 |
ast_copy_string(buf, msg->body, len); |
| 1059 |
} else { |
| 1060 |
ast_log(LOG_WARNING, "Invalid argument to MESSAGE(): '%s'\n", data); |
| 1061 |
} |
| 1062 |
|
| 1063 |
ao2_unlock(msg); |
| 1064 |
ao2_ref(msg, -1); |
| 1065 |
|
| 1066 |
return 0; |
| 1067 |
} |
| 1068 |
|
| 1069 |
static int msg_func_write(struct ast_channel *chan, const char *function, |
| 1070 |
char *data, const char *value) |
| 1071 |
{ |
| 1072 |
struct ast_datastore *ds; |
| 1073 |
struct ast_msg *msg; |
| 1074 |
|
| 1075 |
if (!chan) { |
| 1076 |
ast_log(LOG_WARNING, "No channel was provided to %s function.\n", function); |
| 1077 |
return -1; |
| 1078 |
} |
| 1079 |
|
| 1080 |
ast_channel_lock(chan); |
| 1081 |
|
| 1082 |
if (!(ds = msg_datastore_find_or_create(chan))) { |
| 1083 |
ast_channel_unlock(chan); |
| 1084 |
return -1; |
| 1085 |
} |
| 1086 |
|
| 1087 |
msg = ds->data; |
| 1088 |
ao2_ref(msg, +1); |
| 1089 |
ast_channel_unlock(chan); |
| 1090 |
|
| 1091 |
ao2_lock(msg); |
| 1092 |
|
| 1093 |
if (!strcasecmp(data, "to")) { |
| 1094 |
ast_msg_set_to(msg, "%s", value); |
| 1095 |
} else if (!strcasecmp(data, "from")) { |
| 1096 |
ast_msg_set_from(msg, "%s", value); |
| 1097 |
} else if (!strcasecmp(data, "body")) { |
| 1098 |
ast_msg_set_body(msg, "%s", value); |
| 1099 |
} else if (!strcasecmp(data, "custom_data")) { |
| 1100 |
int outbound = -1; |
| 1101 |
if (!strcasecmp(value, "mark_all_outbound")) { |
| 1102 |
outbound = 1; |
| 1103 |
} else if (!strcasecmp(value, "clear_all_outbound")) { |
| 1104 |
outbound = 0; |
| 1105 |
} else { |
| 1106 |
ast_log(LOG_WARNING, "'%s' is not a valid value for custom_data\n", value); |
| 1107 |
} |
| 1108 |
|
| 1109 |
if (outbound != -1) { |
| 1110 |
struct msg_data *hdr_data; |
| 1111 |
struct ao2_iterator iter = ao2_iterator_init(msg->vars, 0); |
| 1112 |
|
| 1113 |
while ((hdr_data = ao2_iterator_next(&iter))) { |
| 1114 |
hdr_data->send = outbound; |
| 1115 |
ao2_ref(hdr_data, -1); |
| 1116 |
} |
| 1117 |
ao2_iterator_destroy(&iter); |
| 1118 |
} |
| 1119 |
} else { |
| 1120 |
ast_log(LOG_WARNING, "'%s' is not a valid write argument.\n", data); |
| 1121 |
} |
| 1122 |
|
| 1123 |
ao2_unlock(msg); |
| 1124 |
ao2_ref(msg, -1); |
| 1125 |
|
| 1126 |
return 0; |
| 1127 |
} |
| 1128 |
|
| 1129 |
static int msg_data_func_read(struct ast_channel *chan, const char *function, |
| 1130 |
char *data, char *buf, size_t len) |
| 1131 |
{ |
| 1132 |
struct ast_datastore *ds; |
| 1133 |
struct ast_msg *msg; |
| 1134 |
const char *val; |
| 1135 |
|
| 1136 |
if (!chan) { |
| 1137 |
ast_log(LOG_WARNING, "No channel was provided to %s function.\n", function); |
| 1138 |
return -1; |
| 1139 |
} |
| 1140 |
|
| 1141 |
ast_channel_lock(chan); |
| 1142 |
|
| 1143 |
if (!(ds = ast_channel_datastore_find(chan, &msg_datastore, NULL))) { |
| 1144 |
ast_channel_unlock(chan); |
| 1145 |
ast_log(LOG_ERROR, "No MESSAGE data found on the channel to read.\n"); |
| 1146 |
return -1; |
| 1147 |
} |
| 1148 |
|
| 1149 |
msg = ds->data; |
| 1150 |
ao2_ref(msg, +1); |
| 1151 |
ast_channel_unlock(chan); |
| 1152 |
|
| 1153 |
ao2_lock(msg); |
| 1154 |
|
| 1155 |
if ((val = ast_msg_get_var(msg, data))) { |
| 1156 |
ast_copy_string(buf, val, len); |
| 1157 |
} |
| 1158 |
|
| 1159 |
ao2_unlock(msg); |
| 1160 |
ao2_ref(msg, -1); |
| 1161 |
|
| 1162 |
return 0; |
| 1163 |
} |
| 1164 |
|
| 1165 |
static int msg_data_func_write(struct ast_channel *chan, const char *function, |
| 1166 |
char *data, const char *value) |
| 1167 |
{ |
| 1168 |
struct ast_datastore *ds; |
| 1169 |
struct ast_msg *msg; |
| 1170 |
|
| 1171 |
if (!chan) { |
| 1172 |
ast_log(LOG_WARNING, "No channel was provided to %s function.\n", function); |
| 1173 |
return -1; |
| 1174 |
} |
| 1175 |
|
| 1176 |
ast_channel_lock(chan); |
| 1177 |
|
| 1178 |
if (!(ds = msg_datastore_find_or_create(chan))) { |
| 1179 |
ast_channel_unlock(chan); |
| 1180 |
return -1; |
| 1181 |
} |
| 1182 |
|
| 1183 |
msg = ds->data; |
| 1184 |
ao2_ref(msg, +1); |
| 1185 |
ast_channel_unlock(chan); |
| 1186 |
|
| 1187 |
ao2_lock(msg); |
| 1188 |
|
| 1189 |
ast_msg_set_var_outbound(msg, data, value); |
| 1190 |
|
| 1191 |
ao2_unlock(msg); |
| 1192 |
ao2_ref(msg, -1); |
| 1193 |
|
| 1194 |
return 0; |
| 1195 |
} |
| 1196 |
|
| 1197 |
|
| 1198 |
|
| 1199 |
|
| 1200 |
|
| 1201 |
|
| 1202 |
|
| 1203 |
|
| 1204 |
|
| 1205 |
|
| 1206 |
|
| 1207 |
|
| 1208 |
static const struct ast_msg_tech *msg_find_by_tech_name(const char *tech_name) |
| 1209 |
{ |
| 1210 |
const struct ast_msg_tech *current; |
| 1211 |
int i; |
| 1212 |
|
| 1213 |
for (i = 0; i < AST_VECTOR_SIZE(&msg_techs); i++) { |
| 1214 |
current = AST_VECTOR_GET(&msg_techs, i); |
| 1215 |
if (!strcmp(current->name, tech_name)) { |
| 1216 |
return current; |
| 1217 |
} |
| 1218 |
} |
| 1219 |
|
| 1220 |
return NULL; |
| 1221 |
} |
| 1222 |
|
| 1223 |
|
| 1224 |
|
| 1225 |
|
| 1226 |
|
| 1227 |
|
| 1228 |
|
| 1229 |
|
| 1230 |
|
| 1231 |
|
| 1232 |
|
| 1233 |
|
| 1234 |
static const struct ast_msg_handler *msg_handler_find_by_tech_name(const char *tech_name) |
| 1235 |
{ |
| 1236 |
const struct ast_msg_handler *current; |
| 1237 |
int i; |
| 1238 |
|
| 1239 |
for (i = 0; i < AST_VECTOR_SIZE(&msg_handlers); i++) { |
| 1240 |
current = AST_VECTOR_GET(&msg_handlers, i); |
| 1241 |
if (!strcmp(current->name, tech_name)) { |
| 1242 |
return current; |
| 1243 |
} |
| 1244 |
} |
| 1245 |
|
| 1246 |
return NULL; |
| 1247 |
} |
| 1248 |
|
| 1249 |
|
| 1250 |
|
| 1251 |
|
| 1252 |
|
| 1253 |
static int msg_send_exec(struct ast_channel *chan, const char *data) |
| 1254 |
{ |
| 1255 |
struct ast_datastore *ds; |
| 1256 |
struct ast_msg *msg; |
| 1257 |
char *tech_name; |
| 1258 |
const struct ast_msg_tech *msg_tech; |
| 1259 |
char *parse; |
| 1260 |
int res = -1; |
| 1261 |
AST_DECLARE_APP_ARGS(args, |
| 1262 |
AST_APP_ARG(destination); |
| 1263 |
AST_APP_ARG(from); |
| 1264 |
AST_APP_ARG(to); |
| 1265 |
); |
| 1266 |
|
| 1267 |
if (ast_strlen_zero(data)) { |
| 1268 |
ast_log(LOG_WARNING, "An argument is required to MessageSend()\n"); |
| 1269 |
pbx_builtin_setvar_helper(chan, "MESSAGE_SEND_STATUS", "INVALID_URI"); |
| 1270 |
return 0; |
| 1271 |
} |
| 1272 |
|
| 1273 |
parse = ast_strdupa(data); |
| 1274 |
AST_STANDARD_APP_ARGS(args, parse); |
| 1275 |
|
| 1276 |
if (ast_strlen_zero(args.destination)) { |
| 1277 |
ast_log(LOG_WARNING, "A 'to' URI is required for MessageSend()\n"); |
| 1278 |
pbx_builtin_setvar_helper(chan, "MESSAGE_SEND_STATUS", "INVALID_URI"); |
| 1279 |
return 0; |
| 1280 |
} |
| 1281 |
|
| 1282 |
ast_channel_lock(chan); |
| 1283 |
|
| 1284 |
if (!(ds = ast_channel_datastore_find(chan, &msg_datastore, NULL))) { |
| 1285 |
ast_channel_unlock(chan); |
| 1286 |
ast_log(LOG_WARNING, "No message data found on channel to send.\n"); |
| 1287 |
pbx_builtin_setvar_helper(chan, "MESSAGE_SEND_STATUS", "FAILURE"); |
| 1288 |
return 0; |
| 1289 |
} |
| 1290 |
|
| 1291 |
msg = ds->data; |
| 1292 |
ao2_ref(msg, +1); |
| 1293 |
ast_channel_unlock(chan); |
| 1294 |
|
| 1295 |
tech_name = ast_strdupa(args.destination); |
| 1296 |
tech_name = strsep(&tech_name, ":"); |
| 1297 |
|
| 1298 |
ast_rwlock_rdlock(&msg_techs_lock); |
| 1299 |
msg_tech = msg_find_by_tech_name(tech_name); |
| 1300 |
|
| 1301 |
if (!msg_tech) { |
| 1302 |
ast_log(LOG_WARNING, "No message technology '%s' found.\n", tech_name); |
| 1303 |
pbx_builtin_setvar_helper(chan, "MESSAGE_SEND_STATUS", "INVALID_PROTOCOL"); |
| 1304 |
goto exit_cleanup; |
| 1305 |
} |
| 1306 |
|
| 1307 |
|
| 1308 |
|
| 1309 |
|
| 1310 |
|
| 1311 |
if (!ast_strlen_zero(args.to)) { |
| 1312 |
ast_string_field_set(msg, to, args.to); |
| 1313 |
} |
| 1314 |
|
| 1315 |
|
| 1316 |
|
| 1317 |
|
| 1318 |
|
| 1319 |
|
| 1320 |
ao2_lock(msg); |
| 1321 |
res = msg_tech->msg_send(msg, S_OR(args.destination, ""), S_OR(args.from, "")); |
| 1322 |
ao2_unlock(msg); |
| 1323 |
|
| 1324 |
pbx_builtin_setvar_helper(chan, "MESSAGE_SEND_STATUS", res ? "FAILURE" : "SUCCESS"); |
| 1325 |
|
| 1326 |
exit_cleanup: |
| 1327 |
ast_rwlock_unlock(&msg_techs_lock); |
| 1328 |
ao2_ref(msg, -1); |
| 1329 |
|
| 1330 |
return 0; |
| 1331 |
} |
| 1332 |
|
| 1333 |
static int action_messagesend(struct mansession *s, const struct message *m) |
| 1334 |
{ |
| 1335 |
const char *destination = astman_get_header(m, "Destination"); |
| 1336 |
const char *to = astman_get_header(m, "To"); |
| 1337 |
const char *from = astman_get_header(m, "From"); |
| 1338 |
const char *body = astman_get_header(m, "Body"); |
| 1339 |
const char *base64body = astman_get_header(m, "Base64Body"); |
| 1340 |
const char *to_override = NULL; |
| 1341 |
char base64decoded[1301] = { 0, }; |
| 1342 |
char *tech_name = NULL; |
| 1343 |
struct ast_variable *vars = NULL; |
| 1344 |
struct ast_variable *data = NULL; |
| 1345 |
const struct ast_msg_tech *msg_tech; |
| 1346 |
struct ast_msg *msg; |
| 1347 |
int res = -1; |
| 1348 |
|
| 1349 |
if (!ast_strlen_zero(destination)) { |
| 1350 |
if (!ast_strlen_zero(to)) { |
| 1351 |
to_override = to; |
| 1352 |
} |
| 1353 |
to = destination; |
| 1354 |
} else { |
| 1355 |
if (ast_strlen_zero(to)) { |
| 1356 |
astman_send_error(s, m, "No 'To' address specified."); |
| 1357 |
return 0; |
| 1358 |
} |
| 1359 |
} |
| 1360 |
|
| 1361 |
if (!ast_strlen_zero(base64body)) { |
| 1362 |
ast_base64decode((unsigned char *) base64decoded, base64body, sizeof(base64decoded) - 1); |
| 1363 |
body = base64decoded; |
| 1364 |
} |
| 1365 |
|
| 1366 |
tech_name = ast_strdupa(to); |
| 1367 |
tech_name = strsep(&tech_name, ":"); |
| 1368 |
|
| 1369 |
ast_rwlock_rdlock(&msg_techs_lock); |
| 1370 |
msg_tech = msg_find_by_tech_name(tech_name); |
| 1371 |
if (!msg_tech) { |
| 1372 |
ast_rwlock_unlock(&msg_techs_lock); |
| 1373 |
astman_send_error(s, m, "Message technology not found."); |
| 1374 |
return 0; |
| 1375 |
} |
| 1376 |
|
| 1377 |
if (!(msg = ast_msg_alloc())) { |
| 1378 |
ast_rwlock_unlock(&msg_techs_lock); |
| 1379 |
astman_send_error(s, m, "Internal failure\n"); |
| 1380 |
return 0; |
| 1381 |
} |
| 1382 |
|
| 1383 |
data = astman_get_variables_order(m, ORDER_NATURAL); |
| 1384 |
for (vars = data; vars; vars = vars->next) { |
| 1385 |
ast_msg_set_var_outbound(msg, vars->name, vars->value); |
| 1386 |
} |
| 1387 |
|
| 1388 |
ast_msg_set_body(msg, "%s", body); |
| 1389 |
|
| 1390 |
if (to_override) { |
| 1391 |
ast_string_field_set(msg, to, to_override); |
| 1392 |
} |
| 1393 |
|
| 1394 |
res = msg_tech->msg_send(msg, S_OR(to, ""), S_OR(from, "")); |
| 1395 |
|
| 1396 |
ast_rwlock_unlock(&msg_techs_lock); |
| 1397 |
|
| 1398 |
ast_variables_destroy(vars); |
| 1399 |
ao2_ref(msg, -1); |
| 1400 |
|
| 1401 |
if (res) { |
| 1402 |
astman_send_error(s, m, "Message failed to send."); |
| 1403 |
} else { |
| 1404 |
astman_send_ack(s, m, "Message successfully sent"); |
| 1405 |
} |
| 1406 |
return 0; |
| 1407 |
} |
| 1408 |
|
| 1409 |
int ast_msg_send(struct ast_msg *msg, const char *to, const char *from) |
| 1410 |
{ |
| 1411 |
char *tech_name = NULL; |
| 1412 |
const struct ast_msg_tech *msg_tech; |
| 1413 |
int res = -1; |
| 1414 |
|
| 1415 |
if (ast_strlen_zero(to)) { |
| 1416 |
ao2_ref(msg, -1); |
| 1417 |
return -1; |
| 1418 |
} |
| 1419 |
|
| 1420 |
tech_name = ast_strdupa(to); |
| 1421 |
tech_name = strsep(&tech_name, ":"); |
| 1422 |
|
| 1423 |
ast_rwlock_rdlock(&msg_techs_lock); |
| 1424 |
msg_tech = msg_find_by_tech_name(tech_name); |
| 1425 |
|
| 1426 |
if (!msg_tech) { |
| 1427 |
ast_log(LOG_ERROR, "Unknown message tech: %s\n", tech_name); |
| 1428 |
ast_rwlock_unlock(&msg_techs_lock); |
| 1429 |
return -1; |
| 1430 |
} |
| 1431 |
|
| 1432 |
res = msg_tech->msg_send(msg, S_OR(to, ""), S_OR(from, "")); |
| 1433 |
|
| 1434 |
ast_rwlock_unlock(&msg_techs_lock); |
| 1435 |
|
| 1436 |
ao2_ref(msg, -1); |
| 1437 |
|
| 1438 |
return res; |
| 1439 |
} |
| 1440 |
|
| 1441 |
|
| 1442 |
|
| 1443 |
|
| 1444 |
|
| 1445 |
|
| 1446 |
struct ast_msg_data { |
| 1447 |
|
| 1448 |
size_t length; |
| 1449 |
enum ast_msg_data_source_type source; |
| 1450 |
|
| 1451 |
int attribute_value_offsets[__AST_MSG_DATA_ATTR_LAST]; |
| 1452 |
|
| 1453 |
char buf[0]; |
| 1454 |
}; |
| 1455 |
|
| 1456 |
#define ATTRIBUTE_UNSET -1 |
| 1457 |
|
| 1458 |
struct ast_msg_data *ast_msg_data_alloc(enum ast_msg_data_source_type source, |
| 1459 |
struct ast_msg_data_attribute attributes[], size_t count) |
| 1460 |
{ |
| 1461 |
struct ast_msg_data *msg; |
| 1462 |
size_t len = sizeof(*msg); |
| 1463 |
size_t i; |
| 1464 |
size_t current_offset = 0; |
| 1465 |
enum ast_msg_data_attribute_type attr_type; |
| 1466 |
|
| 1467 |
if (!attributes) { |
| 1468 |
ast_assert(attributes != NULL); |
| 1469 |
return NULL; |
| 1470 |
} |
| 1471 |
|
| 1472 |
if (!count) { |
| 1473 |
ast_assert(count > 0); |
| 1474 |
return NULL; |
| 1475 |
} |
| 1476 |
|
| 1477 |
|
| 1478 |
for (i=0; i < count; i++) { |
| 1479 |
if (!attributes[i].value) { |
| 1480 |
ast_assert(attributes[i].value != NULL); |
| 1481 |
return NULL; |
| 1482 |
} |
| 1483 |
len += (strlen(attributes[i].value) + 1); |
| 1484 |
} |
| 1485 |
|
| 1486 |
msg = ast_calloc(1, len); |
| 1487 |
if (!msg) { |
| 1488 |
return NULL; |
| 1489 |
} |
| 1490 |
msg->source = source; |
| 1491 |
msg->length = len; |
| 1492 |
|
| 1493 |
|
| 1494 |
for (attr_type = 0; attr_type < __AST_MSG_DATA_ATTR_LAST; attr_type++) { |
| 1495 |
msg->attribute_value_offsets[attr_type] = ATTRIBUTE_UNSET; |
| 1496 |
} |
| 1497 |
|
| 1498 |
|
| 1499 |
for (i=0; i < count; i++) { |
| 1500 |
len = (strlen(attributes[i].value) + 1); |
| 1501 |
ast_copy_string(msg->buf + current_offset, attributes[i].value, len); |
| 1502 |
msg->attribute_value_offsets[attributes[i].type] = current_offset; |
| 1503 |
current_offset += len; |
| 1504 |
} |
| 1505 |
|
| 1506 |
return msg; |
| 1507 |
} |
| 1508 |
|
| 1509 |
struct ast_msg_data *ast_msg_data_alloc2(enum ast_msg_data_source_type source_type, |
| 1510 |
const char *to, const char *from, const char *content_type, const char *body) |
| 1511 |
{ |
| 1512 |
struct ast_msg_data_attribute attrs[] = |
| 1513 |
{ |
| 1514 |
{ |
| 1515 |
.type = AST_MSG_DATA_ATTR_TO, |
| 1516 |
.value = (char *)S_OR(to, ""), |
| 1517 |
}, |
| 1518 |
{ |
| 1519 |
.type = AST_MSG_DATA_ATTR_FROM, |
| 1520 |
.value = (char *)S_OR(from, ""), |
| 1521 |
}, |
| 1522 |
{ |
| 1523 |
.type = AST_MSG_DATA_ATTR_CONTENT_TYPE, |
| 1524 |
.value = (char *)S_OR(content_type, ""), |
| 1525 |
}, |
| 1526 |
{ |
| 1527 |
.type = AST_MSG_DATA_ATTR_BODY, |
| 1528 |
.value = (char *)S_OR(body, ""), |
| 1529 |
}, |
| 1530 |
}; |
| 1531 |
|
| 1532 |
return ast_msg_data_alloc(source_type, attrs, ARRAY_LEN(attrs)); |
| 1533 |
} |
| 1534 |
|
| 1535 |
struct ast_msg_data *ast_msg_data_dup(struct ast_msg_data *msg) |
| 1536 |
{ |
| 1537 |
struct ast_msg_data *dest; |
| 1538 |
|
| 1539 |
if (!msg) { |
| 1540 |
ast_assert(msg != NULL); |
| 1541 |
return NULL; |
| 1542 |
} |
| 1543 |
|
| 1544 |
dest = ast_malloc(msg->length); |
| 1545 |
if (!dest) { |
| 1546 |
return NULL; |
| 1547 |
} |
| 1548 |
memcpy(dest, msg, msg->length); |
| 1549 |
|
| 1550 |
return dest; |
| 1551 |
} |
| 1552 |
|
| 1553 |
size_t ast_msg_data_get_length(struct ast_msg_data *msg) |
| 1554 |
{ |
| 1555 |
if (!msg) { |
| 1556 |
ast_assert(msg != NULL); |
| 1557 |
return 0; |
| 1558 |
} |
| 1559 |
|
| 1560 |
return msg->length; |
| 1561 |
} |
| 1562 |
|
| 1563 |
enum ast_msg_data_source_type ast_msg_data_get_source_type(struct ast_msg_data *msg) |
| 1564 |
{ |
| 1565 |
if (!msg) { |
| 1566 |
ast_assert(msg != NULL); |
| 1567 |
return AST_MSG_DATA_SOURCE_TYPE_UNKNOWN; |
| 1568 |
} |
| 1569 |
|
| 1570 |
return msg->source; |
| 1571 |
} |
| 1572 |
|
| 1573 |
const char *ast_msg_data_get_attribute(struct ast_msg_data *msg, |
| 1574 |
enum ast_msg_data_attribute_type attribute_type) |
| 1575 |
{ |
| 1576 |
if (!msg) { |
| 1577 |
ast_assert(msg != NULL); |
| 1578 |
return ""; |
| 1579 |
} |
| 1580 |
|
| 1581 |
if (msg->attribute_value_offsets[attribute_type] > ATTRIBUTE_UNSET) { |
| 1582 |
return msg->buf + msg->attribute_value_offsets[attribute_type]; |
| 1583 |
} |
| 1584 |
|
| 1585 |
return ""; |
| 1586 |
} |
| 1587 |
|
| 1588 |
int ast_msg_data_queue_frame(struct ast_channel *channel, struct ast_msg_data *msg) |
| 1589 |
{ |
| 1590 |
struct ast_frame f; |
| 1591 |
|
| 1592 |
if (!channel) { |
| 1593 |
ast_assert(channel != NULL); |
| 1594 |
return -1; |
| 1595 |
} |
| 1596 |
|
| 1597 |
if (!msg) { |
| 1598 |
ast_assert(msg != NULL); |
| 1599 |
return -1; |
| 1600 |
} |
| 1601 |
|
| 1602 |
memset(&f, 0, sizeof(f)); |
| 1603 |
f.frametype = AST_FRAME_TEXT_DATA; |
| 1604 |
f.data.ptr = msg; |
| 1605 |
f.datalen = msg->length; |
| 1606 |
return ast_queue_frame(channel, &f); |
| 1607 |
} |
| 1608 |
|
| 1609 |
int ast_msg_tech_register(const struct ast_msg_tech *tech) |
| 1610 |
{ |
| 1611 |
const struct ast_msg_tech *match; |
| 1612 |
|
| 1613 |
ast_rwlock_wrlock(&msg_techs_lock); |
| 1614 |
|
| 1615 |
match = msg_find_by_tech_name(tech->name); |
| 1616 |
if (match) { |
| 1617 |
ast_log(LOG_ERROR, "Message technology already registered for '%s'\n", |
| 1618 |
tech->name); |
| 1619 |
ast_rwlock_unlock(&msg_techs_lock); |
| 1620 |
return -1; |
| 1621 |
} |
| 1622 |
|
| 1623 |
if (AST_VECTOR_APPEND(&msg_techs, tech)) { |
| 1624 |
ast_log(LOG_ERROR, "Failed to register message technology for '%s'\n", |
| 1625 |
tech->name); |
| 1626 |
ast_rwlock_unlock(&msg_techs_lock); |
| 1627 |
return -1; |
| 1628 |
} |
| 1629 |
ast_verb(5, "Message technology '%s' registered.\n", tech->name); |
| 1630 |
|
| 1631 |
ast_rwlock_unlock(&msg_techs_lock); |
| 1632 |
|
| 1633 |
return 0; |
| 1634 |
} |
| 1635 |
|
| 1636 |
|
| 1637 |
|
| 1638 |
|
| 1639 |
|
| 1640 |
|
| 1641 |
|
| 1642 |
|
| 1643 |
|
| 1644 |
|
| 1645 |
static int msg_tech_cmp(const struct ast_msg_tech *vec_elem, const struct ast_msg_tech *srch) |
| 1646 |
{ |
| 1647 |
return !strcmp(vec_elem->name, srch->name); |
| 1648 |
} |
| 1649 |
|
| 1650 |
int ast_msg_tech_unregister(const struct ast_msg_tech *tech) |
| 1651 |
{ |
| 1652 |
int match; |
| 1653 |
|
| 1654 |
ast_rwlock_wrlock(&msg_techs_lock); |
| 1655 |
match = AST_VECTOR_REMOVE_CMP_UNORDERED(&msg_techs, tech, msg_tech_cmp, |
| 1656 |
AST_VECTOR_ELEM_CLEANUP_NOOP); |
| 1657 |
ast_rwlock_unlock(&msg_techs_lock); |
| 1658 |
|
| 1659 |
if (match) { |
| 1660 |
ast_log(LOG_ERROR, "No '%s' message technology found.\n", tech->name); |
| 1661 |
return -1; |
| 1662 |
} |
| 1663 |
|
| 1664 |
ast_verb(5, "Message technology '%s' unregistered.\n", tech->name); |
| 1665 |
|
| 1666 |
return 0; |
| 1667 |
} |
| 1668 |
|
| 1669 |
int ast_msg_handler_register(const struct ast_msg_handler *handler) |
| 1670 |
{ |
| 1671 |
const struct ast_msg_handler *match; |
| 1672 |
|
| 1673 |
ast_rwlock_wrlock(&msg_handlers_lock); |
| 1674 |
|
| 1675 |
match = msg_handler_find_by_tech_name(handler->name); |
| 1676 |
if (match) { |
| 1677 |
ast_log(LOG_ERROR, "Message handler already registered for '%s'\n", |
| 1678 |
handler->name); |
| 1679 |
ast_rwlock_unlock(&msg_handlers_lock); |
| 1680 |
return -1; |
| 1681 |
} |
| 1682 |
|
| 1683 |
if (AST_VECTOR_APPEND(&msg_handlers, handler)) { |
| 1684 |
ast_log(LOG_ERROR, "Failed to register message handler for '%s'\n", |
| 1685 |
handler->name); |
| 1686 |
ast_rwlock_unlock(&msg_handlers_lock); |
| 1687 |
return -1; |
| 1688 |
} |
| 1689 |
ast_verb(5, "Message handler '%s' registered.\n", handler->name); |
| 1690 |
|
| 1691 |
ast_rwlock_unlock(&msg_handlers_lock); |
| 1692 |
|
| 1693 |
return 0; |
| 1694 |
|
| 1695 |
} |
| 1696 |
|
| 1697 |
|
| 1698 |
|
| 1699 |
|
| 1700 |
|
| 1701 |
|
| 1702 |
|
| 1703 |
|
| 1704 |
|
| 1705 |
|
| 1706 |
static int msg_handler_cmp(const struct ast_msg_handler *vec_elem, const struct ast_msg_handler *srch) |
| 1707 |
{ |
| 1708 |
return !strcmp(vec_elem->name, srch->name); |
| 1709 |
} |
| 1710 |
|
| 1711 |
int ast_msg_handler_unregister(const struct ast_msg_handler *handler) |
| 1712 |
{ |
| 1713 |
int match; |
| 1714 |
|
| 1715 |
ast_rwlock_wrlock(&msg_handlers_lock); |
| 1716 |
match = AST_VECTOR_REMOVE_CMP_UNORDERED(&msg_handlers, handler, msg_handler_cmp, |
| 1717 |
AST_VECTOR_ELEM_CLEANUP_NOOP); |
| 1718 |
ast_rwlock_unlock(&msg_handlers_lock); |
| 1719 |
|
| 1720 |
if (match) { |
| 1721 |
ast_log(LOG_ERROR, "No '%s' message handler found.\n", handler->name); |
| 1722 |
return -1; |
| 1723 |
} |
| 1724 |
|
| 1725 |
ast_verb(5, "Message handler '%s' unregistered.\n", handler->name); |
| 1726 |
return 0; |
| 1727 |
} |
| 1728 |
|
| 1729 |
void ast_msg_shutdown(void) |
| 1730 |
{ |
| 1731 |
if (msg_q_tp) { |
| 1732 |
msg_q_tp = ast_taskprocessor_unreference(msg_q_tp); |
| 1733 |
} |
| 1734 |
} |
| 1735 |
|
| 1736 |
|
| 1737 |
|
| 1738 |
|
| 1739 |
|
| 1740 |
|
| 1741 |
|
| 1742 |
|
| 1743 |
|
| 1744 |
static void message_shutdown(void) |
| 1745 |
{ |
| 1746 |
ast_msg_handler_unregister(&dialplan_msg_handler); |
| 1747 |
|
| 1748 |
ast_custom_function_unregister(&msg_function); |
| 1749 |
ast_custom_function_unregister(&msg_data_function); |
| 1750 |
ast_unregister_application(app_msg_send); |
| 1751 |
ast_manager_unregister("MessageSend"); |
| 1752 |
|
| 1753 |
AST_VECTOR_FREE(&msg_techs); |
| 1754 |
ast_rwlock_destroy(&msg_techs_lock); |
| 1755 |
|
| 1756 |
AST_VECTOR_FREE(&msg_handlers); |
| 1757 |
ast_rwlock_destroy(&msg_handlers_lock); |
| 1758 |
} |
| 1759 |
|
| 1760 |
|
| 1761 |
|
| 1762 |
|
| 1763 |
|
| 1764 |
|
| 1765 |
|
| 1766 |
|
| 1767 |
|
| 1768 |
|
| 1769 |
|
| 1770 |
int ast_msg_init(void) |
| 1771 |
{ |
| 1772 |
int res; |
| 1773 |
|
| 1774 |
msg_q_tp = ast_taskprocessor_get("ast_msg_queue", TPS_REF_DEFAULT); |
| 1775 |
if (!msg_q_tp) { |
| 1776 |
return -1; |
| 1777 |
} |
| 1778 |
|
| 1779 |
ast_rwlock_init(&msg_techs_lock); |
| 1780 |
if (AST_VECTOR_INIT(&msg_techs, 8)) { |
| 1781 |
return -1; |
| 1782 |
} |
| 1783 |
|
| 1784 |
ast_rwlock_init(&msg_handlers_lock); |
| 1785 |
if (AST_VECTOR_INIT(&msg_handlers, 4)) { |
| 1786 |
return -1; |
| 1787 |
} |
| 1788 |
|
| 1789 |
res = ast_msg_handler_register(&dialplan_msg_handler); |
| 1790 |
|
| 1791 |
res |= __ast_custom_function_register(&msg_function, NULL); |
| 1792 |
res |= __ast_custom_function_register(&msg_data_function, NULL); |
| 1793 |
res |= ast_register_application2(app_msg_send, msg_send_exec, NULL, NULL, NULL); |
| 1794 |
res |= ast_manager_register_xml_core("MessageSend", EVENT_FLAG_MESSAGE, action_messagesend); |
| 1795 |
|
| 1796 |
ast_register_cleanup(message_shutdown); |
| 1797 |
|
| 1798 |
return res; |
| 1799 |
} |