2016-02-13 23:58:50 +01:00
// Converse.js (A browser based XMPP chat client)
// http://conversejs.org
//
2017-02-13 15:37:17 +01:00
// Copyright (c) 2012-2017, Jan-Carel Brand <jc@opkode.com>
2016-02-13 23:58:50 +01:00
// Licensed under the Mozilla Public License (MPLv2)
//
2017-04-21 15:52:16 +02:00
/*global define, window, crypto, CryptoJS */
2016-02-13 23:58:50 +01:00
/ * T h i s i s a C o n v e r s e . j s p l u g i n w h i c h a d d s u p p o r t O f f - t h e - r e c o r d ( O T R )
* encryption of one - on - one chat messages .
* /
( function ( root , factory ) {
2018-04-16 21:58:11 +02:00
define ( [
"converse-chatview" ,
"bootstrap" ,
"tpl!toolbar_otr" ,
'otr'
2017-02-14 16:34:32 +01:00
] , factory ) ;
2018-03-09 17:30:42 +01:00
} ( this , function ( converse , bootstrap , tpl _toolbar _otr , otr ) {
2016-02-19 11:43:46 +01:00
"use strict" ;
2017-02-14 16:34:32 +01:00
2018-05-03 13:47:31 +02:00
const { Strophe , utils , _ } = converse . env ;
2016-02-28 09:30:07 +01:00
2017-08-21 15:41:52 +02:00
const HAS _CSPRNG = _ . isUndefined ( window . crypto ) ? false : (
_ . isFunction ( window . crypto . randomBytes ) ||
_ . isFunction ( window . crypto . getRandomValues )
) ;
2016-12-20 10:30:20 +01:00
2017-06-12 20:30:58 +02:00
const HAS _CRYPTO = HAS _CSPRNG && (
2017-01-26 15:49:02 +01:00
( ! _ . isUndefined ( otr . OTR ) ) &&
( ! _ . isUndefined ( otr . DSA ) )
2016-02-13 23:58:50 +01:00
) ;
2017-06-12 20:30:58 +02:00
const UNENCRYPTED = 0 ;
const UNVERIFIED = 1 ;
const VERIFIED = 2 ;
const FINISHED = 3 ;
2016-02-13 23:58:50 +01:00
2017-06-12 20:30:58 +02:00
const OTR _TRANSLATED _MAPPING = { } ; // Populated in initialize
const OTR _CLASS _MAPPING = { } ;
2016-02-13 23:58:50 +01:00
OTR _CLASS _MAPPING [ UNENCRYPTED ] = 'unencrypted' ;
OTR _CLASS _MAPPING [ UNVERIFIED ] = 'unverified' ;
OTR _CLASS _MAPPING [ VERIFIED ] = 'verified' ;
OTR _CLASS _MAPPING [ FINISHED ] = 'finished' ;
2017-07-15 11:03:22 +02:00
2016-12-20 11:42:20 +01:00
converse . plugins . add ( 'converse-otr' , {
2018-01-10 14:26:35 +01:00
/ * P l u g i n d e p e n d e n c i e s a r e o t h e r p l u g i n s w h i c h m i g h t b e
* overridden or relied upon , and therefore need to be loaded before
* this plugin .
*
* If the setting "strict_plugin_dependencies" is set to true ,
* an error will be raised if the plugin is not found . By default it ' s
* false , which means these plugins are only loaded opportunistically .
*
* NB : These plugins need to have already been loaded via require . js .
* /
dependencies : [ "converse-chatview" ] ,
2016-02-13 23:58:50 +01:00
overrides : {
// Overrides mentioned here will be picked up by converse.js's
// plugin architecture they will replace existing methods on the
// relevant objects or classes.
//
// New functions which don't exist yet can also be added.
2018-02-02 21:13:45 +01:00
2016-02-13 23:58:50 +01:00
ChatBox : {
2017-07-10 17:46:22 +02:00
initialize ( ) {
2016-08-31 12:06:17 +02:00
this . _ _super _ _ . initialize . apply ( this , arguments ) ;
2016-02-13 23:58:50 +01:00
if ( this . get ( 'box_id' ) !== 'controlbox' ) {
2017-02-15 11:20:57 +01:00
this . save ( { 'otr_status' : this . get ( 'otr_status' ) || UNENCRYPTED } ) ;
2016-02-13 23:58:50 +01:00
}
} ,
2018-04-16 14:22:27 +02:00
createMessageStanza ( ) {
const stanza = this . _ _super _ _ . createMessageStanza . apply ( this , arguments ) ;
if ( this . get ( 'otr_status' ) !== UNENCRYPTED || utils . isOTRMessage ( stanza . nodeTree ) ) {
// OTR messages aren't carbon copied
stanza . c ( 'private' , { 'xmlns' : Strophe . NS . CARBONS } ) . up ( )
. c ( 'no-store' , { 'xmlns' : Strophe . NS . HINTS } ) . up ( )
. c ( 'no-permanent-store' , { 'xmlns' : Strophe . NS . HINTS } ) . up ( )
. c ( 'no-copy' , { 'xmlns' : Strophe . NS . HINTS } ) ;
}
return stanza ;
} ,
2017-07-10 17:46:22 +02:00
shouldPlayNotification ( $message ) {
2016-02-14 13:10:54 +01:00
/ * D o n ' t p l a y a n o t i f i c a t i o n i f t h i s i s a n O T R m e s s a g e b u t
* encryption is not yet set up . That would mean that the
* OTR session is still being established , so there are no
* "visible" OTR messages being exchanged .
* /
2016-08-31 12:06:17 +02:00
return this . _ _super _ _ . shouldPlayNotification . apply ( this , arguments ) &&
2017-01-26 15:49:02 +01:00
! ( utils . isOTRMessage ( $message [ 0 ] ) && ! _ . includes ( [ UNVERIFIED , VERIFIED ] , this . get ( 'otr_status' ) ) ) ;
2016-02-14 13:10:54 +01:00
} ,
2018-06-07 12:36:11 +02:00
createMessage ( message , original _stanza ) {
2017-07-10 17:46:22 +02:00
const { _converse } = this . _ _super _ _ ,
2016-11-24 02:07:32 +01:00
text = _ . propertyOf ( message . querySelector ( 'body' ) ) ( 'textContent' ) ;
2016-02-14 13:10:54 +01:00
2016-12-20 10:30:20 +01:00
if ( ( ! text ) || ( ! _converse . allow _otr ) ) {
2016-08-31 12:06:17 +02:00
return this . _ _super _ _ . createMessage . apply ( this , arguments ) ;
2016-02-13 23:58:50 +01:00
}
2017-06-14 18:41:45 +02:00
if ( utils . isNewMessage ( original _stanza ) ) {
if ( text . match ( /^\?OTRv23?/ ) ) {
return this . initiateOTR ( text ) ;
} else if ( _ . includes ( [ UNVERIFIED , VERIFIED ] , this . get ( 'otr_status' ) ) ) {
return this . otr . receiveMsg ( text ) ;
} else if ( text . match ( /^\?OTR/ ) ) {
if ( ! this . otr ) {
return this . initiateOTR ( text ) ;
2016-02-13 23:58:50 +01:00
} else {
2017-06-14 18:41:45 +02:00
return this . otr . receiveMsg ( text ) ;
2016-02-13 23:58:50 +01:00
}
}
}
2017-06-14 18:41:45 +02:00
// Normal unencrypted message (or archived message)
return this . _ _super _ _ . createMessage . apply ( this , arguments ) ;
2016-02-13 23:58:50 +01:00
} ,
2017-02-15 11:20:57 +01:00
2017-07-10 17:46:22 +02:00
generatePrivateKey ( instance _tag ) {
const { _converse } = this . _ _super _ _ ;
const key = new otr . DSA ( ) ;
const { jid } = _converse . connection ;
2017-02-15 11:20:57 +01:00
if ( _converse . cache _otr _key ) {
this . save ( {
'otr_priv_key' : key . packPrivate ( ) ,
'otr_instance_tag' : instance _tag
} ) ;
}
return key ;
} ,
2018-02-02 21:13:45 +01:00
2017-07-10 17:46:22 +02:00
getSession ( callback ) {
const { _converse } = this . _ _super _ _ ,
{ _ _ } = _converse ;
let instance _tag , saved _key , encrypted _key ;
2016-12-20 10:30:20 +01:00
if ( _converse . cache _otr _key ) {
2017-04-20 10:04:33 +02:00
encrypted _key = this . get ( 'otr_priv_key' ) ;
if ( _ . isString ( encrypted _key ) ) {
instance _tag = this . get ( 'otr_instance_tag' ) ;
2017-06-12 20:30:58 +02:00
saved _key = otr . DSA . parsePrivate ( encrypted _key ) ;
2017-04-20 10:04:33 +02:00
if ( saved _key && instance _tag ) {
this . trigger ( 'showHelpMessages' , [ _ _ ( 'Re-establishing encrypted session' ) ] ) ;
callback ( {
'key' : saved _key ,
'instance_tag' : instance _tag
} ) ;
return ; // Our work is done here
}
2016-02-13 23:58:50 +01:00
}
}
// We need to generate a new key and instance tag
this . trigger ( 'showHelpMessages' , [
_ _ ( 'Generating private key.' ) ,
_ _ ( 'Your browser might become unresponsive.' ) ] ,
null ,
true // show spinner
) ;
2017-07-10 17:46:22 +02:00
const that = this ;
2016-02-20 16:06:12 +01:00
window . setTimeout ( function ( ) {
2016-02-13 23:58:50 +01:00
callback ( {
2017-02-15 11:20:57 +01:00
'key' : that . generatePrivateKey ( instance _tag ) ,
2017-04-20 10:04:33 +02:00
'instance_tag' : otr . OTR . makeInstanceTag ( )
2016-02-13 23:58:50 +01:00
} ) ;
} , 500 ) ;
} ,
2017-07-10 17:46:22 +02:00
updateOTRStatus ( state ) {
2016-02-13 23:58:50 +01:00
switch ( state ) {
case otr . OTR . CONST . STATUS _AKE _SUCCESS :
if ( this . otr . msgstate === otr . OTR . CONST . MSGSTATE _ENCRYPTED ) {
this . save ( { 'otr_status' : UNVERIFIED } ) ;
}
break ;
case otr . OTR . CONST . STATUS _END _OTR :
if ( this . otr . msgstate === otr . OTR . CONST . MSGSTATE _FINISHED ) {
this . save ( { 'otr_status' : FINISHED } ) ;
} else if ( this . otr . msgstate === otr . OTR . CONST . MSGSTATE _PLAINTEXT ) {
this . save ( { 'otr_status' : UNENCRYPTED } ) ;
}
break ;
}
} ,
2017-07-10 17:46:22 +02:00
onSMP ( type , data ) {
2016-02-13 23:58:50 +01:00
// Event handler for SMP (Socialist's Millionaire Protocol)
// used by OTR (off-the-record).
2017-07-10 17:46:22 +02:00
const { _converse } = this . _ _super _ _ ,
{ _ _ } = _converse ;
2016-02-13 23:58:50 +01:00
switch ( type ) {
case 'question' :
this . otr . smpSecret ( prompt ( _ _ (
'Authentication request from %1$s\n\nYour chat contact is attempting to verify your identity, by asking you the question below.\n\n%2$s' ,
[ this . get ( 'fullname' ) , data ] ) ) ) ;
break ;
case 'trust' :
if ( data === true ) {
this . save ( { 'otr_status' : VERIFIED } ) ;
} else {
this . trigger (
'showHelpMessages' ,
[ _ _ ( "Could not verify this user's identify." ) ] ,
'error' ) ;
this . save ( { 'otr_status' : UNVERIFIED } ) ;
}
break ;
default :
throw new TypeError ( 'ChatBox.onSMP: Unknown type for SMP' ) ;
}
} ,
2017-07-10 17:46:22 +02:00
initiateOTR ( query _msg ) {
2016-02-13 23:58:50 +01:00
// Sets up an OTR object through which we can send and receive
// encrypted messages.
//
// If 'query_msg' is passed in, it means there is an alread incoming
// query message from our contact. Otherwise, it is us who will
// send the query message to them.
2017-07-10 17:46:22 +02:00
const { _converse } = this . _ _super _ _ ,
{ _ _ } = _converse ;
2016-02-13 23:58:50 +01:00
this . save ( { 'otr_status' : UNENCRYPTED } ) ;
2017-07-10 17:46:22 +02:00
this . getSession ( ( session ) => {
const { _converse } = this . _ _super _ _ ;
2016-02-13 23:58:50 +01:00
this . otr = new otr . OTR ( {
fragment _size : 140 ,
send _interval : 200 ,
priv : session . key ,
instance _tag : session . instance _tag ,
debug : this . debug
} ) ;
this . otr . on ( 'status' , this . updateOTRStatus . bind ( this ) ) ;
this . otr . on ( 'smp' , this . onSMP . bind ( this ) ) ;
2017-07-10 17:46:22 +02:00
this . otr . on ( 'ui' , ( msg ) => {
2016-02-13 23:58:50 +01:00
this . trigger ( 'showReceivedOTRMessage' , msg ) ;
2017-07-10 17:46:22 +02:00
} ) ;
this . otr . on ( 'io' , ( msg ) => {
2018-04-14 21:13:02 +02:00
this . sendMessage ( new _converse . Message ( { 'message' : msg } ) ) ;
2017-07-10 17:46:22 +02:00
} ) ;
this . otr . on ( 'error' , ( msg ) => {
2016-02-13 23:58:50 +01:00
this . trigger ( 'showOTRError' , msg ) ;
2017-07-10 17:46:22 +02:00
} ) ;
2016-02-13 23:58:50 +01:00
this . trigger ( 'showHelpMessages' , [ _ _ ( 'Exchanging private key with contact.' ) ] ) ;
if ( query _msg ) {
this . otr . receiveMsg ( query _msg ) ;
} else {
this . otr . sendQueryMsg ( ) ;
}
2017-07-10 17:46:22 +02:00
} ) ;
2016-02-13 23:58:50 +01:00
} ,
2017-07-10 17:46:22 +02:00
endOTR ( ) {
2016-02-13 23:58:50 +01:00
if ( this . otr ) {
this . otr . endOtr ( ) ;
}
this . save ( { 'otr_status' : UNENCRYPTED } ) ;
}
} ,
ChatBoxView : {
events : {
'click .toggle-otr' : 'toggleOTRMenu' ,
'click .start-otr' : 'startOTRFromToolbar' ,
'click .end-otr' : 'endOTR' ,
'click .auth-otr' : 'authOTR'
} ,
2017-07-10 17:46:22 +02:00
initialize ( ) {
const { _converse } = this . _ _super _ _ ;
2016-08-31 12:06:17 +02:00
this . _ _super _ _ . initialize . apply ( this , arguments ) ;
2016-02-13 23:58:50 +01:00
this . model . on ( 'change:otr_status' , this . onOTRStatusChanged , this ) ;
this . model . on ( 'showOTRError' , this . showOTRError , this ) ;
this . model . on ( 'showSentOTRMessage' , function ( text ) {
this . showMessage ( { 'message' : text , 'sender' : 'me' } ) ;
} , this ) ;
this . model . on ( 'showReceivedOTRMessage' , function ( text ) {
this . showMessage ( { 'message' : text , 'sender' : 'them' } ) ;
} , this ) ;
2016-12-23 05:38:08 +01:00
if ( ( _ . includes ( [ UNVERIFIED , VERIFIED ] , this . model . get ( 'otr_status' ) ) ) || _converse . use _otr _by _default ) {
2016-02-13 23:58:50 +01:00
this . model . initiateOTR ( ) ;
}
} ,
2018-02-06 21:21:21 +01:00
parseMessageForCommands ( text ) {
2017-07-10 17:46:22 +02:00
const { _converse } = this . _ _super _ _ ;
const match = text . replace ( /^\s*/ , "" ) . match ( /^\/(.*)\s*$/ ) ;
2016-02-13 23:58:50 +01:00
if ( match ) {
2016-12-20 10:30:20 +01:00
if ( ( _converse . allow _otr ) && ( match [ 1 ] === "endotr" ) ) {
2018-02-06 21:21:21 +01:00
this . endOTR ( ) ;
return true ;
2016-12-20 10:30:20 +01:00
} else if ( ( _converse . allow _otr ) && ( match [ 1 ] === "otr" ) ) {
2018-02-06 21:21:21 +01:00
this . model . initiateOTR ( ) ;
return true ;
2016-02-13 23:58:50 +01:00
}
}
2018-02-06 21:21:21 +01:00
return this . _ _super _ _ . parseMessageForCommands . apply ( this , arguments ) ;
} ,
2018-02-07 11:00:35 +01:00
isOTREncryptedSession ( ) {
return _ . includes ( [ UNVERIFIED , VERIFIED ] , this . model . get ( 'otr_status' ) ) ;
} ,
2018-02-06 21:21:21 +01:00
onMessageSubmitted ( text , spoiler _hint ) {
const { _converse } = this . _ _super _ _ ;
if ( ! _converse . connection . authenticated ) {
this . _ _super _ _ . onMessageSubmitted . apply ( this , arguments ) ;
}
if ( this . parseMessageForCommands ( text ) ) {
return ;
}
2018-02-07 11:00:35 +01:00
if ( this . isOTREncryptedSession ( ) ) {
2016-02-13 23:58:50 +01:00
this . model . otr . sendMsg ( text ) ;
this . model . trigger ( 'showSentOTRMessage' , text ) ;
} else {
2016-08-31 12:06:17 +02:00
this . _ _super _ _ . onMessageSubmitted . apply ( this , arguments ) ;
2016-02-13 23:58:50 +01:00
}
} ,
2017-07-10 17:46:22 +02:00
onOTRStatusChanged ( ) {
2016-02-13 23:58:50 +01:00
this . renderToolbar ( ) . informOTRChange ( ) ;
} ,
2017-07-10 17:46:22 +02:00
informOTRChange ( ) {
const { _converse } = this . _ _super _ _ ,
{ _ _ } = _converse ,
2016-12-20 11:42:20 +01:00
data = this . model . toJSON ( ) ,
msgs = [ ] ;
2016-02-13 23:58:50 +01:00
if ( data . otr _status === UNENCRYPTED ) {
msgs . push ( _ _ ( "Your messages are not encrypted anymore" ) ) ;
} else if ( data . otr _status === UNVERIFIED ) {
msgs . push ( _ _ ( "Your messages are now encrypted but your contact's identity has not been verified." ) ) ;
} else if ( data . otr _status === VERIFIED ) {
msgs . push ( _ _ ( "Your contact's identify has been verified." ) ) ;
} else if ( data . otr _status === FINISHED ) {
msgs . push ( _ _ ( "Your contact has ended encryption on their end, you should do the same." ) ) ;
}
return this . showHelpMessages ( msgs , 'info' , false ) ;
} ,
2017-07-10 17:46:22 +02:00
showOTRError ( msg ) {
const { _converse } = this . _ _super _ _ ,
{ _ _ } = _converse ;
2016-02-13 23:58:50 +01:00
if ( msg === 'Message cannot be sent at this time.' ) {
this . showHelpMessages (
[ _ _ ( 'Your message could not be sent' ) ] , 'error' ) ;
} else if ( msg === 'Received an unencrypted message.' ) {
this . showHelpMessages (
[ _ _ ( 'We received an unencrypted message' ) ] , 'error' ) ;
} else if ( msg === 'Received an unreadable encrypted message.' ) {
this . showHelpMessages (
[ _ _ ( 'We received an unreadable encrypted message' ) ] ,
'error' ) ;
} else {
2017-07-10 17:46:22 +02:00
this . showHelpMessages ( [ ` Encryption error occured: ${ msg } ` ] , 'error' ) ;
2016-02-13 23:58:50 +01:00
}
2017-07-10 17:46:22 +02:00
_converse . log ( ` OTR ERROR: ${ msg } ` , Strophe . LogLevel . ERROR ) ;
2016-02-13 23:58:50 +01:00
} ,
2017-07-10 17:46:22 +02:00
startOTRFromToolbar ( ev ) {
2016-02-13 23:58:50 +01:00
ev . stopPropagation ( ) ;
this . model . initiateOTR ( ) ;
} ,
2017-07-10 17:46:22 +02:00
endOTR ( ev ) {
2017-01-26 15:49:02 +01:00
if ( ! _ . isUndefined ( ev ) ) {
2016-02-13 23:58:50 +01:00
ev . preventDefault ( ) ;
ev . stopPropagation ( ) ;
}
this . model . endOTR ( ) ;
} ,
2017-07-10 17:46:22 +02:00
authOTR ( ev ) {
const { _converse } = this . _ _super _ _ ,
{ _ _ } = _converse ,
2017-12-22 22:38:36 +01:00
scheme = ev . target . getAttribute ( 'data-scheme' ) ;
2017-07-10 17:46:22 +02:00
let result , question , answer ;
2016-02-13 23:58:50 +01:00
if ( scheme === 'fingerprint' ) {
result = confirm ( _ _ ( 'Here are the fingerprints, please confirm them with %1$s, outside of this chat.\n\nFingerprint for you, %2$s: %3$s\n\nFingerprint for %1$s: %4$s\n\nIf you have confirmed that the fingerprints match, click OK, otherwise click Cancel.' , [
this . model . get ( 'fullname' ) ,
2016-12-20 10:30:20 +01:00
_converse . xmppstatus . get ( 'fullname' ) || _converse . bare _jid ,
2016-02-13 23:58:50 +01:00
this . model . otr . priv . fingerprint ( ) ,
this . model . otr . their _priv _pk . fingerprint ( )
]
) ) ;
if ( result === true ) {
this . model . save ( { 'otr_status' : VERIFIED } ) ;
} else {
this . model . save ( { 'otr_status' : UNVERIFIED } ) ;
}
} else if ( scheme === 'smp' ) {
alert ( _ _ ( 'You will be prompted to provide a security question and then an answer to that question.\n\nYour contact will then be prompted the same question and if they type the exact same answer (case sensitive), their identity will be verified.' ) ) ;
question = prompt ( _ _ ( 'What is your security question?' ) ) ;
if ( question ) {
answer = prompt ( _ _ ( 'What is the answer to the security question?' ) ) ;
this . model . otr . smpSecret ( answer , question ) ;
}
} else {
this . showHelpMessages ( [ _ _ ( 'Invalid authentication scheme provided' ) ] , 'error' ) ;
}
} ,
2017-07-10 17:46:22 +02:00
toggleOTRMenu ( ev ) {
2018-03-09 17:30:42 +01:00
if ( _ . isUndefined ( this . otr _dropdown ) ) {
ev . stopPropagation ( ) ;
const dropdown _el = this . el . querySelector ( '.toggle-otr' ) ;
this . otr _dropdown = new bootstrap . Dropdown ( dropdown _el , true ) ;
this . otr _dropdown . toggle ( ) ;
}
2016-02-13 23:58:50 +01:00
} ,
2018-02-02 21:13:45 +01:00
2017-07-10 17:46:22 +02:00
getOTRTooltip ( ) {
const { _converse } = this . _ _super _ _ ,
{ _ _ } = _converse ,
2016-12-20 11:42:20 +01:00
data = this . model . toJSON ( ) ;
2016-02-13 23:58:50 +01:00
if ( data . otr _status === UNENCRYPTED ) {
return _ _ ( 'Your messages are not encrypted. Click here to enable OTR encryption.' ) ;
} else if ( data . otr _status === UNVERIFIED ) {
return _ _ ( 'Your messages are encrypted, but your contact has not been verified.' ) ;
} else if ( data . otr _status === VERIFIED ) {
return _ _ ( 'Your messages are encrypted and your contact verified.' ) ;
} else if ( data . otr _status === FINISHED ) {
return _ _ ( 'Your contact has closed their end of the private session, you should do the same' ) ;
}
} ,
2018-02-02 21:13:45 +01:00
addOTRToolbarButton ( options ) {
2017-07-10 17:46:22 +02:00
const { _converse } = this . _ _super _ _ ,
2018-02-02 21:13:45 +01:00
{ _ _ } = _converse ,
data = this . model . toJSON ( ) ;
2016-02-13 23:58:50 +01:00
options = _ . extend ( options || { } , {
2017-07-10 17:46:22 +02:00
FINISHED ,
UNENCRYPTED ,
UNVERIFIED ,
VERIFIED ,
2016-02-14 12:23:32 +01:00
// FIXME: Leaky abstraction MUC
2016-12-20 10:30:20 +01:00
allow _otr : _converse . allow _otr && ! this . is _chatroom ,
2016-02-13 23:58:50 +01:00
label _end _encrypted _conversation : _ _ ( 'End encrypted conversation' ) ,
label _refresh _encrypted _conversation : _ _ ( 'Refresh encrypted conversation' ) ,
label _start _encrypted _conversation : _ _ ( 'Start encrypted conversation' ) ,
label _verify _with _fingerprints : _ _ ( 'Verify with fingerprints' ) ,
label _verify _with _smp : _ _ ( 'Verify with SMP' ) ,
label _whats _this : _ _ ( "What\'s this?" ) ,
otr _status _class : OTR _CLASS _MAPPING [ data . otr _status ] ,
otr _tooltip : this . getOTRTooltip ( ) ,
otr _translated _status : OTR _TRANSLATED _MAPPING [ data . otr _status ] ,
} ) ;
2017-12-22 22:38:36 +01:00
this . el . querySelector ( '.chat-toolbar' ) . insertAdjacentHTML (
2018-02-02 21:13:45 +01:00
'beforeend' ,
2018-03-02 12:10:25 +01:00
tpl _toolbar _otr ( _ . extend ( data , options || { } ) ) ) ;
2018-02-02 21:13:45 +01:00
} ,
2018-02-07 11:00:35 +01:00
getToolbarOptions ( options ) {
options = this . _ _super _ _ . getToolbarOptions ( ) ;
2018-02-07 13:12:43 +01:00
if ( this . isOTREncryptedSession ( ) ) {
2018-02-07 11:00:35 +01:00
options . show _spoiler _button = false ;
}
return options ;
} ,
2018-02-02 21:13:45 +01:00
renderToolbar ( toolbar , options ) {
const result = this . _ _super _ _ . renderToolbar . apply ( this , arguments ) ;
this . addOTRToolbarButton ( options ) ;
return result ;
2016-02-13 23:58:50 +01:00
}
}
} ,
2017-07-10 17:46:22 +02:00
initialize ( ) {
2016-02-13 23:58:50 +01:00
/ * T h e i n i t i a l i z e f u n c t i o n g e t s c a l l e d a s s o o n a s t h e p l u g i n i s
* loaded by converse . js ' s plugin machinery .
* /
2017-07-10 17:46:22 +02:00
const { _converse } = this ,
{ _ _ } = _converse ;
2016-12-20 11:42:20 +01:00
2017-07-05 11:03:13 +02:00
_converse . api . settings . update ( {
2017-02-15 11:20:57 +01:00
allow _otr : true ,
cache _otr _key : false ,
use _otr _by _default : false
} ) ;
2016-02-28 09:30:07 +01:00
// Translation aware constants
// ---------------------------
// We can only call the __ translation method *after* converse.js
// has been initialized and with it the i18n machinery. That's why
// we do it here in the "initialize" method and not at the top of
// the module.
OTR _TRANSLATED _MAPPING [ UNENCRYPTED ] = _ _ ( 'unencrypted' ) ;
OTR _TRANSLATED _MAPPING [ UNVERIFIED ] = _ _ ( 'unverified' ) ;
OTR _TRANSLATED _MAPPING [ VERIFIED ] = _ _ ( 'verified' ) ;
OTR _TRANSLATED _MAPPING [ FINISHED ] = _ _ ( 'finished' ) ;
2016-02-13 23:58:50 +01:00
// Only allow OTR if we have the capability
2016-12-20 10:30:20 +01:00
_converse . allow _otr = _converse . allow _otr && HAS _CRYPTO ;
2016-02-13 23:58:50 +01:00
// Only use OTR by default if allow OTR is enabled to begin with
2016-12-20 10:30:20 +01:00
_converse . use _otr _by _default = _converse . use _otr _by _default && _converse . allow _otr ;
2016-02-13 23:58:50 +01:00
}
} ) ;
} ) ) ;