Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
sisi0318 committed Nov 19, 2024
2 parents c94983a + ad18e88 commit b56175d
Show file tree
Hide file tree
Showing 25 changed files with 689 additions and 121 deletions.
4 changes: 2 additions & 2 deletions Lagrange.Core/BotContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ public class BotContext : IDisposable

private readonly BotKeystore _keystore;

internal BotContext(BotConfig config, BotDeviceInfo deviceInfo, BotKeystore keystore)
internal BotContext(BotConfig config, BotDeviceInfo deviceInfo, BotKeystore keystore, BotAppInfo appInfo)
{
Invoker = new EventInvoker(this);
Scheduler = new Utility.TaskScheduler();

Config = config;
AppInfo = BotAppInfo.ProtocolToAppInfo[config.Protocol];
AppInfo = appInfo;
_deviceInfo = deviceInfo;
_keystore = keystore;

Expand Down
32 changes: 16 additions & 16 deletions Lagrange.Core/Common/BotAppInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,38 @@ namespace Lagrange.Core.Common;

public class BotAppInfo
{
public string Os { get; private set; }
public string Os { get; set; }

public string VendorOs { get; private set; }
public string VendorOs { get; set; }

public string Kernel { get; private set; }
public string Kernel { get; set; }

public string CurrentVersion { get; private set; }
public string CurrentVersion { get; set; }

public int MiscBitmap { get; private set; }
public int MiscBitmap { get; set; }

public string PtVersion { get; private set; }
public string PtVersion { get; set; }

public int SsoVersion { get; private set; }
public int SsoVersion { get; set; }

public string PackageName { get; private set; }
public string PackageName { get; set; }

public string WtLoginSdk { get; private set; }
public string WtLoginSdk { get; set; }

public int AppId { get; private set; }
public int AppId { get; set; }

/// <summary>Or known as pubId in tencent log</summary>
public int SubAppId { get; private set; }
public int SubAppId { get; set; }

public int AppIdQrCode { get; private set; }
public int AppIdQrCode { get; set; }

public ushort AppClientVersion { get; private set; }
public ushort AppClientVersion { get; set; }

public uint MainSigMap { get; private set; }
public uint MainSigMap { get; set; }

public ushort SubSigMap { get; private set; }
public ushort SubSigMap { get; set; }

public ushort NTLoginType { get; private set; }
public ushort NTLoginType { get; set; }

private static readonly BotAppInfo Linux = new()
{
Expand Down
9 changes: 9 additions & 0 deletions Lagrange.Core/Common/Interface/Api/OperationExt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -289,4 +289,13 @@ public static Task<string> UploadImage(this BotContext bot, ImageEntity entity)

public static Task<(int Code, string ErrMsg, List<AiCharacterList>? Result)> GetAiCharacters(this BotContext bot, uint chatType, uint groupUin = 42)
=> bot.ContextCollection.Business.OperationLogic.GetAiCharacters(chatType, groupUin);

public static Task<(int Retcode, string Message, List<uint> FriendUins, List<uint> GroupUins)> GetPins(this BotContext bot)
=> bot.ContextCollection.Business.OperationLogic.GetPins();

public static Task<(int Retcode, string Message)> SetPinFriend(this BotContext bot, uint uin, bool isPin)
=> bot.ContextCollection.Business.OperationLogic.SetPinFriend(uin, isPin);

public static Task<(int Retcode, string Message)> SetPinGroup(this BotContext bot, uint uin, bool isPin)
=> bot.ContextCollection.Business.OperationLogic.SetPinGroup(uin, isPin);
}
27 changes: 25 additions & 2 deletions Lagrange.Core/Common/Interface/BotFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,18 @@ public static class BotFactory
/// <param name="keystore">Existing Keystore from deserialization</param>
/// <returns>Created BotContext Instance</returns>
public static BotContext Create(BotConfig config, BotDeviceInfo deviceInfo, BotKeystore keystore) =>
new(config, deviceInfo, keystore);
new(config, deviceInfo, keystore, BotAppInfo.ProtocolToAppInfo[config.Protocol]);

/// <summary>
/// Create new Bot from existing <see cref="BotKeystore"/> and <see cref="BotDeviceInfo"/> with custom <see cref="BotAppInfo"/>
/// </summary>
/// <param name="config">The config for Bot</param>
/// <param name="deviceInfo">Existing DeviceInfo from deserialization</param>
/// <param name="keystore">Existing Keystore from deserialization</param>
/// <param name="appInfo">Custom BotAppInfo, including client version, app ID, etc</param>
/// <returns></returns>
public static BotContext Create(BotConfig config, BotDeviceInfo deviceInfo, BotKeystore keystore, BotAppInfo appInfo) =>
new(config, deviceInfo, keystore, appInfo);

/// <summary>
/// Create new Bot from Password and uin
Expand All @@ -21,5 +32,17 @@ public static BotContext Create(BotConfig config, BotDeviceInfo deviceInfo, BotK
/// <param name="device">Created device, should be serialized to files for next use</param>
/// <returns>Created BotContext Instance</returns>
public static BotContext Create(BotConfig config, uint uin, string password, out BotDeviceInfo device) =>
new(config, device = BotDeviceInfo.GenerateInfo(), new BotKeystore(uin, password));
new(config, device = BotDeviceInfo.GenerateInfo(), new BotKeystore(uin, password), BotAppInfo.ProtocolToAppInfo[config.Protocol]);

/// <summary>
/// Create new Bot from Password and uin with custom <see cref="BotAppInfo"/>
/// </summary>
/// <param name="config">The config for Bot</param>
/// <param name="uin">Uin, if QrCode login is used, ensure the account that scans QrCode is consistent with this Uin</param>
/// <param name="password">The password of account, for Password Login</param>
/// <param name="appInfo">Custom BotAppInfo, including client version, app ID, etc</param>
/// <param name="device">Created device, should be serialized to files for next use</param>
/// <returns></returns>
public static BotContext Create(BotConfig config, uint uin, string password, BotAppInfo appInfo, out BotDeviceInfo device) =>
new(config, device = BotDeviceInfo.GenerateInfo(), new BotKeystore(uin, password), appInfo);
}
26 changes: 26 additions & 0 deletions Lagrange.Core/Event/EventArg/PinChangedEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
namespace Lagrange.Core.Event.EventArg;

public class PinChangedEvent : EventBase
{
public ChatType Type { get; }

public uint Uin { get; }

public bool IsPin { get; }

public PinChangedEvent(ChatType type, uint uin, bool isPin)
{
Type = type;
Uin = uin;
IsPin = isPin;

EventMessage = $"{nameof(PinChangedEvent)} {{ChatType: {Type} | Uin: {Uin} | IsPin: {IsPin}}}";
}

public enum ChatType
{
Friend,
Group,
Service
}
}
2 changes: 2 additions & 0 deletions Lagrange.Core/Event/EventInvoker.Events.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,6 @@ public partial class EventInvoker
public event LagrangeEvent<GroupTodoEvent>? OnGroupTodoEvent;

public event LagrangeEvent<GroupMemberEnterEvent>? OnGroupMemberEnterEvent;

public event LagrangeEvent<PinChangedEvent>? OnPinChangedEvent;
}
2 changes: 1 addition & 1 deletion Lagrange.Core/Event/EventInvoker.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Runtime.CompilerServices;
using Lagrange.Core.Event.EventArg;
using Lagrange.Core.Internal.Event.Notify;

namespace Lagrange.Core.Event;

Expand Down Expand Up @@ -44,6 +43,7 @@ internal EventInvoker(BotContext context)
RegisterEvent((GroupNameChangeEvent e) => OnGroupNameChangeEvent?.Invoke(context, e));
RegisterEvent((GroupTodoEvent e) => OnGroupTodoEvent?.Invoke(context, e));
RegisterEvent((GroupMemberEnterEvent e) => OnGroupMemberEnterEvent?.Invoke(context, e));
RegisterEvent((PinChangedEvent e) => OnPinChangedEvent?.Invoke(context, e));
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
using Lagrange.Core.Message;
using Lagrange.Core.Message.Entity;
using Lagrange.Core.Message.Filter;
using Lagrange.Core.Utility.Extension;
using FriendPokeEvent = Lagrange.Core.Event.EventArg.FriendPokeEvent;
using GroupPokeEvent = Lagrange.Core.Event.EventArg.GroupPokeEvent;

Expand Down Expand Up @@ -42,6 +41,9 @@ namespace Lagrange.Core.Internal.Context.Logic.Implementation;
[EventSubscribe(typeof(LoginNotifyEvent))]
[EventSubscribe(typeof(MultiMsgDownloadEvent))]
[EventSubscribe(typeof(GroupSysTodoEvent))]
[EventSubscribe(typeof(SysPinChangedEvent))]
[EventSubscribe(typeof(FetchPinsEvent))]
[EventSubscribe(typeof(SetPinFriendEvent))]
[BusinessLogic("MessagingLogic", "Manage the receiving and sending of messages and notifications")]
internal class MessagingLogic : LogicBase
{
Expand Down Expand Up @@ -253,6 +255,26 @@ public override async Task Incoming(ProtocolEvent e)
Collection.Invoker.PostEvent(new GroupTodoEvent(todo.GroupUin, uin));
break;
}
case SysPinChangedEvent pin:
{
uint uin = pin.GroupUin ?? await Collection.Business.CachingLogic.ResolveUin(null, pin.Uid) ?? 0;

Collection.Invoker.PostEvent(new PinChangedEvent(
pin.GroupUin == null ? PinChangedEvent.ChatType.Friend : PinChangedEvent.ChatType.Group,
uin,
pin.IsPin
));
break;
}
case FetchPinsEvent pins:
{
foreach (var friendUid in pins.FriendUids)
{
pins.FriendUins.Add(await Collection.Business.CachingLogic.ResolveUin(null, friendUid) ?? 0);
}

break;
}
}
}

