Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
converse.js
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
converse.js
Commits
14447942
Commit
14447942
authored
Nov 18, 2020
by
JC Brand
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
omemo: Move functions out of the plugin body
parent
8a857e39
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
96 additions
and
87 deletions
+96
-87
src/converse-omemo.js
src/converse-omemo.js
+96
-87
No files found.
src/converse-omemo.js
View file @
14447942
...
@@ -40,6 +40,53 @@ class IQError extends Error {
...
@@ -40,6 +40,53 @@ class IQError extends Error {
}
}
}
}
function
parseEncryptedMessage
(
stanza
,
attrs
)
{
if
(
attrs
.
is_encrypted
&&
attrs
.
encrypted
.
key
)
{
// https://xmpp.org/extensions/xep-0384.html#usecases-receiving
if
(
attrs
.
encrypted
.
prekey
===
true
)
{
return
decryptPrekeyWhisperMessage
(
attrs
);
}
else
{
return
decryptWhisperMessage
(
attrs
);
}
}
else
{
return
attrs
;
}
}
function
onChatBoxesInitialized
()
{
_converse
.
chatboxes
.
on
(
'
add
'
,
chatbox
=>
{
checkOMEMOSupported
(
chatbox
);
if
(
chatbox
.
get
(
'
type
'
)
===
_converse
.
CHATROOMS_TYPE
)
{
chatbox
.
occupants
.
on
(
'
add
'
,
o
=>
onOccupantAdded
(
chatbox
,
o
));
chatbox
.
features
.
on
(
'
change
'
,
()
=>
checkOMEMOSupported
(
chatbox
));
}
});
}
function
onChatInitialized
(
view
)
{
view
.
listenTo
(
view
.
model
.
messages
,
'
add
'
,
(
message
)
=>
{
if
(
message
.
get
(
'
is_encrypted
'
)
&&
!
message
.
get
(
'
is_error
'
))
{
view
.
model
.
save
(
'
omemo_supported
'
,
true
);
}
});
view
.
listenTo
(
view
.
model
,
'
change:omemo_supported
'
,
()
=>
{
if
(
!
view
.
model
.
get
(
'
omemo_supported
'
)
&&
view
.
model
.
get
(
'
omemo_active
'
))
{
view
.
model
.
set
(
'
omemo_active
'
,
false
);
}
else
{
// Manually trigger an update, setting omemo_active to
// false above will automatically trigger one.
view
.
el
.
querySelector
(
'
converse-chat-toolbar
'
)?.
requestUpdate
();
}
});
view
.
listenTo
(
view
.
model
,
'
change:omemo_active
'
,
()
=>
{
view
.
el
.
querySelector
(
'
converse-chat-toolbar
'
).
requestUpdate
();
});
}
const
omemo
=
converse
.
env
.
omemo
=
{
const
omemo
=
converse
.
env
.
omemo
=
{
async
encryptMessage
(
plaintext
)
{
async
encryptMessage
(
plaintext
)
{
...
@@ -511,6 +558,52 @@ function getOMEMOToolbarButton (toolbar_el, buttons) {
...
@@ -511,6 +558,52 @@ function getOMEMOToolbarButton (toolbar_el, buttons) {
}
}
/**
* Mixin object that contains OMEMO-related methods for
* {@link _converse.ChatBox} or {@link _converse.ChatRoom} objects.
*
* @typedef {Object} OMEMOEnabledChatBox
*/
const
OMEMOEnabledChatBox
=
{
encryptKey
(
plaintext
,
device
)
{
return
getSessionCipher
(
device
.
get
(
'
jid
'
),
device
.
get
(
'
id
'
))
.
encrypt
(
plaintext
)
.
then
(
payload
=>
({
'
payload
'
:
payload
,
'
device
'
:
device
}));
},
handleMessageSendError
(
e
)
{
if
(
e
.
name
===
'
IQError
'
)
{
this
.
save
(
'
omemo_supported
'
,
false
);
const
err_msgs
=
[];
if
(
sizzle
(
`presence-subscription-required[xmlns="
${
Strophe
.
NS
.
PUBSUB_ERROR
}
"]`
,
e
.
iq
).
length
)
{
err_msgs
.
push
(
__
(
"
Sorry, we're unable to send an encrypted message because %1$s
"
+
"
requires you to be subscribed to their presence in order to see their OMEMO information
"
,
e
.
iq
.
getAttribute
(
'
from
'
))
);
}
else
if
(
sizzle
(
`remote-server-not-found[xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"]`
,
e
.
iq
).
length
)
{
err_msgs
.
push
(
__
(
"
Sorry, we're unable to send an encrypted message because the remote server for %1$s could not be found
"
,
e
.
iq
.
getAttribute
(
'
from
'
))
);
}
else
{
err_msgs
.
push
(
__
(
"
Unable to send an encrypted message due to an unexpected error.
"
));
err_msgs
.
push
(
e
.
iq
.
outerHTML
);
}
api
.
alert
(
'
error
'
,
__
(
'
Error
'
),
err_msgs
);
log
.
error
(
e
);
}
else
if
(
e
.
user_facing
)
{
api
.
alert
(
'
error
'
,
__
(
'
Error
'
),
[
e
.
message
]);
log
.
error
(
e
);
}
else
{
throw
e
;
}
}
}
converse
.
plugins
.
add
(
'
converse-omemo
'
,
{
converse
.
plugins
.
add
(
'
converse-omemo
'
,
{
enabled
(
_converse
)
{
enabled
(
_converse
)
{
...
@@ -642,52 +735,10 @@ converse.plugins.add('converse-omemo', {
...
@@ -642,52 +735,10 @@ converse.plugins.add('converse-omemo', {
*/
*/
api
.
settings
.
extend
({
'
omemo_default
'
:
false
});
api
.
settings
.
extend
({
'
omemo_default
'
:
false
});
api
.
promises
.
add
([
'
OMEMOInitialized
'
]);
api
.
promises
.
add
([
'
OMEMOInitialized
'
]);
_converse
.
NUM_PREKEYS
=
100
;
// Set here so that tests can override
_converse
.
NUM_PREKEYS
=
100
;
// Set here so that tests can override
/**
* Mixin object that contains OMEMO-related methods for
* {@link _converse.ChatBox} or {@link _converse.ChatRoom} objects.
*
* @typedef {Object} OMEMOEnabledChatBox
*/
const
OMEMOEnabledChatBox
=
{
encryptKey
(
plaintext
,
device
)
{
return
getSessionCipher
(
device
.
get
(
'
jid
'
),
device
.
get
(
'
id
'
))
.
encrypt
(
plaintext
)
.
then
(
payload
=>
({
'
payload
'
:
payload
,
'
device
'
:
device
}));
},
handleMessageSendError
(
e
)
{
if
(
e
.
name
===
'
IQError
'
)
{
this
.
save
(
'
omemo_supported
'
,
false
);
const
err_msgs
=
[];
if
(
sizzle
(
`presence-subscription-required[xmlns="
${
Strophe
.
NS
.
PUBSUB_ERROR
}
"]`
,
e
.
iq
).
length
)
{
err_msgs
.
push
(
__
(
"
Sorry, we're unable to send an encrypted message because %1$s
"
+
"
requires you to be subscribed to their presence in order to see their OMEMO information
"
,
e
.
iq
.
getAttribute
(
'
from
'
)));
}
else
if
(
sizzle
(
`remote-server-not-found[xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"]`
,
e
.
iq
).
length
)
{
err_msgs
.
push
(
__
(
"
Sorry, we're unable to send an encrypted message because the remote server for %1$s could not be found
"
,
e
.
iq
.
getAttribute
(
'
from
'
)));
}
else
{
err_msgs
.
push
(
__
(
"
Unable to send an encrypted message due to an unexpected error.
"
));
err_msgs
.
push
(
e
.
iq
.
outerHTML
);
}
api
.
alert
(
'
error
'
,
__
(
'
Error
'
),
err_msgs
);
log
.
error
(
e
);
}
else
if
(
e
.
user_facing
)
{
api
.
alert
(
'
error
'
,
__
(
'
Error
'
),
[
e
.
message
]);
log
.
error
(
e
);
}
else
{
throw
e
;
}
}
}
Object
.
assign
(
_converse
.
ChatBox
.
prototype
,
OMEMOEnabledChatBox
);
Object
.
assign
(
_converse
.
ChatBox
.
prototype
,
OMEMOEnabledChatBox
);
...
@@ -1235,53 +1286,12 @@ converse.plugins.add('converse-omemo', {
...
@@ -1235,53 +1286,12 @@ converse.plugins.add('converse-omemo', {
}
}
});
});
function
parseEncryptedMessage
(
stanza
,
attrs
)
{
if
(
attrs
.
is_encrypted
&&
attrs
.
encrypted
.
key
)
{
// https://xmpp.org/extensions/xep-0384.html#usecases-receiving
if
(
attrs
.
encrypted
.
prekey
===
true
)
{
return
decryptPrekeyWhisperMessage
(
attrs
);
}
else
{
return
decryptWhisperMessage
(
attrs
);
}
}
else
{
return
attrs
;
}
}
/******************** Event Handlers ********************/
/******************** Event Handlers ********************/
api
.
waitUntil
(
'
chatBoxesInitialized
'
).
then
(
onChatBoxesInitialized
);
api
.
listen
.
on
(
'
parseMessage
'
,
parseEncryptedMessage
);
api
.
listen
.
on
(
'
parseMessage
'
,
parseEncryptedMessage
);
api
.
listen
.
on
(
'
parseMUCMessage
'
,
parseEncryptedMessage
);
api
.
listen
.
on
(
'
parseMUCMessage
'
,
parseEncryptedMessage
);
api
.
waitUntil
(
'
chatBoxesInitialized
'
).
then
(()
=>
_converse
.
chatboxes
.
on
(
'
add
'
,
chatbox
=>
{
checkOMEMOSupported
(
chatbox
);
if
(
chatbox
.
get
(
'
type
'
)
===
_converse
.
CHATROOMS_TYPE
)
{
chatbox
.
occupants
.
on
(
'
add
'
,
o
=>
onOccupantAdded
(
chatbox
,
o
));
chatbox
.
features
.
on
(
'
change
'
,
()
=>
checkOMEMOSupported
(
chatbox
));
}
})
);
const
onChatInitialized
=
view
=>
{
view
.
listenTo
(
view
.
model
.
messages
,
'
add
'
,
(
message
)
=>
{
if
(
message
.
get
(
'
is_encrypted
'
)
&&
!
message
.
get
(
'
is_error
'
))
{
view
.
model
.
save
(
'
omemo_supported
'
,
true
);
}
});
view
.
listenTo
(
view
.
model
,
'
change:omemo_supported
'
,
()
=>
{
if
(
!
view
.
model
.
get
(
'
omemo_supported
'
)
&&
view
.
model
.
get
(
'
omemo_active
'
))
{
view
.
model
.
set
(
'
omemo_active
'
,
false
);
}
else
{
// Manually trigger an update, setting omemo_active to
// false above will automatically trigger one.
view
.
el
.
querySelector
(
'
converse-chat-toolbar
'
)?.
requestUpdate
();
}
});
view
.
listenTo
(
view
.
model
,
'
change:omemo_active
'
,
()
=>
{
view
.
el
.
querySelector
(
'
converse-chat-toolbar
'
).
requestUpdate
();
});
}
api
.
listen
.
on
(
'
chatBoxViewInitialized
'
,
onChatInitialized
);
api
.
listen
.
on
(
'
chatBoxViewInitialized
'
,
onChatInitialized
);
api
.
listen
.
on
(
'
chatRoomViewInitialized
'
,
onChatInitialized
);
api
.
listen
.
on
(
'
chatRoomViewInitialized
'
,
onChatInitialized
);
...
@@ -1292,7 +1302,7 @@ converse.plugins.add('converse-omemo', {
...
@@ -1292,7 +1302,7 @@ converse.plugins.add('converse-omemo', {
api
.
listen
.
on
(
'
addClientFeatures
'
,
api
.
listen
.
on
(
'
addClientFeatures
'
,
()
=>
api
.
disco
.
own
.
features
.
add
(
`
${
Strophe
.
NS
.
OMEMO_DEVICELIST
}
+notify`
));
()
=>
api
.
disco
.
own
.
features
.
add
(
`
${
Strophe
.
NS
.
OMEMO_DEVICELIST
}
+notify`
));
api
.
listen
.
on
(
'
userDetailsModalInitialized
'
,
(
contact
)
=>
{
api
.
listen
.
on
(
'
userDetailsModalInitialized
'
,
contact
=>
{
const
jid
=
contact
.
get
(
'
jid
'
);
const
jid
=
contact
.
get
(
'
jid
'
);
_converse
.
generateFingerprints
(
jid
).
catch
(
e
=>
log
.
error
(
e
));
_converse
.
generateFingerprints
(
jid
).
catch
(
e
=>
log
.
error
(
e
));
});
});
...
@@ -1312,7 +1322,6 @@ converse.plugins.add('converse-omemo', {
...
@@ -1312,7 +1322,6 @@ converse.plugins.add('converse-omemo', {
/************************ API ************************/
/************************ API ************************/
Object
.
assign
(
_converse
.
api
,
{
Object
.
assign
(
_converse
.
api
,
{
/**
/**
* The "omemo" namespace groups methods relevant to OMEMO
* The "omemo" namespace groups methods relevant to OMEMO
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment