Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions Lagrange.Core/Events/EventArgs/BotGroupInviteSelfEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace Lagrange.Core.Events.EventArgs;

public class BotGroupInviteSelfEvent(long invitationSeq, long initiatorUin, long groupUin) : EventBase
{
public long InvitationSeq { get; } = invitationSeq;

public long InitiatorUin { get; } = initiatorUin;

public long GroupUin { get; } = groupUin;

public override string ToEventMessage()
{
return $"{nameof(BotGroupInviteSelfEvent)}: InvitationSeq: {InvitationSeq}, InitiatorUin: {InitiatorUin}, GroupUin: {GroupUin}";
}
}
20 changes: 20 additions & 0 deletions Lagrange.Core/Internal/Logic/PushLogic.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
using System.Text.Json;
using System.Web;
using Lagrange.Core.Common;
using Lagrange.Core.Common.Entity;
using Lagrange.Core.Events.EventArgs;
using Lagrange.Core.Internal.Events;
using Lagrange.Core.Internal.Events.Message;
using Lagrange.Core.Internal.Events.System;
using Lagrange.Core.Internal.Packets.Notify;
using Lagrange.Core.Message.Entities;
using Lagrange.Core.Utility.Binary;
using ProtoHelper = Lagrange.Core.Utility.ProtoHelper;

Expand All @@ -24,6 +27,23 @@ public async ValueTask Incoming(ProtocolEvent e)
case Type.TempMessage:
{
var message = await context.EventContext.GetLogic<MessagingLogic>().Parse(messageEvent.MsgPush.CommonMessage);
if (message.Entities[0] is LightAppEntity {AppName: "com.tencent.qun.invite"} || message.Entities[0] is LightAppEntity {AppName: "com.tencent.tuwen.lua"})
{
var app = (LightAppEntity)message.Entities[0];
using var document = JsonDocument.Parse(app.Payload);
var root = document.RootElement;

string url = root.GetProperty("meta").GetProperty("news").GetProperty("jumpUrl").GetString() ?? throw new Exception("sb tx! Is this 'com.tencent.qun.invite' or 'com.tencent.tuwen.lua'?");
var query = HttpUtility.ParseQueryString(new Uri(url).Query);
long groupUin = uint.Parse(query["groupcode"] ?? throw new Exception("sb tx! Is this '/group/invite_join'?"));
long sequence = long.Parse(query["msgseq"] ?? throw new Exception("sb tx! Is this '/group/invite_join'?"));
Comment thread
Szzrain marked this conversation as resolved.
Comment thread
Szzrain marked this conversation as resolved.
Comment thread
Szzrain marked this conversation as resolved.
context.EventInvoker.PostEvent(new BotGroupInviteSelfEvent(
sequence,
message.Contact.Uin,
groupUin
));
break;
}
context.EventInvoker.PostEvent(new BotMessageEvent(message, messageEvent.Raw));
break;
}
Expand Down
67 changes: 67 additions & 0 deletions Lagrange.Core/Message/Entities/LightAppEntity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using System.Text;
using System.Text.Json.Nodes;
using Lagrange.Core.Internal.Packets.Message;
using Lagrange.Core.Utility.Binary;
using Lagrange.Core.Utility.Compression;

namespace Lagrange.Core.Message.Entities;

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;
}

Elem[] IMessageEntity.Build()
{
using var payload = new BinaryPacket();
payload.Write<byte>(0x01);
payload.Write(ZCompression.ZCompress(Encoding.UTF8.GetBytes(Payload)));

return new Elem[]
{
new()
{
LightAppElem = new LightAppElem
{
BytesData = payload.ToArray()
}
}
};
}

IMessageEntity? IMessageEntity.Parse(List<Elem> elems, Elem target)
{
if (target.LightAppElem is { } lightApp)
{
var payload = ZCompression.ZDecompress(lightApp.BytesData.Span.Slice(1), false);
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}]";
}
}
18 changes: 18 additions & 0 deletions Lagrange.Milky/Entity/Event/GroupInvitationEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.Text.Json.Serialization;

namespace Lagrange.Milky.Entity.Event;


public class GroupInvitationEvent(long time, long selfId, GroupInvitationEventData data) : EventBase<GroupInvitationEventData>(time, selfId, "group_invitation", data) { }