Expand All @@ -277,6 +299,13 @@ public override async Task Outgoing(ProtocolEvent e)
await Collection.Highway.UploadResources(send.Chain);
break;
}
case SetPinFriendEvent pinFriend: // resolve Uin to Uid
{
pinFriend.Uid = await Collection.Business.CachingLogic.ResolveUid(null, pinFriend.Uin)
?? throw new Exception();

break;
}
}
}

Expand Down Expand Up @@ -398,7 +427,6 @@ private async Task ResolveOutgoingChain(MessageChain chain)
face.SysFaceEntry ??= await cache.GetCachedFaceEntity(face.FaceId);
break;
}

case ForwardEntity forward when forward.TargetUin != 0:
{
var cache = Collection.Business.CachingLogic;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -805,4 +805,46 @@ public async Task<string> UploadImage(ImageEntity image)
var imageUrl = await UploadImage(image);
return await ImageOcr(imageUrl);
}

public async Task<(int Retcode, string Message, List<uint> FriendUins, List<uint> GroupUins)> GetPins()
{
var @event = FetchPinsEvent.Create();

var results = await Collection.Business.SendEvent(@event);
if (results.Count == 0)
{
return (-1, "No Result", new(), new());
}

var result = (FetchPinsEvent)results[0];
return (result.ResultCode, result.Message, result.FriendUins, result.GroupUins);
}

