aboutsummaryrefslogtreecommitdiff
path: root/plugins/omemo/src/manager.vala
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/omemo/src/manager.vala')
-rw-r--r--plugins/omemo/src/manager.vala192
1 files changed, 140 insertions, 52 deletions
diff --git a/plugins/omemo/src/manager.vala b/plugins/omemo/src/manager.vala
index eb0c6378..8654a4f5 100644
--- a/plugins/omemo/src/manager.vala
+++ b/plugins/omemo/src/manager.vala
@@ -12,6 +12,7 @@ public class Manager : StreamInteractionModule, Object {
private StreamInteractor stream_interactor;
private Database db;
+ private TrustManager trust_manager;
private Map<Entities.Message, MessageState> message_states = new HashMap<Entities.Message, MessageState>(Entities.Message.hash_func, Entities.Message.equals_func);
private ReceivedMessageListener received_message_listener = new ReceivedMessageListener();
@@ -21,7 +22,7 @@ public class Manager : StreamInteractionModule, Object {
public int waiting_other_sessions { get; set; }
public int waiting_own_sessions { get; set; }
public bool waiting_own_devicelist { get; set; }
- public bool waiting_other_devicelist { get; set; }
+ public int waiting_other_devicelists { get; set; }
public bool force_next_attempt { get; set; }
public bool will_send_now { get; private set; }
public bool active_send_attempt { get; set; }
@@ -37,12 +38,12 @@ public class Manager : StreamInteractionModule, Object {
this.waiting_other_sessions = new_try.other_unknown;
this.waiting_own_sessions = new_try.own_unknown;
this.waiting_own_devicelist = !new_try.own_list;
- this.waiting_other_devicelist = !new_try.other_list;
+ this.waiting_other_devicelists = new_try.other_waiting_lists;
this.active_send_attempt = false;
will_send_now = false;
if (new_try.other_failure > 0 || (new_try.other_lost == new_try.other_devices && new_try.other_devices > 0)) {
msg.marked = Entities.Message.Marked.WONTSEND;
- } else if (new_try.other_unknown > 0 || new_try.own_unknown > 0 || !new_try.other_list || !new_try.own_list || new_try.own_devices == 0) {
+ } else if (new_try.other_unknown > 0 || new_try.own_unknown > 0 || new_try.other_waiting_lists > 0 || !new_try.own_list || new_try.own_devices == 0) {
msg.marked = Entities.Message.Marked.UNSENT;
} else if (!new_try.encrypted) {
msg.marked = Entities.Message.Marked.WONTSEND;
@@ -52,11 +53,11 @@ public class Manager : StreamInteractionModule, Object {
}
public bool should_retry_now() {
- return !waiting_own_devicelist && !waiting_other_devicelist && waiting_other_sessions <= 0 && waiting_own_sessions <= 0 && !active_send_attempt;
+ return !waiting_own_devicelist && waiting_other_devicelists <= 0 && waiting_other_sessions <= 0 && waiting_own_sessions <= 0 && !active_send_attempt;
}
public string to_string() {
- return @"MessageState (waiting=(others=$waiting_other_sessions, own=$waiting_own_sessions, other_list=$waiting_other_devicelist, own_list=$waiting_own_devicelist))";
+ return @"MessageState (waiting=(others=$waiting_other_sessions, own=$waiting_own_sessions, other_lists=$waiting_other_devicelists, own_list=$waiting_own_devicelist))";
}
}
@@ -64,6 +65,8 @@ public class Manager : StreamInteractionModule, Object {
this.stream_interactor = stream_interactor;
this.db = db;
+ this.trust_manager = new TrustManager(stream_interactor, db);
+
stream_interactor.stream_negotiated.connect(on_stream_negotiated);
stream_interactor.account_added.connect(on_account_added);
stream_interactor.get_module(MessageProcessor.IDENTITY).received_pipeline.connect(received_message_listener);
@@ -85,6 +88,23 @@ public class Manager : StreamInteractionModule, Object {
}
}
+ private Gee.List<Jid> get_occupants(Jid jid, Account account){
+ Gee.List<Jid> occupants = new ArrayList<Jid>(Jid.equals_bare_func);
+ if(!stream_interactor.get_module(MucManager.IDENTITY).is_groupchat(jid, account)){
+ occupants.add(jid);
+ }
+ Gee.List<Jid>? occupant_jids = stream_interactor.get_module(MucManager.IDENTITY).get_offline_members(jid, account);
+ if(occupant_jids == null) {
+ return occupants;
+ }
+ foreach (Jid occupant in occupant_jids) {
+ if(!occupant.equals(account.bare_jid)){
+ occupants.add(occupant.bare_jid);
+ }
+ }
+ return occupants;
+ }
+
private void on_pre_message_send(Entities.Message message, Xmpp.MessageStanza message_stanza, Conversation conversation) {
if (message.encryption == Encryption.OMEMO) {
XmppStream? stream = stream_interactor.get_stream(conversation.account);
@@ -98,7 +118,20 @@ public class Manager : StreamInteractionModule, Object {
return;
}
StreamModule module = (!)module_;
- EncryptState enc_state = module.encrypt(message_stanza, conversation.account.bare_jid);
+
+ Gee.List<Jid> recipients;
+ if (message_stanza.type_ == MessageStanza.TYPE_GROUPCHAT) {
+ recipients = get_occupants((!)message.to.bare_jid, conversation.account);
+ if (recipients.size == 0) {
+ message.marked = Entities.Message.Marked.WONTSEND;
+ return;
+ }
+ } else {
+ recipients = new ArrayList<Jid>(Jid.equals_bare_func);
+ recipients.add(message_stanza.to);
+ }
+
+ EncryptState enc_state = trust_manager.encrypt(message_stanza, conversation.account.bare_jid, recipients, stream, conversation.account);
MessageState state;
lock (message_states) {
if (message_states.has_key(message)) {
@@ -116,17 +149,22 @@ public class Manager : StreamInteractionModule, Object {
if (!state.will_send_now) {
if (message.marked == Entities.Message.Marked.WONTSEND) {
if (Plugin.DEBUG) print(@"OMEMO: message was not sent: $state\n");
+ message_states.unset(message);
} else {
if (Plugin.DEBUG) print(@"OMEMO: message will be delayed: $state\n");
if (state.waiting_own_sessions > 0) {
- module.start_sessions_with((!)stream, conversation.account.bare_jid);
+ module.fetch_bundles((!)stream, conversation.account.bare_jid, trust_manager.get_trusted_devices(conversation.account, conversation.account.bare_jid));
}
if (state.waiting_other_sessions > 0 && message.counterpart != null) {
- module.start_sessions_with((!)stream, ((!)message.counterpart).bare_jid);
+ foreach(Jid jid in get_occupants(((!)message.counterpart).bare_jid, conversation.account)) {
+ module.fetch_bundles((!)stream, jid, trust_manager.get_trusted_devices(conversation.account, jid));
+ }
}
- if (state.waiting_other_devicelist && message.counterpart != null) {
- module.request_user_devicelist((!)stream, ((!)message.counterpart).bare_jid);
+ if (state.waiting_other_devicelists > 0 && message.counterpart != null) {
+ foreach(Jid jid in get_occupants(((!)message.counterpart).bare_jid, conversation.account)) {
+ module.request_user_devicelist((!)stream, jid);
+ }
}
}
}
@@ -135,27 +173,51 @@ public class Manager : StreamInteractionModule, Object {
private void on_account_added(Account account) {
stream_interactor.module_manager.get_module(account, StreamModule.IDENTITY).store_created.connect((store) => on_store_created(account, store));
- stream_interactor.module_manager.get_module(account, StreamModule.IDENTITY).device_list_loaded.connect((jid) => on_device_list_loaded(account, jid));
+ stream_interactor.module_manager.get_module(account, StreamModule.IDENTITY).device_list_loaded.connect((jid, devices) => on_device_list_loaded(account, jid, devices));
stream_interactor.module_manager.get_module(account, StreamModule.IDENTITY).bundle_fetched.connect((jid, device_id, bundle) => on_bundle_fetched(account, jid, device_id, bundle));
- stream_interactor.module_manager.get_module(account, StreamModule.IDENTITY).session_started.connect((jid, device_id) => on_session_started(account, jid, false));
- stream_interactor.module_manager.get_module(account, StreamModule.IDENTITY).session_start_failed.connect((jid, device_id) => on_session_started(account, jid, true));
}
private void on_stream_negotiated(Account account, XmppStream stream) {
stream_interactor.module_manager.get_module(account, StreamModule.IDENTITY).request_user_devicelist(stream, account.bare_jid);
}
- private void on_session_started(Account account, Jid jid, bool failed) {
- if (Plugin.DEBUG) print(@"OMEMO: session start between $(account.bare_jid) and $jid $(failed ? "failed" : "successful")\n");
+ private void on_device_list_loaded(Account account, Jid jid, ArrayList<int32> device_list) {
+ if (Plugin.DEBUG) print(@"OMEMO: received device list for $(account.bare_jid) from $jid\n");
+
+ // Update meta database
+ XmppStream? stream = stream_interactor.get_stream(account);
+ if (stream == null) {
+ return;
+ }
+ StreamModule? module = ((!)stream).get_module(StreamModule.IDENTITY);
+ if (module == null) {
+ return;
+ }
+
+ db.identity_meta.insert_device_list(account.id, jid.bare_jid.to_string(), device_list);
+ int inc = 0;
+ foreach (Row row in db.identity_meta.with_address(account.id, jid.bare_jid.to_string()).with_null(db.identity_meta.identity_key_public_base64)) {
+ module.fetch_bundle(stream, Jid.parse(row[db.identity_meta.address_name]), row[db.identity_meta.device_id]);
+ inc++;
+ }
+ if (inc > 0) {
+ if (Plugin.DEBUG) print(@"OMEMO: new bundles $inc/$(device_list.size) for $jid\n");
+ }
+
+ if (db.trust.select().with(db.trust.identity_id, "=", account.id).with(db.trust.address_name, "=", jid.bare_jid.to_string()).count() == 0) {
+ db.trust.insert().value(db.trust.identity_id, account.id).value(db.trust.address_name, jid.bare_jid.to_string()).value(db.trust.blind_trust, true).perform();
+ }
+
HashSet<Entities.Message> send_now = new HashSet<Entities.Message>();
lock (message_states) {
foreach (Entities.Message msg in message_states.keys) {
if (!msg.account.equals(account)) continue;
+ Gee.List<Jid> occupants = get_occupants(msg.counterpart.bare_jid, account);
MessageState state = message_states[msg];
if (account.bare_jid.equals(jid)) {
- state.waiting_own_sessions--;
- } else if (msg.counterpart != null && msg.counterpart.equals_bare(jid)) {
- state.waiting_other_sessions--;
+ state.waiting_own_devicelist = false;
+ } else if (msg.counterpart != null && occupants.contains(jid)) {
+ state.waiting_other_devicelists--;
}
if (state.should_retry_now()) {
send_now.add(msg);
@@ -165,25 +227,57 @@ public class Manager : StreamInteractionModule, Object {
}
foreach (Entities.Message msg in send_now) {
if (msg.counterpart == null) continue;
- Entities.Conversation? conv = stream_interactor.get_module(ConversationManager.IDENTITY).get_conversation((!)msg.counterpart, account);
+ Entities.Conversation? conv = stream_interactor.get_module(ConversationManager.IDENTITY).get_conversation(((!)msg.counterpart), account);
if (conv == null) continue;
stream_interactor.get_module(MessageProcessor.IDENTITY).send_xmpp_message(msg, (!)conv, true);
}
+
}
- private void on_device_list_loaded(Account account, Jid jid) {
- if (Plugin.DEBUG) print(@"OMEMO: received device list for $(account.bare_jid) from $jid\n");
+ public void on_bundle_fetched(Account account, Jid jid, int32 device_id, Bundle bundle) {
+ bool blind_trust = db.trust.get_blind_trust(account.id, jid.bare_jid.to_string());
+
+ bool untrust = !(blind_trust || db.identity_meta.with_address(account.id, jid.bare_jid.to_string())
+ .with(db.identity_meta.device_id, "=", device_id)
+ .with(db.identity_meta.identity_key_public_base64, "=", Base64.encode(bundle.identity_key.serialize()))
+ .single().row().is_present());
+
+ Database.IdentityMetaTable.TrustLevel trusted = (Database.IdentityMetaTable.TrustLevel) db.identity_meta.with_address(account.id, jid.bare_jid.to_string()).with(db.identity_meta.device_id, "=", device_id).single()[db.identity_meta.trust_level, Database.IdentityMetaTable.TrustLevel.UNKNOWN];
+
+ if(untrust) {
+ trusted = Database.IdentityMetaTable.TrustLevel.UNKNOWN;
+ } else if (blind_trust && trusted == Database.IdentityMetaTable.TrustLevel.UNKNOWN) {
+ trusted = Database.IdentityMetaTable.TrustLevel.TRUSTED;
+ }
+
+ db.identity_meta.insert_device_bundle(account.id, jid.bare_jid.to_string(), device_id, bundle, trusted);
+
+ XmppStream? stream = stream_interactor.get_stream(account);
+ if(stream == null) return;
+ StreamModule? module = ((!)stream).get_module(StreamModule.IDENTITY);
+ if(module == null) return;
+
HashSet<Entities.Message> send_now = new HashSet<Entities.Message>();
lock (message_states) {
foreach (Entities.Message msg in message_states.keys) {
+
+ bool session_created = true;
if (!msg.account.equals(account)) continue;
+ Gee.List<Jid> occupants = get_occupants(msg.counterpart.bare_jid, account);
+
MessageState state = message_states[msg];
- if (account.bare_jid.equals(jid)) {
- state.waiting_own_devicelist = false;
- } else if (msg.counterpart != null && msg.counterpart.equals_bare(jid)) {
- state.waiting_other_devicelist = false;
+
+ if (trusted == Database.IdentityMetaTable.TrustLevel.TRUSTED || trusted == Database.IdentityMetaTable.TrustLevel.VERIFIED) {
+ if(account.bare_jid.equals(jid) || (msg.counterpart != null && (msg.counterpart.equals_bare(jid) || occupants.contains(jid)))) {
+ session_created = module.start_session(stream, jid, device_id, bundle);
+ }
}
- if (state.should_retry_now()) {
+ if (account.bare_jid.equals(jid) && session_created) {
+ state.waiting_own_sessions--;
+ } else if (msg.counterpart != null && (msg.counterpart.equals_bare(jid) || occupants.contains(jid)) && session_created) {
+ state.waiting_other_sessions--;
+ }
+ if (state.should_retry_now()){
send_now.add(msg);
state.active_send_attempt = true;
}
@@ -191,34 +285,10 @@ public class Manager : StreamInteractionModule, Object {
}
foreach (Entities.Message msg in send_now) {
if (msg.counterpart == null) continue;
- Entities.Conversation? conv = stream_interactor.get_module(ConversationManager.IDENTITY).get_conversation(((!)msg.counterpart), account);
+ Entities.Conversation? conv = stream_interactor.get_module(ConversationManager.IDENTITY).get_conversation((!)msg.counterpart, account);
if (conv == null) continue;
stream_interactor.get_module(MessageProcessor.IDENTITY).send_xmpp_message(msg, (!)conv, true);
}
-
- // Update meta database
- XmppStream? stream = stream_interactor.get_stream(account);
- if (stream == null) {
- return;
- }
- StreamModule? module = ((!)stream).get_module(StreamModule.IDENTITY);
- if (module == null) {
- return;
- }
- ArrayList<int32> device_list = module.get_device_list(jid);
- db.identity_meta.insert_device_list(jid.bare_jid.to_string(), device_list);
- int inc = 0;
- foreach (Row row in db.identity_meta.with_address(jid.bare_jid.to_string()).with_null(db.identity_meta.identity_key_public_base64)) {
- module.fetch_bundle(stream, Jid.parse(row[db.identity_meta.address_name]), row[db.identity_meta.device_id]);
- inc++;
- }
- if (inc > 0) {
- if (Plugin.DEBUG) print(@"OMEMO: new bundles $inc/$(device_list.size) for $jid\n");
- }
- }
-
- public void on_bundle_fetched(Account account, Jid jid, int32 device_id, Bundle bundle) {
- db.identity_meta.insert_device_bundle(jid.bare_jid.to_string(), device_id, bundle);
}
private void on_store_created(Account account, Store store) {
@@ -265,7 +335,25 @@ public class Manager : StreamInteractionModule, Object {
if (stream == null) return false;
StreamModule? module = ((!)stream).get_module(StreamModule.IDENTITY);
if (module == null) return false;
- return ((!)module).is_known_address(conversation.counterpart.bare_jid);
+ 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)) {
+ module.request_user_devicelist(stream, jid.bare_jid);
+ return false;
+ }
+ }
+ return true;
+ } else {
+ return false;
+ }
+ } else if (!trust_manager.is_known_address(conversation.account, conversation.counterpart.bare_jid)) {
+ module.request_user_devicelist(stream, conversation.counterpart.bare_jid);
+ return false;
+ }
+ return true;
}
public static void start(StreamInteractor stream_interactor, Database db) {