OMEMO: Rename variables, refactor can_encrypt

This commit is contained in:
fiaxh 2019-05-21 18:06:25 +02:00
parent a7aa5130f8
commit 06db4d0aa6
3 changed files with 25 additions and 27 deletions

View file

@ -358,23 +358,21 @@ public class Manager : StreamInteractionModule, Object {
public bool can_encrypt(Entities.Conversation conversation) {
XmppStream? stream = stream_interactor.get_stream(conversation.account);
if (stream == null) return false;
if (stream_interactor.get_module(MucManager.IDENTITY).is_groupchat(conversation.counterpart, conversation.account)){
Xep.Muc.Flag? flag = stream.get_flag(Xep.Muc.Flag.IDENTITY);
if (flag == null) return false;
if (flag.has_room_feature(conversation.counterpart, Xep.Muc.Feature.NON_ANONYMOUS) && flag.has_room_feature(conversation.counterpart, Xep.Muc.Feature.MEMBERS_ONLY)) {
foreach(Jid jid in stream_interactor.get_module(MucManager.IDENTITY).get_offline_members(conversation.counterpart, conversation.account)) {
if (!trust_manager.is_known_address(conversation.account, jid.bare_jid)) {
debug(@"Can't enable OMEMO for $(conversation.counterpart): missing keys for $(jid.bare_jid)");
return false;
}
}
return true;
} else {
return false;
}
if (stream_interactor.get_module(MucManager.IDENTITY).is_public_room(conversation.account, conversation.counterpart)){
debug("Can't enable OMEMO for %s: Room not members-only or non-anonymous", conversation.counterpart.to_string());
return false;
}
if (stream_interactor.get_module(MucManager.IDENTITY).is_private_room(conversation.account, conversation.counterpart)){
foreach(Jid jid in stream_interactor.get_module(MucManager.IDENTITY).get_offline_members(conversation.counterpart, conversation.account)) {
if (!trust_manager.is_known_address(conversation.account, jid.bare_jid)) {
debug("Can't enable OMEMO for %s: missing keys for %s", conversation.counterpart.to_string(), jid.bare_jid.to_string());
return false;
}
}
return true;
}
return trust_manager.is_known_address(conversation.account, conversation.counterpart.bare_jid);
}

View file

@ -62,7 +62,7 @@ public class TrustManager {
}
}
private StanzaNode create_encrypted_key(uint8[] key, Address address, Store store) throws GLib.Error {
private StanzaNode create_encrypted_key_node(uint8[] key, Address address, Store store) throws GLib.Error {
SessionCipher cipher = store.create_session_cipher(address);
CiphertextMessage device_key = cipher.encrypt(key);
StanzaNode key_node = new StanzaNode.build("key", NS_URI)
@ -108,9 +108,9 @@ public class TrustManager {
Memory.copy(keytag, key, key.length);
Memory.copy((uint8*)keytag + key.length, tag, tag.length);
StanzaNode header;
StanzaNode encrypted = new StanzaNode.build("encrypted", NS_URI).add_self_xmlns()
.put_node(header = new StanzaNode.build("header", NS_URI)
StanzaNode header_node;
StanzaNode encrypted_node = new StanzaNode.build("encrypted", NS_URI).add_self_xmlns()
.put_node(header_node = new StanzaNode.build("header", NS_URI)
.put_attribute("sid", module.store.local_registration_id.to_string())
.put_node(new StanzaNode.build("iv", NS_URI)
.put_node(new StanzaNode.text(Base64.encode(iv)))))
@ -128,8 +128,8 @@ public class TrustManager {
try {
address.name = recipient.bare_jid.to_string();
address.device_id = (int) device_id;
StanzaNode key_node = create_encrypted_key(keytag, address, module.store);
header.put_node(key_node);
StanzaNode key_node = create_encrypted_key_node(keytag, address, module.store);
header_node.put_node(key_node);
status.other_success++;
} catch (Error e) {
if (e.code == ErrorCode.UNKNOWN) status.other_unknown++;
@ -148,8 +148,8 @@ public class TrustManager {
if (device_id != module.store.local_registration_id) {
address.device_id = (int) device_id;
try {
StanzaNode key_node = create_encrypted_key(keytag, address, module.store);
header.put_node(key_node);
StanzaNode key_node = create_encrypted_key_node(keytag, address, module.store);
header_node.put_node(key_node);
status.own_success++;
} catch (Error e) {
if (e.code == ErrorCode.UNKNOWN) status.own_unknown++;
@ -158,7 +158,7 @@ public class TrustManager {
}
}
message.stanza.put_node(encrypted);
message.stanza.put_node(encrypted_node);
Xep.ExplicitEncryption.add_encryption_tag_to_message(message, NS_URI, "OMEMO");
message.body = "[This message is OMEMO encrypted]";
status.encrypted = true;

View file

@ -5,7 +5,7 @@ using Xmpp;
namespace Dino.Plugins.Omemo {
public class Bundle {
private StanzaNode? node;
public StanzaNode? node;
public Bundle(StanzaNode? node) {
this.node = node;