From 578c82a932b42f556534cee4975e0bf4cda49d5b Mon Sep 17 00:00:00 2001 From: Linwenxuan <116782992+Linwenxuan05@users.noreply.github.com> Date: Fri, 9 Feb 2024 15:52:40 +0800 Subject: [PATCH] [Core] Implemented LightAppEntity.cs --- .../Internal/Packets/Message/Element/Elem.cs | 2 +- .../Element/Implementation/LightAppElem.cs | 2 +- .../Message/Entity/LightAppEntity.cs | 70 +++++++++++++++++++ README.md | 2 +- 4 files changed, 73 insertions(+), 3 deletions(-) create mode 100644 Lagrange.Core/Message/Entity/LightAppEntity.cs diff --git a/Lagrange.Core/Internal/Packets/Message/Element/Elem.cs b/Lagrange.Core/Internal/Packets/Message/Element/Elem.cs index b53fbafea..0308f3b8d 100644 --- a/Lagrange.Core/Internal/Packets/Message/Element/Elem.cs +++ b/Lagrange.Core/Internal/Packets/Message/Element/Elem.cs @@ -42,7 +42,7 @@ internal class Elem [ProtoMember(45)] public SrcMsg? SrcMsg { get; set; } // Forward/ReplyEntity - [ProtoMember(51)] public LightAppElem? LightApp { get; set; } + [ProtoMember(51)] public LightAppElem? LightAppElem { get; set; } [ProtoMember(53)] public CommonElem? CommonElem { get; set; } } \ No newline at end of file diff --git a/Lagrange.Core/Internal/Packets/Message/Element/Implementation/LightAppElem.cs b/Lagrange.Core/Internal/Packets/Message/Element/Implementation/LightAppElem.cs index c36372693..fce6add91 100644 --- a/Lagrange.Core/Internal/Packets/Message/Element/Implementation/LightAppElem.cs +++ b/Lagrange.Core/Internal/Packets/Message/Element/Implementation/LightAppElem.cs @@ -8,5 +8,5 @@ internal class LightAppElem { [ProtoMember(1)] public byte[] Data { get; set; } - [ProtoMember(2)] public byte[] MsgResid { get; set; } + [ProtoMember(2)] public byte[]? MsgResid { get; set; } } \ No newline at end of file diff --git a/Lagrange.Core/Message/Entity/LightAppEntity.cs b/Lagrange.Core/Message/Entity/LightAppEntity.cs new file mode 100644 index 000000000..501338c0e --- /dev/null +++ b/Lagrange.Core/Message/Entity/LightAppEntity.cs @@ -0,0 +1,70 @@ +using System.Text; +using System.Text.Json.Nodes; +using Lagrange.Core.Internal.Packets.Message.Element; +using Lagrange.Core.Internal.Packets.Message.Element.Implementation; +using Lagrange.Core.Utility.Binary; +using Lagrange.Core.Utility.Binary.Compression; + +namespace Lagrange.Core.Message.Entity; + +[MessageElement(typeof(LightAppElem))] +public class LightAppEntity : IMessageEntity +{ + public string AppName { get; set; } = string.Empty; + + public string Payload { get; set; } = string.Empty; + + public LightAppEntity() { } + + public LightAppEntity(string payload) + { + Payload = payload; + string? app = JsonNode.Parse(payload)?["app"]?.ToString(); + if (app != null) AppName = app; + } + + IEnumerable IMessageEntity.PackElement() + { + using var payload = new BinaryPacket() + .WriteByte(0x01) + .WriteBytes(ZCompression.ZDecompress(Encoding.UTF8.GetBytes(Payload))); + + return new Elem[] + { + new() + { + LightAppElem = new LightAppElem + { + Data = payload.ToArray(), + MsgResid = null + } + } + }; + } + + IMessageEntity? IMessageEntity.UnpackElement(Elem elems) + { + if (elems.LightAppElem is { } lightApp) + { + var payload = ZCompression.ZDecompress(lightApp.Data[1..]); + string json = Encoding.UTF8.GetString(payload); + string? app = JsonNode.Parse(json)?["app"]?.ToString(); + + if (app != null) + { + return new LightAppEntity + { + AppName = app, + Payload = json + }; + } + } + + return null; + } + + public string ToPreviewString() + { + return $"[{nameof(LightAppEntity)}: {AppName}]"; + } +} \ No newline at end of file diff --git a/README.md b/README.md index 89a7ce35e..d65af74ff 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,7 @@ Please use Lagrange.Core responsibly and in accordance with the law. | | | | | Reply. | 🟢 | Group Request | 🟢 | GroupPromoteAdmin | 🟢 | | | | | | File | 🟡 | ~~Voice Call~~ | 🔴 | GroupInvite | 🟢 | | | | | | Poke | 🟢 | Client Key | 🟢 | GroupRequestJoin | 🔴 | -| | | | | | | Cookies | 🟢 | FriendRequest | 🟢 | +| | | | | LightApp | 🟢 | Cookies | 🟢 | FriendRequest | 🟢 | | | | | | | | Send Message | 🟢 | ~~FriendTyping~~ | 🔴 | | | | | | | | | | ~~FriendVoiceCall~~ | 🔴 |