| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
#include "asterisk.h" |
| 155 |
|
| 156 |
#include <ctype.h> |
| 157 |
#include <sys/time.h> |
| 158 |
#include <sys/stat.h> |
| 159 |
#include <sys/mman.h> |
| 160 |
#include <time.h> |
| 161 |
#include <dirent.h> |
| 162 |
#include <locale.h> |
| 163 |
|
| 164 |
#include "asterisk/paths.h" |
| 165 |
#include "asterisk/lock.h" |
| 166 |
#include "asterisk/file.h" |
| 167 |
#include "asterisk/channel.h" |
| 168 |
#include "asterisk/pbx.h" |
| 169 |
#include "asterisk/config.h" |
| 170 |
#include "asterisk/say.h" |
| 171 |
#include "asterisk/module.h" |
| 172 |
#include "asterisk/app.h" |
| 173 |
#include "asterisk/mwi.h" |
| 174 |
#include "asterisk/dsp.h" |
| 175 |
#include "asterisk/localtime.h" |
| 176 |
#include "asterisk/cli.h" |
| 177 |
#include "asterisk/utils.h" |
| 178 |
#include "asterisk/linkedlists.h" |
| 179 |
#include "asterisk/callerid.h" |
| 180 |
#include "asterisk/stasis.h" |
| 181 |
#include "asterisk/stasis_channels.h" |
| 182 |
#include "asterisk/json.h" |
| 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 |
#ifndef TRUE |
| 551 |
#define TRUE 1 |
| 552 |
#endif |
| 553 |
#ifndef FALSE |
| 554 |
#define FALSE 0 |
| 555 |
#endif |
| 556 |
|
| 557 |
|
| 558 |
#define MVM_REVIEW (1 << 0) |
| 559 |
#define MVM_OPERATOR (1 << 1) |
| 560 |
#define MVM_REALTIME (1 << 2) |
| 561 |
#define MVM_SVMAIL (1 << 3) |
| 562 |
#define MVM_ENVELOPE (1 << 4) |
| 563 |
#define MVM_PBXSKIP (1 << 9) |
| 564 |
#define MVM_ALLOCED (1 << 13) |
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
#define SENDMAIL "/usr/sbin/sendmail -t" |
| 569 |
|
| 570 |
#define SOUND_INTRO "vm-intro" |
| 571 |
#define EOL "\r\n" |
| 572 |
|
| 573 |
#define MAX_DATETIME_FORMAT 512 |
| 574 |
#define MAX_NUM_CID_CONTEXTS 10 |
| 575 |
|
| 576 |
#define ERROR_LOCK_PATH -100 |
| 577 |
#define VOICEMAIL_DIR_MODE 0700 |
| 578 |
|
| 579 |
#define VOICEMAIL_CONFIG "minivm.conf" |
| 580 |
#define ASTERISK_USERNAME "asterisk" |
| 581 |
|
| 582 |
|
| 583 |
enum mvm_messagetype { |
| 584 |
MVM_MESSAGE_EMAIL, |
| 585 |
MVM_MESSAGE_PAGE |
| 586 |
|
| 587 |
}; |
| 588 |
|
| 589 |
static char MVM_SPOOL_DIR[PATH_MAX]; |
| 590 |
|
| 591 |
|
| 592 |
static char *app_minivm_record = "MinivmRecord"; |
| 593 |
static char *app_minivm_greet = "MinivmGreet"; |
| 594 |
static char *app_minivm_notify = "MinivmNotify"; |
| 595 |
static char *app_minivm_delete = "MinivmDelete"; |
| 596 |
static char *app_minivm_accmess = "MinivmAccMess"; |
| 597 |
static char *app_minivm_mwi = "MinivmMWI"; |
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
enum minivm_option_flags { |
| 602 |
OPT_SILENT = (1 << 0), |
| 603 |
OPT_BUSY_GREETING = (1 << 1), |
| 604 |
OPT_UNAVAIL_GREETING = (1 << 2), |
| 605 |
OPT_TEMP_GREETING = (1 << 3), |
| 606 |
OPT_NAME_GREETING = (1 << 4), |
| 607 |
OPT_RECORDGAIN = (1 << 5), |
| 608 |
}; |
| 609 |
|
| 610 |
enum minivm_option_args { |
| 611 |
OPT_ARG_RECORDGAIN = 0, |
| 612 |
OPT_ARG_ARRAY_SIZE = 1, |
| 613 |
}; |
| 614 |
|
| 615 |
AST_APP_OPTIONS(minivm_app_options, { |
| 616 |
AST_APP_OPTION('s', OPT_SILENT), |
| 617 |
AST_APP_OPTION('b', OPT_BUSY_GREETING), |
| 618 |
AST_APP_OPTION('u', OPT_UNAVAIL_GREETING), |
| 619 |
AST_APP_OPTION_ARG('g', OPT_RECORDGAIN, OPT_ARG_RECORDGAIN), |
| 620 |
}); |
| 621 |
|
| 622 |
AST_APP_OPTIONS(minivm_accmess_options, { |
| 623 |
AST_APP_OPTION('b', OPT_BUSY_GREETING), |
| 624 |
AST_APP_OPTION('u', OPT_UNAVAIL_GREETING), |
| 625 |
AST_APP_OPTION('t', OPT_TEMP_GREETING), |
| 626 |
AST_APP_OPTION('n', OPT_NAME_GREETING), |
| 627 |
}); |
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
struct minivm_account { |
| 632 |
char username[AST_MAX_CONTEXT]; |
| 633 |
char domain[AST_MAX_CONTEXT]; |
| 634 |
|
| 635 |
char pincode[10]; |
| 636 |
char fullname[120]; |
| 637 |
char email[80]; |
| 638 |
char pager[80]; |
| 639 |
char accountcode[AST_MAX_ACCOUNT_CODE]; |
| 640 |
char serveremail[80]; |
| 641 |
char externnotify[160]; |
| 642 |
char language[MAX_LANGUAGE]; |
| 643 |
char zonetag[80]; |
| 644 |
char uniqueid[20]; |
| 645 |
char exit[80]; |
| 646 |
char attachfmt[80]; |
| 647 |
char etemplate[80]; |
| 648 |
char ptemplate[80]; |
| 649 |
unsigned int flags; |
| 650 |
struct ast_variable *chanvars; |
| 651 |
double volgain; |
| 652 |
AST_LIST_ENTRY(minivm_account) list; |
| 653 |
}; |
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
static AST_LIST_HEAD_STATIC(minivm_accounts, minivm_account); |
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
struct minivm_template { |
| 665 |
char name[80]; |
| 666 |
char *body; |
| 667 |
char fromaddress[100]; |
| 668 |
char serveremail[80]; |
| 669 |
char subject[100]; |
| 670 |
char charset[32]; |
| 671 |
char locale[20]; |
| 672 |
char dateformat[80]; |
| 673 |
int attachment; |
| 674 |
AST_LIST_ENTRY(minivm_template) list; |
| 675 |
}; |
| 676 |
|
| 677 |
|
| 678 |
static AST_LIST_HEAD_STATIC(message_templates, minivm_template); |
| 679 |
|
| 680 |
|
| 681 |
struct leave_vm_options { |
| 682 |
unsigned int flags; |
| 683 |
signed char record_gain; |
| 684 |
}; |
| 685 |
|
| 686 |
|
| 687 |
struct minivm_zone { |
| 688 |
char name[80]; |
| 689 |
char timezone[80]; |
| 690 |
char msg_format[BUFSIZ]; |
| 691 |
AST_LIST_ENTRY(minivm_zone) list; |
| 692 |
}; |
| 693 |
|
| 694 |
|
| 695 |
static AST_LIST_HEAD_STATIC(minivm_zones, minivm_zone); |
| 696 |
|
| 697 |
|
| 698 |
struct minivm_stats { |
| 699 |
int voicemailaccounts; |
| 700 |
int timezones; |
| 701 |
int templates; |
| 702 |
|
| 703 |
struct timeval reset; |
| 704 |
int receivedmessages; |
| 705 |
struct timeval lastreceived; |
| 706 |
}; |
| 707 |
|
| 708 |
|
| 709 |
static struct minivm_stats global_stats; |
| 710 |
|
| 711 |
AST_MUTEX_DEFINE_STATIC(minivmlock); |
| 712 |
AST_MUTEX_DEFINE_STATIC(minivmloglock); |
| 713 |
|
| 714 |
static FILE *minivmlogfile; |
| 715 |
|
| 716 |
static int global_vmminmessage; |
| 717 |
static int global_vmmaxmessage; |
| 718 |
static int global_maxsilence; |
| 719 |
static int global_maxgreet; |
| 720 |
static int global_silencethreshold = 128; |
| 721 |
static char global_mailcmd[160]; |
| 722 |
static char global_externnotify[160]; |
| 723 |
static char global_logfile[PATH_MAX]; |
| 724 |
static char default_vmformat[80]; |
| 725 |
|
| 726 |
static struct ast_flags globalflags = {0}; |
| 727 |
static int global_saydurationminfo; |
| 728 |
|
| 729 |
static double global_volgain; |
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
#define DEFAULT_DATEFORMAT "%A, %B %d, %Y at %r" |
| 734 |
#define DEFAULT_CHARSET "ISO-8859-1" |
| 735 |
|
| 736 |
|
| 737 |
static char *message_template_parse_filebody(const char *filename); |
| 738 |
static char *message_template_parse_emailbody(const char *body); |
| 739 |
static int create_vmaccount(char *name, struct ast_variable *var, int realtime); |
| 740 |
static struct minivm_account *find_user_realtime(const char *domain, const char *username); |
| 741 |
static char *handle_minivm_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a); |
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
static struct minivm_template *message_template_create(const char *name) |
| 746 |
{ |
| 747 |
struct minivm_template *template; |
| 748 |
|
| 749 |
template = ast_calloc(1, sizeof(*template)); |
| 750 |
if (!template) |
| 751 |
return NULL; |
| 752 |
|
| 753 |
|
| 754 |
ast_copy_string(template->name, name, sizeof(template->name)); |
| 755 |
ast_copy_string(template->dateformat, DEFAULT_DATEFORMAT, sizeof(template->dateformat)); |
| 756 |
ast_copy_string(template->charset, DEFAULT_CHARSET, sizeof(template->charset)); |
| 757 |
ast_copy_string(template->subject, "New message in mailbox ${MVM_USERNAME}@${MVM_DOMAIN}", sizeof(template->subject)); |
| 758 |
template->attachment = TRUE; |
| 759 |
|
| 760 |
return template; |
| 761 |
} |
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
static void message_template_free(struct minivm_template *template) |
| 766 |
{ |
| 767 |
if (template->body) |
| 768 |
ast_free(template->body); |
| 769 |
|
| 770 |
ast_free (template); |
| 771 |
} |
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
static int message_template_build(const char *name, struct ast_variable *var) |
| 776 |
{ |
| 777 |
struct minivm_template *template; |
| 778 |
int error = 0; |
| 779 |
|
| 780 |
template = message_template_create(name); |
| 781 |
if (!template) { |
| 782 |
ast_log(LOG_ERROR, "Out of memory, can't allocate message template object %s.\n", name); |
| 783 |
return -1; |
| 784 |
} |
| 785 |
|
| 786 |
while (var) { |
| 787 |
ast_debug(3, "Configuring template option %s = \"%s\" for template %s\n", var->name, var->value, name); |
| 788 |
if (!strcasecmp(var->name, "fromaddress")) { |
| 789 |
ast_copy_string(template->fromaddress, var->value, sizeof(template->fromaddress)); |
| 790 |
} else if (!strcasecmp(var->name, "fromemail")) { |
| 791 |
ast_copy_string(template->serveremail, var->value, sizeof(template->serveremail)); |
| 792 |
} else if (!strcasecmp(var->name, "subject")) { |
| 793 |
ast_copy_string(template->subject, var->value, sizeof(template->subject)); |
| 794 |
} else if (!strcasecmp(var->name, "locale")) { |
| 795 |
ast_copy_string(template->locale, var->value, sizeof(template->locale)); |
| 796 |
} else if (!strcasecmp(var->name, "attachmedia")) { |
| 797 |
template->attachment = ast_true(var->value); |
| 798 |
} else if (!strcasecmp(var->name, "dateformat")) { |
| 799 |
ast_copy_string(template->dateformat, var->value, sizeof(template->dateformat)); |
| 800 |
} else if (!strcasecmp(var->name, "charset")) { |
| 801 |
ast_copy_string(template->charset, var->value, sizeof(template->charset)); |
| 802 |
} else if (!strcasecmp(var->name, "templatefile")) { |
| 803 |
if (template->body) |
| 804 |
ast_free(template->body); |
| 805 |
template->body = message_template_parse_filebody(var->value); |
| 806 |
if (!template->body) { |
| 807 |
ast_log(LOG_ERROR, "Error reading message body definition file %s\n", var->value); |
| 808 |
error++; |
| 809 |
} |
| 810 |
} else if (!strcasecmp(var->name, "messagebody")) { |
| 811 |
if (template->body) |
| 812 |
ast_free(template->body); |
| 813 |
template->body = message_template_parse_emailbody(var->value); |
| 814 |
if (!template->body) { |
| 815 |
ast_log(LOG_ERROR, "Error parsing message body definition:\n %s\n", var->value); |
| 816 |
error++; |
| 817 |
} |
| 818 |
} else { |
| 819 |
ast_log(LOG_ERROR, "Unknown message template configuration option \"%s=%s\"\n", var->name, var->value); |
| 820 |
error++; |
| 821 |
} |
| 822 |
var = var->next; |
| 823 |
} |
| 824 |
if (error) |
| 825 |
ast_log(LOG_ERROR, "-- %d errors found parsing message template definition %s\n", error, name); |
| 826 |
|
| 827 |
AST_LIST_LOCK(&message_templates); |
| 828 |
AST_LIST_INSERT_TAIL(&message_templates, template, list); |
| 829 |
AST_LIST_UNLOCK(&message_templates); |
| 830 |
|
| 831 |
global_stats.templates++; |
| 832 |
|
| 833 |
return error; |
| 834 |
} |
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
static struct minivm_template *message_template_find(const char *name) |
| 839 |
{ |
| 840 |
struct minivm_template *this, *res = NULL; |
| 841 |
|
| 842 |
if (ast_strlen_zero(name)) |
| 843 |
return NULL; |
| 844 |
|
| 845 |
AST_LIST_LOCK(&message_templates); |
| 846 |
AST_LIST_TRAVERSE(&message_templates, this, list) { |
| 847 |
if (!strcasecmp(this->name, name)) { |
| 848 |
res = this; |
| 849 |
break; |
| 850 |
} |
| 851 |
} |
| 852 |
AST_LIST_UNLOCK(&message_templates); |
| 853 |
|
| 854 |
return res; |
| 855 |
} |
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
|
| 860 |
static void message_destroy_list(void) |
| 861 |
{ |
| 862 |
struct minivm_template *this; |
| 863 |
AST_LIST_LOCK(&message_templates); |
| 864 |
while ((this = AST_LIST_REMOVE_HEAD(&message_templates, list))) { |
| 865 |
message_template_free(this); |
| 866 |
} |
| 867 |
|
| 868 |
AST_LIST_UNLOCK(&message_templates); |
| 869 |
} |
| 870 |
|
| 871 |
static int get_date(char *s, int len) |
| 872 |
{ |
| 873 |
struct ast_tm tm; |
| 874 |
struct timeval now = ast_tvnow(); |
| 875 |
|
| 876 |
ast_localtime(&now, &tm, NULL); |
| 877 |
return ast_strftime(s, len, "%a %b %e %r %Z %Y", &tm); |
| 878 |
} |
| 879 |
|
| 880 |
|
| 881 |
|
| 882 |
|
| 883 |
static void free_user(struct minivm_account *vmu) |
| 884 |
{ |
| 885 |
if (vmu->chanvars) |
| 886 |
ast_variables_destroy(vmu->chanvars); |
| 887 |
ast_free(vmu); |
| 888 |
} |
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
static void prep_email_sub_vars(struct ast_channel *channel, const struct minivm_account *vmu, const char *cidnum, const char *cidname, const char *dur, const char *date, const char *counter) |
| 897 |
{ |
| 898 |
char callerid[256]; |
| 899 |
struct ast_variable *var; |
| 900 |
|
| 901 |
if (!channel) { |
| 902 |
ast_log(LOG_ERROR, "No allocated channel, giving up...\n"); |
| 903 |
return; |
| 904 |
} |
| 905 |
|
| 906 |
for (var = vmu->chanvars ; var ; var = var->next) { |
| 907 |
pbx_builtin_setvar_helper(channel, var->name, var->value); |
| 908 |
} |
| 909 |
|
| 910 |
|
| 911 |
pbx_builtin_setvar_helper(channel, "MVM_NAME", vmu->fullname); |
| 912 |
pbx_builtin_setvar_helper(channel, "MVM_DUR", dur); |
| 913 |
pbx_builtin_setvar_helper(channel, "MVM_DOMAIN", vmu->domain); |
| 914 |
pbx_builtin_setvar_helper(channel, "MVM_USERNAME", vmu->username); |
| 915 |
pbx_builtin_setvar_helper(channel, "MVM_CALLERID", ast_callerid_merge(callerid, sizeof(callerid), cidname, cidnum, "Unknown Caller")); |
| 916 |
pbx_builtin_setvar_helper(channel, "MVM_CIDNAME", (cidname ? cidname : "an unknown caller")); |
| 917 |
pbx_builtin_setvar_helper(channel, "MVM_CIDNUM", (cidnum ? cidnum : "an unknown caller")); |
| 918 |
pbx_builtin_setvar_helper(channel, "MVM_DATE", date); |
| 919 |
if (!ast_strlen_zero(counter)) |
| 920 |
pbx_builtin_setvar_helper(channel, "MVM_COUNTER", counter); |
| 921 |
} |
| 922 |
|
| 923 |
|
| 924 |
|
| 925 |
static void populate_defaults(struct minivm_account *vmu) |
| 926 |
{ |
| 927 |
ast_copy_flags(vmu, (&globalflags), AST_FLAGS_ALL); |
| 928 |
ast_copy_string(vmu->attachfmt, default_vmformat, sizeof(vmu->attachfmt)); |
| 929 |
vmu->volgain = global_volgain; |
| 930 |
} |
| 931 |
|
| 932 |
|
| 933 |
|
| 934 |
static struct minivm_account *mvm_user_alloc(void) |
| 935 |
{ |
| 936 |
struct minivm_account *new; |
| 937 |
|
| 938 |
new = ast_calloc(1, sizeof(*new)); |
| 939 |
if (!new) |
| 940 |
return NULL; |
| 941 |
populate_defaults(new); |
| 942 |
|
| 943 |
return new; |
| 944 |
} |
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
|
| 949 |
static void vmaccounts_destroy_list(void) |
| 950 |
{ |
| 951 |
struct minivm_account *this; |
| 952 |
AST_LIST_LOCK(&minivm_accounts); |
| 953 |
while ((this = AST_LIST_REMOVE_HEAD(&minivm_accounts, list))) |
| 954 |
ast_free(this); |
| 955 |
AST_LIST_UNLOCK(&minivm_accounts); |
| 956 |
} |
| 957 |
|
| 958 |
|
| 959 |
|
| 960 |
|
| 961 |
static struct minivm_account *find_account(const char *domain, const char *username, int createtemp) |
| 962 |
{ |
| 963 |
struct minivm_account *vmu = NULL, *cur; |
| 964 |
|
| 965 |
|
| 966 |
if (ast_strlen_zero(domain) || ast_strlen_zero(username)) { |
| 967 |
ast_log(LOG_NOTICE, "No username or domain? \n"); |
| 968 |
return NULL; |
| 969 |
} |
| 970 |
ast_debug(3, "Looking for voicemail user %s in domain %s\n", username, domain); |
| 971 |
|
| 972 |
AST_LIST_LOCK(&minivm_accounts); |
| 973 |
AST_LIST_TRAVERSE(&minivm_accounts, cur, list) { |
| 974 |
|
| 975 |
if (!strcasecmp(domain, cur->domain) && !strcasecmp(username, cur->username)) |
| 976 |
break; |
| 977 |
} |
| 978 |
AST_LIST_UNLOCK(&minivm_accounts); |
| 979 |
|
| 980 |
if (cur) { |
| 981 |
ast_debug(3, "Found account for %s@%s\n", username, domain); |
| 982 |
vmu = cur; |
| 983 |
|
| 984 |
} else |
| 985 |
vmu = find_user_realtime(domain, username); |
| 986 |
|
| 987 |
if (createtemp && !vmu) { |
| 988 |
|
| 989 |
vmu = mvm_user_alloc(); |
| 990 |
ast_set2_flag(vmu, TRUE, MVM_ALLOCED); |
| 991 |
if (vmu) { |
| 992 |
ast_copy_string(vmu->username, username, sizeof(vmu->username)); |
| 993 |
ast_copy_string(vmu->domain, domain, sizeof(vmu->domain)); |
| 994 |
ast_debug(1, "Created temporary account\n"); |
| 995 |
} |
| 996 |
|
| 997 |
} |
| 998 |
return vmu; |
| 999 |
} |
| 1000 |
|
| 1001 |
|
| 1002 |
|
| 1003 |
|
| 1004 |
|
| 1005 |
static struct minivm_account *find_user_realtime(const char *domain, const char *username) |
| 1006 |
{ |
| 1007 |
struct ast_variable *var; |
| 1008 |
struct minivm_account *retval; |
| 1009 |
char name[MAXHOSTNAMELEN]; |
| 1010 |
|
| 1011 |
retval = mvm_user_alloc(); |
| 1012 |
if (!retval) |
| 1013 |
return NULL; |
| 1014 |
|
| 1015 |
if (username) |
| 1016 |
ast_copy_string(retval->username, username, sizeof(retval->username)); |
| 1017 |
|
| 1018 |
populate_defaults(retval); |
| 1019 |
var = ast_load_realtime("minivm", "username", username, "domain", domain, SENTINEL); |
| 1020 |
|
| 1021 |
if (!var) { |
| 1022 |
ast_free(retval); |
| 1023 |
return NULL; |
| 1024 |
} |
| 1025 |
|
| 1026 |
snprintf(name, sizeof(name), "%s@%s", username, domain); |
| 1027 |
create_vmaccount(name, var, TRUE); |
| 1028 |
|
| 1029 |
ast_variables_destroy(var); |
| 1030 |
return retval; |
| 1031 |
} |
| 1032 |
|
| 1033 |
|
| 1034 |
|
| 1035 |
|
| 1036 |
|
| 1037 |
|
| 1038 |
static int check_mime(const char *str) |
| 1039 |
{ |
| 1040 |
for (; *str; str++) { |
| 1041 |
if (*str > 126 || *str < 32 || strchr("()<>@,:;/\"[]?.=", *str)) { |
| 1042 |
return 1; |
| 1043 |
} |
| 1044 |
} |
| 1045 |
return 0; |
| 1046 |
} |
| 1047 |
|
| 1048 |
|
| 1049 |
|
| 1050 |
|
| 1051 |
|
| 1052 |
|
| 1053 |
|
| 1054 |
|
| 1055 |
|
| 1056 |
|
| 1057 |
|
| 1058 |
|
| 1059 |
|
| 1060 |
|
| 1061 |
|
| 1062 |
|
| 1063 |
|
| 1064 |
|
| 1065 |
|
| 1066 |
|
| 1067 |
static const char *ast_str_encode_mime(struct ast_str **end, ssize_t maxlen, const char *charset, const char *start, size_t preamble, size_t postamble) |
| 1068 |
{ |
| 1069 |
struct ast_str *tmp = ast_str_alloca(80); |
| 1070 |
int first_section = 1; |
| 1071 |
|
| 1072 |
ast_str_reset(*end); |
| 1073 |
ast_str_set(&tmp, -1, "=?%s?Q?", charset); |
| 1074 |
for (; *start; start++) { |
| 1075 |
int need_encoding = 0; |
| 1076 |
if (*start < 33 || *start > 126 || strchr("()<>@,:;/\"[]?.=_", *start)) { |
| 1077 |
need_encoding = 1; |
| 1078 |
} |
| 1079 |
if ((first_section && need_encoding && preamble + ast_str_strlen(tmp) > 70) || |
| 1080 |
(first_section && !need_encoding && preamble + ast_str_strlen(tmp) > 72) || |
| 1081 |
(!first_section && need_encoding && ast_str_strlen(tmp) > 70) || |
| 1082 |
(!first_section && !need_encoding && ast_str_strlen(tmp) > 72)) { |
| 1083 |
|
| 1084 |
ast_str_append(end, maxlen, "%s%s?=", first_section ? "" : " ", ast_str_buffer(tmp)); |
| 1085 |
ast_str_set(&tmp, -1, "=?%s?Q?", charset); |
| 1086 |
first_section = 0; |
| 1087 |
} |
| 1088 |
if (need_encoding && *start == ' ') { |
| 1089 |
ast_str_append(&tmp, -1, "_"); |
| 1090 |
} else if (need_encoding) { |
| 1091 |
ast_str_append(&tmp, -1, "=%hhX", *start); |
| 1092 |
} else { |
| 1093 |
ast_str_append(&tmp, -1, "%c", *start); |
| 1094 |
} |
| 1095 |
} |
| 1096 |
ast_str_append(end, maxlen, "%s%s?=%s", first_section ? "" : " ", ast_str_buffer(tmp), ast_str_strlen(tmp) + postamble > 74 ? " " : ""); |
| 1097 |
return ast_str_buffer(*end); |
| 1098 |
} |
| 1099 |
|
| 1100 |
|
| 1101 |
|
| 1102 |
|
| 1103 |
|
| 1104 |
|
| 1105 |
|
| 1106 |
|
| 1107 |
|
| 1108 |
static const char *ast_str_quote(struct ast_str **buf, ssize_t maxlen, const char *from) |
| 1109 |
{ |
| 1110 |
const char *ptr; |
| 1111 |
|
| 1112 |
|
| 1113 |
ast_str_set(buf, maxlen, "\""); |
| 1114 |
for (ptr = from; *ptr; ptr++) { |
| 1115 |
if (*ptr == '"' || *ptr == '\\') { |
| 1116 |
ast_str_append(buf, maxlen, "\\%c", *ptr); |
| 1117 |
} else { |
| 1118 |
ast_str_append(buf, maxlen, "%c", *ptr); |
| 1119 |
} |
| 1120 |
} |
| 1121 |
ast_str_append(buf, maxlen, "\""); |
| 1122 |
|
| 1123 |
return ast_str_buffer(*buf); |
| 1124 |
} |
| 1125 |
|
| 1126 |
|
| 1127 |
|
| 1128 |
static int sendmail(struct minivm_template *template, struct minivm_account *vmu, char *cidnum, char *cidname, const char *filename, char *format, int duration, int attach_user_voicemail, enum mvm_messagetype type, const char *counter) |
| 1129 |
{ |
| 1130 |
RAII_VAR(struct ast_str *, str1, ast_str_create(16), ast_free); |
| 1131 |
RAII_VAR(struct ast_str *, str2, ast_str_create(16), ast_free); |
| 1132 |
FILE *p = NULL; |
| 1133 |
int pfd; |
| 1134 |
char email[256] = ""; |
| 1135 |
char who[256] = ""; |
| 1136 |
char date[256]; |
| 1137 |
char bound[256]; |
| 1138 |
char fname[PATH_MAX]; |
| 1139 |
char dur[PATH_MAX]; |
| 1140 |
char tmp[80] = "/tmp/astmail-XXXXXX"; |
| 1141 |
char mail_cmd_buffer[PATH_MAX]; |
| 1142 |
char sox_gain_tmpdir[PATH_MAX] = ""; |
| 1143 |
char *file_to_delete = NULL, *dir_to_delete = NULL; |
| 1144 |
struct timeval now; |
| 1145 |
struct ast_tm tm; |
| 1146 |
struct minivm_zone *the_zone = NULL; |
| 1147 |
struct ast_channel *chan = NULL; |
| 1148 |
char *fromaddress; |
| 1149 |
char *fromemail; |
| 1150 |
int res = -1; |
| 1151 |
|
| 1152 |
if (!str1 || !str2) { |
| 1153 |
return -1; |
| 1154 |
} |
| 1155 |
|
| 1156 |
if (type == MVM_MESSAGE_EMAIL) { |
| 1157 |
if (vmu && !ast_strlen_zero(vmu->email)) { |
| 1158 |
ast_copy_string(email, vmu->email, sizeof(email)); |
| 1159 |
} else if (!ast_strlen_zero(vmu->username) && !ast_strlen_zero(vmu->domain)) |
| 1160 |
snprintf(email, sizeof(email), "%s@%s", vmu->username, vmu->domain); |
| 1161 |
} else if (type == MVM_MESSAGE_PAGE) { |
| 1162 |
ast_copy_string(email, vmu->pager, sizeof(email)); |
| 1163 |
} |
| 1164 |
|
| 1165 |
if (ast_strlen_zero(email)) { |
| 1166 |
ast_log(LOG_WARNING, "No address to send message to.\n"); |
| 1167 |
return -1; |
| 1168 |
} |
| 1169 |
|
| 1170 |
ast_debug(3, "Sending mail to %s@%s - Using template %s\n", vmu->username, vmu->domain, template->name); |
| 1171 |
|
| 1172 |
if (!strcmp(format, "wav49")) |
| 1173 |
format = "WAV"; |
| 1174 |
|
| 1175 |
|
| 1176 |
if (type == MVM_MESSAGE_EMAIL && (vmu->volgain < -.001 || vmu->volgain > .001) ) { |
| 1177 |
char sox_gain_cmd[PATH_MAX]; |
| 1178 |
|
| 1179 |
ast_copy_string(sox_gain_tmpdir, "/tmp/minivm-gain-XXXXXX", sizeof(sox_gain_tmpdir)); |
| 1180 |
ast_debug(3, "sox_gain_tmpdir: %s\n", sox_gain_tmpdir); |
| 1181 |
if (!mkdtemp(sox_gain_tmpdir)) { |
| 1182 |
ast_log(LOG_WARNING, "Failed to create temporary directory for volgain: %d\n", errno); |
| 1183 |
return -1; |
| 1184 |
} |
| 1185 |
snprintf(fname, sizeof(fname), "%s/output.%s", sox_gain_tmpdir, format); |
| 1186 |
snprintf(sox_gain_cmd, sizeof(sox_gain_cmd), "sox -v %.4f %s.%s %s", vmu->volgain, filename, format, fname); |
| 1187 |
ast_safe_system(sox_gain_cmd); |
| 1188 |
ast_debug(3, "VOLGAIN: Stored at: %s.%s - Level: %.4f - Mailbox: %s\n", filename, format, vmu->volgain, vmu->username); |
| 1189 |
|
| 1190 |
|
| 1191 |
file_to_delete = fname; |
| 1192 |
dir_to_delete = sox_gain_tmpdir; |
| 1193 |
} else { |
| 1194 |
snprintf(fname, sizeof(fname), "%s.%s", filename, format); |
| 1195 |
} |
| 1196 |
|
| 1197 |
if (template->attachment) |
| 1198 |
ast_debug(1, "Attaching file '%s', format '%s', uservm is '%d'\n", fname, format, attach_user_voicemail); |
| 1199 |
|
| 1200 |
|
| 1201 |
|
| 1202 |
pfd = mkstemp(tmp); |
| 1203 |
if (pfd > -1) { |
| 1204 |
p = fdopen(pfd, "w"); |
| 1205 |
if (!p) { |
| 1206 |
close(pfd); |
| 1207 |
pfd = -1; |
| 1208 |
} |
| 1209 |
ast_debug(1, "Opening temp file for e-mail: %s\n", tmp); |
| 1210 |
} |
| 1211 |
if (!p) { |
| 1212 |
ast_log(LOG_WARNING, "Unable to open temporary file '%s'\n", tmp); |
| 1213 |
goto out; |
| 1214 |
} |
| 1215 |
|
| 1216 |
chan = ast_dummy_channel_alloc(); |
| 1217 |
if (!chan) { |
| 1218 |
goto out; |
| 1219 |
} |
| 1220 |
|
| 1221 |
snprintf(dur, sizeof(dur), "%d:%02d", duration / 60, duration % 60); |
| 1222 |
|
| 1223 |
|
| 1224 |
if (!ast_strlen_zero(vmu->zonetag)) { |
| 1225 |
|
| 1226 |
struct minivm_zone *z; |
| 1227 |
AST_LIST_LOCK(&minivm_zones); |
| 1228 |
AST_LIST_TRAVERSE(&minivm_zones, z, list) { |
| 1229 |
if (strcmp(z->name, vmu->zonetag)) |
| 1230 |
continue; |
| 1231 |
the_zone = z; |
| 1232 |
} |
| 1233 |
AST_LIST_UNLOCK(&minivm_zones); |
| 1234 |
} |
| 1235 |
|
| 1236 |
now = ast_tvnow(); |
| 1237 |
ast_localtime(&now, &tm, the_zone ? the_zone->timezone : NULL); |
| 1238 |
ast_strftime(date, sizeof(date), "%a, %d %b %Y %H:%M:%S %z", &tm); |
| 1239 |
|
| 1240 |
|
| 1241 |
fprintf(p, "Date: %s\n", date); |
| 1242 |
|
| 1243 |
|
| 1244 |
ast_strftime(date, sizeof(date), template->dateformat, &tm); |
| 1245 |
|
| 1246 |
|
| 1247 |
prep_email_sub_vars(chan, vmu, cidnum, cidname, dur, date, counter); |
| 1248 |
|
| 1249 |
|
| 1250 |
|
| 1251 |
fromemail = ast_strlen_zero(vmu->serveremail) ? template->serveremail : vmu->serveremail; |
| 1252 |
|
| 1253 |
|
| 1254 |
fromaddress = ast_strlen_zero(template->fromaddress) ? "" : template->fromaddress; |
| 1255 |
|
| 1256 |
|
| 1257 |
if (ast_strlen_zero(fromemail)) |
| 1258 |
fromemail = "asterisk"; |
| 1259 |
|
| 1260 |
if (strchr(fromemail, '@')) |
| 1261 |
ast_copy_string(who, fromemail, sizeof(who)); |
| 1262 |
else { |
| 1263 |
char host[MAXHOSTNAMELEN]; |
| 1264 |
gethostname(host, sizeof(host)-1); |
| 1265 |
snprintf(who, sizeof(who), "%s@%s", fromemail, host); |
| 1266 |
} |
| 1267 |
|
| 1268 |
if (ast_strlen_zero(fromaddress)) { |
| 1269 |
fprintf(p, "From: Asterisk PBX <%s>\n", who); |
| 1270 |
} else { |
| 1271 |
ast_debug(4, "Fromaddress template: %s\n", fromaddress); |
| 1272 |
ast_str_substitute_variables(&str1, 0, chan, fromaddress); |
| 1273 |
if (check_mime(ast_str_buffer(str1))) { |
| 1274 |
int first_line = 1; |
| 1275 |
char *ptr; |
| 1276 |
ast_str_encode_mime(&str2, 0, template->charset, ast_str_buffer(str1), strlen("From: "), strlen(who) + 3); |
| 1277 |
while ((ptr = strchr(ast_str_buffer(str2), ' '))) { |
| 1278 |
*ptr = '\0'; |
| 1279 |
fprintf(p, "%s %s\n", first_line ? "From:" : "", ast_str_buffer(str2)); |
| 1280 |
first_line = 0; |
| 1281 |
|
| 1282 |
ast_str_set(&str2, 0, "%s", ptr + 1); |
| 1283 |
} |
| 1284 |
fprintf(p, "%s %s <%s>\n", first_line ? "From:" : "", ast_str_buffer(str2), who); |
| 1285 |
} else { |
| 1286 |
fprintf(p, "From: %s <%s>\n", ast_str_quote(&str2, 0, ast_str_buffer(str1)), who); |
| 1287 |
} |
| 1288 |
} |
| 1289 |
|
| 1290 |
fprintf(p, "Message-ID: <Asterisk-%u-%s-%d-%s>\n", (unsigned int)ast_random(), vmu->username, (int)getpid(), who); |
| 1291 |
|
| 1292 |
if (ast_strlen_zero(vmu->email)) { |
| 1293 |
snprintf(email, sizeof(email), "%s@%s", vmu->username, vmu->domain); |
| 1294 |
} else { |
| 1295 |
ast_copy_string(email, vmu->email, sizeof(email)); |
| 1296 |
} |
| 1297 |
|
| 1298 |
if (check_mime(vmu->fullname)) { |
| 1299 |
int first_line = 1; |
| 1300 |
char *ptr; |
| 1301 |
ast_str_encode_mime(&str2, 0, template->charset, vmu->fullname, strlen("To: "), strlen(email) + 3); |
| 1302 |
while ((ptr = strchr(ast_str_buffer(str2), ' '))) { |
| 1303 |
*ptr = '\0'; |
| 1304 |
fprintf(p, "%s %s\n", first_line ? "To:" : "", ast_str_buffer(str2)); |
| 1305 |
first_line = 0; |
| 1306 |
|
| 1307 |
ast_str_set(&str2, 0, "%s", ptr + 1); |
| 1308 |
} |
| 1309 |
fprintf(p, "%s %s <%s>\n", first_line ? "To:" : "", ast_str_buffer(str2), email); |
| 1310 |
} else { |
| 1311 |
fprintf(p, "To: %s <%s>\n", ast_str_quote(&str2, 0, vmu->fullname), email); |
| 1312 |
} |
| 1313 |
|
| 1314 |
if (!ast_strlen_zero(template->subject)) { |
| 1315 |
ast_str_substitute_variables(&str1, 0, chan, template->subject); |
| 1316 |
if (check_mime(ast_str_buffer(str1))) { |
| 1317 |
int first_line = 1; |
| 1318 |
char *ptr; |
| 1319 |
ast_str_encode_mime(&str2, 0, template->charset, ast_str_buffer(str1), strlen("Subject: "), 0); |
| 1320 |
while ((ptr = strchr(ast_str_buffer(str2), ' '))) { |
| 1321 |
*ptr = '\0'; |
| 1322 |
fprintf(p, "%s %s\n", first_line ? "Subject:" : "", ast_str_buffer(str2)); |
| 1323 |
first_line = 0; |
| 1324 |
|
| 1325 |
ast_str_set(&str2, 0, "%s", ptr + 1); |
| 1326 |
} |
| 1327 |
fprintf(p, "%s %s\n", first_line ? "Subject:" : "", ast_str_buffer(str2)); |
| 1328 |
} else { |
| 1329 |
fprintf(p, "Subject: %s\n", ast_str_buffer(str1)); |
| 1330 |
} |
| 1331 |
} else { |
| 1332 |
fprintf(p, "Subject: New message in mailbox %s@%s\n", vmu->username, vmu->domain); |
| 1333 |
ast_debug(1, "Using default subject for this email \n"); |
| 1334 |
} |
| 1335 |
|
| 1336 |
if (DEBUG_ATLEAST(3)) |
| 1337 |
fprintf(p, "X-Asterisk-debug: template %s user account %s@%s\n", template->name, vmu->username, vmu->domain); |
| 1338 |
fprintf(p, "MIME-Version: 1.0\n"); |
| 1339 |
|
| 1340 |
|
| 1341 |
snprintf(bound, sizeof(bound), "voicemail_%s%d%u", vmu->username, (int)getpid(), (unsigned int)ast_random()); |
| 1342 |
|
| 1343 |
fprintf(p, "Content-Type: multipart/mixed; boundary=\"%s\"\n\n\n", bound); |
| 1344 |
|
| 1345 |
fprintf(p, "--%s\n", bound); |
| 1346 |
fprintf(p, "Content-Type: text/plain; charset=%s\nContent-Transfer-Encoding: 8bit\n\n", template->charset); |
| 1347 |
if (!ast_strlen_zero(template->body)) { |
| 1348 |
ast_str_substitute_variables(&str1, 0, chan, template->body); |
| 1349 |
ast_debug(3, "Message now: %s\n-----\n", ast_str_buffer(str1)); |
| 1350 |
fprintf(p, "%s\n", ast_str_buffer(str1)); |
| 1351 |
} else { |
| 1352 |
fprintf(p, "Dear %s:\n\n\tJust wanted to let you know you were just left a %s long message \n" |
| 1353 |
"in mailbox %s from %s, on %s so you might\n" |
| 1354 |
"want to check it when you get a chance. Thanks!\n\n\t\t\t\t--Asterisk\n\n", vmu->fullname, |
| 1355 |
dur, vmu->username, (cidname ? cidname : (cidnum ? cidnum : "an unknown caller")), date); |
| 1356 |
ast_debug(3, "Using default message body (no template)\n-----\n"); |
| 1357 |
} |
| 1358 |
|
| 1359 |
if (template->attachment) { |
| 1360 |
char *ctype = "audio/x-"; |
| 1361 |
ast_debug(3, "Attaching file to message: %s\n", fname); |
| 1362 |
if (!strcasecmp(format, "ogg")) |
| 1363 |
ctype = "application/"; |
| 1364 |
|
| 1365 |
fprintf(p, "--%s\n", bound); |
| 1366 |
fprintf(p, "Content-Type: %s%s; name=\"voicemailmsg.%s\"\n", ctype, format, format); |
| 1367 |
fprintf(p, "Content-Transfer-Encoding: base64\n"); |
| 1368 |
fprintf(p, "Content-Description: Voicemail sound attachment.\n"); |
| 1369 |
fprintf(p, "Content-Disposition: attachment; filename=\"voicemail%s.%s\"\n\n", counter ? counter : "", format); |
| 1370 |
|
| 1371 |
ast_base64_encode_file_path(fname, p, EOL); |
| 1372 |
fprintf(p, "\n\n--%s--\n.\n", bound); |
| 1373 |
} |
| 1374 |
fclose(p); |
| 1375 |
|
| 1376 |
chan = ast_channel_unref(chan); |
| 1377 |
|
| 1378 |
if (file_to_delete && dir_to_delete) { |
| 1379 |
|
| 1380 |
|
| 1381 |
res = snprintf(mail_cmd_buffer, sizeof(mail_cmd_buffer), |
| 1382 |
"( %s < %s ; rm -f %s %s ; rmdir %s ) &", |
| 1383 |
global_mailcmd, tmp, tmp, file_to_delete, dir_to_delete); |
| 1384 |
} else { |
| 1385 |
res = snprintf(mail_cmd_buffer, sizeof(mail_cmd_buffer), |
| 1386 |
"( %s < %s ; rm -f %s ) &", |
| 1387 |
global_mailcmd, tmp, tmp); |
| 1388 |
} |
| 1389 |
|
| 1390 |
if (res < sizeof(mail_cmd_buffer)) { |
| 1391 |
file_to_delete = dir_to_delete = NULL; |
| 1392 |
} else { |
| 1393 |
ast_log(LOG_ERROR, "Could not send message, command line too long\n"); |
| 1394 |
res = -1; |
| 1395 |
goto out; |
| 1396 |
} |
| 1397 |
|
| 1398 |
ast_safe_system(mail_cmd_buffer); |
| 1399 |
ast_debug(1, "Sent message to %s with command '%s'%s\n", vmu->email, global_mailcmd, template->attachment ? " - (media attachment)" : ""); |
| 1400 |
ast_debug(3, "Actual command used: %s\n", mail_cmd_buffer); |
| 1401 |
|
| 1402 |
res = 0; |
| 1403 |
|
| 1404 |
out: |
| 1405 |
if (file_to_delete) { |
| 1406 |
unlink(file_to_delete); |
| 1407 |
} |
| 1408 |
|
| 1409 |
if (dir_to_delete) { |
| 1410 |
rmdir(dir_to_delete); |
| 1411 |
} |
| 1412 |
|
| 1413 |
return res; |
| 1414 |
} |
| 1415 |
|
| 1416 |
|
| 1417 |
|
| 1418 |
static int make_dir(char *dest, int len, const char *domain, const char *username, const char *folder) |
| 1419 |
{ |
| 1420 |
return snprintf(dest, len, "%s%s/%s%s%s", MVM_SPOOL_DIR, domain, username, ast_strlen_zero(folder) ? "" : "/", folder ? folder : ""); |
| 1421 |
} |
| 1422 |
|
| 1423 |
|
| 1424 |
|
| 1425 |
|
| 1426 |
|
| 1427 |
|
| 1428 |
|
| 1429 |
|
| 1430 |
|
| 1431 |
|
| 1432 |
static int check_dirpath(char *dest, int len, char *domain, char *username, char *folder) |
| 1433 |
{ |
| 1434 |
struct stat filestat; |
| 1435 |
make_dir(dest, len, domain, username, folder ? folder : ""); |
| 1436 |
if (stat(dest, &filestat)== -1) |
| 1437 |
return FALSE; |
| 1438 |
else |
| 1439 |
return TRUE; |
| 1440 |
} |
| 1441 |
|
| 1442 |
|
| 1443 |
|
| 1444 |
|
| 1445 |
|
| 1446 |
|
| 1447 |
|
| 1448 |
|
| 1449 |
|
| 1450 |
|
| 1451 |
static int create_dirpath(char *dest, int len, char *domain, char *username, char *folder) |
| 1452 |
{ |
| 1453 |
int res; |
| 1454 |
make_dir(dest, len, domain, username, folder); |
| 1455 |
if ((res = ast_mkdir(dest, 0777))) { |
| 1456 |
ast_log(LOG_WARNING, "ast_mkdir '%s' failed: %s\n", dest, strerror(res)); |
| 1457 |
return -1; |
| 1458 |
} |
| 1459 |
ast_debug(2, "Creating directory for %s@%s folder %s : %s\n", username, domain, folder, dest); |
| 1460 |
return 0; |
| 1461 |
} |
| 1462 |
|
| 1463 |
|
| 1464 |
|
| 1465 |
|
| 1466 |
|
| 1467 |
static int invent_message(struct ast_channel *chan, char *domain, char *username, int busy, char *ecodes) |
| 1468 |
{ |
| 1469 |
int res; |
| 1470 |
char fn[PATH_MAX]; |
| 1471 |
|
| 1472 |
ast_debug(2, "Still preparing to play message ...\n"); |
| 1473 |
|
| 1474 |
snprintf(fn, sizeof(fn), "%s%s/%s/greet", MVM_SPOOL_DIR, domain, username); |
| 1475 |
|
| 1476 |
if (ast_fileexists(fn, NULL, NULL) > 0) { |
| 1477 |
res = ast_streamfile(chan, fn, ast_channel_language(chan)); |
| 1478 |
if (res) |
| 1479 |
return -1; |
| 1480 |
res = ast_waitstream(chan, ecodes); |
| 1481 |
if (res) |
| 1482 |
return res; |
| 1483 |
} else { |
| 1484 |
int numericusername = 1; |
| 1485 |
char *i = username; |
| 1486 |
|
| 1487 |
ast_debug(2, "No personal prompts. Using default prompt set for language\n"); |
| 1488 |
|
| 1489 |
while (*i) { |
| 1490 |
ast_debug(2, "Numeric? Checking %c\n", *i); |
| 1491 |
if (!isdigit(*i)) { |
| 1492 |
numericusername = FALSE; |
| 1493 |
break; |
| 1494 |
} |
| 1495 |
i++; |
| 1496 |
} |
| 1497 |
|
| 1498 |
if (numericusername) { |
| 1499 |
if (ast_streamfile(chan, "vm-theperson", ast_channel_language(chan))) |
| 1500 |
return -1; |
| 1501 |
if ((res = ast_waitstream(chan, ecodes))) |
| 1502 |
return res; |
| 1503 |
|
| 1504 |
res = ast_say_digit_str(chan, username, ecodes, ast_channel_language(chan)); |
| 1505 |
if (res) |
| 1506 |
return res; |
| 1507 |
} else { |
| 1508 |
if (ast_streamfile(chan, "vm-theextensionis", ast_channel_language(chan))) |
| 1509 |
return -1; |
| 1510 |
if ((res = ast_waitstream(chan, ecodes))) |
| 1511 |
return res; |
| 1512 |
} |
| 1513 |
} |
| 1514 |
|
| 1515 |
res = ast_streamfile(chan, busy ? "vm-isonphone" : "vm-isunavail", ast_channel_language(chan)); |
| 1516 |
if (res) |
| 1517 |
return -1; |
| 1518 |
res = ast_waitstream(chan, ecodes); |
| 1519 |
return res; |
| 1520 |
} |
| 1521 |
|
| 1522 |
|
| 1523 |
|
| 1524 |
static int vm_delete(char *file) |
| 1525 |
{ |
| 1526 |
int res; |
| 1527 |
|
| 1528 |
ast_debug(1, "Deleting voicemail file %s\n", file); |
| 1529 |
|
| 1530 |
res = unlink(file); |
| 1531 |
res |= ast_filedelete(file, NULL); |
| 1532 |
return res; |
| 1533 |
} |
| 1534 |
|
| 1535 |
|
| 1536 |
|
| 1537 |
|
| 1538 |
static int play_record_review(struct ast_channel *chan, char *playfile, char *recordfile, int maxtime, char *fmt, |
| 1539 |
int outsidecaller, struct minivm_account *vmu, int *duration, int *sound_duration, const char *unlockdir, |
| 1540 |
signed char record_gain) |
| 1541 |
{ |
| 1542 |
int cmd = 0; |
| 1543 |
int max_attempts = 3; |
| 1544 |
int attempts = 0; |
| 1545 |
int recorded = 0; |
| 1546 |
int message_exists = 0; |
| 1547 |
signed char zero_gain = 0; |
| 1548 |
char *acceptdtmf = "#"; |
| 1549 |
char *canceldtmf = ""; |
| 1550 |
|
| 1551 |
|
| 1552 |
|
| 1553 |
|
| 1554 |
if (duration == NULL) { |
| 1555 |
ast_log(LOG_WARNING, "Error play_record_review called without duration pointer\n"); |
| 1556 |
return -1; |
| 1557 |
} |
| 1558 |
|
| 1559 |
cmd = '3'; |
| 1560 |
|
| 1561 |
while ((cmd >= 0) && (cmd != 't')) { |
| 1562 |
switch (cmd) { |
| 1563 |
case '1': |
| 1564 |
ast_verb(3, "Saving message as is\n"); |
| 1565 |
ast_stream_and_wait(chan, "vm-msgsaved", ""); |
| 1566 |
cmd = 't'; |
| 1567 |
break; |
| 1568 |
case '2': |
| 1569 |
|
| 1570 |
ast_verb(3, "Reviewing the message\n"); |
| 1571 |
ast_streamfile(chan, recordfile, ast_channel_language(chan)); |
| 1572 |
cmd = ast_waitstream(chan, AST_DIGIT_ANY); |
| 1573 |
break; |
| 1574 |
case '3': |
| 1575 |
message_exists = 0; |
| 1576 |
|
| 1577 |
if (recorded == 1) |
| 1578 |
ast_verb(3, "Re-recording the message\n"); |
| 1579 |
else |
| 1580 |
ast_verb(3, "Recording the message\n"); |
| 1581 |
if (recorded && outsidecaller) |
| 1582 |
cmd = ast_play_and_wait(chan, "beep"); |
| 1583 |
recorded = 1; |
| 1584 |
|
| 1585 |
if (record_gain) |
| 1586 |
ast_channel_setoption(chan, AST_OPTION_RXGAIN, &record_gain, sizeof(record_gain), 0); |
| 1587 |
if (ast_test_flag(vmu, MVM_OPERATOR)) |
| 1588 |
canceldtmf = "0"; |
| 1589 |
cmd = ast_play_and_record_full(chan, playfile, recordfile, maxtime, fmt, duration, sound_duration, 0, global_silencethreshold, global_maxsilence, unlockdir, acceptdtmf, canceldtmf, 0, AST_RECORD_IF_EXISTS_OVERWRITE); |
| 1590 |
if (record_gain) |
| 1591 |
ast_channel_setoption(chan, AST_OPTION_RXGAIN, &zero_gain, sizeof(zero_gain), 0); |
| 1592 |
if (cmd == -1) |
| 1593 |
return cmd; |
| 1594 |
if (cmd == '0') |
| 1595 |
break; |
| 1596 |
else if (cmd == '*') |
| 1597 |
break; |
| 1598 |
else { |
| 1599 |
|
| 1600 |
message_exists = 1; |
| 1601 |
cmd = 0; |
| 1602 |
} |
| 1603 |
break; |
| 1604 |
case '4': |
| 1605 |
case '5': |
| 1606 |
case '6': |
| 1607 |
case '7': |
| 1608 |
case '8': |
| 1609 |
case '9': |
| 1610 |
case '*': |
| 1611 |
case '#': |
| 1612 |
cmd = ast_play_and_wait(chan, "vm-sorry"); |
| 1613 |
break; |
| 1614 |
case '0': |
| 1615 |
if(!ast_test_flag(vmu, MVM_OPERATOR)) { |
| 1616 |
cmd = ast_play_and_wait(chan, "vm-sorry"); |
| 1617 |
break; |
| 1618 |
} |
| 1619 |
if (message_exists || recorded) { |
| 1620 |
cmd = ast_play_and_wait(chan, "vm-saveoper"); |
| 1621 |
if (!cmd) |
| 1622 |
cmd = ast_waitfordigit(chan, 3000); |
| 1623 |
if (cmd == '1') { |
| 1624 |
ast_play_and_wait(chan, "vm-msgsaved"); |
| 1625 |
cmd = '0'; |
| 1626 |
} else { |
| 1627 |
ast_play_and_wait(chan, "vm-deleted"); |
| 1628 |
vm_delete(recordfile); |
| 1629 |
cmd = '0'; |
| 1630 |
} |
| 1631 |
} |
| 1632 |
return cmd; |
| 1633 |
default: |
| 1634 |
|
| 1635 |
|
| 1636 |
|
| 1637 |
if (outsidecaller && !ast_test_flag(vmu, MVM_REVIEW)) |
| 1638 |
return cmd; |
| 1639 |
if (message_exists) { |
| 1640 |
cmd = ast_play_and_wait(chan, "vm-review"); |
| 1641 |
} else { |
| 1642 |
cmd = ast_play_and_wait(chan, "vm-torerecord"); |
| 1643 |
if (!cmd) |
| 1644 |
cmd = ast_waitfordigit(chan, 600); |
| 1645 |
} |
| 1646 |
|
| 1647 |
if (!cmd && outsidecaller && ast_test_flag(vmu, MVM_OPERATOR)) { |
| 1648 |
cmd = ast_play_and_wait(chan, "vm-reachoper"); |
| 1649 |
if (!cmd) |
| 1650 |
cmd = ast_waitfordigit(chan, 600); |
| 1651 |
} |
| 1652 |
if (!cmd) |
| 1653 |
cmd = ast_waitfordigit(chan, 6000); |
| 1654 |
if (!cmd) { |
| 1655 |
attempts++; |
| 1656 |
} |
| 1657 |
if (attempts > max_attempts) { |
| 1658 |
cmd = 't'; |
| 1659 |
} |
| 1660 |
} |
| 1661 |
} |
| 1662 |
if (outsidecaller) |
| 1663 |
ast_play_and_wait(chan, "vm-goodbye"); |
| 1664 |
if (cmd == 't') |
| 1665 |
cmd = 0; |
| 1666 |
return cmd; |
| 1667 |
} |
| 1668 |
|
| 1669 |
|
| 1670 |
static void run_externnotify(struct ast_channel *chan, struct minivm_account *vmu) |
| 1671 |
{ |
| 1672 |
char fquser[AST_MAX_CONTEXT * 2]; |
| 1673 |
char *argv[5] = { NULL }; |
| 1674 |
struct ast_party_caller *caller; |
| 1675 |
char *cid; |
| 1676 |
int idx; |
| 1677 |
|
| 1678 |
if (ast_strlen_zero(vmu->externnotify) && ast_strlen_zero(global_externnotify)) { |
| 1679 |
return; |
| 1680 |
} |
| 1681 |
|
| 1682 |
snprintf(fquser, sizeof(fquser), "%s@%s", vmu->username, vmu->domain); |
| 1683 |
|
| 1684 |
caller = ast_channel_caller(chan); |
| 1685 |
idx = 0; |
| 1686 |
argv[idx++] = ast_strlen_zero(vmu->externnotify) ? global_externnotify : vmu->externnotify; |
| 1687 |
argv[idx++] = fquser; |
| 1688 |
cid = S_COR(caller->id.name.valid, caller->id.name.str, NULL); |
| 1689 |
if (cid) { |
| 1690 |
argv[idx++] = cid; |
| 1691 |
} |
| 1692 |
cid = S_COR(caller->id.number.valid, caller->id.number.str, NULL); |
| 1693 |
if (cid) { |
| 1694 |
argv[idx++] = cid; |
| 1695 |
} |
| 1696 |
argv[idx] = NULL; |
| 1697 |
|
| 1698 |
ast_debug(1, "Executing: %s %s %s %s\n", |
| 1699 |
argv[0], argv[1], argv[2] ?: "", argv[3] ?: ""); |
| 1700 |
ast_safe_execvp(1, argv[0], argv); |
| 1701 |
} |
| 1702 |
|
| 1703 |
|
| 1704 |
|
| 1705 |
static int notify_new_message(struct ast_channel *chan, const char *templatename, struct minivm_account *vmu, const char *filename, long duration, const char *format, char *cidnum, char *cidname) |
| 1706 |
{ |
| 1707 |
RAII_VAR(struct ast_json *, json_object, NULL, ast_json_unref); |
| 1708 |
RAII_VAR(struct stasis_message *, message, NULL, ao2_cleanup); |
| 1709 |
RAII_VAR(struct ast_mwi_state *, mwi_state, NULL, ao2_cleanup); |
| 1710 |
char *stringp; |
| 1711 |
struct minivm_template *etemplate; |
| 1712 |
char *messageformat; |
| 1713 |
int res = 0; |
| 1714 |
char oldlocale[100]; |
| 1715 |
const char *counter; |
| 1716 |
|
| 1717 |
if (!ast_strlen_zero(vmu->attachfmt)) { |
| 1718 |
if (strstr(format, vmu->attachfmt)) { |
| 1719 |
format = vmu->attachfmt; |
| 1720 |
} else { |
| 1721 |
ast_log(LOG_WARNING, "Attachment format '%s' is not one of the recorded formats '%s'. Falling back to default format for '%s@%s'.\n", vmu->attachfmt, format, vmu->username, vmu->domain); |
| 1722 |
} |
| 1723 |
} |
| 1724 |
|
| 1725 |
etemplate = message_template_find(vmu->etemplate); |
| 1726 |
if (!etemplate) |
| 1727 |
etemplate = message_template_find(templatename); |
| 1728 |
if (!etemplate) |
| 1729 |
etemplate = message_template_find("email-default"); |
| 1730 |
|
| 1731 |
|
| 1732 |
stringp = messageformat = ast_strdupa(format); |
| 1733 |
strsep(&stringp, "|"); |
| 1734 |
|
| 1735 |
if (!ast_strlen_zero(etemplate->locale)) { |
| 1736 |
char *new_locale; |
| 1737 |
ast_copy_string(oldlocale, setlocale(LC_TIME, NULL), sizeof(oldlocale)); |
| 1738 |
ast_debug(2, "Changing locale from %s to %s\n", oldlocale, etemplate->locale); |
| 1739 |
new_locale = setlocale(LC_TIME, etemplate->locale); |
| 1740 |
if (new_locale == NULL) { |
| 1741 |
ast_log(LOG_WARNING, "-_-_- Changing to new locale did not work. Locale: %s\n", etemplate->locale); |
| 1742 |
} |
| 1743 |
} |
| 1744 |
|
| 1745 |
|
| 1746 |
|
| 1747 |
|
| 1748 |
ast_channel_lock(chan); |
| 1749 |
if ((counter = pbx_builtin_getvar_helper(chan, "MVM_COUNTER"))) { |
| 1750 |
counter = ast_strdupa(counter); |
| 1751 |
} |
| 1752 |
ast_channel_unlock(chan); |
| 1753 |
|
| 1754 |
if (ast_strlen_zero(counter)) { |
| 1755 |
ast_debug(2, "MVM_COUNTER not found\n"); |
| 1756 |
} else { |
| 1757 |
ast_debug(2, "MVM_COUNTER found - will use it with value %s\n", counter); |
| 1758 |
} |
| 1759 |
|
| 1760 |
res = sendmail(etemplate, vmu, cidnum, cidname, filename, messageformat, duration, etemplate->attachment, MVM_MESSAGE_EMAIL, counter); |
| 1761 |
|
| 1762 |
if (res == 0 && !ast_strlen_zero(vmu->pager)) { |
| 1763 |
|
| 1764 |
etemplate = message_template_find(vmu->ptemplate); |
| 1765 |
if (!etemplate) |
| 1766 |
etemplate = message_template_find("pager-default"); |
| 1767 |
|
| 1768 |
if (!ast_strlen_zero(etemplate->locale)) { |
| 1769 |
ast_copy_string(oldlocale, setlocale(LC_TIME, ""), sizeof(oldlocale)); |
| 1770 |
setlocale(LC_TIME, etemplate->locale); |
| 1771 |
} |
| 1772 |
|
| 1773 |
res = sendmail(etemplate, vmu, cidnum, cidname, filename, messageformat, duration, etemplate->attachment, MVM_MESSAGE_PAGE, counter); |
| 1774 |
} |
| 1775 |
|
| 1776 |
mwi_state = ast_mwi_create(vmu->username, vmu->domain); |
| 1777 |
if (!mwi_state) { |
| 1778 |
goto notify_cleanup; |
| 1779 |
} |
| 1780 |
mwi_state->snapshot = ast_channel_snapshot_get_latest(ast_channel_uniqueid(chan)); |
| 1781 |
|
| 1782 |
json_object = ast_json_pack("{s: s, s: s, s: s}", |
| 1783 |
"Event", "MiniVoiceMail", |
| 1784 |
"Action", "SentNotification", |
| 1785 |
"Counter", counter ?: ""); |
| 1786 |
if (!json_object) { |
| 1787 |
goto notify_cleanup; |
| 1788 |
} |
| 1789 |
message = ast_mwi_blob_create(mwi_state, ast_mwi_vm_app_type(), json_object); |
| 1790 |
if (!message) { |
| 1791 |
goto notify_cleanup; |
| 1792 |
} |
| 1793 |
stasis_publish(ast_mwi_topic(mwi_state->uniqueid), message); |
| 1794 |
|
| 1795 |
notify_cleanup: |
| 1796 |
run_externnotify(chan, vmu); |
| 1797 |
if (!ast_strlen_zero(etemplate->locale)) { |
| 1798 |
setlocale(LC_TIME, oldlocale); |
| 1799 |
} |
| 1800 |
return res; |
| 1801 |
} |
| 1802 |
|
| 1803 |
|
| 1804 |
|
| 1805 |
|
| 1806 |
static int leave_voicemail(struct ast_channel *chan, char *username, struct leave_vm_options *options) |
| 1807 |
{ |
| 1808 |
char tmptxtfile[PATH_MAX]; |
| 1809 |
char callerid[256]; |
| 1810 |
FILE *txt; |
| 1811 |
int res = 0, txtdes; |
| 1812 |
int duration = 0; |
| 1813 |
int sound_duration = 0; |
| 1814 |
char date[256]; |
| 1815 |
char tmpdir[PATH_MAX]; |
| 1816 |
char ext_context[256] = ""; |
| 1817 |
char fmt[80]; |
| 1818 |
char *domain; |
| 1819 |
char tmp[256] = ""; |
| 1820 |
struct minivm_account *vmu; |
| 1821 |
int userdir; |
| 1822 |
|
| 1823 |
ast_copy_string(tmp, username, sizeof(tmp)); |
| 1824 |
username = tmp; |
| 1825 |
domain = strchr(tmp, '@'); |
| 1826 |
if (domain) { |
| 1827 |
*domain = '\0'; |
| 1828 |
domain++; |
| 1829 |
} |
| 1830 |
|
| 1831 |
if (!(vmu = find_account(domain, username, TRUE))) { |
| 1832 |
|
| 1833 |
ast_log(LOG_ERROR, "Can't allocate temporary account for '%s@%s'\n", username, domain); |
| 1834 |
pbx_builtin_setvar_helper(chan, "MVM_RECORD_STATUS", "FAILED"); |
| 1835 |
return 0; |
| 1836 |
} |
| 1837 |
|
| 1838 |
|
| 1839 |
if (strcmp(vmu->domain, "localhost")) |
| 1840 |
snprintf(ext_context, sizeof(ext_context), "%s@%s", username, vmu->domain); |
| 1841 |
else |
| 1842 |
ast_copy_string(ext_context, vmu->domain, sizeof(ext_context)); |
| 1843 |
|
| 1844 |
|
| 1845 |
if (ast_strlen_zero(vmu->attachfmt)) |
| 1846 |
ast_copy_string(fmt, default_vmformat, sizeof(fmt)); |
| 1847 |
else |
| 1848 |
ast_copy_string(fmt, vmu->attachfmt, sizeof(fmt)); |
| 1849 |
|
| 1850 |
if (ast_strlen_zero(fmt)) { |
| 1851 |
ast_log(LOG_WARNING, "No format for saving voicemail? Default %s\n", default_vmformat); |
| 1852 |
pbx_builtin_setvar_helper(chan, "MVM_RECORD_STATUS", "FAILED"); |
| 1853 |
return res; |
| 1854 |
} |
| 1855 |
|
| 1856 |
userdir = check_dirpath(tmpdir, sizeof(tmpdir), vmu->domain, username, "tmp"); |
| 1857 |
|
| 1858 |
|
| 1859 |
if (!userdir) { |
| 1860 |
create_dirpath(tmpdir, sizeof(tmpdir), "0000_minivm_temp", "mediafiles", ""); |
| 1861 |
ast_debug(3, "Creating temporary directory %s\n", tmpdir); |
| 1862 |
} |
| 1863 |
|
| 1864 |
|
| 1865 |
snprintf(tmptxtfile, sizeof(tmptxtfile), "%s/XXXXXX", tmpdir); |
| 1866 |
|
| 1867 |
|
| 1868 |
txtdes = mkstemp(tmptxtfile); |
| 1869 |
if (txtdes < 0) { |
| 1870 |
ast_log(LOG_ERROR, "Unable to create message file %s: %s\n", tmptxtfile, strerror(errno)); |
| 1871 |
res = ast_streamfile(chan, "vm-mailboxfull", ast_channel_language(chan)); |
| 1872 |
if (!res) |
| 1873 |
res = ast_waitstream(chan, ""); |
| 1874 |
pbx_builtin_setvar_helper(chan, "MVM_RECORD_STATUS", "FAILED"); |
| 1875 |
return res; |
| 1876 |
} |
| 1877 |
|
| 1878 |
if (res >= 0) { |
| 1879 |
|
| 1880 |
res = ast_streamfile(chan, "beep", ast_channel_language(chan)); |
| 1881 |
if (!res) |
| 1882 |
res = ast_waitstream(chan, ""); |
| 1883 |
} |
| 1884 |
|
| 1885 |
|
| 1886 |
|
| 1887 |
ast_debug(2, "Open file for metadata: %s\n", tmptxtfile); |
| 1888 |
|
| 1889 |
res = play_record_review(chan, NULL, tmptxtfile, global_vmmaxmessage, fmt, 1, vmu, &duration, &sound_duration, NULL, options->record_gain); |
| 1890 |
|
| 1891 |
txt = fdopen(txtdes, "w+"); |
| 1892 |
if (!txt) { |
| 1893 |
ast_log(LOG_WARNING, "Error opening text file for output\n"); |
| 1894 |
} else { |
| 1895 |
struct ast_tm tm; |
| 1896 |
struct timeval now = ast_tvnow(); |
| 1897 |
char timebuf[30]; |
| 1898 |
char logbuf[BUFSIZ]; |
| 1899 |
get_date(date, sizeof(date)); |
| 1900 |
ast_localtime(&now, &tm, NULL); |
| 1901 |
ast_strftime(timebuf, sizeof(timebuf), "%H:%M:%S", &tm); |
| 1902 |
|
| 1903 |
ast_callerid_merge(callerid, sizeof(callerid), |
| 1904 |
S_COR(ast_channel_caller(chan)->id.name.valid, ast_channel_caller(chan)->id.name.str, NULL), |
| 1905 |
S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL), |
| 1906 |
"Unknown"); |
| 1907 |
snprintf(logbuf, sizeof(logbuf), |
| 1908 |
|
| 1909 |
"%s:%s:%s:%d:%s:%s:%s:%s:%d:%s:%s\n", |
| 1910 |
username, |
| 1911 |
ast_channel_context(chan), |
| 1912 |
ast_channel_exten(chan), |
| 1913 |
ast_channel_priority(chan), |
| 1914 |
ast_channel_name(chan), |
| 1915 |
callerid, |
| 1916 |
date, |
| 1917 |
timebuf, |
| 1918 |
duration, |
| 1919 |
duration < global_vmminmessage ? "IGNORED" : "OK", |
| 1920 |
vmu->accountcode |
| 1921 |
); |
| 1922 |
fprintf(txt, "%s", logbuf); |
| 1923 |
if (minivmlogfile) { |
| 1924 |
ast_mutex_lock(&minivmloglock); |
| 1925 |
fprintf(minivmlogfile, "%s", logbuf); |
| 1926 |
ast_mutex_unlock(&minivmloglock); |
| 1927 |
} |
| 1928 |
|
| 1929 |
if (sound_duration < global_vmminmessage) { |
| 1930 |
ast_verb(3, "Recording was %d seconds long but needs to be at least %d - abandoning\n", sound_duration, global_vmminmessage); |
| 1931 |
fclose(txt); |
| 1932 |
ast_filedelete(tmptxtfile, NULL); |
| 1933 |
unlink(tmptxtfile); |
| 1934 |
pbx_builtin_setvar_helper(chan, "MVM_RECORD_STATUS", "FAILED"); |
| 1935 |
return 0; |
| 1936 |
} |
| 1937 |
fclose(txt); |
| 1938 |
if (ast_fileexists(tmptxtfile, NULL, NULL) <= 0) { |
| 1939 |
ast_debug(1, "The recorded media file is gone, so we should remove the .txt file too!\n"); |
| 1940 |
unlink(tmptxtfile); |
| 1941 |
pbx_builtin_setvar_helper(chan, "MVM_RECORD_STATUS", "FAILED"); |
| 1942 |
if(ast_test_flag(vmu, MVM_ALLOCED)) |
| 1943 |
free_user(vmu); |
| 1944 |
return 0; |
| 1945 |
} |
| 1946 |
|
| 1947 |
|
| 1948 |
pbx_builtin_setvar_helper(chan, "MVM_FILENAME", tmptxtfile); |
| 1949 |
snprintf(timebuf, sizeof(timebuf), "%d", duration); |
| 1950 |
pbx_builtin_setvar_helper(chan, "MVM_DURATION", timebuf); |
| 1951 |
pbx_builtin_setvar_helper(chan, "MVM_FORMAT", fmt); |
| 1952 |
|
| 1953 |
} |
| 1954 |
global_stats.lastreceived = ast_tvnow(); |
| 1955 |
global_stats.receivedmessages++; |
| 1956 |
#if 0 |
| 1957 |
|
| 1958 |
if (ast_fileexists(tmptxtfile, NULL, NULL) <= 0) { |
| 1959 |
ast_filedelete(tmptxtfile, NULL); |
| 1960 |
|
| 1961 |
ast_debug(2, "-_-_- Deleted audio file after notification :: %s \n", tmptxtfile); |
| 1962 |
} |
| 1963 |
#endif |
| 1964 |
|
| 1965 |
if (res > 0) |
| 1966 |
res = 0; |
| 1967 |
|
| 1968 |
if(ast_test_flag(vmu, MVM_ALLOCED)) |
| 1969 |
free_user(vmu); |
| 1970 |
|
| 1971 |
pbx_builtin_setvar_helper(chan, "MVM_RECORD_STATUS", "SUCCESS"); |
| 1972 |
return res; |
| 1973 |
} |
| 1974 |
|
| 1975 |
|
| 1976 |
|
| 1977 |
static void queue_mwi_event(const char *channel_id, const char *mbx, const char *ctx, int urgent, int new, int old) |
| 1978 |
{ |
| 1979 |
char *mailbox, *context; |
| 1980 |
|
| 1981 |
mailbox = ast_strdupa(mbx); |
| 1982 |
context = ast_strdupa(ctx); |
| 1983 |
if (ast_strlen_zero(context)) { |
| 1984 |
context = "default"; |
| 1985 |
} |
| 1986 |
|
| 1987 |
ast_publish_mwi_state_channel(mailbox, context, new + urgent, old, channel_id); |
| 1988 |
} |
| 1989 |
|
| 1990 |
|
| 1991 |
|
| 1992 |
static int minivm_mwi_exec(struct ast_channel *chan, const char *data) |
| 1993 |
{ |
| 1994 |
int argc; |
| 1995 |
char *argv[4]; |
| 1996 |
int res = 0; |
| 1997 |
char *tmpptr; |
| 1998 |
char tmp[PATH_MAX]; |
| 1999 |
char *mailbox; |
| 2000 |
char *domain; |
| 2001 |
if (ast_strlen_zero(data)) { |
| 2002 |
ast_log(LOG_ERROR, "Minivm needs at least an account argument \n"); |
| 2003 |
return -1; |
| 2004 |
} |
| 2005 |
tmpptr = ast_strdupa((char *)data); |
| 2006 |
argc = ast_app_separate_args(tmpptr, ',', argv, ARRAY_LEN(argv)); |
| 2007 |
if (argc < 4) { |
| 2008 |
ast_log(LOG_ERROR, "%d arguments passed to MiniVM_MWI, need 4.\n", argc); |
| 2009 |
return -1; |
| 2010 |
} |
| 2011 |
ast_copy_string(tmp, argv[0], sizeof(tmp)); |
| 2012 |
mailbox = tmp; |
| 2013 |
domain = strchr(tmp, '@'); |
| 2014 |
if (domain) { |
| 2015 |
*domain = '\0'; |
| 2016 |
domain++; |
| 2017 |
} |
| 2018 |
if (ast_strlen_zero(domain) || ast_strlen_zero(mailbox)) { |
| 2019 |
ast_log(LOG_ERROR, "Need mailbox@context as argument. Sorry. Argument 0 %s\n", argv[0]); |
| 2020 |
return -1; |
| 2021 |
} |
| 2022 |
queue_mwi_event(ast_channel_uniqueid(chan), mailbox, domain, atoi(argv[1]), atoi(argv[2]), atoi(argv[3])); |
| 2023 |
|
| 2024 |
return res; |
| 2025 |
} |
| 2026 |
|
| 2027 |
|
| 2028 |
|
| 2029 |
|
| 2030 |
static int minivm_notify_exec(struct ast_channel *chan, const char *data) |
| 2031 |
{ |
| 2032 |
int argc; |
| 2033 |
char *argv[2]; |
| 2034 |
int res = 0; |
| 2035 |
char tmp[PATH_MAX]; |
| 2036 |
char *domain; |
| 2037 |
char *tmpptr; |
| 2038 |
struct minivm_account *vmu; |
| 2039 |
char *username; |
| 2040 |
const char *template = ""; |
| 2041 |
const char *filename; |
| 2042 |
const char *format; |
| 2043 |
const char *duration_string; |
| 2044 |
if (ast_strlen_zero(data)) { |
| 2045 |
ast_log(LOG_ERROR, "Minivm needs at least an account argument \n"); |
| 2046 |
return -1; |
| 2047 |
} |
| 2048 |
tmpptr = ast_strdupa((char *)data); |
| 2049 |
argc = ast_app_separate_args(tmpptr, ',', argv, ARRAY_LEN(argv)); |
| 2050 |
|
| 2051 |
if (argc == 2 && !ast_strlen_zero(argv[1])) |
| 2052 |
template = argv[1]; |
| 2053 |
|
| 2054 |
ast_copy_string(tmp, argv[0], sizeof(tmp)); |
| 2055 |
username = tmp; |
| 2056 |
domain = strchr(tmp, '@'); |
| 2057 |
if (domain) { |
| 2058 |
*domain = '\0'; |
| 2059 |
domain++; |
| 2060 |
} |
| 2061 |
if (ast_strlen_zero(domain) || ast_strlen_zero(username)) { |
| 2062 |
ast_log(LOG_ERROR, "Need username@domain as argument. Sorry. Argument 0 %s\n", argv[0]); |
| 2063 |
return -1; |
| 2064 |
} |
| 2065 |
|
| 2066 |
if(!(vmu = find_account(domain, username, TRUE))) { |
| 2067 |
|
| 2068 |
ast_log(LOG_WARNING, "Could not allocate temporary memory for '%s@%s'\n", username, domain); |
| 2069 |
pbx_builtin_setvar_helper(chan, "MVM_NOTIFY_STATUS", "FAILED"); |
| 2070 |
return -1; |
| 2071 |
} |
| 2072 |
|
| 2073 |
ast_channel_lock(chan); |
| 2074 |
if ((filename = pbx_builtin_getvar_helper(chan, "MVM_FILENAME"))) { |
| 2075 |
filename = ast_strdupa(filename); |
| 2076 |
} |
| 2077 |
ast_channel_unlock(chan); |
| 2078 |
|
| 2079 |
if (!ast_strlen_zero(filename)) { |
| 2080 |
ast_channel_lock(chan); |
| 2081 |
if ((format = pbx_builtin_getvar_helper(chan, "MVM_FORMAT"))) { |
| 2082 |
format = ast_strdupa(format); |
| 2083 |
} |
| 2084 |
if ((duration_string = pbx_builtin_getvar_helper(chan, "MVM_DURATION"))) { |
| 2085 |
duration_string = ast_strdupa(duration_string); |
| 2086 |
} |
| 2087 |
ast_channel_unlock(chan); |
| 2088 |
res = notify_new_message(chan, template, vmu, filename, atoi(duration_string), |
| 2089 |
format, |
| 2090 |
S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL), |
| 2091 |
S_COR(ast_channel_caller(chan)->id.name.valid, ast_channel_caller(chan)->id.name.str, NULL)); |
| 2092 |
} |
| 2093 |
|
| 2094 |
pbx_builtin_setvar_helper(chan, "MVM_NOTIFY_STATUS", res == 0 ? "SUCCESS" : "FAILED"); |
| 2095 |
|
| 2096 |
|
| 2097 |
if(ast_test_flag(vmu, MVM_ALLOCED)) |
| 2098 |
free_user(vmu); |
| 2099 |
|
| 2100 |
|
| 2101 |
|
| 2102 |
return res; |
| 2103 |
|
| 2104 |
} |
| 2105 |
|
| 2106 |
|
| 2107 |
|
| 2108 |
static int minivm_record_exec(struct ast_channel *chan, const char *data) |
| 2109 |
{ |
| 2110 |
int res = 0; |
| 2111 |
char *tmp; |
| 2112 |
struct leave_vm_options leave_options; |
| 2113 |
int argc; |
| 2114 |
char *argv[2]; |
| 2115 |
struct ast_flags flags = { 0 }; |
| 2116 |
char *opts[OPT_ARG_ARRAY_SIZE]; |
| 2117 |
|
| 2118 |
memset(&leave_options, 0, sizeof(leave_options)); |
| 2119 |
|
| 2120 |
|
| 2121 |
if (ast_channel_state(chan) != AST_STATE_UP) |
| 2122 |
ast_answer(chan); |
| 2123 |
|
| 2124 |
if (ast_strlen_zero(data)) { |
| 2125 |
ast_log(LOG_ERROR, "Minivm needs at least an account argument \n"); |
| 2126 |
return -1; |
| 2127 |
} |
| 2128 |
tmp = ast_strdupa((char *)data); |
| 2129 |
argc = ast_app_separate_args(tmp, ',', argv, ARRAY_LEN(argv)); |
| 2130 |
if (argc == 2) { |
| 2131 |
if (ast_app_parse_options(minivm_app_options, &flags, opts, argv[1])) { |
| 2132 |
return -1; |
| 2133 |
} |
| 2134 |
ast_copy_flags(&leave_options, &flags, OPT_SILENT | OPT_BUSY_GREETING | OPT_UNAVAIL_GREETING ); |
| 2135 |
if (ast_test_flag(&flags, OPT_RECORDGAIN)) { |
| 2136 |
int gain; |
| 2137 |
|
| 2138 |
if (sscanf(opts[OPT_ARG_RECORDGAIN], "%30d", &gain) != 1) { |
| 2139 |
ast_log(LOG_WARNING, "Invalid value '%s' provided for record gain option\n", opts[OPT_ARG_RECORDGAIN]); |
| 2140 |
return -1; |
| 2141 |
} else |
| 2142 |
leave_options.record_gain = (signed char) gain; |
| 2143 |
} |
| 2144 |
} |
| 2145 |
|
| 2146 |
|
| 2147 |
res = leave_voicemail(chan, argv[0], &leave_options); |
| 2148 |
|
| 2149 |
if (res == ERROR_LOCK_PATH) { |
| 2150 |
ast_log(LOG_ERROR, "Could not leave voicemail. The path is already locked.\n"); |
| 2151 |
pbx_builtin_setvar_helper(chan, "MVM_RECORD_STATUS", "FAILED"); |
| 2152 |
res = 0; |
| 2153 |
} |
| 2154 |
pbx_builtin_setvar_helper(chan, "MVM_RECORD_STATUS", "SUCCESS"); |
| 2155 |
|
| 2156 |
return res; |
| 2157 |
} |
| 2158 |
|
| 2159 |
|
| 2160 |
|
| 2161 |
static int minivm_greet_exec(struct ast_channel *chan, const char *data) |
| 2162 |
{ |
| 2163 |
struct leave_vm_options leave_options = { 0, '\0'}; |
| 2164 |
int argc; |
| 2165 |
char *argv[2]; |
| 2166 |
struct ast_flags flags = { 0 }; |
| 2167 |
char *opts[OPT_ARG_ARRAY_SIZE]; |
| 2168 |
int res = 0; |
| 2169 |
int ouseexten = 0; |
| 2170 |
char tmp[PATH_MAX]; |
| 2171 |
char dest[PATH_MAX]; |
| 2172 |
char prefile[PATH_MAX] = ""; |
| 2173 |
char tempfile[PATH_MAX] = ""; |
| 2174 |
char ext_context[256] = ""; |
| 2175 |
char *domain; |
| 2176 |
char ecodes[16] = "#"; |
| 2177 |
char *tmpptr; |
| 2178 |
struct minivm_account *vmu; |
| 2179 |
char *username; |
| 2180 |
|
| 2181 |
if (ast_strlen_zero(data)) { |
| 2182 |
ast_log(LOG_ERROR, "Minivm needs at least an account argument \n"); |
| 2183 |
return -1; |
| 2184 |
} |
| 2185 |
tmpptr = ast_strdupa((char *)data); |
| 2186 |
argc = ast_app_separate_args(tmpptr, ',', argv, ARRAY_LEN(argv)); |
| 2187 |
|
| 2188 |
if (argc == 2) { |
| 2189 |
if (ast_app_parse_options(minivm_app_options, &flags, opts, argv[1])) |
| 2190 |
return -1; |
| 2191 |
ast_copy_flags(&leave_options, &flags, OPT_SILENT | OPT_BUSY_GREETING | OPT_UNAVAIL_GREETING ); |
| 2192 |
} |
| 2193 |
|
| 2194 |
ast_copy_string(tmp, argv[0], sizeof(tmp)); |
| 2195 |
username = tmp; |
| 2196 |
domain = strchr(tmp, '@'); |
| 2197 |
if (domain) { |
| 2198 |
*domain = '\0'; |
| 2199 |
domain++; |
| 2200 |
} |
| 2201 |
if (ast_strlen_zero(domain) || ast_strlen_zero(username)) { |
| 2202 |
ast_log(LOG_ERROR, "Need username@domain as argument. Sorry. Argument: %s\n", argv[0]); |
| 2203 |
return -1; |
| 2204 |
} |
| 2205 |
ast_debug(1, "Trying to find configuration for user %s in domain %s\n", username, domain); |
| 2206 |
|
| 2207 |
if (!(vmu = find_account(domain, username, TRUE))) { |
| 2208 |
ast_log(LOG_ERROR, "Could not allocate memory. \n"); |
| 2209 |
return -1; |
| 2210 |
} |
| 2211 |
|
| 2212 |
|
| 2213 |
if (ast_channel_state(chan) != AST_STATE_UP) |
| 2214 |
ast_answer(chan); |
| 2215 |
|
| 2216 |
|
| 2217 |
if (strcmp(vmu->domain, "localhost")) |
| 2218 |
snprintf(ext_context, sizeof(ext_context), "%s@%s", username, vmu->domain); |
| 2219 |
else |
| 2220 |
ast_copy_string(ext_context, vmu->domain, sizeof(ext_context)); |
| 2221 |
|
| 2222 |
if (ast_test_flag(&leave_options, OPT_BUSY_GREETING)) { |
| 2223 |
res = check_dirpath(dest, sizeof(dest), vmu->domain, username, "busy"); |
| 2224 |
if (res) |
| 2225 |
snprintf(prefile, sizeof(prefile), "%s%s/%s/busy", MVM_SPOOL_DIR, vmu->domain, username); |
| 2226 |
} else if (ast_test_flag(&leave_options, OPT_UNAVAIL_GREETING)) { |
| 2227 |
res = check_dirpath(dest, sizeof(dest), vmu->domain, username, "unavail"); |
| 2228 |
if (res) |
| 2229 |
snprintf(prefile, sizeof(prefile), "%s%s/%s/unavail", MVM_SPOOL_DIR, vmu->domain, username); |
| 2230 |
} |
| 2231 |
|
| 2232 |
snprintf(tempfile, sizeof(tempfile), "%s%s/%s/temp", MVM_SPOOL_DIR, vmu->domain, username); |
| 2233 |
if (!(res = check_dirpath(dest, sizeof(dest), vmu->domain, username, "temp"))) { |
| 2234 |
ast_debug(2, "Temporary message directory does not exist, using default (%s)\n", tempfile); |
| 2235 |
ast_copy_string(prefile, tempfile, sizeof(prefile)); |
| 2236 |
} |
| 2237 |
ast_debug(2, "Preparing to play message ...\n"); |
| 2238 |
|
| 2239 |
|
| 2240 |
if (ast_test_flag(vmu, MVM_OPERATOR)) { |
| 2241 |
if (!ast_strlen_zero(vmu->exit)) { |
| 2242 |
if (ast_exists_extension(chan, vmu->exit, "o", 1, |
| 2243 |
S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL))) { |
| 2244 |
strncat(ecodes, "0", sizeof(ecodes) - strlen(ecodes) - 1); |
| 2245 |
ouseexten = 1; |
| 2246 |
} |
| 2247 |
} else if (ast_exists_extension(chan, ast_channel_context(chan), "o", 1, |
| 2248 |
S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL))) { |
| 2249 |
strncat(ecodes, "0", sizeof(ecodes) - strlen(ecodes) - 1); |
| 2250 |
ouseexten = 1; |
| 2251 |
} |
| 2252 |
} |
| 2253 |
|
| 2254 |
if (!ast_strlen_zero(vmu->exit)) { |
| 2255 |
if (ast_exists_extension(chan, vmu->exit, "a", 1, |
| 2256 |
S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL))) { |
| 2257 |
strncat(ecodes, "*", sizeof(ecodes) - strlen(ecodes) - 1); |
| 2258 |
} |
| 2259 |
} else if (ast_exists_extension(chan, ast_channel_context(chan), "a", 1, |
| 2260 |
S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL))) { |
| 2261 |
strncat(ecodes, "*", sizeof(ecodes) - strlen(ecodes) - 1); |
| 2262 |
} |
| 2263 |
|
| 2264 |
res = 0; |
| 2265 |
|
| 2266 |
if (!ast_strlen_zero(prefile)) { |
| 2267 |
if (ast_streamfile(chan, prefile, ast_channel_language(chan)) > -1) |
| 2268 |
res = ast_waitstream(chan, ecodes); |
| 2269 |
} else { |
| 2270 |
ast_debug(2, "%s doesn't exist, doing what we can\n", prefile); |
| 2271 |
res = invent_message(chan, vmu->domain, username, ast_test_flag(&leave_options, OPT_BUSY_GREETING), ecodes); |
| 2272 |
} |
| 2273 |
if (res < 0) { |
| 2274 |
ast_debug(2, "Hang up during prefile playback\n"); |
| 2275 |
pbx_builtin_setvar_helper(chan, "MVM_GREET_STATUS", "FAILED"); |
| 2276 |
if(ast_test_flag(vmu, MVM_ALLOCED)) |
| 2277 |
free_user(vmu); |
| 2278 |
return -1; |
| 2279 |
} |
| 2280 |
if (res == '#') { |
| 2281 |
|
| 2282 |
ast_set_flag(&leave_options, OPT_SILENT); |
| 2283 |
res = 0; |
| 2284 |
} |
| 2285 |
if (!res && !ast_test_flag(&leave_options, OPT_SILENT)) { |
| 2286 |
res = ast_streamfile(chan, SOUND_INTRO, ast_channel_language(chan)); |
| 2287 |
if (!res) |
| 2288 |
res = ast_waitstream(chan, ecodes); |
| 2289 |
if (res == '#') { |
| 2290 |
ast_set_flag(&leave_options, OPT_SILENT); |
| 2291 |
res = 0; |
| 2292 |
} |
| 2293 |
} |
| 2294 |
if (res > 0) |
| 2295 |
ast_stopstream(chan); |
| 2296 |
|
| 2297 |
|
| 2298 |
if (res == '*') { |
| 2299 |
ast_channel_exten_set(chan, "a"); |
| 2300 |
if (!ast_strlen_zero(vmu->exit)) { |
| 2301 |
ast_channel_context_set(chan, vmu->exit); |
| 2302 |
} |
| 2303 |
ast_channel_priority_set(chan, 0); |
| 2304 |
pbx_builtin_setvar_helper(chan, "MVM_GREET_STATUS", "USEREXIT"); |
| 2305 |
res = 0; |
| 2306 |
} else if (res == '0') { |
| 2307 |
if(ouseexten) { |
| 2308 |
ast_channel_exten_set(chan, "o"); |
| 2309 |
if (!ast_strlen_zero(vmu->exit)) { |
| 2310 |
ast_channel_context_set(chan, vmu->exit); |
| 2311 |
} |
| 2312 |
ast_play_and_wait(chan, "transfer"); |
| 2313 |
ast_channel_priority_set(chan, 0); |
| 2314 |
pbx_builtin_setvar_helper(chan, "MVM_GREET_STATUS", "USEREXIT"); |
| 2315 |
} |
| 2316 |
res = 0; |
| 2317 |
} else if (res < 0) { |
| 2318 |
pbx_builtin_setvar_helper(chan, "MVM_GREET_STATUS", "FAILED"); |
| 2319 |
res = -1; |
| 2320 |
} else |
| 2321 |
pbx_builtin_setvar_helper(chan, "MVM_GREET_STATUS", "SUCCESS"); |
| 2322 |
|
| 2323 |
if(ast_test_flag(vmu, MVM_ALLOCED)) |
| 2324 |
free_user(vmu); |
| 2325 |
|
| 2326 |
|
| 2327 |
|
| 2328 |
return res; |
| 2329 |
|
| 2330 |
} |
| 2331 |
|
| 2332 |
|
| 2333 |
|
| 2334 |
static int minivm_delete_exec(struct ast_channel *chan, const char *data) |
| 2335 |
{ |
| 2336 |
int res = 0; |
| 2337 |
char filename[BUFSIZ]; |
| 2338 |
|
| 2339 |
if (!ast_strlen_zero(data)) { |
| 2340 |
ast_copy_string(filename, (char *) data, sizeof(filename)); |
| 2341 |
} else { |
| 2342 |
ast_channel_lock(chan); |
| 2343 |
ast_copy_string(filename, pbx_builtin_getvar_helper(chan, "MVM_FILENAME"), sizeof(filename)); |
| 2344 |
ast_channel_unlock(chan); |
| 2345 |
} |
| 2346 |
|
| 2347 |
if (ast_strlen_zero(filename)) { |
| 2348 |
ast_log(LOG_ERROR, "No filename given in application arguments or channel variable MVM_FILENAME\n"); |
| 2349 |
return res; |
| 2350 |
} |
| 2351 |
|
| 2352 |
|
| 2353 |
|
| 2354 |
if (ast_fileexists(filename, NULL, NULL) > 0) { |
| 2355 |
res = vm_delete(filename); |
| 2356 |
if (res) { |
| 2357 |
ast_debug(2, "Can't delete file: %s\n", filename); |
| 2358 |
pbx_builtin_setvar_helper(chan, "MVM_DELETE_STATUS", "FAILED"); |
| 2359 |
} else { |
| 2360 |
ast_debug(2, "Deleted voicemail file :: %s \n", filename); |
| 2361 |
pbx_builtin_setvar_helper(chan, "MVM_DELETE_STATUS", "SUCCESS"); |
| 2362 |
} |
| 2363 |
} else { |
| 2364 |
ast_debug(2, "Filename does not exist: %s\n", filename); |
| 2365 |
pbx_builtin_setvar_helper(chan, "MVM_DELETE_STATUS", "FAILED"); |
| 2366 |
} |
| 2367 |
|
| 2368 |
return res; |
| 2369 |
} |
| 2370 |
|
| 2371 |
|
| 2372 |
static int minivm_accmess_exec(struct ast_channel *chan, const char *data) |
| 2373 |
{ |
| 2374 |
int argc = 0; |
| 2375 |
char *argv[2]; |
| 2376 |
char filename[PATH_MAX]; |
| 2377 |
char tmp[PATH_MAX]; |
| 2378 |
char *domain; |
| 2379 |
char *tmpptr = NULL; |
| 2380 |
struct minivm_account *vmu; |
| 2381 |
char *username; |
| 2382 |
struct ast_flags flags = { 0 }; |
| 2383 |
char *opts[OPT_ARG_ARRAY_SIZE]; |
| 2384 |
int error = FALSE; |
| 2385 |
char *message = NULL; |
| 2386 |
char *prompt = NULL; |
| 2387 |
int duration; |
| 2388 |
|
| 2389 |
if (ast_strlen_zero(data)) { |
| 2390 |
ast_log(LOG_ERROR, "MinivmAccmess needs at least two arguments: account and option\n"); |
| 2391 |
error = TRUE; |
| 2392 |
} else { |
| 2393 |
tmpptr = ast_strdupa((char *)data); |
| 2394 |
argc = ast_app_separate_args(tmpptr, ',', argv, ARRAY_LEN(argv)); |
| 2395 |
} |
| 2396 |
|
| 2397 |
if (argc <=1) { |
| 2398 |
ast_log(LOG_ERROR, "MinivmAccmess needs at least two arguments: account and option\n"); |
| 2399 |
error = TRUE; |
| 2400 |
} |
| 2401 |
if (!error && strlen(argv[1]) > 1) { |
| 2402 |
ast_log(LOG_ERROR, "MinivmAccmess can only handle one option at a time. Bad option string: %s\n", argv[1]); |
| 2403 |
error = TRUE; |
| 2404 |
} |
| 2405 |
|
| 2406 |
if (!error && ast_app_parse_options(minivm_accmess_options, &flags, opts, argv[1])) { |
| 2407 |
ast_log(LOG_ERROR, "Can't parse option %s\n", argv[1]); |
| 2408 |
error = TRUE; |
| 2409 |
} |
| 2410 |
|
| 2411 |
if (error) { |
| 2412 |
pbx_builtin_setvar_helper(chan, "MVM_ACCMESS_STATUS", "FAILED"); |
| 2413 |
return -1; |
| 2414 |
} |
| 2415 |
|
| 2416 |
ast_copy_string(tmp, argv[0], sizeof(tmp)); |
| 2417 |
username = tmp; |
| 2418 |
domain = strchr(tmp, '@'); |
| 2419 |
if (domain) { |
| 2420 |
*domain = '\0'; |
| 2421 |
domain++; |
| 2422 |
} |
| 2423 |
if (ast_strlen_zero(domain) || ast_strlen_zero(username)) { |
| 2424 |
ast_log(LOG_ERROR, "Need username@domain as argument. Sorry. Argument 0 %s\n", argv[0]); |
| 2425 |
pbx_builtin_setvar_helper(chan, "MVM_ACCMESS_STATUS", "FAILED"); |
| 2426 |
return -1; |
| 2427 |
} |
| 2428 |
|
| 2429 |
if(!(vmu = find_account(domain, username, TRUE))) { |
| 2430 |
|
| 2431 |
ast_log(LOG_WARNING, "Could not allocate temporary memory for '%s@%s'\n", username, domain); |
| 2432 |
pbx_builtin_setvar_helper(chan, "MVM_ACCMESS_STATUS", "FAILED"); |
| 2433 |
return -1; |
| 2434 |
} |
| 2435 |
|
| 2436 |
|
| 2437 |
if (ast_channel_state(chan) != AST_STATE_UP) |
| 2438 |
ast_answer(chan); |
| 2439 |
|
| 2440 |
|
| 2441 |
if (ast_test_flag(&flags, OPT_BUSY_GREETING)) { |
| 2442 |
message = "busy"; |
| 2443 |
prompt = "vm-rec-busy"; |
| 2444 |
} else if (ast_test_flag(&flags, OPT_UNAVAIL_GREETING)) { |
| 2445 |
message = "unavailable"; |
| 2446 |
prompt = "vm-rec-unv"; |
| 2447 |
} else if (ast_test_flag(&flags, OPT_TEMP_GREETING)) { |
| 2448 |
message = "temp"; |
| 2449 |
prompt = "vm-rec-temp"; |
| 2450 |
} else if (ast_test_flag(&flags, OPT_NAME_GREETING)) { |
| 2451 |
message = "greet"; |
| 2452 |
prompt = "vm-rec-name"; |
| 2453 |
} |
| 2454 |
snprintf(filename,sizeof(filename), "%s%s/%s/%s", MVM_SPOOL_DIR, vmu->domain, vmu->username, message); |
| 2455 |
|
| 2456 |
play_record_review(chan, prompt, filename, global_maxgreet, default_vmformat, 0, vmu, &duration, NULL, NULL, FALSE); |
| 2457 |
|
| 2458 |
ast_debug(1, "Recorded new %s message in %s (duration %d)\n", message, filename, duration); |
| 2459 |
|
| 2460 |
if(ast_test_flag(vmu, MVM_ALLOCED)) |
| 2461 |
free_user(vmu); |
| 2462 |
|
| 2463 |
pbx_builtin_setvar_helper(chan, "MVM_ACCMESS_STATUS", "SUCCESS"); |
| 2464 |
|
| 2465 |
|
| 2466 |
return 0; |
| 2467 |
} |
| 2468 |
|
| 2469 |
|
| 2470 |
static int create_vmaccount(char *name, struct ast_variable *var, int realtime) |
| 2471 |
{ |
| 2472 |
struct minivm_account *vmu; |
| 2473 |
char *domain; |
| 2474 |
char *username; |
| 2475 |
char accbuf[BUFSIZ]; |
| 2476 |
|
| 2477 |
ast_debug(3, "Creating %s account for [%s]\n", realtime ? "realtime" : "static", name); |
| 2478 |
|
| 2479 |
ast_copy_string(accbuf, name, sizeof(accbuf)); |
| 2480 |
username = accbuf; |
| 2481 |
domain = strchr(accbuf, '@'); |
| 2482 |
if (domain) { |
| 2483 |
*domain = '\0'; |
| 2484 |
domain++; |
| 2485 |
} |
| 2486 |
if (ast_strlen_zero(domain)) { |
| 2487 |
ast_log(LOG_ERROR, "No domain given for mini-voicemail account %s. Not configured.\n", name); |
| 2488 |
return 0; |
| 2489 |
} |
| 2490 |
|
| 2491 |
ast_debug(3, "Creating static account for user %s domain %s\n", username, domain); |
| 2492 |
|
| 2493 |
|
| 2494 |
vmu = ast_calloc(1, sizeof(*vmu)); |
| 2495 |
if (!vmu) |
| 2496 |
return 0; |
| 2497 |
|
| 2498 |
ast_copy_string(vmu->domain, domain, sizeof(vmu->domain)); |
| 2499 |
ast_copy_string(vmu->username, username, sizeof(vmu->username)); |
| 2500 |
|
| 2501 |
populate_defaults(vmu); |
| 2502 |
|
| 2503 |
ast_debug(3, "...Configuring account %s\n", name); |
| 2504 |
|
| 2505 |
while (var) { |
| 2506 |
ast_debug(3, "Configuring %s = \"%s\" for account %s\n", var->name, var->value, name); |
| 2507 |
if (!strcasecmp(var->name, "serveremail")) { |
| 2508 |
ast_copy_string(vmu->serveremail, var->value, sizeof(vmu->serveremail)); |
| 2509 |
} else if (!strcasecmp(var->name, "email")) { |
| 2510 |
ast_copy_string(vmu->email, var->value, sizeof(vmu->email)); |
| 2511 |
} else if (!strcasecmp(var->name, "accountcode")) { |
| 2512 |
ast_copy_string(vmu->accountcode, var->value, sizeof(vmu->accountcode)); |
| 2513 |
} else if (!strcasecmp(var->name, "pincode")) { |
| 2514 |
ast_copy_string(vmu->pincode, var->value, sizeof(vmu->pincode)); |
| 2515 |
} else if (!strcasecmp(var->name, "domain")) { |
| 2516 |
ast_copy_string(vmu->domain, var->value, sizeof(vmu->domain)); |
| 2517 |
} else if (!strcasecmp(var->name, "language")) { |
| 2518 |
ast_copy_string(vmu->language, var->value, sizeof(vmu->language)); |
| 2519 |
} else if (!strcasecmp(var->name, "timezone")) { |
| 2520 |
ast_copy_string(vmu->zonetag, var->value, sizeof(vmu->zonetag)); |
| 2521 |
} else if (!strcasecmp(var->name, "externnotify")) { |
| 2522 |
ast_copy_string(vmu->externnotify, var->value, sizeof(vmu->externnotify)); |
| 2523 |
} else if (!strcasecmp(var->name, "etemplate")) { |
| 2524 |
ast_copy_string(vmu->etemplate, var->value, sizeof(vmu->etemplate)); |
| 2525 |
} else if (!strcasecmp(var->name, "ptemplate")) { |
| 2526 |
ast_copy_string(vmu->ptemplate, var->value, sizeof(vmu->ptemplate)); |
| 2527 |
} else if (!strcasecmp(var->name, "fullname")) { |
| 2528 |
ast_copy_string(vmu->fullname, var->value, sizeof(vmu->fullname)); |
| 2529 |
} else if (!strcasecmp(var->name, "setvar")) { |
| 2530 |
char *varval; |
| 2531 |
char varname[strlen(var->value) + 1]; |
| 2532 |
struct ast_variable *tmpvar; |
| 2533 |
|
| 2534 |
strcpy(varname, var->value); |
| 2535 |
if ((varval = strchr(varname, '='))) { |
| 2536 |
*varval = '\0'; |
| 2537 |
varval++; |
| 2538 |
if ((tmpvar = ast_variable_new(varname, varval, ""))) { |
| 2539 |
tmpvar->next = vmu->chanvars; |
| 2540 |
vmu->chanvars = tmpvar; |
| 2541 |
} |
| 2542 |
} |
| 2543 |
} else if (!strcasecmp(var->name, "pager")) { |
| 2544 |
ast_copy_string(vmu->pager, var->value, sizeof(vmu->pager)); |
| 2545 |
} else if (!strcasecmp(var->name, "volgain")) { |
| 2546 |
sscanf(var->value, "%30lf", &vmu->volgain); |
| 2547 |
} else { |
| 2548 |
ast_log(LOG_ERROR, "Unknown configuration option for minivm account %s : %s\n", name, var->name); |
| 2549 |
} |
| 2550 |
var = var->next; |
| 2551 |
} |
| 2552 |
ast_debug(3, "...Linking account %s\n", name); |
| 2553 |
|
| 2554 |
AST_LIST_LOCK(&minivm_accounts); |
| 2555 |
AST_LIST_INSERT_TAIL(&minivm_accounts, vmu, list); |
| 2556 |
AST_LIST_UNLOCK(&minivm_accounts); |
| 2557 |
|
| 2558 |
global_stats.voicemailaccounts++; |
| 2559 |
|
| 2560 |
ast_debug(2, "MVM :: Created account %s@%s - tz %s etemplate %s %s\n", username, domain, ast_strlen_zero(vmu->zonetag) ? "" : vmu->zonetag, ast_strlen_zero(vmu->etemplate) ? "" : vmu->etemplate, realtime ? "(realtime)" : ""); |
| 2561 |
return 0; |
| 2562 |
} |
| 2563 |
|
| 2564 |
|
| 2565 |
static void free_zone(struct minivm_zone *z) |
| 2566 |
{ |
| 2567 |
ast_free(z); |
| 2568 |
} |
| 2569 |
|
| 2570 |
|
| 2571 |
static void timezone_destroy_list(void) |
| 2572 |
{ |
| 2573 |
struct minivm_zone *this; |
| 2574 |
|
| 2575 |
AST_LIST_LOCK(&minivm_zones); |
| 2576 |
while ((this = AST_LIST_REMOVE_HEAD(&minivm_zones, list))) |
| 2577 |
free_zone(this); |
| 2578 |
|
| 2579 |
AST_LIST_UNLOCK(&minivm_zones); |
| 2580 |
} |
| 2581 |
|
| 2582 |
|
| 2583 |
static int timezone_add(const char *zonename, const char *config) |
| 2584 |
{ |
| 2585 |
struct minivm_zone *newzone; |
| 2586 |
char *msg_format, *timezone_str; |
| 2587 |
|
| 2588 |
newzone = ast_calloc(1, sizeof(*newzone)); |
| 2589 |
if (newzone == NULL) |
| 2590 |
return 0; |
| 2591 |
|
| 2592 |
msg_format = ast_strdupa(config); |
| 2593 |
|
| 2594 |
timezone_str = strsep(&msg_format, "|"); |
| 2595 |
if (!msg_format) { |
| 2596 |
ast_log(LOG_WARNING, "Invalid timezone definition : %s\n", zonename); |
| 2597 |
ast_free(newzone); |
| 2598 |
return 0; |
| 2599 |
} |
| 2600 |
|
| 2601 |
ast_copy_string(newzone->name, zonename, sizeof(newzone->name)); |
| 2602 |
ast_copy_string(newzone->timezone, timezone_str, sizeof(newzone->timezone)); |
| 2603 |
ast_copy_string(newzone->msg_format, msg_format, sizeof(newzone->msg_format)); |
| 2604 |
|
| 2605 |
AST_LIST_LOCK(&minivm_zones); |
| 2606 |
AST_LIST_INSERT_TAIL(&minivm_zones, newzone, list); |
| 2607 |
AST_LIST_UNLOCK(&minivm_zones); |
| 2608 |
|
| 2609 |
global_stats.timezones++; |
| 2610 |
|
| 2611 |
return 0; |
| 2612 |
} |
| 2613 |
|
| 2614 |
|
| 2615 |
static char *message_template_parse_filebody(const char *filename) |
| 2616 |
{ |
| 2617 |
char buf[BUFSIZ * 6]; |
| 2618 |
char readbuf[BUFSIZ]; |
| 2619 |
char filenamebuf[BUFSIZ]; |
| 2620 |
char *writepos; |
| 2621 |
char *messagebody; |
| 2622 |
FILE *fi; |
| 2623 |
|
| 2624 |
if (ast_strlen_zero(filename)) |
| 2625 |
return NULL; |
| 2626 |
if (*filename == '/') |
| 2627 |
ast_copy_string(filenamebuf, filename, sizeof(filenamebuf)); |
| 2628 |
else |
| 2629 |
snprintf(filenamebuf, sizeof(filenamebuf), "%s/%s", ast_config_AST_CONFIG_DIR, filename); |
| 2630 |
|
| 2631 |
if (!(fi = fopen(filenamebuf, "r"))) { |
| 2632 |
ast_log(LOG_ERROR, "Can't read message template from file: %s\n", filenamebuf); |
| 2633 |
return NULL; |
| 2634 |
} |
| 2635 |
writepos = buf; |
| 2636 |
while (fgets(readbuf, sizeof(readbuf), fi)) { |
| 2637 |
if (writepos != buf) { |
| 2638 |
*writepos = '\n'; |
| 2639 |
writepos++; |
| 2640 |
} |
| 2641 |
ast_copy_string(writepos, readbuf, sizeof(buf) - (writepos - buf)); |
| 2642 |
writepos += strlen(readbuf) - 1; |
| 2643 |
} |
| 2644 |
fclose(fi); |
| 2645 |
messagebody = ast_calloc(1, strlen(buf + 1)); |
| 2646 |
ast_copy_string(messagebody, buf, strlen(buf) + 1); |
| 2647 |
ast_debug(4, "---> Size of allocation %d\n", (int) strlen(buf + 1) ); |
| 2648 |
ast_debug(4, "---> Done reading message template : \n%s\n---- END message template--- \n", messagebody); |
| 2649 |
|
| 2650 |
return messagebody; |
| 2651 |
} |
| 2652 |
|
| 2653 |
|
| 2654 |
static char *message_template_parse_emailbody(const char *configuration) |
| 2655 |
{ |
| 2656 |
char *tmpread, *tmpwrite; |
| 2657 |
char *emailbody = ast_strdup(configuration); |
| 2658 |
|
| 2659 |
|
| 2660 |
tmpread = tmpwrite = emailbody; |
| 2661 |
while ((tmpwrite = strchr(tmpread,'\\'))) { |
| 2662 |
int len = strlen("\n"); |
| 2663 |
switch (tmpwrite[1]) { |
| 2664 |
case 'n': |
| 2665 |
memmove(tmpwrite + len, tmpwrite + 2, strlen(tmpwrite + 2) + 1); |
| 2666 |
tmpwrite[0] = '\n'; |
| 2667 |
break; |
| 2668 |
case 't': |
| 2669 |
memmove(tmpwrite + len, tmpwrite + 2, strlen(tmpwrite + 2) + 1); |
| 2670 |
tmpwrite[0] = '\t'; |
| 2671 |
break; |
| 2672 |
default: |
| 2673 |
ast_log(LOG_NOTICE, "Substitution routine does not support this character: %c\n", tmpwrite[1]); |
| 2674 |
} |
| 2675 |
tmpread = tmpwrite + len; |
| 2676 |
} |
| 2677 |
return emailbody; |
| 2678 |
} |
| 2679 |
|
| 2680 |
|
| 2681 |
static int apply_general_options(struct ast_variable *var) |
| 2682 |
{ |
| 2683 |
int error = 0; |
| 2684 |
|
| 2685 |
while (var) { |
| 2686 |
|
| 2687 |
if (!strcmp(var->name, "mailcmd")) { |
| 2688 |
ast_copy_string(global_mailcmd, var->value, sizeof(global_mailcmd)); |
| 2689 |
} else if (!strcmp(var->name, "maxgreet")) { |
| 2690 |
global_maxgreet = atoi(var->value); |
| 2691 |
} else if (!strcmp(var->name, "maxsilence")) { |
| 2692 |
global_maxsilence = atoi(var->value); |
| 2693 |
if (global_maxsilence > 0) |
| 2694 |
global_maxsilence *= 1000; |
| 2695 |
} else if (!strcmp(var->name, "logfile")) { |
| 2696 |
if (!ast_strlen_zero(var->value) ) { |
| 2697 |
if(*(var->value) == '/') |
| 2698 |
ast_copy_string(global_logfile, var->value, sizeof(global_logfile)); |
| 2699 |
else |
| 2700 |
snprintf(global_logfile, sizeof(global_logfile), "%s/%s", ast_config_AST_LOG_DIR, var->value); |
| 2701 |
} |
| 2702 |
} else if (!strcmp(var->name, "externnotify")) { |
| 2703 |
|
| 2704 |
ast_copy_string(global_externnotify, var->value, sizeof(global_externnotify)); |
| 2705 |
} else if (!strcmp(var->name, "silencethreshold") || !strcmp(var->name, "silencetreshold")) { |
| 2706 |
|
| 2707 |
global_silencethreshold = atoi(var->value); |
| 2708 |
} else if (!strcmp(var->name, "maxmessage")) { |
| 2709 |
int x; |
| 2710 |
if (sscanf(var->value, "%30d", &x) == 1) { |
| 2711 |
global_vmmaxmessage = x; |
| 2712 |
} else { |
| 2713 |
error ++; |
| 2714 |
ast_log(LOG_WARNING, "Invalid max message time length\n"); |
| 2715 |
} |
| 2716 |
} else if (!strcmp(var->name, "minmessage")) { |
| 2717 |
int x; |
| 2718 |
if (sscanf(var->value, "%30d", &x) == 1) { |
| 2719 |
global_vmminmessage = x; |
| 2720 |
if (global_maxsilence <= global_vmminmessage) |
| 2721 |
ast_log(LOG_WARNING, "maxsilence should be less than minmessage or you may get empty messages\n"); |
| 2722 |
} else { |
| 2723 |
error ++; |
| 2724 |
ast_log(LOG_WARNING, "Invalid min message time length\n"); |
| 2725 |
} |
| 2726 |
} else if (!strcmp(var->name, "format")) { |
| 2727 |
ast_copy_string(default_vmformat, var->value, sizeof(default_vmformat)); |
| 2728 |
} else if (!strcmp(var->name, "review")) { |
| 2729 |
ast_set2_flag((&globalflags), ast_true(var->value), MVM_REVIEW); |
| 2730 |
} else if (!strcmp(var->name, "operator")) { |
| 2731 |
ast_set2_flag((&globalflags), ast_true(var->value), MVM_OPERATOR); |
| 2732 |
} |
| 2733 |
var = var->next; |
| 2734 |
} |
| 2735 |
return error; |
| 2736 |
} |
| 2737 |
|
| 2738 |
|
| 2739 |
static int load_config(int reload) |
| 2740 |
{ |
| 2741 |
struct ast_config *cfg; |
| 2742 |
struct ast_variable *var; |
| 2743 |
char *cat; |
| 2744 |
const char *chanvar; |
| 2745 |
int error = 0; |
| 2746 |
struct minivm_template *template; |
| 2747 |
struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 }; |
| 2748 |
|
| 2749 |
cfg = ast_config_load(VOICEMAIL_CONFIG, config_flags); |
| 2750 |
if (cfg == CONFIG_STATUS_FILEUNCHANGED) { |
| 2751 |
return 0; |
| 2752 |
} else if (cfg == CONFIG_STATUS_FILEINVALID) { |
| 2753 |
ast_log(LOG_ERROR, "Config file " VOICEMAIL_CONFIG " is in an invalid format. Aborting.\n"); |
| 2754 |
return 0; |
| 2755 |
} |
| 2756 |
|
| 2757 |
ast_mutex_lock(&minivmlock); |
| 2758 |
|
| 2759 |
|
| 2760 |
message_destroy_list(); |
| 2761 |
timezone_destroy_list(); |
| 2762 |
vmaccounts_destroy_list(); |
| 2763 |
ast_debug(2, "Destroyed memory objects...\n"); |
| 2764 |
|
| 2765 |
|
| 2766 |
global_externnotify[0] = '\0'; |
| 2767 |
global_logfile[0] = '\0'; |
| 2768 |
global_vmmaxmessage = 2000; |
| 2769 |
global_maxgreet = 2000; |
| 2770 |
global_vmminmessage = 0; |
| 2771 |
strcpy(global_mailcmd, SENDMAIL); |
| 2772 |
global_maxsilence = 0; |
| 2773 |
global_saydurationminfo = 2; |
| 2774 |
ast_copy_string(default_vmformat, "wav", sizeof(default_vmformat)); |
| 2775 |
ast_set2_flag((&globalflags), FALSE, MVM_REVIEW); |
| 2776 |
ast_set2_flag((&globalflags), FALSE, MVM_OPERATOR); |
| 2777 |
|
| 2778 |
memset(&global_stats, 0, sizeof(global_stats)); |
| 2779 |
global_stats.reset = ast_tvnow(); |
| 2780 |
|
| 2781 |
global_silencethreshold = ast_dsp_get_threshold_from_settings(THRESHOLD_SILENCE); |
| 2782 |
|
| 2783 |
|
| 2784 |
if (!cfg) { |
| 2785 |
ast_log(LOG_WARNING, "Failed to load configuration file. Module activated with default settings.\n"); |
| 2786 |
ast_mutex_unlock(&minivmlock); |
| 2787 |
return 0; |
| 2788 |
} |
| 2789 |
|
| 2790 |
ast_debug(2, "Loaded configuration file, now parsing\n"); |
| 2791 |
|
| 2792 |
|
| 2793 |
|
| 2794 |
cat = ast_category_browse(cfg, NULL); |
| 2795 |
while (cat) { |
| 2796 |
ast_debug(3, "Found configuration section [%s]\n", cat); |
| 2797 |
if (!strcasecmp(cat, "general")) { |
| 2798 |
|
| 2799 |
error += apply_general_options(ast_variable_browse(cfg, cat)); |
| 2800 |
} else if (!strncasecmp(cat, "template-", 9)) { |
| 2801 |
|
| 2802 |
char *name = cat + 9; |
| 2803 |
|
| 2804 |
|
| 2805 |
error += message_template_build(name, ast_variable_browse(cfg, cat)); |
| 2806 |
} else { |
| 2807 |
var = ast_variable_browse(cfg, cat); |
| 2808 |
if (!strcasecmp(cat, "zonemessages")) { |
| 2809 |
|
| 2810 |
while (var) { |
| 2811 |
timezone_add(var->name, var->value); |
| 2812 |
var = var->next; |
| 2813 |
} |
| 2814 |
} else { |
| 2815 |
|
| 2816 |
error += create_vmaccount(cat, var, FALSE); |
| 2817 |
} |
| 2818 |
} |
| 2819 |
|
| 2820 |
cat = ast_category_browse(cfg, cat); |
| 2821 |
} |
| 2822 |
|
| 2823 |
|
| 2824 |
message_template_build("email-default", NULL); |
| 2825 |
template = message_template_find("email-default"); |
| 2826 |
|
| 2827 |
|
| 2828 |
if ((chanvar = ast_variable_retrieve(cfg, "general", "emaildateformat"))) |
| 2829 |
ast_copy_string(template->dateformat, chanvar, sizeof(template->dateformat)); |
| 2830 |
if ((chanvar = ast_variable_retrieve(cfg, "general", "emailfromstring"))) |
| 2831 |
ast_copy_string(template->fromaddress, chanvar, sizeof(template->fromaddress)); |
| 2832 |
if ((chanvar = ast_variable_retrieve(cfg, "general", "emailaaddress"))) |
| 2833 |
ast_copy_string(template->serveremail, chanvar, sizeof(template->serveremail)); |
| 2834 |
if ((chanvar = ast_variable_retrieve(cfg, "general", "emailaddress"))) |
| 2835 |
ast_copy_string(template->serveremail, chanvar, sizeof(template->serveremail)); |
| 2836 |
if ((chanvar = ast_variable_retrieve(cfg, "general", "emailcharset"))) |
| 2837 |
ast_copy_string(template->charset, chanvar, sizeof(template->charset)); |
| 2838 |
if ((chanvar = ast_variable_retrieve(cfg, "general", "emailsubject"))) |
| 2839 |
ast_copy_string(template->subject, chanvar, sizeof(template->subject)); |
| 2840 |
if ((chanvar = ast_variable_retrieve(cfg, "general", "emailbody"))) |
| 2841 |
template->body = message_template_parse_emailbody(chanvar); |
| 2842 |
template->attachment = TRUE; |
| 2843 |
|
| 2844 |
message_template_build("pager-default", NULL); |
| 2845 |
template = message_template_find("pager-default"); |
| 2846 |
if ((chanvar = ast_variable_retrieve(cfg, "general", "pagerfromstring"))) |
| 2847 |
ast_copy_string(template->fromaddress, chanvar, sizeof(template->fromaddress)); |
| 2848 |
if ((chanvar = ast_variable_retrieve(cfg, "general", "pageraddress"))) |
| 2849 |
ast_copy_string(template->serveremail, chanvar, sizeof(template->serveremail)); |
| 2850 |
if ((chanvar = ast_variable_retrieve(cfg, "general", "pagercharset"))) |
| 2851 |
ast_copy_string(template->charset, chanvar, sizeof(template->charset)); |
| 2852 |
if ((chanvar = ast_variable_retrieve(cfg, "general", "pagersubject"))) |
| 2853 |
ast_copy_string(template->subject, chanvar,sizeof(template->subject)); |
| 2854 |
if ((chanvar = ast_variable_retrieve(cfg, "general", "pagerbody"))) |
| 2855 |
template->body = message_template_parse_emailbody(chanvar); |
| 2856 |
template->attachment = FALSE; |
| 2857 |
|
| 2858 |
if (error) |
| 2859 |
ast_log(LOG_ERROR, "--- A total of %d errors found in mini-voicemail configuration\n", error); |
| 2860 |
|
| 2861 |
ast_mutex_unlock(&minivmlock); |
| 2862 |
ast_config_destroy(cfg); |
| 2863 |
|
| 2864 |
|
| 2865 |
if(minivmlogfile) |
| 2866 |
fclose(minivmlogfile); |
| 2867 |
|
| 2868 |
|
| 2869 |
if(!ast_strlen_zero(global_logfile)) { |
| 2870 |
minivmlogfile = fopen(global_logfile, "a"); |
| 2871 |
if(!minivmlogfile) |
| 2872 |
ast_log(LOG_ERROR, "Failed to open minivm log file %s : %s\n", global_logfile, strerror(errno)); |
| 2873 |
if (minivmlogfile) |
| 2874 |
ast_debug(3, "Opened log file %s \n", global_logfile); |
| 2875 |
} |
| 2876 |
|
| 2877 |
return 0; |
| 2878 |
} |
| 2879 |
|
| 2880 |
|
| 2881 |
static char *handle_minivm_list_templates(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 2882 |
{ |
| 2883 |
struct minivm_template *this; |
| 2884 |
#define HVLT_OUTPUT_FORMAT "%-15s %-10s %-10s %-15.15s %-50s\n" |
| 2885 |
int count = 0; |
| 2886 |
|
| 2887 |
switch (cmd) { |
| 2888 |
case CLI_INIT: |
| 2889 |
e->command = "minivm list templates"; |
| 2890 |
e->usage = |
| 2891 |
"Usage: minivm list templates\n" |
| 2892 |
" Lists message templates for e-mail, paging and IM\n"; |
| 2893 |
return NULL; |
| 2894 |
case CLI_GENERATE: |
| 2895 |
return NULL; |
| 2896 |
} |
| 2897 |
|
| 2898 |
if (a->argc > 3) |
| 2899 |
return CLI_SHOWUSAGE; |
| 2900 |
|
| 2901 |
AST_LIST_LOCK(&message_templates); |
| 2902 |
if (AST_LIST_EMPTY(&message_templates)) { |
| 2903 |
ast_cli(a->fd, "There are no message templates defined\n"); |
| 2904 |
AST_LIST_UNLOCK(&message_templates); |
| 2905 |
return CLI_FAILURE; |
| 2906 |
} |
| 2907 |
ast_cli(a->fd, HVLT_OUTPUT_FORMAT, "Template name", "Charset", "Locale", "Attach media", "Subject"); |
| 2908 |
ast_cli(a->fd, HVLT_OUTPUT_FORMAT, "-------------", "-------", "------", "------------", "-------"); |
| 2909 |
AST_LIST_TRAVERSE(&message_templates, this, list) { |
| 2910 |
ast_cli(a->fd, HVLT_OUTPUT_FORMAT, this->name, |
| 2911 |
S_OR(this->charset, "-"), |
| 2912 |
S_OR(this->locale, "-"), |
| 2913 |
this->attachment ? "Yes" : "No", |
| 2914 |
S_OR(this->subject, "-")); |
| 2915 |
count++; |
| 2916 |
} |
| 2917 |
AST_LIST_UNLOCK(&message_templates); |
| 2918 |
ast_cli(a->fd, "\n * Total: %d minivoicemail message templates\n", count); |
| 2919 |
return CLI_SUCCESS; |
| 2920 |
} |
| 2921 |
|
| 2922 |
static char *complete_minivm_show_users(const char *line, const char *word, int pos, int state) |
| 2923 |
{ |
| 2924 |
int which = 0; |
| 2925 |
int wordlen; |
| 2926 |
struct minivm_account *vmu; |
| 2927 |
const char *domain = ""; |
| 2928 |
|
| 2929 |
|
| 2930 |
if (pos > 4) |
| 2931 |
return NULL; |
| 2932 |
wordlen = strlen(word); |
| 2933 |
AST_LIST_TRAVERSE(&minivm_accounts, vmu, list) { |
| 2934 |
if (!strncasecmp(word, vmu->domain, wordlen)) { |
| 2935 |
if (domain && strcmp(domain, vmu->domain) && ++which > state) |
| 2936 |
return ast_strdup(vmu->domain); |
| 2937 |
|
| 2938 |
domain = vmu->domain; |
| 2939 |
} |
| 2940 |
} |
| 2941 |
return NULL; |
| 2942 |
} |
| 2943 |
|
| 2944 |
|
| 2945 |
static char *handle_minivm_show_users(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 2946 |
{ |
| 2947 |
struct minivm_account *vmu; |
| 2948 |
#define HMSU_OUTPUT_FORMAT "%-23s %-15s %-15s %-10s %-10s %-50s\n" |
| 2949 |
int count = 0; |
| 2950 |
|
| 2951 |
switch (cmd) { |
| 2952 |
case CLI_INIT: |
| 2953 |
e->command = "minivm list accounts [for]"; |
| 2954 |
e->usage = |
| 2955 |
"Usage: minivm list accounts [for <domain>]\n" |
| 2956 |
" Lists all mailboxes currently set up\n"; |
| 2957 |
return NULL; |
| 2958 |
case CLI_GENERATE: |
| 2959 |
return complete_minivm_show_users(a->line, a->word, a->pos, a->n); |
| 2960 |
} |
| 2961 |
|
| 2962 |
if ((a->argc < 3) || (a->argc > 5) || (a->argc == 4)) |
| 2963 |
return CLI_SHOWUSAGE; |
| 2964 |
if ((a->argc == 5) && strcmp(a->argv[3],"for")) |
| 2965 |
return CLI_SHOWUSAGE; |
| 2966 |
|
| 2967 |
AST_LIST_LOCK(&minivm_accounts); |
| 2968 |
if (AST_LIST_EMPTY(&minivm_accounts)) { |
| 2969 |
ast_cli(a->fd, "There are no voicemail users currently defined\n"); |
| 2970 |
AST_LIST_UNLOCK(&minivm_accounts); |
| 2971 |
return CLI_FAILURE; |
| 2972 |
} |
| 2973 |
ast_cli(a->fd, HMSU_OUTPUT_FORMAT, "User", "E-Template", "P-template", "Zone", "Format", "Full name"); |
| 2974 |
ast_cli(a->fd, HMSU_OUTPUT_FORMAT, "----", "----------", "----------", "----", "------", "---------"); |
| 2975 |
AST_LIST_TRAVERSE(&minivm_accounts, vmu, list) { |
| 2976 |
char tmp[256] = ""; |
| 2977 |
if ((a->argc == 3) || ((a->argc == 5) && !strcmp(a->argv[4], vmu->domain))) { |
| 2978 |
count++; |
| 2979 |
snprintf(tmp, sizeof(tmp), "%s@%s", vmu->username, vmu->domain); |
| 2980 |
ast_cli(a->fd, HMSU_OUTPUT_FORMAT, tmp, S_OR(vmu->etemplate, "-"), |
| 2981 |
S_OR(vmu->ptemplate, "-"), |
| 2982 |
S_OR(vmu->zonetag, "-"), |
| 2983 |
S_OR(vmu->attachfmt, "-"), |
| 2984 |
vmu->fullname); |
| 2985 |
} |
| 2986 |
} |
| 2987 |
AST_LIST_UNLOCK(&minivm_accounts); |
| 2988 |
ast_cli(a->fd, "\n * Total: %d minivoicemail accounts\n", count); |
| 2989 |
return CLI_SUCCESS; |
| 2990 |
} |
| 2991 |
|
| 2992 |
|
| 2993 |
static char *handle_minivm_show_zones(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 2994 |
{ |
| 2995 |
struct minivm_zone *zone; |
| 2996 |
#define HMSZ_OUTPUT_FORMAT "%-15s %-20s %-45s\n" |
| 2997 |
char *res = CLI_SUCCESS; |
| 2998 |
|
| 2999 |
switch (cmd) { |
| 3000 |
case CLI_INIT: |
| 3001 |
e->command = "minivm list zones"; |
| 3002 |
e->usage = |
| 3003 |
"Usage: minivm list zones\n" |
| 3004 |
" Lists zone message formats\n"; |
| 3005 |
return NULL; |
| 3006 |
case CLI_GENERATE: |
| 3007 |
return NULL; |
| 3008 |
} |
| 3009 |
|
| 3010 |
if (a->argc != e->args) |
| 3011 |
return CLI_SHOWUSAGE; |
| 3012 |
|
| 3013 |
AST_LIST_LOCK(&minivm_zones); |
| 3014 |
if (!AST_LIST_EMPTY(&minivm_zones)) { |
| 3015 |
ast_cli(a->fd, HMSZ_OUTPUT_FORMAT, "Zone", "Timezone", "Message Format"); |
| 3016 |
ast_cli(a->fd, HMSZ_OUTPUT_FORMAT, "----", "--------", "--------------"); |
| 3017 |
AST_LIST_TRAVERSE(&minivm_zones, zone, list) { |
| 3018 |
ast_cli(a->fd, HMSZ_OUTPUT_FORMAT, zone->name, zone->timezone, zone->msg_format); |
| 3019 |
} |
| 3020 |
} else { |
| 3021 |
ast_cli(a->fd, "There are no voicemail zones currently defined\n"); |
| 3022 |
res = CLI_FAILURE; |
| 3023 |
} |
| 3024 |
AST_LIST_UNLOCK(&minivm_zones); |
| 3025 |
|
| 3026 |
return res; |
| 3027 |
} |
| 3028 |
|
| 3029 |
|
| 3030 |
static char *handle_minivm_show_settings(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 3031 |
{ |
| 3032 |
switch (cmd) { |
| 3033 |
case CLI_INIT: |
| 3034 |
e->command = "minivm show settings"; |
| 3035 |
e->usage = |
| 3036 |
"Usage: minivm show settings\n" |
| 3037 |
" Display Mini-Voicemail general settings\n"; |
| 3038 |
return NULL; |
| 3039 |
case CLI_GENERATE: |
| 3040 |
return NULL; |
| 3041 |
} |
| 3042 |
|
| 3043 |
ast_cli(a->fd, "* Mini-Voicemail general settings\n"); |
| 3044 |
ast_cli(a->fd, " -------------------------------\n"); |
| 3045 |
ast_cli(a->fd, "\n"); |
| 3046 |
ast_cli(a->fd, " Mail command (shell): %s\n", global_mailcmd); |
| 3047 |
ast_cli(a->fd, " Max silence: %d\n", global_maxsilence); |
| 3048 |
ast_cli(a->fd, " Silence threshold: %d\n", global_silencethreshold); |
| 3049 |
ast_cli(a->fd, " Max message length (secs): %d\n", global_vmmaxmessage); |
| 3050 |
ast_cli(a->fd, " Min message length (secs): %d\n", global_vmminmessage); |
| 3051 |
ast_cli(a->fd, " Default format: %s\n", default_vmformat); |
| 3052 |
ast_cli(a->fd, " Extern notify (shell): %s\n", global_externnotify); |
| 3053 |
ast_cli(a->fd, " Logfile: %s\n", global_logfile[0] ? global_logfile : "<disabled>"); |
| 3054 |
ast_cli(a->fd, " Operator exit: %s\n", ast_test_flag(&globalflags, MVM_OPERATOR) ? "Yes" : "No"); |
| 3055 |
ast_cli(a->fd, " Message review: %s\n", ast_test_flag(&globalflags, MVM_REVIEW) ? "Yes" : "No"); |
| 3056 |
|
| 3057 |
ast_cli(a->fd, "\n"); |
| 3058 |
return CLI_SUCCESS; |
| 3059 |
} |
| 3060 |
|
| 3061 |
|
| 3062 |
static char *handle_minivm_show_stats(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 3063 |
{ |
| 3064 |
struct ast_tm timebuf; |
| 3065 |
char buf[BUFSIZ]; |
| 3066 |
|
| 3067 |
switch (cmd) { |
| 3068 |
|
| 3069 |
case CLI_INIT: |
| 3070 |
e->command = "minivm show stats"; |
| 3071 |
e->usage = |
| 3072 |
"Usage: minivm show stats\n" |
| 3073 |
" Display Mini-Voicemail counters\n"; |
| 3074 |
return NULL; |
| 3075 |
case CLI_GENERATE: |
| 3076 |
return NULL; |
| 3077 |
} |
| 3078 |
|
| 3079 |
ast_cli(a->fd, "* Mini-Voicemail statistics\n"); |
| 3080 |
ast_cli(a->fd, " -------------------------\n"); |
| 3081 |
ast_cli(a->fd, "\n"); |
| 3082 |
ast_cli(a->fd, " Voicemail accounts: %5d\n", global_stats.voicemailaccounts); |
| 3083 |
ast_cli(a->fd, " Templates: %5d\n", global_stats.templates); |
| 3084 |
ast_cli(a->fd, " Timezones: %5d\n", global_stats.timezones); |
| 3085 |
if (global_stats.receivedmessages == 0) { |
| 3086 |
ast_cli(a->fd, " Received messages since last reset: <none>\n"); |
| 3087 |
} else { |
| 3088 |
ast_cli(a->fd, " Received messages since last reset: %d\n", global_stats.receivedmessages); |
| 3089 |
ast_localtime(&global_stats.lastreceived, &timebuf, NULL); |
| 3090 |
ast_strftime(buf, sizeof(buf), "%a %b %e %r %Z %Y", &timebuf); |
| 3091 |
ast_cli(a->fd, " Last received voicemail: %s\n", buf); |
| 3092 |
} |
| 3093 |
ast_localtime(&global_stats.reset, &timebuf, NULL); |
| 3094 |
ast_strftime(buf, sizeof(buf), "%a %b %e %r %Z %Y", &timebuf); |
| 3095 |
ast_cli(a->fd, " Last reset: %s\n", buf); |
| 3096 |
|
| 3097 |
ast_cli(a->fd, "\n"); |
| 3098 |
return CLI_SUCCESS; |
| 3099 |
} |
| 3100 |
|
| 3101 |
|
| 3102 |
static int minivm_account_func_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len) |
| 3103 |
{ |
| 3104 |
struct minivm_account *vmu; |
| 3105 |
char *username, *domain, *colname; |
| 3106 |
|
| 3107 |
username = ast_strdupa(data); |
| 3108 |
|
| 3109 |
if ((colname = strchr(username, ':'))) { |
| 3110 |
*colname = '\0'; |
| 3111 |
colname++; |
| 3112 |
} else { |
| 3113 |
colname = "path"; |
| 3114 |
} |
| 3115 |
if ((domain = strchr(username, '@'))) { |
| 3116 |
*domain = '\0'; |
| 3117 |
domain++; |
| 3118 |
} |
| 3119 |
if (ast_strlen_zero(username) || ast_strlen_zero(domain)) { |
| 3120 |
ast_log(LOG_ERROR, "This function needs a username and a domain: username@domain\n"); |
| 3121 |
return 0; |
| 3122 |
} |
| 3123 |
|
| 3124 |
if (!(vmu = find_account(domain, username, TRUE))) |
| 3125 |
return 0; |
| 3126 |
|
| 3127 |
if (!strcasecmp(colname, "hasaccount")) { |
| 3128 |
ast_copy_string(buf, (ast_test_flag(vmu, MVM_ALLOCED) ? "0" : "1"), len); |
| 3129 |
} else if (!strcasecmp(colname, "fullname")) { |
| 3130 |
ast_copy_string(buf, vmu->fullname, len); |
| 3131 |
} else if (!strcasecmp(colname, "email")) { |
| 3132 |
if (!ast_strlen_zero(vmu->email)) |
| 3133 |
ast_copy_string(buf, vmu->email, len); |
| 3134 |
else |
| 3135 |
snprintf(buf, len, "%s@%s", vmu->username, vmu->domain); |
| 3136 |
} else if (!strcasecmp(colname, "pager")) { |
| 3137 |
ast_copy_string(buf, vmu->pager, len); |
| 3138 |
} else if (!strcasecmp(colname, "etemplate")) { |
| 3139 |
if (!ast_strlen_zero(vmu->etemplate)) |
| 3140 |
ast_copy_string(buf, vmu->etemplate, len); |
| 3141 |
else |
| 3142 |
ast_copy_string(buf, "email-default", len); |
| 3143 |
} else if (!strcasecmp(colname, "language")) { |
| 3144 |
ast_copy_string(buf, vmu->language, len); |
| 3145 |
} else if (!strcasecmp(colname, "timezone")) { |
| 3146 |
ast_copy_string(buf, vmu->zonetag, len); |
| 3147 |
} else if (!strcasecmp(colname, "ptemplate")) { |
| 3148 |
if (!ast_strlen_zero(vmu->ptemplate)) |
| 3149 |
ast_copy_string(buf, vmu->ptemplate, len); |
| 3150 |
else |
| 3151 |
ast_copy_string(buf, "email-default", len); |
| 3152 |
} else if (!strcasecmp(colname, "accountcode")) { |
| 3153 |
ast_copy_string(buf, vmu->accountcode, len); |
| 3154 |
} else if (!strcasecmp(colname, "pincode")) { |
| 3155 |
ast_copy_string(buf, vmu->pincode, len); |
| 3156 |
} else if (!strcasecmp(colname, "path")) { |
| 3157 |
check_dirpath(buf, len, vmu->domain, vmu->username, NULL); |
| 3158 |
} else { |
| 3159 |
struct ast_variable *var; |
| 3160 |
|
| 3161 |
for (var = vmu->chanvars ; var ; var = var->next) |
| 3162 |
if (!strcmp(var->name, colname)) { |
| 3163 |
ast_copy_string(buf, var->value, len); |
| 3164 |
break; |
| 3165 |
} |
| 3166 |
} |
| 3167 |
|
| 3168 |
if(ast_test_flag(vmu, MVM_ALLOCED)) |
| 3169 |
free_user(vmu); |
| 3170 |
|
| 3171 |
return 0; |
| 3172 |
} |
| 3173 |
|
| 3174 |
|
| 3175 |
|
| 3176 |
|
| 3177 |
|
| 3178 |
|
| 3179 |
static int vm_lock_path(const char *path) |
| 3180 |
{ |
| 3181 |
switch (ast_lock_path(path)) { |
| 3182 |
case AST_LOCK_TIMEOUT: |
| 3183 |
return -1; |
| 3184 |
default: |
| 3185 |
return 0; |
| 3186 |
} |
| 3187 |
} |
| 3188 |
|
| 3189 |
|
| 3190 |
|
| 3191 |
|
| 3192 |
|
| 3193 |
|
| 3194 |
|
| 3195 |
|
| 3196 |
static int access_counter_file(char *directory, char *countername, int value, int operand) |
| 3197 |
{ |
| 3198 |
char filename[BUFSIZ]; |
| 3199 |
char readbuf[BUFSIZ]; |
| 3200 |
FILE *counterfile; |
| 3201 |
int old = 0, counter = 0; |
| 3202 |
|
| 3203 |
|
| 3204 |
if (vm_lock_path(directory)) { |
| 3205 |
return -1; |
| 3206 |
} |
| 3207 |
snprintf(filename, sizeof(filename), "%s/%s.counter", directory, countername); |
| 3208 |
if (operand != 1) { |
| 3209 |
counterfile = fopen(filename, "r"); |
| 3210 |
if (counterfile) { |
| 3211 |
if(fgets(readbuf, sizeof(readbuf), counterfile)) { |
| 3212 |
ast_debug(3, "Read this string from counter file: %s\n", readbuf); |
| 3213 |
old = counter = atoi(readbuf); |
| 3214 |
} |
| 3215 |
fclose(counterfile); |
| 3216 |
} |
| 3217 |
} |
| 3218 |
switch (operand) { |
| 3219 |
case 0: |
| 3220 |
ast_unlock_path(directory); |
| 3221 |
ast_debug(2, "MINIVM Counter %s/%s: Value %d\n", directory, countername, counter); |
| 3222 |
return counter; |
| 3223 |
break; |
| 3224 |
case 1: |
| 3225 |
counter = value; |
| 3226 |
break; |
| 3227 |
case 2: |
| 3228 |
counter += value; |
| 3229 |
if (counter < 0) |
| 3230 |
counter = 0; |
| 3231 |
break; |
| 3232 |
} |
| 3233 |
|
| 3234 |
|
| 3235 |
counterfile = fopen(filename, "w"); |
| 3236 |
if (!counterfile) { |
| 3237 |
ast_log(LOG_ERROR, "Could not open counter file for writing : %s - %s\n", filename, strerror(errno)); |
| 3238 |
ast_unlock_path(directory); |
| 3239 |
return -1; |
| 3240 |
} |
| 3241 |
fprintf(counterfile, "%d\n\n", counter); |
| 3242 |
fclose(counterfile); |
| 3243 |
ast_unlock_path(directory); |
| 3244 |
ast_debug(2, "MINIVM Counter %s/%s: Old value %d New value %d\n", directory, countername, old, counter); |
| 3245 |
return counter; |
| 3246 |
} |
| 3247 |
|
| 3248 |
|
| 3249 |
static int minivm_counter_func_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len) |
| 3250 |
{ |
| 3251 |
char *username, *domain, *countername; |
| 3252 |
char userpath[BUFSIZ]; |
| 3253 |
int res; |
| 3254 |
|
| 3255 |
*buf = '\0'; |
| 3256 |
|
| 3257 |
username = ast_strdupa(data); |
| 3258 |
|
| 3259 |
if ((countername = strchr(username, ':'))) { |
| 3260 |
*countername = '\0'; |
| 3261 |
countername++; |
| 3262 |
} |
| 3263 |
|
| 3264 |
if ((domain = strchr(username, '@'))) { |
| 3265 |
*domain = '\0'; |
| 3266 |
domain++; |
| 3267 |
} |
| 3268 |
|
| 3269 |
|
| 3270 |
if (ast_strlen_zero(username) && ast_strlen_zero(domain)) { |
| 3271 |
ast_log(LOG_ERROR, "No account given\n"); |
| 3272 |
return -1; |
| 3273 |
} |
| 3274 |
|
| 3275 |
if (ast_strlen_zero(countername)) { |
| 3276 |
ast_log(LOG_ERROR, "This function needs two arguments: Account:countername\n"); |
| 3277 |
return -1; |
| 3278 |
} |
| 3279 |
|
| 3280 |
|
| 3281 |
if (!ast_strlen_zero(username) && ast_strlen_zero(domain)) { |
| 3282 |
domain = username; |
| 3283 |
username = NULL; |
| 3284 |
} |
| 3285 |
|
| 3286 |
|
| 3287 |
if (!ast_strlen_zero(username) && !find_account(domain, username, FALSE)) { |
| 3288 |
ast_log(LOG_ERROR, "Minivm account does not exist: %s@%s\n", username, domain); |
| 3289 |
return 0; |
| 3290 |
} |
| 3291 |
|
| 3292 |
create_dirpath(userpath, sizeof(userpath), domain, username, NULL); |
| 3293 |
|
| 3294 |
|
| 3295 |
res = access_counter_file(userpath, countername, 0, 0); |
| 3296 |
if (res >= 0) |
| 3297 |
snprintf(buf, len, "%d", res); |
| 3298 |
return 0; |
| 3299 |
} |
| 3300 |
|
| 3301 |
|
| 3302 |
static int minivm_counter_func_write(struct ast_channel *chan, const char *cmd, char *data, const char *value) |
| 3303 |
{ |
| 3304 |
char *username, *domain, *countername, *operand; |
| 3305 |
char userpath[BUFSIZ]; |
| 3306 |
int change = 0; |
| 3307 |
int operation = 0; |
| 3308 |
|
| 3309 |
if(!value) |
| 3310 |
return -1; |
| 3311 |
change = atoi(value); |
| 3312 |
|
| 3313 |
username = ast_strdupa(data); |
| 3314 |
|
| 3315 |
if ((countername = strchr(username, ':'))) { |
| 3316 |
*countername = '\0'; |
| 3317 |
countername++; |
| 3318 |
} |
| 3319 |
if ((operand = strchr(countername, ':'))) { |
| 3320 |
*operand = '\0'; |
| 3321 |
operand++; |
| 3322 |
} |
| 3323 |
|
| 3324 |
if ((domain = strchr(username, '@'))) { |
| 3325 |
*domain = '\0'; |
| 3326 |
domain++; |
| 3327 |
} |
| 3328 |
|
| 3329 |
|
| 3330 |
if (ast_strlen_zero(username) && ast_strlen_zero(domain)) { |
| 3331 |
ast_log(LOG_ERROR, "No account given\n"); |
| 3332 |
return -1; |
| 3333 |
} |
| 3334 |
|
| 3335 |
|
| 3336 |
if (!ast_strlen_zero(username) && ast_strlen_zero(domain)) { |
| 3337 |
domain = username; |
| 3338 |
username = NULL; |
| 3339 |
} |
| 3340 |
|
| 3341 |
if (ast_strlen_zero(operand) || ast_strlen_zero(countername)) { |
| 3342 |
ast_log(LOG_ERROR, "Writing to this function requires three arguments: Account:countername:operand\n"); |
| 3343 |
return -1; |
| 3344 |
} |
| 3345 |
|
| 3346 |
|
| 3347 |
if (!ast_strlen_zero(username) && !find_account(domain, username, FALSE)) { |
| 3348 |
ast_log(LOG_ERROR, "Minivm account does not exist: %s@%s\n", username, domain); |
| 3349 |
return 0; |
| 3350 |
} |
| 3351 |
|
| 3352 |
create_dirpath(userpath, sizeof(userpath), domain, username, NULL); |
| 3353 |
|
| 3354 |
if (*operand == 'i') |
| 3355 |
operation = 2; |
| 3356 |
else if (*operand == 'd') { |
| 3357 |
change = change * -1; |
| 3358 |
operation = 2; |
| 3359 |
} else if (*operand == 's') |
| 3360 |
operation = 1; |
| 3361 |
else { |
| 3362 |
ast_log(LOG_ERROR, "Unknown operator: %s\n", operand); |
| 3363 |
return -1; |
| 3364 |
} |
| 3365 |
|
| 3366 |
|
| 3367 |
access_counter_file(userpath, countername, change, operation); |
| 3368 |
return 0; |
| 3369 |
} |
| 3370 |
|
| 3371 |
|
| 3372 |
|
| 3373 |
static struct ast_cli_entry cli_minivm[] = { |
| 3374 |
AST_CLI_DEFINE(handle_minivm_show_users, "List defined mini-voicemail boxes"), |
| 3375 |
AST_CLI_DEFINE(handle_minivm_show_zones, "List zone message formats"), |
| 3376 |
AST_CLI_DEFINE(handle_minivm_list_templates, "List message templates"), |
| 3377 |
AST_CLI_DEFINE(handle_minivm_reload, "Reload Mini-voicemail configuration"), |
| 3378 |
AST_CLI_DEFINE(handle_minivm_show_stats, "Show some mini-voicemail statistics"), |
| 3379 |
AST_CLI_DEFINE(handle_minivm_show_settings, "Show mini-voicemail general settings"), |
| 3380 |
}; |
| 3381 |
|
| 3382 |
static struct ast_custom_function minivm_counter_function = { |
| 3383 |
.name = "MINIVMCOUNTER", |
| 3384 |
.read = minivm_counter_func_read, |
| 3385 |
.write = minivm_counter_func_write, |
| 3386 |
}; |
| 3387 |
|
| 3388 |
static struct ast_custom_function minivm_account_function = { |
| 3389 |
.name = "MINIVMACCOUNT", |
| 3390 |
.read = minivm_account_func_read, |
| 3391 |
}; |
| 3392 |
|
| 3393 |
|
| 3394 |
static int load_module(void) |
| 3395 |
{ |
| 3396 |
int res; |
| 3397 |
|
| 3398 |
res = ast_register_application_xml(app_minivm_record, minivm_record_exec); |
| 3399 |
res = ast_register_application_xml(app_minivm_greet, minivm_greet_exec); |
| 3400 |
res = ast_register_application_xml(app_minivm_notify, minivm_notify_exec); |
| 3401 |
res = ast_register_application_xml(app_minivm_delete, minivm_delete_exec); |
| 3402 |
res = ast_register_application_xml(app_minivm_accmess, minivm_accmess_exec); |
| 3403 |
res = ast_register_application_xml(app_minivm_mwi, minivm_mwi_exec); |
| 3404 |
|
| 3405 |
ast_custom_function_register(&minivm_account_function); |
| 3406 |
ast_custom_function_register(&minivm_counter_function); |
| 3407 |
if (res) |
| 3408 |
return(res); |
| 3409 |
|
| 3410 |
if ((res = load_config(0))) |
| 3411 |
return(res); |
| 3412 |
|
| 3413 |
ast_cli_register_multiple(cli_minivm, ARRAY_LEN(cli_minivm)); |
| 3414 |
|
| 3415 |
|
| 3416 |
snprintf(MVM_SPOOL_DIR, sizeof(MVM_SPOOL_DIR), "%s/voicemail/", ast_config_AST_SPOOL_DIR); |
| 3417 |
|
| 3418 |
return res; |
| 3419 |
} |
| 3420 |
|
| 3421 |
|
| 3422 |
static int reload(void) |
| 3423 |
{ |
| 3424 |
return(load_config(1)); |
| 3425 |
} |
| 3426 |
|
| 3427 |
|
| 3428 |
static char *handle_minivm_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 3429 |
{ |
| 3430 |
|
| 3431 |
switch (cmd) { |
| 3432 |
case CLI_INIT: |
| 3433 |
e->command = "minivm reload"; |
| 3434 |
e->usage = |
| 3435 |
"Usage: minivm reload\n" |
| 3436 |
" Reload mini-voicemail configuration and reset statistics\n"; |
| 3437 |
return NULL; |
| 3438 |
case CLI_GENERATE: |
| 3439 |
return NULL; |
| 3440 |
} |
| 3441 |
|
| 3442 |
reload(); |
| 3443 |
ast_cli(a->fd, "\n-- Mini voicemail re-configured \n"); |
| 3444 |
return CLI_SUCCESS; |
| 3445 |
} |
| 3446 |
|
| 3447 |
|
| 3448 |
static int unload_module(void) |
| 3449 |
{ |
| 3450 |
int res; |
| 3451 |
|
| 3452 |
res = ast_unregister_application(app_minivm_record); |
| 3453 |
res |= ast_unregister_application(app_minivm_greet); |
| 3454 |
res |= ast_unregister_application(app_minivm_notify); |
| 3455 |
res |= ast_unregister_application(app_minivm_delete); |
| 3456 |
res |= ast_unregister_application(app_minivm_accmess); |
| 3457 |
res |= ast_unregister_application(app_minivm_mwi); |
| 3458 |
|
| 3459 |
ast_cli_unregister_multiple(cli_minivm, ARRAY_LEN(cli_minivm)); |
| 3460 |
ast_custom_function_unregister(&minivm_account_function); |
| 3461 |
ast_custom_function_unregister(&minivm_counter_function); |
| 3462 |
|
| 3463 |
message_destroy_list(); |
| 3464 |
timezone_destroy_list(); |
| 3465 |
vmaccounts_destroy_list(); |
| 3466 |
|
| 3467 |
return res; |
| 3468 |
} |
| 3469 |
|
| 3470 |
|
| 3471 |
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Mini VoiceMail (A minimal Voicemail e-mail System)", |
| 3472 |
.support_level = AST_MODULE_SUPPORT_EXTENDED, |
| 3473 |
.load = load_module, |
| 3474 |
.unload = unload_module, |
| 3475 |
.reload = reload, |
| 3476 |
); |