From b1fc1db8ca47250ad4cc0cb11c185a070c5e1451 Mon Sep 17 00:00:00 2001 From: Linwenxuan04 Date: Tue, 22 Aug 2023 11:30:13 +0800 Subject: [PATCH] Implemented Url for FileEntity --- .../Logic/Implementation/MessagingLogic.cs | 1 + .../Core/Service/Message/FileDownloadService.cs | 16 +++++++++++++--- .../Core/Service/Message/PushMessageService.cs | 3 ++- Lagrange.Core/Message/Entity/FileEntity.cs | 4 +++- Lagrange.Core/Message/MessagePacker.cs | 2 +- 5 files changed, 20 insertions(+), 6 deletions(-) diff --git a/Lagrange.Core/Core/Context/Logic/Implementation/MessagingLogic.cs b/Lagrange.Core/Core/Context/Logic/Implementation/MessagingLogic.cs index e63ec9b2a..db7f06396 100644 --- a/Lagrange.Core/Core/Context/Logic/Implementation/MessagingLogic.cs +++ b/Lagrange.Core/Core/Context/Logic/Implementation/MessagingLogic.cs @@ -99,6 +99,7 @@ private async Task ResolveAdditionalPackets(MessageChain chain) if (results.Count != 0) { var result = (FileDownloadEvent)results[0]; + file.FileUrl = result.FileUrl; } } if (chain.HasTypeOf()) diff --git a/Lagrange.Core/Core/Service/Message/FileDownloadService.cs b/Lagrange.Core/Core/Service/Message/FileDownloadService.cs index f6bd4997f..05c0289b4 100644 --- a/Lagrange.Core/Core/Service/Message/FileDownloadService.cs +++ b/Lagrange.Core/Core/Service/Message/FileDownloadService.cs @@ -1,12 +1,13 @@ +using System.Text; using Lagrange.Core.Common; using Lagrange.Core.Core.Event.Protocol; using Lagrange.Core.Core.Event.Protocol.Message; using Lagrange.Core.Core.Packets; using Lagrange.Core.Core.Packets.Service.Oidb; using Lagrange.Core.Core.Packets.Service.Oidb.Request; +using Lagrange.Core.Core.Packets.Service.Oidb.Response; using Lagrange.Core.Core.Service.Abstraction; using Lagrange.Core.Utility.Binary; -using Lagrange.Core.Utility.Extension; using ProtoBuf; namespace Lagrange.Core.Core.Service.Message; @@ -43,7 +44,16 @@ protected override bool Build(FileDownloadEvent input, BotKeystore keystore, Bot protected override bool Parse(SsoPacket input, BotKeystore keystore, BotAppInfo appInfo, BotDeviceInfo device, out FileDownloadEvent output, out List? extraEvents) { - Console.WriteLine(input.Payload.ReadBytes(BinaryPacket.Prefix.Uint32 | BinaryPacket.Prefix.WithPrefix).Hex()); - return base.Parse(input, keystore, appInfo, device, out output, out extraEvents); + var payload = input.Payload.ReadBytes(BinaryPacket.Prefix.Uint32 | BinaryPacket.Prefix.WithPrefix); + var packet = Serializer.Deserialize>(payload.AsSpan()); + + var urlBuilder = new StringBuilder() + .Append("http://") + .Append(packet.Body.Body.Result.Server).Append(':').Append(packet.Body.Body.Result.Port) + .Append(packet.Body.Body.Result.Url).Append("&isthumb=0"); + + output = FileDownloadEvent.Result((int)packet.ErrorCode, urlBuilder.ToString()); + extraEvents = null; + return true; } } \ No newline at end of file diff --git a/Lagrange.Core/Core/Service/Message/PushMessageService.cs b/Lagrange.Core/Core/Service/Message/PushMessageService.cs index c73fae7e8..49db4bab6 100644 --- a/Lagrange.Core/Core/Service/Message/PushMessageService.cs +++ b/Lagrange.Core/Core/Service/Message/PushMessageService.cs @@ -30,7 +30,7 @@ protected override bool Parse(SsoPacket input, BotKeystore keystore, BotAppInfo extraEvents = new List(); switch (packetType) { - case PkgType.PrivateMessage or PkgType.GroupMessage: + case PkgType.PrivateMessage or PkgType.GroupMessage or PkgType.PrivateFileMessage: { var chain = MessagePacker.Parse(message.Message); output = PushMessageEvent.Create(chain); @@ -97,6 +97,7 @@ protected override bool Parse(SsoPacket input, BotKeystore keystore, BotAppInfo private enum PkgType { PrivateMessage = 166, + PrivateFileMessage = 529, GroupMessage = 82, GroupInviteNotice = 87, Event0x210 = 528, diff --git a/Lagrange.Core/Message/Entity/FileEntity.cs b/Lagrange.Core/Message/Entity/FileEntity.cs index 1be9ebe4a..0c3445f20 100644 --- a/Lagrange.Core/Message/Entity/FileEntity.cs +++ b/Lagrange.Core/Message/Entity/FileEntity.cs @@ -20,6 +20,8 @@ public class FileEntity : IMessageEntity public byte[] FileMd5 { get; internal set; } + public string? FileUrl { get; internal set; } + internal string? FileUuid { get; set; } internal string? FileHash { get; set; } @@ -90,5 +92,5 @@ private FileEntity(long fileSize, string fileName, byte[] fileMd5, string fileUu : null; } - public string ToPreviewString() => $"[File] {FileName} ({FileSize})"; + public string ToPreviewString() => $"[File] {FileName} ({FileSize}): {FileUrl ?? "failed to receive file url"}"; } \ No newline at end of file diff --git a/Lagrange.Core/Message/MessagePacker.cs b/Lagrange.Core/Message/MessagePacker.cs index fc1cab5c5..084941f64 100644 --- a/Lagrange.Core/Message/MessagePacker.cs +++ b/Lagrange.Core/Message/MessagePacker.cs @@ -133,7 +133,7 @@ public static MessageChain Parse(PushMsgBody message) } } - if (message.Body is { MsgContent: not null, RichText: null }) // if RichText is not null, it means that the message is from Tencent's SSO server + if (message.Body is { MsgContent: not null }) { foreach (var factory in MsgFactory) {