Skip to content

Commit

Permalink
完善 get_stranger_info (#631)
Browse files Browse the repository at this point in the history
Co-authored-by: pk5ls20 <[email protected]>
Co-authored-by: DarkRRb <[email protected]>
  • Loading branch information
3 people authored Oct 12, 2024
1 parent efa35d0 commit 1484051
Show file tree
Hide file tree
Showing 10 changed files with 199 additions and 48 deletions.
10 changes: 10 additions & 0 deletions Lagrange.Core/Common/Entity/BotCustomStatus.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Lagrange.Core.Common.Entity;

public class BotStatus
{
public uint StatusId { get; set; }

public uint? FaceId { get; set; }

public string? Msg { get; set; }
}
35 changes: 21 additions & 14 deletions Lagrange.Core/Common/Entity/BotUserInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,53 @@ namespace Lagrange.Core.Common.Entity;
[Serializable]
public class BotUserInfo
{
internal BotUserInfo(uint uin, string nickname, DateTime birthday, string city, string country, string school, uint age, DateTime registerTime, uint gender, string? qid, uint level)
internal BotUserInfo(uint uin, string nickname, string avatar, DateTime birthday, string city, string country, string school, uint age, DateTime registerTime, GenderInfo gender, string? qid, uint level, string sign, BotStatus status)
{
Uin = uin;
Avatar = $"https://q1.qlogo.cn/g?b=qq&nk={Uin}&s=640";
Avatar = avatar;
// Avatar = $"https://q1.qlogo.cn/g?b=qq&nk={Uin}&s=640";
Nickname = nickname;
Birthday = birthday;
City = city;
Country = country;
School = school;
Age = age;
RegisterTime = registerTime;
Gender = (GenderInfo)gender;
Gender = gender;
Qid = qid;
Level = level;
Sign = sign;
Status = status;
}

public uint Uin { get; set; }

public string Avatar { get; set; }

public string Nickname { get; set; }

public DateTime Birthday { get; set; }

public string City { get; set; }

public string Country { get; set; }

public string School { get; set; }

public uint Age { get; set; }

public DateTime RegisterTime { get; set; }

public GenderInfo Gender { get; set; }

public string? Qid { get; set; }

public uint Level { get; set; }

public string Sign { get; set; }

public BotStatus Status { get; set; }

public enum GenderInfo
{
Unset = 0,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using ProtoBuf;

namespace Lagrange.Core.Internal.Packets.Service.Oidb.Generics;

#pragma warning disable CS8618

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

[ProtoMember(2)] public byte[] Value { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@ namespace Lagrange.Core.Internal.Packets.Service.Oidb.Request;

[ProtoContract]
[OidbSvcTrpcTcp(0xfe1, 2)]
internal class OidbSvcTrpcTcp0xFE1_2
internal class OidbSvcTrpcTcp0xFE1_2Uid
{
[ProtoMember(1)] public string? Uid { get; set; }

[ProtoMember(2)] public uint Field2 { get; set; }

[ProtoMember(3)] public List<OidbSvcTrpcTcp0xFE1_2Key>? Keys { get; set; } // can be regarded as constants
}

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

[ProtoMember(2)] public uint Field2 { get; set; }

[ProtoMember(3)] public List<OidbSvcTrpcTcp0xFE1_2Key>? Keys { get; set; } // can be regarded as constants
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,30 @@ internal class OidbSvcTrpcTcp0xFE1_2Response
internal class OidbSvcTrpcTcp0xFE1_2ResponseBody
{
// [ProtoMember(1)] public string Uid { get; set; } = "";

[ProtoMember(2)] public OidbSvcTrpcTcp0xFE1_2ResponseProperty Properties { get; set; }

[ProtoMember(3)] public uint Uin { get; set; }
}

[ProtoContract]
internal class OidbSvcTrpcTcp0xFE1_2ResponseProperty
{
[ProtoMember(1)] public List<OidbTwoNumber> NumberProperties { get; set; }

[ProtoMember(2)] public List<OidbFriendProperty> StringProperties { get; set; }

[ProtoMember(2)] public List<OidbFriendByteProperty> BytesProperties { get; set; }
}

[ProtoContract]
public class CustomStatus
{
[ProtoMember(1)] public uint FaceId { get; set; }

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

[ProtoContract]
public class Avatar
{
[ProtoMember(5)] public string? Url { get; set; }
}
75 changes: 53 additions & 22 deletions Lagrange.Core/Internal/Service/System/FetchUserInfoService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,37 @@
using Lagrange.Core.Utility.Binary;
using Lagrange.Core.Utility.Extension;
using ProtoBuf;
using static Lagrange.Core.Common.Entity.BotUserInfo;

namespace Lagrange.Core.Internal.Service.System;

[EventSubscribe(typeof(FetchUserInfoEvent))]
[Service("OidbSvcTrpcTcp.0xfe1_2")]
internal class FetchUserInfoService : BaseService<FetchUserInfoEvent>
{
private static readonly List<OidbSvcTrpcTcp0xFE1_2Key> _keys = new() {
new() { Key = 20002 }, new() { Key = 27394 }, new() { Key = 20009 }, new() { Key = 20031 }, new() { Key = 101 }, new() { Key = 103 }, new() { Key = 102 }, new() { Key = 20022 }, new() { Key = 20023 }, new() { Key = 20024 }, new() { Key = 24002 }, new() { Key = 27037 }, new() { Key = 27049 }, new() { Key = 20011 }, new() { Key = 20016 }, new() { Key = 20021 }, new() { Key = 20003 }, new() { Key = 20004 }, new() { Key = 20005 }, new() { Key = 20006 }, new() { Key = 20020 }, new() { Key = 20026 }, new() { Key = 24007 }, new() { Key = 104 }, new() { Key = 105 }, new() { Key = 42432 }, new() { Key = 42362 }, new() { Key = 41756 }, new() { Key = 41757 }, new() { Key = 42257 }, new() { Key = 27372 }, new() { Key = 42315 }, new() { Key = 107 }, new() { Key = 45160 }, new() { Key = 45161 }, new() { Key = 27406 }, new() { Key = 62026 }, new() { Key = 20037 }
};

protected override bool Build(FetchUserInfoEvent input, BotKeystore keystore, BotAppInfo appInfo,
BotDeviceInfo device, out Span<byte> output, out List<Memory<byte>>? extraPackets)
{
var keys = new List<uint> { 20002, 27394, 20009, 20031, 101, 103, 102, 20022, 20023, 20024, 24002, 27037, 27049, 20011, 20016, 20021, 20003, 20004, 20005, 20006, 20020, 20026, 24007, 104, 105, 42432, 42362, 41756, 41757, 42257, 27372, 42315, 107, 45160, 45161, 27406, 62026, 20037 };
// 27406 自定义状态文本
// 27372 状态

object packet = input.Uid == null
output = input.Uid == null
? new OidbSvcTrpcTcpBase<OidbSvcTrpcTcp0xFE1_2Uin>(new OidbSvcTrpcTcp0xFE1_2Uin
{
Uin = input.Uin,
Field2 = 0,
Keys = keys.Select(x => new OidbSvcTrpcTcp0xFE1_2Key { Key = x }).ToList()
}, 0xfe1, 2, false, true)
: new OidbSvcTrpcTcpBase<OidbSvcTrpcTcp0xFE1_2>(new OidbSvcTrpcTcp0xFE1_2
Keys = _keys,
}, 0xfe1, 2, false, true).Serialize()
: new OidbSvcTrpcTcpBase<OidbSvcTrpcTcp0xFE1_2Uid>(new OidbSvcTrpcTcp0xFE1_2Uid
{
Uid = input.Uid,
Field2 = 0,
Keys = keys.Select(x => new OidbSvcTrpcTcp0xFE1_2Key { Key = x }).ToList()
}, 0xfe1, 2);

output = packet.Serialize();
Keys = _keys
}, 0xfe1, 2).Serialize();
extraPackets = null;
return true;
}
Expand All @@ -44,21 +48,48 @@ protected override bool Parse(Span<byte> input, BotKeystore keystore, BotAppInfo
out FetchUserInfoEvent output, out List<ProtocolEvent>? extraEvents)
{
var payload = Serializer.Deserialize<OidbSvcTrpcTcpBase<OidbSvcTrpcTcp0xFE1_2Response>>(input);

var str = GetStringProperties(payload.Body.Body.Properties);
var num = GetNumberProperties(payload.Body.Body.Properties);

var birthday = GetBirthday(str[20031]);
var reg = DateTime.UnixEpoch.AddSeconds(num[20026]);
string? qid = str.GetValueOrDefault<uint, string>(27394);
var info = new BotUserInfo(payload.Body.Body.Uin, str[20002], birthday, str[20020], str[20003], str[20021], num[20037], reg, num[20009], qid, num[105]);
var bytesProperties = GetStringProperties(payload.Body.Body.Properties);
var numberProperties = GetNumberProperties(payload.Body.Body.Properties);

var birthday = GetBirthday(Encoding.UTF8.GetString(bytesProperties[20031]));
var reg = DateTime.UnixEpoch.AddSeconds(numberProperties[20026]);

string? qid = Encoding.UTF8.GetString(bytesProperties[27394]);

uint statusId = numberProperties[27372];
uint mask = 268435455 - statusId;
mask = (uint)((int)mask >> 31);
statusId -= 268435456 & mask;

CustomStatus? customs = null;
if (bytesProperties[27406].Length != 0)
{
using var stream = new MemoryStream();
customs = Serializer.Deserialize<CustomStatus>(stream);
}

Avatar avatars;
using (var stream = new MemoryStream(bytesProperties[101]))
{
avatars = Serializer.Deserialize<Avatar>(stream);
}
string? avatarurl = avatars.Url + "0";

string? nickname = Encoding.UTF8.GetString(bytesProperties[20002]);
string? city = Encoding.UTF8.GetString(bytesProperties[20020]);
string? country = Encoding.UTF8.GetString(bytesProperties[20003]);
string? school = Encoding.UTF8.GetString(bytesProperties[20021]);
string? sign = Encoding.UTF8.GetString(bytesProperties[102]);

var info = new BotUserInfo(payload.Body.Body.Uin, nickname, avatarurl, birthday, city, country, school, numberProperties[20037], reg, (GenderInfo)numberProperties[20009], qid, numberProperties[105], sign, new() { StatusId = statusId, FaceId = customs?.FaceId ?? 0, Msg = customs?.Msg });

output = FetchUserInfoEvent.Result(0, info);
extraEvents = null;
return true;
}

private static DateTime GetBirthday(String birthday)
private static DateTime GetBirthday(string birthday)
{
var bin = new BinaryPacket(Encoding.ASCII.GetBytes(birthday));
var year = bin.ReadUshort();
Expand All @@ -70,18 +101,18 @@ private static DateTime GetBirthday(String birthday)
}
return new DateTime(1970, 1, 1);
}
private static Dictionary<uint, string> GetStringProperties(OidbSvcTrpcTcp0xFE1_2ResponseProperty properties)

private static Dictionary<uint, byte[]> GetStringProperties(OidbSvcTrpcTcp0xFE1_2ResponseProperty properties)
{
var result = new Dictionary<uint, string>();
foreach (var property in properties.StringProperties)
var result = new Dictionary<uint, byte[]>();
foreach (var property in properties.BytesProperties)
{
result[property.Code] = property.Value;
}

return result;
}

private static Dictionary<uint, uint> GetNumberProperties(OidbSvcTrpcTcp0xFE1_2ResponseProperty properties)
{
var result = new Dictionary<uint, uint>();
Expand Down
2 changes: 1 addition & 1 deletion Lagrange.Core/Message/MessageBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ public MessageBuilder MarketFace(string faceId, int tabId, string key, string su
return this;
}

public MessageBuilder GeryTip(string greyTip)
public MessageBuilder GreyTip(string greyTip)
{
var greyTipEntity = new GreyTipEntity(greyTip);
_chain.Add(greyTipEntity);
Expand Down
11 changes: 11 additions & 0 deletions Lagrange.OneBot/Core/Entity/OneBotFriendStatus.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Text.Json.Serialization;

[Serializable]
public class OneBotFriendStatus
{
[JsonPropertyName("status_id")] public uint StatusId { get; set; }

[JsonPropertyName("face_id")] public uint? FaceId { get; set; }

[JsonPropertyName("message")] public string? Message { get; set; }
}
10 changes: 9 additions & 1 deletion Lagrange.OneBot/Core/Entity/OneBotStranger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,21 @@ public class OneBotStranger
{
[JsonPropertyName("user_id")] public uint UserId { get; set; }

[JsonPropertyName("avatar")] public string Avatar { get; set; } = string.Empty;

[JsonPropertyName("q_id")] public string? QId { get; set; }

[JsonPropertyName("nickname")] public string NickName { get; set; } = string.Empty;

[JsonPropertyName("sign")] public string Sign { get; set; } = string.Empty;

[JsonPropertyName("sex")] public string Sex { get; set; } = "unknown";

[JsonPropertyName("age")] public uint Age { get; set; }

[JsonPropertyName("level")] public uint Level { get; set; }

[JsonPropertyName("status")] public OneBotFriendStatus Status { get; set; } = new();

[JsonPropertyName("RegisterTime")] public DateTime RegisterTime { get; set; }
}
Loading

0 comments on commit 1484051

Please sign in to comment.