|
560 | 560 | } |
561 | 561 |
|
562 | 562 | OTR.prototype.sendMsg = function (msg, meta) { |
| 563 | + var promise = (typeof Promise === 'function') ? new Promise(function(){}) : undefined; |
| 564 | + |
563 | 565 | if ( this.REQUIRE_ENCRYPTION || |
564 | 566 | this.msgstate !== CONST.MSGSTATE_PLAINTEXT |
565 | 567 | ) { |
|
572 | 574 | if (this.REQUIRE_ENCRYPTION) { |
573 | 575 | this.storedMgs.push({msg: msg, meta: meta}) |
574 | 576 | this.sendQueryMsg() |
575 | | - return |
| 577 | + return promise |
576 | 578 | } |
577 | 579 | if (this.SEND_WHITESPACE_TAG && !this.receivedPlaintext) { |
578 | 580 | msg += CONST.WHITESPACE_TAG // 16 byte tag |
|
583 | 585 | case CONST.MSGSTATE_FINISHED: |
584 | 586 | this.storedMgs.push({msg: msg, meta: meta}) |
585 | 587 | this.notify('Message cannot be sent at this time.', 'warn') |
586 | | - return |
| 588 | + return promise |
587 | 589 | case CONST.MSGSTATE_ENCRYPTED: |
588 | 590 | msg = this.prepareMsg(msg) |
589 | 591 | break |
590 | 592 | default: |
591 | 593 | throw new Error('Unknown message state.') |
592 | 594 | } |
593 | 595 |
|
594 | | - if (msg) this.io(msg, meta) |
| 596 | + if (msg) { |
| 597 | + this.io(msg, meta) |
| 598 | + |
| 599 | + if (typeof Promise !== 'undefined' && typeof Promise.resolve === 'function') |
| 600 | + return Promise.resolve(msg) |
| 601 | + } |
| 602 | + |
| 603 | + return promise |
595 | 604 | } |
596 | 605 |
|
597 | 606 | OTR.prototype.receiveMsg = function (msg, meta) { |
| 607 | + var promise = (typeof Promise === 'function') ? new Promise(function(){}) : undefined; |
598 | 608 |
|
599 | 609 | // parse type |
600 | 610 | msg = Parse.parseMsg(this, msg) |
601 | 611 |
|
602 | | - if (!msg) return |
| 612 | + if (!msg) return promise |
603 | 613 |
|
604 | 614 | switch (msg.cls) { |
605 | 615 | case 'error': |
606 | 616 | this.notify(msg.msg) |
607 | | - return |
| 617 | + return promise |
608 | 618 | case 'ake': |
609 | 619 | if ( msg.version === CONST.OTR_VERSION_3 && |
610 | 620 | this.checkInstanceTags(msg.instance_tags) |
611 | 621 | ) { |
612 | 622 | this.notify( |
613 | 623 | 'Received a message intended for a different session.', 'warn') |
614 | | - return // ignore |
| 624 | + return promise // ignore |
615 | 625 | } |
616 | 626 | this.ake.handleAKE(msg) |
617 | | - return |
| 627 | + return promise |
618 | 628 | case 'data': |
619 | 629 | if ( msg.version === CONST.OTR_VERSION_3 && |
620 | 630 | this.checkInstanceTags(msg.instance_tags) |
621 | 631 | ) { |
622 | 632 | this.notify( |
623 | 633 | 'Received a message intended for a different session.', 'warn') |
624 | | - return // ignore |
| 634 | + return promise // ignore |
625 | 635 | } |
626 | 636 | msg.msg = this.handleDataMsg(msg) |
627 | 637 | msg.encrypted = true |
|
645 | 655 | this.doAKE(msg) |
646 | 656 | } |
647 | 657 |
|
648 | | - if (msg.msg) this.trigger('ui', [msg.msg, !!msg.encrypted, meta]) |
| 658 | + if (msg.msg) { |
| 659 | + this.trigger('ui', [msg.msg, !!msg.encrypted, meta]) |
| 660 | + |
| 661 | + if (typeof Promise !== 'undefined' && typeof Promise.resolve === 'function') |
| 662 | + return Promise.resolve([msg.msg, !!msg.encrypted]) |
| 663 | + } |
| 664 | + |
| 665 | + return promise; |
649 | 666 | } |
650 | 667 |
|
651 | 668 | OTR.prototype.checkInstanceTags = function (it) { |
|
718 | 735 | } |
719 | 736 |
|
720 | 737 | OTR.prototype.endOtr = function (cb) { |
| 738 | + var promise |
| 739 | + |
| 740 | + if (typeof Promise === 'function') { |
| 741 | + promise = new Promise(function(resolve) { |
| 742 | + var original_cb = cb |
| 743 | + |
| 744 | + cb = function() { |
| 745 | + if (typeof original_cb === 'function') |
| 746 | + original_cb() |
| 747 | + resolve() |
| 748 | + } |
| 749 | + }) |
| 750 | + } |
| 751 | + |
721 | 752 | if (this.msgstate === CONST.MSGSTATE_ENCRYPTED) { |
722 | 753 | if (typeof cb === 'function') |
723 | 754 | cb = new OTRCB(cb) |
|
732 | 763 | this.msgstate = CONST.MSGSTATE_PLAINTEXT |
733 | 764 | this.receivedPlaintext = false |
734 | 765 | this.trigger('status', [CONST.STATUS_END_OTR]) |
| 766 | + |
| 767 | + if (promise) return promise |
735 | 768 | } |
736 | 769 |
|
737 | 770 | // attach methods |
|
0 commit comments