Skip to content

Commit

Permalink
[Core] Implemented FriendRequest Event
Browse files Browse the repository at this point in the history
  • Loading branch information
Linwenxuan authored and Linwenxuan committed Oct 30, 2023
1 parent 9d9e541 commit e4e796b
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ namespace Lagrange.Core.Internal.Context.Logic.Implementation;
[EventSubscribe(typeof(GroupSysAdminEvent))]
[EventSubscribe(typeof(GroupSysIncreaseEvent))]
[EventSubscribe(typeof(GroupSysDecreaseEvent))]
[EventSubscribe(typeof(FriendSysRequestEvent))]
[BusinessLogic("MessagingLogic", "Manage the receiving and sending of messages and notifications")]
internal class MessagingLogic : LogicBase
{
Expand Down Expand Up @@ -75,6 +76,12 @@ public override async Task Incoming(ProtocolEvent e)
Collection.Invoker.PostEvent(decreaseArgs);
break;
}
case FriendSysRequestEvent info:
{
var requestArgs = new FriendRequestEvent(info.SourceUin, info.Name, info.Message);
Collection.Invoker.PostEvent(requestArgs);
break;
}
}
}

Expand Down
19 changes: 19 additions & 0 deletions Lagrange.Core/Internal/Event/EventArg/FriendRequestEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace Lagrange.Core.Internal.Event.EventArg;

public class FriendRequestEvent : EventBase
{
public uint SourceUin { get; }

public string Name { get; }

public string Message { get; }

internal FriendRequestEvent(uint sourceUin, string name, string message)
{
SourceUin = sourceUin;
Name = name;
Message = message;

EventMessage = $"[{nameof(FriendRequestEvent)}]: {SourceUin}:{Name} {Message}";
}
}
2 changes: 2 additions & 0 deletions Lagrange.Core/Internal/Event/EventInvoker.Events.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@ public partial class EventInvoker
public event LagrangeEvent<GroupMemberIncreaseEvent>? OnGroupMemberIncreaseEvent;

public event LagrangeEvent<GroupMemberDecreaseEvent>? OnGroupMemberDecreaseEvent;

public event LagrangeEvent<FriendRequestEvent>? OnFriendRequestEvent;
}
1 change: 1 addition & 0 deletions Lagrange.Core/Internal/Event/EventInvoker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ internal EventInvoker(BotContext context)
RegisterEvent((GroupAdminChangedEvent e) => OnGroupAdminChangedEvent?.Invoke(context, e));
RegisterEvent((GroupMemberIncreaseEvent e) => OnGroupMemberIncreaseEvent?.Invoke(context, e));
RegisterEvent((GroupMemberDecreaseEvent e) => OnGroupMemberDecreaseEvent?.Invoke(context, e));
RegisterEvent((FriendRequestEvent e) => OnFriendRequestEvent?.Invoke(context, e));
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace Lagrange.Core.Internal.Event.Protocol.Notify;

internal class FriendSysRequestEvent : ProtocolEvent
{
public uint SourceUin { get; }

public string SourceUid { get; }

public string Message { get; }

public string Name { get; }

private FriendSysRequestEvent(uint sourceUin, string sourceUid, string message, string name) : base(0)
{
SourceUin = sourceUin;
SourceUid = sourceUid;
Message = message;
Name = name;
}

public static FriendSysRequestEvent Result(uint sourceUin, string sourceUid, string message, string name) =>
new(sourceUin, sourceUid, message, name);
}
25 changes: 25 additions & 0 deletions Lagrange.Core/Internal/Packets/Message/Notify/FriendRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using ProtoBuf;

#pragma warning disable CS8618

namespace Lagrange.Core.Internal.Packets.Message.Notify;

[ProtoContract]
internal class FriendRequest
{
[ProtoMember(1)] public FriendRequestInfo Info { get; set; }
}

[ProtoContract]
internal class FriendRequestInfo
{
[ProtoMember(1)] public uint Field0 { get; set; }

[ProtoMember(2)] public string SourceUid { get; set; }

[ProtoMember(3)] public string Message { get; set; }

[ProtoMember(4)] public string Name { get; set; }

[ProtoMember(5)] public uint Field5 { get; set; }
}
11 changes: 6 additions & 5 deletions Lagrange.Core/Internal/Service/Message/PushMessageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ protected override bool Parse(byte[] input, BotKeystore keystore, BotAppInfo app
}
case PkgType.Event0x210:
{
ProcessEvent0x210(input, message);
ProcessEvent0x210(input, message, extraEvents);
break;
}
case PkgType.Event0x2DC:
Expand Down Expand Up @@ -127,20 +127,21 @@ private static void ProcessEvent0x2DC(byte[] payload, PushMsg msg)
}
}

private static void ProcessEvent0x210(byte[] payload, PushMsg msg)
private static void ProcessEvent0x210(byte[] payload, PushMsg msg, List<ProtocolEvent> extraEvents)
{
var pkgType = (Event0x210SubType)(msg.Message.ContentHead.SubType ?? 0);
switch (pkgType)
{
case Event0x210SubType.FriendRequestNotice when msg.Message.Body?.MsgContent is { } content:
{
var subInfo = content[7..];
Console.WriteLine(subInfo.Hex());
var friend = Serializer.Deserialize<FriendRequest>(content.AsSpan());
var friendEvent = FriendSysRequestEvent.Result(msg.Message.ResponseHead.FromUin, friend.Info.SourceUid, friend.Info.Message, friend.Info.Name);
extraEvents.Add(friendEvent);
break;
}
default:
{
Console.WriteLine($"Unknown Event0x2DC message type: {pkgType}: {payload.Hex()}");
Console.WriteLine($"Unknown Event0x210 message type: {pkgType}: {payload.Hex()}");
break;
}
}
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Please use Lagrange.Core responsibly and in accordance with the law.
| | | | | ~~Flash Image~~ | 🔴 | Group Request | 🔴 | GroupPromoteAdmin | 🟢 |
| | | | | Reply | 🟢 | ~~Voice Call~~ | 🔴 | GroupInvite | 🟢 |
| | | | | File | 🟡 | Client Key | 🟢 | GroupRequestJoin | 🔴 |
| | | | | | | Cookies | 🟢 | FriendRequest | 🔴 |
| | | | | | | Cookies | 🟢 | FriendRequest | 🟢 |
| | | | | | | Send Message | 🟢 | ~~FriendTyping~~ | 🔴 |
| | | | | | | | | ~~FriendVoiceCall~~ | 🔴 |

Expand Down

0 comments on commit e4e796b

Please sign in to comment.