diff options
author | fiaxh <git@lightrise.org> | 2019-12-18 18:23:31 +0100 |
---|---|---|
committer | fiaxh <git@lightrise.org> | 2019-12-18 20:35:04 +0100 |
commit | 321c3529f3fedbd5eaa9cd619ee408fb683921ca (patch) | |
tree | 3743e39ee2a8225ed8d1c2bb1259e17b22a1fa47 | |
parent | 9fe8450571e49aaf376d07b379968c575bc0445c (diff) | |
download | dino-321c3529f3fedbd5eaa9cd619ee408fb683921ca.tar.gz dino-321c3529f3fedbd5eaa9cd619ee408fb683921ca.zip |
Add (partial) support for unique stanza ids (XEP-0359)
-rw-r--r-- | libdino/src/entity/message.vala | 7 | ||||
-rw-r--r-- | libdino/src/service/database.vala | 13 | ||||
-rw-r--r-- | libdino/src/service/message_processor.vala | 39 | ||||
-rw-r--r-- | xmpp-vala/CMakeLists.txt | 1 | ||||
-rw-r--r-- | xmpp-vala/src/module/xep/0045_muc/module.vala | 2 | ||||
-rw-r--r-- | xmpp-vala/src/module/xep/0313_message_archive_management.vala | 7 | ||||
-rw-r--r-- | xmpp-vala/src/module/xep/0359_unique_stable_stanza_ids.vala | 46 |
7 files changed, 103 insertions, 12 deletions
diff --git a/libdino/src/entity/message.vala b/libdino/src/entity/message.vala index 9a25ac0c..4b16d12c 100644 --- a/libdino/src/entity/message.vala +++ b/libdino/src/entity/message.vala @@ -40,6 +40,7 @@ public class Message : Object { public Type type_ { get; set; default = Type.UNKNOWN; } public string? body { get; set; } public string? stanza_id { get; set; } + public string? server_id { get; set; } public DateTime? time { get; set; } /** UTC **/ public DateTime? local_time { get; set; } @@ -63,8 +64,9 @@ public class Message : Object { this.db = db; id = row[db.message.id]; - account = db.get_account_by_id(row[db.message.account_id]); // TODO don’t have to generate acc new + account = db.get_account_by_id(row[db.message.account_id]); stanza_id = row[db.message.stanza_id]; + server_id = row[db.message.server_id]; type_ = (Message.Type) row[db.message.type_]; counterpart = db.get_jid_by_id(row[db.message.counterpart_id]); @@ -108,6 +110,7 @@ public class Message : Object { .value(db.message.encryption, encryption) .value(db.message.marked, marked); if (stanza_id != null) builder.value(db.message.stanza_id, stanza_id); + if (server_id != null) builder.value(db.message.server_id, server_id); id = (int) builder.perform(); if (real_jid != null) { @@ -161,6 +164,8 @@ public class Message : Object { switch (sp.name) { case "stanza-id": update_builder.set(db.message.stanza_id, stanza_id); break; + case "server-id": + update_builder.set(db.message.server_id, server_id); break; case "counterpart": update_builder.set(db.message.counterpart_id, db.get_jid_id(counterpart)); update_builder.set(db.message.counterpart_resource, counterpart.resourcepart); break; diff --git a/libdino/src/service/database.vala b/libdino/src/service/database.vala index 60f1b52c..54854fd1 100644 --- a/libdino/src/service/database.vala +++ b/libdino/src/service/database.vala @@ -7,7 +7,7 @@ using Dino.Entities; namespace Dino { public class Database : Qlite.Database { - private const int VERSION = 9; + private const int VERSION = 10; public class AccountTable : Table { public Column<int> id = new Column.Integer("id") { primary_key = true, auto_increment = true }; @@ -55,6 +55,7 @@ public class Database : Qlite.Database { public class MessageTable : Table { public Column<int> id = new Column.Integer("id") { primary_key = true, auto_increment = true }; public Column<string> stanza_id = new Column.Text("stanza_id"); + public Column<string> server_id = new Column.Text("server_id") { min_version=10 }; public Column<int> account_id = new Column.Integer("account_id") { not_null = true }; public Column<int> counterpart_id = new Column.Integer("counterpart_id") { not_null = true }; public Column<string> counterpart_resource = new Column.Text("counterpart_resource"); @@ -69,7 +70,7 @@ public class Database : Qlite.Database { internal MessageTable(Database db) { base(db, "message"); - init({id, stanza_id, account_id, counterpart_id, our_resource, counterpart_resource, direction, + init({id, stanza_id, server_id, account_id, counterpart_id, our_resource, counterpart_resource, direction, type_, time, local_time, body, encryption, marked}); // get latest messages @@ -405,6 +406,14 @@ public class Database : Qlite.Database { return builder.count() > 0; } + public bool contains_message_by_server_id(Account account, Jid counterpart, string server_id) { + QueryBuilder builder = message.select() + .with(message.server_id, "=", server_id) + .with(message.counterpart_id, "=", get_jid_id(counterpart)) + .with(message.account_id, "=", account.id); + return builder.count() > 0; + } + public Message? get_message_by_id(int id) { Row? row = message.row_with(message.id, id).inner; if (row != null) { diff --git a/libdino/src/service/message_processor.vala b/libdino/src/service/message_processor.vala index 13278ee5..604578b5 100644 --- a/libdino/src/service/message_processor.vala +++ b/libdino/src/service/message_processor.vala @@ -91,7 +91,7 @@ public class MessageProcessor : StreamInteractionModule, Object { public async Entities.Message parse_message_stanza(Account account, Xmpp.MessageStanza message) { Entities.Message new_message = new Entities.Message(message.body); new_message.account = account; - new_message.stanza_id = message.id; + new_message.stanza_id = Xep.UniqueStableStanzaIDs.get_origin_id(message) ?? message.id; Jid? counterpart_override = null; if (message.from.equals(stream_interactor.get_module(MucManager.IDENTITY).get_own_jid(message.from.bare_jid, account))) { @@ -105,7 +105,17 @@ public class MessageProcessor : StreamInteractionModule, Object { new_message.counterpart = counterpart_override ?? (new_message.direction == Entities.Message.DIRECTION_SENT ? message.to : message.from); new_message.ourpart = new_message.direction == Entities.Message.DIRECTION_SENT ? message.from : message.to; + XmppStream? stream = stream_interactor.get_stream(account); Xep.MessageArchiveManagement.MessageFlag? mam_message_flag = Xep.MessageArchiveManagement.MessageFlag.get_flag(message); + Xep.MessageArchiveManagement.Flag? mam_flag = stream != null ? stream.get_flag(Xep.MessageArchiveManagement.Flag.IDENTITY) : null; + if (mam_message_flag != null && mam_flag != null && mam_flag.ns_ver == Xep.MessageArchiveManagement.NS_URI && mam_message_flag.mam_id != null) { + new_message.server_id = mam_message_flag.mam_id; + } else if (message.type_ == Xmpp.MessageStanza.TYPE_GROUPCHAT) { + new_message.server_id = Xep.UniqueStableStanzaIDs.get_stanza_id(message, new_message.counterpart.bare_jid); + } else if (message.type_ == Xmpp.MessageStanza.TYPE_CHAT) { + new_message.server_id = Xep.UniqueStableStanzaIDs.get_stanza_id(message, account.bare_jid); + } + if (mam_message_flag != null) new_message.local_time = mam_message_flag.server_time; if (new_message.local_time == null || new_message.local_time.compare(new DateTime.now_utc()) > 0) new_message.local_time = new DateTime.now_utc(); @@ -167,11 +177,17 @@ public class MessageProcessor : StreamInteractionModule, Object { } public override async bool run(Entities.Message message, Xmpp.MessageStanza stanza, Conversation conversation) { - bool is_uuid = message.stanza_id != null && Regex.match_simple("""[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}""", message.stanza_id); - bool new_uuid_msg = is_uuid && !db.contains_message_by_stanza_id(message, conversation.account); - bool new_misc_msg = !is_uuid && !db.contains_message(message, conversation.account); - bool new_msg = new_uuid_msg || new_misc_msg; - return !new_msg; + if (message.server_id != null) { + return db.contains_message_by_server_id(conversation.account, message.counterpart, message.server_id); + } else if (message.stanza_id != null) { + bool is_uuid = Regex.match_simple("""[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}""", message.stanza_id); + if (is_uuid) { + return db.contains_message_by_stanza_id(message, conversation.account); + } else { + return db.contains_message(message, conversation.account); + } + } + return false; } } @@ -268,8 +284,17 @@ public class MessageProcessor : StreamInteractionModule, Object { if (delayed) { Xmpp.Xep.DelayedDelivery.Module.set_message_delay(new_message, message.time); } + + // Set an origin ID if a MUC doen't guarantee to keep IDs + if (conversation.type_ == Conversation.Type.GROUPCHAT) { + Xep.Muc.Flag? flag = stream.get_flag(Xep.Muc.Flag.IDENTITY); + if (flag == null) return; + if(!flag.has_room_feature(conversation.counterpart, Xep.Muc.Feature.STABLE_ID)) { + Xep.UniqueStableStanzaIDs.set_origin_id(new_message, message.stanza_id); + } + } + stream.get_module(Xmpp.MessageModule.IDENTITY).send_message(stream, new_message); - message.stanza_id = new_message.id; } else { message.marked = Entities.Message.Marked.UNSENT; } diff --git a/xmpp-vala/CMakeLists.txt b/xmpp-vala/CMakeLists.txt index e4f94dff..e059b068 100644 --- a/xmpp-vala/CMakeLists.txt +++ b/xmpp-vala/CMakeLists.txt @@ -86,6 +86,7 @@ SOURCES "src/module/xep/0313_message_archive_management.vala" "src/module/xep/0333_chat_markers.vala" "src/module/xep/0334_message_processing_hints.vala" + "src/module/xep/0359_unique_stable_stanza_ids.vala" "src/module/xep/0363_http_file_upload.vala" "src/module/xep/0368_srv_records_tls.vala" "src/module/xep/0380_explicit_encryption.vala" diff --git a/xmpp-vala/src/module/xep/0045_muc/module.vala b/xmpp-vala/src/module/xep/0045_muc/module.vala index 3ff28b2b..b30145ff 100644 --- a/xmpp-vala/src/module/xep/0045_muc/module.vala +++ b/xmpp-vala/src/module/xep/0045_muc/module.vala @@ -48,6 +48,7 @@ public enum Feature { PUBLIC, ROOMS, SEMI_ANONYMOUS, + STABLE_ID, TEMPORARY, UNMODERATED, UNSECURED @@ -376,6 +377,7 @@ public class Module : XmppStreamModule { case "http://jabber.org/protocol/muc#register": parsed = Feature.REGISTER; break; case "http://jabber.org/protocol/muc#roomconfig": parsed = Feature.ROOMCONFIG; break; case "http://jabber.org/protocol/muc#roominfo": parsed = Feature.ROOMINFO; break; + case "http://jabber.org/protocol/muc#stable_id": parsed = Feature.STABLE_ID; break; case "muc_hidden": parsed = Feature.HIDDEN; break; case "muc_membersonly": parsed = Feature.MEMBERS_ONLY; break; case "muc_moderated": parsed = Feature.MODERATED; break; diff --git a/xmpp-vala/src/module/xep/0313_message_archive_management.vala b/xmpp-vala/src/module/xep/0313_message_archive_management.vala index 69302be3..4f8cadec 100644 --- a/xmpp-vala/src/module/xep/0313_message_archive_management.vala +++ b/xmpp-vala/src/module/xep/0313_message_archive_management.vala @@ -106,7 +106,8 @@ public class ReceivedPipelineListener : StanzaListener<MessageStanza> { StanzaNode? forward_node = message.stanza.get_deep_subnode(NS_VER(stream) + ":result", "urn:xmpp:forward:0:forwarded", DelayedDelivery.NS_URI + ":delay"); DateTime? datetime = DelayedDelivery.Module.get_time_for_node(forward_node); - message.add_flag(new MessageFlag(datetime)); + string? mam_id = message.stanza.get_deep_attribute(NS_VER(stream) + ":result", NS_VER(stream) + ":id"); + message.add_flag(new MessageFlag(datetime, mam_id)); message.stanza = message_node; message.rerun_parsing = true; @@ -132,9 +133,11 @@ public class MessageFlag : Xmpp.MessageFlag { public const string ID = "message_archive_management"; public DateTime? server_time { get; private set; } + public string? mam_id { get; private set; } - public MessageFlag(DateTime? server_time) { + public MessageFlag(DateTime? server_time, string? mam_id) { this.server_time = server_time; + this.mam_id = mam_id; } public static MessageFlag? get_flag(MessageStanza message) { return (MessageFlag) message.get_flag(NS_URI, ID); } diff --git a/xmpp-vala/src/module/xep/0359_unique_stable_stanza_ids.vala b/xmpp-vala/src/module/xep/0359_unique_stable_stanza_ids.vala new file mode 100644 index 00000000..b6defc6d --- /dev/null +++ b/xmpp-vala/src/module/xep/0359_unique_stable_stanza_ids.vala @@ -0,0 +1,46 @@ +namespace Xmpp.Xep.UniqueStableStanzaIDs { + +private const string NS_URI = "urn:xmpp:sid:0"; + +private const string HINT_NO_PERMANENT_STORE = "no-permanent-store"; +private const string HINT_NO_STORE = "no-store"; +private const string HINT_NO_COPY = "no-copy"; +private const string HINT_STORE = "store"; + +public class Module : XmppStreamModule { + public static ModuleIdentity<Module> IDENTITY = new ModuleIdentity<Module>(NS_URI, "0359_unique_and_stable_stanza_ids"); + + public override void attach(XmppStream stream) { + stream.get_module(ServiceDiscovery.Module.IDENTITY).add_feature(stream, NS_URI); + } + + public override void detach(XmppStream stream) {} + + public override string get_ns() { return NS_URI; } + + public override string get_id() { return IDENTITY.id; } +} + +public static void set_origin_id(MessageStanza message, string origin_id) { + StanzaNode hint_node = (new StanzaNode.build("origin-id", NS_URI)).add_self_xmlns().put_attribute("id", origin_id); + message.stanza.put_node(hint_node); +} + +public static string? get_origin_id(MessageStanza message) { + StanzaNode? node = message.stanza.get_subnode("origin-id", NS_URI); + if (node == null) return null; + + return node.get_attribute("id"); +} + +public static string? get_stanza_id(MessageStanza message, Jid by) { + string by_str = by.to_string(); + foreach (StanzaNode node in message.stanza.get_subnodes("stanza-id", NS_URI)) { + if (node.get_attribute("by") == by_str) { + return node.get_attribute("id"); + } + } + return null; +} + +} |