From f020bc7df12afba1f23f4c4fdc6755f11b83163c Mon Sep 17 00:00:00 2001 From: sualko Date: Tue, 19 Sep 2017 13:49:05 +0200 Subject: [PATCH] add promises to sendMsg, receiveMsg and endOtr (fix #80) --- lib/otr.js | 51 ++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 42 insertions(+), 9 deletions(-) diff --git a/lib/otr.js b/lib/otr.js index ca4705d..b4bc4be 100644 --- a/lib/otr.js +++ b/lib/otr.js @@ -560,6 +560,8 @@ } OTR.prototype.sendMsg = function (msg, meta) { + var promise = (typeof Promise === 'function') ? new Promise(function(){}) : undefined; + if ( this.REQUIRE_ENCRYPTION || this.msgstate !== CONST.MSGSTATE_PLAINTEXT ) { @@ -572,7 +574,7 @@ if (this.REQUIRE_ENCRYPTION) { this.storedMgs.push({msg: msg, meta: meta}) this.sendQueryMsg() - return + return promise } if (this.SEND_WHITESPACE_TAG && !this.receivedPlaintext) { msg += CONST.WHITESPACE_TAG // 16 byte tag @@ -583,7 +585,7 @@ case CONST.MSGSTATE_FINISHED: this.storedMgs.push({msg: msg, meta: meta}) this.notify('Message cannot be sent at this time.', 'warn') - return + return promise case CONST.MSGSTATE_ENCRYPTED: msg = this.prepareMsg(msg) break @@ -591,37 +593,45 @@ throw new Error('Unknown message state.') } - if (msg) this.io(msg, meta) + if (msg) { + this.io(msg, meta) + + if (typeof Promise !== 'undefined' && typeof Promise.resolve === 'function') + return Promise.resolve(msg) + } + + return promise } OTR.prototype.receiveMsg = function (msg, meta) { + var promise = (typeof Promise === 'function') ? new Promise(function(){}) : undefined; // parse type msg = Parse.parseMsg(this, msg) - if (!msg) return + if (!msg) return promise switch (msg.cls) { case 'error': this.notify(msg.msg) - return + return promise case 'ake': if ( msg.version === CONST.OTR_VERSION_3 && this.checkInstanceTags(msg.instance_tags) ) { this.notify( 'Received a message intended for a different session.', 'warn') - return // ignore + return promise // ignore } this.ake.handleAKE(msg) - return + return promise case 'data': if ( msg.version === CONST.OTR_VERSION_3 && this.checkInstanceTags(msg.instance_tags) ) { this.notify( 'Received a message intended for a different session.', 'warn') - return // ignore + return promise // ignore } msg.msg = this.handleDataMsg(msg) msg.encrypted = true @@ -645,7 +655,14 @@ this.doAKE(msg) } - if (msg.msg) this.trigger('ui', [msg.msg, !!msg.encrypted, meta]) + if (msg.msg) { + this.trigger('ui', [msg.msg, !!msg.encrypted, meta]) + + if (typeof Promise !== 'undefined' && typeof Promise.resolve === 'function') + return Promise.resolve([msg.msg, !!msg.encrypted]) + } + + return promise; } OTR.prototype.checkInstanceTags = function (it) { @@ -718,6 +735,20 @@ } OTR.prototype.endOtr = function (cb) { + var promise + + if (typeof Promise === 'function') { + promise = new Promise(function(resolve) { + var original_cb = cb + + cb = function() { + if (typeof original_cb === 'function') + original_cb() + resolve() + } + }) + } + if (this.msgstate === CONST.MSGSTATE_ENCRYPTED) { if (typeof cb === 'function') cb = new OTRCB(cb) @@ -732,6 +763,8 @@ this.msgstate = CONST.MSGSTATE_PLAINTEXT this.receivedPlaintext = false this.trigger('status', [CONST.STATUS_END_OTR]) + + if (promise) return promise } // attach methods