-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathMultiMsgEntity.cs
More file actions
152 lines (129 loc) · 5.26 KB
/
MultiMsgEntity.cs
File metadata and controls
152 lines (129 loc) · 5.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
using System.IO.Compression;
using System.Text.Json.Nodes;
using System.Xml;
using Lagrange.Core.Common.Entity;
using Lagrange.Core.Internal.Events.Message;
using Lagrange.Core.Internal.Packets.Message;
using Lagrange.Core.Internal.Packets.Service;
using Lagrange.Core.Utility;
namespace Lagrange.Core.Message.Entities;
public class MultiMsgEntity(string? resId) : IMessageEntity
{
public List<BotMessage> Messages { get; } = [];
public string? ResId { get; private set; } = resId;
public MultiMsgEntity(List<BotMessage> messages) : this(default(string))
{
Messages = messages;
}
public MultiMsgEntity() : this(default(string)) { }
public async Task Preprocess(BotContext context, BotMessage message)
{
if (string.IsNullOrEmpty(ResId))
{
// Recursively preprocess internal messages
Console.WriteLine($"[MultiMsgEntity] Preprocessing {Messages.Count} messages for forward chain...");
foreach (var innerMsg in Messages)
{
foreach (var entity in innerMsg.Entities)
{
try
{
await entity.Preprocess(context, message);
if (entity is ImageEntity img)
{
if (img.MsgInfo != null)
Console.WriteLine($"[MultiMsgEntity] Image uploaded successfully. Size: {img.ImageSize}");
else
Console.WriteLine("[MultiMsgEntity] WARNING: Image MsgInfo is NULL after preprocess!");
}
}
catch (Exception ex)
{
Console.WriteLine($"[MultiMsgEntity] Error preprocessing entity: {ex.Message}");
}
}
}
var result = await context.EventContext.SendEvent<LongMsgSendEventResp>(new LongMsgSendEventReq(message.Receiver, Messages));
ResId = result.ResId;
}
}
public async Task Postprocess(BotContext context, BotMessage message)
{
if (string.IsNullOrEmpty(ResId)) return;
bool isGroup = message.Contact is BotGroupMember;
var result = await context.EventContext.SendEvent<LongMsgRecvEventResp>(new LongMsgRecvEventReq(isGroup, ResId));
Messages.Clear();
Messages.AddRange(result.Messages);
}
string IMessageEntity.ToPreviewString() => "[聊天记录]";
Elem[] IMessageEntity.Build()
{
if (string.IsNullOrEmpty(ResId)) return [];
int count = Math.Clamp(Messages.Count, 0, 4);
string guid = Guid.NewGuid().ToString();
var extra = new JsonObject { { "filename", guid }, { "tsum", count } };
var news = new JsonArray(Messages[..count].Select(x => new JsonObject { { "text", $"{x.Contact.Nickname}: {string.Join(' ', x.Entities.Select(e => e.ToPreviewString()))}" } }).Cast<JsonNode>().ToArray());
var detail = new JsonObject
{
{ "news", news },
{ "resid", ResId },
{ "source", "聊天记录" },
{ "summary", $"查看{count}条转发消息" },
{ "uniseq", guid }
};
var lightApp = new LightApp
{
App = "com.tencent.multimsg",
Config = new Config
{
Autosize = 1,
Forward = 1,
Round = 1,
Type = "normal",
Width = 300
},
Meta = new JsonObject { { "detail", detail } },
Desc = "[聊天记录]",
Extra = JsonHelper.Serialize(extra),
Prompt = "[聊天记录]",
Ver = "0.0.0.5",
View = "contact"
};
var data = JsonHelper.SerializeToUtf8Bytes(lightApp).Span;
using var dest = new MemoryStream();
dest.WriteByte(0x01);
using var zlib = new ZLibStream(dest, CompressionLevel.Optimal, true);
zlib.Write(data);
zlib.Close();
return [new Elem { LightAppElem = new LightAppElem { BytesData = dest.ToArray() } }];
}
IMessageEntity? IMessageEntity.Parse(List<Elem> elements, Elem target)
{
if (target.RichMsg is { ServiceId: 35 } richMsg)
{
using var source = new MemoryStream();
using var dest = new MemoryStream();
using var inflate = new DeflateStream(source, CompressionMode.Decompress);
source.Write(richMsg.BytesTemplate1.Span[3..^4]);
source.Seek(0, SeekOrigin.Begin);
inflate.CopyTo(dest);
dest.Seek(0, SeekOrigin.Begin);
using var xmlReader = XmlReader.Create(dest);
xmlReader.Read();
var doc = new XmlDocument();
doc.Load(xmlReader);
return new MultiMsgEntity(doc["msg"]?.Attributes["m_resid"]?.Value ?? string.Empty);
}
return null;
}
private static byte[] Adler32(ReadOnlySpan<byte> data)
{
uint a = 1, b = 0;
foreach (byte t in data)
{
a = (a + t) % 65521;
b = (b + a) % 65521;
}
return BitConverter.GetBytes((a << 16) | b);
}
}