| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
#include "asterisk.h" |
| 37 |
|
| 38 |
#include <ctype.h> |
| 39 |
#include <errno.h> |
| 40 |
|
| 41 |
#include "asterisk/paths.h" |
| 42 |
#include "asterisk/file.h" |
| 43 |
#include "asterisk/channel.h" |
| 44 |
#include "asterisk/audiohook.h" |
| 45 |
#include "asterisk/features.h" |
| 46 |
#include "asterisk/app.h" |
| 47 |
#include "asterisk/utils.h" |
| 48 |
#include "asterisk/say.h" |
| 49 |
#include "asterisk/pbx.h" |
| 50 |
#include "asterisk/translate.h" |
| 51 |
#include "asterisk/manager.h" |
| 52 |
#include "asterisk/module.h" |
| 53 |
#include "asterisk/lock.h" |
| 54 |
#include "asterisk/options.h" |
| 55 |
#include "asterisk/autochan.h" |
| 56 |
#include "asterisk/stasis_channels.h" |
| 57 |
#include "asterisk/json.h" |
| 58 |
#include "asterisk/format_cache.h" |
| 59 |
|
| 60 |
#define AST_NAME_STRLEN 256 |
| 61 |
#define NUM_SPYGROUPS 128 |
| 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 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
static const char app_chan[] = "ChanSpy"; |
| 389 |
|
| 390 |
static const char app_ext[] = "ExtenSpy"; |
| 391 |
|
| 392 |
static const char app_dahdiscan[] = "DAHDIScan"; |
| 393 |
|
| 394 |
enum { |
| 395 |
OPTION_QUIET = (1 << 0), |
| 396 |
OPTION_BRIDGED = (1 << 1), |
| 397 |
OPTION_VOLUME = (1 << 2), |
| 398 |
OPTION_GROUP = (1 << 3), |
| 399 |
OPTION_RECORD = (1 << 4), |
| 400 |
OPTION_WHISPER = (1 << 5), |
| 401 |
OPTION_PRIVATE = (1 << 6), |
| 402 |
OPTION_READONLY = (1 << 7), |
| 403 |
OPTION_EXIT = (1 << 8), |
| 404 |
OPTION_ENFORCED = (1 << 9), |
| 405 |
OPTION_NOTECH = (1 << 10), |
| 406 |
OPTION_BARGE = (1 << 11), |
| 407 |
OPTION_NAME = (1 << 12), |
| 408 |
OPTION_DTMF_SWITCH_MODES = (1 << 13), |
| 409 |
OPTION_DTMF_EXIT = (1 << 14), |
| 410 |
OPTION_DTMF_CYCLE = (1 << 15), |
| 411 |
OPTION_DAHDI_SCAN = (1 << 16), |
| 412 |
OPTION_STOP = (1 << 17), |
| 413 |
OPTION_EXITONHANGUP = (1 << 18), |
| 414 |
OPTION_UNIQUEID = (1 << 19), |
| 415 |
OPTION_LONG_QUEUE = (1 << 20), |
| 416 |
OPTION_INTERLEAVED = (1 << 21), |
| 417 |
OPTION_NOANSWER = (1 << 22), |
| 418 |
}; |
| 419 |
|
| 420 |
enum { |
| 421 |
OPT_ARG_VOLUME = 0, |
| 422 |
OPT_ARG_GROUP, |
| 423 |
OPT_ARG_RECORD, |
| 424 |
OPT_ARG_ENFORCED, |
| 425 |
OPT_ARG_NAME, |
| 426 |
OPT_ARG_EXIT, |
| 427 |
OPT_ARG_CYCLE, |
| 428 |
OPT_ARG_ARRAY_SIZE, |
| 429 |
}; |
| 430 |
|
| 431 |
AST_APP_OPTIONS(spy_opts, { |
| 432 |
AST_APP_OPTION('b', OPTION_BRIDGED), |
| 433 |
AST_APP_OPTION('B', OPTION_BARGE), |
| 434 |
AST_APP_OPTION_ARG('c', OPTION_DTMF_CYCLE, OPT_ARG_CYCLE), |
| 435 |
AST_APP_OPTION('d', OPTION_DTMF_SWITCH_MODES), |
| 436 |
AST_APP_OPTION('D', OPTION_INTERLEAVED), |
| 437 |
AST_APP_OPTION_ARG('e', OPTION_ENFORCED, OPT_ARG_ENFORCED), |
| 438 |
AST_APP_OPTION('E', OPTION_EXITONHANGUP), |
| 439 |
AST_APP_OPTION_ARG('g', OPTION_GROUP, OPT_ARG_GROUP), |
| 440 |
AST_APP_OPTION('l', OPTION_LONG_QUEUE), |
| 441 |
AST_APP_OPTION_ARG('n', OPTION_NAME, OPT_ARG_NAME), |
| 442 |
AST_APP_OPTION('N', OPTION_NOANSWER), |
| 443 |
AST_APP_OPTION('o', OPTION_READONLY), |
| 444 |
AST_APP_OPTION('q', OPTION_QUIET), |
| 445 |
AST_APP_OPTION_ARG('r', OPTION_RECORD, OPT_ARG_RECORD), |
| 446 |
AST_APP_OPTION('s', OPTION_NOTECH), |
| 447 |
AST_APP_OPTION('S', OPTION_STOP), |
| 448 |
AST_APP_OPTION('u', OPTION_UNIQUEID), |
| 449 |
AST_APP_OPTION_ARG('v', OPTION_VOLUME, OPT_ARG_VOLUME), |
| 450 |
AST_APP_OPTION('w', OPTION_WHISPER), |
| 451 |
AST_APP_OPTION('W', OPTION_PRIVATE), |
| 452 |
AST_APP_OPTION_ARG('x', OPTION_DTMF_EXIT, OPT_ARG_EXIT), |
| 453 |
AST_APP_OPTION('X', OPTION_EXIT), |
| 454 |
}); |
| 455 |
|
| 456 |
struct chanspy_translation_helper { |
| 457 |
|
| 458 |
struct ast_audiohook spy_audiohook; |
| 459 |
struct ast_audiohook whisper_audiohook; |
| 460 |
struct ast_audiohook bridge_whisper_audiohook; |
| 461 |
int fd; |
| 462 |
int volfactor; |
| 463 |
struct ast_flags flags; |
| 464 |
}; |
| 465 |
|
| 466 |
struct spy_dtmf_options { |
| 467 |
char exit; |
| 468 |
char cycle; |
| 469 |
char volume; |
| 470 |
}; |
| 471 |
|
| 472 |
static void *spy_alloc(struct ast_channel *chan, void *data) |
| 473 |
{ |
| 474 |
|
| 475 |
return data; |
| 476 |
} |
| 477 |
|
| 478 |
static void spy_release(struct ast_channel *chan, void *data) |
| 479 |
{ |
| 480 |
|
| 481 |
} |
| 482 |
|
| 483 |
static int spy_generate(struct ast_channel *chan, void *data, int len, int samples) |
| 484 |
{ |
| 485 |
struct chanspy_translation_helper *csth = data; |
| 486 |
struct ast_frame *f, *cur; |
| 487 |
|
| 488 |
ast_audiohook_lock(&csth->spy_audiohook); |
| 489 |
if (csth->spy_audiohook.status != AST_AUDIOHOOK_STATUS_RUNNING) { |
| 490 |
|
| 491 |
ast_audiohook_unlock(&csth->spy_audiohook); |
| 492 |
return -1; |
| 493 |
} |
| 494 |
|
| 495 |
if (ast_test_flag(&csth->flags, OPTION_READONLY)) { |
| 496 |
|
| 497 |
f = ast_audiohook_read_frame(&csth->spy_audiohook, samples, AST_AUDIOHOOK_DIRECTION_READ, ast_format_slin); |
| 498 |
} else if (ast_test_flag(&csth->flags, OPTION_INTERLEAVED)) { |
| 499 |
|
| 500 |
int i; |
| 501 |
struct ast_frame *fr_read = NULL; |
| 502 |
struct ast_frame *fr_write = NULL; |
| 503 |
short read_buf[samples]; |
| 504 |
short write_buf[samples]; |
| 505 |
short stereo_buf[samples * 2]; |
| 506 |
struct ast_frame stereo_frame = { |
| 507 |
.frametype = AST_FRAME_VOICE, |
| 508 |
.datalen = sizeof(stereo_buf), |
| 509 |
.samples = samples, |
| 510 |
}; |
| 511 |
|
| 512 |
f = ast_audiohook_read_frame_all(&csth->spy_audiohook, samples, ast_format_slin, &fr_read, &fr_write); |
| 513 |
if (f) { |
| 514 |
ast_frame_free(f, 0); |
| 515 |
f = NULL; |
| 516 |
} |
| 517 |
|
| 518 |
if (fr_read) { |
| 519 |
memcpy(read_buf, fr_read->data.ptr, sizeof(read_buf)); |
| 520 |
} else { |
| 521 |
|
| 522 |
memset(read_buf, 0, sizeof(read_buf)); |
| 523 |
} |
| 524 |
|
| 525 |
if (fr_write) { |
| 526 |
memcpy(write_buf, fr_write->data.ptr, sizeof(write_buf)); |
| 527 |
} else { |
| 528 |
memset(write_buf, 0, sizeof(write_buf)); |
| 529 |
} |
| 530 |
|
| 531 |
for (i = 0; i < samples; i++) { |
| 532 |
stereo_buf[i*2] = read_buf[i]; |
| 533 |
stereo_buf[i*2+1] = write_buf[i]; |
| 534 |
} |
| 535 |
|
| 536 |
stereo_frame.data.ptr = stereo_buf; |
| 537 |
stereo_frame.subclass.format = ast_format_cache_get_slin_by_rate(samples); |
| 538 |
|
| 539 |
f = ast_frdup(&stereo_frame); |
| 540 |
|
| 541 |
if (fr_read) { |
| 542 |
ast_frame_free(fr_read, 0); |
| 543 |
} |
| 544 |
if (fr_write) { |
| 545 |
ast_frame_free(fr_write, 0); |
| 546 |
} |
| 547 |
|
| 548 |
} else { |
| 549 |
f = ast_audiohook_read_frame(&csth->spy_audiohook, samples, AST_AUDIOHOOK_DIRECTION_BOTH, ast_format_slin); |
| 550 |
} |
| 551 |
|
| 552 |
ast_audiohook_unlock(&csth->spy_audiohook); |
| 553 |
|
| 554 |
if (!f) |
| 555 |
return 0; |
| 556 |
|
| 557 |
for (cur = f; cur; cur = AST_LIST_NEXT(cur, frame_list)) { |
| 558 |
if (ast_write(chan, cur)) { |
| 559 |
ast_frfree(f); |
| 560 |
return -1; |
| 561 |
} |
| 562 |
|
| 563 |
if (csth->fd) { |
| 564 |
if (write(csth->fd, cur->data.ptr, cur->datalen) < 0) { |
| 565 |
ast_log(LOG_WARNING, "write() failed: %s\n", strerror(errno)); |
| 566 |
} |
| 567 |
} |
| 568 |
} |
| 569 |
|
| 570 |
ast_frfree(f); |
| 571 |
|
| 572 |
return 0; |
| 573 |
} |
| 574 |
|
| 575 |
static struct ast_generator spygen = { |
| 576 |
.alloc = spy_alloc, |
| 577 |
.release = spy_release, |
| 578 |
.generate = spy_generate, |
| 579 |
}; |
| 580 |
|
| 581 |
static int start_spying(struct ast_autochan *autochan, const char *spychan_name, struct ast_audiohook *audiohook, struct ast_flags *flags) |
| 582 |
{ |
| 583 |
int res; |
| 584 |
|
| 585 |
ast_autochan_channel_lock(autochan); |
| 586 |
ast_verb(3, "Attaching spy channel %s to %s\n", |
| 587 |
spychan_name, ast_channel_name(autochan->chan)); |
| 588 |
|
| 589 |
if (ast_test_flag(flags, OPTION_READONLY)) { |
| 590 |
ast_audiohook_set_frame_feed_direction(audiohook, AST_AUDIOHOOK_DIRECTION_READ); |
| 591 |
ast_set_flag(audiohook, AST_AUDIOHOOK_MUTE_WRITE); |
| 592 |
} else { |
| 593 |
ast_set_flag(audiohook, AST_AUDIOHOOK_TRIGGER_SYNC); |
| 594 |
} |
| 595 |
if (ast_test_flag(flags, OPTION_LONG_QUEUE)) { |
| 596 |
ast_debug(9, "Using a long queue to store audio frames in spy audiohook\n"); |
| 597 |
} else { |
| 598 |
ast_set_flag(audiohook, AST_AUDIOHOOK_SMALL_QUEUE); |
| 599 |
} |
| 600 |
res = ast_audiohook_attach(autochan->chan, audiohook); |
| 601 |
ast_autochan_channel_unlock(autochan); |
| 602 |
return res; |
| 603 |
} |
| 604 |
|
| 605 |
static int start_whispering(struct ast_autochan *autochan, const char *spychan_name, struct ast_audiohook *audiohook, struct ast_flags *flags) |
| 606 |
{ |
| 607 |
int res; |
| 608 |
|
| 609 |
ast_autochan_channel_lock(autochan); |
| 610 |
ast_verb(3, "Attaching spy channel %s to %s\n", |
| 611 |
spychan_name, ast_channel_name(autochan->chan)); |
| 612 |
|
| 613 |
ast_set_flag(audiohook, AST_AUDIOHOOK_TRIGGER_SYNC); |
| 614 |
if (ast_test_flag(flags, OPTION_LONG_QUEUE)) { |
| 615 |
ast_debug(9, "Using a long queue to store audio frames in whisper audiohook\n"); |
| 616 |
} else { |
| 617 |
ast_set_flag(audiohook, AST_AUDIOHOOK_SMALL_QUEUE); |
| 618 |
} |
| 619 |
res = ast_audiohook_attach(autochan->chan, audiohook); |
| 620 |
ast_autochan_channel_unlock(autochan); |
| 621 |
return res; |
| 622 |
} |
| 623 |
|
| 624 |
static void change_spy_mode(const char digit, struct ast_flags *flags) |
| 625 |
{ |
| 626 |
if (digit == '4') { |
| 627 |
ast_clear_flag(flags, OPTION_WHISPER); |
| 628 |
ast_clear_flag(flags, OPTION_BARGE); |
| 629 |
} else if (digit == '5') { |
| 630 |
ast_clear_flag(flags, OPTION_BARGE); |
| 631 |
ast_set_flag(flags, OPTION_WHISPER); |
| 632 |
} else if (digit == '6') { |
| 633 |
ast_clear_flag(flags, OPTION_WHISPER); |
| 634 |
ast_set_flag(flags, OPTION_BARGE); |
| 635 |
} |
| 636 |
} |
| 637 |
|
| 638 |
static int pack_channel_into_message(struct ast_channel *chan, const char *role, |
| 639 |
struct ast_multi_channel_blob *payload) |
| 640 |
{ |
| 641 |
RAII_VAR(struct ast_channel_snapshot *, snapshot, |
| 642 |
ast_channel_snapshot_get_latest(ast_channel_uniqueid(chan)), |
| 643 |
ao2_cleanup); |
| 644 |
|
| 645 |
if (!snapshot) { |
| 646 |
return -1; |
| 647 |
} |
| 648 |
ast_multi_channel_blob_add_channel(payload, role, snapshot); |
| 649 |
return 0; |
| 650 |
} |
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
static void publish_chanspy_message(struct ast_channel *spyer, |
| 660 |
struct ast_channel *spyee, |
| 661 |
int start) |
| 662 |
{ |
| 663 |
RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref); |
| 664 |
RAII_VAR(struct ast_multi_channel_blob *, payload, NULL, ao2_cleanup); |
| 665 |
RAII_VAR(struct stasis_message *, message, NULL, ao2_cleanup); |
| 666 |
struct stasis_message_type *type = start ? ast_channel_chanspy_start_type(): ast_channel_chanspy_stop_type(); |
| 667 |
|
| 668 |
if (!spyer) { |
| 669 |
ast_log(AST_LOG_WARNING, "Attempt to publish ChanSpy message for NULL spyer channel\n"); |
| 670 |
return; |
| 671 |
} |
| 672 |
blob = ast_json_null(); |
| 673 |
if (!blob || !type) { |
| 674 |
return; |
| 675 |
} |
| 676 |
|
| 677 |
payload = ast_multi_channel_blob_create(blob); |
| 678 |
if (!payload) { |
| 679 |
return; |
| 680 |
} |
| 681 |
|
| 682 |
if (pack_channel_into_message(spyer, "spyer_channel", payload)) { |
| 683 |
return; |
| 684 |
} |
| 685 |
|
| 686 |
if (spyee) { |
| 687 |
if (pack_channel_into_message(spyee, "spyee_channel", payload)) { |
| 688 |
return; |
| 689 |
} |
| 690 |
} |
| 691 |
|
| 692 |
message = stasis_message_create(type, payload); |
| 693 |
if (!message) { |
| 694 |
return; |
| 695 |
} |
| 696 |
stasis_publish(ast_channel_topic(spyer), message); |
| 697 |
} |
| 698 |
|
| 699 |
static int attach_barge(struct ast_autochan *spyee_autochan, |
| 700 |
struct ast_autochan **spyee_bridge_autochan, struct ast_audiohook *bridge_whisper_audiohook, |
| 701 |
const char *spyer_name, const char *name, struct ast_flags *flags) |
| 702 |
{ |
| 703 |
int retval = 0; |
| 704 |
struct ast_autochan *internal_bridge_autochan; |
| 705 |
struct ast_channel *spyee_chan; |
| 706 |
RAII_VAR(struct ast_channel *, bridged, NULL, ast_channel_cleanup); |
| 707 |
|
| 708 |
ast_autochan_channel_lock(spyee_autochan); |
| 709 |
spyee_chan = ast_channel_ref(spyee_autochan->chan); |
| 710 |
ast_autochan_channel_unlock(spyee_autochan); |
| 711 |
bridged = ast_channel_bridge_peer(spyee_chan); |
| 712 |
ast_channel_unref(spyee_chan); |
| 713 |
if (!bridged) { |
| 714 |
return -1; |
| 715 |
} |
| 716 |
|
| 717 |
ast_audiohook_init(bridge_whisper_audiohook, AST_AUDIOHOOK_TYPE_WHISPER, "Chanspy", 0); |
| 718 |
|
| 719 |
internal_bridge_autochan = ast_autochan_setup(bridged); |
| 720 |
if (!internal_bridge_autochan) { |
| 721 |
return -1; |
| 722 |
} |
| 723 |
|
| 724 |
if (start_whispering(internal_bridge_autochan, spyer_name, bridge_whisper_audiohook, flags)) { |
| 725 |
ast_log(LOG_WARNING, "Unable to attach barge audiohook on spyee '%s'. Barge mode disabled.\n", name); |
| 726 |
retval = -1; |
| 727 |
} |
| 728 |
|
| 729 |
*spyee_bridge_autochan = internal_bridge_autochan; |
| 730 |
|
| 731 |
return retval; |
| 732 |
} |
| 733 |
|
| 734 |
static int channel_spy(struct ast_channel *chan, struct ast_autochan *spyee_autochan, |
| 735 |
int *volfactor, int fd, struct spy_dtmf_options *user_options, struct ast_flags *flags, |
| 736 |
char *exitcontext) |
| 737 |
{ |
| 738 |
struct chanspy_translation_helper csth; |
| 739 |
int running = 0, bridge_connected = 0, res, x = 0; |
| 740 |
char inp[24] = {0}; |
| 741 |
char *name; |
| 742 |
struct ast_frame *f; |
| 743 |
struct ast_silence_generator *silgen = NULL; |
| 744 |
struct ast_autochan *spyee_bridge_autochan = NULL; |
| 745 |
const char *spyer_name; |
| 746 |
|
| 747 |
ast_channel_lock(chan); |
| 748 |
if (ast_check_hangup(chan)) { |
| 749 |
ast_channel_unlock(chan); |
| 750 |
return 0; |
| 751 |
} |
| 752 |
spyer_name = ast_strdupa(ast_channel_name(chan)); |
| 753 |
ast_channel_unlock(chan); |
| 754 |
|
| 755 |
ast_autochan_channel_lock(spyee_autochan); |
| 756 |
if (ast_check_hangup(spyee_autochan->chan) |
| 757 |
|| ast_test_flag(ast_channel_flags(spyee_autochan->chan), AST_FLAG_ZOMBIE)) { |
| 758 |
ast_autochan_channel_unlock(spyee_autochan); |
| 759 |
return 0; |
| 760 |
} |
| 761 |
name = ast_strdupa(ast_channel_name(spyee_autochan->chan)); |
| 762 |
|
| 763 |
ast_verb(2, "Spying on channel %s\n", name); |
| 764 |
publish_chanspy_message(chan, spyee_autochan->chan, 1); |
| 765 |
ast_autochan_channel_unlock(spyee_autochan); |
| 766 |
|
| 767 |
memset(&csth, 0, sizeof(csth)); |
| 768 |
ast_copy_flags(&csth.flags, flags, AST_FLAGS_ALL); |
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
ast_audiohook_init(&csth.spy_audiohook, AST_AUDIOHOOK_TYPE_SPY, "ChanSpy", 0); |
| 774 |
|
| 775 |
if (start_spying(spyee_autochan, spyer_name, &csth.spy_audiohook, flags)) { |
| 776 |
ast_audiohook_destroy(&csth.spy_audiohook); |
| 777 |
return 0; |
| 778 |
} |
| 779 |
|
| 780 |
if (ast_test_flag(flags, OPTION_WHISPER | OPTION_BARGE | OPTION_DTMF_SWITCH_MODES)) { |
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
ast_audiohook_init(&csth.whisper_audiohook, AST_AUDIOHOOK_TYPE_WHISPER, "ChanSpy", 0); |
| 785 |
|
| 786 |
if (start_whispering(spyee_autochan, spyer_name, &csth.whisper_audiohook, flags)) { |
| 787 |
ast_log(LOG_WARNING, "Unable to attach whisper audiohook to spyee %s. Whisper mode disabled!\n", name); |
| 788 |
} |
| 789 |
} |
| 790 |
|
| 791 |
ast_channel_set_flag(chan, AST_FLAG_END_DTMF_ONLY); |
| 792 |
|
| 793 |
csth.volfactor = *volfactor; |
| 794 |
|
| 795 |
if (csth.volfactor) { |
| 796 |
csth.spy_audiohook.options.read_volume = csth.volfactor; |
| 797 |
csth.spy_audiohook.options.write_volume = csth.volfactor; |
| 798 |
} |
| 799 |
|
| 800 |
csth.fd = fd; |
| 801 |
|
| 802 |
if (ast_test_flag(flags, OPTION_PRIVATE)) |
| 803 |
silgen = ast_channel_start_silence_generator(chan); |
| 804 |
else |
| 805 |
ast_activate_generator(chan, &spygen, &csth); |
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
|
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
while (ast_waitfor(chan, -1) > -1 && csth.spy_audiohook.status == AST_AUDIOHOOK_STATUS_RUNNING) { |
| 822 |
if (!(f = ast_read(chan)) || ast_check_hangup(chan)) { |
| 823 |
running = -1; |
| 824 |
if (f) { |
| 825 |
ast_frfree(f); |
| 826 |
} |
| 827 |
break; |
| 828 |
} |
| 829 |
|
| 830 |
if (ast_test_flag(flags, OPTION_BARGE) && f->frametype == AST_FRAME_VOICE) { |
| 831 |
|
| 832 |
|
| 833 |
|
| 834 |
|
| 835 |
if (!bridge_connected && attach_barge(spyee_autochan, &spyee_bridge_autochan, |
| 836 |
&csth.bridge_whisper_audiohook, spyer_name, name, flags) == 0) { |
| 837 |
bridge_connected = 1; |
| 838 |
} |
| 839 |
|
| 840 |
ast_audiohook_lock(&csth.whisper_audiohook); |
| 841 |
ast_audiohook_write_frame(&csth.whisper_audiohook, AST_AUDIOHOOK_DIRECTION_WRITE, f); |
| 842 |
ast_audiohook_unlock(&csth.whisper_audiohook); |
| 843 |
|
| 844 |
if (bridge_connected) { |
| 845 |
ast_audiohook_lock(&csth.bridge_whisper_audiohook); |
| 846 |
ast_audiohook_write_frame(&csth.bridge_whisper_audiohook, AST_AUDIOHOOK_DIRECTION_WRITE, f); |
| 847 |
ast_audiohook_unlock(&csth.bridge_whisper_audiohook); |
| 848 |
} |
| 849 |
|
| 850 |
ast_frfree(f); |
| 851 |
continue; |
| 852 |
} else if (ast_test_flag(flags, OPTION_WHISPER) && f->frametype == AST_FRAME_VOICE) { |
| 853 |
ast_audiohook_lock(&csth.whisper_audiohook); |
| 854 |
ast_audiohook_write_frame(&csth.whisper_audiohook, AST_AUDIOHOOK_DIRECTION_WRITE, f); |
| 855 |
ast_audiohook_unlock(&csth.whisper_audiohook); |
| 856 |
ast_frfree(f); |
| 857 |
continue; |
| 858 |
} |
| 859 |
|
| 860 |
res = (f->frametype == AST_FRAME_DTMF) ? f->subclass.integer : 0; |
| 861 |
ast_frfree(f); |
| 862 |
if (!res) |
| 863 |
continue; |
| 864 |
|
| 865 |
if (x == sizeof(inp)) |
| 866 |
x = 0; |
| 867 |
|
| 868 |
if (res < 0) { |
| 869 |
running = -1; |
| 870 |
break; |
| 871 |
} |
| 872 |
|
| 873 |
if (ast_test_flag(flags, OPTION_EXIT)) { |
| 874 |
char tmp[2]; |
| 875 |
tmp[0] = res; |
| 876 |
tmp[1] = '\0'; |
| 877 |
if (!ast_goto_if_exists(chan, exitcontext, tmp, 1)) { |
| 878 |
ast_debug(1, "Got DTMF %c, goto context %s\n", tmp[0], exitcontext); |
| 879 |
pbx_builtin_setvar_helper(chan, "SPY_CHANNEL", name); |
| 880 |
running = -2; |
| 881 |
break; |
| 882 |
} else { |
| 883 |
ast_debug(2, "Exit by single digit did not work in chanspy. Extension %s does not exist in context %s\n", tmp, exitcontext); |
| 884 |
} |
| 885 |
} else if (res >= '0' && res <= '9') { |
| 886 |
if (ast_test_flag(flags, OPTION_DTMF_SWITCH_MODES)) { |
| 887 |
change_spy_mode(res, flags); |
| 888 |
} else { |
| 889 |
inp[x++] = res; |
| 890 |
} |
| 891 |
} |
| 892 |
|
| 893 |
if (res == user_options->cycle) { |
| 894 |
running = 0; |
| 895 |
break; |
| 896 |
} else if (res == user_options->exit) { |
| 897 |
running = -2; |
| 898 |
break; |
| 899 |
} else if (res == user_options->volume) { |
| 900 |
if (!ast_strlen_zero(inp)) { |
| 901 |
running = atoi(inp); |
| 902 |
break; |
| 903 |
} |
| 904 |
|
| 905 |
(*volfactor)++; |
| 906 |
if (*volfactor > 4) |
| 907 |
*volfactor = -4; |
| 908 |
ast_verb(3, "Setting spy volume on %s to %d\n", ast_channel_name(chan), *volfactor); |
| 909 |
|
| 910 |
csth.volfactor = *volfactor; |
| 911 |
csth.spy_audiohook.options.read_volume = csth.volfactor; |
| 912 |
csth.spy_audiohook.options.write_volume = csth.volfactor; |
| 913 |
} |
| 914 |
} |
| 915 |
|
| 916 |
if (ast_test_flag(flags, OPTION_PRIVATE)) |
| 917 |
ast_channel_stop_silence_generator(chan, silgen); |
| 918 |
else |
| 919 |
ast_deactivate_generator(chan); |
| 920 |
|
| 921 |
ast_channel_clear_flag(chan, AST_FLAG_END_DTMF_ONLY); |
| 922 |
|
| 923 |
if (ast_test_flag(flags, OPTION_WHISPER | OPTION_BARGE | OPTION_DTMF_SWITCH_MODES)) { |
| 924 |
ast_audiohook_lock(&csth.whisper_audiohook); |
| 925 |
ast_audiohook_detach(&csth.whisper_audiohook); |
| 926 |
ast_audiohook_unlock(&csth.whisper_audiohook); |
| 927 |
ast_audiohook_destroy(&csth.whisper_audiohook); |
| 928 |
} |
| 929 |
|
| 930 |
if (ast_test_flag(flags, OPTION_BARGE | OPTION_DTMF_SWITCH_MODES)) { |
| 931 |
ast_audiohook_lock(&csth.bridge_whisper_audiohook); |
| 932 |
ast_audiohook_detach(&csth.bridge_whisper_audiohook); |
| 933 |
ast_audiohook_unlock(&csth.bridge_whisper_audiohook); |
| 934 |
ast_audiohook_destroy(&csth.bridge_whisper_audiohook); |
| 935 |
} |
| 936 |
|
| 937 |
ast_audiohook_lock(&csth.spy_audiohook); |
| 938 |
ast_audiohook_detach(&csth.spy_audiohook); |
| 939 |
ast_audiohook_unlock(&csth.spy_audiohook); |
| 940 |
ast_audiohook_destroy(&csth.spy_audiohook); |
| 941 |
|
| 942 |
ast_verb(2, "Done Spying on channel %s\n", name); |
| 943 |
publish_chanspy_message(chan, spyee_autochan->chan, 0); |
| 944 |
|
| 945 |
if (spyee_bridge_autochan) { |
| 946 |
ast_autochan_destroy(spyee_bridge_autochan); |
| 947 |
} |
| 948 |
|
| 949 |
return running; |
| 950 |
} |
| 951 |
|
| 952 |
static struct ast_autochan *next_channel(struct ast_channel_iterator *iter, |
| 953 |
struct ast_channel *chan) |
| 954 |
{ |
| 955 |
struct ast_channel *next; |
| 956 |
struct ast_autochan *autochan_store; |
| 957 |
const size_t pseudo_len = strlen("DAHDI/pseudo"); |
| 958 |
|
| 959 |
if (!iter) { |
| 960 |
return NULL; |
| 961 |
} |
| 962 |
|
| 963 |
for (; (next = ast_channel_iterator_next(iter)); ast_channel_unref(next)) { |
| 964 |
if (!strncmp(ast_channel_name(next), "DAHDI/pseudo", pseudo_len) |
| 965 |
|| next == chan) { |
| 966 |
continue; |
| 967 |
} |
| 968 |
|
| 969 |
autochan_store = ast_autochan_setup(next); |
| 970 |
ast_channel_unref(next); |
| 971 |
|
| 972 |
return autochan_store; |
| 973 |
} |
| 974 |
return NULL; |
| 975 |
} |
| 976 |
|
| 977 |
static int spy_sayname(struct ast_channel *chan, const char *mailbox, const char *context) |
| 978 |
{ |
| 979 |
char *mailbox_id; |
| 980 |
|
| 981 |
mailbox_id = ast_alloca(strlen(mailbox) + strlen(context) + 2); |
| 982 |
sprintf(mailbox_id, "%s@%s", mailbox, context); |
| 983 |
return ast_app_sayname(chan, mailbox_id); |
| 984 |
} |
| 985 |
|
| 986 |
static int common_exec(struct ast_channel *chan, struct ast_flags *flags, |
| 987 |
int volfactor, const int fd, struct spy_dtmf_options *user_options, |
| 988 |
const char *mygroup, const char *myenforced, const char *spec, const char *exten, |
| 989 |
const char *context, const char *mailbox, const char *name_context) |
| 990 |
{ |
| 991 |
char nameprefix[AST_NAME_STRLEN]; |
| 992 |
char exitcontext[AST_MAX_CONTEXT] = ""; |
| 993 |
signed char zero_volume = 0; |
| 994 |
int waitms; |
| 995 |
int res; |
| 996 |
int num_spied_upon = 1; |
| 997 |
struct ast_channel_iterator *iter = NULL; |
| 998 |
|
| 999 |
if (ast_test_flag(flags, OPTION_EXIT)) { |
| 1000 |
const char *c; |
| 1001 |
ast_channel_lock(chan); |
| 1002 |
if ((c = pbx_builtin_getvar_helper(chan, "SPY_EXIT_CONTEXT"))) { |
| 1003 |
ast_copy_string(exitcontext, c, sizeof(exitcontext)); |
| 1004 |
} else { |
| 1005 |
ast_copy_string(exitcontext, ast_channel_context(chan), sizeof(exitcontext)); |
| 1006 |
} |
| 1007 |
ast_channel_unlock(chan); |
| 1008 |
} |
| 1009 |
|
| 1010 |
if (!ast_test_flag(flags, OPTION_NOANSWER) && ast_channel_state(chan) != AST_STATE_UP) { |
| 1011 |
ast_answer(chan); |
| 1012 |
} |
| 1013 |
|
| 1014 |
ast_channel_set_flag(chan, AST_FLAG_SPYING); |
| 1015 |
|
| 1016 |
waitms = 100; |
| 1017 |
|
| 1018 |
for (;;) { |
| 1019 |
struct ast_autochan *autochan = NULL, *next_autochan = NULL; |
| 1020 |
struct ast_channel *prev = NULL; |
| 1021 |
|
| 1022 |
if (!ast_test_flag(flags, OPTION_QUIET) && num_spied_upon) { |
| 1023 |
res = ast_streamfile(chan, "beep", ast_channel_language(chan)); |
| 1024 |
if (!res) |
| 1025 |
res = ast_waitstream(chan, ""); |
| 1026 |
else if (res < 0) { |
| 1027 |
ast_channel_clear_flag(chan, AST_FLAG_SPYING); |
| 1028 |
break; |
| 1029 |
} |
| 1030 |
if (!ast_strlen_zero(exitcontext)) { |
| 1031 |
char tmp[2]; |
| 1032 |
tmp[0] = res; |
| 1033 |
tmp[1] = '\0'; |
| 1034 |
if (!ast_goto_if_exists(chan, exitcontext, tmp, 1)) |
| 1035 |
goto exit; |
| 1036 |
else |
| 1037 |
ast_debug(2, "Exit by single digit did not work in chanspy. Extension %s does not exist in context %s\n", tmp, exitcontext); |
| 1038 |
} |
| 1039 |
} |
| 1040 |
|
| 1041 |
|
| 1042 |
if (!ast_strlen_zero(spec)) { |
| 1043 |
if (ast_test_flag(flags, OPTION_UNIQUEID)) { |
| 1044 |
struct ast_channel *unique_chan; |
| 1045 |
|
| 1046 |
unique_chan = ast_channel_get_by_name(spec); |
| 1047 |
if (!unique_chan) { |
| 1048 |
res = -1; |
| 1049 |
goto exit; |
| 1050 |
} |
| 1051 |
iter = ast_channel_iterator_by_name_new(ast_channel_name(unique_chan), 0); |
| 1052 |
ast_channel_unref(unique_chan); |
| 1053 |
} else { |
| 1054 |
iter = ast_channel_iterator_by_name_new(spec, strlen(spec)); |
| 1055 |
} |
| 1056 |
} else if (!ast_strlen_zero(exten)) { |
| 1057 |
iter = ast_channel_iterator_by_exten_new(exten, context); |
| 1058 |
} else { |
| 1059 |
iter = ast_channel_iterator_all_new(); |
| 1060 |
} |
| 1061 |
|
| 1062 |
if (!iter) { |
| 1063 |
res = -1; |
| 1064 |
goto exit; |
| 1065 |
} |
| 1066 |
|
| 1067 |
res = ast_waitfordigit(chan, waitms); |
| 1068 |
if (res < 0) { |
| 1069 |
iter = ast_channel_iterator_destroy(iter); |
| 1070 |
ast_channel_clear_flag(chan, AST_FLAG_SPYING); |
| 1071 |
break; |
| 1072 |
} |
| 1073 |
if (!ast_strlen_zero(exitcontext)) { |
| 1074 |
char tmp[2]; |
| 1075 |
tmp[0] = res; |
| 1076 |
tmp[1] = '\0'; |
| 1077 |
if (!ast_goto_if_exists(chan, exitcontext, tmp, 1)) { |
| 1078 |
iter = ast_channel_iterator_destroy(iter); |
| 1079 |
goto exit; |
| 1080 |
} else { |
| 1081 |
ast_debug(2, "Exit by single digit did not work in chanspy. Extension %s does not exist in context %s\n", tmp, exitcontext); |
| 1082 |
} |
| 1083 |
} |
| 1084 |
|
| 1085 |
|
| 1086 |
waitms = 100; |
| 1087 |
num_spied_upon = 0; |
| 1088 |
|
| 1089 |
for (autochan = next_channel(iter, chan); |
| 1090 |
autochan; |
| 1091 |
prev = autochan->chan, |
| 1092 |
ast_autochan_destroy(autochan), |
| 1093 |
autochan = next_autochan ?: next_channel(iter, chan), |
| 1094 |
next_autochan = NULL) { |
| 1095 |
int igrp = !mygroup; |
| 1096 |
int ienf = !myenforced; |
| 1097 |
|
| 1098 |
if (autochan->chan == prev) { |
| 1099 |
ast_autochan_destroy(autochan); |
| 1100 |
break; |
| 1101 |
} |
| 1102 |
|
| 1103 |
if (ast_check_hangup(chan)) { |
| 1104 |
ast_autochan_destroy(autochan); |
| 1105 |
break; |
| 1106 |
} |
| 1107 |
|
| 1108 |
ast_autochan_channel_lock(autochan); |
| 1109 |
if (ast_test_flag(flags, OPTION_BRIDGED) |
| 1110 |
&& !ast_channel_is_bridged(autochan->chan)) { |
| 1111 |
ast_autochan_channel_unlock(autochan); |
| 1112 |
continue; |
| 1113 |
} |
| 1114 |
|
| 1115 |
if (ast_check_hangup(autochan->chan) |
| 1116 |
|| ast_test_flag(ast_channel_flags(autochan->chan), AST_FLAG_SPYING)) { |
| 1117 |
ast_autochan_channel_unlock(autochan); |
| 1118 |
continue; |
| 1119 |
} |
| 1120 |
ast_autochan_channel_unlock(autochan); |
| 1121 |
|
| 1122 |
if (mygroup) { |
| 1123 |
int num_groups = 0; |
| 1124 |
int num_mygroups = 0; |
| 1125 |
char dup_group[512]; |
| 1126 |
char dup_mygroup[512]; |
| 1127 |
char *groups[NUM_SPYGROUPS]; |
| 1128 |
char *mygroups[NUM_SPYGROUPS]; |
| 1129 |
const char *group = NULL; |
| 1130 |
int x; |
| 1131 |
int y; |
| 1132 |
ast_copy_string(dup_mygroup, mygroup, sizeof(dup_mygroup)); |
| 1133 |
num_mygroups = ast_app_separate_args(dup_mygroup, ':', mygroups, |
| 1134 |
ARRAY_LEN(mygroups)); |
| 1135 |
|
| 1136 |
|
| 1137 |
|
| 1138 |
ast_autochan_channel_lock(autochan); |
| 1139 |
if (ast_test_flag(flags, OPTION_DAHDI_SCAN)) { |
| 1140 |
group = pbx_builtin_getvar_helper(autochan->chan, "GROUP"); |
| 1141 |
} else { |
| 1142 |
group = pbx_builtin_getvar_helper(autochan->chan, "SPYGROUP"); |
| 1143 |
} |
| 1144 |
ast_autochan_channel_unlock(autochan); |
| 1145 |
|
| 1146 |
if (!ast_strlen_zero(group)) { |
| 1147 |
ast_copy_string(dup_group, group, sizeof(dup_group)); |
| 1148 |
num_groups = ast_app_separate_args(dup_group, ':', groups, |
| 1149 |
ARRAY_LEN(groups)); |
| 1150 |
} |
| 1151 |
|
| 1152 |
for (y = 0; y < num_mygroups; y++) { |
| 1153 |
for (x = 0; x < num_groups; x++) { |
| 1154 |
if (!strcmp(mygroups[y], groups[x])) { |
| 1155 |
igrp = 1; |
| 1156 |
break; |
| 1157 |
} |
| 1158 |
} |
| 1159 |
} |
| 1160 |
} |
| 1161 |
|
| 1162 |
if (!igrp) { |
| 1163 |
continue; |
| 1164 |
} |
| 1165 |
if (myenforced) { |
| 1166 |
char ext[AST_CHANNEL_NAME + 3]; |
| 1167 |
char buffer[512]; |
| 1168 |
char *end; |
| 1169 |
|
| 1170 |
snprintf(buffer, sizeof(buffer) - 1, ":%s:", myenforced); |
| 1171 |
|
| 1172 |
ast_autochan_channel_lock(autochan); |
| 1173 |
ast_copy_string(ext + 1, ast_channel_name(autochan->chan), sizeof(ext) - 1); |
| 1174 |
ast_autochan_channel_unlock(autochan); |
| 1175 |
if ((end = strchr(ext, '-'))) { |
| 1176 |
*end++ = ':'; |
| 1177 |
*end = '\0'; |
| 1178 |
} |
| 1179 |
|
| 1180 |
ext[0] = ':'; |
| 1181 |
|
| 1182 |
if (strcasestr(buffer, ext)) { |
| 1183 |
ienf = 1; |
| 1184 |
} |
| 1185 |
} |
| 1186 |
|
| 1187 |
if (!ienf) { |
| 1188 |
continue; |
| 1189 |
} |
| 1190 |
|
| 1191 |
if (!ast_test_flag(flags, OPTION_QUIET)) { |
| 1192 |
char peer_name[AST_NAME_STRLEN + 5]; |
| 1193 |
char *ptr, *s; |
| 1194 |
|
| 1195 |
strcpy(peer_name, "spy-"); |
| 1196 |
ast_autochan_channel_lock(autochan); |
| 1197 |
strncat(peer_name, ast_channel_name(autochan->chan), AST_NAME_STRLEN - 4 - 1); |
| 1198 |
ast_autochan_channel_unlock(autochan); |
| 1199 |
if ((ptr = strchr(peer_name, '/'))) { |
| 1200 |
*ptr++ = '\0'; |
| 1201 |
for (s = peer_name; s < ptr; s++) { |
| 1202 |
*s = tolower(*s); |
| 1203 |
} |
| 1204 |
if ((s = strchr(ptr, '-'))) { |
| 1205 |
*s = '\0'; |
| 1206 |
} |
| 1207 |
} |
| 1208 |
|
| 1209 |
if (ast_test_flag(flags, OPTION_NAME)) { |
| 1210 |
const char *local_context = S_OR(name_context, "default"); |
| 1211 |
const char *local_mailbox = S_OR(mailbox, ptr); |
| 1212 |
|
| 1213 |
if (local_mailbox) { |
| 1214 |
res = spy_sayname(chan, local_mailbox, local_context); |
| 1215 |
} else { |
| 1216 |
res = -1; |
| 1217 |
} |
| 1218 |
} |
| 1219 |
if (!ast_test_flag(flags, OPTION_NAME) || res < 0) { |
| 1220 |
int num; |
| 1221 |
if (!ast_test_flag(flags, OPTION_NOTECH)) { |
| 1222 |
if (ast_fileexists(peer_name, NULL, NULL) > 0) { |
| 1223 |
res = ast_streamfile(chan, peer_name, ast_channel_language(chan)); |
| 1224 |
if (!res) { |
| 1225 |
res = ast_waitstream(chan, ""); |
| 1226 |
} |
| 1227 |
if (res) { |
| 1228 |
ast_autochan_destroy(autochan); |
| 1229 |
break; |
| 1230 |
} |
| 1231 |
} else { |
| 1232 |
res = ast_say_character_str(chan, peer_name, "", ast_channel_language(chan), AST_SAY_CASE_NONE); |
| 1233 |
} |
| 1234 |
} |
| 1235 |
if (ptr && (num = atoi(ptr))) { |
| 1236 |
ast_say_digits(chan, num, "", ast_channel_language(chan)); |
| 1237 |
} |
| 1238 |
} |
| 1239 |
} |
| 1240 |
|
| 1241 |
res = channel_spy(chan, autochan, &volfactor, fd, user_options, flags, exitcontext); |
| 1242 |
num_spied_upon++; |
| 1243 |
|
| 1244 |
if (res == -1) { |
| 1245 |
ast_autochan_destroy(autochan); |
| 1246 |
iter = ast_channel_iterator_destroy(iter); |
| 1247 |
goto exit; |
| 1248 |
} else if (res == -2) { |
| 1249 |
res = 0; |
| 1250 |
ast_autochan_destroy(autochan); |
| 1251 |
iter = ast_channel_iterator_destroy(iter); |
| 1252 |
goto exit; |
| 1253 |
} else if (res > 1 && spec && !ast_test_flag(flags, OPTION_UNIQUEID)) { |
| 1254 |
struct ast_channel *next; |
| 1255 |
|
| 1256 |
snprintf(nameprefix, AST_NAME_STRLEN, "%s/%d", spec, res); |
| 1257 |
|
| 1258 |
if ((next = ast_channel_get_by_name_prefix(nameprefix, strlen(nameprefix)))) { |
| 1259 |
next_autochan = ast_autochan_setup(next); |
| 1260 |
next = ast_channel_unref(next); |
| 1261 |
} else { |
| 1262 |
|
| 1263 |
ast_autochan_channel_lock(autochan); |
| 1264 |
if (!ast_check_hangup(autochan->chan)) { |
| 1265 |
next_autochan = ast_autochan_setup(autochan->chan); |
| 1266 |
} else { |
| 1267 |
|
| 1268 |
next_autochan = NULL; |
| 1269 |
} |
| 1270 |
ast_autochan_channel_unlock(autochan); |
| 1271 |
} |
| 1272 |
} else if (res == 0 && ast_test_flag(flags, OPTION_EXITONHANGUP)) { |
| 1273 |
ast_autochan_destroy(autochan); |
| 1274 |
iter = ast_channel_iterator_destroy(iter); |
| 1275 |
goto exit; |
| 1276 |
} |
| 1277 |
} |
| 1278 |
|
| 1279 |
iter = ast_channel_iterator_destroy(iter); |
| 1280 |
|
| 1281 |
if (res == -1 || ast_check_hangup(chan)) |
| 1282 |
break; |
| 1283 |
if (ast_test_flag(flags, OPTION_STOP) && !next_autochan) { |
| 1284 |
break; |
| 1285 |
} |
| 1286 |
} |
| 1287 |
exit: |
| 1288 |
|
| 1289 |
ast_channel_clear_flag(chan, AST_FLAG_SPYING); |
| 1290 |
|
| 1291 |
ast_channel_setoption(chan, AST_OPTION_TXGAIN, &zero_volume, sizeof(zero_volume), 0); |
| 1292 |
|
| 1293 |
return res; |
| 1294 |
} |
| 1295 |
|
| 1296 |
static int chanspy_exec(struct ast_channel *chan, const char *data) |
| 1297 |
{ |
| 1298 |
char *myenforced = NULL; |
| 1299 |
char *mygroup = NULL; |
| 1300 |
char *recbase = NULL; |
| 1301 |
int fd = 0; |
| 1302 |
struct ast_flags flags; |
| 1303 |
struct spy_dtmf_options user_options = { |
| 1304 |
.cycle = '*', |
| 1305 |
.volume = '#', |
| 1306 |
.exit = '\0', |
| 1307 |
}; |
| 1308 |
RAII_VAR(struct ast_format *, oldwf, NULL, ao2_cleanup); |
| 1309 |
int volfactor = 0; |
| 1310 |
int res; |
| 1311 |
char *mailbox = NULL; |
| 1312 |
char *name_context = NULL; |
| 1313 |
AST_DECLARE_APP_ARGS(args, |
| 1314 |
AST_APP_ARG(spec); |
| 1315 |
AST_APP_ARG(options); |
| 1316 |
); |
| 1317 |
char *opts[OPT_ARG_ARRAY_SIZE]; |
| 1318 |
char *parse = ast_strdupa(data); |
| 1319 |
|
| 1320 |
AST_STANDARD_APP_ARGS(args, parse); |
| 1321 |
|
| 1322 |
if (args.spec && !strcmp(args.spec, "all")) |
| 1323 |
args.spec = NULL; |
| 1324 |
|
| 1325 |
if (args.options) { |
| 1326 |
char tmp; |
| 1327 |
ast_app_parse_options(spy_opts, &flags, opts, args.options); |
| 1328 |
if (ast_test_flag(&flags, OPTION_GROUP)) |
| 1329 |
mygroup = opts[OPT_ARG_GROUP]; |
| 1330 |
|
| 1331 |
if (ast_test_flag(&flags, OPTION_RECORD) && |
| 1332 |
!(recbase = opts[OPT_ARG_RECORD])) |
| 1333 |
recbase = "chanspy"; |
| 1334 |
|
| 1335 |
if (ast_test_flag(&flags, OPTION_DTMF_EXIT) && opts[OPT_ARG_EXIT]) { |
| 1336 |
tmp = opts[OPT_ARG_EXIT][0]; |
| 1337 |
if (strchr("0123456789*#", tmp) && tmp != '\0') { |
| 1338 |
user_options.exit = tmp; |
| 1339 |
} else { |
| 1340 |
ast_log(LOG_NOTICE, "Argument for option 'x' must be a valid DTMF digit.\n"); |
| 1341 |
} |
| 1342 |
} |
| 1343 |
|
| 1344 |
if (ast_test_flag(&flags, OPTION_DTMF_CYCLE) && opts[OPT_ARG_CYCLE]) { |
| 1345 |
tmp = opts[OPT_ARG_CYCLE][0]; |
| 1346 |
if (strchr("0123456789*#", tmp) && tmp != '\0') { |
| 1347 |
user_options.cycle = tmp; |
| 1348 |
} else { |
| 1349 |
ast_log(LOG_NOTICE, "Argument for option 'c' must be a valid DTMF digit.\n"); |
| 1350 |
} |
| 1351 |
} |
| 1352 |
|
| 1353 |
if (ast_test_flag(&flags, OPTION_VOLUME) && opts[OPT_ARG_VOLUME]) { |
| 1354 |
int vol; |
| 1355 |
|
| 1356 |
if ((sscanf(opts[OPT_ARG_VOLUME], "%30d", &vol) != 1) || (vol > 4) || (vol < -4)) |
| 1357 |
ast_log(LOG_NOTICE, "Volume factor must be a number between -4 and 4\n"); |
| 1358 |
else |
| 1359 |
volfactor = vol; |
| 1360 |
} |
| 1361 |
|
| 1362 |
if (ast_test_flag(&flags, OPTION_PRIVATE)) |
| 1363 |
ast_set_flag(&flags, OPTION_WHISPER); |
| 1364 |
|
| 1365 |
if (ast_test_flag(&flags, OPTION_ENFORCED)) |
| 1366 |
myenforced = opts[OPT_ARG_ENFORCED]; |
| 1367 |
|
| 1368 |
if (ast_test_flag(&flags, OPTION_NAME)) { |
| 1369 |
if (!ast_strlen_zero(opts[OPT_ARG_NAME])) { |
| 1370 |
char *delimiter; |
| 1371 |
if ((delimiter = strchr(opts[OPT_ARG_NAME], '@'))) { |
| 1372 |
mailbox = opts[OPT_ARG_NAME]; |
| 1373 |
*delimiter++ = '\0'; |
| 1374 |
name_context = delimiter; |
| 1375 |
} else { |
| 1376 |
mailbox = opts[OPT_ARG_NAME]; |
| 1377 |
} |
| 1378 |
} |
| 1379 |
} |
| 1380 |
} else { |
| 1381 |
ast_clear_flag(&flags, AST_FLAGS_ALL); |
| 1382 |
} |
| 1383 |
|
| 1384 |
oldwf = ao2_bump(ast_channel_writeformat(chan)); |
| 1385 |
if (ast_set_write_format(chan, ast_format_slin) < 0) { |
| 1386 |
ast_log(LOG_ERROR, "Could Not Set Write Format.\n"); |
| 1387 |
return -1; |
| 1388 |
} |
| 1389 |
|
| 1390 |
if (recbase) { |
| 1391 |
char filename[PATH_MAX]; |
| 1392 |
|
| 1393 |
snprintf(filename, sizeof(filename), "%s/%s.%d.raw", ast_config_AST_MONITOR_DIR, recbase, (int) time(NULL)); |
| 1394 |
if ((fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, AST_FILE_MODE)) <= 0) { |
| 1395 |
ast_log(LOG_WARNING, "Cannot open '%s' for recording\n", filename); |
| 1396 |
fd = 0; |
| 1397 |
} |
| 1398 |
} |
| 1399 |
|
| 1400 |
res = common_exec(chan, &flags, volfactor, fd, &user_options, mygroup, myenforced, args.spec, NULL, NULL, mailbox, name_context); |
| 1401 |
|
| 1402 |
if (fd) |
| 1403 |
close(fd); |
| 1404 |
|
| 1405 |
if (oldwf && ast_set_write_format(chan, oldwf) < 0) |
| 1406 |
ast_log(LOG_ERROR, "Could Not Set Write Format.\n"); |
| 1407 |
|
| 1408 |
if (ast_test_flag(&flags, OPTION_EXITONHANGUP)) { |
| 1409 |
ast_verb(3, "Stopped spying due to the spied-on channel hanging up.\n"); |
| 1410 |
} |
| 1411 |
|
| 1412 |
return res; |
| 1413 |
} |
| 1414 |
|
| 1415 |
static int extenspy_exec(struct ast_channel *chan, const char *data) |
| 1416 |
{ |
| 1417 |
char *ptr, *exten = NULL; |
| 1418 |
char *mygroup = NULL; |
| 1419 |
char *recbase = NULL; |
| 1420 |
int fd = 0; |
| 1421 |
struct ast_flags flags; |
| 1422 |
struct spy_dtmf_options user_options = { |
| 1423 |
.cycle = '*', |
| 1424 |
.volume = '#', |
| 1425 |
.exit = '\0', |
| 1426 |
}; |
| 1427 |
RAII_VAR(struct ast_format *, oldwf, NULL, ao2_cleanup); |
| 1428 |
int volfactor = 0; |
| 1429 |
int res; |
| 1430 |
char *mailbox = NULL; |
| 1431 |
char *name_context = NULL; |
| 1432 |
AST_DECLARE_APP_ARGS(args, |
| 1433 |
AST_APP_ARG(context); |
| 1434 |
AST_APP_ARG(options); |
| 1435 |
); |
| 1436 |
char *parse = ast_strdupa(data); |
| 1437 |
|
| 1438 |
AST_STANDARD_APP_ARGS(args, parse); |
| 1439 |
|
| 1440 |
if (!ast_strlen_zero(args.context) && (ptr = strchr(args.context, '@'))) { |
| 1441 |
exten = args.context; |
| 1442 |
*ptr++ = '\0'; |
| 1443 |
args.context = ptr; |
| 1444 |
} |
| 1445 |
if (ast_strlen_zero(args.context)) |
| 1446 |
args.context = ast_strdupa(ast_channel_context(chan)); |
| 1447 |
|
| 1448 |
if (args.options) { |
| 1449 |
char *opts[OPT_ARG_ARRAY_SIZE]; |
| 1450 |
char tmp; |
| 1451 |
|
| 1452 |
ast_app_parse_options(spy_opts, &flags, opts, args.options); |
| 1453 |
if (ast_test_flag(&flags, OPTION_GROUP)) |
| 1454 |
mygroup = opts[OPT_ARG_GROUP]; |
| 1455 |
|
| 1456 |
if (ast_test_flag(&flags, OPTION_RECORD) && |
| 1457 |
!(recbase = opts[OPT_ARG_RECORD])) |
| 1458 |
recbase = "chanspy"; |
| 1459 |
|
| 1460 |
if (ast_test_flag(&flags, OPTION_DTMF_EXIT) && opts[OPT_ARG_EXIT]) { |
| 1461 |
tmp = opts[OPT_ARG_EXIT][0]; |
| 1462 |
if (strchr("0123456789*#", tmp) && tmp != '\0') { |
| 1463 |
user_options.exit = tmp; |
| 1464 |
} else { |
| 1465 |
ast_log(LOG_NOTICE, "Argument for option 'x' must be a valid DTMF digit.\n"); |
| 1466 |
} |
| 1467 |
} |
| 1468 |
|
| 1469 |
if (ast_test_flag(&flags, OPTION_DTMF_CYCLE) && opts[OPT_ARG_CYCLE]) { |
| 1470 |
tmp = opts[OPT_ARG_CYCLE][0]; |
| 1471 |
if (strchr("0123456789*#", tmp) && tmp != '\0') { |
| 1472 |
user_options.cycle = tmp; |
| 1473 |
} else { |
| 1474 |
ast_log(LOG_NOTICE, "Argument for option 'c' must be a valid DTMF digit.\n"); |
| 1475 |
} |
| 1476 |
} |
| 1477 |
|
| 1478 |
if (ast_test_flag(&flags, OPTION_VOLUME) && opts[OPT_ARG_VOLUME]) { |
| 1479 |
int vol; |
| 1480 |
|
| 1481 |
if ((sscanf(opts[OPT_ARG_VOLUME], "%30d", &vol) != 1) || (vol > 4) || (vol < -4)) |
| 1482 |
ast_log(LOG_NOTICE, "Volume factor must be a number between -4 and 4\n"); |
| 1483 |
else |
| 1484 |
volfactor = vol; |
| 1485 |
} |
| 1486 |
|
| 1487 |
if (ast_test_flag(&flags, OPTION_PRIVATE)) |
| 1488 |
ast_set_flag(&flags, OPTION_WHISPER); |
| 1489 |
|
| 1490 |
if (ast_test_flag(&flags, OPTION_NAME)) { |
| 1491 |
if (!ast_strlen_zero(opts[OPT_ARG_NAME])) { |
| 1492 |
char *delimiter; |
| 1493 |
if ((delimiter = strchr(opts[OPT_ARG_NAME], '@'))) { |
| 1494 |
mailbox = opts[OPT_ARG_NAME]; |
| 1495 |
*delimiter++ = '\0'; |
| 1496 |
name_context = delimiter; |
| 1497 |
} else { |
| 1498 |
mailbox = opts[OPT_ARG_NAME]; |
| 1499 |
} |
| 1500 |
} |
| 1501 |
} |
| 1502 |
|
| 1503 |
} else { |
| 1504 |
|
| 1505 |
ast_clear_flag(&flags, AST_FLAGS_ALL); |
| 1506 |
} |
| 1507 |
|
| 1508 |
oldwf = ao2_bump(ast_channel_writeformat(chan)); |
| 1509 |
if (ast_set_write_format(chan, ast_format_slin) < 0) { |
| 1510 |
ast_log(LOG_ERROR, "Could Not Set Write Format.\n"); |
| 1511 |
return -1; |
| 1512 |
} |
| 1513 |
|
| 1514 |
if (recbase) { |
| 1515 |
char filename[PATH_MAX]; |
| 1516 |
|
| 1517 |
snprintf(filename, sizeof(filename), "%s/%s.%d.raw", ast_config_AST_MONITOR_DIR, recbase, (int) time(NULL)); |
| 1518 |
if ((fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, AST_FILE_MODE)) <= 0) { |
| 1519 |
ast_log(LOG_WARNING, "Cannot open '%s' for recording\n", filename); |
| 1520 |
fd = 0; |
| 1521 |
} |
| 1522 |
} |
| 1523 |
|
| 1524 |
|
| 1525 |
res = common_exec(chan, &flags, volfactor, fd, &user_options, mygroup, NULL, NULL, exten, args.context, mailbox, name_context); |
| 1526 |
|
| 1527 |
if (fd) |
| 1528 |
close(fd); |
| 1529 |
|
| 1530 |
if (oldwf && ast_set_write_format(chan, oldwf) < 0) |
| 1531 |
ast_log(LOG_ERROR, "Could Not Set Write Format.\n"); |
| 1532 |
|
| 1533 |
return res; |
| 1534 |
} |
| 1535 |
|
| 1536 |
static int dahdiscan_exec(struct ast_channel *chan, const char *data) |
| 1537 |
{ |
| 1538 |
const char *spec = "DAHDI"; |
| 1539 |
struct ast_flags flags = {0}; |
| 1540 |
struct spy_dtmf_options user_options = { |
| 1541 |
.cycle = '#', |
| 1542 |
.volume = '\0', |
| 1543 |
.exit = '*', |
| 1544 |
}; |
| 1545 |
struct ast_format *oldwf; |
| 1546 |
int res; |
| 1547 |
char *mygroup = NULL; |
| 1548 |
|
| 1549 |
|
| 1550 |
ast_clear_flag(&flags, AST_FLAGS_ALL); |
| 1551 |
|
| 1552 |
if (!ast_strlen_zero(data)) { |
| 1553 |
mygroup = ast_strdupa(data); |
| 1554 |
} |
| 1555 |
ast_set_flag(&flags, OPTION_DTMF_EXIT); |
| 1556 |
ast_set_flag(&flags, OPTION_DTMF_CYCLE); |
| 1557 |
ast_set_flag(&flags, OPTION_DAHDI_SCAN); |
| 1558 |
|
| 1559 |
oldwf = ao2_bump(ast_channel_writeformat(chan)); |
| 1560 |
if (ast_set_write_format(chan, ast_format_slin) < 0) { |
| 1561 |
ast_log(LOG_ERROR, "Could Not Set Write Format.\n"); |
| 1562 |
ao2_cleanup(oldwf); |
| 1563 |
return -1; |
| 1564 |
} |
| 1565 |
|
| 1566 |
res = common_exec(chan, &flags, 0, 0, &user_options, mygroup, NULL, spec, NULL, NULL, NULL, NULL); |
| 1567 |
|
| 1568 |
if (oldwf && ast_set_write_format(chan, oldwf) < 0) |
| 1569 |
ast_log(LOG_ERROR, "Could Not Set Write Format.\n"); |
| 1570 |
ao2_cleanup(oldwf); |
| 1571 |
|
| 1572 |
return res; |
| 1573 |
} |
| 1574 |
|
| 1575 |
static int unload_module(void) |
| 1576 |
{ |
| 1577 |
int res = 0; |
| 1578 |
|
| 1579 |
res |= ast_unregister_application(app_chan); |
| 1580 |
res |= ast_unregister_application(app_ext); |
| 1581 |
res |= ast_unregister_application(app_dahdiscan); |
| 1582 |
|
| 1583 |
return res; |
| 1584 |
} |
| 1585 |
|
| 1586 |
static int load_module(void) |
| 1587 |
{ |
| 1588 |
int res = 0; |
| 1589 |
|
| 1590 |
res |= ast_register_application_xml(app_chan, chanspy_exec); |
| 1591 |
res |= ast_register_application_xml(app_ext, extenspy_exec); |
| 1592 |
res |= ast_register_application_xml(app_dahdiscan, dahdiscan_exec); |
| 1593 |
|
| 1594 |
return res; |
| 1595 |
} |
| 1596 |
|
| 1597 |
AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Listen to the audio of an active channel"); |