From 2a8352c5439129798b5141ec021b3b9405840a8a Mon Sep 17 00:00:00 2001 From: Samuel Hand Date: Tue, 19 Jun 2018 11:26:31 +0100 Subject: Database migration and code cleanup --- plugins/omemo/src/contact_details_dialog.vala | 18 +++++---- plugins/omemo/src/database.vala | 29 +++++++++----- plugins/omemo/src/manager.vala | 55 +++++---------------------- plugins/omemo/src/stream_module.vala | 22 ++--------- 4 files changed, 43 insertions(+), 81 deletions(-) (limited to 'plugins/omemo') diff --git a/plugins/omemo/src/contact_details_dialog.vala b/plugins/omemo/src/contact_details_dialog.vala index 3b60580e..3ee6380a 100644 --- a/plugins/omemo/src/contact_details_dialog.vala +++ b/plugins/omemo/src/contact_details_dialog.vala @@ -20,11 +20,12 @@ public class ContactDetailsDialog : Gtk.Dialog { private void set_device_trust(Row device, bool trust) { + Database.IdentityMetaTable.TrustLevel trust_level = trust ? Database.IdentityMetaTable.TrustLevel.TRUSTED : Database.IdentityMetaTable.TrustLevel.UNTRUSTED; plugin.db.identity_meta.update() .with(plugin.db.identity_meta.identity_id, "=", account.id) .with(plugin.db.identity_meta.address_name, "=", device[plugin.db.identity_meta.address_name]) .with(plugin.db.identity_meta.device_id, "=", device[plugin.db.identity_meta.device_id]) - .set(plugin.db.identity_meta.trusted_identity, trust).perform(); + .set(plugin.db.identity_meta.trusted_identity, trust_level).perform(); if(!trust) { plugin.app.stream_interactor.module_manager.get_module(account, StreamModule.IDENTITY).untrust_device(jid, device[plugin.db.identity_meta.device_id]); @@ -33,11 +34,12 @@ public class ContactDetailsDialog : Gtk.Dialog { } } - private void add_fingerprint(Row device, int row, bool trust) { + private void add_fingerprint(Row device, int row, Database.IdentityMetaTable.TrustLevel trust) { string res = fingerprint_markup(fingerprint_from_base64(device[plugin.db.identity_meta.identity_key_public_base64])); Label lbl = new Label(res) { use_markup=true, justify=Justification.RIGHT, visible=true, margin = 8, halign = Align.START, valign = Align.CENTER }; - Switch tgl = new Switch() {visible = true, halign = Align.END, valign = Align.CENTER, margin = 8, hexpand = true, active = trust }; + //TODO: handle display of verified devices + Switch tgl = new Switch() {visible = true, halign = Align.END, valign = Align.CENTER, margin = 8, hexpand = true, active = (trust == Database.IdentityMetaTable.TrustLevel.TRUSTED) }; tgl.state_set.connect((active) => { set_device_trust(device, active); @@ -55,17 +57,17 @@ public class ContactDetailsDialog : Gtk.Dialog { this.jid = jid; int i = 0; - foreach (Row device in plugin.db.identity_meta.with_address(jid.to_string()).without_null(plugin.db.identity_meta.trusted_identity).with(plugin.db.identity_meta.identity_id, "=", account.id)) { + foreach (Row device in plugin.db.identity_meta.with_address(jid.to_string()).with(plugin.db.identity_meta.trusted_identity, "!=", Database.IdentityMetaTable.TrustLevel.UNKNOWN).with(plugin.db.identity_meta.identity_id, "=", account.id)) { if(device[plugin.db.identity_meta.identity_key_public_base64] == null) continue; - add_fingerprint(device, i, device[plugin.db.identity_meta.trusted_identity]); + add_fingerprint(device, i, (Database.IdentityMetaTable.TrustLevel) device[plugin.db.identity_meta.trusted_identity]); i++; } int j = 0; - foreach (Row device in plugin.db.identity_meta.with_address(jid.to_string()).with_null(plugin.db.identity_meta.trusted_identity).with(plugin.db.identity_meta.identity_id, "=", account.id)) { + foreach (Row device in plugin.db.identity_meta.with_address(jid.to_string()).with(plugin.db.identity_meta.trusted_identity, "=", Database.IdentityMetaTable.TrustLevel.UNKNOWN).with(plugin.db.identity_meta.identity_id, "=", account.id)) { if(device[plugin.db.identity_meta.identity_key_public_base64] == null) continue; @@ -85,7 +87,7 @@ public class ContactDetailsDialog : Gtk.Dialog { fingerprints_prompt.remove(lbl); j--; - add_fingerprint(device, i, true); + add_fingerprint(device, i, Database.IdentityMetaTable.TrustLevel.TRUSTED); i++; if(j == 0) @@ -102,7 +104,7 @@ public class ContactDetailsDialog : Gtk.Dialog { fingerprints_prompt.remove(lbl); j--; - add_fingerprint(device, i, false); + add_fingerprint(device, i, Database.IdentityMetaTable.TrustLevel.UNTRUSTED); i++; if(j == 0) diff --git a/plugins/omemo/src/database.vala b/plugins/omemo/src/database.vala index 2b295322..48fddde2 100644 --- a/plugins/omemo/src/database.vala +++ b/plugins/omemo/src/database.vala @@ -6,14 +6,27 @@ using Dino.Entities; namespace Dino.Plugins.Omemo { public class Database : Qlite.Database { - private const int VERSION = 1; + private const int VERSION = 2; public class IdentityMetaTable : Table { - public Column identity_id = new Column.Integer("identity_id") { not_null = true }; + public enum TrustLevel { + VERIFIED, + TRUSTED, + UNTRUSTED, + UNKNOWN; + + public string to_string() { + int val = this; + return val.to_string(); + } + } + + //Default to provide backwards compatability + public Column identity_id = new Column.Integer("identity_id") { not_null = true, min_version = 2, default = "-1" }; public Column address_name = new Column.Text("address_name") { not_null = true }; public Column device_id = new Column.Integer("device_id") { not_null = true }; public Column identity_key_public_base64 = new Column.Text("identity_key_public_base64"); - public Column trusted_identity = new Column.BoolInt("trusted_identity"); + public Column trusted_identity = new Column.Integer("trusted_identity") { not_null = true, default = TrustLevel.UNKNOWN.to_string() }; public Column now_active = new Column.BoolInt("now_active") { default = "1" }; public Column last_active = new Column.Long("last_active"); @@ -41,16 +54,14 @@ public class Database : Qlite.Database { } } - public int64 insert_device_bundle(int32 identity_id, string address_name, int device_id, Bundle bundle, bool? trust) { + public int64 insert_device_bundle(int32 identity_id, string address_name, int device_id, Bundle bundle, TrustLevel trust) { if (bundle == null || bundle.identity_key == null) return -1; - UpsertBuilder query = upsert() + return upsert() .value(this.identity_id, identity_id, true) .value(this.address_name, address_name, true) .value(this.device_id, device_id, true) - .value(this.identity_key_public_base64, Base64.encode(bundle.identity_key.serialize())); - if(trust != null) - query.value(this.trusted_identity, trust); - return query.perform(); + .value(this.identity_key_public_base64, Base64.encode(bundle.identity_key.serialize())) + .value(this.trusted_identity, trust).perform(); } } diff --git a/plugins/omemo/src/manager.vala b/plugins/omemo/src/manager.vala index 3047410c..dbb016a4 100644 --- a/plugins/omemo/src/manager.vala +++ b/plugins/omemo/src/manager.vala @@ -121,10 +121,10 @@ public class Manager : StreamInteractionModule, Object { 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); } if (state.waiting_other_sessions > 0 && message.counterpart != null) { - module.start_sessions_with((!)stream, ((!)message.counterpart).bare_jid); + module.fetch_bundles((!)stream, ((!)message.counterpart).bare_jid); } if (state.waiting_other_devicelist && message.counterpart != null) { module.request_user_devicelist((!)stream, ((!)message.counterpart).bare_jid); @@ -138,40 +138,12 @@ public class Manager : StreamInteractionModule, Object { 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).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"); - HashSet send_now = new HashSet(); - lock (message_states) { - foreach (Entities.Message msg in message_states.keys) { - if (!msg.account.equals(account)) continue; - 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--; - } - if (state.should_retry_now()) { - send_now.add(msg); - state.active_send_attempt = true; - } - } - } - 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); - 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"); HashSet send_now = new HashSet(); @@ -231,30 +203,21 @@ public class Manager : StreamInteractionModule, Object { .with(db.identity_meta.identity_key_public_base64, "=", Base64.encode(bundle.identity_key.serialize())) .count() > 0); - bool? trusted = null; + Database.IdentityMetaTable.TrustLevel trusted = Database.IdentityMetaTable.TrustLevel.UNKNOWN; foreach(Row row in db.identity_meta.with_address(jid.bare_jid.to_string()) .with(db.identity_meta.identity_id, "=", account.id) .with(db.identity_meta.device_id, "=", device_id)) { - trusted = row[db.identity_meta.trusted_identity]; + trusted = (Database.IdentityMetaTable.TrustLevel) row[db.identity_meta.trusted_identity]; break; } - if(db.identity_meta.with_address(jid.bare_jid.to_string()) - .with(db.identity_meta.identity_id, "=", account.id) - .with(db.identity_meta.device_id, "=", device_id) - .with_null(db.identity_meta.trusted_identity).count() > 0) - trusted = null; - if(untrust) - trusted = null; - else if (blind_trust && trusted == null) - trusted = true; + 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); - if(trusted == null) - trusted = false; - XmppStream? stream = stream_interactor.get_stream(account); if(stream == null) return; StreamModule? module = ((!)stream).get_module(StreamModule.IDENTITY); @@ -268,11 +231,11 @@ public class Manager : StreamInteractionModule, Object { if (!msg.account.equals(account)) continue; MessageState state = message_states[msg]; - if (trusted != true) { + if (trusted != Database.IdentityMetaTable.TrustLevel.TRUSTED && trusted != Database.IdentityMetaTable.TrustLevel.VERIFIED) { module.untrust_device(jid, device_id); } else { if(account.bare_jid.equals(jid) || (msg.counterpart != null && msg.counterpart.equals_bare(jid))) - session_created = module.create_session_if_needed(stream, jid, device_id, bundle); + session_created = module.start_session(stream, jid, device_id, bundle); } if (account.bare_jid.equals(jid) && session_created) { state.waiting_own_sessions--; diff --git a/plugins/omemo/src/stream_module.vala b/plugins/omemo/src/stream_module.vala index a84c3582..83a3dd54 100644 --- a/plugins/omemo/src/stream_module.vala +++ b/plugins/omemo/src/stream_module.vala @@ -26,8 +26,6 @@ public class StreamModule : XmppStreamModule { public signal void store_created(Store store); public signal void device_list_loaded(Jid jid); public signal void bundle_fetched(Jid jid, int device_id, Bundle bundle); - public signal void session_started(Jid jid, int device_id); - public signal void session_start_failed(Jid jid, int device_id); public EncryptState encrypt(MessageStanza message, Jid self_jid) { EncryptState status = new EncryptState(); @@ -181,7 +179,7 @@ public class StreamModule : XmppStreamModule { device_list_loaded(jid); } - public void start_sessions_with(XmppStream stream, Jid jid) { + public void fetch_bundles(XmppStream stream, Jid jid) { if (!device_lists.has_key(jid)) { return; } @@ -191,7 +189,7 @@ public class StreamModule : XmppStreamModule { address.device_id = device_id; try { if (!store.contains_session(address)) { - start_session_with(stream, jid, device_id); + fetch_bundle(stream, jid, device_id); } } catch (Error e) { // Ignore @@ -201,21 +199,11 @@ public class StreamModule : XmppStreamModule { address.device_id = 0; } - public void start_session_with(XmppStream stream, Jid jid, int device_id) { - if (active_bundle_requests.add(jid.bare_jid.to_string() + @":$device_id")) { - if (Plugin.DEBUG) print(@"OMEMO: Asking for bundle from $(jid.bare_jid.to_string()):$device_id\n"); - stream.get_module(Pubsub.Module.IDENTITY).request(stream, jid.bare_jid, @"$NODE_BUNDLES:$device_id", (stream, jid, id, node) => { - on_other_bundle_result(stream, jid, device_id, id, node); - }); - } - } - public void fetch_bundle(XmppStream stream, Jid jid, int device_id) { if (active_bundle_requests.add(jid.bare_jid.to_string() + @":$device_id")) { if (Plugin.DEBUG) print(@"OMEMO: Asking for bundle from $(jid.bare_jid.to_string()):$device_id\n"); stream.get_module(Pubsub.Module.IDENTITY).request(stream, jid.bare_jid, @"$NODE_BUNDLES:$device_id", (stream, jid, id, node) => { - stream.get_module(IDENTITY).active_bundle_requests.remove(jid.bare_jid.to_string() + @":$device_id"); - bundle_fetched(jid, device_id, new Bundle(node)); + on_other_bundle_result(stream, jid, device_id, id, node); }); } } @@ -240,7 +228,6 @@ public class StreamModule : XmppStreamModule { } ignored_devices[jid].add(device_id); } - //session_start_failed(jid, device_id); } public bool is_ignored_device(Jid jid, int32 device_id) { @@ -261,7 +248,7 @@ public class StreamModule : XmppStreamModule { stream.get_module(IDENTITY).active_bundle_requests.remove(jid.bare_jid.to_string() + @":$device_id"); } - public bool create_session_if_needed(XmppStream stream, Jid jid, int32 device_id, Bundle bundle) { + public bool start_session(XmppStream stream, Jid jid, int32 device_id, Bundle bundle) { bool fail = false; int32 signed_pre_key_id = bundle.signed_pre_key_id; ECPublicKey? signed_pre_key = bundle.signed_pre_key; @@ -285,7 +272,6 @@ public class StreamModule : XmppStreamModule { } SessionBuilder builder = store.create_session_builder(address); builder.process_pre_key_bundle(create_pre_key_bundle(device_id, device_id, pre_key_id, pre_key, signed_pre_key_id, signed_pre_key, signed_pre_key_signature, identity_key)); - //stream.get_module(IDENTITY).session_started(jid, device_id); } catch (Error e) { fail = true; } -- cgit v1.2.3-70-g09d2