From 47d61623b4bbc43df58b5aa1835de0f4c48c2ed2 Mon Sep 17 00:00:00 2001 From: Fabian Sauter Date: Sun, 1 Mar 2020 18:26:34 +0100 Subject: [PATCH] Fixed a cause for duplicate MUC messages #46 When messages do not have an id attribute, but instead only an MAM archive node with an id attribute UWPX would handle them as new unique messages. --- XMPP_API/Classes/Consts.cs | 2 ++ .../Network/XML/Messages/MessageMessage.cs | 26 ++++++++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/XMPP_API/Classes/Consts.cs b/XMPP_API/Classes/Consts.cs index ac67fa5b3..7167e3f4d 100644 --- a/XMPP_API/Classes/Consts.cs +++ b/XMPP_API/Classes/Consts.cs @@ -85,5 +85,7 @@ public static class Consts public const string XML_XEP_0336_NAMESPACE = "urn:xmpp:xdata:dynamic"; // XEP-IoT: public const string XML_XEP_IOT_NAMESPACE = "urn:xmpp:uwpx:iot"; + // XEP-0313 (Message Archive Management) + public const string XML_XEP_0313_NAMESPACE = "urn:xmpp:mam:tmp"; } } diff --git a/XMPP_API/Classes/Network/XML/Messages/MessageMessage.cs b/XMPP_API/Classes/Network/XML/Messages/MessageMessage.cs index 928f4dce3..83a56185d 100644 --- a/XMPP_API/Classes/Network/XML/Messages/MessageMessage.cs +++ b/XMPP_API/Classes/Network/XML/Messages/MessageMessage.cs @@ -53,7 +53,7 @@ public MessageMessage(XmlNode node, string type) : this(node, CarbonCopyType.NON chatMessageId = null; } - public MessageMessage(XmlNode node, CarbonCopyType ccType) : base(node.Attributes["from"]?.Value, node.Attributes["to"]?.Value, (node.Attributes["id"]?.Value) ?? getRandomId()) + public MessageMessage(XmlNode node, CarbonCopyType ccType) : base(node.Attributes["from"]?.Value, node.Attributes["to"]?.Value, loadMessageId(node)) { CC_TYPE = ccType; if (!node.HasChildNodes) @@ -130,6 +130,30 @@ public DateTime getDelay() return delay; } + protected static string loadMessageId(XmlNode node) + { + // Check for the default message ID attribute: + string id = node.Attributes["id"]?.Value; + if (!(id is null)) + { + return id; + } + + // Check for a MAM-ID in the archived node: + XmlNode archivedNode = XMLUtils.getChildNode(node, "archived", Consts.XML_XMLNS, Consts.XML_XEP_0313_NAMESPACE); + if (!(archivedNode is null)) + { + id = archivedNode.Attributes["id"]?.Value; + if (!(id is null)) + { + return id; + } + } + + // Fall back to a new message ID: + return getRandomId(); + } + #endregion //--------------------------------------------------------Misc Methods:---------------------------------------------------------------\\ #region --Misc Methods (Public)--