public class GroupInvitationEventData(long invitationSeq, long initiatorId, long groupId)
{
[JsonPropertyName("invitation_seq")]
public long InvitationSeq { get; } = invitationSeq;

[JsonPropertyName("initiator_id")]
public long InitiatorID { get; } = initiatorId;

[JsonPropertyName("group_id")]
public long GroupID { get; } = groupId;
}
29 changes: 29 additions & 0 deletions Lagrange.Milky/Event/EventService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public Task StartAsync(CancellationToken token)
_bot.EventInvoker.RegisterEvent<LgrEvents.BotGroupNudgeEvent>(HandleGroupNudgeEvent);
_bot.EventInvoker.RegisterEvent<LgrEvents.BotGroupMemberDecreaseEvent>(HandleGroupMemberDecreaseEvent);
_bot.EventInvoker.RegisterEvent<LgrEvents.BotFriendRequestEvent>(HandleFriendRequestEvent);
_bot.EventInvoker.RegisterEvent<LgrEvents.BotGroupInviteSelfEvent>(HandleGroupInvitationEvent);

return Task.CompletedTask;
}
Expand Down Expand Up @@ -175,6 +176,31 @@ private void HandleFriendRequestEvent(BotContext bot, LgrEvents.BotFriendRequest
_logger.LogHandleEventException(nameof(LgrEvents.BotFriendRequestEvent), e);
}
}

private void HandleGroupInvitationEvent(BotContext bot, LgrEvents.BotGroupInviteSelfEvent @event)
{
try
{
_logger.LogGroupInvitationEvent(
@event.InvitationSeq,
@event.InitiatorUin,
@event.GroupUin
);
var result = _convert.GroupInvitationEvent(@event);
byte[] bytes = JsonUtility.SerializeToUtf8Bytes(result.GetType(), result);
using (_lock.UsingReadLock())
{
foreach (var handler in _handlers)
{
handler(bytes);
}
}
}
catch (Exception e)
{
_logger.LogHandleEventException(nameof(LgrEvents.BotGroupInviteSelfEvent), e);
}
}

public Task StopAsync(CancellationToken token)
{
Expand Down Expand Up @@ -220,6 +246,9 @@ public static partial class EventServiceLoggerExtension

[LoggerMessage(EventId = 5, Level = LogLevel.Debug, Message = "BotFriendRequestEvent {{ request: {request}, user: {user}, message: {message}, source: {source} }}")]
public static partial void LogBotFriendRequestEvent(this ILogger<EventService> logger, string request, long user, string? message, string? source);

[LoggerMessage(EventId = 6, Level = LogLevel.Debug, Message = "BotGroupInviteEvent {{ request: {request}, user: {user}, group: {group} }}")]
public static partial void LogGroupInvitationEvent(this ILogger<EventService> logger, long request, long user, long group);

[LoggerMessage(EventId = 999, Level = LogLevel.Error, Message = "Handle {event} exception")]
public static partial void LogHandleEventException(this ILogger<EventService> logger, string @event, Exception e);
Expand Down
7 changes: 7 additions & 0 deletions Lagrange.Milky/Utility/EntityConvert.Event.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,11 @@ public partial class EntityConvert
new FriendRequestEventData(@event.InitiatorUid, @event.InitiatorUin,
@event.Message, @event.Source)
);

public GroupInvitationEvent GroupInvitationEvent(LgrEventArgs.BotGroupInviteSelfEvent @event) => new(
@event.EventTime.ToUnixTimeSeconds(),
_bot.BotUin,
new GroupInvitationEventData(@event.InvitationSeq,
@event.InitiatorUin, @event.GroupUin)
);
}
1 change: 1 addition & 0 deletions Lagrange.Milky/Utility/EntityConvert.Segment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public async Task<MessageChain> FakeSegmentsAsync(IReadOnlyList<IOutgoingSegment
(int)video.VideoLength
),
MultiMsgEntity multiMsg => new ForwardIncomingSegment(multiMsg.ResId!),
LightAppEntity lightApp => new LightAppIncomingSegment(lightApp.AppName, lightApp.Payload),
// ? => new MarketFaceSegment(...),
// ? => new LightAppSegment(...),
// ? => new XmlSegment(...),
Expand Down
2 changes: 2 additions & 0 deletions Lagrange.Milky/Utility/JsonUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ public static partial class JsonUtility
[JsonSerializable(typeof(GroupMemberDecreaseEvent))]
// friend_request
[JsonSerializable(typeof(FriendRequestEvent))]
// group_invitation
[JsonSerializable(typeof(GroupInvitationEvent))]
private partial class JsonContext : JsonSerializerContext;

public static string Serialize<T>(T value) where T : class
Expand Down
Loading