public async Task<(int Retcode, string Message)> SetPinFriend(uint uin, bool isPin)
{
var @event = SetPinFriendEvent.Create(uin, isPin);

var results = await Collection.Business.SendEvent(@event);
if (results.Count == 0)
{
return (-1, "No Result");
}

var result = (SetPinFriendEvent)results[0];
return (result.ResultCode, result.Message);
}

public async Task<(int Retcode, string Message)> SetPinGroup(uint uin, bool isPin)
{
var @event = SetPinGroupEvent.Create(uin, isPin);

var results = await Collection.Business.SendEvent(@event);
if (results.Count == 0)
{
return (-1, "No Result");
}

var result = (SetPinGroupEvent)results[0];
return (result.ResultCode, result.Message);
}
}
42 changes: 42 additions & 0 deletions Lagrange.Core/Internal/Event/Action/FetchPinsEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
namespace Lagrange.Core.Internal.Event.Action;

internal class FetchPinsEvent : ProtocolEvent
{
internal List<string> FriendUids { get; set; }

public List<uint> FriendUins { get; set; }

public List<uint> GroupUins { get; set; }

public string Message { get; set; }

protected FetchPinsEvent() : base(true)
{
FriendUids = new();
FriendUins = new();
GroupUins = new();
Message = string.Empty;
}

protected FetchPinsEvent(List<string> friendUids, List<uint> groupUins) : base(0)
{
FriendUids = friendUids;
FriendUins = new();
GroupUins = groupUins;
Message = string.Empty;
}

protected FetchPinsEvent(int retcode, string message) : base(retcode)
{
FriendUids = new();
FriendUins = new();
GroupUins = new();
Message = string.Empty;
}

public static FetchPinsEvent Create() => new();

public static FetchPinsEvent Result(List<string> friendUids, List<uint> groupUins) => new(friendUids, groupUins);

public static FetchPinsEvent Result(int retcode, string message) => new(retcode, message);
}
31 changes: 31 additions & 0 deletions Lagrange.Core/Internal/Event/Action/SetPinFriendEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
namespace Lagrange.Core.Internal.Event.Action;

internal class SetPinFriendEvent : ProtocolEvent
{
internal string Uid { get; set; }

public uint Uin { get; set; }

public bool IsPin { get; set; }

public string Message { get; set; }

protected SetPinFriendEvent(uint uin, bool isPin) : base(true)
{
Uid = string.Empty;
Uin = uin;
Message = string.Empty;
IsPin = isPin;
}

protected SetPinFriendEvent(int retcode, string message) : base(retcode)
{
Uid = string.Empty;
Uin = 0;
Message = message;
}

public static SetPinFriendEvent Create(uint uin, bool isPin) => new(uin, isPin);

public static SetPinFriendEvent Result(int retcode, string message) => new(retcode, message);
}
27 changes: 27 additions & 0 deletions Lagrange.Core/Internal/Event/Action/SetPinGroupEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
namespace Lagrange.Core.Internal.Event.Action;

internal class SetPinGroupEvent : ProtocolEvent
{
public uint Uin { get; set; }

public bool IsPin { get; set; }

public string Message { get; set; }

protected SetPinGroupEvent(uint uin, bool isPin) : base(true)
{
Uin = uin;
Message = string.Empty;
IsPin = isPin;
}

protected SetPinGroupEvent(int retcode, string message) : base(retcode)
{
Uin = 0;
Message = message;
}

public static SetPinGroupEvent Create(uint uin, bool isPin) => new(uin, isPin);

public static SetPinGroupEvent Result(int retcode, string message) => new(retcode, message);
}
Loading

0 comments on commit b56175d

Please sign in to comment.