| 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 |
#include "asterisk.h" |
| 33 |
|
| 34 |
#include <math.h> |
| 35 |
#include <signal.h> |
| 36 |
#include <sys/time.h> |
| 37 |
#include <sys/wait.h> |
| 38 |
#include <sys/stat.h> |
| 39 |
#include <pthread.h> |
| 40 |
|
| 41 |
#include "asterisk/paths.h" |
| 42 |
#include "asterisk/network.h" |
| 43 |
#include "asterisk/file.h" |
| 44 |
#include "asterisk/channel.h" |
| 45 |
#include "asterisk/pbx.h" |
| 46 |
#include "asterisk/module.h" |
| 47 |
#include "asterisk/astdb.h" |
| 48 |
#include "asterisk/callerid.h" |
| 49 |
#include "asterisk/cli.h" |
| 50 |
#include "asterisk/image.h" |
| 51 |
#include "asterisk/say.h" |
| 52 |
#include "asterisk/app.h" |
| 53 |
#include "asterisk/dsp.h" |
| 54 |
#include "asterisk/musiconhold.h" |
| 55 |
#include "asterisk/utils.h" |
| 56 |
#include "asterisk/lock.h" |
| 57 |
#include "asterisk/strings.h" |
| 58 |
#include "asterisk/manager.h" |
| 59 |
#include "asterisk/ast_version.h" |
| 60 |
#include "asterisk/speech.h" |
| 61 |
#include "asterisk/manager.h" |
| 62 |
#include "asterisk/term.h" |
| 63 |
#include "asterisk/xmldoc.h" |
| 64 |
#include "asterisk/srv.h" |
| 65 |
#include "asterisk/test.h" |
| 66 |
#include "asterisk/netsock2.h" |
| 67 |
#include "asterisk/stasis_channels.h" |
| 68 |
#include "asterisk/stasis_message_router.h" |
| 69 |
#include "asterisk/format_cache.h" |
| 70 |
|
| 71 |
#define AST_API_MODULE |
| 72 |
#include "asterisk/agi.h" |
| 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 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
|
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
|
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
|
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
|
| 832 |
|
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
|
| 841 |
|
| 842 |
|
| 843 |
|
| 844 |
|
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
|
| 850 |
|
| 851 |
|
| 852 |
|
| 853 |
|
| 854 |
|
| 855 |
|
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
|
| 860 |
|
| 861 |
|
| 862 |
|
| 863 |
|
| 864 |
|
| 865 |
|
| 866 |
|
| 867 |
|
| 868 |
|
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
|
| 873 |
|
| 874 |
|
| 875 |
|
| 876 |
|
| 877 |
|
| 878 |
|
| 879 |
|
| 880 |
|
| 881 |
|
| 882 |
|
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
|
| 904 |
|
| 905 |
|
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
|
| 911 |
|
| 912 |
|
| 913 |
|
| 914 |
|
| 915 |
|
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
|
| 922 |
|
| 923 |
|
| 924 |
|
| 925 |
|
| 926 |
|
| 927 |
|
| 928 |
|
| 929 |
|
| 930 |
|
| 931 |
|
| 932 |
|
| 933 |
|
| 934 |
|
| 935 |
|
| 936 |
|
| 937 |
|
| 938 |
|
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
|
| 952 |
|
| 953 |
|
| 954 |
|
| 955 |
|
| 956 |
|
| 957 |
|
| 958 |
|
| 959 |
|
| 960 |
|
| 961 |
|
| 962 |
|
| 963 |
|
| 964 |
|
| 965 |
|
| 966 |
|
| 967 |
|
| 968 |
|
| 969 |
|
| 970 |
|
| 971 |
|
| 972 |
|
| 973 |
|
| 974 |
|
| 975 |
|
| 976 |
|
| 977 |
|
| 978 |
|
| 979 |
|
| 980 |
|
| 981 |
|
| 982 |
|
| 983 |
|
| 984 |
|
| 985 |
|
| 986 |
|
| 987 |
|
| 988 |
|
| 989 |
|
| 990 |
|
| 991 |
|
| 992 |
|
| 993 |
|
| 994 |
|
| 995 |
|
| 996 |
|
| 997 |
|
| 998 |
|
| 999 |
|
| 1000 |
|
| 1001 |
|
| 1002 |
|
| 1003 |
|
| 1004 |
|
| 1005 |
|
| 1006 |
|
| 1007 |
|
| 1008 |
|
| 1009 |
|
| 1010 |
|
| 1011 |
|
| 1012 |
|
| 1013 |
|
| 1014 |
|
| 1015 |
|
| 1016 |
|
| 1017 |
|
| 1018 |
|
| 1019 |
|
| 1020 |
|
| 1021 |
|
| 1022 |
|
| 1023 |
|
| 1024 |
|
| 1025 |
|
| 1026 |
|
| 1027 |
|
| 1028 |
|
| 1029 |
|
| 1030 |
|
| 1031 |
|
| 1032 |
|
| 1033 |
|
| 1034 |
|
| 1035 |
|
| 1036 |
|
| 1037 |
|
| 1038 |
|
| 1039 |
|
| 1040 |
|
| 1041 |
|
| 1042 |
|
| 1043 |
|
| 1044 |
|
| 1045 |
|
| 1046 |
|
| 1047 |
|
| 1048 |
|
| 1049 |
|
| 1050 |
|
| 1051 |
|
| 1052 |
|
| 1053 |
|
| 1054 |
|
| 1055 |
|
| 1056 |
|
| 1057 |
|
| 1058 |
|
| 1059 |
|
| 1060 |
|
| 1061 |
|
| 1062 |
|
| 1063 |
|
| 1064 |
|
| 1065 |
|
| 1066 |
|
| 1067 |
|
| 1068 |
|
| 1069 |
|
| 1070 |
|
| 1071 |
|
| 1072 |
|
| 1073 |
|
| 1074 |
|
| 1075 |
|
| 1076 |
|
| 1077 |
|
| 1078 |
|
| 1079 |
|
| 1080 |
|
| 1081 |
|
| 1082 |
|
| 1083 |
|
| 1084 |
|
| 1085 |
|
| 1086 |
|
| 1087 |
|
| 1088 |
|
| 1089 |
|
| 1090 |
|
| 1091 |
|
| 1092 |
|
| 1093 |
|
| 1094 |
|
| 1095 |
|
| 1096 |
|
| 1097 |
|
| 1098 |
|
| 1099 |
|
| 1100 |
|
| 1101 |
|
| 1102 |
|
| 1103 |
|
| 1104 |
|
| 1105 |
|
| 1106 |
|
| 1107 |
|
| 1108 |
|
| 1109 |
|
| 1110 |
|
| 1111 |
|
| 1112 |
|
| 1113 |
|
| 1114 |
|
| 1115 |
|
| 1116 |
|
| 1117 |
|
| 1118 |
|
| 1119 |
|
| 1120 |
|
| 1121 |
|
| 1122 |
|
| 1123 |
|
| 1124 |
|
| 1125 |
|
| 1126 |
|
| 1127 |
|
| 1128 |
|
| 1129 |
|
| 1130 |
|
| 1131 |
|
| 1132 |
|
| 1133 |
|
| 1134 |
|
| 1135 |
|
| 1136 |
|
| 1137 |
|
| 1138 |
|
| 1139 |
|
| 1140 |
|
| 1141 |
|
| 1142 |
|
| 1143 |
|
| 1144 |
|
| 1145 |
|
| 1146 |
|
| 1147 |
|
| 1148 |
|
| 1149 |
|
| 1150 |
|
| 1151 |
|
| 1152 |
|
| 1153 |
|
| 1154 |
|
| 1155 |
|
| 1156 |
|
| 1157 |
|
| 1158 |
|
| 1159 |
|
| 1160 |
|
| 1161 |
|
| 1162 |
|
| 1163 |
|
| 1164 |
|
| 1165 |
|
| 1166 |
|
| 1167 |
|
| 1168 |
|
| 1169 |
|
| 1170 |
|
| 1171 |
|
| 1172 |
|
| 1173 |
|
| 1174 |
|
| 1175 |
|
| 1176 |
|
| 1177 |
|
| 1178 |
|
| 1179 |
|
| 1180 |
|
| 1181 |
|
| 1182 |
|
| 1183 |
|
| 1184 |
|
| 1185 |
|
| 1186 |
|
| 1187 |
|
| 1188 |
|
| 1189 |
|
| 1190 |
|
| 1191 |
|
| 1192 |
|
| 1193 |
|
| 1194 |
|
| 1195 |
|
| 1196 |
|
| 1197 |
|
| 1198 |
|
| 1199 |
|
| 1200 |
|
| 1201 |
|
| 1202 |
|
| 1203 |
|
| 1204 |
|
| 1205 |
|
| 1206 |
|
| 1207 |
|
| 1208 |
|
| 1209 |
|
| 1210 |
|
| 1211 |
|
| 1212 |
|
| 1213 |
|
| 1214 |
|
| 1215 |
|
| 1216 |
|
| 1217 |
|
| 1218 |
|
| 1219 |
|
| 1220 |
|
| 1221 |
|
| 1222 |
|
| 1223 |
|
| 1224 |
|
| 1225 |
|
| 1226 |
|
| 1227 |
|
| 1228 |
|
| 1229 |
|
| 1230 |
|
| 1231 |
|
| 1232 |
|
| 1233 |
|
| 1234 |
|
| 1235 |
|
| 1236 |
|
| 1237 |
|
| 1238 |
|
| 1239 |
|
| 1240 |
|
| 1241 |
|
| 1242 |
|
| 1243 |
|
| 1244 |
|
| 1245 |
|
| 1246 |
|
| 1247 |
|
| 1248 |
|
| 1249 |
|
| 1250 |
|
| 1251 |
|
| 1252 |
|
| 1253 |
|
| 1254 |
|
| 1255 |
|
| 1256 |
|
| 1257 |
|
| 1258 |
|
| 1259 |
|
| 1260 |
|
| 1261 |
|
| 1262 |
|
| 1263 |
|
| 1264 |
|
| 1265 |
|
| 1266 |
|
| 1267 |
|
| 1268 |
|
| 1269 |
|
| 1270 |
|
| 1271 |
|
| 1272 |
|
| 1273 |
|
| 1274 |
|
| 1275 |
|
| 1276 |
|
| 1277 |
|
| 1278 |
|
| 1279 |
|
| 1280 |
|
| 1281 |
|
| 1282 |
|
| 1283 |
|
| 1284 |
|
| 1285 |
|
| 1286 |
|
| 1287 |
|
| 1288 |
|
| 1289 |
|
| 1290 |
|
| 1291 |
|
| 1292 |
|
| 1293 |
|
| 1294 |
|
| 1295 |
|
| 1296 |
|
| 1297 |
|
| 1298 |
|
| 1299 |
|
| 1300 |
|
| 1301 |
|
| 1302 |
|
| 1303 |
|
| 1304 |
|
| 1305 |
|
| 1306 |
|
| 1307 |
|
| 1308 |
|
| 1309 |
|
| 1310 |
|
| 1311 |
|
| 1312 |
|
| 1313 |
|
| 1314 |
|
| 1315 |
|
| 1316 |
|
| 1317 |
|
| 1318 |
|
| 1319 |
|
| 1320 |
|
| 1321 |
|
| 1322 |
|
| 1323 |
|
| 1324 |
|
| 1325 |
|
| 1326 |
|
| 1327 |
|
| 1328 |
|
| 1329 |
|
| 1330 |
|
| 1331 |
|
| 1332 |
|
| 1333 |
|
| 1334 |
|
| 1335 |
|
| 1336 |
|
| 1337 |
|
| 1338 |
|
| 1339 |
|
| 1340 |
|
| 1341 |
|
| 1342 |
|
| 1343 |
|
| 1344 |
|
| 1345 |
|
| 1346 |
|
| 1347 |
|
| 1348 |
|
| 1349 |
|
| 1350 |
|
| 1351 |
|
| 1352 |
|
| 1353 |
|
| 1354 |
|
| 1355 |
|
| 1356 |
|
| 1357 |
|
| 1358 |
|
| 1359 |
|
| 1360 |
|
| 1361 |
|
| 1362 |
|
| 1363 |
|
| 1364 |
|
| 1365 |
|
| 1366 |
|
| 1367 |
|
| 1368 |
|
| 1369 |
|
| 1370 |
|
| 1371 |
|
| 1372 |
|
| 1373 |
|
| 1374 |
|
| 1375 |
|
| 1376 |
|
| 1377 |
|
| 1378 |
|
| 1379 |
|
| 1380 |
|
| 1381 |
|
| 1382 |
|
| 1383 |
|
| 1384 |
|
| 1385 |
|
| 1386 |
|
| 1387 |
|
| 1388 |
|
| 1389 |
|
| 1390 |
|
| 1391 |
|
| 1392 |
|
| 1393 |
|
| 1394 |
|
| 1395 |
|
| 1396 |
|
| 1397 |
|
| 1398 |
|
| 1399 |
|
| 1400 |
|
| 1401 |
|
| 1402 |
|
| 1403 |
|
| 1404 |
|
| 1405 |
|
| 1406 |
|
| 1407 |
|
| 1408 |
|
| 1409 |
|
| 1410 |
|
| 1411 |
|
| 1412 |
|
| 1413 |
|
| 1414 |
|
| 1415 |
|
| 1416 |
|
| 1417 |
|
| 1418 |
|
| 1419 |
|
| 1420 |
|
| 1421 |
|
| 1422 |
|
| 1423 |
|
| 1424 |
|
| 1425 |
|
| 1426 |
|
| 1427 |
|
| 1428 |
|
| 1429 |
|
| 1430 |
|
| 1431 |
|
| 1432 |
|
| 1433 |
|
| 1434 |
|
| 1435 |
|
| 1436 |
|
| 1437 |
|
| 1438 |
|
| 1439 |
|
| 1440 |
|
| 1441 |
|
| 1442 |
|
| 1443 |
|
| 1444 |
|
| 1445 |
|
| 1446 |
|
| 1447 |
|
| 1448 |
|
| 1449 |
|
| 1450 |
|
| 1451 |
|
| 1452 |
|
| 1453 |
|
| 1454 |
|
| 1455 |
|
| 1456 |
|
| 1457 |
|
| 1458 |
|
| 1459 |
|
| 1460 |
|
| 1461 |
|
| 1462 |
|
| 1463 |
|
| 1464 |
|
| 1465 |
|
| 1466 |
|
| 1467 |
|
| 1468 |
|
| 1469 |
|
| 1470 |
|
| 1471 |
|
| 1472 |
|
| 1473 |
|
| 1474 |
|
| 1475 |
|
| 1476 |
|
| 1477 |
|
| 1478 |
|
| 1479 |
|
| 1480 |
|
| 1481 |
|
| 1482 |
|
| 1483 |
|
| 1484 |
|
| 1485 |
|
| 1486 |
|
| 1487 |
|
| 1488 |
|
| 1489 |
|
| 1490 |
|
| 1491 |
|
| 1492 |
|
| 1493 |
|
| 1494 |
|
| 1495 |
|
| 1496 |
|
| 1497 |
|
| 1498 |
|
| 1499 |
|
| 1500 |
|
| 1501 |
|
| 1502 |
|
| 1503 |
|
| 1504 |
|
| 1505 |
|
| 1506 |
|
| 1507 |
|
| 1508 |
|
| 1509 |
|
| 1510 |
|
| 1511 |
|
| 1512 |
|
| 1513 |
|
| 1514 |
|
| 1515 |
|
| 1516 |
|
| 1517 |
|
| 1518 |
|
| 1519 |
|
| 1520 |
|
| 1521 |
|
| 1522 |
|
| 1523 |
|
| 1524 |
|
| 1525 |
|
| 1526 |
|
| 1527 |
|
| 1528 |
|
| 1529 |
|
| 1530 |
|
| 1531 |
|
| 1532 |
|
| 1533 |
|
| 1534 |
|
| 1535 |
|
| 1536 |
|
| 1537 |
|
| 1538 |
|
| 1539 |
|
| 1540 |
|
| 1541 |
|
| 1542 |
|
| 1543 |
|
| 1544 |
|
| 1545 |
|
| 1546 |
|
| 1547 |
|
| 1548 |
#define MAX_ARGS 128 |
| 1549 |
#define MAX_CMD_LEN 80 |
| 1550 |
#define AGI_NANDFS_RETRY 3 |
| 1551 |
#define AGI_BUF_LEN 8192 |
| 1552 |
#define SRV_PREFIX "_agi._tcp." |
| 1553 |
|
| 1554 |
static char *app = "AGI"; |
| 1555 |
|
| 1556 |
static char *eapp = "EAGI"; |
| 1557 |
|
| 1558 |
static char *deadapp = "DeadAGI"; |
| 1559 |
|
| 1560 |
static int agidebug = 0; |
| 1561 |
|
| 1562 |
#define TONE_BLOCK_SIZE 200 |
| 1563 |
|
| 1564 |
|
| 1565 |
#define MAX_AGI_CONNECT 2000 |
| 1566 |
|
| 1567 |
#define AGI_PORT 4573 |
| 1568 |
|
| 1569 |
|
| 1570 |
#define ASYNC_AGI_BREAK 3 |
| 1571 |
|
| 1572 |
enum agi_result { |
| 1573 |
AGI_RESULT_FAILURE = -1, |
| 1574 |
AGI_RESULT_SUCCESS, |
| 1575 |
AGI_RESULT_SUCCESS_FAST, |
| 1576 |
AGI_RESULT_SUCCESS_ASYNC, |
| 1577 |
AGI_RESULT_NOTFOUND, |
| 1578 |
AGI_RESULT_HANGUP, |
| 1579 |
}; |
| 1580 |
|
| 1581 |
static struct ast_manager_event_blob *agi_channel_to_ami(const char *type, struct stasis_message *message) |
| 1582 |
{ |
| 1583 |
struct ast_channel_blob *obj = stasis_message_data(message); |
| 1584 |
RAII_VAR(struct ast_str *, channel_string, NULL, ast_free); |
| 1585 |
RAII_VAR(struct ast_str *, event_string, NULL, ast_free); |
| 1586 |
|
| 1587 |
channel_string = ast_manager_build_channel_state_string(obj->snapshot); |
| 1588 |
event_string = ast_manager_str_from_json_object(obj->blob, NULL); |
| 1589 |
if (!channel_string || !event_string) { |
| 1590 |
return NULL; |
| 1591 |
} |
| 1592 |
|
| 1593 |
return ast_manager_event_blob_create(EVENT_FLAG_AGI, type, |
| 1594 |
"%s" |
| 1595 |
"%s", |
| 1596 |
ast_str_buffer(channel_string), |
| 1597 |
ast_str_buffer(event_string)); |
| 1598 |
} |
| 1599 |
|
| 1600 |
static struct ast_manager_event_blob *agi_exec_start_to_ami(struct stasis_message *message) |
| 1601 |
{ |
| 1602 |
return agi_channel_to_ami("AGIExecStart", message); |
| 1603 |
} |
| 1604 |
|
| 1605 |
static struct ast_manager_event_blob *agi_exec_end_to_ami(struct stasis_message *message) |
| 1606 |
{ |
| 1607 |
return agi_channel_to_ami("AGIExecEnd", message); |
| 1608 |
} |
| 1609 |
|
| 1610 |
static struct ast_manager_event_blob *agi_async_start_to_ami(struct stasis_message *message) |
| 1611 |
{ |
| 1612 |
return agi_channel_to_ami("AsyncAGIStart", message); |
| 1613 |
} |
| 1614 |
|
| 1615 |
static struct ast_manager_event_blob *agi_async_exec_to_ami(struct stasis_message *message) |
| 1616 |
{ |
| 1617 |
return agi_channel_to_ami("AsyncAGIExec", message); |
| 1618 |
} |
| 1619 |
|
| 1620 |
static struct ast_manager_event_blob *agi_async_end_to_ami(struct stasis_message *message) |
| 1621 |
{ |
| 1622 |
return agi_channel_to_ami("AsyncAGIEnd", message); |
| 1623 |
} |
| 1624 |
|
| 1625 |
STASIS_MESSAGE_TYPE_DEFN_LOCAL(agi_exec_start_type, |
| 1626 |
.to_ami = agi_exec_start_to_ami, |
| 1627 |
); |
| 1628 |
STASIS_MESSAGE_TYPE_DEFN_LOCAL(agi_exec_end_type, |
| 1629 |
.to_ami = agi_exec_end_to_ami, |
| 1630 |
); |
| 1631 |
STASIS_MESSAGE_TYPE_DEFN_LOCAL(agi_async_start_type, |
| 1632 |
.to_ami = agi_async_start_to_ami, |
| 1633 |
); |
| 1634 |
STASIS_MESSAGE_TYPE_DEFN_LOCAL(agi_async_exec_type, |
| 1635 |
.to_ami = agi_async_exec_to_ami, |
| 1636 |
); |
| 1637 |
STASIS_MESSAGE_TYPE_DEFN_LOCAL(agi_async_end_type, |
| 1638 |
.to_ami = agi_async_end_to_ami, |
| 1639 |
); |
| 1640 |
|
| 1641 |
static agi_command *find_command(const char * const cmds[], int exact); |
| 1642 |
|
| 1643 |
AST_THREADSTORAGE(agi_buf); |
| 1644 |
#define AGI_BUF_INITSIZE 256 |
| 1645 |
|
| 1646 |
int AST_OPTIONAL_API_NAME(ast_agi_send)(int fd, struct ast_channel *chan, char *fmt, ...) |
| 1647 |
{ |
| 1648 |
int res = 0; |
| 1649 |
va_list ap; |
| 1650 |
struct ast_str *buf; |
| 1651 |
|
| 1652 |
if (!(buf = ast_str_thread_get(&agi_buf, AGI_BUF_INITSIZE))) |
| 1653 |
return -1; |
| 1654 |
|
| 1655 |
va_start(ap, fmt); |
| 1656 |
res = ast_str_set_va(&buf, 0, fmt, ap); |
| 1657 |
va_end(ap); |
| 1658 |
|
| 1659 |
if (res == -1) { |
| 1660 |
ast_log(LOG_ERROR, "Out of memory\n"); |
| 1661 |
return -1; |
| 1662 |
} |
| 1663 |
|
| 1664 |
if (agidebug) { |
| 1665 |
if (chan) { |
| 1666 |
ast_verbose("<%s>AGI Tx >> %s", ast_channel_name(chan), ast_str_buffer(buf)); |
| 1667 |
} else { |
| 1668 |
ast_verbose("AGI Tx >> %s", ast_str_buffer(buf)); |
| 1669 |
} |
| 1670 |
} |
| 1671 |
|
| 1672 |
return ast_carefulwrite(fd, ast_str_buffer(buf), ast_str_strlen(buf), 100); |
| 1673 |
} |
| 1674 |
|
| 1675 |
|
| 1676 |
struct agi_cmd { |
| 1677 |
char *cmd_buffer; |
| 1678 |
char *cmd_id; |
| 1679 |
AST_LIST_ENTRY(agi_cmd) entry; |
| 1680 |
}; |
| 1681 |
|
| 1682 |
static void free_agi_cmd(struct agi_cmd *cmd) |
| 1683 |
{ |
| 1684 |
ast_free(cmd->cmd_buffer); |
| 1685 |
ast_free(cmd->cmd_id); |
| 1686 |
ast_free(cmd); |
| 1687 |
} |
| 1688 |
|
| 1689 |
|
| 1690 |
static void agi_destroy_commands_cb(void *data) |
| 1691 |
{ |
| 1692 |
struct agi_cmd *cmd; |
| 1693 |
AST_LIST_HEAD(, agi_cmd) *chan_cmds = data; |
| 1694 |
AST_LIST_LOCK(chan_cmds); |
| 1695 |
while ( (cmd = AST_LIST_REMOVE_HEAD(chan_cmds, entry)) ) { |
| 1696 |
free_agi_cmd(cmd); |
| 1697 |
} |
| 1698 |
AST_LIST_UNLOCK(chan_cmds); |
| 1699 |
AST_LIST_HEAD_DESTROY(chan_cmds); |
| 1700 |
ast_free(chan_cmds); |
| 1701 |
} |
| 1702 |
|
| 1703 |
|
| 1704 |
static const struct ast_datastore_info agi_commands_datastore_info = { |
| 1705 |
.type = "AsyncAGI", |
| 1706 |
.destroy = agi_destroy_commands_cb |
| 1707 |
}; |
| 1708 |
|
| 1709 |
|
| 1710 |
|
| 1711 |
|
| 1712 |
|
| 1713 |
|
| 1714 |
|
| 1715 |
|
| 1716 |
|
| 1717 |
|
| 1718 |
static int get_agi_cmd(struct ast_channel *chan, struct agi_cmd **cmd) |
| 1719 |
{ |
| 1720 |
struct ast_datastore *store; |
| 1721 |
AST_LIST_HEAD(, agi_cmd) *agi_commands; |
| 1722 |
|
| 1723 |
ast_channel_lock(chan); |
| 1724 |
store = ast_channel_datastore_find(chan, &agi_commands_datastore_info, NULL); |
| 1725 |
ast_channel_unlock(chan); |
| 1726 |
if (!store) { |
| 1727 |
ast_log(LOG_ERROR, "Huh? Async AGI datastore disappeared on Channel %s!\n", |
| 1728 |
ast_channel_name(chan)); |
| 1729 |
*cmd = NULL; |
| 1730 |
return -1; |
| 1731 |
} |
| 1732 |
agi_commands = store->data; |
| 1733 |
AST_LIST_LOCK(agi_commands); |
| 1734 |
*cmd = AST_LIST_REMOVE_HEAD(agi_commands, entry); |
| 1735 |
AST_LIST_UNLOCK(agi_commands); |
| 1736 |
return 0; |
| 1737 |
} |
| 1738 |
|
| 1739 |
|
| 1740 |
static int add_agi_cmd(struct ast_channel *chan, const char *cmd_buff, const char *cmd_id) |
| 1741 |
{ |
| 1742 |
struct ast_datastore *store; |
| 1743 |
struct agi_cmd *cmd; |
| 1744 |
AST_LIST_HEAD(, agi_cmd) *agi_commands; |
| 1745 |
|
| 1746 |
store = ast_channel_datastore_find(chan, &agi_commands_datastore_info, NULL); |
| 1747 |
if (!store) { |
| 1748 |
ast_log(LOG_WARNING, "Channel %s is not setup for Async AGI.\n", ast_channel_name(chan)); |
| 1749 |
return -1; |
| 1750 |
} |
| 1751 |
agi_commands = store->data; |
| 1752 |
cmd = ast_calloc(1, sizeof(*cmd)); |
| 1753 |
if (!cmd) { |
| 1754 |
return -1; |
| 1755 |
} |
| 1756 |
cmd->cmd_buffer = ast_strdup(cmd_buff); |
| 1757 |
if (!cmd->cmd_buffer) { |
| 1758 |
ast_free(cmd); |
| 1759 |
return -1; |
| 1760 |
} |
| 1761 |
cmd->cmd_id = ast_strdup(cmd_id); |
| 1762 |
if (!cmd->cmd_id) { |
| 1763 |
ast_free(cmd->cmd_buffer); |
| 1764 |
ast_free(cmd); |
| 1765 |
return -1; |
| 1766 |
} |
| 1767 |
AST_LIST_LOCK(agi_commands); |
| 1768 |
AST_LIST_INSERT_TAIL(agi_commands, cmd, entry); |
| 1769 |
AST_LIST_UNLOCK(agi_commands); |
| 1770 |
return 0; |
| 1771 |
} |
| 1772 |
|
| 1773 |
static int add_to_agi(struct ast_channel *chan) |
| 1774 |
{ |
| 1775 |
struct ast_datastore *datastore; |
| 1776 |
AST_LIST_HEAD(, agi_cmd) *agi_cmds_list; |
| 1777 |
|
| 1778 |
|
| 1779 |
ast_channel_lock(chan); |
| 1780 |
datastore = ast_channel_datastore_find(chan, &agi_commands_datastore_info, NULL); |
| 1781 |
ast_channel_unlock(chan); |
| 1782 |
if (datastore) { |
| 1783 |
|
| 1784 |
|
| 1785 |
return 0; |
| 1786 |
} |
| 1787 |
|
| 1788 |
|
| 1789 |
|
| 1790 |
datastore = ast_datastore_alloc(&agi_commands_datastore_info, "AGI"); |
| 1791 |
if (!datastore) { |
| 1792 |
return -1; |
| 1793 |
} |
| 1794 |
agi_cmds_list = ast_calloc(1, sizeof(*agi_cmds_list)); |
| 1795 |
if (!agi_cmds_list) { |
| 1796 |
ast_log(LOG_ERROR, "Unable to allocate Async AGI commands list.\n"); |
| 1797 |
ast_datastore_free(datastore); |
| 1798 |
return -1; |
| 1799 |
} |
| 1800 |
datastore->data = agi_cmds_list; |
| 1801 |
AST_LIST_HEAD_INIT(agi_cmds_list); |
| 1802 |
ast_channel_lock(chan); |
| 1803 |
ast_channel_datastore_add(chan, datastore); |
| 1804 |
ast_channel_unlock(chan); |
| 1805 |
return 0; |
| 1806 |
} |
| 1807 |
|
| 1808 |
|
| 1809 |
|
| 1810 |
|
| 1811 |
|
| 1812 |
|
| 1813 |
|
| 1814 |
|
| 1815 |
|
| 1816 |
|
| 1817 |
static char *handle_cli_agi_add_cmd(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 1818 |
{ |
| 1819 |
struct ast_channel *chan; |
| 1820 |
switch (cmd) { |
| 1821 |
case CLI_INIT: |
| 1822 |
e->command = "agi exec"; |
| 1823 |
e->usage = "Usage: agi exec <channel name> <app and arguments> [id]\n" |
| 1824 |
" Add AGI command to the execute queue of the specified channel in Async AGI\n"; |
| 1825 |
return NULL; |
| 1826 |
case CLI_GENERATE: |
| 1827 |
if (a->pos == 2) |
| 1828 |
return ast_complete_channels(a->line, a->word, a->pos, a->n, 2); |
| 1829 |
return NULL; |
| 1830 |
} |
| 1831 |
|
| 1832 |
if (a->argc < 4) { |
| 1833 |
return CLI_SHOWUSAGE; |
| 1834 |
} |
| 1835 |
|
| 1836 |
if (!(chan = ast_channel_get_by_name(a->argv[2]))) { |
| 1837 |
ast_cli(a->fd, "Channel %s does not exist.\n", a->argv[2]); |
| 1838 |
return CLI_FAILURE; |
| 1839 |
} |
| 1840 |
|
| 1841 |
ast_channel_lock(chan); |
| 1842 |
|
| 1843 |
if (add_agi_cmd(chan, a->argv[3], (a->argc > 4 ? a->argv[4] : ""))) { |
| 1844 |
ast_cli(a->fd, "Failed to add AGI command to queue of channel %s\n", ast_channel_name(chan)); |
| 1845 |
ast_channel_unlock(chan); |
| 1846 |
chan = ast_channel_unref(chan); |
| 1847 |
return CLI_FAILURE; |
| 1848 |
} |
| 1849 |
|
| 1850 |
ast_debug(1, "Added AGI command to channel %s queue\n", ast_channel_name(chan)); |
| 1851 |
|
| 1852 |
ast_channel_unlock(chan); |
| 1853 |
chan = ast_channel_unref(chan); |
| 1854 |
|
| 1855 |
return CLI_SUCCESS; |
| 1856 |
} |
| 1857 |
|
| 1858 |
|
| 1859 |
|
| 1860 |
|
| 1861 |
|
| 1862 |
|
| 1863 |
|
| 1864 |
|
| 1865 |
|
| 1866 |
|
| 1867 |
|
| 1868 |
|
| 1869 |
static int action_add_agi_cmd(struct mansession *s, const struct message *m) |
| 1870 |
{ |
| 1871 |
const char *channel = astman_get_header(m, "Channel"); |
| 1872 |
const char *cmdbuff = astman_get_header(m, "Command"); |
| 1873 |
const char *cmdid = astman_get_header(m, "CommandID"); |
| 1874 |
struct ast_channel *chan; |
| 1875 |
char buf[256]; |
| 1876 |
|
| 1877 |
if (ast_strlen_zero(channel) || ast_strlen_zero(cmdbuff)) { |
| 1878 |
astman_send_error(s, m, "Both, Channel and Command are *required*"); |
| 1879 |
return 0; |
| 1880 |
} |
| 1881 |
|
| 1882 |
if (!(chan = ast_channel_get_by_name(channel))) { |
| 1883 |
snprintf(buf, sizeof(buf), "Channel %s does not exist.", channel); |
| 1884 |
astman_send_error(s, m, buf); |
| 1885 |
return 0; |
| 1886 |
} |
| 1887 |
|
| 1888 |
ast_channel_lock(chan); |
| 1889 |
|
| 1890 |
if (add_agi_cmd(chan, cmdbuff, cmdid)) { |
| 1891 |
snprintf(buf, sizeof(buf), "Failed to add AGI command to channel %s queue", ast_channel_name(chan)); |
| 1892 |
astman_send_error(s, m, buf); |
| 1893 |
ast_channel_unlock(chan); |
| 1894 |
chan = ast_channel_unref(chan); |
| 1895 |
return 0; |
| 1896 |
} |
| 1897 |
|
| 1898 |
ast_channel_unlock(chan); |
| 1899 |
chan = ast_channel_unref(chan); |
| 1900 |
|
| 1901 |
astman_send_ack(s, m, "Added AGI command to queue"); |
| 1902 |
|
| 1903 |
return 0; |
| 1904 |
} |
| 1905 |
|
| 1906 |
static enum agi_result agi_handle_command(struct ast_channel *chan, AGI *agi, char *buf, int dead); |
| 1907 |
static void setup_env(struct ast_channel *chan, char *request, int fd, int enhanced, int argc, char *argv[]); |
| 1908 |
|
| 1909 |
|
| 1910 |
|
| 1911 |
|
| 1912 |
|
| 1913 |
|
| 1914 |
|
| 1915 |
|
| 1916 |
|
| 1917 |
|
| 1918 |
|
| 1919 |
static enum agi_result async_agi_read_frame(struct ast_channel *chan) |
| 1920 |
{ |
| 1921 |
struct ast_frame *f; |
| 1922 |
|
| 1923 |
f = ast_read(chan); |
| 1924 |
if (!f) { |
| 1925 |
ast_debug(3, "No frame read on channel %s, going out ...\n", ast_channel_name(chan)); |
| 1926 |
return AGI_RESULT_HANGUP; |
| 1927 |
} |
| 1928 |
if (f->frametype == AST_FRAME_CONTROL) { |
| 1929 |
|
| 1930 |
|
| 1931 |
|
| 1932 |
|
| 1933 |
switch (f->subclass.integer) { |
| 1934 |
case AST_CONTROL_HANGUP: |
| 1935 |
ast_debug(3, "Got HANGUP frame on channel %s, going out ...\n", ast_channel_name(chan)); |
| 1936 |
ast_frfree(f); |
| 1937 |
return AGI_RESULT_HANGUP; |
| 1938 |
default: |
| 1939 |
break; |
| 1940 |
} |
| 1941 |
} |
| 1942 |
ast_frfree(f); |
| 1943 |
|
| 1944 |
return AGI_RESULT_SUCCESS; |
| 1945 |
} |
| 1946 |
|
| 1947 |
static enum agi_result launch_asyncagi(struct ast_channel *chan, int argc, char *argv[], int *efd) |
| 1948 |
{ |
| 1949 |
|
| 1950 |
|
| 1951 |
|
| 1952 |
|
| 1953 |
|
| 1954 |
|
| 1955 |
|
| 1956 |
|
| 1957 |
|
| 1958 |
|
| 1959 |
|
| 1960 |
|
| 1961 |
|
| 1962 |
|
| 1963 |
|
| 1964 |
|
| 1965 |
|
| 1966 |
#define AGI_BUF_SIZE 1024 |
| 1967 |
#define AMI_BUF_SIZE 2048 |
| 1968 |
enum agi_result cmd_status; |
| 1969 |
struct agi_cmd *cmd; |
| 1970 |
int res; |
| 1971 |
int fds[2]; |
| 1972 |
int hungup; |
| 1973 |
int timeout = 100; |
| 1974 |
char agi_buffer[AGI_BUF_SIZE + 1]; |
| 1975 |
char ami_buffer[AMI_BUF_SIZE]; |
| 1976 |
enum agi_result returnstatus = AGI_RESULT_SUCCESS; |
| 1977 |
AGI async_agi; |
| 1978 |
RAII_VAR(struct ast_json *, startblob, NULL, ast_json_unref); |
| 1979 |
|
| 1980 |
if (efd) { |
| 1981 |
ast_log(LOG_WARNING, "Async AGI does not support Enhanced AGI yet\n"); |
| 1982 |
return AGI_RESULT_FAILURE; |
| 1983 |
} |
| 1984 |
|
| 1985 |
|
| 1986 |
if (add_to_agi(chan)) { |
| 1987 |
ast_log(LOG_ERROR, "Failed to start Async AGI on channel %s\n", ast_channel_name(chan)); |
| 1988 |
return AGI_RESULT_FAILURE; |
| 1989 |
} |
| 1990 |
|
| 1991 |
|
| 1992 |
|
| 1993 |
res = pipe(fds); |
| 1994 |
if (res) { |
| 1995 |
ast_log(LOG_ERROR, "Failed to create Async AGI pipe\n"); |
| 1996 |
|
| 1997 |
|
| 1998 |
|
| 1999 |
|
| 2000 |
|
| 2001 |
return AGI_RESULT_FAILURE; |
| 2002 |
} |
| 2003 |
|
| 2004 |
|
| 2005 |
|
| 2006 |
async_agi.fd = fds[1]; |
| 2007 |
async_agi.ctrl = fds[1]; |
| 2008 |
async_agi.audio = -1; |
| 2009 |
async_agi.fast = 0; |
| 2010 |
async_agi.speech = NULL; |
| 2011 |
|
| 2012 |
|
| 2013 |
|
| 2014 |
setup_env(chan, "async", fds[1], 0, argc, argv); |
| 2015 |
|
| 2016 |
res = read(fds[0], agi_buffer, AGI_BUF_SIZE); |
| 2017 |
if (res <= 0) { |
| 2018 |
ast_log(LOG_ERROR, "Failed to read from Async AGI pipe on channel %s: %s\n", |
| 2019 |
ast_channel_name(chan), res < 0 ? strerror(errno) : "EOF"); |
| 2020 |
returnstatus = AGI_RESULT_FAILURE; |
| 2021 |
goto async_agi_abort; |
| 2022 |
} |
| 2023 |
agi_buffer[res] = '\0'; |
| 2024 |
|
| 2025 |
|
| 2026 |
|
| 2027 |
ast_uri_encode(agi_buffer, ami_buffer, AMI_BUF_SIZE, ast_uri_http); |
| 2028 |
startblob = ast_json_pack("{s: s}", "Env", ami_buffer); |
| 2029 |
|
| 2030 |
ast_channel_publish_cached_blob(chan, agi_async_start_type(), startblob); |
| 2031 |
|
| 2032 |
hungup = ast_check_hangup_locked(chan); |
| 2033 |
|
| 2034 |
for (;;) { |
| 2035 |
|
| 2036 |
|
| 2037 |
|
| 2038 |
|
| 2039 |
while (!hungup) { |
| 2040 |
RAII_VAR(struct ast_json *, execblob, NULL, ast_json_unref); |
| 2041 |
res = get_agi_cmd(chan, &cmd); |
| 2042 |
|
| 2043 |
if (res) { |
| 2044 |
returnstatus = AGI_RESULT_FAILURE; |
| 2045 |
goto async_agi_done; |
| 2046 |
} else if (!cmd) { |
| 2047 |
break; |
| 2048 |
} |
| 2049 |
|
| 2050 |
|
| 2051 |
cmd_status = agi_handle_command(chan, &async_agi, cmd->cmd_buffer, 0); |
| 2052 |
|
| 2053 |
|
| 2054 |
|
| 2055 |
|
| 2056 |
|
| 2057 |
res = read(fds[0], agi_buffer, AGI_BUF_SIZE); |
| 2058 |
if (res <= 0) { |
| 2059 |
ast_log(LOG_ERROR, "Failed to read from Async AGI pipe on channel %s: %s\n", |
| 2060 |
ast_channel_name(chan), res < 0 ? strerror(errno) : "EOF"); |
| 2061 |
free_agi_cmd(cmd); |
| 2062 |
returnstatus = AGI_RESULT_FAILURE; |
| 2063 |
goto async_agi_done; |
| 2064 |
} |
| 2065 |
|
| 2066 |
|
| 2067 |
|
| 2068 |
|
| 2069 |
|
| 2070 |
agi_buffer[res] = '\0'; |
| 2071 |
ast_uri_encode(agi_buffer, ami_buffer, AMI_BUF_SIZE, ast_uri_http); |
| 2072 |
|
| 2073 |
execblob = ast_json_pack("{s: s}", "Result", ami_buffer); |
| 2074 |
if (execblob && !ast_strlen_zero(cmd->cmd_id)) { |
| 2075 |
ast_json_object_set(execblob, "CommandId", ast_json_string_create(cmd->cmd_id)); |
| 2076 |
} |
| 2077 |
ast_channel_publish_cached_blob(chan, agi_async_exec_type(), execblob); |
| 2078 |
|
| 2079 |
free_agi_cmd(cmd); |
| 2080 |
|
| 2081 |
|
| 2082 |
|
| 2083 |
|
| 2084 |
|
| 2085 |
hungup = ast_check_hangup(chan); |
| 2086 |
switch (cmd_status) { |
| 2087 |
case AGI_RESULT_FAILURE: |
| 2088 |
if (!hungup) { |
| 2089 |
|
| 2090 |
returnstatus = AGI_RESULT_FAILURE; |
| 2091 |
goto async_agi_done; |
| 2092 |
} |
| 2093 |
break; |
| 2094 |
case AGI_RESULT_SUCCESS_ASYNC: |
| 2095 |
|
| 2096 |
returnstatus = AGI_RESULT_SUCCESS_ASYNC; |
| 2097 |
goto async_agi_done; |
| 2098 |
default: |
| 2099 |
break; |
| 2100 |
} |
| 2101 |
} |
| 2102 |
|
| 2103 |
if (!hungup) { |
| 2104 |
|
| 2105 |
res = ast_waitfor(chan, timeout); |
| 2106 |
if (res < 0) { |
| 2107 |
ast_debug(1, "ast_waitfor returned <= 0 on chan %s\n", ast_channel_name(chan)); |
| 2108 |
returnstatus = AGI_RESULT_FAILURE; |
| 2109 |
break; |
| 2110 |
} |
| 2111 |
} else { |
| 2112 |
|
| 2113 |
|
| 2114 |
|
| 2115 |
|
| 2116 |
res = 1; |
| 2117 |
} |
| 2118 |
if (0 < res) { |
| 2119 |
do { |
| 2120 |
cmd_status = async_agi_read_frame(chan); |
| 2121 |
if (cmd_status != AGI_RESULT_SUCCESS) { |
| 2122 |
returnstatus = cmd_status; |
| 2123 |
goto async_agi_done; |
| 2124 |
} |
| 2125 |
hungup = ast_check_hangup(chan); |
| 2126 |
} while (hungup); |
| 2127 |
} else { |
| 2128 |
hungup = ast_check_hangup(chan); |
| 2129 |
} |
| 2130 |
} |
| 2131 |
async_agi_done: |
| 2132 |
|
| 2133 |
if (async_agi.speech) { |
| 2134 |
ast_speech_destroy(async_agi.speech); |
| 2135 |
} |
| 2136 |
|
| 2137 |
ast_channel_publish_cached_blob(chan, agi_async_end_type(), NULL); |
| 2138 |
|
| 2139 |
async_agi_abort: |
| 2140 |
|
| 2141 |
close(fds[0]); |
| 2142 |
close(fds[1]); |
| 2143 |
|
| 2144 |
|
| 2145 |
|
| 2146 |
|
| 2147 |
|
| 2148 |
|
| 2149 |
|
| 2150 |
|
| 2151 |
|
| 2152 |
if (returnstatus == AGI_RESULT_SUCCESS) { |
| 2153 |
returnstatus = AGI_RESULT_SUCCESS_ASYNC; |
| 2154 |
} |
| 2155 |
return returnstatus; |
| 2156 |
|
| 2157 |
#undef AGI_BUF_SIZE |
| 2158 |
#undef AMI_BUF_SIZE |
| 2159 |
} |
| 2160 |
|
| 2161 |
|
| 2162 |
|
| 2163 |
|
| 2164 |
|
| 2165 |
|
| 2166 |
|
| 2167 |
|
| 2168 |
|
| 2169 |
|
| 2170 |
|
| 2171 |
|
| 2172 |
static int handle_connection(const char *agiurl, const struct ast_sockaddr addr, const int netsockfd) |
| 2173 |
{ |
| 2174 |
struct pollfd pfds[1]; |
| 2175 |
int res, conresult; |
| 2176 |
socklen_t reslen; |
| 2177 |
|
| 2178 |
reslen = sizeof(conresult); |
| 2179 |
|
| 2180 |
pfds[0].fd = netsockfd; |
| 2181 |
pfds[0].events = POLLOUT; |
| 2182 |
|
| 2183 |
while ((res = ast_poll(pfds, 1, MAX_AGI_CONNECT)) != 1) { |
| 2184 |
if (errno != EINTR) { |
| 2185 |
if (!res) { |
| 2186 |
ast_log(LOG_WARNING, "FastAGI connection to '%s' timed out after MAX_AGI_CONNECT (%d) milliseconds.\n", |
| 2187 |
agiurl, MAX_AGI_CONNECT); |
| 2188 |
} else { |
| 2189 |
ast_log(LOG_WARNING, "Connect to '%s' failed: %s\n", agiurl, strerror(errno)); |
| 2190 |
} |
| 2191 |
|
| 2192 |
return 1; |
| 2193 |
} |
| 2194 |
} |
| 2195 |
|
| 2196 |
if (getsockopt(pfds[0].fd, SOL_SOCKET, SO_ERROR, &conresult, &reslen) < 0) { |
| 2197 |
ast_log(LOG_WARNING, "Connection to %s failed with error: %s\n", |
| 2198 |
ast_sockaddr_stringify(&addr), strerror(errno)); |
| 2199 |
return 1; |
| 2200 |
} |
| 2201 |
|
| 2202 |
if (conresult) { |
| 2203 |
ast_log(LOG_WARNING, "Connecting to '%s' failed for url '%s': %s\n", |
| 2204 |
ast_sockaddr_stringify(&addr), agiurl, strerror(conresult)); |
| 2205 |
return 1; |
| 2206 |
} |
| 2207 |
|
| 2208 |
return 0; |
| 2209 |
} |
| 2210 |
|
| 2211 |
|
| 2212 |
|
| 2213 |
static enum agi_result launch_netscript(char *agiurl, char *argv[], int *fds) |
| 2214 |
{ |
| 2215 |
int s = 0; |
| 2216 |
char *host, *script; |
| 2217 |
int num_addrs = 0, i = 0; |
| 2218 |
struct ast_sockaddr *addrs; |
| 2219 |
|
| 2220 |
|
| 2221 |
host = ast_strdupa(agiurl + 6); |
| 2222 |
|
| 2223 |
|
| 2224 |
if ((script = strchr(host, '/'))) { |
| 2225 |
*script++ = '\0'; |
| 2226 |
} else { |
| 2227 |
script = ""; |
| 2228 |
} |
| 2229 |
|
| 2230 |
if (!(num_addrs = ast_sockaddr_resolve(&addrs, host, 0, AST_AF_UNSPEC))) { |
| 2231 |
ast_log(LOG_WARNING, "Unable to locate host '%s'\n", host); |
| 2232 |
return AGI_RESULT_FAILURE; |
| 2233 |
} |
| 2234 |
|
| 2235 |
for (i = 0; i < num_addrs; i++) { |
| 2236 |
if (!ast_sockaddr_port(&addrs[i])) { |
| 2237 |
ast_sockaddr_set_port(&addrs[i], AGI_PORT); |
| 2238 |
} |
| 2239 |
|
| 2240 |
if ((s = ast_socket_nonblock(addrs[i].ss.ss_family, SOCK_STREAM, IPPROTO_TCP)) < 0) { |
| 2241 |
ast_log(LOG_WARNING, "Unable to create socket: %s\n", strerror(errno)); |
| 2242 |
continue; |
| 2243 |
} |
| 2244 |
|
| 2245 |
if (ast_connect(s, &addrs[i]) && errno == EINPROGRESS) { |
| 2246 |
|
| 2247 |
if (handle_connection(agiurl, addrs[i], s)) { |
| 2248 |
close(s); |
| 2249 |
continue; |
| 2250 |
} |
| 2251 |
|
| 2252 |
} else { |
| 2253 |
ast_log(LOG_WARNING, "Connection to %s failed with unexpected error: %s\n", |
| 2254 |
ast_sockaddr_stringify(&addrs[i]), strerror(errno)); |
| 2255 |
} |
| 2256 |
|
| 2257 |
break; |
| 2258 |
} |
| 2259 |
|
| 2260 |
ast_free(addrs); |
| 2261 |
|
| 2262 |
if (i == num_addrs) { |
| 2263 |
ast_log(LOG_WARNING, "Couldn't connect to any host. FastAGI failed.\n"); |
| 2264 |
return AGI_RESULT_FAILURE; |
| 2265 |
} |
| 2266 |
|
| 2267 |
if (ast_agi_send(s, NULL, "agi_network: yes\n") < 0) { |
| 2268 |
if (errno != EINTR) { |
| 2269 |
ast_log(LOG_WARNING, "Connect to '%s' failed: %s\n", agiurl, strerror(errno)); |
| 2270 |
close(s); |
| 2271 |
return AGI_RESULT_FAILURE; |
| 2272 |
} |
| 2273 |
} |
| 2274 |
|
| 2275 |
|
| 2276 |
|
| 2277 |
if (!ast_strlen_zero(script)) { |
| 2278 |
ast_agi_send(s, NULL, "agi_network_script: %s\n", script); |
| 2279 |
} |
| 2280 |
|
| 2281 |
ast_debug(4, "Wow, connected!\n"); |
| 2282 |
fds[0] = s; |
| 2283 |
fds[1] = s; |
| 2284 |
return AGI_RESULT_SUCCESS_FAST; |
| 2285 |
} |
| 2286 |
|
| 2287 |
|
| 2288 |
|
| 2289 |
|
| 2290 |
|
| 2291 |
|
| 2292 |
|
| 2293 |
|
| 2294 |
|
| 2295 |
|
| 2296 |
|
| 2297 |
|
| 2298 |
|
| 2299 |
|
| 2300 |
|
| 2301 |
|
| 2302 |
|
| 2303 |
|
| 2304 |
|
| 2305 |
|
| 2306 |
static enum agi_result launch_ha_netscript(char *agiurl, char *argv[], int *fds) |
| 2307 |
{ |
| 2308 |
char *host, *script; |
| 2309 |
enum agi_result result; |
| 2310 |
struct srv_context *context = NULL; |
| 2311 |
int srv_ret; |
| 2312 |
char service[256]; |
| 2313 |
char resolved_uri[1024]; |
| 2314 |
const char *srvhost; |
| 2315 |
unsigned short srvport; |
| 2316 |
|
| 2317 |
|
| 2318 |
if (strlen(agiurl) < 7) { |
| 2319 |
ast_log(LOG_WARNING, "An error occurred parsing the AGI URI: %s", agiurl); |
| 2320 |
return AGI_RESULT_FAILURE; |
| 2321 |
} |
| 2322 |
host = ast_strdupa(agiurl + 7); |
| 2323 |
|
| 2324 |
|
| 2325 |
if ((script = strchr(host, '/'))) { |
| 2326 |
*script++ = '\0'; |
| 2327 |
} else { |
| 2328 |
script = ""; |
| 2329 |
} |
| 2330 |
|
| 2331 |
if (strchr(host, ':')) { |
| 2332 |
ast_log(LOG_WARNING, "Specifying a port number disables SRV lookups: %s\n", agiurl); |
| 2333 |
return launch_netscript(agiurl + 1, argv, fds); |
| 2334 |
} |
| 2335 |
|
| 2336 |
snprintf(service, sizeof(service), "%s%s", SRV_PREFIX, host); |
| 2337 |
|
| 2338 |
while (!(srv_ret = ast_srv_lookup(&context, service, &srvhost, &srvport))) { |
| 2339 |
snprintf(resolved_uri, sizeof(resolved_uri), "agi://%s:%d/%s", srvhost, srvport, script); |
| 2340 |
result = launch_netscript(resolved_uri, argv, fds); |
| 2341 |
if (result == AGI_RESULT_FAILURE || result == AGI_RESULT_NOTFOUND) { |
| 2342 |
ast_log(LOG_WARNING, "AGI request failed for host '%s' (%s:%d)\n", host, srvhost, srvport); |
| 2343 |
} else { |
| 2344 |
|
| 2345 |
ast_srv_cleanup(&context); |
| 2346 |
return result; |
| 2347 |
} |
| 2348 |
} |
| 2349 |
|
| 2350 |
|
| 2351 |
|
| 2352 |
|
| 2353 |
if (srv_ret < 0) { |
| 2354 |
ast_log(LOG_WARNING, "SRV lookup failed for %s\n", agiurl); |
| 2355 |
} |
| 2356 |
|
| 2357 |
return AGI_RESULT_FAILURE; |
| 2358 |
} |
| 2359 |
|
| 2360 |
static enum agi_result launch_script(struct ast_channel *chan, char *script, int argc, char *argv[], int *fds, int *efd, int *opid, int *safe_fork_called) |
| 2361 |
{ |
| 2362 |
char tmp[256]; |
| 2363 |
int pid, toast[2], fromast[2], audio[2], res; |
| 2364 |
struct stat st; |
| 2365 |
|
| 2366 |
|
| 2367 |
*safe_fork_called = 0; |
| 2368 |
|
| 2369 |
if (!strncasecmp(script, "agi://", 6)) { |
| 2370 |
return (efd == NULL) ? launch_netscript(script, argv, fds) : AGI_RESULT_FAILURE; |
| 2371 |
} |
| 2372 |
if (!strncasecmp(script, "hagi://", 7)) { |
| 2373 |
return (efd == NULL) ? launch_ha_netscript(script, argv, fds) : AGI_RESULT_FAILURE; |
| 2374 |
} |
| 2375 |
if (!strncasecmp(script, "agi:async", sizeof("agi:async") - 1)) { |
| 2376 |
return launch_asyncagi(chan, argc, argv, efd); |
| 2377 |
} |
| 2378 |
|
| 2379 |
if (script[0] != '/') { |
| 2380 |
snprintf(tmp, sizeof(tmp), "%s/%s", ast_config_AST_AGI_DIR, script); |
| 2381 |
script = tmp; |
| 2382 |
} |
| 2383 |
|
| 2384 |
|
| 2385 |
if (stat(script, &st)) { |
| 2386 |
ast_log(LOG_WARNING, "Failed to execute '%s': File does not exist.\n", script); |
| 2387 |
return AGI_RESULT_NOTFOUND; |
| 2388 |
} |
| 2389 |
|
| 2390 |
if (pipe(toast)) { |
| 2391 |
ast_log(LOG_WARNING, "Unable to create toast pipe: %s\n",strerror(errno)); |
| 2392 |
return AGI_RESULT_FAILURE; |
| 2393 |
} |
| 2394 |
if (pipe(fromast)) { |
| 2395 |
ast_log(LOG_WARNING, "unable to create fromast pipe: %s\n", strerror(errno)); |
| 2396 |
close(toast[0]); |
| 2397 |
close(toast[1]); |
| 2398 |
return AGI_RESULT_FAILURE; |
| 2399 |
} |
| 2400 |
if (efd) { |
| 2401 |
if (pipe(audio)) { |
| 2402 |
ast_log(LOG_WARNING, "unable to create audio pipe: %s\n", strerror(errno)); |
| 2403 |
close(fromast[0]); |
| 2404 |
close(fromast[1]); |
| 2405 |
close(toast[0]); |
| 2406 |
close(toast[1]); |
| 2407 |
return AGI_RESULT_FAILURE; |
| 2408 |
} |
| 2409 |
|
| 2410 |
res = ast_fd_set_flags(audio[1], O_NONBLOCK); |
| 2411 |
if (res < 0) { |
| 2412 |
ast_log(LOG_WARNING, "unable to set audio pipe parameters: %s\n", strerror(errno)); |
| 2413 |
close(fromast[0]); |
| 2414 |
close(fromast[1]); |
| 2415 |
close(toast[0]); |
| 2416 |
close(toast[1]); |
| 2417 |
close(audio[0]); |
| 2418 |
close(audio[1]); |
| 2419 |
return AGI_RESULT_FAILURE; |
| 2420 |
} |
| 2421 |
} |
| 2422 |
|
| 2423 |
*safe_fork_called = 1; |
| 2424 |
|
| 2425 |
if ((pid = ast_safe_fork(1)) < 0) { |
| 2426 |
ast_log(LOG_WARNING, "Failed to fork(): %s\n", strerror(errno)); |
| 2427 |
return AGI_RESULT_FAILURE; |
| 2428 |
} |
| 2429 |
if (!pid) { |
| 2430 |
|
| 2431 |
setenv("AST_CONFIG_DIR", ast_config_AST_CONFIG_DIR, 1); |
| 2432 |
setenv("AST_CONFIG_FILE", ast_config_AST_CONFIG_FILE, 1); |
| 2433 |
setenv("AST_MODULE_DIR", ast_config_AST_MODULE_DIR, 1); |
| 2434 |
setenv("AST_SPOOL_DIR", ast_config_AST_SPOOL_DIR, 1); |
| 2435 |
setenv("AST_MONITOR_DIR", ast_config_AST_MONITOR_DIR, 1); |
| 2436 |
setenv("AST_VAR_DIR", ast_config_AST_VAR_DIR, 1); |
| 2437 |
setenv("AST_DATA_DIR", ast_config_AST_DATA_DIR, 1); |
| 2438 |
setenv("AST_LOG_DIR", ast_config_AST_LOG_DIR, 1); |
| 2439 |
setenv("AST_AGI_DIR", ast_config_AST_AGI_DIR, 1); |
| 2440 |
setenv("AST_KEY_DIR", ast_config_AST_KEY_DIR, 1); |
| 2441 |
setenv("AST_RUN_DIR", ast_config_AST_RUN_DIR, 1); |
| 2442 |
|
| 2443 |
|
| 2444 |
ast_set_priority(0); |
| 2445 |
|
| 2446 |
|
| 2447 |
dup2(fromast[0], STDIN_FILENO); |
| 2448 |
dup2(toast[1], STDOUT_FILENO); |
| 2449 |
if (efd) |
| 2450 |
dup2(audio[0], STDERR_FILENO + 1); |
| 2451 |
else |
| 2452 |
close(STDERR_FILENO + 1); |
| 2453 |
|
| 2454 |
|
| 2455 |
ast_close_fds_above_n(STDERR_FILENO + 1); |
| 2456 |
|
| 2457 |
|
| 2458 |
|
| 2459 |
execv(script, argv); |
| 2460 |
|
| 2461 |
ast_child_verbose(1, "Failed to execute '%s': %s", script, strerror(errno)); |
| 2462 |
|
| 2463 |
fprintf(stdout, "failure\n"); |
| 2464 |
fflush(stdout); |
| 2465 |
_exit(1); |
| 2466 |
} |
| 2467 |
ast_verb(3, "Launched AGI Script %s\n", script); |
| 2468 |
fds[0] = toast[0]; |
| 2469 |
fds[1] = fromast[1]; |
| 2470 |
if (efd) |
| 2471 |
*efd = audio[1]; |
| 2472 |
|
| 2473 |
close(toast[1]); |
| 2474 |
close(fromast[0]); |
| 2475 |
|
| 2476 |
if (efd) |
| 2477 |
close(audio[0]); |
| 2478 |
|
| 2479 |
*opid = pid; |
| 2480 |
return AGI_RESULT_SUCCESS; |
| 2481 |
} |
| 2482 |
|
| 2483 |
static void setup_env(struct ast_channel *chan, char *request, int fd, int enhanced, int argc, char *argv[]) |
| 2484 |
{ |
| 2485 |
int count; |
| 2486 |
|
| 2487 |
|
| 2488 |
|
| 2489 |
ast_agi_send(fd, chan, "agi_request: %s\n", request); |
| 2490 |
ast_agi_send(fd, chan, "agi_channel: %s\n", ast_channel_name(chan)); |
| 2491 |
ast_agi_send(fd, chan, "agi_language: %s\n", ast_channel_language(chan)); |
| 2492 |
ast_agi_send(fd, chan, "agi_type: %s\n", ast_channel_tech(chan)->type); |
| 2493 |
ast_agi_send(fd, chan, "agi_uniqueid: %s\n", ast_channel_uniqueid(chan)); |
| 2494 |
ast_agi_send(fd, chan, "agi_version: %s\n", ast_get_version()); |
| 2495 |
|
| 2496 |
|
| 2497 |
ast_agi_send(fd, chan, "agi_callerid: %s\n", |
| 2498 |
S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, "unknown")); |
| 2499 |
ast_agi_send(fd, chan, "agi_calleridname: %s\n", |
| 2500 |
S_COR(ast_channel_caller(chan)->id.name.valid, ast_channel_caller(chan)->id.name.str, "unknown")); |
| 2501 |
ast_agi_send(fd, chan, "agi_callingpres: %d\n", |
| 2502 |
ast_party_id_presentation(&ast_channel_caller(chan)->id)); |
| 2503 |
ast_agi_send(fd, chan, "agi_callingani2: %d\n", ast_channel_caller(chan)->ani2); |
| 2504 |
ast_agi_send(fd, chan, "agi_callington: %d\n", ast_channel_caller(chan)->id.number.plan); |
| 2505 |
ast_agi_send(fd, chan, "agi_callingtns: %d\n", ast_channel_dialed(chan)->transit_network_select); |
| 2506 |
ast_agi_send(fd, chan, "agi_dnid: %s\n", S_OR(ast_channel_dialed(chan)->number.str, "unknown")); |
| 2507 |
ast_agi_send(fd, chan, "agi_rdnis: %s\n", |
| 2508 |
S_COR(ast_channel_redirecting(chan)->from.number.valid, ast_channel_redirecting(chan)->from.number.str, "unknown")); |
| 2509 |
|
| 2510 |
|
| 2511 |
ast_agi_send(fd, chan, "agi_context: %s\n", ast_channel_context(chan)); |
| 2512 |
ast_agi_send(fd, chan, "agi_extension: %s\n", ast_channel_exten(chan)); |
| 2513 |
ast_agi_send(fd, chan, "agi_priority: %d\n", ast_channel_priority(chan)); |
| 2514 |
ast_agi_send(fd, chan, "agi_enhanced: %s\n", enhanced ? "1.0" : "0.0"); |
| 2515 |
|
| 2516 |
|
| 2517 |
ast_agi_send(fd, chan, "agi_accountcode: %s\n", ast_channel_accountcode(chan) ? ast_channel_accountcode(chan) : ""); |
| 2518 |
ast_agi_send(fd, chan, "agi_threadid: %ld\n", (long)pthread_self()); |
| 2519 |
|
| 2520 |
|
| 2521 |
|
| 2522 |
for(count = 1; count < argc; count++) |
| 2523 |
ast_agi_send(fd, chan, "agi_arg_%d: %s\n", count, argv[count]); |
| 2524 |
|
| 2525 |
|
| 2526 |
ast_agi_send(fd, chan, "\n"); |
| 2527 |
} |
| 2528 |
|
| 2529 |
static int handle_answer(struct ast_channel *chan, AGI *agi, int argc, const char * const argv[]) |
| 2530 |
{ |
| 2531 |
int res = 0; |
| 2532 |
|
| 2533 |
|
| 2534 |
if (ast_channel_state(chan) != AST_STATE_UP) |
| 2535 |
res = ast_answer(chan); |
| 2536 |
|
| 2537 |
ast_agi_send(agi->fd, chan, "200 result=%d\n", res); |
| 2538 |
return (res >= 0) ? RESULT_SUCCESS : RESULT_FAILURE; |
| 2539 |
} |
| 2540 |
|
| 2541 |
static int handle_asyncagi_break(struct ast_channel *chan, AGI *agi, int argc, const char * const argv[]) |
| 2542 |
{ |
| 2543 |
ast_agi_send(agi->fd, chan, "200 result=0\n"); |
| 2544 |
return ASYNC_AGI_BREAK; |
| 2545 |
} |
| 2546 |
|
| 2547 |
static int handle_waitfordigit(struct ast_channel *chan, AGI *agi, int argc, const char * const argv[]) |
| 2548 |
{ |
| 2549 |
int res, to; |
| 2550 |
|
| 2551 |
if (argc != 4) |
| 2552 |
return RESULT_SHOWUSAGE; |
| 2553 |
if (sscanf(argv[3], "%30d", &to) != 1) |
| 2554 |
return RESULT_SHOWUSAGE; |
| 2555 |
res = ast_waitfordigit_full(chan, to, NULL, agi->audio, agi->ctrl); |
| 2556 |
ast_agi_send(agi->fd, chan, "200 result=%d\n", res); |
| 2557 |
return (res >= 0) ? RESULT_SUCCESS : RESULT_FAILURE; |
| 2558 |
} |
| 2559 |
|
| 2560 |
static int handle_sendtext(struct ast_channel *chan, AGI *agi, int argc, const char * const argv[]) |
| 2561 |
{ |
| 2562 |
int res; |
| 2563 |
|
| 2564 |
if (argc != 3) |
| 2565 |
return RESULT_SHOWUSAGE; |
| 2566 |
|
| 2567 |
|
| 2568 |
|
| 2569 |
|
| 2570 |
|
| 2571 |
|
| 2572 |
|
| 2573 |
|
| 2574 |
res = ast_sendtext(chan, argv[2]); |
| 2575 |
ast_agi_send(agi->fd, chan, "200 result=%d\n", res); |
| 2576 |
return (res >= 0) ? RESULT_SUCCESS : RESULT_FAILURE; |
| 2577 |
} |
| 2578 |
|
| 2579 |
static int handle_recvchar(struct ast_channel *chan, AGI *agi, int argc, const char * const argv[]) |
| 2580 |
{ |
| 2581 |
int res; |
| 2582 |
|
| 2583 |
if (argc != 3) |
| 2584 |
return RESULT_SHOWUSAGE; |
| 2585 |
|
| 2586 |
res = ast_recvchar(chan,atoi(argv[2])); |
| 2587 |
if (res == 0) { |
| 2588 |
ast_agi_send(agi->fd, chan, "200 result=%d (timeout)\n", res); |
| 2589 |
return RESULT_SUCCESS; |
| 2590 |
} |
| 2591 |
if (res > 0) { |
| 2592 |
ast_agi_send(agi->fd, chan, "200 result=%d\n", res); |
| 2593 |
return RESULT_SUCCESS; |
| 2594 |
} |
| 2595 |
ast_agi_send(agi->fd, chan, "200 result=%d (hangup)\n", res); |
| 2596 |
return RESULT_FAILURE; |
| 2597 |
} |
| 2598 |
|
| 2599 |
static int handle_recvtext(struct ast_channel *chan, AGI *agi, int argc, const char * const argv[]) |
| 2600 |
{ |
| 2601 |
char *buf; |
| 2602 |
|
| 2603 |
if (argc != 3) |
| 2604 |
return RESULT_SHOWUSAGE; |
| 2605 |
|
| 2606 |
buf = ast_recvtext(chan, atoi(argv[2])); |
| 2607 |
if (buf) { |
| 2608 |
ast_agi_send(agi->fd, chan, "200 result=1 (%s)\n", buf); |
| 2609 |
ast_free(buf); |
| 2610 |
} else { |
| 2611 |
ast_agi_send(agi->fd, chan, "200 result=-1\n"); |
| 2612 |
} |
| 2613 |
return RESULT_SUCCESS; |
| 2614 |
} |
| 2615 |
|
| 2616 |
static int handle_tddmode(struct ast_channel *chan, AGI *agi, int argc, const char * const argv[]) |
| 2617 |
{ |
| 2618 |
int res, x; |
| 2619 |
|
| 2620 |
if (argc != 3) |
| 2621 |
return RESULT_SHOWUSAGE; |
| 2622 |
|
| 2623 |
if (!strncasecmp(argv[2],"on",2)) { |
| 2624 |
x = 1; |
| 2625 |
} else { |
| 2626 |
x = 0; |
| 2627 |
} |
| 2628 |
if (!strncasecmp(argv[2],"mate",4)) { |
| 2629 |
x = 2; |
| 2630 |
} |
| 2631 |
if (!strncasecmp(argv[2],"tdd",3)) { |
| 2632 |
x = 1; |
| 2633 |
} |
| 2634 |
res = ast_channel_setoption(chan, AST_OPTION_TDD, &x, sizeof(char), 0); |
| 2635 |
if (res) { |
| 2636 |
|
| 2637 |
ast_agi_send(agi->fd, chan, "200 result=0\n"); |
| 2638 |
} else { |
| 2639 |
ast_agi_send(agi->fd, chan, "200 result=1\n"); |
| 2640 |
} |
| 2641 |
return RESULT_SUCCESS; |
| 2642 |
} |
| 2643 |
|
| 2644 |
static int handle_sendimage(struct ast_channel *chan, AGI *agi, int argc, const char * const argv[]) |
| 2645 |
{ |
| 2646 |
int res; |
| 2647 |
|
| 2648 |
if (argc != 3) { |
| 2649 |
return RESULT_SHOWUSAGE; |
| 2650 |
} |
| 2651 |
|
| 2652 |
res = ast_send_image(chan, argv[2]); |
| 2653 |
if (!ast_check_hangup(chan)) { |
| 2654 |
res = 0; |
| 2655 |
} |
| 2656 |
ast_agi_send(agi->fd, chan, "200 result=%d\n", res); |
| 2657 |
return (res >= 0) ? RESULT_SUCCESS : RESULT_FAILURE; |
| 2658 |
} |
| 2659 |
|
| 2660 |
static int handle_controlstreamfile(struct ast_channel *chan, AGI *agi, int argc, const char * const argv[]) |
| 2661 |
{ |
| 2662 |
int res = 0, skipms = 3000; |
| 2663 |
const char *fwd = "#", *rev = "*", *suspend = NULL, *stop = NULL; |
| 2664 |
char stopkeybuf[2]; |
| 2665 |
long offsetms = 0; |
| 2666 |
char offsetbuf[20]; |
| 2667 |
|
| 2668 |
if (argc < 5 || argc > 10) { |
| 2669 |
return RESULT_SHOWUSAGE; |
| 2670 |
} |
| 2671 |
|
| 2672 |
if (!ast_strlen_zero(argv[4])) { |
| 2673 |
stop = argv[4]; |
| 2674 |
} |
| 2675 |
|
| 2676 |
if ((argc > 5) && (sscanf(argv[5], "%30d", &skipms) != 1)) { |
| 2677 |
return RESULT_SHOWUSAGE; |
| 2678 |
} |
| 2679 |
|
| 2680 |
if (argc > 6 && !ast_strlen_zero(argv[6])) { |
| 2681 |
fwd = argv[6]; |
| 2682 |
} |
| 2683 |
|
| 2684 |
if (argc > 7 && !ast_strlen_zero(argv[7])) { |
| 2685 |
rev = argv[7]; |
| 2686 |
} |
| 2687 |
|
| 2688 |
if (argc > 8 && !ast_strlen_zero(argv[8])) { |
| 2689 |
suspend = argv[8]; |
| 2690 |
} |
| 2691 |
|
| 2692 |
if (argc > 9 && (sscanf(argv[9], "%30ld", &offsetms) != 1)) { |
| 2693 |
return RESULT_SHOWUSAGE; |
| 2694 |
} |
| 2695 |
|
| 2696 |
res = ast_control_streamfile(chan, argv[3], fwd, rev, stop, suspend, NULL, skipms, &offsetms); |
| 2697 |
|
| 2698 |
|
| 2699 |
if (res > 0 && stop && strchr(stop, res)) { |
| 2700 |
pbx_builtin_setvar_helper(chan, "CPLAYBACKSTATUS", "USERSTOPPED"); |
| 2701 |
snprintf(stopkeybuf, sizeof(stopkeybuf), "%c", res); |
| 2702 |
pbx_builtin_setvar_helper(chan, "CPLAYBACKSTOPKEY", stopkeybuf); |
| 2703 |
} else if (res > 0 && res == AST_CONTROL_STREAM_STOP) { |
| 2704 |
pbx_builtin_setvar_helper(chan, "CPLAYBACKSTATUS", "REMOTESTOPPED"); |
| 2705 |
res = 0; |
| 2706 |
} else { |
| 2707 |
if (res < 0) { |
| 2708 |
pbx_builtin_setvar_helper(chan, "CPLAYBACKSTATUS", "ERROR"); |
| 2709 |
} else { |
| 2710 |
pbx_builtin_setvar_helper(chan, "CPLAYBACKSTATUS", "SUCCESS"); |
| 2711 |
} |
| 2712 |
} |
| 2713 |
|
| 2714 |
snprintf(offsetbuf, sizeof(offsetbuf), "%ld", offsetms); |
| 2715 |
pbx_builtin_setvar_helper(chan, "CPLAYBACKOFFSET", offsetbuf); |
| 2716 |
|
| 2717 |
ast_agi_send(agi->fd, chan, "200 result=%d endpos=%ld\n", res, offsetms); |
| 2718 |
|
| 2719 |
return (res >= 0) ? RESULT_SUCCESS : RESULT_FAILURE; |
| 2720 |
} |
| 2721 |
|
| 2722 |
static int handle_streamfile(struct ast_channel *chan, AGI *agi, int argc, const char * const argv[]) |
| 2723 |
{ |
| 2724 |
int res; |
| 2725 |
struct ast_filestream *fs, *vfs; |
| 2726 |
long sample_offset = 0, max_length; |
| 2727 |
const char *edigits = ""; |
| 2728 |
|
| 2729 |
if (argc < 4 || argc > 5) { |
| 2730 |
return RESULT_SHOWUSAGE; |
| 2731 |
} |
| 2732 |
|
| 2733 |
if (argv[3]) { |
| 2734 |
edigits = argv[3]; |
| 2735 |
} |
| 2736 |
|
| 2737 |
if ((argc > 4) && (sscanf(argv[4], "%30ld", &sample_offset) != 1)) { |
| 2738 |
return RESULT_SHOWUSAGE; |
| 2739 |
} |
| 2740 |
|
| 2741 |
if (!(fs = ast_openstream(chan, argv[2], ast_channel_language(chan)))) { |
| 2742 |
ast_agi_send(agi->fd, chan, "200 result=-1 endpos=%ld\n", sample_offset); |
| 2743 |
return RESULT_FAILURE; |
| 2744 |
} |
| 2745 |
|
| 2746 |
if ((vfs = ast_openvstream(chan, argv[2], ast_channel_language(chan)))) { |
| 2747 |
ast_debug(1, "Ooh, found a video stream, too\n"); |
| 2748 |
} |
| 2749 |
ast_verb(3, "<%s> Playing '%s.%s' (escape_digits=%s) (sample_offset %ld) (language '%s')\n", |
| 2750 |
ast_channel_name(chan), argv[2], ast_format_get_name(ast_channel_writeformat(chan)), |
| 2751 |
edigits, sample_offset, S_OR(ast_channel_language(chan), "default")); |
| 2752 |
|
| 2753 |
ast_seekstream(fs, 0, SEEK_END); |
| 2754 |
max_length = ast_tellstream(fs); |
| 2755 |
ast_seekstream(fs, sample_offset, SEEK_SET); |
| 2756 |
res = ast_applystream(chan, fs); |
| 2757 |
if (vfs) { |
| 2758 |
ast_applystream(chan, vfs); |
| 2759 |
} |
| 2760 |
ast_playstream(fs); |
| 2761 |
if (vfs) { |
| 2762 |
ast_playstream(vfs); |
| 2763 |
} |
| 2764 |
|
| 2765 |
res = ast_waitstream_full(chan, argv[3], agi->audio, agi->ctrl); |
| 2766 |
|
| 2767 |
|
| 2768 |
sample_offset = (ast_channel_stream(chan)) ? ast_tellstream(fs) : max_length; |
| 2769 |
ast_stopstream(chan); |
| 2770 |
if (res == 1) { |
| 2771 |
|
| 2772 |
return RESULT_SUCCESS; |
| 2773 |
} |
| 2774 |
ast_agi_send(agi->fd, chan, "200 result=%d endpos=%ld\n", res, sample_offset); |
| 2775 |
pbx_builtin_setvar_helper(chan, "PLAYBACKSTATUS", res ? "FAILED" : "SUCCESS"); |
| 2776 |
|
| 2777 |
return (res >= 0) ? RESULT_SUCCESS : RESULT_FAILURE; |
| 2778 |
} |
| 2779 |
|
| 2780 |
|
| 2781 |
static int handle_getoption(struct ast_channel *chan, AGI *agi, int argc, const char * const argv[]) |
| 2782 |
{ |
| 2783 |
int res; |
| 2784 |
struct ast_filestream *fs, *vfs; |
| 2785 |
long sample_offset = 0, max_length; |
| 2786 |
int timeout = 0; |
| 2787 |
const char *edigits = ""; |
| 2788 |
|
| 2789 |
if ( argc < 4 || argc > 5 ) |
| 2790 |
return RESULT_SHOWUSAGE; |
| 2791 |
|
| 2792 |
if ( argv[3] ) |
| 2793 |
edigits = argv[3]; |
| 2794 |
|
| 2795 |
if ( argc == 5 ) |
| 2796 |
timeout = atoi(argv[4]); |
| 2797 |
else if (ast_channel_pbx(chan)->dtimeoutms) { |
| 2798 |
|
| 2799 |
timeout = ast_channel_pbx(chan)->dtimeoutms; |
| 2800 |
} |
| 2801 |
|
| 2802 |
if (!(fs = ast_openstream(chan, argv[2], ast_channel_language(chan)))) { |
| 2803 |
ast_agi_send(agi->fd, chan, "200 result=-1 endpos=%ld\n", sample_offset); |
| 2804 |
ast_log(LOG_WARNING, "Unable to open %s\n", argv[2]); |
| 2805 |
return RESULT_FAILURE; |
| 2806 |
} |
| 2807 |
|
| 2808 |
if ((vfs = ast_openvstream(chan, argv[2], ast_channel_language(chan)))) |
| 2809 |
ast_debug(1, "Ooh, found a video stream, too\n"); |
| 2810 |
|
| 2811 |
ast_verb(3, "Playing '%s' (escape_digits=%s) (timeout %d)\n", argv[2], edigits, timeout); |
| 2812 |
|
| 2813 |
ast_seekstream(fs, 0, SEEK_END); |
| 2814 |
max_length = ast_tellstream(fs); |
| 2815 |
ast_seekstream(fs, sample_offset, SEEK_SET); |
| 2816 |
res = ast_applystream(chan, fs); |
| 2817 |
if (vfs) |
| 2818 |
ast_applystream(chan, vfs); |
| 2819 |
ast_playstream(fs); |
| 2820 |
if (vfs) |
| 2821 |
ast_playstream(vfs); |
| 2822 |
|
| 2823 |
res = ast_waitstream_full(chan, argv[3], agi->audio, agi->ctrl); |
| 2824 |
|
| 2825 |
|
| 2826 |
sample_offset = (ast_channel_stream(chan))?ast_tellstream(fs):max_length; |
| 2827 |
ast_stopstream(chan); |
| 2828 |
if (res == 1) { |
| 2829 |
|
| 2830 |
return RESULT_SUCCESS; |
| 2831 |
} |
| 2832 |
|
| 2833 |
|
| 2834 |
if (res == 0 ) { |
| 2835 |
res = ast_waitfordigit_full(chan, timeout, NULL, agi->audio, agi->ctrl); |
| 2836 |
|
| 2837 |
if ( !strchr(edigits,res) ) |
| 2838 |
res=0; |
| 2839 |
} |
| 2840 |
|
| 2841 |
ast_agi_send(agi->fd, chan, "200 result=%d endpos=%ld\n", res, sample_offset); |
| 2842 |
return (res >= 0) ? RESULT_SUCCESS : RESULT_FAILURE; |
| 2843 |
} |
| 2844 |
|
| 2845 |
|
| 2846 |
|
| 2847 |
|
| 2848 |
|
| 2849 |
|
| 2850 |
static int handle_saynumber(struct ast_channel *chan, AGI *agi, int argc, const char * const argv[]) |
| 2851 |
{ |
| 2852 |
int res, num; |
| 2853 |
|
| 2854 |
if (argc < 4 || argc > 5) |
| 2855 |
return RESULT_SHOWUSAGE; |
| 2856 |
if (sscanf(argv[2], "%30d", &num) != 1) |
| 2857 |
return RESULT_SHOWUSAGE; |
| 2858 |
res = ast_say_number_full(chan, num, argv[3], ast_channel_language(chan), argc > 4 ? argv[4] : NULL, agi->audio, agi->ctrl); |
| 2859 |
if (res == 1) |
| 2860 |
return RESULT_SUCCESS; |
| 2861 |
ast_agi_send(agi->fd, chan, "200 result=%d\n", res); |
| 2862 |
return (res >= 0) ? RESULT_SUCCESS : RESULT_FAILURE; |
| 2863 |
} |
| 2864 |
|
| 2865 |
static int handle_saydigits(struct ast_channel *chan, AGI *agi, int argc, const char * const argv[]) |
| 2866 |
{ |
| 2867 |
int res, num; |
| 2868 |
|
| 2869 |
if (argc != 4) |
| 2870 |
return RESULT_SHOWUSAGE; |
| 2871 |
if (sscanf(argv[2], "%30d", &num) != 1) |
| 2872 |
return RESULT_SHOWUSAGE; |
| 2873 |
|
| 2874 |
res = ast_say_digit_str_full(chan, argv[2], argv[3], ast_channel_language(chan), agi->audio, agi->ctrl); |
| 2875 |
if (res == 1) |
| 2876 |
return RESULT_SUCCESS; |
| 2877 |
ast_agi_send(agi->fd, chan, "200 result=%d\n", res); |
| 2878 |
return (res >= 0) ? RESULT_SUCCESS : RESULT_FAILURE; |
| 2879 |
} |
| 2880 |
|
| 2881 |
static int handle_sayalpha(struct ast_channel *chan, AGI *agi, int argc, const char * const argv[]) |
| 2882 |
{ |
| 2883 |
int res; |
| 2884 |
int sensitivity = AST_SAY_CASE_NONE; |
| 2885 |
|
| 2886 |
if (argc < 4 || argc > 5) { |
| 2887 |
return RESULT_SHOWUSAGE; |
| 2888 |
} |
| 2889 |
|
| 2890 |
if (argc > 4) { |
| 2891 |
switch (argv[4][0]) { |
| 2892 |
case 'a': |
| 2893 |
case 'A': |
| 2894 |
sensitivity = AST_SAY_CASE_ALL; |
| 2895 |
break; |
| 2896 |
case 'l': |
| 2897 |
case 'L': |
| 2898 |
sensitivity = AST_SAY_CASE_LOWER; |
| 2899 |
break; |
| 2900 |
case 'n': |
| 2901 |
case 'N': |
| 2902 |
sensitivity = AST_SAY_CASE_NONE; |
| 2903 |
break; |
| 2904 |
case 'u': |
| 2905 |
case 'U': |
| 2906 |
sensitivity = AST_SAY_CASE_UPPER; |
| 2907 |
break; |
| 2908 |
case '\0': |
| 2909 |
break; |
| 2910 |
default: |
| 2911 |
return RESULT_SHOWUSAGE; |
| 2912 |
} |
| 2913 |
} |
| 2914 |
res = ast_say_character_str_full(chan, argv[2], argv[3], ast_channel_language(chan), sensitivity, agi->audio, agi->ctrl); |
| 2915 |
if (res == 1) |
| 2916 |
return RESULT_SUCCESS; |
| 2917 |
ast_agi_send(agi->fd, chan, "200 result=%d\n", res); |
| 2918 |
return (res >= 0) ? RESULT_SUCCESS : RESULT_FAILURE; |
| 2919 |
} |
| 2920 |
|
| 2921 |
static int handle_saydate(struct ast_channel *chan, AGI *agi, int argc, const char * const argv[]) |
| 2922 |
{ |
| 2923 |
int res, num; |
| 2924 |
|
| 2925 |
if (argc != 4) |
| 2926 |
return RESULT_SHOWUSAGE; |
| 2927 |
if (sscanf(argv[2], "%30d", &num) != 1) |
| 2928 |
return RESULT_SHOWUSAGE; |
| 2929 |
res = ast_say_date(chan, num, argv[3], ast_channel_language(chan)); |
| 2930 |
if (res == 1) |
| 2931 |
return RESULT_SUCCESS; |
| 2932 |
ast_agi_send(agi->fd, chan, "200 result=%d\n", res); |
| 2933 |
return (res >= 0) ? RESULT_SUCCESS : RESULT_FAILURE; |
| 2934 |
} |
| 2935 |
|
| 2936 |
static int handle_saytime(struct ast_channel *chan, AGI *agi, int argc, const char * const argv[]) |
| 2937 |
{ |
| 2938 |
int res, num; |
| 2939 |
|
| 2940 |
if (argc != 4) |
| 2941 |
return RESULT_SHOWUSAGE; |
| 2942 |
if (sscanf(argv[2], "%30d", &num) != 1) |
| 2943 |
return RESULT_SHOWUSAGE; |
| 2944 |
res = ast_say_time(chan, num, argv[3], ast_channel_language(chan)); |
| 2945 |
if (res == 1) |
| 2946 |
return RESULT_SUCCESS; |
| 2947 |
ast_agi_send(agi->fd, chan, "200 result=%d\n", res); |
| 2948 |
return (res >= 0) ? RESULT_SUCCESS : RESULT_FAILURE; |
| 2949 |
} |
| 2950 |
|
| 2951 |
static int handle_saydatetime(struct ast_channel *chan, AGI *agi, int argc, const char * const argv[]) |
| 2952 |
{ |
| 2953 |
int res = 0; |
| 2954 |
time_t unixtime; |
| 2955 |
const char *format, *zone = NULL; |
| 2956 |
|
| 2957 |
if (argc < 4) |
| 2958 |
return RESULT_SHOWUSAGE; |
| 2959 |
|
| 2960 |
if (argc > 4) { |
| 2961 |
format = argv[4]; |
| 2962 |
} else { |
| 2963 |
|
| 2964 |
if (!strcasecmp(ast_channel_language(chan), "de")) { |
| 2965 |
format = "A dBY HMS"; |
| 2966 |
} else { |
| 2967 |
format = "ABdY 'digits/at' IMp"; |
| 2968 |
} |
| 2969 |
} |
| 2970 |
|
| 2971 |
if (argc > 5 && !ast_strlen_zero(argv[5])) |
| 2972 |
zone = argv[5]; |
| 2973 |
|
| 2974 |
if (ast_get_time_t(argv[2], &unixtime, 0, NULL)) |
| 2975 |
return RESULT_SHOWUSAGE; |
| 2976 |
|
| 2977 |
res = ast_say_date_with_format(chan, unixtime, argv[3], ast_channel_language(chan), format, zone); |
| 2978 |
if (res == 1) |
| 2979 |
return RESULT_SUCCESS; |
| 2980 |
|
| 2981 |
ast_agi_send(agi->fd, chan, "200 result=%d\n", res); |
| 2982 |
return (res >= 0) ? RESULT_SUCCESS : RESULT_FAILURE; |
| 2983 |
} |
| 2984 |
|
| 2985 |
static int handle_sayphonetic(struct ast_channel *chan, AGI *agi, int argc, const char * const argv[]) |
| 2986 |
{ |
| 2987 |
int res; |
| 2988 |
|
| 2989 |
if (argc != 4) |
| 2990 |
return RESULT_SHOWUSAGE; |
| 2991 |
|
| 2992 |
res = ast_say_phonetic_str_full(chan, argv[2], argv[3], ast_channel_language(chan), agi->audio, agi->ctrl); |
| 2993 |
if (res == 1) |
| 2994 |
return RESULT_SUCCESS; |
| 2995 |
ast_agi_send(agi->fd, chan, "200 result=%d\n", res); |
| 2996 |
return (res >= 0) ? RESULT_SUCCESS : RESULT_FAILURE; |
| 2997 |
} |
| 2998 |
|
| 2999 |
static int handle_getdata(struct ast_channel *chan, AGI *agi, int argc, const char * const argv[]) |
| 3000 |
{ |
| 3001 |
int res, max, timeout; |
| 3002 |
char data[1024]; |
| 3003 |
|
| 3004 |
if (argc < 3) |
| 3005 |
return RESULT_SHOWUSAGE; |
| 3006 |
if (argc >= 4) |
| 3007 |
timeout = atoi(argv[3]); |
| 3008 |
else |
| 3009 |
timeout = 0; |
| 3010 |
if (argc >= 5) |
| 3011 |
max = atoi(argv[4]); |
| 3012 |
else |
| 3013 |
max = 1024; |
| 3014 |
res = ast_app_getdata_full(chan, argv[2], data, max, timeout, agi->audio, agi->ctrl); |
| 3015 |
if (res == 2) |
| 3016 |
return RESULT_SUCCESS; |
| 3017 |
else if (res == 1) |
| 3018 |
ast_agi_send(agi->fd, chan, "200 result=%s (timeout)\n", data); |
| 3019 |
else if (res < 0 ) |
| 3020 |
ast_agi_send(agi->fd, chan, "200 result=-1\n"); |
| 3021 |
else |
| 3022 |
ast_agi_send(agi->fd, chan, "200 result=%s\n", data); |
| 3023 |
return RESULT_SUCCESS; |
| 3024 |
} |
| 3025 |
|
| 3026 |
static int handle_setcontext(struct ast_channel *chan, AGI *agi, int argc, const char * const argv[]) |
| 3027 |
{ |
| 3028 |
|
| 3029 |
if (argc != 3) |
| 3030 |
return RESULT_SHOWUSAGE; |
| 3031 |
ast_channel_context_set(chan, argv[2]); |
| 3032 |
ast_agi_send(agi->fd, chan, "200 result=0\n"); |
| 3033 |
return RESULT_SUCCESS; |
| 3034 |
} |
| 3035 |
|
| 3036 |
static int handle_setextension(struct ast_channel *chan, AGI *agi, int argc, const char * const argv[]) |
| 3037 |
{ |
| 3038 |
if (argc != 3) |
| 3039 |
return RESULT_SHOWUSAGE; |
| 3040 |
ast_channel_exten_set(chan, argv[2]); |
| 3041 |
ast_agi_send(agi->fd, chan, "200 result=0\n"); |
| 3042 |
return RESULT_SUCCESS; |
| 3043 |
} |
| 3044 |
|
| 3045 |
static int handle_setpriority(struct ast_channel *chan, AGI *agi, int argc, const char * const argv[]) |
| 3046 |
{ |
| 3047 |
int pri; |
| 3048 |
|
| 3049 |
if (argc != 3) |
| 3050 |
return RESULT_SHOWUSAGE; |
| 3051 |
|
| 3052 |
if (sscanf(argv[2], "%30d", &pri) != 1) { |
| 3053 |
pri = ast_findlabel_extension(chan, ast_channel_context(chan), ast_channel_exten(chan), argv[2], |
| 3054 |
S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL)); |
| 3055 |
if (pri < 1) |
| 3056 |
return RESULT_SHOWUSAGE; |
| 3057 |
} |
| 3058 |
|
| 3059 |
ast_explicit_goto(chan, NULL, NULL, pri); |
| 3060 |
ast_agi_send(agi->fd, chan, "200 result=0\n"); |
| 3061 |
return RESULT_SUCCESS; |
| 3062 |
} |
| 3063 |
|
| 3064 |
static int handle_recordfile(struct ast_channel *chan, AGI *agi, int argc, const char * const argv[]) |
| 3065 |
{ |
| 3066 |
struct ast_filestream *fs; |
| 3067 |
struct ast_frame *f; |
| 3068 |
struct timeval start; |
| 3069 |
long sample_offset = 0; |
| 3070 |
int res = 0; |
| 3071 |
int ms; |
| 3072 |
|
| 3073 |
struct ast_dsp *sildet=NULL; |
| 3074 |
int totalsilence = 0; |
| 3075 |
int dspsilence = 0; |
| 3076 |
int silence = 0; |
| 3077 |
int gotsilence = 0; |
| 3078 |
const char *silencestr = NULL; |
| 3079 |
RAII_VAR(struct ast_format *, rfmt, NULL, ao2_cleanup); |
| 3080 |
struct ast_silence_generator *silgen = NULL; |
| 3081 |
|
| 3082 |
|
| 3083 |
|
| 3084 |
if (argc < 6) |
| 3085 |
return RESULT_SHOWUSAGE; |
| 3086 |
if (sscanf(argv[5], "%30d", &ms) != 1) |
| 3087 |
return RESULT_SHOWUSAGE; |
| 3088 |
|
| 3089 |
if (argc > 6) |
| 3090 |
silencestr = strchr(argv[6],'s'); |
| 3091 |
if ((argc > 7) && (!silencestr)) |
| 3092 |
silencestr = strchr(argv[7],'s'); |
| 3093 |
if ((argc > 8) && (!silencestr)) |
| 3094 |
silencestr = strchr(argv[8],'s'); |
| 3095 |
|
| 3096 |
if (silencestr) { |
| 3097 |
if (strlen(silencestr) > 2) { |
| 3098 |
if ((silencestr[0] == 's') && (silencestr[1] == '=')) { |
| 3099 |
silencestr++; |
| 3100 |
silencestr++; |
| 3101 |
if (silencestr) |
| 3102 |
silence = atoi(silencestr); |
| 3103 |
if (silence > 0) |
| 3104 |
silence *= 1000; |
| 3105 |
} |
| 3106 |
} |
| 3107 |
} |
| 3108 |
|
| 3109 |
if (silence > 0) { |
| 3110 |
rfmt = ao2_bump(ast_channel_readformat(chan)); |
| 3111 |
res = ast_set_read_format(chan, ast_format_slin); |
| 3112 |
if (res < 0) { |
| 3113 |
ast_log(LOG_WARNING, "Unable to set to linear mode, giving up\n"); |
| 3114 |
ast_agi_send(agi->fd, chan, "200 result=%d\n", res); |
| 3115 |
return RESULT_FAILURE; |
| 3116 |
} |
| 3117 |
sildet = ast_dsp_new(); |
| 3118 |
if (!sildet) { |
| 3119 |
ast_log(LOG_WARNING, "Unable to create silence detector :(\n"); |
| 3120 |
ast_agi_send(agi->fd, chan, "200 result=-1\n"); |
| 3121 |
return RESULT_FAILURE; |
| 3122 |
} |
| 3123 |
ast_dsp_set_threshold(sildet, ast_dsp_get_threshold_from_settings(THRESHOLD_SILENCE)); |
| 3124 |
} |
| 3125 |
|
| 3126 |
|
| 3127 |
|
| 3128 |
|
| 3129 |
|
| 3130 |
|
| 3131 |
|
| 3132 |
|
| 3133 |
|
| 3134 |
|
| 3135 |
|
| 3136 |
if ((argc > 6 && sscanf(argv[6], "%30ld", &sample_offset) != 1 && !ast_begins_with(argv[6], "s=")) |
| 3137 |
|| (argc > 7 && !ast_begins_with(argv[7], "s="))) { |
| 3138 |
res = ast_streamfile(chan, "beep", ast_channel_language(chan)); |
| 3139 |
} |
| 3140 |
|
| 3141 |
if (!res) |
| 3142 |
res = ast_waitstream(chan, argv[4]); |
| 3143 |
if (res) { |
| 3144 |
ast_agi_send(agi->fd, chan, "200 result=%d (randomerror) endpos=%ld\n", res, sample_offset); |
| 3145 |
} else { |
| 3146 |
fs = ast_writefile(argv[2], argv[3], NULL, O_CREAT | O_WRONLY | (sample_offset ? O_APPEND : 0), 0, AST_FILE_MODE); |
| 3147 |
if (!fs) { |
| 3148 |
res = -1; |
| 3149 |
ast_agi_send(agi->fd, chan, "200 result=%d (writefile)\n", res); |
| 3150 |
if (sildet) |
| 3151 |
ast_dsp_free(sildet); |
| 3152 |
return RESULT_FAILURE; |
| 3153 |
} |
| 3154 |
|
| 3155 |
|
| 3156 |
ast_indicate(chan, AST_CONTROL_VIDUPDATE); |
| 3157 |
|
| 3158 |
ast_channel_stream_set(chan, fs); |
| 3159 |
ast_applystream(chan,fs); |
| 3160 |
|
| 3161 |
ast_seekstream(fs, sample_offset, SEEK_SET); |
| 3162 |
ast_truncstream(fs); |
| 3163 |
|
| 3164 |
if (ast_opt_transmit_silence) { |
| 3165 |
silgen = ast_channel_start_silence_generator(chan); |
| 3166 |
} |
| 3167 |
|
| 3168 |
start = ast_tvnow(); |
| 3169 |
while ((ms < 0) || ast_tvdiff_ms(ast_tvnow(), start) < ms) { |
| 3170 |
res = ast_waitfor(chan, ms - ast_tvdiff_ms(ast_tvnow(), start)); |
| 3171 |
if (res < 0) { |
| 3172 |
ast_closestream(fs); |
| 3173 |
ast_agi_send(agi->fd, chan, "200 result=%d (waitfor) endpos=%ld\n", res,sample_offset); |
| 3174 |
if (sildet) |
| 3175 |
ast_dsp_free(sildet); |
| 3176 |
if (silgen) |
| 3177 |
ast_channel_stop_silence_generator(chan, silgen); |
| 3178 |
return RESULT_FAILURE; |
| 3179 |
} |
| 3180 |
f = ast_read(chan); |
| 3181 |
if (!f) { |
| 3182 |
ast_closestream(fs); |
| 3183 |
ast_agi_send(agi->fd, chan, "200 result=%d (hangup) endpos=%ld\n", -1, sample_offset); |
| 3184 |
if (sildet) |
| 3185 |
ast_dsp_free(sildet); |
| 3186 |
if (silgen) |
| 3187 |
ast_channel_stop_silence_generator(chan, silgen); |
| 3188 |
return RESULT_FAILURE; |
| 3189 |
} |
| 3190 |
switch(f->frametype) { |
| 3191 |
case AST_FRAME_DTMF: |
| 3192 |
if (strchr(argv[4], f->subclass.integer)) { |
| 3193 |
|
| 3194 |
|
| 3195 |
|
| 3196 |
ast_stream_rewind(fs, 200); |
| 3197 |
ast_truncstream(fs); |
| 3198 |
sample_offset = ast_tellstream(fs); |
| 3199 |
ast_closestream(fs); |
| 3200 |
ast_agi_send(agi->fd, chan, "200 result=%d (dtmf) endpos=%ld\n", f->subclass.integer, sample_offset); |
| 3201 |
ast_frfree(f); |
| 3202 |
if (sildet) |
| 3203 |
ast_dsp_free(sildet); |
| 3204 |
if (silgen) |
| 3205 |
ast_channel_stop_silence_generator(chan, silgen); |
| 3206 |
return RESULT_SUCCESS; |
| 3207 |
} |
| 3208 |
break; |
| 3209 |
case AST_FRAME_VOICE: |
| 3210 |
ast_writestream(fs, f); |
| 3211 |
|
| 3212 |
|
| 3213 |
|
| 3214 |
sample_offset = ast_tellstream(fs); |
| 3215 |
if (silence > 0) { |
| 3216 |
dspsilence = 0; |
| 3217 |
ast_dsp_silence(sildet, f, &dspsilence); |
| 3218 |
if (dspsilence) { |
| 3219 |
totalsilence = dspsilence; |
| 3220 |
} else { |
| 3221 |
totalsilence = 0; |
| 3222 |
} |
| 3223 |
if (totalsilence > silence) { |
| 3224 |
|
| 3225 |
gotsilence = 1; |
| 3226 |
break; |
| 3227 |
} |
| 3228 |
} |
| 3229 |
break; |
| 3230 |
case AST_FRAME_VIDEO: |
| 3231 |
ast_writestream(fs, f); |
| 3232 |
default: |
| 3233 |
|
| 3234 |
break; |
| 3235 |
} |
| 3236 |
ast_frfree(f); |
| 3237 |
if (gotsilence) |
| 3238 |
break; |
| 3239 |
} |
| 3240 |
|
| 3241 |
if (gotsilence) { |
| 3242 |
ast_stream_rewind(fs, silence-1000); |
| 3243 |
ast_truncstream(fs); |
| 3244 |
sample_offset = ast_tellstream(fs); |
| 3245 |
} |
| 3246 |
ast_closestream(fs); |
| 3247 |
ast_agi_send(agi->fd, chan, "200 result=%d (timeout) endpos=%ld\n", res, sample_offset); |
| 3248 |
} |
| 3249 |
|
| 3250 |
if (silence > 0) { |
| 3251 |
res = ast_set_read_format(chan, rfmt); |
| 3252 |
if (res) |
| 3253 |
ast_log(LOG_WARNING, "Unable to restore read format on '%s'\n", ast_channel_name(chan)); |
| 3254 |
ast_dsp_free(sildet); |
| 3255 |
} |
| 3256 |
|
| 3257 |
if (silgen) { |
| 3258 |
ast_channel_stop_silence_generator(chan, silgen); |
| 3259 |
} |
| 3260 |
|
| 3261 |
return RESULT_SUCCESS; |
| 3262 |
} |
| 3263 |
|
| 3264 |
static int handle_autohangup(struct ast_channel *chan, AGI *agi, int argc, const char * const argv[]) |
| 3265 |
{ |
| 3266 |
double timeout; |
| 3267 |
struct timeval whentohangup = { 0, 0 }; |
| 3268 |
|
| 3269 |
if (argc != 3) |
| 3270 |
return RESULT_SHOWUSAGE; |
| 3271 |
if (sscanf(argv[2], "%30lf", &timeout) != 1) |
| 3272 |
return RESULT_SHOWUSAGE; |
| 3273 |
if (timeout < 0) |
| 3274 |
timeout = 0; |
| 3275 |
if (timeout) { |
| 3276 |
whentohangup.tv_sec = timeout; |
| 3277 |
whentohangup.tv_usec = (timeout - whentohangup.tv_sec) * 1000000.0; |
| 3278 |
} |
| 3279 |
ast_channel_lock(chan); |
| 3280 |
ast_channel_setwhentohangup_tv(chan, whentohangup); |
| 3281 |
ast_channel_unlock(chan); |
| 3282 |
ast_agi_send(agi->fd, chan, "200 result=0\n"); |
| 3283 |
return RESULT_SUCCESS; |
| 3284 |
} |
| 3285 |
|
| 3286 |
static int handle_hangup(struct ast_channel *chan, AGI *agi, int argc, const char * const argv[]) |
| 3287 |
{ |
| 3288 |
struct ast_channel *c; |
| 3289 |
|
| 3290 |
if (argc == 1) { |
| 3291 |
|
| 3292 |
ast_set_hangupsource(chan, "dialplan/agi", 0); |
| 3293 |
ast_softhangup(chan,AST_SOFTHANGUP_EXPLICIT); |
| 3294 |
ast_agi_send(agi->fd, chan, "200 result=1\n"); |
| 3295 |
return RESULT_SUCCESS; |
| 3296 |
} else if (argc == 2) { |
| 3297 |
|
| 3298 |
if ((c = ast_channel_get_by_name(argv[1]))) { |
| 3299 |
|
| 3300 |
ast_set_hangupsource(c, "dialplan/agi", 0); |
| 3301 |
ast_softhangup(c, AST_SOFTHANGUP_EXPLICIT); |
| 3302 |
c = ast_channel_unref(c); |
| 3303 |
ast_agi_send(agi->fd, chan, "200 result=1\n"); |
| 3304 |
return RESULT_SUCCESS; |
| 3305 |
} |
| 3306 |
|
| 3307 |
ast_agi_send(agi->fd, chan, "200 result=-1\n"); |
| 3308 |
return RESULT_SUCCESS; |
| 3309 |
} else { |
| 3310 |
return RESULT_SHOWUSAGE; |
| 3311 |
} |
| 3312 |
} |
| 3313 |
|
| 3314 |
static int handle_exec(struct ast_channel *chan, AGI *agi, int argc, const char * const argv[]) |
| 3315 |
{ |
| 3316 |
int res, workaround; |
| 3317 |
struct ast_app *app_to_exec; |
| 3318 |
const char *agi_exec_full_str; |
| 3319 |
int agi_exec_full; |
| 3320 |
struct ast_str *data_with_var = NULL; |
| 3321 |
|
| 3322 |
if (argc < 2) |
| 3323 |
return RESULT_SHOWUSAGE; |
| 3324 |
|
| 3325 |
ast_verb(3, "AGI Script Executing Application: (%s) Options: (%s)\n", argv[1], argc >= 3 ? argv[2] : ""); |
| 3326 |
|
| 3327 |
if ((app_to_exec = pbx_findapp(argv[1]))) { |
| 3328 |
ast_channel_lock(chan); |
| 3329 |
if (!(workaround = ast_test_flag(ast_channel_flags(chan), AST_FLAG_DISABLE_WORKAROUNDS))) { |
| 3330 |
ast_set_flag(ast_channel_flags(chan), AST_FLAG_DISABLE_WORKAROUNDS); |
| 3331 |
} |
| 3332 |
agi_exec_full_str = pbx_builtin_getvar_helper(chan, "AGIEXECFULL"); |
| 3333 |
agi_exec_full = ast_true(agi_exec_full_str); |
| 3334 |
ast_channel_unlock(chan); |
| 3335 |
|
| 3336 |
if (agi_exec_full) { |
| 3337 |
if ((data_with_var = ast_str_create(16))) { |
| 3338 |
ast_str_substitute_variables(&data_with_var, 0, chan, argv[2]); |
| 3339 |
res = pbx_exec(chan, app_to_exec, argc == 2 ? "" : ast_str_buffer(data_with_var)); |
| 3340 |
ast_free(data_with_var); |
| 3341 |
} else { |
| 3342 |
res = -2; |
| 3343 |
} |
| 3344 |
} else { |
| 3345 |
res = pbx_exec(chan, app_to_exec, argc == 2 ? "" : argv[2]); |
| 3346 |
} |
| 3347 |
if (!workaround) { |
| 3348 |
ast_channel_clear_flag(chan, AST_FLAG_DISABLE_WORKAROUNDS); |
| 3349 |
} |
| 3350 |
} else { |
| 3351 |
ast_log(LOG_WARNING, "Could not find application (%s)\n", argv[1]); |
| 3352 |
res = -2; |
| 3353 |
} |
| 3354 |
ast_agi_send(agi->fd, chan, "200 result=%d\n", res); |
| 3355 |
|
| 3356 |
|
| 3357 |
return res; |
| 3358 |
} |
| 3359 |
|
| 3360 |
static int handle_setcallerid(struct ast_channel *chan, AGI *agi, int argc, const char * const argv[]) |
| 3361 |
{ |
| 3362 |
char tmp[256]=""; |
| 3363 |
char *l = NULL, *n = NULL; |
| 3364 |
|
| 3365 |
if (argv[2]) { |
| 3366 |
ast_copy_string(tmp, argv[2], sizeof(tmp)); |
| 3367 |
ast_callerid_parse(tmp, &n, &l); |
| 3368 |
if (l) |
| 3369 |
ast_shrink_phone_number(l); |
| 3370 |
else |
| 3371 |
l = ""; |
| 3372 |
if (!n) |
| 3373 |
n = ""; |
| 3374 |
ast_set_callerid(chan, l, n, NULL); |
| 3375 |
} |
| 3376 |
|
| 3377 |
ast_agi_send(agi->fd, chan, "200 result=1\n"); |
| 3378 |
return RESULT_SUCCESS; |
| 3379 |
} |
| 3380 |
|
| 3381 |
static int handle_channelstatus(struct ast_channel *chan, AGI *agi, int argc, const char * const argv[]) |
| 3382 |
{ |
| 3383 |
if (argc == 2) { |
| 3384 |
|
| 3385 |
ast_agi_send(agi->fd, chan, "200 result=%u\n", ast_channel_state(chan)); |
| 3386 |
return RESULT_SUCCESS; |
| 3387 |
} else if (argc == 3) { |
| 3388 |
struct ast_channel_snapshot *snapshot; |
| 3389 |
|
| 3390 |
|
| 3391 |
snapshot = ast_channel_snapshot_get_latest_by_name(argv[2]); |
| 3392 |
if (snapshot) { |
| 3393 |
ast_agi_send(agi->fd, chan, "200 result=%u\n", snapshot->state); |
| 3394 |
ao2_ref(snapshot, -1); |
| 3395 |
return RESULT_SUCCESS; |
| 3396 |
} |
| 3397 |
|
| 3398 |
ast_agi_send(agi->fd, chan, "200 result=-1\n"); |
| 3399 |
return RESULT_SUCCESS; |
| 3400 |
} else { |
| 3401 |
return RESULT_SHOWUSAGE; |
| 3402 |
} |
| 3403 |
} |
| 3404 |
|
| 3405 |
static int handle_setvariable(struct ast_channel *chan, AGI *agi, int argc, const char * const argv[]) |
| 3406 |
{ |
| 3407 |
if (argc != 4) { |
| 3408 |
return RESULT_SHOWUSAGE; |
| 3409 |
} |
| 3410 |
|
| 3411 |
if (argv[3]) |
| 3412 |
pbx_builtin_setvar_helper(chan, argv[2], argv[3]); |
| 3413 |
|
| 3414 |
ast_agi_send(agi->fd, chan, "200 result=1\n"); |
| 3415 |
return RESULT_SUCCESS; |
| 3416 |
} |
| 3417 |
|
| 3418 |
static int handle_getvariable(struct ast_channel *chan, AGI *agi, int argc, const char * const argv[]) |
| 3419 |
{ |
| 3420 |
char *ret; |
| 3421 |
char tempstr[1024] = ""; |
| 3422 |
|
| 3423 |
if (argc != 3) |
| 3424 |
return RESULT_SHOWUSAGE; |
| 3425 |
|
| 3426 |
|
| 3427 |
if (!ast_strlen_zero(argv[2]) && (argv[2][strlen(argv[2]) - 1] == ')')) { |
| 3428 |
ret = ast_func_read(chan, argv[2], tempstr, sizeof(tempstr)) ? NULL : tempstr; |
| 3429 |
} else { |
| 3430 |
pbx_retrieve_variable(chan, argv[2], &ret, tempstr, sizeof(tempstr), NULL); |
| 3431 |
} |
| 3432 |
|
| 3433 |
if (ret) |
| 3434 |
ast_agi_send(agi->fd, chan, "200 result=1 (%s)\n", ret); |
| 3435 |
else |
| 3436 |
ast_agi_send(agi->fd, chan, "200 result=0\n"); |
| 3437 |
|
| 3438 |
return RESULT_SUCCESS; |
| 3439 |
} |
| 3440 |
|
| 3441 |
static int handle_getvariablefull(struct ast_channel *chan, AGI *agi, int argc, const char * const argv[]) |
| 3442 |
{ |
| 3443 |
struct ast_channel *chan2 = NULL; |
| 3444 |
|
| 3445 |
if (argc != 4 && argc != 5) { |
| 3446 |
return RESULT_SHOWUSAGE; |
| 3447 |
} |
| 3448 |
|
| 3449 |
if (argc == 5) { |
| 3450 |
chan2 = ast_channel_get_by_name(argv[4]); |
| 3451 |
} else { |
| 3452 |
chan2 = ast_channel_ref(chan); |
| 3453 |
} |
| 3454 |
|
| 3455 |
if (chan2) { |
| 3456 |
struct ast_str *str = ast_str_create(16); |
| 3457 |
if (!str) { |
| 3458 |
ast_agi_send(agi->fd, chan, "200 result=0\n"); |
| 3459 |
return RESULT_SUCCESS; |
| 3460 |
} |
| 3461 |
ast_str_substitute_variables(&str, 0, chan2, argv[3]); |
| 3462 |
ast_agi_send(agi->fd, chan, "200 result=1 (%s)\n", ast_str_buffer(str)); |
| 3463 |
ast_free(str); |
| 3464 |
} else { |
| 3465 |
ast_agi_send(agi->fd, chan, "200 result=0\n"); |
| 3466 |
} |
| 3467 |
|
| 3468 |
if (chan2) { |
| 3469 |
chan2 = ast_channel_unref(chan2); |
| 3470 |
} |
| 3471 |
|
| 3472 |
return RESULT_SUCCESS; |
| 3473 |
} |
| 3474 |
|
| 3475 |
static int handle_verbose(struct ast_channel *chan, AGI *agi, int argc, const char * const argv[]) |
| 3476 |
{ |
| 3477 |
int level = 0; |
| 3478 |
|
| 3479 |
if (argc < 2) |
| 3480 |
return RESULT_SHOWUSAGE; |
| 3481 |
|
| 3482 |
if (argv[2]) |
| 3483 |
sscanf(argv[2], "%30d", &level); |
| 3484 |
|
| 3485 |
ast_verb(level, "%s: %s\n", ast_channel_data(chan), argv[1]); |
| 3486 |
|
| 3487 |
ast_agi_send(agi->fd, chan, "200 result=1\n"); |
| 3488 |
|
| 3489 |
return RESULT_SUCCESS; |
| 3490 |
} |
| 3491 |
|
| 3492 |
static int handle_dbget(struct ast_channel *chan, AGI *agi, int argc, const char * const argv[]) |
| 3493 |
{ |
| 3494 |
int res; |
| 3495 |
struct ast_str *buf; |
| 3496 |
|
| 3497 |
if (argc != 4) |
| 3498 |
return RESULT_SHOWUSAGE; |
| 3499 |
|
| 3500 |
if (!(buf = ast_str_create(16))) { |
| 3501 |
ast_agi_send(agi->fd, chan, "200 result=-1\n"); |
| 3502 |
return RESULT_SUCCESS; |
| 3503 |
} |
| 3504 |
|
| 3505 |
do { |
| 3506 |
res = ast_db_get(argv[2], argv[3], ast_str_buffer(buf), ast_str_size(buf)); |
| 3507 |
ast_str_update(buf); |
| 3508 |
if (ast_str_strlen(buf) < ast_str_size(buf) - 1) { |
| 3509 |
break; |
| 3510 |
} |
| 3511 |
if (ast_str_make_space(&buf, ast_str_size(buf) * 2)) { |
| 3512 |
break; |
| 3513 |
} |
| 3514 |
} while (1); |
| 3515 |
|
| 3516 |
if (res) |
| 3517 |
ast_agi_send(agi->fd, chan, "200 result=0\n"); |
| 3518 |
else |
| 3519 |
ast_agi_send(agi->fd, chan, "200 result=1 (%s)\n", ast_str_buffer(buf)); |
| 3520 |
|
| 3521 |
ast_free(buf); |
| 3522 |
return RESULT_SUCCESS; |
| 3523 |
} |
| 3524 |
|
| 3525 |
static int handle_dbput(struct ast_channel *chan, AGI *agi, int argc, const char * const argv[]) |
| 3526 |
{ |
| 3527 |
int res; |
| 3528 |
|
| 3529 |
if (argc != 5) |
| 3530 |
return RESULT_SHOWUSAGE; |
| 3531 |
res = ast_db_put(argv[2], argv[3], argv[4]); |
| 3532 |
ast_agi_send(agi->fd, chan, "200 result=%c\n", res ? '0' : '1'); |
| 3533 |
return RESULT_SUCCESS; |
| 3534 |
} |
| 3535 |
|
| 3536 |
static int handle_dbdel(struct ast_channel *chan, AGI *agi, int argc, const char * const argv[]) |
| 3537 |
{ |
| 3538 |
int res; |
| 3539 |
|
| 3540 |
if (argc != 4) |
| 3541 |
return RESULT_SHOWUSAGE; |
| 3542 |
res = ast_db_del(argv[2], argv[3]); |
| 3543 |
ast_agi_send(agi->fd, chan, "200 result=%c\n", res ? '0' : '1'); |
| 3544 |
return RESULT_SUCCESS; |
| 3545 |
} |
| 3546 |
|
| 3547 |
static int handle_dbdeltree(struct ast_channel *chan, AGI *agi, int argc, const char * const argv[]) |
| 3548 |
{ |
| 3549 |
int num_deleted; |
| 3550 |
|
| 3551 |
if ((argc < 3) || (argc > 4)) { |
| 3552 |
return RESULT_SHOWUSAGE; |
| 3553 |
} |
| 3554 |
if (argc == 4) { |
| 3555 |
num_deleted = ast_db_deltree(argv[2], argv[3]); |
| 3556 |
} else { |
| 3557 |
num_deleted = ast_db_deltree(argv[2], NULL); |
| 3558 |
} |
| 3559 |
|
| 3560 |
ast_agi_send(agi->fd, chan, "200 result=%c\n", num_deleted > 0 ? '0' : '1'); |
| 3561 |
return RESULT_SUCCESS; |
| 3562 |
} |
| 3563 |
|
| 3564 |
static char *handle_cli_agi_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 3565 |
{ |
| 3566 |
switch (cmd) { |
| 3567 |
case CLI_INIT: |
| 3568 |
e->command = "agi set debug [on|off]"; |
| 3569 |
e->usage = |
| 3570 |
"Usage: agi set debug [on|off]\n" |
| 3571 |
" Enables/disables dumping of AGI transactions for\n" |
| 3572 |
" debugging purposes.\n"; |
| 3573 |
return NULL; |
| 3574 |
|
| 3575 |
case CLI_GENERATE: |
| 3576 |
return NULL; |
| 3577 |
} |
| 3578 |
|
| 3579 |
if (a->argc != e->args) |
| 3580 |
return CLI_SHOWUSAGE; |
| 3581 |
|
| 3582 |
if (strncasecmp(a->argv[3], "off", 3) == 0) { |
| 3583 |
agidebug = 0; |
| 3584 |
} else if (strncasecmp(a->argv[3], "on", 2) == 0) { |
| 3585 |
agidebug = 1; |
| 3586 |
} else { |
| 3587 |
return CLI_SHOWUSAGE; |
| 3588 |
} |
| 3589 |
ast_cli(a->fd, "AGI Debugging %sabled\n", agidebug ? "En" : "Dis"); |
| 3590 |
return CLI_SUCCESS; |
| 3591 |
} |
| 3592 |
|
| 3593 |
static int handle_noop(struct ast_channel *chan, AGI *agi, int arg, const char * const argv[]) |
| 3594 |
{ |
| 3595 |
ast_agi_send(agi->fd, chan, "200 result=0\n"); |
| 3596 |
return RESULT_SUCCESS; |
| 3597 |
} |
| 3598 |
|
| 3599 |
static int handle_setmusic(struct ast_channel *chan, AGI *agi, int argc, const char * const argv[]) |
| 3600 |
{ |
| 3601 |
if (argc < 3) { |
| 3602 |
return RESULT_SHOWUSAGE; |
| 3603 |
} |
| 3604 |
if (!strncasecmp(argv[2], "on", 2)) |
| 3605 |
ast_moh_start(chan, argc > 3 ? argv[3] : NULL, NULL); |
| 3606 |
else if (!strncasecmp(argv[2], "off", 3)) |
| 3607 |
ast_moh_stop(chan); |
| 3608 |
ast_agi_send(agi->fd, chan, "200 result=0\n"); |
| 3609 |
return RESULT_SUCCESS; |
| 3610 |
} |
| 3611 |
|
| 3612 |
static int handle_speechcreate(struct ast_channel *chan, AGI *agi, int argc, const char * const argv[]) |
| 3613 |
{ |
| 3614 |
struct ast_format_cap *cap; |
| 3615 |
|
| 3616 |
|
| 3617 |
if (agi->speech) { |
| 3618 |
ast_agi_send(agi->fd, chan, "200 result=0\n"); |
| 3619 |
return RESULT_SUCCESS; |
| 3620 |
} |
| 3621 |
|
| 3622 |
if (!(cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) { |
| 3623 |
return RESULT_FAILURE; |
| 3624 |
} |
| 3625 |
ast_format_cap_append(cap, ast_format_slin, 0); |
| 3626 |
if ((agi->speech = ast_speech_new(argv[2], cap))) { |
| 3627 |
ast_agi_send(agi->fd, chan, "200 result=1\n"); |
| 3628 |
} else { |
| 3629 |
ast_agi_send(agi->fd, chan, "200 result=0\n"); |
| 3630 |
} |
| 3631 |
ao2_ref(cap, -1); |
| 3632 |
|
| 3633 |
return RESULT_SUCCESS; |
| 3634 |
} |
| 3635 |
|
| 3636 |
static int handle_speechset(struct ast_channel *chan, AGI *agi, int argc, const char * const argv[]) |
| 3637 |
{ |
| 3638 |
|
| 3639 |
if (argc != 4) |
| 3640 |
return RESULT_SHOWUSAGE; |
| 3641 |
|
| 3642 |
|
| 3643 |
if (!agi->speech) { |
| 3644 |
ast_agi_send(agi->fd, chan, "200 result=0\n"); |
| 3645 |
return RESULT_SUCCESS; |
| 3646 |
} |
| 3647 |
|
| 3648 |
ast_speech_change(agi->speech, argv[2], argv[3]); |
| 3649 |
ast_agi_send(agi->fd, chan, "200 result=1\n"); |
| 3650 |
|
| 3651 |
return RESULT_SUCCESS; |
| 3652 |
} |
| 3653 |
|
| 3654 |
static int handle_speechdestroy(struct ast_channel *chan, AGI *agi, int argc, const char * const argv[]) |
| 3655 |
{ |
| 3656 |
if (agi->speech) { |
| 3657 |
ast_speech_destroy(agi->speech); |
| 3658 |
agi->speech = NULL; |
| 3659 |
ast_agi_send(agi->fd, chan, "200 result=1\n"); |
| 3660 |
} else { |
| 3661 |
ast_agi_send(agi->fd, chan, "200 result=0\n"); |
| 3662 |
} |
| 3663 |
|
| 3664 |
return RESULT_SUCCESS; |
| 3665 |
} |
| 3666 |
|
| 3667 |
static int handle_speechloadgrammar(struct ast_channel *chan, AGI *agi, int argc, const char * const argv[]) |
| 3668 |
{ |
| 3669 |
if (argc != 5) |
| 3670 |
return RESULT_SHOWUSAGE; |
| 3671 |
|
| 3672 |
if (!agi->speech) { |
| 3673 |
ast_agi_send(agi->fd, chan, "200 result=0\n"); |
| 3674 |
return RESULT_SUCCESS; |
| 3675 |
} |
| 3676 |
|
| 3677 |
if (ast_speech_grammar_load(agi->speech, argv[3], argv[4])) |
| 3678 |
ast_agi_send(agi->fd, chan, "200 result=0\n"); |
| 3679 |
else |
| 3680 |
ast_agi_send(agi->fd, chan, "200 result=1\n"); |
| 3681 |
|
| 3682 |
return RESULT_SUCCESS; |
| 3683 |
} |
| 3684 |
|
| 3685 |
static int handle_speechunloadgrammar(struct ast_channel *chan, AGI *agi, int argc, const char * const argv[]) |
| 3686 |
{ |
| 3687 |
if (argc != 4) |
| 3688 |
return RESULT_SHOWUSAGE; |
| 3689 |
|
| 3690 |
if (!agi->speech) { |
| 3691 |
ast_agi_send(agi->fd, chan, "200 result=0\n"); |
| 3692 |
return RESULT_SUCCESS; |
| 3693 |
} |
| 3694 |
|
| 3695 |
if (ast_speech_grammar_unload(agi->speech, argv[3])) |
| 3696 |
ast_agi_send(agi->fd, chan, "200 result=0\n"); |
| 3697 |
else |
| 3698 |
ast_agi_send(agi->fd, chan, "200 result=1\n"); |
| 3699 |
|
| 3700 |
return RESULT_SUCCESS; |
| 3701 |
} |
| 3702 |
|
| 3703 |
static int handle_speechactivategrammar(struct ast_channel *chan, AGI *agi, int argc, const char * const argv[]) |
| 3704 |
{ |
| 3705 |
if (argc != 4) |
| 3706 |
return RESULT_SHOWUSAGE; |
| 3707 |
|
| 3708 |
if (!agi->speech) { |
| 3709 |
ast_agi_send(agi->fd, chan, "200 result=0\n"); |
| 3710 |
return RESULT_SUCCESS; |
| 3711 |
} |
| 3712 |
|
| 3713 |
if (ast_speech_grammar_activate(agi->speech, argv[3])) |
| 3714 |
ast_agi_send(agi->fd, chan, "200 result=0\n"); |
| 3715 |
else |
| 3716 |
ast_agi_send(agi->fd, chan, "200 result=1\n"); |
| 3717 |
|
| 3718 |
return RESULT_SUCCESS; |
| 3719 |
} |
| 3720 |
|
| 3721 |
static int handle_speechdeactivategrammar(struct ast_channel *chan, AGI *agi, int argc, const char * const argv[]) |
| 3722 |
{ |
| 3723 |
if (argc != 4) |
| 3724 |
return RESULT_SHOWUSAGE; |
| 3725 |
|
| 3726 |
if (!agi->speech) { |
| 3727 |
ast_agi_send(agi->fd, chan, "200 result=0\n"); |
| 3728 |
return RESULT_SUCCESS; |
| 3729 |
} |
| 3730 |
|
| 3731 |
if (ast_speech_grammar_deactivate(agi->speech, argv[3])) |
| 3732 |
ast_agi_send(agi->fd, chan, "200 result=0\n"); |
| 3733 |
else |
| 3734 |
ast_agi_send(agi->fd, chan, "200 result=1\n"); |
| 3735 |
|
| 3736 |
return RESULT_SUCCESS; |
| 3737 |
} |
| 3738 |
|
| 3739 |
static int speech_streamfile(struct ast_channel *chan, const char *filename, const char *preflang, int offset) |
| 3740 |
{ |
| 3741 |
struct ast_filestream *fs = NULL; |
| 3742 |
|
| 3743 |
if (!(fs = ast_openstream(chan, filename, preflang))) |
| 3744 |
return -1; |
| 3745 |
|
| 3746 |
if (offset) |
| 3747 |
ast_seekstream(fs, offset, SEEK_SET); |
| 3748 |
|
| 3749 |
if (ast_applystream(chan, fs)) |
| 3750 |
return -1; |
| 3751 |
|
| 3752 |
if (ast_playstream(fs)) |
| 3753 |
return -1; |
| 3754 |
|
| 3755 |
return 0; |
| 3756 |
} |
| 3757 |
|
| 3758 |
static int handle_speechrecognize(struct ast_channel *chan, AGI *agi, int argc, const char * const argv[]) |
| 3759 |
{ |
| 3760 |
struct ast_speech *speech = agi->speech; |
| 3761 |
const char *prompt; |
| 3762 |
char dtmf = 0, tmp[4096] = "", *buf = tmp; |
| 3763 |
int timeout = 0, offset = 0, res = 0, i = 0; |
| 3764 |
long current_offset = 0; |
| 3765 |
const char *reason = NULL; |
| 3766 |
struct ast_frame *fr = NULL; |
| 3767 |
struct ast_speech_result *result = NULL; |
| 3768 |
size_t left = sizeof(tmp); |
| 3769 |
time_t start = 0, current; |
| 3770 |
|
| 3771 |
if (argc < 4) |
| 3772 |
return RESULT_SHOWUSAGE; |
| 3773 |
|
| 3774 |
if (!speech) { |
| 3775 |
ast_agi_send(agi->fd, chan, "200 result=0\n"); |
| 3776 |
return RESULT_SUCCESS; |
| 3777 |
} |
| 3778 |
|
| 3779 |
prompt = argv[2]; |
| 3780 |
timeout = atoi(argv[3]); |
| 3781 |
|
| 3782 |
|
| 3783 |
if (argc == 5) |
| 3784 |
offset = atoi(argv[4]); |
| 3785 |
|
| 3786 |
|
| 3787 |
if (ast_set_read_format(chan, ast_format_slin)) { |
| 3788 |
ast_agi_send(agi->fd, chan, "200 result=0\n"); |
| 3789 |
return RESULT_SUCCESS; |
| 3790 |
} |
| 3791 |
|
| 3792 |
|
| 3793 |
if (speech->state == AST_SPEECH_STATE_NOT_READY || speech->state == AST_SPEECH_STATE_DONE) { |
| 3794 |
ast_speech_change_state(speech, AST_SPEECH_STATE_NOT_READY); |
| 3795 |
ast_speech_start(speech); |
| 3796 |
} |
| 3797 |
|
| 3798 |
|
| 3799 |
speech_streamfile(chan, prompt, ast_channel_language(chan), offset); |
| 3800 |
|
| 3801 |
|
| 3802 |
while (ast_strlen_zero(reason)) { |
| 3803 |
|
| 3804 |
ast_sched_runq(ast_channel_sched(chan)); |
| 3805 |
|
| 3806 |
|
| 3807 |
if ((res = ast_sched_wait(ast_channel_sched(chan))) < 0) |
| 3808 |
res = 1000; |
| 3809 |
|
| 3810 |
|
| 3811 |
if (ast_waitfor(chan, res) > 0) { |
| 3812 |
if (!(fr = ast_read(chan))) { |
| 3813 |
reason = "hangup"; |
| 3814 |
break; |
| 3815 |
} |
| 3816 |
} |
| 3817 |
|
| 3818 |
|
| 3819 |
if ((timeout > 0) && (start > 0)) { |
| 3820 |
time(¤t); |
| 3821 |
if ((current - start) >= timeout) { |
| 3822 |
reason = "timeout"; |
| 3823 |
if (fr) { |
| 3824 |
ast_frfree(fr); |
| 3825 |
fr = NULL; |
| 3826 |
} |
| 3827 |
break; |
| 3828 |
} |
| 3829 |
} |
| 3830 |
|
| 3831 |
|
| 3832 |
ast_mutex_lock(&speech->lock); |
| 3833 |
|
| 3834 |
|
| 3835 |
if (ast_test_flag(speech, AST_SPEECH_QUIET) && ast_channel_stream(chan)) { |
| 3836 |
current_offset = ast_tellstream(ast_channel_stream(chan)); |
| 3837 |
ast_stopstream(chan); |
| 3838 |
ast_clear_flag(speech, AST_SPEECH_QUIET); |
| 3839 |
} |
| 3840 |
|
| 3841 |
|
| 3842 |
switch (speech->state) { |
| 3843 |
case AST_SPEECH_STATE_READY: |
| 3844 |
|
| 3845 |
if ((timeout > 0) && start == 0 && ((!ast_channel_stream(chan)) || (ast_channel_streamid(chan) == -1 && ast_channel_timingfunc(chan) == NULL))) { |
| 3846 |
ast_stopstream(chan); |
| 3847 |
time(&start); |
| 3848 |
} |
| 3849 |
|
| 3850 |
if (fr && fr->frametype == AST_FRAME_VOICE) |
| 3851 |
ast_speech_write(speech, fr->data.ptr, fr->datalen); |
| 3852 |
break; |
| 3853 |
case AST_SPEECH_STATE_WAIT: |
| 3854 |
|
| 3855 |
if ((!ast_channel_stream(chan)) || (ast_channel_streamid(chan) == -1 && ast_channel_timingfunc(chan) == NULL)) { |
| 3856 |
ast_stopstream(chan); |
| 3857 |
|
| 3858 |
if (!ast_strlen_zero(speech->processing_sound) && strcasecmp(speech->processing_sound, "none")) |
| 3859 |
speech_streamfile(chan, speech->processing_sound, ast_channel_language(chan), 0); |
| 3860 |
} |
| 3861 |
break; |
| 3862 |
case AST_SPEECH_STATE_DONE: |
| 3863 |
|
| 3864 |
speech->results = ast_speech_results_get(speech); |
| 3865 |
|
| 3866 |
ast_speech_change_state(speech, AST_SPEECH_STATE_NOT_READY); |
| 3867 |
reason = "speech"; |
| 3868 |
break; |
| 3869 |
default: |
| 3870 |
break; |
| 3871 |
} |
| 3872 |
ast_mutex_unlock(&speech->lock); |
| 3873 |
|
| 3874 |
|
| 3875 |
if (fr) { |
| 3876 |
if (fr->frametype == AST_FRAME_DTMF) { |
| 3877 |
reason = "dtmf"; |
| 3878 |
dtmf = fr->subclass.integer; |
| 3879 |
} else if (fr->frametype == AST_FRAME_CONTROL && fr->subclass.integer == AST_CONTROL_HANGUP) { |
| 3880 |
reason = "hangup"; |
| 3881 |
} |
| 3882 |
ast_frfree(fr); |
| 3883 |
fr = NULL; |
| 3884 |
} |
| 3885 |
} |
| 3886 |
|
| 3887 |
if (!strcasecmp(reason, "speech")) { |
| 3888 |
|
| 3889 |
for (result = speech->results; result; result = AST_LIST_NEXT(result, list)) { |
| 3890 |
|
| 3891 |
ast_build_string(&buf, &left, "%sscore%d=%d text%d=\"%s\" grammar%d=%s", (i > 0 ? " " : ""), i, result->score, i, result->text, i, result->grammar); |
| 3892 |
|
| 3893 |
i++; |
| 3894 |
} |
| 3895 |
|
| 3896 |
ast_agi_send(agi->fd, chan, "200 result=1 (speech) endpos=%ld results=%d %s\n", current_offset, i, tmp); |
| 3897 |
} else if (!strcasecmp(reason, "dtmf")) { |
| 3898 |
ast_agi_send(agi->fd, chan, "200 result=1 (digit) digit=%c endpos=%ld\n", dtmf, current_offset); |
| 3899 |
} else if (!strcasecmp(reason, "hangup") || !strcasecmp(reason, "timeout")) { |
| 3900 |
ast_agi_send(agi->fd, chan, "200 result=1 (%s) endpos=%ld\n", reason, current_offset); |
| 3901 |
} else { |
| 3902 |
ast_agi_send(agi->fd, chan, "200 result=0 endpos=%ld\n", current_offset); |
| 3903 |
} |
| 3904 |
|
| 3905 |
return RESULT_SUCCESS; |
| 3906 |
} |
| 3907 |
|
| 3908 |
|
| 3909 |
|
| 3910 |
|
| 3911 |
static struct agi_command commands[] = { |
| 3912 |
{ { "answer", NULL }, handle_answer, NULL, NULL, 0 }, |
| 3913 |
{ { "asyncagi", "break", NULL }, handle_asyncagi_break, NULL, NULL, 1 }, |
| 3914 |
{ { "channel", "status", NULL }, handle_channelstatus, NULL, NULL, 0 }, |
| 3915 |
{ { "database", "del", NULL }, handle_dbdel, NULL, NULL, 1 }, |
| 3916 |
{ { "database", "deltree", NULL }, handle_dbdeltree, NULL, NULL, 1 }, |
| 3917 |
{ { "database", "get", NULL }, handle_dbget, NULL, NULL, 1 }, |
| 3918 |
{ { "database", "put", NULL }, handle_dbput, NULL, NULL, 1 }, |
| 3919 |
{ { "exec", NULL }, handle_exec, NULL, NULL, 1 }, |
| 3920 |
{ { "get", "data", NULL }, handle_getdata, NULL, NULL, 0 }, |
| 3921 |
{ { "get", "full", "variable", NULL }, handle_getvariablefull, NULL, NULL, 1 }, |
| 3922 |
{ { "get", "option", NULL }, handle_getoption, NULL, NULL, 0 }, |
| 3923 |
{ { "get", "variable", NULL }, handle_getvariable, NULL, NULL, 1 }, |
| 3924 |
{ { "hangup", NULL }, handle_hangup, NULL, NULL, 0 }, |
| 3925 |
{ { "noop", NULL }, handle_noop, NULL, NULL, 1 }, |
| 3926 |
{ { "receive", "char", NULL }, handle_recvchar, NULL, NULL, 0 }, |
| 3927 |
{ { "receive", "text", NULL }, handle_recvtext, NULL, NULL, 0 }, |
| 3928 |
{ { "record", "file", NULL }, handle_recordfile, NULL, NULL, 0 }, |
| 3929 |
{ { "say", "alpha", NULL }, handle_sayalpha, NULL, NULL, 0}, |
| 3930 |
{ { "say", "digits", NULL }, handle_saydigits, NULL, NULL, 0 }, |
| 3931 |
{ { "say", "number", NULL }, handle_saynumber, NULL, NULL, 0 }, |
| 3932 |
{ { "say", "phonetic", NULL }, handle_sayphonetic, NULL, NULL, 0}, |
| 3933 |
{ { "say", "date", NULL }, handle_saydate, NULL, NULL, 0}, |
| 3934 |
{ { "say", "time", NULL }, handle_saytime, NULL, NULL, 0}, |
| 3935 |
{ { "say", "datetime", NULL }, handle_saydatetime, NULL, NULL, 0}, |
| 3936 |
{ { "send", "image", NULL }, handle_sendimage, NULL, NULL, 0}, |
| 3937 |
{ { "send", "text", NULL }, handle_sendtext, NULL, NULL, 0}, |
| 3938 |
{ { "set", "autohangup", NULL }, handle_autohangup, NULL, NULL, 0}, |
| 3939 |
{ { "set", "callerid", NULL }, handle_setcallerid, NULL, NULL, 0}, |
| 3940 |
{ { "set", "context", NULL }, handle_setcontext, NULL, NULL, 0}, |
| 3941 |
{ { "set", "extension", NULL }, handle_setextension, NULL, NULL, 0}, |
| 3942 |
{ { "set", "music", NULL }, handle_setmusic, NULL, NULL, 0 }, |
| 3943 |
{ { "set", "priority", NULL }, handle_setpriority, NULL, NULL, 0 }, |
| 3944 |
{ { "set", "variable", NULL }, handle_setvariable, NULL, NULL, 1 }, |
| 3945 |
{ { "stream", "file", NULL }, handle_streamfile, NULL, NULL, 0 }, |
| 3946 |
{ { "control", "stream", "file", NULL }, handle_controlstreamfile, NULL, NULL, 0 }, |
| 3947 |
{ { "tdd", "mode", NULL }, handle_tddmode, NULL, NULL, 0 }, |
| 3948 |
{ { "verbose", NULL }, handle_verbose, NULL, NULL, 1 }, |
| 3949 |
{ { "wait", "for", "digit", NULL }, handle_waitfordigit, NULL, NULL, 0 }, |
| 3950 |
{ { "speech", "create", NULL }, handle_speechcreate, NULL, NULL, 0 }, |
| 3951 |
{ { "speech", "set", NULL }, handle_speechset, NULL, NULL, 0 }, |
| 3952 |
{ { "speech", "destroy", NULL }, handle_speechdestroy, NULL, NULL, 1 }, |
| 3953 |
{ { "speech", "load", "grammar", NULL }, handle_speechloadgrammar, NULL, NULL, 0 }, |
| 3954 |
{ { "speech", "unload", "grammar", NULL }, handle_speechunloadgrammar, NULL, NULL, 1 }, |
| 3955 |
{ { "speech", "activate", "grammar", NULL }, handle_speechactivategrammar, NULL, NULL, 0 }, |
| 3956 |
{ { "speech", "deactivate", "grammar", NULL }, handle_speechdeactivategrammar, NULL, NULL, 0 }, |
| 3957 |
{ { "speech", "recognize", NULL }, handle_speechrecognize, NULL, NULL, 0 }, |
| 3958 |
}; |
| 3959 |
|
| 3960 |
static AST_RWLIST_HEAD_STATIC(agi_commands, agi_command); |
| 3961 |
|
| 3962 |
static char *help_workhorse(int fd, const char * const match[]) |
| 3963 |
{ |
| 3964 |
char fullcmd[MAX_CMD_LEN], matchstr[MAX_CMD_LEN]; |
| 3965 |
struct agi_command *e; |
| 3966 |
|
| 3967 |
if (match) |
| 3968 |
ast_join(matchstr, sizeof(matchstr), match); |
| 3969 |
|
| 3970 |
ast_cli(fd, "%5.5s %30.30s %s\n","Dead","Command","Description"); |
| 3971 |
AST_RWLIST_RDLOCK(&agi_commands); |
| 3972 |
AST_RWLIST_TRAVERSE(&agi_commands, e, list) { |
| 3973 |
if (!e->cmda[0]) |
| 3974 |
break; |
| 3975 |
|
| 3976 |
if ((e->cmda[0])[0] == '_') |
| 3977 |
continue; |
| 3978 |
ast_join(fullcmd, sizeof(fullcmd), e->cmda); |
| 3979 |
if (match && strncasecmp(matchstr, fullcmd, strlen(matchstr))) |
| 3980 |
continue; |
| 3981 |
ast_cli(fd, "%5.5s %30.30s %s\n", e->dead ? "Yes" : "No" , fullcmd, S_OR(e->summary, "Not available")); |
| 3982 |
} |
| 3983 |
AST_RWLIST_UNLOCK(&agi_commands); |
| 3984 |
|
| 3985 |
return CLI_SUCCESS; |
| 3986 |
} |
| 3987 |
|
| 3988 |
int AST_OPTIONAL_API_NAME(ast_agi_register)(struct ast_module *mod, agi_command *cmd) |
| 3989 |
{ |
| 3990 |
char fullcmd[MAX_CMD_LEN]; |
| 3991 |
|
| 3992 |
ast_join(fullcmd, sizeof(fullcmd), cmd->cmda); |
| 3993 |
|
| 3994 |
if (!find_command(cmd->cmda, 1)) { |
| 3995 |
*((enum ast_doc_src *) &cmd->docsrc) = AST_STATIC_DOC; |
| 3996 |
if (ast_strlen_zero(cmd->summary) && ast_strlen_zero(cmd->usage)) { |
| 3997 |
#ifdef AST_XML_DOCS |
| 3998 |
*((char **) &cmd->summary) = ast_xmldoc_build_synopsis("agi", fullcmd, NULL); |
| 3999 |
*((char **) &cmd->since) = ast_xmldoc_build_since("agi", fullcmd, NULL); |
| 4000 |
*((char **) &cmd->usage) = ast_xmldoc_build_description("agi", fullcmd, NULL); |
| 4001 |
*((char **) &cmd->syntax) = ast_xmldoc_build_syntax("agi", fullcmd, NULL); |
| 4002 |
*((char **) &cmd->arguments) = ast_xmldoc_build_arguments("agi", fullcmd, NULL); |
| 4003 |
*((char **) &cmd->seealso) = ast_xmldoc_build_seealso("agi", fullcmd, NULL); |
| 4004 |
*((enum ast_doc_src *) &cmd->docsrc) = AST_XML_DOC; |
| 4005 |
#endif |
| 4006 |
#ifndef HAVE_NULLSAFE_PRINTF |
| 4007 |
if (!cmd->summary) { |
| 4008 |
*((char **) &cmd->summary) = ast_strdup(""); |
| 4009 |
} |
| 4010 |
if (!cmd->usage) { |
| 4011 |
*((char **) &cmd->usage) = ast_strdup(""); |
| 4012 |
} |
| 4013 |
if (!cmd->syntax) { |
| 4014 |
*((char **) &cmd->syntax) = ast_strdup(""); |
| 4015 |
} |
| 4016 |
if (!cmd->seealso) { |
| 4017 |
*((char **) &cmd->seealso) = ast_strdup(""); |
| 4018 |
} |
| 4019 |
#endif |
| 4020 |
} |
| 4021 |
|
| 4022 |
cmd->mod = mod; |
| 4023 |
AST_RWLIST_WRLOCK(&agi_commands); |
| 4024 |
AST_LIST_INSERT_TAIL(&agi_commands, cmd, list); |
| 4025 |
AST_RWLIST_UNLOCK(&agi_commands); |
| 4026 |
ast_verb(5, "AGI Command '%s' registered\n",fullcmd); |
| 4027 |
return 1; |
| 4028 |
} else { |
| 4029 |
ast_log(LOG_WARNING, "Command already registered!\n"); |
| 4030 |
return 0; |
| 4031 |
} |
| 4032 |
} |
| 4033 |
|
| 4034 |
int AST_OPTIONAL_API_NAME(ast_agi_unregister)(agi_command *cmd) |
| 4035 |
{ |
| 4036 |
struct agi_command *e; |
| 4037 |
int unregistered = 0; |
| 4038 |
char fullcmd[MAX_CMD_LEN]; |
| 4039 |
|
| 4040 |
ast_join(fullcmd, sizeof(fullcmd), cmd->cmda); |
| 4041 |
|
| 4042 |
AST_RWLIST_WRLOCK(&agi_commands); |
| 4043 |
AST_RWLIST_TRAVERSE_SAFE_BEGIN(&agi_commands, e, list) { |
| 4044 |
if (cmd == e) { |
| 4045 |
AST_RWLIST_REMOVE_CURRENT(list); |
| 4046 |
#ifdef AST_XML_DOCS |
| 4047 |
if (e->docsrc == AST_XML_DOC) { |
| 4048 |
ast_free((char *) e->summary); |
| 4049 |
ast_free((char *) e->since); |
| 4050 |
ast_free((char *) e->usage); |
| 4051 |
ast_free((char *) e->syntax); |
| 4052 |
ast_free((char *) e->arguments); |
| 4053 |
ast_free((char *) e->seealso); |
| 4054 |
*((char **) &e->summary) = NULL; |
| 4055 |
*((char **) &e->since) = NULL; |
| 4056 |
*((char **) &e->usage) = NULL; |
| 4057 |
*((char **) &e->syntax) = NULL; |
| 4058 |
*((char **) &e->arguments) = NULL; |
| 4059 |
*((char **) &e->seealso) = NULL; |
| 4060 |
} |
| 4061 |
#endif |
| 4062 |
unregistered=1; |
| 4063 |
break; |
| 4064 |
} |
| 4065 |
} |
| 4066 |
AST_RWLIST_TRAVERSE_SAFE_END; |
| 4067 |
AST_RWLIST_UNLOCK(&agi_commands); |
| 4068 |
if (unregistered) { |
| 4069 |
ast_verb(5, "AGI Command '%s' unregistered\n",fullcmd); |
| 4070 |
} |
| 4071 |
return unregistered; |
| 4072 |
} |
| 4073 |
|
| 4074 |
int AST_OPTIONAL_API_NAME(ast_agi_register_multiple)(struct ast_module *mod, struct agi_command *cmd, unsigned int len) |
| 4075 |
{ |
| 4076 |
unsigned int i, x = 0; |
| 4077 |
|
| 4078 |
for (i = 0; i < len; i++) { |
| 4079 |
if (ast_agi_register(mod, cmd + i) == 1) { |
| 4080 |
x++; |
| 4081 |
continue; |
| 4082 |
} |
| 4083 |
|
| 4084 |
|
| 4085 |
|
| 4086 |
|
| 4087 |
for (; x > 0; x--) { |
| 4088 |
|
| 4089 |
|
| 4090 |
|
| 4091 |
|
| 4092 |
|
| 4093 |
|
| 4094 |
|
| 4095 |
|
| 4096 |
(void) ast_agi_unregister(cmd + x - 1); |
| 4097 |
} |
| 4098 |
return -1; |
| 4099 |
} |
| 4100 |
|
| 4101 |
return 0; |
| 4102 |
} |
| 4103 |
|
| 4104 |
int AST_OPTIONAL_API_NAME(ast_agi_unregister_multiple)(struct agi_command *cmd, unsigned int len) |
| 4105 |
{ |
| 4106 |
unsigned int i; |
| 4107 |
int res = 0; |
| 4108 |
|
| 4109 |
for (i = 0; i < len; i++) { |
| 4110 |
|
| 4111 |
|
| 4112 |
|
| 4113 |
|
| 4114 |
res |= ast_agi_unregister(cmd + i); |
| 4115 |
} |
| 4116 |
|
| 4117 |
return res; |
| 4118 |
} |
| 4119 |
|
| 4120 |
static agi_command *find_command(const char * const cmds[], int exact) |
| 4121 |
{ |
| 4122 |
int y, match; |
| 4123 |
struct agi_command *e; |
| 4124 |
|
| 4125 |
AST_RWLIST_RDLOCK(&agi_commands); |
| 4126 |
AST_RWLIST_TRAVERSE(&agi_commands, e, list) { |
| 4127 |
if (!e->cmda[0]) |
| 4128 |
break; |
| 4129 |
|
| 4130 |
match = 1; |
| 4131 |
for (y = 0; match && cmds[y]; y++) { |
| 4132 |
|
| 4133 |
|
| 4134 |
|
| 4135 |
if (!e->cmda[y] && !exact) |
| 4136 |
break; |
| 4137 |
|
| 4138 |
if (!e->cmda[y]) { |
| 4139 |
AST_RWLIST_UNLOCK(&agi_commands); |
| 4140 |
return NULL; |
| 4141 |
} |
| 4142 |
if (strcasecmp(e->cmda[y], cmds[y])) |
| 4143 |
match = 0; |
| 4144 |
} |
| 4145 |
|
| 4146 |
|
| 4147 |
if ((exact > -1) && e->cmda[y]) |
| 4148 |
match = 0; |
| 4149 |
if (match) { |
| 4150 |
AST_RWLIST_UNLOCK(&agi_commands); |
| 4151 |
return e; |
| 4152 |
} |
| 4153 |
} |
| 4154 |
AST_RWLIST_UNLOCK(&agi_commands); |
| 4155 |
return NULL; |
| 4156 |
} |
| 4157 |
|
| 4158 |
static int parse_args(char *s, int *max, const char *argv[]) |
| 4159 |
{ |
| 4160 |
int x = 0, quoted = 0, escaped = 0, whitespace = 1; |
| 4161 |
char *cur; |
| 4162 |
|
| 4163 |
cur = s; |
| 4164 |
while(*s) { |
| 4165 |
switch(*s) { |
| 4166 |
case '"': |
| 4167 |
|
| 4168 |
if (escaped) |
| 4169 |
goto normal; |
| 4170 |
else |
| 4171 |
quoted = !quoted; |
| 4172 |
if (quoted && whitespace) { |
| 4173 |
|
| 4174 |
argv[x++] = cur; |
| 4175 |
whitespace=0; |
| 4176 |
} |
| 4177 |
escaped = 0; |
| 4178 |
break; |
| 4179 |
case ' ': |
| 4180 |
case '\t': |
| 4181 |
if (!quoted && !escaped) { |
| 4182 |
|
| 4183 |
|
| 4184 |
whitespace = 1; |
| 4185 |
*(cur++) = '\0'; |
| 4186 |
} else |
| 4187 |
|
| 4188 |
goto normal; |
| 4189 |
break; |
| 4190 |
case '\\': |
| 4191 |
|
| 4192 |
if (escaped) { |
| 4193 |
goto normal; |
| 4194 |
} else { |
| 4195 |
escaped=1; |
| 4196 |
} |
| 4197 |
break; |
| 4198 |
default: |
| 4199 |
normal: |
| 4200 |
if (whitespace) { |
| 4201 |
if (x >= MAX_ARGS -1) { |
| 4202 |
ast_log(LOG_WARNING, "Too many arguments, truncating\n"); |
| 4203 |
break; |
| 4204 |
} |
| 4205 |
|
| 4206 |
argv[x++] = cur; |
| 4207 |
whitespace=0; |
| 4208 |
} |
| 4209 |
*(cur++) = *s; |
| 4210 |
escaped=0; |
| 4211 |
} |
| 4212 |
s++; |
| 4213 |
} |
| 4214 |
|
| 4215 |
*(cur++) = '\0'; |
| 4216 |
argv[x] = NULL; |
| 4217 |
*max = x; |
| 4218 |
return 0; |
| 4219 |
} |
| 4220 |
|
| 4221 |
static void publish_async_exec_end(struct ast_channel *chan, int command_id, const char *command, int result_code, const char *result) |
| 4222 |
{ |
| 4223 |
RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref); |
| 4224 |
blob = ast_json_pack("{s: i, s: s, s: i, s: s}", |
| 4225 |
"CommandId", command_id, |
| 4226 |
"Command", command, |
| 4227 |
"ResultCode", result_code, |
| 4228 |
"Result", result); |
| 4229 |
ast_channel_publish_cached_blob(chan, agi_exec_end_type(), blob); |
| 4230 |
} |
| 4231 |
|
| 4232 |
static enum agi_result agi_handle_command(struct ast_channel *chan, AGI *agi, char *buf, int dead) |
| 4233 |
{ |
| 4234 |
const char *argv[MAX_ARGS] = {0}; |
| 4235 |
int argc = MAX_ARGS; |
| 4236 |
int res; |
| 4237 |
agi_command *c; |
| 4238 |
char *ami_cmd = ast_strdupa(buf); |
| 4239 |
const char *ami_res; |
| 4240 |
int command_id = ast_random(); |
| 4241 |
int resultcode = 0; |
| 4242 |
RAII_VAR(struct ast_json *, startblob, NULL, ast_json_unref); |
| 4243 |
|
| 4244 |
startblob = ast_json_pack("{s: i, s: s}", |
| 4245 |
"CommandId", command_id, |
| 4246 |
"Command", ami_cmd); |
| 4247 |
ast_channel_publish_cached_blob(chan, agi_exec_start_type(), startblob); |
| 4248 |
|
| 4249 |
parse_args(buf, &argc, argv); |
| 4250 |
c = find_command(argv, 0); |
| 4251 |
if (!c || !ast_module_running_ref(c->mod)) { |
| 4252 |
ami_res = "Invalid or unknown command"; |
| 4253 |
resultcode = 510; |
| 4254 |
|
| 4255 |
ast_agi_send(agi->fd, chan, "%d %s\n", resultcode, ami_res); |
| 4256 |
|
| 4257 |
publish_async_exec_end(chan, command_id, ami_cmd, resultcode, ami_res); |
| 4258 |
|
| 4259 |
return AGI_RESULT_SUCCESS; |
| 4260 |
} |
| 4261 |
|
| 4262 |
if (!dead || (dead && c->dead)) { |
| 4263 |
res = c->handler(chan, agi, argc, argv); |
| 4264 |
switch (res) { |
| 4265 |
case RESULT_SHOWUSAGE: |
| 4266 |
ami_res = "Usage"; |
| 4267 |
resultcode = 520; |
| 4268 |
|
| 4269 |
publish_async_exec_end(chan, command_id, ami_cmd, resultcode, ami_res); |
| 4270 |
|
| 4271 |
if (ast_strlen_zero(c->usage)) { |
| 4272 |
ast_agi_send(agi->fd, chan, "520 Invalid command syntax. Proper usage not available.\n"); |
| 4273 |
} else { |
| 4274 |
ast_agi_send(agi->fd, chan, "520-Invalid command syntax. Proper usage follows:\n"); |
| 4275 |
ast_agi_send(agi->fd, chan, "%s\n", c->usage); |
| 4276 |
ast_agi_send(agi->fd, chan, "520 End of proper usage.\n"); |
| 4277 |
} |
| 4278 |
|
| 4279 |
break; |
| 4280 |
case RESULT_FAILURE: |
| 4281 |
ami_res = "Failure"; |
| 4282 |
resultcode = -1; |
| 4283 |
|
| 4284 |
publish_async_exec_end(chan, command_id, ami_cmd, resultcode, ami_res); |
| 4285 |
|
| 4286 |
|
| 4287 |
return AGI_RESULT_FAILURE; |
| 4288 |
case ASYNC_AGI_BREAK: |
| 4289 |
ami_res = "Success"; |
| 4290 |
resultcode = 200; |
| 4291 |
|
| 4292 |
publish_async_exec_end(chan, command_id, ami_cmd, resultcode, ami_res); |
| 4293 |
|
| 4294 |
return AGI_RESULT_SUCCESS_ASYNC; |
| 4295 |
case RESULT_SUCCESS: |
| 4296 |
ami_res = "Success"; |
| 4297 |
resultcode = 200; |
| 4298 |
|
| 4299 |
publish_async_exec_end(chan, command_id, ami_cmd, resultcode, ami_res); |
| 4300 |
|
| 4301 |
break; |
| 4302 |
default: |
| 4303 |
ami_res = "Unknown Result"; |
| 4304 |
resultcode = 200; |
| 4305 |
|
| 4306 |
publish_async_exec_end(chan, command_id, ami_cmd, resultcode, ami_res); |
| 4307 |
|
| 4308 |
break; |
| 4309 |
} |
| 4310 |
} else { |
| 4311 |
ami_res = "Command Not Permitted on a dead channel or intercept routine"; |
| 4312 |
resultcode = 511; |
| 4313 |
|
| 4314 |
ast_agi_send(agi->fd, chan, "%d %s\n", resultcode, ami_res); |
| 4315 |
|
| 4316 |
publish_async_exec_end(chan, command_id, ami_cmd, resultcode, ami_res); |
| 4317 |
} |
| 4318 |
ast_module_unref(c->mod); |
| 4319 |
|
| 4320 |
return AGI_RESULT_SUCCESS; |
| 4321 |
} |
| 4322 |
|
| 4323 |
static enum agi_result run_agi(struct ast_channel *chan, char *request, AGI *agi, int pid, int *status, int dead, int argc, char *argv[]) |
| 4324 |
{ |
| 4325 |
struct ast_channel *c; |
| 4326 |
int outfd; |
| 4327 |
int ms; |
| 4328 |
int needhup = 0; |
| 4329 |
enum agi_result returnstatus = AGI_RESULT_SUCCESS; |
| 4330 |
struct ast_frame *f; |
| 4331 |
char buf[AGI_BUF_LEN]; |
| 4332 |
char *res = NULL; |
| 4333 |
FILE *readf; |
| 4334 |
|
| 4335 |
|
| 4336 |
int retry = AGI_NANDFS_RETRY; |
| 4337 |
int send_sighup; |
| 4338 |
const char *sighup_str; |
| 4339 |
const char *exit_on_hangup_str; |
| 4340 |
int exit_on_hangup; |
| 4341 |
|
| 4342 |
int in_intercept = ast_channel_get_intercept_mode(); |
| 4343 |
|
| 4344 |
ast_channel_lock(chan); |
| 4345 |
sighup_str = pbx_builtin_getvar_helper(chan, "AGISIGHUP"); |
| 4346 |
send_sighup = !ast_false(sighup_str); |
| 4347 |
exit_on_hangup_str = pbx_builtin_getvar_helper(chan, "AGIEXITONHANGUP"); |
| 4348 |
exit_on_hangup = ast_true(exit_on_hangup_str); |
| 4349 |
ast_channel_unlock(chan); |
| 4350 |
|
| 4351 |
if (!(readf = fdopen(agi->ctrl, "r"))) { |
| 4352 |
ast_log(LOG_WARNING, "Unable to fdopen file descriptor\n"); |
| 4353 |
if (send_sighup && pid > -1) |
| 4354 |
kill(pid, SIGHUP); |
| 4355 |
close(agi->ctrl); |
| 4356 |
return AGI_RESULT_FAILURE; |
| 4357 |
} |
| 4358 |
|
| 4359 |
setlinebuf(readf); |
| 4360 |
setup_env(chan, request, agi->fd, (agi->audio > -1), argc, argv); |
| 4361 |
for (;;) { |
| 4362 |
if (needhup) { |
| 4363 |
needhup = 0; |
| 4364 |
dead = 1; |
| 4365 |
if (send_sighup) { |
| 4366 |
if (pid > -1) { |
| 4367 |
kill(pid, SIGHUP); |
| 4368 |
} else if (agi->fast) { |
| 4369 |
ast_agi_send(agi->fd, chan, "HANGUP\n"); |
| 4370 |
} |
| 4371 |
} |
| 4372 |
if (exit_on_hangup) { |
| 4373 |
break; |
| 4374 |
} |
| 4375 |
} |
| 4376 |
ms = -1; |
| 4377 |
if (dead || in_intercept) { |
| 4378 |
c = ast_waitfor_nandfds(&chan, 0, &agi->ctrl, 1, NULL, &outfd, &ms); |
| 4379 |
} else if (!ast_check_hangup(chan)) { |
| 4380 |
c = ast_waitfor_nandfds(&chan, 1, &agi->ctrl, 1, NULL, &outfd, &ms); |
| 4381 |
} else { |
| 4382 |
|
| 4383 |
|
| 4384 |
|
| 4385 |
|
| 4386 |
c = chan; |
| 4387 |
} |
| 4388 |
if (c) { |
| 4389 |
retry = AGI_NANDFS_RETRY; |
| 4390 |
|
| 4391 |
f = ast_read(c); |
| 4392 |
if (!f) { |
| 4393 |
ast_debug(1, "%s hungup\n", ast_channel_name(chan)); |
| 4394 |
needhup = 1; |
| 4395 |
if (!returnstatus) { |
| 4396 |
returnstatus = AGI_RESULT_HANGUP; |
| 4397 |
} |
| 4398 |
} else { |
| 4399 |
|
| 4400 |
if ((agi->audio > -1) && (f->frametype == AST_FRAME_VOICE)) { |
| 4401 |
|
| 4402 |
if (write(agi->audio, f->data.ptr, f->datalen) < 0) { |
| 4403 |
} |
| 4404 |
} |
| 4405 |
ast_frfree(f); |
| 4406 |
} |
| 4407 |
} else if (outfd > -1) { |
| 4408 |
size_t len = sizeof(buf); |
| 4409 |
size_t buflen = 0; |
| 4410 |
enum agi_result cmd_status; |
| 4411 |
|
| 4412 |
retry = AGI_NANDFS_RETRY; |
| 4413 |
buf[0] = '\0'; |
| 4414 |
|
| 4415 |
while (len > 1) { |
| 4416 |
res = fgets(buf + buflen, len, readf); |
| 4417 |
if (feof(readf)) |
| 4418 |
break; |
| 4419 |
if (ferror(readf) && ((errno != EINTR) && (errno != EAGAIN))) |
| 4420 |
break; |
| 4421 |
if (res != NULL && !agi->fast) |
| 4422 |
break; |
| 4423 |
buflen = strlen(buf); |
| 4424 |
if (buflen && buf[buflen - 1] == '\n') |
| 4425 |
break; |
| 4426 |
len = sizeof(buf) - buflen; |
| 4427 |
if (agidebug) |
| 4428 |
ast_verbose("AGI Rx << temp buffer %s - errno %s\nNo \\n received, checking again.\n", buf, strerror(errno)); |
| 4429 |
} |
| 4430 |
|
| 4431 |
if (!buf[0]) { |
| 4432 |
|
| 4433 |
ast_verb(3, "<%s>AGI Script %s completed, returning %d\n", ast_channel_name(chan), request, returnstatus); |
| 4434 |
if (pid > 0) |
| 4435 |
waitpid(pid, status, 0); |
| 4436 |
|
| 4437 |
pid = -1; |
| 4438 |
break; |
| 4439 |
} |
| 4440 |
|
| 4441 |
|
| 4442 |
if (*buf && strncasecmp(buf, "failure", 7) == 0) { |
| 4443 |
returnstatus = AGI_RESULT_FAILURE; |
| 4444 |
break; |
| 4445 |
} |
| 4446 |
|
| 4447 |
|
| 4448 |
buflen = strlen(buf); |
| 4449 |
if (buflen && buf[buflen - 1] == '\n') { |
| 4450 |
buf[buflen - 1] = '\0'; |
| 4451 |
} |
| 4452 |
|
| 4453 |
if (agidebug) |
| 4454 |
ast_verbose("<%s>AGI Rx << %s\n", ast_channel_name(chan), buf); |
| 4455 |
cmd_status = agi_handle_command(chan, agi, buf, dead || in_intercept); |
| 4456 |
switch (cmd_status) { |
| 4457 |
case AGI_RESULT_FAILURE: |
| 4458 |
if (dead || in_intercept || !ast_check_hangup(chan)) { |
| 4459 |
|
| 4460 |
returnstatus = AGI_RESULT_FAILURE; |
| 4461 |
} |
| 4462 |
break; |
| 4463 |
default: |
| 4464 |
break; |
| 4465 |
} |
| 4466 |
} else { |
| 4467 |
if (--retry <= 0) { |
| 4468 |
ast_log(LOG_WARNING, "No channel, no fd?\n"); |
| 4469 |
returnstatus = AGI_RESULT_FAILURE; |
| 4470 |
break; |
| 4471 |
} |
| 4472 |
} |
| 4473 |
} |
| 4474 |
|
| 4475 |
if (agi->speech) { |
| 4476 |
ast_speech_destroy(agi->speech); |
| 4477 |
} |
| 4478 |
|
| 4479 |
if (send_sighup) { |
| 4480 |
if (pid > -1) { |
| 4481 |
if (kill(pid, SIGHUP)) { |
| 4482 |
ast_log(LOG_WARNING, "unable to send SIGHUP to AGI process %d: %s\n", pid, strerror(errno)); |
| 4483 |
} else { |
| 4484 |
usleep(1); |
| 4485 |
} |
| 4486 |
waitpid(pid, status, WNOHANG); |
| 4487 |
} else if (agi->fast) { |
| 4488 |
ast_agi_send(agi->fd, chan, "HANGUP\n"); |
| 4489 |
} |
| 4490 |
} |
| 4491 |
fclose(readf); |
| 4492 |
return returnstatus; |
| 4493 |
} |
| 4494 |
|
| 4495 |
static char *handle_cli_agi_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 4496 |
{ |
| 4497 |
struct agi_command *command; |
| 4498 |
char fullcmd[MAX_CMD_LEN]; |
| 4499 |
int error = 0; |
| 4500 |
|
| 4501 |
switch (cmd) { |
| 4502 |
case CLI_INIT: |
| 4503 |
e->command = "agi show commands [topic]"; |
| 4504 |
e->usage = |
| 4505 |
"Usage: agi show commands [topic] <topic>\n" |
| 4506 |
" When called with a topic as an argument, displays usage\n" |
| 4507 |
" information on the given command. If called without a\n" |
| 4508 |
" topic, it provides a list of AGI commands.\n"; |
| 4509 |
case CLI_GENERATE: |
| 4510 |
return NULL; |
| 4511 |
} |
| 4512 |
if (a->argc < e->args - 1 || (a->argc >= e->args && strcasecmp(a->argv[e->args - 1], "topic"))) |
| 4513 |
return CLI_SHOWUSAGE; |
| 4514 |
if (a->argc > e->args - 1) { |
| 4515 |
command = find_command(a->argv + e->args, 1); |
| 4516 |
if (command) { |
| 4517 |
char *synopsis = NULL, *since = NULL, *description = NULL, *syntax = NULL, *arguments = NULL, *seealso = NULL; |
| 4518 |
|
| 4519 |
ast_join(fullcmd, sizeof(fullcmd), a->argv + e->args); |
| 4520 |
|
| 4521 |
#ifdef AST_XML_DOCS |
| 4522 |
if (command->docsrc == AST_XML_DOC) { |
| 4523 |
synopsis = ast_xmldoc_printable(S_OR(command->summary, "Not available"), 1); |
| 4524 |
since = ast_xmldoc_printable(S_OR(command->since, "Not available"), 1); |
| 4525 |
description = ast_xmldoc_printable(S_OR(command->usage, "Not available"), 1); |
| 4526 |
syntax = ast_xmldoc_printable(S_OR(command->syntax, "Not available"), 1); |
| 4527 |
arguments = ast_xmldoc_printable(S_OR(command->arguments, "Not available"), 1); |
| 4528 |
seealso = ast_xmldoc_printable(S_OR(command->seealso, "Not available"), 1); |
| 4529 |
} else |
| 4530 |
#endif |
| 4531 |
{ |
| 4532 |
synopsis = ast_strdup(S_OR(command->summary, "Not Available")); |
| 4533 |
since = ast_strdup(S_OR(command->since, "Not Available")); |
| 4534 |
description = ast_strdup(S_OR(command->usage, "Not Available")); |
| 4535 |
syntax = ast_strdup(S_OR(command->syntax, "Not Available")); |
| 4536 |
arguments = ast_strdup(S_OR(command->arguments, "Not Available")); |
| 4537 |
seealso = ast_strdup(S_OR(command->seealso, "Not Available")); |
| 4538 |
} |
| 4539 |
|
| 4540 |
if (!synopsis || !since || !description || !syntax || !arguments || !seealso) { |
| 4541 |
error = 1; |
| 4542 |
goto return_cleanup; |
| 4543 |
} |
| 4544 |
|
| 4545 |
ast_cli(a->fd, "\n" |
| 4546 |
"%s -= Info about AGI '%s' =- %s\n\n" |
| 4547 |
COLORIZE_FMT "\n" |
| 4548 |
"%s\n\n" |
| 4549 |
COLORIZE_FMT "\n" |
| 4550 |
"%s\n\n" |
| 4551 |
COLORIZE_FMT "\n" |
| 4552 |
"%s\n\n" |
| 4553 |
COLORIZE_FMT "\n" |
| 4554 |
"%s\n\n" |
| 4555 |
COLORIZE_FMT "\n" |
| 4556 |
"%s\n\n" |
| 4557 |
COLORIZE_FMT "\n" |
| 4558 |
"%s\n\n" |
| 4559 |
COLORIZE_FMT "\n" |
| 4560 |
"%s\n\n", |
| 4561 |
ast_term_color(COLOR_MAGENTA, 0), fullcmd, ast_term_reset(), |
| 4562 |
COLORIZE(COLOR_MAGENTA, 0, "[Synopsis]"), synopsis, |
| 4563 |
COLORIZE(COLOR_MAGENTA, 0, "[Since]"), since, |
| 4564 |
COLORIZE(COLOR_MAGENTA, 0, "[Description]"), description, |
| 4565 |
COLORIZE(COLOR_MAGENTA, 0, "[Syntax]"), syntax, |
| 4566 |
COLORIZE(COLOR_MAGENTA, 0, "[Arguments]"), arguments, |
| 4567 |
COLORIZE(COLOR_MAGENTA, 0, "[Runs Dead]"), command->dead ? "Yes" : "No", |
| 4568 |
COLORIZE(COLOR_MAGENTA, 0, "[See Also]"), seealso |
| 4569 |
); |
| 4570 |
|
| 4571 |
return_cleanup: |
| 4572 |
ast_free(synopsis); |
| 4573 |
ast_free(since); |
| 4574 |
ast_free(description); |
| 4575 |
ast_free(syntax); |
| 4576 |
ast_free(arguments); |
| 4577 |
ast_free(seealso); |
| 4578 |
} else { |
| 4579 |
if (find_command(a->argv + e->args, -1)) { |
| 4580 |
return help_workhorse(a->fd, a->argv + e->args); |
| 4581 |
} else { |
| 4582 |
ast_join(fullcmd, sizeof(fullcmd), a->argv + e->args); |
| 4583 |
ast_cli(a->fd, "No such command '%s'.\n", fullcmd); |
| 4584 |
} |
| 4585 |
} |
| 4586 |
} else { |
| 4587 |
return help_workhorse(a->fd, NULL); |
| 4588 |
} |
| 4589 |
return (error ? CLI_FAILURE : CLI_SUCCESS); |
| 4590 |
} |
| 4591 |
|
| 4592 |
|
| 4593 |
|
| 4594 |
|
| 4595 |
static void write_html_escaped(FILE *htmlfile, char *str) |
| 4596 |
{ |
| 4597 |
char *cur = str; |
| 4598 |
|
| 4599 |
while(*cur) { |
| 4600 |
switch (*cur) { |
| 4601 |
case '<': |
| 4602 |
fprintf(htmlfile, "%s", "<"); |
| 4603 |
break; |
| 4604 |
case '>': |
| 4605 |
fprintf(htmlfile, "%s", ">"); |
| 4606 |
break; |
| 4607 |
case '&': |
| 4608 |
fprintf(htmlfile, "%s", "&"); |
| 4609 |
break; |
| 4610 |
case '"': |
| 4611 |
fprintf(htmlfile, "%s", """); |
| 4612 |
break; |
| 4613 |
default: |
| 4614 |
fprintf(htmlfile, "%c", *cur); |
| 4615 |
break; |
| 4616 |
} |
| 4617 |
cur++; |
| 4618 |
} |
| 4619 |
|
| 4620 |
return; |
| 4621 |
} |
| 4622 |
|
| 4623 |
static int write_htmldump(const char *filename) |
| 4624 |
{ |
| 4625 |
struct agi_command *command; |
| 4626 |
char fullcmd[MAX_CMD_LEN]; |
| 4627 |
FILE *htmlfile; |
| 4628 |
|
| 4629 |
if (!(htmlfile = fopen(filename, "wt"))) |
| 4630 |
return -1; |
| 4631 |
|
| 4632 |
fprintf(htmlfile, "<HTML>\n<HEAD>\n<TITLE>AGI Commands</TITLE>\n</HEAD>\n"); |
| 4633 |
fprintf(htmlfile, "<BODY>\n<CENTER><B><H1>AGI Commands</H1></B></CENTER>\n\n"); |
| 4634 |
fprintf(htmlfile, "<TABLE BORDER=\"0\" CELLSPACING=\"10\">\n"); |
| 4635 |
|
| 4636 |
AST_RWLIST_RDLOCK(&agi_commands); |
| 4637 |
AST_RWLIST_TRAVERSE(&agi_commands, command, list) { |
| 4638 |
char *tempstr, *stringp; |
| 4639 |
|
| 4640 |
if (!command->cmda[0]) |
| 4641 |
break; |
| 4642 |
|
| 4643 |
if ((command->cmda[0])[0] == '_') |
| 4644 |
continue; |
| 4645 |
ast_join(fullcmd, sizeof(fullcmd), command->cmda); |
| 4646 |
|
| 4647 |
fprintf(htmlfile, "<TR><TD><TABLE BORDER=\"1\" CELLPADDING=\"5\" WIDTH=\"100%%\">\n"); |
| 4648 |
fprintf(htmlfile, "<TR><TH ALIGN=\"CENTER\"><B>%s - %s</B></TH></TR>\n", fullcmd, command->summary); |
| 4649 |
#ifdef AST_XML_DOCS |
| 4650 |
stringp = ast_xmldoc_printable(command->usage, 0); |
| 4651 |
#else |
| 4652 |
stringp = ast_strdup(command->usage); |
| 4653 |
#endif |
| 4654 |
tempstr = strsep(&stringp, "\n"); |
| 4655 |
|
| 4656 |
fprintf(htmlfile, "<TR><TD ALIGN=\"CENTER\">"); |
| 4657 |
write_html_escaped(htmlfile, tempstr); |
| 4658 |
fprintf(htmlfile, "</TD></TR>\n"); |
| 4659 |
fprintf(htmlfile, "<TR><TD ALIGN=\"CENTER\">\n"); |
| 4660 |
|
| 4661 |
while ((tempstr = strsep(&stringp, "\n")) != NULL) { |
| 4662 |
write_html_escaped(htmlfile, tempstr); |
| 4663 |
fprintf(htmlfile, "<BR>\n"); |
| 4664 |
} |
| 4665 |
fprintf(htmlfile, "</TD></TR>\n"); |
| 4666 |
fprintf(htmlfile, "</TABLE></TD></TR>\n\n"); |
| 4667 |
ast_free(stringp); |
| 4668 |
} |
| 4669 |
AST_RWLIST_UNLOCK(&agi_commands); |
| 4670 |
fprintf(htmlfile, "</TABLE>\n</BODY>\n</HTML>\n"); |
| 4671 |
fclose(htmlfile); |
| 4672 |
return 0; |
| 4673 |
} |
| 4674 |
|
| 4675 |
static char *handle_cli_agi_dump_html(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 4676 |
{ |
| 4677 |
switch (cmd) { |
| 4678 |
case CLI_INIT: |
| 4679 |
e->command = "agi dump html"; |
| 4680 |
e->usage = |
| 4681 |
"Usage: agi dump html <filename>\n" |
| 4682 |
" Dumps the AGI command list in HTML format to the given\n" |
| 4683 |
" file.\n"; |
| 4684 |
return NULL; |
| 4685 |
case CLI_GENERATE: |
| 4686 |
return NULL; |
| 4687 |
} |
| 4688 |
if (a->argc != e->args + 1) |
| 4689 |
return CLI_SHOWUSAGE; |
| 4690 |
|
| 4691 |
if (write_htmldump(a->argv[e->args]) < 0) { |
| 4692 |
ast_cli(a->fd, "Could not create file '%s'\n", a->argv[e->args]); |
| 4693 |
return CLI_SHOWUSAGE; |
| 4694 |
} |
| 4695 |
ast_cli(a->fd, "AGI HTML commands dumped to: %s\n", a->argv[e->args]); |
| 4696 |
return CLI_SUCCESS; |
| 4697 |
} |
| 4698 |
|
| 4699 |
static int agi_exec_full(struct ast_channel *chan, const char *data, int enhanced, int dead) |
| 4700 |
{ |
| 4701 |
enum agi_result res; |
| 4702 |
char *buf; |
| 4703 |
int fds[2], efd = -1, pid = -1; |
| 4704 |
int safe_fork_called = 0; |
| 4705 |
AST_DECLARE_APP_ARGS(args, |
| 4706 |
AST_APP_ARG(arg)[MAX_ARGS]; |
| 4707 |
); |
| 4708 |
AGI agi; |
| 4709 |
|
| 4710 |
if (ast_strlen_zero(data)) { |
| 4711 |
ast_log(LOG_WARNING, "AGI requires an argument (script)\n"); |
| 4712 |
return -1; |
| 4713 |
} |
| 4714 |
if (dead) |
| 4715 |
ast_debug(3, "Hungup channel detected, running agi in dead mode.\n"); |
| 4716 |
memset(&agi, 0, sizeof(agi)); |
| 4717 |
buf = ast_strdupa(data); |
| 4718 |
AST_STANDARD_APP_ARGS(args, buf); |
| 4719 |
args.arg[args.argc] = NULL; |
| 4720 |
#if 0 |
| 4721 |
|
| 4722 |
if (chan->_state != AST_STATE_UP) { |
| 4723 |
if (ast_answer(chan)) |
| 4724 |
return -1; |
| 4725 |
} |
| 4726 |
#endif |
| 4727 |
res = launch_script(chan, args.arg[0], args.argc, args.arg, fds, enhanced ? &efd : NULL, &pid, &safe_fork_called); |
| 4728 |
|
| 4729 |
|
| 4730 |
if (res == AGI_RESULT_SUCCESS || res == AGI_RESULT_SUCCESS_FAST) { |
| 4731 |
int status = 0; |
| 4732 |
agi.fd = fds[1]; |
| 4733 |
agi.ctrl = fds[0]; |
| 4734 |
agi.audio = efd; |
| 4735 |
agi.fast = (res == AGI_RESULT_SUCCESS_FAST) ? 1 : 0; |
| 4736 |
res = run_agi(chan, args.arg[0], &agi, pid, &status, dead, args.argc, args.arg); |
| 4737 |
|
| 4738 |
if ((res == AGI_RESULT_SUCCESS || res == AGI_RESULT_SUCCESS_FAST) && status) |
| 4739 |
res = AGI_RESULT_FAILURE; |
| 4740 |
if (fds[1] != fds[0]) |
| 4741 |
close(fds[1]); |
| 4742 |
if (efd > -1) |
| 4743 |
close(efd); |
| 4744 |
} |
| 4745 |
if (safe_fork_called) { |
| 4746 |
ast_safe_fork_cleanup(); |
| 4747 |
} |
| 4748 |
|
| 4749 |
switch (res) { |
| 4750 |
case AGI_RESULT_SUCCESS: |
| 4751 |
case AGI_RESULT_SUCCESS_FAST: |
| 4752 |
case AGI_RESULT_SUCCESS_ASYNC: |
| 4753 |
pbx_builtin_setvar_helper(chan, "AGISTATUS", "SUCCESS"); |
| 4754 |
break; |
| 4755 |
case AGI_RESULT_FAILURE: |
| 4756 |
pbx_builtin_setvar_helper(chan, "AGISTATUS", "FAILURE"); |
| 4757 |
break; |
| 4758 |
case AGI_RESULT_NOTFOUND: |
| 4759 |
pbx_builtin_setvar_helper(chan, "AGISTATUS", "NOTFOUND"); |
| 4760 |
break; |
| 4761 |
case AGI_RESULT_HANGUP: |
| 4762 |
pbx_builtin_setvar_helper(chan, "AGISTATUS", "HANGUP"); |
| 4763 |
return -1; |
| 4764 |
} |
| 4765 |
|
| 4766 |
return 0; |
| 4767 |
} |
| 4768 |
|
| 4769 |
static int agi_exec(struct ast_channel *chan, const char *data) |
| 4770 |
{ |
| 4771 |
if (!ast_check_hangup(chan)) |
| 4772 |
return agi_exec_full(chan, data, 0, 0); |
| 4773 |
else |
| 4774 |
return agi_exec_full(chan, data, 0, 1); |
| 4775 |
} |
| 4776 |
|
| 4777 |
static int eagi_exec(struct ast_channel *chan, const char *data) |
| 4778 |
{ |
| 4779 |
int res; |
| 4780 |
struct ast_format *readformat; |
| 4781 |
struct ast_format *requested_format = NULL; |
| 4782 |
const char *requested_format_name; |
| 4783 |
|
| 4784 |
if (ast_check_hangup(chan)) { |
| 4785 |
ast_log(LOG_ERROR, "EAGI cannot be run on a dead/hungup channel, please use AGI.\n"); |
| 4786 |
return 0; |
| 4787 |
} |
| 4788 |
|
| 4789 |
requested_format_name = pbx_builtin_getvar_helper(chan, "EAGI_AUDIO_FORMAT"); |
| 4790 |
if (requested_format_name) { |
| 4791 |
requested_format = ast_format_cache_get(requested_format_name); |
| 4792 |
if (requested_format) { |
| 4793 |
ast_verb(3, "<%s> Setting EAGI audio pipe format to %s\n", |
| 4794 |
ast_channel_name(chan), ast_format_get_name(requested_format)); |
| 4795 |
} else { |
| 4796 |
ast_log(LOG_ERROR, "Could not find requested format: %s\n", requested_format_name); |
| 4797 |
} |
| 4798 |
} |
| 4799 |
|
| 4800 |
readformat = ao2_bump(ast_channel_readformat(chan)); |
| 4801 |
if (ast_set_read_format(chan, requested_format ?: ast_format_slin)) { |
| 4802 |
ast_log(LOG_WARNING, "Unable to set channel '%s' to linear mode\n", ast_channel_name(chan)); |
| 4803 |
ao2_cleanup(requested_format); |
| 4804 |
ao2_cleanup(readformat); |
| 4805 |
return -1; |
| 4806 |
} |
| 4807 |
res = agi_exec_full(chan, data, 1, 0); |
| 4808 |
if (!res) { |
| 4809 |
if (ast_set_read_format(chan, readformat)) { |
| 4810 |
ast_log(LOG_WARNING, "Unable to restore channel '%s' to format %s\n", ast_channel_name(chan), |
| 4811 |
ast_format_get_name(readformat)); |
| 4812 |
} |
| 4813 |
} |
| 4814 |
ao2_cleanup(requested_format); |
| 4815 |
ao2_cleanup(readformat); |
| 4816 |
return res; |
| 4817 |
} |
| 4818 |
|
| 4819 |
static int deadagi_exec(struct ast_channel *chan, const char *data) |
| 4820 |
{ |
| 4821 |
ast_log(LOG_WARNING, "DeadAGI has been deprecated, please use AGI in all cases!\n"); |
| 4822 |
return agi_exec(chan, data); |
| 4823 |
} |
| 4824 |
|
| 4825 |
static struct ast_cli_entry cli_agi[] = { |
| 4826 |
AST_CLI_DEFINE(handle_cli_agi_add_cmd, "Add AGI command to a channel in Async AGI"), |
| 4827 |
AST_CLI_DEFINE(handle_cli_agi_debug, "Enable/Disable AGI debugging"), |
| 4828 |
AST_CLI_DEFINE(handle_cli_agi_show, "List AGI commands or specific help"), |
| 4829 |
AST_CLI_DEFINE(handle_cli_agi_dump_html, "Dumps a list of AGI commands in HTML format") |
| 4830 |
}; |
| 4831 |
|
| 4832 |
#ifdef TEST_FRAMEWORK |
| 4833 |
AST_TEST_DEFINE(test_agi_null_docs) |
| 4834 |
{ |
| 4835 |
int res = AST_TEST_PASS; |
| 4836 |
struct agi_command noop_command = |
| 4837 |
{ { "testnoop", NULL }, handle_noop, NULL, NULL, 0 }; |
| 4838 |
|
| 4839 |
switch (cmd) { |
| 4840 |
case TEST_INIT: |
| 4841 |
info->name = "null_agi_docs"; |
| 4842 |
info->category = "/res/agi/"; |
| 4843 |
info->summary = "AGI command with no documentation"; |
| 4844 |
info->description = "Test whether an AGI command with no documentation will crash Asterisk"; |
| 4845 |
return AST_TEST_NOT_RUN; |
| 4846 |
case TEST_EXECUTE: |
| 4847 |
break; |
| 4848 |
} |
| 4849 |
|
| 4850 |
if (ast_agi_register(ast_module_info->self, &noop_command) == 0) { |
| 4851 |
ast_test_status_update(test, "Unable to register testnoop command, because res_agi is not loaded.\n"); |
| 4852 |
return AST_TEST_NOT_RUN; |
| 4853 |
} |
| 4854 |
|
| 4855 |
#ifndef HAVE_NULLSAFE_PRINTF |
| 4856 |
|
| 4857 |
if (noop_command.usage == NULL) { |
| 4858 |
ast_test_status_update(test, "AGI testnoop usage was not updated properly.\n"); |
| 4859 |
res = AST_TEST_FAIL; |
| 4860 |
} |
| 4861 |
if (noop_command.syntax == NULL) { |
| 4862 |
ast_test_status_update(test, "AGI testnoop syntax was not updated properly.\n"); |
| 4863 |
res = AST_TEST_FAIL; |
| 4864 |
} |
| 4865 |
#endif |
| 4866 |
|
| 4867 |
ast_agi_unregister(&noop_command); |
| 4868 |
return res; |
| 4869 |
} |
| 4870 |
#endif |
| 4871 |
|
| 4872 |
static int unload_module(void) |
| 4873 |
{ |
| 4874 |
STASIS_MESSAGE_TYPE_CLEANUP(agi_exec_start_type); |
| 4875 |
STASIS_MESSAGE_TYPE_CLEANUP(agi_exec_end_type); |
| 4876 |
STASIS_MESSAGE_TYPE_CLEANUP(agi_async_start_type); |
| 4877 |
STASIS_MESSAGE_TYPE_CLEANUP(agi_async_exec_type); |
| 4878 |
STASIS_MESSAGE_TYPE_CLEANUP(agi_async_end_type); |
| 4879 |
|
| 4880 |
ast_cli_unregister_multiple(cli_agi, ARRAY_LEN(cli_agi)); |
| 4881 |
ast_agi_unregister_multiple(commands, ARRAY_LEN(commands)); |
| 4882 |
ast_unregister_application(eapp); |
| 4883 |
ast_unregister_application(deadapp); |
| 4884 |
ast_manager_unregister("AGI"); |
| 4885 |
ast_unregister_application(app); |
| 4886 |
AST_TEST_UNREGISTER(test_agi_null_docs); |
| 4887 |
return 0; |
| 4888 |
} |
| 4889 |
|
| 4890 |
static int load_module(void) |
| 4891 |
{ |
| 4892 |
int err = 0; |
| 4893 |
|
| 4894 |
err |= STASIS_MESSAGE_TYPE_INIT(agi_exec_start_type); |
| 4895 |
err |= STASIS_MESSAGE_TYPE_INIT(agi_exec_end_type); |
| 4896 |
err |= STASIS_MESSAGE_TYPE_INIT(agi_async_start_type); |
| 4897 |
err |= STASIS_MESSAGE_TYPE_INIT(agi_async_exec_type); |
| 4898 |
err |= STASIS_MESSAGE_TYPE_INIT(agi_async_end_type); |
| 4899 |
|
| 4900 |
err |= ast_cli_register_multiple(cli_agi, ARRAY_LEN(cli_agi)); |
| 4901 |
err |= ast_agi_register_multiple(ast_module_info->self, commands, ARRAY_LEN(commands)); |
| 4902 |
err |= ast_register_application_xml(deadapp, deadagi_exec); |
| 4903 |
err |= ast_register_application_xml(eapp, eagi_exec); |
| 4904 |
err |= ast_manager_register_xml("AGI", EVENT_FLAG_AGI, action_add_agi_cmd); |
| 4905 |
err |= ast_register_application_xml(app, agi_exec); |
| 4906 |
|
| 4907 |
AST_TEST_REGISTER(test_agi_null_docs); |
| 4908 |
|
| 4909 |
if (err) { |
| 4910 |
unload_module(); |
| 4911 |
return AST_MODULE_LOAD_DECLINE; |
| 4912 |
} |
| 4913 |
|
| 4914 |
return AST_MODULE_LOAD_SUCCESS; |
| 4915 |
} |
| 4916 |
|
| 4917 |
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "Asterisk Gateway Interface (AGI)", |
| 4918 |
.support_level = AST_MODULE_SUPPORT_CORE, |
| 4919 |
.load = load_module, |
| 4920 |
.unload = unload_module, |
| 4921 |
.load_pri = AST_MODPRI_APP_DEPEND, |
| 4922 |
.requires = "res_speech", |
| 4923 |
); |